PDF: Make PDFBrowserTest.FindAndCopy interactive, since it touchs the clipboard.
[chromium-blink-merge.git] / components / rappor / rappor_service.cc
blob0f79d8e39833bb0e3e6ac76e199d893fa78b850c
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
3 // found in the LICENSE file.
5 #include "components/rappor/rappor_service.h"
7 #include "base/base64.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/rand_util.h"
12 #include "base/stl_util.h"
13 #include "base/time/time.h"
14 #include "components/metrics/metrics_hashes.h"
15 #include "components/rappor/log_uploader.h"
16 #include "components/rappor/proto/rappor_metric.pb.h"
17 #include "components/rappor/rappor_metric.h"
18 #include "components/rappor/rappor_pref_names.h"
19 #include "components/variations/variations_associated_data.h"
21 namespace rappor {
23 namespace {
25 // Seconds before the initial log is generated.
26 const int kInitialLogIntervalSeconds = 15;
27 // Interval between ongoing logs.
28 const int kLogIntervalSeconds = 30 * 60;
30 const char kMimeType[] = "application/vnd.chrome.rappor";
32 // Constants for the RAPPOR rollout field trial.
33 const char kRapporRolloutFieldTrialName[] = "RapporRollout";
35 // Constant for the finch parameter name for the server URL
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl";
38 // The rappor server's URL.
39 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
41 GURL GetServerUrl() {
42 std::string server_url = variations::GetVariationParamValue(
43 kRapporRolloutFieldTrialName,
44 kRapporRolloutServerUrlParam);
45 if (!server_url.empty())
46 return GURL(server_url);
47 else
48 return GURL(kDefaultServerUrl);
51 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
52 // ETLD_PLUS_ONE_RAPPOR_TYPE
53 {128 /* Num cohorts */,
54 16 /* Bloom filter size bytes */,
55 2 /* Bloom filter hash count */,
56 rappor::PROBABILITY_50 /* Fake data probability */,
57 rappor::PROBABILITY_50 /* Fake one probability */,
58 rappor::PROBABILITY_75 /* One coin probability */,
59 rappor::PROBABILITY_25 /* Zero coin probability */},
62 } // namespace
64 RapporService::RapporService() : cohort_(-1) {}
66 RapporService::~RapporService() {
67 STLDeleteValues(&metrics_map_);
70 void RapporService::Start(PrefService* pref_service,
71 net::URLRequestContextGetter* request_context) {
72 const GURL server_url = GetServerUrl();
73 if (!server_url.is_valid()) {
74 DVLOG(1) << "RapporService not started: "
75 << server_url.spec() << " is invalid.";
76 return;
78 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec();
79 DCHECK(!uploader_);
80 LoadSecret(pref_service);
81 LoadCohort(pref_service);
82 uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
83 log_rotation_timer_.Start(
84 FROM_HERE,
85 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
86 this,
87 &RapporService::OnLogInterval);
90 void RapporService::OnLogInterval() {
91 DCHECK(uploader_);
92 DVLOG(2) << "RapporService::OnLogInterval";
93 RapporReports reports;
94 if (ExportMetrics(&reports)) {
95 std::string log_text;
96 bool success = reports.SerializeToString(&log_text);
97 DCHECK(success);
98 DVLOG(1) << "RapporService sending a report of "
99 << reports.report_size() << " value(s).";
100 uploader_->QueueLog(log_text);
102 log_rotation_timer_.Start(FROM_HERE,
103 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
104 this,
105 &RapporService::OnLogInterval);
108 // static
109 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
110 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
111 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
112 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
115 void RapporService::LoadCohort(PrefService* pref_service) {
116 DCHECK(!IsInitialized());
117 // Ignore and delete old cohort parameter.
118 pref_service->ClearPref(prefs::kRapporCohortDeprecated);
120 cohort_ = pref_service->GetInteger(prefs::kRapporCohortSeed);
121 // If the user is already assigned to a valid cohort, we're done.
122 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts)
123 return;
125 // This is the first time the client has started the service (or their
126 // preferences were corrupted). Randomly assign them to a cohort.
127 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts);
128 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_;
129 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort_);
132 void RapporService::LoadSecret(PrefService* pref_service) {
133 DCHECK(secret_.empty());
134 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret);
135 if (!secret_base64.empty()) {
136 bool decoded = base::Base64Decode(secret_base64, &secret_);
137 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
138 return;
139 // If the preference fails to decode, or is the wrong size, it must be
140 // corrupt, so continue as though it didn't exist yet and generate a new
141 // one.
144 DVLOG(2) << "Generated a new Rappor secret.";
145 secret_ = HmacByteVectorGenerator::GenerateEntropyInput();
146 base::Base64Encode(secret_, &secret_base64);
147 pref_service->SetString(prefs::kRapporSecret, secret_base64);
150 bool RapporService::ExportMetrics(RapporReports* reports) {
151 if (metrics_map_.empty())
152 return false;
154 DCHECK_GE(cohort_, 0);
155 reports->set_cohort(cohort_);
157 for (std::map<std::string, RapporMetric*>::const_iterator it =
158 metrics_map_.begin();
159 it != metrics_map_.end();
160 ++it) {
161 const RapporMetric* metric = it->second;
162 RapporReports::Report* report = reports->add_report();
163 report->set_name_hash(metrics::HashMetricName(it->first));
164 ByteVector bytes = metric->GetReport(secret_);
165 report->set_bits(std::string(bytes.begin(), bytes.end()));
167 STLDeleteValues(&metrics_map_);
168 return true;
171 bool RapporService::IsInitialized() const {
172 return cohort_ >= 0;
175 void RapporService::RecordSample(const std::string& metric_name,
176 RapporType type,
177 const std::string& sample) {
178 // Ignore the sample if the service hasn't started yet.
179 if (!IsInitialized())
180 return;
181 DCHECK_LT(type, NUM_RAPPOR_TYPES);
182 DVLOG(2) << "Recording sample \"" << sample
183 << "\" for metric \"" << metric_name
184 << "\" of type: " << type;
185 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample);
188 void RapporService::RecordSampleInternal(const std::string& metric_name,
189 const RapporParameters& parameters,
190 const std::string& sample) {
191 DCHECK(IsInitialized());
192 RapporMetric* metric = LookUpMetric(metric_name, parameters);
193 metric->AddSample(sample);
196 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
197 const RapporParameters& parameters) {
198 DCHECK(IsInitialized());
199 std::map<std::string, RapporMetric*>::const_iterator it =
200 metrics_map_.find(metric_name);
201 if (it != metrics_map_.end()) {
202 RapporMetric* metric = it->second;
203 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
204 return metric;
207 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
208 metrics_map_[metric_name] = new_metric;
209 return new_metric;
212 } // namespace rappor