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"
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); \
22 UMA_HISTOGRAM_ENUMERATION(permission_insecure, action, \
23 PERMISSION_ACTION_NUM); \
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
{
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
;
46 action_str
= "Granted";
49 action_str
= "Denied";
52 action_str
= "Dismissed";
55 action_str
= "Ignored";
57 case PERMISSION_ACTION_NUM
:
61 std::string permission_str
;
63 case CONTENT_SETTINGS_TYPE_GEOLOCATION
:
64 permission_str
= "Geolocation";
66 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS
:
67 permission_str
= "Notifications";
74 if (permission_str
.empty())
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();
86 case CONTENT_SETTINGS_TYPE_GEOLOCATION
:
87 PERMISSION_ACTION_UMA(
89 "ContentSettings.PermissionActions_Geolocation",
90 "ContentSettings.PermissionActionsSecureOrigin_Geolocation",
91 "ContentSettings.PermissionActionsInsecureOrigin_Geolocation",
94 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS
:
95 PERMISSION_ACTION_UMA(
97 "ContentSettings.PermissionActions_Notifications",
98 "ContentSettings.PermissionActionsSecureOrigin_Notifications",
99 "ContentSettings.PermissionActionsInsecureOrigin_Notifications",
102 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX
:
103 PERMISSION_ACTION_UMA(
105 "ContentSettings.PermissionActions_MidiSysEx",
106 "ContentSettings.PermissionActionsSecureOrigin_MidiSysEx",
107 "ContentSettings.PermissionActionsInsecureOrigin_MidiSysEx",
110 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
:
111 PERMISSION_ACTION_UMA(
113 "ContentSettings.PermissionActions_PushMessaging",
114 "ContentSettings.PermissionActionsSecureOrigin_PushMessaging",
115 "ContentSettings.PermissionActionsInsecureOrigin_PushMessaging",
118 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
119 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
:
120 PERMISSION_ACTION_UMA(
122 "ContentSettings.PermissionActions_ProtectedMedia",
123 "ContentSettings.PermissionActionsSecureOrigin_ProtectedMedia",
124 "ContentSettings.PermissionActionsInsecureOrigin_ProtectedMedia",
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
,
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",
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",
158 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX
:
159 type
= content::PermissionType::MIDI_SYSEX
;
161 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
:
162 type
= content::PermissionType::PUSH_MESSAGING
;
164 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
165 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
:
166 type
= content::PermissionType::PROTECTED_MEDIA_IDENTIFIER
;
170 NOTREACHED() << "PERMISSION " << permission
<< " not accounted for";
173 UMA_HISTOGRAM_ENUMERATION(
174 "ContentSettings.PermissionRequested",
175 static_cast<base::HistogramBase::Sample
>(type
),
176 static_cast<base::HistogramBase::Sample
>(content::PermissionType::NUM
));
178 UMA_HISTOGRAM_ENUMERATION(
179 "ContentSettings.PermissionRequested_SecureOrigin",
180 static_cast<base::HistogramBase::Sample
>(type
),
181 static_cast<base::HistogramBase::Sample
>(content::PermissionType::NUM
));
183 UMA_HISTOGRAM_ENUMERATION(
184 "ContentSettings.PermissionRequested_InsecureOrigin",
185 static_cast<base::HistogramBase::Sample
>(type
),
186 static_cast<base::HistogramBase::Sample
>(content::PermissionType::NUM
));
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
);