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" |
| 11 #include "net/base/completion_callback.h" |
11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
12 #include "webkit/appcache/appcache.h" | 13 #include "webkit/appcache/appcache.h" |
13 #include "webkit/appcache/appcache_backend_impl.h" | 14 #include "webkit/appcache/appcache_backend_impl.h" |
14 #include "webkit/appcache/appcache_entry.h" | 15 #include "webkit/appcache/appcache_entry.h" |
15 #include "webkit/appcache/appcache_histograms.h" | 16 #include "webkit/appcache/appcache_histograms.h" |
16 #include "webkit/appcache/appcache_policy.h" | 17 #include "webkit/appcache/appcache_policy.h" |
17 #include "webkit/appcache/appcache_quota_client.h" | 18 #include "webkit/appcache/appcache_quota_client.h" |
18 #include "webkit/appcache/appcache_response.h" | 19 #include "webkit/appcache/appcache_response.h" |
19 #include "webkit/appcache/appcache_storage_impl.h" | 20 #include "webkit/appcache/appcache_storage_impl.h" |
20 #include "webkit/quota/quota_manager.h" | 21 #include "webkit/quota/quota_manager.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (service_) | 88 if (service_) |
88 service_->pending_helpers_.erase(this); | 89 service_->pending_helpers_.erase(this); |
89 } | 90 } |
90 | 91 |
91 virtual void Start() = 0; | 92 virtual void Start() = 0; |
92 virtual void Cancel(); | 93 virtual void Cancel(); |
93 | 94 |
94 protected: | 95 protected: |
95 void CallCallback(int rv) { | 96 void CallCallback(int rv) { |
96 if (callback_) { | 97 if (callback_) { |
97 // Defer to guarentee async completion. | 98 // Defer to guarantee async completion. |
98 MessageLoop::current()->PostTask( | 99 MessageLoop::current()->PostTask( |
99 FROM_HERE, base::Bind(&DeferredCallCallback, callback_, rv)); | 100 FROM_HERE, base::Bind(&DeferredCallCallback, callback_, rv)); |
100 } | 101 } |
101 callback_ = NULL; | 102 callback_ = NULL; |
102 } | 103 } |
103 | 104 |
104 static void DeferredCallCallback(net::OldCompletionCallback* callback, | 105 static void DeferredCallCallback(net::OldCompletionCallback* callback, |
105 int rv) { | 106 int rv) { |
106 callback->Run(rv); | 107 callback->Run(rv); |
107 } | 108 } |
(...skipping 28 matching lines...) Expand all Loading... |
136 if (policy && !policy->CanLoadAppCache(url_, first_party_)) { | 137 if (policy && !policy->CanLoadAppCache(url_, first_party_)) { |
137 CallCallback(net::ERR_FAILED); | 138 CallCallback(net::ERR_FAILED); |
138 delete this; | 139 delete this; |
139 return; | 140 return; |
140 } | 141 } |
141 | 142 |
142 service_->storage()->FindResponseForMainRequest(url_, GURL(), this); | 143 service_->storage()->FindResponseForMainRequest(url_, GURL(), this); |
143 } | 144 } |
144 | 145 |
145 private: | 146 private: |
146 // AppCacheStorage::Delegate override | 147 // AppCacheStorage::Delegate implementation. |
147 virtual void OnMainResponseFound( | 148 virtual void OnMainResponseFound( |
148 const GURL& url, const AppCacheEntry& entry, | 149 const GURL& url, const AppCacheEntry& entry, |
149 const GURL& fallback_url, const AppCacheEntry& fallback_entry, | 150 const GURL& fallback_url, const AppCacheEntry& fallback_entry, |
150 int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE; | 151 int64 cache_id, int64 group_id, const GURL& mainfest_url) OVERRIDE; |
151 | 152 |
152 GURL url_; | 153 GURL url_; |
153 GURL first_party_; | 154 GURL first_party_; |
154 | 155 |
155 DISALLOW_COPY_AND_ASSIGN(CanHandleOfflineHelper); | 156 DISALLOW_COPY_AND_ASSIGN(CanHandleOfflineHelper); |
156 }; | 157 }; |
157 | 158 |
158 void AppCacheService::CanHandleOfflineHelper::OnMainResponseFound( | 159 void AppCacheService::CanHandleOfflineHelper::OnMainResponseFound( |
159 const GURL& url, const AppCacheEntry& entry, | 160 const GURL& url, const AppCacheEntry& entry, |
160 const GURL& fallback_url, const AppCacheEntry& fallback_entry, | 161 const GURL& fallback_url, const AppCacheEntry& fallback_entry, |
161 int64 cache_id, int64 group_id, const GURL& manifest_url) { | 162 int64 cache_id, int64 group_id, const GURL& manifest_url) { |
162 bool can = (entry.has_response_id() || fallback_entry.has_response_id()); | 163 bool can = (entry.has_response_id() || fallback_entry.has_response_id()); |
163 CallCallback(can ? net::OK : net::ERR_FAILED); | 164 CallCallback(can ? net::OK : net::ERR_FAILED); |
164 delete this; | 165 delete this; |
165 } | 166 } |
166 | 167 |
167 // DeleteHelper ------- | 168 // DeleteHelper ------- |
168 | 169 |
169 class AppCacheService::DeleteHelper : public AsyncHelper { | 170 class AppCacheService::DeleteHelper : public NewAsyncHelper { |
170 public: | 171 public: |
171 DeleteHelper( | 172 DeleteHelper( |
172 AppCacheService* service, const GURL& manifest_url, | 173 AppCacheService* service, const GURL& manifest_url, |
173 net::OldCompletionCallback* callback) | 174 const net::CompletionCallback& callback) |
174 : AsyncHelper(service, callback), manifest_url_(manifest_url) { | 175 : NewAsyncHelper(service, callback), manifest_url_(manifest_url) { |
175 } | 176 } |
176 | 177 |
177 virtual void Start() { | 178 virtual void Start() { |
178 service_->storage()->LoadOrCreateGroup(manifest_url_, this); | 179 service_->storage()->LoadOrCreateGroup(manifest_url_, this); |
179 } | 180 } |
180 | 181 |
181 private: | 182 private: |
182 // AppCacheStorage::Delegate methods | 183 // AppCacheStorage::Delegate implementation. |
183 virtual void OnGroupLoaded( | 184 virtual void OnGroupLoaded( |
184 appcache::AppCacheGroup* group, const GURL& manifest_url); | 185 appcache::AppCacheGroup* group, const GURL& manifest_url); |
185 virtual void OnGroupMadeObsolete( | 186 virtual void OnGroupMadeObsolete( |
186 appcache::AppCacheGroup* group, bool success); | 187 appcache::AppCacheGroup* group, bool success); |
187 | 188 |
188 GURL manifest_url_; | 189 GURL manifest_url_; |
189 DISALLOW_COPY_AND_ASSIGN(DeleteHelper); | 190 DISALLOW_COPY_AND_ASSIGN(DeleteHelper); |
190 }; | 191 }; |
191 | 192 |
192 void AppCacheService::DeleteHelper::OnGroupLoaded( | 193 void AppCacheService::DeleteHelper::OnGroupLoaded( |
(...skipping 24 matching lines...) Expand all Loading... |
217 : AsyncHelper(service, callback), origin_(origin), | 218 : AsyncHelper(service, callback), origin_(origin), |
218 num_caches_to_delete_(0), successes_(0), failures_(0) { | 219 num_caches_to_delete_(0), successes_(0), failures_(0) { |
219 } | 220 } |
220 | 221 |
221 virtual void Start() { | 222 virtual void Start() { |
222 // We start by listing all caches, continues in OnAllInfo(). | 223 // We start by listing all caches, continues in OnAllInfo(). |
223 service_->storage()->GetAllInfo(this); | 224 service_->storage()->GetAllInfo(this); |
224 } | 225 } |
225 | 226 |
226 private: | 227 private: |
227 // AppCacheStorage::Delegate methods | 228 // AppCacheStorage::Delegate implementation. |
228 virtual void OnAllInfo(AppCacheInfoCollection* collection); | 229 virtual void OnAllInfo(AppCacheInfoCollection* collection); |
229 virtual void OnGroupLoaded( | 230 virtual void OnGroupLoaded( |
230 appcache::AppCacheGroup* group, const GURL& manifest_url); | 231 appcache::AppCacheGroup* group, const GURL& manifest_url); |
231 virtual void OnGroupMadeObsolete( | 232 virtual void OnGroupMadeObsolete( |
232 appcache::AppCacheGroup* group, bool success); | 233 appcache::AppCacheGroup* group, bool success); |
233 | 234 |
234 void CacheCompleted(bool success); | 235 void CacheCompleted(bool success); |
235 | 236 |
236 GURL origin_; | 237 GURL origin_; |
237 int num_caches_to_delete_; | 238 int num_caches_to_delete_; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 AppCacheService* service, AppCacheInfoCollection* collection, | 306 AppCacheService* service, AppCacheInfoCollection* collection, |
306 const net::CompletionCallback& callback) | 307 const net::CompletionCallback& callback) |
307 : NewAsyncHelper(service, callback), collection_(collection) { | 308 : NewAsyncHelper(service, callback), collection_(collection) { |
308 } | 309 } |
309 | 310 |
310 virtual void Start() { | 311 virtual void Start() { |
311 service_->storage()->GetAllInfo(this); | 312 service_->storage()->GetAllInfo(this); |
312 } | 313 } |
313 | 314 |
314 private: | 315 private: |
315 // AppCacheStorage::Delegate override | 316 // AppCacheStorage::Delegate implementation. |
316 virtual void OnAllInfo(AppCacheInfoCollection* collection); | 317 virtual void OnAllInfo(AppCacheInfoCollection* collection); |
317 | 318 |
318 scoped_refptr<AppCacheInfoCollection> collection_; | 319 scoped_refptr<AppCacheInfoCollection> collection_; |
319 | 320 |
320 DISALLOW_COPY_AND_ASSIGN(GetInfoHelper); | 321 DISALLOW_COPY_AND_ASSIGN(GetInfoHelper); |
321 }; | 322 }; |
322 | 323 |
323 void AppCacheService::GetInfoHelper::OnAllInfo( | 324 void AppCacheService::GetInfoHelper::OnAllInfo( |
324 AppCacheInfoCollection* collection) { | 325 AppCacheInfoCollection* collection) { |
325 if (collection) | 326 if (collection) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 delete this; | 395 delete this; |
395 return; | 396 return; |
396 } | 397 } |
397 | 398 |
398 cache_ = group->newest_complete_cache(); | 399 cache_ = group->newest_complete_cache(); |
399 const AppCacheEntry* entry = cache_->GetEntryWithResponseId(response_id_); | 400 const AppCacheEntry* entry = cache_->GetEntryWithResponseId(response_id_); |
400 if (!entry) { | 401 if (!entry) { |
401 if (cache_->cache_id() == cache_id_) { | 402 if (cache_->cache_id() == cache_id_) { |
402 AppCacheHistograms::CountCheckResponseResult( | 403 AppCacheHistograms::CountCheckResponseResult( |
403 AppCacheHistograms::ENTRY_NOT_FOUND); | 404 AppCacheHistograms::ENTRY_NOT_FOUND); |
404 service_->DeleteAppCacheGroup(manifest_url_, NULL); | 405 service_->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback()); |
405 } else { | 406 } else { |
406 AppCacheHistograms::CountCheckResponseResult( | 407 AppCacheHistograms::CountCheckResponseResult( |
407 AppCacheHistograms::RESPONSE_OUT_OF_DATE); | 408 AppCacheHistograms::RESPONSE_OUT_OF_DATE); |
408 } | 409 } |
409 delete this; | 410 delete this; |
410 return; | 411 return; |
411 } | 412 } |
412 | 413 |
413 // Verify that we can read the response info and data. | 414 // Verify that we can read the response info and data. |
414 expected_total_size_ = entry->response_size(); | 415 expected_total_size_ = entry->response_size(); |
415 response_reader_.reset(service_->storage()->CreateResponseReader( | 416 response_reader_.reset(service_->storage()->CreateResponseReader( |
416 manifest_url_, group->group_id(), response_id_)); | 417 manifest_url_, group->group_id(), response_id_)); |
417 info_buffer_ = new HttpResponseInfoIOBuffer(); | 418 info_buffer_ = new HttpResponseInfoIOBuffer(); |
418 response_reader_->ReadInfo(info_buffer_, &read_info_callback_); | 419 response_reader_->ReadInfo(info_buffer_, &read_info_callback_); |
419 } | 420 } |
420 | 421 |
421 void AppCacheService::CheckResponseHelper::OnReadInfoComplete(int result) { | 422 void AppCacheService::CheckResponseHelper::OnReadInfoComplete(int result) { |
422 if (result < 0) { | 423 if (result < 0) { |
423 AppCacheHistograms::CountCheckResponseResult( | 424 AppCacheHistograms::CountCheckResponseResult( |
424 AppCacheHistograms::READ_HEADERS_ERROR); | 425 AppCacheHistograms::READ_HEADERS_ERROR); |
425 service_->DeleteAppCacheGroup(manifest_url_, NULL); | 426 service_->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback()); |
426 delete this; | 427 delete this; |
427 return; | 428 return; |
428 } | 429 } |
429 amount_headers_read_ = result; | 430 amount_headers_read_ = result; |
430 | 431 |
431 // Start reading the data. | 432 // Start reading the data. |
432 data_buffer_ = new net::IOBuffer(kIOBufferSize); | 433 data_buffer_ = new net::IOBuffer(kIOBufferSize); |
433 response_reader_->ReadData(data_buffer_, kIOBufferSize, | 434 response_reader_->ReadData(data_buffer_, kIOBufferSize, |
434 &read_data_callback_); | 435 &read_data_callback_); |
435 } | 436 } |
(...skipping 11 matching lines...) Expand all Loading... |
447 if (result < 0) | 448 if (result < 0) |
448 check_result = AppCacheHistograms::READ_DATA_ERROR; | 449 check_result = AppCacheHistograms::READ_DATA_ERROR; |
449 else if (info_buffer_->response_data_size != amount_data_read_ || | 450 else if (info_buffer_->response_data_size != amount_data_read_ || |
450 expected_total_size_ != amount_data_read_ + amount_headers_read_) | 451 expected_total_size_ != amount_data_read_ + amount_headers_read_) |
451 check_result = AppCacheHistograms::UNEXPECTED_DATA_SIZE; | 452 check_result = AppCacheHistograms::UNEXPECTED_DATA_SIZE; |
452 else | 453 else |
453 check_result = AppCacheHistograms::RESPONSE_OK; | 454 check_result = AppCacheHistograms::RESPONSE_OK; |
454 AppCacheHistograms::CountCheckResponseResult(check_result); | 455 AppCacheHistograms::CountCheckResponseResult(check_result); |
455 | 456 |
456 if (check_result != AppCacheHistograms::RESPONSE_OK) | 457 if (check_result != AppCacheHistograms::RESPONSE_OK) |
457 service_->DeleteAppCacheGroup(manifest_url_, NULL); | 458 service_->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback()); |
458 delete this; | 459 delete this; |
459 } | 460 } |
460 | 461 |
461 | 462 |
462 // AppCacheService ------- | 463 // AppCacheService ------- |
463 | 464 |
464 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy) | 465 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy) |
465 : appcache_policy_(NULL), quota_client_(NULL), | 466 : appcache_policy_(NULL), quota_client_(NULL), |
466 quota_manager_proxy_(quota_manager_proxy), | 467 quota_manager_proxy_(quota_manager_proxy), |
467 request_context_(NULL), clear_local_state_on_exit_(false) { | 468 request_context_(NULL), clear_local_state_on_exit_(false) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } | 509 } |
509 | 510 |
510 void AppCacheService::GetAllAppCacheInfo( | 511 void AppCacheService::GetAllAppCacheInfo( |
511 AppCacheInfoCollection* collection, | 512 AppCacheInfoCollection* collection, |
512 const net::CompletionCallback& callback) { | 513 const net::CompletionCallback& callback) { |
513 DCHECK(collection); | 514 DCHECK(collection); |
514 GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); | 515 GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); |
515 helper->Start(); | 516 helper->Start(); |
516 } | 517 } |
517 | 518 |
518 void AppCacheService::DeleteAppCacheGroup(const GURL& manifest_url, | 519 void AppCacheService::DeleteAppCacheGroup( |
519 net::OldCompletionCallback* callback)
{ | 520 const GURL& manifest_url, |
| 521 const net::CompletionCallback& callback) { |
520 DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback); | 522 DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback); |
521 helper->Start(); | 523 helper->Start(); |
522 } | 524 } |
523 | 525 |
524 void AppCacheService::DeleteAppCachesForOrigin( | 526 void AppCacheService::DeleteAppCachesForOrigin( |
525 const GURL& origin, net::OldCompletionCallback* callback) { | 527 const GURL& origin, net::OldCompletionCallback* callback) { |
526 DeleteOriginHelper* helper = new DeleteOriginHelper(this, origin, callback); | 528 DeleteOriginHelper* helper = new DeleteOriginHelper(this, origin, callback); |
527 helper->Start(); | 529 helper->Start(); |
528 } | 530 } |
529 | 531 |
(...skipping 16 matching lines...) Expand all Loading... |
546 backends_.insert( | 548 backends_.insert( |
547 BackendMap::value_type(backend_impl->process_id(), backend_impl)); | 549 BackendMap::value_type(backend_impl->process_id(), backend_impl)); |
548 } | 550 } |
549 | 551 |
550 void AppCacheService::UnregisterBackend( | 552 void AppCacheService::UnregisterBackend( |
551 AppCacheBackendImpl* backend_impl) { | 553 AppCacheBackendImpl* backend_impl) { |
552 backends_.erase(backend_impl->process_id()); | 554 backends_.erase(backend_impl->process_id()); |
553 } | 555 } |
554 | 556 |
555 } // namespace appcache | 557 } // namespace appcache |
OLD | NEW |