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

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: Clean up 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 } 33 return 0;
34 }
34 35
35 virtual ~HelperTask() {} 36 void GetOriginsForTypeOnDBThread(
36 37 DatabaseTracker* db_tracker,
37 DatabaseQuotaClient* client_; 38 std::set<GURL>* origins_ptr) {
38 scoped_refptr<DatabaseTracker> db_tracker_; 39 std::vector<string16> origin_identifiers;
39 }; 40 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) {
40 41 for (std::vector<string16>::const_iterator iter =
41 class DatabaseQuotaClient::GetOriginUsageTask : public HelperTask { 42 origin_identifiers.begin();
42 public: 43 iter != origin_identifiers.end(); ++iter) {
43 GetOriginUsageTask( 44 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
44 DatabaseQuotaClient* client, 45 origins_ptr->insert(origin);
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 } 46 }
61 } 47 }
48 }
62 49
63 virtual void Completed() OVERRIDE { 50 void GetOriginsForHostOnDBThread(
64 client_->DidGetOriginUsage(origin_url_, usage_); 51 DatabaseTracker* db_tracker,
65 } 52 std::set<GURL>* origins_ptr,
66 53 const std::string& host) {
67 private: 54 std::vector<string16> origin_identifiers;
68 GURL origin_url_; 55 if (db_tracker->GetAllOriginIdentifiers(&origin_identifiers)) {
69 int64 usage_; 56 for (std::vector<string16>::const_iterator iter =
70 }; 57 origin_identifiers.begin();
71 58 iter != origin_identifiers.end(); ++iter) {
72 class DatabaseQuotaClient::GetOriginsTaskBase : public HelperTask { 59 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
73 protected: 60 if (host == net::GetHostOrSpecFromURL(origin))
74 GetOriginsTaskBase( 61 origins_ptr->insert(origin);
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 } 62 }
95 } 63 }
64 }
96 65
97 std::set<GURL> origins_; 66 void DidGetOrigins(
98 }; 67 const QuotaClient::GetOriginsCallback& callback,
68 std::set<GURL>* origins_ptr,
69 quota::StorageType type) {
70 callback.Run(*origins_ptr, type);
71 }
99 72
100 class DatabaseQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase { 73 void DidDeleteOriginData(
101 public: 74 const QuotaClient::DeletionCallback& callback,
102 GetAllOriginsTask( 75 int result) {
103 DatabaseQuotaClient* client, 76 if (result == net::OK) {
104 base::MessageLoopProxy* db_tracker_thread, 77 callback.Run(quota::kQuotaStatusOk);
105 quota::StorageType type) 78 } else if (result == net::ERR_IO_PENDING) {
106 : GetOriginsTaskBase(client, db_tracker_thread), 79 // The callback will be invoked via
107 type_(type) { 80 // DatabaseTracker::ScheduleDatabasesForDeletion.
81 return;
82 } else {
83 callback.Run(quota::kQuotaStatusUnknown);
108 } 84 }
85 }
109 86
110 protected: 87 } // namespace
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 88
208 DatabaseQuotaClient::DatabaseQuotaClient( 89 DatabaseQuotaClient::DatabaseQuotaClient(
209 base::MessageLoopProxy* db_tracker_thread, 90 base::MessageLoopProxy* db_tracker_thread,
210 DatabaseTracker* db_tracker) 91 DatabaseTracker* db_tracker)
211 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { 92 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) {
212 } 93 }
213 94
214 DatabaseQuotaClient::~DatabaseQuotaClient() { 95 DatabaseQuotaClient::~DatabaseQuotaClient() {
215 } 96 }
216 97
(...skipping 11 matching lines...) Expand all
228 const GetUsageCallback& callback) { 109 const GetUsageCallback& callback) {
229 DCHECK(!callback.is_null()); 110 DCHECK(!callback.is_null());
230 DCHECK(db_tracker_.get()); 111 DCHECK(db_tracker_.get());
231 112
232 // All databases are in the temp namespace for now. 113 // All databases are in the temp namespace for now.
233 if (type != quota::kStorageTypeTemporary) { 114 if (type != quota::kStorageTypeTemporary) {
234 callback.Run(0); 115 callback.Run(0);
235 return; 116 return;
236 } 117 }
237 118
238 if (usage_for_origin_callbacks_.Add(origin_url, callback)) { 119 base::PostTaskAndReplyWithResult(
239 scoped_refptr<GetOriginUsageTask> task( 120 db_tracker_thread_,
240 new GetOriginUsageTask(this, db_tracker_thread_, origin_url)); 121 FROM_HERE,
241 task->Start(); 122 base::Bind(&GetOriginUsageOnDBThread,
242 } 123 db_tracker_,
124 origin_url),
125 callback);
243 } 126 }
244 127
245 void DatabaseQuotaClient::GetOriginsForType( 128 void DatabaseQuotaClient::GetOriginsForType(
246 quota::StorageType type, 129 quota::StorageType type,
247 const GetOriginsCallback& callback) { 130 const GetOriginsCallback& callback) {
248 DCHECK(!callback.is_null()); 131 DCHECK(!callback.is_null());
249 DCHECK(db_tracker_.get()); 132 DCHECK(db_tracker_.get());
250 133
251 // All databases are in the temp namespace for now. 134 // All databases are in the temp namespace for now.
252 if (type != quota::kStorageTypeTemporary) { 135 if (type != quota::kStorageTypeTemporary) {
253 callback.Run(std::set<GURL>(), type); 136 callback.Run(std::set<GURL>(), type);
254 return; 137 return;
255 } 138 }
256 139
257 if (origins_for_type_callbacks_.Add(callback)) { 140 std::set<GURL>* origins_ptr = new std::set<GURL>();
258 scoped_refptr<GetAllOriginsTask> task( 141 db_tracker_thread_->PostTaskAndReply(
259 new GetAllOriginsTask(this, db_tracker_thread_, type)); 142 FROM_HERE,
260 task->Start(); 143 base::Bind(&GetOriginsForTypeOnDBThread,
michaeln 2012/08/16 21:39:48 naming nit: Maybe call the helper method GetAllOri
nhiroki 2012/08/20 09:32:23 Done.
261 } 144 db_tracker_,
145 base::Unretained(origins_ptr)),
146 base::Bind(&DidGetOrigins,
147 callback,
148 base::Owned(origins_ptr),
149 type));
262 } 150 }
263 151
264 void DatabaseQuotaClient::GetOriginsForHost( 152 void DatabaseQuotaClient::GetOriginsForHost(
265 quota::StorageType type, 153 quota::StorageType type,
266 const std::string& host, 154 const std::string& host,
267 const GetOriginsCallback& callback) { 155 const GetOriginsCallback& callback) {
268 DCHECK(!callback.is_null()); 156 DCHECK(!callback.is_null());
269 DCHECK(db_tracker_.get()); 157 DCHECK(db_tracker_.get());
270 158
271 // All databases are in the temp namespace for now. 159 // All databases are in the temp namespace for now.
272 if (type != quota::kStorageTypeTemporary) { 160 if (type != quota::kStorageTypeTemporary) {
273 callback.Run(std::set<GURL>(), type); 161 callback.Run(std::set<GURL>(), type);
274 return; 162 return;
275 } 163 }
276 164
277 if (origins_for_host_callbacks_.Add(host, callback)) { 165 std::set<GURL>* origins_ptr = new std::set<GURL>();
278 scoped_refptr<GetOriginsForHostTask> task( 166 db_tracker_thread_->PostTaskAndReply(
279 new GetOriginsForHostTask(this, db_tracker_thread_, host, type)); 167 FROM_HERE,
280 task->Start(); 168 base::Bind(&GetOriginsForHostOnDBThread,
281 } 169 db_tracker_,
170 base::Unretained(origins_ptr),
171 host),
172 base::Bind(&DidGetOrigins,
173 callback,
174 base::Owned(origins_ptr),
175 type));
282 } 176 }
283 177
284 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, 178 void DatabaseQuotaClient::DeleteOriginData(
285 quota::StorageType type, 179 const GURL& origin,
286 const DeletionCallback& callback) { 180 quota::StorageType type,
181 const DeletionCallback& callback) {
287 DCHECK(!callback.is_null()); 182 DCHECK(!callback.is_null());
288 DCHECK(db_tracker_.get()); 183 DCHECK(db_tracker_.get());
289 184
290 // All databases are in the temp namespace for now, so nothing to delete. 185 // All databases are in the temp namespace for now, so nothing to delete.
291 if (type != quota::kStorageTypeTemporary) { 186 if (type != quota::kStorageTypeTemporary) {
292 callback.Run(quota::kQuotaStatusOk); 187 callback.Run(quota::kQuotaStatusOk);
293 return; 188 return;
294 } 189 }
295 190
296 scoped_refptr<DeleteOriginTask> task( 191 PostTaskAndReplyWithResult(
297 new DeleteOriginTask(this, db_tracker_thread_, 192 db_tracker_thread_,
298 origin, callback)); 193 FROM_HERE,
299 task->Start(); 194 base::Bind(&DatabaseTracker::DeleteDataForOrigin,
300 } 195 db_tracker_,
301 196 DatabaseUtil::GetOriginIdentifier(origin),
302 void DatabaseQuotaClient::DidGetOriginUsage( 197 base::Bind(&DidDeleteOriginData, callback)),
303 const GURL& origin_url, int64 usage) { 198 base::Bind(&DidDeleteOriginData, callback));
michaeln 2012/08/16 21:39:48 Not sure this PostTaskAndReplyWithResult usage is
michaeln 2012/08/16 22:16:34 I mixed up the async vs immediate cases in the pre
kinuko 2012/08/20 06:41:38 Thanks for catching this! In the original code we
nhiroki 2012/08/20 09:32:23 As kinuko@ mentioned, I modified to pass the origi
304 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url));
305 usage_for_origin_callbacks_.Run(origin_url, usage);
306 }
307
308 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins,
309 quota::StorageType type) {
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 } 199 }
320 200
321 } // namespace webkit_database 201 } // 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