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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..923a7ffd19aa0c80319e6911124b025b391131e6 |
| --- /dev/null |
| +++ b/base/power_monitor/power_monitor.cc |
| @@ -0,0 +1,138 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
vandebo (ex-Chrome)
2012/10/30 20:41:24
Can you set similarity so that this is detected as
Hongbo Min
2012/10/31 12:52:50
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/power_monitor/power_monitor.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) |
| + |
| +PowerMonitor::PowerMonitor() |
| + : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| + battery_in_use_(false), |
| + suspended_(false), |
| + is_get_signaler_called_(false) { |
| + 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() { |
| +#if defined(OS_MACOSX) |
| + PlatformDestroy(); |
| +#endif |
| + DCHECK_EQ(this, g_power_monitor); |
| + g_power_monitor = NULL; |
| +} |
| + |
| +// static |
| +PowerMonitor* PowerMonitor::Get() { |
| + return g_power_monitor; |
| +} |
| + |
| +PowerMonitor::Signaler* PowerMonitor::GetSignalerOnce() { |
| + if (!is_get_signaler_called_) { |
| + is_get_signaler_called_ = true; |
| + return new PowerMonitor::Signaler(); |
| + } |
| + |
| + // Return NULL after the first time it get called. |
| + return NULL; |
| +} |
| + |
| +void PowerMonitor::AddObserver(PowerObserver* obs) { |
| + observers_->AddObserver(obs); |
| +} |
| + |
| +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; |
| + } |
| +} |
| + |
| +void PowerMonitor::NotifyPowerStateChange() { |
| + DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| + << " battery"; |
| + observers_->Notify(&PowerObserver::OnPowerStateChange, |
| + BatteryPower()); |
| +} |
| + |
| +void PowerMonitor::NotifySuspend() { |
| + DVLOG(1) << "Power Suspending"; |
| + observers_->Notify(&PowerObserver::OnSuspend); |
| +} |
| + |
| +void PowerMonitor::NotifyResume() { |
| + DVLOG(1) << "Power Resuming"; |
| + observers_->Notify(&PowerObserver::OnResume); |
| +} |
| + |
| +void PowerMonitor::BatteryCheck() { |
| + ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| +} |
| + |
| +// PowerMonitor::Signaler |
| + |
| +PowerMonitor::Signaler::Signaler() { |
| +} |
| + |
| +PowerMonitor::Signaler::~Signaler() { |
| +} |
| + |
| +#if defined(OS_WIN) |
| +void PowerMonitor::Signaler::ProcessWmPowerBroadcastMessage(int event) { |
| + if (PowerMonitor::Get()) |
| + PowerMonitor::Get()->ProcessWmPowerBroadcastMessage(event); |
| +} |
| +#endif |
| + |
| +void PowerMonitor::Signaler::ProcessPowerEvent(PowerEvent event) { |
| + if (PowerMonitor::Get()) |
| + PowerMonitor::Get()->ProcessPowerEvent(event); |
| +} |
| + |
| +} // namespace base |
| + |