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 "content/browser/browsing_data/conditional_cache_deletion_helper.h" |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/ui/browser.h" | |
18 #include "chrome/test/base/in_process_browser_test.h" | |
19 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" | |
20 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
21 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/storage_partition.h" | 18 #include "content/public/browser/storage_partition.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/cache_test_util.h" |
| 21 #include "content/public/test/content_browser_test.h" |
| 22 #include "content/shell/browser/shell.h" |
23 #include "net/disk_cache/disk_cache.h" | 23 #include "net/disk_cache/disk_cache.h" |
24 #include "net/http/http_cache.h" | 24 #include "net/http/http_cache.h" |
25 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
26 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
27 | 27 |
28 using browsing_data::ConditionalCacheDeletionHelper; | 28 namespace content { |
29 using content::BrowserThread; | |
30 | 29 |
31 namespace { | 30 namespace { |
32 | 31 |
33 bool KeyIsEven(const disk_cache::Entry* entry) { | 32 bool KeyIsEven(const disk_cache::Entry* entry) { |
34 int key_as_int = 0; | 33 int key_as_int = 0; |
35 base::StringToInt(entry->GetKey().c_str(), &key_as_int); | 34 base::StringToInt(entry->GetKey().c_str(), &key_as_int); |
36 return (key_as_int % 2) == 0; | 35 return (key_as_int % 2) == 0; |
37 } | 36 } |
38 | 37 |
39 bool HasHttpsExampleOrigin(const GURL& url) { | 38 bool HasHttpsExampleOrigin(const GURL& url) { |
40 return url.GetOrigin() == "https://example.com/"; | 39 return url.GetOrigin() == "https://example.com/"; |
41 } | 40 } |
42 | 41 |
43 } // namespace | 42 } // namespace |
44 | 43 |
45 class ConditionalCacheDeletionHelperBrowserTest : public InProcessBrowserTest { | 44 class ConditionalCacheDeletionHelperBrowserTest : public ContentBrowserTest { |
46 public: | 45 public: |
47 void SetUpOnMainThread() override { | 46 void SetUpOnMainThread() override { |
48 cache_util_ = base::MakeUnique<CacheTestUtil>( | 47 cache_util_ = base::MakeUnique<CacheTestUtil>( |
49 content::BrowserContext::GetDefaultStoragePartition( | 48 content::BrowserContext::GetDefaultStoragePartition( |
50 browser()->profile())); | 49 shell()->web_contents()->GetBrowserContext())); |
51 done_callback_ = | 50 done_callback_ = |
52 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DoneCallback, | 51 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DoneCallback, |
53 base::Unretained(this)); | 52 base::Unretained(this)); |
54 // UI and IO thread synchronization. | 53 // UI and IO thread synchronization. |
55 waitable_event_ = base::MakeUnique<base::WaitableEvent>( | 54 waitable_event_ = base::MakeUnique<base::WaitableEvent>( |
56 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 55 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
57 base::WaitableEvent::InitialState::NOT_SIGNALED); | 56 base::WaitableEvent::InitialState::NOT_SIGNALED); |
58 } | 57 } |
59 | 58 |
60 void TearDownOnMainThread() override { cache_util_.reset(); } | 59 void TearDownOnMainThread() override { cache_util_.reset(); } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { | 99 IN_PROC_BROWSER_TEST_F(ConditionalCacheDeletionHelperBrowserTest, Condition) { |
101 // Create 5 entries. | 100 // Create 5 entries. |
102 std::set<std::string> keys = {"123", "47", "56", "81", "42"}; | 101 std::set<std::string> keys = {"123", "47", "56", "81", "42"}; |
103 | 102 |
104 GetCacheTestUtil()->CreateCacheEntries(keys); | 103 GetCacheTestUtil()->CreateCacheEntries(keys); |
105 | 104 |
106 // Delete the entries whose keys are even numbers. | 105 // Delete the entries whose keys are even numbers. |
107 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
108 BrowserThread::IO, FROM_HERE, | 107 BrowserThread::IO, FROM_HERE, |
109 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, | 108 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, |
110 base::Unretained(this), | 109 base::Unretained(this), base::Bind(&KeyIsEven))); |
111 base::Bind(&KeyIsEven))); | |
112 WaitForTasksOnIOThread(); | 110 WaitForTasksOnIOThread(); |
113 | 111 |
114 // Expect that the keys with values 56 and 42 were deleted. | 112 // Expect that the keys with values 56 and 42 were deleted. |
115 keys.erase("56"); | 113 keys.erase("56"); |
116 keys.erase("42"); | 114 keys.erase("42"); |
117 CompareRemainingKeys(keys); | 115 CompareRemainingKeys(keys); |
118 } | 116 } |
119 | 117 |
120 // Tests that ConditionalCacheDeletionHelper correctly constructs a condition | 118 // Tests that ConditionalCacheDeletionHelper correctly constructs a condition |
121 // for time and URL. | 119 // for time and URL. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // Create a condition for entries with the "https://example.com" origin | 159 // Create a condition for entries with the "https://example.com" origin |
162 // created after waiting. | 160 // created after waiting. |
163 base::Callback<bool(const disk_cache::Entry*)> condition = | 161 base::Callback<bool(const disk_cache::Entry*)> condition = |
164 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( | 162 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( |
165 base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max()); | 163 base::Bind(&HasHttpsExampleOrigin), now, base::Time::Max()); |
166 | 164 |
167 // Delete the entries. | 165 // Delete the entries. |
168 BrowserThread::PostTask( | 166 BrowserThread::PostTask( |
169 BrowserThread::IO, FROM_HERE, | 167 BrowserThread::IO, FROM_HERE, |
170 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, | 168 base::Bind(&ConditionalCacheDeletionHelperBrowserTest::DeleteEntries, |
171 base::Unretained(this), | 169 base::Unretained(this), base::ConstRef(condition))); |
172 base::ConstRef(condition))); | |
173 WaitForTasksOnIOThread(); | 170 WaitForTasksOnIOThread(); |
174 | 171 |
175 // Expect that only "icon2.png" and "icon3.png" were deleted. | 172 // Expect that only "icon2.png" and "icon3.png" were deleted. |
176 keys.insert(newer_keys.begin(), newer_keys.end()); | 173 keys.insert(newer_keys.begin(), newer_keys.end()); |
177 keys.erase("https://example.com/foo/bar/icon2.png"); | 174 keys.erase("https://example.com/foo/bar/icon2.png"); |
178 keys.erase("https://example.com/foo/bar/icon3.png"); | 175 keys.erase("https://example.com/foo/bar/icon3.png"); |
179 CompareRemainingKeys(keys); | 176 CompareRemainingKeys(keys); |
180 } | 177 } |
| 178 |
| 179 } // namespace content |
OLD | NEW |