Prepare for VS2015 toolchain
[chromium-blink-merge.git] / chrome / browser / content_settings / permission_context_uma_util.cc
blobcb2af3914f54879d57b0553bdedfc76ea0cd9d43
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"
12 #include "url/gurl.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); \
22 } else { \
23 UMA_HISTOGRAM_ENUMERATION(permission_insecure, action, \
24 PERMISSION_ACTION_NUM); \
27 using content::PermissionType;
29 namespace {
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 {
35 GRANTED = 0,
36 DENIED = 1,
37 DISMISSED = 2,
38 IGNORED = 3,
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;
47 switch (action) {
48 case GRANTED:
49 action_str = "Granted";
50 break;
51 case DENIED:
52 action_str = "Denied";
53 break;
54 case DISMISSED:
55 action_str = "Dismissed";
56 break;
57 case IGNORED:
58 action_str = "Ignored";
59 break;
60 case PERMISSION_ACTION_NUM:
61 NOTREACHED();
62 break;
64 std::string permission_str;
65 switch (permission) {
66 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
67 permission_str = "Geolocation";
68 break;
69 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
70 permission_str = "Notifications";
71 break;
72 default:
73 permission_str = "";
74 break;
77 if (permission_str.empty())
78 return "";
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);
88 switch (permission) {
89 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
90 PERMISSION_ACTION_UMA(
91 secure_origin,
92 "ContentSettings.PermissionActions_Geolocation",
93 "ContentSettings.PermissionActionsSecureOrigin_Geolocation",
94 "ContentSettings.PermissionActionsInsecureOrigin_Geolocation",
95 action);
96 break;
97 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
98 PERMISSION_ACTION_UMA(
99 secure_origin,
100 "ContentSettings.PermissionActions_Notifications",
101 "ContentSettings.PermissionActionsSecureOrigin_Notifications",
102 "ContentSettings.PermissionActionsInsecureOrigin_Notifications",
103 action);
104 break;
105 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
106 PERMISSION_ACTION_UMA(
107 secure_origin,
108 "ContentSettings.PermissionActions_MidiSysEx",
109 "ContentSettings.PermissionActionsSecureOrigin_MidiSysEx",
110 "ContentSettings.PermissionActionsInsecureOrigin_MidiSysEx",
111 action);
112 break;
113 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
114 PERMISSION_ACTION_UMA(
115 secure_origin,
116 "ContentSettings.PermissionActions_PushMessaging",
117 "ContentSettings.PermissionActionsSecureOrigin_PushMessaging",
118 "ContentSettings.PermissionActionsInsecureOrigin_PushMessaging",
119 action);
120 break;
121 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
122 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
123 PERMISSION_ACTION_UMA(
124 secure_origin,
125 "ContentSettings.PermissionActions_ProtectedMedia",
126 "ContentSettings.PermissionActionsSecureOrigin_ProtectedMedia",
127 "ContentSettings.PermissionActionsInsecureOrigin_ProtectedMedia",
128 action);
129 break;
130 #endif
131 default:
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,
139 requesting_origin);
142 void RecordPermissionRequest(ContentSettingsType permission,
143 const GURL& requesting_origin) {
144 bool secure_origin = content::IsOriginSecure(requesting_origin);
145 PermissionType type;
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",
152 requesting_origin);
153 break;
154 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
155 type = PermissionType::NOTIFICATIONS;
156 rappor::SampleDomainAndRegistryFromGURL(
157 g_browser_process->rappor_service(),
158 "ContentSettings.PermissionRequested.Notifications.Url",
159 requesting_origin);
160 break;
161 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
162 type = PermissionType::MIDI_SYSEX;
163 break;
164 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
165 type = PermissionType::PUSH_MESSAGING;
166 break;
167 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
168 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
169 type = PermissionType::PROTECTED_MEDIA_IDENTIFIER;
170 break;
171 #endif
172 default:
173 NOTREACHED() << "PERMISSION " << permission << " not accounted for";
174 return;
176 UMA_HISTOGRAM_ENUMERATION(
177 "ContentSettings.PermissionRequested",
178 static_cast<base::HistogramBase::Sample>(type),
179 static_cast<base::HistogramBase::Sample>(PermissionType::NUM));
180 if (secure_origin) {
181 UMA_HISTOGRAM_ENUMERATION(
182 "ContentSettings.PermissionRequested_SecureOrigin",
183 static_cast<base::HistogramBase::Sample>(type),
184 static_cast<base::HistogramBase::Sample>(PermissionType::NUM));
185 } else {
186 UMA_HISTOGRAM_ENUMERATION(
187 "ContentSettings.PermissionRequested_InsecureOrigin",
188 static_cast<base::HistogramBase::Sample>(type),
189 static_cast<base::HistogramBase::Sample>(PermissionType::NUM));
193 } // namespace
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);