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/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 caller_callback_(caller_callback), |
| 136 ALLOW_THIS_IN_INITIALIZER_LIST(completion_callback_( |
| 137 this, &DeleteOriginTask::OnCompletionCallback)) { |
| 138 } |
| 139 |
| 140 private: |
| 141 virtual void Completed() OVERRIDE { |
| 142 if (!caller_callback_.get()) |
| 143 return; |
| 144 caller_callback_->Run(result_); |
| 145 caller_callback_.reset(); |
| 146 } |
| 147 |
| 148 virtual void Aborted() OVERRIDE { |
| 149 caller_callback_.reset(); |
| 150 } |
| 151 |
| 152 virtual bool RunOnTargetThreadAsync() OVERRIDE { |
| 153 AddRef(); // balanced in OnCompletionCallback |
| 154 string16 origin_id = DatabaseUtil::GetOriginIdentifier(origin_url_); |
| 155 int rv = db_tracker_->DeleteDataForOrigin(origin_id, &completion_callback_); |
| 156 if (rv == net::ERR_IO_PENDING) |
| 157 return false; // we wait for the callback |
| 158 OnCompletionCallback(rv); |
| 159 return false; |
| 160 } |
| 161 |
| 162 void OnCompletionCallback(int rv) { |
| 163 if (rv == net::OK) |
| 164 result_ = quota::kQuotaStatusOk; |
| 165 original_message_loop()->PostTask( |
| 166 FROM_HERE, NewRunnableMethod(this, &DeleteOriginTask::CallCompleted)); |
| 167 Release(); // balanced in RunOnTargetThreadAsync |
| 168 } |
| 169 |
| 170 const GURL origin_url_; |
| 171 quota::QuotaStatusCode result_; |
| 172 scoped_ptr<DeletionCallback> caller_callback_; |
| 173 net::CompletionCallbackImpl<DeleteOriginTask> completion_callback_; |
| 174 }; |
| 175 |
122 // DatabaseQuotaClient -------------------------------------------------------- | 176 // DatabaseQuotaClient -------------------------------------------------------- |
123 | 177 |
124 DatabaseQuotaClient::DatabaseQuotaClient( | 178 DatabaseQuotaClient::DatabaseQuotaClient( |
125 base::MessageLoopProxy* db_tracker_thread, | 179 base::MessageLoopProxy* db_tracker_thread, |
126 DatabaseTracker* db_tracker) | 180 DatabaseTracker* db_tracker) |
127 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { | 181 : db_tracker_thread_(db_tracker_thread), db_tracker_(db_tracker) { |
128 } | 182 } |
129 | 183 |
130 DatabaseQuotaClient::~DatabaseQuotaClient() { | 184 DatabaseQuotaClient::~DatabaseQuotaClient() { |
131 } | 185 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 249 |
196 if (origins_for_host_callbacks_.Add(host, callback.release())) { | 250 if (origins_for_host_callbacks_.Add(host, callback.release())) { |
197 scoped_refptr<GetOriginsForHostTask> task( | 251 scoped_refptr<GetOriginsForHostTask> task( |
198 new GetOriginsForHostTask(this, db_tracker_thread_, host)); | 252 new GetOriginsForHostTask(this, db_tracker_thread_, host)); |
199 task->Start(); | 253 task->Start(); |
200 } | 254 } |
201 } | 255 } |
202 | 256 |
203 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, | 257 void DatabaseQuotaClient::DeleteOriginData(const GURL& origin, |
204 quota::StorageType type, | 258 quota::StorageType type, |
205 DeletionCallback* callback) { | 259 DeletionCallback* callback_ptr) { |
206 // TODO(tzik): implement me | 260 DCHECK(callback_ptr); |
207 callback->Run(quota::kQuotaErrorNotSupported); | 261 DCHECK(db_tracker_.get()); |
208 delete callback; | 262 scoped_ptr<DeletionCallback> callback(callback_ptr); |
| 263 |
| 264 // All databases are in the temp namespace for now, so nothing to delete. |
| 265 if (type != quota::kStorageTypeTemporary) { |
| 266 callback->Run(quota::kQuotaStatusOk); |
| 267 return; |
| 268 } |
| 269 |
| 270 scoped_refptr<DeleteOriginTask> task( |
| 271 new DeleteOriginTask(this, db_tracker_thread_, |
| 272 origin, callback.release())); |
| 273 task->Start(); |
209 } | 274 } |
210 | 275 |
211 void DatabaseQuotaClient::DidGetOriginUsage( | 276 void DatabaseQuotaClient::DidGetOriginUsage( |
212 const GURL& origin_url, int64 usage) { | 277 const GURL& origin_url, int64 usage) { |
213 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); | 278 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); |
214 usage_for_origin_callbacks_.Run(origin_url, usage); | 279 usage_for_origin_callbacks_.Run(origin_url, usage); |
215 } | 280 } |
216 | 281 |
217 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { | 282 void DatabaseQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { |
218 DCHECK(origins_for_type_callbacks_.HasCallbacks()); | 283 DCHECK(origins_for_type_callbacks_.HasCallbacks()); |
219 origins_for_type_callbacks_.Run(origins); | 284 origins_for_type_callbacks_.Run(origins); |
220 } | 285 } |
221 | 286 |
222 void DatabaseQuotaClient::DidGetOriginsForHost( | 287 void DatabaseQuotaClient::DidGetOriginsForHost( |
223 const std::string& host, const std::set<GURL>& origins) { | 288 const std::string& host, const std::set<GURL>& origins) { |
224 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); | 289 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); |
225 origins_for_host_callbacks_.Run(host, origins); | 290 origins_for_host_callbacks_.Run(host, origins); |
226 } | 291 } |
227 | 292 |
228 } // namespace webkit_database | 293 } // namespace webkit_database |
OLD | NEW |