OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_STATE_MANAGMER_H_ |
| 6 #define BASE_POWER_STATE_MANAGMER_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" |
| 10 |
| 11 // Windows HiRes timers drain the battery faster so we need to know the battery |
| 12 // status. This isn't true for other platforms. |
| 13 #if defined(OS_WIN) |
| 14 #define ENABLE_BATTERY_MONITORING 1 |
| 15 #else |
| 16 #undef ENABLE_BATTERY_MONITORING |
| 17 #endif // !OS_WIN |
| 18 |
| 19 #include "base/observer_list_threadsafe.h" |
| 20 #if defined(ENABLE_BATTERY_MONITORING) |
| 21 #include "base/timer.h" |
| 22 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 23 |
| 24 #if defined(OS_IOS) |
| 25 #include <objc/runtime.h> |
| 26 #endif // OS_IOS |
| 27 |
| 28 namespace base { |
| 29 |
| 30 class BASE_EXPORT PowerStateManager { |
| 31 public: |
| 32 // Normalized list of power events. |
| 33 enum PowerEvent { |
| 34 POWER_STATE_EVENT, // The Power status of the system has changed. |
| 35 SUSPEND_EVENT, // The system is being suspended. |
| 36 RESUME_EVENT // The system is being resumed. |
| 37 }; |
| 38 |
| 39 // Callbacks will be called on the thread which creates the SystemMonitor. |
| 40 // During the callback, Add/RemoveObserver will block until the callbacks |
| 41 // are finished. Observers should implement quick callback functions; if |
| 42 // lengthy operations are needed, the observer should take care to invoke |
| 43 // the operation on an appropriate thread. |
| 44 class BASE_EXPORT PowerObserver { |
| 45 public: |
| 46 // Notification of a change in power status of the computer, such |
| 47 // as from switching between battery and A/C power. |
| 48 virtual void OnPowerStateChange(bool on_battery_power) {} |
| 49 |
| 50 // Notification that the system is suspending. |
| 51 virtual void OnSuspend() {} |
| 52 |
| 53 // Notification that the system is resuming. |
| 54 virtual void OnResume() {} |
| 55 |
| 56 protected: |
| 57 virtual ~PowerObserver() {} |
| 58 }; |
| 59 |
| 60 PowerStateManager(); |
| 61 ~PowerStateManager(); |
| 62 |
| 63 void AddPowerObserver(PowerObserver* observer); |
| 64 void RemovePowerObserver(PowerObserver* observer); |
| 65 |
| 66 // Handle the power event and notify its observers with the specific state. |
| 67 void HandlePowerEvent(PowerEvent event_id); |
| 68 |
| 69 // Is the computer currently on battery power. Can be called on any thread. |
| 70 bool BatteryPower() const { |
| 71 // Using a lock here is not necessary for just a bool. |
| 72 return battery_in_use_; |
| 73 } |
| 74 |
| 75 // Return the single instance in the application wide. |
| 76 static PowerStateManager* Get(); |
| 77 |
| 78 #if defined(OS_MACOSX) |
| 79 // Allocate system resources needed by the PowerStateManager class. |
| 80 // |
| 81 // This function must be called before instantiating an instance of the class |
| 82 // and before the Sandbox is initialized. |
| 83 #if !defined(OS_IOS) |
| 84 static void AllocateSystemIOPorts(); |
| 85 #else |
| 86 static void AllocateSystemIOPorts() {} |
| 87 #endif // OS_IOS |
| 88 #endif // OS_MACOSX |
| 89 |
| 90 private: |
| 91 #if defined(OS_MACOSX) |
| 92 void PlatformInit(); |
| 93 void PlatformDestroy(); |
| 94 #endif |
| 95 |
| 96 // Platform-specific method to check whether the system is currently |
| 97 // running on battery power. Returns true if running on batteries, |
| 98 // false otherwise. |
| 99 bool IsBatteryPower(); |
| 100 |
| 101 // Checks the battery status and notifies observers if the battery |
| 102 // status has changed. |
| 103 void BatteryCheck(); |
| 104 |
| 105 void NotifyPowerStateChange(); |
| 106 void NotifySuspend(); |
| 107 void NotifyResume(); |
| 108 |
| 109 #if defined(OS_IOS) |
| 110 // Holds pointers to system event notification observers. |
| 111 std::vector<id> notification_observers_; |
| 112 #endif |
| 113 |
| 114 #if defined(ENABLE_BATTERY_MONITORING) |
| 115 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
| 116 #endif |
| 117 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; |
| 118 bool battery_in_use_; |
| 119 bool suspended_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(PowerStateManager); |
| 122 }; |
| 123 |
| 124 } // namespace base |
| 125 |
| 126 #endif // BASE_POWER_STATE_MANAGMER_H_ |
OLD | NEW |