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 static PowerMonitor* g_power_monitor = NULL; |
| 10 |
| 11 #if defined(ENABLE_BATTERY_MONITORING) |
| 12 // The amount of time (in ms) to wait before running the initial |
| 13 // battery check. |
| 14 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 15 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 16 |
| 17 PowerMonitor::PowerMonitor() |
| 18 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), |
| 19 battery_in_use_(false), |
| 20 suspended_(false) { |
| 21 DCHECK(!g_power_monitor); |
| 22 g_power_monitor = this; |
| 23 |
| 24 DCHECK(MessageLoop::current()); |
| 25 #if defined(ENABLE_BATTERY_MONITORING) |
| 26 delayed_battery_check_.Start(FROM_HERE, |
| 27 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 28 &PowerMonitor::BatteryCheck); |
| 29 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 30 |
| 31 #if defined(OS_MACOSX) |
| 32 PlatformInit(); |
| 33 #endif |
| 34 } |
| 35 |
| 36 PowerMonitor::~PowerMonitor() { |
| 37 #if defined(OS_MACOSX) |
| 38 PlaformDestory(); |
| 39 #endif |
| 40 DCHECK_EQ(this, g_power_monitor); |
| 41 g_power_monitor = NULL; |
| 42 } |
| 43 |
| 44 void PowerMonitor::AddPowerObserver(PowerObserver* obs) { |
| 45 power_observer_list_->AddObserver(obs); |
| 46 } |
| 47 |
| 48 void PowerMonitor::RemovePowerObserver(PowerObserver* obs) { |
| 49 power_observer_list_->RemoveObserver(obs); |
| 50 } |
| 51 |
| 52 void PowerMonitor::HandlePowerEvent(PowerEvent event_id) { |
| 53 // Suppress duplicate notifications. Some platforms may |
| 54 // send multiple notifications of the same event. |
| 55 switch (event_id) { |
| 56 case POWER_STATE_EVENT: |
| 57 { |
| 58 bool on_battery = IsBatteryPower(); |
| 59 if (on_battery != battery_in_use_) { |
| 60 battery_in_use_ = on_battery; |
| 61 NotifyPowerStateChange(); |
| 62 } |
| 63 } |
| 64 break; |
| 65 case RESUME_EVENT: |
| 66 if (suspended_) { |
| 67 suspended_ = false; |
| 68 NotifyResume(); |
| 69 } |
| 70 break; |
| 71 case SUSPEND_EVENT: |
| 72 if (!suspended_) { |
| 73 suspended_ = true; |
| 74 NotifySuspend(); |
| 75 } |
| 76 break; |
| 77 } |
| 78 } |
| 79 |
| 80 void PowerMonitor::NotifyPowerStateChange() { |
| 81 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| 82 << " battery"; |
| 83 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, |
| 84 BatteryPower()); |
| 85 } |
| 86 |
| 87 void PowerMonitor::NotifySuspend() { |
| 88 DVLOG(1) << "Power Suspending"; |
| 89 power_observer_list_->Notify(&PowerObserver::OnSuspend); |
| 90 } |
| 91 |
| 92 void PowerMonitor::NotifyResume() { |
| 93 DVLOG(1) << "Power Resuming"; |
| 94 power_observer_list_->Notify(&PowerObserver::OnResume); |
| 95 } |
| 96 |
| 97 void PowerMonitor::BatteryCheck() { |
| 98 HandlePowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| 99 } |
| 100 |
| 101 } // namespace base |
| 102 |
OLD | NEW |