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