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