| 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 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 | 635 |
| 635 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); | 636 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
| 636 } | 637 } |
| 637 | 638 |
| 638 void TestPermissionsBlacklisting( | 639 void TestPermissionsBlacklisting( |
| 639 content::PermissionType permission_type, | 640 content::PermissionType permission_type, |
| 640 ContentSettingsType content_settings_type, | 641 ContentSettingsType content_settings_type, |
| 641 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | 642 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 642 const GURL& url, | 643 const GURL& url, |
| 643 int timeout, | 644 int timeout, |
| 644 ContentSetting response) { | 645 ContentSetting expected_permission_status) { |
| 645 NavigateAndCommit(url); | 646 NavigateAndCommit(url); |
| 646 base::test::ScopedFeatureList scoped_feature_list; | 647 base::test::ScopedFeatureList scoped_feature_list; |
| 647 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); | 648 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); |
| 648 TestPermissionContext permission_context(profile(), permission_type, | 649 TestPermissionContext permission_context(profile(), permission_type, |
| 649 content_settings_type); | 650 content_settings_type); |
| 650 permission_context.SetSafeBrowsingDatabaseManagerAndTimeoutForTest( | 651 PermissionDecisionAutoBlocker::GetForProfile(profile()) |
| 651 db_manager, timeout); | 652 ->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 653 timeout); |
| 652 const PermissionRequestID id( | 654 const PermissionRequestID id( |
| 653 web_contents()->GetRenderProcessHost()->GetID(), | 655 web_contents()->GetRenderProcessHost()->GetID(), |
| 654 web_contents()->GetMainFrame()->GetRoutingID(), -1); | 656 web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 655 // The response callback needs to be set here to test a response being made | 657 |
| 656 // 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 |
| 657 permission_context.SetRespondPermissionCallback(base::Bind( | 659 // expect he permission to be blacklisted, therefore set the response |
| 658 &PermissionContextBaseTests::RespondToPermission, | 660 // callback. |
| 659 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 |
| 660 permission_context.RequestPermission( | 668 permission_context.RequestPermission( |
| 661 web_contents(), id, url, true /* user_gesture */, | 669 web_contents(), id, url, true /* user_gesture */, |
| 662 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 670 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 663 base::Unretained(&permission_context))); | 671 base::Unretained(&permission_context))); |
| 672 EXPECT_EQ(expected_permission_status, |
| 673 permission_context.GetPermissionStatus(url, url)); |
| 664 | 674 |
| 665 ASSERT_EQ(1u, permission_context.decisions().size()); | 675 if (expected_permission_status == CONTENT_SETTING_ALLOW) { |
| 666 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 } |
| 667 } | 679 } |
| 668 | 680 |
| 669 private: | 681 private: |
| 670 // ChromeRenderViewHostTestHarness: | 682 // ChromeRenderViewHostTestHarness: |
| 671 void SetUp() override { | 683 void SetUp() override { |
| 672 ChromeRenderViewHostTestHarness::SetUp(); | 684 ChromeRenderViewHostTestHarness::SetUp(); |
| 673 #if defined(OS_ANDROID) | 685 #if defined(OS_ANDROID) |
| 674 InfoBarService::CreateForWebContents(web_contents()); | 686 InfoBarService::CreateForWebContents(web_contents()); |
| 675 #else | 687 #else |
| 676 PermissionRequestManager::CreateForWebContents(web_contents()); | 688 PermissionRequestManager::CreateForWebContents(web_contents()); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 832 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
| 821 TestParallelRequests(CONTENT_SETTING_ASK); | 833 TestParallelRequests(CONTENT_SETTING_ASK); |
| 822 } | 834 } |
| 823 | 835 |
| 824 // Tests a blacklisted (URL, permission) pair has had its permission request | 836 // Tests a blacklisted (URL, permission) pair has had its permission request |
| 825 // blocked. | 837 // blocked. |
| 826 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { | 838 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { |
| 827 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 839 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 828 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 840 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 829 const GURL url("https://www.example.com"); | 841 const GURL url("https://www.example.com"); |
| 830 std::set<std::string> blacklisted_permissions{ | 842 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 831 PermissionUtil::GetPermissionString( | |
| 832 content::PermissionType::GEOLOCATION)}; | |
| 833 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 843 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 834 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | 844 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, |
| 835 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | 845 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, |
| 836 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); | 846 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); |
| 837 } | 847 } |
| 838 | 848 |
| 839 // 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 |
| 840 // non-blacklisted permission. | 850 // another and grant another. |
| 841 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { | 851 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { |
| 842 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 852 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 843 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 853 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 844 const GURL url("https://www.example.com"); | 854 const GURL url("https://www.example.com"); |
| 845 std::set<std::string> blacklisted_permissions{ | 855 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 846 PermissionUtil::GetPermissionString( | |
| 847 content::PermissionType::GEOLOCATION)}; | |
| 848 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 856 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 849 TestPermissionsBlacklisting( | |
| 850 content::PermissionType::GEOLOCATION, CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 851 db_manager, url, 2000 /* timeout in ms */, CONTENT_SETTING_BLOCK); | |
| 852 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, | 857 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, |
| 853 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, | 858 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, |
| 854 url, 2000 /* timeout in ms */, | 859 url, 2000 /* timeout */, CONTENT_SETTING_ALLOW); |
| 855 CONTENT_SETTING_ALLOW); | |
| 856 } | 860 } |
| 857 | |
| 858 // Tests that a URL with a blacklisted permisison is permitted to request that | |
| 859 // permission if Safe Browsing has timed out. | |
| 860 TEST_F(PermissionContextBaseTests, TestSafeBrowsingTimeout) { | |
| 861 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
| 862 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | |
| 863 const GURL url("https://www.example.com"); | |
| 864 std::set<std::string> blacklisted_permissions{ | |
| 865 PermissionUtil::GetPermissionString( | |
| 866 content::PermissionType::GEOLOCATION)}; | |
| 867 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
| 868 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | |
| 869 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | |
| 870 url, 0 /* timeout in ms */, CONTENT_SETTING_ASK); | |
| 871 } | |
| OLD | NEW |