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 "content/public/common/origin_util.h"
14 // UMA keys need to be statically initialized so plain function would not
15 // work. Use a Macro instead.
16 #define PERMISSION_ACTION_UMA(secure_origin, permission, permission_secure, \
17 permission_insecure, action) \
18 UMA_HISTOGRAM_ENUMERATION(permission, action, PERMISSION_ACTION_NUM); \
19 if (secure_origin) { \
20 UMA_HISTOGRAM_ENUMERATION(permission_secure, action, \
21 PERMISSION_ACTION_NUM); \
23 UMA_HISTOGRAM_ENUMERATION(permission_insecure, action, \
24 PERMISSION_ACTION_NUM); \
27 using content::PermissionType
;
31 // Enum for UMA purposes, make sure you update histograms.xml if you add new
32 // permission actions. Never delete or reorder an entry; only add new entries
33 // immediately before PERMISSION_NUM
34 enum PermissionAction
{
40 // Always keep this at the end.
41 PERMISSION_ACTION_NUM
,
44 const std::string
GetRapporMetric(ContentSettingsType permission
,
45 PermissionAction action
) {
46 std::string action_str
;
49 action_str
= "Granted";
52 action_str
= "Denied";
55 action_str
= "Dismissed";
58 action_str
= "Ignored";
60 case PERMISSION_ACTION_NUM
:
64 std::string permission_str
;
66 case CONTENT_SETTINGS_TYPE_GEOLOCATION
:
67 permission_str
= "Geolocation";
69 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS
:
70 permission_str
= "Notifications";
77 if (permission_str
.empty())
79 return base::StringPrintf("ContentSettings.PermissionActions_%s.%s.Url",
80 permission_str
.c_str(), action_str
.c_str());
83 void RecordPermissionAction(ContentSettingsType permission
,
84 PermissionAction action
,
85 const GURL
& requesting_origin
) {
86 bool secure_origin
= content::IsOriginSecure(requesting_origin
);
89 case CONTENT_SETTINGS_TYPE_GEOLOCATION
:
90 PERMISSION_ACTION_UMA(
92 "ContentSettings.PermissionActions_Geolocation",
93 "ContentSettings.PermissionActionsSecureOrigin_Geolocation",
94 "ContentSettings.PermissionActionsInsecureOrigin_Geolocation",
97 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS
:
98 PERMISSION_ACTION_UMA(
100 "ContentSettings.PermissionActions_Notifications",
101 "ContentSettings.PermissionActionsSecureOrigin_Notifications",
102 "ContentSettings.PermissionActionsInsecureOrigin_Notifications",
105 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX
:
106 PERMISSION_ACTION_UMA(
108 "ContentSettings.PermissionActions_MidiSysEx",
109 "ContentSettings.PermissionActionsSecureOrigin_MidiSysEx",
110 "ContentSettings.PermissionActionsInsecureOrigin_MidiSysEx",
113 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
:
114 PERMISSION_ACTION_UMA(
116 "ContentSettings.PermissionActions_PushMessaging",
117 "ContentSettings.PermissionActionsSecureOrigin_PushMessaging",
118 "ContentSettings.PermissionActionsInsecureOrigin_PushMessaging",
121 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
122 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
:
123 PERMISSION_ACTION_UMA(
125 "ContentSettings.PermissionActions_ProtectedMedia",
126 "ContentSettings.PermissionActionsSecureOrigin_ProtectedMedia",
127 "ContentSettings.PermissionActionsInsecureOrigin_ProtectedMedia",
132 NOTREACHED() << "PERMISSION " << permission
<< " not accounted for";
135 const std::string
& rappor_metric
= GetRapporMetric(permission
, action
);
136 if (!rappor_metric
.empty())
137 rappor::SampleDomainAndRegistryFromGURL(
138 g_browser_process
->rappor_service(), rappor_metric
,
142 void RecordPermissionRequest(ContentSettingsType permission
,
143 const GURL
& requesting_origin
) {
144 bool secure_origin
= content::IsOriginSecure(requesting_origin
);
146 switch (permission
) {
147 case CONTENT_SETTINGS_TYPE_GEOLOCATION
:
148 type
= PermissionType::GEOLOCATION
;
149 rappor::SampleDomainAndRegistryFromGURL(
150 g_browser_process
->rappor_service(),
151 "ContentSettings.PermissionRequested.Geolocation.Url",
154 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS
:
155 type
= PermissionType::NOTIFICATIONS
;
156 rappor::SampleDomainAndRegistryFromGURL(
157 g_browser_process
->rappor_service(),
158 "ContentSettings.PermissionRequested.Notifications.Url",
161 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX
:
162 type
= PermissionType::MIDI_SYSEX
;
164 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
:
165 type
= PermissionType::PUSH_MESSAGING
;
167 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
168 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
:
169 type
= PermissionType::PROTECTED_MEDIA_IDENTIFIER
;
173 NOTREACHED() << "PERMISSION " << permission
<< " not accounted for";
176 UMA_HISTOGRAM_ENUMERATION(
177 "ContentSettings.PermissionRequested",
178 static_cast<base::HistogramBase::Sample
>(type
),
179 static_cast<base::HistogramBase::Sample
>(PermissionType::NUM
));
181 UMA_HISTOGRAM_ENUMERATION(
182 "ContentSettings.PermissionRequested_SecureOrigin",
183 static_cast<base::HistogramBase::Sample
>(type
),
184 static_cast<base::HistogramBase::Sample
>(PermissionType::NUM
));
186 UMA_HISTOGRAM_ENUMERATION(
187 "ContentSettings.PermissionRequested_InsecureOrigin",
188 static_cast<base::HistogramBase::Sample
>(type
),
189 static_cast<base::HistogramBase::Sample
>(PermissionType::NUM
));
195 // Make sure you update histograms.xml permission histogram_suffix if you
196 // add new permission
197 void PermissionContextUmaUtil::PermissionRequested(
198 ContentSettingsType permission
, const GURL
& requesting_origin
) {
199 RecordPermissionRequest(permission
, requesting_origin
);
202 void PermissionContextUmaUtil::PermissionGranted(
203 ContentSettingsType permission
, const GURL
& requesting_origin
) {
204 RecordPermissionAction(permission
, GRANTED
, requesting_origin
);
207 void PermissionContextUmaUtil::PermissionDenied(
208 ContentSettingsType permission
, const GURL
& requesting_origin
) {
209 RecordPermissionAction(permission
, DENIED
, requesting_origin
);
212 void PermissionContextUmaUtil::PermissionDismissed(
213 ContentSettingsType permission
, const GURL
& requesting_origin
) {
214 RecordPermissionAction(permission
, DISMISSED
, requesting_origin
);
217 void PermissionContextUmaUtil::PermissionIgnored(
218 ContentSettingsType permission
, const GURL
& requesting_origin
) {
219 RecordPermissionAction(permission
, IGNORED
, requesting_origin
);