Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: base/power_monitor/power_monitor.h

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Use PowerMonitor instead of SystemMonitor in XXXMain function Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_POWER_MONITOR_POWER_MONITOR_H_
6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11 7
12 #include "base/base_export.h" 8 #include "base/base_export.h"
13 #include "base/basictypes.h" 9 #include "base/basictypes.h"
14 #include "build/build_config.h" 10 #include "base/memory/scoped_ptr.h"
vandebo (ex-Chrome) 2013/03/19 23:36:04 you want base/memory/ref_counted.h for scoped_refp
Hongbo Min 2013/03/20 13:01:50 Added. But I am still a bit confused why to use sc
vandebo (ex-Chrome) 2013/03/20 20:36:26 I think it is kept in a scoped_refptr to avoid inc
11 #include "base/observer_list_threadsafe.h"
12 #include "base/power_monitor/power_observer.h"
15 13
14 #if defined(OS_WIN)
15 #include <windows.h>
16 // Windows HiRes timers drain the battery faster so we need to know the battery 16 // Windows HiRes timers drain the battery faster so we need to know the battery
vandebo (ex-Chrome) 2013/03/19 23:36:04 Nit: add an empty line above comment.
Hongbo Min 2013/03/20 13:01:50 Done.
17 // status. This isn't true for other platforms. 17 // status. This isn't true for other platforms.
18 #if defined(OS_WIN)
19 #define ENABLE_BATTERY_MONITORING 1 18 #define ENABLE_BATTERY_MONITORING 1
20 #else 19 #else
21 #undef ENABLE_BATTERY_MONITORING 20 #undef ENABLE_BATTERY_MONITORING
22 #endif // !OS_WIN 21 #endif // !OS_WIN
23 22
24 #include "base/observer_list_threadsafe.h"
25 #if defined(ENABLE_BATTERY_MONITORING) 23 #if defined(ENABLE_BATTERY_MONITORING)
26 #include "base/timer.h" 24 #include "base/timer.h"
27 #endif // defined(ENABLE_BATTERY_MONITORING) 25 #endif // defined(ENABLE_BATTERY_MONITORING)
28 26
29 #if defined(OS_MACOSX) && !defined(OS_IOS)
30 #include <IOKit/pwr_mgt/IOPMLib.h>
31 #include <IOKit/IOMessage.h>
32 #endif // OS_MACOSX && !OS_IOS
33
34 #if defined(OS_IOS) 27 #if defined(OS_IOS)
35 #include <objc/runtime.h> 28 #include <objc/runtime.h>
36 #endif // OS_IOS 29 #endif // OS_IOS
37 30
38 namespace base { 31 namespace base {
39 32
40 // Class for monitoring various system-related subsystems 33 // A class used to monitor the power state change and notify the observers about
41 // such as power management, network status, etc. 34 // the change event.
42 // TODO(mbelshe): Add support beyond just power management. 35 class BASE_EXPORT PowerMonitor {
43 class BASE_EXPORT SystemMonitor {
44 public: 36 public:
45 // Normalized list of power events. 37 // Normalized list of power events.
46 enum PowerEvent { 38 enum PowerEvent {
47 POWER_STATE_EVENT, // The Power status of the system has changed. 39 POWER_STATE_EVENT, // The Power status of the system has changed.
48 SUSPEND_EVENT, // The system is being suspended. 40 SUSPEND_EVENT, // The system is being suspended.
49 RESUME_EVENT // The system is being resumed. 41 RESUME_EVENT // The system is being resumed.
50 }; 42 };
51 43
52 // Type of devices whose change need to be monitored, such as add/remove. 44 PowerMonitor();
53 enum DeviceType { 45 ~PowerMonitor();
54 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone.
55 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam.
56 DEVTYPE_UNKNOWN, // Other devices.
57 };
58 46
59 // Create SystemMonitor. Only one SystemMonitor instance per application 47 // Get the application-wide PowerMonitor (if not present, returns NULL).
60 // is allowed. 48 static PowerMonitor* Get();
61 SystemMonitor();
62 ~SystemMonitor();
63
64 // Get the application-wide SystemMonitor (if not present, returns NULL).
65 static SystemMonitor* Get();
66 49
67 #if defined(OS_MACOSX) 50 #if defined(OS_MACOSX)
68 // Allocate system resources needed by the SystemMonitor class. 51 // Allocate system resources needed by the PowerMonitor class.
69 // 52 //
70 // This function must be called before instantiating an instance of the class 53 // This function must be called before instantiating an instance of the class
71 // and before the Sandbox is initialized. 54 // and before the Sandbox is initialized.
72 #if !defined(OS_IOS) 55 #if !defined(OS_IOS)
73 static void AllocateSystemIOPorts(); 56 static void AllocateSystemIOPorts();
74 #else 57 #else
75 static void AllocateSystemIOPorts() {} 58 static void AllocateSystemIOPorts() {}
76 #endif // OS_IOS 59 #endif // OS_IOS
77 #endif // OS_MACOSX 60 #endif // OS_MACOSX
78 61
79 // 62 // Add and remove an observer.
80 // Power-related APIs 63 // Can be called from any thread.
81 // 64 // Must not be called from within a notification callback.
65 void AddObserver(PowerObserver* observer);
66 void RemoveObserver(PowerObserver* observer);
82 67
83 // Is the computer currently on battery power. 68 // Is the computer currently on battery power. Can be called on any thread.
84 // Can be called on any thread.
85 bool BatteryPower() const { 69 bool BatteryPower() const {
86 // Using a lock here is not necessary for just a bool. 70 // Using a lock here is not necessary for just a bool.
87 return battery_in_use_; 71 return battery_in_use_;
88 } 72 }
89 73
90 // Callbacks will be called on the thread which creates the SystemMonitor. 74 private:
91 // During the callback, Add/RemoveObserver will block until the callbacks 75 friend class PowerMonitorTest;
92 // are finished. Observers should implement quick callback functions; if 76 // A friend function that is allowed to access the private ProcessPowerEvent.
93 // lengthy operations are needed, the observer should take care to invoke 77 friend void ProcessPowerEventHelper(PowerEvent);
94 // the operation on an appropriate thread.
95 class BASE_EXPORT PowerObserver {
96 public:
97 // Notification of a change in power status of the computer, such
98 // as from switching between battery and A/C power.
99 virtual void OnPowerStateChange(bool on_battery_power) {}
100 78
101 // Notification that the system is suspending.
102 virtual void OnSuspend() {}
103
104 // Notification that the system is resuming.
105 virtual void OnResume() {}
106
107 protected:
108 virtual ~PowerObserver() {}
109 };
110
111 class BASE_EXPORT DevicesChangedObserver {
112 public:
113 // Notification that the devices connected to the system have changed.
114 // This is only implemented on Windows currently.
115 virtual void OnDevicesChanged(DeviceType device_type) {}
116
117 protected:
118 virtual ~DevicesChangedObserver() {}
119 };
120
121 // Add a new observer.
122 // Can be called from any thread.
123 // Must not be called from within a notification callback.
124 void AddPowerObserver(PowerObserver* obs);
125 void AddDevicesChangedObserver(DevicesChangedObserver* obs);
126
127 // Remove an existing observer.
128 // Can be called from any thread.
129 // Must not be called from within a notification callback.
130 void RemovePowerObserver(PowerObserver* obs);
131 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs);
132
133 // The ProcessFoo() style methods are a broken pattern and should not
134 // be copied. Any significant addition to this class is blocked on
135 // refactoring to improve the state of affairs. See http://crbug.com/149059
136
137 // Cross-platform handling of a power event.
138 void ProcessPowerMessage(PowerEvent event_id);
139
140 // Cross-platform handling of a device change event.
141 void ProcessDevicesChanged(DeviceType device_type);
142
143 private:
144 #if defined(OS_WIN) 79 #if defined(OS_WIN)
145 // Represents a message-only window for power message handling on Windows. 80 // Represents a message-only window for power message handling on Windows.
146 // Only allow SystemMonitor to create it. 81 // Only allow PowerMonitor to create it.
147 class PowerMessageWindow { 82 class PowerMessageWindow {
148 public: 83 public:
149 PowerMessageWindow(); 84 PowerMessageWindow();
150 ~PowerMessageWindow(); 85 ~PowerMessageWindow();
151 86
152 private: 87 private:
153 void ProcessWmPowerBroadcastMessage(int event_id); 88 void ProcessWmPowerBroadcastMessage(int event_id);
154 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, 89 LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
155 WPARAM wparam, LPARAM lparam); 90 WPARAM wparam, LPARAM lparam);
156 static LRESULT CALLBACK WndProcThunk(HWND hwnd, 91 static LRESULT CALLBACK WndProcThunk(HWND hwnd,
157 UINT message, 92 UINT message,
158 WPARAM wparam, 93 WPARAM wparam,
159 LPARAM lparam); 94 LPARAM lparam);
160 // Instance of the module containing the window procedure. 95 // Instance of the module containing the window procedure.
161 HMODULE instance_; 96 HMODULE instance_;
162 // A hidden message-only window. 97 // A hidden message-only window.
163 HWND message_hwnd_; 98 HWND message_hwnd_;
164 }; 99 };
165 #endif 100 #endif // OS_WIN
166 101
167 #if defined(OS_MACOSX) 102 #if defined(OS_MACOSX)
168 void PlatformInit(); 103 void PlatformInit();
169 void PlatformDestroy(); 104 void PlatformDestroy();
170 #endif 105 #endif
171 106
107 // Cross-platform handling of a power event.
108 void ProcessPowerEvent(PowerEvent event_id);
109
172 // Platform-specific method to check whether the system is currently 110 // Platform-specific method to check whether the system is currently
173 // running on battery power. Returns true if running on batteries, 111 // running on battery power. Returns true if running on batteries,
174 // false otherwise. 112 // false otherwise.
175 bool IsBatteryPower(); 113 bool IsBatteryPower();
176 114
177 // Checks the battery status and notifies observers if the battery 115 // Checks the battery status and notifies observers if the battery
178 // status has changed. 116 // status has changed.
179 void BatteryCheck(); 117 void BatteryCheck();
180 118
181 // Functions to trigger notifications.
182 void NotifyDevicesChanged(DeviceType device_type);
183 void NotifyPowerStateChange(); 119 void NotifyPowerStateChange();
184 void NotifySuspend(); 120 void NotifySuspend();
185 void NotifyResume(); 121 void NotifyResume();
186 122
187 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_;
188 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> >
189 devices_changed_observer_list_;
190 bool battery_in_use_;
191 bool suspended_;
192
193 #if defined(ENABLE_BATTERY_MONITORING)
194 base::OneShotTimer<SystemMonitor> delayed_battery_check_;
195 #endif
196
197 #if defined(OS_IOS) 123 #if defined(OS_IOS)
198 // Holds pointers to system event notification observers. 124 // Holds pointers to system event notification observers.
199 std::vector<id> notification_observers_; 125 std::vector<id> notification_observers_;
200 #endif 126 #endif
201 127
128 #if defined(ENABLE_BATTERY_MONITORING)
129 base::OneShotTimer<PowerMonitor> delayed_battery_check_;
130 #endif
131
132 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_;
133 bool battery_in_use_;
134 bool suspended_;
135
202 #if defined(OS_WIN) 136 #if defined(OS_WIN)
203 PowerMessageWindow power_message_window_; 137 PowerMessageWindow power_message_window_;
204 #endif 138 #endif
205 139
206 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); 140 DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
207 }; 141 };
208 142
209 } // namespace base 143 } // namespace base
210 144
211 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ 145 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698