ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / interstitials / security_interstitial_metrics_helper.cc
blob2c7961be03ca159819129343b1816ac80ea01a19
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"
7 #include <string>
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"
22 #endif
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),
37 num_visits_(-1) {
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(
46 request_url_,
47 base::Bind(&SecurityInterstitialMetricsHelper::OnGotHistoryCount,
48 base::Unretained(this)),
49 &request_tracker_);
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) {
61 // UMA
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);
69 // Rappor
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(
75 request_url_);
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,
81 domain);
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(
89 sampling_event_name_,
90 request_url_,
91 web_contents_->GetLastCommittedURL(),
92 web_contents_->GetBrowserContext()));
94 switch (decision) {
95 case PROCEED:
96 sampling_event_->CreateUserDecisionEvent(
97 extensions::ExperienceSamplingEvent::kProceed);
98 break;
99 case DONT_PROCEED:
100 sampling_event_->CreateUserDecisionEvent(
101 extensions::ExperienceSamplingEvent::kDeny);
102 break;
103 case SHOW:
104 case PROCEEDING_DISABLED:
105 case MAX_DECISION:
106 break;
108 #endif
110 // Record additional information about sites that users have
111 // visited before.
112 if (num_visits_ < 1 || (decision != PROCEED && decision != DONT_PROCEED))
113 return;
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_,
137 request_url_,
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);
144 break;
145 case SHOW_ADVANCED:
146 sampling_event_->set_has_viewed_details(true);
147 break;
148 case SHOW_PRIVACY_POLICY:
149 case SHOW_DIAGNOSTIC:
150 case RELOAD:
151 case OPEN_TIME_SETTINGS:
152 case TOTAL_VISITS:
153 case MAX_INTERACTION:
154 break;
156 #endif
159 void SecurityInterstitialMetricsHelper::OnGotHistoryCount(
160 bool success,
161 int num_visits,
162 base::Time first_visit) {
163 if (success)
164 num_visits_ = num_visits;