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

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

Powered by Google App Engine
This is Rietveld 408576698