OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/cros/power_library.h" | 5 #include "chrome/browser/chromeos/cros/power_library.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/time.h" | |
15 #include "base/timer.h" | |
16 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
15 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | |
satorux1
2011/10/28 17:36:23
We don't need this, right?
Simon Que
2011/10/29 02:20:11
Done.
| |
17 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
18 #include "third_party/cros/chromeos_power.h" | 17 #include "third_party/cros/chromeos_power.h" |
19 #include "third_party/cros/chromeos_resume.h" | 18 #include "third_party/cros/chromeos_resume.h" |
20 | 19 |
21 namespace chromeos { | 20 namespace chromeos { |
22 | 21 |
23 class PowerLibraryImpl : public PowerLibrary { | 22 class PowerLibraryImpl : public PowerLibrary { |
24 public: | 23 public: |
25 PowerLibraryImpl() | 24 PowerLibraryImpl() |
26 : power_status_connection_(NULL), | 25 : resume_status_connection_(NULL) { |
27 resume_status_connection_(NULL) { | |
28 } | 26 } |
29 | 27 |
30 virtual ~PowerLibraryImpl() { | 28 virtual ~PowerLibraryImpl() { |
31 if (power_status_connection_) { | |
32 chromeos::DisconnectPowerStatus(power_status_connection_); | |
33 power_status_connection_ = NULL; | |
34 } | |
35 if (resume_status_connection_) { | 29 if (resume_status_connection_) { |
36 chromeos::DisconnectResume(resume_status_connection_); | 30 chromeos::DisconnectResume(resume_status_connection_); |
37 resume_status_connection_ = NULL; | 31 resume_status_connection_ = NULL; |
38 } | 32 } |
39 } | 33 } |
40 | 34 |
41 // Begin PowerLibrary implementation. | 35 // Begin PowerLibrary implementation. |
42 virtual void Init() OVERRIDE { | 36 virtual void Init() OVERRIDE { |
43 DCHECK(CrosLibrary::Get()->libcros_loaded()); | 37 DCHECK(CrosLibrary::Get()->libcros_loaded()); |
44 power_status_connection_ = | |
45 chromeos::MonitorPowerStatus(&PowerStatusChangedHandler, this); | |
46 resume_status_connection_ = | 38 resume_status_connection_ = |
47 chromeos::MonitorResume(&SystemResumedHandler, this); | 39 chromeos::MonitorResume(&SystemResumedHandler, this); |
48 } | 40 } |
49 | 41 |
50 virtual void AddObserver(Observer* observer) OVERRIDE { | 42 virtual void AddObserver(Observer* observer) OVERRIDE { |
51 observers_.AddObserver(observer); | 43 observers_.AddObserver(observer); |
52 } | 44 } |
53 | 45 |
54 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 46 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
55 observers_.RemoveObserver(observer); | 47 observers_.RemoveObserver(observer); |
56 } | 48 } |
57 | 49 |
58 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 50 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
59 // TODO(sidor): Examine if it's really a good idea to use void* as a second | 51 // TODO(sidor): Examine if it's really a good idea to use void* as a second |
60 // argument. | 52 // argument. |
61 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); | 53 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); |
62 } | 54 } |
63 | 55 |
64 virtual void EnableScreenLock(bool enable) OVERRIDE { | 56 virtual void EnableScreenLock(bool enable) OVERRIDE { |
65 // Called when the screen preference is changed, which should always | 57 // Make sure we run on FILE thread because chromeos::EnableScreenLock |
66 // run on UI thread. | |
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
68 // Post the task to FILE thread as chromeos::EnableScreenLock | |
69 // would write power manager config file to disk. | 58 // would write power manager config file to disk. |
70 BrowserThread::PostTask( | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
71 BrowserThread::FILE, FROM_HERE, | 60 |
72 base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); | 61 chromeos::EnableScreenLock(enable); |
73 } | 62 } |
74 | 63 |
75 virtual void RequestRestart() OVERRIDE { | 64 virtual void RequestRestart() OVERRIDE { |
76 chromeos::RequestRestart(); | 65 chromeos::RequestRestart(); |
77 } | 66 } |
78 | 67 |
79 virtual void RequestShutdown() OVERRIDE { | 68 virtual void RequestShutdown() OVERRIDE { |
80 chromeos::RequestShutdown(); | 69 chromeos::RequestShutdown(); |
81 } | 70 } |
82 | 71 |
83 virtual void RequestStatusUpdate() OVERRIDE { | 72 virtual void RequestStatusUpdate() OVERRIDE { |
84 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; | 73 // TODO(stevenjb): chromeos::RetrievePowerInformation has been deprecated; |
85 // we should add a mechanism to immediately request an update, probably | 74 // we should add a mechanism to immediately request an update, probably |
86 // when we migrate the DBus code from libcros to here. | 75 // when we migrate the DBus code from libcros to here. |
87 } | 76 } |
88 | |
89 // End PowerLibrary implementation. | 77 // End PowerLibrary implementation. |
90 | 78 |
91 private: | 79 private: |
92 static void DoEnableScreenLock(bool enable) { | 80 static void DoEnableScreenLock(bool enable) { |
93 chromeos::EnableScreenLock(enable); | 81 chromeos::EnableScreenLock(enable); |
94 } | 82 } |
95 | 83 |
96 static void GetIdleTimeCallback(void* object, | 84 static void GetIdleTimeCallback(void* object, |
97 int64_t time_idle_ms, | 85 int64_t time_idle_ms, |
98 bool success) { | 86 bool success) { |
99 DCHECK(object); | 87 DCHECK(object); |
100 CalculateIdleTimeCallback* notify = | 88 CalculateIdleTimeCallback* notify = |
101 static_cast<CalculateIdleTimeCallback*>(object); | 89 static_cast<CalculateIdleTimeCallback*>(object); |
102 if (success) { | 90 if (success) { |
103 notify->Run(time_idle_ms/1000); | 91 notify->Run(time_idle_ms/1000); |
104 } else { | 92 } else { |
105 LOG(ERROR) << "Power manager failed to calculate idle time."; | 93 LOG(ERROR) << "Power manager failed to calculate idle time."; |
106 notify->Run(-1); | 94 notify->Run(-1); |
107 } | 95 } |
108 delete notify; | 96 delete notify; |
109 } | 97 } |
110 | 98 |
111 static void PowerStatusChangedHandler( | |
112 void* object, const chromeos::PowerStatus& power_status) { | |
113 // TODO(sque): this is a temporary copy-over from libcros. Soon libcros | |
114 // will be removed and this will not be necessary. | |
115 PowerSupplyStatus status; | |
116 status.line_power_on = power_status.line_power_on; | |
117 status.battery_is_present = power_status.battery_is_present; | |
118 status.battery_is_full = | |
119 (power_status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED); | |
120 status.battery_seconds_to_empty = power_status.battery_time_to_empty; | |
121 status.battery_seconds_to_full = power_status.battery_time_to_full; | |
122 status.battery_percentage = power_status.battery_percentage; | |
123 | |
124 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | |
125 power->UpdatePowerStatus(status); | |
126 } | |
127 | |
128 static void SystemResumedHandler(void* object) { | 99 static void SystemResumedHandler(void* object) { |
129 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 100 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
130 power->SystemResumed(); | 101 power->SystemResumed(); |
131 } | 102 } |
132 | 103 |
133 void UpdatePowerStatus(const PowerSupplyStatus& status) { | |
134 // Called from PowerStatusChangedHandler, a libcros callback which | |
135 // should always run on UI thread. | |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
137 | |
138 DVLOG(1) << "Power line_power_on = " << status.line_power_on | |
139 << " percentage = " << status.battery_percentage | |
140 << " seconds_to_empty = " << status.battery_seconds_to_empty | |
141 << " seconds_to_full = " << status.battery_seconds_to_full; | |
142 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
143 } | |
144 | |
145 void SystemResumed() { | 104 void SystemResumed() { |
146 // Called from SystemResumedHandler, a libcros callback which should | 105 // Called from SystemResumedHandler, a libcros callback which should |
147 // always run on UI thread. | 106 // always run on UI thread. |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
108 | |
149 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 109 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
150 } | 110 } |
151 | 111 |
152 ObserverList<Observer> observers_; | 112 ObserverList<Observer> observers_; |
153 | 113 |
154 // A reference to the power battery API, to allow callbacks when the battery | |
155 // status changes. | |
156 chromeos::PowerStatusConnection power_status_connection_; | |
157 | |
158 // A reference to the resume alerts. | 114 // A reference to the resume alerts. |
159 chromeos::ResumeConnection resume_status_connection_; | 115 chromeos::ResumeConnection resume_status_connection_; |
160 | 116 |
161 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 117 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
162 }; | 118 }; |
163 | 119 |
164 // The stub implementation runs the battery up and down, pausing at the | 120 // The stub implementation runs the battery up and down, pausing at the |
165 // fully charged and fully depleted states. | 121 // fully charged and fully depleted states. |
166 class PowerLibraryStubImpl : public PowerLibrary { | 122 class PowerLibraryStubImpl : public PowerLibrary { |
167 public: | 123 public: |
168 PowerLibraryStubImpl() | 124 PowerLibraryStubImpl() { |
169 : discharging_(true), | |
170 battery_percentage_(80), | |
171 pause_count_(0) { | |
172 } | 125 } |
173 | 126 |
174 virtual ~PowerLibraryStubImpl() {} | 127 virtual ~PowerLibraryStubImpl() {} |
175 | 128 |
176 // Begin PowerLibrary implementation. | 129 // Begin PowerLibrary implementation. |
177 virtual void Init() OVERRIDE {} | 130 virtual void Init() OVERRIDE {} |
178 virtual void AddObserver(Observer* observer) OVERRIDE { | 131 virtual void AddObserver(Observer* observer) OVERRIDE { |
179 observers_.AddObserver(observer); | 132 observers_.AddObserver(observer); |
180 } | 133 } |
181 | 134 |
182 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 135 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
183 observers_.RemoveObserver(observer); | 136 observers_.RemoveObserver(observer); |
184 } | 137 } |
185 | 138 |
186 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | 139 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { |
187 callback->Run(0); | 140 callback->Run(0); |
188 delete callback; | 141 delete callback; |
189 } | 142 } |
190 | 143 |
191 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 144 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
192 | 145 |
193 virtual void RequestRestart() OVERRIDE {} | 146 virtual void RequestRestart() OVERRIDE {} |
194 | 147 |
195 virtual void RequestShutdown() OVERRIDE {} | 148 virtual void RequestShutdown() OVERRIDE {} |
196 | 149 |
197 virtual void RequestStatusUpdate() OVERRIDE { | |
198 if (!timer_.IsRunning()) { | |
199 timer_.Start( | |
200 FROM_HERE, | |
201 base::TimeDelta::FromMilliseconds(100), | |
202 this, | |
203 &PowerLibraryStubImpl::Update); | |
204 } else { | |
205 timer_.Stop(); | |
206 } | |
207 } | |
208 | |
209 // End PowerLibrary implementation. | 150 // End PowerLibrary implementation. |
210 | |
211 private: | 151 private: |
212 void Update() { | |
213 // We pause at 0 and 100% so that it's easier to check those conditions. | |
214 if (pause_count_ > 1) { | |
215 pause_count_--; | |
216 return; | |
217 } | |
218 | |
219 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | |
220 if (pause_count_) { | |
221 pause_count_ = 0; | |
222 discharging_ = !discharging_; | |
223 } else { | |
224 pause_count_ = 20; | |
225 return; | |
226 } | |
227 } | |
228 battery_percentage_ += (discharging_ ? -1 : 1); | |
229 | |
230 PowerSupplyStatus status; | |
231 status.line_power_on = !discharging_; | |
232 status.battery_is_present = true; | |
233 status.battery_percentage = battery_percentage_; | |
234 status.battery_seconds_to_empty = | |
235 std::max(1, battery_percentage_ * 180 / 100); | |
236 status.battery_seconds_to_full = | |
237 std::max(static_cast<int64>(1), 180 - status.battery_seconds_to_empty); | |
238 | |
239 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | |
240 } | |
241 | |
242 bool discharging_; | |
243 int battery_percentage_; | |
244 int pause_count_; | |
245 ObserverList<Observer> observers_; | 152 ObserverList<Observer> observers_; |
246 base::RepeatingTimer<PowerLibraryStubImpl> timer_; | |
247 }; | 153 }; |
248 | 154 |
249 // static | 155 // static |
250 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 156 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
251 PowerLibrary* impl; | 157 PowerLibrary* impl; |
252 if (stub) | 158 if (stub) |
253 impl = new PowerLibraryStubImpl(); | 159 impl = new PowerLibraryStubImpl(); |
254 else | 160 else |
255 impl = new PowerLibraryImpl(); | 161 impl = new PowerLibraryImpl(); |
256 impl->Init(); | 162 impl->Init(); |
257 return impl; | 163 return impl; |
258 } | 164 } |
259 | 165 |
260 } // namespace chromeos | 166 } // namespace chromeos |
OLD | NEW |