| OLD | NEW |
| 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/base_export.h" | 9 #include "base/base_export.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 | 12 |
| 13 // 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 |
| 14 // status. This isn't true for other platforms. | 14 // status. This isn't true for other platforms. |
| 15 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 16 #define ENABLE_BATTERY_MONITORING 1 | 16 #define ENABLE_BATTERY_MONITORING 1 |
| 17 #else | 17 #else |
| 18 #undef ENABLE_BATTERY_MONITORING | 18 #undef ENABLE_BATTERY_MONITORING |
| 19 #endif // !OS_WIN | 19 #endif // !OS_WIN |
| 20 | 20 |
| 21 #include "base/observer_list_threadsafe.h" | 21 #include "base/observer_list_threadsafe.h" |
| 22 #if defined(ENABLE_BATTERY_MONITORING) | 22 #if defined(ENABLE_BATTERY_MONITORING) |
| 23 #include "base/timer.h" | 23 #include "base/timer.h" |
| 24 #endif // defined(ENABLE_BATTERY_MONITORING) | 24 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 25 | 25 |
| 26 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
| 27 #include <IOKit/pwr_mgt/IOPMLib.h> | 27 #include <IOKit/pwr_mgt/IOPMLib.h> |
| 28 #include <IOKit/IOMessage.h> | 28 #include <IOKit/IOMessage.h> |
| 29 #endif // OS_MACOSX | 29 #endif // OS_MACOSX |
| 30 | 30 |
| 31 class FilePath; |
| 32 |
| 31 namespace base { | 33 namespace base { |
| 32 | 34 |
| 33 // Class for monitoring various system-related subsystems | 35 // Class for monitoring various system-related subsystems |
| 34 // such as power management, network status, etc. | 36 // such as power management, network status, etc. |
| 35 // TODO(mbelshe): Add support beyond just power management. | 37 // TODO(mbelshe): Add support beyond just power management. |
| 36 class BASE_EXPORT SystemMonitor { | 38 class BASE_EXPORT SystemMonitor { |
| 37 public: | 39 public: |
| 38 // Normalized list of power events. | 40 // Normalized list of power events. |
| 39 enum PowerEvent { | 41 enum PowerEvent { |
| 40 POWER_STATE_EVENT, // The Power status of the system has changed. | 42 POWER_STATE_EVENT, // The Power status of the system has changed. |
| 41 SUSPEND_EVENT, // The system is being suspended. | 43 SUSPEND_EVENT, // The system is being suspended. |
| 42 RESUME_EVENT // The system is being resumed. | 44 RESUME_EVENT // The system is being resumed. |
| 43 }; | 45 }; |
| 44 | 46 |
| 47 typedef unsigned long DeviceIdType; |
| 48 |
| 45 // Create SystemMonitor. Only one SystemMonitor instance per application | 49 // Create SystemMonitor. Only one SystemMonitor instance per application |
| 46 // is allowed. | 50 // is allowed. |
| 47 SystemMonitor(); | 51 SystemMonitor(); |
| 48 ~SystemMonitor(); | 52 ~SystemMonitor(); |
| 49 | 53 |
| 50 // Get the application-wide SystemMonitor (if not present, returns NULL). | 54 // Get the application-wide SystemMonitor (if not present, returns NULL). |
| 51 static SystemMonitor* Get(); | 55 static SystemMonitor* Get(); |
| 52 | 56 |
| 53 #if defined(OS_MACOSX) | 57 #if defined(OS_MACOSX) |
| 54 // Allocate system resources needed by the SystemMonitor class. | 58 // Allocate system resources needed by the SystemMonitor class. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // Notification that the system is resuming. | 90 // Notification that the system is resuming. |
| 87 virtual void OnResume() {} | 91 virtual void OnResume() {} |
| 88 | 92 |
| 89 protected: | 93 protected: |
| 90 virtual ~PowerObserver() {} | 94 virtual ~PowerObserver() {} |
| 91 }; | 95 }; |
| 92 | 96 |
| 93 class BASE_EXPORT DevicesChangedObserver { | 97 class BASE_EXPORT DevicesChangedObserver { |
| 94 public: | 98 public: |
| 95 // Notification that the devices connected to the system have changed. | 99 // Notification that the devices connected to the system have changed. |
| 100 // This is only implemented on Windows currently. |
| 96 virtual void OnDevicesChanged() {} | 101 virtual void OnDevicesChanged() {} |
| 97 | 102 |
| 103 // When a media device is attached or detached, one of these two events |
| 104 // if triggered. |
| 105 // TODO(vandebo) Pass an appropriate device identifier or way to interact |
| 106 // with the devices instead of FilePath. |
| 107 virtual void OnMediaDeviceAttached(const DeviceIdType& id, |
| 108 const std::string& name, |
| 109 const FilePath& path) {} |
| 110 |
| 111 virtual void OnMediaDeviceDetached(const DeviceIdType& id) {} |
| 112 |
| 98 protected: | 113 protected: |
| 99 virtual ~DevicesChangedObserver() {} | 114 virtual ~DevicesChangedObserver() {} |
| 100 }; | 115 }; |
| 101 | 116 |
| 102 // Add a new observer. | 117 // Add a new observer. |
| 103 // Can be called from any thread. | 118 // Can be called from any thread. |
| 104 // Must not be called from within a notification callback. | 119 // Must not be called from within a notification callback. |
| 105 void AddPowerObserver(PowerObserver* obs); | 120 void AddPowerObserver(PowerObserver* obs); |
| 106 void AddDevicesChangedObserver(DevicesChangedObserver* obs); | 121 void AddDevicesChangedObserver(DevicesChangedObserver* obs); |
| 107 | 122 |
| 108 // Remove an existing observer. | 123 // Remove an existing observer. |
| 109 // Can be called from any thread. | 124 // Can be called from any thread. |
| 110 // Must not be called from within a notification callback. | 125 // Must not be called from within a notification callback. |
| 111 void RemovePowerObserver(PowerObserver* obs); | 126 void RemovePowerObserver(PowerObserver* obs); |
| 112 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); | 127 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); |
| 113 | 128 |
| 114 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
| 115 // Windows-specific handling of a WM_POWERBROADCAST message. | 130 // Windows-specific handling of a WM_POWERBROADCAST message. |
| 116 // Embedders of this API should hook their top-level window | 131 // Embedders of this API should hook their top-level window |
| 117 // message loop and forward WM_POWERBROADCAST through this call. | 132 // message loop and forward WM_POWERBROADCAST through this call. |
| 118 void ProcessWmPowerBroadcastMessage(int event_id); | 133 void ProcessWmPowerBroadcastMessage(int event_id); |
| 119 #endif | 134 #endif |
| 120 | 135 |
| 121 // Cross-platform handling of a power event. | 136 // Cross-platform handling of a power event. |
| 122 void ProcessPowerMessage(PowerEvent event_id); | 137 void ProcessPowerMessage(PowerEvent event_id); |
| 123 | 138 |
| 124 // Cross-platform handling of a device change event. | 139 // Cross-platform handling of a device change event. |
| 125 void ProcessDevicesChanged(); | 140 void ProcessDevicesChanged(); |
| 141 void ProcessMediaDeviceAttached(const DeviceIdType& id, |
| 142 const std::string& name, |
| 143 const FilePath& path); |
| 144 void ProcessMediaDeviceDetached(const DeviceIdType& id); |
| 126 | 145 |
| 127 private: | 146 private: |
| 128 #if defined(OS_MACOSX) | 147 #if defined(OS_MACOSX) |
| 129 void PlatformInit(); | 148 void PlatformInit(); |
| 130 void PlatformDestroy(); | 149 void PlatformDestroy(); |
| 131 #endif | 150 #endif |
| 132 | 151 |
| 133 // Platform-specific method to check whether the system is currently | 152 // Platform-specific method to check whether the system is currently |
| 134 // running on battery power. Returns true if running on batteries, | 153 // running on battery power. Returns true if running on batteries, |
| 135 // false otherwise. | 154 // false otherwise. |
| 136 bool IsBatteryPower(); | 155 bool IsBatteryPower(); |
| 137 | 156 |
| 138 // Checks the battery status and notifies observers if the battery | 157 // Checks the battery status and notifies observers if the battery |
| 139 // status has changed. | 158 // status has changed. |
| 140 void BatteryCheck(); | 159 void BatteryCheck(); |
| 141 | 160 |
| 142 // Functions to trigger notifications. | 161 // Functions to trigger notifications. |
| 143 void NotifyDevicesChanged(); | 162 void NotifyDevicesChanged(); |
| 163 void NotifyMediaDeviceAttached(const DeviceIdType& id, |
| 164 const std::string& name, |
| 165 const FilePath& path); |
| 166 void NotifyMediaDeviceDetached(const DeviceIdType& id); |
| 144 void NotifyPowerStateChange(); | 167 void NotifyPowerStateChange(); |
| 145 void NotifySuspend(); | 168 void NotifySuspend(); |
| 146 void NotifyResume(); | 169 void NotifyResume(); |
| 147 | 170 |
| 148 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | 171 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; |
| 149 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 172 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
| 150 devices_changed_observer_list_; | 173 devices_changed_observer_list_; |
| 151 bool battery_in_use_; | 174 bool battery_in_use_; |
| 152 bool suspended_; | 175 bool suspended_; |
| 153 | 176 |
| 154 #if defined(ENABLE_BATTERY_MONITORING) | 177 #if defined(ENABLE_BATTERY_MONITORING) |
| 155 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | 178 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
| 156 #endif | 179 #endif |
| 157 | 180 |
| 158 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 181 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
| 159 }; | 182 }; |
| 160 | 183 |
| 161 } // namespace base | 184 } // namespace base |
| 162 | 185 |
| 163 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 186 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
| OLD | NEW |