1 // Copyright (c) 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 "chrome/browser/interstitials/security_interstitial_metrics_helper.h"
9 #include "base/metrics/histogram.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/history/history_service.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/metrics/rappor/sampling.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/webdata/web_data_service_factory.h"
16 #include "components/rappor/rappor_service.h"
17 #include "content/public/browser/web_contents.h"
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
20 #if defined(ENABLE_EXTENSIONS)
21 #include "chrome/browser/extensions/api/experience_sampling_private/experience_sampling.h"
24 SecurityInterstitialMetricsHelper::SecurityInterstitialMetricsHelper(
25 content::WebContents
* web_contents
,
26 const GURL
& request_url
,
27 const std::string
& uma_prefix
,
28 const std::string
& rappor_prefix
,
29 RapporReporting rappor_reporting
,
30 const std::string
& sampling_event_name
)
31 : web_contents_(web_contents
),
32 request_url_(request_url
),
33 uma_prefix_(uma_prefix
),
34 rappor_prefix_(rappor_prefix
),
35 rappor_reporting_(rappor_reporting
),
36 sampling_event_name_(sampling_event_name
),
38 DCHECK(!uma_prefix_
.empty());
39 DCHECK(!rappor_prefix_
.empty());
40 DCHECK(!sampling_event_name_
.empty());
41 HistoryService
* history_service
= HistoryServiceFactory::GetForProfile(
42 Profile::FromBrowserContext(web_contents
->GetBrowserContext()),
43 ServiceAccessType::EXPLICIT_ACCESS
);
44 if (history_service
) {
45 history_service
->GetVisibleVisitCountToHost(
47 base::Bind(&SecurityInterstitialMetricsHelper::OnGotHistoryCount
,
48 base::Unretained(this)),
53 SecurityInterstitialMetricsHelper::~SecurityInterstitialMetricsHelper() {
56 // Directly adds to the UMA histograms, using the same properties as
57 // UMA_HISTOGRAM_ENUMERATION, because the macro doesn't allow non-constant
58 // histogram names. Reports to Rappor for certain decisions.
59 void SecurityInterstitialMetricsHelper::RecordUserDecision(
60 SecurityInterstitialDecision decision
) {
62 std::string
decision_histogram_name(
63 "interstitial." + uma_prefix_
+ ".decision");
64 base::HistogramBase
* decision_histogram
= base::LinearHistogram::FactoryGet(
65 decision_histogram_name
, 1, MAX_DECISION
, MAX_DECISION
+ 1,
66 base::HistogramBase::kUmaTargetedHistogramFlag
);
67 decision_histogram
->Add(decision
);
70 rappor::RapporService
* rappor_service
= g_browser_process
->rappor_service();
71 if (rappor_service
&& rappor_reporting_
== REPORT_RAPPOR
&&
72 (decision
== PROCEED
|| decision
== DONT_PROCEED
)) {
73 // |domain| will be empty for hosts w/o TLDs
74 const std::string domain
= rappor::GetDomainAndRegistrySampleFromGURL(
77 // e.g. "interstitial.malware.domain" or "interstitial.ssl.domain"
78 const std::string metric_name
=
79 "interstitial." + rappor_prefix_
+ ".domain";
80 rappor_service
->RecordSample(metric_name
, rappor::COARSE_RAPPOR_TYPE
,
82 // TODO(nparker): Add reporting of (num_visits > 0) and decision
83 // once http://crbug.com/451647 is fixed.
86 #if defined(ENABLE_EXTENSIONS)
87 if (!sampling_event_
.get()) {
88 sampling_event_
.reset(new extensions::ExperienceSamplingEvent(
91 web_contents_
->GetLastCommittedURL(),
92 web_contents_
->GetBrowserContext()));
96 sampling_event_
->CreateUserDecisionEvent(
97 extensions::ExperienceSamplingEvent::kProceed
);
100 sampling_event_
->CreateUserDecisionEvent(
101 extensions::ExperienceSamplingEvent::kDeny
);
104 case PROCEEDING_DISABLED
:
110 // Record additional information about sites that users have
112 if (num_visits_
< 1 || (decision
!= PROCEED
&& decision
!= DONT_PROCEED
))
114 std::string
history_histogram_name(
115 "interstitial." + uma_prefix_
+ ".decision.repeat_visit");
116 base::HistogramBase
* history_histogram
= base::LinearHistogram::FactoryGet(
117 history_histogram_name
, 1, MAX_DECISION
, MAX_DECISION
+ 1,
118 base::HistogramBase::kUmaTargetedHistogramFlag
);
119 history_histogram
->Add(SHOW
);
120 history_histogram
->Add(decision
);
123 void SecurityInterstitialMetricsHelper::RecordUserInteraction(
124 SecurityInterstitialInteraction interaction
) {
125 std::string
interaction_histogram_name(
126 "interstitial." + uma_prefix_
+ ".interaction");
127 base::HistogramBase
* interaction_histogram
=
128 base::LinearHistogram::FactoryGet(
129 interaction_histogram_name
, 1, MAX_INTERACTION
, MAX_INTERACTION
+ 1,
130 base::HistogramBase::kUmaTargetedHistogramFlag
);
131 interaction_histogram
->Add(interaction
);
133 #if defined(ENABLE_EXTENSIONS)
134 if (!sampling_event_
.get()) {
135 sampling_event_
.reset(new extensions::ExperienceSamplingEvent(
136 sampling_event_name_
,
138 web_contents_
->GetLastCommittedURL(),
139 web_contents_
->GetBrowserContext()));
141 switch (interaction
) {
142 case SHOW_LEARN_MORE
:
143 sampling_event_
->set_has_viewed_learn_more(true);
146 sampling_event_
->set_has_viewed_details(true);
148 case SHOW_PRIVACY_POLICY
:
149 case SHOW_DIAGNOSTIC
:
151 case OPEN_TIME_SETTINGS
:
153 case MAX_INTERACTION
:
159 void SecurityInterstitialMetricsHelper::OnGotHistoryCount(
162 base::Time first_visit
) {
164 num_visits_
= num_visits
;