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 "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" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 return base::RandInt(0, kMaxLowEntropySize - 1); | 38 return base::RandInt(0, kMaxLowEntropySize - 1); |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 // static | 43 // static |
| 44 bool MetricsStateManager::instance_exists_ = false; | 44 bool MetricsStateManager::instance_exists_ = false; |
| 45 | 45 |
| 46 MetricsStateManager::MetricsStateManager( | 46 MetricsStateManager::MetricsStateManager( |
| 47 PrefService* local_state, | 47 PrefService* local_state, |
| 48 const base::Callback<bool(void)>& is_reporting_enabled_callback) | 48 const base::Callback<bool(void)>& is_reporting_enabled_callback, |
| 49 const SaveClientInfoCallback& save_client_info, | |
| 50 const RetrieveClientInfoCallback& retrieve_client_info) | |
| 49 : local_state_(local_state), | 51 : local_state_(local_state), |
| 50 is_reporting_enabled_callback_(is_reporting_enabled_callback), | 52 is_reporting_enabled_callback_(is_reporting_enabled_callback), |
| 53 save_client_info_(save_client_info), | |
| 54 retrieve_client_info_(retrieve_client_info), | |
| 51 low_entropy_source_(kLowEntropySourceNotSet), | 55 low_entropy_source_(kLowEntropySourceNotSet), |
| 52 entropy_source_returned_(ENTROPY_SOURCE_NONE) { | 56 entropy_source_returned_(ENTROPY_SOURCE_NONE) { |
| 53 ResetMetricsIDsIfNecessary(); | 57 ResetMetricsIDsIfNecessary(); |
| 54 if (IsMetricsReportingEnabled()) | 58 if (IsMetricsReportingEnabled()) |
| 55 ForceClientIdCreation(); | 59 ForceClientIdCreation(); |
| 56 | 60 |
| 57 DCHECK(!instance_exists_); | 61 DCHECK(!instance_exists_); |
| 58 instance_exists_ = true; | 62 instance_exists_ = true; |
| 59 } | 63 } |
| 60 | 64 |
| 61 MetricsStateManager::~MetricsStateManager() { | 65 MetricsStateManager::~MetricsStateManager() { |
| 62 DCHECK(instance_exists_); | 66 DCHECK(instance_exists_); |
| 63 instance_exists_ = false; | 67 instance_exists_ = false; |
| 64 } | 68 } |
| 65 | 69 |
| 66 bool MetricsStateManager::IsMetricsReportingEnabled() { | 70 bool MetricsStateManager::IsMetricsReportingEnabled() { |
| 67 return is_reporting_enabled_callback_.Run(); | 71 return is_reporting_enabled_callback_.Run(); |
| 68 } | 72 } |
| 69 | 73 |
| 70 void MetricsStateManager::ForceClientIdCreation() { | 74 void MetricsStateManager::ForceClientIdCreation() { |
| 71 if (!client_id_.empty()) | 75 if (!client_id_.empty()) |
| 72 return; | 76 return; |
| 73 | 77 |
| 74 client_id_ = local_state_->GetString(prefs::kMetricsClientID); | 78 client_id_ = local_state_->GetString(prefs::kMetricsClientID); |
| 75 if (!client_id_.empty()) | 79 if (!client_id_.empty()) { |
| 80 // It is technically sufficient to only save a backup of the client id when | |
| 81 // it is initially generated below, but since the backup was only introduced | |
| 82 // in M38, seed it explicitly from here for some time. | |
| 83 const int64 installation_date = local_state_->GetInt64(prefs::kInstallDate); | |
| 84 save_client_info_.Run(client_id_, installation_date); | |
| 76 return; | 85 return; |
| 86 } | |
| 77 | 87 |
| 88 std::string client_id_backup; | |
| 89 int64 installation_date_backup = 0; | |
| 90 if (retrieve_client_info_.Run(&client_id_backup, &installation_date_backup) && | |
| 91 !client_id_backup.empty()) { | |
| 92 client_id_ = client_id_backup; | |
| 93 | |
| 94 const base::Time now(base::Time::Now()); | |
| 95 if (installation_date_backup == 0) | |
| 96 installation_date_backup = now.ToTimeT(); | |
| 97 // Save the recovered client id and set the install date to match the | |
| 98 // recovered client id in order to avoid weird scenarios where we could | |
| 99 // report an old client id with a recent install date. | |
| 100 local_state_->SetString(prefs::kMetricsClientID, client_id_); | |
| 101 local_state_->SetInt64(prefs::kInstallDate, installation_date_backup); | |
| 102 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp, | |
| 103 now.ToTimeT()); | |
| 104 | |
| 105 base::TimeDelta recovered_installation_age; | |
| 106 if (installation_date_backup != 0) { | |
|
Ilya Sherman
2014/07/08 01:10:22
Given lines 95-96, I don't think this check does a
| |
| 107 recovered_installation_age = | |
| 108 now - base::Time::FromTimeT(installation_date_backup); | |
| 109 } | |
| 110 UMA_HISTOGRAM_CUSTOM_TIMES("UMA.ClientIdBackupRecoveredWithAge", | |
| 111 recovered_installation_age, | |
| 112 base::TimeDelta::FromHours(0), | |
| 113 base::TimeDelta::FromDays(365), | |
| 114 100); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 // Failing attempts at getting an existing client ID, generate a new one. | |
| 78 client_id_ = base::GenerateGUID(); | 119 client_id_ = base::GenerateGUID(); |
| 79 local_state_->SetString(prefs::kMetricsClientID, client_id_); | 120 local_state_->SetString(prefs::kMetricsClientID, client_id_); |
| 80 | 121 |
| 81 if (local_state_->GetString(prefs::kMetricsOldClientID).empty()) { | 122 if (local_state_->GetString(prefs::kMetricsOldClientID).empty()) { |
| 82 // Record the timestamp of when the user opted in to UMA. | 123 // Record the timestamp of when the user opted in to UMA. |
| 83 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp, | 124 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp, |
| 84 base::Time::Now().ToTimeT()); | 125 base::Time::Now().ToTimeT()); |
| 85 } else { | 126 } else { |
| 86 UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true); | 127 UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true); |
| 87 } | 128 } |
| 88 local_state_->ClearPref(prefs::kMetricsOldClientID); | 129 local_state_->ClearPref(prefs::kMetricsOldClientID); |
| 130 | |
| 131 const int64 installation_date = local_state_->GetInt64(prefs::kInstallDate); | |
| 132 save_client_info_.Run(client_id_, installation_date); | |
| 89 } | 133 } |
| 90 | 134 |
| 91 void MetricsStateManager::CheckForClonedInstall( | 135 void MetricsStateManager::CheckForClonedInstall( |
| 92 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 136 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 93 DCHECK(!cloned_install_detector_); | 137 DCHECK(!cloned_install_detector_); |
| 94 | 138 |
| 95 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); | 139 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); |
| 96 if (!provider) | 140 if (!provider) |
| 97 return; | 141 return; |
| 98 | 142 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 #else | 178 #else |
| 135 return scoped_ptr<const base::FieldTrial::EntropyProvider>( | 179 return scoped_ptr<const base::FieldTrial::EntropyProvider>( |
| 136 new PermutedEntropyProvider(low_entropy_source_value, | 180 new PermutedEntropyProvider(low_entropy_source_value, |
| 137 kMaxLowEntropySize)); | 181 kMaxLowEntropySize)); |
| 138 #endif | 182 #endif |
| 139 } | 183 } |
| 140 | 184 |
| 141 // static | 185 // static |
| 142 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( | 186 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( |
| 143 PrefService* local_state, | 187 PrefService* local_state, |
| 144 const base::Callback<bool(void)>& is_reporting_enabled_callback) { | 188 const base::Callback<bool(void)>& is_reporting_enabled_callback, |
| 189 const SaveClientInfoCallback& save_client_info, | |
| 190 const RetrieveClientInfoCallback& retrieve_client_info) { | |
| 145 scoped_ptr<MetricsStateManager> result; | 191 scoped_ptr<MetricsStateManager> result; |
| 146 // Note: |instance_exists_| is updated in the constructor and destructor. | 192 // Note: |instance_exists_| is updated in the constructor and destructor. |
| 147 if (!instance_exists_) { | 193 if (!instance_exists_) { |
| 148 result.reset( | 194 result.reset(new MetricsStateManager(local_state, |
| 149 new MetricsStateManager(local_state, is_reporting_enabled_callback)); | 195 is_reporting_enabled_callback, |
| 196 save_client_info, | |
| 197 retrieve_client_info)); | |
| 150 } | 198 } |
| 151 return result.Pass(); | 199 return result.Pass(); |
| 152 } | 200 } |
| 153 | 201 |
| 154 // static | 202 // static |
| 155 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 203 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
| 156 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 204 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
| 157 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); | 205 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); |
| 158 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); | 206 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); |
| 159 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, | 207 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 | 256 |
| 209 DCHECK(client_id_.empty()); | 257 DCHECK(client_id_.empty()); |
| 210 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); | 258 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); |
| 211 | 259 |
| 212 local_state_->ClearPref(prefs::kMetricsClientID); | 260 local_state_->ClearPref(prefs::kMetricsClientID); |
| 213 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 261 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
| 214 local_state_->ClearPref(prefs::kMetricsResetIds); | 262 local_state_->ClearPref(prefs::kMetricsResetIds); |
| 215 } | 263 } |
| 216 | 264 |
| 217 } // namespace metrics | 265 } // namespace metrics |
| OLD | NEW |