Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: components/metrics/metrics_state_manager.cc

Issue 372473004: Retrieve client_id from GoogleUpdateSettings when its missing from Local State. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: +member comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 StoreClientInfoCallback& store_client_info,
50 const LoadClientInfoCallback& 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 store_client_info_(store_client_info),
54 load_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 BackupCurrentClientInfo();
76 return; 84 return;
85 }
77 86
87 scoped_ptr<ClientInfo> client_info_backup = load_client_info_.Run();
88 if (client_info_backup) {
89 client_id_ = client_info_backup->client_id;
90
91 const base::Time now(base::Time::Now());
92
93 // Save the recovered client id and also try to reinstantiate the backup
94 // values for the dates corresponding with that client id in order to avoid
95 // weird scenarios where we could report an old client id with a recent
96 // install date.
97 local_state_->SetString(prefs::kMetricsClientID, client_id_);
98 local_state_->SetInt64(prefs::kInstallDate,
99 client_info_backup->installation_date ?
100 client_info_backup->installation_date :
101 now.ToTimeT());
102 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp,
103 client_info_backup->reporting_enabled_date ?
104 client_info_backup->reporting_enabled_date :
105 now.ToTimeT());
106
107 base::TimeDelta recovered_installation_age;
108 if (client_info_backup->installation_date != 0) {
109 recovered_installation_age =
110 now - base::Time::FromTimeT(client_info_backup->installation_date);
111 }
112 UMA_HISTOGRAM_CUSTOM_TIMES("UMA.ClientIdBackupRecoveredWithAge",
113 recovered_installation_age,
114 base::TimeDelta::FromHours(0),
115 base::TimeDelta::FromDays(365),
116 100);
117
118 // Flush the backup back to persistent storage in case we re-generated
119 // missing data above.
120 BackupCurrentClientInfo();
121 return;
122 }
123
124 // Failing attempts at getting an existing client ID, generate a new one.
78 client_id_ = base::GenerateGUID(); 125 client_id_ = base::GenerateGUID();
79 local_state_->SetString(prefs::kMetricsClientID, client_id_); 126 local_state_->SetString(prefs::kMetricsClientID, client_id_);
80 127
81 if (local_state_->GetString(prefs::kMetricsOldClientID).empty()) { 128 if (local_state_->GetString(prefs::kMetricsOldClientID).empty()) {
82 // Record the timestamp of when the user opted in to UMA. 129 // Record the timestamp of when the user opted in to UMA.
83 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp, 130 local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp,
84 base::Time::Now().ToTimeT()); 131 base::Time::Now().ToTimeT());
85 } else { 132 } else {
86 UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true); 133 UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true);
87 } 134 }
88 local_state_->ClearPref(prefs::kMetricsOldClientID); 135 local_state_->ClearPref(prefs::kMetricsOldClientID);
136
137 BackupCurrentClientInfo();
89 } 138 }
90 139
91 void MetricsStateManager::CheckForClonedInstall( 140 void MetricsStateManager::CheckForClonedInstall(
92 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 141 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
93 DCHECK(!cloned_install_detector_); 142 DCHECK(!cloned_install_detector_);
94 143
95 MachineIdProvider* provider = MachineIdProvider::CreateInstance(); 144 MachineIdProvider* provider = MachineIdProvider::CreateInstance();
96 if (!provider) 145 if (!provider)
97 return; 146 return;
98 147
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 #else 183 #else
135 return scoped_ptr<const base::FieldTrial::EntropyProvider>( 184 return scoped_ptr<const base::FieldTrial::EntropyProvider>(
136 new PermutedEntropyProvider(low_entropy_source_value, 185 new PermutedEntropyProvider(low_entropy_source_value,
137 kMaxLowEntropySize)); 186 kMaxLowEntropySize));
138 #endif 187 #endif
139 } 188 }
140 189
141 // static 190 // static
142 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( 191 scoped_ptr<MetricsStateManager> MetricsStateManager::Create(
143 PrefService* local_state, 192 PrefService* local_state,
144 const base::Callback<bool(void)>& is_reporting_enabled_callback) { 193 const base::Callback<bool(void)>& is_reporting_enabled_callback,
194 const StoreClientInfoCallback& store_client_info,
195 const LoadClientInfoCallback& retrieve_client_info) {
145 scoped_ptr<MetricsStateManager> result; 196 scoped_ptr<MetricsStateManager> result;
146 // Note: |instance_exists_| is updated in the constructor and destructor. 197 // Note: |instance_exists_| is updated in the constructor and destructor.
147 if (!instance_exists_) { 198 if (!instance_exists_) {
148 result.reset( 199 result.reset(new MetricsStateManager(local_state,
149 new MetricsStateManager(local_state, is_reporting_enabled_callback)); 200 is_reporting_enabled_callback,
201 store_client_info,
202 retrieve_client_info));
150 } 203 }
151 return result.Pass(); 204 return result.Pass();
152 } 205 }
153 206
154 // static 207 // static
155 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { 208 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) {
156 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); 209 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false);
157 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); 210 registry->RegisterStringPref(prefs::kMetricsClientID, std::string());
158 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); 211 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0);
159 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, 212 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource,
160 kLowEntropySourceNotSet); 213 kLowEntropySourceNotSet);
161 214
162 ClonedInstallDetector::RegisterPrefs(registry); 215 ClonedInstallDetector::RegisterPrefs(registry);
163 CachingPermutedEntropyProvider::RegisterPrefs(registry); 216 CachingPermutedEntropyProvider::RegisterPrefs(registry);
164 217
165 // TODO(asvitkine): Remove these once a couple of releases have passed. 218 // TODO(asvitkine): Remove these once a couple of releases have passed.
166 // http://crbug.com/357704 219 // http://crbug.com/357704
167 registry->RegisterStringPref(prefs::kMetricsOldClientID, std::string()); 220 registry->RegisterStringPref(prefs::kMetricsOldClientID, std::string());
168 registry->RegisterIntegerPref(prefs::kMetricsOldLowEntropySource, 0); 221 registry->RegisterIntegerPref(prefs::kMetricsOldLowEntropySource, 0);
169 } 222 }
170 223
224 void MetricsStateManager::BackupCurrentClientInfo() {
225 ClientInfo client_info;
226 client_info.client_id = client_id_;
227 client_info.installation_date = local_state_->GetInt64(prefs::kInstallDate);
228 client_info.reporting_enabled_date =
229 local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp);
230 store_client_info_.Run(client_info);
231 }
232
171 int MetricsStateManager::GetLowEntropySource() { 233 int MetricsStateManager::GetLowEntropySource() {
172 // Note that the default value for the low entropy source and the default pref 234 // Note that the default value for the low entropy source and the default pref
173 // value are both kLowEntropySourceNotSet, which is used to identify if the 235 // value are both kLowEntropySourceNotSet, which is used to identify if the
174 // value has been set or not. 236 // value has been set or not.
175 if (low_entropy_source_ != kLowEntropySourceNotSet) 237 if (low_entropy_source_ != kLowEntropySourceNotSet)
176 return low_entropy_source_; 238 return low_entropy_source_;
177 239
178 const CommandLine* command_line(CommandLine::ForCurrentProcess()); 240 const CommandLine* command_line(CommandLine::ForCurrentProcess());
179 // Only try to load the value from prefs if the user did not request a 241 // Only try to load the value from prefs if the user did not request a
180 // reset. 242 // reset.
(...skipping 23 matching lines...) Expand all
204 if (!local_state_->GetBoolean(prefs::kMetricsResetIds)) 266 if (!local_state_->GetBoolean(prefs::kMetricsResetIds))
205 return; 267 return;
206 268
207 UMA_HISTOGRAM_BOOLEAN("UMA.MetricsIDsReset", true); 269 UMA_HISTOGRAM_BOOLEAN("UMA.MetricsIDsReset", true);
208 270
209 DCHECK(client_id_.empty()); 271 DCHECK(client_id_.empty());
210 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); 272 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_);
211 273
212 local_state_->ClearPref(prefs::kMetricsClientID); 274 local_state_->ClearPref(prefs::kMetricsClientID);
213 local_state_->ClearPref(prefs::kMetricsLowEntropySource); 275 local_state_->ClearPref(prefs::kMetricsLowEntropySource);
214 local_state_->ClearPref(prefs::kMetricsResetIds); 276 local_state_->ClearPref(prefs::kMetricsResetIds);
Ilya Sherman 2014/07/11 04:45:41 This should clear the backup as well.
215 } 277 }
216 278
217 } // namespace metrics 279 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698