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

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

Issue 7046019: Implement DatabaseQuotaClient's DeteleteOriginData method. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/memory/scoped_ptr.h"
9 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h"
10 #include "net/base/net_util.h" 13 #include "net/base/net_util.h"
11 #include "webkit/database/database_tracker.h" 14 #include "webkit/database/database_tracker.h"
12 #include "webkit/database/database_util.h" 15 #include "webkit/database/database_util.h"
13 16
14 using quota::QuotaClient; 17 using quota::QuotaClient;
15 18
16 namespace webkit_database { 19 namespace webkit_database {
17 20
18 // Helper tasks --------------------------------------------------------------- 21 // Helper tasks ---------------------------------------------------------------
19 22
20 class DatabaseQuotaClient::HelperTask : public quota::QuotaThreadTask { 23 class DatabaseQuotaClient::HelperTask : public quota::QuotaThreadTask {
21 protected: 24 protected:
22 HelperTask( 25 HelperTask(
23 DatabaseQuotaClient* client, 26 DatabaseQuotaClient* client,
24 scoped_refptr<base::MessageLoopProxy> db_tracker_thread) 27 base::MessageLoopProxy* db_tracker_thread)
25 : QuotaThreadTask(client, db_tracker_thread), 28 : QuotaThreadTask(client, db_tracker_thread),
26 client_(client), db_tracker_(client->db_tracker_) { 29 client_(client), db_tracker_(client->db_tracker_) {
27 } 30 }
28 31
29 DatabaseQuotaClient* client_; 32 DatabaseQuotaClient* client_;
30 scoped_refptr<DatabaseTracker> db_tracker_; 33 scoped_refptr<DatabaseTracker> db_tracker_;
31 }; 34 };
32 35
33 class DatabaseQuotaClient::GetOriginUsageTask : public HelperTask { 36 class DatabaseQuotaClient::GetOriginUsageTask : public HelperTask {
34 public: 37 public:
35 GetOriginUsageTask( 38 GetOriginUsageTask(
36 DatabaseQuotaClient* client, 39 DatabaseQuotaClient* client,
37 scoped_refptr<base::MessageLoopProxy> db_tracker_thread, 40 base::MessageLoopProxy* db_tracker_thread,
38 const GURL& origin_url) 41 const GURL& origin_url)
39 : HelperTask(client, db_tracker_thread), 42 : HelperTask(client, db_tracker_thread),
40 origin_url_(origin_url), usage_(0) { 43 origin_url_(origin_url), usage_(0) {
41 } 44 }
42 45
43 private: 46 private:
44 virtual void RunOnTargetThread() OVERRIDE { 47 virtual void RunOnTargetThread() OVERRIDE {
45 OriginInfo info; 48 OriginInfo info;
46 if (db_tracker_->GetOriginInfo( 49 if (db_tracker_->GetOriginInfo(
47 DatabaseUtil::GetOriginIdentifier(origin_url_), 50 DatabaseUtil::GetOriginIdentifier(origin_url_),
48 &info)) { 51 &info)) {
49 usage_ = info.TotalSize(); 52 usage_ = info.TotalSize();
50 } 53 }
51 } 54 }
52 virtual void Completed() OVERRIDE { 55 virtual void Completed() OVERRIDE {
53 client_->DidGetOriginUsage(origin_url_, usage_); 56 client_->DidGetOriginUsage(origin_url_, usage_);
54 } 57 }
55 GURL origin_url_; 58 GURL origin_url_;
56 int64 usage_; 59 int64 usage_;
57 }; 60 };
58 61
59 class DatabaseQuotaClient::GetOriginsTaskBase : public HelperTask { 62 class DatabaseQuotaClient::GetOriginsTaskBase : public HelperTask {
60 protected: 63 protected:
61 GetOriginsTaskBase( 64 GetOriginsTaskBase(
62 DatabaseQuotaClient* client, 65 DatabaseQuotaClient* client,
63 scoped_refptr<base::MessageLoopProxy> db_tracker_thread) 66 base::MessageLoopProxy* db_tracker_thread)
64 : HelperTask(client, db_tracker_thread) { 67 : HelperTask(client, db_tracker_thread) {
65 } 68 }
66 69
67 virtual bool ShouldAddOrigin(const GURL& origin) = 0; 70 virtual bool ShouldAddOrigin(const GURL& origin) = 0;
68 71
69 virtual void RunOnTargetThread() OVERRIDE { 72 virtual void RunOnTargetThread() OVERRIDE {
70 std::vector<string16> origin_identifiers; 73 std::vector<string16> origin_identifiers;
71 if (db_tracker_->GetAllOriginIdentifiers(&origin_identifiers)) { 74 if (db_tracker_->GetAllOriginIdentifiers(&origin_identifiers)) {
72 for (std::vector<string16>::const_iterator iter = 75 for (std::vector<string16>::const_iterator iter =
73 origin_identifiers.begin(); 76 origin_identifiers.begin();
74 iter != origin_identifiers.end(); ++iter) { 77 iter != origin_identifiers.end(); ++iter) {
75 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter); 78 GURL origin = DatabaseUtil::GetOriginFromIdentifier(*iter);
76 if (ShouldAddOrigin(origin)) 79 if (ShouldAddOrigin(origin))
77 origins_.insert(origin); 80 origins_.insert(origin);
78 } 81 }
79 } 82 }
80 } 83 }
81 84
82 std::set<GURL> origins_; 85 std::set<GURL> origins_;
83 }; 86 };
84 87
85 class DatabaseQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase { 88 class DatabaseQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase {
86 public: 89 public:
87 GetAllOriginsTask( 90 GetAllOriginsTask(
88 DatabaseQuotaClient* client, 91 DatabaseQuotaClient* client,
89 scoped_refptr<base::MessageLoopProxy> db_tracker_thread) 92 base::MessageLoopProxy* db_tracker_thread)
90 : GetOriginsTaskBase(client, db_tracker_thread) { 93 : GetOriginsTaskBase(client, db_tracker_thread) {
91 } 94 }
92 95
93 protected: 96 protected:
94 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE { 97 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE {
95 return true; 98 return true;
96 } 99 }
97 virtual void Completed() OVERRIDE { 100 virtual void Completed() OVERRIDE {
98 client_->DidGetAllOrigins(origins_); 101 client_->DidGetAllOrigins(origins_);
99 } 102 }
100 }; 103 };
101 104
102 class DatabaseQuotaClient::GetOriginsForHostTask : public GetOriginsTaskBase { 105 class DatabaseQuotaClient::GetOriginsForHostTask : public GetOriginsTaskBase {
103 public: 106 public:
104 GetOriginsForHostTask( 107 GetOriginsForHostTask(
105 DatabaseQuotaClient* client, 108 DatabaseQuotaClient* client,
106 scoped_refptr<base::MessageLoopProxy> db_tracker_thread, 109 base::MessageLoopProxy* db_tracker_thread,
107 const std::string& host) 110 const std::string& host)
108 : GetOriginsTaskBase(client, db_tracker_thread), 111 : GetOriginsTaskBase(client, db_tracker_thread),
109 host_(host) { 112 host_(host) {
110 } 113 }
111 114
112 private: 115 private:
113 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE { 116 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE {
114 return host_ == net::GetHostOrSpecFromURL(origin); 117 return host_ == net::GetHostOrSpecFromURL(origin);
115 } 118 }
116 virtual void Completed() OVERRIDE { 119 virtual void Completed() OVERRIDE {
117 client_->DidGetOriginsForHost(host_, origins_); 120 client_->DidGetOriginsForHost(host_, origins_);
118 } 121 }
119 std::string host_; 122 std::string host_;
120 }; 123 };
121 124
125 class DatabaseQuotaClient::DeleteOriginTask : public HelperTask {
126 public:
127 DeleteOriginTask(
128 DatabaseQuotaClient* client,
129 base::MessageLoopProxy* db_tracker_thread,
130 const GURL& origin_url,
131 DeletionCallback* caller_callback)
132 : HelperTask(client, db_tracker_thread),
133 origin_url_(origin_url),
134 result_(quota::kQuotaStatusUnknown),
135 is_complete_(false),
136 caller_callback_(caller_callback),
137 ALLOW_THIS_IN_INITIALIZER_LIST(completion_callback_(
138 this, &DeleteOriginTask::OnCompletionCallback)) {
139 }
140
141 private:
142 virtual void Completed() OVERRIDE {
143 if (!is_complete_ || !caller_callback_.get())
144 return; // not done yet or we were aborted
145 caller_callback_->Run(result_);
146 caller_callback_.reset();
147 }
148
149 virtual void Aborted() OVERRIDE {
150 caller_callback_.reset();
151 }
152
153 virtual void RunOnTargetThread() OVERRIDE {
154 AddRef(); // balanced in OnCompletionCallback
155 string16 origin_id = DatabaseUtil::GetOriginIdentifier(origin_url_);
156 int rv = db_tracker_->DeleteDataForOrigin(origin_id, &completion_callback_);
157 if (rv == net::ERR_IO_PENDING)
158 return; // we wait for the callback
159 OnCompletionCallback(rv);
160 }
161
162 void OnCompletionCallback(int rv) {
163 is_complete_ = true;
164 if (rv == net::OK)
165 result_ = quota::kQuotaStatusOk;
166 original_message_loop()->PostTask(
167 FROM_HERE, NewRunnableMethod(this, &DeleteOriginTask::Completed));
168 Release(); // balanced in RunOnTargetThread
169 }
170
171 const GURL origin_url_;
172 quota::QuotaStatusCode result_;
173 bool is_complete_;
174 scoped_ptr<DeletionCallback> caller_callback_;
175 net::CompletionCallbackImpl<DeleteOriginTask> completion_callback_;
176 };
177
122 // DatabaseQuotaClient -------------------------------------------------------- 178 // DatabaseQuotaClient --------------------------------------------------------
123 179
124 DatabaseQuotaClient::DatabaseQuotaClient( 180 DatabaseQuotaClient::DatabaseQuotaClient(
125 base::MessageLoopProxy* db_tracker_thread, 181 base::MessageLoopProxy* db_tracker_thread,
126 DatabaseTracker* db_tracker) 182 DatabaseTracker* db_tracker)
127 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { 183 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) {
128 } 184 }
129 185
130 DatabaseQuotaClient::~DatabaseQuotaClient() { 186 DatabaseQuotaClient::~DatabaseQuotaClient() {
131 } 187 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 251
196 if (origins_for_host_callbacks_.Add(host, callback.release())) { 252 if (origins_for_host_callbacks_.Add(host, callback.release())) {
197 scoped_refptr<GetOriginsForHostTask> task( 253 scoped_refptr<GetOriginsForHostTask> task(
198 new GetOriginsForHostTask(this, db_tracker_thread_, host)); 254 new GetOriginsForHostTask(this, db_tracker_thread_, host));
199 task->Start(); 255 task->Start();
200 } 256 }
201 } 257 }
202 258
203 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, 259 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin,
204 quota::StorageType type, 260 quota::StorageType type,
205 DeletionCallback* callback) { 261 DeletionCallback* callback_ptr) {
206 // TODO(tzik): implement me 262 DCHECK(callback_ptr);
207 callback->Run(quota::kQuotaErrorNotSupported); 263 DCHECK(db_tracker_.get());
208 delete callback; 264 scoped_ptr<DeletionCallback> callback(callback_ptr);
265
266 // All databases are in the temp namespace for now.
267 if (type != quota::kStorageTypeTemporary) {
268 callback->Run(quota::kQuotaStatusOk);
269 return;
270 }
271
272 scoped_refptr<DeleteOriginTask> task(
273 new DeleteOriginTask(this, db_tracker_thread_,
274 origin, callback.release()));
275 task->Start();
209 } 276 }
210 277
211 void DatabaseQuotaClient::DidGetOriginUsage( 278 void DatabaseQuotaClient::DidGetOriginUsage(
212 const GURL& origin_url, int64 usage) { 279 const GURL& origin_url, int64 usage) {
213 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); 280 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url));
214 usage_for_origin_callbacks_.Run(origin_url, usage); 281 usage_for_origin_callbacks_.Run(origin_url, usage);
215 } 282 }
216 283
217 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { 284 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) {
218 DCHECK(origins_for_type_callbacks_.HasCallbacks()); 285 DCHECK(origins_for_type_callbacks_.HasCallbacks());
219 origins_for_type_callbacks_.Run(origins); 286 origins_for_type_callbacks_.Run(origins);
220 } 287 }
221 288
222 void DatabaseQuotaClient::DidGetOriginsForHost( 289 void DatabaseQuotaClient::DidGetOriginsForHost(
223 const std::string& host, const std::set<GURL>& origins) { 290 const std::string& host, const std::set<GURL>& origins) {
224 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); 291 DCHECK(origins_for_host_callbacks_.HasCallbacks(host));
225 origins_for_host_callbacks_.Run(host, origins); 292 origins_for_host_callbacks_.Run(host, origins);
226 } 293 }
227 294
228 } // namespace webkit_database 295 } // namespace webkit_database
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698