Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 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_POWER_MONITOR_POWER_MONITOR_H_ | 5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_H_ |
| 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ | 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/observer_list_threadsafe.h" | 11 #include "base/observer_list_threadsafe.h" |
| 12 #include "base/power_monitor/power_observer.h" | 12 #include "base/power_monitor/power_observer.h" |
| 13 | 13 |
| 14 #if defined(OS_WIN) | 14 namespace base { |
| 15 #include <windows.h> | |
| 16 | 15 |
| 17 // Windows HiRes timers drain the battery faster so we need to know the battery | 16 class PowerMonitorSource; |
| 18 // status. This isn't true for other platforms. | |
| 19 #define ENABLE_BATTERY_MONITORING 1 | |
| 20 #else | |
| 21 #undef ENABLE_BATTERY_MONITORING | |
| 22 #endif // !OS_WIN | |
| 23 | |
| 24 #if defined(ENABLE_BATTERY_MONITORING) | |
| 25 #include "base/timer.h" | |
| 26 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 27 | |
| 28 #if defined(OS_IOS) | |
| 29 #include <objc/runtime.h> | |
| 30 #endif // OS_IOS | |
| 31 | |
| 32 namespace base { | |
| 33 | 17 |
| 34 // A class used to monitor the power state change and notify the observers about | 18 // A class used to monitor the power state change and notify the observers about |
| 35 // the change event. | 19 // the change event. |
| 36 class BASE_EXPORT PowerMonitor { | 20 class BASE_EXPORT PowerMonitor { |
| 37 public: | 21 public: |
| 38 // Normalized list of power events. | |
| 39 enum PowerEvent { | |
| 40 POWER_STATE_EVENT, // The Power status of the system has changed. | |
| 41 SUSPEND_EVENT, // The system is being suspended. | |
| 42 RESUME_EVENT // The system is being resumed. | |
| 43 }; | |
| 44 | |
| 45 PowerMonitor(); | 22 PowerMonitor(); |
| 23 PowerMonitor(PowerMonitorSource* source); | |
|
apatrick_chromium
2013/07/01 23:59:58
This constructor should be explicit because it tak
| |
| 46 ~PowerMonitor(); | 24 ~PowerMonitor(); |
| 47 | 25 |
| 48 // Get the application-wide PowerMonitor (if not present, returns NULL). | 26 // Get the process-wide PowerMonitor (if not present, returns NULL). |
| 49 static PowerMonitor* Get(); | 27 static PowerMonitor* Get(); |
| 50 | 28 |
| 51 #if defined(OS_MACOSX) | |
| 52 // Allocate system resources needed by the PowerMonitor class. | |
| 53 // | |
| 54 // This function must be called before instantiating an instance of the class | |
| 55 // and before the Sandbox is initialized. | |
| 56 #if !defined(OS_IOS) | |
| 57 static void AllocateSystemIOPorts(); | |
| 58 #else | |
| 59 static void AllocateSystemIOPorts() {} | |
| 60 #endif // OS_IOS | |
| 61 #endif // OS_MACOSX | |
| 62 | |
| 63 // Add and remove an observer. | 29 // Add and remove an observer. |
| 64 // Can be called from any thread. | 30 // Can be called from any thread. |
| 65 // Must not be called from within a notification callback. | 31 // Must not be called from within a notification callback. |
| 66 void AddObserver(PowerObserver* observer); | 32 void AddObserver(PowerObserver* observer); |
| 67 void RemoveObserver(PowerObserver* observer); | 33 void RemoveObserver(PowerObserver* observer); |
| 68 | 34 |
| 69 // Is the computer currently on battery power. Can be called on any thread. | 35 PowerMonitorSource* Source(); |
| 70 bool BatteryPower() const { | |
| 71 // Using a lock here is not necessary for just a bool. | |
| 72 return battery_in_use_; | |
| 73 } | |
| 74 | 36 |
| 75 private: | 37 private: |
| 76 friend class PowerMonitorTest; | 38 friend class PowerMonitorSource; |
| 77 // A friend function that is allowed to access the private ProcessPowerEvent. | |
| 78 friend void ProcessPowerEventHelper(PowerEvent); | |
| 79 | 39 |
| 80 #if defined(OS_WIN) | 40 void NotifyPowerStateChange(bool battery_in_use); |
| 81 // Represents a message-only window for power message handling on Windows. | |
| 82 // Only allow PowerMonitor to create it. | |
| 83 class PowerMessageWindow { | |
| 84 public: | |
| 85 PowerMessageWindow(); | |
| 86 ~PowerMessageWindow(); | |
| 87 | |
| 88 private: | |
| 89 void ProcessWmPowerBroadcastMessage(int event_id); | |
| 90 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, | |
| 91 WPARAM wparam, LPARAM lparam); | |
| 92 static LRESULT CALLBACK WndProcThunk(HWND hwnd, | |
| 93 UINT message, | |
| 94 WPARAM wparam, | |
| 95 LPARAM lparam); | |
| 96 // Instance of the module containing the window procedure. | |
| 97 HMODULE instance_; | |
| 98 // A hidden message-only window. | |
| 99 HWND message_hwnd_; | |
| 100 }; | |
| 101 #endif // OS_WIN | |
| 102 | |
| 103 #if defined(OS_MACOSX) | |
| 104 void PlatformInit(); | |
| 105 void PlatformDestroy(); | |
| 106 #endif | |
| 107 | |
| 108 // Cross-platform handling of a power event. | |
| 109 void ProcessPowerEvent(PowerEvent event_id); | |
| 110 | |
| 111 // Platform-specific method to check whether the system is currently | |
| 112 // running on battery power. Returns true if running on batteries, | |
| 113 // false otherwise. | |
| 114 bool IsBatteryPower(); | |
| 115 | |
| 116 // Checks the battery status and notifies observers if the battery | |
| 117 // status has changed. | |
| 118 void BatteryCheck(); | |
| 119 | |
| 120 void NotifyPowerStateChange(); | |
| 121 void NotifySuspend(); | 41 void NotifySuspend(); |
| 122 void NotifyResume(); | 42 void NotifyResume(); |
| 123 | 43 |
| 124 #if defined(OS_IOS) | |
| 125 // Holds pointers to system event notification observers. | |
| 126 std::vector<id> notification_observers_; | |
| 127 #endif | |
| 128 | |
| 129 #if defined(ENABLE_BATTERY_MONITORING) | |
| 130 base::OneShotTimer<PowerMonitor> delayed_battery_check_; | |
| 131 #endif | |
| 132 | |
| 133 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; | 44 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; |
| 134 bool battery_in_use_; | 45 scoped_ptr<PowerMonitorSource> source_; |
| 135 bool suspended_; | |
| 136 | |
| 137 #if defined(OS_WIN) | |
| 138 PowerMessageWindow power_message_window_; | |
| 139 #endif | |
| 140 | 46 |
| 141 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | 47 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); |
| 142 }; | 48 }; |
| 143 | 49 |
| 144 } // namespace base | 50 } // namespace base |
| 145 | 51 |
| 146 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ | 52 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ |
| OLD | NEW |