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"
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";
42 std::string server_url
= variations::GetVariationParamValue(
43 kRapporRolloutFieldTrialName
,
44 kRapporRolloutServerUrlParam
);
45 if (!server_url
.empty())
46 return GURL(server_url
);
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 */},
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.";
78 DVLOG(1) << "RapporService started. Reporting to " << server_url
.spec();
80 LoadSecret(pref_service
);
81 LoadCohort(pref_service
);
82 uploader_
.reset(new LogUploader(server_url
, kMimeType
, request_context
));
83 log_rotation_timer_
.Start(
85 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds
),
87 &RapporService::OnLogInterval
);
90 void RapporService::OnLogInterval() {
92 DVLOG(2) << "RapporService::OnLogInterval";
93 RapporReports reports
;
94 if (ExportMetrics(&reports
)) {
96 bool success
= reports
.SerializeToString(&log_text
);
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
),
105 &RapporService::OnLogInterval
);
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
)
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
)
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
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())
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();
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_
);
171 bool RapporService::IsInitialized() const {
175 void RapporService::RecordSample(const std::string
& metric_name
,
177 const std::string
& sample
) {
178 // Ignore the sample if the service hasn't started yet.
179 if (!IsInitialized())
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());
207 RapporMetric
* new_metric
= new RapporMetric(metric_name
, parameters
, cohort_
);
208 metrics_map_
[metric_name
] = new_metric
;
212 } // namespace rappor