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/timer.h" | 26 #include "base/timer/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(); |
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; | |
77 // A friend function that is allowed to access the private ProcessPowerEvent. | |
78 friend void ProcessPowerEventHelper(PowerEvent); | |
79 | |
80 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
81 // Represents a message-only window for power message handling on Windows. | 56 // Represents a message-only window for power message handling on Windows. |
82 // Only allow PowerMonitor to create it. | 57 // Only allow PowerMonitor to create it. |
83 class PowerMessageWindow { | 58 class PowerMessageWindow { |
84 public: | 59 public: |
85 PowerMessageWindow(); | 60 PowerMessageWindow(); |
86 ~PowerMessageWindow(); | 61 ~PowerMessageWindow(); |
87 | 62 |
88 private: | 63 private: |
89 void ProcessWmPowerBroadcastMessage(int event_id); | 64 void ProcessWmPowerBroadcastMessage(int event_id); |
90 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, | 65 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
91 WPARAM wparam, LPARAM lparam); | 66 WPARAM wparam, LPARAM lparam); |
92 static LRESULT CALLBACK WndProcThunk(HWND hwnd, | 67 static LRESULT CALLBACK WndProcThunk(HWND hwnd, |
93 UINT message, | 68 UINT message, |
94 WPARAM wparam, | 69 WPARAM wparam, |
95 LPARAM lparam); | 70 LPARAM lparam); |
96 // Instance of the module containing the window procedure. | 71 // Instance of the module containing the window procedure. |
97 HMODULE instance_; | 72 HMODULE instance_; |
98 // A hidden message-only window. | 73 // A hidden message-only window. |
99 HWND message_hwnd_; | 74 HWND message_hwnd_; |
100 }; | 75 }; |
101 #endif // OS_WIN | 76 #endif // OS_WIN |
102 | 77 |
103 #if defined(OS_MACOSX) | 78 #if defined(OS_MACOSX) |
104 void PlatformInit(); | 79 void PlatformInit(); |
105 void PlatformDestroy(); | 80 void PlatformDestroy(); |
106 #endif | 81 #endif |
107 | 82 |
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 | 83 // Platform-specific method to check whether the system is currently |
112 // running on battery power. Returns true if running on batteries, | 84 // running on battery power. Returns true if running on batteries, |
113 // false otherwise. | 85 // false otherwise. |
114 bool IsBatteryPower(); | 86 virtual bool IsBatteryPower() OVERRIDE; |
palmer
2013/07/09 21:16:10
Harmonize this name with whatever you choose for t
| |
115 | 87 |
116 // Checks the battery status and notifies observers if the battery | 88 // Checks the battery status and notifies observers if the battery |
117 // status has changed. | 89 // status has changed. |
118 void BatteryCheck(); | 90 void BatteryCheck(); |
119 | 91 |
120 void NotifyPowerStateChange(); | |
121 void NotifySuspend(); | |
122 void NotifyResume(); | |
123 | |
124 #if defined(OS_IOS) | 92 #if defined(OS_IOS) |
125 // Holds pointers to system event notification observers. | 93 // Holds pointers to system event notification observers. |
126 std::vector<id> notification_observers_; | 94 std::vector<id> notification_observers_; |
127 #endif | 95 #endif |
128 | 96 |
129 #if defined(ENABLE_BATTERY_MONITORING) | 97 #if defined(ENABLE_BATTERY_MONITORING) |
130 base::OneShotTimer<PowerMonitor> delayed_battery_check_; | 98 base::OneShotTimer<PowerMonitorDeviceSource> delayed_battery_check_; |
131 #endif | 99 #endif |
132 | 100 |
133 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; | |
134 bool battery_in_use_; | |
135 bool suspended_; | |
136 | |
137 #if defined(OS_WIN) | 101 #if defined(OS_WIN) |
138 PowerMessageWindow power_message_window_; | 102 PowerMessageWindow power_message_window_; |
139 #endif | 103 #endif |
140 | 104 |
141 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | 105 DISALLOW_COPY_AND_ASSIGN(PowerMonitorDeviceSource); |
142 }; | 106 }; |
143 | 107 |
144 } // namespace base | 108 } // namespace base |
145 | 109 |
146 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ | 110 #endif // BASE_POWER_MONITOR_POWER_MONITOR_DEVICE_SOURCE_H_ |
OLD | NEW |