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_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/observer_list_threadsafe.h" |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "build/build_config.h" | |
| 18 | 12 |
| 19 // Windows HiRes timers drain the battery faster so we need to know the battery | 13 // Windows HiRes timers drain the battery faster so we need to know the battery |
| 20 // status. This isn't true for other platforms. | 14 // status. This isn't true for other platforms. |
| 21 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 22 #define ENABLE_BATTERY_MONITORING 1 | 16 #define ENABLE_BATTERY_MONITORING 1 |
| 23 #else | 17 #else |
| 24 #undef ENABLE_BATTERY_MONITORING | 18 #undef ENABLE_BATTERY_MONITORING |
| 25 #endif // !OS_WIN | 19 #endif // !OS_WIN |
| 26 | 20 |
| 27 #include "base/observer_list_threadsafe.h" | |
| 28 #if defined(ENABLE_BATTERY_MONITORING) | 21 #if defined(ENABLE_BATTERY_MONITORING) |
| 29 #include "base/timer.h" | 22 #include "base/timer.h" |
| 30 #endif // defined(ENABLE_BATTERY_MONITORING) | 23 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 31 | 24 |
| 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) | 25 #if defined(OS_IOS) |
| 38 #include <objc/runtime.h> | 26 #include <objc/runtime.h> |
| 39 #endif // OS_IOS | 27 #endif // OS_IOS |
| 40 | 28 |
| 41 namespace base { | 29 namespace base { |
| 42 | 30 |
| 43 // Class for monitoring various system-related subsystems | 31 // Callbacks will be called on the thread which creates the PowerMonitor. |
| 44 // such as power management, network status, etc. | 32 // During the callback, Add/RemoveObserver will block until the callbacks |
| 45 // TODO(mbelshe): Add support beyond just power management. | 33 // are finished. Observers should implement quick callback functions; if |
| 46 class BASE_EXPORT SystemMonitor { | 34 // lengthy operations are needed, the observer should take care to invoke |
| 35 // the operation on an appropriate thread. | |
| 36 class BASE_EXPORT PowerObserver { | |
|
jam
2012/10/11 15:38:19
nit: put this interface in its own header
Hongbo Min
2012/10/12 14:43:53
Done.
| |
| 37 public: | |
| 38 // Notification of a change in power status of the computer, such | |
| 39 // as from switching between battery and A/C power. | |
| 40 virtual void OnPowerStateChange(bool on_battery_power) {} | |
| 41 | |
| 42 // Notification that the system is suspending. | |
| 43 virtual void OnSuspend() {} | |
| 44 | |
| 45 // Notification that the system is resuming. | |
| 46 virtual void OnResume() {} | |
| 47 | |
| 48 protected: | |
| 49 virtual ~PowerObserver() {} | |
| 50 }; | |
| 51 | |
| 52 class BASE_EXPORT PowerMonitor { | |
| 47 public: | 53 public: |
| 48 // Normalized list of power events. | 54 // Normalized list of power events. |
| 49 enum PowerEvent { | 55 enum PowerEvent { |
| 50 POWER_STATE_EVENT, // The Power status of the system has changed. | 56 POWER_STATE_EVENT, // The Power status of the system has changed. |
| 51 SUSPEND_EVENT, // The system is being suspended. | 57 SUSPEND_EVENT, // The system is being suspended. |
| 52 RESUME_EVENT // The system is being resumed. | 58 RESUME_EVENT // The system is being resumed. |
| 53 }; | 59 }; |
| 54 | 60 |
| 55 // Type of devices whose change need to be monitored, such as add/remove. | 61 // Add and remove an observer. |
| 56 enum DeviceType { | 62 // Can be called from any thread. |
| 57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 63 // Must not be called from within a notification callback. |
| 58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 64 void AddObserver(PowerObserver* observer); |
|
vandebo (ex-Chrome)
2012/10/11 20:49:57
Move these two methods down to line 87. The const
Hongbo Min
2012/10/12 14:43:53
Done.
| |
| 59 DEVTYPE_UNKNOWN, // Other devices. | 65 void RemoveObserver(PowerObserver* observer); |
| 60 }; | |
| 61 | 66 |
| 62 struct BASE_EXPORT RemovableStorageInfo { | 67 // Is the computer currently on battery power. Can be called on any thread. |
| 63 RemovableStorageInfo(); | 68 bool BatteryPower() const { |
| 64 RemovableStorageInfo(const std::string& id, | 69 // Using a lock here is not necessary for just a bool. |
| 65 const string16& device_name, | 70 return battery_in_use_; |
| 66 const FilePath::StringType& device_location); | 71 } |
| 67 | 72 |
| 68 // Unique device id - persists between device attachments. | 73 // Return the singleton instance in the application wide. |
| 69 std::string device_id; | 74 static PowerMonitor* GetInstance(); |
| 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 | 75 |
| 86 #if defined(OS_MACOSX) | 76 #if defined(OS_MACOSX) |
| 87 // Allocate system resources needed by the SystemMonitor class. | 77 // Allocate system resources needed by the PowerMonitor class. |
| 88 // | 78 // |
| 89 // This function must be called before instantiating an instance of the class | 79 // This function must be called before instantiating an instance of the class |
| 90 // and before the Sandbox is initialized. | 80 // and before the Sandbox is initialized. |
| 91 #if !defined(OS_IOS) | 81 #if !defined(OS_IOS) |
| 92 static void AllocateSystemIOPorts(); | 82 static void AllocateSystemIOPorts(); |
| 93 #else | 83 #else |
| 94 static void AllocateSystemIOPorts() {} | 84 static void AllocateSystemIOPorts() {} |
| 95 #endif // OS_IOS | 85 #endif // OS_IOS |
| 96 #endif // OS_MACOSX | 86 #endif // OS_MACOSX |
| 97 | 87 |
| 98 // Returns information for attached removable storage. | |
| 99 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; | |
| 100 | |
| 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 { | |
| 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 | |
| 167 #if defined(OS_WIN) | 88 #if defined(OS_WIN) |
| 168 // Windows-specific handling of a WM_POWERBROADCAST message. | 89 // Windows-specific handling of a WM_POWERBROADCAST message. |
|
vandebo (ex-Chrome)
2012/10/11 20:49:57
These two methods need to be private.
Hongbo Min
2012/10/12 14:43:53
Done.
| |
| 169 // Embedders of this API should hook their top-level window | 90 // Embedders of this API should hook their top-level window |
| 170 // message loop and forward WM_POWERBROADCAST through this call. | 91 // message loop and forward WM_POWERBROADCAST through this call. |
| 171 void ProcessWmPowerBroadcastMessage(int event_id); | 92 void ProcessWmPowerBroadcastMessage(int event_id); |
| 172 #endif | 93 #endif |
| 173 | 94 |
| 174 // Cross-platform handling of a power event. | 95 // Cross-platform handling of a power event. |
| 175 void ProcessPowerMessage(PowerEvent event_id); | 96 void ProcessPowerMessage(PowerEvent event_id); |
| 176 | 97 |
| 177 // Cross-platform handling of a device change event. | 98 private: |
| 178 void ProcessDevicesChanged(DeviceType device_type); | 99 PowerMonitor(); |
| 179 void ProcessRemovableStorageAttached(const std::string& id, | 100 ~PowerMonitor(); |
| 180 const string16& name, | |
| 181 const FilePath::StringType& location); | |
| 182 void ProcessRemovableStorageDetached(const std::string& id); | |
| 183 | 101 |
| 184 private: | 102 friend struct DefaultSingletonTraits<PowerMonitor>; |
| 185 // Mapping of unique device id to device info tuple. | 103 friend class PowerMonitorTest; |
| 186 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap; | 104 friend class HiResTimerManagerTest; |
|
vandebo (ex-Chrome)
2012/10/11 20:49:57
Do you need this friend? It looks like the test c
Hongbo Min
2012/10/12 14:43:53
Removed it.
Done.
| |
| 187 | 105 |
| 188 #if defined(OS_MACOSX) | 106 #if defined(OS_MACOSX) |
| 189 void PlatformInit(); | 107 void PlatformInit(); |
| 190 void PlatformDestroy(); | 108 void PlatformDestroy(); |
| 191 #endif | 109 #endif |
| 192 | 110 |
| 193 // Platform-specific method to check whether the system is currently | 111 // Platform-specific method to check whether the system is currently |
| 194 // running on battery power. Returns true if running on batteries, | 112 // running on battery power. Returns true if running on batteries, |
| 195 // false otherwise. | 113 // false otherwise. |
| 196 bool IsBatteryPower(); | 114 bool IsBatteryPower(); |
| 197 | 115 |
| 198 // Checks the battery status and notifies observers if the battery | 116 // Checks the battery status and notifies observers if the battery |
| 199 // status has changed. | 117 // status has changed. |
| 200 void BatteryCheck(); | 118 void BatteryCheck(); |
| 201 | 119 |
| 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(); | 120 void NotifyPowerStateChange(); |
| 209 void NotifySuspend(); | 121 void NotifySuspend(); |
| 210 void NotifyResume(); | 122 void NotifyResume(); |
| 211 | 123 |
| 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) | 124 #if defined(OS_IOS) |
| 223 // Holds pointers to system event notification observers. | 125 // Holds pointers to system event notification observers. |
| 224 std::vector<id> notification_observers_; | 126 std::vector<id> notification_observers_; |
| 225 #endif | 127 #endif |
| 226 | 128 |
| 227 // For manipulating removable_storage_map_ structure. | 129 #if defined(ENABLE_BATTERY_MONITORING) |
| 228 mutable base::Lock removable_storage_lock_; | 130 base::OneShotTimer<PowerMonitor> delayed_battery_check_; |
| 229 // Map of all the attached removable storage devices. | 131 #endif |
| 230 RemovableStorageMap removable_storage_map_; | 132 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; |
|
vandebo (ex-Chrome)
2012/10/11 20:49:57
This is created in the constructor and never destr
Hongbo Min
2012/10/12 14:43:53
It should be destroyed on process exiting, the sam
vandebo (ex-Chrome)
2012/10/12 22:12:30
Yes, making it a member will make it have the same
| |
| 133 bool battery_in_use_; | |
| 134 bool suspended_; | |
| 231 | 135 |
| 232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 136 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); |
| 233 }; | 137 }; |
| 234 | 138 |
| 235 } // namespace base | 139 } // namespace base |
| 236 | 140 |
| 237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 141 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ |
| OLD | NEW |