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

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: 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 GetOriginUsageOnTargetThread(
kinuko 2012/08/14 13:42:15 nit: maybe TargetThread -> DBThread?
nhiroki 2012/08/15 02:12:53 Done.
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))
kinuko 2012/08/14 13:42:15 nit: indent is off
nhiroki 2012/08/15 02:12:53 Done.
32 client_(client), db_tracker_(client->db_tracker_) { 32 return info.TotalSize();
33 } 33 return 0;
34 }
34 35
35 virtual ~HelperTask() {} 36 void GetOriginsForTypeOnTargetThread(
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 GetOriginsForHostOnTargetThread(
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 base::Callback<void(const std::set<GURL>&,
68 quota::StorageType)>& callback,
kinuko 2012/08/14 13:42:15 nit: if Callback type spans multiple lines having
nhiroki 2012/08/15 02:12:53 Done.
69 std::set<GURL>* origins_ptr,
70 quota::StorageType type) {
71 callback.Run(*origins_ptr, type);
72 }
99 73
100 class DatabaseQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase { 74 void DidDeleteOriginData(
101 public: 75 const base::Callback<void(enum quota::QuotaStatusCode)>& callback,
102 GetAllOriginsTask( 76 int result) {
103 DatabaseQuotaClient* client, 77 if (result == net::OK) {
104 base::MessageLoopProxy* db_tracker_thread, 78 callback.Run(quota::kQuotaStatusOk);
105 quota::StorageType type) 79 } else if (result == net::ERR_IO_PENDING) {
106 : GetOriginsTaskBase(client, db_tracker_thread), 80 // The callback will be invoked via
107 type_(type) { 81 // DatabaseTracker::ScheduleDatabasesForDeletion.
82 return;
83 } else {
84 callback.Run(quota::kQuotaStatusUnknown);
108 } 85 }
86 }
109 87
110 protected: 88 } // 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 89
208 DatabaseQuotaClient::DatabaseQuotaClient( 90 DatabaseQuotaClient::DatabaseQuotaClient(
209 base::MessageLoopProxy* db_tracker_thread, 91 base::MessageLoopProxy* db_tracker_thread,
210 DatabaseTracker* db_tracker) 92 DatabaseTracker* db_tracker)
211 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { 93 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) {
212 } 94 }
213 95
214 DatabaseQuotaClient::~DatabaseQuotaClient() { 96 DatabaseQuotaClient::~DatabaseQuotaClient() {
215 } 97 }
216 98
(...skipping 11 matching lines...) Expand all
228 const GetUsageCallback& callback) { 110 const GetUsageCallback& callback) {
229 DCHECK(!callback.is_null()); 111 DCHECK(!callback.is_null());
230 DCHECK(db_tracker_.get()); 112 DCHECK(db_tracker_.get());
231 113
232 // All databases are in the temp namespace for now. 114 // All databases are in the temp namespace for now.
233 if (type != quota::kStorageTypeTemporary) { 115 if (type != quota::kStorageTypeTemporary) {
234 callback.Run(0); 116 callback.Run(0);
235 return; 117 return;
236 } 118 }
237 119
238 if (usage_for_origin_callbacks_.Add(origin_url, callback)) { 120 base::PostTaskAndReplyWithResult(
239 scoped_refptr<GetOriginUsageTask> task( 121 db_tracker_thread_,
240 new GetOriginUsageTask(this, db_tracker_thread_, origin_url)); 122 FROM_HERE,
241 task->Start(); 123 base::Bind(&GetOriginUsageOnTargetThread,
242 } 124 db_tracker_,
125 origin_url),
126 callback);
243 } 127 }
244 128
245 void DatabaseQuotaClient::GetOriginsForType( 129 void DatabaseQuotaClient::GetOriginsForType(
246 quota::StorageType type, 130 quota::StorageType type,
247 const GetOriginsCallback& callback) { 131 const GetOriginsCallback& callback) {
248 DCHECK(!callback.is_null()); 132 DCHECK(!callback.is_null());
249 DCHECK(db_tracker_.get()); 133 DCHECK(db_tracker_.get());
250 134
251 // All databases are in the temp namespace for now. 135 // All databases are in the temp namespace for now.
252 if (type != quota::kStorageTypeTemporary) { 136 if (type != quota::kStorageTypeTemporary) {
253 callback.Run(std::set<GURL>(), type); 137 callback.Run(std::set<GURL>(), type);
254 return; 138 return;
255 } 139 }
256 140
257 if (origins_for_type_callbacks_.Add(callback)) { 141 std::set<GURL>* origins_ptr = new std::set<GURL>();
258 scoped_refptr<GetAllOriginsTask> task( 142 db_tracker_thread_->PostTaskAndReply(
259 new GetAllOriginsTask(this, db_tracker_thread_, type)); 143 FROM_HERE,
260 task->Start(); 144 base::Bind(&GetOriginsForTypeOnTargetThread,
261 } 145 db_tracker_,
146 base::Unretained(origins_ptr)),
147 base::Bind(&DidGetOrigins,
148 callback,
149 base::Owned(origins_ptr),
150 type));
262 } 151 }
263 152
264 void DatabaseQuotaClient::GetOriginsForHost( 153 void DatabaseQuotaClient::GetOriginsForHost(
265 quota::StorageType type, 154 quota::StorageType type,
266 const std::string& host, 155 const std::string& host,
267 const GetOriginsCallback& callback) { 156 const GetOriginsCallback& callback) {
268 DCHECK(!callback.is_null()); 157 DCHECK(!callback.is_null());
269 DCHECK(db_tracker_.get()); 158 DCHECK(db_tracker_.get());
270 159
271 // All databases are in the temp namespace for now. 160 // All databases are in the temp namespace for now.
272 if (type != quota::kStorageTypeTemporary) { 161 if (type != quota::kStorageTypeTemporary) {
273 callback.Run(std::set<GURL>(), type); 162 callback.Run(std::set<GURL>(), type);
274 return; 163 return;
275 } 164 }
276 165
277 if (origins_for_host_callbacks_.Add(host, callback)) { 166 std::set<GURL>* origins_ptr = new std::set<GURL>();
278 scoped_refptr<GetOriginsForHostTask> task( 167 db_tracker_thread_->PostTaskAndReply(
279 new GetOriginsForHostTask(this, db_tracker_thread_, host, type)); 168 FROM_HERE,
280 task->Start(); 169 base::Bind(&GetOriginsForHostOnTargetThread,
281 } 170 db_tracker_,
171 base::Unretained(origins_ptr),
172 host),
173 base::Bind(&DidGetOrigins,
174 callback,
175 base::Owned(origins_ptr),
176 type));
282 } 177 }
283 178
284 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, 179 void DatabaseQuotaClient::DeleteOriginData(
285 quota::StorageType type, 180 const GURL& origin,
286 const DeletionCallback& callback) { 181 quota::StorageType type,
182 const DeletionCallback& callback) {
287 DCHECK(!callback.is_null()); 183 DCHECK(!callback.is_null());
288 DCHECK(db_tracker_.get()); 184 DCHECK(db_tracker_.get());
289 185
290 // All databases are in the temp namespace for now, so nothing to delete. 186 // All databases are in the temp namespace for now, so nothing to delete.
291 if (type != quota::kStorageTypeTemporary) { 187 if (type != quota::kStorageTypeTemporary) {
292 callback.Run(quota::kQuotaStatusOk); 188 callback.Run(quota::kQuotaStatusOk);
293 return; 189 return;
294 } 190 }
295 191
296 scoped_refptr<DeleteOriginTask> task( 192 PostTaskAndReplyWithResult(
297 new DeleteOriginTask(this, db_tracker_thread_, 193 db_tracker_thread_,
298 origin, callback)); 194 FROM_HERE,
299 task->Start(); 195 base::Bind(&DatabaseTracker::DeleteDataForOrigin,
300 } 196 db_tracker_,
301 197 DatabaseUtil::GetOriginIdentifier(origin),
302 void DatabaseQuotaClient::DidGetOriginUsage( 198 base::Bind(&DidDeleteOriginData, callback)),
303 const GURL& origin_url, int64 usage) { 199 base::Bind(&DidDeleteOriginData, callback));
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 } 200 }
320 201
321 } // namespace webkit_database 202 } // 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