rappor_service.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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.
4
5#include "components/rappor/rappor_service.h"
6
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"
20
21namespace rappor {
22
23namespace {
24
25// Seconds before the initial log is generated.
26const int kInitialLogIntervalSeconds = 15;
27// Interval between ongoing logs.
28const int kLogIntervalSeconds = 30 * 60;
29
30const char kMimeType[] = "application/vnd.chrome.rappor";
31
32// Constants for the RAPPOR rollout field trial.
33const char kRapporRolloutFieldTrialName[] = "RapporRollout";
34
35// Constant for the finch parameter name for the server URL
36const char kRapporRolloutServerUrlParam[] = "ServerUrl";
37
38// Constant for the finch parameter name for the server URL
39const char kRapporRolloutRequireUmaParam[] = "RequireUma";
40
41// The rappor server's URL.
42const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
43
44GURL GetServerUrl(bool metrics_enabled) {
45  bool require_uma = variations::GetVariationParamValue(
46      kRapporRolloutFieldTrialName,
47      kRapporRolloutRequireUmaParam) != "False";
48  if (!metrics_enabled && require_uma)
49    return GURL();  // Invalid URL disables Rappor.
50  std::string server_url = variations::GetVariationParamValue(
51      kRapporRolloutFieldTrialName,
52      kRapporRolloutServerUrlParam);
53  if (!server_url.empty())
54    return GURL(server_url);
55  else
56    return GURL(kDefaultServerUrl);
57}
58
59const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
60    // ETLD_PLUS_ONE_RAPPOR_TYPE
61    {128 /* Num cohorts */,
62     16 /* Bloom filter size bytes */,
63     2 /* Bloom filter hash count */,
64     rappor::PROBABILITY_50 /* Fake data probability */,
65     rappor::PROBABILITY_50 /* Fake one probability */,
66     rappor::PROBABILITY_75 /* One coin probability */,
67     rappor::PROBABILITY_25 /* Zero coin probability */},
68};
69
70}  // namespace
71
72RapporService::RapporService() : cohort_(-1) {}
73
74RapporService::~RapporService() {
75  STLDeleteValues(&metrics_map_);
76}
77
78void RapporService::Start(PrefService* pref_service,
79                          net::URLRequestContextGetter* request_context,
80                          bool metrics_enabled) {
81  const GURL server_url = GetServerUrl(metrics_enabled);
82  if (!server_url.is_valid()) {
83    DVLOG(1) << server_url.spec() << " is invalid. "
84             << "RapporService not started.";
85    return;
86  }
87  DVLOG(1) << "RapporService started. Reporting to " << server_url.spec();
88  DCHECK(!uploader_);
89  LoadSecret(pref_service);
90  LoadCohort(pref_service);
91  uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
92  log_rotation_timer_.Start(
93      FROM_HERE,
94      base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
95      this,
96      &RapporService::OnLogInterval);
97}
98
99void RapporService::OnLogInterval() {
100  DCHECK(uploader_);
101  DVLOG(2) << "RapporService::OnLogInterval";
102  RapporReports reports;
103  if (ExportMetrics(&reports)) {
104    std::string log_text;
105    bool success = reports.SerializeToString(&log_text);
106    DCHECK(success);
107    DVLOG(1) << "RapporService sending a report of "
108             << reports.report_size() << " value(s).";
109    uploader_->QueueLog(log_text);
110  }
111  log_rotation_timer_.Start(FROM_HERE,
112                            base::TimeDelta::FromSeconds(kLogIntervalSeconds),
113                            this,
114                            &RapporService::OnLogInterval);
115}
116
117// static
118void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
119  registry->RegisterStringPref(prefs::kRapporSecret, std::string());
120  registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
121  registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
122}
123
124void RapporService::LoadCohort(PrefService* pref_service) {
125  DCHECK(!IsInitialized());
126  // Ignore and delete old cohort parameter.
127  pref_service->ClearPref(prefs::kRapporCohortDeprecated);
128
129  cohort_ = pref_service->GetInteger(prefs::kRapporCohortSeed);
130  // If the user is already assigned to a valid cohort, we're done.
131  if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts)
132    return;
133
134  // This is the first time the client has started the service (or their
135  // preferences were corrupted).  Randomly assign them to a cohort.
136  cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts);
137  DVLOG(2) << "Selected a new Rappor cohort: " << cohort_;
138  pref_service->SetInteger(prefs::kRapporCohortSeed, cohort_);
139}
140
141void RapporService::LoadSecret(PrefService* pref_service) {
142  DCHECK(secret_.empty());
143  std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret);
144  if (!secret_base64.empty()) {
145    bool decoded = base::Base64Decode(secret_base64, &secret_);
146    if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
147      return;
148    // If the preference fails to decode, or is the wrong size, it must be
149    // corrupt, so continue as though it didn't exist yet and generate a new
150    // one.
151  }
152
153  DVLOG(2) << "Generated a new Rappor secret.";
154  secret_ = HmacByteVectorGenerator::GenerateEntropyInput();
155  base::Base64Encode(secret_, &secret_base64);
156  pref_service->SetString(prefs::kRapporSecret, secret_base64);
157}
158
159bool RapporService::ExportMetrics(RapporReports* reports) {
160  if (metrics_map_.empty())
161    return false;
162
163  DCHECK_GE(cohort_, 0);
164  reports->set_cohort(cohort_);
165
166  for (std::map<std::string, RapporMetric*>::const_iterator it =
167           metrics_map_.begin();
168       it != metrics_map_.end();
169       ++it) {
170    const RapporMetric* metric = it->second;
171    RapporReports::Report* report = reports->add_report();
172    report->set_name_hash(metrics::HashMetricName(it->first));
173    ByteVector bytes = metric->GetReport(secret_);
174    report->set_bits(std::string(bytes.begin(), bytes.end()));
175  }
176  STLDeleteValues(&metrics_map_);
177  return true;
178}
179
180bool RapporService::IsInitialized() const {
181  return cohort_ >= 0;
182}
183
184void RapporService::RecordSample(const std::string& metric_name,
185                                 RapporType type,
186                                 const std::string& sample) {
187  // Ignore the sample if the service hasn't started yet.
188  if (!IsInitialized())
189    return;
190  DCHECK_LT(type, NUM_RAPPOR_TYPES);
191  DVLOG(2) << "Recording sample \"" << sample
192           << "\" for metric \"" << metric_name
193           << "\" of type: " << type;
194  RecordSampleInternal(metric_name, kRapporParametersForType[type], sample);
195}
196
197void RapporService::RecordSampleInternal(const std::string& metric_name,
198                                         const RapporParameters& parameters,
199                                         const std::string& sample) {
200  DCHECK(IsInitialized());
201  RapporMetric* metric = LookUpMetric(metric_name, parameters);
202  metric->AddSample(sample);
203}
204
205RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
206                                          const RapporParameters& parameters) {
207  DCHECK(IsInitialized());
208  std::map<std::string, RapporMetric*>::const_iterator it =
209      metrics_map_.find(metric_name);
210  if (it != metrics_map_.end()) {
211    RapporMetric* metric = it->second;
212    DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
213    return metric;
214  }
215
216  RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
217  metrics_map_[metric_name] = new_metric;
218  return new_metric;
219}
220
221}  // namespace rappor
222