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_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/webdata/web_data_service_factory.h"
14 #include "components/history/core/browser/history_service.h"
15 #include "components/rappor/rappor_service.h"
16 #include "components/rappor/rappor_utils.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 history::HistoryService
* history_service
=
42 HistoryServiceFactory::GetForProfile(
43 Profile::FromBrowserContext(web_contents
->GetBrowserContext()),
44 ServiceAccessType::EXPLICIT_ACCESS
);
45 if (history_service
) {
46 history_service
->GetVisibleVisitCountToHost(
48 base::Bind(&SecurityInterstitialMetricsHelper::OnGotHistoryCount
,
49 base::Unretained(this)),
54 SecurityInterstitialMetricsHelper::~SecurityInterstitialMetricsHelper() {
57 // Directly adds to the UMA histograms, using the same properties as
58 // UMA_HISTOGRAM_ENUMERATION, because the macro doesn't allow non-constant
59 // histogram names. Reports to Rappor for certain decisions.
60 void SecurityInterstitialMetricsHelper::RecordUserDecision(
61 SecurityInterstitialDecision decision
) {
63 std::string
decision_histogram_name(
64 "interstitial." + uma_prefix_
+ ".decision");
65 base::HistogramBase
* decision_histogram
= base::LinearHistogram::FactoryGet(
66 decision_histogram_name
, 1, MAX_DECISION
, MAX_DECISION
+ 1,
67 base::HistogramBase::kUmaTargetedHistogramFlag
);
68 decision_histogram
->Add(decision
);
71 rappor::RapporService
* rappor_service
= g_browser_process
->rappor_service();
72 if (rappor_service
&& rappor_reporting_
== REPORT_RAPPOR
&&
73 (decision
== PROCEED
|| decision
== DONT_PROCEED
)) {
74 // |domain| will be empty for hosts w/o TLDs
75 const std::string domain
= rappor::GetDomainAndRegistrySampleFromGURL(
78 // e.g. "interstitial.malware.domain" or "interstitial.ssl.domain"
79 const std::string metric_name
=
80 "interstitial." + rappor_prefix_
+ ".domain";
81 rappor_service
->RecordSample(metric_name
, rappor::COARSE_RAPPOR_TYPE
,
83 // TODO(nparker): Add reporting of (num_visits > 0) and decision
84 // once http://crbug.com/451647 is fixed.
87 #if defined(ENABLE_EXTENSIONS)
88 if (!sampling_event_
.get()) {
89 sampling_event_
.reset(new extensions::ExperienceSamplingEvent(
92 web_contents_
->GetLastCommittedURL(),
93 web_contents_
->GetBrowserContext()));
97 sampling_event_
->CreateUserDecisionEvent(
98 extensions::ExperienceSamplingEvent::kProceed
);
101 sampling_event_
->CreateUserDecisionEvent(
102 extensions::ExperienceSamplingEvent::kDeny
);
105 case PROCEEDING_DISABLED
:
111 // Record additional information about sites that users have
113 if (num_visits_
< 1 || (decision
!= PROCEED
&& decision
!= DONT_PROCEED
))
115 std::string
history_histogram_name(
116 "interstitial." + uma_prefix_
+ ".decision.repeat_visit");
117 base::HistogramBase
* history_histogram
= base::LinearHistogram::FactoryGet(
118 history_histogram_name
, 1, MAX_DECISION
, MAX_DECISION
+ 1,
119 base::HistogramBase::kUmaTargetedHistogramFlag
);
120 history_histogram
->Add(SHOW
);
121 history_histogram
->Add(decision
);
124 void SecurityInterstitialMetricsHelper::RecordUserInteraction(
125 SecurityInterstitialInteraction interaction
) {
126 std::string
interaction_histogram_name(
127 "interstitial." + uma_prefix_
+ ".interaction");
128 base::HistogramBase
* interaction_histogram
=
129 base::LinearHistogram::FactoryGet(
130 interaction_histogram_name
, 1, MAX_INTERACTION
, MAX_INTERACTION
+ 1,
131 base::HistogramBase::kUmaTargetedHistogramFlag
);
132 interaction_histogram
->Add(interaction
);
134 #if defined(ENABLE_EXTENSIONS)
135 if (!sampling_event_
.get()) {
136 sampling_event_
.reset(new extensions::ExperienceSamplingEvent(
137 sampling_event_name_
,
139 web_contents_
->GetLastCommittedURL(),
140 web_contents_
->GetBrowserContext()));
142 switch (interaction
) {
143 case SHOW_LEARN_MORE
:
144 sampling_event_
->set_has_viewed_learn_more(true);
147 sampling_event_
->set_has_viewed_details(true);
149 case SHOW_PRIVACY_POLICY
:
150 case SHOW_DIAGNOSTIC
:
152 case OPEN_TIME_SETTINGS
:
154 case SET_EXTENDED_REPORTING_ENABLED
:
155 case SET_EXTENDED_REPORTING_DISABLED
:
156 case EXTENDED_REPORTING_IS_ENABLED
:
157 case MAX_INTERACTION
:
163 void SecurityInterstitialMetricsHelper::OnGotHistoryCount(
166 base::Time first_visit
) {
168 num_visits_
= num_visits
;