| OLD | NEW | 
|---|
|  | (Empty) | 
| 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 |  | 
| 3 // found in the LICENSE file. |  | 
| 4 |  | 
| 5 #include "chrome/browser/metrics/metrics_state_manager.h" |  | 
| 6 |  | 
| 7 #include "base/command_line.h" |  | 
| 8 #include "base/guid.h" |  | 
| 9 #include "base/metrics/histogram.h" |  | 
| 10 #include "base/metrics/sparse_histogram.h" |  | 
| 11 #include "base/prefs/pref_registry_simple.h" |  | 
| 12 #include "base/prefs/pref_service.h" |  | 
| 13 #include "base/rand_util.h" |  | 
| 14 #include "base/strings/string_number_conversions.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" |  | 
| 19 #include "components/metrics/machine_id_provider.h" |  | 
| 20 #include "components/metrics/metrics_pref_names.h" |  | 
| 21 #include "components/variations/caching_permuted_entropy_provider.h" |  | 
| 22 |  | 
| 23 namespace metrics { |  | 
| 24 |  | 
| 25 namespace { |  | 
| 26 |  | 
| 27 // 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 |  | 
| 29 // [0, 7999] as the entropy source (12.97 bits of entropy). |  | 
| 30 const int kMaxLowEntropySize = 8000; |  | 
| 31 |  | 
| 32 // Default prefs value for ::prefs::kMetricsLowEntropySource to indicate that |  | 
| 33 // the value has not yet been set. |  | 
| 34 const int kLowEntropySourceNotSet = -1; |  | 
| 35 |  | 
| 36 // Generates a new non-identifying entropy source used to seed persistent |  | 
| 37 // activities. |  | 
| 38 int GenerateLowEntropySource() { |  | 
| 39   return base::RandInt(0, kMaxLowEntropySize - 1); |  | 
| 40 } |  | 
| 41 |  | 
| 42 }  // namespace |  | 
| 43 |  | 
| 44 // static |  | 
| 45 bool MetricsStateManager::instance_exists_ = false; |  | 
| 46 |  | 
| 47 MetricsStateManager::MetricsStateManager( |  | 
| 48     PrefService* local_state, |  | 
| 49     const base::Callback<bool(void)>& is_reporting_enabled_callback) |  | 
| 50     : local_state_(local_state), |  | 
| 51       is_reporting_enabled_callback_(is_reporting_enabled_callback), |  | 
| 52       low_entropy_source_(kLowEntropySourceNotSet), |  | 
| 53       entropy_source_returned_(ENTROPY_SOURCE_NONE) { |  | 
| 54   ResetMetricsIDsIfNecessary(); |  | 
| 55   if (IsMetricsReportingEnabled()) |  | 
| 56     ForceClientIdCreation(); |  | 
| 57 |  | 
| 58   DCHECK(!instance_exists_); |  | 
| 59   instance_exists_ = true; |  | 
| 60 } |  | 
| 61 |  | 
| 62 MetricsStateManager::~MetricsStateManager() { |  | 
| 63   DCHECK(instance_exists_); |  | 
| 64   instance_exists_ = false; |  | 
| 65 } |  | 
| 66 |  | 
| 67 bool MetricsStateManager::IsMetricsReportingEnabled() { |  | 
| 68   return is_reporting_enabled_callback_.Run(); |  | 
| 69 } |  | 
| 70 |  | 
| 71 void MetricsStateManager::ForceClientIdCreation() { |  | 
| 72   if (!client_id_.empty()) |  | 
| 73     return; |  | 
| 74 |  | 
| 75   client_id_ = local_state_->GetString(::prefs::kMetricsClientID); |  | 
| 76   if (!client_id_.empty()) |  | 
| 77     return; |  | 
| 78 |  | 
| 79   client_id_ = base::GenerateGUID(); |  | 
| 80   local_state_->SetString(::prefs::kMetricsClientID, client_id_); |  | 
| 81 |  | 
| 82   if (local_state_->GetString(::prefs::kMetricsOldClientID).empty()) { |  | 
| 83     // Record the timestamp of when the user opted in to UMA. |  | 
| 84     local_state_->SetInt64(::prefs::kMetricsReportingEnabledTimestamp, |  | 
| 85                            base::Time::Now().ToTimeT()); |  | 
| 86   } else { |  | 
| 87     UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true); |  | 
| 88   } |  | 
| 89   local_state_->ClearPref(::prefs::kMetricsOldClientID); |  | 
| 90 } |  | 
| 91 |  | 
| 92 void MetricsStateManager::CheckForClonedInstall( |  | 
| 93     scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |  | 
| 94   DCHECK(!cloned_install_detector_); |  | 
| 95 |  | 
| 96   MachineIdProvider* provider = MachineIdProvider::CreateInstance(); |  | 
| 97   if (!provider) |  | 
| 98     return; |  | 
| 99 |  | 
| 100   cloned_install_detector_.reset(new ClonedInstallDetector(provider)); |  | 
| 101   cloned_install_detector_->CheckForClonedInstall(local_state_, task_runner); |  | 
| 102 } |  | 
| 103 |  | 
| 104 scoped_ptr<const base::FieldTrial::EntropyProvider> |  | 
| 105 MetricsStateManager::CreateEntropyProvider() { |  | 
| 106   // For metrics reporting-enabled users, we combine the client ID and low |  | 
| 107   // entropy source to get the final entropy source. Otherwise, only use the low |  | 
| 108   // entropy source. |  | 
| 109   // This has two useful properties: |  | 
| 110   //  1) It makes the entropy source less identifiable for parties that do not |  | 
| 111   //     know the low entropy source. |  | 
| 112   //  2) It makes the final entropy source resettable. |  | 
| 113   const int low_entropy_source_value = GetLowEntropySource(); |  | 
| 114   UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue", |  | 
| 115                               low_entropy_source_value); |  | 
| 116   if (IsMetricsReportingEnabled()) { |  | 
| 117     if (entropy_source_returned_ == ENTROPY_SOURCE_NONE) |  | 
| 118       entropy_source_returned_ = ENTROPY_SOURCE_HIGH; |  | 
| 119     DCHECK_EQ(ENTROPY_SOURCE_HIGH, entropy_source_returned_); |  | 
| 120     const std::string high_entropy_source = |  | 
| 121         client_id_ + base::IntToString(low_entropy_source_value); |  | 
| 122     return scoped_ptr<const base::FieldTrial::EntropyProvider>( |  | 
| 123         new SHA1EntropyProvider(high_entropy_source)); |  | 
| 124   } |  | 
| 125 |  | 
| 126   if (entropy_source_returned_ == ENTROPY_SOURCE_NONE) |  | 
| 127     entropy_source_returned_ = ENTROPY_SOURCE_LOW; |  | 
| 128   DCHECK_EQ(ENTROPY_SOURCE_LOW, entropy_source_returned_); |  | 
| 129 |  | 
| 130 #if defined(OS_ANDROID) || defined(OS_IOS) |  | 
| 131   return scoped_ptr<const base::FieldTrial::EntropyProvider>( |  | 
| 132       new CachingPermutedEntropyProvider(local_state_, |  | 
| 133                                          low_entropy_source_value, |  | 
| 134                                          kMaxLowEntropySize)); |  | 
| 135 #else |  | 
| 136   return scoped_ptr<const base::FieldTrial::EntropyProvider>( |  | 
| 137       new PermutedEntropyProvider(low_entropy_source_value, |  | 
| 138                                   kMaxLowEntropySize)); |  | 
| 139 #endif |  | 
| 140 } |  | 
| 141 |  | 
| 142 // static |  | 
| 143 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( |  | 
| 144     PrefService* local_state, |  | 
| 145     const base::Callback<bool(void)>& is_reporting_enabled_callback) { |  | 
| 146   scoped_ptr<MetricsStateManager> result; |  | 
| 147   // Note: |instance_exists_| is updated in the constructor and destructor. |  | 
| 148   if (!instance_exists_) { |  | 
| 149     result.reset( |  | 
| 150         new MetricsStateManager(local_state, is_reporting_enabled_callback)); |  | 
| 151   } |  | 
| 152   return result.Pass(); |  | 
| 153 } |  | 
| 154 |  | 
| 155 // static |  | 
| 156 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |  | 
| 157   registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |  | 
| 158   registry->RegisterStringPref(::prefs::kMetricsClientID, std::string()); |  | 
| 159   registry->RegisterInt64Pref(::prefs::kMetricsReportingEnabledTimestamp, 0); |  | 
| 160   registry->RegisterIntegerPref(::prefs::kMetricsLowEntropySource, |  | 
| 161                                 kLowEntropySourceNotSet); |  | 
| 162 |  | 
| 163   ClonedInstallDetector::RegisterPrefs(registry); |  | 
| 164   CachingPermutedEntropyProvider::RegisterPrefs(registry); |  | 
| 165 |  | 
| 166   // TODO(asvitkine): Remove these once a couple of releases have passed. |  | 
| 167   // http://crbug.com/357704 |  | 
| 168   registry->RegisterStringPref(::prefs::kMetricsOldClientID, std::string()); |  | 
| 169   registry->RegisterIntegerPref(::prefs::kMetricsOldLowEntropySource, 0); |  | 
| 170 } |  | 
| 171 |  | 
| 172 int MetricsStateManager::GetLowEntropySource() { |  | 
| 173   // 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 |  | 
| 175   // value has been set or not. |  | 
| 176   if (low_entropy_source_ != kLowEntropySourceNotSet) |  | 
| 177     return low_entropy_source_; |  | 
| 178 |  | 
| 179   const CommandLine* command_line(CommandLine::ForCurrentProcess()); |  | 
| 180   // Only try to load the value from ::prefs if the user did not request a |  | 
| 181   // reset. |  | 
| 182   // Otherwise, skip to generating a new value. |  | 
| 183   if (!command_line->HasSwitch(switches::kResetVariationState)) { |  | 
| 184     int value = local_state_->GetInteger(::prefs::kMetricsLowEntropySource); |  | 
| 185     // If the value is outside the [0, kMaxLowEntropySize) range, re-generate |  | 
| 186     // it below. |  | 
| 187     if (value >= 0 && value < kMaxLowEntropySize) { |  | 
| 188       low_entropy_source_ = value; |  | 
| 189       UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", false); |  | 
| 190       return low_entropy_source_; |  | 
| 191     } |  | 
| 192   } |  | 
| 193 |  | 
| 194   UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", true); |  | 
| 195   low_entropy_source_ = GenerateLowEntropySource(); |  | 
| 196   local_state_->SetInteger(::prefs::kMetricsLowEntropySource, |  | 
| 197                            low_entropy_source_); |  | 
| 198   local_state_->ClearPref(::prefs::kMetricsOldLowEntropySource); |  | 
| 199   CachingPermutedEntropyProvider::ClearCache(local_state_); |  | 
| 200 |  | 
| 201   return low_entropy_source_; |  | 
| 202 } |  | 
| 203 |  | 
| 204 void MetricsStateManager::ResetMetricsIDsIfNecessary() { |  | 
| 205   if (!local_state_->GetBoolean(prefs::kMetricsResetIds)) |  | 
| 206     return; |  | 
| 207 |  | 
| 208   UMA_HISTOGRAM_BOOLEAN("UMA.MetricsIDsReset", true); |  | 
| 209 |  | 
| 210   DCHECK(client_id_.empty()); |  | 
| 211   DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); |  | 
| 212 |  | 
| 213   local_state_->ClearPref(::prefs::kMetricsClientID); |  | 
| 214   local_state_->ClearPref(::prefs::kMetricsLowEntropySource); |  | 
| 215   local_state_->ClearPref(prefs::kMetricsResetIds); |  | 
| 216 } |  | 
| 217 |  | 
| 218 }  // namespace metrics |  | 
| OLD | NEW | 
|---|