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

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

Issue 2651163002: Add UMA for autoblocking and embargoing. (Closed)
Patch Set: Sync branch, add test Created 3 years, 10 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 <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/feature_list.h" 14 #include "base/feature_list.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/metrics/field_trial.h" 17 #include "base/metrics/field_trial.h"
18 #include "base/run_loop.h" 18 #include "base/run_loop.h"
19 #include "base/test/histogram_tester.h" 19 #include "base/test/histogram_tester.h"
20 #include "base/test/mock_entropy_provider.h" 20 #include "base/test/mock_entropy_provider.h"
21 #include "base/test/scoped_feature_list.h" 21 #include "base/test/scoped_feature_list.h"
22 #include "build/build_config.h" 22 #include "build/build_config.h"
23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
24 #include "chrome/browser/infobars/infobar_service.h" 24 #include "chrome/browser/infobars/infobar_service.h"
25 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" 25 #include "chrome/browser/permissions/permission_decision_auto_blocker.h"
26 #include "chrome/browser/permissions/permission_queue_controller.h" 26 #include "chrome/browser/permissions/permission_queue_controller.h"
27 #include "chrome/browser/permissions/permission_request_id.h" 27 #include "chrome/browser/permissions/permission_request_id.h"
28 #include "chrome/browser/permissions/permission_uma_util.h"
28 #include "chrome/browser/permissions/permission_util.h" 29 #include "chrome/browser/permissions/permission_util.h"
29 #include "chrome/common/chrome_features.h" 30 #include "chrome/common/chrome_features.h"
30 #include "chrome/common/chrome_switches.h" 31 #include "chrome/common/chrome_switches.h"
31 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 32 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
32 #include "chrome/test/base/testing_profile.h" 33 #include "chrome/test/base/testing_profile.h"
33 #include "components/content_settings/core/browser/host_content_settings_map.h" 34 #include "components/content_settings/core/browser/host_content_settings_map.h"
34 #include "components/content_settings/core/common/content_settings.h" 35 #include "components/content_settings/core/common/content_settings.h"
35 #include "components/content_settings/core/common/content_settings_types.h" 36 #include "components/content_settings/core/common/content_settings_types.h"
36 #include "components/safe_browsing_db/database_manager.h" 37 #include "components/safe_browsing_db/database_manager.h"
37 #include "components/safe_browsing_db/test_database_manager.h" 38 #include "components/safe_browsing_db/test_database_manager.h"
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 base::Unretained(this), &permission_context, id, url, 282 base::Unretained(this), &permission_context, id, url,
282 persist, decision)); 283 persist, decision));
283 permission_context.RequestPermission( 284 permission_context.RequestPermission(
284 web_contents(), 285 web_contents(),
285 id, url, true /* user_gesture */, 286 id, url, true /* user_gesture */,
286 base::Bind(&TestPermissionContext::TrackPermissionDecision, 287 base::Bind(&TestPermissionContext::TrackPermissionDecision,
287 base::Unretained(&permission_context))); 288 base::Unretained(&permission_context)));
288 ASSERT_EQ(1u, permission_context.decisions().size()); 289 ASSERT_EQ(1u, permission_context.decisions().size());
289 EXPECT_EQ(decision, permission_context.decisions()[0]); 290 EXPECT_EQ(decision, permission_context.decisions()[0]);
290 EXPECT_TRUE(permission_context.tab_context_updated()); 291 EXPECT_TRUE(permission_context.tab_context_updated());
291 292 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
293 PermissionEmbargoReason::NOT_EMBARGOED, 1);
292 std::string decision_string; 294 std::string decision_string;
293 if (decision == CONTENT_SETTING_ALLOW) 295 if (decision == CONTENT_SETTING_ALLOW)
294 decision_string = "Accepted"; 296 decision_string = "Accepted";
295 else if (decision == CONTENT_SETTING_BLOCK) 297 else if (decision == CONTENT_SETTING_BLOCK)
296 decision_string = "Denied"; 298 decision_string = "Denied";
297 else if (decision == CONTENT_SETTING_ASK) 299 else if (decision == CONTENT_SETTING_ASK)
298 decision_string = "Dismissed"; 300 decision_string = "Dismissed";
299 301
300 if (decision_string.size()) { 302 if (decision_string.size()) {
301 histograms.ExpectUniqueSample( 303 histograms.ExpectUniqueSample(
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 base::Bind(&TestPermissionContext::TrackPermissionDecision, 347 base::Bind(&TestPermissionContext::TrackPermissionDecision,
346 base::Unretained(&permission_context))); 348 base::Unretained(&permission_context)));
347 histograms.ExpectTotalCount( 349 histograms.ExpectTotalCount(
348 "Permissions.Prompt.Dismissed.PriorDismissCount." + 350 "Permissions.Prompt.Dismissed.PriorDismissCount." +
349 PermissionUtil::GetPermissionString(permission_type), 351 PermissionUtil::GetPermissionString(permission_type),
350 i + 1); 352 i + 1);
351 histograms.ExpectBucketCount( 353 histograms.ExpectBucketCount(
352 "Permissions.Prompt.Dismissed.PriorDismissCount." + 354 "Permissions.Prompt.Dismissed.PriorDismissCount." +
353 PermissionUtil::GetPermissionString(permission_type), 355 PermissionUtil::GetPermissionString(permission_type),
354 i, 1); 356 i, 1);
357 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
358 PermissionEmbargoReason::NOT_EMBARGOED,
359 i + 1);
355 ASSERT_EQ(1u, permission_context.decisions().size()); 360 ASSERT_EQ(1u, permission_context.decisions().size());
356 EXPECT_EQ(expected, permission_context.decisions()[0]); 361 EXPECT_EQ(expected, permission_context.decisions()[0]);
357 EXPECT_TRUE(permission_context.tab_context_updated()); 362 EXPECT_TRUE(permission_context.tab_context_updated());
358 EXPECT_EQ(expected, permission_context.GetPermissionStatus(url, url)); 363 EXPECT_EQ(expected, permission_context.GetPermissionStatus(url, url));
359 } 364 }
360 365
361 TestPermissionContext permission_context(profile(), permission_type, 366 TestPermissionContext permission_context(profile(), permission_type,
362 content_settings_type); 367 content_settings_type);
363 const PermissionRequestID id( 368 const PermissionRequestID id(
364 web_contents()->GetRenderProcessHost()->GetID(), 369 web_contents()->GetRenderProcessHost()->GetID(),
365 web_contents()->GetMainFrame()->GetRoutingID(), -1); 370 web_contents()->GetMainFrame()->GetRoutingID(), -1);
366 371
367 permission_context.SetRespondPermissionCallback( 372 permission_context.SetRespondPermissionCallback(
368 base::Bind(&PermissionContextBaseTests::RespondToPermission, 373 base::Bind(&PermissionContextBaseTests::RespondToPermission,
369 base::Unretained(this), &permission_context, id, url, false, 374 base::Unretained(this), &permission_context, id, url, false,
370 CONTENT_SETTING_ASK)); 375 CONTENT_SETTING_ASK));
371 376
372 permission_context.RequestPermission( 377 permission_context.RequestPermission(
373 web_contents(), id, url, true /* user_gesture */, 378 web_contents(), id, url, true /* user_gesture */,
374 base::Bind(&TestPermissionContext::TrackPermissionDecision, 379 base::Bind(&TestPermissionContext::TrackPermissionDecision,
375 base::Unretained(&permission_context))); 380 base::Unretained(&permission_context)));
376 381
377 EXPECT_EQ(CONTENT_SETTING_BLOCK, 382 EXPECT_EQ(CONTENT_SETTING_BLOCK,
378 permission_context.GetPermissionStatus(url, url)); 383 permission_context.GetPermissionStatus(url, url));
384 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
385 PermissionEmbargoReason::REPEATED_DISMISSALS,
386 1);
379 } 387 }
380 388
381 void TestBlockOnSeveralDismissals_TestContent() { 389 void TestBlockOnSeveralDismissals_TestContent() {
382 GURL url("https://www.google.com"); 390 GURL url("https://www.google.com");
383 NavigateAndCommit(url); 391 NavigateAndCommit(url);
384 base::HistogramTester histograms; 392 base::HistogramTester histograms;
385 393
386 // First, ensure that > 3 dismissals behaves correctly. 394 // First, ensure that > 3 dismissals behaves correctly.
387 for (uint32_t i = 0; i < 4; ++i) { 395 for (uint32_t i = 0; i < 4; ++i) {
388 TestPermissionContext permission_context( 396 TestPermissionContext permission_context(
(...skipping 10 matching lines...) Expand all
399 false, CONTENT_SETTING_ASK)); 407 false, CONTENT_SETTING_ASK));
400 permission_context.RequestPermission( 408 permission_context.RequestPermission(
401 web_contents(), id, url, true /* user_gesture */, 409 web_contents(), id, url, true /* user_gesture */,
402 base::Bind(&TestPermissionContext::TrackPermissionDecision, 410 base::Bind(&TestPermissionContext::TrackPermissionDecision,
403 base::Unretained(&permission_context))); 411 base::Unretained(&permission_context)));
404 histograms.ExpectTotalCount( 412 histograms.ExpectTotalCount(
405 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", 413 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation",
406 i + 1); 414 i + 1);
407 histograms.ExpectBucketCount( 415 histograms.ExpectBucketCount(
408 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); 416 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1);
417 histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason",
418 PermissionEmbargoReason::NOT_EMBARGOED,
419 i + 1);
409 ASSERT_EQ(1u, permission_context.decisions().size()); 420 ASSERT_EQ(1u, permission_context.decisions().size());
410 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); 421 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]);
411 EXPECT_TRUE(permission_context.tab_context_updated()); 422 EXPECT_TRUE(permission_context.tab_context_updated());
412 EXPECT_EQ(CONTENT_SETTING_ASK, 423 EXPECT_EQ(CONTENT_SETTING_ASK,
413 permission_context.GetContentSettingFromMap(url, url)); 424 permission_context.GetContentSettingFromMap(url, url));
414 } 425 }
415 426
416 // Flush the dismissal counts. Enable the block on too many dismissals 427 // Flush the dismissal counts. Enable the block on too many dismissals
417 // feature, which is disabled by default. 428 // feature, which is disabled by default.
418 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); 429 auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 } 845 }
835 846
836 // Tests a blacklisted (URL, permission) pair has had its permission request 847 // Tests a blacklisted (URL, permission) pair has had its permission request
837 // blocked. 848 // blocked.
838 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { 849 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) {
839 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 850 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
840 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); 851 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
841 const GURL url("https://www.example.com"); 852 const GURL url("https://www.example.com");
842 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 853 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
843 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 854 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
855 base::HistogramTester histograms;
844 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, 856 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION,
845 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, 857 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager,
846 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); 858 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK);
859 histograms.ExpectUniqueSample(
860 "Permissions.AutoBlocker.EmbargoReason",
861 PermissionEmbargoReason::PERMISSIONS_BLACKLISTING, 1);
847 } 862 }
848 863
849 // Tests that a URL that is blacklisted for one permission can still request 864 // Tests that a URL that is blacklisted for one permission can still request
850 // another and grant another. 865 // another and grant another.
851 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { 866 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) {
852 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 867 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
853 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); 868 new MockSafeBrowsingDatabaseManager(true /* perform_callback */);
854 const GURL url("https://www.example.com"); 869 const GURL url("https://www.example.com");
855 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 870 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
856 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 871 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
872 base::HistogramTester histograms;
857 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, 873 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS,
858 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, 874 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager,
859 url, 2000 /* timeout */, CONTENT_SETTING_ALLOW); 875 url, 2000 /* timeout */, CONTENT_SETTING_ALLOW);
876 histograms.ExpectUniqueSample("Permissions.AutoBlocker.EmbargoReason",
877 PermissionEmbargoReason::NOT_EMBARGOED, 1);
860 } 878 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698