1 // Copyright 2013 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_POWER_MONITOR_POWER_MONITOR_H_ 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ 7 8 #include <memory> 9 10 #include "base/base_export.h" 11 #include "base/no_destructor.h" 12 #include "base/observer_list_threadsafe.h" 13 #include "base/power_monitor/power_observer.h" 14 #include "base/time/time.h" 15 #include "build/build_config.h" 16 17 namespace base { 18 19 class PowerMonitorSource; 20 21 // A class used to monitor the power state change and notify the observers about 22 // the change event. The threading model of this class is as follows: 23 // Once initialized, it is threadsafe. However, the client must ensure that 24 // initialization happens before any other methods are invoked, including 25 // IsInitialized(). IsInitialized() exists only as a convenience for detection 26 // of test contexts where the PowerMonitor global is never created. 27 class BASE_EXPORT PowerMonitor { 28 public: 29 // Initializes global PowerMonitor state. Takes ownership of |source|, which 30 // will be leaked on process teardown. May only be called once. Not threadsafe 31 // - no other PowerMonitor methods may be called on any thread while calling 32 // Initialize(). |source| must not be nullptr. 33 static void Initialize(std::unique_ptr<PowerMonitorSource> source); 34 35 PowerMonitor(const PowerMonitor&) = delete; 36 PowerMonitor& operator=(const PowerMonitor&) = delete; 37 38 // Returns true if Initialize() has been called. Safe to call on any thread, 39 // but must not be called while Initialize() or ShutdownForTesting() is being 40 // invoked. 41 static bool IsInitialized(); 42 43 // Add and remove an observer. 44 // Can be called from any thread. |observer| is notified on the sequence 45 // from which it was registered. 46 // Must not be called from within a notification callback. 47 // 48 // It is safe to add observers before the PowerMonitor is initialized. It is 49 // safe to remove an observer even if it was not added as an observer. 50 static void AddPowerSuspendObserver(PowerSuspendObserver* observer); 51 static void RemovePowerSuspendObserver(PowerSuspendObserver* observer); 52 static void AddPowerStateObserver(PowerStateObserver* observer); 53 static void RemovePowerStateObserver(PowerStateObserver* observer); 54 static void AddPowerThermalObserver(PowerThermalObserver* observer); 55 static void RemovePowerThermalObserver(PowerThermalObserver* observer); 56 57 // Atomically add a PowerSuspendObserver and read the current power suspended 58 // state. This variant must be used to avoid race between adding an observer 59 // and reading the power state. The following code would be racy: 60 // AddOPowerSuspendbserver(...); 61 // if (PowerMonitor::IsSystemSuspended()) { ... } 62 // 63 // Returns true if the system is currently suspended. 64 static bool AddPowerSuspendObserverAndReturnSuspendedState( 65 PowerSuspendObserver* observer); 66 // Returns true if the system is on-battery. 67 static bool AddPowerStateObserverAndReturnOnBatteryState( 68 PowerStateObserver* observer); 69 // Returns the power thermal state. 70 static PowerThermalObserver::DeviceThermalState 71 AddPowerStateObserverAndReturnPowerThermalState( 72 PowerThermalObserver* observer); 73 74 // Is the computer currently on battery power. May only be called if the 75 // PowerMonitor has been initialized. 76 static bool IsOnBatteryPower(); 77 78 // Returns the time of the last system resume. If no system suspend/resume was 79 // observed, returns an empty time. If the system is currently suspended, 80 // returns TimeTicks::Max(). 81 static TimeTicks GetLastSystemResumeTime(); 82 83 // Read the current DeviceThermalState if known. Can be called on any thread. 84 // May only be called if the PowerMonitor has been initialized. 85 static PowerThermalObserver::DeviceThermalState GetCurrentThermalState(); 86 87 // Update the result of thermal state. 88 static void SetCurrentThermalState( 89 PowerThermalObserver::DeviceThermalState state); 90 91 #if BUILDFLAG(IS_ANDROID) 92 // Read and return the current remaining battery capacity (microampere-hours). 93 // Only supported with a device power source (i.e. not in child processes in 94 // Chrome) and on devices with Android >= Lollipop as well as a power supply 95 // that supports this counter. Returns 0 if unsupported. 96 static int GetRemainingBatteryCapacity(); 97 #endif // BUILDFLAG(IS_ANDROID) 98 99 // Uninitializes the PowerMonitor. Should be called at the end of any unit 100 // test that mocks out the PowerMonitor, to avoid affecting subsequent tests. 101 // There must be no live observers when invoked. Safe to call even if the 102 // PowerMonitor hasn't been initialized. 103 static void ShutdownForTesting(); 104 105 private: 106 friend class PowerMonitorSource; 107 friend class base::NoDestructor<PowerMonitor>; 108 109 PowerMonitor(); 110 ~PowerMonitor(); 111 112 static PowerMonitorSource* Source(); 113 114 static void NotifyPowerStateChange(bool on_battery_power); 115 static void NotifySuspend(); 116 static void NotifyResume(); 117 static void NotifyThermalStateChange( 118 PowerThermalObserver::DeviceThermalState new_state); 119 static void NotifySpeedLimitChange(int speed_limit); 120 121 static PowerMonitor* GetInstance(); 122 123 bool is_system_suspended_ GUARDED_BY(is_system_suspended_lock_) = false; 124 Lock is_system_suspended_lock_; 125 TimeTicks last_system_resume_time_ GUARDED_BY(is_system_suspended_lock_); 126 127 bool on_battery_power_ GUARDED_BY(on_battery_power_lock_) = false; 128 Lock on_battery_power_lock_; 129 130 PowerThermalObserver::DeviceThermalState power_thermal_state_ 131 GUARDED_BY(power_thermal_state_lock_) = 132 PowerThermalObserver::DeviceThermalState::kUnknown; 133 int speed_limit_ GUARDED_BY(power_thermal_state_lock_) = 134 PowerThermalObserver::kSpeedLimitMax; 135 Lock power_thermal_state_lock_; 136 137 scoped_refptr<ObserverListThreadSafe<PowerStateObserver>> 138 power_state_observers_; 139 scoped_refptr<ObserverListThreadSafe<PowerSuspendObserver>> 140 power_suspend_observers_; 141 scoped_refptr<ObserverListThreadSafe<PowerThermalObserver>> 142 thermal_state_observers_; 143 std::unique_ptr<PowerMonitorSource> source_; 144 }; 145 146 } // namespace base 147 148 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ 149