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

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

Issue 3002693002: Revert of [CacheStorage] Pad and bin opaque resource sizes. (Closed)
Patch Set: Created 3 years, 4 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>
10 #include <limits> 9 #include <limits>
11 #include <memory> 10 #include <memory>
12 #include <string> 11 #include <string>
13 #include <utility> 12 #include <utility>
14 13
15 #include "base/barrier_closure.h" 14 #include "base/barrier_closure.h"
16 #include "base/bind_helpers.h" 15 #include "base/bind_helpers.h"
17 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
18 #include "base/guid.h" 17 #include "base/guid.h"
19 #include "base/macros.h" 18 #include "base/macros.h"
20 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
21 #include "base/metrics/histogram_macros.h" 20 #include "base/metrics/histogram_macros.h"
22 #include "base/strings/string_split.h" 21 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
24 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
25 #include "content/browser/cache_storage/cache_storage.pb.h" 24 #include "content/browser/cache_storage/cache_storage.pb.h"
26 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h" 25 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h"
27 #include "content/browser/cache_storage/cache_storage_cache_handle.h" 26 #include "content/browser/cache_storage/cache_storage_cache_handle.h"
28 #include "content/browser/cache_storage/cache_storage_cache_observer.h" 27 #include "content/browser/cache_storage/cache_storage_cache_observer.h"
29 #include "content/browser/cache_storage/cache_storage_scheduler.h" 28 #include "content/browser/cache_storage/cache_storage_scheduler.h"
30 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
31 #include "content/public/common/content_features.h" 30 #include "content/public/common/content_features.h"
32 #include "content/public/common/referrer.h" 31 #include "content/public/common/referrer.h"
33 #include "crypto/hmac.h"
34 #include "crypto/symmetric_key.h"
35 #include "net/base/completion_callback.h" 32 #include "net/base/completion_callback.h"
36 #include "net/base/io_buffer.h" 33 #include "net/base/io_buffer.h"
37 #include "net/base/net_errors.h" 34 #include "net/base/net_errors.h"
38 #include "net/disk_cache/disk_cache.h" 35 #include "net/disk_cache/disk_cache.h"
39 #include "net/url_request/url_request_context_getter.h" 36 #include "net/url_request/url_request_context_getter.h"
40 #include "services/network/public/interfaces/fetch_api.mojom.h" 37 #include "services/network/public/interfaces/fetch_api.mojom.h"
41 #include "storage/browser/blob/blob_data_builder.h" 38 #include "storage/browser/blob/blob_data_builder.h"
42 #include "storage/browser/blob/blob_data_handle.h" 39 #include "storage/browser/blob/blob_data_handle.h"
43 #include "storage/browser/blob/blob_impl.h" 40 #include "storage/browser/blob/blob_impl.h"
44 #include "storage/browser/blob/blob_storage_context.h" 41 #include "storage/browser/blob/blob_storage_context.h"
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 ProtoResponseTypeToFetchResponseType(metadata.response().response_type()), 253 ProtoResponseTypeToFetchResponseType(metadata.response().response_type()),
257 std::move(headers), "", 0, nullptr /* blob */, 254 std::move(headers), "", 0, nullptr /* blob */,
258 blink::kWebServiceWorkerResponseErrorUnknown, 255 blink::kWebServiceWorkerResponseErrorUnknown,
259 base::Time::FromInternalValue(metadata.response().response_time()), 256 base::Time::FromInternalValue(metadata.response().response_time()),
260 true /* is_in_cache_storage */, cache_name, 257 true /* is_in_cache_storage */, cache_name,
261 base::MakeUnique<ServiceWorkerHeaderList>( 258 base::MakeUnique<ServiceWorkerHeaderList>(
262 metadata.response().cors_exposed_header_names().begin(), 259 metadata.response().cors_exposed_header_names().begin(),
263 metadata.response().cors_exposed_header_names().end())); 260 metadata.response().cors_exposed_header_names().end()));
264 } 261 }
265 262
266 // The size of opaque (non-cors) resource responses are padded in order
267 // to obfuscate their actual size.
268 bool ShouldPadResponseType(network::mojom::FetchResponseType response_type,
269 bool has_urls) {
270 switch (response_type) {
271 case network::mojom::FetchResponseType::kBasic:
272 case network::mojom::FetchResponseType::kCORS:
273 case network::mojom::FetchResponseType::kDefault:
274 case network::mojom::FetchResponseType::kError:
275 return false;
276 case network::mojom::FetchResponseType::kOpaque:
277 case network::mojom::FetchResponseType::kOpaqueRedirect:
278 return has_urls;
279 }
280 NOTREACHED();
281 return false;
282 }
283
284 bool ShouldPadResourceSize(const content::proto::CacheResponse* response) {
285 return ShouldPadResponseType(
286 ProtoResponseTypeToFetchResponseType(response->response_type()),
287 response->url_list_size());
288 }
289
290 bool ShouldPadResourceSize(const ServiceWorkerResponse* response) {
291 return ShouldPadResponseType(response->response_type,
292 !response->url_list.empty());
293 }
294
295 // If the way that a cache's padding is calculated changes increment this
296 // version.
297 //
298 // History:
299 //
300 // 1: Uniform random 400K.
301 const int32_t kCachePaddingAlgorithmVersion = 1;
302
303 int64_t CalculateResponsePaddingInternal(
304 const std::string& response_url,
305 const crypto::SymmetricKey* padding_key,
306 int side_data_size) {
307 const uint64_t kPaddingRange = 400 * 1024; // Increment version if changed.
308
309 DCHECK(!response_url.empty());
310
311 crypto::HMAC hmac(crypto::HMAC::SHA256);
312 if (!hmac.Init(padding_key))
313 LOG(FATAL) << "Failed to init HMAC.";
314
315 std::vector<uint8_t> digest(hmac.DigestLength());
316 bool success;
317 if (side_data_size)
318 success = hmac.Sign(response_url + "METADATA", &digest[0], digest.size());
319 else
320 success = hmac.Sign(response_url, &digest[0], digest.size());
321 if (!success)
322 LOG(FATAL) << "Failed to sign URL.";
323
324 DCHECK_GE(digest.size(), sizeof(uint64_t));
325 uint64_t val = *(reinterpret_cast<uint64_t*>(&digest[0]));
326 return val % kPaddingRange;
327 }
328
329 int64_t CalculateResponsePaddingInternal(
330 const ::content::proto::CacheResponse* response,
331 const crypto::SymmetricKey* padding_key,
332 int side_data_size) {
333 DCHECK(ShouldPadResourceSize(response));
334 const std::string& url = response->url_list(response->url_list_size() - 1);
335 return CalculateResponsePaddingInternal(url, padding_key, side_data_size);
336 }
337
338 } // namespace 263 } // namespace
339 264
340 // The state needed to pass between CacheStorageCache::Put callbacks. 265 // The state needed to pass between CacheStorageCache::Put callbacks.
341 struct CacheStorageCache::PutContext { 266 struct CacheStorageCache::PutContext {
342 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 267 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
343 std::unique_ptr<ServiceWorkerResponse> response, 268 std::unique_ptr<ServiceWorkerResponse> response,
344 std::unique_ptr<storage::BlobDataHandle> blob_data_handle, 269 std::unique_ptr<storage::BlobDataHandle> blob_data_handle,
345 CacheStorageCache::ErrorCallback callback) 270 CacheStorageCache::ErrorCallback callback)
346 : request(std::move(request)), 271 : request(std::move(request)),
347 response(std::move(response)), 272 response(std::move(response)),
(...skipping 17 matching lines...) Expand all
365 std::unique_ptr<ServiceWorkerFetchRequest> request; 290 std::unique_ptr<ServiceWorkerFetchRequest> request;
366 std::unique_ptr<ServiceWorkerResponse> response; 291 std::unique_ptr<ServiceWorkerResponse> response;
367 std::unique_ptr<storage::BlobDataHandle> blob_handle; 292 std::unique_ptr<storage::BlobDataHandle> blob_handle;
368 disk_cache::ScopedEntryPtr entry; 293 disk_cache::ScopedEntryPtr entry;
369 base::Time entry_time; 294 base::Time entry_time;
370 }; 295 };
371 296
372 struct CacheStorageCache::QueryCacheContext { 297 struct CacheStorageCache::QueryCacheContext {
373 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 298 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
374 const CacheStorageCacheQueryParams& options, 299 const CacheStorageCacheQueryParams& options,
375 QueryCacheCallback callback, 300 QueryCacheCallback callback)
376 QueryTypes query_types)
377 : request(std::move(request)), 301 : request(std::move(request)),
378 options(options), 302 options(options),
379 callback(std::move(callback)), 303 callback(std::move(callback)),
380 query_types(query_types),
381 matches(base::MakeUnique<QueryCacheResults>()) {} 304 matches(base::MakeUnique<QueryCacheResults>()) {}
382 305
383 ~QueryCacheContext() { 306 ~QueryCacheContext() {
384 // If the CacheStorageCache is deleted before a backend operation to open 307 // If the CacheStorageCache is deleted before a backend operation to open
385 // an entry completes, the callback won't be run and the resulting entry 308 // an entry completes, the callback won't be run and the resulting entry
386 // will be leaked unless we close it here. 309 // will be leaked unless we close it here.
387 if (enumerated_entry) { 310 if (enumerated_entry) {
388 enumerated_entry->Close(); 311 enumerated_entry->Close();
389 enumerated_entry = nullptr; 312 enumerated_entry = nullptr;
390 } 313 }
391 } 314 }
392 315
393 // Input to QueryCache 316 // Input to QueryCache
394 std::unique_ptr<ServiceWorkerFetchRequest> request; 317 std::unique_ptr<ServiceWorkerFetchRequest> request;
395 CacheStorageCacheQueryParams options; 318 CacheStorageCacheQueryParams options;
396 QueryCacheCallback callback; 319 QueryCacheCallback callback;
397 QueryTypes query_types = 0; 320 QueryCacheType query_type;
398 size_t estimated_out_bytes = 0; 321 size_t estimated_out_bytes = 0;
399 322
400 // Iteration state 323 // Iteration state
401 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator; 324 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator;
402 disk_cache::Entry* enumerated_entry = nullptr; 325 disk_cache::Entry* enumerated_entry = nullptr;
403 326
404 // Output of QueryCache 327 // Output of QueryCache
405 std::unique_ptr<std::vector<QueryCacheResult>> matches; 328 std::unique_ptr<std::vector<QueryCacheResult>> matches;
406 329
407 private: 330 private:
408 DISALLOW_COPY_AND_ASSIGN(QueryCacheContext); 331 DISALLOW_COPY_AND_ASSIGN(QueryCacheContext);
409 }; 332 };
410 333
411 // static 334 // static
412 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 335 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
413 const GURL& origin, 336 const GURL& origin,
414 const std::string& cache_name, 337 const std::string& cache_name,
415 CacheStorage* cache_storage, 338 CacheStorage* cache_storage,
416 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 339 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
417 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 340 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
418 base::WeakPtr<storage::BlobStorageContext> blob_context, 341 base::WeakPtr<storage::BlobStorageContext> blob_context) {
419 std::unique_ptr<crypto::SymmetricKey> cache_padding_key) {
420 CacheStorageCache* cache = new CacheStorageCache( 342 CacheStorageCache* cache = new CacheStorageCache(
421 origin, cache_name, base::FilePath(), cache_storage, 343 origin, cache_name, base::FilePath(), cache_storage,
422 std::move(request_context_getter), std::move(quota_manager_proxy), 344 std::move(request_context_getter), std::move(quota_manager_proxy),
423 blob_context, 0 /* cache_size */, 0 /* cache_padding */, 345 blob_context, 0 /* cache_size */);
424 std::move(cache_padding_key));
425 cache->SetObserver(cache_storage); 346 cache->SetObserver(cache_storage);
426 cache->InitBackend(); 347 cache->InitBackend();
427 return base::WrapUnique(cache); 348 return base::WrapUnique(cache);
428 } 349 }
429 350
430 // static 351 // static
431 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache( 352 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache(
432 const GURL& origin, 353 const GURL& origin,
433 const std::string& cache_name, 354 const std::string& cache_name,
434 CacheStorage* cache_storage, 355 CacheStorage* cache_storage,
435 const base::FilePath& path, 356 const base::FilePath& path,
436 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 357 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
437 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 358 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
438 base::WeakPtr<storage::BlobStorageContext> blob_context, 359 base::WeakPtr<storage::BlobStorageContext> blob_context,
439 int64_t cache_size, 360 int64_t cache_size) {
440 int64_t cache_padding,
441 std::unique_ptr<crypto::SymmetricKey> cache_padding_key) {
442 CacheStorageCache* cache = new CacheStorageCache( 361 CacheStorageCache* cache = new CacheStorageCache(
443 origin, cache_name, path, cache_storage, 362 origin, cache_name, path, cache_storage,
444 std::move(request_context_getter), std::move(quota_manager_proxy), 363 std::move(request_context_getter), std::move(quota_manager_proxy),
445 blob_context, cache_size, cache_padding, std::move(cache_padding_key)); 364 blob_context, cache_size);
446 cache->SetObserver(cache_storage), cache->InitBackend(); 365 cache->SetObserver(cache_storage), cache->InitBackend();
447 return base::WrapUnique(cache); 366 return base::WrapUnique(cache);
448 } 367 }
449 368
450 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { 369 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() {
451 return weak_ptr_factory_.GetWeakPtr(); 370 return weak_ptr_factory_.GetWeakPtr();
452 } 371 }
453 372
454 void CacheStorageCache::Match( 373 void CacheStorageCache::Match(
455 std::unique_ptr<ServiceWorkerFetchRequest> request, 374 std::unique_ptr<ServiceWorkerFetchRequest> request,
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 } 600 }
682 601
683 CacheStorageCache::CacheStorageCache( 602 CacheStorageCache::CacheStorageCache(
684 const GURL& origin, 603 const GURL& origin,
685 const std::string& cache_name, 604 const std::string& cache_name,
686 const base::FilePath& path, 605 const base::FilePath& path,
687 CacheStorage* cache_storage, 606 CacheStorage* cache_storage,
688 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 607 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
689 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 608 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
690 base::WeakPtr<storage::BlobStorageContext> blob_context, 609 base::WeakPtr<storage::BlobStorageContext> blob_context,
691 int64_t cache_size, 610 int64_t cache_size)
692 int64_t cache_padding,
693 std::unique_ptr<crypto::SymmetricKey> cache_padding_key)
694 : origin_(origin), 611 : origin_(origin),
695 cache_name_(cache_name), 612 cache_name_(cache_name),
696 path_(path), 613 path_(path),
697 cache_storage_(cache_storage), 614 cache_storage_(cache_storage),
698 request_context_getter_(std::move(request_context_getter)), 615 request_context_getter_(std::move(request_context_getter)),
699 quota_manager_proxy_(std::move(quota_manager_proxy)), 616 quota_manager_proxy_(std::move(quota_manager_proxy)),
700 blob_storage_context_(blob_context), 617 blob_storage_context_(blob_context),
701 scheduler_( 618 scheduler_(
702 new CacheStorageScheduler(CacheStorageSchedulerClient::CLIENT_CACHE)), 619 new CacheStorageScheduler(CacheStorageSchedulerClient::CLIENT_CACHE)),
703 cache_size_(cache_size), 620 cache_size_(cache_size),
704 cache_padding_(cache_padding),
705 cache_padding_key_(std::move(cache_padding_key)),
706 max_query_size_bytes_(kMaxQueryCacheResultBytes), 621 max_query_size_bytes_(kMaxQueryCacheResultBytes),
707 cache_observer_(nullptr), 622 cache_observer_(nullptr),
708 memory_only_(path.empty()), 623 memory_only_(path.empty()),
709 weak_ptr_factory_(this) { 624 weak_ptr_factory_(this) {
710 DCHECK(!origin_.is_empty()); 625 DCHECK(!origin_.is_empty());
711 DCHECK(quota_manager_proxy_.get()); 626 DCHECK(quota_manager_proxy_.get());
712 DCHECK(cache_padding_key_.get());
713 627
714 quota_manager_proxy_->NotifyOriginInUse(origin_); 628 quota_manager_proxy_->NotifyOriginInUse(origin_);
715 } 629 }
716 630
717 void CacheStorageCache::QueryCache( 631 void CacheStorageCache::QueryCache(
718 std::unique_ptr<ServiceWorkerFetchRequest> request, 632 std::unique_ptr<ServiceWorkerFetchRequest> request,
719 const CacheStorageCacheQueryParams& options, 633 const CacheStorageCacheQueryParams& options,
720 QueryTypes query_types, 634 QueryCacheType query_type,
721 QueryCacheCallback callback) { 635 QueryCacheCallback callback) {
722 DCHECK_NE( 636 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
723 QUERY_CACHE_ENTRIES | QUERY_CACHE_RESPONSES_WITH_BODIES, 637 if (backend_state_ != BACKEND_OPEN) {
724 query_types & (QUERY_CACHE_ENTRIES | QUERY_CACHE_RESPONSES_WITH_BODIES));
725 if (backend_state_ == BACKEND_CLOSED) {
726 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE, 638 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
727 std::unique_ptr<QueryCacheResults>()); 639 std::unique_ptr<QueryCacheResults>());
728 return; 640 return;
729 } 641 }
730 642
731 if (!options.ignore_method && request && !request->method.empty() && 643 if (!options.ignore_method && request && !request->method.empty() &&
732 request->method != "GET") { 644 request->method != "GET") {
733 std::move(callback).Run(CACHE_STORAGE_OK, 645 std::move(callback).Run(CACHE_STORAGE_OK,
734 base::MakeUnique<QueryCacheResults>()); 646 base::MakeUnique<QueryCacheResults>());
735 return; 647 return;
736 } 648 }
737 649
738 ServiceWorkerFetchRequest* request_ptr = request.get(); 650 ServiceWorkerFetchRequest* request_ptr = request.get();
739 std::unique_ptr<QueryCacheContext> query_cache_context(new QueryCacheContext( 651 std::unique_ptr<QueryCacheContext> query_cache_context(
740 std::move(request), options, std::move(callback), query_types)); 652 new QueryCacheContext(std::move(request), options, std::move(callback)));
653 query_cache_context->query_type = query_type;
741 654
742 if (query_cache_context->request && 655 if (query_cache_context->request &&
743 !query_cache_context->request->url.is_empty() && !options.ignore_search) { 656 !query_cache_context->request->url.is_empty() && !options.ignore_search) {
744 // There is no need to scan the entire backend, just open the exact 657 // There is no need to scan the entire backend, just open the exact
745 // URL. 658 // URL.
746 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry; 659 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry;
747 net::CompletionCallback open_entry_callback = 660 net::CompletionCallback open_entry_callback =
748 base::AdaptCallbackForRepeating( 661 base::AdaptCallbackForRepeating(
749 base::BindOnce(&CacheStorageCache::QueryCacheDidOpenFastPath, 662 base::BindOnce(&CacheStorageCache::QueryCacheDidOpenFastPath,
750 weak_ptr_factory_.GetWeakPtr(), 663 weak_ptr_factory_.GetWeakPtr(),
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
813 if (rv < 0) { 726 if (rv < 0) {
814 std::move(query_cache_context->callback) 727 std::move(query_cache_context->callback)
815 .Run(CACHE_STORAGE_ERROR_STORAGE, 728 .Run(CACHE_STORAGE_ERROR_STORAGE,
816 std::move(query_cache_context->matches)); 729 std::move(query_cache_context->matches));
817 return; 730 return;
818 } 731 }
819 732
820 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry); 733 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry);
821 query_cache_context->enumerated_entry = nullptr; 734 query_cache_context->enumerated_entry = nullptr;
822 735
823 if (backend_state_ == BACKEND_CLOSED) { 736 if (backend_state_ != BACKEND_OPEN) {
824 std::move(query_cache_context->callback) 737 std::move(query_cache_context->callback)
825 .Run(CACHE_STORAGE_ERROR_NOT_FOUND, 738 .Run(CACHE_STORAGE_ERROR_NOT_FOUND,
826 std::move(query_cache_context->matches)); 739 std::move(query_cache_context->matches));
827 return; 740 return;
828 } 741 }
829 742
830 if (query_cache_context->request && 743 if (query_cache_context->request &&
831 !query_cache_context->request->url.is_empty()) { 744 !query_cache_context->request->url.is_empty()) {
832 GURL requestURL = query_cache_context->request->url; 745 GURL requestURL = query_cache_context->request->url;
833 GURL cachedURL = GURL(entry->GetKey()); 746 GURL cachedURL = GURL(entry->GetKey());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 788
876 if (query_cache_context->request && 789 if (query_cache_context->request &&
877 !query_cache_context->options.ignore_vary && 790 !query_cache_context->options.ignore_vary &&
878 !VaryMatches(query_cache_context->request->headers, 791 !VaryMatches(query_cache_context->request->headers,
879 match->request->headers, match->response->headers)) { 792 match->request->headers, match->response->headers)) {
880 query_cache_context->matches->pop_back(); 793 query_cache_context->matches->pop_back();
881 QueryCacheOpenNextEntry(std::move(query_cache_context)); 794 QueryCacheOpenNextEntry(std::move(query_cache_context));
882 return; 795 return;
883 } 796 }
884 797
885 if (query_cache_context->query_types & QUERY_CACHE_ENTRIES) 798 if (query_cache_context->query_type == QueryCacheType::CACHE_ENTRIES) {
799 match->request.reset();
800 match->response.reset();
886 match->entry = std::move(entry); 801 match->entry = std::move(entry);
887 802 QueryCacheOpenNextEntry(std::move(query_cache_context));
888 if (query_cache_context->query_types & QUERY_CACHE_REQUESTS) { 803 return;
889 query_cache_context->estimated_out_bytes +=
890 match->request->EstimatedStructSize();
891 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
892 std::move(query_cache_context->callback)
893 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
894 std::unique_ptr<QueryCacheResults>());
895 return;
896 }
897 } else {
898 match->request.reset();
899 } 804 }
900 805
901 if (query_cache_context->query_types & QUERY_CACHE_RESPONSES_WITH_BODIES) { 806 query_cache_context->estimated_out_bytes +=
902 query_cache_context->estimated_out_bytes += 807 match->request->EstimatedStructSize();
903 match->response->EstimatedStructSize(); 808 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
904 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { 809 std::move(query_cache_context->callback)
905 std::move(query_cache_context->callback) 810 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
906 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, 811 std::unique_ptr<QueryCacheResults>());
907 std::unique_ptr<QueryCacheResults>()); 812 return;
908 return; 813 }
909 }
910 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
911 QueryCacheOpenNextEntry(std::move(query_cache_context));
912 return;
913 }
914 814
915 if (!blob_storage_context_) { 815 if (query_cache_context->query_type == QueryCacheType::REQUESTS) {
916 std::move(query_cache_context->callback) 816 match->response.reset();
917 .Run(CACHE_STORAGE_ERROR_STORAGE, 817 QueryCacheOpenNextEntry(std::move(query_cache_context));
918 std::unique_ptr<QueryCacheResults>()); 818 return;
919 return; 819 }
920 }
921 820
922 match->blob_handle = 821 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES,
923 PopulateResponseBody(std::move(entry), match->response.get()); 822 query_cache_context->query_type);
924 } else if (!(query_cache_context->query_types & 823
925 QUERY_CACHE_RESPONSES_NO_BODIES)) { 824 query_cache_context->estimated_out_bytes +=
926 match->response.reset(); 825 match->response->EstimatedStructSize();
826 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
827 std::move(query_cache_context->callback)
828 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
829 std::unique_ptr<QueryCacheResults>());
830 return;
927 } 831 }
928 832
833 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
834 QueryCacheOpenNextEntry(std::move(query_cache_context));
835 return;
836 }
837
838 if (!blob_storage_context_) {
839 std::move(query_cache_context->callback)
840 .Run(CACHE_STORAGE_ERROR_STORAGE,
841 base::MakeUnique<QueryCacheResults>());
842 return;
843 }
844
845 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
846 PopulateResponseBody(std::move(entry), match->response.get());
847 match->blob_handle = std::move(blob_data_handle);
848
929 QueryCacheOpenNextEntry(std::move(query_cache_context)); 849 QueryCacheOpenNextEntry(std::move(query_cache_context));
930 } 850 }
931 851
932 // static 852 // static
933 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs, 853 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs,
934 const QueryCacheResult& rhs) { 854 const QueryCacheResult& rhs) {
935 return lhs.entry_time < rhs.entry_time; 855 return lhs.entry_time < rhs.entry_time;
936 } 856 }
937 857
938 // static
939 int64_t CacheStorageCache::CalculateResponsePadding(
940 const ServiceWorkerResponse& response,
941 const crypto::SymmetricKey* padding_key,
942 int side_data_size) {
943 if (!ShouldPadResourceSize(&response))
944 return 0;
945 return CalculateResponsePaddingInternal(response.url_list.back().spec(),
946 padding_key, side_data_size);
947 }
948
949 // static
950 int32_t CacheStorageCache::GetResponsePaddingVersion() {
951 return kCachePaddingAlgorithmVersion;
952 }
953
954 void CacheStorageCache::MatchImpl( 858 void CacheStorageCache::MatchImpl(
955 std::unique_ptr<ServiceWorkerFetchRequest> request, 859 std::unique_ptr<ServiceWorkerFetchRequest> request,
956 const CacheStorageCacheQueryParams& match_params, 860 const CacheStorageCacheQueryParams& match_params,
957 ResponseCallback callback) { 861 ResponseCallback callback) {
958 MatchAllImpl( 862 MatchAllImpl(
959 std::move(request), match_params, 863 std::move(request), match_params,
960 base::BindOnce(&CacheStorageCache::MatchDidMatchAll, 864 base::BindOnce(&CacheStorageCache::MatchDidMatchAll,
961 weak_ptr_factory_.GetWeakPtr(), std::move(callback))); 865 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
962 } 866 }
963 867
(...skipping 29 matching lines...) Expand all
993 ResponsesCallback callback) { 897 ResponsesCallback callback) {
994 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 898 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
995 if (backend_state_ != BACKEND_OPEN) { 899 if (backend_state_ != BACKEND_OPEN) {
996 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE, 900 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
997 std::unique_ptr<Responses>(), 901 std::unique_ptr<Responses>(),
998 std::unique_ptr<BlobDataHandles>()); 902 std::unique_ptr<BlobDataHandles>());
999 return; 903 return;
1000 } 904 }
1001 905
1002 QueryCache( 906 QueryCache(
1003 std::move(request), options, 907 std::move(request), options, QueryCacheType::REQUESTS_AND_RESPONSES,
1004 QUERY_CACHE_REQUESTS | QUERY_CACHE_RESPONSES_WITH_BODIES,
1005 base::BindOnce(&CacheStorageCache::MatchAllDidQueryCache, 908 base::BindOnce(&CacheStorageCache::MatchAllDidQueryCache,
1006 weak_ptr_factory_.GetWeakPtr(), std::move(callback))); 909 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1007 } 910 }
1008 911
1009 void CacheStorageCache::MatchAllDidQueryCache( 912 void CacheStorageCache::MatchAllDidQueryCache(
1010 ResponsesCallback callback, 913 ResponsesCallback callback,
1011 CacheStorageError error, 914 CacheStorageError error,
1012 std::unique_ptr<QueryCacheResults> query_cache_results) { 915 std::unique_ptr<QueryCacheResults> query_cache_results) {
1013 if (error != CACHE_STORAGE_OK) { 916 if (error != CACHE_STORAGE_OK) {
1014 std::move(callback).Run(error, std::unique_ptr<Responses>(), 917 std::move(callback).Run(error, std::unique_ptr<Responses>(),
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1107 std::unique_ptr<proto::CacheMetadata> headers) { 1010 std::unique_ptr<proto::CacheMetadata> headers) {
1108 if (!headers || 1011 if (!headers ||
1109 headers->response().response_time() != 1012 headers->response().response_time() !=
1110 expected_response_time.ToInternalValue()) { 1013 expected_response_time.ToInternalValue()) {
1111 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1014 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
1112 return; 1015 return;
1113 } 1016 }
1114 // Get a temporary copy of the entry pointer before passing it in base::Bind. 1017 // Get a temporary copy of the entry pointer before passing it in base::Bind.
1115 disk_cache::Entry* temp_entry_ptr = entry.get(); 1018 disk_cache::Entry* temp_entry_ptr = entry.get();
1116 1019
1117 std::unique_ptr<content::proto::CacheResponse> response(
1118 headers->release_response());
1119
1120 int side_data_size_before_write = 0;
1121 if (ShouldPadResourceSize(response.get()))
1122 side_data_size_before_write = entry->GetDataSize(INDEX_SIDE_DATA);
1123
1124 net::CompletionCallback write_side_data_callback = 1020 net::CompletionCallback write_side_data_callback =
1125 base::AdaptCallbackForRepeating( 1021 base::AdaptCallbackForRepeating(
1126 base::BindOnce(&CacheStorageCache::WriteSideDataDidWrite, 1022 base::BindOnce(&CacheStorageCache::WriteSideDataDidWrite,
1127 weak_ptr_factory_.GetWeakPtr(), std::move(callback), 1023 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1128 base::Passed(std::move(entry)), buf_len, 1024 base::Passed(std::move(entry)), buf_len));
1129 std::move(response), side_data_size_before_write));
1130 1025
1131 int rv = temp_entry_ptr->WriteData( 1026 int rv = temp_entry_ptr->WriteData(
1132 INDEX_SIDE_DATA, 0 /* offset */, buffer.get(), buf_len, 1027 INDEX_SIDE_DATA, 0 /* offset */, buffer.get(), buf_len,
1133 write_side_data_callback, true /* truncate */); 1028 write_side_data_callback, true /* truncate */);
1134 1029
1135 if (rv != net::ERR_IO_PENDING) 1030 if (rv != net::ERR_IO_PENDING)
1136 write_side_data_callback.Run(rv); 1031 write_side_data_callback.Run(rv);
1137 } 1032 }
1138 1033
1139 void CacheStorageCache::WriteSideDataDidWrite( 1034 void CacheStorageCache::WriteSideDataDidWrite(ErrorCallback callback,
1140 ErrorCallback callback, 1035 disk_cache::ScopedEntryPtr entry,
1141 disk_cache::ScopedEntryPtr entry, 1036 int expected_bytes,
1142 int expected_bytes, 1037 int rv) {
1143 std::unique_ptr<::content::proto::CacheResponse> response,
1144 int side_data_size_before_write,
1145 int rv) {
1146 if (rv != expected_bytes) { 1038 if (rv != expected_bytes) {
1147 entry->Doom(); 1039 entry->Doom();
1148 UpdateCacheSize( 1040 UpdateCacheSize(
1149 base::BindOnce(std::move(callback), CACHE_STORAGE_ERROR_NOT_FOUND)); 1041 base::BindOnce(std::move(callback), CACHE_STORAGE_ERROR_NOT_FOUND));
1150 return; 1042 return;
1151 } 1043 }
1152 1044
1153 if (rv > 0) 1045 if (rv > 0)
1154 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1046 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1155 1047
1156 if (ShouldPadResourceSize(response.get())) {
1157 cache_padding_ -= CalculateResponsePaddingInternal(
1158 response.get(), cache_padding_key_.get(), side_data_size_before_write);
1159
1160 cache_padding_ += CalculateResponsePaddingInternal(
1161 response.get(), cache_padding_key_.get(), rv);
1162 }
1163
1164 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK)); 1048 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK));
1165 } 1049 }
1166 1050
1167 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, 1051 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation,
1168 ErrorCallback callback) { 1052 ErrorCallback callback) {
1169 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1053 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1170 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); 1054 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type);
1171 1055
1172 std::unique_ptr<ServiceWorkerFetchRequest> request( 1056 std::unique_ptr<ServiceWorkerFetchRequest> request(
1173 new ServiceWorkerFetchRequest( 1057 new ServiceWorkerFetchRequest(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1208 base::Passed(std::move(put_context)))); 1092 base::Passed(std::move(put_context))));
1209 } 1093 }
1210 1094
1211 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) { 1095 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) {
1212 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1096 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1213 if (backend_state_ != BACKEND_OPEN) { 1097 if (backend_state_ != BACKEND_OPEN) {
1214 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE); 1098 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1215 return; 1099 return;
1216 } 1100 }
1217 1101
1218 // Explicitly delete the incumbent resource (which may not exist). This is 1102 std::string key = put_context->request->url.spec();
1219 // only done so that it's padding will be decremented from the calculated
1220 // cache padding.
1221 // TODO(cmumford): Research alternatives to this explicit delete as it
1222 // seriously impacts put performance.
1223 auto delete_request = base::MakeUnique<ServiceWorkerFetchRequest>(
1224 put_context->request->url, "", ServiceWorkerHeaderMap(), Referrer(),
1225 false);
1226 1103
1227 CacheStorageCacheQueryParams query_options; 1104 net::CompletionCallback callback =
1228 query_options.ignore_method = true; 1105 base::AdaptCallbackForRepeating(base::BindOnce(
1229 query_options.ignore_vary = true; 1106 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(),
1230 DeleteImpl(std::move(delete_request), query_options, 1107 base::Passed(std::move(put_context))));
1231 base::BindOnce(&CacheStorageCache::PutDidDeleteEntry, 1108
1232 weak_ptr_factory_.GetWeakPtr(), 1109 int rv = backend_->DoomEntry(key, callback);
1233 base::Passed(std::move(put_context)))); 1110 if (rv != net::ERR_IO_PENDING)
1111 callback.Run(rv);
1234 } 1112 }
1235 1113
1236 void CacheStorageCache::PutDidDeleteEntry( 1114 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context,
1237 std::unique_ptr<PutContext> put_context, 1115 int rv) {
1238 CacheStorageError error) {
1239 if (backend_state_ != BACKEND_OPEN) { 1116 if (backend_state_ != BACKEND_OPEN) {
1240 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE); 1117 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1241 return; 1118 return;
1242 } 1119 }
1243 1120
1244 if (error != CACHE_STORAGE_OK && error != CACHE_STORAGE_ERROR_NOT_FOUND) { 1121 // |rv| is ignored as doom entry can fail if the entry doesn't exist.
1245 std::move(put_context->callback).Run(error);
1246 return;
1247 }
1248 1122
1249 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 1123 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
1250 new disk_cache::Entry*()); 1124 new disk_cache::Entry*());
1251 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 1125 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
1252 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 1126 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
1253 disk_cache::Backend* backend_ptr = backend_.get(); 1127 disk_cache::Backend* backend_ptr = backend_.get();
1254 1128
1255 net::CompletionCallback create_entry_callback = 1129 net::CompletionCallback create_entry_callback =
1256 base::AdaptCallbackForRepeating(base::BindOnce( 1130 base::AdaptCallbackForRepeating(base::BindOnce(
1257 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 1131 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
1258 base::Passed(std::move(scoped_entry_ptr)), 1132 base::Passed(std::move(scoped_entry_ptr)),
1259 base::Passed(std::move(put_context)))); 1133 base::Passed(std::move(put_context))));
1260 1134
1261 int rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, 1135 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr,
1262 create_entry_callback); 1136 create_entry_callback);
1263 1137
1264 if (rv != net::ERR_IO_PENDING) 1138 if (create_rv != net::ERR_IO_PENDING)
1265 create_entry_callback.Run(rv); 1139 create_entry_callback.Run(create_rv);
1266 } 1140 }
1267 1141
1268 void CacheStorageCache::PutDidCreateEntry( 1142 void CacheStorageCache::PutDidCreateEntry(
1269 std::unique_ptr<disk_cache::Entry*> entry_ptr, 1143 std::unique_ptr<disk_cache::Entry*> entry_ptr,
1270 std::unique_ptr<PutContext> put_context, 1144 std::unique_ptr<PutContext> put_context,
1271 int rv) { 1145 int rv) {
1272 put_context->cache_entry.reset(*entry_ptr); 1146 put_context->cache_entry.reset(*entry_ptr);
1273 1147
1274 if (rv != net::OK) { 1148 if (rv != net::OK) {
1275 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_EXISTS); 1149 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_EXISTS);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 int expected_bytes, 1216 int expected_bytes,
1343 int rv) { 1217 int rv) {
1344 if (rv != expected_bytes) { 1218 if (rv != expected_bytes) {
1345 put_context->cache_entry->Doom(); 1219 put_context->cache_entry->Doom();
1346 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE); 1220 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1347 return; 1221 return;
1348 } 1222 }
1349 1223
1350 if (rv > 0) 1224 if (rv > 0)
1351 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1225 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1352 if (ShouldPadResourceSize(put_context->response.get())) {
1353 cache_padding_ += CalculateResponsePadding(*put_context->response,
1354 cache_padding_key_.get(),
1355 0 /* side_data_size */);
1356 }
1357 1226
1358 // The metadata is written, now for the response content. The data is streamed 1227 // The metadata is written, now for the response content. The data is streamed
1359 // from the blob into the cache entry. 1228 // from the blob into the cache entry.
1360 1229
1361 if (put_context->response->blob_uuid.empty()) { 1230 if (put_context->response->blob_uuid.empty()) {
1362 UpdateCacheSize( 1231 UpdateCacheSize(
1363 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK)); 1232 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK));
1364 return; 1233 return;
1365 } 1234 }
1366 1235
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1398 if (!success) { 1267 if (!success) {
1399 put_context->cache_entry->Doom(); 1268 put_context->cache_entry->Doom();
1400 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE); 1269 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1401 return; 1270 return;
1402 } 1271 }
1403 1272
1404 UpdateCacheSize( 1273 UpdateCacheSize(
1405 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK)); 1274 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK));
1406 } 1275 }
1407 1276
1408 void CacheStorageCache::CalculateCacheSizePadding(
1409 SizePaddingCallback got_sizes_callback) {
1410 net::CompletionCallback got_size_callback =
1411 base::AdaptCallbackForRepeating(base::BindOnce(
1412 &CacheStorageCache::CalculateCacheSizePaddingGotSize,
1413 weak_ptr_factory_.GetWeakPtr(), std::move(got_sizes_callback)));
1414
1415 int rv = backend_->CalculateSizeOfAllEntries(got_size_callback);
1416 if (rv != net::ERR_IO_PENDING)
1417 got_size_callback.Run(rv);
1418 }
1419
1420 void CacheStorageCache::CalculateCacheSizePaddingGotSize(
1421 SizePaddingCallback callback,
1422 int cache_size) {
1423 // Enumerating entries is only done during cache initialization and only if
1424 // necessary.
1425 DCHECK_EQ(backend_state_, BACKEND_UNINITIALIZED);
1426 std::unique_ptr<ServiceWorkerFetchRequest> request;
1427 CacheStorageCacheQueryParams options;
1428 options.ignore_search = true;
1429 QueryCache(std::move(request), options, QUERY_CACHE_RESPONSES_NO_BODIES,
1430 base::BindOnce(&CacheStorageCache::PaddingDidQueryCache,
1431 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1432 cache_size));
1433 }
1434
1435 void CacheStorageCache::PaddingDidQueryCache(
1436 SizePaddingCallback callback,
1437 int cache_size,
1438 CacheStorageError error,
1439 std::unique_ptr<QueryCacheResults> query_cache_results) {
1440 int64_t cache_padding = 0;
1441 if (error == CACHE_STORAGE_OK) {
1442 for (const auto& result : *query_cache_results) {
1443 if (ShouldPadResourceSize(result.response.get())) {
1444 int32_t side_data_size =
1445 result.entry ? result.entry->GetDataSize(INDEX_SIDE_DATA) : 0;
1446 cache_padding += CalculateResponsePadding(
1447 *result.response, cache_padding_key_.get(), side_data_size);
1448 }
1449 }
1450 }
1451
1452 std::move(callback).Run(cache_size, cache_padding);
1453 }
1454
1455 void CacheStorageCache::CalculateCacheSize(
1456 const net::CompletionCallback& callback) {
1457 int rv = backend_->CalculateSizeOfAllEntries(callback);
1458 if (rv != net::ERR_IO_PENDING)
1459 callback.Run(rv);
1460 }
1461
1462 void CacheStorageCache::UpdateCacheSize(base::OnceClosure callback) { 1277 void CacheStorageCache::UpdateCacheSize(base::OnceClosure callback) {
1463 if (backend_state_ != BACKEND_OPEN) 1278 if (backend_state_ != BACKEND_OPEN)
1464 return; 1279 return;
1465 1280
1466 // Note that the callback holds a cache handle to keep the cache alive during 1281 // Note that the callback holds a cache handle to keep the cache alive during
1467 // the operation since this UpdateCacheSize is often run after an operation 1282 // the operation since this UpdateCacheSize is often run after an operation
1468 // completes and runs its callback. 1283 // completes and runs its callback.
1469 CalculateCacheSize(base::AdaptCallbackForRepeating( 1284 net::CompletionCallback size_callback = base::AdaptCallbackForRepeating(
1470 base::BindOnce(&CacheStorageCache::UpdateCacheSizeGotSize, 1285 base::BindOnce(&CacheStorageCache::UpdateCacheSizeGotSize,
1471 weak_ptr_factory_.GetWeakPtr(), 1286 weak_ptr_factory_.GetWeakPtr(),
1472 base::Passed(CreateCacheHandle()), std::move(callback)))); 1287 base::Passed(CreateCacheHandle()), std::move(callback)));
1288
1289 int rv = backend_->CalculateSizeOfAllEntries(size_callback);
1290 if (rv != net::ERR_IO_PENDING)
1291 std::move(size_callback).Run(rv);
1473 } 1292 }
1474 1293
1475 void CacheStorageCache::UpdateCacheSizeGotSize( 1294 void CacheStorageCache::UpdateCacheSizeGotSize(
1476 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 1295 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1477 base::OnceClosure callback, 1296 base::OnceClosure callback,
1478 int current_cache_size) { 1297 int current_cache_size) {
1479 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown); 1298 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown);
1299 int64_t old_cache_size = cache_size_;
1480 cache_size_ = current_cache_size; 1300 cache_size_ = current_cache_size;
1481 int64_t size_delta = PaddedCacheSize() - last_reported_size_; 1301
1482 last_reported_size_ = PaddedCacheSize(); 1302 int64_t size_delta = current_cache_size - old_cache_size;
1483 1303
1484 quota_manager_proxy_->NotifyStorageModified( 1304 quota_manager_proxy_->NotifyStorageModified(
1485 storage::QuotaClient::kServiceWorkerCache, origin_, 1305 storage::QuotaClient::kServiceWorkerCache, origin_,
1486 storage::kStorageTypeTemporary, size_delta); 1306 storage::kStorageTypeTemporary, size_delta);
1487 1307
1488 if (cache_observer_) 1308 if (cache_observer_)
1489 cache_observer_->CacheSizeUpdated(this); 1309 cache_observer_->CacheSizeUpdated(this, current_cache_size);
1490 1310
1491 std::move(callback).Run(); 1311 std::move(callback).Run();
1492 } 1312 }
1493 1313
1494 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, 1314 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
1495 ErrorCallback callback) { 1315 ErrorCallback callback) {
1496 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1316 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1497 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, 1317 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
1498 operation.operation_type); 1318 operation.operation_type);
1499 1319
(...skipping 13 matching lines...) Expand all
1513 std::unique_ptr<ServiceWorkerFetchRequest> request, 1333 std::unique_ptr<ServiceWorkerFetchRequest> request,
1514 const CacheStorageCacheQueryParams& match_params, 1334 const CacheStorageCacheQueryParams& match_params,
1515 ErrorCallback callback) { 1335 ErrorCallback callback) {
1516 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1336 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1517 if (backend_state_ != BACKEND_OPEN) { 1337 if (backend_state_ != BACKEND_OPEN) {
1518 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE); 1338 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1519 return; 1339 return;
1520 } 1340 }
1521 1341
1522 QueryCache( 1342 QueryCache(
1523 std::move(request), match_params, 1343 std::move(request), match_params, QueryCacheType::CACHE_ENTRIES,
1524 QUERY_CACHE_ENTRIES | QUERY_CACHE_RESPONSES_NO_BODIES,
1525 base::BindOnce(&CacheStorageCache::DeleteDidQueryCache, 1344 base::BindOnce(&CacheStorageCache::DeleteDidQueryCache,
1526 weak_ptr_factory_.GetWeakPtr(), std::move(callback))); 1345 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1527 } 1346 }
1528 1347
1529 void CacheStorageCache::DeleteDidQueryCache( 1348 void CacheStorageCache::DeleteDidQueryCache(
1530 ErrorCallback callback, 1349 ErrorCallback callback,
1531 CacheStorageError error, 1350 CacheStorageError error,
1532 std::unique_ptr<QueryCacheResults> query_cache_results) { 1351 std::unique_ptr<QueryCacheResults> query_cache_results) {
1533 if (error != CACHE_STORAGE_OK) { 1352 if (error != CACHE_STORAGE_OK) {
1534 std::move(callback).Run(error); 1353 std::move(callback).Run(error);
1535 return; 1354 return;
1536 } 1355 }
1537 1356
1538 if (query_cache_results->empty()) { 1357 if (query_cache_results->empty()) {
1539 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1358 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
1540 return; 1359 return;
1541 } 1360 }
1542 1361
1543 for (auto& result : *query_cache_results) { 1362 for (auto& result : *query_cache_results) {
1544 disk_cache::ScopedEntryPtr entry = std::move(result.entry); 1363 disk_cache::ScopedEntryPtr entry = std::move(result.entry);
1545 if (ShouldPadResourceSize(result.response.get())) {
1546 cache_padding_ -=
1547 CalculateResponsePadding(*result.response, cache_padding_key_.get(),
1548 entry->GetDataSize(INDEX_SIDE_DATA));
1549 }
1550 entry->Doom(); 1364 entry->Doom();
1551 } 1365 }
1552 1366
1553 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK)); 1367 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK));
1554 } 1368 }
1555 1369
1556 void CacheStorageCache::KeysImpl( 1370 void CacheStorageCache::KeysImpl(
1557 std::unique_ptr<ServiceWorkerFetchRequest> request, 1371 std::unique_ptr<ServiceWorkerFetchRequest> request,
1558 const CacheStorageCacheQueryParams& options, 1372 const CacheStorageCacheQueryParams& options,
1559 RequestsCallback callback) { 1373 RequestsCallback callback) {
1560 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1374 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1561 if (backend_state_ != BACKEND_OPEN) { 1375 if (backend_state_ != BACKEND_OPEN) {
1562 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE, 1376 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
1563 std::unique_ptr<Requests>()); 1377 std::unique_ptr<Requests>());
1564 return; 1378 return;
1565 } 1379 }
1566 1380
1567 QueryCache( 1381 QueryCache(
1568 std::move(request), options, QUERY_CACHE_REQUESTS, 1382 std::move(request), options, QueryCacheType::REQUESTS,
1569 base::BindOnce(&CacheStorageCache::KeysDidQueryCache, 1383 base::BindOnce(&CacheStorageCache::KeysDidQueryCache,
1570 weak_ptr_factory_.GetWeakPtr(), std::move(callback))); 1384 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1571 } 1385 }
1572 1386
1573 void CacheStorageCache::KeysDidQueryCache( 1387 void CacheStorageCache::KeysDidQueryCache(
1574 RequestsCallback callback, 1388 RequestsCallback callback,
1575 CacheStorageError error, 1389 CacheStorageError error,
1576 std::unique_ptr<QueryCacheResults> query_cache_results) { 1390 std::unique_ptr<QueryCacheResults> query_cache_results) {
1577 if (error != CACHE_STORAGE_OK) { 1391 if (error != CACHE_STORAGE_OK) {
1578 std::move(callback).Run(error, std::unique_ptr<Requests>()); 1392 std::move(callback).Run(error, std::unique_ptr<Requests>());
(...skipping 19 matching lines...) Expand all
1598 if (!post_backend_closed_callback_.is_null()) { 1412 if (!post_backend_closed_callback_.is_null()) {
1599 DCHECK_NE(BACKEND_CLOSED, backend_state_); 1413 DCHECK_NE(BACKEND_CLOSED, backend_state_);
1600 backend_state_ = BACKEND_CLOSED; 1414 backend_state_ = BACKEND_CLOSED;
1601 std::move(post_backend_closed_callback_).Run(); 1415 std::move(post_backend_closed_callback_).Run();
1602 } 1416 }
1603 } 1417 }
1604 1418
1605 void CacheStorageCache::SizeImpl(SizeCallback callback) { 1419 void CacheStorageCache::SizeImpl(SizeCallback callback) {
1606 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1420 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1607 1421
1608 // TODO(cmumford): Can CacheStorage::kSizeUnknown be returned instead of zero? 1422 int64_t size = backend_state_ == BACKEND_OPEN ? cache_size_ : 0;
1609 if (backend_state_ != BACKEND_OPEN) {
1610 base::ThreadTaskRunnerHandle::Get()->PostTask(
1611 FROM_HERE, base::BindOnce(std::move(callback), 0));
1612 return;
1613 }
1614
1615 int64_t size = backend_state_ == BACKEND_OPEN ? PaddedCacheSize() : 0;
1616 base::ThreadTaskRunnerHandle::Get()->PostTask( 1423 base::ThreadTaskRunnerHandle::Get()->PostTask(
1617 FROM_HERE, base::BindOnce(std::move(callback), size)); 1424 FROM_HERE, base::BindOnce(std::move(callback), size));
1618 } 1425 }
1619 1426
1620 void CacheStorageCache::GetSizeThenCloseDidGetSize(SizeCallback callback, 1427 void CacheStorageCache::GetSizeThenCloseDidGetSize(SizeCallback callback,
1621 int64_t cache_size) { 1428 int64_t cache_size) {
1622 CloseImpl(base::BindOnce(std::move(callback), cache_size)); 1429 CloseImpl(base::BindOnce(std::move(callback), cache_size));
1623 } 1430 }
1624 1431
1625 void CacheStorageCache::CreateBackend(ErrorCallback callback) { 1432 void CacheStorageCache::CreateBackend(ErrorCallback callback) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1678 } 1485 }
1679 1486
1680 void CacheStorageCache::InitDidCreateBackend( 1487 void CacheStorageCache::InitDidCreateBackend(
1681 base::OnceClosure callback, 1488 base::OnceClosure callback,
1682 CacheStorageError cache_create_error) { 1489 CacheStorageError cache_create_error) {
1683 if (cache_create_error != CACHE_STORAGE_OK) { 1490 if (cache_create_error != CACHE_STORAGE_OK) {
1684 InitGotCacheSize(std::move(callback), cache_create_error, 0); 1491 InitGotCacheSize(std::move(callback), cache_create_error, 0);
1685 return; 1492 return;
1686 } 1493 }
1687 1494
1688 auto calculate_size_callback = 1495 net::CompletionCallback size_callback =
1689 base::AdaptCallbackForRepeating(std::move(callback)); 1496 base::AdaptCallbackForRepeating(base::BindOnce(
1690 int rv = backend_->CalculateSizeOfAllEntries(base::Bind( 1497 &CacheStorageCache::InitGotCacheSize, weak_ptr_factory_.GetWeakPtr(),
1691 &CacheStorageCache::InitGotCacheSize, weak_ptr_factory_.GetWeakPtr(), 1498 std::move(callback), cache_create_error));
1692 calculate_size_callback, cache_create_error));
1693 1499
1500 int rv = backend_->CalculateSizeOfAllEntries(size_callback);
1694 if (rv != net::ERR_IO_PENDING) 1501 if (rv != net::ERR_IO_PENDING)
1695 InitGotCacheSize(calculate_size_callback, cache_create_error, rv); 1502 std::move(size_callback).Run(rv);
1696 } 1503 }
1697 1504
1698 void CacheStorageCache::InitGotCacheSize(base::OnceClosure callback, 1505 void CacheStorageCache::InitGotCacheSize(base::OnceClosure callback,
1699 CacheStorageError cache_create_error, 1506 CacheStorageError cache_create_error,
1700 int cache_size) { 1507 int cache_size) {
1701 // Now that we know the cache size either 1) the cache size should be unknown 1508 // Now that we know the cache size either 1) the cache size should be unknown
1702 // (which is why the size was calculated), or 2) it must match the current 1509 // (which is why the size was calculated), or 2) it must match the current
1703 // size. If the sizes aren't equal then there is a bug in how the cache size 1510 // size. If the sizes aren't equal then there is a bug in how the cache size
1704 // is saved in the store's index. 1511 // is saved in the store's index.
1705 if (cache_size_ != CacheStorage::kSizeUnknown) { 1512 if (cache_size_ != CacheStorage::kSizeUnknown) {
1706 DLOG_IF(ERROR, cache_size_ != cache_size) 1513 DLOG_IF(ERROR, cache_size_ != cache_size)
1707 << "Cache size: " << cache_size 1514 << "Cache size: " << cache_size
1708 << " does not match size from index: " << cache_size_; 1515 << " does not match size from index: " << cache_size_;
1709 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference", 1516 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference",
1710 std::abs(cache_size_ - cache_size)); 1517 std::abs(cache_size_ - cache_size));
1711 if (cache_size_ != cache_size) { 1518 // Disabled for crbug.com/681900.
1712 // We assume that if the sizes match then then cached padding is still 1519 // DCHECK_EQ(cache_size_, cache_size);
1713 // correct. If not then we recalculate the padding.
1714 CalculateCacheSizePaddingGotSize(
1715 base::BindOnce(&CacheStorageCache::InitGotCacheSizeAndPadding,
1716 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1717 cache_create_error),
1718 cache_size);
1719 return;
1720 }
1721 } 1520 }
1722
1723 if (cache_padding_ == CacheStorage::kSizeUnknown || cache_padding_ < 0) {
1724 CalculateCacheSizePaddingGotSize(
1725 base::BindOnce(&CacheStorageCache::InitGotCacheSizeAndPadding,
1726 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1727 cache_create_error),
1728 cache_size);
1729 return;
1730 }
1731
1732 // If cached size matches actual size then assume cached padding is still
1733 // correct.
1734 InitGotCacheSizeAndPadding(std::move(callback), cache_create_error,
1735 cache_size, cache_padding_);
1736 }
1737
1738 void CacheStorageCache::InitGotCacheSizeAndPadding(
1739 base::OnceClosure callback,
1740 CacheStorageError cache_create_error,
1741 int64_t cache_size,
1742 int64_t cache_padding) {
1743 cache_size_ = cache_size; 1521 cache_size_ = cache_size;
1744 cache_padding_ = cache_padding;
1745
1746 initializing_ = false; 1522 initializing_ = false;
1747 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && 1523 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ &&
1748 backend_state_ == BACKEND_UNINITIALIZED) 1524 backend_state_ == BACKEND_UNINITIALIZED)
1749 ? BACKEND_OPEN 1525 ? BACKEND_OPEN
1750 : BACKEND_CLOSED; 1526 : BACKEND_CLOSED;
1751 1527
1752 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1528 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1753 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1529 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1754 1530
1755 if (cache_observer_) 1531 if (cache_observer_)
1756 cache_observer_->CacheSizeUpdated(this); 1532 cache_observer_->CacheSizeUpdated(this, cache_size_);
1757 1533
1758 std::move(callback).Run(); 1534 std::move(callback).Run();
1759 } 1535 }
1760 1536
1761 std::unique_ptr<storage::BlobDataHandle> 1537 std::unique_ptr<storage::BlobDataHandle>
1762 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry, 1538 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry,
1763 ServiceWorkerResponse* response) { 1539 ServiceWorkerResponse* response) {
1764 DCHECK(blob_storage_context_); 1540 DCHECK(blob_storage_context_);
1765 1541
1766 // Create a blob with the response body data. 1542 // Create a blob with the response body data.
(...skipping 17 matching lines...) Expand all
1784 } 1560 }
1785 1561
1786 return result; 1562 return result;
1787 } 1563 }
1788 1564
1789 std::unique_ptr<CacheStorageCacheHandle> 1565 std::unique_ptr<CacheStorageCacheHandle>
1790 CacheStorageCache::CreateCacheHandle() { 1566 CacheStorageCache::CreateCacheHandle() {
1791 return cache_storage_->CreateCacheHandle(this); 1567 return cache_storage_->CreateCacheHandle(this);
1792 } 1568 }
1793 1569
1794 int64_t CacheStorageCache::PaddedCacheSize() const {
1795 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1796 if (cache_size_ == CacheStorage::kSizeUnknown ||
1797 cache_padding_ == CacheStorage::kSizeUnknown) {
1798 return CacheStorage::kSizeUnknown;
1799 }
1800 return cache_size_ + cache_padding_;
1801 }
1802
1803 } // namespace content 1570 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache.h ('k') | content/browser/cache_storage/cache_storage_cache_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698