OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef APP_SYSTEM_MONITOR_H_ | |
6 #define APP_SYSTEM_MONITOR_H_ | |
7 #pragma once | |
8 | |
9 #include "build/build_config.h" | |
10 | |
11 // Windows HiRes timers drain the battery faster so we need to know the battery | |
12 // status. This isn't true for other platforms. | |
13 #if defined(OS_WIN) | |
14 #define ENABLE_BATTERY_MONITORING 1 | |
15 #else | |
16 #undef ENABLE_BATTERY_MONITORING | |
17 #endif // !OS_WIN | |
18 | |
19 #include "base/observer_list_threadsafe.h" | |
20 #if defined(ENABLE_BATTERY_MONITORING) | |
21 #include "base/timer.h" | |
22 #endif // defined(ENABLE_BATTERY_MONITORING) | |
23 | |
24 #if defined(OS_MACOSX) | |
25 #ifdef __OBJC__ | |
26 @class SystemMonitorBridge; | |
27 #else | |
28 class SystemMonitorBridge; | |
29 #endif | |
30 #endif | |
31 | |
32 // Class for monitoring various system-related subsystems | |
33 // such as power management, network status, etc. | |
34 // TODO(mbelshe): Add support beyond just power management. | |
35 class SystemMonitor { | |
36 public: | |
37 // Normalized list of power events. | |
38 enum PowerEvent { | |
39 POWER_STATE_EVENT, // The Power status of the system has changed. | |
40 SUSPEND_EVENT, // The system is being suspended. | |
41 RESUME_EVENT // The system is being resumed. | |
42 }; | |
43 | |
44 // Create SystemMonitor. Only one SystemMonitor instance per application | |
45 // is allowed. | |
46 SystemMonitor(); | |
47 ~SystemMonitor(); | |
48 | |
49 // Get the application-wide SystemMonitor (if not present, returns NULL). | |
50 static SystemMonitor* Get(); | |
51 | |
52 // | |
53 // Power-related APIs | |
54 // | |
55 | |
56 // Is the computer currently on battery power. | |
57 // Can be called on any thread. | |
58 bool BatteryPower() const { | |
59 // Using a lock here is not necessary for just a bool. | |
60 return battery_in_use_; | |
61 } | |
62 | |
63 // Callbacks will be called on the thread which creates the SystemMonitor. | |
64 // During the callback, Add/RemoveObserver will block until the callbacks | |
65 // are finished. Observers should implement quick callback functions; if | |
66 // lengthy operations are needed, the observer should take care to invoke | |
67 // the operation on an appropriate thread. | |
68 class PowerObserver { | |
69 public: | |
70 // Notification of a change in power status of the computer, such | |
71 // as from switching between battery and A/C power. | |
72 virtual void OnPowerStateChange(bool on_battery_power) {} | |
73 | |
74 // Notification that the system is suspending. | |
75 virtual void OnSuspend() {} | |
76 | |
77 // Notification that the system is resuming. | |
78 virtual void OnResume() {} | |
79 | |
80 protected: | |
81 virtual ~PowerObserver() {} | |
82 }; | |
83 | |
84 // Add a new observer. | |
85 // Can be called from any thread. | |
86 // Must not be called from within a notification callback. | |
87 void AddObserver(PowerObserver* obs); | |
88 | |
89 // Remove an existing observer. | |
90 // Can be called from any thread. | |
91 // Must not be called from within a notification callback. | |
92 void RemoveObserver(PowerObserver* obs); | |
93 | |
94 #if defined(OS_WIN) | |
95 // Windows-specific handling of a WM_POWERBROADCAST message. | |
96 // Embedders of this API should hook their top-level window | |
97 // message loop and forward WM_POWERBROADCAST through this call. | |
98 void ProcessWmPowerBroadcastMessage(int event_id); | |
99 #endif | |
100 | |
101 // Cross-platform handling of a power event. | |
102 void ProcessPowerMessage(PowerEvent event_id); | |
103 | |
104 private: | |
105 #if defined(OS_MACOSX) | |
106 void PlatformInit(); | |
107 void PlatformDestroy(); | |
108 #endif | |
109 | |
110 // Platform-specific method to check whether the system is currently | |
111 // running on battery power. Returns true if running on batteries, | |
112 // false otherwise. | |
113 bool IsBatteryPower(); | |
114 | |
115 // Checks the battery status and notifies observers if the battery | |
116 // status has changed. | |
117 void BatteryCheck(); | |
118 | |
119 // Functions to trigger notifications. | |
120 void NotifyPowerStateChange(); | |
121 void NotifySuspend(); | |
122 void NotifyResume(); | |
123 | |
124 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; | |
125 bool battery_in_use_; | |
126 bool suspended_; | |
127 | |
128 #if defined(ENABLE_BATTERY_MONITORING) | |
129 base::OneShotTimer<SystemMonitor> delayed_battery_check_; | |
130 #endif | |
131 | |
132 #if defined(OS_MACOSX) | |
133 SystemMonitorBridge* system_monitor_bridge_; | |
134 #endif | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); | |
137 }; | |
138 | |
139 #endif // APP_SYSTEM_MONITOR_H_ | |
OLD | NEW |