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 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 */},
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_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.";
112 DVLOG(1) << "RapporService reporting to " << server_url
.spec();
113 InitializeInternal(make_scoped_ptr(new LogUploader(server_url
,
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: "
131 recording_groups_
= recording_groups
;
132 ScheduleNextLogRotation(
133 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds
));
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
;
150 void RapporService::RegisterPrefs(PrefRegistrySimple
* registry
) {
151 internal::RegisterPrefs(registry
);
154 void RapporService::InitializeInternal(
155 scoped_ptr
<LogUploaderInterface
> uploader
,
157 const std::string
& secret
) {
158 DCHECK(thread_checker_
.CalledOnValidThread());
159 DCHECK(!IsInitialized());
160 DCHECK(secret_
.empty());
161 uploader_
.swap(uploader
);
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
,
176 &RapporService::OnLogInterval
);
179 void RapporService::OnLogInterval() {
180 DCHECK(thread_checker_
.CalledOnValidThread());
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
);
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()));
208 STLDeleteValues(&metrics_map_
);
210 sampler_
.ExportMetrics(secret_
, reports
);
212 DVLOG(2) << "Generated a report with " << reports
->report_size()
214 return reports
->report_size() > 0;
217 bool RapporService::IsInitialized() const {
221 bool RapporService::RecordingAllowed(const RapporParameters
& parameters
) {
222 // Skip recording in incognito mode.
223 if (is_incognito_callback_
.Run()) {
224 DVLOG(2) << "Metric not logged due to incognito mode.";
227 // Skip this metric if its recording_group is not enabled.
228 if (!(recording_groups_
& parameters
.recording_group
)) {
229 DVLOG(2) << "Metric not logged due to recording_group "
230 << recording_groups_
<< " < " << parameters
.recording_group
;
236 void RapporService::RecordSample(const std::string
& metric_name
,
238 const std::string
& sample
) {
239 DCHECK(thread_checker_
.CalledOnValidThread());
240 // Ignore the sample if the service hasn't started yet.
241 if (!IsInitialized())
243 DCHECK_LT(type
, NUM_RAPPOR_TYPES
);
244 const RapporParameters
& parameters
= kRapporParametersForType
[type
];
245 DVLOG(2) << "Recording sample \"" << sample
246 << "\" for metric \"" << metric_name
247 << "\" of type: " << type
;
248 RecordSampleInternal(metric_name
, parameters
, sample
);
251 void RapporService::RecordSampleInternal(const std::string
& metric_name
,
252 const RapporParameters
& parameters
,
253 const std::string
& sample
) {
254 DCHECK(thread_checker_
.CalledOnValidThread());
255 DCHECK(IsInitialized());
256 if (!RecordingAllowed(parameters
))
258 RapporMetric
* metric
= LookUpMetric(metric_name
, parameters
);
259 metric
->AddSample(sample
);
262 RapporMetric
* RapporService::LookUpMetric(const std::string
& metric_name
,
263 const RapporParameters
& parameters
) {
264 DCHECK(thread_checker_
.CalledOnValidThread());
265 DCHECK(IsInitialized());
266 std::map
<std::string
, RapporMetric
*>::const_iterator it
=
267 metrics_map_
.find(metric_name
);
268 if (it
!= metrics_map_
.end()) {
269 RapporMetric
* metric
= it
->second
;
270 DCHECK_EQ(parameters
.ToString(), metric
->parameters().ToString());
274 RapporMetric
* new_metric
= new RapporMetric(metric_name
, parameters
, cohort_
);
275 metrics_map_
[metric_name
] = new_metric
;
279 scoped_ptr
<Sample
> RapporService::CreateSample(RapporType type
) {
280 DCHECK(thread_checker_
.CalledOnValidThread());
281 DCHECK(IsInitialized());
282 return scoped_ptr
<Sample
>(
283 new Sample(cohort_
, kRapporParametersForType
[type
]));
286 void RapporService::RecordSampleObj(const std::string
& metric_name
,
287 scoped_ptr
<Sample
> sample
) {
288 DCHECK(thread_checker_
.CalledOnValidThread());
289 if (!RecordingAllowed(sample
->parameters()))
291 sampler_
.AddSample(metric_name
, sample
.Pass());
294 } // namespace rappor