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_source.h" | 5 #include "base/power_monitor/power_monitor_source.h" |
6 | 6 |
7 #include "base/power_monitor/power_monitor.h" | 7 #include "base/power_monitor/power_monitor.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 PowerMonitorSource::PowerMonitorSource() | 11 PowerMonitorSource::PowerMonitorSource() |
12 : on_battery_power_(false), | 12 : on_battery_power_(false), |
13 suspended_(false) { | 13 suspended_(false) { |
14 } | 14 } |
15 | 15 |
16 PowerMonitorSource::~PowerMonitorSource() { | 16 PowerMonitorSource::~PowerMonitorSource() { |
17 } | 17 } |
18 | 18 |
19 bool PowerMonitorSource::IsOnBatteryPower() { | 19 bool PowerMonitorSource::IsOnBatteryPower() { |
20 AutoLock auto_lock(battery_lock_); | 20 AutoLock auto_lock(battery_lock_); |
21 return on_battery_power_; | 21 return on_battery_power_; |
22 } | 22 } |
23 | 23 |
24 void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { | 24 void PowerMonitorSource::ProcessPowerEvent(PowerEvent event_id) { |
25 AutoLock lock(*PowerMonitor::GetLock()); | 25 PowerMonitor* monitor = PowerMonitor::Get(); |
26 if (!PowerMonitor::IsInitializedLocked()) | 26 if (!monitor) |
27 return; | 27 return; |
28 | 28 |
29 PowerMonitorSource* source = PowerMonitor::GetSource(); | 29 PowerMonitorSource* source = monitor->Source(); |
30 | 30 |
31 // Suppress duplicate notifications. Some platforms may | 31 // Suppress duplicate notifications. Some platforms may |
32 // send multiple notifications of the same event. | 32 // send multiple notifications of the same event. |
33 switch (event_id) { | 33 switch (event_id) { |
34 case POWER_STATE_EVENT: | 34 case POWER_STATE_EVENT: |
35 { | 35 { |
36 bool new_on_battery_power = source->IsOnBatteryPowerImpl(); | 36 bool new_on_battery_power = source->IsOnBatteryPowerImpl(); |
37 bool changed = false; | 37 bool changed = false; |
38 | 38 |
39 { | 39 { |
40 AutoLock auto_lock(source->battery_lock_); | 40 AutoLock auto_lock(source->battery_lock_); |
41 if (source->on_battery_power_ != new_on_battery_power) { | 41 if (source->on_battery_power_ != new_on_battery_power) { |
42 changed = true; | 42 changed = true; |
43 source->on_battery_power_ = new_on_battery_power; | 43 source->on_battery_power_ = new_on_battery_power; |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 if (changed) | 47 if (changed) |
48 PowerMonitor::NotifyPowerStateChange(new_on_battery_power); | 48 monitor->NotifyPowerStateChange(new_on_battery_power); |
49 } | 49 } |
50 break; | 50 break; |
51 case RESUME_EVENT: | 51 case RESUME_EVENT: |
52 if (source->suspended_) { | 52 if (source->suspended_) { |
53 source->suspended_ = false; | 53 source->suspended_ = false; |
54 PowerMonitor::NotifyResume(); | 54 monitor->NotifyResume(); |
55 } | 55 } |
56 break; | 56 break; |
57 case SUSPEND_EVENT: | 57 case SUSPEND_EVENT: |
58 if (!source->suspended_) { | 58 if (!source->suspended_) { |
59 source->suspended_ = true; | 59 source->suspended_ = true; |
60 PowerMonitor::NotifySuspend(); | 60 monitor->NotifySuspend(); |
61 } | 61 } |
62 break; | 62 break; |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 } // namespace base | 66 } // namespace base |
OLD | NEW |