| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/cache_storage/cache_storage.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/stl_util.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "content/browser/cache_storage/cache_storage_cache_handle.h" | |
| 15 #include "content/browser/quota/mock_quota_manager_proxy.h" | |
| 16 #include "content/public/test/mock_special_storage_policy.h" | |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | |
| 18 #include "net/url_request/url_request_context_getter.h" | |
| 19 #include "storage/browser/quota/quota_manager_proxy.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 namespace { | |
| 25 const char kOrigin[] = "http://example.com/"; | |
| 26 } | |
| 27 | |
| 28 class TestCacheStorage : public CacheStorage { | |
| 29 public: | |
| 30 TestCacheStorage( | |
| 31 const base::FilePath& file_path, | |
| 32 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy) | |
| 33 : CacheStorage(file_path, | |
| 34 false /* memory_only */, | |
| 35 base::ThreadTaskRunnerHandle::Get().get(), | |
| 36 scoped_refptr<net::URLRequestContextGetter>(), | |
| 37 quota_manager_proxy, | |
| 38 base::WeakPtr<storage::BlobStorageContext>(), | |
| 39 GURL(kOrigin)), | |
| 40 delay_preserved_cache_callback_(false) {} | |
| 41 | |
| 42 void RunPreservedCacheCallback() { | |
| 43 ASSERT_FALSE(preserved_cache_callback_.is_null()); | |
| 44 preserved_cache_callback_.Run(); | |
| 45 preserved_cache_callback_.Reset(); | |
| 46 } | |
| 47 | |
| 48 void set_delay_preserved_cache_callback(bool should_delay) { | |
| 49 delay_preserved_cache_callback_ = should_delay; | |
| 50 } | |
| 51 | |
| 52 bool IsPreservingCache(const CacheStorageCache* cache) { | |
| 53 return ContainsKey(preserved_caches_, cache); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 void SchedulePreservedCacheRemoval(const base::Closure& callback) override { | |
| 58 if (!delay_preserved_cache_callback_) { | |
| 59 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
| 60 return; | |
| 61 } | |
| 62 preserved_cache_callback_ = callback; | |
| 63 } | |
| 64 | |
| 65 bool delay_preserved_cache_callback_; | |
| 66 base::Closure preserved_cache_callback_; | |
| 67 }; | |
| 68 | |
| 69 class CacheStorageTest : public testing::Test { | |
| 70 protected: | |
| 71 CacheStorageTest() | |
| 72 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | |
| 73 | |
| 74 void SetUp() override { | |
| 75 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 76 | |
| 77 quota_policy_ = new MockSpecialStoragePolicy; | |
| 78 mock_quota_manager_ = new MockQuotaManager( | |
| 79 false /* is incognito */, temp_dir_.path(), | |
| 80 base::ThreadTaskRunnerHandle::Get().get(), | |
| 81 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); | |
| 82 quota_manager_proxy_ = new MockQuotaManagerProxy( | |
| 83 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); | |
| 84 | |
| 85 test_cache_storage_.reset( | |
| 86 new TestCacheStorage(temp_dir_.path(), quota_manager_proxy_)); | |
| 87 } | |
| 88 | |
| 89 bool OpenCache(const std::string& cache_name) { | |
| 90 bool callback_called = false; | |
| 91 test_cache_storage_->OpenCache( | |
| 92 cache_name, base::Bind(&CacheStorageTest::OpenCacheCallback, | |
| 93 base::Unretained(this), &callback_called)); | |
| 94 base::RunLoop().RunUntilIdle(); | |
| 95 EXPECT_TRUE(callback_called); | |
| 96 return callback_error_ == CACHE_STORAGE_OK; | |
| 97 } | |
| 98 | |
| 99 void OpenCacheCallback(bool* callback_called, | |
| 100 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 101 CacheStorageError error) { | |
| 102 *callback_called = true; | |
| 103 callback_cache_handle_ = std::move(cache_handle); | |
| 104 callback_error_ = error; | |
| 105 } | |
| 106 | |
| 107 base::ScopedTempDir temp_dir_; | |
| 108 TestBrowserThreadBundle browser_thread_bundle_; | |
| 109 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | |
| 110 scoped_refptr<MockQuotaManager> mock_quota_manager_; | |
| 111 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | |
| 112 std::unique_ptr<TestCacheStorage> test_cache_storage_; | |
| 113 | |
| 114 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; | |
| 115 CacheStorageError callback_error_; | |
| 116 }; | |
| 117 | |
| 118 TEST_F(CacheStorageTest, PreserveCache) { | |
| 119 test_cache_storage_->set_delay_preserved_cache_callback(true); | |
| 120 EXPECT_TRUE(OpenCache("foo")); | |
| 121 EXPECT_TRUE( | |
| 122 test_cache_storage_->IsPreservingCache(callback_cache_handle_->value())); | |
| 123 test_cache_storage_->RunPreservedCacheCallback(); | |
| 124 EXPECT_FALSE( | |
| 125 test_cache_storage_->IsPreservingCache(callback_cache_handle_->value())); | |
| 126 | |
| 127 // Try opening it again, since the cache is already open | |
| 128 // (callback_cache_handle_ is referencing it) the cache shouldn't be preserved | |
| 129 // again. | |
| 130 EXPECT_TRUE(OpenCache("foo")); | |
| 131 EXPECT_FALSE( | |
| 132 test_cache_storage_->IsPreservingCache(callback_cache_handle_->value())); | |
| 133 | |
| 134 // Remove the reference to the cache so that it's deleted. After that, the | |
| 135 // next time it's opened will require the cache to be created again and | |
| 136 // preserved. | |
| 137 callback_cache_handle_.reset(); | |
| 138 EXPECT_TRUE(OpenCache("foo")); | |
| 139 EXPECT_TRUE( | |
| 140 test_cache_storage_->IsPreservingCache(callback_cache_handle_->value())); | |
| 141 test_cache_storage_->RunPreservedCacheCallback(); | |
| 142 EXPECT_FALSE( | |
| 143 test_cache_storage_->IsPreservingCache(callback_cache_handle_->value())); | |
| 144 } | |
| 145 | |
| 146 } // namespace content | |
| OLD | NEW |