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