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

Side by Side Diff: webkit/database/database_quota_client.cc

Issue 10831305: Refactors DatabaseQuotaClient using base::Callback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Runs the callback on the original thread. 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/database/database_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/database/database_quota_client.h" 5 #include "webkit/database/database_quota_client.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop_proxy.h" 13 #include "base/message_loop_proxy.h"
14 #include "net/base/completion_callback.h" 14 #include "base/task_runner_util.h"
15 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
16 #include "net/base/net_util.h" 16 #include "net/base/net_util.h"
17 #include "webkit/database/database_tracker.h" 17 #include "webkit/database/database_tracker.h"
18 #include "webkit/database/database_util.h" 18 #include "webkit/database/database_util.h"
19 19
20 using quota::QuotaClient; 20 using quota::QuotaClient;
21 21
22 namespace webkit_database { 22 namespace webkit_database {
23 23
24 // Helper tasks --------------------------------------------------------------- 24 namespace {
25 25
26 class DatabaseQuotaClient::HelperTask : public quota::QuotaThreadTask { 26 int64 GetOriginUsageOnDBThread(
27 protected: 27 DatabaseTracker* db_tracker,
28 HelperTask( 28 const GURL& origin_url) {
29 DatabaseQuotaClient* client, 29 OriginInfo info;
30 base::MessageLoopProxy* db_tracker_thread) 30 if (db_tracker->GetOriginInfo(
31 : QuotaThreadTask(client, db_tracker_thread), 31 DatabaseUtil::GetOriginIdentifier(origin_url), &info))
32 client_(client), db_tracker_(client->db_tracker_) { 32 return info.TotalSize();
33 return 0;
34 }
35
36 void GetOriginsOnDBThread(
37 DatabaseTracker* db_tracker,
38 std::set<GURL>* origins_ptr) {
39 std::vector<string16> origin_identifiers;
40 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) {
41 for (std::vector<string16>::const_iterator iter =
42 origin_identifiers.begin();
43 iter != origin_identifiers.end(); ++iter) {
44 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
45 origins_ptr->insert(origin);
46 }
47 }
48 }
49
50 void GetOriginsForHostOnDBThread(
51 DatabaseTracker* db_tracker,
52 std::set<GURL>* origins_ptr,
53 const std::string& host) {
54 std::vector<string16> origin_identifiers;
55 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) {
56 for (std::vector<string16>::const_iterator iter =
57 origin_identifiers.begin();
58 iter != origin_identifiers.end(); ++iter) {
59 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
60 if (host == net::GetHostOrSpecFromURL(origin))
61 origins_ptr->insert(origin);
62 }
63 }
64 }
65
66 void DidGetOrigins(
67 const QuotaClient::GetOriginsCallback& callback,
68 std::set<GURL>* origins_ptr,
69 quota::StorageType type) {
70 callback.Run(*origins_ptr, type);
71 }
72
73 void DidDeleteOriginData(
74 base::SingleThreadTaskRunner* original_task_runner,
75 const QuotaClient::DeletionCallback& callback,
76 int result) {
77 if (result == net::ERR_IO_PENDING) {
78 // The callback will be invoked via
79 // DatabaseTracker::ScheduleDatabasesForDeletion.
80 return;
33 } 81 }
34 82
35 virtual ~HelperTask() {} 83 quota::QuotaStatusCode status;
84 if (result == net::OK)
85 status = quota::kQuotaStatusOk;
86 else
87 status = quota::kQuotaStatusUnknown;
36 88
37 DatabaseQuotaClient* client_; 89 if (original_task_runner->BelongsToCurrentThread())
38 scoped_refptr<DatabaseTracker> db_tracker_; 90 callback.Run(status);
39 }; 91 else
92 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, status));
93 }
40 94
41 class DatabaseQuotaClient::GetOriginUsageTask : public HelperTask { 95 } // namespace
42 public:
43 GetOriginUsageTask(
44 DatabaseQuotaClient* client,
45 base::MessageLoopProxy* db_tracker_thread,
46 const GURL& origin_url)
47 : HelperTask(client, db_tracker_thread),
48 origin_url_(origin_url), usage_(0) {
49 }
50
51 protected:
52 virtual ~GetOriginUsageTask() {}
53
54 virtual void RunOnTargetThread() OVERRIDE {
55 OriginInfo info;
56 if (db_tracker_->GetOriginInfo(
57 DatabaseUtil::GetOriginIdentifier(origin_url_),
58 &info)) {
59 usage_ = info.TotalSize();
60 }
61 }
62
63 virtual void Completed() OVERRIDE {
64 client_->DidGetOriginUsage(origin_url_, usage_);
65 }
66
67 private:
68 GURL origin_url_;
69 int64 usage_;
70 };
71
72 class DatabaseQuotaClient::GetOriginsTaskBase : public HelperTask {
73 protected:
74 GetOriginsTaskBase(
75 DatabaseQuotaClient* client,
76 base::MessageLoopProxy* db_tracker_thread)
77 : HelperTask(client, db_tracker_thread) {
78 }
79
80 virtual ~GetOriginsTaskBase() {}
81
82 virtual bool ShouldAddOrigin(const GURL& origin) = 0;
83
84 virtual void RunOnTargetThread() OVERRIDE {
85 std::vector<string16> origin_identifiers;
86 if (db_tracker_->GetAllOriginIdentifiers(&origin_identifiers)) {
87 for (std::vector<string16>::const_iterator iter =
88 origin_identifiers.begin();
89 iter != origin_identifiers.end(); ++iter) {
90 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
91 if (ShouldAddOrigin(origin))
92 origins_.insert(origin);
93 }
94 }
95 }
96
97 std::set<GURL> origins_;
98 };
99
100 class DatabaseQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase {
101 public:
102 GetAllOriginsTask(
103 DatabaseQuotaClient* client,
104 base::MessageLoopProxy* db_tracker_thread,
105 quota::StorageType type)
106 : GetOriginsTaskBase(client, db_tracker_thread),
107 type_(type) {
108 }
109
110 protected:
111 virtual ~GetAllOriginsTask() {}
112
113 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE {
114 return true;
115 }
116 virtual void Completed() OVERRIDE {
117 client_->DidGetAllOrigins(origins_, type_);
118 }
119
120 private:
121 quota::StorageType type_;
122 };
123
124 class DatabaseQuotaClient::GetOriginsForHostTask : public GetOriginsTaskBase {
125 public:
126 GetOriginsForHostTask(
127 DatabaseQuotaClient* client,
128 base::MessageLoopProxy* db_tracker_thread,
129 const std::string& host,
130 quota::StorageType type)
131 : GetOriginsTaskBase(client, db_tracker_thread),
132 host_(host),
133 type_(type) {
134 }
135
136 protected:
137 virtual ~GetOriginsForHostTask() {}
138
139 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE {
140 return host_ == net::GetHostOrSpecFromURL(origin);
141 }
142
143 virtual void Completed() OVERRIDE {
144 client_->DidGetOriginsForHost(host_, origins_, type_);
145 }
146
147 private:
148 std::string host_;
149 quota::StorageType type_;
150 };
151
152 class DatabaseQuotaClient::DeleteOriginTask : public HelperTask {
153 public:
154 DeleteOriginTask(
155 DatabaseQuotaClient* client,
156 base::MessageLoopProxy* db_tracker_thread,
157 const GURL& origin_url,
158 const DeletionCallback& caller_callback)
159 : HelperTask(client, db_tracker_thread),
160 origin_url_(origin_url),
161 result_(quota::kQuotaStatusUnknown),
162 caller_callback_(caller_callback) {
163 }
164
165 protected:
166 virtual ~DeleteOriginTask() {}
167
168 virtual void Completed() OVERRIDE {
169 if (caller_callback_.is_null())
170 return;
171 caller_callback_.Run(result_);
172 caller_callback_.Reset();
173 }
174
175 virtual void Aborted() OVERRIDE {
176 caller_callback_.Reset();
177 }
178
179 virtual bool RunOnTargetThreadAsync() OVERRIDE {
180 AddRef(); // balanced in OnCompletionCallback
181 string16 origin_id = DatabaseUtil::GetOriginIdentifier(origin_url_);
182 int rv = db_tracker_->DeleteDataForOrigin(
183 origin_id, base::Bind(&DeleteOriginTask::OnCompletionCallback,
184 base::Unretained(this)));
185 if (rv == net::ERR_IO_PENDING)
186 return false; // we wait for the callback
187 OnCompletionCallback(rv);
188 return false;
189 }
190
191 private:
192 void OnCompletionCallback(int rv) {
193 if (rv == net::OK)
194 result_ = quota::kQuotaStatusOk;
195 original_task_runner()->PostTask(
196 FROM_HERE, base::Bind(&DeleteOriginTask::CallCompleted, this));
197 Release(); // balanced in RunOnTargetThreadAsync
198 }
199
200 const GURL origin_url_;
201 quota::QuotaStatusCode result_;
202 DeletionCallback caller_callback_;
203 net::CompletionCallback completion_callback_;
204 };
205
206 // DatabaseQuotaClient --------------------------------------------------------
207 96
208 DatabaseQuotaClient::DatabaseQuotaClient( 97 DatabaseQuotaClient::DatabaseQuotaClient(
209 base::MessageLoopProxy* db_tracker_thread, 98 base::MessageLoopProxy* db_tracker_thread,
210 DatabaseTracker* db_tracker) 99 DatabaseTracker* db_tracker)
211 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { 100 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) {
212 } 101 }
213 102
214 DatabaseQuotaClient::~DatabaseQuotaClient() { 103 DatabaseQuotaClient::~DatabaseQuotaClient() {
215 } 104 }
216 105
(...skipping 11 matching lines...) Expand all
228 const GetUsageCallback& callback) { 117 const GetUsageCallback& callback) {
229 DCHECK(!callback.is_null()); 118 DCHECK(!callback.is_null());
230 DCHECK(db_tracker_.get()); 119 DCHECK(db_tracker_.get());
231 120
232 // All databases are in the temp namespace for now. 121 // All databases are in the temp namespace for now.
233 if (type != quota::kStorageTypeTemporary) { 122 if (type != quota::kStorageTypeTemporary) {
234 callback.Run(0); 123 callback.Run(0);
235 return; 124 return;
236 } 125 }
237 126
238 if (usage_for_origin_callbacks_.Add(origin_url, callback)) { 127 base::PostTaskAndReplyWithResult(
239 scoped_refptr<GetOriginUsageTask> task( 128 db_tracker_thread_,
240 new GetOriginUsageTask(this, db_tracker_thread_, origin_url)); 129 FROM_HERE,
241 task->Start(); 130 base::Bind(&GetOriginUsageOnDBThread,
242 } 131 db_tracker_,
132 origin_url),
133 callback);
243 } 134 }
244 135
245 void DatabaseQuotaClient::GetOriginsForType( 136 void DatabaseQuotaClient::GetOriginsForType(
246 quota::StorageType type, 137 quota::StorageType type,
247 const GetOriginsCallback& callback) { 138 const GetOriginsCallback& callback) {
248 DCHECK(!callback.is_null()); 139 DCHECK(!callback.is_null());
249 DCHECK(db_tracker_.get()); 140 DCHECK(db_tracker_.get());
250 141
251 // All databases are in the temp namespace for now. 142 // All databases are in the temp namespace for now.
252 if (type != quota::kStorageTypeTemporary) { 143 if (type != quota::kStorageTypeTemporary) {
253 callback.Run(std::set<GURL>(), type); 144 callback.Run(std::set<GURL>(), type);
254 return; 145 return;
255 } 146 }
256 147
257 if (origins_for_type_callbacks_.Add(callback)) { 148 std::set<GURL>* origins_ptr = new std::set<GURL>();
258 scoped_refptr<GetAllOriginsTask> task( 149 db_tracker_thread_->PostTaskAndReply(
259 new GetAllOriginsTask(this, db_tracker_thread_, type)); 150 FROM_HERE,
260 task->Start(); 151 base::Bind(&GetOriginsOnDBThread,
261 } 152 db_tracker_,
153 base::Unretained(origins_ptr)),
154 base::Bind(&DidGetOrigins,
155 callback,
156 base::Owned(origins_ptr),
157 type));
262 } 158 }
263 159
264 void DatabaseQuotaClient::GetOriginsForHost( 160 void DatabaseQuotaClient::GetOriginsForHost(
265 quota::StorageType type, 161 quota::StorageType type,
266 const std::string& host, 162 const std::string& host,
267 const GetOriginsCallback& callback) { 163 const GetOriginsCallback& callback) {
268 DCHECK(!callback.is_null()); 164 DCHECK(!callback.is_null());
269 DCHECK(db_tracker_.get()); 165 DCHECK(db_tracker_.get());
270 166
271 // All databases are in the temp namespace for now. 167 // All databases are in the temp namespace for now.
272 if (type != quota::kStorageTypeTemporary) { 168 if (type != quota::kStorageTypeTemporary) {
273 callback.Run(std::set<GURL>(), type); 169 callback.Run(std::set<GURL>(), type);
274 return; 170 return;
275 } 171 }
276 172
277 if (origins_for_host_callbacks_.Add(host, callback)) { 173 std::set<GURL>* origins_ptr = new std::set<GURL>();
278 scoped_refptr<GetOriginsForHostTask> task( 174 db_tracker_thread_->PostTaskAndReply(
279 new GetOriginsForHostTask(this, db_tracker_thread_, host, type)); 175 FROM_HERE,
280 task->Start(); 176 base::Bind(&GetOriginsForHostOnDBThread,
281 } 177 db_tracker_,
178 base::Unretained(origins_ptr),
179 host),
180 base::Bind(&DidGetOrigins,
181 callback,
182 base::Owned(origins_ptr),
183 type));
282 } 184 }
283 185
284 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, 186 void DatabaseQuotaClient::DeleteOriginData(
285 quota::StorageType type, 187 const GURL& origin,
286 const DeletionCallback& callback) { 188 quota::StorageType type,
189 const DeletionCallback& callback) {
287 DCHECK(!callback.is_null()); 190 DCHECK(!callback.is_null());
288 DCHECK(db_tracker_.get()); 191 DCHECK(db_tracker_.get());
289 192
290 // All databases are in the temp namespace for now, so nothing to delete. 193 // All databases are in the temp namespace for now, so nothing to delete.
291 if (type != quota::kStorageTypeTemporary) { 194 if (type != quota::kStorageTypeTemporary) {
292 callback.Run(quota::kQuotaStatusOk); 195 callback.Run(quota::kQuotaStatusOk);
293 return; 196 return;
294 } 197 }
295 198
296 scoped_refptr<DeleteOriginTask> task( 199 base::Callback<void(int)> delete_callback =
297 new DeleteOriginTask(this, db_tracker_thread_, 200 base::Bind(&DidDeleteOriginData,
298 origin, callback)); 201 base::MessageLoopProxy::current(),
299 task->Start(); 202 callback);
300 }
301 203
302 void DatabaseQuotaClient::DidGetOriginUsage( 204 PostTaskAndReplyWithResult(
303 const GURL& origin_url, int64 usage) { 205 db_tracker_thread_,
304 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); 206 FROM_HERE,
305 usage_for_origin_callbacks_.Run(origin_url, usage); 207 base::Bind(&DatabaseTracker::DeleteDataForOrigin,
306 } 208 db_tracker_,
307 209 DatabaseUtil::GetOriginIdentifier(origin),
308 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins, 210 delete_callback),
309 quota::StorageType type) { 211 delete_callback);
310 DCHECK(origins_for_type_callbacks_.HasCallbacks());
311 origins_for_type_callbacks_.Run(origins, type);
312 }
313
314 void DatabaseQuotaClient::DidGetOriginsForHost(
315 const std::string& host, const std::set<GURL>& origins,
316 quota::StorageType type) {
317 DCHECK(origins_for_host_callbacks_.HasCallbacks(host));
318 origins_for_host_callbacks_.Run(host, origins, type);
319 } 212 }
320 213
321 } // namespace webkit_database 214 } // namespace webkit_database
OLDNEW
« no previous file with comments | « webkit/database/database_quota_client.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698