OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 | |
7 namespace base { | |
8 | |
9 static PowerMonitor* g_power_monitor = NULL; | |
10 | |
11 #if defined(ENABLE_BATTERY_MONITORING) | |
12 // The amount of time (in ms) to wait before running the initial | |
13 // battery check. | |
14 static int kDelayedBatteryCheckMs = 10 * 1000; | |
15 #endif // defined(ENABLE_BATTERY_MONITORING) | |
16 | |
17 PowerMonitor::PowerMonitor() | |
18 : observers_(new ObserverListThreadSafe<PowerObserver>()), | |
19 battery_in_use_(false), | |
20 suspended_(false), | |
21 signaler_(NULL) { | |
22 DCHECK(!g_power_monitor); | |
23 g_power_monitor = this; | |
24 | |
25 DCHECK(MessageLoop::current()); | |
26 #if defined(ENABLE_BATTERY_MONITORING) | |
27 delayed_battery_check_.Start(FROM_HERE, | |
28 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | |
29 &PowerMonitor::BatteryCheck); | |
30 #endif // defined(ENABLE_BATTERY_MONITORING) | |
31 | |
32 #if defined(OS_MACOSX) | |
33 PlatformInit(); | |
34 #endif | |
35 } | |
36 | |
37 PowerMonitor::~PowerMonitor() { | |
38 #if defined(OS_MACOSX) | |
39 PlatformDestroy(); | |
40 #endif | |
41 if (signaler_) | |
42 delete signaler_; | |
vandebo (ex-Chrome)
2012/10/29 18:04:38
Don't manage lifetimes in the destructor: If this
Hongbo Min
2012/10/30 14:33:47
You are right.
| |
43 DCHECK_EQ(this, g_power_monitor); | |
44 g_power_monitor = NULL; | |
45 } | |
46 | |
47 // static | |
48 PowerMonitor* PowerMonitor::Get() { | |
49 return g_power_monitor; | |
50 } | |
51 | |
52 PowerMonitor::Signaler* PowerMonitor::GetSignalerOnce() { | |
53 CHECK(signaler_ == NULL); | |
vandebo (ex-Chrome)
2012/10/29 18:04:38
The interface says it will return NULL on the seco
Hongbo Min
2012/10/30 14:33:47
Sorry for this low level mistake:)
| |
54 | |
55 if (signaler_ == NULL) { | |
56 // The signaler is allowed to be leaked since the PowerMonitor and Signaler | |
vandebo (ex-Chrome)
2012/10/29 18:04:38
Comment is out of date.
Hongbo Min
2012/10/30 14:33:47
Done.
| |
57 // hold a reference to each other. | |
58 signaler_ = new PowerMonitor::Signaler(this); | |
59 return signaler_; | |
60 } | |
61 | |
62 // Return NULL after the first time it get called. | |
63 return NULL; | |
64 } | |
65 | |
66 void PowerMonitor::AddObserver(PowerObserver* obs) { | |
67 observers_->AddObserver(obs); | |
68 } | |
69 | |
70 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | |
71 observers_->RemoveObserver(obs); | |
72 } | |
73 | |
74 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | |
75 // Suppress duplicate notifications. Some platforms may | |
76 // send multiple notifications of the same event. | |
77 switch (event_id) { | |
78 case POWER_STATE_EVENT: | |
79 { | |
80 bool on_battery = IsBatteryPower(); | |
81 if (on_battery != battery_in_use_) { | |
82 battery_in_use_ = on_battery; | |
83 NotifyPowerStateChange(); | |
84 } | |
85 } | |
86 break; | |
87 case RESUME_EVENT: | |
88 if (suspended_) { | |
89 suspended_ = false; | |
90 NotifyResume(); | |
91 } | |
92 break; | |
93 case SUSPEND_EVENT: | |
94 if (!suspended_) { | |
95 suspended_ = true; | |
96 NotifySuspend(); | |
97 } | |
98 break; | |
99 } | |
100 } | |
101 | |
102 void PowerMonitor::NotifyPowerStateChange() { | |
103 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
104 << " battery"; | |
105 observers_->Notify(&PowerObserver::OnPowerStateChange, | |
106 BatteryPower()); | |
107 } | |
108 | |
109 void PowerMonitor::NotifySuspend() { | |
110 DVLOG(1) << "Power Suspending"; | |
111 observers_->Notify(&PowerObserver::OnSuspend); | |
112 } | |
113 | |
114 void PowerMonitor::NotifyResume() { | |
115 DVLOG(1) << "Power Resuming"; | |
116 observers_->Notify(&PowerObserver::OnResume); | |
117 } | |
118 | |
119 void PowerMonitor::BatteryCheck() { | |
120 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | |
121 } | |
122 | |
123 } // namespace base | |
124 | |
OLD | NEW |