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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/guid.h" | 10 #include "base/guid.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 void MetricsStateManager::ForceClientIdCreation() { | 85 void MetricsStateManager::ForceClientIdCreation() { |
86 { | 86 { |
87 std::string client_id_from_prefs = | 87 std::string client_id_from_prefs = |
88 local_state_->GetString(prefs::kMetricsClientID); | 88 local_state_->GetString(prefs::kMetricsClientID); |
89 // If client id in prefs matches the cached copy, return early. | 89 // If client id in prefs matches the cached copy, return early. |
90 if (!client_id_from_prefs.empty() && client_id_from_prefs == client_id_) | 90 if (!client_id_from_prefs.empty() && client_id_from_prefs == client_id_) |
91 return; | 91 return; |
92 client_id_.swap(client_id_from_prefs); | 92 client_id_.swap(client_id_from_prefs); |
93 } | 93 } |
94 | 94 |
95 if (!client_id_.empty()) { | 95 if (!client_id_.empty()) |
96 // It is technically sufficient to only save a backup of the client id when | |
97 // it is initially generated below, but since the backup was only introduced | |
98 // in M38, seed it explicitly from here for some time. | |
99 BackUpCurrentClientInfo(); | |
100 return; | 96 return; |
101 } | |
102 | 97 |
103 const std::unique_ptr<ClientInfo> client_info_backup = | 98 const std::unique_ptr<ClientInfo> client_info_backup = LoadClientInfo(); |
104 LoadClientInfoAndMaybeMigrate(); | |
105 if (client_info_backup) { | 99 if (client_info_backup) { |
106 client_id_ = client_info_backup->client_id; | 100 client_id_ = client_info_backup->client_id; |
107 | 101 |
108 const base::Time now = base::Time::Now(); | 102 const base::Time now = base::Time::Now(); |
109 | 103 |
110 // Save the recovered client id and also try to reinstantiate the backup | 104 // Save the recovered client id and also try to reinstantiate the backup |
111 // values for the dates corresponding with that client id in order to avoid | 105 // values for the dates corresponding with that client id in order to avoid |
112 // weird scenarios where we could report an old client id with a recent | 106 // weird scenarios where we could report an old client id with a recent |
113 // install date. | 107 // install date. |
114 local_state_->SetString(prefs::kMetricsClientID, client_id_); | 108 local_state_->SetString(prefs::kMetricsClientID, client_id_); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 220 |
227 void MetricsStateManager::BackUpCurrentClientInfo() { | 221 void MetricsStateManager::BackUpCurrentClientInfo() { |
228 ClientInfo client_info; | 222 ClientInfo client_info; |
229 client_info.client_id = client_id_; | 223 client_info.client_id = client_id_; |
230 client_info.installation_date = local_state_->GetInt64(prefs::kInstallDate); | 224 client_info.installation_date = local_state_->GetInt64(prefs::kInstallDate); |
231 client_info.reporting_enabled_date = | 225 client_info.reporting_enabled_date = |
232 local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp); | 226 local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp); |
233 store_client_info_.Run(client_info); | 227 store_client_info_.Run(client_info); |
234 } | 228 } |
235 | 229 |
236 std::unique_ptr<ClientInfo> | 230 std::unique_ptr<ClientInfo> MetricsStateManager::LoadClientInfo() { |
237 MetricsStateManager::LoadClientInfoAndMaybeMigrate() { | |
238 std::unique_ptr<ClientInfo> client_info = load_client_info_.Run(); | 231 std::unique_ptr<ClientInfo> client_info = load_client_info_.Run(); |
239 | 232 |
240 // Prior to 2014-07, the client ID was stripped of its dashes before being | 233 // The GUID retrieved should be valid unless retrieval failed. |
241 // saved. Migrate back to a proper GUID if this is the case. This migration | 234 // If not, return nullptr. This will result in a new GUID being generated by |
242 // code can be removed in M41+. | 235 // the calling function ForceClientIdCreation(). |
243 const size_t kGUIDLengthWithoutDashes = 32U; | |
244 if (client_info && | |
245 client_info->client_id.length() == kGUIDLengthWithoutDashes) { | |
246 DCHECK(client_info->client_id.find('-') == std::string::npos); | |
247 | |
248 std::string client_id_with_dashes; | |
249 client_id_with_dashes.reserve(kGUIDLengthWithoutDashes + 4U); | |
250 std::string::const_iterator client_id_it = client_info->client_id.begin(); | |
251 for (size_t i = 0; i < kGUIDLengthWithoutDashes + 4U; ++i) { | |
252 if (i == 8U || i == 13U || i == 18U || i == 23U) { | |
253 client_id_with_dashes.push_back('-'); | |
254 } else { | |
255 client_id_with_dashes.push_back(*client_id_it); | |
256 ++client_id_it; | |
257 } | |
258 } | |
259 DCHECK(client_id_it == client_info->client_id.end()); | |
260 client_info->client_id.assign(client_id_with_dashes); | |
261 } | |
262 | |
263 // The GUID retrieved (and possibly fixed above) should be valid unless | |
264 // retrieval failed. If not, return nullptr. This will result in a new GUID | |
265 // being generated by the calling function ForceClientIdCreation(). | |
266 if (client_info && !base::IsValidGUID(client_info->client_id)) | 236 if (client_info && !base::IsValidGUID(client_info->client_id)) |
267 return nullptr; | 237 return nullptr; |
268 | 238 |
269 return client_info; | 239 return client_info; |
270 } | 240 } |
271 | 241 |
272 int MetricsStateManager::GetLowEntropySource() { | 242 int MetricsStateManager::GetLowEntropySource() { |
273 UpdateLowEntropySource(); | 243 UpdateLowEntropySource(); |
274 return low_entropy_source_; | 244 return low_entropy_source_; |
275 } | 245 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 | 294 |
325 local_state_->ClearPref(prefs::kMetricsClientID); | 295 local_state_->ClearPref(prefs::kMetricsClientID); |
326 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 296 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
327 local_state_->ClearPref(prefs::kMetricsResetIds); | 297 local_state_->ClearPref(prefs::kMetricsResetIds); |
328 | 298 |
329 // Also clear the backed up client info. | 299 // Also clear the backed up client info. |
330 store_client_info_.Run(ClientInfo()); | 300 store_client_info_.Run(ClientInfo()); |
331 } | 301 } |
332 | 302 |
333 } // namespace metrics | 303 } // namespace metrics |
OLD | NEW |