| 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 #include "base/power_monitor/power_monitor.h" | 5 #include "base/power_monitor/power_monitor.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/power_monitor/power_monitor_device_source.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 static PowerMonitor* g_power_monitor = NULL; | 11 static PowerMonitor* g_power_monitor = NULL; |
| 12 | 12 |
| 13 #if defined(ENABLE_BATTERY_MONITORING) | 13 // This constructor is provided to preserve backwards compatibility with the |
| 14 // The amount of time (in ms) to wait before running the initial | 14 // most common use case, where the power monitor is created on a UI thread to |
| 15 // battery check. | 15 // directly monitor system power events. |
| 16 static int kDelayedBatteryCheckMs = 10 * 1000; | 16 // |
| 17 #endif // defined(ENABLE_BATTERY_MONITORING) | 17 // TODO(bajones): Update all instances of PowerMonitor to pass in an explicit |
| 18 | 18 // new PowerMonitorDeviceSource() |
| 19 PowerMonitor::PowerMonitor() | 19 PowerMonitor::PowerMonitor() |
| 20 : observers_(new ObserverListThreadSafe<PowerObserver>()), | 20 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 21 battery_in_use_(false), | 21 source_(new PowerMonitorDeviceSource()) { |
| 22 suspended_(false) { | |
| 23 DCHECK(!g_power_monitor); | 22 DCHECK(!g_power_monitor); |
| 24 g_power_monitor = this; | 23 g_power_monitor = this; |
| 24 } |
| 25 | 25 |
| 26 DCHECK(MessageLoop::current()); | 26 PowerMonitor::PowerMonitor(PowerMonitorSource* source) |
| 27 #if defined(ENABLE_BATTERY_MONITORING) | 27 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 28 delayed_battery_check_.Start(FROM_HERE, | 28 source_(source) { |
| 29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 29 DCHECK(!g_power_monitor); |
| 30 &PowerMonitor::BatteryCheck); | 30 g_power_monitor = this; |
| 31 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 32 #if defined(OS_MACOSX) | |
| 33 PlatformInit(); | |
| 34 #endif | |
| 35 } | 31 } |
| 36 | 32 |
| 37 PowerMonitor::~PowerMonitor() { | 33 PowerMonitor::~PowerMonitor() { |
| 38 #if defined(OS_MACOSX) | |
| 39 PlatformDestroy(); | |
| 40 #endif | |
| 41 DCHECK_EQ(this, g_power_monitor); | 34 DCHECK_EQ(this, g_power_monitor); |
| 42 g_power_monitor = NULL; | 35 g_power_monitor = NULL; |
| 43 } | 36 } |
| 44 | 37 |
| 45 // static | 38 // static |
| 46 PowerMonitor* PowerMonitor::Get() { | 39 PowerMonitor* PowerMonitor::Get() { |
| 47 return g_power_monitor; | 40 return g_power_monitor; |
| 48 } | 41 } |
| 49 | 42 |
| 50 void PowerMonitor::AddObserver(PowerObserver* obs) { | 43 void PowerMonitor::AddObserver(PowerObserver* obs) { |
| 51 observers_->AddObserver(obs); | 44 observers_->AddObserver(obs); |
| 52 } | 45 } |
| 53 | 46 |
| 54 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | 47 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
| 55 observers_->RemoveObserver(obs); | 48 observers_->RemoveObserver(obs); |
| 56 } | 49 } |
| 57 | 50 |
| 58 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | 51 PowerMonitorSource* PowerMonitor::Source() { |
| 59 // Suppress duplicate notifications. Some platforms may | 52 return source_.get(); |
| 60 // send multiple notifications of the same event. | |
| 61 switch (event_id) { | |
| 62 case POWER_STATE_EVENT: | |
| 63 { | |
| 64 bool on_battery = IsBatteryPower(); | |
| 65 if (on_battery != battery_in_use_) { | |
| 66 battery_in_use_ = on_battery; | |
| 67 NotifyPowerStateChange(); | |
| 68 } | |
| 69 } | |
| 70 break; | |
| 71 case RESUME_EVENT: | |
| 72 if (suspended_) { | |
| 73 suspended_ = false; | |
| 74 NotifyResume(); | |
| 75 } | |
| 76 break; | |
| 77 case SUSPEND_EVENT: | |
| 78 if (!suspended_) { | |
| 79 suspended_ = true; | |
| 80 NotifySuspend(); | |
| 81 } | |
| 82 break; | |
| 83 } | |
| 84 } | 53 } |
| 85 | 54 |
| 86 void PowerMonitor::BatteryCheck() { | 55 bool PowerMonitor::IsOnBatteryPower() { |
| 87 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | 56 return source_->IsOnBatteryPower(); |
| 88 } | 57 } |
| 89 | 58 |
| 90 void PowerMonitor::NotifyPowerStateChange() { | 59 void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { |
| 91 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 60 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") |
| 92 << " battery"; | 61 << " battery"; |
| 93 observers_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | 62 observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use); |
| 94 } | 63 } |
| 95 | 64 |
| 96 void PowerMonitor::NotifySuspend() { | 65 void PowerMonitor::NotifySuspend() { |
| 97 DVLOG(1) << "Power Suspending"; | 66 DVLOG(1) << "Power Suspending"; |
| 98 observers_->Notify(&PowerObserver::OnSuspend); | 67 observers_->Notify(&PowerObserver::OnSuspend); |
| 99 } | 68 } |
| 100 | 69 |
| 101 void PowerMonitor::NotifyResume() { | 70 void PowerMonitor::NotifyResume() { |
| 102 DVLOG(1) << "Power Resuming"; | 71 DVLOG(1) << "Power Resuming"; |
| 103 observers_->Notify(&PowerObserver::OnResume); | 72 observers_->Notify(&PowerObserver::OnResume); |
| 104 } | 73 } |
| 105 | 74 |
| 106 } // namespace base | 75 } // namespace base |
| OLD | NEW |