Chromium Code Reviews| 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/metrics/metrics_state_manager.h" | 5 #include "components/metrics/metrics_state_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 36 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that | 36 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that |
| 37 // the value has not yet been set. | 37 // the value has not yet been set. |
| 38 const int kLowEntropySourceNotSet = -1; | 38 const int kLowEntropySourceNotSet = -1; |
| 39 | 39 |
| 40 // Generates a new non-identifying entropy source used to seed persistent | 40 // Generates a new non-identifying entropy source used to seed persistent |
| 41 // activities. | 41 // activities. |
| 42 int GenerateLowEntropySource() { | 42 int GenerateLowEntropySource() { |
| 43 return base::RandInt(0, kMaxLowEntropySize - 1); | 43 return base::RandInt(0, kMaxLowEntropySize - 1); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Records the given |low_entorpy_source_value| in a histogram. | |
| 47 void LogLowEntropyValue(int low_entropy_source_value) { | |
| 48 UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue", | |
| 49 low_entropy_source_value); | |
| 50 } | |
| 51 | |
| 46 } // namespace | 52 } // namespace |
| 47 | 53 |
| 48 // static | 54 // static |
| 49 bool MetricsStateManager::instance_exists_ = false; | 55 bool MetricsStateManager::instance_exists_ = false; |
| 50 | 56 |
| 51 MetricsStateManager::MetricsStateManager( | 57 MetricsStateManager::MetricsStateManager( |
| 52 PrefService* local_state, | 58 PrefService* local_state, |
| 53 EnabledStateProvider* enabled_state_provider, | 59 EnabledStateProvider* enabled_state_provider, |
| 54 const StoreClientInfoCallback& store_client_info, | 60 const StoreClientInfoCallback& store_client_info, |
| 55 const LoadClientInfoCallback& retrieve_client_info) | 61 const LoadClientInfoCallback& retrieve_client_info) |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 | 147 |
| 142 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); | 148 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); |
| 143 if (!provider) | 149 if (!provider) |
| 144 return; | 150 return; |
| 145 | 151 |
| 146 cloned_install_detector_.reset(new ClonedInstallDetector(provider)); | 152 cloned_install_detector_.reset(new ClonedInstallDetector(provider)); |
| 147 cloned_install_detector_->CheckForClonedInstall(local_state_, task_runner); | 153 cloned_install_detector_->CheckForClonedInstall(local_state_, task_runner); |
| 148 } | 154 } |
| 149 | 155 |
| 150 std::unique_ptr<const base::FieldTrial::EntropyProvider> | 156 std::unique_ptr<const base::FieldTrial::EntropyProvider> |
| 151 MetricsStateManager::CreateEntropyProvider() { | 157 MetricsStateManager::CreateDefaultEntropyProvider() { |
| 152 // For metrics reporting-enabled users, we combine the client ID and low | 158 // For metrics reporting-enabled users, we combine the client ID and low |
| 153 // entropy source to get the final entropy source. Otherwise, only use the low | 159 // entropy source to get the final entropy source. Otherwise, only use the low |
| 154 // entropy source. | 160 // entropy source. |
| 155 // This has two useful properties: | 161 // This has two useful properties: |
| 156 // 1) It makes the entropy source less identifiable for parties that do not | 162 // 1) It makes the entropy source less identifiable for parties that do not |
| 157 // know the low entropy source. | 163 // know the low entropy source. |
| 158 // 2) It makes the final entropy source resettable. | 164 // 2) It makes the final entropy source resettable. |
| 159 const int low_entropy_source_value = GetLowEntropySource(); | 165 const int low_entropy_source_value = GetLowEntropySource(); |
|
Alexei Svitkine (slow)
2016/05/18 15:03:30
Nit: Move this inside the if, so that it's not don
jwd
2016/05/18 18:31:41
Done.
| |
| 160 UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue", | |
| 161 low_entropy_source_value); | |
| 162 if (enabled_state_provider_->IsConsentGiven()) { | 166 if (enabled_state_provider_->IsConsentGiven()) { |
| 163 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH); | 167 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH); |
| 164 const std::string high_entropy_source = | 168 const std::string high_entropy_source = |
| 165 client_id_ + base::IntToString(low_entropy_source_value); | 169 client_id_ + base::IntToString(low_entropy_source_value); |
| 166 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( | 170 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( |
| 167 new SHA1EntropyProvider(high_entropy_source)); | 171 new SHA1EntropyProvider(high_entropy_source)); |
| 168 } | 172 } |
| 169 | 173 |
| 170 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW); | 174 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW); |
| 175 return CreateLowEntropyProvider(); | |
| 176 } | |
| 177 | |
| 178 std::unique_ptr<const base::FieldTrial::EntropyProvider> | |
| 179 MetricsStateManager::CreateLowEntropyProvider() { | |
| 180 const int low_entropy_source_value = GetLowEntropySource(); | |
| 181 | |
| 171 #if defined(OS_ANDROID) || defined(OS_IOS) | 182 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 172 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( | 183 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( |
| 173 new CachingPermutedEntropyProvider(local_state_, low_entropy_source_value, | 184 new CachingPermutedEntropyProvider(local_state_, low_entropy_source_value, |
| 174 kMaxLowEntropySize)); | 185 kMaxLowEntropySize)); |
| 175 #else | 186 #else |
| 176 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( | 187 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( |
| 177 new PermutedEntropyProvider(low_entropy_source_value, | 188 new PermutedEntropyProvider(low_entropy_source_value, |
| 178 kMaxLowEntropySize)); | 189 kMaxLowEntropySize)); |
| 179 #endif | 190 #endif |
| 180 } | 191 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 191 result.reset(new MetricsStateManager(local_state, enabled_state_provider, | 202 result.reset(new MetricsStateManager(local_state, enabled_state_provider, |
| 192 store_client_info, | 203 store_client_info, |
| 193 retrieve_client_info)); | 204 retrieve_client_info)); |
| 194 } | 205 } |
| 195 return result; | 206 return result; |
| 196 } | 207 } |
| 197 | 208 |
| 198 // static | 209 // static |
| 199 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 210 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
| 200 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 211 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
| 201 // registry->RegisterIntegerPref(prefs::kMetricsDefaultOptIn, | |
| 202 // DEFAULT_UNKNOWN); | |
| 203 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); | 212 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); |
| 204 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); | 213 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); |
| 205 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, | 214 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, |
| 206 kLowEntropySourceNotSet); | 215 kLowEntropySourceNotSet); |
| 207 | 216 |
| 208 ClonedInstallDetector::RegisterPrefs(registry); | 217 ClonedInstallDetector::RegisterPrefs(registry); |
| 209 CachingPermutedEntropyProvider::RegisterPrefs(registry); | 218 CachingPermutedEntropyProvider::RegisterPrefs(registry); |
| 210 } | 219 } |
| 211 | 220 |
| 212 void MetricsStateManager::BackUpCurrentClientInfo() { | 221 void MetricsStateManager::BackUpCurrentClientInfo() { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 const base::CommandLine* command_line(base::CommandLine::ForCurrentProcess()); | 276 const base::CommandLine* command_line(base::CommandLine::ForCurrentProcess()); |
| 268 // Only try to load the value from prefs if the user did not request a | 277 // Only try to load the value from prefs if the user did not request a |
| 269 // reset. | 278 // reset. |
| 270 // Otherwise, skip to generating a new value. | 279 // Otherwise, skip to generating a new value. |
| 271 if (!command_line->HasSwitch(switches::kResetVariationState)) { | 280 if (!command_line->HasSwitch(switches::kResetVariationState)) { |
| 272 int value = local_state_->GetInteger(prefs::kMetricsLowEntropySource); | 281 int value = local_state_->GetInteger(prefs::kMetricsLowEntropySource); |
| 273 // If the value is outside the [0, kMaxLowEntropySize) range, re-generate | 282 // If the value is outside the [0, kMaxLowEntropySize) range, re-generate |
| 274 // it below. | 283 // it below. |
| 275 if (value >= 0 && value < kMaxLowEntropySize) { | 284 if (value >= 0 && value < kMaxLowEntropySize) { |
| 276 low_entropy_source_ = value; | 285 low_entropy_source_ = value; |
| 286 LogLowEntropyValue(low_entropy_source_); | |
| 277 return; | 287 return; |
| 278 } | 288 } |
| 279 } | 289 } |
| 280 | 290 |
| 281 low_entropy_source_ = GenerateLowEntropySource(); | 291 low_entropy_source_ = GenerateLowEntropySource(); |
| 292 LogLowEntropyValue(low_entropy_source_); | |
| 282 local_state_->SetInteger(prefs::kMetricsLowEntropySource, | 293 local_state_->SetInteger(prefs::kMetricsLowEntropySource, |
| 283 low_entropy_source_); | 294 low_entropy_source_); |
| 284 CachingPermutedEntropyProvider::ClearCache(local_state_); | 295 CachingPermutedEntropyProvider::ClearCache(local_state_); |
| 285 } | 296 } |
| 286 | 297 |
| 287 void MetricsStateManager::UpdateEntropySourceReturnedValue( | 298 void MetricsStateManager::UpdateEntropySourceReturnedValue( |
| 288 EntropySourceType type) { | 299 EntropySourceType type) { |
| 289 if (entropy_source_returned_ != ENTROPY_SOURCE_NONE) | 300 if (entropy_source_returned_ != ENTROPY_SOURCE_NONE) |
| 290 return; | 301 return; |
| 291 | 302 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 305 | 316 |
| 306 local_state_->ClearPref(prefs::kMetricsClientID); | 317 local_state_->ClearPref(prefs::kMetricsClientID); |
| 307 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 318 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
| 308 local_state_->ClearPref(prefs::kMetricsResetIds); | 319 local_state_->ClearPref(prefs::kMetricsResetIds); |
| 309 | 320 |
| 310 // Also clear the backed up client info. | 321 // Also clear the backed up client info. |
| 311 store_client_info_.Run(ClientInfo()); | 322 store_client_info_.Run(ClientInfo()); |
| 312 } | 323 } |
| 313 | 324 |
| 314 } // namespace metrics | 325 } // namespace metrics |
| OLD | NEW |