| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 BrowserContext::GetDefaultStoragePartition(&browser_context_) | 210 BrowserContext::GetDefaultStoragePartition(&browser_context_) |
| 211 ->GetURLRequestContext(), | 211 ->GetURLRequestContext(), |
| 212 blob_storage_context->context()->AsWeakPtr()); | 212 blob_storage_context->context()->AsWeakPtr()); |
| 213 } | 213 } |
| 214 | 214 |
| 215 bool FlushCacheStorageIndex(const GURL& origin) { | 215 bool FlushCacheStorageIndex(const GURL& origin) { |
| 216 callback_bool_ = false; | 216 callback_bool_ = false; |
| 217 base::RunLoop loop; | 217 base::RunLoop loop; |
| 218 bool write_was_scheduled = | 218 bool write_was_scheduled = |
| 219 CacheStorageForOrigin(origin)->InitiateScheduledIndexWriteForTest( | 219 CacheStorageForOrigin(origin)->InitiateScheduledIndexWriteForTest( |
| 220 base::Bind(&CacheStorageManagerTest::BoolCallback, | 220 base::BindOnce(&CacheStorageManagerTest::BoolCallback, |
| 221 base::Unretained(this), &loop)); | 221 base::Unretained(this), &loop)); |
| 222 loop.Run(); | 222 loop.Run(); |
| 223 DCHECK(callback_bool_); | 223 DCHECK(callback_bool_); |
| 224 return write_was_scheduled; | 224 return write_was_scheduled; |
| 225 } | 225 } |
| 226 | 226 |
| 227 void DestroyStorageManager() { | 227 void DestroyStorageManager() { |
| 228 if (quota_manager_proxy_) | 228 if (quota_manager_proxy_) |
| 229 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 229 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 230 base::RunLoop().RunUntilIdle(); | 230 base::RunLoop().RunUntilIdle(); |
| 231 quota_manager_proxy_ = nullptr; | 231 quota_manager_proxy_ = nullptr; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 243 callback_cache_index_ = CacheStorageIndex(); | 243 callback_cache_index_ = CacheStorageIndex(); |
| 244 callback_all_origins_usage_.clear(); | 244 callback_all_origins_usage_.clear(); |
| 245 | 245 |
| 246 cache_manager_ = nullptr; | 246 cache_manager_ = nullptr; |
| 247 } | 247 } |
| 248 | 248 |
| 249 bool Open(const GURL& origin, const std::string& cache_name) { | 249 bool Open(const GURL& origin, const std::string& cache_name) { |
| 250 base::RunLoop loop; | 250 base::RunLoop loop; |
| 251 cache_manager_->OpenCache( | 251 cache_manager_->OpenCache( |
| 252 origin, cache_name, | 252 origin, cache_name, |
| 253 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 253 base::BindOnce(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 254 base::Unretained(this), base::Unretained(&loop))); | 254 base::Unretained(this), base::Unretained(&loop))); |
| 255 loop.Run(); | 255 loop.Run(); |
| 256 | 256 |
| 257 bool error = callback_error_ != CACHE_STORAGE_OK; | 257 bool error = callback_error_ != CACHE_STORAGE_OK; |
| 258 if (error) | 258 if (error) |
| 259 EXPECT_FALSE(callback_cache_handle_); | 259 EXPECT_FALSE(callback_cache_handle_); |
| 260 else | 260 else |
| 261 EXPECT_TRUE(callback_cache_handle_); | 261 EXPECT_TRUE(callback_cache_handle_); |
| 262 return !error; | 262 return !error; |
| 263 } | 263 } |
| 264 | 264 |
| 265 bool Has(const GURL& origin, const std::string& cache_name) { | 265 bool Has(const GURL& origin, const std::string& cache_name) { |
| 266 base::RunLoop loop; | 266 base::RunLoop loop; |
| 267 cache_manager_->HasCache( | 267 cache_manager_->HasCache( |
| 268 origin, cache_name, | 268 origin, cache_name, |
| 269 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 269 base::BindOnce(&CacheStorageManagerTest::BoolAndErrorCallback, |
| 270 base::Unretained(this), base::Unretained(&loop))); | 270 base::Unretained(this), base::Unretained(&loop))); |
| 271 loop.Run(); | 271 loop.Run(); |
| 272 | 272 |
| 273 return callback_bool_; | 273 return callback_bool_; |
| 274 } | 274 } |
| 275 | 275 |
| 276 bool Delete(const GURL& origin, const std::string& cache_name) { | 276 bool Delete(const GURL& origin, const std::string& cache_name) { |
| 277 base::RunLoop loop; | 277 base::RunLoop loop; |
| 278 cache_manager_->DeleteCache( | 278 cache_manager_->DeleteCache( |
| 279 origin, cache_name, | 279 origin, cache_name, |
| 280 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 280 base::BindOnce(&CacheStorageManagerTest::BoolAndErrorCallback, |
| 281 base::Unretained(this), base::Unretained(&loop))); | 281 base::Unretained(this), base::Unretained(&loop))); |
| 282 loop.Run(); | 282 loop.Run(); |
| 283 | 283 |
| 284 return callback_bool_; | 284 return callback_bool_; |
| 285 } | 285 } |
| 286 | 286 |
| 287 size_t Keys(const GURL& origin) { | 287 size_t Keys(const GURL& origin) { |
| 288 base::RunLoop loop; | 288 base::RunLoop loop; |
| 289 cache_manager_->EnumerateCaches( | 289 cache_manager_->EnumerateCaches( |
| 290 origin, base::Bind(&CacheStorageManagerTest::CacheMetadataCallback, | 290 origin, |
| 291 base::Unretained(this), base::Unretained(&loop))); | 291 base::BindOnce(&CacheStorageManagerTest::CacheMetadataCallback, |
| 292 base::Unretained(this), base::Unretained(&loop))); |
| 292 loop.Run(); | 293 loop.Run(); |
| 293 return callback_cache_index_.num_entries(); | 294 return callback_cache_index_.num_entries(); |
| 294 } | 295 } |
| 295 | 296 |
| 296 bool StorageMatch(const GURL& origin, | 297 bool StorageMatch(const GURL& origin, |
| 297 const std::string& cache_name, | 298 const std::string& cache_name, |
| 298 const GURL& url, | 299 const GURL& url, |
| 299 const CacheStorageCacheQueryParams& match_params = | 300 const CacheStorageCacheQueryParams& match_params = |
| 300 CacheStorageCacheQueryParams()) { | 301 CacheStorageCacheQueryParams()) { |
| 301 ServiceWorkerFetchRequest request; | 302 ServiceWorkerFetchRequest request; |
| 302 request.url = url; | 303 request.url = url; |
| 303 return StorageMatchWithRequest(origin, cache_name, request, match_params); | 304 return StorageMatchWithRequest(origin, cache_name, request, match_params); |
| 304 } | 305 } |
| 305 | 306 |
| 306 bool StorageMatchWithRequest( | 307 bool StorageMatchWithRequest( |
| 307 const GURL& origin, | 308 const GURL& origin, |
| 308 const std::string& cache_name, | 309 const std::string& cache_name, |
| 309 const ServiceWorkerFetchRequest& request, | 310 const ServiceWorkerFetchRequest& request, |
| 310 const CacheStorageCacheQueryParams& match_params = | 311 const CacheStorageCacheQueryParams& match_params = |
| 311 CacheStorageCacheQueryParams()) { | 312 CacheStorageCacheQueryParams()) { |
| 312 std::unique_ptr<ServiceWorkerFetchRequest> unique_request = | 313 std::unique_ptr<ServiceWorkerFetchRequest> unique_request = |
| 313 base::MakeUnique<ServiceWorkerFetchRequest>(request); | 314 base::MakeUnique<ServiceWorkerFetchRequest>(request); |
| 314 | 315 |
| 315 base::RunLoop loop; | 316 base::RunLoop loop; |
| 316 cache_manager_->MatchCache( | 317 cache_manager_->MatchCache( |
| 317 origin, cache_name, std::move(unique_request), match_params, | 318 origin, cache_name, std::move(unique_request), match_params, |
| 318 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 319 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback, |
| 319 base::Unretained(this), base::Unretained(&loop))); | 320 base::Unretained(this), base::Unretained(&loop))); |
| 320 loop.Run(); | 321 loop.Run(); |
| 321 | 322 |
| 322 return callback_error_ == CACHE_STORAGE_OK; | 323 return callback_error_ == CACHE_STORAGE_OK; |
| 323 } | 324 } |
| 324 | 325 |
| 325 bool StorageMatchAll(const GURL& origin, | 326 bool StorageMatchAll(const GURL& origin, |
| 326 const GURL& url, | 327 const GURL& url, |
| 327 const CacheStorageCacheQueryParams& match_params = | 328 const CacheStorageCacheQueryParams& match_params = |
| 328 CacheStorageCacheQueryParams()) { | 329 CacheStorageCacheQueryParams()) { |
| 329 ServiceWorkerFetchRequest request; | 330 ServiceWorkerFetchRequest request; |
| 330 request.url = url; | 331 request.url = url; |
| 331 return StorageMatchAllWithRequest(origin, request, match_params); | 332 return StorageMatchAllWithRequest(origin, request, match_params); |
| 332 } | 333 } |
| 333 | 334 |
| 334 bool StorageMatchAllWithRequest( | 335 bool StorageMatchAllWithRequest( |
| 335 const GURL& origin, | 336 const GURL& origin, |
| 336 const ServiceWorkerFetchRequest& request, | 337 const ServiceWorkerFetchRequest& request, |
| 337 const CacheStorageCacheQueryParams& match_params = | 338 const CacheStorageCacheQueryParams& match_params = |
| 338 CacheStorageCacheQueryParams()) { | 339 CacheStorageCacheQueryParams()) { |
| 339 std::unique_ptr<ServiceWorkerFetchRequest> unique_request = | 340 std::unique_ptr<ServiceWorkerFetchRequest> unique_request = |
| 340 base::MakeUnique<ServiceWorkerFetchRequest>(request); | 341 base::MakeUnique<ServiceWorkerFetchRequest>(request); |
| 341 base::RunLoop loop; | 342 base::RunLoop loop; |
| 342 cache_manager_->MatchAllCaches( | 343 cache_manager_->MatchAllCaches( |
| 343 origin, std::move(unique_request), match_params, | 344 origin, std::move(unique_request), match_params, |
| 344 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 345 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback, |
| 345 base::Unretained(this), base::Unretained(&loop))); | 346 base::Unretained(this), base::Unretained(&loop))); |
| 346 loop.Run(); | 347 loop.Run(); |
| 347 | 348 |
| 348 return callback_error_ == CACHE_STORAGE_OK; | 349 return callback_error_ == CACHE_STORAGE_OK; |
| 349 } | 350 } |
| 350 | 351 |
| 351 bool CachePut(CacheStorageCache* cache, const GURL& url) { | 352 bool CachePut(CacheStorageCache* cache, const GURL& url) { |
| 352 ServiceWorkerFetchRequest request; | 353 ServiceWorkerFetchRequest request; |
| 353 request.url = url; | 354 request.url = url; |
| 354 | 355 |
| 355 return CachePutWithStatusCode(cache, request, 200); | 356 return CachePutWithStatusCode(cache, request, 200); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); | 389 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
| 389 | 390 |
| 390 CacheStorageBatchOperation operation; | 391 CacheStorageBatchOperation operation; |
| 391 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 392 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 392 operation.request = request; | 393 operation.request = request; |
| 393 operation.response = response; | 394 operation.response = response; |
| 394 | 395 |
| 395 base::RunLoop loop; | 396 base::RunLoop loop; |
| 396 cache->BatchOperation( | 397 cache->BatchOperation( |
| 397 std::vector<CacheStorageBatchOperation>(1, operation), | 398 std::vector<CacheStorageBatchOperation>(1, operation), |
| 398 base::Bind(&CacheStorageManagerTest::CachePutCallback, | 399 base::BindOnce(&CacheStorageManagerTest::CachePutCallback, |
| 399 base::Unretained(this), base::Unretained(&loop))); | 400 base::Unretained(this), base::Unretained(&loop))); |
| 400 loop.Run(); | 401 loop.Run(); |
| 401 | 402 |
| 402 return callback_error_ == CACHE_STORAGE_OK; | 403 return callback_error_ == CACHE_STORAGE_OK; |
| 403 } | 404 } |
| 404 | 405 |
| 405 bool CacheMatch(CacheStorageCache* cache, const GURL& url) { | 406 bool CacheMatch(CacheStorageCache* cache, const GURL& url) { |
| 406 std::unique_ptr<ServiceWorkerFetchRequest> request( | 407 std::unique_ptr<ServiceWorkerFetchRequest> request( |
| 407 new ServiceWorkerFetchRequest()); | 408 new ServiceWorkerFetchRequest()); |
| 408 request->url = url; | 409 request->url = url; |
| 409 base::RunLoop loop; | 410 base::RunLoop loop; |
| 410 cache->Match(std::move(request), CacheStorageCacheQueryParams(), | 411 cache->Match( |
| 411 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 412 std::move(request), CacheStorageCacheQueryParams(), |
| 412 base::Unretained(this), base::Unretained(&loop))); | 413 base::BindOnce(&CacheStorageManagerTest::CacheMatchCallback, |
| 414 base::Unretained(this), base::Unretained(&loop))); |
| 413 loop.Run(); | 415 loop.Run(); |
| 414 | 416 |
| 415 return callback_error_ == CACHE_STORAGE_OK; | 417 return callback_error_ == CACHE_STORAGE_OK; |
| 416 } | 418 } |
| 417 | 419 |
| 418 CacheStorage* CacheStorageForOrigin(const GURL& origin) { | 420 CacheStorage* CacheStorageForOrigin(const GURL& origin) { |
| 419 return cache_manager_->FindOrCreateCacheStorage(origin); | 421 return cache_manager_->FindOrCreateCacheStorage(origin); |
| 420 } | 422 } |
| 421 | 423 |
| 422 int64_t GetOriginUsage(const GURL& origin) { | 424 int64_t GetOriginUsage(const GURL& origin) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 446 base::RunLoop* run_loop, | 448 base::RunLoop* run_loop, |
| 447 const std::vector<CacheStorageUsageInfo>& usage) { | 449 const std::vector<CacheStorageUsageInfo>& usage) { |
| 448 callback_all_origins_usage_ = usage; | 450 callback_all_origins_usage_ = usage; |
| 449 run_loop->Quit(); | 451 run_loop->Quit(); |
| 450 } | 452 } |
| 451 | 453 |
| 452 int64_t GetSizeThenCloseAllCaches(const GURL& origin) { | 454 int64_t GetSizeThenCloseAllCaches(const GURL& origin) { |
| 453 base::RunLoop loop; | 455 base::RunLoop loop; |
| 454 CacheStorage* cache_storage = CacheStorageForOrigin(origin); | 456 CacheStorage* cache_storage = CacheStorageForOrigin(origin); |
| 455 cache_storage->GetSizeThenCloseAllCaches( | 457 cache_storage->GetSizeThenCloseAllCaches( |
| 456 base::Bind(&CacheStorageManagerTest::UsageCallback, | 458 base::BindOnce(&CacheStorageManagerTest::UsageCallback, |
| 457 base::Unretained(this), &loop)); | 459 base::Unretained(this), &loop)); |
| 458 loop.Run(); | 460 loop.Run(); |
| 459 return callback_usage_; | 461 return callback_usage_; |
| 460 } | 462 } |
| 461 | 463 |
| 462 int64_t Size(const GURL& origin) { | 464 int64_t Size(const GURL& origin) { |
| 463 base::RunLoop loop; | 465 base::RunLoop loop; |
| 464 CacheStorage* cache_storage = CacheStorageForOrigin(origin); | 466 CacheStorage* cache_storage = CacheStorageForOrigin(origin); |
| 465 cache_storage->Size(base::Bind(&CacheStorageManagerTest::UsageCallback, | 467 cache_storage->Size(base::BindOnce(&CacheStorageManagerTest::UsageCallback, |
| 466 base::Unretained(this), &loop)); | 468 base::Unretained(this), &loop)); |
| 467 loop.Run(); | 469 loop.Run(); |
| 468 return callback_usage_; | 470 return callback_usage_; |
| 469 } | 471 } |
| 470 | 472 |
| 471 protected: | 473 protected: |
| 472 // Temporary directory must be allocated first so as to be destroyed last. | 474 // Temporary directory must be allocated first so as to be destroyed last. |
| 473 base::ScopedTempDir temp_dir_; | 475 base::ScopedTempDir temp_dir_; |
| 474 | 476 |
| 475 TestBrowserThreadBundle browser_thread_bundle_; | 477 TestBrowserThreadBundle browser_thread_bundle_; |
| 476 TestBrowserContext browser_context_; | 478 TestBrowserContext browser_context_; |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 } | 939 } |
| 938 | 940 |
| 939 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) { | 941 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) { |
| 940 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage. | 942 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage. |
| 941 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); | 943 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); |
| 942 cache_storage->StartAsyncOperationForTesting(); | 944 cache_storage->StartAsyncOperationForTesting(); |
| 943 | 945 |
| 944 base::RunLoop open_loop; | 946 base::RunLoop open_loop; |
| 945 cache_manager_->OpenCache( | 947 cache_manager_->OpenCache( |
| 946 origin1_, "foo", | 948 origin1_, "foo", |
| 947 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 949 base::BindOnce(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 948 base::Unretained(this), base::Unretained(&open_loop))); | 950 base::Unretained(this), base::Unretained(&open_loop))); |
| 949 | 951 |
| 950 base::RunLoop().RunUntilIdle(); | 952 base::RunLoop().RunUntilIdle(); |
| 951 EXPECT_FALSE(callback_cache_handle_); | 953 EXPECT_FALSE(callback_cache_handle_); |
| 952 | 954 |
| 953 cache_storage->CompleteAsyncOperationForTesting(); | 955 cache_storage->CompleteAsyncOperationForTesting(); |
| 954 open_loop.Run(); | 956 open_loop.Run(); |
| 955 EXPECT_TRUE(callback_cache_handle_); | 957 EXPECT_TRUE(callback_cache_handle_); |
| 956 } | 958 } |
| 957 | 959 |
| 958 TEST_P(CacheStorageManagerTestP, GetOriginUsage) { | 960 TEST_P(CacheStorageManagerTestP, GetOriginUsage) { |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1502 | 1504 |
| 1503 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 1505 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
| 1504 CacheStorageManagerTestP, | 1506 CacheStorageManagerTestP, |
| 1505 ::testing::Values(false, true)); | 1507 ::testing::Values(false, true)); |
| 1506 | 1508 |
| 1507 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 1509 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
| 1508 CacheStorageQuotaClientTestP, | 1510 CacheStorageQuotaClientTestP, |
| 1509 ::testing::Values(false, true)); | 1511 ::testing::Values(false, true)); |
| 1510 | 1512 |
| 1511 } // namespace content | 1513 } // namespace content |
| OLD | NEW |