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

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: modify per 1st round review 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
« no previous file with comments | « base/hi_res_timer_manager_win.cc ('k') | base/power_monitor/power_monitor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
vandebo (ex-Chrome) 2013/03/20 20:36:26 nit: You don't need scoped_ptr.h
12 #include "base/observer_list_threadsafe.h"
13 #include "base/power_monitor/power_observer.h"
14
15 #if defined(OS_WIN)
16 #include <windows.h>
15 17
16 // 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
17 // status. This isn't true for other platforms. 19 // status. This isn't true for other platforms.
18 #if defined(OS_WIN)
19 #define ENABLE_BATTERY_MONITORING 1 20 #define ENABLE_BATTERY_MONITORING 1
20 #else 21 #else
21 #undef ENABLE_BATTERY_MONITORING 22 #undef ENABLE_BATTERY_MONITORING
22 #endif // !OS_WIN 23 #endif // !OS_WIN
23 24
24 #include "base/observer_list_threadsafe.h"
25 #if defined(ENABLE_BATTERY_MONITORING) 25 #if defined(ENABLE_BATTERY_MONITORING)
26 #include "base/timer.h" 26 #include "base/timer.h"
27 #endif // defined(ENABLE_BATTERY_MONITORING) 27 #endif // defined(ENABLE_BATTERY_MONITORING)
28 28
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) 29 #if defined(OS_IOS)
35 #include <objc/runtime.h> 30 #include <objc/runtime.h>
36 #endif // OS_IOS 31 #endif // OS_IOS
37 32
38 namespace base { 33 namespace base {
39 34
40 // Class for monitoring various system-related subsystems 35 // A class used to monitor the power state change and notify the observers about
41 // such as power management, network status, etc. 36 // the change event.
42 // TODO(mbelshe): Add support beyond just power management. 37 class BASE_EXPORT PowerMonitor {
43 class BASE_EXPORT SystemMonitor {
44 public: 38 public:
45 // Normalized list of power events. 39 // Normalized list of power events.
46 enum PowerEvent { 40 enum PowerEvent {
47 POWER_STATE_EVENT, // The Power status of the system has changed. 41 POWER_STATE_EVENT, // The Power status of the system has changed.
48 SUSPEND_EVENT, // The system is being suspended. 42 SUSPEND_EVENT, // The system is being suspended.
49 RESUME_EVENT // The system is being resumed. 43 RESUME_EVENT // The system is being resumed.
50 }; 44 };
51 45
52 // Type of devices whose change need to be monitored, such as add/remove. 46 PowerMonitor();
53 enum DeviceType { 47 ~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 48
59 // Create SystemMonitor. Only one SystemMonitor instance per application 49 // Get the application-wide PowerMonitor (if not present, returns NULL).
60 // is allowed. 50 static PowerMonitor* Get();
61 SystemMonitor();
62 ~SystemMonitor();
63
64 // Get the application-wide SystemMonitor (if not present, returns NULL).
65 static SystemMonitor* Get();
66 51
67 #if defined(OS_MACOSX) 52 #if defined(OS_MACOSX)
68 // Allocate system resources needed by the SystemMonitor class. 53 // Allocate system resources needed by the PowerMonitor class.
69 // 54 //
70 // This function must be called before instantiating an instance of the class 55 // This function must be called before instantiating an instance of the class
71 // and before the Sandbox is initialized. 56 // and before the Sandbox is initialized.
72 #if !defined(OS_IOS) 57 #if !defined(OS_IOS)
73 static void AllocateSystemIOPorts(); 58 static void AllocateSystemIOPorts();
74 #else 59 #else
75 static void AllocateSystemIOPorts() {} 60 static void AllocateSystemIOPorts() {}
76 #endif // OS_IOS 61 #endif // OS_IOS
77 #endif // OS_MACOSX 62 #endif // OS_MACOSX
78 63
79 // 64 // Add and remove an observer.
80 // Power-related APIs 65 // Can be called from any thread.
81 // 66 // Must not be called from within a notification callback.
67 void AddObserver(PowerObserver* observer);
68 void RemoveObserver(PowerObserver* observer);
82 69
83 // Is the computer currently on battery power. 70 // Is the computer currently on battery power. Can be called on any thread.
84 // Can be called on any thread.
85 bool BatteryPower() const { 71 bool BatteryPower() const {
86 // Using a lock here is not necessary for just a bool. 72 // Using a lock here is not necessary for just a bool.
87 return battery_in_use_; 73 return battery_in_use_;
88 } 74 }
89 75
90 // Callbacks will be called on the thread which creates the SystemMonitor. 76 private:
91 // During the callback, Add/RemoveObserver will block until the callbacks 77 friend class PowerMonitorTest;
92 // are finished. Observers should implement quick callback functions; if 78 // A friend function that is allowed to access the private ProcessPowerEvent.
93 // lengthy operations are needed, the observer should take care to invoke 79 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 80
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) 81 #if defined(OS_WIN)
145 // Represents a message-only window for power message handling on Windows. 82 // Represents a message-only window for power message handling on Windows.
146 // Only allow SystemMonitor to create it. 83 // Only allow PowerMonitor to create it.
147 class PowerMessageWindow { 84 class PowerMessageWindow {
148 public: 85 public:
149 PowerMessageWindow(); 86 PowerMessageWindow();
150 ~PowerMessageWindow(); 87 ~PowerMessageWindow();
151 88
152 private: 89 private:
153 void ProcessWmPowerBroadcastMessage(int event_id); 90 void ProcessWmPowerBroadcastMessage(int event_id);
154 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, 91 LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
155 WPARAM wparam, LPARAM lparam); 92 WPARAM wparam, LPARAM lparam);
156 static LRESULT CALLBACK WndProcThunk(HWND hwnd, 93 static LRESULT CALLBACK WndProcThunk(HWND hwnd,
157 UINT message, 94 UINT message,
158 WPARAM wparam, 95 WPARAM wparam,
159 LPARAM lparam); 96 LPARAM lparam);
160 // Instance of the module containing the window procedure. 97 // Instance of the module containing the window procedure.
161 HMODULE instance_; 98 HMODULE instance_;
162 // A hidden message-only window. 99 // A hidden message-only window.
163 HWND message_hwnd_; 100 HWND message_hwnd_;
164 }; 101 };
165 #endif 102 #endif // OS_WIN
166 103
167 #if defined(OS_MACOSX) 104 #if defined(OS_MACOSX)
168 void PlatformInit(); 105 void PlatformInit();
169 void PlatformDestroy(); 106 void PlatformDestroy();
170 #endif 107 #endif
171 108
109 // Cross-platform handling of a power event.
110 void ProcessPowerEvent(PowerEvent event_id);
111
172 // Platform-specific method to check whether the system is currently 112 // Platform-specific method to check whether the system is currently
173 // running on battery power. Returns true if running on batteries, 113 // running on battery power. Returns true if running on batteries,
174 // false otherwise. 114 // false otherwise.
175 bool IsBatteryPower(); 115 bool IsBatteryPower();
176 116
177 // Checks the battery status and notifies observers if the battery 117 // Checks the battery status and notifies observers if the battery
178 // status has changed. 118 // status has changed.
179 void BatteryCheck(); 119 void BatteryCheck();
180 120
181 // Functions to trigger notifications.
182 void NotifyDevicesChanged(DeviceType device_type);
183 void NotifyPowerStateChange(); 121 void NotifyPowerStateChange();
184 void NotifySuspend(); 122 void NotifySuspend();
185 void NotifyResume(); 123 void NotifyResume();
186 124
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) 125 #if defined(OS_IOS)
198 // Holds pointers to system event notification observers. 126 // Holds pointers to system event notification observers.
199 std::vector<id> notification_observers_; 127 std::vector<id> notification_observers_;
200 #endif 128 #endif
201 129
130 #if defined(ENABLE_BATTERY_MONITORING)
131 base::OneShotTimer<PowerMonitor> delayed_battery_check_;
132 #endif
133
134 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_;
135 bool battery_in_use_;
136 bool suspended_;
137
202 #if defined(OS_WIN) 138 #if defined(OS_WIN)
203 PowerMessageWindow power_message_window_; 139 PowerMessageWindow power_message_window_;
204 #endif 140 #endif
205 141
206 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); 142 DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
207 }; 143 };
208 144
209 } // namespace base 145 } // namespace base
210 146
211 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ 147 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_
OLDNEW
« no previous file with comments | « base/hi_res_timer_manager_win.cc ('k') | base/power_monitor/power_monitor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698