Chromium Code Reviews| 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/logging.h" | |
|
raymes
2017/02/08 01:42:06
nit: is this needed?
meredithl
2017/02/08 02:18:23
No! Forgot to remove it. Done.
| |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/test/scoped_feature_list.h" | 12 #include "base/test/scoped_feature_list.h" |
| 12 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
| 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 14 #include "chrome/browser/permissions/permission_util.h" | 15 #include "chrome/browser/permissions/permission_util.h" |
| 15 #include "chrome/common/chrome_features.h" | 16 #include "chrome/common/chrome_features.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 19 #include "components/safe_browsing_db/test_database_manager.h" | 20 #include "components/safe_browsing_db/test_database_manager.h" |
| 20 #include "content/public/browser/permission_type.h" | 21 #include "content/public/browser/permission_type.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 bool FilterGoogle(const GURL& url) { | 25 bool FilterGoogle(const GURL& url) { |
| 25 return url == "https://www.google.com/"; | 26 return url == "https://www.google.com/"; |
| 26 } | 27 } |
| 27 | 28 |
| 28 bool FilterAll(const GURL& url) { | 29 bool FilterAll(const GURL& url) { |
| 29 return true; | 30 return true; |
| 30 } | 31 } |
| 31 | 32 |
| 32 } // namespace | |
| 33 | |
| 34 class MockSafeBrowsingDatabaseManager | 33 class MockSafeBrowsingDatabaseManager |
| 35 : public safe_browsing::TestSafeBrowsingDatabaseManager { | 34 : public safe_browsing::TestSafeBrowsingDatabaseManager { |
| 36 public: | 35 public: |
| 37 explicit MockSafeBrowsingDatabaseManager(bool perform_callback) | 36 explicit MockSafeBrowsingDatabaseManager(bool perform_callback, bool enabled) |
| 38 : perform_callback_(perform_callback) {} | 37 : perform_callback_(perform_callback), enabled_(enabled) { |
| 38 } | |
| 39 | 39 |
| 40 bool CheckApiBlacklistUrl( | 40 bool CheckApiBlacklistUrl( |
| 41 const GURL& url, | 41 const GURL& url, |
| 42 safe_browsing::SafeBrowsingDatabaseManager::Client* client) override { | 42 safe_browsing::SafeBrowsingDatabaseManager::Client* client) override { |
| 43 | |
| 44 // Return true when able to synchronously determine that the url is safe. | |
| 45 if (!enabled_) { | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 43 if (perform_callback_) { | 49 if (perform_callback_) { |
| 44 safe_browsing::ThreatMetadata metadata; | 50 safe_browsing::ThreatMetadata metadata; |
| 45 const auto& blacklisted_permissions = permissions_blacklist_.find(url); | 51 const auto& blacklisted_permissions = permissions_blacklist_.find(url); |
| 46 if (blacklisted_permissions != permissions_blacklist_.end()) | 52 if (blacklisted_permissions != permissions_blacklist_.end()) |
| 47 metadata.api_permissions = blacklisted_permissions->second; | 53 metadata.api_permissions = blacklisted_permissions->second; |
| 48 client->OnCheckApiBlacklistUrlResult(url, metadata); | 54 client->OnCheckApiBlacklistUrlResult(url, metadata); |
| 49 } | 55 } |
| 50 return false; | 56 return false; |
| 51 } | 57 } |
| 52 | 58 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 63 | 69 |
| 64 void SetPerformCallback(bool perform_callback) { | 70 void SetPerformCallback(bool perform_callback) { |
| 65 perform_callback_ = perform_callback; | 71 perform_callback_ = perform_callback; |
| 66 } | 72 } |
| 67 | 73 |
| 68 protected: | 74 protected: |
| 69 ~MockSafeBrowsingDatabaseManager() override {} | 75 ~MockSafeBrowsingDatabaseManager() override {} |
| 70 | 76 |
| 71 private: | 77 private: |
| 72 bool perform_callback_; | 78 bool perform_callback_; |
| 79 bool enabled_; | |
| 73 std::map<GURL, std::set<std::string>> permissions_blacklist_; | 80 std::map<GURL, std::set<std::string>> permissions_blacklist_; |
| 74 | 81 |
| 75 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); | 82 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); |
| 76 }; | 83 }; |
| 77 | 84 |
| 85 } // namespace | |
| 86 | |
| 78 class PermissionDecisionAutoBlockerUnitTest | 87 class PermissionDecisionAutoBlockerUnitTest |
| 79 : public ChromeRenderViewHostTestHarness { | 88 : public ChromeRenderViewHostTestHarness { |
| 80 protected: | 89 protected: |
| 81 void SetUp() override { | 90 void SetUp() override { |
| 82 ChromeRenderViewHostTestHarness::SetUp(); | 91 ChromeRenderViewHostTestHarness::SetUp(); |
| 83 autoblocker_ = PermissionDecisionAutoBlocker::GetForProfile(profile()); | 92 autoblocker_ = PermissionDecisionAutoBlocker::GetForProfile(profile()); |
| 84 feature_list_.InitWithFeatures({features::kBlockPromptsIfDismissedOften, | 93 feature_list_.InitWithFeatures({features::kBlockPromptsIfDismissedOften, |
| 85 features::kPermissionsBlacklist}, | 94 features::kPermissionsBlacklist}, |
| 86 {}); | 95 {}); |
| 87 last_embargoed_status_ = false; | 96 last_embargoed_status_ = false; |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 url2, content::PermissionType::DURABLE_STORAGE)); | 258 url2, content::PermissionType::DURABLE_STORAGE)); |
| 250 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( | 259 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( |
| 251 url2, content::PermissionType::MIDI_SYSEX)); | 260 url2, content::PermissionType::MIDI_SYSEX)); |
| 252 } | 261 } |
| 253 | 262 |
| 254 // Test that an origin that has been blacklisted for a permission is embargoed. | 263 // Test that an origin that has been blacklisted for a permission is embargoed. |
| 255 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { | 264 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { |
| 256 GURL url("https://www.google.com"); | 265 GURL url("https://www.google.com"); |
| 257 | 266 |
| 258 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 267 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 259 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 268 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, |
| 269 true /* enabled */); | |
| 260 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 270 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 261 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 271 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 262 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 272 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 263 2000 /* timeout in ms */); | 273 2000 /* timeout in ms */); |
| 264 | 274 |
| 265 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 275 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); |
| 266 EXPECT_TRUE(last_embargoed_status()); | 276 EXPECT_TRUE(last_embargoed_status()); |
| 267 } | 277 } |
| 268 | 278 |
| 269 // Check that IsUnderEmbargo returns the correct value when the embargo is set | 279 // Check that IsUnderEmbargo returns the correct value when the embargo is set |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 clock()->Advance(base::TimeDelta::FromDays(3)); | 382 clock()->Advance(base::TimeDelta::FromDays(3)); |
| 373 EXPECT_TRUE( | 383 EXPECT_TRUE( |
| 374 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 384 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
| 375 } | 385 } |
| 376 | 386 |
| 377 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { | 387 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { |
| 378 GURL url("https://www.google.com"); | 388 GURL url("https://www.google.com"); |
| 379 clock()->SetNow(base::Time::Now()); | 389 clock()->SetNow(base::Time::Now()); |
| 380 | 390 |
| 381 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 391 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 382 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | 392 new MockSafeBrowsingDatabaseManager(false /* perform_callback */, |
| 393 true /* enabled */); | |
| 383 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 394 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 384 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 395 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 385 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 396 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 386 0 /* timeout in ms */); | 397 0 /* timeout in ms */); |
| 387 | 398 |
| 388 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 399 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); |
| 389 EXPECT_FALSE(last_embargoed_status()); | 400 EXPECT_FALSE(last_embargoed_status()); |
| 390 EXPECT_FALSE( | 401 EXPECT_FALSE( |
| 391 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 402 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); |
| 392 db_manager->SetPerformCallback(true); | 403 db_manager->SetPerformCallback(true); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 profile()->GetPrefs(), false /* is_incognito_profile */, | 477 profile()->GetPrefs(), false /* is_incognito_profile */, |
| 467 false /* is_guest_profile */)); | 478 false /* is_guest_profile */)); |
| 468 temp_map->ShutdownOnUIThread(); | 479 temp_map->ShutdownOnUIThread(); |
| 469 } | 480 } |
| 470 | 481 |
| 471 EXPECT_EQ(100, autoblocker()->GetDismissCount( | 482 EXPECT_EQ(100, autoblocker()->GetDismissCount( |
| 472 url, content::PermissionType::GEOLOCATION)); | 483 url, content::PermissionType::GEOLOCATION)); |
| 473 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( | 484 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( |
| 474 url, content::PermissionType::GEOLOCATION)); | 485 url, content::PermissionType::GEOLOCATION)); |
| 475 } | 486 } |
| 487 | |
| 488 // Test that a blacklisted permission should not be autoblocked if the database | |
| 489 // manager is disabled. | |
| 490 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDisabledDatabaseManager) { | |
| 491 GURL url("https://www.google.com"); | |
| 492 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
| 493 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, | |
| 494 false /* enabled */); | |
| 495 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | |
| 496 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
| 497 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | |
| 498 2000 /* timeout in ms */); | |
| 499 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | |
| 500 EXPECT_FALSE(last_embargoed_status()); | |
|
kcarattini
2017/02/08 02:07:28
Can you also check that the callback was run. Othe
meredithl
2017/02/08 02:18:23
Done.
| |
| 501 } | |
| OLD | NEW |