Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / metrics / cloned_install_detector.cc
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.
4
5 #include "components/metrics/cloned_install_detector.h"
6
7 #include <string>
8
9 #include "base/bind.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"
19
20 namespace metrics {
21
22 namespace {
23
24 uint32 HashRawId(const std::string& value) {
25   uint64 hash = HashMetricName(value);
26
27   // Only use 24 bits from the 64-bit hash.
28   return hash & ((1 << 24) - 1);
29 }
30
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
39 };
40
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);
44 }
45
46 }  // namespace
47
48 ClonedInstallDetector::ClonedInstallDetector(MachineIdProvider* raw_id_provider)
49     : raw_id_provider_(raw_id_provider), weak_ptr_factory_(this) {
50 }
51
52 ClonedInstallDetector::~ClonedInstallDetector() {
53 }
54
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));
65 }
66
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;
73   }
74
75   int hashed_id = HashRawId(raw_id);
76
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;
86     }
87   }
88
89   LogMachineIdState(id_state);
90
91   local_state->SetInteger(prefs::kMetricsMachineId, hashed_id);
92 }
93
94 // static
95 void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple* registry) {
96   registry->RegisterIntegerPref(prefs::kMetricsMachineId, 0);
97 }
98
99 }  // namespace metrics