1 // Copyright 2015 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 "components/security_interstitials/core/metrics_helper.h"
7 #include "base/metrics/histogram.h"
8 #include "components/history/core/browser/history_service.h"
9 #include "components/rappor/rappor_service.h"
10 #include "components/rappor/rappor_utils.h"
11 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14 // Used for setting bits in Rappor's "interstitial.*.flags"
15 enum InterstitialFlagBits
{
22 namespace security_interstitials
{
24 MetricsHelper::MetricsHelper(const GURL
& request_url
,
25 const ReportDetails settings
,
26 history::HistoryService
* history_service
,
27 rappor::RapporService
* rappor_service
)
28 : request_url_(request_url
),
30 rappor_service_(rappor_service
),
32 DCHECK(!settings_
.metric_prefix
.empty());
33 if (settings_
.rappor_report_type
== rappor::NUM_RAPPOR_TYPES
) // Default.
34 rappor_service_
= nullptr;
35 DCHECK(!rappor_service_
|| !settings_
.rappor_prefix
.empty());
36 if (history_service
) {
37 history_service
->GetVisibleVisitCountToHost(
39 base::Bind(&MetricsHelper::OnGotHistoryCount
, base::Unretained(this)),
44 // Directly adds to the UMA histograms, using the same properties as
45 // UMA_HISTOGRAM_ENUMERATION, because the macro doesn't allow non-constant
46 // histogram names. Reports to Rappor for certain decisions.
47 void MetricsHelper::RecordUserDecision(Decision decision
) {
49 const std::string
decision_histogram_name(
50 "interstitial." + settings_
.metric_prefix
+ ".decision");
51 base::HistogramBase
* decision_histogram
= base::LinearHistogram::FactoryGet(
52 decision_histogram_name
, 1, MAX_DECISION
, MAX_DECISION
+ 1,
53 base::HistogramBase::kUmaTargetedHistogramFlag
);
54 decision_histogram
->Add(decision
);
57 if (rappor_service_
&& (decision
== PROCEED
|| decision
== DONT_PROCEED
)) {
58 scoped_ptr
<rappor::Sample
> sample
=
59 rappor_service_
->CreateSample(settings_
.rappor_report_type
);
61 // This will populate, for example, "intersitial.malware.domain" or
62 // "interstitial.ssl2.domain". |domain| will be empty for hosts w/o TLDs.
63 const std::string domain
=
64 rappor::GetDomainAndRegistrySampleFromGURL(request_url_
);
65 sample
->SetStringField("domain", domain
);
67 // Only report history and decision if we have history data.
68 if (num_visits_
>= 0) {
70 if (decision
== PROCEED
)
71 flags
|= 1 << InterstitialFlagBits::DID_PROCEED
;
73 flags
|= 1 << InterstitialFlagBits::IS_REPEAT_VISIT
;
74 // e.g. "interstitial.malware.flags"
75 sample
->SetFlagsField("flags", flags
,
76 InterstitialFlagBits::HIGHEST_USED_BIT
+ 1);
78 rappor_service_
->RecordSampleObj("interstitial." + settings_
.rappor_prefix
,
82 // Record additional information about sites that users have
86 std::string
history_histogram_name("interstitial." + settings_
.metric_prefix
+
87 ".decision.repeat_visit");
88 base::HistogramBase
* history_histogram
= base::LinearHistogram::FactoryGet(
89 history_histogram_name
, 1, MAX_DECISION
, MAX_DECISION
+ 1,
90 base::HistogramBase::kUmaTargetedHistogramFlag
);
91 history_histogram
->Add(SHOW
);
92 history_histogram
->Add(decision
);
94 RecordExtraUserDecisionMetrics(decision
);
97 void MetricsHelper::RecordUserInteraction(Interaction interaction
) {
98 const std::string
interaction_histogram_name(
99 "interstitial." + settings_
.metric_prefix
+ ".interaction");
100 base::HistogramBase
* interaction_histogram
=
101 base::LinearHistogram::FactoryGet(
102 interaction_histogram_name
, 1, MAX_INTERACTION
, MAX_INTERACTION
+ 1,
103 base::HistogramBase::kUmaTargetedHistogramFlag
);
104 interaction_histogram
->Add(interaction
);
106 RecordExtraUserInteractionMetrics(interaction
);
109 void MetricsHelper::OnGotHistoryCount(bool success
,
111 base::Time
/*first_visit*/) {
113 num_visits_
= num_visits
;
116 } // namespace security_interstitials