Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / content_settings / permission_context_uma_util.cc
blobdd4c827669b642856dd3e9831ce70ec935b9651b
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.
5 #include "base/metrics/histogram_macros.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/content_settings/permission_context_uma_util.h"
9 #include "components/rappor/rappor_utils.h"
10 #include "content/public/browser/permission_type.h"
11 #include "url/gurl.h"
13 // UMA keys need to be statically initialized so plain function would not
14 // work. Use a Macro instead.
15 #define PERMISSION_ACTION_UMA(secure_origin, permission, permission_secure, \
16 permission_insecure, action) \
17 UMA_HISTOGRAM_ENUMERATION(permission, action, PERMISSION_ACTION_NUM); \
18 if (secure_origin) { \
19 UMA_HISTOGRAM_ENUMERATION(permission_secure, action, \
20 PERMISSION_ACTION_NUM); \
21 } else { \
22 UMA_HISTOGRAM_ENUMERATION(permission_insecure, action, \
23 PERMISSION_ACTION_NUM); \
26 namespace {
28 // Enum for UMA purposes, make sure you update histograms.xml if you add new
29 // permission actions. Never delete or reorder an entry; only add new entries
30 // immediately before PERMISSION_NUM
31 enum PermissionAction {
32 GRANTED = 0,
33 DENIED = 1,
34 DISMISSED = 2,
35 IGNORED = 3,
37 // Always keep this at the end.
38 PERMISSION_ACTION_NUM,
41 const std::string GetRapporMetric(ContentSettingsType permission,
42 PermissionAction action) {
43 std::string action_str;
44 switch (action) {
45 case GRANTED:
46 action_str = "Granted";
47 break;
48 case DENIED:
49 action_str = "Denied";
50 break;
51 case DISMISSED:
52 action_str = "Dismissed";
53 break;
54 case IGNORED:
55 action_str = "Ignored";
56 break;
57 case PERMISSION_ACTION_NUM:
58 NOTREACHED();
59 break;
61 std::string permission_str;
62 switch (permission) {
63 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
64 permission_str = "Geolocation";
65 break;
66 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
67 permission_str = "Notifications";
68 break;
69 default:
70 permission_str = "";
71 break;
74 if (permission_str.empty())
75 return "";
76 return base::StringPrintf("ContentSettings.PermissionActions_%s.%s.Url",
77 permission_str.c_str(), action_str.c_str());
80 void RecordPermissionAction(ContentSettingsType permission,
81 PermissionAction action,
82 const GURL& requesting_origin) {
83 bool secure_origin = requesting_origin.SchemeIsSecure();
85 switch (permission) {
86 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
87 PERMISSION_ACTION_UMA(
88 secure_origin,
89 "ContentSettings.PermissionActions_Geolocation",
90 "ContentSettings.PermissionActionsSecureOrigin_Geolocation",
91 "ContentSettings.PermissionActionsInsecureOrigin_Geolocation",
92 action);
93 break;
94 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
95 PERMISSION_ACTION_UMA(
96 secure_origin,
97 "ContentSettings.PermissionActions_Notifications",
98 "ContentSettings.PermissionActionsSecureOrigin_Notifications",
99 "ContentSettings.PermissionActionsInsecureOrigin_Notifications",
100 action);
101 break;
102 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
103 PERMISSION_ACTION_UMA(
104 secure_origin,
105 "ContentSettings.PermissionActions_MidiSysEx",
106 "ContentSettings.PermissionActionsSecureOrigin_MidiSysEx",
107 "ContentSettings.PermissionActionsInsecureOrigin_MidiSysEx",
108 action);
109 break;
110 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
111 PERMISSION_ACTION_UMA(
112 secure_origin,
113 "ContentSettings.PermissionActions_PushMessaging",
114 "ContentSettings.PermissionActionsSecureOrigin_PushMessaging",
115 "ContentSettings.PermissionActionsInsecureOrigin_PushMessaging",
116 action);
117 break;
118 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
119 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
120 PERMISSION_ACTION_UMA(
121 secure_origin,
122 "ContentSettings.PermissionActions_ProtectedMedia",
123 "ContentSettings.PermissionActionsSecureOrigin_ProtectedMedia",
124 "ContentSettings.PermissionActionsInsecureOrigin_ProtectedMedia",
125 action);
126 break;
127 #endif
128 default:
129 NOTREACHED() << "PERMISSION " << permission << " not accounted for";
132 const std::string& rappor_metric = GetRapporMetric(permission, action);
133 if (!rappor_metric.empty())
134 rappor::SampleDomainAndRegistryFromGURL(
135 g_browser_process->rappor_service(), rappor_metric,
136 requesting_origin);
139 void RecordPermissionRequest(ContentSettingsType permission,
140 const GURL& requesting_origin) {
141 bool secure_origin = requesting_origin.SchemeIsSecure();
142 content::PermissionType type;
143 switch (permission) {
144 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
145 type = content::PermissionType::GEOLOCATION;
146 rappor::SampleDomainAndRegistryFromGURL(
147 g_browser_process->rappor_service(),
148 "ContentSettings.PermissionRequested.Geolocation.Url",
149 requesting_origin);
150 break;
151 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
152 type = content::PermissionType::NOTIFICATIONS;
153 rappor::SampleDomainAndRegistryFromGURL(
154 g_browser_process->rappor_service(),
155 "ContentSettings.PermissionRequested.Notifications.Url",
156 requesting_origin);
157 break;
158 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
159 type = content::PermissionType::MIDI_SYSEX;
160 break;
161 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
162 type = content::PermissionType::PUSH_MESSAGING;
163 break;
164 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
165 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
166 type = content::PermissionType::PROTECTED_MEDIA_IDENTIFIER;
167 break;
168 #endif
169 default:
170 NOTREACHED() << "PERMISSION " << permission << " not accounted for";
171 return;
173 UMA_HISTOGRAM_ENUMERATION(
174 "ContentSettings.PermissionRequested",
175 static_cast<base::HistogramBase::Sample>(type),
176 static_cast<base::HistogramBase::Sample>(content::PermissionType::NUM));
177 if (secure_origin) {
178 UMA_HISTOGRAM_ENUMERATION(
179 "ContentSettings.PermissionRequested_SecureOrigin",
180 static_cast<base::HistogramBase::Sample>(type),
181 static_cast<base::HistogramBase::Sample>(content::PermissionType::NUM));
182 } else {
183 UMA_HISTOGRAM_ENUMERATION(
184 "ContentSettings.PermissionRequested_InsecureOrigin",
185 static_cast<base::HistogramBase::Sample>(type),
186 static_cast<base::HistogramBase::Sample>(content::PermissionType::NUM));
190 } // namespace
192 // Make sure you update histograms.xml permission histogram_suffix if you
193 // add new permission
194 void PermissionContextUmaUtil::PermissionRequested(
195 ContentSettingsType permission, const GURL& requesting_origin) {
196 RecordPermissionRequest(permission, requesting_origin);
199 void PermissionContextUmaUtil::PermissionGranted(
200 ContentSettingsType permission, const GURL& requesting_origin) {
201 RecordPermissionAction(permission, GRANTED, requesting_origin);
204 void PermissionContextUmaUtil::PermissionDenied(
205 ContentSettingsType permission, const GURL& requesting_origin) {
206 RecordPermissionAction(permission, DENIED, requesting_origin);
209 void PermissionContextUmaUtil::PermissionDismissed(
210 ContentSettingsType permission, const GURL& requesting_origin) {
211 RecordPermissionAction(permission, DISMISSED, requesting_origin);
214 void PermissionContextUmaUtil::PermissionIgnored(
215 ContentSettingsType permission, const GURL& requesting_origin) {
216 RecordPermissionAction(permission, IGNORED, requesting_origin);