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