• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_POWER_MONITOR_H_
6 #define BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_POWER_MONITOR_H_
7 
8 #include "base/base_export.h"
9 #include "base/power_monitor/power_observer.h"
10 
11 namespace base {
12 namespace sequence_manager {
13 namespace internal {
14 
15 // A helper class that keeps track of the power state and handles power
16 // notifications. The class register itself to the PowerMonitor and receives
17 // notifications on the bound thread (see BindToCurrentThread(...)).
18 class BASE_EXPORT ThreadControllerPowerMonitor : public PowerSuspendObserver {
19  public:
20   ThreadControllerPowerMonitor();
21   ~ThreadControllerPowerMonitor() override;
22   ThreadControllerPowerMonitor(const ThreadControllerPowerMonitor&) = delete;
23   ThreadControllerPowerMonitor& operator=(const ThreadControllerPowerMonitor&) =
24       delete;
25 
26   // Register this class to the power monitor to receive notifications on this
27   // thread. It is safe to call this before PowerMonitor is initialized.
28   void BindToCurrentThread();
29 
30   // Returns whether the process is between power suspend and resume
31   // notifications.
32   bool IsProcessInPowerSuspendState();
33 
34   // Initialize the ThreadControllerPowerMonitor. Must be called once on the
35   // main thread during startup while single-threaded.
36   static void InitializeOnMainThread();
37 
38   static void OverrideUsePowerMonitorForTesting(bool use_power_monitor);
39   static void ResetForTesting();
40 
41   // base::PowerSuspendObserver:
42   void OnSuspend() override;
43   void OnResume() override;
44 
45  private:
46   // Power state based on notifications delivered to this observer.
47   bool is_power_suspended_ = false;
48 
49   // Whether PowerMonitor observer is registered.
50   bool is_observer_registered_ = false;
51 };
52 
53 }  // namespace internal
54 }  // namespace sequence_manager
55 }  // namespace base
56 
57 #endif  // BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_POWER_MONITOR_H_
58