OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_H_ |
6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | 7 |
12 #include "base/base_export.h" | 8 #include "base/base_export.h" |
13 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
14 #include "base/file_path.h" | 10 #include "base/memory/scoped_ptr.h" |
15 #include "base/string16.h" | 11 #include "base/power_monitor/power_observer.h" |
16 #include "base/synchronization/lock.h" | 12 #include "base/observer_list_threadsafe.h" |
17 #include "build/build_config.h" | |
18 | 13 |
19 // Windows HiRes timers drain the battery faster so we need to know the battery | 14 // Windows HiRes timers drain the battery faster so we need to know the battery |
20 // status. This isn't true for other platforms. | 15 // status. This isn't true for other platforms. |
21 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
22 #define ENABLE_BATTERY_MONITORING 1 | 17 #define ENABLE_BATTERY_MONITORING 1 |
23 #else | 18 #else |
24 #undef ENABLE_BATTERY_MONITORING | 19 #undef ENABLE_BATTERY_MONITORING |
25 #endif // !OS_WIN | 20 #endif // !OS_WIN |
26 | 21 |
27 #include "base/observer_list_threadsafe.h" | |
28 #if defined(ENABLE_BATTERY_MONITORING) | 22 #if defined(ENABLE_BATTERY_MONITORING) |
29 #include "base/timer.h" | 23 #include "base/timer.h" |
30 #endif // defined(ENABLE_BATTERY_MONITORING) | 24 #endif // defined(ENABLE_BATTERY_MONITORING) |
31 | 25 |
32 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
33 #include <IOKit/pwr_mgt/IOPMLib.h> | |
34 #include <IOKit/IOMessage.h> | |
35 #endif // OS_MACOSX && !OS_IOS | |
36 | |
37 #if defined(OS_IOS) | 26 #if defined(OS_IOS) |
38 #include <objc/runtime.h> | 27 #include <objc/runtime.h> |
39 #endif // OS_IOS | 28 #endif // OS_IOS |
40 | 29 |
41 namespace base { | 30 namespace base { |
42 | 31 |
43 // Class for monitoring various system-related subsystems | 32 // A class used to monitor the power state change and notify the observers about |
44 // such as power management, network status, etc. | 33 // the change event. |
45 // TODO(mbelshe): Add support beyond just power management. | 34 class BASE_EXPORT PowerMonitor { |
46 class BASE_EXPORT SystemMonitor { | |
47 public: | 35 public: |
48 // Normalized list of power events. | 36 // Normalized list of power events. |
49 enum PowerEvent { | 37 enum PowerEvent { |
50 POWER_STATE_EVENT, // The Power status of the system has changed. | 38 POWER_STATE_EVENT, // The Power status of the system has changed. |
51 SUSPEND_EVENT, // The system is being suspended. | 39 SUSPEND_EVENT, // The system is being suspended. |
52 RESUME_EVENT // The system is being resumed. | 40 RESUME_EVENT // The system is being resumed. |
53 }; | 41 }; |
54 | 42 |
55 // Type of devices whose change need to be monitored, such as add/remove. | 43 // A helper class to wrap the process of power event in PowerMonitor. The |
56 enum DeviceType { | 44 // power event source is allowed to notify the PowerMonitor's observers via |
57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 45 // ProcessPowerEvent methods of Signaler instance. |
58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 46 // |
59 DEVTYPE_UNKNOWN, // Other devices. | 47 // The reason why not making ProcessPowerEvent as the public method of |
48 // PowerMonitor instead is to avoid layering violation for the friendship | |
49 // declaration of PowerMonitor, e.g. making views::HWNDMessageHandler as a | |
50 // friend of base::PowerMonitor violates the layering rule. | |
51 class BASE_EXPORT Signaler { | |
52 public: | |
53 #if defined(OS_WIN) | |
54 void ProcessWmPowerBroadcastMessage(int event) { | |
55 CHECK(monitor_); | |
vandebo (ex-Chrome)
2012/10/29 18:04:38
These checks don't do anything.
Hongbo Min
2012/10/30 14:33:47
Use if stmt instead.
| |
56 monitor_->ProcessWmPowerBroadcastMessage(event); | |
57 } | |
58 #endif | |
59 void ProcessPowerEvent(PowerMonitor::PowerEvent event) { | |
60 CHECK(monitor_); | |
61 monitor_->ProcessPowerEvent(event); | |
62 } | |
63 | |
64 private: | |
65 friend class PowerMonitor; | |
66 // Only allows PowerMonitor to create it. | |
67 Signaler(PowerMonitor* monitor) : monitor_(monitor) {} | |
68 ~Signaler() {} | |
vandebo (ex-Chrome)
2012/10/29 18:04:38
Don't inline the implementation, but it in the imp
Hongbo Min
2012/10/30 14:33:47
Done.
| |
69 | |
70 PowerMonitor* monitor_; | |
60 }; | 71 }; |
61 | 72 |
62 struct BASE_EXPORT RemovableStorageInfo { | 73 PowerMonitor(); |
63 RemovableStorageInfo(); | 74 ~PowerMonitor(); |
64 RemovableStorageInfo(const std::string& id, | |
65 const string16& device_name, | |
66 const FilePath::StringType& device_location); | |
67 | 75 |
68 // Unique device id - persists between device attachments. | 76 // Return the singleton instance in the application wide. |
69 std::string device_id; | 77 static PowerMonitor* Get(); |
70 | 78 |
71 // Human readable removable storage device name. | 79 // Return the |signaler_| pointer at the first time it gets called, otherwise |
vandebo (ex-Chrome)
2012/10/29 18:04:38
nit: The first call to this method will return an
Hongbo Min
2012/10/30 14:33:47
Done.
| |
72 string16 name; | 80 // NULL is returned. It allows the power event source to have a chance to |
73 | 81 // store the |signaler_| pointer of PowerMonitor for use in future. |
74 // Current attached removable storage device location. | 82 // |
75 FilePath::StringType location; | 83 // Note that we assume there is at most only one power event source. |
76 }; | 84 PowerMonitor::Signaler* GetSignalerOnce(); |
77 | |
78 // Create SystemMonitor. Only one SystemMonitor instance per application | |
79 // is allowed. | |
80 SystemMonitor(); | |
81 ~SystemMonitor(); | |
82 | |
83 // Get the application-wide SystemMonitor (if not present, returns NULL). | |
84 static SystemMonitor* Get(); | |
85 | 85 |
86 #if defined(OS_MACOSX) | 86 #if defined(OS_MACOSX) |
87 // Allocate system resources needed by the SystemMonitor class. | 87 // Allocate system resources needed by the PowerMonitor class. |
88 // | 88 // |
89 // This function must be called before instantiating an instance of the class | 89 // This function must be called before instantiating an instance of the class |
90 // and before the Sandbox is initialized. | 90 // and before the Sandbox is initialized. |
91 #if !defined(OS_IOS) | 91 #if !defined(OS_IOS) |
92 static void AllocateSystemIOPorts(); | 92 static void AllocateSystemIOPorts(); |
93 #else | 93 #else |
94 static void AllocateSystemIOPorts() {} | 94 static void AllocateSystemIOPorts() {} |
95 #endif // OS_IOS | 95 #endif // OS_IOS |
96 #endif // OS_MACOSX | 96 #endif // OS_MACOSX |
97 | 97 |
98 // Returns information for attached removable storage. | 98 // Add and remove an observer. |
99 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; | 99 // Can be called from any thread. |
100 // Must not be called from within a notification callback. | |
101 void AddObserver(PowerObserver* observer); | |
102 void RemoveObserver(PowerObserver* observer); | |
100 | 103 |
101 // | 104 // Is the computer currently on battery power. Can be called on any thread. |
102 // Power-related APIs | |
103 // | |
104 | |
105 // Is the computer currently on battery power. | |
106 // Can be called on any thread. | |
107 bool BatteryPower() const { | 105 bool BatteryPower() const { |
108 // Using a lock here is not necessary for just a bool. | 106 // Using a lock here is not necessary for just a bool. |
109 return battery_in_use_; | 107 return battery_in_use_; |
110 } | 108 } |
111 | 109 |
112 // Callbacks will be called on the thread which creates the SystemMonitor. | 110 private: |
113 // During the callback, Add/RemoveObserver will block until the callbacks | 111 friend class PowerMonitorTest; |
114 // are finished. Observers should implement quick callback functions; if | 112 friend class Signaler; |
115 // lengthy operations are needed, the observer should take care to invoke | |
116 // the operation on an appropriate thread. | |
117 class BASE_EXPORT PowerObserver { | |
118 public: | |
119 // Notification of a change in power status of the computer, such | |
120 // as from switching between battery and A/C power. | |
121 virtual void OnPowerStateChange(bool on_battery_power) {} | |
122 | 113 |
123 // Notification that the system is suspending. | 114 #if defined(OS_MACOSX) |
124 virtual void OnSuspend() {} | 115 void PlatformInit(); |
125 | 116 void PlatformDestroy(); |
126 // Notification that the system is resuming. | 117 #endif |
127 virtual void OnResume() {} | |
128 | |
129 protected: | |
130 virtual ~PowerObserver() {} | |
131 }; | |
132 | |
133 class BASE_EXPORT DevicesChangedObserver { | |
134 public: | |
135 // Notification that the devices connected to the system have changed. | |
136 // This is only implemented on Windows currently. | |
137 virtual void OnDevicesChanged(DeviceType device_type) {} | |
138 | |
139 // When a removable storage device is attached or detached, one of these | |
140 // two events is triggered. | |
141 virtual void OnRemovableStorageAttached( | |
142 const std::string& id, | |
143 const string16& name, | |
144 const FilePath::StringType& location) {} | |
145 virtual void OnRemovableStorageDetached(const std::string& id) {} | |
146 | |
147 protected: | |
148 virtual ~DevicesChangedObserver() {} | |
149 }; | |
150 | |
151 // Add a new observer. | |
152 // Can be called from any thread. | |
153 // Must not be called from within a notification callback. | |
154 void AddPowerObserver(PowerObserver* obs); | |
155 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | |
156 | |
157 // Remove an existing observer. | |
158 // Can be called from any thread. | |
159 // Must not be called from within a notification callback. | |
160 void RemovePowerObserver(PowerObserver* obs); | |
161 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | |
162 | |
163 // The ProcessFoo() style methods are a broken pattern and should not | |
164 // be copied. Any significant addition to this class is blocked on | |
165 // refactoring to improve the state of affairs. See http://crbug.com/149059 | |
166 | 118 |
167 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
168 // Windows-specific handling of a WM_POWERBROADCAST message. | 120 // Windows-specific handling of a WM_POWERBROADCAST message. |
169 // Embedders of this API should hook their top-level window | 121 // Embedders of this API should hook their top-level window |
170 // message loop and forward WM_POWERBROADCAST through this call. | 122 // message loop and forward WM_POWERBROADCAST through this call. |
171 void ProcessWmPowerBroadcastMessage(int event_id); | 123 void ProcessWmPowerBroadcastMessage(int event_id); |
172 #endif | 124 #endif |
173 | 125 |
174 // Cross-platform handling of a power event. | 126 // Cross-platform handling of a power event. |
175 void ProcessPowerMessage(PowerEvent event_id); | 127 void ProcessPowerEvent(PowerEvent event_id); |
176 | |
177 // Cross-platform handling of a device change event. | |
178 void ProcessDevicesChanged(DeviceType device_type); | |
179 void ProcessRemovableStorageAttached(const std::string& id, | |
180 const string16& name, | |
181 const FilePath::StringType& location); | |
182 void ProcessRemovableStorageDetached(const std::string& id); | |
183 | |
184 private: | |
185 // Mapping of unique device id to device info tuple. | |
186 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; | |
187 | |
188 #if defined(OS_MACOSX) | |
189 void PlatformInit(); | |
190 void PlatformDestroy(); | |
191 #endif | |
192 | 128 |
193 // Platform-specific method to check whether the system is currently | 129 // Platform-specific method to check whether the system is currently |
194 // running on battery power. Returns true if running on batteries, | 130 // running on battery power. Returns true if running on batteries, |
195 // false otherwise. | 131 // false otherwise. |
196 bool IsBatteryPower(); | 132 bool IsBatteryPower(); |
197 | 133 |
198 // Checks the battery status and notifies observers if the battery | 134 // Checks the battery status and notifies observers if the battery |
199 // status has changed. | 135 // status has changed. |
200 void BatteryCheck(); | 136 void BatteryCheck(); |
201 | 137 |
202 // Functions to trigger notifications. | |
203 void NotifyDevicesChanged(DeviceType device_type); | |
204 void NotifyRemovableStorageAttached(const std::string& id, | |
205 const string16& name, | |
206 const FilePath::StringType& location); | |
207 void NotifyRemovableStorageDetached(const std::string& id); | |
208 void NotifyPowerStateChange(); | 138 void NotifyPowerStateChange(); |
209 void NotifySuspend(); | 139 void NotifySuspend(); |
210 void NotifyResume(); | 140 void NotifyResume(); |
211 | 141 |
212 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | |
213 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | |
214 devices_changed_observer_list_; | |
215 bool battery_in_use_; | |
216 bool suspended_; | |
217 | |
218 #if defined(ENABLE_BATTERY_MONITORING) | |
219 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | |
220 #endif | |
221 | |
222 #if defined(OS_IOS) | 142 #if defined(OS_IOS) |
223 // Holds pointers to system event notification observers. | 143 // Holds pointers to system event notification observers. |
224 std::vector<id> notification_observers_; | 144 std::vector<id> notification_observers_; |
225 #endif | 145 #endif |
226 | 146 |
227 // For manipulating removable_storage_map_ structure. | 147 #if defined(ENABLE_BATTERY_MONITORING) |
228 mutable base::Lock removable_storage_lock_; | 148 base::OneShotTimer<PowerMonitor> delayed_battery_check_; |
229 // Map of all the attached removable storage devices. | 149 #endif |
230 RemovableStorageMap removable_storage_map_; | |
231 | 150 |
232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 151 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; |
152 bool battery_in_use_; | |
153 bool suspended_; | |
154 | |
155 // The Signaler instance owned by PowerMonitor, but might be referenced by | |
156 // other object and used in the whole life time of the program. | |
157 PowerMonitor::Signaler* signaler_; | |
158 | |
159 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | |
233 }; | 160 }; |
234 | 161 |
235 } // namespace base | 162 } // namespace base |
236 | 163 |
237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 164 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ |
OLD | NEW |