OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/power_monitor/power_monitor.h" |
| 6 |
| 7 namespace base { |
| 8 |
| 9 #if defined(ENABLE_BATTERY_MONITORING) |
| 10 // The amount of time (in ms) to wait before running the initial |
| 11 // battery check. |
| 12 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 13 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 14 |
| 15 PowerMonitor::PowerMonitor() |
| 16 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
| 17 battery_in_use_(false), |
| 18 suspended_(false) { |
| 19 DCHECK(MessageLoop::current()); |
| 20 #if defined(ENABLE_BATTERY_MONITORING) |
| 21 delayed_battery_check_.Start(FROM_HERE, |
| 22 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 23 &PowerMonitor::BatteryCheck); |
| 24 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 25 |
| 26 #if defined(OS_MACOSX) |
| 27 PlatformInit(); |
| 28 #endif |
| 29 } |
| 30 |
| 31 PowerMonitor::~PowerMonitor() { |
| 32 #if defined(OS_MACOSX) |
| 33 PlatformDestroy(); |
| 34 #endif |
| 35 } |
| 36 |
| 37 // static |
| 38 PowerMonitor* PowerMonitor::GetInstance() { |
| 39 return Singleton<PowerMonitor>::get(); |
| 40 } |
| 41 |
| 42 void PowerMonitor::AddObserver(PowerObserver* obs) { |
| 43 observers_->AddObserver(obs); |
| 44 } |
| 45 |
| 46 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
| 47 observers_->RemoveObserver(obs); |
| 48 } |
| 49 |
| 50 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { |
| 51 // Suppress duplicate notifications. Some platforms may |
| 52 // send multiple notifications of the same event. |
| 53 switch (event_id) { |
| 54 case POWER_STATE_EVENT: |
| 55 { |
| 56 bool on_battery = IsBatteryPower(); |
| 57 if (on_battery != battery_in_use_) { |
| 58 battery_in_use_ = on_battery; |
| 59 NotifyPowerStateChange(); |
| 60 } |
| 61 } |
| 62 break; |
| 63 case RESUME_EVENT: |
| 64 if (suspended_) { |
| 65 suspended_ = false; |
| 66 NotifyResume(); |
| 67 } |
| 68 break; |
| 69 case SUSPEND_EVENT: |
| 70 if (!suspended_) { |
| 71 suspended_ = true; |
| 72 NotifySuspend(); |
| 73 } |
| 74 break; |
| 75 } |
| 76 } |
| 77 |
| 78 void PowerMonitor::NotifyPowerStateChange() { |
| 79 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| 80 << " battery"; |
| 81 observers_->Notify(&PowerObserver::OnPowerStateChange, |
| 82 BatteryPower()); |
| 83 } |
| 84 |
| 85 void PowerMonitor::NotifySuspend() { |
| 86 DVLOG(1) << "Power Suspending"; |
| 87 observers_->Notify(&PowerObserver::OnSuspend); |
| 88 } |
| 89 |
| 90 void PowerMonitor::NotifyResume() { |
| 91 DVLOG(1) << "Power Resuming"; |
| 92 observers_->Notify(&PowerObserver::OnResume); |
| 93 } |
| 94 |
| 95 void PowerMonitor::BatteryCheck() { |
| 96 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| 97 } |
| 98 |
| 99 } // namespace base |
| 100 |
OLD | NEW |