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/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "webkit/quota/special_storage_policy.h" | 21 #include "webkit/quota/special_storage_policy.h" |
22 | 22 |
23 namespace appcache { | 23 namespace appcache { |
24 | 24 |
25 AppCacheInfoCollection::AppCacheInfoCollection() {} | 25 AppCacheInfoCollection::AppCacheInfoCollection() {} |
26 | 26 |
27 AppCacheInfoCollection::~AppCacheInfoCollection() {} | 27 AppCacheInfoCollection::~AppCacheInfoCollection() {} |
28 | 28 |
29 // AsyncHelper ------- | 29 // AsyncHelper ------- |
30 | 30 |
| 31 class AppCacheService::NewAsyncHelper |
| 32 : public AppCacheStorage::Delegate { |
| 33 public: |
| 34 NewAsyncHelper(AppCacheService* service, |
| 35 const net::CompletionCallback& callback) |
| 36 : service_(service), callback_(callback) { |
| 37 service_->pending_new_helpers_.insert(this); |
| 38 } |
| 39 |
| 40 virtual ~NewAsyncHelper() { |
| 41 if (service_) |
| 42 service_->pending_new_helpers_.erase(this); |
| 43 } |
| 44 |
| 45 virtual void Start() = 0; |
| 46 virtual void Cancel(); |
| 47 |
| 48 protected: |
| 49 void CallCallback(int rv) { |
| 50 if (!callback_.is_null()) { |
| 51 // Defer to guarantee async completion. |
| 52 MessageLoop::current()->PostTask( |
| 53 FROM_HERE, base::Bind(&DeferredCallCallback, callback_, rv)); |
| 54 } |
| 55 callback_.Reset(); |
| 56 } |
| 57 |
| 58 static void DeferredCallCallback(const net::CompletionCallback& callback, |
| 59 int rv) { |
| 60 callback.Run(rv); |
| 61 } |
| 62 |
| 63 AppCacheService* service_; |
| 64 net::CompletionCallback callback_; |
| 65 }; |
| 66 |
| 67 void AppCacheService::NewAsyncHelper::Cancel() { |
| 68 if (!callback_.is_null()) { |
| 69 callback_.Run(net::ERR_ABORTED); |
| 70 callback_.Reset(); |
| 71 } |
| 72 |
| 73 service_->storage()->CancelDelegateCallbacks(this); |
| 74 service_ = NULL; |
| 75 } |
| 76 |
31 class AppCacheService::AsyncHelper | 77 class AppCacheService::AsyncHelper |
32 : public AppCacheStorage::Delegate { | 78 : public AppCacheStorage::Delegate { |
33 public: | 79 public: |
34 AsyncHelper( | 80 AsyncHelper( |
35 AppCacheService* service, net::OldCompletionCallback* callback) | 81 AppCacheService* service, net::OldCompletionCallback* callback) |
36 : service_(service), callback_(callback) { | 82 : service_(service), callback_(callback) { |
37 service_->pending_helpers_.insert(this); | 83 service_->pending_helpers_.insert(this); |
38 } | 84 } |
39 | 85 |
40 virtual ~AsyncHelper() { | 86 virtual ~AsyncHelper() { |
(...skipping 27 matching lines...) Expand all Loading... |
68 if (callback_) { | 114 if (callback_) { |
69 callback_->Run(net::ERR_ABORTED); | 115 callback_->Run(net::ERR_ABORTED); |
70 callback_ = NULL; | 116 callback_ = NULL; |
71 } | 117 } |
72 service_->storage()->CancelDelegateCallbacks(this); | 118 service_->storage()->CancelDelegateCallbacks(this); |
73 service_ = NULL; | 119 service_ = NULL; |
74 } | 120 } |
75 | 121 |
76 // CanHandleOfflineHelper ------- | 122 // CanHandleOfflineHelper ------- |
77 | 123 |
78 class AppCacheService::CanHandleOfflineHelper : AsyncHelper { | 124 class AppCacheService::CanHandleOfflineHelper : NewAsyncHelper { |
79 public: | 125 public: |
80 CanHandleOfflineHelper( | 126 CanHandleOfflineHelper( |
81 AppCacheService* service, const GURL& url, | 127 AppCacheService* service, const GURL& url, |
82 const GURL& first_party, net::OldCompletionCallback* callback) | 128 const GURL& first_party, const net::CompletionCallback& callback) |
83 : AsyncHelper(service, callback), url_(url), first_party_(first_party) { | 129 : NewAsyncHelper(service, callback), |
| 130 url_(url), |
| 131 first_party_(first_party) { |
84 } | 132 } |
85 | 133 |
86 virtual void Start() { | 134 virtual void Start() { |
87 AppCachePolicy* policy = service_->appcache_policy(); | 135 AppCachePolicy* policy = service_->appcache_policy(); |
88 if (policy && !policy->CanLoadAppCache(url_, first_party_)) { | 136 if (policy && !policy->CanLoadAppCache(url_, first_party_)) { |
89 CallCallback(net::ERR_FAILED); | 137 CallCallback(net::ERR_FAILED); |
90 delete this; | 138 delete this; |
91 return; | 139 return; |
92 } | 140 } |
| 141 |
93 service_->storage()->FindResponseForMainRequest(url_, GURL(), this); | 142 service_->storage()->FindResponseForMainRequest(url_, GURL(), this); |
94 } | 143 } |
95 | 144 |
96 private: | 145 private: |
97 // AppCacheStorage::Delegate override | 146 // AppCacheStorage::Delegate override |
98 virtual void OnMainResponseFound( | 147 virtual void OnMainResponseFound( |
99 const GURL& url, const AppCacheEntry& entry, | 148 const GURL& url, const AppCacheEntry& entry, |
100 const GURL& fallback_url, const AppCacheEntry& fallback_entry, | 149 const GURL& fallback_url, const AppCacheEntry& fallback_entry, |
101 int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE; | 150 int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE; |
102 | 151 |
103 GURL url_; | 152 GURL url_; |
104 GURL first_party_; | 153 GURL first_party_; |
| 154 |
105 DISALLOW_COPY_AND_ASSIGN(CanHandleOfflineHelper); | 155 DISALLOW_COPY_AND_ASSIGN(CanHandleOfflineHelper); |
106 }; | 156 }; |
107 | 157 |
108 void AppCacheService::CanHandleOfflineHelper::OnMainResponseFound( | 158 void AppCacheService::CanHandleOfflineHelper::OnMainResponseFound( |
109 const GURL& url, const AppCacheEntry& entry, | 159 const GURL& url, const AppCacheEntry& entry, |
110 const GURL& fallback_url, const AppCacheEntry& fallback_entry, | 160 const GURL& fallback_url, const AppCacheEntry& fallback_entry, |
111 int64 cache_id, int64 group_id, const GURL& manifest_url) { | 161 int64 cache_id, int64 group_id, const GURL& manifest_url) { |
112 bool can = (entry.has_response_id() || fallback_entry.has_response_id()); | 162 bool can = (entry.has_response_id() || fallback_entry.has_response_id()); |
113 CallCallback(can ? net::OK : net::ERR_FAILED); | 163 CallCallback(can ? net::OK : net::ERR_FAILED); |
114 delete this; | 164 delete this; |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 quota_manager_proxy_->RegisterClient(quota_client_); | 469 quota_manager_proxy_->RegisterClient(quota_client_); |
420 } | 470 } |
421 } | 471 } |
422 | 472 |
423 AppCacheService::~AppCacheService() { | 473 AppCacheService::~AppCacheService() { |
424 DCHECK(backends_.empty()); | 474 DCHECK(backends_.empty()); |
425 std::for_each(pending_helpers_.begin(), | 475 std::for_each(pending_helpers_.begin(), |
426 pending_helpers_.end(), | 476 pending_helpers_.end(), |
427 std::mem_fun(&AsyncHelper::Cancel)); | 477 std::mem_fun(&AsyncHelper::Cancel)); |
428 STLDeleteElements(&pending_helpers_); | 478 STLDeleteElements(&pending_helpers_); |
| 479 std::for_each(pending_new_helpers_.begin(), |
| 480 pending_new_helpers_.end(), |
| 481 std::mem_fun(&NewAsyncHelper::Cancel)); |
| 482 STLDeleteElements(&pending_new_helpers_); |
429 if (quota_client_) | 483 if (quota_client_) |
430 quota_client_->NotifyAppCacheDestroyed(); | 484 quota_client_->NotifyAppCacheDestroyed(); |
431 | 485 |
432 // Destroy storage_ first; ~AppCacheStorageImpl accesses other data members | 486 // Destroy storage_ first; ~AppCacheStorageImpl accesses other data members |
433 // (special_storage_policy_). | 487 // (special_storage_policy_). |
434 storage_.reset(); | 488 storage_.reset(); |
435 } | 489 } |
436 | 490 |
437 void AppCacheService::Initialize(const FilePath& cache_directory, | 491 void AppCacheService::Initialize(const FilePath& cache_directory, |
438 base::MessageLoopProxy* db_thread, | 492 base::MessageLoopProxy* db_thread, |
439 base::MessageLoopProxy* cache_thread) { | 493 base::MessageLoopProxy* cache_thread) { |
440 DCHECK(!storage_.get()); | 494 DCHECK(!storage_.get()); |
441 AppCacheStorageImpl* storage = new AppCacheStorageImpl(this); | 495 AppCacheStorageImpl* storage = new AppCacheStorageImpl(this); |
442 storage->Initialize(cache_directory, db_thread, cache_thread); | 496 storage->Initialize(cache_directory, db_thread, cache_thread); |
443 storage_.reset(storage); | 497 storage_.reset(storage); |
444 } | 498 } |
445 | 499 |
446 void AppCacheService::CanHandleMainResourceOffline( | 500 void AppCacheService::CanHandleMainResourceOffline( |
447 const GURL& url, | 501 const GURL& url, |
448 const GURL& first_party, | 502 const GURL& first_party, |
449 net::OldCompletionCallback* callback) { | 503 const net::CompletionCallback& callback) { |
450 CanHandleOfflineHelper* helper = | 504 CanHandleOfflineHelper* helper = |
451 new CanHandleOfflineHelper(this, url, first_party, callback); | 505 new CanHandleOfflineHelper(this, url, first_party, callback); |
452 helper->Start(); | 506 helper->Start(); |
453 } | 507 } |
454 | 508 |
455 void AppCacheService::GetAllAppCacheInfo(AppCacheInfoCollection* collection, | 509 void AppCacheService::GetAllAppCacheInfo(AppCacheInfoCollection* collection, |
456 net::OldCompletionCallback* callback) { | 510 net::OldCompletionCallback* callback) { |
457 DCHECK(collection); | 511 DCHECK(collection); |
458 GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); | 512 GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); |
459 helper->Start(); | 513 helper->Start(); |
(...skipping 30 matching lines...) Expand all Loading... |
490 backends_.insert( | 544 backends_.insert( |
491 BackendMap::value_type(backend_impl->process_id(), backend_impl)); | 545 BackendMap::value_type(backend_impl->process_id(), backend_impl)); |
492 } | 546 } |
493 | 547 |
494 void AppCacheService::UnregisterBackend( | 548 void AppCacheService::UnregisterBackend( |
495 AppCacheBackendImpl* backend_impl) { | 549 AppCacheBackendImpl* backend_impl) { |
496 backends_.erase(backend_impl->process_id()); | 550 backends_.erase(backend_impl->process_id()); |
497 } | 551 } |
498 | 552 |
499 } // namespace appcache | 553 } // namespace appcache |
OLD | NEW |