Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
It'd be nice to have this file tracked as a move.
Hongbo Min
2012/10/11 07:57:53
Done. git cl upload have an option "--similarity"
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_POWER_MONITOR_POWER_MONITOR_H_ | |
| 6 #define BASE_POWER_MONITOR_POWER_MONITOR_H_ | |
| 7 | |
| 8 #include "base/base_export.h" | |
| 9 #include "base/basictypes.h" | |
| 10 | |
| 11 // Windows HiRes timers drain the battery faster so we need to know the battery | |
| 12 // status. This isn't true for other platforms. | |
| 13 #if defined(OS_WIN) | |
| 14 #define ENABLE_BATTERY_MONITORING 1 | |
| 15 #else | |
| 16 #undef ENABLE_BATTERY_MONITORING | |
| 17 #endif // !OS_WIN | |
| 18 | |
| 19 #include "base/observer_list_threadsafe.h" | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Move this to line 10
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 20 #if defined(ENABLE_BATTERY_MONITORING) | |
| 21 #include "base/timer.h" | |
| 22 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 23 | |
| 24 #if defined(OS_IOS) | |
| 25 #include <objc/runtime.h> | |
| 26 #endif // OS_IOS | |
| 27 | |
| 28 namespace base { | |
| 29 | |
| 30 class BASE_EXPORT PowerMonitor { | |
| 31 public: | |
| 32 // Normalized list of power events. | |
| 33 enum PowerEvent { | |
| 34 POWER_STATE_EVENT, // The Power status of the system has changed. | |
| 35 SUSPEND_EVENT, // The system is being suspended. | |
| 36 RESUME_EVENT // The system is being resumed. | |
| 37 }; | |
| 38 | |
| 39 PowerMonitor(); | |
| 40 ~PowerMonitor(); | |
| 41 | |
| 42 // Callbacks will be called on the thread which creates the SystemMonitor. | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
SystemMonitor -> PowerMonitor
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 43 // During the callback, Add/RemoveObserver will block until the callbacks | |
| 44 // are finished. Observers should implement quick callback functions; if | |
| 45 // lengthy operations are needed, the observer should take care to invoke | |
| 46 // the operation on an appropriate thread. | |
| 47 class BASE_EXPORT PowerObserver { | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
per willchan's comment, move this to the top of th
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 48 public: | |
| 49 // Notification of a change in power status of the computer, such | |
| 50 // as from switching between battery and A/C power. | |
| 51 virtual void OnPowerStateChange(bool on_battery_power) {} | |
| 52 | |
| 53 // Notification that the system is suspending. | |
| 54 virtual void OnSuspend() {} | |
| 55 | |
| 56 // Notification that the system is resuming. | |
| 57 virtual void OnResume() {} | |
| 58 | |
| 59 protected: | |
| 60 virtual ~PowerObserver() {} | |
| 61 }; | |
| 62 | |
| 63 void AddPowerObserver(PowerObserver* observer); | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Add a copy of the comment from system_monitor.h:15
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 64 void RemovePowerObserver(PowerObserver* observer); | |
| 65 | |
| 66 // Is the computer currently on battery power. Can be called on any thread. | |
| 67 bool BatteryPower() const { | |
| 68 // Using a lock here is not necessary for just a bool. | |
| 69 return battery_in_use_; | |
| 70 } | |
| 71 | |
| 72 // Return the single instance in the application wide. | |
| 73 static PowerMonitor* Get(); | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
We should use Singleton instead of the custom Get
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 74 | |
| 75 #if defined(OS_MACOSX) | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Move these init type methods to just below the con
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 76 // Allocate system resources needed by the PowerMonitor class. | |
| 77 // | |
| 78 // This function must be called before instantiating an instance of the class | |
| 79 // and before the Sandbox is initialized. | |
| 80 #if !defined(OS_IOS) | |
| 81 static void AllocateSystemIOPorts(); | |
| 82 #else | |
| 83 static void AllocateSystemIOPorts() {} | |
| 84 #endif // OS_IOS | |
| 85 #endif // OS_MACOSX | |
| 86 | |
| 87 private: | |
| 88 friend class HWNDMessageHandler; | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Looks like the Mac implementation needs a friend f
Hongbo Min
2012/10/11 07:57:53
If making a friend function for Mac, it will intro
vandebo (ex-Chrome)
2012/10/11 20:49:57
Part of the point of refactoring is to remove/make
vandebo (ex-Chrome)
2012/10/12 00:15:51
If need be, you can create another function that m
| |
| 89 friend class PowerMonitorTest_PowerNotifications_Test; | |
| 90 | |
| 91 #if defined(OS_MACOSX) | |
| 92 void PlatformInit(); | |
| 93 void PlatformDestroy(); | |
| 94 #endif | |
| 95 | |
| 96 // Handle the power event and notify its observers with the specific state. | |
| 97 void HandlePowerEvent(PowerEvent event_id); | |
| 98 | |
| 99 // Platform-specific method to check whether the system is currently | |
| 100 // running on battery power. Returns true if running on batteries, | |
| 101 // false otherwise. | |
| 102 bool IsBatteryPower(); | |
| 103 | |
| 104 // Checks the battery status and notifies observers if the battery | |
| 105 // status has changed. | |
| 106 void BatteryCheck(); | |
| 107 | |
| 108 void NotifyPowerStateChange(); | |
| 109 void NotifySuspend(); | |
| 110 void NotifyResume(); | |
| 111 | |
| 112 #if defined(OS_IOS) | |
| 113 // Holds pointers to system event notification observers. | |
| 114 std::vector<id> notification_observers_; | |
| 115 #endif | |
| 116 | |
| 117 #if defined(ENABLE_BATTERY_MONITORING) | |
| 118 base::OneShotTimer<PowerMonitor> delayed_battery_check_; | |
| 119 #endif | |
| 120 scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
power_observer_list_ -> observers_
Hongbo Min
2012/10/11 07:57:53
Done.
| |
| 121 bool battery_in_use_; | |
| 122 bool suspended_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(PowerMonitor); | |
| 125 }; | |
| 126 | |
| 127 } // namespace base | |
| 128 | |
| 129 #endif // BASE_POWER_MONITOR_POWER_MONITOR_H_ | |
| OLD | NEW |