[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
lalitm | ca47d9c4 | 2015-07-08 13:48:14 | [diff] [blame] | 5 | #include "chrome/browser/permissions/permission_context_base.h" |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 6 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 7 | #include <map> |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 8 | #include <set> |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 13 | #include "base/bind.h" |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 14 | #include "base/feature_list.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 15 | #include "base/macros.h" |
robliao | 79393ffb | 2016-09-21 18:45:29 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 17 | #include "base/metrics/field_trial.h" |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 18 | #include "base/run_loop.h" |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 19 | #include "base/test/histogram_tester.h" |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 20 | #include "base/test/mock_entropy_provider.h" |
dominickn | c2726ec | 2016-09-15 12:15:39 | [diff] [blame] | 21 | #include "base/test/scoped_feature_list.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 22 | #include "build/build_config.h" |
peconn | 5100d43 | 2015-09-16 12:03:08 | [diff] [blame] | 23 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 24 | #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
Patricia Lor | acc56f7 | 2017-07-04 01:21:56 | [diff] [blame] | 25 | #include "chrome/browser/permissions/permission_manager.h" |
mlamouri | dfbf569 | 2015-06-06 18:53:41 | [diff] [blame] | 26 | #include "chrome/browser/permissions/permission_request_id.h" |
timloh | 90e8cec | 2017-05-22 04:26:19 | [diff] [blame] | 27 | #include "chrome/browser/permissions/permission_request_manager.h" |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 28 | #include "chrome/browser/permissions/permission_uma_util.h" |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 29 | #include "chrome/browser/permissions/permission_util.h" |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 30 | #include "chrome/browser/ui/permission_bubble/mock_permission_prompt_factory.h" |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 31 | #include "chrome/common/chrome_features.h" |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 32 | #include "chrome/common/chrome_switches.h" |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 33 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 34 | #include "chrome/test/base/testing_profile.h" |
mukai | 8eaec82 | 2014-10-25 17:53:16 | [diff] [blame] | 35 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
mukai | 077089f | 2014-09-11 18:41:52 | [diff] [blame] | 36 | #include "components/content_settings/core/common/content_settings.h" |
[email protected] | 08f7101 | 2014-07-25 10:27:54 | [diff] [blame] | 37 | #include "components/content_settings/core/common/content_settings_types.h" |
Tim Volodine | e4593847 | 2017-09-21 10:08:22 | [diff] [blame] | 38 | #include "components/safe_browsing/db/database_manager.h" |
| 39 | #include "components/safe_browsing/db/test_database_manager.h" |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 40 | #include "components/variations/variations_associated_data.h" |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 41 | #include "content/public/browser/browser_thread.h" |
mlamouri | 5fc460f | 2015-06-03 17:30:28 | [diff] [blame] | 42 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 43 | #include "content/public/browser/web_contents.h" |
| 44 | #include "content/public/test/mock_render_process_host.h" |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 45 | #include "testing/gtest/include/gtest/gtest.h" |
| 46 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 47 | const char* const kPermissionsKillSwitchFieldStudy = |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 48 | PermissionContextBase::kPermissionsKillSwitchFieldStudy; |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 49 | const char* const kPermissionsKillSwitchBlockedValue = |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 50 | PermissionContextBase::kPermissionsKillSwitchBlockedValue; |
| 51 | const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 52 | const char* const kPromptGroupName = kPermissionsKillSwitchTestGroup; |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 53 | const char kPromptTrialName[] = "PermissionPromptsUX"; |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 54 | |
meredithl | 48ad1681 | 2017-02-08 03:15:37 | [diff] [blame] | 55 | namespace { |
| 56 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 57 | class MockSafeBrowsingDatabaseManager |
| 58 | : public safe_browsing::TestSafeBrowsingDatabaseManager { |
| 59 | public: |
| 60 | explicit MockSafeBrowsingDatabaseManager(bool perform_callback) |
| 61 | : perform_callback_(perform_callback) {} |
| 62 | |
| 63 | bool CheckApiBlacklistUrl( |
| 64 | const GURL& url, |
| 65 | safe_browsing::SafeBrowsingDatabaseManager::Client* client) override { |
| 66 | if (perform_callback_) { |
| 67 | safe_browsing::ThreatMetadata metadata; |
| 68 | const auto& blacklisted_permissions = permissions_blacklist_.find(url); |
| 69 | if (blacklisted_permissions != permissions_blacklist_.end()) |
| 70 | metadata.api_permissions = blacklisted_permissions->second; |
| 71 | client->OnCheckApiBlacklistUrlResult(url, metadata); |
| 72 | } |
| 73 | // Returns false if scheme is HTTP/HTTPS and able to be checked. |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | bool CancelApiCheck(Client* client) override { |
| 78 | DCHECK(!perform_callback_); |
| 79 | // Returns true when client check could be stopped. |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void BlacklistUrlPermissions(const GURL& url, |
| 84 | const std::set<std::string> permissions) { |
| 85 | permissions_blacklist_[url] = permissions; |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | ~MockSafeBrowsingDatabaseManager() override {} |
| 90 | |
| 91 | private: |
| 92 | bool perform_callback_; |
| 93 | std::map<GURL, std::set<std::string>> permissions_blacklist_; |
| 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); |
| 96 | }; |
| 97 | |
meredithl | 48ad1681 | 2017-02-08 03:15:37 | [diff] [blame] | 98 | } // namespace |
| 99 | |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 100 | class TestPermissionContext : public PermissionContextBase { |
| 101 | public: |
| 102 | TestPermissionContext(Profile* profile, |
lshang | 88ec36a | 2015-12-09 04:50:17 | [diff] [blame] | 103 | const ContentSettingsType content_settings_type) |
raymes | 21b9affc | 2017-05-31 06:15:26 | [diff] [blame] | 104 | : PermissionContextBase(profile, |
| 105 | content_settings_type, |
Luna Lu | bd7ae2f | 2017-11-07 15:18:03 | [diff] [blame] | 106 | blink::FeaturePolicyFeature::kNotFound), |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 107 | tab_context_updated_(false) {} |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 108 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 109 | ~TestPermissionContext() override {} |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 110 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 111 | const std::vector<ContentSetting>& decisions() const { return decisions_; } |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 112 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 113 | bool tab_context_updated() const { return tab_context_updated_; } |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 114 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 115 | // Once a decision for the requested permission has been made, run the |
| 116 | // callback. |
mlamouri | df357a31 | 2015-03-03 17:34:05 | [diff] [blame] | 117 | void TrackPermissionDecision(ContentSetting content_setting) { |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 118 | decisions_.push_back(content_setting); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 119 | // Null check required here as the quit_closure_ can also be run and reset |
| 120 | // first from within DecidePermission. |
| 121 | if (quit_closure_) { |
| 122 | quit_closure_.Run(); |
| 123 | quit_closure_.Reset(); |
| 124 | } |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 125 | } |
| 126 | |
johnme | c41dfee | 2016-01-13 14:35:16 | [diff] [blame] | 127 | ContentSetting GetContentSettingFromMap(const GURL& url_a, |
| 128 | const GURL& url_b) { |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 129 | auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 130 | return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
cm.sanchi | 761e67a | 2017-11-16 08:23:28 | [diff] [blame^] | 131 | content_settings_type(), std::string()); |
johnme | c41dfee | 2016-01-13 14:35:16 | [diff] [blame] | 132 | } |
| 133 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 134 | void RequestPermission(content::WebContents* web_contents, |
| 135 | const PermissionRequestID& id, |
| 136 | const GURL& requesting_frame, |
| 137 | bool user_gesture, |
| 138 | const BrowserPermissionCallback& callback) override { |
| 139 | base::RunLoop run_loop; |
| 140 | quit_closure_ = run_loop.QuitClosure(); |
| 141 | PermissionContextBase::RequestPermission(web_contents, id, requesting_frame, |
| 142 | true /* user_gesture */, callback); |
| 143 | run_loop.Run(); |
| 144 | } |
| 145 | |
| 146 | void DecidePermission(content::WebContents* web_contents, |
| 147 | const PermissionRequestID& id, |
| 148 | const GURL& requesting_origin, |
| 149 | const GURL& embedding_origin, |
| 150 | bool user_gesture, |
| 151 | const BrowserPermissionCallback& callback) override { |
| 152 | PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, |
| 153 | embedding_origin, user_gesture, |
| 154 | callback); |
| 155 | if (respond_permission_) { |
| 156 | respond_permission_.Run(); |
| 157 | respond_permission_.Reset(); |
| 158 | } else { |
| 159 | // Stop the run loop from spinning indefinitely if no response callback |
| 160 | // has been set, as is the case with TestParallelRequests. |
| 161 | quit_closure_.Run(); |
| 162 | quit_closure_.Reset(); |
| 163 | } |
| 164 | } |
| 165 | |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 166 | // Set the callback to run if the permission is being responded to in the |
| 167 | // test. This is left empty where no response is needed, such as in parallel |
| 168 | // requests, permissions blacklisting, invalid origin, and killswitch. |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 169 | void SetRespondPermissionCallback(base::Closure callback) { |
| 170 | respond_permission_ = callback; |
| 171 | } |
| 172 | |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 173 | protected: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 174 | void UpdateTabContext(const PermissionRequestID& id, |
| 175 | const GURL& requesting_origin, |
| 176 | bool allowed) override { |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 177 | tab_context_updated_ = true; |
| 178 | } |
| 179 | |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 180 | bool IsRestrictedToSecureOrigins() const override { return false; } |
mlamouri | a31c6ff1 | 2015-06-01 15:40:52 | [diff] [blame] | 181 | |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 182 | private: |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 183 | std::vector<ContentSetting> decisions_; |
| 184 | bool tab_context_updated_; |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 185 | base::Closure quit_closure_; |
| 186 | // Callback for responding to a permission once the request has been completed |
| 187 | // (valid URL, kill switch disabled, not blacklisted) |
| 188 | base::Closure respond_permission_; |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 189 | DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | class TestKillSwitchPermissionContext : public TestPermissionContext { |
| 193 | public: |
| 194 | TestKillSwitchPermissionContext( |
| 195 | Profile* profile, |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 196 | const ContentSettingsType content_settings_type) |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 197 | : TestPermissionContext(profile, content_settings_type), |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 198 | field_trial_list_(base::MakeUnique<base::FieldTrialList>( |
| 199 | base::MakeUnique<base::MockEntropyProvider>())) {} |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 200 | |
| 201 | void ResetFieldTrialList() { |
| 202 | // Destroy the existing FieldTrialList before creating a new one to avoid |
| 203 | // a DCHECK. |
| 204 | field_trial_list_.reset(); |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 205 | field_trial_list_ = base::MakeUnique<base::FieldTrialList>( |
| 206 | base::MakeUnique<base::MockEntropyProvider>()); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 207 | variations::testing::ClearAllVariationParams(); |
| 208 | } |
| 209 | |
| 210 | private: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 211 | std::unique_ptr<base::FieldTrialList> field_trial_list_; |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 212 | |
| 213 | DISALLOW_COPY_AND_ASSIGN(TestKillSwitchPermissionContext); |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 214 | }; |
| 215 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 216 | class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 217 | protected: |
| 218 | PermissionContextBaseTests() {} |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 219 | ~PermissionContextBaseTests() override {} |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 220 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 221 | // Accept or dismiss the permission prompt. |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 222 | void RespondToPermission(TestPermissionContext* context, |
| 223 | const PermissionRequestID& id, |
| 224 | const GURL& url, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 225 | bool persist, |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 226 | ContentSetting response) { |
| 227 | DCHECK(response == CONTENT_SETTING_ALLOW || |
| 228 | response == CONTENT_SETTING_BLOCK || |
| 229 | response == CONTENT_SETTING_ASK); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 230 | PermissionRequestManager* manager = |
| 231 | PermissionRequestManager::FromWebContents(web_contents()); |
| 232 | manager->TogglePersist(persist); |
| 233 | using AutoResponseType = PermissionRequestManager::AutoResponseType; |
| 234 | AutoResponseType decision = AutoResponseType::DISMISS; |
| 235 | if (response == CONTENT_SETTING_ALLOW) |
| 236 | decision = AutoResponseType::ACCEPT_ALL; |
| 237 | else if (response == CONTENT_SETTING_BLOCK) |
| 238 | decision = AutoResponseType::DENY_ALL; |
| 239 | prompt_factory_->set_response_type(decision); |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 240 | } |
| 241 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 242 | void TestAskAndDecide_TestContent(ContentSettingsType content_settings_type, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 243 | ContentSetting decision, |
| 244 | bool persist) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 245 | TestPermissionContext permission_context(profile(), content_settings_type); |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 246 | GURL url("https://www.google.com"); |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 247 | SetUpUrl(url); |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 248 | base::HistogramTester histograms; |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 249 | |
| 250 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 251 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 252 | web_contents()->GetMainFrame()->GetRoutingID(), -1); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 253 | permission_context.SetRespondPermissionCallback( |
| 254 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 255 | base::Unretained(this), &permission_context, id, url, |
| 256 | persist, decision)); |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 257 | permission_context.RequestPermission( |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 258 | web_contents(), id, url, true /* user_gesture */, |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 259 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 260 | base::Unretained(&permission_context))); |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 261 | ASSERT_EQ(1u, permission_context.decisions().size()); |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 262 | EXPECT_EQ(decision, permission_context.decisions()[0]); |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 263 | EXPECT_TRUE(permission_context.tab_context_updated()); |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 264 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 265 | std::string decision_string; |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 266 | if (decision == CONTENT_SETTING_ALLOW) |
| 267 | decision_string = "Accepted"; |
| 268 | else if (decision == CONTENT_SETTING_BLOCK) |
| 269 | decision_string = "Denied"; |
| 270 | else if (decision == CONTENT_SETTING_ASK) |
| 271 | decision_string = "Dismissed"; |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 272 | |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 273 | if (decision_string.size()) { |
| 274 | histograms.ExpectUniqueSample( |
| 275 | "Permissions.Prompt." + decision_string + ".PriorDismissCount." + |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 276 | PermissionUtil::GetPermissionString(content_settings_type), |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 277 | 0, 1); |
| 278 | histograms.ExpectUniqueSample( |
| 279 | "Permissions.Prompt." + decision_string + ".PriorIgnoreCount." + |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 280 | PermissionUtil::GetPermissionString(content_settings_type), |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 281 | 0, 1); |
| 282 | } |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 283 | |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 284 | if (persist) { |
| 285 | EXPECT_EQ(decision, |
| 286 | permission_context.GetContentSettingFromMap(url, url)); |
| 287 | } else { |
| 288 | EXPECT_EQ(CONTENT_SETTING_ASK, |
| 289 | permission_context.GetContentSettingFromMap(url, url)); |
| 290 | } |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 291 | |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 292 | histograms.ExpectUniqueSample( |
| 293 | "Permissions.AutoBlocker.EmbargoPromptSuppression", |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 294 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), 1); |
| 295 | histograms.ExpectUniqueSample( |
| 296 | "Permissions.AutoBlocker.EmbargoStatus", |
| 297 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), 1); |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 298 | } |
| 299 | |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 300 | void DismissMultipleTimesAndExpectBlock( |
| 301 | const GURL& url, |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 302 | ContentSettingsType content_settings_type, |
| 303 | uint32_t iterations) { |
| 304 | base::HistogramTester histograms; |
| 305 | |
| 306 | // Dismiss |iterations| times. The final dismiss should change the decision |
| 307 | // from dismiss to block, and hence change the persisted content setting. |
| 308 | for (uint32_t i = 0; i < iterations; ++i) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 309 | TestPermissionContext permission_context(profile(), |
| 310 | content_settings_type); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 311 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 312 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 313 | web_contents()->GetMainFrame()->GetRoutingID(), i); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 314 | |
| 315 | permission_context.SetRespondPermissionCallback( |
| 316 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 317 | base::Unretained(this), &permission_context, id, url, |
| 318 | false, CONTENT_SETTING_ASK)); |
| 319 | |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 320 | permission_context.RequestPermission( |
| 321 | web_contents(), id, url, true /* user_gesture */, |
| 322 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 323 | base::Unretained(&permission_context))); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 324 | histograms.ExpectTotalCount( |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 325 | "Permissions.Prompt.Dismissed.PriorDismissCount." + |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 326 | PermissionUtil::GetPermissionString(content_settings_type), |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 327 | i + 1); |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 328 | histograms.ExpectBucketCount( |
| 329 | "Permissions.Prompt.Dismissed.PriorDismissCount." + |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 330 | PermissionUtil::GetPermissionString(content_settings_type), |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 331 | i, 1); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 332 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 333 | histograms.ExpectTotalCount("Permissions.AutoBlocker.EmbargoStatus", |
| 334 | i + 1); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 335 | |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 336 | PermissionResult result = permission_context.GetPermissionStatus( |
| 337 | nullptr /* render_frame_host */, url, url); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 338 | |
| 339 | histograms.ExpectUniqueSample( |
| 340 | "Permissions.AutoBlocker.EmbargoPromptSuppression", |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 341 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), i + 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 342 | if (i < 2) { |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 343 | EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 344 | EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 345 | histograms.ExpectUniqueSample( |
| 346 | "Permissions.AutoBlocker.EmbargoStatus", |
| 347 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), i + 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 348 | } else { |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 349 | EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 350 | EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 351 | histograms.ExpectBucketCount( |
| 352 | "Permissions.AutoBlocker.EmbargoStatus", |
| 353 | static_cast<int>(PermissionEmbargoStatus::REPEATED_DISMISSALS), 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 354 | } |
| 355 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 356 | ASSERT_EQ(1u, permission_context.decisions().size()); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 357 | EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 358 | EXPECT_TRUE(permission_context.tab_context_updated()); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 359 | } |
| 360 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 361 | TestPermissionContext permission_context(profile(), content_settings_type); |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 362 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 363 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 364 | web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 365 | |
| 366 | permission_context.SetRespondPermissionCallback( |
| 367 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 368 | base::Unretained(this), &permission_context, id, url, false, |
| 369 | CONTENT_SETTING_ASK)); |
| 370 | |
| 371 | permission_context.RequestPermission( |
| 372 | web_contents(), id, url, true /* user_gesture */, |
| 373 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 374 | base::Unretained(&permission_context))); |
meredithl | cda94daf | 2017-01-19 03:03:35 | [diff] [blame] | 375 | |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 376 | PermissionResult result = permission_context.GetPermissionStatus( |
| 377 | nullptr /* render_frame_host */, url, url); |
raymes | ab35971 | 2017-02-15 06:23:25 | [diff] [blame] | 378 | EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 379 | EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 380 | histograms.ExpectBucketCount( |
| 381 | "Permissions.AutoBlocker.EmbargoPromptSuppression", |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 382 | static_cast<int>(PermissionEmbargoStatus::REPEATED_DISMISSALS), 1); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void TestBlockOnSeveralDismissals_TestContent() { |
| 386 | GURL url("https://www.google.com"); |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 387 | SetUpUrl(url); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 388 | base::HistogramTester histograms; |
| 389 | |
dominickn | 5b41f22 | 2017-05-11 03:44:26 | [diff] [blame] | 390 | { |
| 391 | // Ensure that > 3 dismissals behaves correctly when the |
| 392 | // BlockPromptsIfDismissedOften feature is off. |
| 393 | base::test::ScopedFeatureList feature_list; |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 394 | feature_list.InitAndDisableFeature( |
| 395 | features::kBlockPromptsIfDismissedOften); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 396 | |
dominickn | 5b41f22 | 2017-05-11 03:44:26 | [diff] [blame] | 397 | for (uint32_t i = 0; i < 4; ++i) { |
| 398 | TestPermissionContext permission_context( |
| 399 | profile(), CONTENT_SETTINGS_TYPE_GEOLOCATION); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 400 | |
dominickn | 5b41f22 | 2017-05-11 03:44:26 | [diff] [blame] | 401 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 402 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
dominickn | 5b41f22 | 2017-05-11 03:44:26 | [diff] [blame] | 403 | web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 404 | |
| 405 | permission_context.SetRespondPermissionCallback( |
| 406 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 407 | base::Unretained(this), &permission_context, id, url, |
| 408 | false, CONTENT_SETTING_ASK)); |
| 409 | permission_context.RequestPermission( |
| 410 | web_contents(), id, url, true /* user_gesture */, |
| 411 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 412 | base::Unretained(&permission_context))); |
| 413 | histograms.ExpectTotalCount( |
| 414 | "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", |
| 415 | i + 1); |
| 416 | histograms.ExpectBucketCount( |
| 417 | "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); |
| 418 | histograms.ExpectUniqueSample( |
| 419 | "Permissions.AutoBlocker.EmbargoPromptSuppression", |
| 420 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), i + 1); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 421 | histograms.ExpectUniqueSample( |
| 422 | "Permissions.AutoBlocker.EmbargoStatus", |
| 423 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), i + 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 424 | |
dominickn | 5b41f22 | 2017-05-11 03:44:26 | [diff] [blame] | 425 | ASSERT_EQ(1u, permission_context.decisions().size()); |
| 426 | EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
| 427 | EXPECT_TRUE(permission_context.tab_context_updated()); |
| 428 | EXPECT_EQ(CONTENT_SETTING_ASK, |
| 429 | permission_context.GetContentSettingFromMap(url, url)); |
| 430 | } |
| 431 | |
| 432 | // Flush the dismissal counts. |
| 433 | auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 434 | map->ClearSettingsForOneType( |
| 435 | CONTENT_SETTINGS_TYPE_PERMISSION_AUTOBLOCKER_DATA); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 436 | } |
| 437 | |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 438 | EXPECT_TRUE( |
| 439 | base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)); |
| 440 | |
| 441 | // Sanity check independence per permission type by checking two of them. |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 442 | DismissMultipleTimesAndExpectBlock(url, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 443 | 3); |
| 444 | DismissMultipleTimesAndExpectBlock(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 445 | 3); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | void TestVariationBlockOnSeveralDismissals_TestContent() { |
| 449 | GURL url("https://www.google.com"); |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 450 | SetUpUrl(url); |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 451 | base::HistogramTester histograms; |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 452 | |
| 453 | // Set up the custom parameter and custom value. |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 454 | base::FieldTrialList field_trials(nullptr); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 455 | base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
| 456 | kPromptTrialName, kPromptGroupName); |
| 457 | std::map<std::string, std::string> params; |
| 458 | params[PermissionDecisionAutoBlocker::kPromptDismissCountKey] = "5"; |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 459 | ASSERT_TRUE(variations::AssociateVariationParams(kPromptTrialName, |
| 460 | kPromptGroupName, params)); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 461 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 462 | std::unique_ptr<base::FeatureList> feature_list = |
| 463 | base::MakeUnique<base::FeatureList>(); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 464 | feature_list->RegisterFieldTrialOverride( |
| 465 | features::kBlockPromptsIfDismissedOften.name, |
| 466 | base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial); |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 467 | |
| 468 | base::test::ScopedFeatureList scoped_feature_list; |
| 469 | scoped_feature_list.InitWithFeatureList(std::move(feature_list)); |
| 470 | |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 471 | EXPECT_EQ(base::FeatureList::GetFieldTrial( |
| 472 | features::kBlockPromptsIfDismissedOften), |
| 473 | trial); |
| 474 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 475 | { |
| 476 | std::map<std::string, std::string> actual_params; |
| 477 | EXPECT_TRUE(variations::GetVariationParamsByFeature( |
| 478 | features::kBlockPromptsIfDismissedOften, &actual_params)); |
| 479 | EXPECT_EQ(params, actual_params); |
| 480 | } |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 481 | |
| 482 | for (uint32_t i = 0; i < 5; ++i) { |
| 483 | TestPermissionContext permission_context( |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 484 | profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 485 | |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 486 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 487 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 488 | web_contents()->GetMainFrame()->GetRoutingID(), i); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 489 | permission_context.SetRespondPermissionCallback( |
| 490 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 491 | base::Unretained(this), &permission_context, id, url, |
| 492 | false, CONTENT_SETTING_ASK)); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 493 | permission_context.RequestPermission( |
| 494 | web_contents(), id, url, true /* user_gesture */, |
| 495 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 496 | base::Unretained(&permission_context))); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 497 | |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 498 | EXPECT_EQ(1u, permission_context.decisions().size()); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 499 | ASSERT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 500 | EXPECT_TRUE(permission_context.tab_context_updated()); |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 501 | PermissionResult result = permission_context.GetPermissionStatus( |
| 502 | nullptr /* render_frame_host */, url, url); |
dominickn | 6da2b38 | 2016-08-23 20:21:30 | [diff] [blame] | 503 | |
| 504 | histograms.ExpectTotalCount( |
| 505 | "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); |
| 506 | histograms.ExpectBucketCount( |
| 507 | "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 508 | histograms.ExpectUniqueSample( |
| 509 | "Permissions.AutoBlocker.EmbargoPromptSuppression", |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 510 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), i + 1); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 511 | histograms.ExpectTotalCount("Permissions.AutoBlocker.EmbargoStatus", |
| 512 | i + 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 513 | if (i < 4) { |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 514 | EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 515 | EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 516 | histograms.ExpectUniqueSample( |
| 517 | "Permissions.AutoBlocker.EmbargoStatus", |
| 518 | static_cast<int>(PermissionEmbargoStatus::NOT_EMBARGOED), i + 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 519 | } else { |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 520 | EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 521 | EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 522 | histograms.ExpectBucketCount( |
| 523 | "Permissions.AutoBlocker.EmbargoStatus", |
| 524 | static_cast<int>(PermissionEmbargoStatus::REPEATED_DISMISSALS), 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 525 | } |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Ensure that we finish in the block state. |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 529 | TestPermissionContext permission_context(profile(), |
| 530 | CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 531 | PermissionResult result = permission_context.GetPermissionStatus( |
| 532 | nullptr /* render_frame_host */, url, url); |
raymes | ab35971 | 2017-02-15 06:23:25 | [diff] [blame] | 533 | EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 534 | EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 535 | variations::testing::ClearAllVariationParams(); |
| 536 | } |
| 537 | |
lshang | 88ec36a | 2015-12-09 04:50:17 | [diff] [blame] | 538 | void TestRequestPermissionInvalidUrl( |
lshang | 88ec36a | 2015-12-09 04:50:17 | [diff] [blame] | 539 | ContentSettingsType content_settings_type) { |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 540 | base::HistogramTester histograms; |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 541 | TestPermissionContext permission_context(profile(), content_settings_type); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 542 | GURL url; |
| 543 | ASSERT_FALSE(url.is_valid()); |
clamy | 05ebb4e | 2017-08-17 18:59:05 | [diff] [blame] | 544 | controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 545 | std::string()); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 546 | |
| 547 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 548 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 549 | web_contents()->GetMainFrame()->GetRoutingID(), -1); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 550 | permission_context.RequestPermission( |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 551 | web_contents(), id, url, true /* user_gesture */, |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 552 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 553 | base::Unretained(&permission_context))); |
| 554 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 555 | ASSERT_EQ(1u, permission_context.decisions().size()); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 556 | EXPECT_EQ(CONTENT_SETTING_BLOCK, permission_context.decisions()[0]); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 557 | EXPECT_TRUE(permission_context.tab_context_updated()); |
johnme | c41dfee | 2016-01-13 14:35:16 | [diff] [blame] | 558 | EXPECT_EQ(CONTENT_SETTING_ASK, |
| 559 | permission_context.GetContentSettingFromMap(url, url)); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 560 | histograms.ExpectTotalCount( |
| 561 | "Permissions.AutoBlocker.EmbargoPromptSuppression", 0); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 562 | } |
| 563 | |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 564 | void TestGrantAndRevoke_TestContent(ContentSettingsType content_settings_type, |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 565 | ContentSetting expected_default) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 566 | TestPermissionContext permission_context(profile(), content_settings_type); |
toyoshim | 9eb573f4 | 2015-03-30 10:39:39 | [diff] [blame] | 567 | GURL url("https://www.google.com"); |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 568 | SetUpUrl(url); |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 569 | |
| 570 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 571 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 572 | web_contents()->GetMainFrame()->GetRoutingID(), -1); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 573 | permission_context.SetRespondPermissionCallback( |
| 574 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 575 | base::Unretained(this), &permission_context, id, url, true, |
| 576 | CONTENT_SETTING_ALLOW)); |
| 577 | |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 578 | permission_context.RequestPermission( |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 579 | web_contents(), id, url, true /* user_gesture */, |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 580 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 581 | base::Unretained(&permission_context))); |
| 582 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 583 | ASSERT_EQ(1u, permission_context.decisions().size()); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 584 | EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 585 | EXPECT_TRUE(permission_context.tab_context_updated()); |
johnme | c41dfee | 2016-01-13 14:35:16 | [diff] [blame] | 586 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 587 | permission_context.GetContentSettingFromMap(url, url)); |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 588 | |
| 589 | // Try to reset permission. |
| 590 | permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
| 591 | ContentSetting setting_after_reset = |
johnme | c41dfee | 2016-01-13 14:35:16 | [diff] [blame] | 592 | permission_context.GetContentSettingFromMap(url, url); |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 593 | ContentSetting default_setting = |
peconn | 5100d43 | 2015-09-16 12:03:08 | [diff] [blame] | 594 | HostContentSettingsMapFactory::GetForProfile(profile()) |
lshang | 88ec36a | 2015-12-09 04:50:17 | [diff] [blame] | 595 | ->GetDefaultContentSetting(content_settings_type, nullptr); |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 596 | EXPECT_EQ(default_setting, setting_after_reset); |
| 597 | } |
| 598 | |
lshang | 88ec36a | 2015-12-09 04:50:17 | [diff] [blame] | 599 | void TestGlobalPermissionsKillSwitch( |
lshang | 88ec36a | 2015-12-09 04:50:17 | [diff] [blame] | 600 | ContentSettingsType content_settings_type) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 601 | TestKillSwitchPermissionContext permission_context(profile(), |
| 602 | content_settings_type); |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 603 | permission_context.ResetFieldTrialList(); |
| 604 | |
| 605 | EXPECT_FALSE(permission_context.IsPermissionKillSwitchOn()); |
| 606 | std::map<std::string, std::string> params; |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 607 | params[PermissionUtil::GetPermissionString(content_settings_type)] = |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 608 | kPermissionsKillSwitchBlockedValue; |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 609 | variations::AssociateVariationParams(kPermissionsKillSwitchFieldStudy, |
| 610 | kPermissionsKillSwitchTestGroup, |
| 611 | params); |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 612 | base::FieldTrialList::CreateFieldTrial(kPermissionsKillSwitchFieldStudy, |
| 613 | kPermissionsKillSwitchTestGroup); |
| 614 | EXPECT_TRUE(permission_context.IsPermissionKillSwitchOn()); |
| 615 | } |
| 616 | |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 617 | // Don't call this more than once in the same test, as it persists data to |
| 618 | // HostContentSettingsMap. |
| 619 | void TestParallelRequests(ContentSetting response) { |
| 620 | TestPermissionContext permission_context( |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 621 | profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 622 | GURL url("http://www.google.com"); |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 623 | SetUpUrl(url); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 624 | |
| 625 | const PermissionRequestID id0( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 626 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 627 | web_contents()->GetMainFrame()->GetRoutingID(), 0); |
| 628 | const PermissionRequestID id1( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 629 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 630 | web_contents()->GetMainFrame()->GetRoutingID(), 1); |
| 631 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 632 | bool persist = (response == CONTENT_SETTING_ALLOW || |
| 633 | response == CONTENT_SETTING_BLOCK); |
| 634 | |
| 635 | // Request a permission without setting the callback to DecidePermission. |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 636 | permission_context.RequestPermission( |
benwells | fd2b155 | 2016-07-05 04:26:53 | [diff] [blame] | 637 | web_contents(), id0, url, true /* user_gesture */, |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 638 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 639 | base::Unretained(&permission_context))); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 640 | |
| 641 | EXPECT_EQ(0u, permission_context.decisions().size()); |
| 642 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 643 | // Set the callback, and make a second permission request. |
| 644 | permission_context.SetRespondPermissionCallback( |
| 645 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 646 | base::Unretained(this), &permission_context, id0, url, |
| 647 | persist, response)); |
| 648 | permission_context.RequestPermission( |
| 649 | web_contents(), id1, url, true /* user_gesture */, |
| 650 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 651 | base::Unretained(&permission_context))); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 652 | |
thestig | 9bdf7f2 | 2016-09-28 00:56:20 | [diff] [blame] | 653 | ASSERT_EQ(2u, permission_context.decisions().size()); |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 654 | EXPECT_EQ(response, permission_context.decisions()[0]); |
| 655 | EXPECT_EQ(response, permission_context.decisions()[1]); |
| 656 | EXPECT_TRUE(permission_context.tab_context_updated()); |
| 657 | |
| 658 | EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
| 659 | } |
| 660 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 661 | void TestPermissionsBlacklisting( |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 662 | ContentSettingsType content_settings_type, |
| 663 | scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 664 | const GURL& url, |
| 665 | int timeout, |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 666 | ContentSetting expected_permission_status, |
| 667 | PermissionEmbargoStatus expected_embargo_reason) { |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 668 | SetUpUrl(url); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 669 | base::HistogramTester histograms; |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 670 | base::test::ScopedFeatureList scoped_feature_list; |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 671 | scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 672 | TestPermissionContext permission_context(profile(), content_settings_type); |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 673 | PermissionDecisionAutoBlocker::GetForProfile(profile()) |
| 674 | ->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 675 | timeout); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 676 | const PermissionRequestID id( |
Lukasz Anforowicz | 1452b89 | 2017-09-28 20:10:09 | [diff] [blame] | 677 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 678 | web_contents()->GetMainFrame()->GetRoutingID(), -1); |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 679 | |
| 680 | // A response only needs to be made to the permission request if we do not |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 681 | // expect the permission to be blacklisted. |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 682 | if (expected_permission_status == CONTENT_SETTING_ALLOW) { |
| 683 | permission_context.SetRespondPermissionCallback( |
| 684 | base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 685 | base::Unretained(this), &permission_context, id, url, |
| 686 | true /* persist */, expected_permission_status)); |
| 687 | } |
| 688 | |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 689 | permission_context.RequestPermission( |
| 690 | web_contents(), id, url, true /* user_gesture */, |
| 691 | base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 692 | base::Unretained(&permission_context))); |
raymes | f6104d49 | 2017-03-09 01:20:18 | [diff] [blame] | 693 | PermissionResult result = permission_context.GetPermissionStatus( |
| 694 | nullptr /* render_frame_host */, url, url); |
raymes | ab35971 | 2017-02-15 06:23:25 | [diff] [blame] | 695 | EXPECT_EQ(expected_permission_status, result.content_setting); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 696 | |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 697 | if (expected_permission_status == CONTENT_SETTING_ALLOW) { |
| 698 | ASSERT_EQ(1u, permission_context.decisions().size()); |
| 699 | EXPECT_EQ(expected_permission_status, permission_context.decisions()[0]); |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 700 | EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 701 | } else { |
| 702 | EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 703 | } |
dominickn | 2391315 | 2017-02-23 12:04:02 | [diff] [blame] | 704 | histograms.ExpectUniqueSample( |
| 705 | "Permissions.AutoBlocker.EmbargoPromptSuppression", |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 706 | static_cast<int>(expected_embargo_reason), 1); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 707 | histograms.ExpectUniqueSample("Permissions.AutoBlocker.EmbargoStatus", |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 708 | static_cast<int>(expected_embargo_reason), 1); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 709 | } |
| 710 | |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 711 | void SetUpUrl(const GURL& url) { |
| 712 | NavigateAndCommit(url); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 713 | prompt_factory_->DocumentOnLoadCompletedInMainFrame(); |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 714 | } |
| 715 | |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 716 | private: |
| 717 | // ChromeRenderViewHostTestHarness: |
dcheng | 17131836 | 2014-12-29 18:31:25 | [diff] [blame] | 718 | void SetUp() override { |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 719 | ChromeRenderViewHostTestHarness::SetUp(); |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 720 | PermissionRequestManager::CreateForWebContents(web_contents()); |
| 721 | PermissionRequestManager* manager = |
| 722 | PermissionRequestManager::FromWebContents(web_contents()); |
| 723 | prompt_factory_.reset(new MockPermissionPromptFactory(manager)); |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 724 | } |
| 725 | |
timloh | 86d8eaf | 2017-05-09 03:43:09 | [diff] [blame] | 726 | void TearDown() override { |
| 727 | prompt_factory_.reset(); |
| 728 | ChromeRenderViewHostTestHarness::TearDown(); |
| 729 | } |
| 730 | |
| 731 | std::unique_ptr<MockPermissionPromptFactory> prompt_factory_; |
| 732 | |
felt | 88bf98cb | 2014-12-16 03:50:23 | [diff] [blame] | 733 | DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); |
| 734 | }; |
| 735 | |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 736 | // Simulates clicking Accept. The permission should be granted and |
| 737 | // saved for future use. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 738 | TEST_F(PermissionContextBaseTests, TestAskAndGrantPersist) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 739 | TestAskAndDecide_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 740 | CONTENT_SETTING_ALLOW, true); |
| 741 | } |
| 742 | |
| 743 | // Simulates clicking Accept. The permission should be granted, but not |
| 744 | // persisted. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 745 | TEST_F(PermissionContextBaseTests, TestAskAndGrantNoPersist) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 746 | TestAskAndDecide_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 747 | CONTENT_SETTING_ALLOW, false); |
| 748 | } |
| 749 | |
| 750 | // Simulates clicking Block. The permission should be denied and |
| 751 | // saved for future use. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 752 | TEST_F(PermissionContextBaseTests, TestAskAndBlockPersist) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 753 | TestAskAndDecide_TestContent(CONTENT_SETTINGS_TYPE_GEOLOCATION, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 754 | CONTENT_SETTING_BLOCK, true); |
| 755 | } |
| 756 | |
| 757 | // Simulates clicking Block. The permission should be denied, but not persisted. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 758 | TEST_F(PermissionContextBaseTests, TestAskAndBlockNoPersist) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 759 | TestAskAndDecide_TestContent(CONTENT_SETTINGS_TYPE_GEOLOCATION, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 760 | CONTENT_SETTING_BLOCK, false); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 761 | } |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 762 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 763 | // Simulates clicking Dismiss (X) in the prompt. |
[email protected] | 89a8dd5d | 2014-07-11 12:02:07 | [diff] [blame] | 764 | // The permission should be denied but not saved for future use. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 765 | TEST_F(PermissionContextBaseTests, TestAskAndDismiss) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 766 | TestAskAndDecide_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
dominickn | d4e446a | 2016-09-13 07:44:13 | [diff] [blame] | 767 | CONTENT_SETTING_ASK, false); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 768 | } |
| 769 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 770 | // Simulates clicking Dismiss (X) in the prompt with the block on too |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 771 | // many dismissals feature active. The permission should be blocked after |
| 772 | // several dismissals. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 773 | TEST_F(PermissionContextBaseTests, TestDismissUntilBlocked) { |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 774 | TestBlockOnSeveralDismissals_TestContent(); |
| 775 | } |
| 776 | |
| 777 | // Test setting a custom number of dismissals before block via variations. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 778 | TEST_F(PermissionContextBaseTests, TestDismissVariations) { |
dominickn | 6947d75 | 2016-08-10 02:00:06 | [diff] [blame] | 779 | TestVariationBlockOnSeveralDismissals_TestContent(); |
| 780 | } |
| 781 | |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 782 | // Simulates non-valid requesting URL. |
| 783 | // The permission should be denied but not saved for future use. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 784 | TEST_F(PermissionContextBaseTests, TestNonValidRequestingUrl) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 785 | TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 786 | TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 787 | TestRequestPermissionInvalidUrl(CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
timvolodine | a283055 | 2015-01-20 17:21:23 | [diff] [blame] | 788 | #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 789 | TestRequestPermissionInvalidUrl( |
| 790 | CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); |
| 791 | #endif |
| 792 | } |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 793 | |
timloh | 90e8cec | 2017-05-22 04:26:19 | [diff] [blame] | 794 | // Simulates granting and revoking of permissions. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 795 | TEST_F(PermissionContextBaseTests, TestGrantAndRevoke) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 796 | TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_GEOLOCATION, |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 797 | CONTENT_SETTING_ASK); |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 798 | TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 799 | CONTENT_SETTING_ASK); |
timloh | 90e8cec | 2017-05-22 04:26:19 | [diff] [blame] | 800 | #if defined(OS_ANDROID) |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 801 | TestGrantAndRevoke_TestContent( |
| 802 | CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, CONTENT_SETTING_ASK); |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 803 | // TODO(timvolodine): currently no test for |
| 804 | // CONTENT_SETTINGS_TYPE_NOTIFICATIONS because notification permissions work |
| 805 | // differently with infobars as compared to bubbles (crbug.com/453784). |
timloh | 90e8cec | 2017-05-22 04:26:19 | [diff] [blame] | 806 | #else |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 807 | TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
timvolodine | 16be520 | 2015-02-02 17:44:54 | [diff] [blame] | 808 | CONTENT_SETTING_ASK); |
felt | cb9e736 | 2015-06-25 00:36:43 | [diff] [blame] | 809 | #endif |
timloh | 90e8cec | 2017-05-22 04:26:19 | [diff] [blame] | 810 | } |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 811 | |
| 812 | // Tests the global kill switch by enabling/disabling the Field Trials. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 813 | TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 814 | TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 815 | TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 816 | TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 817 | TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 818 | #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 819 | TestGlobalPermissionsKillSwitch( |
| 820 | CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); |
| 821 | #endif |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 822 | TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); |
| 823 | TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); |
kcarattini | 2ee48ad5 | 2015-10-26 23:45:31 | [diff] [blame] | 824 | } |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 825 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 826 | TEST_F(PermissionContextBaseTests, TestParallelRequestsAllowed) { |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 827 | TestParallelRequests(CONTENT_SETTING_ALLOW); |
| 828 | } |
| 829 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 830 | TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 831 | TestParallelRequests(CONTENT_SETTING_BLOCK); |
| 832 | } |
| 833 | |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 834 | TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
johnme | 7fa91f7 | 2016-01-29 22:13:44 | [diff] [blame] | 835 | TestParallelRequests(CONTENT_SETTING_ASK); |
| 836 | } |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 837 | |
| 838 | // Tests a blacklisted (URL, permission) pair has had its permission request |
| 839 | // blocked. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 840 | TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 841 | scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 842 | new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 843 | const GURL url("https://www.example.com"); |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 844 | std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 845 | db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 846 | TestPermissionsBlacklisting( |
dominickn | 2e27dea | 2017-02-23 23:00:25 | [diff] [blame] | 847 | CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, url, 2000 /* timeout */, |
| 848 | CONTENT_SETTING_BLOCK, PermissionEmbargoStatus::PERMISSIONS_BLACKLISTING); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 849 | } |
| 850 | |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 851 | // Tests that a URL that is blacklisted for one permission can still request |
| 852 | // another and grant another. |
Timothy Loh | 246355d | 2017-08-18 05:10:30 | [diff] [blame] | 853 | TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 854 | scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 855 | new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 856 | const GURL url("https://www.example.com"); |
meredithl | 03b1285 | 2017-01-25 05:08:01 | [diff] [blame] | 857 | std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 858 | db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
timloh | 9a180ad | 2017-02-20 07:15:23 | [diff] [blame] | 859 | TestPermissionsBlacklisting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, |
dominickn | 79b96cc | 2017-02-14 04:14:21 | [diff] [blame] | 860 | url, 2000 /* timeout */, CONTENT_SETTING_ALLOW, |
| 861 | PermissionEmbargoStatus::NOT_EMBARGOED); |
meredithl | 62b8c3d | 2017-01-10 05:47:53 | [diff] [blame] | 862 | } |