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