Clear "system" proxy preference from persistent preferences.
[chromium-blink-merge.git] / components / rappor / rappor_service.cc
blob7fc4fce7fb4e86f94b330d1e6a94df7927960137
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/metrics/field_trial.h"
8 #include "base/stl_util.h"
9 #include "base/time/time.h"
10 #include "components/metrics/metrics_hashes.h"
11 #include "components/rappor/log_uploader.h"
12 #include "components/rappor/proto/rappor_metric.pb.h"
13 #include "components/rappor/rappor_metric.h"
14 #include "components/rappor/rappor_pref_names.h"
15 #include "components/rappor/rappor_prefs.h"
16 #include "components/variations/variations_associated_data.h"
18 namespace rappor {
20 namespace {
22 // Seconds before the initial log is generated.
23 const int kInitialLogIntervalSeconds = 15;
24 // Interval between ongoing logs.
25 const int kLogIntervalSeconds = 30 * 60;
27 const char kMimeType[] = "application/vnd.chrome.rappor";
29 const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType";
31 // Constants for the RAPPOR rollout field trial.
32 const char kRapporRolloutFieldTrialName[] = "RapporRollout";
34 // Constant for the finch parameter name for the server URL
35 const char kRapporRolloutServerUrlParam[] = "ServerUrl";
37 // The rappor server's URL.
38 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor";
40 GURL GetServerUrl() {
41 std::string server_url = variations::GetVariationParamValue(
42 kRapporRolloutFieldTrialName,
43 kRapporRolloutServerUrlParam);
44 if (!server_url.empty())
45 return GURL(server_url);
46 else
47 return GURL(kDefaultServerUrl);
50 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = {
51 // ETLD_PLUS_ONE_RAPPOR_TYPE
52 {128 /* Num cohorts */,
53 16 /* Bloom filter size bytes */,
54 2 /* Bloom filter hash count */,
55 rappor::PROBABILITY_50 /* Fake data probability */,
56 rappor::PROBABILITY_50 /* Fake one probability */,
57 rappor::PROBABILITY_75 /* One coin probability */,
58 rappor::PROBABILITY_25 /* Zero coin probability */,
59 FINE_LEVEL /* Recording level */},
60 // COARSE_RAPPOR_TYPE
61 {128 /* Num cohorts */,
62 1 /* 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 COARSE_LEVEL /* Recording level */},
71 } // namespace
73 RapporService::RapporService(
74 PrefService* pref_service,
75 const base::Callback<bool(void)> is_incognito_callback)
76 : pref_service_(pref_service),
77 is_incognito_callback_(is_incognito_callback),
78 cohort_(-1),
79 daily_event_(pref_service,
80 prefs::kRapporLastDailySample,
81 kRapporDailyEventHistogram),
82 recording_level_(RECORDING_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::Initialize(net::URLRequestContextGetter* request_context) {
95 DCHECK(!IsInitialized());
96 const GURL server_url = GetServerUrl();
97 if (!server_url.is_valid()) {
98 DVLOG(1) << server_url.spec() << " is invalid. "
99 << "RapporService not started.";
100 return;
102 DVLOG(1) << "RapporService reporting to " << server_url.spec();
103 InitializeInternal(make_scoped_ptr(new LogUploader(server_url,
104 kMimeType,
105 request_context)),
106 internal::LoadCohort(pref_service_),
107 internal::LoadSecret(pref_service_));
110 void RapporService::Update(RecordingLevel recording_level, bool may_upload) {
111 DCHECK(IsInitialized());
112 if (recording_level_ != recording_level) {
113 if (recording_level == RECORDING_DISABLED) {
114 DVLOG(1) << "Rappor service stopped due to RECORDING_DISABLED.";
115 recording_level_ = RECORDING_DISABLED;
116 CancelNextLogRotation();
117 } else if (recording_level_ == RECORDING_DISABLED) {
118 DVLOG(1) << "RapporService started at recording level: "
119 << recording_level;
120 recording_level_ = recording_level;
121 ScheduleNextLogRotation(
122 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds));
123 } else {
124 DVLOG(1) << "RapporService recording_level changed:" << recording_level;
125 recording_level_ = recording_level;
129 DVLOG(1) << "RapporService recording_level=" << recording_level_
130 << " may_upload=" << may_upload;
131 if (may_upload) {
132 uploader_->Start();
133 } else {
134 uploader_->Stop();
138 // static
139 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
140 internal::RegisterPrefs(registry);
143 void RapporService::InitializeInternal(
144 scoped_ptr<LogUploaderInterface> uploader,
145 int32_t cohort,
146 const std::string& secret) {
147 DCHECK(!IsInitialized());
148 DCHECK(secret_.empty());
149 uploader_.swap(uploader);
150 cohort_ = cohort;
151 secret_ = secret;
154 void RapporService::SetRecordingLevel(RecordingLevel recording_level) {
155 recording_level_ = recording_level;
158 void RapporService::CancelNextLogRotation() {
159 STLDeleteValues(&metrics_map_);
160 log_rotation_timer_.Stop();
163 void RapporService::ScheduleNextLogRotation(base::TimeDelta interval) {
164 log_rotation_timer_.Start(FROM_HERE,
165 interval,
166 this,
167 &RapporService::OnLogInterval);
170 void RapporService::OnLogInterval() {
171 DCHECK(uploader_);
172 DVLOG(2) << "RapporService::OnLogInterval";
173 daily_event_.CheckInterval();
174 RapporReports reports;
175 if (ExportMetrics(&reports)) {
176 std::string log_text;
177 bool success = reports.SerializeToString(&log_text);
178 DCHECK(success);
179 DVLOG(1) << "RapporService sending a report of "
180 << reports.report_size() << " value(s).";
181 uploader_->QueueLog(log_text);
183 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds));
186 bool RapporService::ExportMetrics(RapporReports* reports) {
187 if (metrics_map_.empty()) {
188 DVLOG(2) << "metrics_map_ is empty.";
189 return false;
192 DCHECK_GE(cohort_, 0);
193 reports->set_cohort(cohort_);
195 for (std::map<std::string, RapporMetric*>::const_iterator it =
196 metrics_map_.begin();
197 it != metrics_map_.end();
198 ++it) {
199 const RapporMetric* metric = it->second;
200 RapporReports::Report* report = reports->add_report();
201 report->set_name_hash(metrics::HashMetricName(it->first));
202 ByteVector bytes = metric->GetReport(secret_);
203 report->set_bits(std::string(bytes.begin(), bytes.end()));
205 STLDeleteValues(&metrics_map_);
206 return true;
209 bool RapporService::IsInitialized() const {
210 return cohort_ >= 0;
213 void RapporService::RecordSample(const std::string& metric_name,
214 RapporType type,
215 const std::string& sample) {
216 // Ignore the sample if the service hasn't started yet.
217 if (!IsInitialized())
218 return;
219 DCHECK_LT(type, NUM_RAPPOR_TYPES);
220 const RapporParameters& parameters = kRapporParametersForType[type];
221 DVLOG(2) << "Recording sample \"" << sample
222 << "\" for metric \"" << metric_name
223 << "\" of type: " << type;
224 RecordSampleInternal(metric_name, parameters, sample);
227 void RapporService::RecordSampleInternal(const std::string& metric_name,
228 const RapporParameters& parameters,
229 const std::string& sample) {
230 DCHECK(IsInitialized());
231 if (is_incognito_callback_.Run()) {
232 DVLOG(2) << "Metric not logged due to incognito mode.";
233 return;
235 // Skip this metric if it's reporting level is less than the enabled
236 // reporting level.
237 if (recording_level_ < parameters.recording_level) {
238 DVLOG(2) << "Metric not logged due to recording_level "
239 << recording_level_ << " < " << parameters.recording_level;
240 return;
242 RapporMetric* metric = LookUpMetric(metric_name, parameters);
243 metric->AddSample(sample);
246 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
247 const RapporParameters& parameters) {
248 DCHECK(IsInitialized());
249 std::map<std::string, RapporMetric*>::const_iterator it =
250 metrics_map_.find(metric_name);
251 if (it != metrics_map_.end()) {
252 RapporMetric* metric = it->second;
253 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
254 return metric;
257 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
258 metrics_map_[metric_name] = new_metric;
259 return new_metric;
262 } // namespace rappor