| 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_service.h" | 5 #include "webkit/appcache/appcache_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 AppCacheInfoCollection::AppCacheInfoCollection() {} | 35 AppCacheInfoCollection::AppCacheInfoCollection() {} |
| 36 | 36 |
| 37 AppCacheInfoCollection::~AppCacheInfoCollection() {} | 37 AppCacheInfoCollection::~AppCacheInfoCollection() {} |
| 38 | 38 |
| 39 // AsyncHelper ------- | 39 // AsyncHelper ------- |
| 40 | 40 |
| 41 class AppCacheService::NewAsyncHelper | |
| 42 : public AppCacheStorage::Delegate { | |
| 43 public: | |
| 44 NewAsyncHelper(AppCacheService* service, | |
| 45 const net::CompletionCallback& callback) | |
| 46 : service_(service), callback_(callback) { | |
| 47 service_->pending_new_helpers_.insert(this); | |
| 48 } | |
| 49 | |
| 50 virtual ~NewAsyncHelper() { | |
| 51 if (service_) | |
| 52 service_->pending_new_helpers_.erase(this); | |
| 53 } | |
| 54 | |
| 55 virtual void Start() = 0; | |
| 56 virtual void Cancel(); | |
| 57 | |
| 58 protected: | |
| 59 void CallCallback(int rv) { | |
| 60 if (!callback_.is_null()) { | |
| 61 // Defer to guarantee async completion. | |
| 62 MessageLoop::current()->PostTask( | |
| 63 FROM_HERE, base::Bind(&DeferredCallback, callback_, rv)); | |
| 64 } | |
| 65 callback_.Reset(); | |
| 66 } | |
| 67 | |
| 68 AppCacheService* service_; | |
| 69 net::CompletionCallback callback_; | |
| 70 }; | |
| 71 | |
| 72 void AppCacheService::NewAsyncHelper::Cancel() { | |
| 73 if (!callback_.is_null()) { | |
| 74 callback_.Run(net::ERR_ABORTED); | |
| 75 callback_.Reset(); | |
| 76 } | |
| 77 | |
| 78 service_->storage()->CancelDelegateCallbacks(this); | |
| 79 service_ = NULL; | |
| 80 } | |
| 81 | |
| 82 class AppCacheService::AsyncHelper | 41 class AppCacheService::AsyncHelper |
| 83 : public AppCacheStorage::Delegate { | 42 : public AppCacheStorage::Delegate { |
| 84 public: | 43 public: |
| 85 AsyncHelper( | 44 AsyncHelper(AppCacheService* service, |
| 86 AppCacheService* service, const net::CompletionCallback& callback) | 45 const net::CompletionCallback& callback) |
| 87 : service_(service), | 46 : service_(service), callback_(callback) { |
| 88 callback_(callback) { | |
| 89 service_->pending_helpers_.insert(this); | 47 service_->pending_helpers_.insert(this); |
| 90 } | 48 } |
| 91 | 49 |
| 92 virtual ~AsyncHelper() { | 50 virtual ~AsyncHelper() { |
| 93 if (service_) | 51 if (service_) |
| 94 service_->pending_helpers_.erase(this); | 52 service_->pending_helpers_.erase(this); |
| 95 } | 53 } |
| 96 | 54 |
| 97 virtual void Start() = 0; | 55 virtual void Start() = 0; |
| 98 virtual void Cancel(); | 56 virtual void Cancel(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 115 if (!callback_.is_null()) { | 73 if (!callback_.is_null()) { |
| 116 callback_.Run(net::ERR_ABORTED); | 74 callback_.Run(net::ERR_ABORTED); |
| 117 callback_.Reset(); | 75 callback_.Reset(); |
| 118 } | 76 } |
| 119 service_->storage()->CancelDelegateCallbacks(this); | 77 service_->storage()->CancelDelegateCallbacks(this); |
| 120 service_ = NULL; | 78 service_ = NULL; |
| 121 } | 79 } |
| 122 | 80 |
| 123 // CanHandleOfflineHelper ------- | 81 // CanHandleOfflineHelper ------- |
| 124 | 82 |
| 125 class AppCacheService::CanHandleOfflineHelper : NewAsyncHelper { | 83 class AppCacheService::CanHandleOfflineHelper : AsyncHelper { |
| 126 public: | 84 public: |
| 127 CanHandleOfflineHelper( | 85 CanHandleOfflineHelper( |
| 128 AppCacheService* service, const GURL& url, | 86 AppCacheService* service, const GURL& url, |
| 129 const GURL& first_party, const net::CompletionCallback& callback) | 87 const GURL& first_party, const net::CompletionCallback& callback) |
| 130 : NewAsyncHelper(service, callback), | 88 : AsyncHelper(service, callback), |
| 131 url_(url), | 89 url_(url), |
| 132 first_party_(first_party) { | 90 first_party_(first_party) { |
| 133 } | 91 } |
| 134 | 92 |
| 135 virtual void Start() { | 93 virtual void Start() { |
| 136 AppCachePolicy* policy = service_->appcache_policy(); | 94 AppCachePolicy* policy = service_->appcache_policy(); |
| 137 if (policy && !policy->CanLoadAppCache(url_, first_party_)) { | 95 if (policy && !policy->CanLoadAppCache(url_, first_party_)) { |
| 138 CallCallback(net::ERR_FAILED); | 96 CallCallback(net::ERR_FAILED); |
| 139 delete this; | 97 delete this; |
| 140 return; | 98 return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 160 const GURL& url, const AppCacheEntry& entry, | 118 const GURL& url, const AppCacheEntry& entry, |
| 161 const GURL& fallback_url, const AppCacheEntry& fallback_entry, | 119 const GURL& fallback_url, const AppCacheEntry& fallback_entry, |
| 162 int64 cache_id, int64 group_id, const GURL& manifest_url) { | 120 int64 cache_id, int64 group_id, const GURL& manifest_url) { |
| 163 bool can = (entry.has_response_id() || fallback_entry.has_response_id()); | 121 bool can = (entry.has_response_id() || fallback_entry.has_response_id()); |
| 164 CallCallback(can ? net::OK : net::ERR_FAILED); | 122 CallCallback(can ? net::OK : net::ERR_FAILED); |
| 165 delete this; | 123 delete this; |
| 166 } | 124 } |
| 167 | 125 |
| 168 // DeleteHelper ------- | 126 // DeleteHelper ------- |
| 169 | 127 |
| 170 class AppCacheService::DeleteHelper : public NewAsyncHelper { | 128 class AppCacheService::DeleteHelper : public AsyncHelper { |
| 171 public: | 129 public: |
| 172 DeleteHelper( | 130 DeleteHelper( |
| 173 AppCacheService* service, const GURL& manifest_url, | 131 AppCacheService* service, const GURL& manifest_url, |
| 174 const net::CompletionCallback& callback) | 132 const net::CompletionCallback& callback) |
| 175 : NewAsyncHelper(service, callback), manifest_url_(manifest_url) { | 133 : AsyncHelper(service, callback), manifest_url_(manifest_url) { |
| 176 } | 134 } |
| 177 | 135 |
| 178 virtual void Start() { | 136 virtual void Start() { |
| 179 service_->storage()->LoadOrCreateGroup(manifest_url_, this); | 137 service_->storage()->LoadOrCreateGroup(manifest_url_, this); |
| 180 } | 138 } |
| 181 | 139 |
| 182 private: | 140 private: |
| 183 // AppCacheStorage::Delegate implementation. | 141 // AppCacheStorage::Delegate implementation. |
| 184 virtual void OnGroupLoaded( | 142 virtual void OnGroupLoaded( |
| 185 appcache::AppCacheGroup* group, const GURL& manifest_url); | 143 appcache::AppCacheGroup* group, const GURL& manifest_url); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 203 } | 161 } |
| 204 | 162 |
| 205 void AppCacheService::DeleteHelper::OnGroupMadeObsolete( | 163 void AppCacheService::DeleteHelper::OnGroupMadeObsolete( |
| 206 appcache::AppCacheGroup* group, bool success) { | 164 appcache::AppCacheGroup* group, bool success) { |
| 207 CallCallback(success ? net::OK : net::ERR_FAILED); | 165 CallCallback(success ? net::OK : net::ERR_FAILED); |
| 208 delete this; | 166 delete this; |
| 209 } | 167 } |
| 210 | 168 |
| 211 // DeleteOriginHelper ------- | 169 // DeleteOriginHelper ------- |
| 212 | 170 |
| 213 class AppCacheService::DeleteOriginHelper : public NewAsyncHelper { | 171 class AppCacheService::DeleteOriginHelper : public AsyncHelper { |
| 214 public: | 172 public: |
| 215 DeleteOriginHelper( | 173 DeleteOriginHelper( |
| 216 AppCacheService* service, const GURL& origin, | 174 AppCacheService* service, const GURL& origin, |
| 217 const net::CompletionCallback& callback) | 175 const net::CompletionCallback& callback) |
| 218 : NewAsyncHelper(service, callback), origin_(origin), | 176 : AsyncHelper(service, callback), origin_(origin), |
| 219 num_caches_to_delete_(0), successes_(0), failures_(0) { | 177 num_caches_to_delete_(0), successes_(0), failures_(0) { |
| 220 } | 178 } |
| 221 | 179 |
| 222 virtual void Start() { | 180 virtual void Start() { |
| 223 // We start by listing all caches, continues in OnAllInfo(). | 181 // We start by listing all caches, continues in OnAllInfo(). |
| 224 service_->storage()->GetAllInfo(this); | 182 service_->storage()->GetAllInfo(this); |
| 225 } | 183 } |
| 226 | 184 |
| 227 private: | 185 private: |
| 228 // AppCacheStorage::Delegate implementation. | 186 // AppCacheStorage::Delegate implementation. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 if ((successes_ + failures_) < num_caches_to_delete_) | 253 if ((successes_ + failures_) < num_caches_to_delete_) |
| 296 return; | 254 return; |
| 297 | 255 |
| 298 CallCallback(!failures_ ? net::OK : net::ERR_FAILED); | 256 CallCallback(!failures_ ? net::OK : net::ERR_FAILED); |
| 299 delete this; | 257 delete this; |
| 300 } | 258 } |
| 301 | 259 |
| 302 | 260 |
| 303 // GetInfoHelper ------- | 261 // GetInfoHelper ------- |
| 304 | 262 |
| 305 class AppCacheService::GetInfoHelper : NewAsyncHelper { | 263 class AppCacheService::GetInfoHelper : AsyncHelper { |
| 306 public: | 264 public: |
| 307 GetInfoHelper( | 265 GetInfoHelper( |
| 308 AppCacheService* service, AppCacheInfoCollection* collection, | 266 AppCacheService* service, AppCacheInfoCollection* collection, |
| 309 const net::CompletionCallback& callback) | 267 const net::CompletionCallback& callback) |
| 310 : NewAsyncHelper(service, callback), collection_(collection) { | 268 : AsyncHelper(service, callback), collection_(collection) { |
| 311 } | 269 } |
| 312 | 270 |
| 313 virtual void Start() { | 271 virtual void Start() { |
| 314 service_->storage()->GetAllInfo(this); | 272 service_->storage()->GetAllInfo(this); |
| 315 } | 273 } |
| 316 | 274 |
| 317 private: | 275 private: |
| 318 // AppCacheStorage::Delegate implementation. | 276 // AppCacheStorage::Delegate implementation. |
| 319 virtual void OnAllInfo(AppCacheInfoCollection* collection); | 277 virtual void OnAllInfo(AppCacheInfoCollection* collection); |
| 320 | 278 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 quota_manager_proxy_->RegisterClient(quota_client_); | 432 quota_manager_proxy_->RegisterClient(quota_client_); |
| 475 } | 433 } |
| 476 } | 434 } |
| 477 | 435 |
| 478 AppCacheService::~AppCacheService() { | 436 AppCacheService::~AppCacheService() { |
| 479 DCHECK(backends_.empty()); | 437 DCHECK(backends_.empty()); |
| 480 std::for_each(pending_helpers_.begin(), | 438 std::for_each(pending_helpers_.begin(), |
| 481 pending_helpers_.end(), | 439 pending_helpers_.end(), |
| 482 std::mem_fun(&AsyncHelper::Cancel)); | 440 std::mem_fun(&AsyncHelper::Cancel)); |
| 483 STLDeleteElements(&pending_helpers_); | 441 STLDeleteElements(&pending_helpers_); |
| 484 std::for_each(pending_new_helpers_.begin(), | |
| 485 pending_new_helpers_.end(), | |
| 486 std::mem_fun(&NewAsyncHelper::Cancel)); | |
| 487 STLDeleteElements(&pending_new_helpers_); | |
| 488 if (quota_client_) | 442 if (quota_client_) |
| 489 quota_client_->NotifyAppCacheDestroyed(); | 443 quota_client_->NotifyAppCacheDestroyed(); |
| 490 | 444 |
| 491 // Destroy storage_ first; ~AppCacheStorageImpl accesses other data members | 445 // Destroy storage_ first; ~AppCacheStorageImpl accesses other data members |
| 492 // (special_storage_policy_). | 446 // (special_storage_policy_). |
| 493 storage_.reset(); | 447 storage_.reset(); |
| 494 } | 448 } |
| 495 | 449 |
| 496 void AppCacheService::Initialize(const FilePath& cache_directory, | 450 void AppCacheService::Initialize(const FilePath& cache_directory, |
| 497 base::MessageLoopProxy* db_thread, | 451 base::MessageLoopProxy* db_thread, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 backends_.insert( | 505 backends_.insert( |
| 552 BackendMap::value_type(backend_impl->process_id(), backend_impl)); | 506 BackendMap::value_type(backend_impl->process_id(), backend_impl)); |
| 553 } | 507 } |
| 554 | 508 |
| 555 void AppCacheService::UnregisterBackend( | 509 void AppCacheService::UnregisterBackend( |
| 556 AppCacheBackendImpl* backend_impl) { | 510 AppCacheBackendImpl* backend_impl) { |
| 557 backends_.erase(backend_impl->process_id()); | 511 backends_.erase(backend_impl->process_id()); |
| 558 } | 512 } |
| 559 | 513 |
| 560 } // namespace appcache | 514 } // namespace appcache |
| OLD | NEW |