| 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 "chrome/browser/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/browser/metrics/cloned_install_detector.h" | 16 #include "chrome/browser/metrics/cloned_install_detector.h" |
| 17 #include "chrome/browser/metrics/machine_id_provider.h" | 17 #include "chrome/browser/metrics/machine_id_provider.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "components/variations/caching_permuted_entropy_provider.h" | 20 #include "components/variations/caching_permuted_entropy_provider.h" |
| 21 | 21 |
| 22 #if defined(OS_CHROMEOS) |
| 23 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 24 #endif |
| 25 |
| 22 namespace metrics { | 26 namespace metrics { |
| 23 | 27 |
| 24 namespace { | 28 namespace { |
| 25 | 29 |
| 26 // The argument used to generate a non-identifying entropy source. We want no | 30 // The argument used to generate a non-identifying entropy source. We want no |
| 27 // more than 13 bits of entropy, so use this max to return a number in the range | 31 // more than 13 bits of entropy, so use this max to return a number in the range |
| 28 // [0, 7999] as the entropy source (12.97 bits of entropy). | 32 // [0, 7999] as the entropy source (12.97 bits of entropy). |
| 29 const int kMaxLowEntropySize = 8000; | 33 const int kMaxLowEntropySize = 8000; |
| 30 | 34 |
| 31 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that the | 35 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that the |
| 32 // value has not yet been set. | 36 // value has not yet been set. |
| 33 const int kLowEntropySourceNotSet = -1; | 37 const int kLowEntropySourceNotSet = -1; |
| 34 | 38 |
| 35 // Generates a new non-identifying entropy source used to seed persistent | 39 // Generates a new non-identifying entropy source used to seed persistent |
| 36 // activities. | 40 // activities. |
| 37 int GenerateLowEntropySource() { | 41 int GenerateLowEntropySource() { |
| 38 return base::RandInt(0, kMaxLowEntropySize - 1); | 42 return base::RandInt(0, kMaxLowEntropySize - 1); |
| 39 } | 43 } |
| 40 | 44 |
| 41 } // namespace | 45 } // namespace |
| 42 | 46 |
| 43 // static | 47 // static |
| 44 bool MetricsStateManager::instance_exists_ = false; | 48 bool MetricsStateManager::instance_exists_ = false; |
| 45 | 49 |
| 46 MetricsStateManager::MetricsStateManager( | 50 MetricsStateManager::MetricsStateManager(PrefService* local_state) |
| 47 PrefService* local_state, | |
| 48 const base::Callback<bool(void)>& is_reporting_enabled_callback) | |
| 49 : local_state_(local_state), | 51 : local_state_(local_state), |
| 50 is_reporting_enabled_callback_(is_reporting_enabled_callback), | |
| 51 low_entropy_source_(kLowEntropySourceNotSet), | 52 low_entropy_source_(kLowEntropySourceNotSet), |
| 52 entropy_source_returned_(ENTROPY_SOURCE_NONE) { | 53 entropy_source_returned_(ENTROPY_SOURCE_NONE) { |
| 53 ResetMetricsIDsIfNecessary(); | 54 ResetMetricsIDsIfNecessary(); |
| 54 if (IsMetricsReportingEnabled()) | 55 if (IsMetricsReportingEnabled()) |
| 55 ForceClientIdCreation(); | 56 ForceClientIdCreation(); |
| 56 | 57 |
| 57 DCHECK(!instance_exists_); | 58 DCHECK(!instance_exists_); |
| 58 instance_exists_ = true; | 59 instance_exists_ = true; |
| 59 } | 60 } |
| 60 | 61 |
| 61 MetricsStateManager::~MetricsStateManager() { | 62 MetricsStateManager::~MetricsStateManager() { |
| 62 DCHECK(instance_exists_); | 63 DCHECK(instance_exists_); |
| 63 instance_exists_ = false; | 64 instance_exists_ = false; |
| 64 } | 65 } |
| 65 | 66 |
| 66 bool MetricsStateManager::IsMetricsReportingEnabled() { | 67 bool MetricsStateManager::IsMetricsReportingEnabled() { |
| 67 return is_reporting_enabled_callback_.Run(); | 68 // If the user permits metrics reporting with the checkbox in the |
| 69 // prefs, we turn on recording. We disable metrics completely for |
| 70 // non-official builds. This can be forced with a flag. |
| 71 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 72 if (command_line->HasSwitch(switches::kEnableMetricsReportingForTesting)) |
| 73 return true; |
| 74 |
| 75 // Disable metrics reporting when field trials are forced. |
| 76 if (command_line->HasSwitch(switches::kForceFieldTrials)) |
| 77 return false; |
| 78 |
| 79 bool enabled = false; |
| 80 #if defined(GOOGLE_CHROME_BUILD) |
| 81 #if defined(OS_CHROMEOS) |
| 82 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, |
| 83 &enabled); |
| 84 #else |
| 85 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); |
| 86 #endif // #if defined(OS_CHROMEOS) |
| 87 #endif // defined(GOOGLE_CHROME_BUILD) |
| 88 return enabled; |
| 68 } | 89 } |
| 69 | 90 |
| 70 void MetricsStateManager::ForceClientIdCreation() { | 91 void MetricsStateManager::ForceClientIdCreation() { |
| 71 if (!client_id_.empty()) | 92 if (!client_id_.empty()) |
| 72 return; | 93 return; |
| 73 | 94 |
| 74 client_id_ = local_state_->GetString(prefs::kMetricsClientID); | 95 client_id_ = local_state_->GetString(prefs::kMetricsClientID); |
| 75 if (!client_id_.empty()) | 96 if (!client_id_.empty()) |
| 76 return; | 97 return; |
| 77 | 98 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 kMaxLowEntropySize)); | 154 kMaxLowEntropySize)); |
| 134 #else | 155 #else |
| 135 return scoped_ptr<const base::FieldTrial::EntropyProvider>( | 156 return scoped_ptr<const base::FieldTrial::EntropyProvider>( |
| 136 new PermutedEntropyProvider(low_entropy_source_value, | 157 new PermutedEntropyProvider(low_entropy_source_value, |
| 137 kMaxLowEntropySize)); | 158 kMaxLowEntropySize)); |
| 138 #endif | 159 #endif |
| 139 } | 160 } |
| 140 | 161 |
| 141 // static | 162 // static |
| 142 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( | 163 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( |
| 143 PrefService* local_state, | 164 PrefService* local_state) { |
| 144 const base::Callback<bool(void)>& is_reporting_enabled_callback) { | |
| 145 scoped_ptr<MetricsStateManager> result; | 165 scoped_ptr<MetricsStateManager> result; |
| 146 // Note: |instance_exists_| is updated in the constructor and destructor. | 166 // Note: |instance_exists_| is updated in the constructor and destructor. |
| 147 if (!instance_exists_) { | 167 if (!instance_exists_) |
| 148 result.reset( | 168 result.reset(new MetricsStateManager(local_state)); |
| 149 new MetricsStateManager(local_state, is_reporting_enabled_callback)); | |
| 150 } | |
| 151 return result.Pass(); | 169 return result.Pass(); |
| 152 } | 170 } |
| 153 | 171 |
| 154 // static | 172 // static |
| 155 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 173 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
| 156 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 174 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
| 157 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); | 175 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); |
| 158 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); | 176 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); |
| 159 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, | 177 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, |
| 160 kLowEntropySourceNotSet); | 178 kLowEntropySourceNotSet); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 225 |
| 208 DCHECK(client_id_.empty()); | 226 DCHECK(client_id_.empty()); |
| 209 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); | 227 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); |
| 210 | 228 |
| 211 local_state_->ClearPref(prefs::kMetricsClientID); | 229 local_state_->ClearPref(prefs::kMetricsClientID); |
| 212 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 230 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
| 213 local_state_->ClearPref(prefs::kMetricsResetIds); | 231 local_state_->ClearPref(prefs::kMetricsResetIds); |
| 214 } | 232 } |
| 215 | 233 |
| 216 } // namespace metrics | 234 } // namespace metrics |
| OLD | NEW |