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

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

Issue 1108083002: Create blobs from Disk Cache entries. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase better Created 5 years, 6 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/barrier_closure.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 13 matching lines...) Expand all
24 #include "storage/browser/blob/blob_data_handle.h" 24 #include "storage/browser/blob/blob_data_handle.h"
25 #include "storage/browser/blob/blob_storage_context.h" 25 #include "storage/browser/blob/blob_storage_context.h"
26 #include "storage/browser/blob/blob_url_request_job_factory.h" 26 #include "storage/browser/blob/blob_url_request_job_factory.h"
27 #include "storage/browser/quota/quota_manager_proxy.h" 27 #include "storage/browser/quota/quota_manager_proxy.h"
28 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" 28 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h"
29 29
30 namespace content { 30 namespace content {
31 31
32 namespace { 32 namespace {
33 33
34 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> 34 // This class ensures that the cache and the entry have a lifetime as long as
35 EntryBoolCallback; 35 // the blob that is created to contain them.
36 class CacheStorageCacheDataHandle
37 : public storage::BlobDataBuilder::DataHandle {
38 public:
39 CacheStorageCacheDataHandle(const scoped_refptr<CacheStorageCache>& cache,
40 disk_cache::ScopedEntryPtr entry)
41 : cache_(cache), entry_(entry.Pass()) {}
42
43 private:
44 ~CacheStorageCacheDataHandle() override {}
45
46 scoped_refptr<CacheStorageCache> cache_;
47 disk_cache::ScopedEntryPtr entry_;
jkarlin 2015/06/12 15:53:37 DISALLOW_COPY_AND_ASSIGN?
gavinp 2015/06/12 18:10:30 Done.
48 };
49
36 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; 50 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback;
37 51
38 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; 52 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
39 53
40 // The maximum size of an individual cache. Ultimately cache size is controlled 54 // The maximum size of an individual cache. Ultimately cache size is controlled
41 // per-origin. 55 // per-origin.
42 const int kMaxCacheBytes = 512 * 1024 * 1024; 56 const int kMaxCacheBytes = 512 * 1024 * 1024;
43 57
44 // Buffer size for cache and blob reading/writing. 58 // Buffer size for cache and blob reading/writing.
45 const int kBufferSize = 1024 * 512; 59 const int kBufferSize = 1024 * 512;
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // The output of the Keys function. 313 // The output of the Keys function.
300 scoped_ptr<CacheStorageCache::Requests> out_keys; 314 scoped_ptr<CacheStorageCache::Requests> out_keys;
301 315
302 // Used for enumerating cache entries. 316 // Used for enumerating cache entries.
303 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; 317 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator;
304 disk_cache::Entry* enumerated_entry; 318 disk_cache::Entry* enumerated_entry;
305 319
306 DISALLOW_COPY_AND_ASSIGN(KeysContext); 320 DISALLOW_COPY_AND_ASSIGN(KeysContext);
307 }; 321 };
308 322
309 struct CacheStorageCache::MatchContext { 323 struct CacheStorageCache::MatchContext {
jkarlin 2015/06/12 15:53:37 Might as well remove MatchContext at this point.
gavinp 2015/06/12 18:10:30 Done.
310 MatchContext(scoped_ptr<ServiceWorkerFetchRequest> request, 324 MatchContext(scoped_ptr<ServiceWorkerFetchRequest> request,
311 const CacheStorageCache::ResponseCallback& callback, 325 const CacheStorageCache::ResponseCallback& callback)
312 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) 326 : request(request.Pass()), original_callback(callback) {}
313 : request(request.Pass()),
314 original_callback(callback),
315 blob_storage_context(blob_storage_context),
316 entry(nullptr),
317 total_bytes_read(0) {}
318 327
319 ~MatchContext() {
320 if (entry)
321 entry->Close();
322 }
323
324 // Input
325 scoped_ptr<ServiceWorkerFetchRequest> request; 328 scoped_ptr<ServiceWorkerFetchRequest> request;
326 CacheStorageCache::ResponseCallback original_callback; 329 CacheStorageCache::ResponseCallback original_callback;
327 base::WeakPtr<storage::BlobStorageContext> blob_storage_context; 330 disk_cache::ScopedEntryPtr entry;
328 disk_cache::Entry* entry;
329
330 // Output
331 scoped_ptr<ServiceWorkerResponse> response;
332 scoped_ptr<storage::BlobDataBuilder> blob_data;
333
334 // For reading the cache entry data into a blob.
335 scoped_refptr<net::IOBufferWithSize> response_body_buffer;
336 size_t total_bytes_read;
337 331
338 DISALLOW_COPY_AND_ASSIGN(MatchContext); 332 DISALLOW_COPY_AND_ASSIGN(MatchContext);
339 }; 333 };
340 334
341 // The state needed to pass between CacheStorageCache::Put callbacks. 335 // The state needed to pass between CacheStorageCache::Put callbacks.
342 struct CacheStorageCache::PutContext { 336 struct CacheStorageCache::PutContext {
343 PutContext( 337 PutContext(
344 const GURL& origin, 338 const GURL& origin,
345 scoped_ptr<ServiceWorkerFetchRequest> request, 339 scoped_ptr<ServiceWorkerFetchRequest> request,
346 scoped_ptr<ServiceWorkerResponse> response, 340 scoped_ptr<ServiceWorkerResponse> response,
347 scoped_ptr<storage::BlobDataHandle> blob_data_handle, 341 scoped_ptr<storage::BlobDataHandle> blob_data_handle,
348 const CacheStorageCache::ErrorCallback& callback, 342 const CacheStorageCache::ErrorCallback& callback,
349 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, 343 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
350 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) 344 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy)
351 : origin(origin), 345 : origin(origin),
352 request(request.Pass()), 346 request(request.Pass()),
353 response(response.Pass()), 347 response(response.Pass()),
354 blob_data_handle(blob_data_handle.Pass()), 348 blob_data_handle(blob_data_handle.Pass()),
355 callback(callback), 349 callback(callback),
356 request_context_getter(request_context_getter), 350 request_context_getter(request_context_getter),
357 quota_manager_proxy(quota_manager_proxy), 351 quota_manager_proxy(quota_manager_proxy) {}
358 cache_entry(NULL) {}
359 ~PutContext() {
360 if (cache_entry)
361 cache_entry->Close();
362 }
363 352
364 // Input parameters to the Put function. 353 // Input parameters to the Put function.
365 GURL origin; 354 GURL origin;
366 scoped_ptr<ServiceWorkerFetchRequest> request; 355 scoped_ptr<ServiceWorkerFetchRequest> request;
367 scoped_ptr<ServiceWorkerResponse> response; 356 scoped_ptr<ServiceWorkerResponse> response;
368 scoped_ptr<storage::BlobDataHandle> blob_data_handle; 357 scoped_ptr<storage::BlobDataHandle> blob_data_handle;
369 CacheStorageCache::ErrorCallback callback; 358 CacheStorageCache::ErrorCallback callback;
370 scoped_refptr<net::URLRequestContextGetter> request_context_getter; 359 scoped_refptr<net::URLRequestContextGetter> request_context_getter;
371 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; 360 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy;
372 361 disk_cache::ScopedEntryPtr cache_entry;
373 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to
374 // CreateEntry.
375 disk_cache::Entry* cache_entry;
376 362
377 DISALLOW_COPY_AND_ASSIGN(PutContext); 363 DISALLOW_COPY_AND_ASSIGN(PutContext);
378 }; 364 };
379 365
380 // static 366 // static
381 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 367 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
382 const GURL& origin, 368 const GURL& origin,
383 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, 369 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
384 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 370 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
385 base::WeakPtr<storage::BlobStorageContext> blob_context) { 371 base::WeakPtr<storage::BlobStorageContext> blob_context) {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 const ResponseCallback& callback) { 564 const ResponseCallback& callback) {
579 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 565 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
580 if (backend_state_ != BACKEND_OPEN) { 566 if (backend_state_ != BACKEND_OPEN) {
581 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 567 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
582 scoped_ptr<ServiceWorkerResponse>(), 568 scoped_ptr<ServiceWorkerResponse>(),
583 scoped_ptr<storage::BlobDataHandle>()); 569 scoped_ptr<storage::BlobDataHandle>());
584 return; 570 return;
585 } 571 }
586 572
587 scoped_ptr<MatchContext> match_context( 573 scoped_ptr<MatchContext> match_context(
588 new MatchContext(request.Pass(), callback, blob_storage_context_)); 574 new MatchContext(request.Pass(), callback));
589 575
590 disk_cache::Entry** entry_ptr = &match_context->entry; 576 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*());
577 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
591 ServiceWorkerFetchRequest* request_ptr = match_context->request.get(); 578 ServiceWorkerFetchRequest* request_ptr = match_context->request.get();
592 579
593 net::CompletionCallback open_entry_callback = base::Bind( 580 net::CompletionCallback open_entry_callback = base::Bind(
594 &CacheStorageCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), 581 &CacheStorageCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(),
582 base::Passed(scoped_entry_ptr.Pass()),
595 base::Passed(match_context.Pass())); 583 base::Passed(match_context.Pass()));
596 584
597 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, 585 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr,
598 open_entry_callback); 586 open_entry_callback);
599 if (rv != net::ERR_IO_PENDING) 587 if (rv != net::ERR_IO_PENDING)
600 open_entry_callback.Run(rv); 588 open_entry_callback.Run(rv);
601 } 589 }
602 590
603 void CacheStorageCache::MatchDidOpenEntry( 591 void CacheStorageCache::MatchDidOpenEntry(
592 scoped_ptr<disk_cache::Entry*> entry_ptr,
604 scoped_ptr<MatchContext> match_context, 593 scoped_ptr<MatchContext> match_context,
605 int rv) { 594 int rv) {
606 if (rv != net::OK) { 595 if (rv != net::OK) {
607 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 596 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
608 scoped_ptr<ServiceWorkerResponse>(), 597 scoped_ptr<ServiceWorkerResponse>(),
609 scoped_ptr<storage::BlobDataHandle>()); 598 scoped_ptr<storage::BlobDataHandle>());
610 return; 599 return;
611 } 600 }
612 601
613 // Copy the entry pointer before passing it in base::Bind. 602 match_context->entry.reset(*entry_ptr);
614 disk_cache::Entry* tmp_entry_ptr = match_context->entry;
615 DCHECK(tmp_entry_ptr);
616
617 MetadataCallback headers_callback = base::Bind( 603 MetadataCallback headers_callback = base::Bind(
618 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), 604 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(),
619 base::Passed(match_context.Pass())); 605 base::Passed(match_context.Pass()));
620 606
621 ReadMetadata(tmp_entry_ptr, headers_callback); 607 ReadMetadata(*entry_ptr, headers_callback);
622 } 608 }
623 609
624 void CacheStorageCache::MatchDidReadMetadata( 610 void CacheStorageCache::MatchDidReadMetadata(
625 scoped_ptr<MatchContext> match_context, 611 scoped_ptr<MatchContext> match_context,
626 scoped_ptr<CacheMetadata> metadata) { 612 scoped_ptr<CacheMetadata> metadata) {
627 if (!metadata) { 613 if (!metadata) {
628 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, 614 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE,
629 scoped_ptr<ServiceWorkerResponse>(), 615 scoped_ptr<ServiceWorkerResponse>(),
630 scoped_ptr<storage::BlobDataHandle>()); 616 scoped_ptr<storage::BlobDataHandle>());
631 return; 617 return;
632 } 618 }
633 619
634 match_context->response.reset(new ServiceWorkerResponse( 620 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse(
635 match_context->request->url, metadata->response().status_code(), 621 match_context->request->url, metadata->response().status_code(),
636 metadata->response().status_text(), 622 metadata->response().status_text(),
637 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), 623 ProtoResponseTypeToWebResponseType(metadata->response().response_type()),
638 ServiceWorkerHeaderMap(), "", 0, GURL())); 624 ServiceWorkerHeaderMap(), "", 0, GURL()));
639 625
640 ServiceWorkerResponse* response = match_context->response.get();
641
642 if (metadata->response().has_url()) 626 if (metadata->response().has_url())
643 response->url = GURL(metadata->response().url()); 627 response->url = GURL(metadata->response().url());
644 628
645 for (int i = 0; i < metadata->response().headers_size(); ++i) { 629 for (int i = 0; i < metadata->response().headers_size(); ++i) {
646 const CacheHeaderMap header = metadata->response().headers(i); 630 const CacheHeaderMap header = metadata->response().headers(i);
647 DCHECK_EQ(std::string::npos, header.name().find('\0')); 631 DCHECK_EQ(std::string::npos, header.name().find('\0'));
648 DCHECK_EQ(std::string::npos, header.value().find('\0')); 632 DCHECK_EQ(std::string::npos, header.value().find('\0'));
649 response->headers.insert(std::make_pair(header.name(), header.value())); 633 response->headers.insert(std::make_pair(header.name(), header.value()));
650 } 634 }
651 635
652 ServiceWorkerHeaderMap cached_request_headers; 636 ServiceWorkerHeaderMap cached_request_headers;
653 for (int i = 0; i < metadata->request().headers_size(); ++i) { 637 for (int i = 0; i < metadata->request().headers_size(); ++i) {
654 const CacheHeaderMap header = metadata->request().headers(i); 638 const CacheHeaderMap header = metadata->request().headers(i);
655 DCHECK_EQ(std::string::npos, header.name().find('\0')); 639 DCHECK_EQ(std::string::npos, header.name().find('\0'));
656 DCHECK_EQ(std::string::npos, header.value().find('\0')); 640 DCHECK_EQ(std::string::npos, header.value().find('\0'));
657 cached_request_headers[header.name()] = header.value(); 641 cached_request_headers[header.name()] = header.value();
658 } 642 }
659 643
660 if (!VaryMatches(match_context->request->headers, cached_request_headers, 644 if (!VaryMatches(match_context->request->headers, cached_request_headers,
661 response->headers)) { 645 response->headers)) {
662 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 646 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
663 scoped_ptr<ServiceWorkerResponse>(), 647 scoped_ptr<ServiceWorkerResponse>(),
664 scoped_ptr<storage::BlobDataHandle>()); 648 scoped_ptr<storage::BlobDataHandle>());
665 return; 649 return;
666 } 650 }
667 651
668 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { 652 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
669 match_context->original_callback.Run(CACHE_STORAGE_OK, 653 match_context->original_callback.Run(CACHE_STORAGE_OK, response.Pass(),
670 match_context->response.Pass(),
671 scoped_ptr<storage::BlobDataHandle>()); 654 scoped_ptr<storage::BlobDataHandle>());
672 return; 655 return;
673 } 656 }
674 657
675 // Stream the response body into a blob. 658 if (!blob_storage_context_) {
676 if (!match_context->blob_storage_context) {
677 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, 659 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE,
678 scoped_ptr<ServiceWorkerResponse>(), 660 scoped_ptr<ServiceWorkerResponse>(),
679 scoped_ptr<storage::BlobDataHandle>()); 661 scoped_ptr<storage::BlobDataHandle>());
680 return; 662 return;
681 } 663 }
682 664
665 // Create a blob with the response body data.
666 response->blob_size = match_context->entry->GetDataSize(INDEX_RESPONSE_BODY);
683 response->blob_uuid = base::GenerateGUID(); 667 response->blob_uuid = base::GenerateGUID();
668 scoped_ptr<storage::BlobDataBuilder> blob_data(
669 new storage::BlobDataBuilder(response->blob_uuid));
684 670
685 match_context->blob_data.reset( 671 // Copy the Entry* before the scoped_ptr<> is passed to its DataHandle.
686 new storage::BlobDataBuilder(response->blob_uuid)); 672 disk_cache::Entry* tmp_entry = match_context->entry.get();
687 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize); 673 blob_data->AppendDiskCacheEntry(
688 674 new CacheStorageCacheDataHandle(this, match_context->entry.Pass()),
689 disk_cache::Entry* tmp_entry_ptr = match_context->entry; 675 tmp_entry, INDEX_RESPONSE_BODY);
690 net::IOBufferWithSize* response_body_buffer =
691 match_context->response_body_buffer.get();
692
693 net::CompletionCallback read_callback = base::Bind(
694 &CacheStorageCache::MatchDidReadResponseBodyData,
695 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass()));
696
697 int read_rv =
698 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer,
699 response_body_buffer->size(), read_callback);
700
701 if (read_rv != net::ERR_IO_PENDING)
702 read_callback.Run(read_rv);
703 }
704
705 void CacheStorageCache::MatchDidReadResponseBodyData(
706 scoped_ptr<MatchContext> match_context,
707 int rv) {
708 if (rv < 0) {
709 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE,
710 scoped_ptr<ServiceWorkerResponse>(),
711 scoped_ptr<storage::BlobDataHandle>());
712 return;
713 }
714
715 if (rv == 0) {
716 match_context->response->blob_uuid = match_context->blob_data->uuid();
717 match_context->response->blob_size = match_context->total_bytes_read;
718 MatchDoneWithBody(match_context.Pass());
719 return;
720 }
721
722 // TODO(jkarlin): This copying of the the entire cache response into memory is
723 // awful. Create a new interface around SimpleCache that provides access the
724 // data directly from the file. See bug http://crbug.com/403493.
725 match_context->blob_data->AppendData(
726 match_context->response_body_buffer->data(), rv);
727 match_context->total_bytes_read += rv;
728 int total_bytes_read = match_context->total_bytes_read;
729
730 // Grab some pointers before passing match_context in bind.
731 net::IOBufferWithSize* buffer = match_context->response_body_buffer.get();
732 disk_cache::Entry* tmp_entry_ptr = match_context->entry;
733
734 net::CompletionCallback read_callback = base::Bind(
735 &CacheStorageCache::MatchDidReadResponseBodyData,
736 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass()));
737
738 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read,
739 buffer, buffer->size(), read_callback);
740
741 if (read_rv != net::ERR_IO_PENDING)
742 read_callback.Run(read_rv);
743 }
744
745 void CacheStorageCache::MatchDoneWithBody(
746 scoped_ptr<MatchContext> match_context) {
747 if (!match_context->blob_storage_context) {
748 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE,
749 scoped_ptr<ServiceWorkerResponse>(),
750 scoped_ptr<storage::BlobDataHandle>());
751 return;
752 }
753
754 scoped_ptr<storage::BlobDataHandle> blob_data_handle( 676 scoped_ptr<storage::BlobDataHandle> blob_data_handle(
755 match_context->blob_storage_context->AddFinishedBlob( 677 blob_storage_context_->AddFinishedBlob(blob_data.get()));
756 match_context->blob_data.get())); 678 match_context->original_callback.Run(CACHE_STORAGE_OK, response.Pass(),
757
758 match_context->original_callback.Run(CACHE_STORAGE_OK,
759 match_context->response.Pass(),
760 blob_data_handle.Pass()); 679 blob_data_handle.Pass());
761 } 680 }
762 681
763 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, 682 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation,
764 const ErrorCallback& callback) { 683 const ErrorCallback& callback) {
765 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 684 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
766 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); 685 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type);
767 686
768 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( 687 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest(
769 operation.request.url, operation.request.method, 688 operation.request.url, operation.request.method,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 base::Passed(put_context.Pass()))); 740 base::Passed(put_context.Pass())));
822 } 741 }
823 742
824 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, 743 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context,
825 CacheStorageError delete_error) { 744 CacheStorageError delete_error) {
826 if (backend_state_ != BACKEND_OPEN) { 745 if (backend_state_ != BACKEND_OPEN) {
827 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 746 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
828 return; 747 return;
829 } 748 }
830 749
831 disk_cache::Entry** entry_ptr = &put_context->cache_entry; 750 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*());
751 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
832 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 752 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
833 disk_cache::Backend* backend_ptr = backend_.get(); 753 disk_cache::Backend* backend_ptr = backend_.get();
834 754
835 net::CompletionCallback create_entry_callback = base::Bind( 755 net::CompletionCallback create_entry_callback = base::Bind(
836 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 756 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
837 base::Passed(put_context.Pass())); 757 base::Passed(scoped_entry_ptr.Pass()), base::Passed(put_context.Pass()));
838 758
839 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, 759 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr,
840 create_entry_callback); 760 create_entry_callback);
841 761
842 if (create_rv != net::ERR_IO_PENDING) 762 if (create_rv != net::ERR_IO_PENDING)
843 create_entry_callback.Run(create_rv); 763 create_entry_callback.Run(create_rv);
844 } 764 }
845 765
846 void CacheStorageCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context, 766 void CacheStorageCache::PutDidCreateEntry(
847 int rv) { 767 scoped_ptr<disk_cache::Entry*> entry_ptr,
768 scoped_ptr<PutContext> put_context,
769 int rv) {
848 if (rv != net::OK) { 770 if (rv != net::OK) {
849 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); 771 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS);
850 return; 772 return;
851 } 773 }
852 774 put_context->cache_entry.reset(*entry_ptr);
853 DCHECK(put_context->cache_entry);
854 775
855 CacheMetadata metadata; 776 CacheMetadata metadata;
856 CacheRequest* request_metadata = metadata.mutable_request(); 777 CacheRequest* request_metadata = metadata.mutable_request();
857 request_metadata->set_method(put_context->request->method); 778 request_metadata->set_method(put_context->request->method);
858 for (ServiceWorkerHeaderMap::const_iterator it = 779 for (ServiceWorkerHeaderMap::const_iterator it =
859 put_context->request->headers.begin(); 780 put_context->request->headers.begin();
860 it != put_context->request->headers.end(); ++it) { 781 it != put_context->request->headers.end(); ++it) {
861 DCHECK_EQ(std::string::npos, it->first.find('\0')); 782 DCHECK_EQ(std::string::npos, it->first.find('\0'));
862 DCHECK_EQ(std::string::npos, it->second.find('\0')); 783 DCHECK_EQ(std::string::npos, it->second.find('\0'));
863 CacheHeaderMap* header_map = request_metadata->add_headers(); 784 CacheHeaderMap* header_map = request_metadata->add_headers();
(...skipping 20 matching lines...) Expand all
884 scoped_ptr<std::string> serialized(new std::string()); 805 scoped_ptr<std::string> serialized(new std::string());
885 if (!metadata.SerializeToString(serialized.get())) { 806 if (!metadata.SerializeToString(serialized.get())) {
886 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 807 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
887 return; 808 return;
888 } 809 }
889 810
890 scoped_refptr<net::StringIOBuffer> buffer( 811 scoped_refptr<net::StringIOBuffer> buffer(
891 new net::StringIOBuffer(serialized.Pass())); 812 new net::StringIOBuffer(serialized.Pass()));
892 813
893 // Get a temporary copy of the entry pointer before passing it in base::Bind. 814 // Get a temporary copy of the entry pointer before passing it in base::Bind.
894 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry; 815 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry.get();
895 816
896 net::CompletionCallback write_headers_callback = base::Bind( 817 net::CompletionCallback write_headers_callback = base::Bind(
897 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), 818 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(),
898 base::Passed(put_context.Pass()), buffer->size()); 819 base::Passed(put_context.Pass()), buffer->size());
899 820
900 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), 821 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(),
901 buffer->size(), write_headers_callback, 822 buffer->size(), write_headers_callback,
902 true /* truncate */); 823 true /* truncate */);
903 824
904 if (rv != net::ERR_IO_PENDING) 825 if (rv != net::ERR_IO_PENDING)
(...skipping 19 matching lines...) Expand all
924 storage::kStorageTypeTemporary, 845 storage::kStorageTypeTemporary,
925 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); 846 put_context->cache_entry->GetDataSize(INDEX_HEADERS));
926 } 847 }
927 848
928 put_context->callback.Run(CACHE_STORAGE_OK); 849 put_context->callback.Run(CACHE_STORAGE_OK);
929 return; 850 return;
930 } 851 }
931 852
932 DCHECK(put_context->blob_data_handle); 853 DCHECK(put_context->blob_data_handle);
933 854
934 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); 855 disk_cache::ScopedEntryPtr entry(put_context->cache_entry.Pass());
935 put_context->cache_entry = NULL;
936 scoped_ptr<BlobReader> reader(new BlobReader()); 856 scoped_ptr<BlobReader> reader(new BlobReader());
937 BlobReader* reader_ptr = reader.get(); 857 BlobReader* reader_ptr = reader.get();
938 858
939 // Grab some pointers before passing put_context in Bind. 859 // Grab some pointers before passing put_context in Bind.
940 scoped_refptr<net::URLRequestContextGetter> request_context_getter = 860 scoped_refptr<net::URLRequestContextGetter> request_context_getter =
941 put_context->request_context_getter; 861 put_context->request_context_getter;
942 scoped_ptr<storage::BlobDataHandle> blob_data_handle = 862 scoped_ptr<storage::BlobDataHandle> blob_data_handle =
943 put_context->blob_data_handle.Pass(); 863 put_context->blob_data_handle.Pass();
944 864
945 reader_ptr->StreamBlobToCache( 865 reader_ptr->StreamBlobToCache(
946 entry.Pass(), request_context_getter, blob_data_handle.Pass(), 866 entry.Pass(), request_context_getter, blob_data_handle.Pass(),
947 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, 867 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache,
948 weak_ptr_factory_.GetWeakPtr(), 868 weak_ptr_factory_.GetWeakPtr(),
949 base::Passed(put_context.Pass()), 869 base::Passed(put_context.Pass()),
950 base::Passed(reader.Pass()))); 870 base::Passed(reader.Pass())));
951 } 871 }
952 872
953 void CacheStorageCache::PutDidWriteBlobToCache( 873 void CacheStorageCache::PutDidWriteBlobToCache(
954 scoped_ptr<PutContext> put_context, 874 scoped_ptr<PutContext> put_context,
955 scoped_ptr<BlobReader> blob_reader, 875 scoped_ptr<BlobReader> blob_reader,
956 disk_cache::ScopedEntryPtr entry, 876 disk_cache::ScopedEntryPtr entry,
957 bool success) { 877 bool success) {
958 DCHECK(entry); 878 DCHECK(entry);
959 put_context->cache_entry = entry.release(); 879 put_context->cache_entry = entry.Pass();
960 880
961 if (!success) { 881 if (!success) {
962 put_context->cache_entry->Doom(); 882 put_context->cache_entry->Doom();
963 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 883 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
964 return; 884 return;
965 } 885 }
966 886
967 if (put_context->quota_manager_proxy.get()) { 887 if (put_context->quota_manager_proxy.get()) {
968 put_context->quota_manager_proxy->NotifyStorageModified( 888 put_context->quota_manager_proxy->NotifyStorageModified(
969 storage::QuotaClient::kServiceWorkerCache, put_context->origin, 889 storage::QuotaClient::kServiceWorkerCache, put_context->origin,
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1271 CacheStorageError error, 1191 CacheStorageError error,
1272 scoped_ptr<Requests> requests) { 1192 scoped_ptr<Requests> requests) {
1273 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); 1193 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1274 1194
1275 callback.Run(error, requests.Pass()); 1195 callback.Run(error, requests.Pass());
1276 if (cache) 1196 if (cache)
1277 scheduler_->CompleteOperationAndRunNext(); 1197 scheduler_->CompleteOperationAndRunNext();
1278 } 1198 }
1279 1199
1280 } // namespace content 1200 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698