|
1 // Copyright (c) 2008 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 BASE_SYSTEM_MONITOR_H_ |
|
6 #define BASE_SYSTEM_MONITOR_H_ |
|
7 |
|
8 #include "base/observer_list_threadsafe.h" |
|
9 #include "base/singleton.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 namespace base { |
|
20 |
|
21 // Class for monitoring various system-related subsystems |
|
22 // such as power management, network status, etc. |
|
23 // TODO(mbelshe): Add support beyond just power management. |
|
24 class SystemMonitor { |
|
25 public: |
|
26 // Access to the Singleton |
|
27 static SystemMonitor* Get() { |
|
28 // Uses the LeakySingletonTrait because cleanup is optional. |
|
29 return |
|
30 Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get(); |
|
31 } |
|
32 |
|
33 // Start the System Monitor within a process. This method |
|
34 // is provided so that the battery check can be deferred. |
|
35 // The MessageLoop must be started before calling this |
|
36 // method. |
|
37 // This is a no-op on platforms for which ENABLE_BATTERY_MONITORING is |
|
38 // disabled. |
|
39 static void Start(); |
|
40 |
|
41 // |
|
42 // Power-related APIs |
|
43 // |
|
44 |
|
45 // Is the computer currently on battery power. |
|
46 // Can be called on any thread. |
|
47 bool BatteryPower() { |
|
48 // Using a lock here is not necessary for just a bool. |
|
49 return battery_in_use_; |
|
50 } |
|
51 |
|
52 // Normalized list of power events. |
|
53 enum PowerEvent { |
|
54 POWER_STATE_EVENT, // The Power status of the system has changed. |
|
55 SUSPEND_EVENT, // The system is being suspended. |
|
56 RESUME_EVENT // The system is being resumed. |
|
57 }; |
|
58 |
|
59 // Callbacks will be called on the thread which creates the SystemMonitor. |
|
60 // During the callback, Add/RemoveObserver will block until the callbacks |
|
61 // are finished. Observers should implement quick callback functions; if |
|
62 // lengthy operations are needed, the observer should take care to invoke |
|
63 // the operation on an appropriate thread. |
|
64 class PowerObserver { |
|
65 public: |
|
66 // Notification of a change in power status of the computer, such |
|
67 // as from switching between battery and A/C power. |
|
68 virtual void OnPowerStateChange(SystemMonitor*) = 0; |
|
69 |
|
70 // Notification that the system is suspending. |
|
71 virtual void OnSuspend(SystemMonitor*) = 0; |
|
72 |
|
73 // Notification that the system is resuming. |
|
74 virtual void OnResume(SystemMonitor*) = 0; |
|
75 }; |
|
76 |
|
77 // Add a new observer. |
|
78 // Can be called from any thread. |
|
79 // Must not be called from within a notification callback. |
|
80 void AddObserver(PowerObserver* obs); |
|
81 |
|
82 // Remove an existing observer. |
|
83 // Can be called from any thread. |
|
84 // Must not be called from within a notification callback. |
|
85 void RemoveObserver(PowerObserver* obs); |
|
86 |
|
87 #if defined(OS_WIN) |
|
88 // Windows-specific handling of a WM_POWERBROADCAST message. |
|
89 // Embedders of this API should hook their top-level window |
|
90 // message loop and forward WM_POWERBROADCAST through this call. |
|
91 void ProcessWmPowerBroadcastMessage(int event_id); |
|
92 #endif |
|
93 |
|
94 // Cross-platform handling of a power event. |
|
95 void ProcessPowerMessage(PowerEvent event_id); |
|
96 |
|
97 // Constructor. |
|
98 // Don't use this; access SystemMonitor via the Singleton. |
|
99 SystemMonitor(); |
|
100 |
|
101 private: |
|
102 // Platform-specific method to check whether the system is currently |
|
103 // running on battery power. Returns true if running on batteries, |
|
104 // false otherwise. |
|
105 bool IsBatteryPower(); |
|
106 |
|
107 // Checks the battery status and notifies observers if the battery |
|
108 // status has changed. |
|
109 void BatteryCheck(); |
|
110 |
|
111 // Functions to trigger notifications. |
|
112 void NotifyPowerStateChange(); |
|
113 void NotifySuspend(); |
|
114 void NotifyResume(); |
|
115 |
|
116 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; |
|
117 bool battery_in_use_; |
|
118 bool suspended_; |
|
119 |
|
120 #if defined(ENABLE_BATTERY_MONITORING) |
|
121 base::OneShotTimer<SystemMonitor> delayed_battery_check_; |
|
122 #endif |
|
123 |
|
124 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); |
|
125 }; |
|
126 |
|
127 } |
|
128 |
|
129 #endif // BASE_SYSTEM_MONITOR_H_ |