Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(753)

Side by Side Diff: content/browser/cache_storage/cache_storage_cache.cc

Issue 1113303003: CacheStorage: Support multiple batch operations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove_responses
Patch Set: add comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(CACHE_STORAGE_ERROR_STORAGE);
412 return;
413 }
414 blob_data_handle =
415 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid);
416 if (!blob_data_handle) {
417 callback.Run(CACHE_STORAGE_ERROR_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(CACHE_STORAGE_ERROR_STORAGE, 412 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
446 scoped_ptr<ServiceWorkerResponse>(), 413 scoped_ptr<ServiceWorkerResponse>(),
447 scoped_ptr<storage::BlobDataHandle>()); 414 scoped_ptr<storage::BlobDataHandle>());
448 return; 415 return;
449 case BACKEND_OPEN: 416 case BACKEND_OPEN:
450 DCHECK(backend_); 417 DCHECK(backend_);
451 break; 418 break;
452 } 419 }
453 420
454 ResponseCallback pending_callback = 421 ResponseCallback pending_callback =
455 base::Bind(&CacheStorageCache::PendingResponseCallback, 422 base::Bind(&CacheStorageCache::PendingResponseCallback,
456 weak_ptr_factory_.GetWeakPtr(), callback); 423 weak_ptr_factory_.GetWeakPtr(), callback);
457 scheduler_->ScheduleOperation( 424 scheduler_->ScheduleOperation(
458 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), 425 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(),
459 base::Passed(request.Pass()), pending_callback)); 426 base::Passed(request.Pass()), pending_callback));
460 } 427 }
461 428
462 void CacheStorageCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request, 429 void CacheStorageCache::BatchOperation(
463 const ErrorCallback& callback) { 430 const std::vector<CacheStorageBatchOperation>& operations,
431 const ErrorCallback& callback) {
464 switch (backend_state_) { 432 switch (backend_state_) {
465 case BACKEND_UNINITIALIZED: 433 case BACKEND_UNINITIALIZED:
466 InitBackend(); 434 InitBackend();
467 break; 435 break;
468 case BACKEND_CLOSED: 436 case BACKEND_CLOSED:
469 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 437 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
470 return; 438 return;
471 case BACKEND_OPEN: 439 case BACKEND_OPEN:
472 DCHECK(backend_); 440 DCHECK(backend_);
473 break; 441 break;
474 } 442 }
475 ErrorCallback pending_callback = 443
476 base::Bind(&CacheStorageCache::PendingErrorCallback, 444 scoped_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback));
477 weak_ptr_factory_.GetWeakPtr(), callback); 445 ErrorCallback* callback_ptr = callback_copy.get();
478 scheduler_->ScheduleOperation( 446 base::Closure barrier_closure = base::BarrierClosure(
479 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), 447 operations.size(), base::Bind(&CacheStorageCache::BatchDidAllOperations,
480 base::Passed(request.Pass()), pending_callback)); 448 this, base::Passed(callback_copy.Pass())));
449 ErrorCallback completion_callback =
450 base::Bind(&CacheStorageCache::BatchDidOneOperation, this,
451 barrier_closure, callback_ptr);
452
453 for (const auto& operation : operations) {
454 switch (operation.operation_type) {
455 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT:
456 Put(operation, completion_callback);
457 break;
458 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE:
459 DCHECK_EQ(1u, operations.size());
460 Delete(operation, completion_callback);
461 break;
462 case CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED:
463 NOTREACHED();
464 // TODO(nhiroki): This should return "TypeError".
465 // http://crbug.com/425505
466 completion_callback.Run(CACHE_STORAGE_ERROR_STORAGE);
467 break;
468 }
469 }
470 }
471
472 void CacheStorageCache::BatchDidOneOperation(
473 const base::Closure& barrier_closure,
474 ErrorCallback* callback,
475 CacheStorageError error) {
476 if (callback->is_null() || error == CACHE_STORAGE_OK) {
477 barrier_closure.Run();
478 return;
479 }
480 callback->Run(error);
481 callback->Reset(); // Only call the callback once.
482
483 barrier_closure.Run();
484 }
485
486 void CacheStorageCache::BatchDidAllOperations(
487 scoped_ptr<ErrorCallback> callback) {
488 if (callback->is_null())
489 return;
490 callback->Run(CACHE_STORAGE_OK);
481 } 491 }
482 492
483 void CacheStorageCache::Keys(const RequestsCallback& callback) { 493 void CacheStorageCache::Keys(const RequestsCallback& callback) {
484 switch (backend_state_) { 494 switch (backend_state_) {
485 case BACKEND_UNINITIALIZED: 495 case BACKEND_UNINITIALIZED:
486 InitBackend(); 496 InitBackend();
487 break; 497 break;
488 case BACKEND_CLOSED: 498 case BACKEND_CLOSED:
489 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); 499 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>());
490 return; 500 return;
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 749
740 scoped_ptr<storage::BlobDataHandle> blob_data_handle( 750 scoped_ptr<storage::BlobDataHandle> blob_data_handle(
741 match_context->blob_storage_context->AddFinishedBlob( 751 match_context->blob_storage_context->AddFinishedBlob(
742 match_context->blob_data.get())); 752 match_context->blob_data.get()));
743 753
744 match_context->original_callback.Run(CACHE_STORAGE_OK, 754 match_context->original_callback.Run(CACHE_STORAGE_OK,
745 match_context->response.Pass(), 755 match_context->response.Pass(),
746 blob_data_handle.Pass()); 756 blob_data_handle.Pass());
747 } 757 }
748 758
759 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation,
760 const ErrorCallback& callback) {
761 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
762 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type);
763
764 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest(
765 operation.request.url, operation.request.method,
766 operation.request.headers, operation.request.referrer,
767 operation.request.is_reload));
768
769 // We don't support streaming for cache.
770 DCHECK(operation.response.stream_url.is_empty());
771 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse(
772 operation.response.url, operation.response.status_code,
773 operation.response.status_text, operation.response.response_type,
774 operation.response.headers, operation.response.blob_uuid,
775 operation.response.blob_size, operation.response.stream_url));
776
777 scoped_ptr<storage::BlobDataHandle> blob_data_handle;
778
779 if (!response->blob_uuid.empty()) {
780 if (!blob_storage_context_) {
781 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
782 return;
783 }
784 blob_data_handle =
785 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid);
786 if (!blob_data_handle) {
787 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
788 return;
789 }
790 }
791
792 ErrorCallback pending_callback =
793 base::Bind(&CacheStorageCache::PendingErrorCallback,
794 weak_ptr_factory_.GetWeakPtr(), callback);
795
796 scoped_ptr<PutContext> put_context(new PutContext(
797 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(),
798 pending_callback, request_context_, quota_manager_proxy_));
799
800 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::PutImpl,
801 weak_ptr_factory_.GetWeakPtr(),
802 base::Passed(put_context.Pass())));
803 }
804
749 void CacheStorageCache::PutImpl(scoped_ptr<PutContext> put_context) { 805 void CacheStorageCache::PutImpl(scoped_ptr<PutContext> put_context) {
750 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); 806 DCHECK(backend_state_ != BACKEND_UNINITIALIZED);
751 if (backend_state_ != BACKEND_OPEN) { 807 if (backend_state_ != BACKEND_OPEN) {
752 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 808 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
753 return; 809 return;
754 } 810 }
755 811
756 scoped_ptr<ServiceWorkerFetchRequest> request_copy( 812 scoped_ptr<ServiceWorkerFetchRequest> request_copy(
757 new ServiceWorkerFetchRequest(*put_context->request)); 813 new ServiceWorkerFetchRequest(*put_context->request));
758 814
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 put_context->quota_manager_proxy->NotifyStorageModified( 963 put_context->quota_manager_proxy->NotifyStorageModified(
908 storage::QuotaClient::kServiceWorkerCache, put_context->origin, 964 storage::QuotaClient::kServiceWorkerCache, put_context->origin,
909 storage::kStorageTypeTemporary, 965 storage::kStorageTypeTemporary,
910 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + 966 put_context->cache_entry->GetDataSize(INDEX_HEADERS) +
911 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); 967 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY));
912 } 968 }
913 969
914 put_context->callback.Run(CACHE_STORAGE_OK); 970 put_context->callback.Run(CACHE_STORAGE_OK);
915 } 971 }
916 972
973 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
974 const ErrorCallback& callback) {
975 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
976 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
977 operation.operation_type);
978
979 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest(
980 operation.request.url, operation.request.method,
981 operation.request.headers, operation.request.referrer,
982 operation.request.is_reload));
983
984 ErrorCallback pending_callback =
985 base::Bind(&CacheStorageCache::PendingErrorCallback,
986 weak_ptr_factory_.GetWeakPtr(), callback);
987 scheduler_->ScheduleOperation(
988 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(),
989 base::Passed(request.Pass()), pending_callback));
990 }
991
917 void CacheStorageCache::DeleteImpl( 992 void CacheStorageCache::DeleteImpl(
918 scoped_ptr<ServiceWorkerFetchRequest> request, 993 scoped_ptr<ServiceWorkerFetchRequest> request,
919 const ErrorCallback& callback) { 994 const ErrorCallback& callback) {
920 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); 995 DCHECK(backend_state_ != BACKEND_UNINITIALIZED);
921 if (backend_state_ != BACKEND_OPEN) { 996 if (backend_state_ != BACKEND_OPEN) {
922 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 997 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
923 return; 998 return;
924 } 999 }
925 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); 1000 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*);
926 1001
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
1191 CacheStorageError error, 1266 CacheStorageError error,
1192 scoped_ptr<Requests> requests) { 1267 scoped_ptr<Requests> requests) {
1193 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); 1268 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1194 1269
1195 callback.Run(error, requests.Pass()); 1270 callback.Run(error, requests.Pass());
1196 if (cache) 1271 if (cache)
1197 scheduler_->CompleteOperationAndRunNext(); 1272 scheduler_->CompleteOperationAndRunNext();
1198 } 1273 }
1199 1274
1200 } // namespace content 1275 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache.h ('k') | content/browser/cache_storage/cache_storage_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698