| OLD | NEW |
| 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...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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( |
| 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( |
| 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...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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) { |
| 504 DCHECK((observer == nullptr) ^ (cache_observer_ == nullptr)); |
| 505 cache_observer_ = observer; |
| 506 } |
| 507 |
| 501 CacheStorageCache::~CacheStorageCache() { | 508 CacheStorageCache::~CacheStorageCache() { |
| 502 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_); | 509 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_); |
| 503 } | 510 } |
| 504 | 511 |
| 505 CacheStorageCache::CacheStorageCache( | 512 CacheStorageCache::CacheStorageCache( |
| 506 const GURL& origin, | 513 const GURL& origin, |
| 507 const std::string& cache_name, | 514 const std::string& cache_name, |
| 508 const base::FilePath& path, | 515 const base::FilePath& path, |
| 509 CacheStorage* cache_storage, | 516 CacheStorage* cache_storage, |
| 510 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 517 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 511 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 518 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 512 base::WeakPtr<storage::BlobStorageContext> blob_context) | 519 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 520 int64_t cache_size) |
| 513 : origin_(origin), | 521 : origin_(origin), |
| 514 cache_name_(cache_name), | 522 cache_name_(cache_name), |
| 515 path_(path), | 523 path_(path), |
| 516 cache_storage_(cache_storage), | 524 cache_storage_(cache_storage), |
| 517 request_context_getter_(std::move(request_context_getter)), | 525 request_context_getter_(std::move(request_context_getter)), |
| 518 quota_manager_proxy_(std::move(quota_manager_proxy)), | 526 quota_manager_proxy_(std::move(quota_manager_proxy)), |
| 519 blob_storage_context_(blob_context), | 527 blob_storage_context_(blob_context), |
| 520 scheduler_( | 528 scheduler_( |
| 521 new CacheStorageScheduler(CacheStorageSchedulerClient::CLIENT_CACHE)), | 529 new CacheStorageScheduler(CacheStorageSchedulerClient::CLIENT_CACHE)), |
| 530 cache_size_(cache_size), |
| 522 max_query_size_bytes_(kMaxQueryCacheResultBytes), | 531 max_query_size_bytes_(kMaxQueryCacheResultBytes), |
| 532 cache_observer_(nullptr), |
| 523 memory_only_(path.empty()), | 533 memory_only_(path.empty()), |
| 524 weak_ptr_factory_(this) { | 534 weak_ptr_factory_(this) { |
| 525 DCHECK(!origin_.is_empty()); | 535 DCHECK(!origin_.is_empty()); |
| 526 DCHECK(quota_manager_proxy_.get()); | 536 DCHECK(quota_manager_proxy_.get()); |
| 527 | 537 |
| 528 quota_manager_proxy_->NotifyOriginInUse(origin_); | 538 quota_manager_proxy_->NotifyOriginInUse(origin_); |
| 529 } | 539 } |
| 530 | 540 |
| 531 void CacheStorageCache::QueryCache( | 541 void CacheStorageCache::QueryCache( |
| 532 std::unique_ptr<ServiceWorkerFetchRequest> request, | 542 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 &CacheStorageCache::UpdateCacheSizeGotSize, | 1182 &CacheStorageCache::UpdateCacheSizeGotSize, |
| 1173 weak_ptr_factory_.GetWeakPtr(), base::Passed(CreateCacheHandle()))); | 1183 weak_ptr_factory_.GetWeakPtr(), base::Passed(CreateCacheHandle()))); |
| 1174 | 1184 |
| 1175 if (rv != net::ERR_IO_PENDING) | 1185 if (rv != net::ERR_IO_PENDING) |
| 1176 UpdateCacheSizeGotSize(CreateCacheHandle(), rv); | 1186 UpdateCacheSizeGotSize(CreateCacheHandle(), rv); |
| 1177 } | 1187 } |
| 1178 | 1188 |
| 1179 void CacheStorageCache::UpdateCacheSizeGotSize( | 1189 void CacheStorageCache::UpdateCacheSizeGotSize( |
| 1180 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 1190 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 1181 int current_cache_size) { | 1191 int current_cache_size) { |
| 1192 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown); |
| 1182 int64_t old_cache_size = cache_size_; | 1193 int64_t old_cache_size = cache_size_; |
| 1183 cache_size_ = current_cache_size; | 1194 cache_size_ = current_cache_size; |
| 1184 | 1195 |
| 1196 int64_t size_delta = current_cache_size - old_cache_size; |
| 1197 |
| 1185 quota_manager_proxy_->NotifyStorageModified( | 1198 quota_manager_proxy_->NotifyStorageModified( |
| 1186 storage::QuotaClient::kServiceWorkerCache, origin_, | 1199 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 1187 storage::kStorageTypeTemporary, current_cache_size - old_cache_size); | 1200 storage::kStorageTypeTemporary, size_delta); |
| 1201 |
| 1202 if (cache_observer_) |
| 1203 cache_observer_->CacheSizeUpdated(this, current_cache_size); |
| 1188 } | 1204 } |
| 1189 | 1205 |
| 1190 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, | 1206 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, |
| 1191 const ErrorCallback& callback) { | 1207 const ErrorCallback& callback) { |
| 1192 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); | 1208 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); |
| 1193 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, | 1209 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, |
| 1194 operation.operation_type); | 1210 operation.operation_type); |
| 1195 | 1211 |
| 1196 std::unique_ptr<ServiceWorkerFetchRequest> request( | 1212 std::unique_ptr<ServiceWorkerFetchRequest> request( |
| 1197 new ServiceWorkerFetchRequest( | 1213 new ServiceWorkerFetchRequest( |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1362 base::Bind(&CacheStorageCache::InitGotCacheSize, | 1378 base::Bind(&CacheStorageCache::InitGotCacheSize, |
| 1363 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error)); | 1379 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error)); |
| 1364 | 1380 |
| 1365 if (rv != net::ERR_IO_PENDING) | 1381 if (rv != net::ERR_IO_PENDING) |
| 1366 InitGotCacheSize(callback, cache_create_error, rv); | 1382 InitGotCacheSize(callback, cache_create_error, rv); |
| 1367 } | 1383 } |
| 1368 | 1384 |
| 1369 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback, | 1385 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback, |
| 1370 CacheStorageError cache_create_error, | 1386 CacheStorageError cache_create_error, |
| 1371 int cache_size) { | 1387 int cache_size) { |
| 1388 // Now that we know the cache size either 1) the cache size should be unknown |
| 1389 // (which is why the size was calculated), or 2) it must match the current |
| 1390 // size. If the sizes aren't equal then there is a bug in how the cache size |
| 1391 // is saved in the store's index. |
| 1392 if (cache_size_ != CacheStorage::kSizeUnknown && cache_size_ != cache_size) { |
| 1393 // TODO(cmumford): Add UMA for this. |
| 1394 LOG(ERROR) << "Cache size/index mismatch"; |
| 1395 DCHECK_EQ(cache_size_, cache_size); |
| 1396 } |
| 1372 cache_size_ = cache_size; | 1397 cache_size_ = cache_size; |
| 1373 initializing_ = false; | 1398 initializing_ = false; |
| 1374 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && | 1399 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && |
| 1375 backend_state_ == BACKEND_UNINITIALIZED) | 1400 backend_state_ == BACKEND_UNINITIALIZED) |
| 1376 ? BACKEND_OPEN | 1401 ? BACKEND_OPEN |
| 1377 : BACKEND_CLOSED; | 1402 : BACKEND_CLOSED; |
| 1378 | 1403 |
| 1379 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", | 1404 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", |
| 1380 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); | 1405 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); |
| 1381 | 1406 |
| 1407 if (cache_observer_) |
| 1408 cache_observer_->CacheSizeUpdated(this, cache_size_); |
| 1409 |
| 1382 callback.Run(); | 1410 callback.Run(); |
| 1383 } | 1411 } |
| 1384 | 1412 |
| 1385 void CacheStorageCache::PopulateRequestFromMetadata( | 1413 void CacheStorageCache::PopulateRequestFromMetadata( |
| 1386 const proto::CacheMetadata& metadata, | 1414 const proto::CacheMetadata& metadata, |
| 1387 const GURL& request_url, | 1415 const GURL& request_url, |
| 1388 ServiceWorkerFetchRequest* request) { | 1416 ServiceWorkerFetchRequest* request) { |
| 1389 *request = | 1417 *request = |
| 1390 ServiceWorkerFetchRequest(request_url, metadata.request().method(), | 1418 ServiceWorkerFetchRequest(request_url, metadata.request().method(), |
| 1391 ServiceWorkerHeaderMap(), Referrer(), false); | 1419 ServiceWorkerHeaderMap(), Referrer(), false); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); | 1480 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); |
| 1453 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1481 return blob_storage_context_->AddFinishedBlob(&blob_data); |
| 1454 } | 1482 } |
| 1455 | 1483 |
| 1456 std::unique_ptr<CacheStorageCacheHandle> | 1484 std::unique_ptr<CacheStorageCacheHandle> |
| 1457 CacheStorageCache::CreateCacheHandle() { | 1485 CacheStorageCache::CreateCacheHandle() { |
| 1458 return cache_storage_->CreateCacheHandle(this); | 1486 return cache_storage_->CreateCacheHandle(this); |
| 1459 } | 1487 } |
| 1460 | 1488 |
| 1461 } // namespace content | 1489 } // namespace content |
| OLD | NEW |