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 "chrome/browser/metrics/cloned_install_detector.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/metrics/cloned_install_detector.h"
12 #include "chrome/browser/metrics/machine_id_provider.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/metrics/metrics_hashes.h"
15 #include "content/public/browser/browser_thread.h"
21 uint32
HashRawId(const std::string
& value
) {
22 uint64 hash
= metrics::HashMetricName(value
);
24 // Only use 24 bits from the 64-bit hash.
25 return hash
& ((1 << 24) - 1);
28 // State of the generated machine id in relation to the previously stored value.
29 // Note: UMA histogram enum - don't re-order or remove entries
38 // Logs the state of generating a machine id and comparing it to a stored value.
39 void LogMachineIdState(MachineIdState state
) {
40 UMA_HISTOGRAM_ENUMERATION("UMA.MachineIdState", state
, ID_ENUM_SIZE
);
45 ClonedInstallDetector::ClonedInstallDetector(MachineIdProvider
* raw_id_provider
)
46 : raw_id_provider_(raw_id_provider
),
47 weak_ptr_factory_(this) {}
49 ClonedInstallDetector::~ClonedInstallDetector() {}
51 void ClonedInstallDetector::CheckForClonedInstall(PrefService
* local_state
) {
52 content::BrowserThread::PostTaskAndReplyWithResult(
53 content::BrowserThread::FILE,
55 base::Bind(&metrics::MachineIdProvider::GetMachineId
,
57 base::Bind(&metrics::ClonedInstallDetector::SaveMachineId
,
58 weak_ptr_factory_
.GetWeakPtr(),
62 void ClonedInstallDetector::SaveMachineId(
63 PrefService
* local_state
,
66 LogMachineIdState(ID_GENERATION_FAILED
);
67 local_state
->ClearPref(prefs::kMetricsMachineId
);
71 int hashed_id
= HashRawId(raw_id
);
73 MachineIdState id_state
= ID_NO_STORED_VALUE
;
74 if (local_state
->HasPrefPath(prefs::kMetricsMachineId
)) {
75 if (local_state
->GetInteger(prefs::kMetricsMachineId
) != hashed_id
) {
76 id_state
= ID_CHANGED
;
77 // TODO(jwd): Use a callback to set the reset pref. That way
78 // ClonedInstallDetector doesn't need to know about this pref.
79 local_state
->SetBoolean(prefs::kMetricsResetIds
, true);
81 id_state
= ID_UNCHANGED
;
85 LogMachineIdState(id_state
);
87 local_state
->SetInteger(prefs::kMetricsMachineId
, hashed_id
);
91 void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple
* registry
) {
92 registry
->RegisterIntegerPref(prefs::kMetricsMachineId
, 0);
95 } // namespace metrics