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 const char kRapporDailyEventHistogram
[] = "Rappor.DailyEvent.IntervalType";
34 // Constants for the RAPPOR rollout field trial.
35 const char kRapporRolloutFieldTrialName
[] = "RapporRollout";
37 // Constant for the finch parameter name for the server URL
38 const char kRapporRolloutServerUrlParam
[] = "ServerUrl";
40 // Constant for the finch parameter name for the server URL
41 const char kRapporRolloutRequireUmaParam
[] = "RequireUma";
43 // The rappor server's URL.
44 const char kDefaultServerUrl
[] = "https://clients4.google.com/rappor";
46 GURL
GetServerUrl(bool metrics_enabled
) {
47 bool require_uma
= variations::GetVariationParamValue(
48 kRapporRolloutFieldTrialName
,
49 kRapporRolloutRequireUmaParam
) != "False";
50 if (!metrics_enabled
&& require_uma
)
51 return GURL(); // Invalid URL disables Rappor.
52 std::string server_url
= variations::GetVariationParamValue(
53 kRapporRolloutFieldTrialName
,
54 kRapporRolloutServerUrlParam
);
55 if (!server_url
.empty())
56 return GURL(server_url
);
58 return GURL(kDefaultServerUrl
);
61 const RapporParameters kRapporParametersForType
[NUM_RAPPOR_TYPES
] = {
62 // ETLD_PLUS_ONE_RAPPOR_TYPE
63 {128 /* Num cohorts */,
64 16 /* Bloom filter size bytes */,
65 2 /* Bloom filter hash count */,
66 rappor::PROBABILITY_50
/* Fake data probability */,
67 rappor::PROBABILITY_50
/* Fake one probability */,
68 rappor::PROBABILITY_75
/* One coin probability */,
69 rappor::PROBABILITY_25
/* Zero coin probability */},
74 RapporService::RapporService(PrefService
* pref_service
)
75 : pref_service_(pref_service
),
77 daily_event_(pref_service
,
78 prefs::kRapporLastDailySample
,
79 kRapporDailyEventHistogram
) {
82 RapporService::~RapporService() {
83 STLDeleteValues(&metrics_map_
);
86 void RapporService::AddDailyObserver(
87 scoped_ptr
<metrics::DailyEvent::Observer
> observer
) {
88 daily_event_
.AddObserver(observer
.Pass());
91 void RapporService::Start(net::URLRequestContextGetter
* request_context
,
92 bool metrics_enabled
) {
93 const GURL server_url
= GetServerUrl(metrics_enabled
);
94 if (!server_url
.is_valid()) {
95 DVLOG(1) << server_url
.spec() << " is invalid. "
96 << "RapporService not started.";
99 DVLOG(1) << "RapporService started. Reporting to " << server_url
.spec();
103 uploader_
.reset(new LogUploader(server_url
, kMimeType
, request_context
));
104 log_rotation_timer_
.Start(
106 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds
),
108 &RapporService::OnLogInterval
);
111 void RapporService::OnLogInterval() {
113 DVLOG(2) << "RapporService::OnLogInterval";
114 daily_event_
.CheckInterval();
115 RapporReports reports
;
116 if (ExportMetrics(&reports
)) {
117 std::string log_text
;
118 bool success
= reports
.SerializeToString(&log_text
);
120 DVLOG(1) << "RapporService sending a report of "
121 << reports
.report_size() << " value(s).";
122 uploader_
->QueueLog(log_text
);
124 log_rotation_timer_
.Start(FROM_HERE
,
125 base::TimeDelta::FromSeconds(kLogIntervalSeconds
),
127 &RapporService::OnLogInterval
);
131 void RapporService::RegisterPrefs(PrefRegistrySimple
* registry
) {
132 registry
->RegisterStringPref(prefs::kRapporSecret
, std::string());
133 registry
->RegisterIntegerPref(prefs::kRapporCohortDeprecated
, -1);
134 registry
->RegisterIntegerPref(prefs::kRapporCohortSeed
, -1);
135 metrics::DailyEvent::RegisterPref(registry
,
136 prefs::kRapporLastDailySample
);
139 void RapporService::LoadCohort() {
140 DCHECK(!IsInitialized());
141 // Ignore and delete old cohort parameter.
142 pref_service_
->ClearPref(prefs::kRapporCohortDeprecated
);
144 cohort_
= pref_service_
->GetInteger(prefs::kRapporCohortSeed
);
145 // If the user is already assigned to a valid cohort, we're done.
146 if (cohort_
>= 0 && cohort_
< RapporParameters::kMaxCohorts
)
149 // This is the first time the client has started the service (or their
150 // preferences were corrupted). Randomly assign them to a cohort.
151 cohort_
= base::RandGenerator(RapporParameters::kMaxCohorts
);
152 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_
;
153 pref_service_
->SetInteger(prefs::kRapporCohortSeed
, cohort_
);
156 void RapporService::LoadSecret() {
157 DCHECK(secret_
.empty());
158 std::string secret_base64
= pref_service_
->GetString(prefs::kRapporSecret
);
159 if (!secret_base64
.empty()) {
160 bool decoded
= base::Base64Decode(secret_base64
, &secret_
);
161 if (decoded
&& secret_
.size() == HmacByteVectorGenerator::kEntropyInputSize
)
163 // If the preference fails to decode, or is the wrong size, it must be
164 // corrupt, so continue as though it didn't exist yet and generate a new
168 DVLOG(2) << "Generated a new Rappor secret.";
169 secret_
= HmacByteVectorGenerator::GenerateEntropyInput();
170 base::Base64Encode(secret_
, &secret_base64
);
171 pref_service_
->SetString(prefs::kRapporSecret
, secret_base64
);
174 bool RapporService::ExportMetrics(RapporReports
* reports
) {
175 if (metrics_map_
.empty())
178 DCHECK_GE(cohort_
, 0);
179 reports
->set_cohort(cohort_
);
181 for (std::map
<std::string
, RapporMetric
*>::const_iterator it
=
182 metrics_map_
.begin();
183 it
!= metrics_map_
.end();
185 const RapporMetric
* metric
= it
->second
;
186 RapporReports::Report
* report
= reports
->add_report();
187 report
->set_name_hash(metrics::HashMetricName(it
->first
));
188 ByteVector bytes
= metric
->GetReport(secret_
);
189 report
->set_bits(std::string(bytes
.begin(), bytes
.end()));
191 STLDeleteValues(&metrics_map_
);
195 bool RapporService::IsInitialized() const {
199 void RapporService::RecordSample(const std::string
& metric_name
,
201 const std::string
& sample
) {
202 // Ignore the sample if the service hasn't started yet.
203 if (!IsInitialized())
205 DCHECK_LT(type
, NUM_RAPPOR_TYPES
);
206 DVLOG(2) << "Recording sample \"" << sample
207 << "\" for metric \"" << metric_name
208 << "\" of type: " << type
;
209 RecordSampleInternal(metric_name
, kRapporParametersForType
[type
], sample
);
212 void RapporService::RecordSampleInternal(const std::string
& metric_name
,
213 const RapporParameters
& parameters
,
214 const std::string
& sample
) {
215 DCHECK(IsInitialized());
216 RapporMetric
* metric
= LookUpMetric(metric_name
, parameters
);
217 metric
->AddSample(sample
);
220 RapporMetric
* RapporService::LookUpMetric(const std::string
& metric_name
,
221 const RapporParameters
& parameters
) {
222 DCHECK(IsInitialized());
223 std::map
<std::string
, RapporMetric
*>::const_iterator it
=
224 metrics_map_
.find(metric_name
);
225 if (it
!= metrics_map_
.end()) {
226 RapporMetric
* metric
= it
->second
;
227 DCHECK_EQ(parameters
.ToString(), metric
->parameters().ToString());
231 RapporMetric
* new_metric
= new RapporMetric(metric_name
, parameters
, cohort_
);
232 metrics_map_
[metric_name
] = new_metric
;
236 } // namespace rappor