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 DCHECK(service_); |
| 138 if (quota_manager_is_destroyed_) |
| 139 return; |
| 140 |
| 141 // Finish the request by calling our callers callback. |
| 142 current_delete_request_callback_->Run(NetErrorCodeToQuotaStatus(rv)); |
| 143 current_delete_request_callback_.reset(); |
| 144 if (pending_delete_requests_.empty()) |
| 145 return; |
| 146 |
| 147 // Start the next in the queue. |
| 148 DeleteRequest& next_request = pending_delete_requests_.front(); |
| 149 current_delete_request_callback_.reset(next_request.callback); |
| 150 service_->DeleteAppCachesForOrigin(next_request.origin, |
| 151 service_delete_callback_); |
| 152 pending_delete_requests_.pop_front(); |
| 153 } |
| 154 |
| 155 void AppCacheQuotaClient::GetOriginsHelper( |
| 156 quota::StorageType type, |
| 157 const std::string& opt_host, |
| 158 GetOriginsCallback* callback_ptr) { |
| 159 DCHECK(callback_ptr); |
| 160 DCHECK(!quota_manager_is_destroyed_); |
| 161 |
| 162 scoped_ptr<GetOriginsCallback> callback(callback_ptr); |
| 163 if (!service_) { |
| 164 callback->Run(std::set<GURL>()); |
| 165 return; |
| 166 } |
| 167 |
| 168 if (!appcache_is_ready_) { |
| 169 pending_origins_requests_.push_back(OriginsRequest()); |
| 170 pending_origins_requests_.back().opt_host = opt_host; |
| 171 pending_origins_requests_.back().type = type; |
| 172 pending_origins_requests_.back().callback = callback.release(); |
| 173 return; |
| 174 } |
| 175 |
| 176 if (type == quota::kStorageTypePersistent) { |
| 177 callback->Run(std::set<GURL>()); |
| 178 return; |
| 179 } |
| 180 |
| 181 const AppCacheStorage::UsageMap* map = GetUsageMap(); |
| 182 std::set<GURL> origins; |
| 183 for (AppCacheStorage::UsageMap::const_iterator iter = map->begin(); |
| 184 iter != map->end(); ++iter) { |
| 185 if (opt_host.empty() || iter->first.host() == opt_host) |
| 186 origins.insert(iter->first); |
| 187 } |
| 188 callback->Run(origins); |
| 189 } |
| 190 |
| 191 void AppCacheQuotaClient::ProcessPendingRequests() { |
| 192 DCHECK(appcache_is_ready_); |
| 193 while (!pending_usage_requests_.empty()) { |
| 194 UsageRequest& request = pending_usage_requests_.front(); |
| 195 GetOriginUsage(request.origin, request.type, request.callback); |
| 196 pending_usage_requests_.pop_front(); |
| 197 } |
| 198 while (!pending_origins_requests_.empty()) { |
| 199 OriginsRequest& request = pending_origins_requests_.front(); |
| 200 GetOriginsHelper(request.type, request.opt_host, request.callback); |
| 201 pending_origins_requests_.pop_front(); |
| 202 } |
| 203 if (!pending_delete_requests_.empty()) { |
| 204 // Just start the first delete, others will follow upon completion. |
| 205 DeleteRequest request = pending_delete_requests_.front(); |
| 206 pending_delete_requests_.pop_front(); |
| 207 DeleteOriginData(request.origin, request.type, request.callback); |
| 208 } |
| 209 } |
| 210 |
| 211 void AppCacheQuotaClient::AbortPendingRequests() { |
| 212 while (!pending_usage_requests_.empty()) { |
| 213 pending_usage_requests_.front().callback->Run(0); |
| 214 delete pending_usage_requests_.front().callback; |
| 215 pending_usage_requests_.pop_front(); |
| 216 } |
| 217 while (!pending_origins_requests_.empty()) { |
| 218 pending_origins_requests_.front().callback->Run(std::set<GURL>()); |
| 219 delete pending_origins_requests_.front().callback; |
| 220 pending_origins_requests_.pop_front(); |
| 221 } |
| 222 while (!pending_delete_requests_.empty()) { |
| 223 pending_delete_requests_.front().callback->Run(quota::kQuotaErrorAbort); |
| 224 delete pending_delete_requests_.front().callback; |
| 225 pending_delete_requests_.pop_front(); |
| 226 } |
| 227 } |
| 228 |
| 229 void AppCacheQuotaClient::DeletePendingRequests() { |
| 230 while (!pending_usage_requests_.empty()) { |
| 231 delete pending_usage_requests_.front().callback; |
| 232 pending_usage_requests_.pop_front(); |
| 233 } |
| 234 while (!pending_origins_requests_.empty()) { |
| 235 delete pending_origins_requests_.front().callback; |
| 236 pending_origins_requests_.pop_front(); |
| 237 } |
| 238 while (!pending_delete_requests_.empty()) { |
| 239 delete pending_delete_requests_.front().callback; |
| 240 pending_delete_requests_.pop_front(); |
| 241 } |
| 242 } |
| 243 |
| 244 const AppCacheStorage::UsageMap* AppCacheQuotaClient::GetUsageMap() { |
| 245 DCHECK(service_); |
| 246 return service_->storage()->usage_map(); |
| 247 } |
| 248 |
| 249 void AppCacheQuotaClient::NotifyAppCacheReady() { |
| 250 appcache_is_ready_ = true; |
| 251 ProcessPendingRequests(); |
| 252 } |
| 253 |
| 254 void AppCacheQuotaClient::NotifyAppCacheDestroyed() { |
| 255 service_ = NULL; |
| 256 AbortPendingRequests(); |
| 257 if (current_delete_request_callback_.get()) { |
| 258 current_delete_request_callback_->Run(quota::kQuotaErrorAbort); |
| 259 current_delete_request_callback_.reset(); |
| 260 service_delete_callback_.release()->Cancel(); |
| 261 } else { |
| 262 service_delete_callback_ = NULL; |
| 263 } |
| 264 if (quota_manager_is_destroyed_) |
| 265 delete this; |
| 266 } |
| 267 |
| 268 } // namespace appcache |
OLD | NEW |