summaryrefslogtreecommitdiffstats
path: root/chromium/webkit/browser/fileapi/quota/quota_backend_impl.cc
blob: de0b3ac0369ce9c842c899f086e82f354a1f3ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/browser/fileapi/quota/quota_backend_impl.h"

#include <string>

#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/sequenced_task_runner.h"
#include "webkit/browser/fileapi/file_system_usage_cache.h"
#include "webkit/browser/quota/quota_client.h"
#include "webkit/browser/quota/quota_manager_proxy.h"
#include "webkit/common/fileapi/file_system_util.h"

namespace fileapi {

QuotaBackendImpl::QuotaBackendImpl(
    base::SequencedTaskRunner* file_task_runner,
    ObfuscatedFileUtil* obfuscated_file_util,
    FileSystemUsageCache* file_system_usage_cache,
    quota::QuotaManagerProxy* quota_manager_proxy)
    : file_task_runner_(file_task_runner),
      obfuscated_file_util_(obfuscated_file_util),
      file_system_usage_cache_(file_system_usage_cache),
      quota_manager_proxy_(quota_manager_proxy),
      weak_ptr_factory_(this) {
}

QuotaBackendImpl::~QuotaBackendImpl() {
}

void QuotaBackendImpl::ReserveQuota(const GURL& origin,
                                    FileSystemType type,
                                    int64 delta,
                                    const ReserveQuotaCallback& callback) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(origin.is_valid());
  if (!delta) {
    callback.Run(base::File::FILE_OK, 0);
    return;
  }
  DCHECK(quota_manager_proxy_);
  quota_manager_proxy_->GetUsageAndQuota(
      file_task_runner_, origin, FileSystemTypeToQuotaStorageType(type),
      base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota,
                 weak_ptr_factory_.GetWeakPtr(),
                 QuotaReservationInfo(origin, type, delta), callback));
}

void QuotaBackendImpl::ReleaseReservedQuota(const GURL& origin,
                                            FileSystemType type,
                                            int64 size) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(origin.is_valid());
  DCHECK_LE(0, size);
  if (!size)
    return;
  ReserveQuotaInternal(QuotaReservationInfo(origin, type, -size));
}

void QuotaBackendImpl::CommitQuotaUsage(const GURL& origin,
                                        FileSystemType type,
                                        int64 delta) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(origin.is_valid());
  if (!delta)
    return;
  ReserveQuotaInternal(QuotaReservationInfo(origin, type, delta));
  base::FilePath path;
  if (GetUsageCachePath(origin, type, &path) != base::File::FILE_OK)
    return;
  bool result = file_system_usage_cache_->AtomicUpdateUsageByDelta(path, delta);
  DCHECK(result);
}

void QuotaBackendImpl::IncrementDirtyCount(const GURL& origin,
                                           FileSystemType type) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(origin.is_valid());
  base::FilePath path;
  if (GetUsageCachePath(origin, type, &path) != base::File::FILE_OK)
    return;
  DCHECK(file_system_usage_cache_);
  file_system_usage_cache_->IncrementDirty(path);
}

void QuotaBackendImpl::DecrementDirtyCount(const GURL& origin,
                                           FileSystemType type) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(origin.is_valid());
  base::FilePath path;
  if (GetUsageCachePath(origin, type, &path) != base::File::FILE_OK)
    return;
  DCHECK(file_system_usage_cache_);
  file_system_usage_cache_->DecrementDirty(path);
}

void QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota(
    const QuotaReservationInfo& info,
    const ReserveQuotaCallback& callback,
    quota::QuotaStatusCode status, int64 usage, int64 quota) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(info.origin.is_valid());
  DCHECK_LE(0, usage);
  DCHECK_LE(0, quota);
  if (status != quota::kQuotaStatusOk) {
    callback.Run(base::File::FILE_ERROR_FAILED, 0);
    return;
  }

  QuotaReservationInfo normalized_info = info;
  if (info.delta > 0) {
    int64 new_usage =
        base::saturated_cast<int64>(usage + static_cast<uint64>(info.delta));
    if (quota < new_usage)
      new_usage = quota;
    normalized_info.delta = std::max(static_cast<int64>(0), new_usage - usage);
  }

  ReserveQuotaInternal(normalized_info);
  if (callback.Run(base::File::FILE_OK, normalized_info.delta))
    return;
  // The requester could not accept the reserved quota. Revert it.
  ReserveQuotaInternal(
      QuotaReservationInfo(normalized_info.origin,
                           normalized_info.type,
                           -normalized_info.delta));
}

void QuotaBackendImpl::ReserveQuotaInternal(const QuotaReservationInfo& info) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(info.origin.is_valid());
  DCHECK(quota_manager_proxy_);
  quota_manager_proxy_->NotifyStorageModified(
      quota::QuotaClient::kFileSystem, info.origin,
      FileSystemTypeToQuotaStorageType(info.type), info.delta);
}

base::File::Error QuotaBackendImpl::GetUsageCachePath(
    const GURL& origin,
    FileSystemType type,
    base::FilePath* usage_file_path) {
  DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
  DCHECK(origin.is_valid());
  DCHECK(usage_file_path);
  base::File::Error error = base::File::FILE_OK;
  *usage_file_path =
      SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType(
          obfuscated_file_util_, origin, type, &error);
  return error;
}

QuotaBackendImpl::QuotaReservationInfo::QuotaReservationInfo(
    const GURL& origin, FileSystemType type, int64 delta)
    : origin(origin), type(type), delta(delta) {
}

QuotaBackendImpl::QuotaReservationInfo::~QuotaReservationInfo() {
}

}  // namespace fileapi