| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 16 /* Bloom filter size bytes */, | 62 16 /* Bloom filter size bytes */, |
| 63 2 /* Bloom filter hash count */, | 63 2 /* Bloom filter hash count */, |
| 64 rappor::PROBABILITY_50 /* Fake data probability */, | 64 rappor::PROBABILITY_50 /* Fake data probability */, |
| 65 rappor::PROBABILITY_50 /* Fake one probability */, | 65 rappor::PROBABILITY_50 /* Fake one probability */, |
| 66 rappor::PROBABILITY_75 /* One coin probability */, | 66 rappor::PROBABILITY_75 /* One coin probability */, |
| 67 rappor::PROBABILITY_25 /* Zero coin probability */}, | 67 rappor::PROBABILITY_25 /* Zero coin probability */}, |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 } // namespace | 70 } // namespace |
| 71 | 71 |
| 72 RapporService::RapporService() : cohort_(-1) {} | 72 RapporService::RapporService(PrefService* pref_service) |
| 73 : pref_service_(pref_service), |
| 74 cohort_(-1), |
| 75 daily_interval_(pref_service, prefs::kRapporLastDailySample) { |
| 76 } |
| 73 | 77 |
| 74 RapporService::~RapporService() { | 78 RapporService::~RapporService() { |
| 75 STLDeleteValues(&metrics_map_); | 79 STLDeleteValues(&metrics_map_); |
| 76 } | 80 } |
| 77 | 81 |
| 78 void RapporService::Start(PrefService* pref_service, | 82 void RapporService::AddDailyMetric(metrics::DailyObserver* observer) { |
| 79 net::URLRequestContextGetter* request_context, | 83 daily_interval_.AddObserver(observer); |
| 84 } |
| 85 |
| 86 void RapporService::Start(net::URLRequestContextGetter* request_context, |
| 80 bool metrics_enabled) { | 87 bool metrics_enabled) { |
| 81 const GURL server_url = GetServerUrl(metrics_enabled); | 88 const GURL server_url = GetServerUrl(metrics_enabled); |
| 82 if (!server_url.is_valid()) { | 89 if (!server_url.is_valid()) { |
| 83 DVLOG(1) << server_url.spec() << " is invalid. " | 90 DVLOG(1) << server_url.spec() << " is invalid. " |
| 84 << "RapporService not started."; | 91 << "RapporService not started."; |
| 85 return; | 92 return; |
| 86 } | 93 } |
| 87 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); | 94 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); |
| 88 DCHECK(!uploader_); | 95 DCHECK(!uploader_); |
| 89 LoadSecret(pref_service); | 96 LoadSecret(); |
| 90 LoadCohort(pref_service); | 97 LoadCohort(); |
| 91 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 98 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
| 92 log_rotation_timer_.Start( | 99 log_rotation_timer_.Start( |
| 93 FROM_HERE, | 100 FROM_HERE, |
| 94 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 101 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
| 95 this, | 102 this, |
| 96 &RapporService::OnLogInterval); | 103 &RapporService::OnLogInterval); |
| 97 } | 104 } |
| 98 | 105 |
| 99 void RapporService::OnLogInterval() { | 106 void RapporService::OnLogInterval() { |
| 100 DCHECK(uploader_); | 107 DCHECK(uploader_); |
| 101 DVLOG(2) << "RapporService::OnLogInterval"; | 108 DVLOG(2) << "RapporService::OnLogInterval"; |
| 109 daily_interval_.CheckInterval(); |
| 102 RapporReports reports; | 110 RapporReports reports; |
| 103 if (ExportMetrics(&reports)) { | 111 if (ExportMetrics(&reports)) { |
| 104 std::string log_text; | 112 std::string log_text; |
| 105 bool success = reports.SerializeToString(&log_text); | 113 bool success = reports.SerializeToString(&log_text); |
| 106 DCHECK(success); | 114 DCHECK(success); |
| 107 DVLOG(1) << "RapporService sending a report of " | 115 DVLOG(1) << "RapporService sending a report of " |
| 108 << reports.report_size() << " value(s)."; | 116 << reports.report_size() << " value(s)."; |
| 109 uploader_->QueueLog(log_text); | 117 uploader_->QueueLog(log_text); |
| 110 } | 118 } |
| 111 log_rotation_timer_.Start(FROM_HERE, | 119 log_rotation_timer_.Start(FROM_HERE, |
| 112 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | 120 base::TimeDelta::FromSeconds(kLogIntervalSeconds), |
| 113 this, | 121 this, |
| 114 &RapporService::OnLogInterval); | 122 &RapporService::OnLogInterval); |
| 115 } | 123 } |
| 116 | 124 |
| 117 // static | 125 // static |
| 118 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | 126 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 119 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | 127 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); |
| 120 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); | 128 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); |
| 121 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); | 129 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); |
| 130 metrics::DailyInterval::RegisterPref(registry, |
| 131 prefs::kRapporLastDailySample); |
| 122 } | 132 } |
| 123 | 133 |
| 124 void RapporService::LoadCohort(PrefService* pref_service) { | 134 void RapporService::LoadCohort() { |
| 125 DCHECK(!IsInitialized()); | 135 DCHECK(!IsInitialized()); |
| 126 // Ignore and delete old cohort parameter. | 136 // Ignore and delete old cohort parameter. |
| 127 pref_service->ClearPref(prefs::kRapporCohortDeprecated); | 137 pref_service_->ClearPref(prefs::kRapporCohortDeprecated); |
| 128 | 138 |
| 129 cohort_ = pref_service->GetInteger(prefs::kRapporCohortSeed); | 139 cohort_ = pref_service_->GetInteger(prefs::kRapporCohortSeed); |
| 130 // If the user is already assigned to a valid cohort, we're done. | 140 // If the user is already assigned to a valid cohort, we're done. |
| 131 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) | 141 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) |
| 132 return; | 142 return; |
| 133 | 143 |
| 134 // This is the first time the client has started the service (or their | 144 // This is the first time the client has started the service (or their |
| 135 // preferences were corrupted). Randomly assign them to a cohort. | 145 // preferences were corrupted). Randomly assign them to a cohort. |
| 136 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); | 146 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); |
| 137 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_; | 147 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_; |
| 138 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort_); | 148 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort_); |
| 139 } | 149 } |
| 140 | 150 |
| 141 void RapporService::LoadSecret(PrefService* pref_service) { | 151 void RapporService::LoadSecret() { |
| 142 DCHECK(secret_.empty()); | 152 DCHECK(secret_.empty()); |
| 143 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret); | 153 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret); |
| 144 if (!secret_base64.empty()) { | 154 if (!secret_base64.empty()) { |
| 145 bool decoded = base::Base64Decode(secret_base64, &secret_); | 155 bool decoded = base::Base64Decode(secret_base64, &secret_); |
| 146 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | 156 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) |
| 147 return; | 157 return; |
| 148 // If the preference fails to decode, or is the wrong size, it must be | 158 // If the preference fails to decode, or is the wrong size, it must be |
| 149 // corrupt, so continue as though it didn't exist yet and generate a new | 159 // corrupt, so continue as though it didn't exist yet and generate a new |
| 150 // one. | 160 // one. |
| 151 } | 161 } |
| 152 | 162 |
| 153 DVLOG(2) << "Generated a new Rappor secret."; | 163 DVLOG(2) << "Generated a new Rappor secret."; |
| 154 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); | 164 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); |
| 155 base::Base64Encode(secret_, &secret_base64); | 165 base::Base64Encode(secret_, &secret_base64); |
| 156 pref_service->SetString(prefs::kRapporSecret, secret_base64); | 166 pref_service_->SetString(prefs::kRapporSecret, secret_base64); |
| 157 } | 167 } |
| 158 | 168 |
| 159 bool RapporService::ExportMetrics(RapporReports* reports) { | 169 bool RapporService::ExportMetrics(RapporReports* reports) { |
| 160 if (metrics_map_.empty()) | 170 if (metrics_map_.empty()) |
| 161 return false; | 171 return false; |
| 162 | 172 |
| 163 DCHECK_GE(cohort_, 0); | 173 DCHECK_GE(cohort_, 0); |
| 164 reports->set_cohort(cohort_); | 174 reports->set_cohort(cohort_); |
| 165 | 175 |
| 166 for (std::map<std::string, RapporMetric*>::const_iterator it = | 176 for (std::map<std::string, RapporMetric*>::const_iterator it = |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 222 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
| 213 return metric; | 223 return metric; |
| 214 } | 224 } |
| 215 | 225 |
| 216 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 226 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
| 217 metrics_map_[metric_name] = new_metric; | 227 metrics_map_[metric_name] = new_metric; |
| 218 return new_metric; | 228 return new_metric; |
| 219 } | 229 } |
| 220 | 230 |
| 221 } // namespace rappor | 231 } // namespace rappor |
| OLD | NEW |