OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
vandebo (ex-Chrome)
2012/10/30 20:41:24
Can you set similarity so that this is detected as
Hongbo Min
2012/10/31 12:52:50
Done.
| |
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 is_get_signaler_called_(false) { | |
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 DCHECK_EQ(this, g_power_monitor); | |
42 g_power_monitor = NULL; | |
43 } | |
44 | |
45 // static | |
46 PowerMonitor* PowerMonitor::Get() { | |
47 return g_power_monitor; | |
48 } | |
49 | |
50 PowerMonitor::Signaler* PowerMonitor::GetSignalerOnce() { | |
51 if (!is_get_signaler_called_) { | |
52 is_get_signaler_called_ = true; | |
53 return new PowerMonitor::Signaler(); | |
54 } | |
55 | |
56 // Return NULL after the first time it get called. | |
57 return NULL; | |
58 } | |
59 | |
60 void PowerMonitor::AddObserver(PowerObserver* obs) { | |
61 observers_->AddObserver(obs); | |
62 } | |
63 | |
64 void PowerMonitor::RemoveObserver(PowerObserver* obs) { | |
65 observers_->RemoveObserver(obs); | |
66 } | |
67 | |
68 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) { | |
69 // Suppress duplicate notifications. Some platforms may | |
70 // send multiple notifications of the same event. | |
71 switch (event_id) { | |
72 case POWER_STATE_EVENT: | |
73 { | |
74 bool on_battery = IsBatteryPower(); | |
75 if (on_battery != battery_in_use_) { | |
76 battery_in_use_ = on_battery; | |
77 NotifyPowerStateChange(); | |
78 } | |
79 } | |
80 break; | |
81 case RESUME_EVENT: | |
82 if (suspended_) { | |
83 suspended_ = false; | |
84 NotifyResume(); | |
85 } | |
86 break; | |
87 case SUSPEND_EVENT: | |
88 if (!suspended_) { | |
89 suspended_ = true; | |
90 NotifySuspend(); | |
91 } | |
92 break; | |
93 } | |
94 } | |
95 | |
96 void PowerMonitor::NotifyPowerStateChange() { | |
97 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
98 << " battery"; | |
99 observers_->Notify(&PowerObserver::OnPowerStateChange, | |
100 BatteryPower()); | |
101 } | |
102 | |
103 void PowerMonitor::NotifySuspend() { | |
104 DVLOG(1) << "Power Suspending"; | |
105 observers_->Notify(&PowerObserver::OnSuspend); | |
106 } | |
107 | |
108 void PowerMonitor::NotifyResume() { | |
109 DVLOG(1) << "Power Resuming"; | |
110 observers_->Notify(&PowerObserver::OnResume); | |
111 } | |
112 | |
113 void PowerMonitor::BatteryCheck() { | |
114 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | |
115 } | |
116 | |
117 // PowerMonitor::Signaler | |
118 | |
119 PowerMonitor::Signaler::Signaler() { | |
120 } | |
121 | |
122 PowerMonitor::Signaler::~Signaler() { | |
123 } | |
124 | |
125 #if defined(OS_WIN) | |
126 void PowerMonitor::Signaler::ProcessWmPowerBroadcastMessage(int event) { | |
127 if (PowerMonitor::Get()) | |
128 PowerMonitor::Get()->ProcessWmPowerBroadcastMessage(event); | |
129 } | |
130 #endif | |
131 | |
132 void PowerMonitor::Signaler::ProcessPowerEvent(PowerEvent event) { | |
133 if (PowerMonitor::Get()) | |
134 PowerMonitor::Get()->ProcessPowerEvent(event); | |
135 } | |
136 | |
137 } // namespace base | |
138 | |
OLD | NEW |