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