| 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/run_loop.h" | |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" | 17 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" |
| 18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/storage_partition.h" | 20 #include "content/public/browser/storage_partition.h" |
| 21 #include "net/disk_cache/disk_cache.h" | 21 #include "net/disk_cache/disk_cache.h" |
| 22 #include "net/http/http_cache.h" | 22 #include "net/http/http_cache.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 43 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest { | 43 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest { |
| 44 public: | 44 public: |
| 45 // Initialization ------------------------------------------------------------ | 45 // Initialization ------------------------------------------------------------ |
| 46 | 46 |
| 47 void SetUpOnMainThread() override { | 47 void SetUpOnMainThread() override { |
| 48 // Prepare the commonly used callbacks. | 48 // Prepare the commonly used callbacks. |
| 49 done_callback_ = base::Bind( | 49 done_callback_ = base::Bind( |
| 50 &ConditionalCacheDeletionHelperBrowserTest::DoneCallback, | 50 &ConditionalCacheDeletionHelperBrowserTest::DoneCallback, |
| 51 base::Unretained(this)); | 51 base::Unretained(this)); |
| 52 | 52 |
| 53 // UI and IO thread synchronization. |
| 54 waitable_event_ = base::MakeUnique<base::WaitableEvent>( |
| 55 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 56 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 57 |
| 53 // Get the storage partition. | 58 // Get the storage partition. |
| 54 partition_ = content::BrowserContext::GetDefaultStoragePartition( | 59 partition_ = content::BrowserContext::GetDefaultStoragePartition( |
| 55 browser()->profile()); | 60 browser()->profile()); |
| 56 | 61 |
| 57 // Get the cache backends. | 62 // Get the cache backends. |
| 58 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
| 59 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
| 60 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::SetUpOnIOThread, | 65 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::SetUpOnIOThread, |
| 61 base::Unretained(this))); | 66 base::Unretained(this))); |
| 62 WaitForTasksOnIOThread(); | 67 WaitForTasksOnIOThread(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 85 | 90 |
| 86 void TearDownOnIOThread() { | 91 void TearDownOnIOThread() { |
| 87 iterator_.reset(); | 92 iterator_.reset(); |
| 88 DoneCallback(net::OK); | 93 DoneCallback(net::OK); |
| 89 } | 94 } |
| 90 | 95 |
| 91 // Waiting for tasks to be done on IO thread. -------------------------------- | 96 // Waiting for tasks to be done on IO thread. -------------------------------- |
| 92 | 97 |
| 93 void WaitForTasksOnIOThread() { | 98 void WaitForTasksOnIOThread() { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 99 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 95 io_thread_loop_.reset(new base::RunLoop()); | 100 waitable_event_->Wait(); |
| 96 io_thread_loop_->Run(); | |
| 97 } | 101 } |
| 98 | 102 |
| 99 void SetNumberOfWaitedTasks(int count) { | 103 void SetNumberOfWaitedTasks(int count) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 101 remaining_tasks_ = count; | 105 remaining_tasks_ = count; |
| 102 } | 106 } |
| 103 | 107 |
| 104 void WaitForCompletion(int value) { | 108 void WaitForCompletion(int value) { |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 106 if (value >= 0) { | 110 if (value >= 0) { |
| 107 // We got the result immediately. | 111 // We got the result immediately. |
| 108 DoneCallback(value); | 112 DoneCallback(value); |
| 109 } else if (value == net::ERR_IO_PENDING) { | 113 } else if (value == net::ERR_IO_PENDING) { |
| 110 // We need to wait for the callback. | 114 // We need to wait for the callback. |
| 111 } else { | 115 } else { |
| 112 // An error has occurred. | 116 // An error has occurred. |
| 113 NOTREACHED(); | 117 NOTREACHED(); |
| 114 } | 118 } |
| 115 } | 119 } |
| 116 | 120 |
| 117 void DoneCallback(int value) { | 121 void DoneCallback(int value) { |
| 118 DCHECK_GE(value, 0); // Negative values represent an error. | 122 DCHECK_GE(value, 0); // Negative values represent an error. |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 120 if (--remaining_tasks_ > 0) | 124 if (--remaining_tasks_ > 0) |
| 121 return; | 125 return; |
| 122 | 126 |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 waitable_event_->Signal(); |
| 124 BrowserThread::PostTask( | |
| 125 BrowserThread::UI, FROM_HERE, | |
| 126 base::Bind(&base::RunLoop::Quit, | |
| 127 base::Unretained(io_thread_loop_.get()))); | |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Cache operation shorthands. ----------------------------------------------- | 130 // Cache operation shorthands. ----------------------------------------------- |
| 131 | 131 |
| 132 void CreateCacheEntries(const std::set<std::string>& keys) { | 132 void CreateCacheEntries(const std::set<std::string>& keys) { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 133 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 134 | 134 |
| 135 entries_.resize(keys.size()); | 135 entries_.resize(keys.size()); |
| 136 SetNumberOfWaitedTasks(keys.size()); | 136 SetNumberOfWaitedTasks(keys.size()); |
| 137 | 137 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 188 |
| 189 private: | 189 private: |
| 190 content::StoragePartition* partition_; | 190 content::StoragePartition* partition_; |
| 191 disk_cache::Backend* backend_ = nullptr; | 191 disk_cache::Backend* backend_ = nullptr; |
| 192 std::unique_ptr<disk_cache::Backend::Iterator> iterator_; | 192 std::unique_ptr<disk_cache::Backend::Iterator> iterator_; |
| 193 disk_cache::Entry* current_entry_; | 193 disk_cache::Entry* current_entry_; |
| 194 std::vector<disk_cache::Entry*> entries_; | 194 std::vector<disk_cache::Entry*> entries_; |
| 195 | 195 |
| 196 base::Callback<void(int)> done_callback_; | 196 base::Callback<void(int)> done_callback_; |
| 197 | 197 |
| 198 std::unique_ptr<base::RunLoop> io_thread_loop_; | 198 std::unique_ptr<base::WaitableEvent> waitable_event_; |
| 199 int remaining_tasks_; | 199 int remaining_tasks_; |
| 200 | 200 |
| 201 std::vector<std::string> remaining_keys_; | 201 std::vector<std::string> remaining_keys_; |
| 202 }; | 202 }; |
| 203 | 203 |
| 204 // Tests that ConditionalCacheDeletionHelper only deletes those cache entries | 204 // Tests that ConditionalCacheDeletionHelper only deletes those cache entries |
| 205 // that match the condition. | 205 // that match the condition. |
| 206 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { | 206 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { |
| 207 // Create 5 entries. | 207 // Create 5 entries. |
| 208 std::set<std::string> keys; | 208 std::set<std::string> keys; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 BrowserThread::IO, FROM_HERE, | 308 BrowserThread::IO, FROM_HERE, |
| 309 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetRemainingKeys, | 309 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::GetRemainingKeys, |
| 310 base::Unretained(this))); | 310 base::Unretained(this))); |
| 311 WaitForTasksOnIOThread(); | 311 WaitForTasksOnIOThread(); |
| 312 | 312 |
| 313 keys.insert(newer_keys.begin(), newer_keys.end()); | 313 keys.insert(newer_keys.begin(), newer_keys.end()); |
| 314 keys.erase("https://example.com/foo/bar/icon2.png"); | 314 keys.erase("https://example.com/foo/bar/icon2.png"); |
| 315 keys.erase("https://example.com/foo/bar/icon3.png"); | 315 keys.erase("https://example.com/foo/bar/icon3.png"); |
| 316 CompareRemainingKeys(keys); | 316 CompareRemainingKeys(keys); |
| 317 } | 317 } |
| OLD | NEW |