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