Chromium Code Reviews

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

Issue 2416713002: Write out CacheStorageCache size to index file. (Closed)
Patch Set: Only updating cache sizes for non-doomed caches Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 <limits> 9 #include <limits>
10 #include <memory> 10 #include <memory>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/barrier_closure.h" 14 #include "base/barrier_closure.h"
15 #include "base/bind_helpers.h" 15 #include "base/bind_helpers.h"
16 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
17 #include "base/guid.h" 17 #include "base/guid.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
20 #include "base/metrics/histogram_macros.h" 20 #include "base/metrics/histogram_macros.h"
21 #include "base/strings/string_split.h" 21 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
23 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
24 #include "content/browser/cache_storage/cache_storage.pb.h" 24 #include "content/browser/cache_storage/cache_storage.pb.h"
25 #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"
26 #include "content/browser/cache_storage/cache_storage_cache_handle.h" 26 #include "content/browser/cache_storage/cache_storage_cache_handle.h"
27 #include "content/browser/cache_storage/cache_storage_cache_observer.h"
27 #include "content/browser/cache_storage/cache_storage_scheduler.h" 28 #include "content/browser/cache_storage/cache_storage_scheduler.h"
28 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
29 #include "content/public/common/referrer.h" 30 #include "content/public/common/referrer.h"
30 #include "net/base/completion_callback.h" 31 #include "net/base/completion_callback.h"
31 #include "net/base/io_buffer.h" 32 #include "net/base/io_buffer.h"
32 #include "net/base/net_errors.h" 33 #include "net/base/net_errors.h"
33 #include "net/disk_cache/disk_cache.h" 34 #include "net/disk_cache/disk_cache.h"
34 #include "net/url_request/url_request_context_getter.h" 35 #include "net/url_request/url_request_context_getter.h"
35 #include "storage/browser/blob/blob_data_builder.h" 36 #include "storage/browser/blob/blob_data_builder.h"
36 #include "storage/browser/blob/blob_data_handle.h" 37 #include "storage/browser/blob/blob_data_handle.h"
(...skipping 219 matching lines...)
256 }; 257 };
257 258
258 // static 259 // static
259 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 260 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
260 const GURL& origin, 261 const GURL& origin,
261 const std::string& cache_name, 262 const std::string& cache_name,
262 CacheStorage* cache_storage, 263 CacheStorage* cache_storage,
263 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 264 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
264 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 265 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
265 base::WeakPtr<storage::BlobStorageContext> blob_context) { 266 base::WeakPtr<storage::BlobStorageContext> blob_context) {
266 CacheStorageCache* cache = 267 CacheStorageCache* cache = new CacheStorageCache(
jkarlin 2016/12/14 14:19:55 As part of the transition to MakeUnique, could you
cmumford 2016/12/15 22:29:14 The CacheStorageCache constructor is provide, so t
267 new CacheStorageCache(origin, cache_name, base::FilePath(), cache_storage, 268 origin, cache_name, base::FilePath(), cache_storage,
268 std::move(request_context_getter), 269 std::move(request_context_getter), std::move(quota_manager_proxy),
269 std::move(quota_manager_proxy), blob_context); 270 blob_context, 0 /* cache_size */);
270 cache->InitBackend(); 271 cache->SetObserver(cache_storage), cache->InitBackend();
271 return base::WrapUnique(cache); 272 return base::WrapUnique(cache);
272 } 273 }
273 274
274 // static 275 // static
275 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache( 276 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache(
276 const GURL& origin, 277 const GURL& origin,
277 const std::string& cache_name, 278 const std::string& cache_name,
278 CacheStorage* cache_storage, 279 CacheStorage* cache_storage,
279 const base::FilePath& path, 280 const base::FilePath& path,
280 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 281 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
281 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 282 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
282 base::WeakPtr<storage::BlobStorageContext> blob_context) { 283 base::WeakPtr<storage::BlobStorageContext> blob_context,
283 CacheStorageCache* cache = 284 int64_t cache_size) {
284 new CacheStorageCache(origin, cache_name, path, cache_storage, 285 CacheStorageCache* cache = new CacheStorageCache(
jkarlin 2016/12/14 14:19:55 Same.
cmumford 2016/12/15 22:29:14 see above.
285 std::move(request_context_getter), 286 origin, cache_name, path, cache_storage,
286 std::move(quota_manager_proxy), blob_context); 287 std::move(request_context_getter), std::move(quota_manager_proxy),
287 cache->InitBackend(); 288 blob_context, cache_size);
289 cache->SetObserver(cache_storage), cache->InitBackend();
288 return base::WrapUnique(cache); 290 return base::WrapUnique(cache);
289 } 291 }
290 292
291 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { 293 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() {
292 return weak_ptr_factory_.GetWeakPtr(); 294 return weak_ptr_factory_.GetWeakPtr();
293 } 295 }
294 296
295 void CacheStorageCache::Match( 297 void CacheStorageCache::Match(
296 std::unique_ptr<ServiceWorkerFetchRequest> request, 298 std::unique_ptr<ServiceWorkerFetchRequest> request,
297 const CacheStorageCacheQueryParams& match_params, 299 const CacheStorageCacheQueryParams& match_params,
(...skipping 193 matching lines...)
491 return; 493 return;
492 } 494 }
493 495
494 scheduler_->ScheduleOperation( 496 scheduler_->ScheduleOperation(
495 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 497 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
496 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize, 498 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize,
497 weak_ptr_factory_.GetWeakPtr(), 499 weak_ptr_factory_.GetWeakPtr(),
498 scheduler_->WrapCallbackToRunNext(callback)))); 500 scheduler_->WrapCallbackToRunNext(callback))));
499 } 501 }
500 502
503 void CacheStorageCache::SetObserver(CacheStorageCacheObserver* observer) {
jkarlin 2016/12/14 14:19:56 Perhaps: DCHECK(observer ^ cache_observer_)?
cmumford 2016/12/15 22:29:14 Done.
504 cache_observer_ = observer;
505 }
506
501 CacheStorageCache::~CacheStorageCache() { 507 CacheStorageCache::~CacheStorageCache() {
502 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_); 508 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_);
503 } 509 }
504 510
505 CacheStorageCache::CacheStorageCache( 511 CacheStorageCache::CacheStorageCache(
506 const GURL& origin, 512 const GURL& origin,
507 const std::string& cache_name, 513 const std::string& cache_name,
508 const base::FilePath& path, 514 const base::FilePath& path,
509 CacheStorage* cache_storage, 515 CacheStorage* cache_storage,
510 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 516 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
511 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 517 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
512 base::WeakPtr<storage::BlobStorageContext> blob_context) 518 base::WeakPtr<storage::BlobStorageContext> blob_context,
519 int64_t cache_size)
513 : origin_(origin), 520 : origin_(origin),
514 cache_name_(cache_name), 521 cache_name_(cache_name),
515 path_(path), 522 path_(path),
516 cache_storage_(cache_storage), 523 cache_storage_(cache_storage),
517 request_context_getter_(std::move(request_context_getter)), 524 request_context_getter_(std::move(request_context_getter)),
518 quota_manager_proxy_(std::move(quota_manager_proxy)), 525 quota_manager_proxy_(std::move(quota_manager_proxy)),
519 blob_storage_context_(blob_context), 526 blob_storage_context_(blob_context),
520 scheduler_( 527 scheduler_(
521 new CacheStorageScheduler(CacheStorageSchedulerClient::CLIENT_CACHE)), 528 new CacheStorageScheduler(CacheStorageSchedulerClient::CLIENT_CACHE)),
529 cache_size_(cache_size),
522 max_query_size_bytes_(kMaxQueryCacheResultBytes), 530 max_query_size_bytes_(kMaxQueryCacheResultBytes),
531 cache_observer_(nullptr),
523 memory_only_(path.empty()), 532 memory_only_(path.empty()),
524 weak_ptr_factory_(this) { 533 weak_ptr_factory_(this) {
525 DCHECK(!origin_.is_empty()); 534 DCHECK(!origin_.is_empty());
526 DCHECK(quota_manager_proxy_.get()); 535 DCHECK(quota_manager_proxy_.get());
527 536
528 quota_manager_proxy_->NotifyOriginInUse(origin_); 537 quota_manager_proxy_->NotifyOriginInUse(origin_);
529 } 538 }
530 539
531 void CacheStorageCache::QueryCache( 540 void CacheStorageCache::QueryCache(
532 std::unique_ptr<ServiceWorkerFetchRequest> request, 541 std::unique_ptr<ServiceWorkerFetchRequest> request,
(...skipping 645 matching lines...)
1178 &CacheStorageCache::UpdateCacheSizeGotSize, 1187 &CacheStorageCache::UpdateCacheSizeGotSize,
1179 weak_ptr_factory_.GetWeakPtr(), base::Passed(CreateCacheHandle()))); 1188 weak_ptr_factory_.GetWeakPtr(), base::Passed(CreateCacheHandle())));
1180 1189
1181 if (rv != net::ERR_IO_PENDING) 1190 if (rv != net::ERR_IO_PENDING)
1182 UpdateCacheSizeGotSize(CreateCacheHandle(), rv); 1191 UpdateCacheSizeGotSize(CreateCacheHandle(), rv);
1183 } 1192 }
1184 1193
1185 void CacheStorageCache::UpdateCacheSizeGotSize( 1194 void CacheStorageCache::UpdateCacheSizeGotSize(
1186 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 1195 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1187 int current_cache_size) { 1196 int current_cache_size) {
1197 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown);
1188 int64_t old_cache_size = cache_size_; 1198 int64_t old_cache_size = cache_size_;
1189 cache_size_ = current_cache_size; 1199 cache_size_ = current_cache_size;
1190 1200
1201 int64_t size_delta = current_cache_size - old_cache_size;
1202
1191 quota_manager_proxy_->NotifyStorageModified( 1203 quota_manager_proxy_->NotifyStorageModified(
1192 storage::QuotaClient::kServiceWorkerCache, origin_, 1204 storage::QuotaClient::kServiceWorkerCache, origin_,
1193 storage::kStorageTypeTemporary, current_cache_size - old_cache_size); 1205 storage::kStorageTypeTemporary, size_delta);
1206
1207 if (cache_observer_)
1208 cache_observer_->CacheSizeUpdated(this, current_cache_size);
1194 } 1209 }
1195 1210
1196 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, 1211 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
1197 const ErrorCallback& callback) { 1212 const ErrorCallback& callback) {
1198 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1213 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1199 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, 1214 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
1200 operation.operation_type); 1215 operation.operation_type);
1201 1216
1202 std::unique_ptr<ServiceWorkerFetchRequest> request( 1217 std::unique_ptr<ServiceWorkerFetchRequest> request(
1203 new ServiceWorkerFetchRequest( 1218 new ServiceWorkerFetchRequest(
(...skipping 164 matching lines...)
1368 base::Bind(&CacheStorageCache::InitGotCacheSize, 1383 base::Bind(&CacheStorageCache::InitGotCacheSize,
1369 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error)); 1384 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error));
1370 1385
1371 if (rv != net::ERR_IO_PENDING) 1386 if (rv != net::ERR_IO_PENDING)
1372 InitGotCacheSize(callback, cache_create_error, rv); 1387 InitGotCacheSize(callback, cache_create_error, rv);
1373 } 1388 }
1374 1389
1375 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback, 1390 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback,
1376 CacheStorageError cache_create_error, 1391 CacheStorageError cache_create_error,
1377 int cache_size) { 1392 int cache_size) {
1393 // Now that we know the cache size either 1) the cache size should be unknown
1394 // (which is why the size was calculated), or 2) it must match the current
1395 // size. If the sizes aren't equal then there is a bug in how the cache size
1396 // is saved in the store's index.
1397 if (cache_size_ != CacheStorage::kSizeUnknown && cache_size_ != cache_size) {
1398 LOG(ERROR) << "Cache size/index mismatch";
jkarlin 2016/12/14 14:19:55 Can you add a TODO to add UMA for this as well?
cmumford 2016/12/15 22:29:14 Done.
1399 NOTREACHED();
1400 }
1378 cache_size_ = cache_size; 1401 cache_size_ = cache_size;
1379 initializing_ = false; 1402 initializing_ = false;
1380 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && 1403 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ &&
1381 backend_state_ == BACKEND_UNINITIALIZED) 1404 backend_state_ == BACKEND_UNINITIALIZED)
1382 ? BACKEND_OPEN 1405 ? BACKEND_OPEN
1383 : BACKEND_CLOSED; 1406 : BACKEND_CLOSED;
1384 1407
1385 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1408 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1386 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1409 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1387 1410
1411 if (cache_observer_)
1412 cache_observer_->CacheSizeUpdated(this, cache_size_);
1413
1388 callback.Run(); 1414 callback.Run();
1389 } 1415 }
1390 1416
1391 void CacheStorageCache::PopulateRequestFromMetadata( 1417 void CacheStorageCache::PopulateRequestFromMetadata(
1392 const proto::CacheMetadata& metadata, 1418 const proto::CacheMetadata& metadata,
1393 const GURL& request_url, 1419 const GURL& request_url,
1394 ServiceWorkerFetchRequest* request) { 1420 ServiceWorkerFetchRequest* request) {
1395 *request = 1421 *request =
1396 ServiceWorkerFetchRequest(request_url, metadata.request().method(), 1422 ServiceWorkerFetchRequest(request_url, metadata.request().method(),
1397 ServiceWorkerHeaderMap(), Referrer(), false); 1423 ServiceWorkerHeaderMap(), Referrer(), false);
(...skipping 45 matching lines...)
1443 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1469 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1444 return blob_storage_context_->AddFinishedBlob(&blob_data); 1470 return blob_storage_context_->AddFinishedBlob(&blob_data);
1445 } 1471 }
1446 1472
1447 std::unique_ptr<CacheStorageCacheHandle> 1473 std::unique_ptr<CacheStorageCacheHandle>
1448 CacheStorageCache::CreateCacheHandle() { 1474 CacheStorageCache::CreateCacheHandle() {
1449 return cache_storage_->CreateCacheHandle(this); 1475 return cache_storage_->CreateCacheHandle(this);
1450 } 1476 }
1451 1477
1452 } // namespace content 1478 } // namespace content
OLDNEW

Powered by Google App Engine