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 FINE_LEVEL /* Reporting level */}, | |
63 // COARSE_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 COARSE_LEVEL /* Reporting level */}, | |
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 reporting_level_(REPORTING_DISABLED) { | |
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) { |
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 // TODO(holte): Consider moving this logic once we've determined the | |
103 // conditions for COARSE metrics. | |
104 ReportingLevel reporting_level = metrics_enabled ? | |
105 FINE_LEVEL : REPORTING_DISABLED; | |
106 DVLOG(1) << "RapporService reporting_level_? " << reporting_level; | |
107 if (reporting_level <= REPORTING_DISABLED) { | |
Alexei Svitkine (slow)
2014/11/04 15:39:24
Nit: No {}'s
Steven Holte
2014/11/04 18:57:22
Done.
| |
108 return; | |
109 } | |
99 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); | 110 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); |
100 DCHECK(!uploader_); | 111 DCHECK(!uploader_); |
101 LoadSecret(); | 112 Initialize(LoadCohort(), LoadSecret(), reporting_level_); |
102 LoadCohort(); | |
103 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 113 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
104 log_rotation_timer_.Start( | 114 log_rotation_timer_.Start( |
105 FROM_HERE, | 115 FROM_HERE, |
106 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 116 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
107 this, | 117 this, |
108 &RapporService::OnLogInterval); | 118 &RapporService::OnLogInterval); |
109 } | 119 } |
110 | 120 |
121 void RapporService::Initialize(int32_t cohort, | |
122 const std::string& secret, | |
123 const ReportingLevel& reporting_level) { | |
124 DCHECK(!IsInitialized()); | |
125 DCHECK(secret_.empty()); | |
126 cohort_ = cohort; | |
127 secret_ = secret; | |
128 reporting_level_ = reporting_level; | |
129 } | |
130 | |
111 void RapporService::OnLogInterval() { | 131 void RapporService::OnLogInterval() { |
112 DCHECK(uploader_); | 132 DCHECK(uploader_); |
113 DVLOG(2) << "RapporService::OnLogInterval"; | 133 DVLOG(2) << "RapporService::OnLogInterval"; |
114 daily_event_.CheckInterval(); | 134 daily_event_.CheckInterval(); |
115 RapporReports reports; | 135 RapporReports reports; |
116 if (ExportMetrics(&reports)) { | 136 if (ExportMetrics(&reports)) { |
117 std::string log_text; | 137 std::string log_text; |
118 bool success = reports.SerializeToString(&log_text); | 138 bool success = reports.SerializeToString(&log_text); |
119 DCHECK(success); | 139 DCHECK(success); |
120 DVLOG(1) << "RapporService sending a report of " | 140 DVLOG(1) << "RapporService sending a report of " |
121 << reports.report_size() << " value(s)."; | 141 << reports.report_size() << " value(s)."; |
122 uploader_->QueueLog(log_text); | 142 uploader_->QueueLog(log_text); |
123 } | 143 } |
124 log_rotation_timer_.Start(FROM_HERE, | 144 log_rotation_timer_.Start(FROM_HERE, |
125 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | 145 base::TimeDelta::FromSeconds(kLogIntervalSeconds), |
126 this, | 146 this, |
127 &RapporService::OnLogInterval); | 147 &RapporService::OnLogInterval); |
128 } | 148 } |
129 | 149 |
130 // static | 150 // static |
131 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | 151 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { |
132 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | 152 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); |
133 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); | 153 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); |
134 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); | 154 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); |
135 metrics::DailyEvent::RegisterPref(registry, | 155 metrics::DailyEvent::RegisterPref(registry, |
136 prefs::kRapporLastDailySample); | 156 prefs::kRapporLastDailySample); |
137 } | 157 } |
138 | 158 |
139 void RapporService::LoadCohort() { | 159 int32_t RapporService::LoadCohort() { |
140 DCHECK(!IsInitialized()); | |
141 // Ignore and delete old cohort parameter. | 160 // Ignore and delete old cohort parameter. |
142 pref_service_->ClearPref(prefs::kRapporCohortDeprecated); | 161 pref_service_->ClearPref(prefs::kRapporCohortDeprecated); |
143 | 162 |
144 cohort_ = pref_service_->GetInteger(prefs::kRapporCohortSeed); | 163 int32_t cohort = pref_service_->GetInteger(prefs::kRapporCohortSeed); |
145 // If the user is already assigned to a valid cohort, we're done. | 164 // If the user is already assigned to a valid cohort, we're done. |
146 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) | 165 if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts) |
147 return; | 166 return cohort; |
148 | 167 |
149 // This is the first time the client has started the service (or their | 168 // This is the first time the client has started the service (or their |
150 // preferences were corrupted). Randomly assign them to a cohort. | 169 // preferences were corrupted). Randomly assign them to a cohort. |
151 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); | 170 cohort = base::RandGenerator(RapporParameters::kMaxCohorts); |
152 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_; | 171 DVLOG(2) << "Selected a new Rappor cohort: " << cohort; |
153 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort_); | 172 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort); |
173 return cohort; | |
154 } | 174 } |
155 | 175 |
156 void RapporService::LoadSecret() { | 176 std::string RapporService::LoadSecret() { |
157 DCHECK(secret_.empty()); | 177 std::string secret; |
158 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret); | 178 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret); |
159 if (!secret_base64.empty()) { | 179 if (!secret_base64.empty()) { |
160 bool decoded = base::Base64Decode(secret_base64, &secret_); | 180 bool decoded = base::Base64Decode(secret_base64, &secret); |
161 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | 181 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) |
162 return; | 182 return secret; |
163 // If the preference fails to decode, or is the wrong size, it must be | 183 // If the preference fails to decode, or is the wrong size, it must be |
164 // corrupt, so continue as though it didn't exist yet and generate a new | 184 // corrupt, so continue as though it didn't exist yet and generate a new |
165 // one. | 185 // one. |
166 } | 186 } |
167 | 187 |
168 DVLOG(2) << "Generated a new Rappor secret."; | 188 DVLOG(2) << "Generated a new Rappor secret."; |
169 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); | 189 secret = HmacByteVectorGenerator::GenerateEntropyInput(); |
170 base::Base64Encode(secret_, &secret_base64); | 190 base::Base64Encode(secret, &secret_base64); |
171 pref_service_->SetString(prefs::kRapporSecret, secret_base64); | 191 pref_service_->SetString(prefs::kRapporSecret, secret_base64); |
172 } | 192 return secret; |
193 }; | |
Alexei Svitkine (slow)
2014/11/04 15:39:24
Nit: Remove trailing ;
Steven Holte
2014/11/04 18:57:22
Done.
| |
173 | 194 |
174 bool RapporService::ExportMetrics(RapporReports* reports) { | 195 bool RapporService::ExportMetrics(RapporReports* reports) { |
175 if (metrics_map_.empty()) | 196 if (metrics_map_.empty()) |
176 return false; | 197 return false; |
177 | 198 |
178 DCHECK_GE(cohort_, 0); | 199 DCHECK_GE(cohort_, 0); |
179 reports->set_cohort(cohort_); | 200 reports->set_cohort(cohort_); |
180 | 201 |
181 for (std::map<std::string, RapporMetric*>::const_iterator it = | 202 for (std::map<std::string, RapporMetric*>::const_iterator it = |
182 metrics_map_.begin(); | 203 metrics_map_.begin(); |
(...skipping 13 matching lines...) Expand all Loading... | |
196 return cohort_ >= 0; | 217 return cohort_ >= 0; |
197 } | 218 } |
198 | 219 |
199 void RapporService::RecordSample(const std::string& metric_name, | 220 void RapporService::RecordSample(const std::string& metric_name, |
200 RapporType type, | 221 RapporType type, |
201 const std::string& sample) { | 222 const std::string& sample) { |
202 // Ignore the sample if the service hasn't started yet. | 223 // Ignore the sample if the service hasn't started yet. |
203 if (!IsInitialized()) | 224 if (!IsInitialized()) |
204 return; | 225 return; |
205 DCHECK_LT(type, NUM_RAPPOR_TYPES); | 226 DCHECK_LT(type, NUM_RAPPOR_TYPES); |
227 const RapporParameters& parameters = kRapporParametersForType[type]; | |
206 DVLOG(2) << "Recording sample \"" << sample | 228 DVLOG(2) << "Recording sample \"" << sample |
207 << "\" for metric \"" << metric_name | 229 << "\" for metric \"" << metric_name |
208 << "\" of type: " << type; | 230 << "\" of type: " << type; |
209 RecordSampleInternal(metric_name, kRapporParametersForType[type], sample); | 231 RecordSampleInternal(metric_name, parameters, sample); |
210 } | 232 } |
211 | 233 |
212 void RapporService::RecordSampleInternal(const std::string& metric_name, | 234 void RapporService::RecordSampleInternal(const std::string& metric_name, |
213 const RapporParameters& parameters, | 235 const RapporParameters& parameters, |
214 const std::string& sample) { | 236 const std::string& sample) { |
215 DCHECK(IsInitialized()); | 237 DCHECK(IsInitialized()); |
238 // Skip this metric if it's reporting level is less than the enabled | |
239 // reporting level. | |
240 if (reporting_level_ < parameters.reporting_level) | |
241 return; | |
216 RapporMetric* metric = LookUpMetric(metric_name, parameters); | 242 RapporMetric* metric = LookUpMetric(metric_name, parameters); |
217 metric->AddSample(sample); | 243 metric->AddSample(sample); |
218 } | 244 } |
219 | 245 |
220 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, | 246 RapporMetric* RapporService::LookUpMetric(const std::string& metric_name, |
221 const RapporParameters& parameters) { | 247 const RapporParameters& parameters) { |
222 DCHECK(IsInitialized()); | 248 DCHECK(IsInitialized()); |
223 std::map<std::string, RapporMetric*>::const_iterator it = | 249 std::map<std::string, RapporMetric*>::const_iterator it = |
224 metrics_map_.find(metric_name); | 250 metrics_map_.find(metric_name); |
225 if (it != metrics_map_.end()) { | 251 if (it != metrics_map_.end()) { |
226 RapporMetric* metric = it->second; | 252 RapporMetric* metric = it->second; |
227 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 253 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
228 return metric; | 254 return metric; |
229 } | 255 } |
230 | 256 |
231 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 257 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
232 metrics_map_[metric_name] = new_metric; | 258 metrics_map_[metric_name] = new_metric; |
233 return new_metric; | 259 return new_metric; |
234 } | 260 } |
235 | 261 |
236 } // namespace rappor | 262 } // namespace rappor |
OLD | NEW |