| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <memory> | 6 #include <memory> |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 15 #include "chrome/browser/browsing_data/cache_test_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" | 19 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" |
| 19 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/storage_partition.h" | 22 #include "content/public/browser/storage_partition.h" |
| 22 #include "net/disk_cache/disk_cache.h" | 23 #include "net/disk_cache/disk_cache.h" |
| 23 #include "net/http/http_cache.h" | 24 #include "net/http/http_cache.h" |
| 24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 } | 37 } |
| 37 | 38 |
| 38 bool HasHttpsExampleOrigin(const GURL& url) { | 39 bool HasHttpsExampleOrigin(const GURL& url) { |
| 39 return url.GetOrigin() == "https://example.com/"; | 40 return url.GetOrigin() == "https://example.com/"; |
| 40 } | 41 } |
| 41 | 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest { | 45 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest { |
| 45 public: | 46 public: |
| 46 // Initialization ------------------------------------------------------------ | |
| 47 | |
| 48 void SetUpOnMainThread() override { | 47 void SetUpOnMainThread() override { |
| 49 // Prepare the commonly used callbacks. | 48 cache_util_ = base::MakeUnique<CacheTestUtil>( |
| 50 done_callback_ = base::Bind( | 49 content::BrowserContext::GetDefaultStoragePartition( |
| 51 &ConditionalCacheDeletionHelperBrowserTest::DoneCallback, | 50 browser()->profile())); |
| 52 base::Unretained(this)); | 51 done_callback_ = |
| 53 | 52 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DoneCallback, |
| 53 base::Unretained(this)); |
| 54 // UI and IO thread synchronization. | 54 // UI and IO thread synchronization. |
| 55 waitable_event_ = base::MakeUnique<base::WaitableEvent>( | 55 waitable_event_ = base::MakeUnique<base::WaitableEvent>( |
| 56 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 56 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 57 base::WaitableEvent::InitialState::NOT_SIGNALED); | 57 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 58 | |
| 59 // Get the storage partition. | |
| 60 partition_ = content::BrowserContext::GetDefaultStoragePartition( | |
| 61 browser()->profile()); | |
| 62 | |
| 63 // Get the cache backends. | |
| 64 BrowserThread::PostTask( | |
| 65 BrowserThread::IO, FROM_HERE, | |
| 66 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::SetUpOnIOThread, | |
| 67 base::Unretained(this))); | |
| 68 WaitForTasksOnIOThread(); | |
| 69 } | 58 } |
| 70 | 59 |
| 71 void SetUpOnIOThread() { | 60 void TearDownOnMainThread() override { cache_util_.reset(); } |
| 72 net::URLRequestContextGetter* context = partition_->GetURLRequestContext(); | |
| 73 | 61 |
| 74 net::HttpCache* cache = context->GetURLRequestContext()-> | 62 void DeleteEntries( |
| 75 http_transaction_factory()->GetCache(); | 63 const base::Callback<bool(const disk_cache::Entry*)>& condition) { |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 65 auto* helper = |
| 66 new ConditionalCacheDeletionHelper(cache_util_->backend(), condition); |
| 76 | 67 |
| 77 SetNumberOfWaitedTasks(1); | 68 helper->DeleteAndDestroySelfWhenFinished(done_callback_); |
| 78 WaitForCompletion(cache->GetBackend(&backend_, done_callback_)); | |
| 79 } | 69 } |
| 80 | 70 |
| 81 void TearDownOnMainThread() override { | 71 void CompareRemainingKeys(std::set<std::string> expected_set) { |
| 82 // The cache iterator must be deleted on the thread where it was created, | 72 std::vector<std::string> remaining_keys = cache_util_->GetEntryKeys(); |
| 83 // which is the IO thread. | 73 std::sort(remaining_keys.begin(), remaining_keys.end()); |
| 84 BrowserThread::PostTask( | 74 std::vector<std::string> expected; |
| 85 BrowserThread::IO, FROM_HERE, | 75 expected.assign(expected_set.begin(), expected_set.end()); |
| 86 base::Bind( | 76 EXPECT_EQ(expected, remaining_keys); |
| 87 &ConditionalCacheDeletionHelperBrowserTest::TearDownOnIOThread, | |
| 88 base::Unretained(this))); | |
| 89 WaitForTasksOnIOThread(); | |
| 90 } | 77 } |
| 91 | 78 |
| 92 void TearDownOnIOThread() { | 79 void DoneCallback(int value) { |
| 93 iterator_.reset(); | 80 DCHECK_GE(value, 0); // Negative values represent an error. |
| 94 DoneCallback(net::OK); | 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 82 waitable_event_->Signal(); |
| 95 } | 83 } |
| 96 | 84 |
| 97 // Waiting for tasks to be done on IO thread. -------------------------------- | |
| 98 | |
| 99 void WaitForTasksOnIOThread() { | 85 void WaitForTasksOnIOThread() { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 86 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 101 waitable_event_->Wait(); | 87 waitable_event_->Wait(); |
| 102 } | 88 } |
| 103 | 89 |
| 104 void SetNumberOfWaitedTasks(int count) { | 90 CacheTestUtil* GetCacheTestUtil() { return cache_util_.get(); } |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 106 remaining_tasks_ = count; | |
| 107 } | |
| 108 | |
| 109 void WaitForCompletion(int value) { | |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 111 if (value >= 0) { | |
| 112 // We got the result immediately. | |
| 113 DoneCallback(value); | |
| 114 } else if (value == net::ERR_IO_PENDING) { | |
| 115 // We need to wait for the callback. | |
| 116 } else { | |
| 117 // An error has occurred. | |
| 118 NOTREACHED(); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void DoneCallback(int value) { | |
| 123 DCHECK_GE(value, 0); // Negative values represent an error. | |
| 124 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 125 if (--remaining_tasks_ > 0) | |
| 126 return; | |
| 127 | |
| 128 waitable_event_->Signal(); | |
| 129 } | |
| 130 | |
| 131 // Cache operation shorthands. ----------------------------------------------- | |
| 132 | |
| 133 void CreateCacheEntries(const std::set<std::string>& keys) { | |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 135 | |
| 136 entries_.resize(keys.size()); | |
| 137 SetNumberOfWaitedTasks(keys.size()); | |
| 138 | |
| 139 int pos = 0; | |
| 140 for (const std::string& key : keys) { | |
| 141 WaitForCompletion(backend_->CreateEntry( | |
| 142 key, &entries_[pos++], done_callback_)); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void DeleteEntries( | |
| 147 const base::Callback<bool(const disk_cache::Entry*)>& condition) { | |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 149 ConditionalCacheDeletionHelper* helper = new ConditionalCacheDeletionHelper( | |
| 150 backend_, | |
| 151 condition); | |
| 152 | |
| 153 WaitForCompletion(helper->DeleteAndDestroySelfWhenFinished(done_callback_)); | |
| 154 } | |
| 155 | |
| 156 void GetRemainingKeys() { | |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 158 current_entry_ = nullptr; | |
| 159 iterator_ = backend_->CreateIterator(); | |
| 160 GetNextKey(net::OK); | |
| 161 } | |
| 162 | |
| 163 void GetNextKey(int error) { | |
| 164 while (error != net::ERR_IO_PENDING) { | |
| 165 if (error == net::ERR_FAILED) { | |
| 166 DoneCallback(net::OK); | |
| 167 return; | |
| 168 } | |
| 169 | |
| 170 if (current_entry_) { | |
| 171 remaining_keys_.push_back(current_entry_->GetKey()); | |
| 172 } | |
| 173 | |
| 174 error = iterator_->OpenNextEntry( | |
| 175 ¤t_entry_, | |
| 176 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetNextKey, | |
| 177 base::Unretained(this))); | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 void CompareRemainingKeys(std::set<std::string> expected_set) { | |
| 182 std::vector<std::string> expected; | |
| 183 expected.assign(expected_set.begin(), expected_set.end()); | |
| 184 std::sort(remaining_keys_.begin(), remaining_keys_.end()); | |
| 185 EXPECT_EQ(expected, remaining_keys_); | |
| 186 } | |
| 187 | |
| 188 // Miscellaneous. ------------------------------------------------------------ | |
| 189 | 91 |
| 190 private: | 92 private: |
| 191 content::StoragePartition* partition_; | |
| 192 disk_cache::Backend* backend_ = nullptr; | |
| 193 std::unique_ptr<disk_cache::Backend::Iterator> iterator_; | |
| 194 disk_cache::Entry* current_entry_; | |
| 195 std::vector<disk_cache::Entry*> entries_; | |
| 196 | |
| 197 base::Callback<void(int)> done_callback_; | 93 base::Callback<void(int)> done_callback_; |
| 198 | 94 std::unique_ptr<CacheTestUtil> cache_util_; |
| 199 std::unique_ptr<base::WaitableEvent> waitable_event_; | 95 std::unique_ptr<base::WaitableEvent> waitable_event_; |
| 200 int remaining_tasks_; | |
| 201 | |
| 202 std::vector<std::string> remaining_keys_; | |
| 203 }; | 96 }; |
| 204 | 97 |
| 205 // Tests that ConditionalCacheDeletionHelper only deletes those cache entries | 98 // Tests that ConditionalCacheDeletionHelper only deletes those cache entries |
| 206 // that match the condition. | 99 // that match the condition. |
| 207 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { | 100 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { |
| 208 // Create 5 entries. | 101 // Create 5 entries. |
| 209 std::set<std::string> keys; | 102 std::set<std::string> keys = {"123", "47", "56", "81", "42"}; |
| 210 keys.insert("123"); | |
| 211 keys.insert("47"); | |
| 212 keys.insert("56"); | |
| 213 keys.insert("81"); | |
| 214 keys.insert("42"); | |
| 215 | 103 |
| 216 BrowserThread::PostTask( | 104 GetCacheTestUtil()->CreateCacheEntries(keys); |
| 217 BrowserThread::IO, FROM_HERE, | |
| 218 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::CreateCacheEntries, | |
| 219 base::Unretained(this), | |
| 220 base::ConstRef(keys))); | |
| 221 WaitForTasksOnIOThread(); | |
| 222 | 105 |
| 223 // Delete the entries whose keys are even numbers. | 106 // Delete the entries whose keys are even numbers. |
| 224 BrowserThread::PostTask( | 107 BrowserThread::PostTask( |
| 225 BrowserThread::IO, FROM_HERE, | 108 BrowserThread::IO, FROM_HERE, |
| 226 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, | 109 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, |
| 227 base::Unretained(this), | 110 base::Unretained(this), |
| 228 base::Bind(&KeyIsEven))); | 111 base::Bind(&KeyIsEven))); |
| 229 WaitForTasksOnIOThread(); | 112 WaitForTasksOnIOThread(); |
| 230 | 113 |
| 231 // Expect that the keys with values 56 and 42 were deleted. | 114 // Expect that the keys with values 56 and 42 were deleted. |
| 232 BrowserThread::PostTask( | |
| 233 BrowserThread::IO, FROM_HERE, | |
| 234 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetRemainingKeys, | |
| 235 base::Unretained(this))); | |
| 236 WaitForTasksOnIOThread(); | |
| 237 | |
| 238 keys.erase("56"); | 115 keys.erase("56"); |
| 239 keys.erase("42"); | 116 keys.erase("42"); |
| 240 CompareRemainingKeys(keys); | 117 CompareRemainingKeys(keys); |
| 241 } | 118 } |
| 242 | 119 |
| 243 // Tests that ConditionalCacheDeletionHelper correctly constructs a condition | 120 // Tests that ConditionalCacheDeletionHelper correctly constructs a condition |
| 244 // for time and URL. | 121 // for time and URL. |
| 245 // | 122 // |
| 246 // Note: This test depends on the timing in cache backends and can be flaky | 123 // Note: This test depends on the timing in cache backends and can be flaky |
| 247 // if those backends are slow. If this turns out to be a problem, consider | 124 // if those backends are slow. If this turns out to be a problem, consider |
| 248 // increasing the |timeout_ms| constant. | 125 // increasing the |timeout_ms| constant. |
| 249 // | 126 // |
| 250 // Flakily timing out on Mac 10.11 (crbug.com/646119) and flakily | 127 // Flakily timing out on Mac 10.11 (crbug.com/646119) and flakily |
| 251 // failing on Linux/ChromeOS (crbug.com/624836). | 128 // failing on Linux/ChromeOS (crbug.com/624836). |
| 252 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_CHROMEOS) | 129 #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 253 #define MAYBE_TimeAndURL DISABLED_TimeAndURL | 130 #define MAYBE_TimeAndURL DISABLED_TimeAndURL |
| 254 #else | 131 #else |
| 255 #define MAYBE_TimeAndURL TimeAndURL | 132 #define MAYBE_TimeAndURL TimeAndURL |
| 256 #endif | 133 #endif |
| 257 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, | 134 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, |
| 258 MAYBE_TimeAndURL) { | 135 MAYBE_TimeAndURL) { |
| 259 const int64_t timeout_ms = 1; | 136 const int64_t timeout_ms = 1; |
| 260 | 137 |
| 261 // Create some entries. | 138 // Create some entries. |
| 262 std::set<std::string> keys; | 139 std::set<std::string> keys; |
| 263 keys.insert("https://google.com/index.html"); | 140 keys.insert("https://google.com/index.html"); |
| 264 keys.insert("https://example.com/foo/bar/icon.png"); | 141 keys.insert("https://example.com/foo/bar/icon.png"); |
| 265 keys.insert("http://chrome.com"); | 142 keys.insert("http://chrome.com"); |
| 266 | 143 |
| 267 BrowserThread::PostTask( | 144 GetCacheTestUtil()->CreateCacheEntries(keys); |
| 268 BrowserThread::IO, FROM_HERE, | |
| 269 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::CreateCacheEntries, | |
| 270 base::Unretained(this), | |
| 271 base::ConstRef(keys))); | |
| 272 WaitForTasksOnIOThread(); | |
| 273 | 145 |
| 274 // Wait |timeout_ms| milliseconds for the cache to write the entries. | 146 // Wait |timeout_ms| milliseconds for the cache to write the entries. |
| 275 // This assures that future entries will have timestamps strictly greater than | 147 // This assures that future entries will have timestamps strictly greater than |
| 276 // the ones we just added. | 148 // the ones we just added. |
| 277 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(timeout_ms)); | 149 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(timeout_ms)); |
| 278 base::Time now = base::Time::Now(); | 150 base::Time now = base::Time::Now(); |
| 279 | 151 |
| 280 // Create a few more entries with a later timestamp. | 152 // Create a few more entries with a later timestamp. |
| 281 std::set<std::string> newer_keys; | 153 std::set<std::string> newer_keys; |
| 282 newer_keys.insert("https://google.com/"); | 154 newer_keys.insert("https://google.com/"); |
| 283 newer_keys.insert("https://example.com/foo/bar/icon2.png"); | 155 newer_keys.insert("https://example.com/foo/bar/icon2.png"); |
| 284 newer_keys.insert("https://example.com/foo/bar/icon3.png"); | 156 newer_keys.insert("https://example.com/foo/bar/icon3.png"); |
| 285 newer_keys.insert("http://example.com/foo/bar/icon4.png"); | 157 newer_keys.insert("http://example.com/foo/bar/icon4.png"); |
| 286 | 158 |
| 287 BrowserThread::PostTask( | 159 GetCacheTestUtil()->CreateCacheEntries(newer_keys); |
| 288 BrowserThread::IO, FROM_HERE, | |
| 289 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::CreateCacheEntries, | |
| 290 base::Unretained(this), | |
| 291 base::ConstRef(newer_keys))); | |
| 292 WaitForTasksOnIOThread(); | |
| 293 | 160 |
| 294 // Create a condition for entries with the "https://example.com" origin | 161 // Create a condition for entries with the "https://example.com" origin |
| 295 // created after waiting. | 162 // created after waiting. |
| 296 base::Callback<bool(const disk_cache::Entry*)> condition = | 163 base::Callback<bool(const disk_cache::Entry*)> condition = |
| 297 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( | 164 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( |
| 298 base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max()); | 165 base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max()); |
| 299 | 166 |
| 300 // Delete the entries. | 167 // Delete the entries. |
| 301 BrowserThread::PostTask( | 168 BrowserThread::PostTask( |
| 302 BrowserThread::IO, FROM_HERE, | 169 BrowserThread::IO, FROM_HERE, |
| 303 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, | 170 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, |
| 304 base::Unretained(this), | 171 base::Unretained(this), |
| 305 base::ConstRef(condition))); | 172 base::ConstRef(condition))); |
| 306 WaitForTasksOnIOThread(); | 173 WaitForTasksOnIOThread(); |
| 307 | 174 |
| 308 // Expect that only "icon2.png" and "icon3.png" were deleted. | 175 // Expect that only "icon2.png" and "icon3.png" were deleted. |
| 309 BrowserThread::PostTask( | |
| 310 BrowserThread::IO, FROM_HERE, | |
| 311 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetRemainingKeys, | |
| 312 base::Unretained(this))); | |
| 313 WaitForTasksOnIOThread(); | |
| 314 | |
| 315 keys.insert(newer_keys.begin(), newer_keys.end()); | 176 keys.insert(newer_keys.begin(), newer_keys.end()); |
| 316 keys.erase("https://example.com/foo/bar/icon2.png"); | 177 keys.erase("https://example.com/foo/bar/icon2.png"); |
| 317 keys.erase("https://example.com/foo/bar/icon3.png"); | 178 keys.erase("https://example.com/foo/bar/icon3.png"); |
| 318 CompareRemainingKeys(keys); | 179 CompareRemainingKeys(keys); |
| 319 } | 180 } |
| OLD | NEW |