OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager.h" | 5 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager.h" |
6 | 6 |
| 7 #include <set> |
| 8 #include <utility> |
| 9 |
7 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/task/cancelable_task_tracker.h" |
8 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/test/simple_test_clock.h" |
| 16 #include "base/time/time.h" |
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 18 #include "chrome/browser/history/history_service_factory.h" |
10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 19 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 20 #include "chrome/browser/subresource_filter/subresource_filter_profile_context.h
" |
11 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f
actory.h" | 21 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f
actory.h" |
12 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
13 #include "components/content_settings/core/browser/host_content_settings_map.h" | 23 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 24 #include "components/content_settings/core/common/content_settings.h" |
| 25 #include "components/history/core/browser/history_service.h" |
| 26 #include "components/history/core/test/history_service_test_util.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "url/gurl.h" |
16 | 30 |
17 namespace { | 31 namespace { |
18 | 32 |
19 const char kActionsHistogram[] = "SubresourceFilter.Actions"; | 33 const char kActionsHistogram[] = "SubresourceFilter.Actions"; |
20 | 34 |
21 class SubresourceFilterContentSettingsManagerTest : public testing::Test { | 35 class SubresourceFilterContentSettingsManagerTest : public testing::Test { |
22 public: | 36 public: |
23 SubresourceFilterContentSettingsManagerTest() {} | 37 SubresourceFilterContentSettingsManagerTest() {} |
24 | 38 |
25 void SetUp() override { | 39 void SetUp() override { |
26 SubresourceFilterProfileContextFactory::EnsureForProfile(&testing_profile_); | 40 settings_manager_ = |
| 41 SubresourceFilterProfileContextFactory::GetForProfile(&testing_profile_) |
| 42 ->settings_manager(); |
| 43 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 44 test_clock_ = test_clock.get(); |
| 45 settings_manager_->set_clock_for_testing(std::move(test_clock)); |
27 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); | 46 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
28 } | 47 } |
29 | 48 |
30 HostContentSettingsMap* GetSettingsMap() { | 49 HostContentSettingsMap* GetSettingsMap() { |
31 return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); | 50 return HostContentSettingsMapFactory::GetForProfile(profile()); |
32 } | 51 } |
33 | 52 |
34 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | 53 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
35 | 54 |
| 55 SubresourceFilterContentSettingsManager* settings_manager() { |
| 56 return settings_manager_; |
| 57 } |
| 58 |
| 59 TestingProfile* profile() { return &testing_profile_; } |
| 60 |
| 61 ContentSetting GetContentSettingMatchingUrlWithEmptyPath(const GURL& url) { |
| 62 ContentSettingsForOneType host_settings; |
| 63 GetSettingsMap()->GetSettingsForOneType( |
| 64 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 65 std::string(), &host_settings); |
| 66 GURL url_with_empty_path = url.GetWithEmptyPath(); |
| 67 for (const auto& it : host_settings) { |
| 68 // Need GURL conversion to get rid of unnecessary default ports. |
| 69 if (GURL(it.primary_pattern.ToString()) == url_with_empty_path) |
| 70 return it.setting; |
| 71 } |
| 72 return CONTENT_SETTING_DEFAULT; |
| 73 } |
| 74 |
| 75 base::SimpleTestClock* test_clock() { return test_clock_; } |
| 76 |
36 private: | 77 private: |
| 78 base::ScopedTempDir scoped_dir_; |
| 79 |
37 content::TestBrowserThreadBundle thread_bundle_; | 80 content::TestBrowserThreadBundle thread_bundle_; |
38 TestingProfile testing_profile_; | 81 TestingProfile testing_profile_; |
39 base::HistogramTester histogram_tester_; | 82 base::HistogramTester histogram_tester_; |
40 | 83 |
| 84 // Owned by the testing_profile_. |
| 85 SubresourceFilterContentSettingsManager* settings_manager_ = nullptr; |
| 86 |
| 87 // Owned by the settings_manager_. |
| 88 base::SimpleTestClock* test_clock_ = nullptr; |
| 89 |
41 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerTest); | 90 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerTest); |
42 }; | 91 }; |
43 | 92 |
| 93 // It isn't very cheap to initialize the history service. Tests that need it can |
| 94 // use this harness. |
| 95 class SubresourceFilterContentSettingsManagerHistoryTest |
| 96 : public SubresourceFilterContentSettingsManagerTest { |
| 97 public: |
| 98 void SetUp() override { |
| 99 ASSERT_TRUE(profile()->CreateHistoryService(true /* delete_file */, |
| 100 false /* no_db */)); |
| 101 SubresourceFilterContentSettingsManagerTest::SetUp(); |
| 102 } |
| 103 }; |
| 104 |
44 TEST_F(SubresourceFilterContentSettingsManagerTest, IrrelevantSetting) { | 105 TEST_F(SubresourceFilterContentSettingsManagerTest, IrrelevantSetting) { |
45 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 106 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
46 CONTENT_SETTING_ALLOW); | 107 CONTENT_SETTING_ALLOW); |
47 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); | 108 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
48 } | 109 } |
49 | 110 |
50 TEST_F(SubresourceFilterContentSettingsManagerTest, DefaultSetting) { | 111 TEST_F(SubresourceFilterContentSettingsManagerTest, DefaultSetting) { |
51 GetSettingsMap()->SetDefaultContentSetting( | 112 GetSettingsMap()->SetDefaultContentSetting( |
52 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, CONTENT_SETTING_ALLOW); | 113 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, CONTENT_SETTING_ALLOW); |
53 histogram_tester().ExpectBucketCount(kActionsHistogram, | 114 histogram_tester().ExpectBucketCount(kActionsHistogram, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 152 |
92 GetSettingsMap()->SetContentSettingCustomScope( | 153 GetSettingsMap()->SetContentSettingCustomScope( |
93 primary_pattern, secondary_pattern, | 154 primary_pattern, secondary_pattern, |
94 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), | 155 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
95 CONTENT_SETTING_ALLOW); | 156 CONTENT_SETTING_ALLOW); |
96 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); | 157 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); |
97 histogram_tester().ExpectBucketCount(kActionsHistogram, | 158 histogram_tester().ExpectBucketCount(kActionsHistogram, |
98 kActionContentSettingsWildcardUpdate, 2); | 159 kActionContentSettingsWildcardUpdate, 2); |
99 } | 160 } |
100 | 161 |
| 162 TEST_F(SubresourceFilterContentSettingsManagerTest, SmartUI) { |
| 163 if (!settings_manager()->should_use_smart_ui()) |
| 164 return; |
| 165 |
| 166 GURL url("https://example.test/"); |
| 167 GURL url2("https://example.test/path"); |
| 168 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 169 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| 170 |
| 171 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 172 GetContentSettingMatchingUrlWithEmptyPath(url)); |
| 173 settings_manager()->OnDidShowUI(url); |
| 174 |
| 175 // Subsequent same-origin navigations should not show UI. |
| 176 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| 177 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2)); |
| 178 |
| 179 // Showing the UI should trigger a forced content setting update, but no |
| 180 // metrics should be recorded. |
| 181 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 182 kActionContentSettingsAllowed, 0); |
| 183 |
| 184 // Fast forward the clock. |
| 185 test_clock()->Advance( |
| 186 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain); |
| 187 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 188 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| 189 } |
| 190 |
| 191 // If the user manually sets a content setting to block the feature, the smart |
| 192 // UI should be reset. |
| 193 TEST_F(SubresourceFilterContentSettingsManagerTest, |
| 194 SmartUIWithOverride_Resets) { |
| 195 if (!settings_manager()->should_use_smart_ui()) |
| 196 return; |
| 197 |
| 198 GURL url("https://example.test/"); |
| 199 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 200 |
| 201 settings_manager()->OnDidShowUI(url); |
| 202 |
| 203 // Subsequent navigations to same-domains should not show UI. |
| 204 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| 205 |
| 206 // The user changed their mind, make sure the feature is showing up in the |
| 207 // settings UI. i.e. the setting should be non-default. |
| 208 EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetSitePermission(url)); |
| 209 GetSettingsMap()->SetContentSettingDefaultScope( |
| 210 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| 211 CONTENT_SETTING_BLOCK); |
| 212 |
| 213 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 214 kActionContentSettingsBlocked, 1); |
| 215 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 216 kActionContentSettingsBlockedFromUI, 0); |
| 217 histogram_tester().ExpectBucketCount( |
| 218 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 1); |
| 219 |
| 220 // Smart UI should be reset. |
| 221 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 222 } |
| 223 |
| 224 TEST_F(SubresourceFilterContentSettingsManagerTest, |
| 225 DistinguishMetricsFromUIAndSettingsPage) { |
| 226 GURL url("https://example.test/"); |
| 227 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 228 |
| 229 settings_manager()->OnDidShowUI(url); |
| 230 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
| 231 |
| 232 // Simulate changing the setting via the infobar UI. |
| 233 settings_manager()->WhitelistSite(url); |
| 234 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 235 kActionContentSettingsBlockedFromUI, 1); |
| 236 |
| 237 // The standard "Block" histograms are only triggered when blocking from the |
| 238 // settings UI, not our standard UI. |
| 239 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 240 kActionContentSettingsBlocked, 0); |
| 241 histogram_tester().ExpectBucketCount( |
| 242 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 0); |
| 243 |
| 244 GURL url2("https://example.test2/"); |
| 245 GetSettingsMap()->SetContentSettingDefaultScope( |
| 246 url2, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| 247 CONTENT_SETTING_BLOCK); |
| 248 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 249 kActionContentSettingsBlocked, 1); |
| 250 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 251 kActionContentSettingsBlockedFromUI, 1); |
| 252 } |
| 253 |
| 254 TEST_F(SubresourceFilterContentSettingsManagerHistoryTest, |
| 255 HistoryUrlDeleted_ClearsWebsiteSetting) { |
| 256 if (!settings_manager()->should_use_smart_ui()) |
| 257 return; |
| 258 |
| 259 // Simulate a history already populated with a URL. |
| 260 auto* history_service = HistoryServiceFactory::GetForProfile( |
| 261 profile(), ServiceAccessType::EXPLICIT_ACCESS); |
| 262 ASSERT_TRUE(history_service); |
| 263 history_service->AddPage(GURL("https://already-browsed.com/"), |
| 264 base::Time::Now(), history::SOURCE_BROWSED); |
| 265 |
| 266 // Ensure the website setting is set. |
| 267 GURL url("https://example.test"); |
| 268 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 269 settings_manager()->OnDidShowUI(url); |
| 270 |
| 271 // Simulate adding the page to the history. |
| 272 history_service->AddPage(url, base::Time::Now(), history::SOURCE_BROWSED); |
| 273 history::BlockUntilHistoryProcessesPendingRequests(history_service); |
| 274 |
| 275 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| 276 |
| 277 // Deleting the URL from history should clear the setting for this URL. Note |
| 278 // that since there is another URL in the history this won't clear all items. |
| 279 history_service->DeleteURL(url); |
| 280 history::BlockUntilHistoryProcessesPendingRequests(history_service); |
| 281 |
| 282 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 283 } |
| 284 |
| 285 TEST_F(SubresourceFilterContentSettingsManagerHistoryTest, |
| 286 AllHistoryUrlDeleted_ClearsWebsiteSetting) { |
| 287 if (!settings_manager()->should_use_smart_ui()) |
| 288 return; |
| 289 |
| 290 auto* history_service = HistoryServiceFactory::GetForProfile( |
| 291 profile(), ServiceAccessType::EXPLICIT_ACCESS); |
| 292 ASSERT_TRUE(history_service); |
| 293 |
| 294 GURL url1("https://example.test"); |
| 295 GURL url2("https://example.test"); |
| 296 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url1)); |
| 297 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| 298 settings_manager()->OnDidShowUI(url1); |
| 299 settings_manager()->OnDidShowUI(url2); |
| 300 |
| 301 // Simulate adding the pages to the history. |
| 302 history_service->AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED); |
| 303 history_service->AddPage(url2, base::Time::Now(), history::SOURCE_BROWSED); |
| 304 history::BlockUntilHistoryProcessesPendingRequests(history_service); |
| 305 |
| 306 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url1)); |
| 307 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2)); |
| 308 |
| 309 // Deleting all the URLs should clear everything. |
| 310 base::RunLoop run_loop; |
| 311 base::CancelableTaskTracker task_tracker; |
| 312 history_service->ExpireHistoryBetween(std::set<GURL>(), base::Time(), |
| 313 base::Time(), run_loop.QuitClosure(), |
| 314 &task_tracker); |
| 315 run_loop.Run(); |
| 316 |
| 317 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url1)); |
| 318 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| 319 } |
| 320 |
101 } // namespace | 321 } // namespace |
OLD | NEW |