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

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

Issue 2901083002: [CacheStorage] Pad and bin opaque resource sizes. (Closed)
Patch Set: Rebased and resolved conflicts. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/cache_storage/cache_storage_cache.h" 5 #include "content/browser/cache_storage/cache_storage_cache.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <functional>
9 #include <limits> 10 #include <limits>
10 #include <memory> 11 #include <memory>
11 #include <string> 12 #include <string>
12 #include <utility> 13 #include <utility>
13 14
14 #include "base/barrier_closure.h" 15 #include "base/barrier_closure.h"
15 #include "base/bind_helpers.h" 16 #include "base/bind_helpers.h"
16 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
17 #include "base/guid.h" 18 #include "base/guid.h"
19 #include "base/lazy_instance.h"
18 #include "base/macros.h" 20 #include "base/macros.h"
19 #include "base/memory/ptr_util.h" 21 #include "base/memory/ptr_util.h"
20 #include "base/metrics/histogram_macros.h" 22 #include "base/metrics/histogram_macros.h"
21 #include "base/strings/string_split.h" 23 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
23 #include "base/threading/thread_task_runner_handle.h" 25 #include "base/threading/thread_task_runner_handle.h"
24 #include "content/browser/cache_storage/cache_storage.pb.h" 26 #include "content/browser/cache_storage/cache_storage.pb.h"
25 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h" 27 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h"
26 #include "content/browser/cache_storage/cache_storage_cache_handle.h" 28 #include "content/browser/cache_storage/cache_storage_cache_handle.h"
27 #include "content/browser/cache_storage/cache_storage_cache_observer.h" 29 #include "content/browser/cache_storage/cache_storage_cache_observer.h"
28 #include "content/browser/cache_storage/cache_storage_scheduler.h" 30 #include "content/browser/cache_storage/cache_storage_scheduler.h"
29 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
30 #include "content/public/common/referrer.h" 32 #include "content/public/common/referrer.h"
33 #include "crypto/hmac.h"
34 #include "crypto/random.h"
31 #include "net/base/completion_callback.h" 35 #include "net/base/completion_callback.h"
32 #include "net/base/io_buffer.h" 36 #include "net/base/io_buffer.h"
33 #include "net/base/net_errors.h" 37 #include "net/base/net_errors.h"
34 #include "net/disk_cache/disk_cache.h" 38 #include "net/disk_cache/disk_cache.h"
35 #include "net/url_request/url_request_context_getter.h" 39 #include "net/url_request/url_request_context_getter.h"
36 #include "storage/browser/blob/blob_data_builder.h" 40 #include "storage/browser/blob/blob_data_builder.h"
37 #include "storage/browser/blob/blob_data_handle.h" 41 #include "storage/browser/blob/blob_data_handle.h"
38 #include "storage/browser/blob/blob_storage_context.h" 42 #include "storage/browser/blob/blob_storage_context.h"
39 #include "storage/browser/blob/blob_url_request_job_factory.h" 43 #include "storage/browser/blob/blob_url_request_job_factory.h"
40 #include "storage/browser/quota/quota_manager_proxy.h" 44 #include "storage/browser/quota/quota_manager_proxy.h"
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 metadata.response().status_text(), 251 metadata.response().status_text(),
248 ProtoResponseTypeToWebResponseType(metadata.response().response_type()), 252 ProtoResponseTypeToWebResponseType(metadata.response().response_type()),
249 std::move(headers), "", 0, blink::kWebServiceWorkerResponseErrorUnknown, 253 std::move(headers), "", 0, blink::kWebServiceWorkerResponseErrorUnknown,
250 base::Time::FromInternalValue(metadata.response().response_time()), 254 base::Time::FromInternalValue(metadata.response().response_time()),
251 true /* is_in_cache_storage */, cache_name, 255 true /* is_in_cache_storage */, cache_name,
252 base::MakeUnique<ServiceWorkerHeaderList>( 256 base::MakeUnique<ServiceWorkerHeaderList>(
253 metadata.response().cors_exposed_header_names().begin(), 257 metadata.response().cors_exposed_header_names().begin(),
254 metadata.response().cors_exposed_header_names().end())); 258 metadata.response().cors_exposed_header_names().end()));
255 } 259 }
256 260
261 // The size of opaque (non-cors) resource responses are padded in order
262 // to obfuscate their actual size.
263 bool ShouldPadResourceSize(const ServiceWorkerResponse& response) {
264 return (response.response_type ==
265 blink::kWebServiceWorkerResponseTypeOpaque ||
266 response.response_type ==
267 blink::kWebServiceWorkerResponseTypeOpaqueRedirect) &&
268 !response.url_list.empty();
269 }
270
271 // Return a hash of the |response| values to be used for the resource padding
272 // calculation.
273 std::string CalculateResponsePaddingHash(
274 const ServiceWorkerResponse& response) {
275 DCHECK(!response.url_list.empty());
276
277 size_t h = std::hash<std::string>{}(response.url_list.back().spec());
278 return std::string(reinterpret_cast<const char*>(&h), sizeof(h));
279 }
280
281 class PaddingHMACKey : public std::string {
282 public:
283 PaddingHMACKey() {
284 const size_t kKeyLen = 20;
285 reserve(kKeyLen);
286 crypto::RandBytes(&(*this)[0], capacity());
287 }
288 };
289
290 const std::string& GetPaddingHMACKey() {
291 static base::LazyInstance<PaddingHMACKey>::Leaky s_key =
292 LAZY_INSTANCE_INITIALIZER;
293 return s_key.Get();
294 }
295
296 std::string CalculatePaddingHMAC(const std::string& value) {
297 crypto::HMAC hmac(crypto::HMAC::SHA256);
298 std::string result(hmac.DigestLength(), '\0');
299 if (!hmac.Init(GetPaddingHMACKey()) ||
300 !hmac.Sign(value, reinterpret_cast<uint8_t*>(&result[0]),
301 result.length())) {
302 LOG(FATAL) << "Failed to calculate HMAC.";
303 }
304 return result;
305 }
306
307 // Converts the |response_hmac| bytes into a value from the random distribution
308 // determined by threat analysis.
309 int64_t ResponsePaddingDistribution(const std::string& response_hmac) {
310 size_t response_hash = std::hash<std::string>{}(response_hmac);
311 const size_t kPaddingRange = 400 * 1024;
312 const size_t kMinPadding = 20 * 1024;
313 return kMinPadding + (response_hash % kPaddingRange);
314 }
315
257 } // namespace 316 } // namespace
258 317
259 // The state needed to pass between CacheStorageCache::Put callbacks. 318 // The state needed to pass between CacheStorageCache::Put callbacks.
260 struct CacheStorageCache::PutContext { 319 struct CacheStorageCache::PutContext {
261 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 320 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
262 std::unique_ptr<ServiceWorkerResponse> response, 321 std::unique_ptr<ServiceWorkerResponse> response,
263 std::unique_ptr<storage::BlobDataHandle> blob_data_handle, 322 std::unique_ptr<storage::BlobDataHandle> blob_data_handle,
264 const CacheStorageCache::ErrorCallback& callback) 323 const CacheStorageCache::ErrorCallback& callback)
265 : request(std::move(request)), 324 : request(std::move(request)),
266 response(std::move(response)), 325 response(std::move(response)),
(...skipping 17 matching lines...) Expand all
284 std::unique_ptr<ServiceWorkerFetchRequest> request; 343 std::unique_ptr<ServiceWorkerFetchRequest> request;
285 std::unique_ptr<ServiceWorkerResponse> response; 344 std::unique_ptr<ServiceWorkerResponse> response;
286 std::unique_ptr<storage::BlobDataHandle> blob_handle; 345 std::unique_ptr<storage::BlobDataHandle> blob_handle;
287 disk_cache::ScopedEntryPtr entry; 346 disk_cache::ScopedEntryPtr entry;
288 base::Time entry_time; 347 base::Time entry_time;
289 }; 348 };
290 349
291 struct CacheStorageCache::QueryCacheContext { 350 struct CacheStorageCache::QueryCacheContext {
292 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 351 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
293 const CacheStorageCacheQueryParams& options, 352 const CacheStorageCacheQueryParams& options,
294 const QueryCacheCallback& callback) 353 const QueryCacheCallback& callback,
354 uint32_t query_types)
295 : request(std::move(request)), 355 : request(std::move(request)),
296 options(options), 356 options(options),
297 callback(callback), 357 callback(callback),
358 query_types(query_types),
298 matches(base::MakeUnique<QueryCacheResults>()) {} 359 matches(base::MakeUnique<QueryCacheResults>()) {}
299 360
300 ~QueryCacheContext() { 361 ~QueryCacheContext() {
301 // If the CacheStorageCache is deleted before a backend operation to open 362 // If the CacheStorageCache is deleted before a backend operation to open
302 // an entry completes, the callback won't be run and the resulting entry 363 // an entry completes, the callback won't be run and the resulting entry
303 // will be leaked unless we close it here. 364 // will be leaked unless we close it here.
304 if (enumerated_entry) { 365 if (enumerated_entry) {
305 enumerated_entry->Close(); 366 enumerated_entry->Close();
306 enumerated_entry = nullptr; 367 enumerated_entry = nullptr;
307 } 368 }
308 } 369 }
309 370
310 // Input to QueryCache 371 // Input to QueryCache
311 std::unique_ptr<ServiceWorkerFetchRequest> request; 372 std::unique_ptr<ServiceWorkerFetchRequest> request;
312 CacheStorageCacheQueryParams options; 373 CacheStorageCacheQueryParams options;
313 QueryCacheCallback callback; 374 QueryCacheCallback callback;
314 QueryCacheType query_type; 375 uint32_t query_types = 0x0;
315 size_t estimated_out_bytes = 0; 376 size_t estimated_out_bytes = 0;
316 377
317 // Iteration state 378 // Iteration state
318 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator; 379 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator;
319 disk_cache::Entry* enumerated_entry = nullptr; 380 disk_cache::Entry* enumerated_entry = nullptr;
320 381
321 // Output of QueryCache 382 // Output of QueryCache
322 std::unique_ptr<std::vector<QueryCacheResult>> matches; 383 std::unique_ptr<std::vector<QueryCacheResult>> matches;
323 384
324 private: 385 private:
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 weak_ptr_factory_(this) { 665 weak_ptr_factory_(this) {
605 DCHECK(!origin_.is_empty()); 666 DCHECK(!origin_.is_empty());
606 DCHECK(quota_manager_proxy_.get()); 667 DCHECK(quota_manager_proxy_.get());
607 668
608 quota_manager_proxy_->NotifyOriginInUse(origin_); 669 quota_manager_proxy_->NotifyOriginInUse(origin_);
609 } 670 }
610 671
611 void CacheStorageCache::QueryCache( 672 void CacheStorageCache::QueryCache(
612 std::unique_ptr<ServiceWorkerFetchRequest> request, 673 std::unique_ptr<ServiceWorkerFetchRequest> request,
613 const CacheStorageCacheQueryParams& options, 674 const CacheStorageCacheQueryParams& options,
614 QueryCacheType query_type, 675 uint32_t query_types,
615 const QueryCacheCallback& callback) { 676 const QueryCacheCallback& callback) {
616 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 677 DCHECK_NE(
617 if (backend_state_ != BACKEND_OPEN) { 678 QUERY_CACHE_ENTRIES | QUERY_CACHE_RESPONSES_WITH_BODIES,
679 query_types & (QUERY_CACHE_ENTRIES | QUERY_CACHE_RESPONSES_WITH_BODIES));
680 if (backend_state_ == BACKEND_CLOSED) {
618 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 681 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
619 std::unique_ptr<QueryCacheResults>()); 682 std::unique_ptr<QueryCacheResults>());
620 return; 683 return;
621 } 684 }
622 685
623 if (!options.ignore_method && request && !request->method.empty() && 686 if (!options.ignore_method && request && !request->method.empty() &&
624 request->method != "GET") { 687 request->method != "GET") {
625 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>()); 688 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>());
626 return; 689 return;
627 } 690 }
628 691
629 ServiceWorkerFetchRequest* request_ptr = request.get(); 692 ServiceWorkerFetchRequest* request_ptr = request.get();
630 std::unique_ptr<QueryCacheContext> query_cache_context( 693 std::unique_ptr<QueryCacheContext> query_cache_context(new QueryCacheContext(
631 new QueryCacheContext(std::move(request), options, callback)); 694 std::move(request), options, callback, query_types));
632 query_cache_context->query_type = query_type;
633 695
634 if (query_cache_context->request && 696 if (query_cache_context->request &&
635 !query_cache_context->request->url.is_empty() && !options.ignore_search) { 697 !query_cache_context->request->url.is_empty() && !options.ignore_search) {
636 // There is no need to scan the entire backend, just open the exact 698 // There is no need to scan the entire backend, just open the exact
637 // URL. 699 // URL.
638 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry; 700 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry;
639 net::CompletionCallback open_entry_callback = 701 net::CompletionCallback open_entry_callback =
640 base::Bind(&CacheStorageCache::QueryCacheDidOpenFastPath, 702 base::Bind(&CacheStorageCache::QueryCacheDidOpenFastPath,
641 weak_ptr_factory_.GetWeakPtr(), 703 weak_ptr_factory_.GetWeakPtr(),
642 base::Passed(std::move(query_cache_context))); 704 base::Passed(std::move(query_cache_context)));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 if (rv < 0) { 765 if (rv < 0) {
704 QueryCacheCallback callback = query_cache_context->callback; 766 QueryCacheCallback callback = query_cache_context->callback;
705 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 767 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
706 std::move(query_cache_context->matches)); 768 std::move(query_cache_context->matches));
707 return; 769 return;
708 } 770 }
709 771
710 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry); 772 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry);
711 query_cache_context->enumerated_entry = nullptr; 773 query_cache_context->enumerated_entry = nullptr;
712 774
713 if (backend_state_ != BACKEND_OPEN) { 775 if (backend_state_ == BACKEND_CLOSED) {
714 QueryCacheCallback callback = query_cache_context->callback; 776 QueryCacheCallback callback = query_cache_context->callback;
715 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 777 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
716 std::move(query_cache_context->matches)); 778 std::move(query_cache_context->matches));
717 return; 779 return;
718 } 780 }
719 781
720 if (query_cache_context->request && 782 if (query_cache_context->request &&
721 !query_cache_context->request->url.is_empty()) { 783 !query_cache_context->request->url.is_empty()) {
722 GURL requestURL = query_cache_context->request->url; 784 GURL requestURL = query_cache_context->request->url;
723 GURL cachedURL = GURL(entry->GetKey()); 785 GURL cachedURL = GURL(entry->GetKey());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 827
766 if (query_cache_context->request && 828 if (query_cache_context->request &&
767 !query_cache_context->options.ignore_vary && 829 !query_cache_context->options.ignore_vary &&
768 !VaryMatches(query_cache_context->request->headers, 830 !VaryMatches(query_cache_context->request->headers,
769 match->request->headers, match->response->headers)) { 831 match->request->headers, match->response->headers)) {
770 query_cache_context->matches->pop_back(); 832 query_cache_context->matches->pop_back();
771 QueryCacheOpenNextEntry(std::move(query_cache_context)); 833 QueryCacheOpenNextEntry(std::move(query_cache_context));
772 return; 834 return;
773 } 835 }
774 836
775 if (query_cache_context->query_type == QueryCacheType::CACHE_ENTRIES) { 837 if (query_cache_context->query_types & QUERY_CACHE_ENTRIES)
838 match->entry = std::move(entry);
839
840 if (query_cache_context->query_types & QUERY_CACHE_REQUESTS) {
841 query_cache_context->estimated_out_bytes +=
842 match->request->EstimatedStructSize();
843 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
844 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
845 std::unique_ptr<QueryCacheResults>());
846 return;
847 }
848 } else {
776 match->request.reset(); 849 match->request.reset();
777 match->response.reset();
778 match->entry = std::move(entry);
779 QueryCacheOpenNextEntry(std::move(query_cache_context));
780 return;
781 } 850 }
782 851
783 query_cache_context->estimated_out_bytes += 852 if (query_cache_context->query_types & QUERY_CACHE_RESPONSES_WITH_BODIES) {
784 match->request->EstimatedStructSize(); 853 query_cache_context->estimated_out_bytes +=
785 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { 854 match->response->EstimatedStructSize();
786 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, 855 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
787 std::unique_ptr<QueryCacheResults>()); 856 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
788 return; 857 std::unique_ptr<QueryCacheResults>());
858 return;
859 }
860 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
861 QueryCacheOpenNextEntry(std::move(query_cache_context));
862 return;
863 }
864
865 if (!blob_storage_context_) {
866 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE,
867 std::unique_ptr<QueryCacheResults>());
868 return;
869 }
870
871 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
872 PopulateResponseBody(std::move(entry), match->response.get());
873 match->blob_handle = std::move(blob_data_handle);
874 } else if (!(query_cache_context->query_types &
875 QUERY_CACHE_RESPONSES_NO_BODIES)) {
876 match->response.reset();
789 } 877 }
790 878
791 if (query_cache_context->query_type == QueryCacheType::REQUESTS) {
792 match->response.reset();
793 QueryCacheOpenNextEntry(std::move(query_cache_context));
794 return;
795 }
796
797 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES,
798 query_cache_context->query_type);
799
800 query_cache_context->estimated_out_bytes +=
801 match->response->EstimatedStructSize();
802 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
803 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
804 std::unique_ptr<QueryCacheResults>());
805 return;
806 }
807
808 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
809 QueryCacheOpenNextEntry(std::move(query_cache_context));
810 return;
811 }
812
813 if (!blob_storage_context_) {
814 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE,
815 base::MakeUnique<QueryCacheResults>());
816 return;
817 }
818
819 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
820 PopulateResponseBody(std::move(entry), match->response.get());
821 match->blob_handle = std::move(blob_data_handle);
822
823 QueryCacheOpenNextEntry(std::move(query_cache_context)); 879 QueryCacheOpenNextEntry(std::move(query_cache_context));
824 } 880 }
825 881
826 // static 882 // static
827 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs, 883 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs,
828 const QueryCacheResult& rhs) { 884 const QueryCacheResult& rhs) {
829 return lhs.entry_time < rhs.entry_time; 885 return lhs.entry_time < rhs.entry_time;
830 } 886 }
831 887
888 // static
889 int64_t CacheStorageCache::CalculateResponsePadding(
890 const ServiceWorkerResponse& response) {
891 if (!ShouldPadResourceSize(response))
892 return 0;
893 return ResponsePaddingDistribution(
894 CalculatePaddingHMAC(CalculateResponsePaddingHash(response)));
895 }
896
832 void CacheStorageCache::MatchImpl( 897 void CacheStorageCache::MatchImpl(
833 std::unique_ptr<ServiceWorkerFetchRequest> request, 898 std::unique_ptr<ServiceWorkerFetchRequest> request,
834 const CacheStorageCacheQueryParams& match_params, 899 const CacheStorageCacheQueryParams& match_params,
835 const ResponseCallback& callback) { 900 const ResponseCallback& callback) {
836 MatchAllImpl(std::move(request), match_params, 901 MatchAllImpl(std::move(request), match_params,
837 base::Bind(&CacheStorageCache::MatchDidMatchAll, 902 base::Bind(&CacheStorageCache::MatchDidMatchAll,
838 weak_ptr_factory_.GetWeakPtr(), callback)); 903 weak_ptr_factory_.GetWeakPtr(), callback));
839 } 904 }
840 905
841 void CacheStorageCache::MatchDidMatchAll( 906 void CacheStorageCache::MatchDidMatchAll(
(...skipping 26 matching lines...) Expand all
868 const CacheStorageCacheQueryParams& options, 933 const CacheStorageCacheQueryParams& options,
869 const ResponsesCallback& callback) { 934 const ResponsesCallback& callback) {
870 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 935 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
871 if (backend_state_ != BACKEND_OPEN) { 936 if (backend_state_ != BACKEND_OPEN) {
872 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 937 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
873 std::unique_ptr<BlobDataHandles>()); 938 std::unique_ptr<BlobDataHandles>());
874 return; 939 return;
875 } 940 }
876 941
877 QueryCache(std::move(request), options, 942 QueryCache(std::move(request), options,
878 QueryCacheType::REQUESTS_AND_RESPONSES, 943 QUERY_CACHE_REQUESTS | QUERY_CACHE_RESPONSES_WITH_BODIES,
879 base::Bind(&CacheStorageCache::MatchAllDidQueryCache, 944 base::Bind(&CacheStorageCache::MatchAllDidQueryCache,
880 weak_ptr_factory_.GetWeakPtr(), callback)); 945 weak_ptr_factory_.GetWeakPtr(), callback));
881 } 946 }
882 947
883 void CacheStorageCache::MatchAllDidQueryCache( 948 void CacheStorageCache::MatchAllDidQueryCache(
884 const ResponsesCallback& callback, 949 const ResponsesCallback& callback,
885 CacheStorageError error, 950 CacheStorageError error,
886 std::unique_ptr<QueryCacheResults> query_cache_results) { 951 std::unique_ptr<QueryCacheResults> query_cache_results) {
887 if (error != CACHE_STORAGE_OK) { 952 if (error != CACHE_STORAGE_OK) {
888 callback.Run(error, std::unique_ptr<Responses>(), 953 callback.Run(error, std::unique_ptr<Responses>(),
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 base::Passed(std::move(put_context)))); 1122 base::Passed(std::move(put_context))));
1058 } 1123 }
1059 1124
1060 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) { 1125 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) {
1061 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1126 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1062 if (backend_state_ != BACKEND_OPEN) { 1127 if (backend_state_ != BACKEND_OPEN) {
1063 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1128 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1064 return; 1129 return;
1065 } 1130 }
1066 1131
1067 std::string key = put_context->request->url.spec(); 1132 // Explicitly delete the incumbent resource (which may not exist). This is
1133 // only done so that it's padding will be decremented from the calculated
1134 // cache padding.
1135 auto delete_request = base::MakeUnique<ServiceWorkerFetchRequest>(
1136 put_context->request->url, "", ServiceWorkerHeaderMap(), Referrer(),
1137 false);
1068 1138
1069 net::CompletionCallback callback = base::Bind( 1139 CacheStorageCacheQueryParams query_options;
1070 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(), 1140 query_options.ignore_method = true;
1071 base::Passed(std::move(put_context))); 1141 query_options.ignore_vary = true;
1072 1142 DeleteImpl(std::move(delete_request), query_options,
1073 int rv = backend_->DoomEntry(key, callback); 1143 base::Bind(&CacheStorageCache::PutDidDeleteEntry,
1074 if (rv != net::ERR_IO_PENDING) 1144 weak_ptr_factory_.GetWeakPtr(),
1075 callback.Run(rv); 1145 base::Passed(std::move(put_context))));
1076 } 1146 }
1077 1147
1078 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context, 1148 void CacheStorageCache::PutDidDeleteEntry(
1079 int rv) { 1149 std::unique_ptr<PutContext> put_context,
1150 CacheStorageError error,
1151 std::unique_ptr<QueryCacheResults> query_results) {
1080 if (backend_state_ != BACKEND_OPEN) { 1152 if (backend_state_ != BACKEND_OPEN) {
1081 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1153 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1082 return; 1154 return;
1083 } 1155 }
1084 1156
1085 // |rv| is ignored as doom entry can fail if the entry doesn't exist. 1157 if (error != CACHE_STORAGE_OK && error != CACHE_STORAGE_ERROR_NOT_FOUND) {
jkarlin 2017/05/26 13:21:25 Why can't we just use doom as we did before and ch
cmumford 2017/05/30 20:56:13 Currently I use the last URL in the URL list. That
jkarlin 2017/05/31 11:47:25 I'm not following what you're saying here.
cmumford 2017/06/08 17:54:04 In answer to your initial question; DoomEntry retu
jkarlin 2017/06/08 18:04:02 Got it, thanks!
1158 cache_padding_ -= CalculateResponsePadding(*put_context->response);
1159 put_context->callback.Run(error);
1160 return;
1161 }
1086 1162
1087 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 1163 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
1088 new disk_cache::Entry*()); 1164 new disk_cache::Entry*());
1089 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 1165 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
1090 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 1166 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
1091 disk_cache::Backend* backend_ptr = backend_.get(); 1167 disk_cache::Backend* backend_ptr = backend_.get();
1092 1168
1093 net::CompletionCallback create_entry_callback = base::Bind( 1169 net::CompletionCallback create_entry_callback = base::Bind(
1094 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 1170 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
1095 base::Passed(std::move(scoped_entry_ptr)), 1171 base::Passed(std::move(scoped_entry_ptr)),
1096 base::Passed(std::move(put_context))); 1172 base::Passed(std::move(put_context)));
1097 1173
1098 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, 1174 int rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr,
1099 create_entry_callback); 1175 create_entry_callback);
1100 1176
1101 if (create_rv != net::ERR_IO_PENDING) 1177 if (rv != net::ERR_IO_PENDING)
1102 create_entry_callback.Run(create_rv); 1178 create_entry_callback.Run(rv);
1103 } 1179 }
1104 1180
1105 void CacheStorageCache::PutDidCreateEntry( 1181 void CacheStorageCache::PutDidCreateEntry(
1106 std::unique_ptr<disk_cache::Entry*> entry_ptr, 1182 std::unique_ptr<disk_cache::Entry*> entry_ptr,
1107 std::unique_ptr<PutContext> put_context, 1183 std::unique_ptr<PutContext> put_context,
1108 int rv) { 1184 int rv) {
1109 put_context->cache_entry.reset(*entry_ptr); 1185 put_context->cache_entry.reset(*entry_ptr);
1110 1186
1111 if (rv != net::OK) { 1187 if (rv != net::OK) {
1112 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); 1188 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 int expected_bytes, 1253 int expected_bytes,
1178 int rv) { 1254 int rv) {
1179 if (rv != expected_bytes) { 1255 if (rv != expected_bytes) {
1180 put_context->cache_entry->Doom(); 1256 put_context->cache_entry->Doom();
1181 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1257 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1182 return; 1258 return;
1183 } 1259 }
1184 1260
1185 if (rv > 0) 1261 if (rv > 0)
1186 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1262 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1263 cache_padding_ += CalculateResponsePadding(*put_context->response);
1187 1264
1188 // The metadata is written, now for the response content. The data is streamed 1265 // The metadata is written, now for the response content. The data is streamed
1189 // from the blob into the cache entry. 1266 // from the blob into the cache entry.
1190 1267
1191 if (put_context->response->blob_uuid.empty()) { 1268 if (put_context->response->blob_uuid.empty()) {
1192 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK)); 1269 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK));
1193 return; 1270 return;
1194 } 1271 }
1195 1272
1196 DCHECK(put_context->blob_data_handle); 1273 DCHECK(put_context->blob_data_handle);
(...skipping 29 matching lines...) Expand all
1226 1303
1227 if (!success) { 1304 if (!success) {
1228 put_context->cache_entry->Doom(); 1305 put_context->cache_entry->Doom();
1229 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1306 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1230 return; 1307 return;
1231 } 1308 }
1232 1309
1233 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK)); 1310 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK));
1234 } 1311 }
1235 1312
1313 void CacheStorageCache::CalculateCacheSizePadding(
1314 const SizePaddingCallback& got_sizes_callback) {
1315 net::CompletionCallback got_size_callback =
1316 base::Bind(&CacheStorageCache::CalculateCacheSizePaddingGotSize,
1317 weak_ptr_factory_.GetWeakPtr(), std::move(got_sizes_callback));
1318
1319 int rv = backend_->CalculateSizeOfAllEntries(got_size_callback);
1320 if (rv != net::ERR_IO_PENDING)
1321 got_size_callback.Run(rv);
1322 }
1323
1324 void CacheStorageCache::CalculateCacheSizePaddingGotSize(
1325 const SizePaddingCallback& callback,
1326 int cache_size) {
1327 // Enumerating entries is only done during cache initialization.
1328 DCHECK_EQ(backend_state_, BACKEND_UNINITIALIZED);
1329 std::unique_ptr<ServiceWorkerFetchRequest> request;
1330 CacheStorageCacheQueryParams options;
1331 options.ignore_search = true;
1332 QueryCache(std::move(request), options,
1333 QUERY_CACHE_REQUESTS | QUERY_CACHE_RESPONSES_WITH_BODIES,
1334 base::Bind(&CacheStorageCache::PaddingDidQueryCache,
1335 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1336 cache_size));
1337 }
1338
1339 void CacheStorageCache::PaddingDidQueryCache(
1340 const SizePaddingCallback& callback,
1341 int cache_size,
1342 CacheStorageError error,
1343 std::unique_ptr<QueryCacheResults> query_cache_results) {
1344 int64_t cache_padding = 0;
1345 if (error == CACHE_STORAGE_OK) {
1346 for (const auto& result : *query_cache_results)
1347 cache_padding += CalculateResponsePadding(*result.response);
1348 }
1349
1350 callback.Run(cache_size, cache_padding);
1351 }
1352
1353 void CacheStorageCache::CalculateCacheSize(
1354 const net::CompletionCallback& callback) {
1355 int rv = backend_->CalculateSizeOfAllEntries(callback);
1356 if (rv != net::ERR_IO_PENDING)
1357 callback.Run(rv);
1358 }
1359
1236 void CacheStorageCache::UpdateCacheSize(const base::Closure& callback) { 1360 void CacheStorageCache::UpdateCacheSize(const base::Closure& callback) {
1237 if (backend_state_ != BACKEND_OPEN) 1361 if (backend_state_ != BACKEND_OPEN)
1238 return; 1362 return;
1239 1363
1240 // Note that the callback holds a cache handle to keep the cache alive during 1364 // Note that the callback holds a cache handle to keep the cache alive during
1241 // the operation since this UpdateCacheSize is often run after an operation 1365 // the operation since this UpdateCacheSize is often run after an operation
1242 // completes and runs its callback. 1366 // completes and runs its callback.
1243 int rv = backend_->CalculateSizeOfAllEntries( 1367 CalculateCacheSize(base::Bind(&CacheStorageCache::UpdateCacheSizeGotSize,
1244 base::Bind(&CacheStorageCache::UpdateCacheSizeGotSize, 1368 weak_ptr_factory_.GetWeakPtr(),
1245 weak_ptr_factory_.GetWeakPtr(), 1369 base::Passed(CreateCacheHandle()), callback));
1246 base::Passed(CreateCacheHandle()), callback));
1247
1248 if (rv != net::ERR_IO_PENDING)
1249 UpdateCacheSizeGotSize(CreateCacheHandle(), callback, rv);
1250 } 1370 }
1251 1371
1252 void CacheStorageCache::UpdateCacheSizeGotSize( 1372 void CacheStorageCache::UpdateCacheSizeGotSize(
1253 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 1373 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1254 const base::Closure& callback, 1374 const base::Closure& callback,
1255 int current_cache_size) { 1375 int current_cache_size) {
1256 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown); 1376 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown);
1257 int64_t old_cache_size = cache_size_;
1258 cache_size_ = current_cache_size; 1377 cache_size_ = current_cache_size;
1259 1378 int64_t size_delta = CacheSize() - last_reported_size_;
1260 int64_t size_delta = current_cache_size - old_cache_size; 1379 last_reported_size_ = CacheSize();
1261 1380
1262 quota_manager_proxy_->NotifyStorageModified( 1381 quota_manager_proxy_->NotifyStorageModified(
1263 storage::QuotaClient::kServiceWorkerCache, origin_, 1382 storage::QuotaClient::kServiceWorkerCache, origin_,
1264 storage::kStorageTypeTemporary, size_delta); 1383 storage::kStorageTypeTemporary, size_delta);
1265 1384
1266 if (cache_observer_) 1385 if (cache_observer_)
1267 cache_observer_->CacheSizeUpdated(this, current_cache_size); 1386 cache_observer_->CacheSizeUpdated(this, CacheSize());
1268 1387
1269 callback.Run(); 1388 callback.Run();
1270 } 1389 }
1271 1390
1272 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, 1391 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
1273 const ErrorCallback& callback) { 1392 const ErrorCallback& callback) {
1274 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1393 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1275 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, 1394 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
1276 operation.operation_type); 1395 operation.operation_type);
1277 1396
1278 std::unique_ptr<ServiceWorkerFetchRequest> request( 1397 std::unique_ptr<ServiceWorkerFetchRequest> request(
1279 new ServiceWorkerFetchRequest( 1398 new ServiceWorkerFetchRequest(
1280 operation.request.url, operation.request.method, 1399 operation.request.url, operation.request.method,
1281 operation.request.headers, operation.request.referrer, 1400 operation.request.headers, operation.request.referrer,
1282 operation.request.is_reload)); 1401 operation.request.is_reload));
1283 1402
1403 QueryCacheCallback delete_callback =
1404 base::Bind(&CacheStorageCache::DeleteDidDelete,
1405 weak_ptr_factory_.GetWeakPtr(), callback);
1406
1284 scheduler_->ScheduleOperation( 1407 scheduler_->ScheduleOperation(
1285 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), 1408 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(),
1286 base::Passed(std::move(request)), operation.match_params, 1409 base::Passed(std::move(request)), operation.match_params,
1287 scheduler_->WrapCallbackToRunNext(callback))); 1410 scheduler_->WrapCallbackToRunNext(delete_callback)));
1411 }
1412
1413 void CacheStorageCache::DeleteDidDelete(
1414 const ErrorCallback& callback,
1415 CacheStorageError error,
1416 std::unique_ptr<QueryCacheResults> query_results) {
1417 callback.Run(error);
1288 } 1418 }
1289 1419
1290 void CacheStorageCache::DeleteImpl( 1420 void CacheStorageCache::DeleteImpl(
1291 std::unique_ptr<ServiceWorkerFetchRequest> request, 1421 std::unique_ptr<ServiceWorkerFetchRequest> request,
1292 const CacheStorageCacheQueryParams& match_params, 1422 const CacheStorageCacheQueryParams& match_params,
1293 const ErrorCallback& callback) { 1423 const QueryCacheCallback& callback) {
1294 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1424 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1295 if (backend_state_ != BACKEND_OPEN) { 1425 if (backend_state_ != BACKEND_OPEN) {
1296 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1426 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
1427 base::MakeUnique<QueryCacheResults>());
1297 return; 1428 return;
1298 } 1429 }
1299 1430
1300 QueryCache(std::move(request), match_params, QueryCacheType::CACHE_ENTRIES, 1431 QueryCache(std::move(request), match_params,
1432 QUERY_CACHE_ENTRIES | QUERY_CACHE_RESPONSES_NO_BODIES,
1301 base::Bind(&CacheStorageCache::DeleteDidQueryCache, 1433 base::Bind(&CacheStorageCache::DeleteDidQueryCache,
1302 weak_ptr_factory_.GetWeakPtr(), callback)); 1434 weak_ptr_factory_.GetWeakPtr(), callback));
1303 } 1435 }
1304 1436
1305 void CacheStorageCache::DeleteDidQueryCache( 1437 void CacheStorageCache::DeleteDidQueryCache(
1306 const ErrorCallback& callback, 1438 const QueryCacheCallback& callback,
1307 CacheStorageError error, 1439 CacheStorageError error,
1308 std::unique_ptr<QueryCacheResults> query_cache_results) { 1440 std::unique_ptr<QueryCacheResults> query_cache_results) {
1309 if (error != CACHE_STORAGE_OK) { 1441 if (error != CACHE_STORAGE_OK) {
1310 callback.Run(error); 1442 callback.Run(error, std::move(query_cache_results));
1311 return; 1443 return;
1312 } 1444 }
1313 1445
1314 if (query_cache_results->empty()) { 1446 if (query_cache_results->empty()) {
1315 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1447 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, std::move(query_cache_results));
1316 return; 1448 return;
1317 } 1449 }
1318 1450
1319 for (auto& result : *query_cache_results) { 1451 for (auto& result : *query_cache_results) {
1320 disk_cache::ScopedEntryPtr entry = std::move(result.entry); 1452 disk_cache::ScopedEntryPtr entry = std::move(result.entry);
1453 cache_padding_ -= CalculateResponsePadding(*result.response);
1454 // TODO(cmumford): Not sure why this should be allowed to go negative.
1455 // DCHECK_GT(cache_padding_, 0);
1321 entry->Doom(); 1456 entry->Doom();
1322 } 1457 }
1323 1458
1324 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_OK)); 1459 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_OK,
1460 base::Passed(std::move(query_cache_results))));
1325 } 1461 }
1326 1462
1327 void CacheStorageCache::KeysImpl( 1463 void CacheStorageCache::KeysImpl(
1328 std::unique_ptr<ServiceWorkerFetchRequest> request, 1464 std::unique_ptr<ServiceWorkerFetchRequest> request,
1329 const CacheStorageCacheQueryParams& options, 1465 const CacheStorageCacheQueryParams& options,
1330 const RequestsCallback& callback) { 1466 const RequestsCallback& callback) {
1331 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1467 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1332 if (backend_state_ != BACKEND_OPEN) { 1468 if (backend_state_ != BACKEND_OPEN) {
1333 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 1469 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>());
1334 return; 1470 return;
1335 } 1471 }
1336 1472
1337 QueryCache(std::move(request), options, QueryCacheType::REQUESTS, 1473 QueryCache(std::move(request), options, QUERY_CACHE_REQUESTS,
1338 base::Bind(&CacheStorageCache::KeysDidQueryCache, 1474 base::Bind(&CacheStorageCache::KeysDidQueryCache,
1339 weak_ptr_factory_.GetWeakPtr(), callback)); 1475 weak_ptr_factory_.GetWeakPtr(), callback));
1340 } 1476 }
1341 1477
1342 void CacheStorageCache::KeysDidQueryCache( 1478 void CacheStorageCache::KeysDidQueryCache(
1343 const RequestsCallback& callback, 1479 const RequestsCallback& callback,
1344 CacheStorageError error, 1480 CacheStorageError error,
1345 std::unique_ptr<QueryCacheResults> query_cache_results) { 1481 std::unique_ptr<QueryCacheResults> query_cache_results) {
1346 if (error != CACHE_STORAGE_OK) { 1482 if (error != CACHE_STORAGE_OK) {
1347 callback.Run(error, std::unique_ptr<Requests>()); 1483 callback.Run(error, std::unique_ptr<Requests>());
(...skipping 12 matching lines...) Expand all
1360 DCHECK_NE(BACKEND_CLOSED, backend_state_); 1496 DCHECK_NE(BACKEND_CLOSED, backend_state_);
1361 1497
1362 backend_state_ = BACKEND_CLOSED; 1498 backend_state_ = BACKEND_CLOSED;
1363 backend_.reset(); 1499 backend_.reset();
1364 callback.Run(); 1500 callback.Run();
1365 } 1501 }
1366 1502
1367 void CacheStorageCache::SizeImpl(const SizeCallback& callback) { 1503 void CacheStorageCache::SizeImpl(const SizeCallback& callback) {
1368 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1504 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1369 1505
1370 int64_t size = backend_state_ == BACKEND_OPEN ? cache_size_ : 0; 1506 // TODO(cmumford): Can CacheStorage::kSizeUnknown be returned instead of zero?
1507 if (backend_state_ != BACKEND_OPEN) {
1508 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1509 base::Bind(callback, 0));
1510 return;
1511 }
1512
1513 int64_t size = backend_state_ == BACKEND_OPEN ? CacheSize() : 0;
1371 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1514 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1372 base::Bind(callback, size)); 1515 base::Bind(callback, size));
1373 } 1516 }
1374 1517
1375 void CacheStorageCache::GetSizeThenCloseDidGetSize(const SizeCallback& callback, 1518 void CacheStorageCache::GetSizeThenCloseDidGetSize(const SizeCallback& callback,
1376 int64_t cache_size) { 1519 int64_t cache_size) {
1377 CloseImpl(base::Bind(callback, cache_size)); 1520 CloseImpl(base::Bind(callback, cache_size));
1378 } 1521 }
1379 1522
1380 void CacheStorageCache::CreateBackend(const ErrorCallback& callback) { 1523 void CacheStorageCache::CreateBackend(const ErrorCallback& callback) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1428 base::Bind( 1571 base::Bind(
1429 &CacheStorageCache::InitDidCreateBackend, 1572 &CacheStorageCache::InitDidCreateBackend,
1430 weak_ptr_factory_.GetWeakPtr(), 1573 weak_ptr_factory_.GetWeakPtr(),
1431 scheduler_->WrapCallbackToRunNext(base::Bind(&base::DoNothing))))); 1574 scheduler_->WrapCallbackToRunNext(base::Bind(&base::DoNothing)))));
1432 } 1575 }
1433 1576
1434 void CacheStorageCache::InitDidCreateBackend( 1577 void CacheStorageCache::InitDidCreateBackend(
1435 const base::Closure& callback, 1578 const base::Closure& callback,
1436 CacheStorageError cache_create_error) { 1579 CacheStorageError cache_create_error) {
1437 if (cache_create_error != CACHE_STORAGE_OK) { 1580 if (cache_create_error != CACHE_STORAGE_OK) {
1438 InitGotCacheSize(callback, cache_create_error, 0); 1581 InitGotCacheSize(callback, cache_create_error, 0 /* cache_size */,
1582 0 /* cache_padding */);
1439 return; 1583 return;
1440 } 1584 }
1441 1585
1442 int rv = backend_->CalculateSizeOfAllEntries( 1586 CalculateCacheSizePadding(base::Bind(&CacheStorageCache::InitGotCacheSize,
jkarlin 2017/05/26 13:21:25 QueryCache calls that require enumerations are *in
cmumford 2017/05/30 20:56:13 Understood. I understand your concerns about #1, b
jkarlin 2017/05/31 11:44:48 So, the plan is for each browsing session to have
cmumford 2017/06/08 17:54:04 Yes.
1443 base::Bind(&CacheStorageCache::InitGotCacheSize, 1587 weak_ptr_factory_.GetWeakPtr(), callback,
1444 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error)); 1588 cache_create_error));
1445
1446 if (rv != net::ERR_IO_PENDING)
1447 InitGotCacheSize(callback, cache_create_error, rv);
1448 } 1589 }
1449 1590
1450 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback, 1591 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback,
1451 CacheStorageError cache_create_error, 1592 CacheStorageError cache_create_error,
1452 int cache_size) { 1593 int64_t cache_size,
1594 int64_t cache_padding) {
1453 // Now that we know the cache size either 1) the cache size should be unknown 1595 // Now that we know the cache size either 1) the cache size should be unknown
1454 // (which is why the size was calculated), or 2) it must match the current 1596 // (which is why the size was calculated), or 2) it must match the current
1455 // size. If the sizes aren't equal then there is a bug in how the cache size 1597 // size. If the sizes aren't equal then there is a bug in how the cache size
1456 // is saved in the store's index. 1598 // is saved in the store's index.
1457 if (cache_size_ != CacheStorage::kSizeUnknown) { 1599 int64_t prior_total_cache_size = CacheSize();
1458 LOG_IF(ERROR, cache_size_ != cache_size) 1600 cache_size_ = cache_size;
1459 << "Cache size: " << cache_size 1601 cache_padding_ = cache_padding;
1460 << " does not match size from index: " << cache_size_; 1602
1603 if (prior_total_cache_size != CacheStorage::kSizeUnknown) {
1604 LOG_IF(ERROR, prior_total_cache_size != CacheSize())
1605 << "Cache size: " << CacheSize()
1606 << " does not match size from index: " << prior_total_cache_size;
1461 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference", 1607 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference",
1462 std::abs(cache_size_ - cache_size)); 1608 std::abs(CacheSize() - prior_total_cache_size));
1463 // Disabled for crbug.com/681900. 1609 // Disabled for crbug.com/681900.
1464 // DCHECK_EQ(cache_size_, cache_size); 1610 // DCHECK_EQ(prior_total_cache_size, CacheSize());
1465 } 1611 }
1466 cache_size_ = cache_size;
1467 initializing_ = false; 1612 initializing_ = false;
1468 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && 1613 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ &&
1469 backend_state_ == BACKEND_UNINITIALIZED) 1614 backend_state_ == BACKEND_UNINITIALIZED)
1470 ? BACKEND_OPEN 1615 ? BACKEND_OPEN
1471 : BACKEND_CLOSED; 1616 : BACKEND_CLOSED;
1472 1617
1473 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1618 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1474 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1619 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1475 1620
1476 if (cache_observer_) 1621 if (cache_observer_)
1477 cache_observer_->CacheSizeUpdated(this, cache_size_); 1622 cache_observer_->CacheSizeUpdated(this, CacheSize());
1478 1623
1479 callback.Run(); 1624 callback.Run();
1480 } 1625 }
1481 1626
1482 std::unique_ptr<storage::BlobDataHandle> 1627 std::unique_ptr<storage::BlobDataHandle>
1483 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry, 1628 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry,
1484 ServiceWorkerResponse* response) { 1629 ServiceWorkerResponse* response) {
1485 DCHECK(blob_storage_context_); 1630 DCHECK(blob_storage_context_);
1486 1631
1487 // Create a blob with the response body data. 1632 // Create a blob with the response body data.
1488 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); 1633 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY);
1489 response->blob_uuid = base::GenerateGUID(); 1634 response->blob_uuid = base::GenerateGUID();
1490 storage::BlobDataBuilder blob_data(response->blob_uuid); 1635 storage::BlobDataBuilder blob_data(response->blob_uuid);
1491 1636
1492 disk_cache::Entry* temp_entry = entry.get(); 1637 disk_cache::Entry* temp_entry = entry.get();
1493 blob_data.AppendDiskCacheEntryWithSideData( 1638 blob_data.AppendDiskCacheEntryWithSideData(
1494 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)), 1639 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)),
1495 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1640 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1496 return blob_storage_context_->AddFinishedBlob(&blob_data); 1641 return blob_storage_context_->AddFinishedBlob(&blob_data);
1497 } 1642 }
1498 1643
1499 std::unique_ptr<CacheStorageCacheHandle> 1644 std::unique_ptr<CacheStorageCacheHandle>
1500 CacheStorageCache::CreateCacheHandle() { 1645 CacheStorageCache::CreateCacheHandle() {
1501 return cache_storage_->CreateCacheHandle(this); 1646 return cache_storage_->CreateCacheHandle(this);
1502 } 1647 }
1503 1648
1649 int64_t CacheStorageCache::CacheSize() const {
1650 return cache_size_ == CacheStorage::kSizeUnknown
1651 ? CacheStorage::kSizeUnknown
1652 : cache_size_ + cache_padding_;
1653 }
1654
1504 } // namespace content 1655 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache.h ('k') | content/browser/cache_storage/cache_storage_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698