1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// found in the LICENSE file.
4010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/metrics/metrics_state_manager.h"
6010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/command_line.h"
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/guid.h"
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/metrics/histogram.h"
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/metrics/sparse_histogram.h"
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/prefs/pref_service.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/rand_util.h"
14010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/threading/thread_restrictions.h"
16010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/time/time.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/cloned_install_detector.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/machine_id_provider.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/metrics_pref_names.h"
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/metrics/metrics_switches.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/variations/caching_permuted_entropy_provider.h"
22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
23010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace metrics {
24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace {
26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// The argument used to generate a non-identifying entropy source. We want no
28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// more than 13 bits of entropy, so use this max to return a number in the range
29010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// [0, 7999] as the entropy source (12.97 bits of entropy).
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)const int kMaxLowEntropySize = 8000;
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Default prefs value for prefs::kMetricsLowEntropySource to indicate that
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// the value has not yet been set.
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)const int kLowEntropySourceNotSet = -1;
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Generates a new non-identifying entropy source used to seed persistent
37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// activities.
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)int GenerateLowEntropySource() {
39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return base::RandInt(0, kMaxLowEntropySize - 1);
40010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
41010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
42010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}  // namespace
43010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
44010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static
45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool MetricsStateManager::instance_exists_ = false;
46010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MetricsStateManager::MetricsStateManager(
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefService* local_state,
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const base::Callback<bool(void)>& is_reporting_enabled_callback,
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const StoreClientInfoCallback& store_client_info,
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const LoadClientInfoCallback& retrieve_client_info)
52010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    : local_state_(local_state),
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      is_reporting_enabled_callback_(is_reporting_enabled_callback),
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      store_client_info_(store_client_info),
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      load_client_info_(retrieve_client_info),
56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      low_entropy_source_(kLowEntropySourceNotSet),
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      entropy_source_returned_(ENTROPY_SOURCE_NONE) {
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ResetMetricsIDsIfNecessary();
59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (IsMetricsReportingEnabled())
60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ForceClientIdCreation();
61010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
62010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(!instance_exists_);
63010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  instance_exists_ = true;
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
65010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)MetricsStateManager::~MetricsStateManager() {
67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(instance_exists_);
68010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  instance_exists_ = false;
69010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
70010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool MetricsStateManager::IsMetricsReportingEnabled() {
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return is_reporting_enabled_callback_.Run();
73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void MetricsStateManager::ForceClientIdCreation() {
76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!client_id_.empty())
77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
7946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  client_id_ = local_state_->GetString(prefs::kMetricsClientID);
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!client_id_.empty()) {
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // It is technically sufficient to only save a backup of the client id when
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // it is initially generated below, but since the backup was only introduced
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // in M38, seed it explicitly from here for some time.
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    BackUpCurrentClientInfo();
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const scoped_ptr<ClientInfo> client_info_backup =
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      LoadClientInfoAndMaybeMigrate();
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (client_info_backup) {
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    client_id_ = client_info_backup->client_id;
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const base::Time now = base::Time::Now();
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Save the recovered client id and also try to reinstantiate the backup
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // values for the dates corresponding with that client id in order to avoid
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // weird scenarios where we could report an old client id with a recent
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // install date.
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    local_state_->SetString(prefs::kMetricsClientID, client_id_);
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    local_state_->SetInt64(prefs::kInstallDate,
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                           client_info_backup->installation_date != 0
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               ? client_info_backup->installation_date
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               : now.ToTimeT());
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp,
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                           client_info_backup->reporting_enabled_date != 0
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               ? client_info_backup->reporting_enabled_date
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               : now.ToTimeT());
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    base::TimeDelta recovered_installation_age;
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (client_info_backup->installation_date != 0) {
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      recovered_installation_age =
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          now - base::Time::FromTimeT(client_info_backup->installation_date);
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    UMA_HISTOGRAM_COUNTS_10000("UMA.ClientIdBackupRecoveredWithAge",
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                               recovered_installation_age.InHours());
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Flush the backup back to persistent storage in case we re-generated
1185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // missing data above.
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    BackUpCurrentClientInfo();
1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Failing attempts at getting an existing client ID, generate a new one.
124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  client_id_ = base::GenerateGUID();
12546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->SetString(prefs::kMetricsClientID, client_id_);
126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
12746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (local_state_->GetString(prefs::kMetricsOldClientID).empty()) {
128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // Record the timestamp of when the user opted in to UMA.
12946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    local_state_->SetInt64(prefs::kMetricsReportingEnabledTimestamp,
130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           base::Time::Now().ToTimeT());
131010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  } else {
132010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    UMA_HISTOGRAM_BOOLEAN("UMA.ClientIdMigrated", true);
133010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
13446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->ClearPref(prefs::kMetricsOldClientID);
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  BackUpCurrentClientInfo();
137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsStateManager::CheckForClonedInstall(
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(!cloned_install_detector_);
142010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
143010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  MachineIdProvider* provider = MachineIdProvider::CreateInstance();
144010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!provider)
145010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
146010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
147010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  cloned_install_detector_.reset(new ClonedInstallDetector(provider));
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  cloned_install_detector_->CheckForClonedInstall(local_state_, task_runner);
149010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
150010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
151010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)scoped_ptr<const base::FieldTrial::EntropyProvider>
152010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)MetricsStateManager::CreateEntropyProvider() {
153010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // For metrics reporting-enabled users, we combine the client ID and low
154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // entropy source to get the final entropy source. Otherwise, only use the low
155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // entropy source.
156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // This has two useful properties:
157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  //  1) It makes the entropy source less identifiable for parties that do not
158010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  //     know the low entropy source.
159010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  //  2) It makes the final entropy source resettable.
160010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const int low_entropy_source_value = GetLowEntropySource();
161010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue",
162010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                              low_entropy_source_value);
163010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (IsMetricsReportingEnabled()) {
164010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (entropy_source_returned_ == ENTROPY_SOURCE_NONE)
165010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      entropy_source_returned_ = ENTROPY_SOURCE_HIGH;
166010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    const std::string high_entropy_source =
167010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        client_id_ + base::IntToString(low_entropy_source_value);
168010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return scoped_ptr<const base::FieldTrial::EntropyProvider>(
169010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        new SHA1EntropyProvider(high_entropy_source));
170010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
171010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
172010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (entropy_source_returned_ == ENTROPY_SOURCE_NONE)
173010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    entropy_source_returned_ = ENTROPY_SOURCE_LOW;
174010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
175010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#if defined(OS_ANDROID) || defined(OS_IOS)
176010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return scoped_ptr<const base::FieldTrial::EntropyProvider>(
177010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      new CachingPermutedEntropyProvider(local_state_,
178010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                         low_entropy_source_value,
179010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                         kMaxLowEntropySize));
180010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#else
181010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return scoped_ptr<const base::FieldTrial::EntropyProvider>(
182010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      new PermutedEntropyProvider(low_entropy_source_value,
183010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                  kMaxLowEntropySize));
184010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#endif
185010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
186010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
187010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static
188010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)scoped_ptr<MetricsStateManager> MetricsStateManager::Create(
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefService* local_state,
1905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const base::Callback<bool(void)>& is_reporting_enabled_callback,
1915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const StoreClientInfoCallback& store_client_info,
1925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const LoadClientInfoCallback& retrieve_client_info) {
193010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  scoped_ptr<MetricsStateManager> result;
194010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Note: |instance_exists_| is updated in the constructor and destructor.
195cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!instance_exists_) {
1965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    result.reset(new MetricsStateManager(local_state,
1975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                         is_reporting_enabled_callback,
1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                         store_client_info,
1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                         retrieve_client_info));
200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
201010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return result.Pass();
202010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
203010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
204010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static
205010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) {
206010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  registry->RegisterBooleanPref(prefs::kMetricsResetIds, false);
20746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterStringPref(prefs::kMetricsClientID, std::string());
20846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0);
20946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource,
210010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                kLowEntropySourceNotSet);
211010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
212010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ClonedInstallDetector::RegisterPrefs(registry);
213010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CachingPermutedEntropyProvider::RegisterPrefs(registry);
214010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
215010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(asvitkine): Remove these once a couple of releases have passed.
216010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // http://crbug.com/357704
21746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterStringPref(prefs::kMetricsOldClientID, std::string());
21846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterIntegerPref(prefs::kMetricsOldLowEntropySource, 0);
219010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
220010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
2215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void MetricsStateManager::BackUpCurrentClientInfo() {
2221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(gayane): Eliminate use of ScopedAllowIO. crbug.com/413783
2231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::ThreadRestrictions::ScopedAllowIO allow_io;
2241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ClientInfo client_info;
2265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  client_info.client_id = client_id_;
2275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  client_info.installation_date = local_state_->GetInt64(prefs::kInstallDate);
2285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  client_info.reporting_enabled_date =
2295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp);
2305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  store_client_info_.Run(client_info);
2315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)scoped_ptr<ClientInfo> MetricsStateManager::LoadClientInfoAndMaybeMigrate() {
2345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<metrics::ClientInfo> client_info = load_client_info_.Run();
2355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Prior to 2014-07, the client ID was stripped of its dashes before being
2375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // saved. Migrate back to a proper GUID if this is the case. This migration
2385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // code can be removed in M41+.
2395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const size_t kGUIDLengthWithoutDashes = 32U;
2405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (client_info &&
2415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      client_info->client_id.length() == kGUIDLengthWithoutDashes) {
2425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DCHECK(client_info->client_id.find('-') == std::string::npos);
2435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    std::string client_id_with_dashes;
2455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    client_id_with_dashes.reserve(kGUIDLengthWithoutDashes + 4U);
2465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    std::string::const_iterator client_id_it = client_info->client_id.begin();
2475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    for (size_t i = 0; i < kGUIDLengthWithoutDashes + 4U; ++i) {
2485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      if (i == 8U || i == 13U || i == 18U || i == 23U) {
2495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        client_id_with_dashes.push_back('-');
2505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      } else {
2515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        client_id_with_dashes.push_back(*client_id_it);
2525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        ++client_id_it;
2535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      }
2545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
2555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DCHECK(client_id_it == client_info->client_id.end());
2565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    client_info->client_id.assign(client_id_with_dashes);
2575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // The GUID retrieved (and possibly fixed above) should be valid unless
2605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // retrieval failed.
2615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!client_info || base::IsValidGUID(client_info->client_id));
2625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return client_info.Pass();
2645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
266010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)int MetricsStateManager::GetLowEntropySource() {
267010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Note that the default value for the low entropy source and the default pref
268010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // value are both kLowEntropySourceNotSet, which is used to identify if the
269010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // value has been set or not.
270010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (low_entropy_source_ != kLowEntropySourceNotSet)
271010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return low_entropy_source_;
272010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
273010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const CommandLine* command_line(CommandLine::ForCurrentProcess());
27446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Only try to load the value from prefs if the user did not request a
275cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // reset.
276010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Otherwise, skip to generating a new value.
277010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!command_line->HasSwitch(switches::kResetVariationState)) {
27846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    int value = local_state_->GetInteger(prefs::kMetricsLowEntropySource);
279010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // If the value is outside the [0, kMaxLowEntropySize) range, re-generate
280010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // it below.
281010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (value >= 0 && value < kMaxLowEntropySize) {
282010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      low_entropy_source_ = value;
283010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", false);
284010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return low_entropy_source_;
285010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
286010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
287010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
288010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", true);
289010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  low_entropy_source_ = GenerateLowEntropySource();
29046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->SetInteger(prefs::kMetricsLowEntropySource,
291010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                           low_entropy_source_);
29246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->ClearPref(prefs::kMetricsOldLowEntropySource);
293cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CachingPermutedEntropyProvider::ClearCache(local_state_);
294010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
295010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return low_entropy_source_;
296010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
297010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
298010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void MetricsStateManager::ResetMetricsIDsIfNecessary() {
299010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!local_state_->GetBoolean(prefs::kMetricsResetIds))
300010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
301010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
302010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN("UMA.MetricsIDsReset", true);
303010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
304010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(client_id_.empty());
305010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_);
306010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
30746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->ClearPref(prefs::kMetricsClientID);
30846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->ClearPref(prefs::kMetricsLowEntropySource);
309010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  local_state_->ClearPref(prefs::kMetricsResetIds);
3105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Also clear the backed up client info.
3125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  store_client_info_.Run(ClientInfo());
313010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
314010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
315010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}  // namespace metrics
316