Chromium Code Reviews| 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. |
|
dominickn
2017/01/20 04:03:47
Nit: comment indentation
meredithl
2017/01/20 04:21:33
Done.
| |
| 172 // This is left empty where no response is needed, such as in parallel | |
| 173 // requests, permissions blacklisting, invalid origin, and killswitch. | |
| 172 void SetRespondPermissionCallback(base::Closure callback) { | 174 void SetRespondPermissionCallback(base::Closure callback) { |
| 173 respond_permission_ = callback; | 175 respond_permission_ = callback; |
| 174 } | 176 } |
| 175 | 177 |
| 176 protected: | 178 protected: |
| 177 void UpdateTabContext(const PermissionRequestID& id, | 179 void UpdateTabContext(const PermissionRequestID& id, |
| 178 const GURL& requesting_origin, | 180 const GURL& requesting_origin, |
| 179 bool allowed) override { | 181 bool allowed) override { |
| 180 tab_context_updated_ = true; | 182 tab_context_updated_ = true; |
| 181 } | 183 } |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 ContentSettingsType content_settings_type, | 630 ContentSettingsType content_settings_type, |
| 629 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | 631 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 630 const GURL& url, | 632 const GURL& url, |
| 631 int timeout, | 633 int timeout, |
| 632 ContentSetting response) { | 634 ContentSetting response) { |
| 633 NavigateAndCommit(url); | 635 NavigateAndCommit(url); |
| 634 base::test::ScopedFeatureList scoped_feature_list; | 636 base::test::ScopedFeatureList scoped_feature_list; |
| 635 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); | 637 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); |
| 636 TestPermissionContext permission_context(profile(), permission_type, | 638 TestPermissionContext permission_context(profile(), permission_type, |
| 637 content_settings_type); | 639 content_settings_type); |
| 638 permission_context.SetSafeBrowsingDatabaseManagerAndTimeoutForTest( | 640 PermissionDecisionAutoBlocker::GetForProfile(profile()) |
| 639 db_manager, timeout); | 641 ->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 642 timeout); | |
| 640 const PermissionRequestID id( | 643 const PermissionRequestID id( |
| 641 web_contents()->GetRenderProcessHost()->GetID(), | 644 web_contents()->GetRenderProcessHost()->GetID(), |
| 642 web_contents()->GetMainFrame()->GetRoutingID(), -1); | 645 web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 643 // The response callback needs to be set here to test a response being made | 646 // The response callback needs to be set here to test a response being made |
| 644 // in the case of a site not being blacklisted or a safe browsing timeout. | 647 // in the case of a site not being blacklisted or a safe browsing timeout. |
| 645 permission_context.SetRespondPermissionCallback(base::Bind( | 648 if (response == CONTENT_SETTING_ALLOW) { |
| 646 &PermissionContextBaseTests::RespondToPermission, | 649 permission_context.SetRespondPermissionCallback( |
| 647 base::Unretained(this), &permission_context, id, url, false, response)); | 650 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 651 base::Unretained(this), &permission_context, id, url, | |
| 652 true /* persist */, response)); | |
| 653 } | |
| 654 | |
| 648 permission_context.RequestPermission( | 655 permission_context.RequestPermission( |
| 649 web_contents(), id, url, true /* user_gesture */, | 656 web_contents(), id, url, true /* user_gesture */, |
| 650 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 657 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 651 base::Unretained(&permission_context))); | 658 base::Unretained(&permission_context))); |
| 659 EXPECT_EQ(response, permission_context.GetPermissionStatus(url, url)); | |
| 652 | 660 |
| 653 ASSERT_EQ(1u, permission_context.decisions().size()); | 661 if (response == CONTENT_SETTING_ALLOW) { |
| 654 EXPECT_EQ(response, permission_context.decisions()[0]); | 662 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 663 EXPECT_EQ(response, permission_context.decisions()[0]); | |
| 664 } | |
| 655 } | 665 } |
| 656 | 666 |
| 657 private: | 667 private: |
| 658 // ChromeRenderViewHostTestHarness: | 668 // ChromeRenderViewHostTestHarness: |
| 659 void SetUp() override { | 669 void SetUp() override { |
| 660 ChromeRenderViewHostTestHarness::SetUp(); | 670 ChromeRenderViewHostTestHarness::SetUp(); |
| 661 #if defined(OS_ANDROID) | 671 #if defined(OS_ANDROID) |
| 662 InfoBarService::CreateForWebContents(web_contents()); | 672 InfoBarService::CreateForWebContents(web_contents()); |
| 663 #else | 673 #else |
| 664 PermissionRequestManager::CreateForWebContents(web_contents()); | 674 PermissionRequestManager::CreateForWebContents(web_contents()); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 817 const GURL url("https://www.example.com"); | 827 const GURL url("https://www.example.com"); |
| 818 std::set<std::string> blacklisted_permissions{ | 828 std::set<std::string> blacklisted_permissions{ |
| 819 PermissionUtil::GetPermissionString( | 829 PermissionUtil::GetPermissionString( |
| 820 content::PermissionType::GEOLOCATION)}; | 830 content::PermissionType::GEOLOCATION)}; |
| 821 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 831 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 822 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | 832 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, |
| 823 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | 833 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, |
| 824 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); | 834 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); |
| 825 } | 835 } |
| 826 | 836 |
| 827 // Tests that a URL with a blacklisted permission is permitted to request a | 837 // Tests that a URL that is blacklisted for one permission can still request |
| 828 // non-blacklisted permission. | 838 // another and grant another. |
| 829 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { | 839 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { |
| 830 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 840 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 831 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 841 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 832 const GURL url("https://www.example.com"); | 842 const GURL url("https://www.example.com"); |
| 833 std::set<std::string> blacklisted_permissions{ | 843 std::set<std::string> blacklisted_permissions{ |
| 834 PermissionUtil::GetPermissionString( | 844 PermissionUtil::GetPermissionString( |
| 835 content::PermissionType::GEOLOCATION)}; | 845 content::PermissionType::GEOLOCATION)}; |
| 836 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 846 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, | 847 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, |
| 841 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, | 848 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, |
| 842 url, 2000 /* timeout in ms */, | 849 url, 2000 /* timeout */, CONTENT_SETTING_ALLOW); |
| 843 CONTENT_SETTING_ALLOW); | |
| 844 } | 850 } |
| 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 |