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
8namespace base {
9
10static PowerMonitor* g_power_monitor = NULL;
11
12PowerMonitor::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
19PowerMonitor::~PowerMonitor() {
20  DCHECK_EQ(this, g_power_monitor);
21  g_power_monitor = NULL;
22}
23
24// static
25PowerMonitor* PowerMonitor::Get() {
26  return g_power_monitor;
27}
28
29void PowerMonitor::AddObserver(PowerObserver* obs) {
30  observers_->AddObserver(obs);
31}
32
33void PowerMonitor::RemoveObserver(PowerObserver* obs) {
34  observers_->RemoveObserver(obs);
35}
36
37PowerMonitorSource* PowerMonitor::Source() {
38  return source_.get();
39}
40
41bool PowerMonitor::IsOnBatteryPower() {
42  return source_->IsOnBatteryPower();
43}
44
45void 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
51void PowerMonitor::NotifySuspend() {
52  DVLOG(1) << "Power Suspending";
53  observers_->Notify(&PowerObserver::OnSuspend);
54}
55
56void PowerMonitor::NotifyResume() {
57  DVLOG(1) << "Power Resuming";
58  observers_->Notify(&PowerObserver::OnResume);
59}
60
61}  // namespace base
62