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 notifier_(NULL) { |
| 20 DCHECK(MessageLoop::current()); |
| 21 #if defined(ENABLE_BATTERY_MONITORING) |
| 22 delayed_battery_check_.Start(FROM_HERE, |
| 23 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 24 &PowerMonitor::BatteryCheck); |
| 25 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 26 |
| 27 #if defined(OS_MACOSX) |
| 28 PlatformInit(); |
| 29 #endif |
| 30 } |
| 31 |
| 32 PowerMonitor::~PowerMonitor() { |
| 33 #if defined(OS_MACOSX) |
| 34 PlatformDestroy(); |
| 35 #endif |
| 36 } |
| 37 |
| 38 // static |
| 39 PowerMonitor* PowerMonitor::GetInstance() { |
| 40 // The singleton PowerMonitor instance is designed to be leaked since it is |
| 41 // hard to handle the dependency between PowerMonitor and Notifier. We can not |
| 42 // easily anticipate the life time of Notifier after calling GetNotifierOnce. |
| 43 return Singleton<PowerMonitor, LeakySingletonTraits<PowerMonitor> >::get(); |
| 44 } |
| 45 |
| 46 PowerMonitor::Notifier* PowerMonitor::GetNotifierOnce() { |
| 47 DCHECK(notifier_ == NULL); |
| 48 |
| 49 if (notifier_ == NULL) { |
| 50 // The notifier is allowed to be leaked since the dependency and ownship |
| 51 // between PowerMonitor and Notifier is hard to be handled. |
| 52 notifier_ = new PowerMonitor::Notifier(this); |
| 53 return notifier_; |
| 54 } |
| 55 |
| 56 // Return NULL after the first time it get called. |
| 57 return NULL; |
| 58 } |
| 59 |
| 60 void PowerMonitor::AddObserver(PowerObserver* obs) { |
| 61 observers_->AddObserver(obs); |
| 62 } |
| 63 |
| 64 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
| 65 observers_->RemoveObserver(obs); |
| 66 } |
| 67 |
| 68 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { |
| 69 // Suppress duplicate notifications. Some platforms may |
| 70 // send multiple notifications of the same event. |
| 71 switch (event_id) { |
| 72 case POWER_STATE_EVENT: |
| 73 { |
| 74 bool on_battery = IsBatteryPower(); |
| 75 if (on_battery != battery_in_use_) { |
| 76 battery_in_use_ = on_battery; |
| 77 NotifyPowerStateChange(); |
| 78 } |
| 79 } |
| 80 break; |
| 81 case RESUME_EVENT: |
| 82 if (suspended_) { |
| 83 suspended_ = false; |
| 84 NotifyResume(); |
| 85 } |
| 86 break; |
| 87 case SUSPEND_EVENT: |
| 88 if (!suspended_) { |
| 89 suspended_ = true; |
| 90 NotifySuspend(); |
| 91 } |
| 92 break; |
| 93 } |
| 94 } |
| 95 |
| 96 void PowerMonitor::NotifyPowerStateChange() { |
| 97 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") |
| 98 << " battery"; |
| 99 observers_->Notify(&PowerObserver::OnPowerStateChange, |
| 100 BatteryPower()); |
| 101 } |
| 102 |
| 103 void PowerMonitor::NotifySuspend() { |
| 104 DVLOG(1) << "Power Suspending"; |
| 105 observers_->Notify(&PowerObserver::OnSuspend); |
| 106 } |
| 107 |
| 108 void PowerMonitor::NotifyResume() { |
| 109 DVLOG(1) << "Power Resuming"; |
| 110 observers_->Notify(&PowerObserver::OnResume); |
| 111 } |
| 112 |
| 113 void PowerMonitor::BatteryCheck() { |
| 114 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| 115 } |
| 116 |
| 117 } // namespace base |
| 118 |
OLD | NEW |