Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: webkit/browser/fileapi/quota/quota_backend_impl.cc

Issue 61593002: Quota: Implement QuotaBackendImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix build and test Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "webkit/browser/fileapi/quota/quota_backend_impl.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/logging.h"
12 #include "base/sequenced_task_runner.h"
13 #include "webkit/browser/fileapi/file_system_usage_cache.h"
14 #include "webkit/browser/quota/quota_client.h"
15 #include "webkit/browser/quota/quota_manager.h"
16 #include "webkit/common/fileapi/file_system_util.h"
17
18 namespace fileapi {
19
20 QuotaBackendImpl::QuotaBackendImpl(
21 base::SequencedTaskRunner* file_task_runner,
22 ObfuscatedFileUtil* obfuscated_file_util,
23 FileSystemUsageCache* file_system_usage_cache,
24 quota::QuotaManagerProxy* quota_manager_proxy)
25 : file_task_runner_(file_task_runner),
26 obfuscated_file_util_(obfuscated_file_util),
27 file_system_usage_cache_(file_system_usage_cache),
28 quota_manager_proxy_(quota_manager_proxy),
29 weak_ptr_factory_(this) {
30 }
31
32 QuotaBackendImpl::~QuotaBackendImpl() {
33 }
34
35 void QuotaBackendImpl::ReserveQuota(const GURL& origin,
36 FileSystemType type,
37 int64 delta,
38 const ReserveQuotaCallback& callback) {
39 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
40 if (!delta) {
41 callback.Run(base::PLATFORM_FILE_OK);
42 return;
43 }
44 DCHECK(quota_manager_proxy_);
45 quota_manager_proxy_->GetUsageAndQuota(
46 file_task_runner_, origin, FileSystemTypeToQuotaStorageType(type),
47 base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota,
48 weak_ptr_factory_.GetWeakPtr(),
49 QuotaReservationInfo(origin, type, delta), callback));
50 }
51
52 void QuotaBackendImpl::ReleaseReservedQuota(const GURL& origin,
53 FileSystemType type,
54 int64 size) {
55 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
56 DCHECK_LE(0, size);
57 if (!size)
58 return;
59 ReserveQuotaInternal(QuotaReservationInfo(origin, type, -size));
60 }
61
62 void QuotaBackendImpl::CommitQuotaUsage(const GURL& origin,
63 FileSystemType type,
64 int64 delta,
65 const StatusCallback& callback) {
66 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
67 if (!delta) {
68 callback.Run(base::PLATFORM_FILE_OK);
69 return;
70 }
71 DCHECK(quota_manager_proxy_);
72 quota_manager_proxy_->GetUsageAndQuota(
73 file_task_runner_, origin, FileSystemTypeToQuotaStorageType(type),
74 base::Bind(&QuotaBackendImpl::DidGetUsageAndQuotaForCommitQuotaUsage,
75 weak_ptr_factory_.GetWeakPtr(),
76 QuotaReservationInfo(origin, type, delta), callback));
77 }
78
79 void QuotaBackendImpl::IncrementDirtyCount(const GURL& origin,
80 FileSystemType type) {
81 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
82 base::FilePath path;
83 if (GetUsageCachePath(origin, type, &path) != base::PLATFORM_FILE_OK)
84 return;
85 DCHECK(file_system_usage_cache_);
86 file_system_usage_cache_->IncrementDirty(path);
87 }
88
89 void QuotaBackendImpl::DecrementDirtyCount(const GURL& origin,
90 FileSystemType type) {
91 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
92 base::FilePath path;
93 if (GetUsageCachePath(origin, type, &path) != base::PLATFORM_FILE_OK)
94 return;
95 DCHECK(file_system_usage_cache_);
96 file_system_usage_cache_->DecrementDirty(path);
97 }
98
99 void QuotaBackendImpl::DidGetUsageAndQuotaForReserveQuota(
100 const QuotaReservationInfo& info,
101 const ReserveQuotaCallback& callback,
102 quota::QuotaStatusCode status, int64 usage, int64 quota) {
103 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
104 if (status != quota::kQuotaStatusOk) {
105 callback.Run(base::PLATFORM_FILE_ERROR_FAILED);
106 return;
107 }
108
109 if (quota < usage + info.delta) {
110 callback.Run(base::PLATFORM_FILE_ERROR_NO_SPACE);
111 return;
112 }
113
114 ReserveQuotaInternal(info);
115 if (callback.Run(base::PLATFORM_FILE_OK))
116 return;
117 // The requester could not accept the reserved quota. Revert it.
118 ReserveQuotaInternal(
119 QuotaReservationInfo(info.origin, info.type, -info.delta));
120 }
121
122 void QuotaBackendImpl::DidGetUsageAndQuotaForCommitQuotaUsage(
123 const QuotaReservationInfo& info,
124 const StatusCallback& callback,
125 quota::QuotaStatusCode status, int64 usage, int64 quota) {
126 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
127 if (status != quota::kQuotaStatusOk) {
128 callback.Run(base::PLATFORM_FILE_ERROR_FAILED);
129 return;
130 }
131
132 if (quota < usage + info.delta) {
133 callback.Run(base::PLATFORM_FILE_ERROR_NO_SPACE);
kinuko 2013/11/07 10:10:09 I don't think querying first and returning here re
nhiroki 2013/11/08 00:12:05 Removed the querying part.
134 return;
135 }
136
137 ReserveQuotaInternal(info);
138 base::PlatformFileError error = CommitQuotaUsageInternal(info);
139 if (error != base::PLATFORM_FILE_OK) {
140 // Failed to update usage. Revert it.
141 ReserveQuotaInternal(
142 QuotaReservationInfo(info.origin, info.type, -info.delta));
143 }
144 callback.Run(error);
145 }
146
147 void QuotaBackendImpl::ReserveQuotaInternal(const QuotaReservationInfo& info) {
148 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
149 DCHECK(quota_manager_proxy_);
150 quota_manager_proxy_->NotifyStorageModified(
151 quota::QuotaClient::kFileSystem, info.origin,
152 FileSystemTypeToQuotaStorageType(info.type), info.delta);
153 }
154
155 base::PlatformFileError QuotaBackendImpl::CommitQuotaUsageInternal(
156 const QuotaReservationInfo& info) {
157 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
158 base::FilePath path;
159 base::PlatformFileError error =
160 GetUsageCachePath(info.origin, info.type, &path);
161 if (error != base::PLATFORM_FILE_OK)
162 return error;
163 bool result =
164 file_system_usage_cache_->AtomicUpdateUsageByDelta(path, info.delta);
165 return result ? base::PLATFORM_FILE_OK
166 : base::PLATFORM_FILE_ERROR_FAILED;
kinuko 2013/11/07 10:10:09 The quota reservation code doesn't seem to handle
tzik 2013/11/07 11:45:15 Ah right, it ignores the result. CommitQuotaUsage
nhiroki 2013/11/08 00:12:05 Okay, removed them. That looks simpler :)
167 }
168
169 base::PlatformFileError QuotaBackendImpl::GetUsageCachePath(
170 const GURL& origin,
171 FileSystemType type,
172 base::FilePath* usage_file_path) {
173 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
174 DCHECK(usage_file_path);
175 base::PlatformFileError error = base::PLATFORM_FILE_OK;
176 *usage_file_path =
177 SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType(
178 obfuscated_file_util_, origin, type, &error);
179 return error;
180 }
181
182 QuotaBackendImpl::QuotaReservationInfo::QuotaReservationInfo(
183 const GURL& origin, FileSystemType type, int64 delta)
184 : origin(origin), type(type), delta(delta) {
185 }
186
187 QuotaBackendImpl::QuotaReservationInfo::~QuotaReservationInfo() {
188 }
189
190 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698