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> |
9 #include <string> | 10 #include <string> |
| 11 #include <utility> |
10 | 12 |
11 #include "base/base_export.h" | 13 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/threading/thread_checker.h" |
13 #include "build/build_config.h" | 16 #include "build/build_config.h" |
14 | 17 |
15 // Windows HiRes timers drain the battery faster so we need to know the battery | 18 // Windows HiRes timers drain the battery faster so we need to know the battery |
16 // status. This isn't true for other platforms. | 19 // status. This isn't true for other platforms. |
17 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
18 #define ENABLE_BATTERY_MONITORING 1 | 21 #define ENABLE_BATTERY_MONITORING 1 |
19 #else | 22 #else |
20 #undef ENABLE_BATTERY_MONITORING | 23 #undef ENABLE_BATTERY_MONITORING |
21 #endif // !OS_WIN | 24 #endif // !OS_WIN |
22 | 25 |
(...skipping 16 matching lines...) Expand all Loading... |
39 // TODO(mbelshe): Add support beyond just power management. | 42 // TODO(mbelshe): Add support beyond just power management. |
40 class BASE_EXPORT SystemMonitor { | 43 class BASE_EXPORT SystemMonitor { |
41 public: | 44 public: |
42 // Normalized list of power events. | 45 // Normalized list of power events. |
43 enum PowerEvent { | 46 enum PowerEvent { |
44 POWER_STATE_EVENT, // The Power status of the system has changed. | 47 POWER_STATE_EVENT, // The Power status of the system has changed. |
45 SUSPEND_EVENT, // The system is being suspended. | 48 SUSPEND_EVENT, // The system is being suspended. |
46 RESUME_EVENT // The system is being resumed. | 49 RESUME_EVENT // The system is being resumed. |
47 }; | 50 }; |
48 | 51 |
| 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 |
49 typedef unsigned int DeviceIdType; | 59 typedef unsigned int DeviceIdType; |
50 | 60 |
51 // Create SystemMonitor. Only one SystemMonitor instance per application | 61 // Create SystemMonitor. Only one SystemMonitor instance per application |
52 // is allowed. | 62 // is allowed. |
53 SystemMonitor(); | 63 SystemMonitor(); |
54 ~SystemMonitor(); | 64 ~SystemMonitor(); |
55 | 65 |
56 // Get the application-wide SystemMonitor (if not present, returns NULL). | 66 // Get the application-wide SystemMonitor (if not present, returns NULL). |
57 static SystemMonitor* Get(); | 67 static SystemMonitor* Get(); |
58 | 68 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // Cross-platform handling of a power event. | 148 // Cross-platform handling of a power event. |
139 void ProcessPowerMessage(PowerEvent event_id); | 149 void ProcessPowerMessage(PowerEvent event_id); |
140 | 150 |
141 // Cross-platform handling of a device change event. | 151 // Cross-platform handling of a device change event. |
142 void ProcessDevicesChanged(); | 152 void ProcessDevicesChanged(); |
143 void ProcessMediaDeviceAttached(const DeviceIdType& id, | 153 void ProcessMediaDeviceAttached(const DeviceIdType& id, |
144 const std::string& name, | 154 const std::string& name, |
145 const FilePath& path); | 155 const FilePath& path); |
146 void ProcessMediaDeviceDetached(const DeviceIdType& id); | 156 void ProcessMediaDeviceDetached(const DeviceIdType& id); |
147 | 157 |
| 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 |
148 private: | 174 private: |
149 #if defined(OS_MACOSX) | 175 #if defined(OS_MACOSX) |
150 void PlatformInit(); | 176 void PlatformInit(); |
151 void PlatformDestroy(); | 177 void PlatformDestroy(); |
152 #endif | 178 #endif |
153 | 179 |
154 // Platform-specific method to check whether the system is currently | 180 // Platform-specific method to check whether the system is currently |
155 // running on battery power. Returns true if running on batteries, | 181 // running on battery power. Returns true if running on batteries, |
156 // false otherwise. | 182 // false otherwise. |
157 bool IsBatteryPower(); | 183 bool IsBatteryPower(); |
(...skipping 11 matching lines...) Expand all Loading... |
169 void NotifyPowerStateChange(); | 195 void NotifyPowerStateChange(); |
170 void NotifySuspend(); | 196 void NotifySuspend(); |
171 void NotifyResume(); | 197 void NotifyResume(); |
172 | 198 |
173 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | 199 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; |
174 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > | 200 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > |
175 devices_changed_observer_list_; | 201 devices_changed_observer_list_; |
176 bool battery_in_use_; | 202 bool battery_in_use_; |
177 bool suspended_; | 203 bool suspended_; |
178 | 204 |
| 205 #if defined(OS_WIN) |
| 206 std::map<std::string, std::pair<HANDLE, int> > handles_; |
| 207 base::ThreadChecker thread_checker_; |
| 208 #endif |
| 209 |
179 #if defined(ENABLE_BATTERY_MONITORING) | 210 #if defined(ENABLE_BATTERY_MONITORING) |
180 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | 211 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
181 #endif | 212 #endif |
182 | 213 |
183 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | 214 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
184 }; | 215 }; |
185 | 216 |
186 } // namespace base | 217 } // namespace base |
187 | 218 |
188 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ | 219 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ |
OLD | NEW |