| 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_manager.h" | 5 #include "content/browser/cache_storage/cache_storage_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 #include "storage/browser/blob/blob_data_builder.h" | 41 #include "storage/browser/blob/blob_data_builder.h" |
| 42 #include "storage/browser/blob/blob_data_handle.h" | 42 #include "storage/browser/blob/blob_data_handle.h" |
| 43 #include "storage/browser/blob/blob_storage_context.h" | 43 #include "storage/browser/blob/blob_storage_context.h" |
| 44 #include "storage/browser/blob/blob_url_request_job_factory.h" | 44 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 45 #include "storage/browser/quota/quota_manager_proxy.h" | 45 #include "storage/browser/quota/quota_manager_proxy.h" |
| 46 #include "storage/browser/test/mock_quota_manager_proxy.h" | 46 #include "storage/browser/test/mock_quota_manager_proxy.h" |
| 47 #include "storage/browser/test/mock_special_storage_policy.h" | 47 #include "storage/browser/test/mock_special_storage_policy.h" |
| 48 #include "storage/common/blob_storage/blob_handle.h" | 48 #include "storage/common/blob_storage/blob_handle.h" |
| 49 #include "testing/gtest/include/gtest/gtest.h" | 49 #include "testing/gtest/include/gtest/gtest.h" |
| 50 | 50 |
| 51 using network::mojom::FetchResponseType; |
| 52 |
| 51 namespace content { | 53 namespace content { |
| 52 | 54 |
| 53 namespace { | 55 namespace { |
| 54 | 56 |
| 55 bool IsIndexFileCurrent(const base::FilePath& cache_dir) { | 57 bool IsIndexFileCurrent(const base::FilePath& cache_dir) { |
| 56 base::File::Info info; | 58 base::File::Info info; |
| 57 const base::FilePath index_path = | 59 const base::FilePath index_path = |
| 58 cache_dir.AppendASCII(CacheStorage::kIndexFileName); | 60 cache_dir.AppendASCII(CacheStorage::kIndexFileName); |
| 59 if (!GetFileInfo(index_path, &info)) | 61 if (!GetFileInfo(index_path, &info)) |
| 60 return false; | 62 return false; |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 base::RunLoop loop; | 344 base::RunLoop loop; |
| 343 cache_manager_->MatchAllCaches( | 345 cache_manager_->MatchAllCaches( |
| 344 origin, std::move(unique_request), match_params, | 346 origin, std::move(unique_request), match_params, |
| 345 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback, | 347 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback, |
| 346 base::Unretained(this), base::Unretained(&loop))); | 348 base::Unretained(this), base::Unretained(&loop))); |
| 347 loop.Run(); | 349 loop.Run(); |
| 348 | 350 |
| 349 return callback_error_ == CACHE_STORAGE_OK; | 351 return callback_error_ == CACHE_STORAGE_OK; |
| 350 } | 352 } |
| 351 | 353 |
| 352 bool CachePut(CacheStorageCache* cache, const GURL& url) { | 354 bool CachePut(CacheStorageCache* cache, |
| 355 const GURL& url, |
| 356 FetchResponseType response_type = FetchResponseType::kDefault) { |
| 353 ServiceWorkerFetchRequest request; | 357 ServiceWorkerFetchRequest request; |
| 354 request.url = url; | 358 request.url = url; |
| 355 | 359 |
| 356 return CachePutWithStatusCode(cache, request, 200); | 360 return CachePutWithStatusCode(cache, request, 200, response_type); |
| 357 } | 361 } |
| 358 | 362 |
| 359 bool CachePutWithRequestAndHeaders( | 363 bool CachePutWithRequestAndHeaders( |
| 360 CacheStorageCache* cache, | 364 CacheStorageCache* cache, |
| 361 const ServiceWorkerFetchRequest& request, | 365 const ServiceWorkerFetchRequest& request, |
| 362 const ServiceWorkerHeaderMap& response_headers) { | 366 const ServiceWorkerHeaderMap& response_headers, |
| 363 return CachePutWithStatusCode(cache, request, 200, response_headers); | 367 FetchResponseType response_type = FetchResponseType::kDefault) { |
| 368 return CachePutWithStatusCode(cache, request, 200, response_type, |
| 369 response_headers); |
| 364 } | 370 } |
| 365 | 371 |
| 366 bool CachePutWithStatusCode(CacheStorageCache* cache, | 372 bool CachePutWithStatusCode( |
| 367 const ServiceWorkerFetchRequest& request, | 373 CacheStorageCache* cache, |
| 368 int status_code, | 374 const ServiceWorkerFetchRequest& request, |
| 369 const ServiceWorkerHeaderMap& response_headers = | 375 int status_code, |
| 370 ServiceWorkerHeaderMap()) { | 376 FetchResponseType response_type = FetchResponseType::kDefault, |
| 377 const ServiceWorkerHeaderMap& response_headers = |
| 378 ServiceWorkerHeaderMap()) { |
| 371 std::unique_ptr<storage::BlobDataBuilder> blob_data( | 379 std::unique_ptr<storage::BlobDataBuilder> blob_data( |
| 372 new storage::BlobDataBuilder(base::GenerateGUID())); | 380 new storage::BlobDataBuilder(base::GenerateGUID())); |
| 373 blob_data->AppendData(request.url.spec()); | 381 blob_data->AppendData(request.url.spec()); |
| 374 | 382 |
| 375 std::unique_ptr<storage::BlobDataHandle> blob_handle = | 383 std::unique_ptr<storage::BlobDataHandle> blob_handle = |
| 376 blob_storage_context_->AddFinishedBlob(blob_data.get()); | 384 blob_storage_context_->AddFinishedBlob(blob_data.get()); |
| 377 std::unique_ptr<std::vector<GURL>> url_list = | 385 std::unique_ptr<std::vector<GURL>> url_list = |
| 378 base::MakeUnique<std::vector<GURL>>(); | 386 base::MakeUnique<std::vector<GURL>>(); |
| 379 url_list->push_back(request.url); | 387 url_list->push_back(request.url); |
| 380 ServiceWorkerResponse response( | 388 ServiceWorkerResponse response( |
| 381 std::move(url_list), status_code, "OK", | 389 std::move(url_list), status_code, "OK", response_type, |
| 382 network::mojom::FetchResponseType::kDefault, | |
| 383 base::MakeUnique<ServiceWorkerHeaderMap>(response_headers), | 390 base::MakeUnique<ServiceWorkerHeaderMap>(response_headers), |
| 384 blob_handle->uuid(), request.url.spec().size(), nullptr /* blob */, | 391 blob_handle->uuid(), request.url.spec().size(), nullptr /* blob */, |
| 385 blink::kWebServiceWorkerResponseErrorUnknown, base::Time(), | 392 blink::kWebServiceWorkerResponseErrorUnknown, base::Time(), |
| 386 false /* is_in_cache_storage */, | 393 false /* is_in_cache_storage */, |
| 387 std::string() /* cache_storage_cache_name */, | 394 std::string() /* cache_storage_cache_name */, |
| 388 base::MakeUnique< | 395 base::MakeUnique< |
| 389 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); | 396 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
| 390 | 397 |
| 391 CacheStorageBatchOperation operation; | 398 CacheStorageBatchOperation operation; |
| 392 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 399 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 470 |
| 464 int64_t Size(const GURL& origin) { | 471 int64_t Size(const GURL& origin) { |
| 465 base::RunLoop loop; | 472 base::RunLoop loop; |
| 466 CacheStorage* cache_storage = CacheStorageForOrigin(origin); | 473 CacheStorage* cache_storage = CacheStorageForOrigin(origin); |
| 467 cache_storage->Size(base::BindOnce(&CacheStorageManagerTest::UsageCallback, | 474 cache_storage->Size(base::BindOnce(&CacheStorageManagerTest::UsageCallback, |
| 468 base::Unretained(this), &loop)); | 475 base::Unretained(this), &loop)); |
| 469 loop.Run(); | 476 loop.Run(); |
| 470 return callback_usage_; | 477 return callback_usage_; |
| 471 } | 478 } |
| 472 | 479 |
| 480 int64_t GetQuotaOriginUsage(const GURL& origin) { |
| 481 int64_t usage(CacheStorage::kSizeUnknown); |
| 482 base::RunLoop loop; |
| 483 quota_manager_proxy_->GetUsageAndQuota( |
| 484 base::ThreadTaskRunnerHandle::Get().get(), origin, |
| 485 StorageType::kStorageTypeTemporary, |
| 486 base::Bind(&CacheStorageManagerTest::DidGetQuotaOriginUsage, |
| 487 base::Unretained(this), base::Unretained(&usage), &loop)); |
| 488 loop.Run(); |
| 489 return usage; |
| 490 } |
| 491 |
| 492 void DidGetQuotaOriginUsage(int64_t* out_usage, |
| 493 base::RunLoop* run_loop, |
| 494 QuotaStatusCode status_code, |
| 495 int64_t usage, |
| 496 int64_t quota) { |
| 497 if (status_code == storage::kQuotaStatusOk) |
| 498 *out_usage = usage; |
| 499 run_loop->Quit(); |
| 500 } |
| 501 |
| 473 protected: | 502 protected: |
| 474 // Temporary directory must be allocated first so as to be destroyed last. | 503 // Temporary directory must be allocated first so as to be destroyed last. |
| 475 base::ScopedTempDir temp_dir_; | 504 base::ScopedTempDir temp_dir_; |
| 476 | 505 |
| 477 TestBrowserThreadBundle browser_thread_bundle_; | 506 TestBrowserThreadBundle browser_thread_bundle_; |
| 478 TestBrowserContext browser_context_; | 507 TestBrowserContext browser_context_; |
| 479 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 508 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 480 storage::BlobStorageContext* blob_storage_context_; | 509 storage::BlobStorageContext* blob_storage_context_; |
| 481 | 510 |
| 482 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 511 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 auto size_before_close = Size(origin1_); | 941 auto size_before_close = Size(origin1_); |
| 913 EXPECT_GT(size_before_close, 0); | 942 EXPECT_GT(size_before_close, 0); |
| 914 | 943 |
| 915 DestroyStorageManager(); | 944 DestroyStorageManager(); |
| 916 CreateStorageManager(); | 945 CreateStorageManager(); |
| 917 | 946 |
| 918 EXPECT_TRUE(Open(origin1_, kCacheName)); | 947 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 919 EXPECT_EQ(size_before_close, Size(origin1_)); | 948 EXPECT_EQ(size_before_close, Size(origin1_)); |
| 920 } | 949 } |
| 921 | 950 |
| 951 TEST_F(CacheStorageManagerTest, CacheSizePaddedAfterReopen) { |
| 952 const GURL kFooURL = origin1_.Resolve("foo"); |
| 953 const std::string kCacheName = "foo"; |
| 954 |
| 955 int64_t put_delta = quota_manager_proxy_->last_notified_delta(); |
| 956 EXPECT_EQ(0, put_delta); |
| 957 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count()); |
| 958 |
| 959 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 960 std::unique_ptr<CacheStorageCacheHandle> original_handle = |
| 961 std::move(callback_cache_handle_); |
| 962 |
| 963 base::RunLoop().RunUntilIdle(); |
| 964 put_delta += quota_manager_proxy_->last_notified_delta(); |
| 965 EXPECT_EQ(0, put_delta); |
| 966 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count()); |
| 967 |
| 968 EXPECT_TRUE( |
| 969 CachePut(original_handle->value(), kFooURL, FetchResponseType::kOpaque)); |
| 970 int64_t cache_size_before_close = Size(origin1_); |
| 971 base::FilePath storage_dir = original_handle->value()->path().DirName(); |
| 972 original_handle = nullptr; |
| 973 EXPECT_GT(cache_size_before_close, 0); |
| 974 |
| 975 base::RunLoop().RunUntilIdle(); |
| 976 EXPECT_EQ(cache_size_before_close, GetQuotaOriginUsage(origin1_)); |
| 977 |
| 978 base::RunLoop().RunUntilIdle(); |
| 979 put_delta = quota_manager_proxy_->last_notified_delta(); |
| 980 EXPECT_GT(put_delta, 0); |
| 981 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count()); |
| 982 |
| 983 EXPECT_EQ(GetQuotaOriginUsage(origin1_), put_delta); |
| 984 |
| 985 // Close the caches and cache manager. |
| 986 EXPECT_TRUE(FlushCacheStorageIndex(origin1_)); |
| 987 DestroyStorageManager(); |
| 988 |
| 989 // Create a new CacheStorageManager that hasn't yet loaded the origin. |
| 990 CreateStorageManager(); |
| 991 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 992 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 993 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 994 |
| 995 base::RunLoop().RunUntilIdle(); |
| 996 put_delta = quota_manager_proxy_->last_notified_delta(); |
| 997 EXPECT_EQ(0, put_delta); |
| 998 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count()); |
| 999 |
| 1000 EXPECT_EQ(cache_size_before_close, Size(origin1_)); |
| 1001 } |
| 1002 |
| 1003 TEST_F(CacheStorageManagerTest, PersistedCacheKeyUsed) { |
| 1004 const GURL kFooURL = origin1_.Resolve("foo"); |
| 1005 const std::string kCacheName = "foo"; |
| 1006 |
| 1007 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 1008 std::unique_ptr<CacheStorageCacheHandle> original_handle = |
| 1009 std::move(callback_cache_handle_); |
| 1010 |
| 1011 EXPECT_TRUE( |
| 1012 CachePut(original_handle->value(), kFooURL, FetchResponseType::kOpaque)); |
| 1013 |
| 1014 int64_t cache_size_after_put = Size(origin1_); |
| 1015 EXPECT_GT(cache_size_after_put, 0); |
| 1016 |
| 1017 // Close the caches and cache manager. |
| 1018 EXPECT_TRUE(FlushCacheStorageIndex(origin1_)); |
| 1019 DestroyStorageManager(); |
| 1020 |
| 1021 // GenerateNewKeyForTest isn't thread safe so |
| 1022 base::RunLoop().RunUntilIdle(); |
| 1023 CacheStorage::GenerateNewKeyForTesting(); |
| 1024 |
| 1025 // Create a new CacheStorageManager that hasn't yet loaded the origin. |
| 1026 CreateStorageManager(); |
| 1027 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 1028 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 1029 |
| 1030 // Reopening the origin/cache creates a new CacheStorage instance with a new |
| 1031 // random key. |
| 1032 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 1033 |
| 1034 // Size (before any change) should be the same as before it was closed. |
| 1035 EXPECT_EQ(cache_size_after_put, Size(origin1_)); |
| 1036 |
| 1037 // Delete the value. If the new padding key was used to deduct the padded size |
| 1038 // then after deletion we would expect to see a non-zero cache size. |
| 1039 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 1040 EXPECT_EQ(0, Size(origin1_)); |
| 1041 |
| 1042 // Now put the exact same resource back into the cache. This time we expect to |
| 1043 // see a different size as the padding is calculated with a different key. |
| 1044 std::unique_ptr<CacheStorageCacheHandle> new_handle = |
| 1045 std::move(callback_cache_handle_); |
| 1046 EXPECT_TRUE( |
| 1047 CachePut(new_handle->value(), kFooURL, FetchResponseType::kOpaque)); |
| 1048 |
| 1049 EXPECT_NE(cache_size_after_put, Size(origin1_)); |
| 1050 } |
| 1051 |
| 922 // With a memory cache the cache can't be freed from memory until the client | 1052 // With a memory cache the cache can't be freed from memory until the client |
| 923 // calls delete. | 1053 // calls delete. |
| 924 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) { | 1054 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) { |
| 925 EXPECT_TRUE(Open(origin1_, "foo")); | 1055 EXPECT_TRUE(Open(origin1_, "foo")); |
| 926 base::WeakPtr<CacheStorageCache> cache = | 1056 base::WeakPtr<CacheStorageCache> cache = |
| 927 callback_cache_handle_->value()->AsWeakPtr(); | 1057 callback_cache_handle_->value()->AsWeakPtr(); |
| 928 callback_cache_handle_ = nullptr; | 1058 callback_cache_handle_ = nullptr; |
| 929 EXPECT_TRUE(cache); | 1059 EXPECT_TRUE(cache); |
| 930 EXPECT_TRUE(Delete(origin1_, "foo")); | 1060 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 931 base::RunLoop().RunUntilIdle(); | 1061 base::RunLoop().RunUntilIdle(); |
| (...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1504 | 1634 |
| 1505 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 1635 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
| 1506 CacheStorageManagerTestP, | 1636 CacheStorageManagerTestP, |
| 1507 ::testing::Values(false, true)); | 1637 ::testing::Values(false, true)); |
| 1508 | 1638 |
| 1509 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 1639 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
| 1510 CacheStorageQuotaClientTestP, | 1640 CacheStorageQuotaClientTestP, |
| 1511 ::testing::Values(false, true)); | 1641 ::testing::Values(false, true)); |
| 1512 | 1642 |
| 1513 } // namespace content | 1643 } // namespace content |
| OLD | NEW |