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"
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";
41 std::string server_url
= variations::GetVariationParamValue(
42 kRapporRolloutFieldTrialName
,
43 kRapporRolloutServerUrlParam
);
44 if (!server_url
.empty())
45 return GURL(server_url
);
47 return GURL(kDefaultServerUrl
);
50 const RapporParameters kRapporParametersForType
[NUM_RAPPOR_TYPES
] = {
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 FINE_LEVEL
/* Recording level */},
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 */},
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 FINE_LEVEL
/* Recording level */},
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
),
88 daily_event_(pref_service
,
89 prefs::kRapporLastDailySample
,
90 kRapporDailyEventHistogram
),
91 recording_level_(RECORDING_DISABLED
) {
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(!IsInitialized());
105 const GURL server_url
= GetServerUrl();
106 if (!server_url
.is_valid()) {
107 DVLOG(1) << server_url
.spec() << " is invalid. "
108 << "RapporService not started.";
111 DVLOG(1) << "RapporService reporting to " << server_url
.spec();
112 InitializeInternal(make_scoped_ptr(new LogUploader(server_url
,
115 internal::LoadCohort(pref_service_
),
116 internal::LoadSecret(pref_service_
));
119 void RapporService::Update(RecordingLevel recording_level
, bool may_upload
) {
120 DCHECK(IsInitialized());
121 if (recording_level_
!= recording_level
) {
122 if (recording_level
== RECORDING_DISABLED
) {
123 DVLOG(1) << "Rappor service stopped due to RECORDING_DISABLED.";
124 recording_level_
= RECORDING_DISABLED
;
125 CancelNextLogRotation();
126 } else if (recording_level_
== RECORDING_DISABLED
) {
127 DVLOG(1) << "RapporService started at recording level: "
129 recording_level_
= recording_level
;
130 ScheduleNextLogRotation(
131 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds
));
133 DVLOG(1) << "RapporService recording_level changed:" << recording_level
;
134 recording_level_
= recording_level
;
138 DVLOG(1) << "RapporService recording_level=" << recording_level_
139 << " may_upload=" << may_upload
;
148 void RapporService::RegisterPrefs(PrefRegistrySimple
* registry
) {
149 internal::RegisterPrefs(registry
);
152 void RapporService::InitializeInternal(
153 scoped_ptr
<LogUploaderInterface
> uploader
,
155 const std::string
& secret
) {
156 DCHECK(!IsInitialized());
157 DCHECK(secret_
.empty());
158 uploader_
.swap(uploader
);
163 void RapporService::SetRecordingLevel(RecordingLevel recording_level
) {
164 recording_level_
= recording_level
;
167 void RapporService::CancelNextLogRotation() {
168 STLDeleteValues(&metrics_map_
);
169 log_rotation_timer_
.Stop();
172 void RapporService::ScheduleNextLogRotation(base::TimeDelta interval
) {
173 log_rotation_timer_
.Start(FROM_HERE
,
176 &RapporService::OnLogInterval
);
179 void RapporService::OnLogInterval() {
181 DVLOG(2) << "RapporService::OnLogInterval";
182 daily_event_
.CheckInterval();
183 RapporReports reports
;
184 if (ExportMetrics(&reports
)) {
185 std::string log_text
;
186 bool success
= reports
.SerializeToString(&log_text
);
188 DVLOG(1) << "RapporService sending a report of "
189 << reports
.report_size() << " value(s).";
190 uploader_
->QueueLog(log_text
);
192 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds
));
195 bool RapporService::ExportMetrics(RapporReports
* reports
) {
196 DCHECK_GE(cohort_
, 0);
197 reports
->set_cohort(cohort_
);
199 for (const auto& kv
: metrics_map_
) {
200 const RapporMetric
* metric
= kv
.second
;
201 RapporReports::Report
* report
= reports
->add_report();
202 report
->set_name_hash(metrics::HashMetricName(kv
.first
));
203 ByteVector bytes
= metric
->GetReport(secret_
);
204 report
->set_bits(std::string(bytes
.begin(), bytes
.end()));
206 STLDeleteValues(&metrics_map_
);
208 sampler_
.ExportMetrics(secret_
, reports
);
210 DVLOG(2) << "Generated a report with " << reports
->report_size()
212 return reports
->report_size() > 0;
215 bool RapporService::IsInitialized() const {
219 void RapporService::RecordSample(const std::string
& metric_name
,
221 const std::string
& sample
) {
222 // Ignore the sample if the service hasn't started yet.
223 if (!IsInitialized())
225 DCHECK_LT(type
, NUM_RAPPOR_TYPES
);
226 const RapporParameters
& parameters
= kRapporParametersForType
[type
];
227 DVLOG(2) << "Recording sample \"" << sample
228 << "\" for metric \"" << metric_name
229 << "\" of type: " << type
;
230 RecordSampleInternal(metric_name
, parameters
, sample
);
233 void RapporService::RecordSampleInternal(const std::string
& metric_name
,
234 const RapporParameters
& parameters
,
235 const std::string
& sample
) {
236 DCHECK(IsInitialized());
237 if (is_incognito_callback_
.Run()) {
238 DVLOG(2) << "Metric not logged due to incognito mode.";
241 // Skip this metric if its reporting level is less than the enabled
243 if (recording_level_
< parameters
.recording_level
) {
244 DVLOG(2) << "Metric not logged due to recording_level "
245 << recording_level_
<< " < " << parameters
.recording_level
;
248 RapporMetric
* metric
= LookUpMetric(metric_name
, parameters
);
249 metric
->AddSample(sample
);
252 RapporMetric
* RapporService::LookUpMetric(const std::string
& metric_name
,
253 const RapporParameters
& parameters
) {
254 DCHECK(IsInitialized());
255 std::map
<std::string
, RapporMetric
*>::const_iterator it
=
256 metrics_map_
.find(metric_name
);
257 if (it
!= metrics_map_
.end()) {
258 RapporMetric
* metric
= it
->second
;
259 DCHECK_EQ(parameters
.ToString(), metric
->parameters().ToString());
263 RapporMetric
* new_metric
= new RapporMetric(metric_name
, parameters
, cohort_
);
264 metrics_map_
[metric_name
] = new_metric
;
268 scoped_ptr
<Sample
> RapporService::CreateSample(RapporType type
) {
269 DCHECK(IsInitialized());
270 return scoped_ptr
<Sample
>(
271 new Sample(cohort_
, kRapporParametersForType
[type
]));
274 void RapporService::RecordSampleObj(const std::string
& metric_name
,
275 scoped_ptr
<Sample
> sample
) {
276 if (is_incognito_callback_
.Run()) {
277 DVLOG(2) << "Metric not logged due to incognito mode.";
280 // Skip this metric if its reporting level is less than the enabled
282 if (recording_level_
< sample
->parameters().recording_level
) {
283 DVLOG(2) << "Metric not logged due to recording_level "
284 << recording_level_
<< " < "
285 << sample
->parameters().recording_level
;
288 sampler_
.AddSample(metric_name
, sample
.Pass());
291 } // namespace rappor