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 <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/base_export.h" | 12 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
13 #include "base/tuple.h" | 14 #include "base/file_path.h" |
15 #include "base/string16.h" | |
14 #include "build/build_config.h" | 16 #include "build/build_config.h" |
15 | 17 |
16 // 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 |
17 // status. This isn't true for other platforms. | 19 // status. This isn't true for other platforms. |
18 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
19 #define ENABLE_BATTERY_MONITORING 1 | 21 #define ENABLE_BATTERY_MONITORING 1 |
20 #else | 22 #else |
21 #undef ENABLE_BATTERY_MONITORING | 23 #undef ENABLE_BATTERY_MONITORING |
22 #endif // !OS_WIN | 24 #endif // !OS_WIN |
23 | 25 |
24 #include "base/observer_list_threadsafe.h" | 26 #include "base/observer_list_threadsafe.h" |
25 #if defined(ENABLE_BATTERY_MONITORING) | 27 #if defined(ENABLE_BATTERY_MONITORING) |
26 #include "base/timer.h" | 28 #include "base/timer.h" |
27 #endif // defined(ENABLE_BATTERY_MONITORING) | 29 #endif // defined(ENABLE_BATTERY_MONITORING) |
28 | 30 |
29 #if defined(OS_MACOSX) && !defined(OS_IOS) | 31 #if defined(OS_MACOSX) && !defined(OS_IOS) |
30 #include <IOKit/pwr_mgt/IOPMLib.h> | 32 #include <IOKit/pwr_mgt/IOPMLib.h> |
31 #include <IOKit/IOMessage.h> | 33 #include <IOKit/IOMessage.h> |
32 #endif // OS_MACOSX && !OS_IOS | 34 #endif // OS_MACOSX && !OS_IOS |
33 | 35 |
34 #if defined(OS_IOS) | 36 #if defined(OS_IOS) |
35 #include <objc/runtime.h> | 37 #include <objc/runtime.h> |
36 #endif // OS_IOS | 38 #endif // OS_IOS |
37 | 39 |
38 class FilePath; | |
39 | |
40 namespace base { | 40 namespace base { |
41 | 41 |
42 // Class for monitoring various system-related subsystems | 42 // Class for monitoring various system-related subsystems |
43 // such as power management, network status, etc. | 43 // such as power management, network status, etc. |
44 // TODO(mbelshe): Add support beyond just power management. | 44 // TODO(mbelshe): Add support beyond just power management. |
45 class BASE_EXPORT SystemMonitor { | 45 class BASE_EXPORT SystemMonitor { |
46 public: | 46 public: |
47 // Normalized list of power events. | 47 // Normalized list of power events. |
48 enum PowerEvent { | 48 enum PowerEvent { |
49 POWER_STATE_EVENT, // The Power status of the system has changed. | 49 POWER_STATE_EVENT, // The Power status of the system has changed. |
50 SUSPEND_EVENT, // The system is being suspended. | 50 SUSPEND_EVENT, // The system is being suspended. |
51 RESUME_EVENT // The system is being resumed. | 51 RESUME_EVENT // The system is being resumed. |
52 }; | 52 }; |
53 | 53 |
54 typedef unsigned int DeviceIdType; | 54 // Type of location data to identify a currently attached media device. |
55 // (Media device id, Media device name, Media device path) | 55 enum MediaDeviceType { |
56 typedef Tuple3<DeviceIdType, std::string, FilePath> MediaDeviceInfo; | 56 TYPE_PATH, // FilePath::StringType, e.g. a mount point. |
57 TYPE_MTP, // (W)string to locate a MTP device, e.g. its usb bus/port. | |
58 }; | |
59 | |
60 struct MediaDeviceInfo { | |
61 MediaDeviceInfo(const std::string& id, | |
62 const string16& device_name, | |
63 MediaDeviceType device_type, | |
64 const FilePath::StringType& device_location) | |
65 : unique_id(id), | |
66 name(device_name), | |
67 type(device_type), | |
68 location(device_location) { | |
69 } | |
70 | |
71 // Unique media device id - persists between device attachments. | |
72 std::string unique_id; | |
Evan Stade
2012/07/20 18:39:00
we also need a way to get the unique_id and locati
Evan Stade
2012/07/20 18:40:08
and also, the mount point for any unique_id (assum
| |
73 | |
74 // Human readable media device name. | |
75 string16 name; | |
76 | |
77 // Media device type. | |
78 MediaDeviceType type; | |
79 | |
80 // Current attached media device location. | |
81 FilePath::StringType location; | |
82 }; | |
57 | 83 |
58 // Create SystemMonitor. Only one SystemMonitor instance per application | 84 // Create SystemMonitor. Only one SystemMonitor instance per application |
59 // is allowed. | 85 // is allowed. |
60 SystemMonitor(); | 86 SystemMonitor(); |
61 ~SystemMonitor(); | 87 ~SystemMonitor(); |
62 | 88 |
63 // Get the application-wide SystemMonitor (if not present, returns NULL). | 89 // Get the application-wide SystemMonitor (if not present, returns NULL). |
64 static SystemMonitor* Get(); | 90 static SystemMonitor* Get(); |
65 | 91 |
66 #if defined(OS_MACOSX) | 92 #if defined(OS_MACOSX) |
67 // Allocate system resources needed by the SystemMonitor class. | 93 // Allocate system resources needed by the SystemMonitor class. |
68 // | 94 // |
69 // This function must be called before instantiating an instance of the class | 95 // This function must be called before instantiating an instance of the class |
70 // and before the Sandbox is initialized. | 96 // and before the Sandbox is initialized. |
71 #if !defined(OS_IOS) | 97 #if !defined(OS_IOS) |
72 static void AllocateSystemIOPorts(); | 98 static void AllocateSystemIOPorts(); |
73 #else | 99 #else |
74 static void AllocateSystemIOPorts() {} | 100 static void AllocateSystemIOPorts() {} |
75 #endif // OS_IOS | 101 #endif // OS_IOS |
76 #endif // OS_MACOSX | 102 #endif // OS_MACOSX |
77 | 103 |
104 // Returns information for attached media devices. | |
105 std::vector<MediaDeviceInfo> GetAttachedMediaDevices() const; | |
106 | |
78 // | 107 // |
79 // Power-related APIs | 108 // Power-related APIs |
80 // | 109 // |
81 | 110 |
82 // Is the computer currently on battery power. | 111 // Is the computer currently on battery power. |
83 // Can be called on any thread. | 112 // Can be called on any thread. |
84 bool BatteryPower() const { | 113 bool BatteryPower() const { |
85 // Using a lock here is not necessary for just a bool. | 114 // Using a lock here is not necessary for just a bool. |
86 return battery_in_use_; | 115 return battery_in_use_; |
87 } | 116 } |
(...skipping 20 matching lines...) Expand all Loading... | |
108 }; | 137 }; |
109 | 138 |
110 class BASE_EXPORT DevicesChangedObserver { | 139 class BASE_EXPORT DevicesChangedObserver { |
111 public: | 140 public: |
112 // Notification that the devices connected to the system have changed. | 141 // Notification that the devices connected to the system have changed. |
113 // This is only implemented on Windows currently. | 142 // This is only implemented on Windows currently. |
114 virtual void OnDevicesChanged() {} | 143 virtual void OnDevicesChanged() {} |
115 | 144 |
116 // When a media device is attached or detached, one of these two events | 145 // When a media device is attached or detached, one of these two events |
117 // is triggered. | 146 // is triggered. |
118 // TODO(vandebo) Pass an appropriate device identifier or way to interact | 147 virtual void OnMediaDeviceAttached(const std::string& id, |
119 // with the devices instead of FilePath. | 148 const string16& name, |
120 virtual void OnMediaDeviceAttached(const DeviceIdType& id, | 149 MediaDeviceType type, |
121 const std::string& name, | 150 const FilePath::StringType& location) {} |
122 const FilePath& path) {} | |
123 | 151 |
124 virtual void OnMediaDeviceDetached(const DeviceIdType& id) {} | 152 virtual void OnMediaDeviceDetached(const std::string& id) {} |
125 | 153 |
126 protected: | 154 protected: |
127 virtual ~DevicesChangedObserver() {} | 155 virtual ~DevicesChangedObserver() {} |
128 }; | 156 }; |
129 | 157 |
130 // Add a new observer. | 158 // Add a new observer. |
131 // Can be called from any thread. | 159 // Can be called from any thread. |
132 // Must not be called from within a notification callback. | 160 // Must not be called from within a notification callback. |
133 void AddPowerObserver(PowerObserver* obs); | 161 void AddPowerObserver(PowerObserver* obs); |
134 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | 162 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
135 | 163 |
136 // Remove an existing observer. | 164 // Remove an existing observer. |
137 // Can be called from any thread. | 165 // Can be called from any thread. |
138 // Must not be called from within a notification callback. | 166 // Must not be called from within a notification callback. |
139 void RemovePowerObserver(PowerObserver* obs); | 167 void RemovePowerObserver(PowerObserver* obs); |
140 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 168 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
141 | 169 |
142 #if defined(OS_WIN) | 170 #if defined(OS_WIN) |
143 // Windows-specific handling of a WM_POWERBROADCAST message. | 171 // Windows-specific handling of a WM_POWERBROADCAST message. |
144 // Embedders of this API should hook their top-level window | 172 // Embedders of this API should hook their top-level window |
145 // message loop and forward WM_POWERBROADCAST through this call. | 173 // message loop and forward WM_POWERBROADCAST through this call. |
146 void ProcessWmPowerBroadcastMessage(int event_id); | 174 void ProcessWmPowerBroadcastMessage(int event_id); |
147 #endif | 175 #endif |
148 | 176 |
149 // Cross-platform handling of a power event. | 177 // Cross-platform handling of a power event. |
150 void ProcessPowerMessage(PowerEvent event_id); | 178 void ProcessPowerMessage(PowerEvent event_id); |
151 | 179 |
152 // Cross-platform handling of a device change event. | 180 // Cross-platform handling of a device change event. |
153 void ProcessDevicesChanged(); | 181 void ProcessDevicesChanged(); |
154 void ProcessMediaDeviceAttached(const DeviceIdType& id, | 182 void ProcessMediaDeviceAttached(const std::string& id, |
155 const std::string& name, | 183 const string16& name, |
156 const FilePath& path); | 184 MediaDeviceType type, |
157 void ProcessMediaDeviceDetached(const DeviceIdType& id); | 185 const FilePath::StringType& location); |
158 | 186 void ProcessMediaDeviceDetached(const std::string& id); |
159 // Returns information for attached media devices. | |
160 std::vector<MediaDeviceInfo> GetAttachedMediaDevices() const; | |
161 | 187 |
162 private: | 188 private: |
163 typedef std::map<base::SystemMonitor::DeviceIdType, | 189 // Mapping of unique device id to device info tuple. |
164 MediaDeviceInfo> MediaDeviceMap; | 190 typedef std::map<std::string, MediaDeviceInfo> MediaDeviceMap; |
191 | |
165 #if defined(OS_MACOSX) | 192 #if defined(OS_MACOSX) |
166 void PlatformInit(); | 193 void PlatformInit(); |
167 void PlatformDestroy(); | 194 void PlatformDestroy(); |
168 #endif | 195 #endif |
169 | 196 |
170 // Platform-specific method to check whether the system is currently | 197 // Platform-specific method to check whether the system is currently |
171 // running on battery power. Returns true if running on batteries, | 198 // running on battery power. Returns true if running on batteries, |
172 // false otherwise. | 199 // false otherwise. |
173 bool IsBatteryPower(); | 200 bool IsBatteryPower(); |
174 | 201 |
175 // Checks the battery status and notifies observers if the battery | 202 // Checks the battery status and notifies observers if the battery |
176 // status has changed. | 203 // status has changed. |
177 void BatteryCheck(); | 204 void BatteryCheck(); |
178 | 205 |
179 // Functions to trigger notifications. | 206 // Functions to trigger notifications. |
180 void NotifyDevicesChanged(); | 207 void NotifyDevicesChanged(); |
181 void NotifyMediaDeviceAttached(const DeviceIdType& id, | 208 void NotifyMediaDeviceAttached(const std::string& id, |
182 const std::string& name, | 209 const string16& name, |
183 const FilePath& path); | 210 MediaDeviceType type, |
184 void NotifyMediaDeviceDetached(const DeviceIdType& id); | 211 const FilePath::StringType& data); |
212 void NotifyMediaDeviceDetached(const std::string& id); | |
185 void NotifyPowerStateChange(); | 213 void NotifyPowerStateChange(); |
186 void NotifySuspend(); | 214 void NotifySuspend(); |
187 void NotifyResume(); | 215 void NotifyResume(); |
188 | 216 |
189 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | 217 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; |
190 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 218 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
191 devices_changed_observer_list_; | 219 devices_changed_observer_list_; |
192 bool battery_in_use_; | 220 bool battery_in_use_; |
193 bool suspended_; | 221 bool suspended_; |
194 | 222 |
195 #if defined(ENABLE_BATTERY_MONITORING) | 223 #if defined(ENABLE_BATTERY_MONITORING) |
196 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | 224 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
197 #endif | 225 #endif |
198 | 226 |
199 #if defined(OS_IOS) | 227 #if defined(OS_IOS) |
200 // Holds pointers to system event notification observers. | 228 // Holds pointers to system event notification observers. |
201 std::vector<id> notification_observers_; | 229 std::vector<id> notification_observers_; |
202 #endif | 230 #endif |
203 | 231 |
232 // Map of all the attached media devices. | |
204 MediaDeviceMap media_device_map_; | 233 MediaDeviceMap media_device_map_; |
205 | 234 |
206 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 235 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
207 }; | 236 }; |
208 | 237 |
209 } // namespace base | 238 } // namespace base |
210 | 239 |
211 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 240 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
OLD | NEW |