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/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 |
| 41 namespace base { | 30 namespace base { |
| 42 | 31 |
| 43 // Class for monitoring various system-related subsystems | 32 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: | 33 public: |
| 48 // Normalized list of power events. | 34 // Normalized list of power events. |
| 49 enum PowerEvent { | 35 enum PowerEvent { |
| 50 POWER_STATE_EVENT, // The Power status of the system has changed. | 36 POWER_STATE_EVENT, // The Power status of the system has changed. |
| 51 SUSPEND_EVENT, // The system is being suspended. | 37 SUSPEND_EVENT, // The system is being suspended. |
| 52 RESUME_EVENT // The system is being resumed. | 38 RESUME_EVENT // The system is being resumed. |
| 53 }; | 39 }; |
| 54 | 40 |
| 55 // Type of devices whose change need to be monitored, such as add/remove. | 41 // A helper class to wrap the process of power event in PowerMonitor. The |
| 56 enum DeviceType { | 42 // power event source is allowed to notify the PowerMonitor's observers via |
| 57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 43 // ProcessPowerEvent methods of Notifier instance. |
| 58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 44 // |
| 59 DEVTYPE_UNKNOWN, // Other devices. | 45 // The reason why not making ProcessPowerEvent as the public method of |
| 46 // PowerMonitor instead is to avoid layering violation for the friendship | |
| 47 // declaration of PowerMonitor, e.g. making views::HWNDMessageHandler as a | |
| 48 // friend of base::PowerMonitor violates the layering rule. | |
| 49 class BASE_EXPORT Notifier { | |
|
vandebo (ex-Chrome)
2012/10/23 01:27:57
Notifier doesn't feel like a very good name, but I
| |
| 50 public: | |
| 51 #if defined(OS_WIN) | |
| 52 void ProcessWmPowerBroadcastMessage(int event) { | |
| 53 monitor_->ProcessWmPowerBroadcastMessage(event); | |
| 54 } | |
| 55 #endif | |
| 56 void ProcessPowerEvent(PowerMonitor::PowerEvent event) { | |
| 57 monitor_->ProcessPowerEvent(event); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 friend class PowerMonitor; | |
| 62 Notifier(PowerMonitor* monitor) : monitor_(monitor) {} | |
|
vandebo (ex-Chrome)
2012/10/23 01:27:57
Add a comment about why the constructor is private
| |
| 63 ~Notifier() {} | |
| 64 | |
| 65 PowerMonitor* monitor_; | |
|
vandebo (ex-Chrome)
2012/10/23 01:27:57
Actually, this don't need to store the PowerMonito
| |
| 60 }; | 66 }; |
| 61 | 67 |
| 62 struct BASE_EXPORT RemovableStorageInfo { | 68 // Return the singleton instance in the application wide. |
| 63 RemovableStorageInfo(); | 69 static PowerMonitor* GetInstance(); |
| 64 RemovableStorageInfo(const std::string& id, | |
| 65 const string16& device_name, | |
| 66 const FilePath::StringType& device_location); | |
| 67 | 70 |
| 68 // Unique device id - persists between device attachments. | 71 // Return the |notifier_| pointer at the first time it gets called, otherwise |
| 69 std::string device_id; | 72 // NULL is returned. It allows the power event source to have a chance to |
| 70 | 73 // store the |notifier_| pointer of PowerMonitor for use in future. |
| 71 // Human readable removable storage device name. | 74 // |
| 72 string16 name; | 75 // Note that we assume there is at most only one power event source. |
| 73 | 76 PowerMonitor::Notifier* GetNotifierOnce(); |
| 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 | 77 |
| 86 #if defined(OS_MACOSX) | 78 #if defined(OS_MACOSX) |
| 87 // Allocate system resources needed by the SystemMonitor class. | 79 // Allocate system resources needed by the PowerMonitor class. |
| 88 // | 80 // |
| 89 // This function must be called before instantiating an instance of the class | 81 // This function must be called before instantiating an instance of the class |
| 90 // and before the Sandbox is initialized. | 82 // and before the Sandbox is initialized. |
| 91 #if !defined(OS_IOS) | 83 #if !defined(OS_IOS) |
| 92 static void AllocateSystemIOPorts(); | 84 static void AllocateSystemIOPorts(); |
| 93 #else | 85 #else |
| 94 static void AllocateSystemIOPorts() {} | 86 static void AllocateSystemIOPorts() {} |
| 95 #endif // OS_IOS | 87 #endif // OS_IOS |
| 96 #endif // OS_MACOSX | 88 #endif // OS_MACOSX |
| 97 | 89 |
| 98 // Returns information for attached removable storage. | 90 // Add and remove an observer. |
| 99 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; | 91 // Can be called from any thread. |
| 92 // Must not be called from within a notification callback. | |
| 93 void AddObserver(PowerObserver* observer); | |
| 94 void RemoveObserver(PowerObserver* observer); | |
| 100 | 95 |
| 101 // | 96 // 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 { | 97 bool BatteryPower() const { |
| 108 // Using a lock here is not necessary for just a bool. | 98 // Using a lock here is not necessary for just a bool. |
| 109 return battery_in_use_; | 99 return battery_in_use_; |
| 110 } | 100 } |
| 111 | 101 |
| 112 // Callbacks will be called on the thread which creates the SystemMonitor. | 102 private: |
| 113 // During the callback, Add/RemoveObserver will block until the callbacks | 103 friend struct DefaultSingletonTraits<PowerMonitor>; |
| 114 // are finished. Observers should implement quick callback functions; if | 104 friend class PowerMonitorTest; |
| 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 | 105 |
| 123 // Notification that the system is suspending. | 106 PowerMonitor(); |
| 124 virtual void OnSuspend() {} | 107 ~PowerMonitor(); |
| 125 | 108 |
| 126 // Notification that the system is resuming. | 109 #if defined(OS_MACOSX) |
| 127 virtual void OnResume() {} | 110 void PlatformInit(); |
| 128 | 111 void PlatformDestroy(); |
| 129 protected: | 112 #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 | 113 |
| 167 #if defined(OS_WIN) | 114 #if defined(OS_WIN) |
| 168 // Windows-specific handling of a WM_POWERBROADCAST message. | 115 // Windows-specific handling of a WM_POWERBROADCAST message. |
| 169 // Embedders of this API should hook their top-level window | 116 // Embedders of this API should hook their top-level window |
| 170 // message loop and forward WM_POWERBROADCAST through this call. | 117 // message loop and forward WM_POWERBROADCAST through this call. |
| 171 void ProcessWmPowerBroadcastMessage(int event_id); | 118 void ProcessWmPowerBroadcastMessage(int event_id); |
| 172 #endif | 119 #endif |
| 173 | 120 |
| 174 // Cross-platform handling of a power event. | 121 // Cross-platform handling of a power event. |
| 175 void ProcessPowerMessage(PowerEvent event_id); | 122 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 | 123 |
| 193 // Platform-specific method to check whether the system is currently | 124 // Platform-specific method to check whether the system is currently |
| 194 // running on battery power. Returns true if running on batteries, | 125 // running on battery power. Returns true if running on batteries, |
| 195 // false otherwise. | 126 // false otherwise. |
| 196 bool IsBatteryPower(); | 127 bool IsBatteryPower(); |
| 197 | 128 |
| 198 // Checks the battery status and notifies observers if the battery | 129 // Checks the battery status and notifies observers if the battery |
| 199 // status has changed. | 130 // status has changed. |
| 200 void BatteryCheck(); | 131 void BatteryCheck(); |
| 201 | 132 |
| 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(); | 133 void NotifyPowerStateChange(); |
| 209 void NotifySuspend(); | 134 void NotifySuspend(); |
| 210 void NotifyResume(); | 135 void NotifyResume(); |
| 211 | 136 |
| 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) | 137 #if defined(OS_IOS) |
| 223 // Holds pointers to system event notification observers. | 138 // Holds pointers to system event notification observers. |
| 224 std::vector<id> notification_observers_; | 139 std::vector<id> notification_observers_; |
| 225 #endif | 140 #endif |
| 226 | 141 |
| 227 // For manipulating removable_storage_map_ structure. | 142 #if defined(ENABLE_BATTERY_MONITORING) |
| 228 mutable base::Lock removable_storage_lock_; | 143 base::OneShotTimer<PowerMonitor> delayed_battery_check_; |
| 229 // Map of all the attached removable storage devices. | 144 #endif |
| 230 RemovableStorageMap removable_storage_map_; | |
| 231 | 145 |
| 232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 146 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; |
| 147 bool battery_in_use_; | |
| 148 bool suspended_; | |
| 149 | |
| 150 // The Notifier instance owned by PowerMonitor, but might be referenced by | |
| 151 // other object and used in the whole life time of the program. | |
| 152 PowerMonitor::Notifier* notifier_; | |
|
vandebo (ex-Chrome)
2012/10/23 01:27:57
I think the caller that gets the notifier owns it.
| |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | |
| 233 }; | 155 }; |
| 234 | 156 |
| 235 } // namespace base | 157 } // namespace base |
| 236 | 158 |
| 237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 159 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ |
| OLD | NEW |