OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/power_monitor/power_monitor.h" | 5 #include "base/power_monitor/power_monitor.h" |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "base/power_monitor/power_monitor_source.h" | 6 #include "base/power_monitor/power_monitor_source.h" |
9 #include "base/synchronization/lock.h" | |
10 | 7 |
11 namespace base { | 8 namespace base { |
12 | 9 |
13 class PowerMonitorImpl; | 10 static PowerMonitor* g_power_monitor = NULL; |
14 | 11 |
15 LazyInstance<Lock>::Leaky g_power_monitor_lock = LAZY_INSTANCE_INITIALIZER; | 12 PowerMonitor::PowerMonitor(scoped_ptr<PowerMonitorSource> source) |
16 static PowerMonitorImpl* g_power_monitor = NULL; | 13 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
17 | 14 source_(source.Pass()) { |
18 // A class used to monitor the power state change and notify the observers about | |
19 // the change event. | |
20 class BASE_EXPORT PowerMonitorImpl { | |
21 public: | |
22 explicit PowerMonitorImpl(scoped_ptr<PowerMonitorSource> source) | |
23 : observers_(new ObserverListThreadSafe<PowerObserver>()), | |
24 source_(source.Pass()) { } | |
25 | |
26 ~PowerMonitorImpl() { } | |
27 | |
28 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_; | |
29 scoped_ptr<PowerMonitorSource> source_; | |
30 | |
31 DISALLOW_COPY_AND_ASSIGN(PowerMonitorImpl); | |
32 }; | |
33 | |
34 void PowerMonitor::Initialize(scoped_ptr<PowerMonitorSource> source) { | |
35 AutoLock(g_power_monitor_lock.Get()); | |
36 DCHECK(!g_power_monitor); | 15 DCHECK(!g_power_monitor); |
37 g_power_monitor = new PowerMonitorImpl(source.Pass()); | 16 g_power_monitor = this; |
38 } | 17 } |
39 | 18 |
40 void PowerMonitor::ShutdownForTesting() { | 19 PowerMonitor::~PowerMonitor() { |
41 AutoLock(g_power_monitor_lock.Get()); | 20 DCHECK_EQ(this, g_power_monitor); |
42 if (g_power_monitor) { | 21 g_power_monitor = NULL; |
43 delete g_power_monitor; | |
44 g_power_monitor = NULL; | |
45 } | |
46 } | 22 } |
47 | 23 |
48 bool PowerMonitor::IsInitialized() { | 24 // static |
49 AutoLock(g_power_monitor_lock.Get()); | 25 PowerMonitor* PowerMonitor::Get() { |
50 return g_power_monitor != NULL; | 26 return g_power_monitor; |
51 } | 27 } |
52 | 28 |
53 bool PowerMonitor::AddObserver(PowerObserver* observer) { | 29 void PowerMonitor::AddObserver(PowerObserver* obs) { |
54 AutoLock(g_power_monitor_lock.Get()); | 30 observers_->AddObserver(obs); |
55 if (!g_power_monitor) | |
56 return false; | |
57 g_power_monitor->observers_->AddObserver(observer); | |
58 return true; | |
59 } | 31 } |
60 | 32 |
61 bool PowerMonitor::RemoveObserver(PowerObserver* observer) { | 33 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
62 AutoLock(g_power_monitor_lock.Get()); | 34 observers_->RemoveObserver(obs); |
63 if (!g_power_monitor) | 35 } |
64 return false; | 36 |
65 g_power_monitor->observers_->RemoveObserver(observer); | 37 PowerMonitorSource* PowerMonitor::Source() { |
66 return true; | 38 return source_.get(); |
67 } | 39 } |
68 | 40 |
69 bool PowerMonitor::IsOnBatteryPower() { | 41 bool PowerMonitor::IsOnBatteryPower() { |
70 g_power_monitor_lock.Get().AssertAcquired(); | 42 return source_->IsOnBatteryPower(); |
71 if (!g_power_monitor) | |
72 return false; | |
73 return g_power_monitor->source_->IsOnBatteryPower(); | |
74 } | |
75 | |
76 Lock* PowerMonitor::GetLock() { | |
77 return &g_power_monitor_lock.Get(); | |
78 } | |
79 | |
80 bool PowerMonitor::IsInitializedLocked() { | |
81 g_power_monitor_lock.Get().AssertAcquired(); | |
82 return g_power_monitor != NULL; | |
83 } | |
84 | |
85 PowerMonitorSource* PowerMonitor::GetSource() { | |
86 g_power_monitor_lock.Get().AssertAcquired(); | |
87 return g_power_monitor->source_.get(); | |
88 } | 43 } |
89 | 44 |
90 void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { | 45 void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { |
91 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") | 46 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") |
92 << " battery"; | 47 << " battery"; |
93 g_power_monitor_lock.Get().AssertAcquired(); | 48 observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use); |
94 g_power_monitor->observers_->Notify(&PowerObserver::OnPowerStateChange, | |
95 battery_in_use); | |
96 } | 49 } |
97 | 50 |
98 void PowerMonitor::NotifySuspend() { | 51 void PowerMonitor::NotifySuspend() { |
99 DVLOG(1) << "Power Suspending"; | 52 DVLOG(1) << "Power Suspending"; |
100 g_power_monitor_lock.Get().AssertAcquired(); | 53 observers_->Notify(&PowerObserver::OnSuspend); |
101 g_power_monitor->observers_->Notify(&PowerObserver::OnSuspend); | |
102 } | 54 } |
103 | 55 |
104 void PowerMonitor::NotifyResume() { | 56 void PowerMonitor::NotifyResume() { |
105 DVLOG(1) << "Power Resuming"; | 57 DVLOG(1) << "Power Resuming"; |
106 g_power_monitor_lock.Get().AssertAcquired(); | 58 observers_->Notify(&PowerObserver::OnResume); |
107 g_power_monitor->observers_->Notify(&PowerObserver::OnResume); | |
108 } | 59 } |
109 | 60 |
110 } // namespace base | 61 } // namespace base |
OLD | NEW |