xref: /aosp_15_r20/external/cronet/base/task/sequence_manager/thread_controller_power_monitor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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   // Initializes features for this class. See `base::features::Init()`.
35   static void InitializeFeatures();
36 
37   static void OverrideUsePowerMonitorForTesting(bool use_power_monitor);
38   static void ResetForTesting();
39 
40   // base::PowerSuspendObserver:
41   void OnSuspend() override;
42   void OnResume() override;
43 
44  private:
45   // Power state based on notifications delivered to this observer.
46   bool is_power_suspended_ = false;
47 
48   // Whether PowerMonitor observer is registered.
49   bool is_observer_registered_ = false;
50 };
51 
52 }  // namespace internal
53 }  // namespace sequence_manager
54 }  // namespace base
55 
56 #endif  // BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_POWER_MONITOR_H_
57