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