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

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

Issue 2150903002: Reland add hooks to permission layer for permission action reporting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix nits Created 4 years, 5 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_uma_util.h" 5 #include "chrome/browser/permissions/permission_uma_util.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/command_line.h"
9 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
10 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/permissions/permission_manager.h" 13 #include "chrome/browser/permissions/permission_manager.h"
13 #include "chrome/browser/permissions/permission_util.h" 14 #include "chrome/browser/permissions/permission_util.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
17 #include "chrome/browser/safe_browsing/ui_manager.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" 19 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/browser_sync/browser/profile_sync_service.h"
23 #include "components/prefs/pref_service.h"
16 #include "components/rappor/rappor_service.h" 24 #include "components/rappor/rappor_service.h"
17 #include "components/rappor/rappor_utils.h" 25 #include "components/rappor/rappor_utils.h"
18 #include "content/public/browser/permission_type.h" 26 #include "content/public/browser/permission_type.h"
19 #include "content/public/common/origin_util.h" 27 #include "content/public/common/origin_util.h"
20 #include "url/gurl.h" 28 #include "url/gurl.h"
21 29
22 // UMA keys need to be statically initialized so plain function would not 30 // UMA keys need to be statically initialized so plain function would not
23 // work. Use macros instead. 31 // work. Use macros instead.
24 #define PERMISSION_ACTION_UMA(secure_origin, permission, permission_secure, \ 32 #define PERMISSION_ACTION_UMA(secure_origin, permission, permission_secure, \
25 permission_insecure, action) \ 33 permission_insecure, action) \
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 } 75 }
68 76
69 std::string permission_str = 77 std::string permission_str =
70 PermissionUtil::GetPermissionString(permission); 78 PermissionUtil::GetPermissionString(permission);
71 if (permission_str.empty()) 79 if (permission_str.empty())
72 return ""; 80 return "";
73 return base::StringPrintf("ContentSettings.PermissionActions_%s.%s.Url", 81 return base::StringPrintf("ContentSettings.PermissionActions_%s.%s.Url",
74 permission_str.c_str(), action_str.c_str()); 82 permission_str.c_str(), action_str.c_str());
75 } 83 }
76 84
77 void RecordPermissionAction(PermissionType permission,
78 PermissionAction action,
79 const GURL& requesting_origin) {
80 bool secure_origin = content::IsOriginSecure(requesting_origin);
81
82 switch (permission) {
83 case PermissionType::GEOLOCATION:
84 PERMISSION_ACTION_UMA(
85 secure_origin,
86 "Permissions.Action.Geolocation",
87 "Permissions.Action.SecureOrigin.Geolocation",
88 "Permissions.Action.InsecureOrigin.Geolocation",
89 action);
90 break;
91 case PermissionType::NOTIFICATIONS:
92 PERMISSION_ACTION_UMA(
93 secure_origin,
94 "Permissions.Action.Notifications",
95 "Permissions.Action.SecureOrigin.Notifications",
96 "Permissions.Action.InsecureOrigin.Notifications",
97 action);
98 break;
99 case PermissionType::MIDI_SYSEX:
100 PERMISSION_ACTION_UMA(
101 secure_origin,
102 "Permissions.Action.MidiSysEx",
103 "Permissions.Action.SecureOrigin.MidiSysEx",
104 "Permissions.Action.InsecureOrigin.MidiSysEx",
105 action);
106 break;
107 case PermissionType::PUSH_MESSAGING:
108 PERMISSION_ACTION_UMA(
109 secure_origin,
110 "Permissions.Action.PushMessaging",
111 "Permissions.Action.SecureOrigin.PushMessaging",
112 "Permissions.Action.InsecureOrigin.PushMessaging",
113 action);
114 break;
115 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
116 PERMISSION_ACTION_UMA(
117 secure_origin,
118 "Permissions.Action.ProtectedMedia",
119 "Permissions.Action.SecureOrigin.ProtectedMedia",
120 "Permissions.Action.InsecureOrigin.ProtectedMedia",
121 action);
122 break;
123 case PermissionType::DURABLE_STORAGE:
124 PERMISSION_ACTION_UMA(
125 secure_origin,
126 "Permissions.Action.DurableStorage",
127 "Permissions.Action.SecureOrigin.DurableStorage",
128 "Permissions.Action.InsecureOrigin.DurableStorage",
129 action);
130 break;
131 case PermissionType::AUDIO_CAPTURE:
132 // Media permissions are disabled on insecure origins, so there's no
133 // need to record metrics for secure/insecue.
134 UMA_HISTOGRAM_ENUMERATION("Permissions.Action.AudioCapture", action,
135 PERMISSION_ACTION_NUM);
136 break;
137 case PermissionType::VIDEO_CAPTURE:
138 UMA_HISTOGRAM_ENUMERATION("Permissions.Action.VideoCapture", action,
139 PERMISSION_ACTION_NUM);
140 break;
141 // The user is not prompted for these permissions, thus there is no
142 // permission action recorded for them.
143 case PermissionType::MIDI:
144 case PermissionType::BACKGROUND_SYNC:
145 case PermissionType::NUM:
146 NOTREACHED() << "PERMISSION "
147 << PermissionUtil::GetPermissionString(permission)
148 << " not accounted for";
149 }
150
151 // Retrieve the name of the RAPPOR metric. Currently, the new metric name is
152 // the deprecated name with "2" on the end, e.g.
153 // ContentSettings.PermissionActions_Geolocation.Granted.Url2. For simplicity,
154 // we retrieve the deprecated name and append the "2" for the new name.
155 // TODO(dominickn): remove the deprecated metric and replace it solely with
156 // the new one in GetRapporMetric - crbug.com/605836.
157 const std::string deprecated_metric = GetRapporMetric(permission, action);
158 rappor::RapporService* rappor_service = g_browser_process->rappor_service();
159 if (!deprecated_metric.empty() && rappor_service) {
160 rappor::SampleDomainAndRegistryFromGURL(rappor_service, deprecated_metric,
161 requesting_origin);
162
163 std::string rappor_metric = deprecated_metric + "2";
164 rappor_service->RecordSample(
165 rappor_metric, rappor::LOW_FREQUENCY_ETLD_PLUS_ONE_RAPPOR_TYPE,
166 rappor::GetDomainAndRegistrySampleFromGURL(requesting_origin));
167 }
168 }
169
170 void RecordPermissionRequest(PermissionType permission, 85 void RecordPermissionRequest(PermissionType permission,
171 const GURL& requesting_origin, 86 const GURL& requesting_origin,
172 const GURL& embedding_origin, 87 const GURL& embedding_origin,
173 Profile* profile) { 88 Profile* profile) {
174 rappor::RapporService* rappor_service = g_browser_process->rappor_service(); 89 rappor::RapporService* rappor_service = g_browser_process->rappor_service();
175 if (rappor_service) { 90 if (rappor_service) {
176 if (permission == PermissionType::GEOLOCATION) { 91 if (permission == PermissionType::GEOLOCATION) {
177 // TODO(dominickn): remove this deprecated metric - crbug.com/605836. 92 // TODO(dominickn): remove this deprecated metric - crbug.com/605836.
178 rappor::SampleDomainAndRegistryFromGURL( 93 rappor::SampleDomainAndRegistryFromGURL(
179 rappor_service, 94 rappor_service,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 // add new permission 188 // add new permission
274 void PermissionUmaUtil::PermissionRequested(PermissionType permission, 189 void PermissionUmaUtil::PermissionRequested(PermissionType permission,
275 const GURL& requesting_origin, 190 const GURL& requesting_origin,
276 const GURL& embedding_origin, 191 const GURL& embedding_origin,
277 Profile* profile) { 192 Profile* profile) {
278 RecordPermissionRequest(permission, requesting_origin, embedding_origin, 193 RecordPermissionRequest(permission, requesting_origin, embedding_origin,
279 profile); 194 profile);
280 } 195 }
281 196
282 void PermissionUmaUtil::PermissionGranted(PermissionType permission, 197 void PermissionUmaUtil::PermissionGranted(PermissionType permission,
283 const GURL& requesting_origin) { 198 const GURL& requesting_origin,
284 RecordPermissionAction(permission, GRANTED, requesting_origin); 199 Profile* profile) {
200 RecordPermissionAction(permission, GRANTED, requesting_origin, profile);
285 } 201 }
286 202
287 void PermissionUmaUtil::PermissionDenied(PermissionType permission, 203 void PermissionUmaUtil::PermissionDenied(PermissionType permission,
288 const GURL& requesting_origin) { 204 const GURL& requesting_origin,
289 RecordPermissionAction(permission, DENIED, requesting_origin); 205 Profile* profile) {
206 RecordPermissionAction(permission, DENIED, requesting_origin, profile);
290 } 207 }
291 208
292 void PermissionUmaUtil::PermissionDismissed(PermissionType permission, 209 void PermissionUmaUtil::PermissionDismissed(PermissionType permission,
293 const GURL& requesting_origin) { 210 const GURL& requesting_origin,
294 RecordPermissionAction(permission, DISMISSED, requesting_origin); 211 Profile* profile) {
212 RecordPermissionAction(permission, DISMISSED, requesting_origin, profile);
295 } 213 }
296 214
297 void PermissionUmaUtil::PermissionIgnored(PermissionType permission, 215 void PermissionUmaUtil::PermissionIgnored(PermissionType permission,
298 const GURL& requesting_origin) { 216 const GURL& requesting_origin,
299 RecordPermissionAction(permission, IGNORED, requesting_origin); 217 Profile* profile) {
218 RecordPermissionAction(permission, IGNORED, requesting_origin, profile);
300 } 219 }
301 220
302 void PermissionUmaUtil::PermissionRevoked(PermissionType permission, 221 void PermissionUmaUtil::PermissionRevoked(PermissionType permission,
303 const GURL& revoked_origin) { 222 const GURL& revoked_origin,
223 Profile* profile) {
304 // TODO(tsergeant): Expand metrics definitions for revocation to include all 224 // TODO(tsergeant): Expand metrics definitions for revocation to include all
305 // permissions. 225 // permissions.
306 if (permission == PermissionType::NOTIFICATIONS || 226 if (permission == PermissionType::NOTIFICATIONS ||
307 permission == PermissionType::GEOLOCATION || 227 permission == PermissionType::GEOLOCATION ||
308 permission == PermissionType::AUDIO_CAPTURE || 228 permission == PermissionType::AUDIO_CAPTURE ||
309 permission == PermissionType::VIDEO_CAPTURE) { 229 permission == PermissionType::VIDEO_CAPTURE) {
310 RecordPermissionAction(permission, REVOKED, revoked_origin); 230 RecordPermissionAction(permission, REVOKED, revoked_origin, profile);
311 } 231 }
312 } 232 }
313 233
314 void PermissionUmaUtil::PermissionPromptShown( 234 void PermissionUmaUtil::PermissionPromptShown(
315 const std::vector<PermissionBubbleRequest*>& requests) { 235 const std::vector<PermissionBubbleRequest*>& requests) {
316 DCHECK(!requests.empty()); 236 DCHECK(!requests.empty());
317 237
318 PermissionBubbleType permission_prompt_type = PermissionBubbleType::MULTIPLE; 238 PermissionBubbleType permission_prompt_type = PermissionBubbleType::MULTIPLE;
319 if (requests.size() == 1) 239 if (requests.size() == 1)
320 permission_prompt_type = requests[0]->GetPermissionBubbleType(); 240 permission_prompt_type = requests[0]->GetPermissionBubbleType();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 287 }
368 288
369 void PermissionUmaUtil::PermissionPromptDenied( 289 void PermissionUmaUtil::PermissionPromptDenied(
370 const std::vector<PermissionBubbleRequest*>& requests) { 290 const std::vector<PermissionBubbleRequest*>& requests) {
371 DCHECK(!requests.empty()); 291 DCHECK(!requests.empty());
372 DCHECK(requests.size() == 1); 292 DCHECK(requests.size() == 1);
373 293
374 PERMISSION_BUBBLE_TYPE_UMA(kPermissionsPromptDenied, 294 PERMISSION_BUBBLE_TYPE_UMA(kPermissionsPromptDenied,
375 requests[0]->GetPermissionBubbleType()); 295 requests[0]->GetPermissionBubbleType());
376 } 296 }
297
298 bool PermissionUmaUtil::IsOptedIntoPermissionActionReporting(Profile* profile) {
299 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
300 switches::kEnablePermissionActionReporting))
301 return false;
302 // TODO(stefanocs): Remove this check once all callsites have been updated
303 // to not pass a nullptr.
304 if (!profile)
305 return false;
306 if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE)
307 return false;
308 if (!profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled))
309 return false;
310
311 ProfileSyncService* profile_sync_service =
312 ProfileSyncServiceFactory::GetForProfile(profile);
313
314 // Do not report if profile can't get a profile sync service due to disable
315 // sync flag.
316 if (!profile_sync_service)
317 return false;
318
319 if (!profile_sync_service->CanSyncStart())
320 return false;
321
322 syncer::ModelTypeSet preferred_data_types =
323 profile_sync_service->GetPreferredDataTypes();
324 if (!preferred_data_types.Has(syncer::PROXY_TABS))
325 return false;
326 if (!preferred_data_types.Has(syncer::PRIORITY_PREFERENCES))
327 return false;
328
329 return true;
330 }
331
332 void PermissionUmaUtil::RecordPermissionAction(PermissionType permission,
333 PermissionAction action,
334 const GURL& requesting_origin,
335 Profile* profile) {
336 if (IsOptedIntoPermissionActionReporting(profile)) {
337 // TODO(stefanocs): Add browsertests to make sure the reports are being
338 // sent.
339 g_browser_process->safe_browsing_service()
340 ->ui_manager()
341 ->ReportPermissionAction(requesting_origin, permission, action);
342 }
343
344 bool secure_origin = content::IsOriginSecure(requesting_origin);
345
346 switch (permission) {
347 case PermissionType::GEOLOCATION:
348 PERMISSION_ACTION_UMA(secure_origin, "Permissions.Action.Geolocation",
349 "Permissions.Action.SecureOrigin.Geolocation",
350 "Permissions.Action.InsecureOrigin.Geolocation",
351 action);
352 break;
353 case PermissionType::NOTIFICATIONS:
354 PERMISSION_ACTION_UMA(secure_origin, "Permissions.Action.Notifications",
355 "Permissions.Action.SecureOrigin.Notifications",
356 "Permissions.Action.InsecureOrigin.Notifications",
357 action);
358 break;
359 case PermissionType::MIDI_SYSEX:
360 PERMISSION_ACTION_UMA(secure_origin, "Permissions.Action.MidiSysEx",
361 "Permissions.Action.SecureOrigin.MidiSysEx",
362 "Permissions.Action.InsecureOrigin.MidiSysEx",
363 action);
364 break;
365 case PermissionType::PUSH_MESSAGING:
366 PERMISSION_ACTION_UMA(secure_origin, "Permissions.Action.PushMessaging",
367 "Permissions.Action.SecureOrigin.PushMessaging",
368 "Permissions.Action.InsecureOrigin.PushMessaging",
369 action);
370 break;
371 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
372 PERMISSION_ACTION_UMA(secure_origin, "Permissions.Action.ProtectedMedia",
373 "Permissions.Action.SecureOrigin.ProtectedMedia",
374 "Permissions.Action.InsecureOrigin.ProtectedMedia",
375 action);
376 break;
377 case PermissionType::DURABLE_STORAGE:
378 PERMISSION_ACTION_UMA(secure_origin, "Permissions.Action.DurableStorage",
379 "Permissions.Action.SecureOrigin.DurableStorage",
380 "Permissions.Action.InsecureOrigin.DurableStorage",
381 action);
382 break;
383 case PermissionType::AUDIO_CAPTURE:
384 // Media permissions are disabled on insecure origins, so there's no
385 // need to record metrics for secure/insecue.
386 UMA_HISTOGRAM_ENUMERATION("Permissions.Action.AudioCapture", action,
387 PERMISSION_ACTION_NUM);
388 break;
389 case PermissionType::VIDEO_CAPTURE:
390 UMA_HISTOGRAM_ENUMERATION("Permissions.Action.VideoCapture", action,
391 PERMISSION_ACTION_NUM);
392 break;
393 // The user is not prompted for these permissions, thus there is no
394 // permission action recorded for them.
395 case PermissionType::MIDI:
396 case PermissionType::BACKGROUND_SYNC:
397 case PermissionType::NUM:
398 NOTREACHED() << "PERMISSION "
399 << PermissionUtil::GetPermissionString(permission)
400 << " not accounted for";
401 }
402
403 // Retrieve the name of the RAPPOR metric. Currently, the new metric name is
404 // the deprecated name with "2" on the end, e.g.
405 // ContentSettings.PermissionActions_Geolocation.Granted.Url2. For simplicity,
406 // we retrieve the deprecated name and append the "2" for the new name.
407 // TODO(dominickn): remove the deprecated metric and replace it solely with
408 // the new one in GetRapporMetric - crbug.com/605836.
409 const std::string deprecated_metric = GetRapporMetric(permission, action);
410 rappor::RapporService* rappor_service = g_browser_process->rappor_service();
411 if (!deprecated_metric.empty() && rappor_service) {
412 rappor::SampleDomainAndRegistryFromGURL(rappor_service, deprecated_metric,
413 requesting_origin);
414
415 std::string rappor_metric = deprecated_metric + "2";
416 rappor_service->RecordSample(
417 rappor_metric, rappor::LOW_FREQUENCY_ETLD_PLUS_ONE_RAPPOR_TYPE,
418 rappor::GetDomainAndRegistrySampleFromGURL(requesting_origin));
419 }
420 }
OLDNEW
« no previous file with comments | « chrome/browser/permissions/permission_uma_util.h ('k') | chrome/browser/permissions/permission_uma_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698