OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/rappor_service.h" | 5 #include "components/rappor/rappor_service.h" |
6 | 6 |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "components/metrics/metrics_hashes.h" | 10 #include "components/metrics/metrics_hashes.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 std::string server_url = variations::GetVariationParamValue( | 41 std::string server_url = variations::GetVariationParamValue( |
42 kRapporRolloutFieldTrialName, | 42 kRapporRolloutFieldTrialName, |
43 kRapporRolloutServerUrlParam); | 43 kRapporRolloutServerUrlParam); |
44 if (!server_url.empty()) | 44 if (!server_url.empty()) |
45 return GURL(server_url); | 45 return GURL(server_url); |
46 else | 46 else |
47 return GURL(kDefaultServerUrl); | 47 return GURL(kDefaultServerUrl); |
48 } | 48 } |
49 | 49 |
50 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { | 50 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { |
51 // ETLD_PLUS_ONE_RAPPOR_TYPE | 51 // UMA_STRING_RAPPOR_TYPE |
52 {128 /* Num cohorts */, | 52 {128 /* Num cohorts */, |
53 16 /* Bloom filter size bytes */, | 53 16 /* Bloom filter size bytes */, |
54 2 /* Bloom filter hash count */, | 54 2 /* Bloom filter hash count */, |
| 55 0, /* Flag bytes */ |
55 rappor::PROBABILITY_50 /* Fake data probability */, | 56 rappor::PROBABILITY_50 /* Fake data probability */, |
56 rappor::PROBABILITY_50 /* Fake one probability */, | 57 rappor::PROBABILITY_50 /* Fake one probability */, |
57 rappor::PROBABILITY_75 /* One coin probability */, | 58 rappor::PROBABILITY_75 /* One coin probability */, |
58 rappor::PROBABILITY_25 /* Zero coin probability */, | 59 rappor::PROBABILITY_25 /* Zero coin probability */, |
59 FINE_LEVEL /* Recording level */}, | 60 FINE_LEVEL /* Recording level */}, |
60 // COARSE_RAPPOR_TYPE | 61 // UMA_STRING_AND_FLAGS_RAPPOR_TYPE |
| 62 {128 /* Num cohorts */, |
| 63 4 /* Bloom filter size bytes */, |
| 64 2 /* Bloom filter hash count */, |
| 65 4, /* Flag bytes */ |
| 66 rappor::PROBABILITY_50 /* Fake data probability */, |
| 67 rappor::PROBABILITY_50 /* Fake one probability */, |
| 68 rappor::PROBABILITY_75 /* One coin probability */, |
| 69 rappor::PROBABILITY_25 /* Zero coin probability */, |
| 70 FINE_LEVEL /* Recording level */}, |
| 71 // SB_STRING_RAPPOR_TYPE |
61 {128 /* Num cohorts */, | 72 {128 /* Num cohorts */, |
62 1 /* Bloom filter size bytes */, | 73 1 /* Bloom filter size bytes */, |
63 2 /* Bloom filter hash count */, | 74 2 /* Bloom filter hash count */, |
| 75 0, /* Flag bytes */ |
64 rappor::PROBABILITY_50 /* Fake data probability */, | 76 rappor::PROBABILITY_50 /* Fake data probability */, |
65 rappor::PROBABILITY_50 /* Fake one probability */, | 77 rappor::PROBABILITY_50 /* Fake one probability */, |
66 rappor::PROBABILITY_75 /* One coin probability */, | 78 rappor::PROBABILITY_75 /* One coin probability */, |
| 79 rappor::PROBABILITY_25 /* Zero coin probability */, |
| 80 COARSE_LEVEL /* Recording level */}, |
| 81 // SB_STRING_AND_FLAGS_RAPPOR_TYPE |
| 82 {128 /* Num cohorts */, |
| 83 1 /* Bloom filter size bytes */, |
| 84 2 /* Bloom filter hash count */, |
| 85 1, /* Flag bytes */ |
| 86 rappor::PROBABILITY_50 /* Fake data probability */, |
| 87 rappor::PROBABILITY_50 /* Fake one probability */, |
| 88 rappor::PROBABILITY_75 /* One coin probability */, |
67 rappor::PROBABILITY_25 /* Zero coin probability */, | 89 rappor::PROBABILITY_25 /* Zero coin probability */, |
68 COARSE_LEVEL /* Recording level */}, | 90 COARSE_LEVEL /* Recording level */}, |
69 }; | 91 }; |
70 | 92 |
71 } // namespace | 93 } // namespace |
72 | 94 |
73 RapporService::RapporService( | 95 RapporService::RapporService( |
74 PrefService* pref_service, | 96 PrefService* pref_service, |
75 const base::Callback<bool(void)> is_incognito_callback) | 97 const base::Callback<bool(void)> is_incognito_callback) |
76 : pref_service_(pref_service), | 98 : pref_service_(pref_service), |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 207 |
186 bool RapporService::ExportMetrics(RapporReports* reports) { | 208 bool RapporService::ExportMetrics(RapporReports* reports) { |
187 if (metrics_map_.empty()) { | 209 if (metrics_map_.empty()) { |
188 DVLOG(2) << "metrics_map_ is empty."; | 210 DVLOG(2) << "metrics_map_ is empty."; |
189 return false; | 211 return false; |
190 } | 212 } |
191 | 213 |
192 DCHECK_GE(cohort_, 0); | 214 DCHECK_GE(cohort_, 0); |
193 reports->set_cohort(cohort_); | 215 reports->set_cohort(cohort_); |
194 | 216 |
195 for (std::map<std::string, RapporMetric*>::const_iterator it = | 217 for (std::map<std::string, internal::RapporMetric*>::const_iterator it = |
196 metrics_map_.begin(); | 218 metrics_map_.begin(); |
197 it != metrics_map_.end(); | 219 it != metrics_map_.end(); |
198 ++it) { | 220 ++it) { |
199 const RapporMetric* metric = it->second; | 221 const internal::RapporMetric* metric = it->second; |
200 RapporReports::Report* report = reports->add_report(); | 222 RapporReports::Report* report = reports->add_report(); |
201 report->set_name_hash(metrics::HashMetricName(it->first)); | 223 report->set_name_hash(metrics::HashMetricName(it->first)); |
202 ByteVector bytes = metric->GetReport(secret_); | 224 ByteVector bytes = metric->GetReport(secret_); |
203 report->set_bits(std::string(bytes.begin(), bytes.end())); | 225 report->set_bits(std::string(bytes.begin(), bytes.end())); |
204 } | 226 } |
205 STLDeleteValues(&metrics_map_); | 227 STLDeleteValues(&metrics_map_); |
206 return true; | 228 return true; |
207 } | 229 } |
208 | 230 |
209 bool RapporService::IsInitialized() const { | 231 bool RapporService::IsInitialized() const { |
210 return cohort_ >= 0; | 232 return cohort_ >= 0; |
211 } | 233 } |
212 | 234 |
213 void RapporService::RecordSample(const std::string& metric_name, | 235 void RapporService::RecordSample(const std::string& metric_name, |
214 RapporType type, | 236 RapporType type, |
215 const std::string& sample) { | 237 const std::string& str) { |
| 238 RecordStringAndFlags(metric_name, type, str, 0); |
| 239 } |
| 240 |
| 241 void RapporService::RecordStringAndFlags(const std::string& metric_name, |
| 242 RapporType type, |
| 243 const std::string& str, |
| 244 uint64_t flags) { |
216 // Ignore the sample if the service hasn't started yet. | 245 // Ignore the sample if the service hasn't started yet. |
217 if (!IsInitialized()) | 246 if (!IsInitialized()) |
218 return; | 247 return; |
219 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 248 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
220 const RapporParameters& parameters = kRapporParametersForType[type]; | 249 const RapporParameters& parameters = kRapporParametersForType[type]; |
221 DVLOG(2) << "Recording sample \"" << sample | 250 DVLOG(2) << "Recording sample \"" << str << "\"," << flags |
222 << "\" for metric \"" << metric_name | 251 << " for metric \"" << metric_name |
223 << "\" of type: " << type; | 252 << "\" of type: " << type; |
| 253 internal::Sample sample; |
| 254 internal::SetSampleBits(parameters, cohort_, str, flags, &sample); |
224 RecordSampleInternal(metric_name, parameters, sample); | 255 RecordSampleInternal(metric_name, parameters, sample); |
225 } | 256 } |
226 | 257 |
227 void RapporService::RecordSampleInternal(const std::string& metric_name, | 258 void RapporService::RecordSampleInternal(const std::string& metric_name, |
228 const RapporParameters& parameters, | 259 const RapporParameters& parameters, |
229 const std::string& sample) { | 260 const internal::Sample& sample) { |
230 DCHECK(IsInitialized()); | 261 DCHECK(IsInitialized()); |
231 if (is_incognito_callback_.Run()) { | 262 if (is_incognito_callback_.Run()) { |
232 DVLOG(2) << "Metric not logged due to incognito mode."; | 263 DVLOG(2) << "Metric not logged due to incognito mode."; |
233 return; | 264 return; |
234 } | 265 } |
235 // Skip this metric if it's reporting level is less than the enabled | 266 // Skip this metric if it's reporting level is less than the enabled |
236 // reporting level. | 267 // reporting level. |
237 if (recording_level_ < parameters.recording_level) { | 268 if (recording_level_ < parameters.recording_level) { |
238 DVLOG(2) << "Metric not logged due to recording_level " | 269 DVLOG(2) << "Metric not logged due to recording_level " |
239 << recording_level_ << " < " << parameters.recording_level; | 270 << recording_level_ << " < " << parameters.recording_level; |
240 return; | 271 return; |
241 } | 272 } |
242 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 273 internal::RapporMetric* metric = LookUpMetric(metric_name, parameters); |
243 metric->AddSample(sample); | 274 metric->AddSample(sample); |
244 } | 275 } |
245 | 276 |
246 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 277 internal::RapporMetric* RapporService::LookUpMetric( |
247 const RapporParameters& parameters) { | 278 const std::string& metric_name, |
| 279 const RapporParameters& parameters) { |
248 DCHECK(IsInitialized()); | 280 DCHECK(IsInitialized()); |
249 std::map<std::string, RapporMetric*>::const_iterator it = | 281 std::map<std::string, internal::RapporMetric*>::const_iterator it = |
250 metrics_map_.find(metric_name); | 282 metrics_map_.find(metric_name); |
251 if (it != metrics_map_.end()) { | 283 if (it != metrics_map_.end()) { |
252 RapporMetric* metric = it->second; | 284 internal::RapporMetric* metric = it->second; |
253 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 285 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
254 return metric; | 286 return metric; |
255 } | 287 } |
256 | 288 |
257 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 289 internal::RapporMetric* new_metric = |
| 290 new internal::RapporMetric(metric_name, parameters); |
258 metrics_map_[metric_name] = new_metric; | 291 metrics_map_[metric_name] = new_metric; |
259 return new_metric; | 292 return new_metric; |
260 } | 293 } |
261 | 294 |
262 } // namespace rappor | 295 } // namespace rappor |
OLD | NEW |