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