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

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

Issue 2056983004: [CacheStorage] Give ownership of all CacheStorageCaches to CacheStorage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Self review Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/cache_storage/cache_storage_cache.h" 5 #include "content/browser/cache_storage/cache_storage_cache.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/barrier_closure.h" 11 #include "base/barrier_closure.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
16 #include "base/strings/string_split.h" 16 #include "base/strings/string_split.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "content/browser/cache_storage/cache_storage.pb.h" 19 #include "content/browser/cache_storage/cache_storage.pb.h"
20 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h" 20 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h"
21 #include "content/browser/cache_storage/cache_storage_cache_handle.h"
21 #include "content/browser/cache_storage/cache_storage_scheduler.h" 22 #include "content/browser/cache_storage/cache_storage_scheduler.h"
22 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
23 #include "content/public/common/referrer.h" 24 #include "content/public/common/referrer.h"
24 #include "net/base/completion_callback.h" 25 #include "net/base/completion_callback.h"
25 #include "net/base/io_buffer.h" 26 #include "net/base/io_buffer.h"
26 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
27 #include "net/disk_cache/disk_cache.h" 28 #include "net/disk_cache/disk_cache.h"
28 #include "net/url_request/url_request_context_getter.h" 29 #include "net/url_request/url_request_context_getter.h"
29 #include "storage/browser/blob/blob_data_builder.h" 30 #include "storage/browser/blob/blob_data_builder.h"
30 #include "storage/browser/blob/blob_data_handle.h" 31 #include "storage/browser/blob/blob_data_handle.h"
31 #include "storage/browser/blob/blob_storage_context.h" 32 #include "storage/browser/blob/blob_storage_context.h"
32 #include "storage/browser/blob/blob_url_request_job_factory.h" 33 #include "storage/browser/blob/blob_url_request_job_factory.h"
33 #include "storage/browser/quota/quota_manager_proxy.h" 34 #include "storage/browser/quota/quota_manager_proxy.h"
34 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerResponseType.h" 35 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerResponseType.h"
35 36
36 namespace content { 37 namespace content {
37 38
38 namespace { 39 namespace {
39 40
40 // This class ensures that the cache and the entry have a lifetime as long as 41 // This class ensures that the cache and the entry have a lifetime as long as
41 // the blob that is created to contain them. 42 // the blob that is created to contain them.
42 class CacheStorageCacheDataHandle 43 class CacheStorageCacheDataHandle
43 : public storage::BlobDataBuilder::DataHandle { 44 : public storage::BlobDataBuilder::DataHandle {
44 public: 45 public:
45 CacheStorageCacheDataHandle(scoped_refptr<CacheStorageCache> cache, 46 CacheStorageCacheDataHandle(
46 disk_cache::ScopedEntryPtr entry) 47 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
47 : cache_(cache), entry_(std::move(entry)) {} 48 disk_cache::ScopedEntryPtr entry)
49 : cache_handle_(std::move(cache_handle)), entry_(std::move(entry)) {}
48 50
49 private: 51 private:
50 ~CacheStorageCacheDataHandle() override {} 52 ~CacheStorageCacheDataHandle() override {}
51 53
52 scoped_refptr<CacheStorageCache> cache_; 54 std::unique_ptr<CacheStorageCacheHandle> cache_handle_;
53 disk_cache::ScopedEntryPtr entry_; 55 disk_cache::ScopedEntryPtr entry_;
54 56
55 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle); 57 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle);
56 }; 58 };
57 59
58 typedef base::Callback<void(std::unique_ptr<CacheMetadata>)> MetadataCallback; 60 typedef base::Callback<void(std::unique_ptr<CacheMetadata>)> MetadataCallback;
59 61
60 // The maximum size of each cache. Ultimately, cache size 62 // The maximum size of each cache. Ultimately, cache size
61 // is controlled per-origin by the QuotaManager. 63 // is controlled per-origin by the QuotaManager.
62 const int kMaxCacheBytes = std::numeric_limits<int>::max(); 64 const int kMaxCacheBytes = std::numeric_limits<int>::max();
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 276 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
275 CacheStorageCache::ErrorCallback callback; 277 CacheStorageCache::ErrorCallback callback;
276 disk_cache::ScopedEntryPtr cache_entry; 278 disk_cache::ScopedEntryPtr cache_entry;
277 int64_t available_bytes = 0; 279 int64_t available_bytes = 0;
278 280
279 private: 281 private:
280 DISALLOW_COPY_AND_ASSIGN(PutContext); 282 DISALLOW_COPY_AND_ASSIGN(PutContext);
281 }; 283 };
282 284
283 // static 285 // static
284 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 286 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
285 const GURL& origin, 287 const GURL& origin,
286 const std::string& cache_name, 288 const std::string& cache_name,
289 CacheStorage* cache_storage,
287 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 290 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
288 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 291 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
289 base::WeakPtr<storage::BlobStorageContext> blob_context) { 292 base::WeakPtr<storage::BlobStorageContext> blob_context) {
290 return make_scoped_refptr(new CacheStorageCache( 293 return std::unique_ptr<CacheStorageCache>(
291 origin, cache_name, base::FilePath(), std::move(request_context_getter), 294 new CacheStorageCache(origin, cache_name, base::FilePath(), cache_storage,
292 std::move(quota_manager_proxy), blob_context)); 295 std::move(request_context_getter),
296 std::move(quota_manager_proxy), blob_context));
293 } 297 }
294 298
295 // static 299 // static
296 scoped_refptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache( 300 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache(
297 const GURL& origin, 301 const GURL& origin,
298 const std::string& cache_name, 302 const std::string& cache_name,
303 CacheStorage* cache_storage,
299 const base::FilePath& path, 304 const base::FilePath& path,
300 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 305 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
301 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 306 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
302 base::WeakPtr<storage::BlobStorageContext> blob_context) { 307 base::WeakPtr<storage::BlobStorageContext> blob_context) {
303 return make_scoped_refptr(new CacheStorageCache( 308 return std::unique_ptr<CacheStorageCache>(
304 origin, cache_name, path, std::move(request_context_getter), 309 new CacheStorageCache(origin, cache_name, path, cache_storage,
305 std::move(quota_manager_proxy), blob_context)); 310 std::move(request_context_getter),
306 } 311 std::move(quota_manager_proxy), blob_context));
307
308 CacheStorageCache::~CacheStorageCache() {
309 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_);
310 } 312 }
311 313
312 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { 314 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() {
313 return weak_ptr_factory_.GetWeakPtr(); 315 return weak_ptr_factory_.GetWeakPtr();
314 } 316 }
315 317
316 void CacheStorageCache::Match( 318 void CacheStorageCache::Match(
317 std::unique_ptr<ServiceWorkerFetchRequest> request, 319 std::unique_ptr<ServiceWorkerFetchRequest> request,
318 const ResponseCallback& callback) { 320 const ResponseCallback& callback) {
319 if (!LazyInitialize()) { 321 if (!LazyInitialize()) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 const std::vector<CacheStorageBatchOperation>& operations, 376 const std::vector<CacheStorageBatchOperation>& operations,
375 const ErrorCallback& callback) { 377 const ErrorCallback& callback) {
376 if (!LazyInitialize()) { 378 if (!LazyInitialize()) {
377 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 379 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
378 return; 380 return;
379 } 381 }
380 382
381 std::unique_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback)); 383 std::unique_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback));
382 ErrorCallback* callback_ptr = callback_copy.get(); 384 ErrorCallback* callback_ptr = callback_copy.get();
383 base::Closure barrier_closure = base::BarrierClosure( 385 base::Closure barrier_closure = base::BarrierClosure(
384 operations.size(), 386 operations.size(), base::Bind(&CacheStorageCache::BatchDidAllOperations,
385 base::Bind(&CacheStorageCache::BatchDidAllOperations, this, 387 weak_ptr_factory_.GetWeakPtr(),
386 base::Passed(std::move(callback_copy)))); 388 base::Passed(std::move(callback_copy))));
387 ErrorCallback completion_callback = 389 ErrorCallback completion_callback =
388 base::Bind(&CacheStorageCache::BatchDidOneOperation, this, 390 base::Bind(&CacheStorageCache::BatchDidOneOperation,
389 barrier_closure, callback_ptr); 391 weak_ptr_factory_.GetWeakPtr(), barrier_closure, callback_ptr);
390 392
391 for (const auto& operation : operations) { 393 for (const auto& operation : operations) {
392 switch (operation.operation_type) { 394 switch (operation.operation_type) {
393 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT: 395 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT:
394 Put(operation, completion_callback); 396 Put(operation, completion_callback);
395 break; 397 break;
396 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE: 398 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE:
397 DCHECK_EQ(1u, operations.size()); 399 DCHECK_EQ(1u, operations.size());
398 Delete(operation, completion_callback); 400 Delete(operation, completion_callback);
399 break; 401 break;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 SizeCallback pending_callback = 487 SizeCallback pending_callback =
486 base::Bind(&CacheStorageCache::PendingSizeCallback, 488 base::Bind(&CacheStorageCache::PendingSizeCallback,
487 weak_ptr_factory_.GetWeakPtr(), callback); 489 weak_ptr_factory_.GetWeakPtr(), callback);
488 490
489 scheduler_->ScheduleOperation( 491 scheduler_->ScheduleOperation(
490 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 492 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
491 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize, 493 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize,
492 weak_ptr_factory_.GetWeakPtr(), pending_callback))); 494 weak_ptr_factory_.GetWeakPtr(), pending_callback)));
493 } 495 }
494 496
497 CacheStorageCache::~CacheStorageCache() {
498 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_);
499 }
500
495 CacheStorageCache::CacheStorageCache( 501 CacheStorageCache::CacheStorageCache(
496 const GURL& origin, 502 const GURL& origin,
497 const std::string& cache_name, 503 const std::string& cache_name,
498 const base::FilePath& path, 504 const base::FilePath& path,
505 CacheStorage* cache_storage,
499 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 506 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
500 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 507 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
501 base::WeakPtr<storage::BlobStorageContext> blob_context) 508 base::WeakPtr<storage::BlobStorageContext> blob_context)
502 : origin_(origin), 509 : origin_(origin),
503 cache_name_(cache_name), 510 cache_name_(cache_name),
504 path_(path), 511 path_(path),
512 cache_storage_(cache_storage),
505 request_context_getter_(std::move(request_context_getter)), 513 request_context_getter_(std::move(request_context_getter)),
506 quota_manager_proxy_(std::move(quota_manager_proxy)), 514 quota_manager_proxy_(std::move(quota_manager_proxy)),
507 blob_storage_context_(blob_context), 515 blob_storage_context_(blob_context),
508 scheduler_(new CacheStorageScheduler()), 516 scheduler_(new CacheStorageScheduler()),
509 memory_only_(path.empty()), 517 memory_only_(path.empty()),
510 weak_ptr_factory_(this) { 518 weak_ptr_factory_(this) {
511 DCHECK(!origin_.is_empty()); 519 DCHECK(!origin_.is_empty());
512 DCHECK(quota_manager_proxy_.get()); 520 DCHECK(quota_manager_proxy_.get());
513 521
514 quota_manager_proxy_->NotifyOriginInUse(origin_); 522 quota_manager_proxy_->NotifyOriginInUse(origin_);
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 } 1144 }
1137 1145
1138 UpdateCacheSize(); 1146 UpdateCacheSize();
1139 put_context->callback.Run(CACHE_STORAGE_OK); 1147 put_context->callback.Run(CACHE_STORAGE_OK);
1140 } 1148 }
1141 1149
1142 void CacheStorageCache::UpdateCacheSize() { 1150 void CacheStorageCache::UpdateCacheSize() {
1143 if (backend_state_ != BACKEND_OPEN) 1151 if (backend_state_ != BACKEND_OPEN)
1144 return; 1152 return;
1145 1153
1146 // Note that the callback holds a refptr to |this| since UpdateCacheSize is 1154 // Note that the callback holds a cache handle to keep the cache alive during
1147 // often called after an operation completes and the cache might be freed. 1155 // the operation since this UpdateCacheSize is often run after an operation
1148 int rv = backend_->CalculateSizeOfAllEntries( 1156 // completes and runs its callback.
1149 base::Bind(&CacheStorageCache::UpdateCacheSizeGotSize, this)); 1157 int rv = backend_->CalculateSizeOfAllEntries(base::Bind(
1158 &CacheStorageCache::UpdateCacheSizeGotSize,
1159 weak_ptr_factory_.GetWeakPtr(), base::Passed(CreateCacheHandle())));
1150 1160
1151 if (rv != net::ERR_IO_PENDING) 1161 if (rv != net::ERR_IO_PENDING)
1152 UpdateCacheSizeGotSize(rv); 1162 UpdateCacheSizeGotSize(CreateCacheHandle(), rv);
1153 } 1163 }
1154 1164
1155 void CacheStorageCache::UpdateCacheSizeGotSize(int current_cache_size) { 1165 void CacheStorageCache::UpdateCacheSizeGotSize(
1166 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1167 int current_cache_size) {
1156 int64_t old_cache_size = cache_size_; 1168 int64_t old_cache_size = cache_size_;
1157 cache_size_ = current_cache_size; 1169 cache_size_ = current_cache_size;
1158 1170
1159 quota_manager_proxy_->NotifyStorageModified( 1171 quota_manager_proxy_->NotifyStorageModified(
1160 storage::QuotaClient::kServiceWorkerCache, origin_, 1172 storage::QuotaClient::kServiceWorkerCache, origin_,
1161 storage::kStorageTypeTemporary, current_cache_size - old_cache_size); 1173 storage::kStorageTypeTemporary, current_cache_size - old_cache_size);
1162 } 1174 }
1163 1175
1164 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, 1176 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
1165 const ErrorCallback& callback) { 1177 const ErrorCallback& callback) {
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 ServiceWorkerResponse* response) { 1550 ServiceWorkerResponse* response) {
1539 DCHECK(blob_storage_context_); 1551 DCHECK(blob_storage_context_);
1540 1552
1541 // Create a blob with the response body data. 1553 // Create a blob with the response body data.
1542 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); 1554 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY);
1543 response->blob_uuid = base::GenerateGUID(); 1555 response->blob_uuid = base::GenerateGUID();
1544 storage::BlobDataBuilder blob_data(response->blob_uuid); 1556 storage::BlobDataBuilder blob_data(response->blob_uuid);
1545 1557
1546 disk_cache::Entry* temp_entry = entry.get(); 1558 disk_cache::Entry* temp_entry = entry.get();
1547 blob_data.AppendDiskCacheEntryWithSideData( 1559 blob_data.AppendDiskCacheEntryWithSideData(
1548 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, 1560 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)),
1549 INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1561 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1550 return blob_storage_context_->AddFinishedBlob(&blob_data); 1562 return blob_storage_context_->AddFinishedBlob(&blob_data);
1551 } 1563 }
1552 1564
1565 std::unique_ptr<CacheStorageCacheHandle>
1566 CacheStorageCache::CreateCacheHandle() {
1567 return cache_storage_->CreateCacheHandle(this);
1568 }
1569
1553 } // namespace content 1570 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698