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 "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
13 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 13 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
14 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 14 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
15 #include "content/browser/quota/mock_quota_manager_proxy.h" | 15 #include "content/browser/quota/mock_quota_manager_proxy.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/cache_storage_usage_info.h" | |
17 #include "content/public/test/test_browser_context.h" | 18 #include "content/public/test/test_browser_context.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
20 #include "storage/browser/blob/blob_storage_context.h" | 21 #include "storage/browser/blob/blob_storage_context.h" |
21 #include "storage/browser/quota/quota_manager_proxy.h" | 22 #include "storage/browser/quota/quota_manager_proxy.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 | 26 |
26 class CacheStorageManagerTest : public testing::Test { | 27 class CacheStorageManagerTest : public testing::Test { |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 base::Unretained(this), base::Unretained(loop.get()))); | 218 base::Unretained(this), base::Unretained(loop.get()))); |
218 loop->Run(); | 219 loop->Run(); |
219 | 220 |
220 return callback_error_ == CACHE_STORAGE_OK; | 221 return callback_error_ == CACHE_STORAGE_OK; |
221 } | 222 } |
222 | 223 |
223 CacheStorage* CacheStorageForOrigin(const GURL& origin) { | 224 CacheStorage* CacheStorageForOrigin(const GURL& origin) { |
224 return cache_manager_->FindOrCreateCacheStorage(origin); | 225 return cache_manager_->FindOrCreateCacheStorage(origin); |
225 } | 226 } |
226 | 227 |
228 int64 GetOriginUsage(const GURL& origin) { | |
229 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
Bernhard Bauer
2015/08/20 11:18:53
Just allocate the RunLoop directly on the stack.
jsbell
2015/08/20 19:04:51
Done. (Here and elsewhere.)
| |
230 cache_manager_->GetOriginUsage( | |
231 origin, | |
232 base::Bind(&CacheStorageManagerTest::UsageCallback, | |
233 base::Unretained(this), base::Unretained(loop.get()))); | |
234 loop->Run(); | |
235 return callback_usage_; | |
236 } | |
237 | |
238 void UsageCallback(base::RunLoop* run_loop, int64 usage) { | |
239 callback_usage_ = usage; | |
240 run_loop->Quit(); | |
241 } | |
242 | |
243 std::vector<CacheStorageUsageInfo> GetAllOriginsUsage() { | |
244 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
245 cache_manager_->GetAllOriginsUsage( | |
246 base::Bind(&CacheStorageManagerTest::AllOriginsUsageCallback, | |
247 base::Unretained(this), base::Unretained(loop.get()))); | |
248 loop->Run(); | |
249 return callback_all_origins_usage_; | |
250 } | |
251 | |
252 void AllOriginsUsageCallback( | |
253 base::RunLoop* run_loop, | |
254 const std::vector<CacheStorageUsageInfo>& usage) { | |
255 callback_all_origins_usage_ = usage; | |
256 run_loop->Quit(); | |
257 } | |
258 | |
227 protected: | 259 protected: |
228 TestBrowserContext browser_context_; | 260 TestBrowserContext browser_context_; |
229 TestBrowserThreadBundle browser_thread_bundle_; | 261 TestBrowserThreadBundle browser_thread_bundle_; |
230 | 262 |
231 base::ScopedTempDir temp_dir_; | 263 base::ScopedTempDir temp_dir_; |
232 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 264 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
233 scoped_ptr<CacheStorageManager> cache_manager_; | 265 scoped_ptr<CacheStorageManager> cache_manager_; |
234 | 266 |
235 scoped_refptr<CacheStorageCache> callback_cache_; | 267 scoped_refptr<CacheStorageCache> callback_cache_; |
236 int callback_bool_; | 268 int callback_bool_; |
237 CacheStorageError callback_error_; | 269 CacheStorageError callback_error_; |
238 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; | 270 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; |
239 std::vector<std::string> callback_strings_; | 271 std::vector<std::string> callback_strings_; |
240 | 272 |
241 const GURL origin1_; | 273 const GURL origin1_; |
242 const GURL origin2_; | 274 const GURL origin2_; |
243 | 275 |
276 int64 callback_usage_; | |
277 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; | |
278 | |
244 private: | 279 private: |
245 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); | 280 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); |
246 }; | 281 }; |
247 | 282 |
248 class CacheStorageManagerMemoryOnlyTest : public CacheStorageManagerTest { | 283 class CacheStorageManagerMemoryOnlyTest : public CacheStorageManagerTest { |
284 public: | |
249 bool MemoryOnly() override { return true; } | 285 bool MemoryOnly() override { return true; } |
250 }; | 286 }; |
251 | 287 |
252 class CacheStorageManagerTestP : public CacheStorageManagerTest, | 288 class CacheStorageManagerTestP : public CacheStorageManagerTest, |
253 public testing::WithParamInterface<bool> { | 289 public testing::WithParamInterface<bool> { |
290 public: | |
254 bool MemoryOnly() override { return !GetParam(); } | 291 bool MemoryOnly() override { return !GetParam(); } |
255 }; | 292 }; |
256 | 293 |
257 TEST_F(CacheStorageManagerTest, TestsRunOnIOThread) { | 294 TEST_F(CacheStorageManagerTest, TestsRunOnIOThread) { |
258 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 295 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
259 } | 296 } |
260 | 297 |
261 TEST_P(CacheStorageManagerTestP, OpenCache) { | 298 TEST_P(CacheStorageManagerTestP, OpenCache) { |
262 EXPECT_TRUE(Open(origin1_, "foo")); | 299 EXPECT_TRUE(Open(origin1_, "foo")); |
263 } | 300 } |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
505 base::Unretained(this), base::Unretained(open_loop.get()))); | 542 base::Unretained(this), base::Unretained(open_loop.get()))); |
506 | 543 |
507 base::RunLoop().RunUntilIdle(); | 544 base::RunLoop().RunUntilIdle(); |
508 EXPECT_FALSE(callback_cache_); | 545 EXPECT_FALSE(callback_cache_); |
509 | 546 |
510 cache_storage->CompleteAsyncOperationForTesting(); | 547 cache_storage->CompleteAsyncOperationForTesting(); |
511 open_loop->Run(); | 548 open_loop->Run(); |
512 EXPECT_TRUE(callback_cache_); | 549 EXPECT_TRUE(callback_cache_); |
513 } | 550 } |
514 | 551 |
552 TEST_P(CacheStorageManagerTestP, GetOriginUsage) { | |
553 EXPECT_EQ(0, GetOriginUsage(origin1_)); | |
554 EXPECT_TRUE(Open(origin1_, "foo")); | |
555 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | |
556 EXPECT_LT(0, GetOriginUsage(origin1_)); | |
557 EXPECT_EQ(0, GetOriginUsage(origin2_)); | |
558 } | |
559 | |
560 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsage) { | |
561 EXPECT_EQ(0ULL, GetAllOriginsUsage().size()); | |
562 EXPECT_TRUE(Open(origin1_, "foo")); | |
563 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); | |
564 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage(); | |
565 EXPECT_EQ(1ULL, usage.size()); | |
566 const CacheStorageUsageInfo& info = usage[0]; | |
567 EXPECT_EQ(origin1_, info.origin); | |
568 EXPECT_LT(0, info.total_size_bytes); | |
569 if (MemoryOnly()) | |
570 EXPECT_TRUE(info.last_modified.is_null()); | |
571 else | |
572 EXPECT_FALSE(info.last_modified.is_null()); | |
573 } | |
574 | |
515 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryBackedSize) { | 575 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryBackedSize) { |
516 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); | 576 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); |
517 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); | 577 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); |
518 | 578 |
519 EXPECT_TRUE(Open(origin1_, "foo")); | 579 EXPECT_TRUE(Open(origin1_, "foo")); |
520 scoped_refptr<CacheStorageCache> foo_cache = callback_cache_; | 580 scoped_refptr<CacheStorageCache> foo_cache = callback_cache_; |
521 EXPECT_TRUE(Open(origin1_, "bar")); | 581 EXPECT_TRUE(Open(origin1_, "bar")); |
522 scoped_refptr<CacheStorageCache> bar_cache = callback_cache_; | 582 scoped_refptr<CacheStorageCache> bar_cache = callback_cache_; |
523 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); | 583 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); |
524 | 584 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
561 ASSERT_TRUE(base::DirectoryExists(new_path_)); | 621 ASSERT_TRUE(base::DirectoryExists(new_path_)); |
562 | 622 |
563 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 623 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
564 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 624 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
565 | 625 |
566 ASSERT_TRUE(base::Move(new_path_, legacy_path_)); | 626 ASSERT_TRUE(base::Move(new_path_, legacy_path_)); |
567 ASSERT_TRUE(base::DirectoryExists(legacy_path_)); | 627 ASSERT_TRUE(base::DirectoryExists(legacy_path_)); |
568 ASSERT_FALSE(base::DirectoryExists(new_path_)); | 628 ASSERT_FALSE(base::DirectoryExists(new_path_)); |
569 } | 629 } |
570 | 630 |
571 int64 GetOriginUsage(const GURL& origin) { | |
572 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
573 cache_manager_->GetOriginUsage( | |
574 origin, | |
575 base::Bind(&CacheStorageMigrationTest::UsageCallback, | |
576 base::Unretained(this), base::Unretained(loop.get()))); | |
577 loop->Run(); | |
578 return callback_usage_; | |
579 } | |
580 | |
581 void UsageCallback(base::RunLoop* run_loop, int64 usage) { | |
582 callback_usage_ = usage; | |
583 run_loop->Quit(); | |
584 } | |
585 | |
586 base::FilePath legacy_path_; | 631 base::FilePath legacy_path_; |
587 base::FilePath new_path_; | 632 base::FilePath new_path_; |
588 | 633 |
589 const std::string cache1_; | 634 const std::string cache1_; |
590 const std::string cache2_; | 635 const std::string cache2_; |
591 | 636 |
592 int64 callback_usage_; | |
593 | |
594 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest); | 637 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest); |
595 }; | 638 }; |
596 | 639 |
597 TEST_F(CacheStorageMigrationTest, OpenCache) { | 640 TEST_F(CacheStorageMigrationTest, OpenCache) { |
598 EXPECT_TRUE(Open(origin1_, cache1_)); | 641 EXPECT_TRUE(Open(origin1_, cache1_)); |
599 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); | 642 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); |
600 EXPECT_TRUE(base::DirectoryExists(new_path_)); | 643 EXPECT_TRUE(base::DirectoryExists(new_path_)); |
601 | 644 |
602 EXPECT_TRUE(Keys(origin1_)); | 645 EXPECT_TRUE(Keys(origin1_)); |
603 std::vector<std::string> expected_keys; | 646 std::vector<std::string> expected_keys; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
648 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { | 691 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { |
649 protected: | 692 protected: |
650 CacheStorageQuotaClientTest() {} | 693 CacheStorageQuotaClientTest() {} |
651 | 694 |
652 void SetUp() override { | 695 void SetUp() override { |
653 CacheStorageManagerTest::SetUp(); | 696 CacheStorageManagerTest::SetUp(); |
654 quota_client_.reset( | 697 quota_client_.reset( |
655 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); | 698 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); |
656 } | 699 } |
657 | 700 |
658 void UsageCallback(base::RunLoop* run_loop, int64 usage) { | 701 void QuotaUsageCallback(base::RunLoop* run_loop, int64 usage) { |
659 callback_usage_ = usage; | 702 callback_quota_usage_ = usage; |
660 run_loop->Quit(); | 703 run_loop->Quit(); |
661 } | 704 } |
662 | 705 |
663 void OriginsCallback(base::RunLoop* run_loop, const std::set<GURL>& origins) { | 706 void OriginsCallback(base::RunLoop* run_loop, const std::set<GURL>& origins) { |
664 callback_origins_ = origins; | 707 callback_origins_ = origins; |
665 run_loop->Quit(); | 708 run_loop->Quit(); |
666 } | 709 } |
667 | 710 |
668 void DeleteOriginCallback(base::RunLoop* run_loop, | 711 void DeleteOriginCallback(base::RunLoop* run_loop, |
669 storage::QuotaStatusCode status) { | 712 storage::QuotaStatusCode status) { |
670 callback_status_ = status; | 713 callback_status_ = status; |
671 run_loop->Quit(); | 714 run_loop->Quit(); |
672 } | 715 } |
673 | 716 |
674 int64 QuotaGetOriginUsage(const GURL& origin) { | 717 int64 QuotaGetOriginUsage(const GURL& origin) { |
675 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 718 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
676 quota_client_->GetOriginUsage( | 719 quota_client_->GetOriginUsage( |
677 origin, storage::kStorageTypeTemporary, | 720 origin, storage::kStorageTypeTemporary, |
678 base::Bind(&CacheStorageQuotaClientTest::UsageCallback, | 721 base::Bind(&CacheStorageQuotaClientTest::QuotaUsageCallback, |
679 base::Unretained(this), base::Unretained(loop.get()))); | 722 base::Unretained(this), base::Unretained(loop.get()))); |
680 loop->Run(); | 723 loop->Run(); |
681 return callback_usage_; | 724 return callback_quota_usage_; |
682 } | 725 } |
683 | 726 |
684 size_t QuotaGetOriginsForType() { | 727 size_t QuotaGetOriginsForType() { |
685 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 728 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
686 quota_client_->GetOriginsForType( | 729 quota_client_->GetOriginsForType( |
687 storage::kStorageTypeTemporary, | 730 storage::kStorageTypeTemporary, |
688 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback, | 731 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback, |
689 base::Unretained(this), base::Unretained(loop.get()))); | 732 base::Unretained(this), base::Unretained(loop.get()))); |
690 loop->Run(); | 733 loop->Run(); |
691 return callback_origins_.size(); | 734 return callback_origins_.size(); |
(...skipping 19 matching lines...) Expand all Loading... | |
711 return callback_status_ == storage::kQuotaStatusOk; | 754 return callback_status_ == storage::kQuotaStatusOk; |
712 } | 755 } |
713 | 756 |
714 bool QuotaDoesSupport(storage::StorageType type) { | 757 bool QuotaDoesSupport(storage::StorageType type) { |
715 return quota_client_->DoesSupport(type); | 758 return quota_client_->DoesSupport(type); |
716 } | 759 } |
717 | 760 |
718 scoped_ptr<CacheStorageQuotaClient> quota_client_; | 761 scoped_ptr<CacheStorageQuotaClient> quota_client_; |
719 | 762 |
720 storage::QuotaStatusCode callback_status_; | 763 storage::QuotaStatusCode callback_status_; |
721 int64 callback_usage_; | 764 int64 callback_quota_usage_ = 0; |
722 std::set<GURL> callback_origins_; | 765 std::set<GURL> callback_origins_; |
723 | 766 |
724 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest); | 767 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest); |
725 }; | 768 }; |
726 | 769 |
727 class CacheStorageQuotaClientTestP : public CacheStorageQuotaClientTest, | 770 class CacheStorageQuotaClientTestP : public CacheStorageQuotaClientTest, |
728 public testing::WithParamInterface<bool> { | 771 public testing::WithParamInterface<bool> { |
729 bool MemoryOnly() override { return !GetParam(); } | 772 bool MemoryOnly() override { return !GetParam(); } |
730 }; | 773 }; |
731 | 774 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
790 | 833 |
791 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 834 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
792 CacheStorageManagerTestP, | 835 CacheStorageManagerTestP, |
793 ::testing::Values(false, true)); | 836 ::testing::Values(false, true)); |
794 | 837 |
795 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 838 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
796 CacheStorageQuotaClientTestP, | 839 CacheStorageQuotaClientTestP, |
797 ::testing::Values(false, true)); | 840 ::testing::Values(false, true)); |
798 | 841 |
799 } // namespace content | 842 } // namespace content |
OLD | NEW |