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 |
7 #include "base/time.h" | 7 #include "base/power_monitor/power_monitor_device_source.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 static PowerMonitor* g_power_monitor = NULL; | 11 static PowerMonitor* g_power_monitor = NULL; |
12 | 12 |
13 #if defined(ENABLE_BATTERY_MONITORING) | 13 // This constructor is provided to preserve backwards compatibility with the |
14 // The amount of time (in ms) to wait before running the initial | 14 // most common use case, where the power monitor is created on a UI thread to |
15 // battery check. | 15 // directly monitor system power events. |
16 static int kDelayedBatteryCheckMs = 10 * 1000; | |
17 #endif // defined(ENABLE_BATTERY_MONITORING) | |
18 | |
19 PowerMonitor::PowerMonitor() | 16 PowerMonitor::PowerMonitor() |
20 : observers_(new ObserverListThreadSafe<PowerObserver>()), | 17 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
21 battery_in_use_(false), | 18 source_(new PowerMonitorDeviceSource()) { |
22 suspended_(false) { | |
23 DCHECK(!g_power_monitor); | 19 DCHECK(!g_power_monitor); |
24 g_power_monitor = this; | 20 g_power_monitor = this; |
| 21 source_->SetMonitor(this); |
| 22 } |
25 | 23 |
26 DCHECK(MessageLoop::current()); | 24 PowerMonitor::PowerMonitor(PowerMonitorSource* source) |
27 #if defined(ENABLE_BATTERY_MONITORING) | 25 : observers_(new ObserverListThreadSafe<PowerObserver>()), |
28 delayed_battery_check_.Start(FROM_HERE, | 26 source_(source) { |
29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 27 DCHECK(!g_power_monitor); |
30 &PowerMonitor::BatteryCheck); | 28 g_power_monitor = this; |
31 #endif // defined(ENABLE_BATTERY_MONITORING) | 29 source_->SetMonitor(this); |
32 #if defined(OS_MACOSX) | |
33 PlatformInit(); | |
34 #endif | |
35 } | 30 } |
36 | 31 |
37 PowerMonitor::~PowerMonitor() { | 32 PowerMonitor::~PowerMonitor() { |
38 #if defined(OS_MACOSX) | |
39 PlatformDestroy(); | |
40 #endif | |
41 DCHECK_EQ(this, g_power_monitor); | 33 DCHECK_EQ(this, g_power_monitor); |
42 g_power_monitor = NULL; | 34 g_power_monitor = NULL; |
| 35 source_->SetMonitor(NULL); |
43 } | 36 } |
44 | 37 |
45 // static | 38 // static |
46 PowerMonitor* PowerMonitor::Get() { | 39 PowerMonitor* PowerMonitor::Get() { |
47 return g_power_monitor; | 40 return g_power_monitor; |
48 } | 41 } |
49 | 42 |
50 void PowerMonitor::AddObserver(PowerObserver* obs) { | 43 void PowerMonitor::AddObserver(PowerObserver* obs) { |
51 observers_->AddObserver(obs); | 44 observers_->AddObserver(obs); |
52 } | 45 } |
53 | 46 |
54 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | 47 void PowerMonitor::RemoveObserver(PowerObserver* obs) { |
55 observers_->RemoveObserver(obs); | 48 observers_->RemoveObserver(obs); |
56 } | 49 } |
57 | 50 |
58 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | 51 PowerMonitorSource* PowerMonitor::Source() { |
59 // Suppress duplicate notifications. Some platforms may | 52 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 } | 53 } |
85 | 54 |
86 void PowerMonitor::BatteryCheck() { | 55 void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) { |
87 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | 56 DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off") |
88 } | |
89 | |
90 void PowerMonitor::NotifyPowerStateChange() { | |
91 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
92 << " battery"; | 57 << " battery"; |
93 observers_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | 58 observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use); |
94 } | 59 } |
95 | 60 |
96 void PowerMonitor::NotifySuspend() { | 61 void PowerMonitor::NotifySuspend() { |
97 DVLOG(1) << "Power Suspending"; | 62 DVLOG(1) << "Power Suspending"; |
98 observers_->Notify(&PowerObserver::OnSuspend); | 63 observers_->Notify(&PowerObserver::OnSuspend); |
99 } | 64 } |
100 | 65 |
101 void PowerMonitor::NotifyResume() { | 66 void PowerMonitor::NotifyResume() { |
102 DVLOG(1) << "Power Resuming"; | 67 DVLOG(1) << "Power Resuming"; |
103 observers_->Notify(&PowerObserver::OnResume); | 68 observers_->Notify(&PowerObserver::OnResume); |
104 } | 69 } |
105 | 70 |
106 } // namespace base | 71 } // namespace base |
OLD | NEW |