Invalidate scans when the host volume is unmounted.
[chromium-blink-merge.git] / components / rappor / rappor_service.cc
blob2519326beabdd98f3a3862e39fc95ed56cba8e15
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"
21 namespace rappor {
23 namespace {
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";
43 GURL GetServerUrl() {
44 std::string server_url = variations::GetVariationParamValue(
45 kRapporRolloutFieldTrialName,
46 kRapporRolloutServerUrlParam);
47 if (!server_url.empty())
48 return GURL(server_url);
49 else
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 /* Recording level */},
63 // COARSE_RAPPOR_TYPE
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 /* Recording level */},
74 } // namespace
76 RapporService::RapporService(
77 PrefService* pref_service,
78 const base::Callback<bool(void)> is_incognito_callback)
79 : pref_service_(pref_service),
80 is_incognito_callback_(is_incognito_callback),
81 cohort_(-1),
82 daily_event_(pref_service,
83 prefs::kRapporLastDailySample,
84 kRapporDailyEventHistogram),
85 recording_level_(RECORDING_DISABLED) {
88 RapporService::~RapporService() {
89 STLDeleteValues(&metrics_map_);
92 void RapporService::AddDailyObserver(
93 scoped_ptr<metrics::DailyEvent::Observer> observer) {
94 daily_event_.AddObserver(observer.Pass());
97 void RapporService::Initialize(net::URLRequestContextGetter* request_context) {
98 DCHECK(!IsInitialized());
99 const GURL server_url = GetServerUrl();
100 if (!server_url.is_valid()) {
101 DVLOG(1) << server_url.spec() << " is invalid. "
102 << "RapporService not started.";
103 return;
105 DVLOG(1) << "RapporService reporting to " << server_url.spec();
106 InitializeInternal(make_scoped_ptr(new LogUploader(server_url,
107 kMimeType,
108 request_context)),
109 LoadCohort(),
110 LoadSecret());
113 void RapporService::Update(RecordingLevel recording_level, bool may_upload) {
114 DCHECK(IsInitialized());
115 if (recording_level_ != recording_level) {
116 if (recording_level == RECORDING_DISABLED) {
117 DVLOG(1) << "Rappor service stopped due to RECORDING_DISABLED.";
118 recording_level_ = RECORDING_DISABLED;
119 CancelNextLogRotation();
120 } else if (recording_level_ == RECORDING_DISABLED) {
121 DVLOG(1) << "RapporService started at recording level: "
122 << recording_level;
123 recording_level_ = recording_level;
124 ScheduleNextLogRotation(
125 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds));
126 } else {
127 DVLOG(1) << "RapporService recording_level changed:" << recording_level;
128 recording_level_ = recording_level;
132 DVLOG(1) << "RapporService may_upload=" << may_upload;
133 if (may_upload) {
134 uploader_->Start();
135 } else {
136 uploader_->Stop();
140 void RapporService::InitializeInternal(
141 scoped_ptr<LogUploaderInterface> uploader,
142 int32_t cohort,
143 const std::string& secret) {
144 DCHECK(!IsInitialized());
145 DCHECK(secret_.empty());
146 uploader_.swap(uploader);
147 cohort_ = cohort;
148 secret_ = secret;
151 void RapporService::SetRecordingLevel(RecordingLevel recording_level) {
152 recording_level_ = recording_level;
155 void RapporService::CancelNextLogRotation() {
156 STLDeleteValues(&metrics_map_);
157 log_rotation_timer_.Stop();
160 void RapporService::ScheduleNextLogRotation(base::TimeDelta interval) {
161 log_rotation_timer_.Start(FROM_HERE,
162 interval,
163 this,
164 &RapporService::OnLogInterval);
167 void RapporService::OnLogInterval() {
168 DCHECK(uploader_);
169 DVLOG(2) << "RapporService::OnLogInterval";
170 daily_event_.CheckInterval();
171 RapporReports reports;
172 if (ExportMetrics(&reports)) {
173 std::string log_text;
174 bool success = reports.SerializeToString(&log_text);
175 DCHECK(success);
176 DVLOG(1) << "RapporService sending a report of "
177 << reports.report_size() << " value(s).";
178 uploader_->QueueLog(log_text);
180 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds));
183 // static
184 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
185 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
186 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
187 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
188 metrics::DailyEvent::RegisterPref(registry, prefs::kRapporLastDailySample);
191 int32_t RapporService::LoadCohort() {
192 // Ignore and delete old cohort parameter.
193 pref_service_->ClearPref(prefs::kRapporCohortDeprecated);
195 int32_t cohort = pref_service_->GetInteger(prefs::kRapporCohortSeed);
196 // If the user is already assigned to a valid cohort, we're done.
197 if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts)
198 return cohort;
200 // This is the first time the client has started the service (or their
201 // preferences were corrupted). Randomly assign them to a cohort.
202 cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
203 DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
204 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort);
205 return cohort;
208 std::string RapporService::LoadSecret() {
209 std::string secret;
210 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret);
211 if (!secret_base64.empty()) {
212 bool decoded = base::Base64Decode(secret_base64, &secret);
213 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize)
214 return secret;
215 // If the preference fails to decode, or is the wrong size, it must be
216 // corrupt, so continue as though it didn't exist yet and generate a new
217 // one.
220 DVLOG(2) << "Generated a new Rappor secret.";
221 secret = HmacByteVectorGenerator::GenerateEntropyInput();
222 base::Base64Encode(secret, &secret_base64);
223 pref_service_->SetString(prefs::kRapporSecret, secret_base64);
224 return secret;
227 bool RapporService::ExportMetrics(RapporReports* reports) {
228 if (metrics_map_.empty()) {
229 DVLOG(2) << "metrics_map_ is empty.";
230 return false;
233 DCHECK_GE(cohort_, 0);
234 reports->set_cohort(cohort_);
236 for (std::map<std::string, RapporMetric*>::const_iterator it =
237 metrics_map_.begin();
238 it != metrics_map_.end();
239 ++it) {
240 const RapporMetric* metric = it->second;
241 RapporReports::Report* report = reports->add_report();
242 report->set_name_hash(metrics::HashMetricName(it->first));
243 ByteVector bytes = metric->GetReport(secret_);
244 report->set_bits(std::string(bytes.begin(), bytes.end()));
246 STLDeleteValues(&metrics_map_);
247 return true;
250 bool RapporService::IsInitialized() const {
251 return cohort_ >= 0;
254 void RapporService::RecordSample(const std::string& metric_name,
255 RapporType type,
256 const std::string& sample) {
257 // Ignore the sample if the service hasn't started yet.
258 if (!IsInitialized())
259 return;
260 DCHECK_LT(type, NUM_RAPPOR_TYPES);
261 const RapporParameters& parameters = kRapporParametersForType[type];
262 DVLOG(2) << "Recording sample \"" << sample
263 << "\" for metric \"" << metric_name
264 << "\" of type: " << type;
265 RecordSampleInternal(metric_name, parameters, sample);
268 void RapporService::RecordSampleInternal(const std::string& metric_name,
269 const RapporParameters& parameters,
270 const std::string& sample) {
271 DCHECK(IsInitialized());
272 if (is_incognito_callback_.Run()) {
273 DVLOG(2) << "Metric not logged due to incognito mode.";
274 return;
276 // Skip this metric if it's reporting level is less than the enabled
277 // reporting level.
278 if (recording_level_ < parameters.recording_level) {
279 DVLOG(2) << "Metric not logged due to recording_level "
280 << recording_level_ << " < " << parameters.recording_level;
281 return;
283 RapporMetric* metric = LookUpMetric(metric_name, parameters);
284 metric->AddSample(sample);
287 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
288 const RapporParameters& parameters) {
289 DCHECK(IsInitialized());
290 std::map<std::string, RapporMetric*>::const_iterator it =
291 metrics_map_.find(metric_name);
292 if (it != metrics_map_.end()) {
293 RapporMetric* metric = it->second;
294 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
295 return metric;
298 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
299 metrics_map_[metric_name] = new_metric;
300 return new_metric;
303 } // namespace rappor