| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_context_base.h" | 5 #include "chrome/browser/permissions/permission_context_base.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 respond_permission_.Run(); | 160 respond_permission_.Run(); |
| 161 respond_permission_.Reset(); | 161 respond_permission_.Reset(); |
| 162 } else { | 162 } else { |
| 163 // Stop the run loop from spinning indefinitely if no response callback | 163 // Stop the run loop from spinning indefinitely if no response callback |
| 164 // has been set, as is the case with TestParallelRequests. | 164 // has been set, as is the case with TestParallelRequests. |
| 165 quit_closure_.Run(); | 165 quit_closure_.Run(); |
| 166 quit_closure_.Reset(); | 166 quit_closure_.Reset(); |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Permission request will need to be responded to, so pass a callback to be | 170 // Set the callback to run if the permission is being responded to in the |
| 171 // run once the request has completed and the decision has been made. | 171 // test. This is left empty where no response is needed, such as in parallel |
| 172 // requests, permissions blacklisting, invalid origin, and killswitch. |
| 172 void SetRespondPermissionCallback(base::Closure callback) { | 173 void SetRespondPermissionCallback(base::Closure callback) { |
| 173 respond_permission_ = callback; | 174 respond_permission_ = callback; |
| 174 } | 175 } |
| 175 | 176 |
| 176 protected: | 177 protected: |
| 177 void UpdateTabContext(const PermissionRequestID& id, | 178 void UpdateTabContext(const PermissionRequestID& id, |
| 178 const GURL& requesting_origin, | 179 const GURL& requesting_origin, |
| 179 bool allowed) override { | 180 bool allowed) override { |
| 180 tab_context_updated_ = true; | 181 tab_context_updated_ = true; |
| 181 } | 182 } |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 PermissionUtil::GetPermissionString(permission_type), | 353 PermissionUtil::GetPermissionString(permission_type), |
| 353 i, 1); | 354 i, 1); |
| 354 ASSERT_EQ(1u, permission_context.decisions().size()); | 355 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 355 EXPECT_EQ(expected, permission_context.decisions()[0]); | 356 EXPECT_EQ(expected, permission_context.decisions()[0]); |
| 356 EXPECT_TRUE(permission_context.tab_context_updated()); | 357 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 357 EXPECT_EQ(expected, permission_context.GetPermissionStatus(url, url)); | 358 EXPECT_EQ(expected, permission_context.GetPermissionStatus(url, url)); |
| 358 } | 359 } |
| 359 | 360 |
| 360 TestPermissionContext permission_context(profile(), permission_type, | 361 TestPermissionContext permission_context(profile(), permission_type, |
| 361 content_settings_type); | 362 content_settings_type); |
| 363 const PermissionRequestID id( |
| 364 web_contents()->GetRenderProcessHost()->GetID(), |
| 365 web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 366 |
| 367 permission_context.SetRespondPermissionCallback( |
| 368 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 369 base::Unretained(this), &permission_context, id, url, false, |
| 370 CONTENT_SETTING_ASK)); |
| 371 |
| 372 permission_context.RequestPermission( |
| 373 web_contents(), id, url, true /* user_gesture */, |
| 374 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 375 base::Unretained(&permission_context))); |
| 362 | 376 |
| 363 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 377 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 364 permission_context.GetPermissionStatus(url, url)); | 378 permission_context.GetPermissionStatus(url, url)); |
| 365 } | 379 } |
| 366 | 380 |
| 367 void TestBlockOnSeveralDismissals_TestContent() { | 381 void TestBlockOnSeveralDismissals_TestContent() { |
| 368 GURL url("https://www.google.com"); | 382 GURL url("https://www.google.com"); |
| 369 NavigateAndCommit(url); | 383 NavigateAndCommit(url); |
| 370 base::HistogramTester histograms; | 384 base::HistogramTester histograms; |
| 371 | 385 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 histograms.ExpectTotalCount( | 495 histograms.ExpectTotalCount( |
| 482 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); | 496 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); |
| 483 histograms.ExpectBucketCount( | 497 histograms.ExpectBucketCount( |
| 484 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); | 498 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); |
| 485 } | 499 } |
| 486 | 500 |
| 487 // Ensure that we finish in the block state. | 501 // Ensure that we finish in the block state. |
| 488 TestPermissionContext permission_context( | 502 TestPermissionContext permission_context( |
| 489 profile(), content::PermissionType::MIDI_SYSEX, | 503 profile(), content::PermissionType::MIDI_SYSEX, |
| 490 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 504 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
| 491 | |
| 492 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 505 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 493 permission_context.GetPermissionStatus(url, url)); | 506 permission_context.GetPermissionStatus(url, url)); |
| 494 variations::testing::ClearAllVariationParams(); | 507 variations::testing::ClearAllVariationParams(); |
| 495 } | 508 } |
| 496 | 509 |
| 497 void TestRequestPermissionInvalidUrl( | 510 void TestRequestPermissionInvalidUrl( |
| 498 content::PermissionType permission_type, | 511 content::PermissionType permission_type, |
| 499 ContentSettingsType content_settings_type) { | 512 ContentSettingsType content_settings_type) { |
| 500 TestPermissionContext permission_context(profile(), permission_type, | 513 TestPermissionContext permission_context(profile(), permission_type, |
| 501 content_settings_type); | 514 content_settings_type); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 | 635 |
| 623 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); | 636 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
| 624 } | 637 } |
| 625 | 638 |
| 626 void TestPermissionsBlacklisting( | 639 void TestPermissionsBlacklisting( |
| 627 content::PermissionType permission_type, | 640 content::PermissionType permission_type, |
| 628 ContentSettingsType content_settings_type, | 641 ContentSettingsType content_settings_type, |
| 629 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | 642 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 630 const GURL& url, | 643 const GURL& url, |
| 631 int timeout, | 644 int timeout, |
| 632 ContentSetting response) { | 645 ContentSetting expected_permission_status) { |
| 633 NavigateAndCommit(url); | 646 NavigateAndCommit(url); |
| 634 base::test::ScopedFeatureList scoped_feature_list; | 647 base::test::ScopedFeatureList scoped_feature_list; |
| 635 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); | 648 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); |
| 636 TestPermissionContext permission_context(profile(), permission_type, | 649 TestPermissionContext permission_context(profile(), permission_type, |
| 637 content_settings_type); | 650 content_settings_type); |
| 638 permission_context.SetSafeBrowsingDatabaseManagerAndTimeoutForTest( | 651 PermissionDecisionAutoBlocker::GetForProfile(profile()) |
| 639 db_manager, timeout); | 652 ->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 653 timeout); |
| 640 const PermissionRequestID id( | 654 const PermissionRequestID id( |
| 641 web_contents()->GetRenderProcessHost()->GetID(), | 655 web_contents()->GetRenderProcessHost()->GetID(), |
| 642 web_contents()->GetMainFrame()->GetRoutingID(), -1); | 656 web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 643 // The response callback needs to be set here to test a response being made | 657 |
| 644 // in the case of a site not being blacklisted or a safe browsing timeout. | 658 // A response only needs to be made to the permission request if we do not |
| 645 permission_context.SetRespondPermissionCallback(base::Bind( | 659 // expect he permission to be blacklisted, therefore set the response |
| 646 &PermissionContextBaseTests::RespondToPermission, | 660 // callback. |
| 647 base::Unretained(this), &permission_context, id, url, false, response)); | 661 if (expected_permission_status == CONTENT_SETTING_ALLOW) { |
| 662 permission_context.SetRespondPermissionCallback( |
| 663 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 664 base::Unretained(this), &permission_context, id, url, |
| 665 true /* persist */, expected_permission_status)); |
| 666 } |
| 667 |
| 648 permission_context.RequestPermission( | 668 permission_context.RequestPermission( |
| 649 web_contents(), id, url, true /* user_gesture */, | 669 web_contents(), id, url, true /* user_gesture */, |
| 650 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 670 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 651 base::Unretained(&permission_context))); | 671 base::Unretained(&permission_context))); |
| 672 EXPECT_EQ(expected_permission_status, |
| 673 permission_context.GetPermissionStatus(url, url)); |
| 652 | 674 |
| 653 ASSERT_EQ(1u, permission_context.decisions().size()); | 675 if (expected_permission_status == CONTENT_SETTING_ALLOW) { |
| 654 EXPECT_EQ(response, permission_context.decisions()[0]); | 676 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 677 EXPECT_EQ(expected_permission_status, permission_context.decisions()[0]); |
| 678 } |
| 655 } | 679 } |
| 656 | 680 |
| 657 private: | 681 private: |
| 658 // ChromeRenderViewHostTestHarness: | 682 // ChromeRenderViewHostTestHarness: |
| 659 void SetUp() override { | 683 void SetUp() override { |
| 660 ChromeRenderViewHostTestHarness::SetUp(); | 684 ChromeRenderViewHostTestHarness::SetUp(); |
| 661 #if defined(OS_ANDROID) | 685 #if defined(OS_ANDROID) |
| 662 InfoBarService::CreateForWebContents(web_contents()); | 686 InfoBarService::CreateForWebContents(web_contents()); |
| 663 #else | 687 #else |
| 664 PermissionRequestManager::CreateForWebContents(web_contents()); | 688 PermissionRequestManager::CreateForWebContents(web_contents()); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 832 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
| 809 TestParallelRequests(CONTENT_SETTING_ASK); | 833 TestParallelRequests(CONTENT_SETTING_ASK); |
| 810 } | 834 } |
| 811 | 835 |
| 812 // Tests a blacklisted (URL, permission) pair has had its permission request | 836 // Tests a blacklisted (URL, permission) pair has had its permission request |
| 813 // blocked. | 837 // blocked. |
| 814 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { | 838 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { |
| 815 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 839 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 816 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 840 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 817 const GURL url("https://www.example.com"); | 841 const GURL url("https://www.example.com"); |
| 818 std::set<std::string> blacklisted_permissions{ | 842 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 819 PermissionUtil::GetPermissionString( | |
| 820 content::PermissionType::GEOLOCATION)}; | |
| 821 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 843 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 822 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | 844 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, |
| 823 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | 845 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, |
| 824 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); | 846 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); |
| 825 } | 847 } |
| 826 | 848 |
| 827 // Tests that a URL with a blacklisted permission is permitted to request a | 849 // Tests that a URL that is blacklisted for one permission can still request |
| 828 // non-blacklisted permission. | 850 // another and grant another. |
| 829 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { | 851 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { |
| 830 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 852 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 831 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 853 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 832 const GURL url("https://www.example.com"); | 854 const GURL url("https://www.example.com"); |
| 833 std::set<std::string> blacklisted_permissions{ | 855 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 834 PermissionUtil::GetPermissionString( | |
| 835 content::PermissionType::GEOLOCATION)}; | |
| 836 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 856 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 837 TestPermissionsBlacklisting( | |
| 838 content::PermissionType::GEOLOCATION, CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 839 db_manager, url, 2000 /* timeout in ms */, CONTENT_SETTING_BLOCK); | |
| 840 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, | 857 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, |
| 841 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, | 858 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, |
| 842 url, 2000 /* timeout in ms */, | 859 url, 2000 /* timeout */, CONTENT_SETTING_ALLOW); |
| 843 CONTENT_SETTING_ALLOW); | |
| 844 } | 860 } |
| 845 | |
| 846 // Tests that a URL with a blacklisted permisison is permitted to request that | |
| 847 // permission if Safe Browsing has timed out. | |
| 848 TEST_F(PermissionContextBaseTests, TestSafeBrowsingTimeout) { | |
| 849 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
| 850 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | |
| 851 const GURL url("https://www.example.com"); | |
| 852 std::set<std::string> blacklisted_permissions{ | |
| 853 PermissionUtil::GetPermissionString( | |
| 854 content::PermissionType::GEOLOCATION)}; | |
| 855 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
| 856 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | |
| 857 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | |
| 858 url, 0 /* timeout in ms */, CONTENT_SETTING_ASK); | |
| 859 } | |
| OLD | NEW |