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

Side by Side Diff: content/browser/service_worker/service_worker_cache.cc

Issue 810403004: [Storage] Blob Storage Refactoring pt 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: memory leak fixed Created 5 years, 11 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/service_worker/service_worker_cache.h" 5 #include "content/browser/service_worker/service_worker_cache.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/guid.h" 10 #include "base/guid.h"
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 public: 176 public:
177 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> 177 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)>
178 EntryAndBoolCallback; 178 EntryAndBoolCallback;
179 179
180 BlobReader() 180 BlobReader()
181 : cache_entry_offset_(0), 181 : cache_entry_offset_(0),
182 buffer_(new net::IOBufferWithSize(kBufferSize)), 182 buffer_(new net::IOBufferWithSize(kBufferSize)),
183 weak_ptr_factory_(this) {} 183 weak_ptr_factory_(this) {}
184 184
185 // |entry| is passed to the callback once complete. 185 // |entry| is passed to the callback once complete.
186 void StreamBlobToCache(disk_cache::ScopedEntryPtr entry, 186 void StreamBlobToCache(
187 net::URLRequestContext* request_context, 187 disk_cache::ScopedEntryPtr entry,
188 scoped_ptr<storage::BlobDataHandle> blob_data_handle, 188 net::URLRequestContext* request_context,
189 const EntryAndBoolCallback& callback) { 189 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle,
190 const EntryAndBoolCallback& callback) {
190 DCHECK(entry); 191 DCHECK(entry);
191 entry_ = entry.Pass(); 192 entry_ = entry.Pass();
192 callback_ = callback; 193 callback_ = callback;
193 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( 194 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest(
194 blob_data_handle.Pass(), request_context, this); 195 blob_data_handle.Pass(), request_context, this);
195 blob_request_->Start(); 196 blob_request_->Start();
196 } 197 }
197 198
198 // net::URLRequest::Delegate overrides for reading blobs. 199 // net::URLRequest::Delegate overrides for reading blobs.
199 void OnReceivedRedirect(net::URLRequest* request, 200 void OnReceivedRedirect(net::URLRequest* request,
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 } 337 }
337 338
338 // Input 339 // Input
339 scoped_ptr<ServiceWorkerFetchRequest> request; 340 scoped_ptr<ServiceWorkerFetchRequest> request;
340 ServiceWorkerCache::ResponseCallback original_callback; 341 ServiceWorkerCache::ResponseCallback original_callback;
341 base::WeakPtr<storage::BlobStorageContext> blob_storage_context; 342 base::WeakPtr<storage::BlobStorageContext> blob_storage_context;
342 disk_cache::Entry* entry; 343 disk_cache::Entry* entry;
343 344
344 // Output 345 // Output
345 scoped_ptr<ServiceWorkerResponse> response; 346 scoped_ptr<ServiceWorkerResponse> response;
346 scoped_refptr<storage::BlobData> blob_data; 347 scoped_ptr<storage::BlobDataBuilder> blob_data;
347 348
348 // For reading the cache entry data into a blob. 349 // For reading the cache entry data into a blob.
349 scoped_refptr<net::IOBufferWithSize> response_body_buffer; 350 scoped_refptr<net::IOBufferWithSize> response_body_buffer;
350 size_t total_bytes_read; 351 size_t total_bytes_read;
351 352
352 DISALLOW_COPY_AND_ASSIGN(MatchContext); 353 DISALLOW_COPY_AND_ASSIGN(MatchContext);
353 }; 354 };
354 355
355 // The state needed to pass between ServiceWorkerCache::Put callbacks. 356 // The state needed to pass between ServiceWorkerCache::Put callbacks.
356 struct ServiceWorkerCache::PutContext { 357 struct ServiceWorkerCache::PutContext {
357 PutContext( 358 PutContext(
358 const GURL& origin, 359 const GURL& origin,
359 scoped_ptr<ServiceWorkerFetchRequest> request, 360 scoped_ptr<ServiceWorkerFetchRequest> request,
360 scoped_ptr<ServiceWorkerResponse> response, 361 scoped_ptr<ServiceWorkerResponse> response,
361 scoped_ptr<storage::BlobDataHandle> blob_data_handle, 362 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle,
362 const ServiceWorkerCache::ResponseCallback& callback, 363 const ServiceWorkerCache::ResponseCallback& callback,
363 net::URLRequestContext* request_context, 364 net::URLRequestContext* request_context,
364 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) 365 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy)
365 : origin(origin), 366 : origin(origin),
366 request(request.Pass()), 367 request(request.Pass()),
367 response(response.Pass()), 368 response(response.Pass()),
368 blob_data_handle(blob_data_handle.Pass()), 369 blob_data_handle(blob_data_handle.Pass()),
369 callback(callback), 370 callback(callback),
370 request_context(request_context), 371 request_context(request_context),
371 quota_manager_proxy(quota_manager_proxy), 372 quota_manager_proxy(quota_manager_proxy),
372 cache_entry(NULL) {} 373 cache_entry(NULL) {}
373 ~PutContext() { 374 ~PutContext() {
374 if (cache_entry) 375 if (cache_entry)
375 cache_entry->Close(); 376 cache_entry->Close();
376 } 377 }
377 378
378 // Input parameters to the Put function. 379 // Input parameters to the Put function.
379 GURL origin; 380 GURL origin;
380 scoped_ptr<ServiceWorkerFetchRequest> request; 381 scoped_ptr<ServiceWorkerFetchRequest> request;
381 scoped_ptr<ServiceWorkerResponse> response; 382 scoped_ptr<ServiceWorkerResponse> response;
382 scoped_ptr<storage::BlobDataHandle> blob_data_handle; 383 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle;
383 ServiceWorkerCache::ResponseCallback callback; 384 ServiceWorkerCache::ResponseCallback callback;
384 net::URLRequestContext* request_context; 385 net::URLRequestContext* request_context;
385 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; 386 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy;
386 387
387 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to 388 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to
388 // CreateEntry. 389 // CreateEntry.
389 disk_cache::Entry* cache_entry; 390 disk_cache::Entry* cache_entry;
390 391
391 // The BlobDataHandle for the output ServiceWorkerResponse. 392 // The BlobDataHandle for the output ServiceWorkerResponse.
392 scoped_ptr<storage::BlobDataHandle> out_blob_data_handle; 393 scoped_ptr<storage::BlobDataSnapshotHandle> out_blob_data_handle;
393 394
394 DISALLOW_COPY_AND_ASSIGN(PutContext); 395 DISALLOW_COPY_AND_ASSIGN(PutContext);
395 }; 396 };
396 397
397 // static 398 // static
398 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( 399 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache(
399 const GURL& origin, 400 const GURL& origin,
400 net::URLRequestContext* request_context, 401 net::URLRequestContext* request_context,
401 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 402 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
402 base::WeakPtr<storage::BlobStorageContext> blob_context) { 403 base::WeakPtr<storage::BlobStorageContext> blob_context) {
(...skipping 22 matching lines...) Expand all
425 return weak_ptr_factory_.GetWeakPtr(); 426 return weak_ptr_factory_.GetWeakPtr();
426 } 427 }
427 428
428 void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, 429 void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request,
429 scoped_ptr<ServiceWorkerResponse> response, 430 scoped_ptr<ServiceWorkerResponse> response,
430 const ResponseCallback& callback) { 431 const ResponseCallback& callback) {
431 IncPendingOps(); 432 IncPendingOps();
432 ResponseCallback pending_callback = 433 ResponseCallback pending_callback =
433 base::Bind(&ServiceWorkerCache::PendingResponseCallback, 434 base::Bind(&ServiceWorkerCache::PendingResponseCallback,
434 weak_ptr_factory_.GetWeakPtr(), callback); 435 weak_ptr_factory_.GetWeakPtr(), callback);
435 scoped_ptr<storage::BlobDataHandle> blob_data_handle; 436 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle;
436 437
437 if (!response->blob_uuid.empty()) { 438 if (!response->blob_uuid.empty()) {
438 if (!blob_storage_context_) { 439 if (!blob_storage_context_) {
439 pending_callback.Run(ErrorTypeStorage, 440 pending_callback.Run(ErrorTypeStorage,
440 scoped_ptr<ServiceWorkerResponse>(), 441 scoped_ptr<ServiceWorkerResponse>(),
441 scoped_ptr<storage::BlobDataHandle>()); 442 scoped_ptr<storage::BlobDataSnapshotHandle>());
442 return; 443 return;
443 } 444 }
444 blob_data_handle = 445 blob_data_handle =
445 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); 446 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid);
446 if (!blob_data_handle) { 447 if (!blob_data_handle) {
447 pending_callback.Run(ErrorTypeStorage, 448 pending_callback.Run(ErrorTypeStorage,
448 scoped_ptr<ServiceWorkerResponse>(), 449 scoped_ptr<ServiceWorkerResponse>(),
449 scoped_ptr<storage::BlobDataHandle>()); 450 scoped_ptr<storage::BlobDataSnapshotHandle>());
450 return; 451 return;
451 } 452 }
452 } 453 }
453 454
454 scoped_ptr<PutContext> put_context(new PutContext( 455 scoped_ptr<PutContext> put_context(new PutContext(
455 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(), 456 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(),
456 pending_callback, request_context_, quota_manager_proxy_)); 457 pending_callback, request_context_, quota_manager_proxy_));
457 458
458 if (put_context->blob_data_handle) { 459 if (put_context->blob_data_handle) {
459 // Grab another handle to the blob for the callback response. 460 // Grab another handle to the blob for the callback response.
(...skipping 23 matching lines...) Expand all
483 484
484 switch (backend_state_) { 485 switch (backend_state_) {
485 case BACKEND_UNINITIALIZED: 486 case BACKEND_UNINITIALIZED:
486 InitBackend(base::Bind(&ServiceWorkerCache::Match, 487 InitBackend(base::Bind(&ServiceWorkerCache::Match,
487 weak_ptr_factory_.GetWeakPtr(), 488 weak_ptr_factory_.GetWeakPtr(),
488 base::Passed(request.Pass()), pending_callback)); 489 base::Passed(request.Pass()), pending_callback));
489 return; 490 return;
490 case BACKEND_CLOSED: 491 case BACKEND_CLOSED:
491 pending_callback.Run(ErrorTypeStorage, 492 pending_callback.Run(ErrorTypeStorage,
492 scoped_ptr<ServiceWorkerResponse>(), 493 scoped_ptr<ServiceWorkerResponse>(),
493 scoped_ptr<storage::BlobDataHandle>()); 494 scoped_ptr<storage::BlobDataSnapshotHandle>());
494 return; 495 return;
495 case BACKEND_OPEN: 496 case BACKEND_OPEN:
496 DCHECK(backend_); 497 DCHECK(backend_);
497 break; 498 break;
498 } 499 }
499 500
500 scoped_ptr<MatchContext> match_context(new MatchContext( 501 scoped_ptr<MatchContext> match_context(new MatchContext(
501 request.Pass(), pending_callback, blob_storage_context_)); 502 request.Pass(), pending_callback, blob_storage_context_));
502 503
503 disk_cache::Entry** entry_ptr = &match_context->entry; 504 disk_cache::Entry** entry_ptr = &match_context->entry;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 backend_state_(BACKEND_UNINITIALIZED), 655 backend_state_(BACKEND_UNINITIALIZED),
655 memory_only_(path.empty()), 656 memory_only_(path.empty()),
656 pending_ops_(0), 657 pending_ops_(0),
657 weak_ptr_factory_(this) { 658 weak_ptr_factory_(this) {
658 } 659 }
659 660
660 void ServiceWorkerCache::MatchDidOpenEntry( 661 void ServiceWorkerCache::MatchDidOpenEntry(
661 scoped_ptr<MatchContext> match_context, 662 scoped_ptr<MatchContext> match_context,
662 int rv) { 663 int rv) {
663 if (rv != net::OK) { 664 if (rv != net::OK) {
664 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeNotFound, 665 match_context->original_callback.Run(
665 scoped_ptr<ServiceWorkerResponse>(), 666 ServiceWorkerCache::ErrorTypeNotFound,
666 scoped_ptr<storage::BlobDataHandle>()); 667 scoped_ptr<ServiceWorkerResponse>(),
668 scoped_ptr<storage::BlobDataSnapshotHandle>());
667 return; 669 return;
668 } 670 }
669 671
670 // Copy the entry pointer before passing it in base::Bind. 672 // Copy the entry pointer before passing it in base::Bind.
671 disk_cache::Entry* tmp_entry_ptr = match_context->entry; 673 disk_cache::Entry* tmp_entry_ptr = match_context->entry;
672 DCHECK(tmp_entry_ptr); 674 DCHECK(tmp_entry_ptr);
673 675
674 MetadataCallback headers_callback = base::Bind( 676 MetadataCallback headers_callback = base::Bind(
675 &ServiceWorkerCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), 677 &ServiceWorkerCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(),
676 base::Passed(match_context.Pass())); 678 base::Passed(match_context.Pass()));
677 679
678 ReadMetadata(tmp_entry_ptr, headers_callback); 680 ReadMetadata(tmp_entry_ptr, headers_callback);
679 } 681 }
680 682
681 void ServiceWorkerCache::MatchDidReadMetadata( 683 void ServiceWorkerCache::MatchDidReadMetadata(
682 scoped_ptr<MatchContext> match_context, 684 scoped_ptr<MatchContext> match_context,
683 scoped_ptr<ServiceWorkerCacheMetadata> metadata) { 685 scoped_ptr<ServiceWorkerCacheMetadata> metadata) {
684 if (!metadata) { 686 if (!metadata) {
685 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeStorage, 687 match_context->original_callback.Run(
686 scoped_ptr<ServiceWorkerResponse>(), 688 ServiceWorkerCache::ErrorTypeStorage,
687 scoped_ptr<storage::BlobDataHandle>()); 689 scoped_ptr<ServiceWorkerResponse>(),
690 scoped_ptr<storage::BlobDataSnapshotHandle>());
688 return; 691 return;
689 } 692 }
690 693
691 match_context->response.reset(new ServiceWorkerResponse( 694 match_context->response.reset(new ServiceWorkerResponse(
692 match_context->request->url, metadata->response().status_code(), 695 match_context->request->url, metadata->response().status_code(),
693 metadata->response().status_text(), 696 metadata->response().status_text(),
694 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), 697 ProtoResponseTypeToWebResponseType(metadata->response().response_type()),
695 ServiceWorkerHeaderMap(), "", 0, GURL())); 698 ServiceWorkerHeaderMap(), "", 0, GURL()));
696 699
697 ServiceWorkerResponse* response = match_context->response.get(); 700 ServiceWorkerResponse* response = match_context->response.get();
698 701
699 if (metadata->response().has_url()) 702 if (metadata->response().has_url())
700 response->url = GURL(metadata->response().url()); 703 response->url = GURL(metadata->response().url());
701 704
702 for (int i = 0; i < metadata->response().headers_size(); ++i) { 705 for (int i = 0; i < metadata->response().headers_size(); ++i) {
703 const ServiceWorkerCacheHeaderMap header = metadata->response().headers(i); 706 const ServiceWorkerCacheHeaderMap header = metadata->response().headers(i);
704 response->headers.insert(std::make_pair(header.name(), header.value())); 707 response->headers.insert(std::make_pair(header.name(), header.value()));
705 } 708 }
706 709
707 ServiceWorkerHeaderMap cached_request_headers; 710 ServiceWorkerHeaderMap cached_request_headers;
708 for (int i = 0; i < metadata->request().headers_size(); ++i) { 711 for (int i = 0; i < metadata->request().headers_size(); ++i) {
709 const ServiceWorkerCacheHeaderMap header = metadata->request().headers(i); 712 const ServiceWorkerCacheHeaderMap header = metadata->request().headers(i);
710 cached_request_headers[header.name()] = header.value(); 713 cached_request_headers[header.name()] = header.value();
711 } 714 }
712 715
713 if (!VaryMatches(match_context->request->headers, cached_request_headers, 716 if (!VaryMatches(match_context->request->headers, cached_request_headers,
714 response->headers)) { 717 response->headers)) {
715 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeNotFound, 718 match_context->original_callback.Run(
716 scoped_ptr<ServiceWorkerResponse>(), 719 ServiceWorkerCache::ErrorTypeNotFound,
717 scoped_ptr<storage::BlobDataHandle>()); 720 scoped_ptr<ServiceWorkerResponse>(),
721 scoped_ptr<storage::BlobDataSnapshotHandle>());
718 return; 722 return;
719 } 723 }
720 724
721 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { 725 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
722 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeOK, 726 match_context->original_callback.Run(
723 match_context->response.Pass(), 727 ServiceWorkerCache::ErrorTypeOK, match_context->response.Pass(),
724 scoped_ptr<storage::BlobDataHandle>()); 728 scoped_ptr<storage::BlobDataSnapshotHandle>());
725 return; 729 return;
726 } 730 }
727 731
728 // Stream the response body into a blob. 732 // Stream the response body into a blob.
729 if (!match_context->blob_storage_context) { 733 if (!match_context->blob_storage_context) {
730 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeStorage, 734 match_context->original_callback.Run(
731 scoped_ptr<ServiceWorkerResponse>(), 735 ServiceWorkerCache::ErrorTypeStorage,
732 scoped_ptr<storage::BlobDataHandle>()); 736 scoped_ptr<ServiceWorkerResponse>(),
737 scoped_ptr<storage::BlobDataSnapshotHandle>());
733 return; 738 return;
734 } 739 }
735 740
736 response->blob_uuid = base::GenerateGUID(); 741 response->blob_uuid = base::GenerateGUID();
737 742
738 match_context->blob_data = new storage::BlobData(response->blob_uuid); 743 match_context->blob_data.reset(
744 new storage::BlobDataBuilder(response->blob_uuid));
739 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize); 745 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize);
740 746
741 disk_cache::Entry* tmp_entry_ptr = match_context->entry; 747 disk_cache::Entry* tmp_entry_ptr = match_context->entry;
742 net::IOBufferWithSize* response_body_buffer = 748 net::IOBufferWithSize* response_body_buffer =
743 match_context->response_body_buffer.get(); 749 match_context->response_body_buffer.get();
744 750
745 net::CompletionCallback read_callback = base::Bind( 751 net::CompletionCallback read_callback = base::Bind(
746 &ServiceWorkerCache::MatchDidReadResponseBodyData, 752 &ServiceWorkerCache::MatchDidReadResponseBodyData,
747 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); 753 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass()));
748 754
749 int read_rv = 755 int read_rv =
750 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer, 756 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer,
751 response_body_buffer->size(), read_callback); 757 response_body_buffer->size(), read_callback);
752 758
753 if (read_rv != net::ERR_IO_PENDING) 759 if (read_rv != net::ERR_IO_PENDING)
754 read_callback.Run(read_rv); 760 read_callback.Run(read_rv);
755 } 761 }
756 762
757 void ServiceWorkerCache::MatchDidReadResponseBodyData( 763 void ServiceWorkerCache::MatchDidReadResponseBodyData(
758 scoped_ptr<MatchContext> match_context, 764 scoped_ptr<MatchContext> match_context,
759 int rv) { 765 int rv) {
760 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. 766 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
761 tracked_objects::ScopedTracker tracking_profile( 767 tracked_objects::ScopedTracker tracking_profile(
762 FROM_HERE_WITH_EXPLICIT_FUNCTION( 768 FROM_HERE_WITH_EXPLICIT_FUNCTION(
763 "422516 ServiceWorkerCache::MatchDidReadResponseBodyData")); 769 "422516 ServiceWorkerCache::MatchDidReadResponseBodyData"));
764 770
765 if (rv < 0) { 771 if (rv < 0) {
766 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeStorage, 772 match_context->original_callback.Run(
767 scoped_ptr<ServiceWorkerResponse>(), 773 ServiceWorkerCache::ErrorTypeStorage,
768 scoped_ptr<storage::BlobDataHandle>()); 774 scoped_ptr<ServiceWorkerResponse>(),
775 scoped_ptr<storage::BlobDataSnapshotHandle>());
769 return; 776 return;
770 } 777 }
771 778
772 if (rv == 0) { 779 if (rv == 0) {
773 match_context->response->blob_uuid = match_context->blob_data->uuid(); 780 match_context->response->blob_uuid = match_context->blob_data->uuid();
774 match_context->response->blob_size = match_context->total_bytes_read; 781 match_context->response->blob_size = match_context->total_bytes_read;
775 MatchDoneWithBody(match_context.Pass()); 782 MatchDoneWithBody(match_context.Pass());
776 return; 783 return;
777 } 784 }
778 785
(...skipping 16 matching lines...) Expand all
795 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read, 802 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read,
796 buffer, buffer->size(), read_callback); 803 buffer, buffer->size(), read_callback);
797 804
798 if (read_rv != net::ERR_IO_PENDING) 805 if (read_rv != net::ERR_IO_PENDING)
799 read_callback.Run(read_rv); 806 read_callback.Run(read_rv);
800 } 807 }
801 808
802 void ServiceWorkerCache::MatchDoneWithBody( 809 void ServiceWorkerCache::MatchDoneWithBody(
803 scoped_ptr<MatchContext> match_context) { 810 scoped_ptr<MatchContext> match_context) {
804 if (!match_context->blob_storage_context) { 811 if (!match_context->blob_storage_context) {
805 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeStorage, 812 match_context->original_callback.Run(
806 scoped_ptr<ServiceWorkerResponse>(), 813 ServiceWorkerCache::ErrorTypeStorage,
807 scoped_ptr<storage::BlobDataHandle>()); 814 scoped_ptr<ServiceWorkerResponse>(),
815 scoped_ptr<storage::BlobDataSnapshotHandle>());
808 return; 816 return;
809 } 817 }
810 818
811 scoped_ptr<storage::BlobDataHandle> blob_data_handle( 819 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle(
812 match_context->blob_storage_context->AddFinishedBlob( 820 match_context->blob_storage_context->AddFinishedBlob(
813 match_context->blob_data.get())); 821 *match_context->blob_data.get()));
814 822
815 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeOK, 823 match_context->original_callback.Run(ServiceWorkerCache::ErrorTypeOK,
816 match_context->response.Pass(), 824 match_context->response.Pass(),
817 blob_data_handle.Pass()); 825 blob_data_handle.Pass());
818 } 826 }
819 827
820 void ServiceWorkerCache::PutImpl(scoped_ptr<PutContext> put_context) { 828 void ServiceWorkerCache::PutImpl(scoped_ptr<PutContext> put_context) {
821 if (backend_state_ != BACKEND_OPEN) { 829 if (backend_state_ != BACKEND_OPEN) {
822 put_context->callback.Run(ErrorTypeStorage, 830 put_context->callback.Run(ErrorTypeStorage,
823 scoped_ptr<ServiceWorkerResponse>(), 831 scoped_ptr<ServiceWorkerResponse>(),
824 scoped_ptr<storage::BlobDataHandle>()); 832 scoped_ptr<storage::BlobDataSnapshotHandle>());
825 return; 833 return;
826 } 834 }
827 835
828 scoped_ptr<ServiceWorkerFetchRequest> request_copy( 836 scoped_ptr<ServiceWorkerFetchRequest> request_copy(
829 new ServiceWorkerFetchRequest(*put_context->request)); 837 new ServiceWorkerFetchRequest(*put_context->request));
830 838
831 Delete(request_copy.Pass(), base::Bind(&ServiceWorkerCache::PutDidDelete, 839 Delete(request_copy.Pass(), base::Bind(&ServiceWorkerCache::PutDidDelete,
832 weak_ptr_factory_.GetWeakPtr(), 840 weak_ptr_factory_.GetWeakPtr(),
833 base::Passed(put_context.Pass()))); 841 base::Passed(put_context.Pass())));
834 } 842 }
835 843
836 void ServiceWorkerCache::PutDidDelete(scoped_ptr<PutContext> put_context, 844 void ServiceWorkerCache::PutDidDelete(scoped_ptr<PutContext> put_context,
837 ErrorType delete_error) { 845 ErrorType delete_error) {
838 if (backend_state_ != BACKEND_OPEN) { 846 if (backend_state_ != BACKEND_OPEN) {
839 put_context->callback.Run(ErrorTypeStorage, 847 put_context->callback.Run(ErrorTypeStorage,
840 scoped_ptr<ServiceWorkerResponse>(), 848 scoped_ptr<ServiceWorkerResponse>(),
841 scoped_ptr<storage::BlobDataHandle>()); 849 scoped_ptr<storage::BlobDataSnapshotHandle>());
842 return; 850 return;
843 } 851 }
844 852
845 disk_cache::Entry** entry_ptr = &put_context->cache_entry; 853 disk_cache::Entry** entry_ptr = &put_context->cache_entry;
846 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 854 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
847 disk_cache::Backend* backend_ptr = backend_.get(); 855 disk_cache::Backend* backend_ptr = backend_.get();
848 856
849 net::CompletionCallback create_entry_callback = base::Bind( 857 net::CompletionCallback create_entry_callback = base::Bind(
850 &ServiceWorkerCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 858 &ServiceWorkerCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
851 base::Passed(put_context.Pass())); 859 base::Passed(put_context.Pass()));
852 860
853 int create_rv = backend_ptr->CreateEntry( 861 int create_rv = backend_ptr->CreateEntry(
854 request_ptr->url.spec(), entry_ptr, create_entry_callback); 862 request_ptr->url.spec(), entry_ptr, create_entry_callback);
855 863
856 if (create_rv != net::ERR_IO_PENDING) 864 if (create_rv != net::ERR_IO_PENDING)
857 create_entry_callback.Run(create_rv); 865 create_entry_callback.Run(create_rv);
858 } 866 }
859 867
860 void ServiceWorkerCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context, 868 void ServiceWorkerCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context,
861 int rv) { 869 int rv) {
862 if (rv != net::OK) { 870 if (rv != net::OK) {
863 put_context->callback.Run(ServiceWorkerCache::ErrorTypeExists, 871 put_context->callback.Run(ServiceWorkerCache::ErrorTypeExists,
864 scoped_ptr<ServiceWorkerResponse>(), 872 scoped_ptr<ServiceWorkerResponse>(),
865 scoped_ptr<storage::BlobDataHandle>()); 873 scoped_ptr<storage::BlobDataSnapshotHandle>());
866 return; 874 return;
867 } 875 }
868 876
869 DCHECK(put_context->cache_entry); 877 DCHECK(put_context->cache_entry);
870 878
871 ServiceWorkerCacheMetadata metadata; 879 ServiceWorkerCacheMetadata metadata;
872 ServiceWorkerCacheRequest* request_metadata = metadata.mutable_request(); 880 ServiceWorkerCacheRequest* request_metadata = metadata.mutable_request();
873 request_metadata->set_method(put_context->request->method); 881 request_metadata->set_method(put_context->request->method);
874 for (ServiceWorkerHeaderMap::const_iterator it = 882 for (ServiceWorkerHeaderMap::const_iterator it =
875 put_context->request->headers.begin(); 883 put_context->request->headers.begin();
(...skipping 16 matching lines...) Expand all
892 ++it) { 900 ++it) {
893 ServiceWorkerCacheHeaderMap* header_map = response_metadata->add_headers(); 901 ServiceWorkerCacheHeaderMap* header_map = response_metadata->add_headers();
894 header_map->set_name(it->first); 902 header_map->set_name(it->first);
895 header_map->set_value(it->second); 903 header_map->set_value(it->second);
896 } 904 }
897 905
898 scoped_ptr<std::string> serialized(new std::string()); 906 scoped_ptr<std::string> serialized(new std::string());
899 if (!metadata.SerializeToString(serialized.get())) { 907 if (!metadata.SerializeToString(serialized.get())) {
900 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, 908 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage,
901 scoped_ptr<ServiceWorkerResponse>(), 909 scoped_ptr<ServiceWorkerResponse>(),
902 scoped_ptr<storage::BlobDataHandle>()); 910 scoped_ptr<storage::BlobDataSnapshotHandle>());
903 return; 911 return;
904 } 912 }
905 913
906 scoped_refptr<net::StringIOBuffer> buffer( 914 scoped_refptr<net::StringIOBuffer> buffer(
907 new net::StringIOBuffer(serialized.Pass())); 915 new net::StringIOBuffer(serialized.Pass()));
908 916
909 // Get a temporary copy of the entry pointer before passing it in base::Bind. 917 // Get a temporary copy of the entry pointer before passing it in base::Bind.
910 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry; 918 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry;
911 919
912 net::CompletionCallback write_headers_callback = base::Bind( 920 net::CompletionCallback write_headers_callback = base::Bind(
(...skipping 11 matching lines...) Expand all
924 write_headers_callback.Run(rv); 932 write_headers_callback.Run(rv);
925 } 933 }
926 934
927 void ServiceWorkerCache::PutDidWriteHeaders(scoped_ptr<PutContext> put_context, 935 void ServiceWorkerCache::PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
928 int expected_bytes, 936 int expected_bytes,
929 int rv) { 937 int rv) {
930 if (rv != expected_bytes) { 938 if (rv != expected_bytes) {
931 put_context->cache_entry->Doom(); 939 put_context->cache_entry->Doom();
932 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, 940 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage,
933 scoped_ptr<ServiceWorkerResponse>(), 941 scoped_ptr<ServiceWorkerResponse>(),
934 scoped_ptr<storage::BlobDataHandle>()); 942 scoped_ptr<storage::BlobDataSnapshotHandle>());
935 return; 943 return;
936 } 944 }
937 945
938 // The metadata is written, now for the response content. The data is streamed 946 // The metadata is written, now for the response content. The data is streamed
939 // from the blob into the cache entry. 947 // from the blob into the cache entry.
940 948
941 if (put_context->response->blob_uuid.empty()) { 949 if (put_context->response->blob_uuid.empty()) {
942 if (put_context->quota_manager_proxy.get()) { 950 if (put_context->quota_manager_proxy.get()) {
943 put_context->quota_manager_proxy->NotifyStorageModified( 951 put_context->quota_manager_proxy->NotifyStorageModified(
944 storage::QuotaClient::kServiceWorkerCache, 952 storage::QuotaClient::kServiceWorkerCache,
945 put_context->origin, 953 put_context->origin,
946 storage::kStorageTypeTemporary, 954 storage::kStorageTypeTemporary,
947 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); 955 put_context->cache_entry->GetDataSize(INDEX_HEADERS));
948 } 956 }
949 957
950 put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK, 958 put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK,
951 put_context->response.Pass(), 959 put_context->response.Pass(),
952 scoped_ptr<storage::BlobDataHandle>()); 960 scoped_ptr<storage::BlobDataSnapshotHandle>());
953 return; 961 return;
954 } 962 }
955 963
956 DCHECK(put_context->blob_data_handle); 964 DCHECK(put_context->blob_data_handle);
957 965
958 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); 966 disk_cache::ScopedEntryPtr entry(put_context->cache_entry);
959 put_context->cache_entry = NULL; 967 put_context->cache_entry = NULL;
960 scoped_ptr<BlobReader> reader(new BlobReader()); 968 scoped_ptr<BlobReader> reader(new BlobReader());
961 BlobReader* reader_ptr = reader.get(); 969 BlobReader* reader_ptr = reader.get();
962 970
963 // Grab some pointers before passing put_context in Bind. 971 // Grab some pointers before passing put_context in Bind.
964 net::URLRequestContext* request_context = put_context->request_context; 972 net::URLRequestContext* request_context = put_context->request_context;
965 scoped_ptr<storage::BlobDataHandle> blob_data_handle = 973 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle =
966 put_context->blob_data_handle.Pass(); 974 put_context->blob_data_handle.Pass();
967 975
968 reader_ptr->StreamBlobToCache( 976 reader_ptr->StreamBlobToCache(
969 entry.Pass(), request_context, blob_data_handle.Pass(), 977 entry.Pass(), request_context, blob_data_handle.Pass(),
970 base::Bind(&ServiceWorkerCache::PutDidWriteBlobToCache, 978 base::Bind(&ServiceWorkerCache::PutDidWriteBlobToCache,
971 weak_ptr_factory_.GetWeakPtr(), 979 weak_ptr_factory_.GetWeakPtr(),
972 base::Passed(put_context.Pass()), 980 base::Passed(put_context.Pass()),
973 base::Passed(reader.Pass()))); 981 base::Passed(reader.Pass())));
974 } 982 }
975 983
976 void ServiceWorkerCache::PutDidWriteBlobToCache( 984 void ServiceWorkerCache::PutDidWriteBlobToCache(
977 scoped_ptr<PutContext> put_context, 985 scoped_ptr<PutContext> put_context,
978 scoped_ptr<BlobReader> blob_reader, 986 scoped_ptr<BlobReader> blob_reader,
979 disk_cache::ScopedEntryPtr entry, 987 disk_cache::ScopedEntryPtr entry,
980 bool success) { 988 bool success) {
981 DCHECK(entry); 989 DCHECK(entry);
982 put_context->cache_entry = entry.release(); 990 put_context->cache_entry = entry.release();
983 991
984 if (!success) { 992 if (!success) {
985 put_context->cache_entry->Doom(); 993 put_context->cache_entry->Doom();
986 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, 994 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage,
987 scoped_ptr<ServiceWorkerResponse>(), 995 scoped_ptr<ServiceWorkerResponse>(),
988 scoped_ptr<storage::BlobDataHandle>()); 996 scoped_ptr<storage::BlobDataSnapshotHandle>());
989 return; 997 return;
990 } 998 }
991 999
992 if (put_context->quota_manager_proxy.get()) { 1000 if (put_context->quota_manager_proxy.get()) {
993 put_context->quota_manager_proxy->NotifyStorageModified( 1001 put_context->quota_manager_proxy->NotifyStorageModified(
994 storage::QuotaClient::kServiceWorkerCache, 1002 storage::QuotaClient::kServiceWorkerCache,
995 put_context->origin, 1003 put_context->origin,
996 storage::kStorageTypeTemporary, 1004 storage::kStorageTypeTemporary,
997 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + 1005 put_context->cache_entry->GetDataSize(INDEX_HEADERS) +
998 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); 1006 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY));
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 void ServiceWorkerCache::PendingErrorCallback(const ErrorCallback& callback, 1209 void ServiceWorkerCache::PendingErrorCallback(const ErrorCallback& callback,
1202 ErrorType error) { 1210 ErrorType error) {
1203 callback.Run(error); 1211 callback.Run(error);
1204 DecPendingOps(); 1212 DecPendingOps();
1205 } 1213 }
1206 1214
1207 void ServiceWorkerCache::PendingResponseCallback( 1215 void ServiceWorkerCache::PendingResponseCallback(
1208 const ResponseCallback& callback, 1216 const ResponseCallback& callback,
1209 ErrorType error, 1217 ErrorType error,
1210 scoped_ptr<ServiceWorkerResponse> response, 1218 scoped_ptr<ServiceWorkerResponse> response,
1211 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { 1219 scoped_ptr<storage::BlobDataSnapshotHandle> blob_data_handle) {
1212 callback.Run(error, response.Pass(), blob_data_handle.Pass()); 1220 callback.Run(error, response.Pass(), blob_data_handle.Pass());
1213 DecPendingOps(); 1221 DecPendingOps();
1214 } 1222 }
1215 1223
1216 void ServiceWorkerCache::PendingRequestsCallback( 1224 void ServiceWorkerCache::PendingRequestsCallback(
1217 const RequestsCallback& callback, 1225 const RequestsCallback& callback,
1218 ErrorType error, 1226 ErrorType error,
1219 scoped_ptr<Requests> requests) { 1227 scoped_ptr<Requests> requests) {
1220 callback.Run(error, requests.Pass()); 1228 callback.Run(error, requests.Pass());
1221 DecPendingOps(); 1229 DecPendingOps();
1222 } 1230 }
1223 1231
1224 } // namespace content 1232 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698