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

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: update Created 8 years, 1 month 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 (c) 2012 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 "base/file_path.h" 10 #include "base/memory/scoped_ptr.h"
15 #include "base/string16.h" 11 #include "base/power_monitor/power_observer.h"
16 #include "base/synchronization/lock.h" 12 #include "base/observer_list_threadsafe.h"
17 #include "build/build_config.h"
18 13
19 // Windows HiRes timers drain the battery faster so we need to know the battery 14 // Windows HiRes timers drain the battery faster so we need to know the battery
20 // status. This isn't true for other platforms. 15 // status. This isn't true for other platforms.
21 #if defined(OS_WIN) 16 #if defined(OS_WIN)
22 #define ENABLE_BATTERY_MONITORING 1 17 #define ENABLE_BATTERY_MONITORING 1
23 #else 18 #else
24 #undef ENABLE_BATTERY_MONITORING 19 #undef ENABLE_BATTERY_MONITORING
25 #endif // !OS_WIN 20 #endif // !OS_WIN
26 21
27 #include "base/observer_list_threadsafe.h"
28 #if defined(ENABLE_BATTERY_MONITORING) 22 #if defined(ENABLE_BATTERY_MONITORING)
29 #include "base/timer.h" 23 #include "base/timer.h"
30 #endif // defined(ENABLE_BATTERY_MONITORING) 24 #endif // defined(ENABLE_BATTERY_MONITORING)
31 25
32 #if defined(OS_MACOSX) && !defined(OS_IOS)
33 #include <IOKit/pwr_mgt/IOPMLib.h>
34 #include <IOKit/IOMessage.h>
35 #endif // OS_MACOSX && !OS_IOS
36
37 #if defined(OS_IOS) 26 #if defined(OS_IOS)
38 #include <objc/runtime.h> 27 #include <objc/runtime.h>
39 #endif // OS_IOS 28 #endif // OS_IOS
40 29
41 namespace base { 30 namespace base {
42 31
43 // Class for monitoring various system-related subsystems 32 // A class used to monitor the power state change and notify the observers about
44 // such as power management, network status, etc. 33 // the change event.
45 // TODO(mbelshe): Add support beyond just power management. 34 class BASE_EXPORT PowerMonitor {
46 class BASE_EXPORT SystemMonitor {
47 public: 35 public:
48 // Normalized list of power events. 36 // Normalized list of power events.
49 enum PowerEvent { 37 enum PowerEvent {
50 POWER_STATE_EVENT, // The Power status of the system has changed. 38 POWER_STATE_EVENT, // The Power status of the system has changed.
51 SUSPEND_EVENT, // The system is being suspended. 39 SUSPEND_EVENT, // The system is being suspended.
52 RESUME_EVENT // The system is being resumed. 40 RESUME_EVENT // The system is being resumed.
53 }; 41 };
54 42
55 // Type of devices whose change need to be monitored, such as add/remove. 43 // A helper class that exposes ProcessPowerEvent to the power information
56 enum DeviceType { 44 // provider. This approach is taken instead of using friends because some
57 DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. 45 // providers live outside of base (i.e. views::HWNDMessageHandler).
58 DEVTYPE_VIDEO_CAPTURE, // Video capture device, e.g., webcam. 46 class BASE_EXPORT Signaler {
59 DEVTYPE_UNKNOWN, // Other devices. 47 public:
48 ~Signaler();
49 #if defined(OS_WIN)
50 void ProcessWmPowerBroadcastMessage(int event);
51 #endif
52 void ProcessPowerEvent(PowerMonitor::PowerEvent event);
53
54 private:
55 friend class PowerMonitor;
56 // Only PowerMonitor can create this.
57 Signaler();
60 }; 58 };
61 59
62 struct BASE_EXPORT RemovableStorageInfo { 60 PowerMonitor();
63 RemovableStorageInfo(); 61 ~PowerMonitor();
64 RemovableStorageInfo(const std::string& id,
65 const string16& device_name,
66 const FilePath::StringType& device_location);
67 62
68 // Unique device id - persists between device attachments. 63 // Get the application-wide PowerMonitor (if not present, returns NULL).
69 std::string device_id; 64 static PowerMonitor* Get();
70 65
71 // Human readable removable storage device name. 66 // The first call to this method will return an instance of Signaler which
72 string16 name; 67 // the caller can use to signal power events. All subsequent calls will
73 68 // return NULL. The caller owns the Signaler instance.
74 // Current attached removable storage device location. 69 PowerMonitor::Signaler* GetSignalerOnce();
75 FilePath::StringType location;
76 };
77
78 // Create SystemMonitor. Only one SystemMonitor instance per application
79 // is allowed.
80 SystemMonitor();
81 ~SystemMonitor();
82
83 // Get the application-wide SystemMonitor (if not present, returns NULL).
84 static SystemMonitor* Get();
85 70
86 #if defined(OS_MACOSX) 71 #if defined(OS_MACOSX)
87 // Allocate system resources needed by the SystemMonitor class. 72 // Allocate system resources needed by the PowerMonitor class.
88 // 73 //
89 // This function must be called before instantiating an instance of the class 74 // This function must be called before instantiating an instance of the class
90 // and before the Sandbox is initialized. 75 // and before the Sandbox is initialized.
91 #if !defined(OS_IOS) 76 #if !defined(OS_IOS)
92 static void AllocateSystemIOPorts(); 77 static void AllocateSystemIOPorts();
93 #else 78 #else
94 static void AllocateSystemIOPorts() {} 79 static void AllocateSystemIOPorts() {}
95 #endif // OS_IOS 80 #endif // OS_IOS
96 #endif // OS_MACOSX 81 #endif // OS_MACOSX
97 82
98 // Returns information for attached removable storage. 83 // Add and remove an observer.
99 std::vector<RemovableStorageInfo> GetAttachedRemovableStorage() const; 84 // Can be called from any thread.
85 // Must not be called from within a notification callback.
86 void AddObserver(PowerObserver* observer);
87 void RemoveObserver(PowerObserver* observer);
100 88
101 // 89 // Is the computer currently on battery power. Can be called on any thread.
102 // Power-related APIs
103 //
104
105 // Is the computer currently on battery power.
106 // Can be called on any thread.
107 bool BatteryPower() const { 90 bool BatteryPower() const {
108 // Using a lock here is not necessary for just a bool. 91 // Using a lock here is not necessary for just a bool.
109 return battery_in_use_; 92 return battery_in_use_;
110 } 93 }
111 94
112 // Callbacks will be called on the thread which creates the SystemMonitor. 95 private:
113 // During the callback, Add/RemoveObserver will block until the callbacks 96 friend class Signaler;
114 // are finished. Observers should implement quick callback functions; if
115 // lengthy operations are needed, the observer should take care to invoke
116 // the operation on an appropriate thread.
117 class BASE_EXPORT PowerObserver {
118 public:
119 // Notification of a change in power status of the computer, such
120 // as from switching between battery and A/C power.
121 virtual void OnPowerStateChange(bool on_battery_power) {}
122 97
123 // Notification that the system is suspending. 98 #if defined(OS_MACOSX)
124 virtual void OnSuspend() {} 99 void PlatformInit();
125 100 void PlatformDestroy();
126 // Notification that the system is resuming. 101 #endif
127 virtual void OnResume() {}
128
129 protected:
130 virtual ~PowerObserver() {}
131 };
132
133 class BASE_EXPORT DevicesChangedObserver {
134 public:
135 // Notification that the devices connected to the system have changed.
136 // This is only implemented on Windows currently.
137 virtual void OnDevicesChanged(DeviceType device_type) {}
138
139 // When a removable storage device is attached or detached, one of these
140 // two events is triggered.
141 virtual void OnRemovableStorageAttached(
142 const std::string& id,
143 const string16& name,
144 const FilePath::StringType& location) {}
145 virtual void OnRemovableStorageDetached(const std::string& id) {}
146
147 protected:
148 virtual ~DevicesChangedObserver() {}
149 };
150
151 // Add a new observer.
152 // Can be called from any thread.
153 // Must not be called from within a notification callback.
154 void AddPowerObserver(PowerObserver* obs);
155 void AddDevicesChangedObserver(DevicesChangedObserver* obs);
156
157 // Remove an existing observer.
158 // Can be called from any thread.
159 // Must not be called from within a notification callback.
160 void RemovePowerObserver(PowerObserver* obs);
161 void RemoveDevicesChangedObserver(DevicesChangedObserver* obs);
162
163 // The ProcessFoo() style methods are a broken pattern and should not
164 // be copied. Any significant addition to this class is blocked on
165 // refactoring to improve the state of affairs. See http://crbug.com/149059
166 102
167 #if defined(OS_WIN) 103 #if defined(OS_WIN)
168 // Windows-specific handling of a WM_POWERBROADCAST message. 104 // Windows-specific handling of a WM_POWERBROADCAST message.
169 // Embedders of this API should hook their top-level window 105 // Embedders of this API should hook their top-level window
170 // message loop and forward WM_POWERBROADCAST through this call. 106 // message loop and forward WM_POWERBROADCAST through this call.
171 void ProcessWmPowerBroadcastMessage(int event_id); 107 void ProcessWmPowerBroadcastMessage(int event_id);
172 #endif 108 #endif
173 109
174 // Cross-platform handling of a power event. 110 // Cross-platform handling of a power event.
175 void ProcessPowerMessage(PowerEvent event_id); 111 void ProcessPowerEvent(PowerEvent event_id);
176
177 // Cross-platform handling of a device change event.
178 void ProcessDevicesChanged(DeviceType device_type);
179 void ProcessRemovableStorageAttached(const std::string& id,
180 const string16& name,
181 const FilePath::StringType& location);
182 void ProcessRemovableStorageDetached(const std::string& id);
183
184 private:
185 // Mapping of unique device id to device info tuple.
186 typedef std::map<std::string, RemovableStorageInfo> RemovableStorageMap;
187
188 #if defined(OS_MACOSX)
189 void PlatformInit();
190 void PlatformDestroy();
191 #endif
192 112
193 // Platform-specific method to check whether the system is currently 113 // Platform-specific method to check whether the system is currently
194 // running on battery power. Returns true if running on batteries, 114 // running on battery power. Returns true if running on batteries,
195 // false otherwise. 115 // false otherwise.
196 bool IsBatteryPower(); 116 bool IsBatteryPower();
197 117
198 // Checks the battery status and notifies observers if the battery 118 // Checks the battery status and notifies observers if the battery
199 // status has changed. 119 // status has changed.
200 void BatteryCheck(); 120 void BatteryCheck();
201 121
202 // Functions to trigger notifications.
203 void NotifyDevicesChanged(DeviceType device_type);
204 void NotifyRemovableStorageAttached(const std::string& id,
205 const string16& name,
206 const FilePath::StringType& location);
207 void NotifyRemovableStorageDetached(const std::string& id);
208 void NotifyPowerStateChange(); 122 void NotifyPowerStateChange();
209 void NotifySuspend(); 123 void NotifySuspend();
210 void NotifyResume(); 124 void NotifyResume();
211 125
212 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_;
213 scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> >
214 devices_changed_observer_list_;
215 bool battery_in_use_;
216 bool suspended_;
217
218 #if defined(ENABLE_BATTERY_MONITORING)
219 base::OneShotTimer<SystemMonitor> delayed_battery_check_;
220 #endif
221
222 #if defined(OS_IOS) 126 #if defined(OS_IOS)
223 // Holds pointers to system event notification observers. 127 // Holds pointers to system event notification observers.
224 std::vector<id> notification_observers_; 128 std::vector<id> notification_observers_;
225 #endif 129 #endif
226 130
227 // For manipulating removable_storage_map_ structure. 131 #if defined(ENABLE_BATTERY_MONITORING)
228 mutable base::Lock removable_storage_lock_; 132 base::OneShotTimer<PowerMonitor> delayed_battery_check_;
229 // Map of all the attached removable storage devices. 133 #endif
230 RemovableStorageMap removable_storage_map_;
231 134
232 DISALLOW_COPY_AND_ASSIGN(SystemMonitor); 135 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_;
136 bool battery_in_use_;
137 bool suspended_;
138
139 // True means the Signaler will be returned via GetSignalerOnce, otherwise
140 // NULL will be returned via GetSignalerOnce.
141 bool is_signaler_available_;
vandebo (ex-Chrome) 2012/10/31 17:30:34 The style guide says that variable should be nouns
Hongbo Min 2012/11/01 09:26:24 Done.
142
143 DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
233 }; 144 };
234 145
235 } // namespace base 146 } // namespace base
236 147
237 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ 148 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698