OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/barrier_closure.h" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/guid.h" | 11 #include "base/guid.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "content/browser/cache_storage/cache_storage.pb.h" | 15 #include "content/browser/cache_storage/cache_storage.pb.h" |
15 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 16 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/common/referrer.h" | 18 #include "content/public/common/referrer.h" |
18 #include "net/base/completion_callback.h" | 19 #include "net/base/completion_callback.h" |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 origin, path, request_context, quota_manager_proxy, blob_context)); | 395 origin, path, request_context, quota_manager_proxy, blob_context)); |
395 } | 396 } |
396 | 397 |
397 CacheStorageCache::~CacheStorageCache() { | 398 CacheStorageCache::~CacheStorageCache() { |
398 } | 399 } |
399 | 400 |
400 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { | 401 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { |
401 return weak_ptr_factory_.GetWeakPtr(); | 402 return weak_ptr_factory_.GetWeakPtr(); |
402 } | 403 } |
403 | 404 |
404 void CacheStorageCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, | |
405 scoped_ptr<ServiceWorkerResponse> response, | |
406 const ErrorCallback& callback) { | |
407 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | |
408 | |
409 if (!response->blob_uuid.empty()) { | |
410 if (!blob_storage_context_) { | |
411 callback.Run(ERROR_TYPE_STORAGE); | |
412 return; | |
413 } | |
414 blob_data_handle = | |
415 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); | |
416 if (!blob_data_handle) { | |
417 callback.Run(ERROR_TYPE_STORAGE); | |
418 return; | |
419 } | |
420 } | |
421 | |
422 ErrorCallback pending_callback = | |
423 base::Bind(&CacheStorageCache::PendingErrorCallback, | |
424 weak_ptr_factory_.GetWeakPtr(), callback); | |
425 | |
426 scoped_ptr<PutContext> put_context(new PutContext( | |
427 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(), | |
428 pending_callback, request_context_, quota_manager_proxy_)); | |
429 | |
430 if (backend_state_ == BACKEND_UNINITIALIZED) | |
431 InitBackend(); | |
432 | |
433 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::PutImpl, | |
434 weak_ptr_factory_.GetWeakPtr(), | |
435 base::Passed(put_context.Pass()))); | |
436 } | |
437 | |
438 void CacheStorageCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 405 void CacheStorageCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
439 const ResponseCallback& callback) { | 406 const ResponseCallback& callback) { |
440 switch (backend_state_) { | 407 switch (backend_state_) { |
441 case BACKEND_UNINITIALIZED: | 408 case BACKEND_UNINITIALIZED: |
442 InitBackend(); | 409 InitBackend(); |
443 break; | 410 break; |
444 case BACKEND_CLOSED: | 411 case BACKEND_CLOSED: |
445 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), | 412 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), |
446 scoped_ptr<storage::BlobDataHandle>()); | 413 scoped_ptr<storage::BlobDataHandle>()); |
447 return; | 414 return; |
448 case BACKEND_OPEN: | 415 case BACKEND_OPEN: |
449 DCHECK(backend_); | 416 DCHECK(backend_); |
450 break; | 417 break; |
451 } | 418 } |
452 | 419 |
453 ResponseCallback pending_callback = | 420 ResponseCallback pending_callback = |
454 base::Bind(&CacheStorageCache::PendingResponseCallback, | 421 base::Bind(&CacheStorageCache::PendingResponseCallback, |
455 weak_ptr_factory_.GetWeakPtr(), callback); | 422 weak_ptr_factory_.GetWeakPtr(), callback); |
456 scheduler_->ScheduleOperation( | 423 scheduler_->ScheduleOperation( |
457 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), | 424 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), |
458 base::Passed(request.Pass()), pending_callback)); | 425 base::Passed(request.Pass()), pending_callback)); |
459 } | 426 } |
460 | 427 |
461 void CacheStorageCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 428 void CacheStorageCache::BatchOperation( |
462 const ErrorCallback& callback) { | 429 const std::vector<CacheStorageBatchOperation>& operations, |
| 430 const ErrorCallback& callback) { |
463 switch (backend_state_) { | 431 switch (backend_state_) { |
464 case BACKEND_UNINITIALIZED: | 432 case BACKEND_UNINITIALIZED: |
465 InitBackend(); | 433 InitBackend(); |
466 break; | 434 break; |
467 case BACKEND_CLOSED: | 435 case BACKEND_CLOSED: |
468 callback.Run(ERROR_TYPE_STORAGE); | 436 callback.Run(ERROR_TYPE_STORAGE); |
469 return; | 437 return; |
470 case BACKEND_OPEN: | 438 case BACKEND_OPEN: |
471 DCHECK(backend_); | 439 DCHECK(backend_); |
472 break; | 440 break; |
473 } | 441 } |
474 ErrorCallback pending_callback = | 442 |
475 base::Bind(&CacheStorageCache::PendingErrorCallback, | 443 scoped_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback)); |
476 weak_ptr_factory_.GetWeakPtr(), callback); | 444 ErrorCallback* callback_ptr = callback_copy.get(); |
477 scheduler_->ScheduleOperation( | 445 base::Closure barrier_closure = base::BarrierClosure( |
478 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), | 446 operations.size(), base::Bind(&CacheStorageCache::BatchDidAllOperations, |
479 base::Passed(request.Pass()), pending_callback)); | 447 this, base::Passed(callback_copy.Pass()))); |
| 448 ErrorCallback completion_callback = |
| 449 base::Bind(&CacheStorageCache::BatchDidOperation, this, barrier_closure, |
| 450 callback_ptr); |
| 451 |
| 452 for (const auto& operation : operations) { |
| 453 switch (operation.operation_type) { |
| 454 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT: |
| 455 Put(operation, completion_callback); |
| 456 break; |
| 457 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE: |
| 458 DCHECK_EQ(1u, operations.size()); |
| 459 Delete(operation, completion_callback); |
| 460 break; |
| 461 case CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED: |
| 462 NOTREACHED(); |
| 463 // TODO(nhiroki): This should return "TypeError". |
| 464 // http://crbug.com/425505 |
| 465 completion_callback.Run(ERROR_TYPE_STORAGE); |
| 466 break; |
| 467 } |
| 468 } |
| 469 } |
| 470 |
| 471 void CacheStorageCache::BatchDidOperation(const base::Closure& barrier_closure, |
| 472 ErrorCallback* callback, |
| 473 ErrorType error) { |
| 474 if (callback->is_null() || error == ERROR_TYPE_OK) { |
| 475 barrier_closure.Run(); |
| 476 return; |
| 477 } |
| 478 callback->Run(error); |
| 479 callback->Reset(); // Only call the callback once. |
| 480 |
| 481 barrier_closure.Run(); |
| 482 } |
| 483 |
| 484 void CacheStorageCache::BatchDidAllOperations( |
| 485 scoped_ptr<ErrorCallback> callback) { |
| 486 if (callback->is_null()) |
| 487 return; |
| 488 callback->Run(ERROR_TYPE_OK); |
480 } | 489 } |
481 | 490 |
482 void CacheStorageCache::Keys(const RequestsCallback& callback) { | 491 void CacheStorageCache::Keys(const RequestsCallback& callback) { |
483 switch (backend_state_) { | 492 switch (backend_state_) { |
484 case BACKEND_UNINITIALIZED: | 493 case BACKEND_UNINITIALIZED: |
485 InitBackend(); | 494 InitBackend(); |
486 break; | 495 break; |
487 case BACKEND_CLOSED: | 496 case BACKEND_CLOSED: |
488 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<Requests>()); | 497 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<Requests>()); |
489 return; | 498 return; |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 | 748 |
740 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | 749 scoped_ptr<storage::BlobDataHandle> blob_data_handle( |
741 match_context->blob_storage_context->AddFinishedBlob( | 750 match_context->blob_storage_context->AddFinishedBlob( |
742 match_context->blob_data.get())); | 751 match_context->blob_data.get())); |
743 | 752 |
744 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_OK, | 753 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_OK, |
745 match_context->response.Pass(), | 754 match_context->response.Pass(), |
746 blob_data_handle.Pass()); | 755 blob_data_handle.Pass()); |
747 } | 756 } |
748 | 757 |
| 758 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, |
| 759 const ErrorCallback& callback) { |
| 760 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); |
| 761 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); |
| 762 |
| 763 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( |
| 764 operation.request.url, operation.request.method, |
| 765 operation.request.headers, operation.request.referrer, |
| 766 operation.request.is_reload)); |
| 767 |
| 768 // We don't support streaming for cache. |
| 769 DCHECK(operation.response.stream_url.is_empty()); |
| 770 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse( |
| 771 operation.response.url, operation.response.status_code, |
| 772 operation.response.status_text, operation.response.response_type, |
| 773 operation.response.headers, operation.response.blob_uuid, |
| 774 operation.response.blob_size, operation.response.stream_url)); |
| 775 |
| 776 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
| 777 |
| 778 if (!response->blob_uuid.empty()) { |
| 779 if (!blob_storage_context_) { |
| 780 callback.Run(ERROR_TYPE_STORAGE); |
| 781 return; |
| 782 } |
| 783 blob_data_handle = |
| 784 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); |
| 785 if (!blob_data_handle) { |
| 786 callback.Run(ERROR_TYPE_STORAGE); |
| 787 return; |
| 788 } |
| 789 } |
| 790 |
| 791 ErrorCallback pending_callback = |
| 792 base::Bind(&CacheStorageCache::PendingErrorCallback, |
| 793 weak_ptr_factory_.GetWeakPtr(), callback); |
| 794 |
| 795 scoped_ptr<PutContext> put_context(new PutContext( |
| 796 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(), |
| 797 pending_callback, request_context_, quota_manager_proxy_)); |
| 798 |
| 799 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::PutImpl, |
| 800 weak_ptr_factory_.GetWeakPtr(), |
| 801 base::Passed(put_context.Pass()))); |
| 802 } |
| 803 |
749 void CacheStorageCache::PutImpl(scoped_ptr<PutContext> put_context) { | 804 void CacheStorageCache::PutImpl(scoped_ptr<PutContext> put_context) { |
750 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); | 805 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); |
751 if (backend_state_ != BACKEND_OPEN) { | 806 if (backend_state_ != BACKEND_OPEN) { |
752 put_context->callback.Run(ERROR_TYPE_STORAGE); | 807 put_context->callback.Run(ERROR_TYPE_STORAGE); |
753 return; | 808 return; |
754 } | 809 } |
755 | 810 |
756 scoped_ptr<ServiceWorkerFetchRequest> request_copy( | 811 scoped_ptr<ServiceWorkerFetchRequest> request_copy( |
757 new ServiceWorkerFetchRequest(*put_context->request)); | 812 new ServiceWorkerFetchRequest(*put_context->request)); |
758 | 813 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 put_context->quota_manager_proxy->NotifyStorageModified( | 962 put_context->quota_manager_proxy->NotifyStorageModified( |
908 storage::QuotaClient::kServiceWorkerCache, put_context->origin, | 963 storage::QuotaClient::kServiceWorkerCache, put_context->origin, |
909 storage::kStorageTypeTemporary, | 964 storage::kStorageTypeTemporary, |
910 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + | 965 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + |
911 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); | 966 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); |
912 } | 967 } |
913 | 968 |
914 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_OK); | 969 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_OK); |
915 } | 970 } |
916 | 971 |
| 972 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, |
| 973 const ErrorCallback& callback) { |
| 974 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); |
| 975 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, |
| 976 operation.operation_type); |
| 977 |
| 978 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( |
| 979 operation.request.url, operation.request.method, |
| 980 operation.request.headers, operation.request.referrer, |
| 981 operation.request.is_reload)); |
| 982 |
| 983 ErrorCallback pending_callback = |
| 984 base::Bind(&CacheStorageCache::PendingErrorCallback, |
| 985 weak_ptr_factory_.GetWeakPtr(), callback); |
| 986 scheduler_->ScheduleOperation( |
| 987 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), |
| 988 base::Passed(request.Pass()), pending_callback)); |
| 989 } |
| 990 |
917 void CacheStorageCache::DeleteImpl( | 991 void CacheStorageCache::DeleteImpl( |
918 scoped_ptr<ServiceWorkerFetchRequest> request, | 992 scoped_ptr<ServiceWorkerFetchRequest> request, |
919 const ErrorCallback& callback) { | 993 const ErrorCallback& callback) { |
920 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); | 994 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); |
921 if (backend_state_ != BACKEND_OPEN) { | 995 if (backend_state_ != BACKEND_OPEN) { |
922 callback.Run(ERROR_TYPE_STORAGE); | 996 callback.Run(ERROR_TYPE_STORAGE); |
923 return; | 997 return; |
924 } | 998 } |
925 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); | 999 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); |
926 | 1000 |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1191 ErrorType error, | 1265 ErrorType error, |
1192 scoped_ptr<Requests> requests) { | 1266 scoped_ptr<Requests> requests) { |
1193 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1267 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
1194 | 1268 |
1195 callback.Run(error, requests.Pass()); | 1269 callback.Run(error, requests.Pass()); |
1196 if (cache) | 1270 if (cache) |
1197 scheduler_->CompleteOperationAndRunNext(); | 1271 scheduler_->CompleteOperationAndRunNext(); |
1198 } | 1272 } |
1199 | 1273 |
1200 } // namespace content | 1274 } // namespace content |
OLD | NEW |