Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / metrics / cloned_install_detector.cc
blobdb52b842d1d84f256a6a5fcdd9293c0849c3c78b
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"
7 #include "base/bind.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"
17 namespace metrics {
19 namespace {
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
30 enum MachineIdState {
31 ID_GENERATION_FAILED,
32 ID_NO_STORED_VALUE,
33 ID_CHANGED,
34 ID_UNCHANGED,
35 ID_ENUM_SIZE
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);
43 } // namespace
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,
54 FROM_HERE,
55 base::Bind(&metrics::MachineIdProvider::GetMachineId,
56 raw_id_provider_),
57 base::Bind(&metrics::ClonedInstallDetector::SaveMachineId,
58 weak_ptr_factory_.GetWeakPtr(),
59 local_state));
62 void ClonedInstallDetector::SaveMachineId(
63 PrefService* local_state,
64 std::string raw_id) {
65 if (raw_id.empty()) {
66 LogMachineIdState(ID_GENERATION_FAILED);
67 local_state->ClearPref(prefs::kMetricsMachineId);
68 return;
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);
80 } else {
81 id_state = ID_UNCHANGED;
85 LogMachineIdState(id_state);
87 local_state->SetInteger(prefs::kMetricsMachineId, hashed_id);
90 // static
91 void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple* registry) {
92 registry->RegisterIntegerPref(prefs::kMetricsMachineId, 0);
95 } // namespace metrics