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