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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | |
10 #include <string> | 9 #include <string> |
11 #include <utility> | |
12 | 10 |
13 #include "base/base_export.h" | 11 #include "base/base_export.h" |
14 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
15 #include "base/threading/thread_checker.h" | |
16 #include "build/build_config.h" | 13 #include "build/build_config.h" |
17 | 14 |
18 // 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 |
19 // status. This isn't true for other platforms. | 16 // status. This isn't true for other platforms. |
20 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
21 #define ENABLE_BATTERY_MONITORING 1 | 18 #define ENABLE_BATTERY_MONITORING 1 |
22 #else | 19 #else |
23 #undef ENABLE_BATTERY_MONITORING | 20 #undef ENABLE_BATTERY_MONITORING |
24 #endif // !OS_WIN | 21 #endif // !OS_WIN |
25 | 22 |
(...skipping 16 matching lines...) Expand all Loading... |
42 // TODO(mbelshe): Add support beyond just power management. | 39 // TODO(mbelshe): Add support beyond just power management. |
43 class BASE_EXPORT SystemMonitor { | 40 class BASE_EXPORT SystemMonitor { |
44 public: | 41 public: |
45 // Normalized list of power events. | 42 // Normalized list of power events. |
46 enum PowerEvent { | 43 enum PowerEvent { |
47 POWER_STATE_EVENT, // The Power status of the system has changed. | 44 POWER_STATE_EVENT, // The Power status of the system has changed. |
48 SUSPEND_EVENT, // The system is being suspended. | 45 SUSPEND_EVENT, // The system is being suspended. |
49 RESUME_EVENT // The system is being resumed. | 46 RESUME_EVENT // The system is being resumed. |
50 }; | 47 }; |
51 | 48 |
52 enum PowerRequirement { | |
53 DISPLAY_REQUIRED, // The display should not be shut down. | |
54 SYSTEM_REQUIRED, // The system should not be suspended. | |
55 CPU_REQUIRED, // The process should not be suspended. | |
56 TEST_REQUIRED // This is used by unit tests. | |
57 }; | |
58 | |
59 typedef unsigned int DeviceIdType; | 49 typedef unsigned int DeviceIdType; |
60 | 50 |
61 // Create SystemMonitor. Only one SystemMonitor instance per application | 51 // Create SystemMonitor. Only one SystemMonitor instance per application |
62 // is allowed. | 52 // is allowed. |
63 SystemMonitor(); | 53 SystemMonitor(); |
64 ~SystemMonitor(); | 54 ~SystemMonitor(); |
65 | 55 |
66 // Get the application-wide SystemMonitor (if not present, returns NULL). | 56 // Get the application-wide SystemMonitor (if not present, returns NULL). |
67 static SystemMonitor* Get(); | 57 static SystemMonitor* Get(); |
68 | 58 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // Cross-platform handling of a power event. | 138 // Cross-platform handling of a power event. |
149 void ProcessPowerMessage(PowerEvent event_id); | 139 void ProcessPowerMessage(PowerEvent event_id); |
150 | 140 |
151 // Cross-platform handling of a device change event. | 141 // Cross-platform handling of a device change event. |
152 void ProcessDevicesChanged(); | 142 void ProcessDevicesChanged(); |
153 void ProcessMediaDeviceAttached(const DeviceIdType& id, | 143 void ProcessMediaDeviceAttached(const DeviceIdType& id, |
154 const std::string& name, | 144 const std::string& name, |
155 const FilePath& path); | 145 const FilePath& path); |
156 void ProcessMediaDeviceDetached(const DeviceIdType& id); | 146 void ProcessMediaDeviceDetached(const DeviceIdType& id); |
157 | 147 |
158 // Enters or leaves a period of time with a given |requirement|. If the | |
159 // operation has multiple requirements, make sure to use a unique |reason| for | |
160 // each one. Reusing the same |reason| is OK as far as the |requirement| is | |
161 // the same in every call, and each BeginPowerRequirement call is paired with | |
162 // a call to EndPowerRequirement. |reason| is expected to be an ASCII string. | |
163 // Must be called from the thread that created the SystemMonitor. | |
164 // Warning: Please remember that sleep deprivation is not a good thing; use | |
165 // with caution. | |
166 void BeginPowerRequirement(PowerRequirement requirement, | |
167 const std::string& reason); | |
168 void EndPowerRequirement(PowerRequirement requirement, | |
169 const std::string& reason); | |
170 | |
171 // Returns the number of outsanding power requirement requests. | |
172 size_t GetPowerRequirementsCountForTest() const; | |
173 | |
174 private: | 148 private: |
175 #if defined(OS_MACOSX) | 149 #if defined(OS_MACOSX) |
176 void PlatformInit(); | 150 void PlatformInit(); |
177 void PlatformDestroy(); | 151 void PlatformDestroy(); |
178 #endif | 152 #endif |
179 | 153 |
180 // Platform-specific method to check whether the system is currently | 154 // Platform-specific method to check whether the system is currently |
181 // running on battery power. Returns true if running on batteries, | 155 // running on battery power. Returns true if running on batteries, |
182 // false otherwise. | 156 // false otherwise. |
183 bool IsBatteryPower(); | 157 bool IsBatteryPower(); |
(...skipping 11 matching lines...) Expand all Loading... |
195 void NotifyPowerStateChange(); | 169 void NotifyPowerStateChange(); |
196 void NotifySuspend(); | 170 void NotifySuspend(); |
197 void NotifyResume(); | 171 void NotifyResume(); |
198 | 172 |
199 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | 173 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; |
200 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 174 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
201 devices_changed_observer_list_; | 175 devices_changed_observer_list_; |
202 bool battery_in_use_; | 176 bool battery_in_use_; |
203 bool suspended_; | 177 bool suspended_; |
204 | 178 |
205 #if defined(OS_WIN) | |
206 std::map<std::string, std::pair<HANDLE, int> > handles_; | |
207 base::ThreadChecker thread_checker_; | |
208 #endif | |
209 | |
210 #if defined(ENABLE_BATTERY_MONITORING) | 179 #if defined(ENABLE_BATTERY_MONITORING) |
211 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | 180 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
212 #endif | 181 #endif |
213 | 182 |
214 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 183 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
215 }; | 184 }; |
216 | 185 |
217 } // namespace base | 186 } // namespace base |
218 | 187 |
219 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 188 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
OLD | NEW |