| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_H_ | 5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_ |
| 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ | 6 #define BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/observer_list_threadsafe.h" | 11 #include "base/observer_list_threadsafe.h" |
| 12 #include "base/power_monitor/power_monitor_source.h" |
| 12 #include "base/power_monitor/power_observer.h" | 13 #include "base/power_monitor/power_observer.h" |
| 13 | 14 |
| 14 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 15 #include <windows.h> | 16 #include <windows.h> |
| 16 | 17 |
| 17 // Windows HiRes timers drain the battery faster so we need to know the battery | 18 // Windows HiRes timers drain the battery faster so we need to know the battery |
| 18 // status. This isn't true for other platforms. | 19 // status. This isn't true for other platforms. |
| 19 #define ENABLE_BATTERY_MONITORING 1 | 20 #define ENABLE_BATTERY_MONITORING 1 |
| 20 #else | 21 #else |
| 21 #undef ENABLE_BATTERY_MONITORING | 22 #undef ENABLE_BATTERY_MONITORING |
| 22 #endif // !OS_WIN | 23 #endif // !OS_WIN |
| 23 | 24 |
| 24 #if defined(ENABLE_BATTERY_MONITORING) | 25 #if defined(ENABLE_BATTERY_MONITORING) |
| 25 #include "base/timer.h" | 26 #include "base/timer.h" |
| 26 #endif // defined(ENABLE_BATTERY_MONITORING) | 27 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 27 | 28 |
| 28 #if defined(OS_IOS) | 29 #if defined(OS_IOS) |
| 29 #include <objc/runtime.h> | 30 #include <objc/runtime.h> |
| 30 #endif // OS_IOS | 31 #endif // OS_IOS |
| 31 | 32 |
| 32 namespace base { | 33 namespace base { |
| 33 | 34 |
| 34 // A class used to monitor the power state change and notify the observers about | 35 // A class used to monitor the power state change and notify the observers about |
| 35 // the change event. | 36 // the change event. |
| 36 class BASE_EXPORT PowerMonitor { | 37 class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource { |
| 37 public: | 38 public: |
| 38 // Normalized list of power events. | 39 PowerMonitorDeviceSource(); |
| 39 enum PowerEvent { | 40 virtual ~PowerMonitorDeviceSource() OVERRIDE; |
| 40 POWER_STATE_EVENT, // The Power status of the system has changed. | |
| 41 SUSPEND_EVENT, // The system is being suspended. | |
| 42 RESUME_EVENT // The system is being resumed. | |
| 43 }; | |
| 44 | |
| 45 PowerMonitor(); | |
| 46 ~PowerMonitor(); | |
| 47 | |
| 48 // Get the application-wide PowerMonitor (if not present, returns NULL). | |
| 49 static PowerMonitor* Get(); | |
| 50 | 41 |
| 51 #if defined(OS_MACOSX) | 42 #if defined(OS_MACOSX) |
| 52 // Allocate system resources needed by the PowerMonitor class. | 43 // Allocate system resources needed by the PowerMonitor class. |
| 53 // | 44 // |
| 54 // This function must be called before instantiating an instance of the class | 45 // This function must be called before instantiating an instance of the class |
| 55 // and before the Sandbox is initialized. | 46 // and before the Sandbox is initialized. |
| 56 #if !defined(OS_IOS) | 47 #if !defined(OS_IOS) |
| 57 static void AllocateSystemIOPorts(); | 48 static void AllocateSystemIOPorts(); |
| 58 #else | 49 #else |
| 59 static void AllocateSystemIOPorts() {} | 50 static void AllocateSystemIOPorts() {} |
| 60 #endif // OS_IOS | 51 #endif // OS_IOS |
| 61 #endif // OS_MACOSX | 52 #endif // OS_MACOSX |
| 62 | 53 |
| 63 // Add and remove an observer. | |
| 64 // Can be called from any thread. | |
| 65 // Must not be called from within a notification callback. | |
| 66 void AddObserver(PowerObserver* observer); | |
| 67 void RemoveObserver(PowerObserver* observer); | |
| 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 private: | 54 private: |
| 76 friend class PowerMonitorTest; | 55 friend class PowerMonitorTest; |
| 77 // A friend function that is allowed to access the private ProcessPowerEvent. | |
| 78 friend void ProcessPowerEventHelper(PowerEvent); | |
| 79 | 56 |
| 80 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
| 81 // Represents a message-only window for power message handling on Windows. | 58 // Represents a message-only window for power message handling on Windows. |
| 82 // Only allow PowerMonitor to create it. | 59 // Only allow PowerMonitor to create it. |
| 83 class PowerMessageWindow { | 60 class PowerMessageWindow { |
| 84 public: | 61 public: |
| 85 PowerMessageWindow(); | 62 PowerMessageWindow(); |
| 86 ~PowerMessageWindow(); | 63 ~PowerMessageWindow(); |
| 87 | 64 |
| 88 private: | 65 private: |
| 89 void ProcessWmPowerBroadcastMessage(int event_id); | 66 void ProcessWmPowerBroadcastMessage(int event_id); |
| 90 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, | 67 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
| 91 WPARAM wparam, LPARAM lparam); | 68 WPARAM wparam, LPARAM lparam); |
| 92 static LRESULT CALLBACK WndProcThunk(HWND hwnd, | 69 static LRESULT CALLBACK WndProcThunk(HWND hwnd, |
| 93 UINT message, | 70 UINT message, |
| 94 WPARAM wparam, | 71 WPARAM wparam, |
| 95 LPARAM lparam); | 72 LPARAM lparam); |
| 96 // Instance of the module containing the window procedure. | 73 // Instance of the module containing the window procedure. |
| 97 HMODULE instance_; | 74 HMODULE instance_; |
| 98 // A hidden message-only window. | 75 // A hidden message-only window. |
| 99 HWND message_hwnd_; | 76 HWND message_hwnd_; |
| 100 }; | 77 }; |
| 101 #endif // OS_WIN | 78 #endif // OS_WIN |
| 102 | 79 |
| 103 #if defined(OS_MACOSX) | 80 #if defined(OS_MACOSX) |
| 104 void PlatformInit(); | 81 void PlatformInit(); |
| 105 void PlatformDestroy(); | 82 void PlatformDestroy(); |
| 106 #endif | 83 #endif |
| 107 | 84 |
| 108 // Cross-platform handling of a power event. | |
| 109 void ProcessPowerEvent(PowerEvent event_id); | |
| 110 | |
| 111 // Platform-specific method to check whether the system is currently | 85 // Platform-specific method to check whether the system is currently |
| 112 // running on battery power. Returns true if running on batteries, | 86 // running on battery power. Returns true if running on batteries, |
| 113 // false otherwise. | 87 // false otherwise. |
| 114 bool IsBatteryPower(); | 88 virtual bool IsBatteryPower() OVERRIDE; |
| 115 | 89 |
| 116 // Checks the battery status and notifies observers if the battery | 90 // Checks the battery status and notifies observers if the battery |
| 117 // status has changed. | 91 // status has changed. |
| 118 void BatteryCheck(); | 92 void BatteryCheck(); |
| 119 | 93 |
| 120 void NotifyPowerStateChange(); | |
| 121 void NotifySuspend(); | |
| 122 void NotifyResume(); | |
| 123 | |
| 124 #if defined(OS_IOS) | 94 #if defined(OS_IOS) |
| 125 // Holds pointers to system event notification observers. | 95 // Holds pointers to system event notification observers. |
| 126 std::vector<id> notification_observers_; | 96 std::vector<id> notification_observers_; |
| 127 #endif | 97 #endif |
| 128 | 98 |
| 129 #if defined(ENABLE_BATTERY_MONITORING) | 99 #if defined(ENABLE_BATTERY_MONITORING) |
| 130 base::OneShotTimer<PowerMonitor> delayed_battery_check_; | 100 base::OneShotTimer<PowerMonitor> delayed_battery_check_; |
| 131 #endif | 101 #endif |
| 132 | 102 |
| 133 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; | |
| 134 bool battery_in_use_; | |
| 135 bool suspended_; | |
| 136 | |
| 137 #if defined(OS_WIN) | 103 #if defined(OS_WIN) |
| 138 PowerMessageWindow power_message_window_; | 104 PowerMessageWindow power_message_window_; |
| 139 #endif | 105 #endif |
| 140 | 106 |
| 141 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | 107 DISALLOW_COPY_AND_ASSIGN(PowerMonitorDeviceSource); |
| 142 }; | 108 }; |
| 143 | 109 |
| 144 } // namespace base | 110 } // namespace base |
| 145 | 111 |
| 146 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ | 112 #endif // BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_ |
| OLD | NEW |