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/singleton.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 |
30 namespace views { | |
willchan no longer on Chromium
2012/10/16 20:32:37
This is a layering violation. Can we remove this r
vandebo (ex-Chrome)
2012/10/16 23:55:09
We could hide it by using inheritance instead of f
| |
31 class HWNDMessageHandler; | |
32 } | |
33 | |
41 namespace base { | 34 namespace base { |
42 | 35 |
43 // Class for monitoring various system-related subsystems | 36 class BASE_EXPORT PowerMonitor { |
44 // such as power management, network status, etc. | |
45 // TODO(mbelshe): Add support beyond just power management. | |
46 class BASE_EXPORT SystemMonitor { | |
47 public: | 37 public: |
48 // Normalized list of power events. | 38 // Normalized list of power events. |
49 enum PowerEvent { | 39 enum PowerEvent { |
50 POWER_STATE_EVENT, // The Power status of the system has changed. | 40 POWER_STATE_EVENT, // The Power status of the system has changed. |
51 SUSPEND_EVENT, // The system is being suspended. | 41 SUSPEND_EVENT, // The system is being suspended. |
52 RESUME_EVENT // The system is being resumed. | 42 RESUME_EVENT // The system is being resumed. |
53 }; | 43 }; |
54 | 44 |
55 // Type of devices whose change need to be monitored, such as add/remove. | 45 // Return the singleton instance in the application wide. |
56 enum DeviceType { | 46 static PowerMonitor* GetInstance(); |
57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | |
58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | |
59 DEVTYPE_UNKNOWN, // Other devices. | |
60 }; | |
61 | |
62 struct BASE_EXPORT RemovableStorageInfo { | |
63 RemovableStorageInfo(); | |
64 RemovableStorageInfo(const std::string& id, | |
65 const string16& device_name, | |
66 const FilePath::StringType& device_location); | |
67 | |
68 // Unique device id - persists between device attachments. | |
69 std::string device_id; | |
70 | |
71 // Human readable removable storage device name. | |
72 string16 name; | |
73 | |
74 // Current attached removable storage device location. | |
75 FilePath::StringType location; | |
76 }; | |
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 | 47 |
86 #if defined(OS_MACOSX) | 48 #if defined(OS_MACOSX) |
87 // Allocate system resources needed by the SystemMonitor class. | 49 // Allocate system resources needed by the PowerMonitor class. |
88 // | 50 // |
89 // This function must be called before instantiating an instance of the class | 51 // This function must be called before instantiating an instance of the class |
90 // and before the Sandbox is initialized. | 52 // and before the Sandbox is initialized. |
91 #if !defined(OS_IOS) | 53 #if !defined(OS_IOS) |
92 static void AllocateSystemIOPorts(); | 54 static void AllocateSystemIOPorts(); |
93 #else | 55 #else |
94 static void AllocateSystemIOPorts() {} | 56 static void AllocateSystemIOPorts() {} |
95 #endif // OS_IOS | 57 #endif // OS_IOS |
96 #endif // OS_MACOSX | 58 #endif // OS_MACOSX |
97 | 59 |
98 // Returns information for attached removable storage. | 60 // Add and remove an observer. |
99 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; | 61 // Can be called from any thread. |
62 // Must not be called from within a notification callback. | |
63 void AddObserver(PowerObserver* observer); | |
64 void RemoveObserver(PowerObserver* observer); | |
100 | 65 |
101 // | 66 // 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 { | 67 bool BatteryPower() const { |
108 // Using a lock here is not necessary for just a bool. | 68 // Using a lock here is not necessary for just a bool. |
109 return battery_in_use_; | 69 return battery_in_use_; |
110 } | 70 } |
111 | 71 |
112 // Callbacks will be called on the thread which creates the SystemMonitor. | 72 private: |
113 // During the callback, Add/RemoveObserver will block until the callbacks | 73 friend struct DefaultSingletonTraits<PowerMonitor>; |
114 // are finished. Observers should implement quick callback functions; if | 74 friend class PowerMonitorTest; |
115 // lengthy operations are needed, the observer should take care to invoke | 75 friend class views::HWNDMessageHandler; |
116 // the operation on an appropriate thread. | 76 // Defined in power_monitor_mac.mm. |
117 class BASE_EXPORT PowerObserver { | 77 friend void ProcessPowerEventHelper(PowerEvent event_id); |
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 | 78 |
123 // Notification that the system is suspending. | 79 PowerMonitor(); |
124 virtual void OnSuspend() {} | 80 ~PowerMonitor(); |
125 | 81 |
126 // Notification that the system is resuming. | 82 #if defined(OS_MACOSX) |
127 virtual void OnResume() {} | 83 void PlatformInit(); |
128 | 84 void PlatformDestroy(); |
129 protected: | 85 #endif |
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 | 86 |
167 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
168 // Windows-specific handling of a WM_POWERBROADCAST message. | 88 // Windows-specific handling of a WM_POWERBROADCAST message. |
169 // Embedders of this API should hook their top-level window | 89 // Embedders of this API should hook their top-level window |
170 // message loop and forward WM_POWERBROADCAST through this call. | 90 // message loop and forward WM_POWERBROADCAST through this call. |
171 void ProcessWmPowerBroadcastMessage(int event_id); | 91 void ProcessWmPowerBroadcastMessage(int event_id); |
172 #endif | 92 #endif |
173 | 93 |
174 // Cross-platform handling of a power event. | 94 // Cross-platform handling of a power event. |
175 void ProcessPowerMessage(PowerEvent event_id); | 95 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 | 96 |
193 // Platform-specific method to check whether the system is currently | 97 // Platform-specific method to check whether the system is currently |
194 // running on battery power. Returns true if running on batteries, | 98 // running on battery power. Returns true if running on batteries, |
195 // false otherwise. | 99 // false otherwise. |
196 bool IsBatteryPower(); | 100 bool IsBatteryPower(); |
197 | 101 |
198 // Checks the battery status and notifies observers if the battery | 102 // Checks the battery status and notifies observers if the battery |
199 // status has changed. | 103 // status has changed. |
200 void BatteryCheck(); | 104 void BatteryCheck(); |
201 | 105 |
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(); | 106 void NotifyPowerStateChange(); |
209 void NotifySuspend(); | 107 void NotifySuspend(); |
210 void NotifyResume(); | 108 void NotifyResume(); |
211 | 109 |
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) | 110 #if defined(OS_IOS) |
223 // Holds pointers to system event notification observers. | 111 // Holds pointers to system event notification observers. |
224 std::vector<id> notification_observers_; | 112 std::vector<id> notification_observers_; |
225 #endif | 113 #endif |
226 | 114 |
227 // For manipulating removable_storage_map_ structure. | 115 #if defined(ENABLE_BATTERY_MONITORING) |
228 mutable base::Lock removable_storage_lock_; | 116 base::OneShotTimer<PowerMonitor> delayed_battery_check_; |
229 // Map of all the attached removable storage devices. | 117 #endif |
230 RemovableStorageMap removable_storage_map_; | |
231 | 118 |
232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 119 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; |
120 bool battery_in_use_; | |
121 bool suspended_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | |
233 }; | 124 }; |
234 | 125 |
235 } // namespace base | 126 } // namespace base |
236 | 127 |
237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 128 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ |
OLD | NEW |