| 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 "chrome/browser/metrics/metrics_state_manager.h" | 5 #include "components/metrics/metrics_state_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| 11 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "components/metrics/cloned_install_detector.h" | 16 #include "components/metrics/cloned_install_detector.h" |
| 19 #include "components/metrics/machine_id_provider.h" | 17 #include "components/metrics/machine_id_provider.h" |
| 20 #include "components/metrics/metrics_pref_names.h" | 18 #include "components/metrics/metrics_pref_names.h" |
| 19 #include "components/metrics/metrics_switches.h" |
| 21 #include "components/variations/caching_permuted_entropy_provider.h" | 20 #include "components/variations/caching_permuted_entropy_provider.h" |
| 22 | 21 |
| 23 namespace metrics { | 22 namespace metrics { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 // The argument used to generate a non-identifying entropy source. We want no | 26 // The argument used to generate a non-identifying entropy source. We want no |
| 28 // more than 13 bits of entropy, so use this max to return a number in the range | 27 // more than 13 bits of entropy, so use this max to return a number in the range |
| 29 // [0, 7999] as the entropy source (12.97 bits of entropy). | 28 // [0, 7999] as the entropy source (12.97 bits of entropy). |
| 30 const int kMaxLowEntropySize = 8000; | 29 const int kMaxLowEntropySize = 8000; |
| 31 | 30 |
| 32 // Default prefs value for ::prefs::kMetricsLowEntropySource to indicate that | 31 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that |
| 33 // the value has not yet been set. | 32 // the value has not yet been set. |
| 34 const int kLowEntropySourceNotSet = -1; | 33 const int kLowEntropySourceNotSet = -1; |
| 35 | 34 |
| 36 // Generates a new non-identifying entropy source used to seed persistent | 35 // Generates a new non-identifying entropy source used to seed persistent |
| 37 // activities. | 36 // activities. |
| 38 int GenerateLowEntropySource() { | 37 int GenerateLowEntropySource() { |
| 39 return base::RandInt(0, kMaxLowEntropySize - 1); | 38 return base::RandInt(0, kMaxLowEntropySize - 1); |
| 40 } | 39 } |
| 41 | 40 |
| 42 } // namespace | 41 } // namespace |
| (...skipping 22 matching lines...) Expand all Loading... |
| 65 } | 64 } |
| 66 | 65 |
| 67 bool MetricsStateManager::IsMetricsReportingEnabled() { | 66 bool MetricsStateManager::IsMetricsReportingEnabled() { |
| 68 return is_reporting_enabled_callback_.Run(); | 67 return is_reporting_enabled_callback_.Run(); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void MetricsStateManager::ForceClientIdCreation() { | 70 void MetricsStateManager::ForceClientIdCreation() { |
| 72 if (!client_id_.empty()) | 71 if (!client_id_.empty()) |
| 73 return; | 72 return; |
| 74 | 73 |
| 75 client_id_ = local_state_->GetString(::prefs::kMetricsClientID); | 74 client_id_ = local_state_->GetString(prefs::kMetricsClientID); |
| 76 if (!client_id_.empty()) | 75 if (!client_id_.empty()) |
| 77 return; | 76 return; |
| 78 | 77 |
| 79 client_id_ = base::GenerateGUID(); | 78 client_id_ = base::GenerateGUID(); |
| 80 local_state_->SetString(::prefs::kMetricsClientID, client_id_); | 79 local_state_->SetString(prefs::kMetricsClientID, client_id_); |
| 81 | 80 |
| 82 if (local_state_->GetString(::prefs::kMetricsOldClientID).empty()) { | 81 if (local_state_->GetString(prefs::kMetricsOldClientID).empty()) { |
| 83 // Record the timestamp of when the user opted in to UMA. | 82 // Record the timestamp of when the user opted in to UMA. |
| 84 local_state_->SetInt64(::prefs::kMetricsReportingEnabledTimestamp, | 83 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp, |
| 85 base::Time::Now().ToTimeT()); | 84 base::Time::Now().ToTimeT()); |
| 86 } else { | 85 } else { |
| 87 UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true); | 86 UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true); |
| 88 } | 87 } |
| 89 local_state_->ClearPref(::prefs::kMetricsOldClientID); | 88 local_state_->ClearPref(prefs::kMetricsOldClientID); |
| 90 } | 89 } |
| 91 | 90 |
| 92 void MetricsStateManager::CheckForClonedInstall( | 91 void MetricsStateManager::CheckForClonedInstall( |
| 93 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 92 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 94 DCHECK(!cloned_install_detector_); | 93 DCHECK(!cloned_install_detector_); |
| 95 | 94 |
| 96 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); | 95 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); |
| 97 if (!provider) | 96 if (!provider) |
| 98 return; | 97 return; |
| 99 | 98 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (!instance_exists_) { | 147 if (!instance_exists_) { |
| 149 result.reset( | 148 result.reset( |
| 150 new MetricsStateManager(local_state, is_reporting_enabled_callback)); | 149 new MetricsStateManager(local_state, is_reporting_enabled_callback)); |
| 151 } | 150 } |
| 152 return result.Pass(); | 151 return result.Pass(); |
| 153 } | 152 } |
| 154 | 153 |
| 155 // static | 154 // static |
| 156 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 155 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
| 157 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 156 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
| 158 registry->RegisterStringPref(::prefs::kMetricsClientID, std::string()); | 157 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); |
| 159 registry->RegisterInt64Pref(::prefs::kMetricsReportingEnabledTimestamp, 0); | 158 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); |
| 160 registry->RegisterIntegerPref(::prefs::kMetricsLowEntropySource, | 159 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, |
| 161 kLowEntropySourceNotSet); | 160 kLowEntropySourceNotSet); |
| 162 | 161 |
| 163 ClonedInstallDetector::RegisterPrefs(registry); | 162 ClonedInstallDetector::RegisterPrefs(registry); |
| 164 CachingPermutedEntropyProvider::RegisterPrefs(registry); | 163 CachingPermutedEntropyProvider::RegisterPrefs(registry); |
| 165 | 164 |
| 166 // TODO(asvitkine): Remove these once a couple of releases have passed. | 165 // TODO(asvitkine): Remove these once a couple of releases have passed. |
| 167 // http://crbug.com/357704 | 166 // http://crbug.com/357704 |
| 168 registry->RegisterStringPref(::prefs::kMetricsOldClientID, std::string()); | 167 registry->RegisterStringPref(prefs::kMetricsOldClientID, std::string()); |
| 169 registry->RegisterIntegerPref(::prefs::kMetricsOldLowEntropySource, 0); | 168 registry->RegisterIntegerPref(prefs::kMetricsOldLowEntropySource, 0); |
| 170 } | 169 } |
| 171 | 170 |
| 172 int MetricsStateManager::GetLowEntropySource() { | 171 int MetricsStateManager::GetLowEntropySource() { |
| 173 // Note that the default value for the low entropy source and the default pref | 172 // Note that the default value for the low entropy source and the default pref |
| 174 // value are both kLowEntropySourceNotSet, which is used to identify if the | 173 // value are both kLowEntropySourceNotSet, which is used to identify if the |
| 175 // value has been set or not. | 174 // value has been set or not. |
| 176 if (low_entropy_source_ != kLowEntropySourceNotSet) | 175 if (low_entropy_source_ != kLowEntropySourceNotSet) |
| 177 return low_entropy_source_; | 176 return low_entropy_source_; |
| 178 | 177 |
| 179 const CommandLine* command_line(CommandLine::ForCurrentProcess()); | 178 const CommandLine* command_line(CommandLine::ForCurrentProcess()); |
| 180 // Only try to load the value from ::prefs if the user did not request a | 179 // Only try to load the value from prefs if the user did not request a |
| 181 // reset. | 180 // reset. |
| 182 // Otherwise, skip to generating a new value. | 181 // Otherwise, skip to generating a new value. |
| 183 if (!command_line->HasSwitch(switches::kResetVariationState)) { | 182 if (!command_line->HasSwitch(switches::kResetVariationState)) { |
| 184 int value = local_state_->GetInteger(::prefs::kMetricsLowEntropySource); | 183 int value = local_state_->GetInteger(prefs::kMetricsLowEntropySource); |
| 185 // If the value is outside the [0, kMaxLowEntropySize) range, re-generate | 184 // If the value is outside the [0, kMaxLowEntropySize) range, re-generate |
| 186 // it below. | 185 // it below. |
| 187 if (value >= 0 && value < kMaxLowEntropySize) { | 186 if (value >= 0 && value < kMaxLowEntropySize) { |
| 188 low_entropy_source_ = value; | 187 low_entropy_source_ = value; |
| 189 UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", false); | 188 UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", false); |
| 190 return low_entropy_source_; | 189 return low_entropy_source_; |
| 191 } | 190 } |
| 192 } | 191 } |
| 193 | 192 |
| 194 UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", true); | 193 UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", true); |
| 195 low_entropy_source_ = GenerateLowEntropySource(); | 194 low_entropy_source_ = GenerateLowEntropySource(); |
| 196 local_state_->SetInteger(::prefs::kMetricsLowEntropySource, | 195 local_state_->SetInteger(prefs::kMetricsLowEntropySource, |
| 197 low_entropy_source_); | 196 low_entropy_source_); |
| 198 local_state_->ClearPref(::prefs::kMetricsOldLowEntropySource); | 197 local_state_->ClearPref(prefs::kMetricsOldLowEntropySource); |
| 199 CachingPermutedEntropyProvider::ClearCache(local_state_); | 198 CachingPermutedEntropyProvider::ClearCache(local_state_); |
| 200 | 199 |
| 201 return low_entropy_source_; | 200 return low_entropy_source_; |
| 202 } | 201 } |
| 203 | 202 |
| 204 void MetricsStateManager::ResetMetricsIDsIfNecessary() { | 203 void MetricsStateManager::ResetMetricsIDsIfNecessary() { |
| 205 if (!local_state_->GetBoolean(prefs::kMetricsResetIds)) | 204 if (!local_state_->GetBoolean(prefs::kMetricsResetIds)) |
| 206 return; | 205 return; |
| 207 | 206 |
| 208 UMA_HISTOGRAM_BOOLEAN("UMA.MetricsIDsReset", true); | 207 UMA_HISTOGRAM_BOOLEAN("UMA.MetricsIDsReset", true); |
| 209 | 208 |
| 210 DCHECK(client_id_.empty()); | 209 DCHECK(client_id_.empty()); |
| 211 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); | 210 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); |
| 212 | 211 |
| 213 local_state_->ClearPref(::prefs::kMetricsClientID); | 212 local_state_->ClearPref(prefs::kMetricsClientID); |
| 214 local_state_->ClearPref(::prefs::kMetricsLowEntropySource); | 213 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
| 215 local_state_->ClearPref(prefs::kMetricsResetIds); | 214 local_state_->ClearPref(prefs::kMetricsResetIds); |
| 216 } | 215 } |
| 217 | 216 |
| 218 } // namespace metrics | 217 } // namespace metrics |
| OLD | NEW |