Update V8 to version 4.7.1.
[chromium-blink-merge.git] / components / rappor / rappor_service.cc
blobe266232f8ef9354c127bbf284222bf8b22104658
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 // UMA_RAPPOR_TYPE
52 {128 /* Num cohorts */,
53 4 /* 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 UMA_RAPPOR_GROUP /* Recording group */},
60 // SAFEBROWSING_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 SAFEBROWSING_RAPPOR_GROUP /* Recording group */},
69 // ETLD_PLUS_ONE_RAPPOR_TYPE
70 {128 /* Num cohorts */,
71 16 /* Bloom filter size bytes */,
72 2 /* Bloom filter hash count */,
73 rappor::PROBABILITY_50 /* Fake data probability */,
74 rappor::PROBABILITY_50 /* Fake one probability */,
75 rappor::PROBABILITY_75 /* One coin probability */,
76 rappor::PROBABILITY_25 /* Zero coin probability */,
77 UMA_RAPPOR_GROUP /* Recording group */},
80 } // namespace
82 RapporService::RapporService(
83 PrefService* pref_service,
84 const base::Callback<bool(void)> is_incognito_callback)
85 : pref_service_(pref_service),
86 is_incognito_callback_(is_incognito_callback),
87 cohort_(-1),
88 daily_event_(pref_service,
89 prefs::kRapporLastDailySample,
90 kRapporDailyEventHistogram),
91 recording_groups_(0) {
94 RapporService::~RapporService() {
95 STLDeleteValues(&metrics_map_);
98 void RapporService::AddDailyObserver(
99 scoped_ptr<metrics::DailyEvent::Observer> observer) {
100 daily_event_.AddObserver(observer.Pass());
103 void RapporService::Initialize(net::URLRequestContextGetter* request_context) {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 DCHECK(!IsInitialized());
106 const GURL server_url = GetServerUrl();
107 if (!server_url.is_valid()) {
108 DVLOG(1) << server_url.spec() << " is invalid. "
109 << "RapporService not started.";
110 return;
112 DVLOG(1) << "RapporService reporting to " << server_url.spec();
113 InitializeInternal(make_scoped_ptr(new LogUploader(server_url,
114 kMimeType,
115 request_context)),
116 internal::LoadCohort(pref_service_),
117 internal::LoadSecret(pref_service_));
120 void RapporService::Update(int recording_groups, bool may_upload) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 DCHECK(IsInitialized());
123 if (recording_groups_ != recording_groups) {
124 if (recording_groups == 0) {
125 DVLOG(1) << "Rappor service stopped because all groups were disabled.";
126 recording_groups_ = 0;
127 CancelNextLogRotation();
128 } else if (recording_groups_ == 0) {
129 DVLOG(1) << "RapporService started for groups: "
130 << recording_groups;
131 recording_groups_ = recording_groups;
132 ScheduleNextLogRotation(
133 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds));
134 } else {
135 DVLOG(1) << "RapporService recording_groups changed:" << recording_groups;
136 recording_groups_ = recording_groups;
140 DVLOG(1) << "RapporService recording_groups=" << recording_groups_
141 << " may_upload=" << may_upload;
142 if (may_upload) {
143 uploader_->Start();
144 } else {
145 uploader_->Stop();
149 // static
150 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) {
151 internal::RegisterPrefs(registry);
154 void RapporService::InitializeInternal(
155 scoped_ptr<LogUploaderInterface> uploader,
156 int32_t cohort,
157 const std::string& secret) {
158 DCHECK(thread_checker_.CalledOnValidThread());
159 DCHECK(!IsInitialized());
160 DCHECK(secret_.empty());
161 uploader_.swap(uploader);
162 cohort_ = cohort;
163 secret_ = secret;
166 void RapporService::CancelNextLogRotation() {
167 DCHECK(thread_checker_.CalledOnValidThread());
168 STLDeleteValues(&metrics_map_);
169 log_rotation_timer_.Stop();
172 void RapporService::ScheduleNextLogRotation(base::TimeDelta interval) {
173 log_rotation_timer_.Start(FROM_HERE,
174 interval,
175 this,
176 &RapporService::OnLogInterval);
179 void RapporService::OnLogInterval() {
180 DCHECK(thread_checker_.CalledOnValidThread());
181 DCHECK(uploader_);
182 DVLOG(2) << "RapporService::OnLogInterval";
183 daily_event_.CheckInterval();
184 RapporReports reports;
185 if (ExportMetrics(&reports)) {
186 std::string log_text;
187 bool success = reports.SerializeToString(&log_text);
188 DCHECK(success);
189 DVLOG(1) << "RapporService sending a report of "
190 << reports.report_size() << " value(s).";
191 uploader_->QueueLog(log_text);
193 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds));
196 bool RapporService::ExportMetrics(RapporReports* reports) {
197 DCHECK(thread_checker_.CalledOnValidThread());
198 DCHECK_GE(cohort_, 0);
199 reports->set_cohort(cohort_);
201 for (const auto& kv : metrics_map_) {
202 const RapporMetric* metric = kv.second;
203 RapporReports::Report* report = reports->add_report();
204 report->set_name_hash(metrics::HashMetricName(kv.first));
205 ByteVector bytes = metric->GetReport(secret_);
206 report->set_bits(std::string(bytes.begin(), bytes.end()));
207 DVLOG(2) << "Exporting metric " << kv.first;
209 STLDeleteValues(&metrics_map_);
211 sampler_.ExportMetrics(secret_, reports);
213 DVLOG(2) << "Generated a report with " << reports->report_size()
214 << " metrics.";
215 return reports->report_size() > 0;
218 bool RapporService::IsInitialized() const {
219 return cohort_ >= 0;
222 bool RapporService::RecordingAllowed(const RapporParameters& parameters) {
223 // Skip recording in incognito mode.
224 if (is_incognito_callback_.Run()) {
225 DVLOG(2) << "Metric not logged due to incognito mode.";
226 return false;
228 // Skip this metric if its recording_group is not enabled.
229 if (!(recording_groups_ & parameters.recording_group)) {
230 DVLOG(2) << "Metric not logged due to recording_group "
231 << recording_groups_ << " < " << parameters.recording_group;
232 return false;
234 return true;
237 void RapporService::RecordSample(const std::string& metric_name,
238 RapporType type,
239 const std::string& sample) {
240 DCHECK(thread_checker_.CalledOnValidThread());
241 // Ignore the sample if the service hasn't started yet.
242 if (!IsInitialized())
243 return;
244 DCHECK_LT(type, NUM_RAPPOR_TYPES);
245 const RapporParameters& parameters = kRapporParametersForType[type];
246 DVLOG(2) << "Recording sample \"" << sample
247 << "\" for metric \"" << metric_name
248 << "\" of type: " << type;
249 RecordSampleInternal(metric_name, parameters, sample);
252 void RapporService::RecordSampleInternal(const std::string& metric_name,
253 const RapporParameters& parameters,
254 const std::string& sample) {
255 DCHECK(thread_checker_.CalledOnValidThread());
256 DCHECK(IsInitialized());
257 if (!RecordingAllowed(parameters))
258 return;
259 RapporMetric* metric = LookUpMetric(metric_name, parameters);
260 metric->AddSample(sample);
263 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name,
264 const RapporParameters& parameters) {
265 DCHECK(thread_checker_.CalledOnValidThread());
266 DCHECK(IsInitialized());
267 std::map<std::string, RapporMetric*>::const_iterator it =
268 metrics_map_.find(metric_name);
269 if (it != metrics_map_.end()) {
270 RapporMetric* metric = it->second;
271 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString());
272 return metric;
275 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_);
276 metrics_map_[metric_name] = new_metric;
277 return new_metric;
280 scoped_ptr<Sample> RapporService::CreateSample(RapporType type) {
281 DCHECK(thread_checker_.CalledOnValidThread());
282 DCHECK(IsInitialized());
283 return scoped_ptr<Sample>(
284 new Sample(cohort_, kRapporParametersForType[type]));
287 void RapporService::RecordSampleObj(const std::string& metric_name,
288 scoped_ptr<Sample> sample) {
289 DCHECK(thread_checker_.CalledOnValidThread());
290 if (!RecordingAllowed(sample->parameters()))
291 return;
292 DVLOG(1) << "Recording sample of metric \"" << metric_name << "\"";
293 sampler_.AddSample(metric_name, sample.Pass());
296 } // namespace rappor