summaryrefslogtreecommitdiff
path: root/chromium/components/metrics/clean_exit_beacon.cc
blob: cbf41b2d02ee4e1a0b100ffc8a53309b92a6f8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/clean_exit_beacon.h"

#include "base/check_op.h"
#include "build/build_config.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

#if defined(OS_WIN)
#include <windows.h>
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#endif

namespace metrics {

CleanExitBeacon::CleanExitBeacon(const base::string16& backup_registry_key,
                                 PrefService* local_state)
    : local_state_(local_state),
      initial_value_(local_state->GetBoolean(prefs::kStabilityExitedCleanly)),
      initial_browser_last_live_timestamp_(
          local_state->GetTime(prefs::kStabilityBrowserLastLiveTimeStamp)),
      backup_registry_key_(backup_registry_key) {
  DCHECK_NE(PrefService::INITIALIZATION_STATUS_WAITING,
            local_state_->GetInitializationStatus());

#if defined(OS_WIN)
  // An enumeration of all possible permutations of the the beacon state in the
  // registry and in Local State.
  enum {
    DIRTY_DIRTY,
    DIRTY_CLEAN,
    CLEAN_DIRTY,
    CLEAN_CLEAN,
    MISSING_DIRTY,
    MISSING_CLEAN,
    NUM_CONSISTENCY_ENUMS
  } consistency = DIRTY_DIRTY;

  base::win::RegKey regkey;
  DWORD value = 0u;
  if (regkey.Open(HKEY_CURRENT_USER,
                  backup_registry_key_.c_str(),
                  KEY_ALL_ACCESS) == ERROR_SUCCESS &&
      regkey.ReadValueDW(
          base::ASCIIToUTF16(prefs::kStabilityExitedCleanly).c_str(), &value) ==
          ERROR_SUCCESS) {
    if (value)
      consistency = initial_value_ ? CLEAN_CLEAN : CLEAN_DIRTY;
    else
      consistency = initial_value_ ? DIRTY_CLEAN : DIRTY_DIRTY;
  } else {
    consistency = initial_value_ ? MISSING_CLEAN : MISSING_DIRTY;
  }

  UMA_HISTOGRAM_ENUMERATION(
      "UMA.CleanExitBeaconConsistency", consistency, NUM_CONSISTENCY_ENUMS);
#endif
}

CleanExitBeacon::~CleanExitBeacon() {
}

// static
void CleanExitBeacon::RegisterPrefs(PrefRegistrySimple* registry) {
  registry->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);

  registry->RegisterTimePref(prefs::kStabilityBrowserLastLiveTimeStamp,
                             base::Time(), PrefRegistry::LOSSY_PREF);
}

void CleanExitBeacon::WriteBeaconValue(bool value) {
  UpdateLastLiveTimestamp();
  local_state_->SetBoolean(prefs::kStabilityExitedCleanly, value);

#if defined(OS_WIN)
  base::win::RegKey regkey;
  if (regkey.Create(HKEY_CURRENT_USER,
                    backup_registry_key_.c_str(),
                    KEY_ALL_ACCESS) == ERROR_SUCCESS) {
    regkey.WriteValue(
        base::ASCIIToUTF16(prefs::kStabilityExitedCleanly).c_str(),
        value ? 1u : 0u);
  }
#endif
}

void CleanExitBeacon::UpdateLastLiveTimestamp() {
  local_state_->SetTime(prefs::kStabilityBrowserLastLiveTimeStamp,
                        base::Time::Now());
}

}  // namespace metrics