422a1100db4f90245a9c188065a9bd768bfad76a
[platform/framework/web/crosswalk.git] / src / components / metrics / clean_exit_beacon.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/clean_exit_beacon.h"
6
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "components/metrics/metrics_pref_names.h"
10
11 #if defined(OS_WIN)
12 #include "base/metrics/histogram.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/registry.h"
15 #endif
16
17 namespace metrics {
18
19 CleanExitBeacon::CleanExitBeacon(const base::string16& backup_registry_key,
20                                  PrefService* local_state)
21     : local_state_(local_state),
22       initial_value_(local_state->GetBoolean(prefs::kStabilityExitedCleanly)),
23       backup_registry_key_(backup_registry_key) {
24   DCHECK_NE(PrefService::INITIALIZATION_STATUS_WAITING,
25             local_state_->GetInitializationStatus());
26
27 #if defined(OS_WIN)
28   // An enumeration of all possible permutations of the the beacon state in the
29   // registry and in Local State.
30   enum {
31     DIRTY_DIRTY,
32     DIRTY_CLEAN,
33     CLEAN_DIRTY,
34     CLEAN_CLEAN,
35     MISSING_DIRTY,
36     MISSING_CLEAN,
37     NUM_CONSISTENCY_ENUMS
38   } consistency = DIRTY_DIRTY;
39
40   base::win::RegKey regkey;
41   DWORD value = 0u;
42   if (regkey.Open(HKEY_CURRENT_USER,
43                   backup_registry_key_.c_str(),
44                   KEY_ALL_ACCESS) == ERROR_SUCCESS &&
45       regkey.ReadValueDW(
46           base::ASCIIToUTF16(prefs::kStabilityExitedCleanly).c_str(), &value) ==
47           ERROR_SUCCESS) {
48     if (value)
49       consistency = initial_value_ ? CLEAN_CLEAN : CLEAN_DIRTY;
50     else
51       consistency = initial_value_ ? DIRTY_CLEAN : DIRTY_DIRTY;
52   } else {
53     consistency = initial_value_ ? MISSING_CLEAN : MISSING_DIRTY;
54   }
55
56   UMA_HISTOGRAM_ENUMERATION(
57       "UMA.CleanExitBeaconConsistency", consistency, NUM_CONSISTENCY_ENUMS);
58 #endif
59 }
60
61 CleanExitBeacon::~CleanExitBeacon() {
62 }
63
64 void CleanExitBeacon::WriteBeaconValue(bool value) {
65   local_state_->SetBoolean(prefs::kStabilityExitedCleanly, value);
66
67 #if defined(OS_WIN)
68   base::win::RegKey regkey;
69   if (regkey.Create(HKEY_CURRENT_USER,
70                     backup_registry_key_.c_str(),
71                     KEY_ALL_ACCESS) == ERROR_SUCCESS) {
72     regkey.WriteValue(
73         base::ASCIIToUTF16(prefs::kStabilityExitedCleanly).c_str(),
74         value ? 1u : 0u);
75   }
76 #endif
77 }
78
79 }  // namespace metrics