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_H_ |
6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ | 6 #define BASE_POWER_MONITOR_POWER_MONITOR_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_observer.h" | 12 #include "base/power_monitor/power_observer.h" |
13 | 13 |
14 #if defined(OS_WIN) | 14 namespace base { |
15 #include <windows.h> | |
16 | 15 |
17 // Windows HiRes timers drain the battery faster so we need to know the battery | 16 class PowerMonitorSource; |
18 // status. This isn't true for other platforms. | |
19 #define ENABLE_BATTERY_MONITORING 1 | |
20 #else | |
21 #undef ENABLE_BATTERY_MONITORING | |
22 #endif // !OS_WIN | |
23 | |
24 #if defined(ENABLE_BATTERY_MONITORING) | |
25 #include "base/timer/timer.h" | |
26 #endif // defined(ENABLE_BATTERY_MONITORING) | |
27 | |
28 #if defined(OS_IOS) | |
29 #include <objc/runtime.h> | |
30 #endif // OS_IOS | |
31 | |
32 namespace base { | |
33 | 17 |
34 // A class used to monitor the power state change and notify the observers about | 18 // A class used to monitor the power state change and notify the observers about |
35 // the change event. | 19 // the change event. |
36 class BASE_EXPORT PowerMonitor { | 20 class BASE_EXPORT PowerMonitor { |
37 public: | 21 public: |
38 // Normalized list of power events. | 22 // The default constructor should only be used in the browser process. All |
39 enum PowerEvent { | 23 // other processes should explicitly pass an appropriate PowerMonitorSource |
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(); | 24 PowerMonitor(); |
25 // Takes ownership of |source|. | |
26 explicit PowerMonitor(PowerMonitorSource* source); | |
piman
2013/07/12 21:09:58
nit: pass a scoped_ptr instead to make it obvious
| |
46 ~PowerMonitor(); | 27 ~PowerMonitor(); |
47 | 28 |
48 // Get the application-wide PowerMonitor (if not present, returns NULL). | 29 // Get the process-wide PowerMonitor (if not present, returns NULL). |
49 static PowerMonitor* Get(); | 30 static PowerMonitor* Get(); |
50 | 31 |
51 #if defined(OS_MACOSX) | |
52 // Allocate system resources needed by the PowerMonitor class. | |
53 // | |
54 // This function must be called before instantiating an instance of the class | |
55 // and before the Sandbox is initialized. | |
56 #if !defined(OS_IOS) | |
57 static void AllocateSystemIOPorts(); | |
58 #else | |
59 static void AllocateSystemIOPorts() {} | |
60 #endif // OS_IOS | |
61 #endif // OS_MACOSX | |
62 | |
63 // Add and remove an observer. | 32 // Add and remove an observer. |
64 // Can be called from any thread. | 33 // Can be called from any thread. |
65 // Must not be called from within a notification callback. | 34 // Must not be called from within a notification callback. |
66 void AddObserver(PowerObserver* observer); | 35 void AddObserver(PowerObserver* observer); |
67 void RemoveObserver(PowerObserver* observer); | 36 void RemoveObserver(PowerObserver* observer); |
68 | 37 |
69 // Is the computer currently on battery power. Can be called on any thread. | 38 // Is the computer currently on battery power. |
70 bool BatteryPower() const { | 39 bool IsOnBatteryPower(); |
71 // Using a lock here is not necessary for just a bool. | |
72 return battery_in_use_; | |
73 } | |
74 | 40 |
75 private: | 41 private: |
76 friend class PowerMonitorTest; | 42 friend class PowerMonitorSource; |
77 // A friend function that is allowed to access the private ProcessPowerEvent. | |
78 friend void ProcessPowerEventHelper(PowerEvent); | |
79 | 43 |
80 #if defined(OS_WIN) | 44 PowerMonitorSource* Source(); |
81 // Represents a message-only window for power message handling on Windows. | |
82 // Only allow PowerMonitor to create it. | |
83 class PowerMessageWindow { | |
84 public: | |
85 PowerMessageWindow(); | |
86 ~PowerMessageWindow(); | |
87 | 45 |
88 private: | 46 void NotifyPowerStateChange(bool battery_in_use); |
89 void ProcessWmPowerBroadcastMessage(int event_id); | |
90 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, | |
91 WPARAM wparam, LPARAM lparam); | |
92 static LRESULT CALLBACK WndProcThunk(HWND hwnd, | |
93 UINT message, | |
94 WPARAM wparam, | |
95 LPARAM lparam); | |
96 // Instance of the module containing the window procedure. | |
97 HMODULE instance_; | |
98 // A hidden message-only window. | |
99 HWND message_hwnd_; | |
100 }; | |
101 #endif // OS_WIN | |
102 | |
103 #if defined(OS_MACOSX) | |
104 void PlatformInit(); | |
105 void PlatformDestroy(); | |
106 #endif | |
107 | |
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 | |
112 // running on battery power. Returns true if running on batteries, | |
113 // false otherwise. | |
114 bool IsBatteryPower(); | |
115 | |
116 // Checks the battery status and notifies observers if the battery | |
117 // status has changed. | |
118 void BatteryCheck(); | |
119 | |
120 void NotifyPowerStateChange(); | |
121 void NotifySuspend(); | 47 void NotifySuspend(); |
122 void NotifyResume(); | 48 void NotifyResume(); |
123 | 49 |
124 #if defined(OS_IOS) | |
125 // Holds pointers to system event notification observers. | |
126 std::vector<id> notification_observers_; | |
127 #endif | |
128 | |
129 #if defined(ENABLE_BATTERY_MONITORING) | |
130 base::OneShotTimer<PowerMonitor> delayed_battery_check_; | |
131 #endif | |
132 | |
133 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; | 50 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; |
134 bool battery_in_use_; | 51 scoped_ptr<PowerMonitorSource> source_; |
135 bool suspended_; | |
136 | |
137 #if defined(OS_WIN) | |
138 PowerMessageWindow power_message_window_; | |
139 #endif | |
140 | 52 |
141 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | 53 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); |
142 }; | 54 }; |
143 | 55 |
144 } // namespace base | 56 } // namespace base |
145 | 57 |
146 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ | 58 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ |
OLD | NEW |