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> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/observer_list_threadsafe.h" | |
| 14 #include "build/build_config.h" | 11 #include "build/build_config.h" |
|
vandebo (ex-Chrome)
2013/03/19 23:36:04
Also add base/memory/ref_counted.h for the scoped_
Hongbo Min
2013/03/20 13:01:50
Added, but have the same concern about scoped_refp
| |
| 15 | 12 |
| 16 // Windows HiRes timers drain the battery faster so we need to know the battery | |
| 17 // status. This isn't true for other platforms. | |
| 18 #if defined(OS_WIN) | |
| 19 #define ENABLE_BATTERY_MONITORING 1 | |
| 20 #else | |
| 21 #undef ENABLE_BATTERY_MONITORING | |
| 22 #endif // !OS_WIN | |
| 23 | |
| 24 #include "base/observer_list_threadsafe.h" | |
| 25 #if defined(ENABLE_BATTERY_MONITORING) | |
| 26 #include "base/timer.h" | |
| 27 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 28 | |
| 29 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 30 #include <IOKit/pwr_mgt/IOPMLib.h> | |
| 31 #include <IOKit/IOMessage.h> | |
| 32 #endif // OS_MACOSX && !OS_IOS | |
| 33 | |
| 34 #if defined(OS_IOS) | |
| 35 #include <objc/runtime.h> | |
| 36 #endif // OS_IOS | |
| 37 | |
| 38 namespace base { | 13 namespace base { |
| 39 | 14 |
| 40 // Class for monitoring various system-related subsystems | 15 // Class for monitoring various system-related subsystems |
| 41 // such as power management, network status, etc. | 16 // such as power management, network status, etc. |
| 42 // TODO(mbelshe): Add support beyond just power management. | 17 // TODO(mbelshe): Add support beyond just power management. |
| 43 class BASE_EXPORT SystemMonitor { | 18 class BASE_EXPORT SystemMonitor { |
| 44 public: | 19 public: |
| 45 // Normalized list of power events. | |
| 46 enum PowerEvent { | |
| 47 POWER_STATE_EVENT, // The Power status of the system has changed. | |
| 48 SUSPEND_EVENT, // The system is being suspended. | |
| 49 RESUME_EVENT // The system is being resumed. | |
| 50 }; | |
| 51 | |
| 52 // Type of devices whose change need to be monitored, such as add/remove. | 20 // Type of devices whose change need to be monitored, such as add/remove. |
| 53 enum DeviceType { | 21 enum DeviceType { |
| 54 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. | 22 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. |
| 55 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. | 23 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. |
| 56 DEVTYPE_UNKNOWN, // Other devices. | 24 DEVTYPE_UNKNOWN, // Other devices. |
| 57 }; | 25 }; |
| 58 | 26 |
| 59 // Create SystemMonitor. Only one SystemMonitor instance per application | 27 // Create SystemMonitor. Only one SystemMonitor instance per application |
| 60 // is allowed. | 28 // is allowed. |
| 61 SystemMonitor(); | 29 SystemMonitor(); |
| 62 ~SystemMonitor(); | 30 ~SystemMonitor(); |
| 63 | 31 |
| 64 // Get the application-wide SystemMonitor (if not present, returns NULL). | 32 // Get the application-wide SystemMonitor (if not present, returns NULL). |
| 65 static SystemMonitor* Get(); | 33 static SystemMonitor* Get(); |
| 66 | 34 |
| 67 #if defined(OS_MACOSX) | |
| 68 // Allocate system resources needed by the SystemMonitor class. | |
| 69 // | |
| 70 // This function must be called before instantiating an instance of the class | |
| 71 // and before the Sandbox is initialized. | |
| 72 #if !defined(OS_IOS) | |
| 73 static void AllocateSystemIOPorts(); | |
| 74 #else | |
| 75 static void AllocateSystemIOPorts() {} | |
| 76 #endif // OS_IOS | |
| 77 #endif // OS_MACOSX | |
| 78 | |
| 79 // | |
| 80 // Power-related APIs | |
| 81 // | |
| 82 | |
| 83 // Is the computer currently on battery power. | |
| 84 // Can be called on any thread. | |
| 85 bool BatteryPower() const { | |
| 86 // Using a lock here is not necessary for just a bool. | |
| 87 return battery_in_use_; | |
| 88 } | |
| 89 | |
| 90 // Callbacks will be called on the thread which creates the SystemMonitor. | |
| 91 // During the callback, Add/RemoveObserver will block until the callbacks | |
| 92 // are finished. Observers should implement quick callback functions; if | |
| 93 // lengthy operations are needed, the observer should take care to invoke | |
| 94 // the operation on an appropriate thread. | |
| 95 class BASE_EXPORT PowerObserver { | |
| 96 public: | |
| 97 // Notification of a change in power status of the computer, such | |
| 98 // as from switching between battery and A/C power. | |
| 99 virtual void OnPowerStateChange(bool on_battery_power) {} | |
| 100 | |
| 101 // Notification that the system is suspending. | |
| 102 virtual void OnSuspend() {} | |
| 103 | |
| 104 // Notification that the system is resuming. | |
| 105 virtual void OnResume() {} | |
| 106 | |
| 107 protected: | |
| 108 virtual ~PowerObserver() {} | |
| 109 }; | |
| 110 | |
| 111 class BASE_EXPORT DevicesChangedObserver { | 35 class BASE_EXPORT DevicesChangedObserver { |
| 112 public: | 36 public: |
| 113 // Notification that the devices connected to the system have changed. | 37 // Notification that the devices connected to the system have changed. |
| 114 // This is only implemented on Windows currently. | 38 // This is only implemented on Windows currently. |
| 115 virtual void OnDevicesChanged(DeviceType device_type) {} | 39 virtual void OnDevicesChanged(DeviceType device_type) {} |
| 116 | 40 |
| 117 protected: | 41 protected: |
| 118 virtual ~DevicesChangedObserver() {} | 42 virtual ~DevicesChangedObserver() {} |
| 119 }; | 43 }; |
| 120 | 44 |
| 121 // Add a new observer. | 45 // Add a new observer. |
| 122 // Can be called from any thread. | 46 // Can be called from any thread. |
| 123 // Must not be called from within a notification callback. | 47 // Must not be called from within a notification callback. |
| 124 void AddPowerObserver(PowerObserver* obs); | |
| 125 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | 48 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
| 126 | 49 |
| 127 // Remove an existing observer. | 50 // Remove an existing observer. |
| 128 // Can be called from any thread. | 51 // Can be called from any thread. |
| 129 // Must not be called from within a notification callback. | 52 // Must not be called from within a notification callback. |
| 130 void RemovePowerObserver(PowerObserver* obs); | |
| 131 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 53 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
| 132 | 54 |
| 133 // The ProcessFoo() style methods are a broken pattern and should not | 55 // The ProcessFoo() style methods are a broken pattern and should not |
| 134 // be copied. Any significant addition to this class is blocked on | 56 // be copied. Any significant addition to this class is blocked on |
| 135 // refactoring to improve the state of affairs. See http://crbug.com/149059 | 57 // refactoring to improve the state of affairs. See http://crbug.com/149059 |
| 136 | 58 |
| 137 // Cross-platform handling of a power event. | |
| 138 void ProcessPowerMessage(PowerEvent event_id); | |
| 139 | |
| 140 // Cross-platform handling of a device change event. | 59 // Cross-platform handling of a device change event. |
| 141 void ProcessDevicesChanged(DeviceType device_type); | 60 void ProcessDevicesChanged(DeviceType device_type); |
| 142 | 61 |
| 143 private: | 62 private: |
| 144 #if defined(OS_WIN) | |
| 145 // Represents a message-only window for power message handling on Windows. | |
| 146 // Only allow SystemMonitor to create it. | |
| 147 class PowerMessageWindow { | |
| 148 public: | |
| 149 PowerMessageWindow(); | |
| 150 ~PowerMessageWindow(); | |
| 151 | |
| 152 private: | |
| 153 void ProcessWmPowerBroadcastMessage(int event_id); | |
| 154 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, | |
| 155 WPARAM wparam, LPARAM lparam); | |
| 156 static LRESULT CALLBACK WndProcThunk(HWND hwnd, | |
| 157 UINT message, | |
| 158 WPARAM wparam, | |
| 159 LPARAM lparam); | |
| 160 // Instance of the module containing the window procedure. | |
| 161 HMODULE instance_; | |
| 162 // A hidden message-only window. | |
| 163 HWND message_hwnd_; | |
| 164 }; | |
| 165 #endif | |
| 166 | |
| 167 #if defined(OS_MACOSX) | |
| 168 void PlatformInit(); | |
| 169 void PlatformDestroy(); | |
| 170 #endif | |
| 171 | |
| 172 // Platform-specific method to check whether the system is currently | |
| 173 // running on battery power. Returns true if running on batteries, | |
| 174 // false otherwise. | |
| 175 bool IsBatteryPower(); | |
| 176 | |
| 177 // Checks the battery status and notifies observers if the battery | |
| 178 // status has changed. | |
| 179 void BatteryCheck(); | |
| 180 | |
| 181 // Functions to trigger notifications. | 63 // Functions to trigger notifications. |
| 182 void NotifyDevicesChanged(DeviceType device_type); | 64 void NotifyDevicesChanged(DeviceType device_type); |
| 183 void NotifyPowerStateChange(); | |
| 184 void NotifySuspend(); | |
| 185 void NotifyResume(); | |
| 186 | 65 |
| 187 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | |
| 188 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 66 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
| 189 devices_changed_observer_list_; | 67 devices_changed_observer_list_; |
| 190 bool battery_in_use_; | |
| 191 bool suspended_; | |
| 192 | |
| 193 #if defined(ENABLE_BATTERY_MONITORING) | |
| 194 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | |
| 195 #endif | |
| 196 | |
| 197 #if defined(OS_IOS) | |
| 198 // Holds pointers to system event notification observers. | |
| 199 std::vector<id> notification_observers_; | |
| 200 #endif | |
| 201 | |
| 202 #if defined(OS_WIN) | |
| 203 PowerMessageWindow power_message_window_; | |
| 204 #endif | |
| 205 | 68 |
| 206 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 69 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
| 207 }; | 70 }; |
| 208 | 71 |
| 209 } // namespace base | 72 } // namespace base |
| 210 | 73 |
| 211 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 74 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| OLD | NEW |