Chromium Code Reviews| Index: base/power_monitor/power_monitor.cc |
| diff --git a/base/power_monitor/power_monitor.cc b/base/power_monitor/power_monitor.cc |
| index 87188f1c591eb1c6a4bdf38bacc4125f045b91bd..7bb1f473a747fc7ed440ef1f0fbd32605d1ec005 100644 |
| --- a/base/power_monitor/power_monitor.cc |
| +++ b/base/power_monitor/power_monitor.cc |
| @@ -4,40 +4,33 @@ |
| #include "base/power_monitor/power_monitor.h" |
| -#include "base/time/time.h" |
| +#include "base/power_monitor/power_monitor_device_source.h" |
| namespace base { |
| static PowerMonitor* g_power_monitor = NULL; |
| -#if defined(ENABLE_BATTERY_MONITORING) |
| -// The amount of time (in ms) to wait before running the initial |
| -// battery check. |
| -static int kDelayedBatteryCheckMs = 10 * 1000; |
| -#endif // defined(ENABLE_BATTERY_MONITORING) |
| - |
| +// This constructor is provided to preserve backwards compatibility with the |
| +// most common use case, where the power monitor is created on a UI thread to |
| +// directly monitor system power events. |
| +// |
| +// TODO(bajones): Update all instances of PowerMonitor to pass in an explicit |
| +// new PowerMonitorDeviceSource() |
|
palmer
2013/07/09 21:16:10
Maybe this is the real fix, rather than DCHECKing
vandebo (ex-Chrome)
2013/07/09 22:22:08
All that will happen if the 0-arg constructor is u
bajones
2013/07/09 22:28:00
If used on a non-browser process then they most li
|
| PowerMonitor::PowerMonitor() |
| : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| - battery_in_use_(false), |
| - suspended_(false) { |
| + source_(new PowerMonitorDeviceSource()) { |
| DCHECK(!g_power_monitor); |
| g_power_monitor = this; |
| +} |
| - DCHECK(MessageLoop::current()); |
| -#if defined(ENABLE_BATTERY_MONITORING) |
| - delayed_battery_check_.Start(FROM_HERE, |
| - base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| - &PowerMonitor::BatteryCheck); |
| -#endif // defined(ENABLE_BATTERY_MONITORING) |
| -#if defined(OS_MACOSX) |
| - PlatformInit(); |
| -#endif |
| +PowerMonitor::PowerMonitor(PowerMonitorSource* source) |
| + : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| + source_(source) { |
| + DCHECK(!g_power_monitor); |
| + g_power_monitor = this; |
| } |
| PowerMonitor::~PowerMonitor() { |
| -#if defined(OS_MACOSX) |
| - PlatformDestroy(); |
| -#endif |
| DCHECK_EQ(this, g_power_monitor); |
| g_power_monitor = NULL; |
| } |
| @@ -55,42 +48,18 @@ void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
| observers_->RemoveObserver(obs); |
| } |
| -void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { |
| - // Suppress duplicate notifications. Some platforms may |
| - // send multiple notifications of the same event. |
| - switch (event_id) { |
| - case POWER_STATE_EVENT: |
| - { |
| - bool on_battery = IsBatteryPower(); |
| - if (on_battery != battery_in_use_) { |
| - battery_in_use_ = on_battery; |
| - NotifyPowerStateChange(); |
| - } |
| - } |
| - break; |
| - case RESUME_EVENT: |
| - if (suspended_) { |
| - suspended_ = false; |
| - NotifyResume(); |
| - } |
| - break; |
| - case SUSPEND_EVENT: |
| - if (!suspended_) { |
| - suspended_ = true; |
| - NotifySuspend(); |
| - } |
| - break; |
| - } |
| +PowerMonitorSource* PowerMonitor::Source() { |
| + return source_.get(); |
| } |
| -void PowerMonitor::BatteryCheck() { |
| - ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| +bool PowerMonitor::BatteryPower() { |
| + return source_->BatteryPower(); |
| } |
| -void PowerMonitor::NotifyPowerStateChange() { |
| - DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| +void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { |
| + DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") |
| << " battery"; |
| - observers_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); |
| + observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use); |
| } |
| void PowerMonitor::NotifySuspend() { |