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 3 // found in the LICENSE file. 4 5 #include "base/power_monitor/power_monitor.h" 6 7 #include <utility> 8 9 #include "base/power_monitor/power_monitor_source.h" 10 11 namespace base { 12 13 static PowerMonitor* g_power_monitor = nullptr; 14 PowerMonitor(std::unique_ptr<PowerMonitorSource> source)15PowerMonitor::PowerMonitor(std::unique_ptr<PowerMonitorSource> source) 16 : observers_(new ObserverListThreadSafe<PowerObserver>()), 17 source_(std::move(source)) { 18 DCHECK(!g_power_monitor); 19 g_power_monitor = this; 20 } 21 ~PowerMonitor()22PowerMonitor::~PowerMonitor() { 23 source_->Shutdown(); 24 DCHECK_EQ(this, g_power_monitor); 25 g_power_monitor = nullptr; 26 } 27 28 // static Get()29PowerMonitor* PowerMonitor::Get() { 30 return g_power_monitor; 31 } 32 AddObserver(PowerObserver * obs)33void PowerMonitor::AddObserver(PowerObserver* obs) { 34 observers_->AddObserver(obs); 35 } 36 RemoveObserver(PowerObserver * obs)37void PowerMonitor::RemoveObserver(PowerObserver* obs) { 38 observers_->RemoveObserver(obs); 39 } 40 Source()41PowerMonitorSource* PowerMonitor::Source() { 42 return source_.get(); 43 } 44 IsOnBatteryPower()45bool PowerMonitor::IsOnBatteryPower() { 46 return source_->IsOnBatteryPower(); 47 } 48 NotifyPowerStateChange(bool battery_in_use)49void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { 50 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") 51 << " battery"; 52 observers_->Notify(FROM_HERE, &PowerObserver::OnPowerStateChange, 53 battery_in_use); 54 } 55 NotifySuspend()56void PowerMonitor::NotifySuspend() { 57 DVLOG(1) << "Power Suspending"; 58 observers_->Notify(FROM_HERE, &PowerObserver::OnSuspend); 59 } 60 NotifyResume()61void PowerMonitor::NotifyResume() { 62 DVLOG(1) << "Power Resuming"; 63 observers_->Notify(FROM_HERE, &PowerObserver::OnResume); 64 } 65 66 } // namespace base 67