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 23 matching lines...) Expand all Loading... | |
34 | 34 |
35 // Constant for the finch parameter name for the server URL | 35 // Constant for the finch parameter name for the server URL |
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | 36 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; |
37 | 37 |
38 // Constant for the finch parameter name for the server URL | 38 // Constant for the finch parameter name for the server URL |
39 const char kRapporRolloutRequireUmaParam[] = "RequireUma"; | 39 const char kRapporRolloutRequireUmaParam[] = "RequireUma"; |
40 | 40 |
41 // The rappor server's URL. | 41 // The rappor server's URL. |
42 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor"; | 42 const char kDefaultServerUrl[] = "https://clients4.google.com/rappor"; |
43 | 43 |
44 GURL GetServerUrl(bool metrics_enabled) { | 44 GURL GetServerUrl() { |
45 bool require_uma = variations::GetVariationParamValue( | |
46 kRapporRolloutFieldTrialName, | |
47 kRapporRolloutRequireUmaParam) != "False"; | |
48 if (!metrics_enabled && require_uma) | |
49 return GURL(); // Invalid URL disables Rappor. | |
50 std::string server_url = variations::GetVariationParamValue( | 45 std::string server_url = variations::GetVariationParamValue( |
51 kRapporRolloutFieldTrialName, | 46 kRapporRolloutFieldTrialName, |
52 kRapporRolloutServerUrlParam); | 47 kRapporRolloutServerUrlParam); |
53 if (!server_url.empty()) | 48 if (!server_url.empty()) |
54 return GURL(server_url); | 49 return GURL(server_url); |
55 else | 50 else |
56 return GURL(kDefaultServerUrl); | 51 return GURL(kDefaultServerUrl); |
57 } | 52 } |
58 | 53 |
54 bool ShouldEnableAllMetrics(bool metrics_enabled) { | |
55 bool require_uma = variations::GetVariationParamValue( | |
56 kRapporRolloutFieldTrialName, | |
57 kRapporRolloutRequireUmaParam) != "False"; | |
jwd
2014/08/29 15:45:47
Super good, awesome choice for the condition!
| |
58 return metrics_enabled || !require_uma; | |
59 } | |
60 | |
59 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { | 61 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { |
60 // ETLD_PLUS_ONE_RAPPOR_TYPE | 62 // ETLD_PLUS_ONE_RAPPOR_TYPE |
61 {128 /* Num cohorts */, | 63 {128 /* Num cohorts */, |
62 16 /* Bloom filter size bytes */, | 64 16 /* Bloom filter size bytes */, |
63 2 /* Bloom filter hash count */, | 65 2 /* Bloom filter hash count */, |
64 rappor::PROBABILITY_50 /* Fake data probability */, | 66 rappor::PROBABILITY_50 /* Fake data probability */, |
65 rappor::PROBABILITY_50 /* Fake one probability */, | 67 rappor::PROBABILITY_50 /* Fake one probability */, |
66 rappor::PROBABILITY_75 /* One coin probability */, | 68 rappor::PROBABILITY_75 /* One coin probability */, |
67 rappor::PROBABILITY_25 /* Zero coin probability */}, | 69 rappor::PROBABILITY_25 /* Zero coin probability */, |
70 false /* Require UMA */}, | |
71 // SAFE_BROWSING_PATTERN_RAPPOR_TYPE | |
72 {64 /* Num cohorts */, | |
73 16 /* Bloom filter size bytes */, | |
74 2 /* Bloom filter hash count */, | |
75 rappor::PROBABILITY_50 /* Fake data probability */, | |
76 rappor::PROBABILITY_50 /* Fake one probability */, | |
77 rappor::PROBABILITY_75 /* One coin probability */, | |
78 rappor::PROBABILITY_25 /* Zero coin probability */, | |
79 true /* Don't require UMA */}, | |
68 }; | 80 }; |
69 | 81 |
70 } // namespace | 82 } // namespace |
71 | 83 |
72 RapporService::RapporService() : cohort_(-1) {} | 84 RapporService::RapporService() : cohort_(-1) {} |
73 | 85 |
74 RapporService::~RapporService() { | 86 RapporService::~RapporService() { |
75 STLDeleteValues(&metrics_map_); | 87 STLDeleteValues(&metrics_map_); |
76 } | 88 } |
77 | 89 |
78 void RapporService::Start(PrefService* pref_service, | 90 void RapporService::Start(PrefService* pref_service, |
79 net::URLRequestContextGetter* request_context, | 91 net::URLRequestContextGetter* request_context, |
80 bool metrics_enabled) { | 92 bool metrics_enabled) { |
81 const GURL server_url = GetServerUrl(metrics_enabled); | 93 const GURL server_url = GetServerUrl(); |
82 if (!server_url.is_valid()) { | 94 if (!server_url.is_valid()) { |
83 DVLOG(1) << server_url.spec() << " is invalid. " | 95 DVLOG(1) << server_url.spec() << " is invalid. " |
84 << "RapporService not started."; | 96 << "RapporService not started."; |
85 return; | 97 return; |
86 } | 98 } |
99 all_metrics_enabled_ = ShouldEnableAllMetrics(metrics_enabled); | |
100 DVLOG(1) << "RapporService all_metrics_enabled_? " << all_metrics_enabled_; | |
87 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); | 101 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); |
88 DCHECK(!uploader_); | 102 DCHECK(!uploader_); |
89 LoadSecret(pref_service); | 103 LoadSecret(pref_service); |
90 LoadCohort(pref_service); | 104 LoadCohort(pref_service); |
91 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 105 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
92 log_rotation_timer_.Start( | 106 log_rotation_timer_.Start( |
93 FROM_HERE, | 107 FROM_HERE, |
94 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 108 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
95 this, | 109 this, |
96 &RapporService::OnLogInterval); | 110 &RapporService::OnLogInterval); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 return cohort_ >= 0; | 195 return cohort_ >= 0; |
182 } | 196 } |
183 | 197 |
184 void RapporService::RecordSample(const std::string& metric_name, | 198 void RapporService::RecordSample(const std::string& metric_name, |
185 RapporType type, | 199 RapporType type, |
186 const std::string& sample) { | 200 const std::string& sample) { |
187 // Ignore the sample if the service hasn't started yet. | 201 // Ignore the sample if the service hasn't started yet. |
188 if (!IsInitialized()) | 202 if (!IsInitialized()) |
189 return; | 203 return; |
190 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 204 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
205 const RapporParameters& parameters = kRapporParametersForType[type]; | |
206 // Skip this metric if all metrics aren't enabled and this isn't a | |
207 // whitelisted metric. | |
208 if (!all_metrics_enabled_ && !parameters.always_enabled) | |
209 return; | |
191 DVLOG(2) << "Recording sample \"" << sample | 210 DVLOG(2) << "Recording sample \"" << sample |
192 << "\" for metric \"" << metric_name | 211 << "\" for metric \"" << metric_name |
193 << "\" of type: " << type; | 212 << "\" of type: " << type; |
194 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | 213 RecordSampleInternal(metric_name, parameters, sample); |
195 } | 214 } |
196 | 215 |
197 void RapporService::RecordSampleInternal(const std::string& metric_name, | 216 void RapporService::RecordSampleInternal(const std::string& metric_name, |
198 const RapporParameters& parameters, | 217 const RapporParameters& parameters, |
199 const std::string& sample) { | 218 const std::string& sample) { |
200 DCHECK(IsInitialized()); | 219 DCHECK(IsInitialized()); |
201 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 220 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
202 metric->AddSample(sample); | 221 metric->AddSample(sample); |
203 } | 222 } |
204 | 223 |
205 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 224 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
206 const RapporParameters& parameters) { | 225 const RapporParameters& parameters) { |
207 DCHECK(IsInitialized()); | 226 DCHECK(IsInitialized()); |
208 std::map<std::string, RapporMetric*>::const_iterator it = | 227 std::map<std::string, RapporMetric*>::const_iterator it = |
209 metrics_map_.find(metric_name); | 228 metrics_map_.find(metric_name); |
210 if (it != metrics_map_.end()) { | 229 if (it != metrics_map_.end()) { |
211 RapporMetric* metric = it->second; | 230 RapporMetric* metric = it->second; |
212 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 231 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
213 return metric; | 232 return metric; |
214 } | 233 } |
215 | 234 |
216 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 235 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
217 metrics_map_[metric_name] = new_metric; | 236 metrics_map_[metric_name] = new_metric; |
218 return new_metric; | 237 return new_metric; |
219 } | 238 } |
220 | 239 |
221 } // namespace rappor | 240 } // namespace rappor |
OLD | NEW |