Change histogram.h includes to histogram_macros.h in metrics code.
[chromium-blink-merge.git] / components / metrics / cloned_install_detector.cc
blob16598b4c2995a2d86f31efc6bf47217a3754452a
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/metrics/histogram_macros.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"
20 namespace metrics {
22 namespace {
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
33 enum MachineIdState {
34 ID_GENERATION_FAILED,
35 ID_NO_STORED_VALUE,
36 ID_CHANGED,
37 ID_UNCHANGED,
38 ID_ENUM_SIZE
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);
46 } // namespace
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(
59 task_runner.get(),
60 FROM_HERE,
61 base::Bind(&MachineIdProvider::GetMachineId, raw_id_provider_),
62 base::Bind(&ClonedInstallDetector::SaveMachineId,
63 weak_ptr_factory_.GetWeakPtr(),
64 local_state));
67 void ClonedInstallDetector::SaveMachineId(PrefService* local_state,
68 std::string raw_id) {
69 if (raw_id.empty()) {
70 LogMachineIdState(ID_GENERATION_FAILED);
71 local_state->ClearPref(prefs::kMetricsMachineId);
72 return;
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);
84 } else {
85 id_state = ID_UNCHANGED;
89 LogMachineIdState(id_state);
91 local_state->SetInteger(prefs::kMetricsMachineId, hashed_id);
94 // static
95 void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple* registry) {
96 registry->RegisterIntegerPref(prefs::kMetricsMachineId, 0);
99 } // namespace metrics