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
32const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType";
33
34// Constants for the RAPPOR rollout field trial.
35const char kRapporRolloutFieldTrialName[] = "RapporRollout";
36
37// Constant for the finch parameter name for the server URL
38const char kRapporRolloutServerUrlParam[] = "ServerUrl";
39
40// Constant for the finch parameter name for the server URL
41const char kRapporRolloutRequireUmaParam[] = "RequireUma";
42
43// The rappor server's URL.
44const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
45
46GURL 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);
57  else
58    return GURL(kDefaultServerUrl);
59}
60
61const 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 */},
70};
71
72}  // namespace
73
74RapporService::RapporService(PrefService* pref_service)
75    : pref_service_(pref_service),
76      cohort_(-1),
77      daily_event_(pref_service,
78                      prefs::kRapporLastDailySample,
79                      kRapporDailyEventHistogram) {
80}
81
82RapporService::~RapporService() {
83  STLDeleteValues(&metrics_map_);
84}
85
86void RapporService::AddDailyObserver(
87    scoped_ptr<metrics::DailyEvent::Observer> observer) {
88  daily_event_.AddObserver(observer.Pass());
89}
90
91void 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.";
97    return;
98  }
99  DVLOG(1) << "RapporService started. Reporting to " << server_url.spec();
100  DCHECK(!uploader_);
101  LoadSecret();
102  LoadCohort();
103  uploader_.reset(new LogUploader(server_url, kMimeType, request_context));
104  log_rotation_timer_.Start(
105      FROM_HERE,
106      base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds),
107      this,
108      &RapporService::OnLogInterval);
109}
110
111void RapporService::OnLogInterval() {
112  DCHECK(uploader_);
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);
119    DCHECK(success);
120    DVLOG(1) << "RapporService sending a report of "
121             << reports.report_size() << " value(s).";
122    uploader_->QueueLog(log_text);
123  }
124  log_rotation_timer_.Start(FROM_HERE,
125                            base::TimeDelta::FromSeconds(kLogIntervalSeconds),
126                            this,
127                            &RapporService::OnLogInterval);
128}
129
130// static
131void 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);
137}
138
139void RapporService::LoadCohort() {
140  DCHECK(!IsInitialized());
141  // Ignore and delete old cohort parameter.
142  pref_service_->ClearPref(prefs::kRapporCohortDeprecated);
143
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)
147    return;
148
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_);
154}
155
156void 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)
162      return;
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
165    // one.
166  }
167
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);
172}
173
174bool RapporService::ExportMetrics(RapporReports* reports) {
175  if (metrics_map_.empty())
176    return false;
177
178  DCHECK_GE(cohort_, 0);
179  reports->set_cohort(cohort_);
180
181  for (std::map<std::string, RapporMetric*>::const_iterator it =
182           metrics_map_.begin();
183       it != metrics_map_.end();
184       ++it) {
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()));
190  }
191  STLDeleteValues(&metrics_map_);
192  return true;
193}
194
195bool RapporService::IsInitialized() const {
196  return cohort_ >= 0;
197}
198
199void RapporService::RecordSample(const std::string& metric_name,
200                                 RapporType type,
201                                 const std::string& sample) {
202  // Ignore the sample if the service hasn't started yet.
203  if (!IsInitialized())
204    return;
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);
210}
211
212void 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);
218}
219
220RapporMetric* 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());
228    return metric;
229  }
230
231  RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
232  metrics_map_[metric_name] = new_metric;
233  return new_metric;
234}
235
236}  // namespace rappor
237