| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/permissions/permission_decision_auto_blocker.h" | 5 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/test/scoped_feature_list.h" | 11 #include "base/test/scoped_feature_list.h" |
| 12 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
| 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 14 #include "chrome/browser/permissions/permission_util.h" | 14 #include "chrome/browser/permissions/permission_util.h" |
| 15 #include "chrome/common/chrome_features.h" | 15 #include "chrome/common/chrome_features.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/safe_browsing_db/test_database_manager.h" | 19 #include "components/safe_browsing_db/test_database_manager.h" |
| 19 #include "content/public/browser/permission_type.h" | 20 #include "content/public/browser/permission_type.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 bool FilterGoogle(const GURL& url) { | 24 bool FilterGoogle(const GURL& url) { |
| 24 return url == "https://www.google.com/"; | 25 return url == "https://www.google.com/"; |
| 25 } | 26 } |
| 26 | 27 |
| 27 bool FilterAll(const GURL& url) { | 28 bool FilterAll(const GURL& url) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 if (quit_closure) { | 125 if (quit_closure) { |
| 125 quit_closure.Run(); | 126 quit_closure.Run(); |
| 126 quit_closure.Reset(); | 127 quit_closure.Reset(); |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 | 130 |
| 130 bool last_embargoed_status() { return last_embargoed_status_; } | 131 bool last_embargoed_status() { return last_embargoed_status_; } |
| 131 | 132 |
| 132 base::SimpleTestClock* clock() { return clock_; } | 133 base::SimpleTestClock* clock() { return clock_; } |
| 133 | 134 |
| 135 const char* GetDismissKey() { |
| 136 return PermissionDecisionAutoBlocker::kPromptDismissCountKey; |
| 137 } |
| 138 |
| 139 const char* GetIgnoreKey() { |
| 140 return PermissionDecisionAutoBlocker::kPromptIgnoreCountKey; |
| 141 } |
| 142 |
| 134 private: | 143 private: |
| 135 PermissionDecisionAutoBlocker* autoblocker_; | 144 PermissionDecisionAutoBlocker* autoblocker_; |
| 136 base::test::ScopedFeatureList feature_list_; | 145 base::test::ScopedFeatureList feature_list_; |
| 137 base::SimpleTestClock* clock_; | 146 base::SimpleTestClock* clock_; |
| 138 bool last_embargoed_status_; | 147 bool last_embargoed_status_; |
| 139 }; | 148 }; |
| 140 | 149 |
| 141 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { | 150 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { |
| 142 GURL url1("https://www.google.com"); | 151 GURL url1("https://www.google.com"); |
| 143 GURL url2("https://www.example.com"); | 152 GURL url2("https://www.example.com"); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 2000 /* timeout in ms */); | 394 2000 /* timeout in ms */); |
| 386 | 395 |
| 387 clock()->Advance(base::TimeDelta::FromDays(1)); | 396 clock()->Advance(base::TimeDelta::FromDays(1)); |
| 388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 397 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); |
| 389 EXPECT_TRUE(last_embargoed_status()); | 398 EXPECT_TRUE(last_embargoed_status()); |
| 390 | 399 |
| 391 clock()->Advance(base::TimeDelta::FromDays(1)); | 400 clock()->Advance(base::TimeDelta::FromDays(1)); |
| 392 EXPECT_TRUE( | 401 EXPECT_TRUE( |
| 393 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 402 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
| 394 } | 403 } |
| 404 |
| 405 // TODO(raymes): See crbug.com/681709. Remove after M60. |
| 406 TEST_F(PermissionDecisionAutoBlockerUnitTest, |
| 407 MigrateNoDecisionCountToPermissionAutoBlockerData) { |
| 408 GURL url("https://www.google.com"); |
| 409 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 410 |
| 411 // Write to the old content setting. |
| 412 base::DictionaryValue permissions_dict; |
| 413 permissions_dict.SetInteger(GetDismissKey(), 100); |
| 414 permissions_dict.SetInteger(GetIgnoreKey(), 50); |
| 415 |
| 416 base::DictionaryValue origin_dict; |
| 417 origin_dict.Set( |
| 418 PermissionUtil::GetPermissionString(content::PermissionType::GEOLOCATION), |
| 419 permissions_dict.CreateDeepCopy()); |
| 420 map->SetWebsiteSettingDefaultScope( |
| 421 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, |
| 422 std::string(), origin_dict.CreateDeepCopy()); |
| 423 |
| 424 // Nothing should be migrated yet, so the current values should be 0. |
| 425 EXPECT_EQ(0, autoblocker()->GetDismissCount( |
| 426 url, content::PermissionType::GEOLOCATION)); |
| 427 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( |
| 428 url, content::PermissionType::GEOLOCATION)); |
| 429 |
| 430 // Trigger pref migration which happens at the creation of the |
| 431 // HostContentSettingsMap. |
| 432 { |
| 433 scoped_refptr<HostContentSettingsMap> temp_map(new HostContentSettingsMap( |
| 434 profile()->GetPrefs(), false /* is_incognito_profile */, |
| 435 false /* is_guest_profile */)); |
| 436 temp_map->ShutdownOnUIThread(); |
| 437 } |
| 438 |
| 439 // The values should now be migrated. |
| 440 EXPECT_EQ(100, autoblocker()->GetDismissCount( |
| 441 url, content::PermissionType::GEOLOCATION)); |
| 442 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( |
| 443 url, content::PermissionType::GEOLOCATION)); |
| 444 |
| 445 // The old pref should be deleted. |
| 446 std::unique_ptr<base::DictionaryValue> old_dict = |
| 447 base::DictionaryValue::From(map->GetWebsiteSetting( |
| 448 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, |
| 449 std::string(), nullptr)); |
| 450 EXPECT_EQ(nullptr, old_dict); |
| 451 |
| 452 // Write to the old content setting again, but with different numbers. |
| 453 permissions_dict.SetInteger(GetDismissKey(), 99); |
| 454 permissions_dict.SetInteger(GetIgnoreKey(), 99); |
| 455 |
| 456 origin_dict.Set( |
| 457 PermissionUtil::GetPermissionString(content::PermissionType::GEOLOCATION), |
| 458 permissions_dict.CreateDeepCopy()); |
| 459 map->SetWebsiteSettingDefaultScope( |
| 460 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, |
| 461 std::string(), origin_dict.CreateDeepCopy()); |
| 462 |
| 463 // Ensure that migrating again does nothing. |
| 464 { |
| 465 scoped_refptr<HostContentSettingsMap> temp_map(new HostContentSettingsMap( |
| 466 profile()->GetPrefs(), false /* is_incognito_profile */, |
| 467 false /* is_guest_profile */)); |
| 468 temp_map->ShutdownOnUIThread(); |
| 469 } |
| 470 |
| 471 EXPECT_EQ(100, autoblocker()->GetDismissCount( |
| 472 url, content::PermissionType::GEOLOCATION)); |
| 473 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( |
| 474 url, content::PermissionType::GEOLOCATION)); |
| 475 } |
| OLD | NEW |