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/base64.h" | 7 #include "base/base64.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 // TODO(holte): Consider moving this logic once we've determined the | 102 // TODO(holte): Consider moving this logic once we've determined the |
103 // conditions for COARSE metrics. | 103 // conditions for COARSE metrics. |
104 ReportingLevel reporting_level = metrics_enabled ? | 104 ReportingLevel reporting_level = metrics_enabled ? |
105 FINE_LEVEL : REPORTING_DISABLED; | 105 FINE_LEVEL : REPORTING_DISABLED; |
106 DVLOG(1) << "RapporService reporting_level_? " << reporting_level; | 106 DVLOG(1) << "RapporService reporting_level_? " << reporting_level; |
107 if (reporting_level <= REPORTING_DISABLED) | 107 if (reporting_level <= REPORTING_DISABLED) |
108 return; | 108 return; |
109 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); | 109 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); |
110 DCHECK(!uploader_); | 110 DCHECK(!uploader_); |
111 Initialize(LoadCohort(), LoadSecret(), reporting_level_); | 111 Initialize(LoadCohort(), LoadSecret(), reporting_level); |
112 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 112 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
113 log_rotation_timer_.Start( | 113 log_rotation_timer_.Start( |
114 FROM_HERE, | 114 FROM_HERE, |
115 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 115 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
116 this, | 116 this, |
117 &RapporService::OnLogInterval); | 117 &RapporService::OnLogInterval); |
118 } | 118 } |
119 | 119 |
120 void RapporService::Initialize(int32_t cohort, | 120 void RapporService::Initialize(int32_t cohort, |
121 const std::string& secret, | 121 const std::string& secret, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } | 185 } |
186 | 186 |
187 DVLOG(2) << "Generated a new Rappor secret."; | 187 DVLOG(2) << "Generated a new Rappor secret."; |
188 secret = HmacByteVectorGenerator::GenerateEntropyInput(); | 188 secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
189 base::Base64Encode(secret, &secret_base64); | 189 base::Base64Encode(secret, &secret_base64); |
190 pref_service_->SetString(prefs::kRapporSecret, secret_base64); | 190 pref_service_->SetString(prefs::kRapporSecret, secret_base64); |
191 return secret; | 191 return secret; |
192 } | 192 } |
193 | 193 |
194 bool RapporService::ExportMetrics(RapporReports* reports) { | 194 bool RapporService::ExportMetrics(RapporReports* reports) { |
195 if (metrics_map_.empty()) | 195 if (metrics_map_.empty()) { |
| 196 DVLOG(2) << "metrics_map_ is empty."; |
196 return false; | 197 return false; |
| 198 } |
197 | 199 |
198 DCHECK_GE(cohort_, 0); | 200 DCHECK_GE(cohort_, 0); |
199 reports->set_cohort(cohort_); | 201 reports->set_cohort(cohort_); |
200 | 202 |
201 for (std::map<std::string, RapporMetric*>::const_iterator it = | 203 for (std::map<std::string, RapporMetric*>::const_iterator it = |
202 metrics_map_.begin(); | 204 metrics_map_.begin(); |
203 it != metrics_map_.end(); | 205 it != metrics_map_.end(); |
204 ++it) { | 206 ++it) { |
205 const RapporMetric* metric = it->second; | 207 const RapporMetric* metric = it->second; |
206 RapporReports::Report* report = reports->add_report(); | 208 RapporReports::Report* report = reports->add_report(); |
(...skipping 22 matching lines...) Expand all Loading... |
229 << "\" of type: " << type; | 231 << "\" of type: " << type; |
230 RecordSampleInternal(metric_name, parameters, sample); | 232 RecordSampleInternal(metric_name, parameters, sample); |
231 } | 233 } |
232 | 234 |
233 void RapporService::RecordSampleInternal(const std::string& metric_name, | 235 void RapporService::RecordSampleInternal(const std::string& metric_name, |
234 const RapporParameters& parameters, | 236 const RapporParameters& parameters, |
235 const std::string& sample) { | 237 const std::string& sample) { |
236 DCHECK(IsInitialized()); | 238 DCHECK(IsInitialized()); |
237 // Skip this metric if it's reporting level is less than the enabled | 239 // Skip this metric if it's reporting level is less than the enabled |
238 // reporting level. | 240 // reporting level. |
239 if (reporting_level_ < parameters.reporting_level) | 241 if (reporting_level_ < parameters.reporting_level) { |
| 242 DVLOG(2) << "Metric not logged due to reporting_level " |
| 243 << reporting_level_ << " < " << parameters.reporting_level; |
240 return; | 244 return; |
| 245 } |
241 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 246 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
242 metric->AddSample(sample); | 247 metric->AddSample(sample); |
243 } | 248 } |
244 | 249 |
245 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 250 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
246 const RapporParameters& parameters) { | 251 const RapporParameters& parameters) { |
247 DCHECK(IsInitialized()); | 252 DCHECK(IsInitialized()); |
248 std::map<std::string, RapporMetric*>::const_iterator it = | 253 std::map<std::string, RapporMetric*>::const_iterator it = |
249 metrics_map_.find(metric_name); | 254 metrics_map_.find(metric_name); |
250 if (it != metrics_map_.end()) { | 255 if (it != metrics_map_.end()) { |
251 RapporMetric* metric = it->second; | 256 RapporMetric* metric = it->second; |
252 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 257 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
253 return metric; | 258 return metric; |
254 } | 259 } |
255 | 260 |
256 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 261 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
257 metrics_map_[metric_name] = new_metric; | 262 metrics_map_[metric_name] = new_metric; |
258 return new_metric; | 263 return new_metric; |
259 } | 264 } |
260 | 265 |
261 } // namespace rappor | 266 } // namespace rappor |
OLD | NEW |