Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: chrome/browser/permissions/permission_context_base_unittest.cc

Issue 2640033006: Convert AutoBlocker static class to KeyedService. (Closed)
Patch Set: Add clock, move browsing data tests, nits Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 response) {
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 // The response callback needs to be set here to test a response being made
656 // in the case of a site not being blacklisted or a safe browsing timeout. 658 // in the case of a site not being blacklisted or a safe browsing timeout.
657 permission_context.SetRespondPermissionCallback(base::Bind( 659 if (response == CONTENT_SETTING_ALLOW) {
658 &PermissionContextBaseTests::RespondToPermission, 660 permission_context.SetRespondPermissionCallback(
659 base::Unretained(this), &permission_context, id, url, false, response)); 661 base::Bind(&PermissionContextBaseTests::RespondToPermission,
662 base::Unretained(this), &permission_context, id, url,
663 true /* persist */, response));
664 }
665
660 permission_context.RequestPermission( 666 permission_context.RequestPermission(
661 web_contents(), id, url, true /* user_gesture */, 667 web_contents(), id, url, true /* user_gesture */,
662 base::Bind(&TestPermissionContext::TrackPermissionDecision, 668 base::Bind(&TestPermissionContext::TrackPermissionDecision,
663 base::Unretained(&permission_context))); 669 base::Unretained(&permission_context)));
670 EXPECT_EQ(response, permission_context.GetPermissionStatus(url, url));
664 671
665 ASSERT_EQ(1u, permission_context.decisions().size()); 672 if (response == CONTENT_SETTING_ALLOW) {
666 EXPECT_EQ(response, permission_context.decisions()[0]); 673 ASSERT_EQ(1u, permission_context.decisions().size());
674 EXPECT_EQ(response, permission_context.decisions()[0]);
675 }
667 } 676 }
668 677
669 private: 678 private:
670 // ChromeRenderViewHostTestHarness: 679 // ChromeRenderViewHostTestHarness:
671 void SetUp() override { 680 void SetUp() override {
672 ChromeRenderViewHostTestHarness::SetUp(); 681 ChromeRenderViewHostTestHarness::SetUp();
673 #if defined(OS_ANDROID) 682 #if defined(OS_ANDROID)
674 InfoBarService::CreateForWebContents(web_contents()); 683 InfoBarService::CreateForWebContents(web_contents());
675 #else 684 #else
676 PermissionRequestManager::CreateForWebContents(web_contents()); 685 PermissionRequestManager::CreateForWebContents(web_contents());
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 const GURL url("https://www.example.com"); 838 const GURL url("https://www.example.com");
830 std::set<std::string> blacklisted_permissions{ 839 std::set<std::string> blacklisted_permissions{
831 PermissionUtil::GetPermissionString( 840 PermissionUtil::GetPermissionString(
832 content::PermissionType::GEOLOCATION)}; 841 content::PermissionType::GEOLOCATION)};
833 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 842 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
834 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, 843 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION,
835 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, 844 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager,
836 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); 845 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK);
837 } 846 }
838 847
839 // Tests that a URL with a blacklisted permission is permitted to request a 848 // Tests that a URL that is blacklisted for one permission can still request
840 // non-blacklisted permission. 849 // another and grant another.
841 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { 850 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) {
842 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 851 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
843 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); 852 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
844 const GURL url("https://www.example.com"); 853 const GURL url("https://www.example.com");
845 std::set<std::string> blacklisted_permissions{ 854 std::set<std::string> blacklisted_permissions{
846 PermissionUtil::GetPermissionString( 855 PermissionUtil::GetPermissionString(
847 content::PermissionType::GEOLOCATION)}; 856 content::PermissionType::GEOLOCATION)};
848 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 857 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, 858 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS,
853 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, 859 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager,
854 url, 2000 /* timeout in ms */, 860 url, 2000 /* timeout */, CONTENT_SETTING_ALLOW);
855 CONTENT_SETTING_ALLOW);
856 } 861 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698