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/appcache/appcache_quota_client.h" | 5 #include "webkit/appcache/appcache_quota_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <set> | |
9 | 10 |
10 #include "webkit/appcache/appcache_service.h" | 11 #include "webkit/appcache/appcache_service.h" |
11 | 12 |
12 using quota::QuotaClient; | 13 using quota::QuotaClient; |
13 | 14 |
14 namespace { | 15 namespace { |
15 quota::QuotaStatusCode NetErrorCodeToQuotaStatus(int code) { | 16 quota::QuotaStatusCode NetErrorCodeToQuotaStatus(int code) { |
16 if (code == net::OK) | 17 if (code == net::OK) |
17 return quota::kQuotaStatusOk; | 18 return quota::kQuotaStatusOk; |
18 else if (code == net::ERR_ABORTED) | 19 else if (code == net::ERR_ABORTED) |
(...skipping 10 matching lines...) Expand all Loading... | |
29 new net::CancelableCompletionCallback<AppCacheQuotaClient>( | 30 new net::CancelableCompletionCallback<AppCacheQuotaClient>( |
30 this, &AppCacheQuotaClient::DidDeleteAppCachesForOrigin))), | 31 this, &AppCacheQuotaClient::DidDeleteAppCachesForOrigin))), |
31 service_(service), appcache_is_ready_(false), | 32 service_(service), appcache_is_ready_(false), |
32 quota_manager_is_destroyed_(false) { | 33 quota_manager_is_destroyed_(false) { |
33 } | 34 } |
34 | 35 |
35 AppCacheQuotaClient::~AppCacheQuotaClient() { | 36 AppCacheQuotaClient::~AppCacheQuotaClient() { |
36 DCHECK(pending_usage_requests_.empty()); | 37 DCHECK(pending_usage_requests_.empty()); |
37 DCHECK(pending_origins_requests_.empty()); | 38 DCHECK(pending_origins_requests_.empty()); |
38 DCHECK(pending_delete_requests_.empty()); | 39 DCHECK(pending_delete_requests_.empty()); |
39 DCHECK(!current_delete_request_callback_.get()); | 40 DCHECK(current_delete_request_callback_.is_null()); |
40 } | 41 } |
41 | 42 |
42 QuotaClient::ID AppCacheQuotaClient::id() const { | 43 QuotaClient::ID AppCacheQuotaClient::id() const { |
43 return kAppcache; | 44 return kAppcache; |
44 } | 45 } |
45 | 46 |
46 void AppCacheQuotaClient::OnQuotaManagerDestroyed() { | 47 void AppCacheQuotaClient::OnQuotaManagerDestroyed() { |
47 DeletePendingRequests(); | 48 DeletePendingRequests(); |
48 if (current_delete_request_callback_.get()) { | 49 if (!current_delete_request_callback_.is_null()) { |
49 current_delete_request_callback_.reset(); | 50 current_delete_request_callback_.Reset(); |
50 service_delete_callback_.release()->Cancel(); | 51 service_delete_callback_.release()->Cancel(); |
51 } else { | 52 } else { |
52 service_delete_callback_ = NULL; | 53 service_delete_callback_ = NULL; |
53 } | 54 } |
54 quota_manager_is_destroyed_ = true; | 55 quota_manager_is_destroyed_ = true; |
55 if (!service_) | 56 if (!service_) |
56 delete this; | 57 delete this; |
57 } | 58 } |
58 | 59 |
59 void AppCacheQuotaClient::GetOriginUsage( | 60 void AppCacheQuotaClient::GetOriginUsage( |
60 const GURL& origin, | 61 const GURL& origin, |
61 quota::StorageType type, | 62 quota::StorageType type, |
62 GetUsageCallback* callback_ptr) { | 63 GetUsageCallback callback) { |
63 DCHECK(callback_ptr); | 64 DCHECK(!callback.is_null()); |
64 DCHECK(!quota_manager_is_destroyed_); | 65 DCHECK(!quota_manager_is_destroyed_); |
65 | 66 |
66 scoped_ptr<GetUsageCallback> callback(callback_ptr); | |
67 if (!service_) { | 67 if (!service_) { |
68 callback->Run(0); | 68 callback.Run(0); |
69 return; | 69 return; |
70 } | 70 } |
71 | 71 |
72 if (!appcache_is_ready_) { | 72 if (!appcache_is_ready_) { |
73 pending_usage_requests_.push_back(UsageRequest()); | 73 pending_usage_requests_.push_back(UsageRequest()); |
74 pending_usage_requests_.back().origin = origin; | 74 pending_usage_requests_.back().origin = origin; |
75 pending_usage_requests_.back().type = type; | 75 pending_usage_requests_.back().type = type; |
76 pending_usage_requests_.back().callback = callback.release(); | 76 pending_usage_requests_.back().callback = callback; |
77 return; | 77 return; |
78 } | 78 } |
79 | 79 |
80 if (type == quota::kStorageTypePersistent) { | 80 if (type == quota::kStorageTypePersistent) { |
81 callback->Run(0); | 81 callback.Run(0); |
82 return; | 82 return; |
83 } | 83 } |
84 | 84 |
85 const AppCacheStorage::UsageMap* map = GetUsageMap(); | 85 const AppCacheStorage::UsageMap* map = GetUsageMap(); |
86 AppCacheStorage::UsageMap::const_iterator found = map->find(origin); | 86 AppCacheStorage::UsageMap::const_iterator found = map->find(origin); |
87 if (found == map->end()) { | 87 if (found == map->end()) { |
88 callback->Run(0); | 88 callback.Run(0); |
89 return; | 89 return; |
90 } | 90 } |
91 callback->Run(found->second); | 91 callback.Run(found->second); |
92 } | 92 } |
93 | 93 |
94 void AppCacheQuotaClient::GetOriginsForType( | 94 void AppCacheQuotaClient::GetOriginsForType( |
95 quota::StorageType type, | 95 quota::StorageType type, |
96 GetOriginsCallback* callback_ptr) { | 96 GetOriginsCallback callback) { |
97 GetOriginsHelper(type, std::string(), callback_ptr); | 97 GetOriginsHelper(type, std::string(), callback); |
98 } | 98 } |
99 | 99 |
100 void AppCacheQuotaClient::GetOriginsForHost( | 100 void AppCacheQuotaClient::GetOriginsForHost( |
101 quota::StorageType type, | 101 quota::StorageType type, |
102 const std::string& host, | 102 const std::string& host, |
103 GetOriginsCallback* callback_ptr) { | 103 GetOriginsCallback callback) { |
104 DCHECK(callback_ptr); | 104 DCHECK(!callback.is_null()); |
105 if (host.empty()) { | 105 if (host.empty()) { |
106 callback_ptr->Run(std::set<GURL>(), type); | 106 callback.Run(std::set<GURL>(), type); |
107 delete callback_ptr; | |
108 return; | 107 return; |
109 } | 108 } |
110 GetOriginsHelper(type, host, callback_ptr); | 109 GetOriginsHelper(type, host, callback); |
111 } | 110 } |
112 | 111 |
113 void AppCacheQuotaClient::DeleteOriginData(const GURL& origin, | 112 void AppCacheQuotaClient::DeleteOriginData(const GURL& origin, |
114 quota::StorageType type, | 113 quota::StorageType type, |
115 DeletionCallback* callback_ptr) { | 114 DeletionCallback callback) { |
116 DCHECK(callback_ptr); | |
117 DCHECK(!quota_manager_is_destroyed_); | 115 DCHECK(!quota_manager_is_destroyed_); |
118 | 116 |
119 scoped_ptr<DeletionCallback> callback(callback_ptr); | |
120 if (!service_) { | 117 if (!service_) { |
121 callback->Run(quota::kQuotaErrorAbort); | 118 callback.Run(quota::kQuotaErrorAbort); |
122 return; | 119 return; |
123 } | 120 } |
124 | 121 |
125 if (!appcache_is_ready_ || current_delete_request_callback_.get()) { | 122 if (!appcache_is_ready_ || !current_delete_request_callback_.is_null()) { |
126 pending_delete_requests_.push_back(DeleteRequest()); | 123 pending_delete_requests_.push_back(DeleteRequest()); |
127 pending_delete_requests_.back().origin = origin; | 124 pending_delete_requests_.back().origin = origin; |
128 pending_delete_requests_.back().type = type; | 125 pending_delete_requests_.back().type = type; |
129 pending_delete_requests_.back().callback = callback.release(); | 126 pending_delete_requests_.back().callback = callback; |
130 return; | 127 return; |
131 } | 128 } |
132 | 129 |
133 current_delete_request_callback_.swap(callback); | 130 std::swap(current_delete_request_callback_, callback); |
134 if (type == quota::kStorageTypePersistent) { | 131 if (type == quota::kStorageTypePersistent) { |
135 DidDeleteAppCachesForOrigin(net::OK); | 132 DidDeleteAppCachesForOrigin(net::OK); |
136 return; | 133 return; |
137 } | 134 } |
138 service_->DeleteAppCachesForOrigin(origin, service_delete_callback_); | 135 service_->DeleteAppCachesForOrigin(origin, service_delete_callback_); |
139 } | 136 } |
140 | 137 |
138 AppCacheQuotaClient::UsageRequest::UsageRequest() {} | |
139 AppCacheQuotaClient::UsageRequest::~UsageRequest() {} | |
140 | |
141 AppCacheQuotaClient::OriginsRequest::OriginsRequest() {} | |
142 AppCacheQuotaClient::OriginsRequest::~OriginsRequest() {} | |
143 | |
144 AppCacheQuotaClient::DeleteRequest::DeleteRequest() {} | |
145 AppCacheQuotaClient::DeleteRequest::~DeleteRequest() {} | |
146 | |
141 void AppCacheQuotaClient::DidDeleteAppCachesForOrigin(int rv) { | 147 void AppCacheQuotaClient::DidDeleteAppCachesForOrigin(int rv) { |
142 DCHECK(service_); | 148 DCHECK(service_); |
143 if (quota_manager_is_destroyed_) | 149 if (quota_manager_is_destroyed_) |
144 return; | 150 return; |
145 | 151 |
146 // Finish the request by calling our callers callback. | 152 // Finish the request by calling our callers callback. |
147 current_delete_request_callback_->Run(NetErrorCodeToQuotaStatus(rv)); | 153 current_delete_request_callback_.Run(NetErrorCodeToQuotaStatus(rv)); |
148 current_delete_request_callback_.reset(); | 154 current_delete_request_callback_.Reset(); |
149 if (pending_delete_requests_.empty()) | 155 if (pending_delete_requests_.empty()) |
150 return; | 156 return; |
151 | 157 |
152 // Start the next in the queue. | 158 // Start the next in the queue. |
153 DeleteRequest& next_request = pending_delete_requests_.front(); | 159 DeleteRequest& next_request = pending_delete_requests_.front(); |
154 current_delete_request_callback_.reset(next_request.callback); | 160 current_delete_request_callback_ = next_request.callback; |
155 service_->DeleteAppCachesForOrigin(next_request.origin, | 161 service_->DeleteAppCachesForOrigin(next_request.origin, |
156 service_delete_callback_); | 162 service_delete_callback_); |
157 pending_delete_requests_.pop_front(); | 163 pending_delete_requests_.pop_front(); |
158 } | 164 } |
159 | 165 |
160 void AppCacheQuotaClient::GetOriginsHelper( | 166 void AppCacheQuotaClient::GetOriginsHelper( |
161 quota::StorageType type, | 167 quota::StorageType type, |
162 const std::string& opt_host, | 168 const std::string& opt_host, |
163 GetOriginsCallback* callback_ptr) { | 169 GetOriginsCallback callback) { |
164 DCHECK(callback_ptr); | 170 DCHECK(!callback.is_null()); |
165 DCHECK(!quota_manager_is_destroyed_); | 171 DCHECK(!quota_manager_is_destroyed_); |
166 | 172 |
167 scoped_ptr<GetOriginsCallback> callback(callback_ptr); | |
168 if (!service_) { | 173 if (!service_) { |
169 callback->Run(std::set<GURL>(), type); | 174 callback.Run(std::set<GURL>(), type); |
170 return; | 175 return; |
171 } | 176 } |
172 | 177 |
173 if (!appcache_is_ready_) { | 178 if (!appcache_is_ready_) { |
174 pending_origins_requests_.push_back(OriginsRequest()); | 179 pending_origins_requests_.push_back(OriginsRequest()); |
175 pending_origins_requests_.back().opt_host = opt_host; | 180 pending_origins_requests_.back().opt_host = opt_host; |
176 pending_origins_requests_.back().type = type; | 181 pending_origins_requests_.back().type = type; |
177 pending_origins_requests_.back().callback = callback.release(); | 182 pending_origins_requests_.back().callback = callback; |
178 return; | 183 return; |
179 } | 184 } |
180 | 185 |
181 if (type == quota::kStorageTypePersistent) { | 186 if (type == quota::kStorageTypePersistent) { |
182 callback->Run(std::set<GURL>(), type); | 187 callback.Run(std::set<GURL>(), type); |
183 return; | 188 return; |
184 } | 189 } |
185 | 190 |
186 const AppCacheStorage::UsageMap* map = GetUsageMap(); | 191 const AppCacheStorage::UsageMap* map = GetUsageMap(); |
187 std::set<GURL> origins; | 192 std::set<GURL> origins; |
188 for (AppCacheStorage::UsageMap::const_iterator iter = map->begin(); | 193 for (AppCacheStorage::UsageMap::const_iterator iter = map->begin(); |
189 iter != map->end(); ++iter) { | 194 iter != map->end(); ++iter) { |
190 if (opt_host.empty() || iter->first.host() == opt_host) | 195 if (opt_host.empty() || iter->first.host() == opt_host) |
191 origins.insert(iter->first); | 196 origins.insert(iter->first); |
192 } | 197 } |
193 callback->Run(origins, type); | 198 callback.Run(origins, type); |
194 } | 199 } |
195 | 200 |
196 void AppCacheQuotaClient::ProcessPendingRequests() { | 201 void AppCacheQuotaClient::ProcessPendingRequests() { |
197 DCHECK(appcache_is_ready_); | 202 DCHECK(appcache_is_ready_); |
198 while (!pending_usage_requests_.empty()) { | 203 while (!pending_usage_requests_.empty()) { |
199 UsageRequest& request = pending_usage_requests_.front(); | 204 UsageRequest& request = pending_usage_requests_.front(); |
awong
2011/09/29 18:05:15
Hmm...I wonder if you could replace all these requ
tzik
2011/10/11 04:53:57
Sounds great. But let me do in another CL.
| |
200 GetOriginUsage(request.origin, request.type, request.callback); | 205 GetOriginUsage(request.origin, request.type, request.callback); |
201 pending_usage_requests_.pop_front(); | 206 pending_usage_requests_.pop_front(); |
202 } | 207 } |
203 while (!pending_origins_requests_.empty()) { | 208 while (!pending_origins_requests_.empty()) { |
204 OriginsRequest& request = pending_origins_requests_.front(); | 209 OriginsRequest& request = pending_origins_requests_.front(); |
205 GetOriginsHelper(request.type, request.opt_host, request.callback); | 210 GetOriginsHelper(request.type, request.opt_host, request.callback); |
206 pending_origins_requests_.pop_front(); | 211 pending_origins_requests_.pop_front(); |
207 } | 212 } |
208 if (!pending_delete_requests_.empty()) { | 213 if (!pending_delete_requests_.empty()) { |
209 // Just start the first delete, others will follow upon completion. | 214 // Just start the first delete, others will follow upon completion. |
210 DeleteRequest request = pending_delete_requests_.front(); | 215 DeleteRequest request = pending_delete_requests_.front(); |
211 pending_delete_requests_.pop_front(); | 216 pending_delete_requests_.pop_front(); |
212 DeleteOriginData(request.origin, request.type, request.callback); | 217 DeleteOriginData(request.origin, request.type, request.callback); |
213 } | 218 } |
214 } | 219 } |
215 | 220 |
216 void AppCacheQuotaClient::AbortPendingRequests() { | 221 void AppCacheQuotaClient::AbortPendingRequests() { |
217 while (!pending_usage_requests_.empty()) { | 222 while (!pending_usage_requests_.empty()) { |
218 pending_usage_requests_.front().callback->Run(0); | 223 pending_usage_requests_.front().callback.Run(0); |
219 delete pending_usage_requests_.front().callback; | |
220 pending_usage_requests_.pop_front(); | 224 pending_usage_requests_.pop_front(); |
221 } | 225 } |
222 while (!pending_origins_requests_.empty()) { | 226 while (!pending_origins_requests_.empty()) { |
223 pending_origins_requests_.front().callback->Run(std::set<GURL>(), | 227 pending_origins_requests_.front().callback.Run(std::set<GURL>(), |
224 pending_origins_requests_.front().type); | 228 pending_origins_requests_.front().type); |
225 delete pending_origins_requests_.front().callback; | |
226 pending_origins_requests_.pop_front(); | 229 pending_origins_requests_.pop_front(); |
227 } | 230 } |
228 while (!pending_delete_requests_.empty()) { | 231 while (!pending_delete_requests_.empty()) { |
229 pending_delete_requests_.front().callback->Run(quota::kQuotaErrorAbort); | 232 pending_delete_requests_.front().callback.Run(quota::kQuotaErrorAbort); |
230 delete pending_delete_requests_.front().callback; | |
231 pending_delete_requests_.pop_front(); | 233 pending_delete_requests_.pop_front(); |
232 } | 234 } |
233 } | 235 } |
234 | 236 |
235 void AppCacheQuotaClient::DeletePendingRequests() { | 237 void AppCacheQuotaClient::DeletePendingRequests() { |
236 while (!pending_usage_requests_.empty()) { | 238 while (!pending_usage_requests_.empty()) { |
237 delete pending_usage_requests_.front().callback; | |
238 pending_usage_requests_.pop_front(); | 239 pending_usage_requests_.pop_front(); |
239 } | 240 } |
240 while (!pending_origins_requests_.empty()) { | 241 while (!pending_origins_requests_.empty()) { |
241 delete pending_origins_requests_.front().callback; | |
242 pending_origins_requests_.pop_front(); | 242 pending_origins_requests_.pop_front(); |
243 } | 243 } |
244 while (!pending_delete_requests_.empty()) { | 244 while (!pending_delete_requests_.empty()) { |
245 delete pending_delete_requests_.front().callback; | |
246 pending_delete_requests_.pop_front(); | 245 pending_delete_requests_.pop_front(); |
247 } | 246 } |
248 } | 247 } |
249 | 248 |
250 const AppCacheStorage::UsageMap* AppCacheQuotaClient::GetUsageMap() { | 249 const AppCacheStorage::UsageMap* AppCacheQuotaClient::GetUsageMap() { |
251 DCHECK(service_); | 250 DCHECK(service_); |
252 return service_->storage()->usage_map(); | 251 return service_->storage()->usage_map(); |
253 } | 252 } |
254 | 253 |
255 void AppCacheQuotaClient::NotifyAppCacheReady() { | 254 void AppCacheQuotaClient::NotifyAppCacheReady() { |
256 appcache_is_ready_ = true; | 255 appcache_is_ready_ = true; |
257 ProcessPendingRequests(); | 256 ProcessPendingRequests(); |
258 } | 257 } |
259 | 258 |
260 void AppCacheQuotaClient::NotifyAppCacheDestroyed() { | 259 void AppCacheQuotaClient::NotifyAppCacheDestroyed() { |
261 service_ = NULL; | 260 service_ = NULL; |
262 AbortPendingRequests(); | 261 AbortPendingRequests(); |
263 if (current_delete_request_callback_.get()) { | 262 if (!current_delete_request_callback_.is_null()) { |
264 current_delete_request_callback_->Run(quota::kQuotaErrorAbort); | 263 current_delete_request_callback_.Run(quota::kQuotaErrorAbort); |
265 current_delete_request_callback_.reset(); | 264 current_delete_request_callback_.Reset(); |
266 service_delete_callback_.release()->Cancel(); | 265 service_delete_callback_.release()->Cancel(); |
267 } else { | 266 } else { |
268 service_delete_callback_ = NULL; | 267 service_delete_callback_ = NULL; |
269 } | 268 } |
270 if (quota_manager_is_destroyed_) | 269 if (quota_manager_is_destroyed_) |
271 delete this; | 270 delete this; |
272 } | 271 } |
273 | 272 |
274 } // namespace appcache | 273 } // namespace appcache |
OLD | NEW |