Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 30 const char kMimeType[] = "application/vnd.chrome.rappor"; | 30 const char kMimeType[] = "application/vnd.chrome.rappor"; |
| 31 | 31 |
| 32 const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType"; | 32 const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType"; |
| 33 | 33 |
| 34 // Constants for the RAPPOR rollout field trial. | 34 // Constants for the RAPPOR rollout field trial. |
| 35 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; | 35 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; |
| 36 | 36 |
| 37 // Constant for the finch parameter name for the server URL | 37 // Constant for the finch parameter name for the server URL |
| 38 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | 38 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; |
| 39 | 39 |
| 40 // Constant for the finch parameter name for the server URL | |
| 41 const char kRapporRolloutRequireUmaParam[] = "RequireUma"; | |
| 42 | |
| 43 // The rappor server's URL. | 40 // The rappor server's URL. |
| 44 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor"; | 41 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor"; |
| 45 | 42 |
| 46 GURL GetServerUrl(bool metrics_enabled) { | 43 GURL GetServerUrl() { |
| 47 bool require_uma = variations::GetVariationParamValue( | |
| 48 kRapporRolloutFieldTrialName, | |
| 49 kRapporRolloutRequireUmaParam) != "False"; | |
| 50 if (!metrics_enabled && require_uma) | |
| 51 return GURL(); // Invalid URL disables Rappor. | |
| 52 std::string server_url = variations::GetVariationParamValue( | 44 std::string server_url = variations::GetVariationParamValue( |
| 53 kRapporRolloutFieldTrialName, | 45 kRapporRolloutFieldTrialName, |
| 54 kRapporRolloutServerUrlParam); | 46 kRapporRolloutServerUrlParam); |
| 55 if (!server_url.empty()) | 47 if (!server_url.empty()) |
| 56 return GURL(server_url); | 48 return GURL(server_url); |
| 57 else | 49 else |
| 58 return GURL(kDefaultServerUrl); | 50 return GURL(kDefaultServerUrl); |
| 59 } | 51 } |
| 60 | 52 |
| 61 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { | 53 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { |
| 62 // ETLD_PLUS_ONE_RAPPOR_TYPE | 54 // ETLD_PLUS_ONE_RAPPOR_TYPE |
| 63 {128 /* Num cohorts */, | 55 {128 /* Num cohorts */, |
| 64 16 /* Bloom filter size bytes */, | 56 16 /* Bloom filter size bytes */, |
| 65 2 /* Bloom filter hash count */, | 57 2 /* Bloom filter hash count */, |
| 66 rappor::PROBABILITY_50 /* Fake data probability */, | 58 rappor::PROBABILITY_50 /* Fake data probability */, |
| 67 rappor::PROBABILITY_50 /* Fake one probability */, | 59 rappor::PROBABILITY_50 /* Fake one probability */, |
| 68 rappor::PROBABILITY_75 /* One coin probability */, | 60 rappor::PROBABILITY_75 /* One coin probability */, |
| 69 rappor::PROBABILITY_25 /* Zero coin probability */}, | 61 rappor::PROBABILITY_25 /* Zero coin probability */, |
| 62 false /* Require UMA */}, | |
| 63 // FULL_POPULATION_RAPPOR_TYPE | |
| 64 {128 /* Num cohorts */, | |
| 65 1 /* Bloom filter size bytes */, | |
| 66 2 /* Bloom filter hash count */, | |
| 67 rappor::PROBABILITY_50 /* Fake data probability */, | |
| 68 rappor::PROBABILITY_50 /* Fake one probability */, | |
| 69 rappor::PROBABILITY_75 /* One coin probability */, | |
| 70 rappor::PROBABILITY_25 /* Zero coin probability */, | |
| 71 true /* Don't require UMA */}, | |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 } // namespace | 74 } // namespace |
| 73 | 75 |
| 74 RapporService::RapporService(PrefService* pref_service) | 76 RapporService::RapporService(PrefService* pref_service) |
| 75 : pref_service_(pref_service), | 77 : pref_service_(pref_service), |
| 76 cohort_(-1), | 78 cohort_(-1), |
| 77 daily_event_(pref_service, | 79 daily_event_(pref_service, |
| 78 prefs::kRapporLastDailySample, | 80 prefs::kRapporLastDailySample, |
| 79 kRapporDailyEventHistogram) { | 81 kRapporDailyEventHistogram), |
| 82 all_metrics_enabled_(false) { | |
| 80 } | 83 } |
| 81 | 84 |
| 82 RapporService::~RapporService() { | 85 RapporService::~RapporService() { |
| 83 STLDeleteValues(&metrics_map_); | 86 STLDeleteValues(&metrics_map_); |
| 84 } | 87 } |
| 85 | 88 |
| 86 void RapporService::AddDailyObserver( | 89 void RapporService::AddDailyObserver( |
| 87 scoped_ptr<metrics::DailyEvent::Observer> observer) { | 90 scoped_ptr<metrics::DailyEvent::Observer> observer) { |
| 88 daily_event_.AddObserver(observer.Pass()); | 91 daily_event_.AddObserver(observer.Pass()); |
| 89 } | 92 } |
| 90 | 93 |
| 91 void RapporService::Start(net::URLRequestContextGetter* request_context, | 94 void RapporService::Start(net::URLRequestContextGetter* request_context, |
| 92 bool metrics_enabled) { | 95 bool metrics_enabled) { |
|
Alexei Svitkine (slow)
2014/11/03 21:48:21
Can you document the 2nd param better in the heade
Steven Holte
2014/11/04 02:44:19
Done.
| |
| 93 const GURL server_url = GetServerUrl(metrics_enabled); | 96 const GURL server_url = GetServerUrl(); |
| 94 if (!server_url.is_valid()) { | 97 if (!server_url.is_valid()) { |
| 95 DVLOG(1) << server_url.spec() << " is invalid. " | 98 DVLOG(1) << server_url.spec() << " is invalid. " |
| 96 << "RapporService not started."; | 99 << "RapporService not started."; |
| 97 return; | 100 return; |
| 98 } | 101 } |
| 102 all_metrics_enabled_ = metrics_enabled; | |
| 103 DVLOG(1) << "RapporService all_metrics_enabled_? " << all_metrics_enabled_; | |
| 99 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); | 104 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); |
| 100 DCHECK(!uploader_); | 105 DCHECK(!uploader_); |
| 101 LoadSecret(); | 106 LoadSecret(); |
| 102 LoadCohort(); | 107 LoadCohort(); |
| 103 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 108 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
| 104 log_rotation_timer_.Start( | 109 log_rotation_timer_.Start( |
| 105 FROM_HERE, | 110 FROM_HERE, |
| 106 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 111 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
| 107 this, | 112 this, |
| 108 &RapporService::OnLogInterval); | 113 &RapporService::OnLogInterval); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 return cohort_ >= 0; | 201 return cohort_ >= 0; |
| 197 } | 202 } |
| 198 | 203 |
| 199 void RapporService::RecordSample(const std::string& metric_name, | 204 void RapporService::RecordSample(const std::string& metric_name, |
| 200 RapporType type, | 205 RapporType type, |
| 201 const std::string& sample) { | 206 const std::string& sample) { |
| 202 // Ignore the sample if the service hasn't started yet. | 207 // Ignore the sample if the service hasn't started yet. |
| 203 if (!IsInitialized()) | 208 if (!IsInitialized()) |
| 204 return; | 209 return; |
| 205 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 210 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
| 211 const RapporParameters& parameters = kRapporParametersForType[type]; | |
| 212 // Skip this metric if all metrics aren't enabled and this isn't a | |
| 213 // whitelisted metric. | |
| 214 if (!all_metrics_enabled_ && !parameters.always_enabled) | |
|
Alexei Svitkine (slow)
2014/11/03 21:48:21
Can you add a test for this?
Steven Holte
2014/11/04 02:44:20
Done.
| |
| 215 return; | |
| 206 DVLOG(2) << "Recording sample \"" << sample | 216 DVLOG(2) << "Recording sample \"" << sample |
| 207 << "\" for metric \"" << metric_name | 217 << "\" for metric \"" << metric_name |
| 208 << "\" of type: " << type; | 218 << "\" of type: " << type; |
| 209 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | 219 RecordSampleInternal(metric_name, parameters, sample); |
| 210 } | 220 } |
| 211 | 221 |
| 212 void RapporService::RecordSampleInternal(const std::string& metric_name, | 222 void RapporService::RecordSampleInternal(const std::string& metric_name, |
| 213 const RapporParameters& parameters, | 223 const RapporParameters& parameters, |
| 214 const std::string& sample) { | 224 const std::string& sample) { |
| 215 DCHECK(IsInitialized()); | 225 DCHECK(IsInitialized()); |
| 216 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 226 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
| 217 metric->AddSample(sample); | 227 metric->AddSample(sample); |
| 218 } | 228 } |
| 219 | 229 |
| 220 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 230 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
| 221 const RapporParameters& parameters) { | 231 const RapporParameters& parameters) { |
| 222 DCHECK(IsInitialized()); | 232 DCHECK(IsInitialized()); |
| 223 std::map<std::string, RapporMetric*>::const_iterator it = | 233 std::map<std::string, RapporMetric*>::const_iterator it = |
| 224 metrics_map_.find(metric_name); | 234 metrics_map_.find(metric_name); |
| 225 if (it != metrics_map_.end()) { | 235 if (it != metrics_map_.end()) { |
| 226 RapporMetric* metric = it->second; | 236 RapporMetric* metric = it->second; |
| 227 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 237 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
| 228 return metric; | 238 return metric; |
| 229 } | 239 } |
| 230 | 240 |
| 231 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 241 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
| 232 metrics_map_[metric_name] = new_metric; | 242 metrics_map_[metric_name] = new_metric; |
| 233 return new_metric; | 243 return new_metric; |
| 234 } | 244 } |
| 235 | 245 |
| 236 } // namespace rappor | 246 } // namespace rappor |
| OLD | NEW |