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 // The rappor server's URL.
41 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
44 std::string server_url = variations::GetVariationParamValue(
45 kRapporRolloutFieldTrialName,
46 kRapporRolloutServerUrlParam);
47 if (!server_url.empty())
48 return GURL(server_url);
50 return GURL(kDefaultServerUrl);
53 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
54 // ETLD_PLUS_ONE_RAPPOR_TYPE
55 {128 /* Num cohorts */,
56 16 /* Bloom filter size bytes */,
57 2 /* Bloom filter hash count */,
58 rappor::PROBABILITY_50 /* Fake data probability */,
59 rappor::PROBABILITY_50 /* Fake one probability */,
60 rappor::PROBABILITY_75 /* One coin probability */,
61 rappor::PROBABILITY_25 /* Zero coin probability */,
62 FINE_LEVEL /* Reporting level */},
64 {128 /* Num cohorts */,
65 1 /* Bloom filter size bytes */,
66 2 /* Bloom filter hash count */,
67 rappor::PROBABILITY_50 /* Fake data probability */,
68 rappor::PROBABILITY_50 /* Fake one probability */,
69 rappor::PROBABILITY_75 /* One coin probability */,
70 rappor::PROBABILITY_25 /* Zero coin probability */,
71 COARSE_LEVEL /* Reporting level */},
76 RapporService::RapporService(PrefService* pref_service)
77 : pref_service_(pref_service),
79 daily_event_(pref_service,
80 prefs::kRapporLastDailySample,
81 kRapporDailyEventHistogram),
82 reporting_level_(REPORTING_DISABLED) {
85 RapporService::~RapporService() {
86 STLDeleteValues(&metrics_map_);
89 void RapporService::AddDailyObserver(
90 scoped_ptr<metrics::DailyEvent::Observer> observer) {
91 daily_event_.AddObserver(observer.Pass());
94 void RapporService::Start(net::URLRequestContextGetter* request_context,
95 bool metrics_enabled) {
96 const GURL server_url = GetServerUrl();
97 if (!server_url.is_valid()) {
98 DVLOG(1) << server_url.spec() << " is invalid. "
99 << "RapporService not started.";
102 // TODO(holte): Consider moving this logic once we've determined the
103 // conditions for COARSE metrics.
104 ReportingLevel reporting_level = metrics_enabled ?
105 FINE_LEVEL : REPORTING_DISABLED;
106 DVLOG(1) << "RapporService reporting_level_? " << reporting_level;
107 if (reporting_level <= REPORTING_DISABLED)
109 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec();
111 Initialize(LoadCohort(), LoadSecret(), reporting_level);
112 uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
113 log_rotation_timer_.Start(
115 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
117 &RapporService::OnLogInterval);
120 void RapporService::Initialize(int32_t cohort,
121 const std::string& secret,
122 const ReportingLevel& reporting_level) {
123 DCHECK(!IsInitialized());
124 DCHECK(secret_.empty());
127 reporting_level_ = reporting_level;
130 void RapporService::OnLogInterval() {
132 DVLOG(2) << "RapporService::OnLogInterval";
133 daily_event_.CheckInterval();
134 RapporReports reports;
135 if (ExportMetrics(&reports)) {
136 std::string log_text;
137 bool success = reports.SerializeToString(&log_text);
139 DVLOG(1) << "RapporService sending a report of "
140 << reports.report_size() << " value(s).";
141 uploader_->QueueLog(log_text);
143 log_rotation_timer_.Start(FROM_HERE,
144 base::TimeDelta::FromSeconds(kLogIntervalSeconds),
146 &RapporService::OnLogInterval);
150 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
151 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
152 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
153 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
154 metrics::DailyEvent::RegisterPref(registry,
155 prefs::kRapporLastDailySample);
158 int32_t RapporService::LoadCohort() {
159 // Ignore and delete old cohort parameter.
160 pref_service_->ClearPref(prefs::kRapporCohortDeprecated);
162 int32_t cohort = pref_service_->GetInteger(prefs::kRapporCohortSeed);
163 // If the user is already assigned to a valid cohort, we're done.
164 if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts)
167 // This is the first time the client has started the service (or their
168 // preferences were corrupted). Randomly assign them to a cohort.
169 cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
170 DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
171 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort);
175 std::string RapporService::LoadSecret() {
177 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret);
178 if (!secret_base64.empty()) {
179 bool decoded = base::Base64Decode(secret_base64, &secret);
180 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
182 // If the preference fails to decode, or is the wrong size, it must be
183 // corrupt, so continue as though it didn't exist yet and generate a new
187 DVLOG(2) << "Generated a new Rappor secret.";
188 secret = HmacByteVectorGenerator::GenerateEntropyInput();
189 base::Base64Encode(secret, &secret_base64);
190 pref_service_->SetString(prefs::kRapporSecret, secret_base64);
194 bool RapporService::ExportMetrics(RapporReports* reports) {
195 if (metrics_map_.empty()) {
196 DVLOG(2) << "metrics_map_ is empty.";
200 DCHECK_GE(cohort_, 0);
201 reports->set_cohort(cohort_);
203 for (std::map<std::string, RapporMetric*>::const_iterator it =
204 metrics_map_.begin();
205 it != metrics_map_.end();
207 const RapporMetric* metric = it->second;
208 RapporReports::Report* report = reports->add_report();
209 report->set_name_hash(metrics::HashMetricName(it->first));
210 ByteVector bytes = metric->GetReport(secret_);
211 report->set_bits(std::string(bytes.begin(), bytes.end()));
213 STLDeleteValues(&metrics_map_);
217 bool RapporService::IsInitialized() const {
221 void RapporService::RecordSample(const std::string& metric_name,
223 const std::string& sample) {
224 // Ignore the sample if the service hasn't started yet.
225 if (!IsInitialized())
227 DCHECK_LT(type, NUM_RAPPOR_TYPES);
228 const RapporParameters& parameters = kRapporParametersForType[type];
229 DVLOG(2) << "Recording sample \"" << sample
230 << "\" for metric \"" << metric_name
231 << "\" of type: " << type;
232 RecordSampleInternal(metric_name, parameters, sample);
235 void RapporService::RecordSampleInternal(const std::string& metric_name,
236 const RapporParameters& parameters,
237 const std::string& sample) {
238 DCHECK(IsInitialized());
239 // Skip this metric if it's reporting level is less than the enabled
241 if (reporting_level_ < parameters.reporting_level) {
242 DVLOG(2) << "Metric not logged due to reporting_level "
243 << reporting_level_ << " < " << parameters.reporting_level;
246 RapporMetric* metric = LookUpMetric(metric_name, parameters);
247 metric->AddSample(sample);
250 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
251 const RapporParameters& parameters) {
252 DCHECK(IsInitialized());
253 std::map<std::string, RapporMetric*>::const_iterator it =
254 metrics_map_.find(metric_name);
255 if (it != metrics_map_.end()) {
256 RapporMetric* metric = it->second;
257 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
261 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
262 metrics_map_[metric_name] = new_metric;
266 } // namespace rappor