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 | 6 #include "base/power_monitor/power_monitor_source.h" |
7 #include "base/time/time.h" | |
8 | 7 |
9 namespace base { | 8 namespace base { |
10 | 9 |
11 static PowerMonitor* g_power_monitor = NULL; | 10 static PowerMonitor* g_power_monitor = NULL; |
12 | 11 |
13 #if defined(ENABLE_BATTERY_MONITORING) | 12 PowerMonitor::PowerMonitor(scoped_ptr<PowerMonitorSource> source) |
14 // The amount of time (in ms) to wait before running the initial | |
15 // battery check. | |
16 static int kDelayedBatteryCheckMs = 10 * 1000; | |
17 #endif // defined(ENABLE_BATTERY_MONITORING) | |
18 | |
19 PowerMonitor::PowerMonitor() | |
20 : observers_(new ObserverListThreadSafe<PowerObserver>()), | 13 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
21 battery_in_use_(false), | 14 source_(source.Pass()) { |
22 suspended_(false) { | |
23 DCHECK(!g_power_monitor); | 15 DCHECK(!g_power_monitor); |
24 g_power_monitor = this; | 16 g_power_monitor = this; |
25 | |
26 DCHECK(MessageLoop::current()); | |
27 #if defined(ENABLE_BATTERY_MONITORING) | |
28 delayed_battery_check_.Start(FROM_HERE, | |
29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | |
30 &PowerMonitor::BatteryCheck); | |
31 #endif // defined(ENABLE_BATTERY_MONITORING) | |
32 #if defined(OS_MACOSX) | |
33 PlatformInit(); | |
34 #endif | |
35 } | 17 } |
36 | 18 |
37 PowerMonitor::~PowerMonitor() { | 19 PowerMonitor::~PowerMonitor() { |
38 #if defined(OS_MACOSX) | |
39 PlatformDestroy(); | |
40 #endif | |
41 DCHECK_EQ(this, g_power_monitor); | 20 DCHECK_EQ(this, g_power_monitor); |
42 g_power_monitor = NULL; | 21 g_power_monitor = NULL; |
43 } | 22 } |
44 | 23 |
45 // static | 24 // static |
46 PowerMonitor* PowerMonitor::Get() { | 25 PowerMonitor* PowerMonitor::Get() { |
47 return g_power_monitor; | 26 return g_power_monitor; |
48 } | 27 } |
49 | 28 |
50 void PowerMonitor::AddObserver(PowerObserver* obs) { | 29 void PowerMonitor::AddObserver(PowerObserver* obs) { |
51 observers_->AddObserver(obs); | 30 observers_->AddObserver(obs); |
52 } | 31 } |
53 | 32 |
54 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | 33 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
55 observers_->RemoveObserver(obs); | 34 observers_->RemoveObserver(obs); |
56 } | 35 } |
57 | 36 |
58 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | 37 PowerMonitorSource* PowerMonitor::Source() { |
59 // Suppress duplicate notifications. Some platforms may | 38 return source_.get(); |
60 // send multiple notifications of the same event. | |
61 switch (event_id) { | |
62 case POWER_STATE_EVENT: | |
63 { | |
64 bool on_battery = IsBatteryPower(); | |
65 if (on_battery != battery_in_use_) { | |
66 battery_in_use_ = on_battery; | |
67 NotifyPowerStateChange(); | |
68 } | |
69 } | |
70 break; | |
71 case RESUME_EVENT: | |
72 if (suspended_) { | |
73 suspended_ = false; | |
74 NotifyResume(); | |
75 } | |
76 break; | |
77 case SUSPEND_EVENT: | |
78 if (!suspended_) { | |
79 suspended_ = true; | |
80 NotifySuspend(); | |
81 } | |
82 break; | |
83 } | |
84 } | 39 } |
85 | 40 |
86 void PowerMonitor::BatteryCheck() { | 41 bool PowerMonitor::IsOnBatteryPower() { |
87 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | 42 return source_->IsOnBatteryPower(); |
88 } | 43 } |
89 | 44 |
90 void PowerMonitor::NotifyPowerStateChange() { | 45 void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { |
91 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | 46 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") |
92 << " battery"; | 47 << " battery"; |
93 observers_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | 48 observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use); |
94 } | 49 } |
95 | 50 |
96 void PowerMonitor::NotifySuspend() { | 51 void PowerMonitor::NotifySuspend() { |
97 DVLOG(1) << "Power Suspending"; | 52 DVLOG(1) << "Power Suspending"; |
98 observers_->Notify(&PowerObserver::OnSuspend); | 53 observers_->Notify(&PowerObserver::OnSuspend); |
99 } | 54 } |
100 | 55 |
101 void PowerMonitor::NotifyResume() { | 56 void PowerMonitor::NotifyResume() { |
102 DVLOG(1) << "Power Resuming"; | 57 DVLOG(1) << "Power Resuming"; |
103 observers_->Notify(&PowerObserver::OnResume); | 58 observers_->Notify(&PowerObserver::OnResume); |
104 } | 59 } |
105 | 60 |
106 } // namespace base | 61 } // namespace base |
OLD | NEW |