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.
5 #include "components/metrics/cloned_install_detector.h"
8 #include "base/location.h"
9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/task_runner_util.h"
14 #include "components/metrics/cloned_install_detector.h"
15 #include "components/metrics/machine_id_provider.h"
16 #include "components/metrics/metrics_hashes.h"
17 #include "components/metrics/metrics_pref_names.h"
23 uint32
HashRawId(const std::string
& value
) {
24 uint64 hash
= metrics::HashMetricName(value
);
26 // Only use 24 bits from the 64-bit hash.
27 return hash
& ((1 << 24) - 1);
30 // State of the generated machine id in relation to the previously stored value.
31 // Note: UMA histogram enum - don't re-order or remove entries
40 // Logs the state of generating a machine id and comparing it to a stored value.
41 void LogMachineIdState(MachineIdState state
) {
42 UMA_HISTOGRAM_ENUMERATION("UMA.MachineIdState", state
, ID_ENUM_SIZE
);
47 ClonedInstallDetector::ClonedInstallDetector(MachineIdProvider
* raw_id_provider
)
48 : raw_id_provider_(raw_id_provider
), weak_ptr_factory_(this) {
51 ClonedInstallDetector::~ClonedInstallDetector() {
54 void ClonedInstallDetector::CheckForClonedInstall(
55 PrefService
* local_state
,
56 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
) {
57 base::PostTaskAndReplyWithResult(
60 base::Bind(&metrics::MachineIdProvider::GetMachineId
, raw_id_provider_
),
61 base::Bind(&metrics::ClonedInstallDetector::SaveMachineId
,
62 weak_ptr_factory_
.GetWeakPtr(),
66 void ClonedInstallDetector::SaveMachineId(PrefService
* local_state
,
69 LogMachineIdState(ID_GENERATION_FAILED
);
70 local_state
->ClearPref(prefs::kMetricsMachineId
);
74 int hashed_id
= HashRawId(raw_id
);
76 MachineIdState id_state
= ID_NO_STORED_VALUE
;
77 if (local_state
->HasPrefPath(prefs::kMetricsMachineId
)) {
78 if (local_state
->GetInteger(prefs::kMetricsMachineId
) != hashed_id
) {
79 id_state
= ID_CHANGED
;
80 // TODO(jwd): Use a callback to set the reset pref. That way
81 // ClonedInstallDetector doesn't need to know about this pref.
82 local_state
->SetBoolean(prefs::kMetricsResetIds
, true);
84 id_state
= ID_UNCHANGED
;
88 LogMachineIdState(id_state
);
90 local_state
->SetInteger(prefs::kMetricsMachineId
, hashed_id
);
94 void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple
* registry
) {
95 registry
->RegisterIntegerPref(prefs::kMetricsMachineId
, 0);
98 } // namespace metrics