OLD | NEW |
---|---|
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/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/memory/scoped_ptr.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(); // should i call it with the ABORT status code? | |
kinuko
2011/05/20 02:13:24
If we could assume this task wouldn't be aborted e
michaeln
2011/05/20 05:14:23
keeping parity with the fsclient is good, so long
| |
151 } | |
152 | |
153 virtual void RunOnTargetThread() OVERRIDE { | |
154 AddRef(); // balanced in CompletionCallback | |
155 | |
156 string16 origin_id = DatabaseUtil::GetOriginIdentifier(origin_url_); | |
157 OriginInfo info; | |
158 if (!db_tracker_->GetOriginInfo(origin_id, &info)) { | |
159 OnCompletionCallback(net::OK); | |
160 return; | |
161 } | |
162 | |
163 int rv = db_tracker_->DeleteDataForOrigin(origin_id, &completion_callback_); | |
164 if (rv == net::ERR_IO_PENDING) | |
165 return; // we wait for the callback | |
166 | |
167 OnCompletionCallback(rv); | |
168 } | |
169 | |
170 void OnCompletionCallback(int rv) { | |
171 is_complete_ = true; | |
172 if (rv == net::OK) | |
173 result_ = quota::kQuotaStatusOk; | |
174 original_message_loop()->PostTask( | |
175 FROM_HERE, NewRunnableMethod(this, &DeleteOriginTask::Completed)); | |
176 Release(); | |
177 } | |
178 | |
179 const GURL origin_url_; | |
180 quota::QuotaStatusCode result_; | |
181 bool is_complete_; | |
182 scoped_ptr<DeletionCallback> caller_callback_; | |
183 net::CompletionCallbackImpl<DeleteOriginTask> completion_callback_; | |
184 }; | |
185 | |
122 // DatabaseQuotaClient -------------------------------------------------------- | 186 // DatabaseQuotaClient -------------------------------------------------------- |
123 | 187 |
124 DatabaseQuotaClient::DatabaseQuotaClient( | 188 DatabaseQuotaClient::DatabaseQuotaClient( |
125 base::MessageLoopProxy* db_tracker_thread, | 189 base::MessageLoopProxy* db_tracker_thread, |
126 DatabaseTracker* db_tracker) | 190 DatabaseTracker* db_tracker) |
127 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { | 191 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { |
128 } | 192 } |
129 | 193 |
130 DatabaseQuotaClient::~DatabaseQuotaClient() { | 194 DatabaseQuotaClient::~DatabaseQuotaClient() { |
131 } | 195 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 | 259 |
196 if (origins_for_host_callbacks_.Add(host, callback.release())) { | 260 if (origins_for_host_callbacks_.Add(host, callback.release())) { |
197 scoped_refptr<GetOriginsForHostTask> task( | 261 scoped_refptr<GetOriginsForHostTask> task( |
198 new GetOriginsForHostTask(this, db_tracker_thread_, host)); | 262 new GetOriginsForHostTask(this, db_tracker_thread_, host)); |
199 task->Start(); | 263 task->Start(); |
200 } | 264 } |
201 } | 265 } |
202 | 266 |
203 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, | 267 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, |
204 quota::StorageType type, | 268 quota::StorageType type, |
205 DeletionCallback* callback) { | 269 DeletionCallback* callback_ptr) { |
206 // TODO(tzik): implement me | 270 DCHECK(callback_ptr); |
207 callback->Run(quota::kQuotaErrorNotSupported); | 271 DCHECK(db_tracker_.get()); |
208 delete callback; | 272 scoped_ptr<DeletionCallback> callback(callback_ptr); |
273 | |
274 // All databases are in the temp namespace for now. | |
275 if (type != quota::kStorageTypeTemporary) { | |
276 callback->Run(quota::kQuotaStatusOk); | |
tzik
2011/05/20 03:19:54
maybe callback->Run(quota::kQuotaErrorNotSupported
michaeln
2011/05/20 05:14:23
There's no way for the caller to know not to call
tzik
2011/05/20 13:26:50
I see. I totally agree with you.
| |
277 return; | |
278 } | |
279 | |
280 scoped_refptr<DeleteOriginTask> task( | |
281 new DeleteOriginTask(this, db_tracker_thread_, | |
282 origin, callback.release())); | |
283 task->Start(); | |
209 } | 284 } |
210 | 285 |
211 void DatabaseQuotaClient::DidGetOriginUsage( | 286 void DatabaseQuotaClient::DidGetOriginUsage( |
212 const GURL& origin_url, int64 usage) { | 287 const GURL& origin_url, int64 usage) { |
213 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); | 288 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); |
214 usage_for_origin_callbacks_.Run(origin_url, usage); | 289 usage_for_origin_callbacks_.Run(origin_url, usage); |
215 } | 290 } |
216 | 291 |
217 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { | 292 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { |
218 DCHECK(origins_for_type_callbacks_.HasCallbacks()); | 293 DCHECK(origins_for_type_callbacks_.HasCallbacks()); |
219 origins_for_type_callbacks_.Run(origins); | 294 origins_for_type_callbacks_.Run(origins); |
220 } | 295 } |
221 | 296 |
222 void DatabaseQuotaClient::DidGetOriginsForHost( | 297 void DatabaseQuotaClient::DidGetOriginsForHost( |
223 const std::string& host, const std::set<GURL>& origins) { | 298 const std::string& host, const std::set<GURL>& origins) { |
224 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); | 299 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); |
225 origins_for_host_callbacks_.Run(host, origins); | 300 origins_for_host_callbacks_.Run(host, origins); |
226 } | 301 } |
227 | 302 |
228 } // namespace webkit_database | 303 } // namespace webkit_database |
OLD | NEW |