1 // Copyright 2013 The Chromium Authors. All rights reserved. 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_SOURCE_H_ 6 #define BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_ 7 8 #include "base/base_export.h" 9 #include "base/macros.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/observer_list_threadsafe.h" 12 #include "base/synchronization/lock.h" 13 14 namespace base { 15 16 class PowerMonitor; 17 18 // Communicates power state changes to the power monitor. 19 class BASE_EXPORT PowerMonitorSource { 20 public: 21 PowerMonitorSource(); 22 virtual ~PowerMonitorSource(); 23 24 // Normalized list of power events. 25 enum PowerEvent { 26 POWER_STATE_EVENT, // The Power status of the system has changed. 27 SUSPEND_EVENT, // The system is being suspended. 28 RESUME_EVENT // The system is being resumed. 29 }; 30 31 // Is the computer currently on battery power. Can be called on any thread. 32 bool IsOnBatteryPower(); 33 34 // Called by PowerMonitor just before PowerMonitor destroys both itself and 35 // this instance). After return from this call it is no longer safe for 36 // subclasses to call into PowerMonitor (e.g., via PowerMonitor::Get(). Hence, 37 // subclasses should take any necessary actions here to ensure that after 38 // return from this invocation they will no longer make any calls on 39 // PowerMonitor. 40 virtual void Shutdown() = 0; 41 42 protected: 43 friend class PowerMonitorTest; 44 45 // Friend function that is allowed to access the protected ProcessPowerEvent. 46 friend void ProcessPowerEventHelper(PowerEvent); 47 48 // ProcessPowerEvent should only be called from a single thread, most likely 49 // the UI thread or, in child processes, the IO thread. 50 static void ProcessPowerEvent(PowerEvent event_id); 51 52 // Platform-specific method to check whether the system is currently 53 // running on battery power. Returns true if running on batteries, 54 // false otherwise. 55 virtual bool IsOnBatteryPowerImpl() = 0; 56 57 // Sets the initial state for |on_battery_power_|, which defaults to false 58 // since not all implementations can provide the value at construction. May 59 // only be called before a base::PowerMonitor has been created. 60 void SetInitialOnBatteryPowerState(bool on_battery_power); 61 62 private: 63 bool on_battery_power_ = false; 64 bool suspended_ = false; 65 66 // This lock guards access to on_battery_power_, to ensure that 67 // IsOnBatteryPower can be called from any thread. 68 Lock battery_lock_; 69 70 DISALLOW_COPY_AND_ASSIGN(PowerMonitorSource); 71 }; 72 73 } // namespace base 74 75 #endif // BASE_POWER_MONITOR_POWER_MONITOR_SOURCE_H_ 76