cloned_install_detector.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "components/metrics/cloned_install_detector.h"
  5. #include <stdint.h>
  6. #include <string>
  7. #include "base/bind.h"
  8. #include "base/location.h"
  9. #include "base/metrics/histogram_macros.h"
  10. #include "base/metrics/metrics_hashes.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/task/task_runner_util.h"
  13. #include "base/task/thread_pool.h"
  14. #include "components/metrics/machine_id_provider.h"
  15. #include "components/metrics/metrics_pref_names.h"
  16. #include "components/prefs/pref_registry_simple.h"
  17. #include "components/prefs/pref_service.h"
  18. namespace metrics {
  19. namespace {
  20. uint32_t HashRawId(const std::string& value) {
  21. uint64_t hash = base::HashMetricName(value);
  22. // Only use 24 bits from the 64-bit hash.
  23. return hash & ((1 << 24) - 1);
  24. }
  25. // State of the generated machine id in relation to the previously stored value.
  26. // Note: UMA histogram enum - don't re-order or remove entries
  27. enum MachineIdState {
  28. ID_GENERATION_FAILED,
  29. ID_NO_STORED_VALUE,
  30. ID_CHANGED,
  31. ID_UNCHANGED,
  32. ID_ENUM_SIZE
  33. };
  34. // Logs the state of generating a machine id and comparing it to a stored value.
  35. void LogMachineIdState(MachineIdState state) {
  36. UMA_HISTOGRAM_ENUMERATION("UMA.MachineIdState", state, ID_ENUM_SIZE);
  37. }
  38. } // namespace
  39. ClonedInstallDetector::ClonedInstallDetector() {}
  40. ClonedInstallDetector::~ClonedInstallDetector() {
  41. }
  42. void ClonedInstallDetector::CheckForClonedInstall(PrefService* local_state) {
  43. if (!MachineIdProvider::HasId())
  44. return;
  45. base::ThreadPool::PostTaskAndReplyWithResult(
  46. FROM_HERE,
  47. {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
  48. base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  49. base::BindOnce(&MachineIdProvider::GetMachineId),
  50. base::BindOnce(&ClonedInstallDetector::SaveMachineId,
  51. weak_ptr_factory_.GetWeakPtr(), local_state));
  52. }
  53. void ClonedInstallDetector::SaveMachineId(PrefService* local_state,
  54. const std::string& raw_id) {
  55. if (raw_id.empty()) {
  56. LogMachineIdState(ID_GENERATION_FAILED);
  57. local_state->ClearPref(prefs::kMetricsMachineId);
  58. return;
  59. }
  60. int hashed_id = HashRawId(raw_id);
  61. MachineIdState id_state = ID_NO_STORED_VALUE;
  62. if (local_state->HasPrefPath(prefs::kMetricsMachineId)) {
  63. if (local_state->GetInteger(prefs::kMetricsMachineId) != hashed_id) {
  64. id_state = ID_CHANGED;
  65. detected_this_session_ = true;
  66. local_state->SetBoolean(prefs::kMetricsResetIds, true);
  67. } else {
  68. id_state = ID_UNCHANGED;
  69. }
  70. }
  71. LogMachineIdState(id_state);
  72. local_state->SetInteger(prefs::kMetricsMachineId, hashed_id);
  73. }
  74. bool ClonedInstallDetector::ShouldResetClientIds(PrefService* local_state) {
  75. // The existence of the pref indicates that it has been set when we saved the
  76. // MachineId and thus we need to update the member variable for this session
  77. // and clear the pref for future runs. We shouldn't clear the pref multiple
  78. // times because it may have been cloned again.
  79. if (!should_reset_client_ids_ &&
  80. local_state->HasPrefPath(prefs::kMetricsResetIds)) {
  81. should_reset_client_ids_ = local_state->GetBoolean(prefs::kMetricsResetIds);
  82. local_state->ClearPref(prefs::kMetricsResetIds);
  83. }
  84. return should_reset_client_ids_;
  85. }
  86. bool ClonedInstallDetector::ClonedInstallDetectedInCurrentSession() const {
  87. return detected_this_session_;
  88. }
  89. // static
  90. void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple* registry) {
  91. registry->RegisterBooleanPref(prefs::kMetricsResetIds, false);
  92. registry->RegisterIntegerPref(prefs::kMetricsMachineId, 0);
  93. registry->RegisterIntegerPref(prefs::kClonedResetCount, 0);
  94. registry->RegisterInt64Pref(prefs::kFirstClonedResetTimestamp, 0);
  95. registry->RegisterInt64Pref(prefs::kLastClonedResetTimestamp, 0);
  96. }
  97. ClonedInstallInfo ClonedInstallDetector::ReadClonedInstallInfo(
  98. PrefService* local_state) {
  99. return ClonedInstallInfo{
  100. .last_reset_timestamp =
  101. local_state->GetInt64(prefs::kLastClonedResetTimestamp),
  102. .first_reset_timestamp =
  103. local_state->GetInt64(prefs::kFirstClonedResetTimestamp),
  104. .reset_count = local_state->GetInteger(prefs::kClonedResetCount)};
  105. }
  106. void ClonedInstallDetector::ClearClonedInstallInfo(PrefService* local_state) {
  107. local_state->ClearPref(prefs::kClonedResetCount);
  108. local_state->ClearPref(prefs::kFirstClonedResetTimestamp);
  109. local_state->ClearPref(prefs::kLastClonedResetTimestamp);
  110. }
  111. void ClonedInstallDetector::RecordClonedInstallInfo(PrefService* local_state) {
  112. ClonedInstallInfo cloned = ReadClonedInstallInfo(local_state);
  113. // Make sure that at the first time of reset, the first_timestamp matches with
  114. // the last_timestamp.
  115. int64_t time = base::Time::Now().ToTimeT();
  116. // Only set |prefs::kFirstClonedResetTimestamp| when the client needs to be
  117. // reset due to cloned install for the first time.
  118. if (cloned.reset_count == 0) {
  119. local_state->SetInt64(prefs::kFirstClonedResetTimestamp, time);
  120. }
  121. local_state->SetInt64(prefs::kLastClonedResetTimestamp, time);
  122. local_state->SetInteger(prefs::kClonedResetCount, cloned.reset_count + 1);
  123. }
  124. } // namespace metrics