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