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