summaryrefslogtreecommitdiff
path: root/chromium/components/metrics/execution_phase.cc
blob: 992ef820e77c945b17665f4390f49a1a6a924bfc (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
// Copyright 2017 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/execution_phase.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 "components/browser_watcher/stability_data_names.h"
#include "components/browser_watcher/stability_debugging.h"
#endif  // defined(OS_WIN)

namespace metrics {

ExecutionPhaseManager::ExecutionPhaseManager(PrefService* local_state)
    : local_state_(local_state) {}

ExecutionPhaseManager::~ExecutionPhaseManager() {}

// static
void ExecutionPhaseManager::RegisterPrefs(PrefRegistrySimple* registry) {
  registry->RegisterIntegerPref(
      prefs::kStabilityExecutionPhase,
      static_cast<int>(ExecutionPhase::UNINITIALIZED_PHASE));
}

// static
ExecutionPhase ExecutionPhaseManager::execution_phase_ =
    ExecutionPhase::UNINITIALIZED_PHASE;

void ExecutionPhaseManager::SetExecutionPhase(ExecutionPhase execution_phase) {
  DCHECK(execution_phase != ExecutionPhase::START_METRICS_RECORDING ||
         execution_phase_ == ExecutionPhase::UNINITIALIZED_PHASE);
  execution_phase_ = execution_phase;
  local_state_->SetInteger(prefs::kStabilityExecutionPhase,
                           static_cast<int>(execution_phase_));
#if defined(OS_WIN)
  browser_watcher::SetStabilityDataInt(
      browser_watcher::kStabilityExecutionPhase,
      static_cast<int>(execution_phase_));
#endif  // defined(OS_WIN)
}

ExecutionPhase ExecutionPhaseManager::GetExecutionPhase() {
  // TODO(rtenneti): On windows, consider saving/getting execution_phase from
  // the registry.
  return static_cast<ExecutionPhase>(
      local_state_->GetInteger(prefs::kStabilityExecutionPhase));
}

#if defined(OS_ANDROID) || defined(OS_IOS)
void ExecutionPhaseManager::OnAppEnterBackground() {
  // Note: the in-memory ExecutionPhaseManager::execution_phase_ is not updated.
  local_state_->SetInteger(prefs::kStabilityExecutionPhase,
                           static_cast<int>(ExecutionPhase::SHUTDOWN_COMPLETE));
}

void ExecutionPhaseManager::OnAppEnterForeground() {
  // Restore prefs value altered by OnEnterBackground.
  local_state_->SetInteger(prefs::kStabilityExecutionPhase,
                           static_cast<int>(execution_phase_));
}
#endif  // defined(OS_ANDROID) || defined(OS_IOS)

}  // namespace metrics