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

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

Issue 2184823007: Add a feature which, when enabled, blocks permissions after X prompt dismissals. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/permissions/permission_context_base.h" 5 #include "chrome/browser/permissions/permission_context_base.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/feature_list.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
13 #include "base/test/mock_entropy_provider.h" 14 #include "base/test/mock_entropy_provider.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
16 #include "chrome/browser/infobars/infobar_service.h" 17 #include "chrome/browser/infobars/infobar_service.h"
17 #include "chrome/browser/permissions/permission_queue_controller.h" 18 #include "chrome/browser/permissions/permission_queue_controller.h"
18 #include "chrome/browser/permissions/permission_request_id.h" 19 #include "chrome/browser/permissions/permission_request_id.h"
19 #include "chrome/browser/permissions/permission_util.h" 20 #include "chrome/browser/permissions/permission_util.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 base::Unretained(&permission_context))); 183 base::Unretained(&permission_context)));
183 184
184 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK); 185 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK);
185 EXPECT_EQ(1u, permission_context.decisions().size()); 186 EXPECT_EQ(1u, permission_context.decisions().size());
186 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); 187 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]);
187 EXPECT_TRUE(permission_context.tab_context_updated()); 188 EXPECT_TRUE(permission_context.tab_context_updated());
188 EXPECT_EQ(CONTENT_SETTING_ASK, 189 EXPECT_EQ(CONTENT_SETTING_ASK,
189 permission_context.GetContentSettingFromMap(url, url)); 190 permission_context.GetContentSettingFromMap(url, url));
190 } 191 }
191 192
193 void TestBlockOnSeveralDismissals_TestContent() {
194 {
195 TestPermissionContext permission_context(
196 profile(), content::PermissionType::GEOLOCATION,
197 CONTENT_SETTINGS_TYPE_GEOLOCATION);
198 GURL url("https://www.google.com");
199 NavigateAndCommit(url);
200
201 // First, ensure that > 3 dismissals behaves correctly.
202 for (unsigned int i = 0; i < 4; ++i) {
203 const PermissionRequestID id(
204 web_contents()->GetRenderProcessHost()->GetID(),
205 web_contents()->GetMainFrame()->GetRoutingID(), i);
206 permission_context.RequestPermission(
207 web_contents(), id, url, true /* user_gesture */,
208 base::Bind(&TestPermissionContext::TrackPermissionDecision,
209 base::Unretained(&permission_context)));
210
211 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK);
212 EXPECT_EQ(i+1, permission_context.decisions().size());
213 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[i]);
214 EXPECT_TRUE(permission_context.tab_context_updated());
215 EXPECT_EQ(CONTENT_SETTING_ASK,
216 permission_context.GetContentSettingFromMap(url, url));
217 }
218 }
219
220 // Flush the dismissal counts.
221 HostContentSettingsMapFactory::GetForProfile(profile())
222 ->ClearSettingsForOneType(
223 CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT);
224
225 {
226 TestPermissionContext permission_context(
227 profile(), content::PermissionType::GEOLOCATION,
228 CONTENT_SETTINGS_TYPE_GEOLOCATION);
229 GURL url("https://www.google.com");
230 NavigateAndCommit(url);
231
232 // Enable the block on too many dismissals feature, which is disabled by
233 // default.
234 base::FeatureList::ClearInstanceForTesting();
235 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
236 feature_list->InitializeFromCommandLine(
237 "BlockPromptsIfDismissedOften<PermissionPromptsUX", "");
238 base::FeatureList::SetInstance(std::move(feature_list));
239
240 // Dismiss three times. The third dismiss should change the decision from
241 // dismiss to block, and hence change the persisted content setting.
242 for (unsigned int i = 0; i < 3; ++i) {
243 ContentSetting expected =
244 (i < 2) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK;
245 const PermissionRequestID id(
246 web_contents()->GetRenderProcessHost()->GetID(),
247 web_contents()->GetMainFrame()->GetRoutingID(), i);
248 permission_context.RequestPermission(
249 web_contents(), id, url, true /* user_gesture */,
250 base::Bind(&TestPermissionContext::TrackPermissionDecision,
251 base::Unretained(&permission_context)));
252
253 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK);
254 EXPECT_EQ(i+1, permission_context.decisions().size());
255 EXPECT_EQ(expected, permission_context.decisions()[i]);
256 EXPECT_TRUE(permission_context.tab_context_updated());
257 EXPECT_EQ(expected,
258 permission_context.GetContentSettingFromMap(url, url));
259 }
260 }
261 }
262
192 void TestRequestPermissionInvalidUrl( 263 void TestRequestPermissionInvalidUrl(
193 content::PermissionType permission_type, 264 content::PermissionType permission_type,
194 ContentSettingsType content_settings_type) { 265 ContentSettingsType content_settings_type) {
195 TestPermissionContext permission_context(profile(), permission_type, 266 TestPermissionContext permission_context(profile(), permission_type,
196 content_settings_type); 267 content_settings_type);
197 GURL url; 268 GURL url;
198 ASSERT_FALSE(url.is_valid()); 269 ASSERT_FALSE(url.is_valid());
199 NavigateAndCommit(url); 270 NavigateAndCommit(url);
200 271
201 const PermissionRequestID id( 272 const PermissionRequestID id(
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 TEST_F(PermissionContextBaseTests, TestAskAndGrant) { 396 TEST_F(PermissionContextBaseTests, TestAskAndGrant) {
326 TestAskAndGrant_TestContent(); 397 TestAskAndGrant_TestContent();
327 } 398 }
328 399
329 // Simulates clicking Dismiss (X) in the infobar/bubble. 400 // Simulates clicking Dismiss (X) in the infobar/bubble.
330 // The permission should be denied but not saved for future use. 401 // The permission should be denied but not saved for future use.
331 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) { 402 TEST_F(PermissionContextBaseTests, TestAskAndDismiss) {
332 TestAskAndDismiss_TestContent(); 403 TestAskAndDismiss_TestContent();
333 } 404 }
334 405
406 // Simulates clicking Dismiss (X) in the infobar/bubble with the block on too
407 // many dismissals feature active.
408 // The permission should be blocked after several dismissals.
409 TEST_F(PermissionContextBaseTests, TestDismissUntilBlocked) {
410 TestBlockOnSeveralDismissals_TestContent();
411 }
412
335 // Simulates non-valid requesting URL. 413 // Simulates non-valid requesting URL.
336 // The permission should be denied but not saved for future use. 414 // The permission should be denied but not saved for future use.
337 TEST_F(PermissionContextBaseTests, TestNonValidRequestingUrl) { 415 TEST_F(PermissionContextBaseTests, TestNonValidRequestingUrl) {
338 TestRequestPermissionInvalidUrl(content::PermissionType::GEOLOCATION, 416 TestRequestPermissionInvalidUrl(content::PermissionType::GEOLOCATION,
339 CONTENT_SETTINGS_TYPE_GEOLOCATION); 417 CONTENT_SETTINGS_TYPE_GEOLOCATION);
340 TestRequestPermissionInvalidUrl(content::PermissionType::NOTIFICATIONS, 418 TestRequestPermissionInvalidUrl(content::PermissionType::NOTIFICATIONS,
341 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 419 CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
342 TestRequestPermissionInvalidUrl(content::PermissionType::MIDI_SYSEX, 420 TestRequestPermissionInvalidUrl(content::PermissionType::MIDI_SYSEX,
343 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); 421 CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
344 TestRequestPermissionInvalidUrl(content::PermissionType::PUSH_MESSAGING, 422 TestRequestPermissionInvalidUrl(content::PermissionType::PUSH_MESSAGING,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 TestParallelRequests(CONTENT_SETTING_ALLOW); 497 TestParallelRequests(CONTENT_SETTING_ALLOW);
420 } 498 }
421 499
422 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { 500 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) {
423 TestParallelRequests(CONTENT_SETTING_BLOCK); 501 TestParallelRequests(CONTENT_SETTING_BLOCK);
424 } 502 }
425 503
426 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { 504 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) {
427 TestParallelRequests(CONTENT_SETTING_ASK); 505 TestParallelRequests(CONTENT_SETTING_ASK);
428 } 506 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698