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_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 RESUME_EVENT // The system is being resumed. | 51 RESUME_EVENT // The system is being resumed. |
52 }; | 52 }; |
53 | 53 |
54 // Type of devices whose change need to be monitored, such as add/remove. | 54 // Type of devices whose change need to be monitored, such as add/remove. |
55 enum DeviceType { | 55 enum DeviceType { |
56 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 56 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. |
57 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 57 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. |
58 DEVTYPE_UNKNOWN, // Other devices. | 58 DEVTYPE_UNKNOWN, // Other devices. |
59 }; | 59 }; |
60 | 60 |
61 struct BASE_EXPORT MediaDeviceInfo { | 61 struct BASE_EXPORT RemovableStorageInfo { |
62 MediaDeviceInfo(const std::string& id, | 62 RemovableStorageInfo(const std::string& id, |
63 const string16& device_name, | 63 const string16& device_name, |
64 const FilePath::StringType& device_location); | 64 const FilePath::StringType& device_location); |
65 | 65 |
66 // Unique media device id - persists between device attachments. | 66 // Unique device id - persists between device attachments. |
67 std::string unique_id; | 67 std::string unique_id; |
vandebo (ex-Chrome)
2012/08/25 00:58:10
nit: while you're here, can you make this device_i
Lei Zhang
2012/08/25 01:12:26
Done.
| |
68 | 68 |
69 // Human readable media device name. | 69 // Human readable removable storage device name. |
70 string16 name; | 70 string16 name; |
71 | 71 |
72 // Current attached media device location. | 72 // Current attached removable storage device location. |
73 FilePath::StringType location; | 73 FilePath::StringType location; |
74 }; | 74 }; |
75 | 75 |
76 // Create SystemMonitor. Only one SystemMonitor instance per application | 76 // Create SystemMonitor. Only one SystemMonitor instance per application |
77 // is allowed. | 77 // is allowed. |
78 SystemMonitor(); | 78 SystemMonitor(); |
79 ~SystemMonitor(); | 79 ~SystemMonitor(); |
80 | 80 |
81 // Get the application-wide SystemMonitor (if not present, returns NULL). | 81 // Get the application-wide SystemMonitor (if not present, returns NULL). |
82 static SystemMonitor* Get(); | 82 static SystemMonitor* Get(); |
83 | 83 |
84 #if defined(OS_MACOSX) | 84 #if defined(OS_MACOSX) |
85 // Allocate system resources needed by the SystemMonitor class. | 85 // Allocate system resources needed by the SystemMonitor class. |
86 // | 86 // |
87 // This function must be called before instantiating an instance of the class | 87 // This function must be called before instantiating an instance of the class |
88 // and before the Sandbox is initialized. | 88 // and before the Sandbox is initialized. |
89 #if !defined(OS_IOS) | 89 #if !defined(OS_IOS) |
90 static void AllocateSystemIOPorts(); | 90 static void AllocateSystemIOPorts(); |
91 #else | 91 #else |
92 static void AllocateSystemIOPorts() {} | 92 static void AllocateSystemIOPorts() {} |
93 #endif // OS_IOS | 93 #endif // OS_IOS |
94 #endif // OS_MACOSX | 94 #endif // OS_MACOSX |
95 | 95 |
96 // Returns information for attached media devices. | 96 // Returns information for attached removable storages. |
vandebo (ex-Chrome)
2012/08/25 00:58:10
nit: storages? Storage is generally both plural an
Lei Zhang
2012/08/25 01:12:26
Done.
| |
97 std::vector<MediaDeviceInfo> GetAttachedMediaDevices() const; | 97 std::vector<RemovableStorageInfo> GetAttachedRemovableStorages() const; |
98 | 98 |
99 // | 99 // |
100 // Power-related APIs | 100 // Power-related APIs |
101 // | 101 // |
102 | 102 |
103 // Is the computer currently on battery power. | 103 // Is the computer currently on battery power. |
104 // Can be called on any thread. | 104 // Can be called on any thread. |
105 bool BatteryPower() const { | 105 bool BatteryPower() const { |
106 // Using a lock here is not necessary for just a bool. | 106 // Using a lock here is not necessary for just a bool. |
107 return battery_in_use_; | 107 return battery_in_use_; |
(...skipping 19 matching lines...) Expand all Loading... | |
127 protected: | 127 protected: |
128 virtual ~PowerObserver() {} | 128 virtual ~PowerObserver() {} |
129 }; | 129 }; |
130 | 130 |
131 class BASE_EXPORT DevicesChangedObserver { | 131 class BASE_EXPORT DevicesChangedObserver { |
132 public: | 132 public: |
133 // Notification that the devices connected to the system have changed. | 133 // Notification that the devices connected to the system have changed. |
134 // This is only implemented on Windows currently. | 134 // This is only implemented on Windows currently. |
135 virtual void OnDevicesChanged(DeviceType device_type) {} | 135 virtual void OnDevicesChanged(DeviceType device_type) {} |
136 | 136 |
137 // When a media device is attached or detached, one of these two events | 137 // When a removable storage device is attached or detached, one of these |
138 // is triggered. | 138 // two events is triggered. |
139 virtual void OnMediaDeviceAttached(const std::string& id, | 139 virtual void OnRemovableStorageAttached( |
140 const string16& name, | 140 const std::string& id, |
141 const FilePath::StringType& location) {} | 141 const string16& name, |
142 | 142 const FilePath::StringType& location) {} |
143 virtual void OnMediaDeviceDetached(const std::string& id) {} | 143 virtual void OnRemovableStorageDetached(const std::string& id) {} |
144 | 144 |
145 protected: | 145 protected: |
146 virtual ~DevicesChangedObserver() {} | 146 virtual ~DevicesChangedObserver() {} |
147 }; | 147 }; |
148 | 148 |
149 // Add a new observer. | 149 // Add a new observer. |
150 // Can be called from any thread. | 150 // Can be called from any thread. |
151 // Must not be called from within a notification callback. | 151 // Must not be called from within a notification callback. |
152 void AddPowerObserver(PowerObserver* obs); | 152 void AddPowerObserver(PowerObserver* obs); |
153 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | 153 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
154 | 154 |
155 // Remove an existing observer. | 155 // Remove an existing observer. |
156 // Can be called from any thread. | 156 // Can be called from any thread. |
157 // Must not be called from within a notification callback. | 157 // Must not be called from within a notification callback. |
158 void RemovePowerObserver(PowerObserver* obs); | 158 void RemovePowerObserver(PowerObserver* obs); |
159 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 159 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
160 | 160 |
161 #if defined(OS_WIN) | 161 #if defined(OS_WIN) |
162 // Windows-specific handling of a WM_POWERBROADCAST message. | 162 // Windows-specific handling of a WM_POWERBROADCAST message. |
163 // Embedders of this API should hook their top-level window | 163 // Embedders of this API should hook their top-level window |
164 // message loop and forward WM_POWERBROADCAST through this call. | 164 // message loop and forward WM_POWERBROADCAST through this call. |
165 void ProcessWmPowerBroadcastMessage(int event_id); | 165 void ProcessWmPowerBroadcastMessage(int event_id); |
166 #endif | 166 #endif |
167 | 167 |
168 // Cross-platform handling of a power event. | 168 // Cross-platform handling of a power event. |
169 void ProcessPowerMessage(PowerEvent event_id); | 169 void ProcessPowerMessage(PowerEvent event_id); |
170 | 170 |
171 // Cross-platform handling of a device change event. | 171 // Cross-platform handling of a device change event. |
172 void ProcessDevicesChanged(DeviceType device_type); | 172 void ProcessDevicesChanged(DeviceType device_type); |
173 void ProcessMediaDeviceAttached(const std::string& id, | 173 void ProcessRemovableStorageAttached(const std::string& id, |
174 const string16& name, | 174 const string16& name, |
175 const FilePath::StringType& location); | 175 const FilePath::StringType& location); |
176 void ProcessMediaDeviceDetached(const std::string& id); | 176 void ProcessRemovableStorageDetached(const std::string& id); |
177 | 177 |
178 private: | 178 private: |
179 // Mapping of unique device id to device info tuple. | 179 // Mapping of unique device id to device info tuple. |
180 typedef std::map<std::string, MediaDeviceInfo> MediaDeviceMap; | 180 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; |
181 | 181 |
182 #if defined(OS_MACOSX) | 182 #if defined(OS_MACOSX) |
183 void PlatformInit(); | 183 void PlatformInit(); |
184 void PlatformDestroy(); | 184 void PlatformDestroy(); |
185 #endif | 185 #endif |
186 | 186 |
187 // Platform-specific method to check whether the system is currently | 187 // Platform-specific method to check whether the system is currently |
188 // running on battery power. Returns true if running on batteries, | 188 // running on battery power. Returns true if running on batteries, |
189 // false otherwise. | 189 // false otherwise. |
190 bool IsBatteryPower(); | 190 bool IsBatteryPower(); |
191 | 191 |
192 // Checks the battery status and notifies observers if the battery | 192 // Checks the battery status and notifies observers if the battery |
193 // status has changed. | 193 // status has changed. |
194 void BatteryCheck(); | 194 void BatteryCheck(); |
195 | 195 |
196 // Functions to trigger notifications. | 196 // Functions to trigger notifications. |
197 void NotifyDevicesChanged(DeviceType device_type); | 197 void NotifyDevicesChanged(DeviceType device_type); |
198 void NotifyMediaDeviceAttached(const std::string& id, | 198 void NotifyRemovableStorageAttached(const std::string& id, |
199 const string16& name, | 199 const string16& name, |
200 const FilePath::StringType& data); | 200 const FilePath::StringType& location); |
201 void NotifyMediaDeviceDetached(const std::string& id); | 201 void NotifyRemovableStorageDetached(const std::string& id); |
202 void NotifyPowerStateChange(); | 202 void NotifyPowerStateChange(); |
203 void NotifySuspend(); | 203 void NotifySuspend(); |
204 void NotifyResume(); | 204 void NotifyResume(); |
205 | 205 |
206 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | 206 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; |
207 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 207 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
208 devices_changed_observer_list_; | 208 devices_changed_observer_list_; |
209 bool battery_in_use_; | 209 bool battery_in_use_; |
210 bool suspended_; | 210 bool suspended_; |
211 | 211 |
212 #if defined(ENABLE_BATTERY_MONITORING) | 212 #if defined(ENABLE_BATTERY_MONITORING) |
213 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | 213 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
214 #endif | 214 #endif |
215 | 215 |
216 #if defined(OS_IOS) | 216 #if defined(OS_IOS) |
217 // Holds pointers to system event notification observers. | 217 // Holds pointers to system event notification observers. |
218 std::vector<id> notification_observers_; | 218 std::vector<id> notification_observers_; |
219 #endif | 219 #endif |
220 | 220 |
221 // Map of all the attached media devices. | 221 // Map of all the attached removable storage devices. |
222 MediaDeviceMap media_device_map_; | 222 RemovableStorageMap removable_storage_map_; |
223 | 223 |
224 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 224 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
225 }; | 225 }; |
226 | 226 |
227 } // namespace base | 227 } // namespace base |
228 | 228 |
229 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 229 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
OLD | NEW |