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

Side by Side Diff: webkit/fileapi/file_system_quota_client.cc

Issue 10828177: Refactors FileSystemQuotaClient using base::Callback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 8 years, 4 months 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
« no previous file with comments | « webkit/fileapi/file_system_quota_client.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "webkit/fileapi/file_system_quota_client.h" 5 #include "webkit/fileapi/file_system_quota_client.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 9
10 #include "base/bind.h"
10 #include "base/file_path.h" 11 #include "base/file_path.h"
11 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/location.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/sequenced_task_runner.h"
14 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/task_runner_util.h"
15 #include "googleurl/src/gurl.h" 19 #include "googleurl/src/gurl.h"
16 #include "net/base/net_util.h" 20 #include "net/base/net_util.h"
17 #include "webkit/fileapi/file_system_context.h" 21 #include "webkit/fileapi/file_system_context.h"
18 #include "webkit/fileapi/file_system_quota_util.h" 22 #include "webkit/fileapi/file_system_quota_util.h"
19 #include "webkit/fileapi/file_system_task_runners.h" 23 #include "webkit/fileapi/file_system_task_runners.h"
20 #include "webkit/fileapi/file_system_usage_cache.h" 24 #include "webkit/fileapi/file_system_usage_cache.h"
21 #include "webkit/fileapi/file_system_util.h" 25 #include "webkit/fileapi/file_system_util.h"
22 #include "webkit/fileapi/sandbox_mount_point_provider.h" 26 #include "webkit/fileapi/sandbox_mount_point_provider.h"
23 27
24 using base::SequencedTaskRunner;
25 using quota::QuotaThreadTask;
26 using quota::StorageType; 28 using quota::StorageType;
27 29
28 namespace fileapi { 30 namespace fileapi {
29 31
30 class FileSystemQuotaClient::GetOriginUsageTask : public QuotaThreadTask { 32 namespace {
31 public:
32 GetOriginUsageTask(
33 FileSystemQuotaClient* quota_client,
34 const GURL& origin_url,
35 FileSystemType type)
36 : QuotaThreadTask(quota_client, quota_client->file_task_runner()),
37 quota_client_(quota_client),
38 origin_url_(origin_url),
39 type_(type),
40 fs_usage_(0) {
41 DCHECK(quota_client_);
42 file_system_context_ = quota_client_->file_system_context_;
43 }
44 33
45 protected: 34 void GetOriginsForTypeOnFileThread(FileSystemContext* context,
46 virtual ~GetOriginUsageTask() {} 35 StorageType storage_type,
36 std::set<GURL>* origins_ptr) {
37 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
38 DCHECK(type != kFileSystemTypeUnknown);
47 39
48 // QuotaThreadTask: 40 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type);
49 virtual void RunOnTargetThread() OVERRIDE { 41 if (!quota_util)
50 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); 42 return;
51 if (quota_util) 43 quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr);
52 fs_usage_ = quota_util->GetOriginUsageOnFileThread( 44 }
53 file_system_context_, origin_url_, type_);
54 }
55 45
56 virtual void Completed() OVERRIDE { 46 void GetOriginsForHostOnFileThread(FileSystemContext* context,
57 quota_client_->DidGetOriginUsage(type_, origin_url_, fs_usage_); 47 StorageType storage_type,
58 } 48 const std::string& host,
49 std::set<GURL>* origins_ptr) {
50 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
51 DCHECK(type != kFileSystemTypeUnknown);
59 52
60 FileSystemQuotaClient* quota_client_; 53 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type);
61 scoped_refptr<FileSystemContext> file_system_context_; 54 if (!quota_util)
62 GURL origin_url_; 55 return;
63 FileSystemType type_; 56 quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr);
64 int64 fs_usage_; 57 }
65 };
66 58
67 class FileSystemQuotaClient::GetOriginsForTypeTask : public QuotaThreadTask { 59 void DidGetOrigins(
68 public: 60 const base::Callback<void(const std::set<GURL>&, StorageType)>& callback,
69 GetOriginsForTypeTask( 61 std::set<GURL>* origins_ptr,
70 FileSystemQuotaClient* quota_client, 62 StorageType storage_type) {
71 FileSystemType type) 63 callback.Run(*origins_ptr, storage_type);
72 : QuotaThreadTask(quota_client, quota_client->file_task_runner()), 64 }
73 quota_client_(quota_client),
74 type_(type) {
75 DCHECK(quota_client_);
76 file_system_context_ = quota_client_->file_system_context_;
77 }
78 65
79 protected: 66 quota::QuotaStatusCode DeleteOriginOnTargetThread(
80 virtual ~GetOriginsForTypeTask() {} 67 FileSystemContext* context,
68 const GURL& origin,
69 FileSystemType type) {
70 base::PlatformFileError result =
71 context->sandbox_provider()->DeleteOriginDataOnFileThread(
72 context, context->quota_manager_proxy(), origin, type);
73 if (result == base::PLATFORM_FILE_OK)
74 return quota::kQuotaStatusOk;
75 return quota::kQuotaErrorInvalidModification;
76 }
81 77
82 // QuotaThreadTask: 78 } // namespace
83 virtual void RunOnTargetThread() OVERRIDE {
84 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_);
85 if (quota_util)
86 quota_util->GetOriginsForTypeOnFileThread(type_, &origins_);
87 }
88
89 virtual void Completed() OVERRIDE {
90 quota_client_->DidGetOriginsForType(type_, origins_);
91 }
92
93 private:
94 FileSystemQuotaClient* quota_client_;
95 scoped_refptr<FileSystemContext> file_system_context_;
96 std::set<GURL> origins_;
97 FileSystemType type_;
98 };
99
100 class FileSystemQuotaClient::GetOriginsForHostTask : public QuotaThreadTask {
101 public:
102 GetOriginsForHostTask(
103 FileSystemQuotaClient* quota_client,
104 FileSystemType type,
105 const std::string& host)
106 : QuotaThreadTask(quota_client, quota_client->file_task_runner()),
107 quota_client_(quota_client),
108 type_(type),
109 host_(host) {
110 DCHECK(quota_client_);
111 file_system_context_ = quota_client_->file_system_context_;
112 }
113
114 protected:
115 virtual ~GetOriginsForHostTask() {}
116
117 // QuotaThreadTask:
118 virtual void RunOnTargetThread() OVERRIDE {
119 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_);
120 if (quota_util)
121 quota_util->GetOriginsForHostOnFileThread(type_, host_, &origins_);
122 }
123
124 virtual void Completed() OVERRIDE {
125 quota_client_->DidGetOriginsForHost(std::make_pair(type_, host_), origins_);
126 }
127
128 private:
129 FileSystemQuotaClient* quota_client_;
130 scoped_refptr<FileSystemContext> file_system_context_;
131 std::set<GURL> origins_;
132 FileSystemType type_;
133 std::string host_;
134 };
135
136 class FileSystemQuotaClient::DeleteOriginTask
137 : public QuotaThreadTask {
138 public:
139 DeleteOriginTask(
140 FileSystemQuotaClient* quota_client,
141 const GURL& origin,
142 FileSystemType type,
143 const DeletionCallback& callback)
144 : QuotaThreadTask(quota_client, quota_client->file_task_runner()),
145 file_system_context_(quota_client->file_system_context_),
146 origin_(origin),
147 type_(type),
148 status_(quota::kQuotaStatusUnknown),
149 callback_(callback) {
150 }
151
152 protected:
153 virtual ~DeleteOriginTask() {}
154
155 // QuotaThreadTask:
156 virtual void RunOnTargetThread() OVERRIDE {
157 base::PlatformFileError result =
158 file_system_context_->sandbox_provider()->DeleteOriginDataOnFileThread(
159 file_system_context_,
160 file_system_context_->quota_manager_proxy(),
161 origin_,
162 type_);
163 if (result == base::PLATFORM_FILE_OK)
164 status_ = quota::kQuotaStatusOk;
165 else
166 status_ = quota::kQuotaErrorInvalidModification;
167 }
168
169 virtual void Completed() OVERRIDE {
170 callback_.Run(status_);
171 }
172
173 private:
174 FileSystemContext* file_system_context_;
175 GURL origin_;
176 FileSystemType type_;
177 quota::QuotaStatusCode status_;
178 DeletionCallback callback_;
179 };
180 79
181 FileSystemQuotaClient::FileSystemQuotaClient( 80 FileSystemQuotaClient::FileSystemQuotaClient(
182 FileSystemContext* file_system_context, 81 FileSystemContext* file_system_context,
183 bool is_incognito) 82 bool is_incognito)
184 : file_system_context_(file_system_context), 83 : file_system_context_(file_system_context),
185 is_incognito_(is_incognito) { 84 is_incognito_(is_incognito) {
186 } 85 }
187 86
188 FileSystemQuotaClient::~FileSystemQuotaClient() {} 87 FileSystemQuotaClient::~FileSystemQuotaClient() {}
189 88
(...skipping 13 matching lines...) Expand all
203 102
204 if (is_incognito_) { 103 if (is_incognito_) {
205 // We don't support FileSystem in incognito mode yet. 104 // We don't support FileSystem in incognito mode yet.
206 callback.Run(0); 105 callback.Run(0);
207 return; 106 return;
208 } 107 }
209 108
210 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 109 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
211 DCHECK(type != kFileSystemTypeUnknown); 110 DCHECK(type != kFileSystemTypeUnknown);
212 111
213 if (pending_usage_callbacks_.Add( 112 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type);
214 std::make_pair(type, origin_url.spec()), callback)) { 113 if (!quota_util) {
215 scoped_refptr<GetOriginUsageTask> task( 114 callback.Run(0);
216 new GetOriginUsageTask(this, origin_url, type)); 115 return;
217 task->Start();
218 } 116 }
117
118 base::PostTaskAndReplyWithResult(
119 file_task_runner(),
120 FROM_HERE,
121 // It is safe to pass Unretained(quota_util) since context owns it.
122 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileThread,
123 base::Unretained(quota_util),
124 file_system_context_,
125 origin_url,
126 type),
127 callback);
219 } 128 }
220 129
221 void FileSystemQuotaClient::GetOriginsForType( 130 void FileSystemQuotaClient::GetOriginsForType(
222 StorageType storage_type, 131 StorageType storage_type,
223 const GetOriginsCallback& callback) { 132 const GetOriginsCallback& callback) {
224 DCHECK(!callback.is_null()); 133 DCHECK(!callback.is_null());
225 134
226 std::set<GURL> origins;
227 if (is_incognito_) { 135 if (is_incognito_) {
228 // We don't support FileSystem in incognito mode yet. 136 // We don't support FileSystem in incognito mode yet.
137 std::set<GURL> origins;
229 callback.Run(origins, storage_type); 138 callback.Run(origins, storage_type);
230 return; 139 return;
231 } 140 }
232 141
233 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 142 std::set<GURL>* origins_ptr = new std::set<GURL>();
234 DCHECK(type != kFileSystemTypeUnknown); 143 file_task_runner()->PostTaskAndReply(
235 144 FROM_HERE,
236 if (pending_origins_for_type_callbacks_.Add(type, callback)) { 145 base::Bind(&GetOriginsForTypeOnFileThread,
237 scoped_refptr<GetOriginsForTypeTask> task( 146 file_system_context_,
238 new GetOriginsForTypeTask(this, type)); 147 storage_type,
239 task->Start(); 148 base::Unretained(origins_ptr)),
240 } 149 base::Bind(&DidGetOrigins,
150 callback,
151 base::Owned(origins_ptr),
152 storage_type));
241 } 153 }
242 154
243 void FileSystemQuotaClient::GetOriginsForHost( 155 void FileSystemQuotaClient::GetOriginsForHost(
244 StorageType storage_type, 156 StorageType storage_type,
245 const std::string& host, 157 const std::string& host,
246 const GetOriginsCallback& callback) { 158 const GetOriginsCallback& callback) {
247 DCHECK(!callback.is_null()); 159 DCHECK(!callback.is_null());
248 160
249 std::set<GURL> origins;
250 if (is_incognito_) { 161 if (is_incognito_) {
251 // We don't support FileSystem in incognito mode yet. 162 // We don't support FileSystem in incognito mode yet.
163 std::set<GURL> origins;
252 callback.Run(origins, storage_type); 164 callback.Run(origins, storage_type);
253 return; 165 return;
254 } 166 }
255 167
256 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 168 std::set<GURL>* origins_ptr = new std::set<GURL>();
257 DCHECK(type != kFileSystemTypeUnknown); 169 file_task_runner()->PostTaskAndReply(
258 170 FROM_HERE,
259 if (pending_origins_for_host_callbacks_.Add( 171 base::Bind(&GetOriginsForHostOnFileThread,
260 std::make_pair(type, host), callback)) { 172 file_system_context_,
261 scoped_refptr<GetOriginsForHostTask> task( 173 storage_type,
262 new GetOriginsForHostTask(this, type, host)); 174 host,
263 task->Start(); 175 base::Unretained(origins_ptr)),
264 } 176 base::Bind(&DidGetOrigins,
177 callback,
178 base::Owned(origins_ptr),
179 storage_type));
265 } 180 }
266 181
267 void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, 182 void FileSystemQuotaClient::DeleteOriginData(const GURL& origin,
268 StorageType type, 183 StorageType type,
269 const DeletionCallback& callback) { 184 const DeletionCallback& callback) {
270 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); 185 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type);
271 DCHECK(fs_type != kFileSystemTypeUnknown); 186 DCHECK(fs_type != kFileSystemTypeUnknown);
272 scoped_refptr<DeleteOriginTask> task(
273 new DeleteOriginTask(this, origin, fs_type, callback));
274 task->Start();
275 }
276 187
277 void FileSystemQuotaClient::DidGetOriginUsage( 188 base::PostTaskAndReplyWithResult(
278 FileSystemType type, const GURL& origin_url, int64 usage) { 189 file_task_runner(),
279 TypeAndHostOrOrigin type_and_origin(std::make_pair( 190 FROM_HERE,
280 type, origin_url.spec())); 191 base::Bind(&DeleteOriginOnTargetThread,
281 DCHECK(pending_usage_callbacks_.HasCallbacks(type_and_origin)); 192 file_system_context_,
282 pending_usage_callbacks_.Run(type_and_origin, usage); 193 origin,
283 } 194 fs_type),
284 195 callback);
285 void FileSystemQuotaClient::DidGetOriginsForType(
286 FileSystemType type, const std::set<GURL>& origins) {
287 DCHECK(pending_origins_for_type_callbacks_.HasCallbacks(type));
288 pending_origins_for_type_callbacks_.Run(type, origins,
289 FileSystemTypeToQuotaStorageType(type));
290 }
291
292 void FileSystemQuotaClient::DidGetOriginsForHost(
293 const TypeAndHostOrOrigin& type_and_host, const std::set<GURL>& origins) {
294 DCHECK(pending_origins_for_host_callbacks_.HasCallbacks(type_and_host));
295 pending_origins_for_host_callbacks_.Run(type_and_host, origins,
296 FileSystemTypeToQuotaStorageType(type_and_host.first));
297 } 196 }
298 197
299 base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { 198 base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const {
300 return file_system_context_->task_runners()->file_task_runner(); 199 return file_system_context_->task_runners()->file_task_runner();
301 } 200 }
302 201
303 } // namespace fileapi 202 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_quota_client.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698