Chromium Code Reviews| 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> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/observer_list_threadsafe.h" | |
| 15 #include "base/string16.h" | 16 #include "base/string16.h" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 | 19 |
| 19 // Windows HiRes timers drain the battery faster so we need to know the battery | |
| 20 // status. This isn't true for other platforms. | |
| 21 #if defined(OS_WIN) | |
| 22 #define ENABLE_BATTERY_MONITORING 1 | |
| 23 #else | |
| 24 #undef ENABLE_BATTERY_MONITORING | |
| 25 #endif // !OS_WIN | |
| 26 | |
| 27 #include "base/observer_list_threadsafe.h" | |
| 28 #if defined(ENABLE_BATTERY_MONITORING) | |
| 29 #include "base/timer.h" | |
| 30 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 31 | |
| 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) | |
| 38 #include <objc/runtime.h> | |
| 39 #endif // OS_IOS | |
| 40 | |
| 41 namespace base { | 20 namespace base { |
| 42 | 21 |
| 43 // Class for monitoring various system-related subsystems | 22 // Class for monitoring various system-related subsystems |
| 44 // such as power management, network status, etc. | 23 // such as power management, network status, etc. |
| 45 // TODO(mbelshe): Add support beyond just power management. | 24 // TODO(mbelshe): Add support beyond just power management. |
| 46 class BASE_EXPORT SystemMonitor { | 25 class BASE_EXPORT SystemMonitor { |
| 47 public: | 26 public: |
| 48 // Normalized list of power events. | |
| 49 enum PowerEvent { | |
| 50 POWER_STATE_EVENT, // The Power status of the system has changed. | |
| 51 SUSPEND_EVENT, // The system is being suspended. | |
| 52 RESUME_EVENT // The system is being resumed. | |
| 53 }; | |
| 54 | |
| 55 // Type of devices whose change need to be monitored, such as add/remove. | 27 // Type of devices whose change need to be monitored, such as add/remove. |
| 56 enum DeviceType { | 28 enum DeviceType { |
| 57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 29 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. |
| 58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 30 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. |
| 59 DEVTYPE_UNKNOWN, // Other devices. | 31 DEVTYPE_UNKNOWN, // Other devices. |
| 60 }; | 32 }; |
| 61 | 33 |
| 62 struct BASE_EXPORT RemovableStorageInfo { | 34 struct BASE_EXPORT RemovableStorageInfo { |
| 63 RemovableStorageInfo(); | 35 RemovableStorageInfo(); |
| 64 RemovableStorageInfo(const std::string& id, | 36 RemovableStorageInfo(const std::string& id, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 76 }; | 48 }; |
| 77 | 49 |
| 78 // Create SystemMonitor. Only one SystemMonitor instance per application | 50 // Create SystemMonitor. Only one SystemMonitor instance per application |
| 79 // is allowed. | 51 // is allowed. |
| 80 SystemMonitor(); | 52 SystemMonitor(); |
| 81 ~SystemMonitor(); | 53 ~SystemMonitor(); |
| 82 | 54 |
| 83 // Get the application-wide SystemMonitor (if not present, returns NULL). | 55 // Get the application-wide SystemMonitor (if not present, returns NULL). |
| 84 static SystemMonitor* Get(); | 56 static SystemMonitor* Get(); |
| 85 | 57 |
| 86 #if defined(OS_MACOSX) | |
| 87 // Allocate system resources needed by the SystemMonitor class. | |
| 88 // | |
| 89 // This function must be called before instantiating an instance of the class | |
| 90 // and before the Sandbox is initialized. | |
| 91 #if !defined(OS_IOS) | |
| 92 static void AllocateSystemIOPorts(); | |
| 93 #else | |
| 94 static void AllocateSystemIOPorts() {} | |
| 95 #endif // OS_IOS | |
| 96 #endif // OS_MACOSX | |
| 97 | |
| 98 // Returns information for attached removable storage. | 58 // Returns information for attached removable storage. |
| 99 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; | 59 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; |
| 100 | 60 |
| 101 // | |
| 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 { | |
| 108 // Using a lock here is not necessary for just a bool. | |
| 109 return battery_in_use_; | |
| 110 } | |
| 111 | |
| 112 // Callbacks will be called on the thread which creates the SystemMonitor. | |
| 113 // During the callback, Add/RemoveObserver will block until the callbacks | |
| 114 // are finished. Observers should implement quick callback functions; if | |
| 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 | |
| 123 // Notification that the system is suspending. | |
| 124 virtual void OnSuspend() {} | |
| 125 | |
| 126 // Notification that the system is resuming. | |
| 127 virtual void OnResume() {} | |
| 128 | |
| 129 protected: | |
| 130 virtual ~PowerObserver() {} | |
| 131 }; | |
| 132 | |
| 133 class BASE_EXPORT DevicesChangedObserver { | 61 class BASE_EXPORT DevicesChangedObserver { |
| 134 public: | 62 public: |
| 135 // Notification that the devices connected to the system have changed. | 63 // Notification that the devices connected to the system have changed. |
| 136 // This is only implemented on Windows currently. | 64 // This is only implemented on Windows currently. |
| 137 virtual void OnDevicesChanged(DeviceType device_type) {} | 65 virtual void OnDevicesChanged(DeviceType device_type) {} |
| 138 | 66 |
| 139 // When a removable storage device is attached or detached, one of these | 67 // When a removable storage device is attached or detached, one of these |
| 140 // two events is triggered. | 68 // two events is triggered. |
| 141 virtual void OnRemovableStorageAttached( | 69 virtual void OnRemovableStorageAttached( |
| 142 const std::string& id, | 70 const std::string& id, |
| 143 const string16& name, | 71 const string16& name, |
| 144 const FilePath::StringType& location) {} | 72 const FilePath::StringType& location) {} |
| 145 virtual void OnRemovableStorageDetached(const std::string& id) {} | 73 virtual void OnRemovableStorageDetached(const std::string& id) {} |
| 146 | 74 |
| 147 protected: | 75 protected: |
| 148 virtual ~DevicesChangedObserver() {} | 76 virtual ~DevicesChangedObserver() {} |
| 149 }; | 77 }; |
| 150 | 78 |
| 151 // Add a new observer. | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Leave a copy of this comment in this file.
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 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); | 79 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
| 156 | 80 |
| 157 // Remove an existing observer. | 81 // Remove an existing observer. |
| 158 // Can be called from any thread. | 82 // Can be called from any thread. |
| 159 // Must not be called from within a notification callback. | 83 // Must not be called from within a notification callback. |
| 160 void RemovePowerObserver(PowerObserver* obs); | |
| 161 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 84 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
| 162 | 85 |
| 163 // The ProcessFoo() style methods are a broken pattern and should not | 86 // The ProcessFoo() style methods are a broken pattern and should not |
| 164 // be copied. Any significant addition to this class is blocked on | 87 // be copied. Any significant addition to this class is blocked on |
| 165 // refactoring to improve the state of affairs. See http://crbug.com/149059 | 88 // refactoring to improve the state of affairs. See http://crbug.com/149059 |
| 166 | 89 |
| 167 #if defined(OS_WIN) | |
| 168 // Windows-specific handling of a WM_POWERBROADCAST message. | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Move this entire comment over.
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 169 // Embedders of this API should hook their top-level window | |
| 170 // message loop and forward WM_POWERBROADCAST through this call. | |
| 171 void ProcessWmPowerBroadcastMessage(int event_id); | |
| 172 #endif | |
| 173 | |
| 174 // Cross-platform handling of a power event. | |
| 175 void ProcessPowerMessage(PowerEvent event_id); | |
| 176 | |
| 177 // Cross-platform handling of a device change event. | 90 // Cross-platform handling of a device change event. |
| 178 void ProcessDevicesChanged(DeviceType device_type); | 91 void ProcessDevicesChanged(DeviceType device_type); |
| 179 void ProcessRemovableStorageAttached(const std::string& id, | 92 void ProcessRemovableStorageAttached(const std::string& id, |
| 180 const string16& name, | 93 const string16& name, |
| 181 const FilePath::StringType& location); | 94 const FilePath::StringType& location); |
| 182 void ProcessRemovableStorageDetached(const std::string& id); | 95 void ProcessRemovableStorageDetached(const std::string& id); |
| 183 | 96 |
| 184 private: | 97 private: |
| 185 // Mapping of unique device id to device info tuple. | 98 // Mapping of unique device id to device info tuple. |
| 186 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; | 99 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; |
| 187 | 100 |
| 188 #if defined(OS_MACOSX) | |
| 189 void PlatformInit(); | |
| 190 void PlatformDestroy(); | |
| 191 #endif | |
| 192 | |
| 193 // Platform-specific method to check whether the system is currently | |
| 194 // running on battery power. Returns true if running on batteries, | |
| 195 // false otherwise. | |
| 196 bool IsBatteryPower(); | |
| 197 | |
| 198 // Checks the battery status and notifies observers if the battery | |
| 199 // status has changed. | |
| 200 void BatteryCheck(); | |
| 201 | |
| 202 // Functions to trigger notifications. | 101 // Functions to trigger notifications. |
| 203 void NotifyDevicesChanged(DeviceType device_type); | 102 void NotifyDevicesChanged(DeviceType device_type); |
| 204 void NotifyRemovableStorageAttached(const std::string& id, | 103 void NotifyRemovableStorageAttached(const std::string& id, |
| 205 const string16& name, | 104 const string16& name, |
| 206 const FilePath::StringType& location); | 105 const FilePath::StringType& location); |
| 207 void NotifyRemovableStorageDetached(const std::string& id); | 106 void NotifyRemovableStorageDetached(const std::string& id); |
| 208 void NotifyPowerStateChange(); | |
| 209 void NotifySuspend(); | |
| 210 void NotifyResume(); | |
| 211 | |
| 212 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | |
| 213 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 107 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
| 214 devices_changed_observer_list_; | 108 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) | |
| 223 // Holds pointers to system event notification observers. | |
| 224 std::vector<id> notification_observers_; | |
| 225 #endif | |
| 226 | 109 |
| 227 // For manipulating removable_storage_map_ structure. | 110 // For manipulating removable_storage_map_ structure. |
| 228 mutable base::Lock removable_storage_lock_; | 111 mutable base::Lock removable_storage_lock_; |
| 229 // Map of all the attached removable storage devices. | 112 // Map of all the attached removable storage devices. |
| 230 RemovableStorageMap removable_storage_map_; | 113 RemovableStorageMap removable_storage_map_; |
| 231 | 114 |
| 232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 115 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
| 233 }; | 116 }; |
| 234 | 117 |
| 235 } // namespace base | 118 } // namespace base |
| 236 | 119 |
| 237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 120 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| OLD | NEW |