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/dbus/power_manager_client.h" | 5 #include "chrome/browser/chromeos/dbus/power_manager_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 #include "chrome/browser/chromeos/system/runtime_environment.h" | 17 #include "chrome/browser/chromeos/system/runtime_environment.h" |
18 #include "content/public/browser/browser_thread.h" | |
satorux1
2011/11/29 01:29:12
don't need this.
Simon Que
2011/11/29 01:47:32
Done.
| |
18 #include "dbus/bus.h" | 19 #include "dbus/bus.h" |
19 #include "dbus/message.h" | 20 #include "dbus/message.h" |
20 #include "dbus/object_proxy.h" | 21 #include "dbus/object_proxy.h" |
21 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
22 | 23 |
23 namespace chromeos { | 24 namespace chromeos { |
24 | 25 |
25 PowerSupplyStatus::PowerSupplyStatus() | 26 PowerSupplyStatus::PowerSupplyStatus() |
26 : line_power_on(false), | 27 : line_power_on(false), |
27 battery_is_present(false), | 28 battery_is_present(false), |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 weak_ptr_factory_.GetWeakPtr())); | 86 weak_ptr_factory_.GetWeakPtr())); |
86 | 87 |
87 // Monitor the D-Bus signal for power state changed signals. | 88 // Monitor the D-Bus signal for power state changed signals. |
88 power_manager_proxy_->ConnectToSignal( | 89 power_manager_proxy_->ConnectToSignal( |
89 power_manager::kPowerManagerInterface, | 90 power_manager::kPowerManagerInterface, |
90 power_manager::kPowerStateChangedSignal, | 91 power_manager::kPowerStateChangedSignal, |
91 base::Bind(&PowerManagerClientImpl::PowerStateChangedSignalReceived, | 92 base::Bind(&PowerManagerClientImpl::PowerStateChangedSignalReceived, |
92 weak_ptr_factory_.GetWeakPtr()), | 93 weak_ptr_factory_.GetWeakPtr()), |
93 base::Bind(&PowerManagerClientImpl::SignalConnected, | 94 base::Bind(&PowerManagerClientImpl::SignalConnected, |
94 weak_ptr_factory_.GetWeakPtr())); | 95 weak_ptr_factory_.GetWeakPtr())); |
96 | |
97 // Monitor the D-Bus signal for screen lock and unlock signals. | |
98 power_manager_proxy_->ConnectToSignal( | |
99 chromium::kChromiumInterface, | |
100 chromium::kLockScreenSignal, | |
101 base::Bind(&PowerManagerClientImpl::ScreenLockSignalReceived, | |
102 weak_ptr_factory_.GetWeakPtr()), | |
103 base::Bind(&PowerManagerClientImpl::SignalConnected, | |
104 weak_ptr_factory_.GetWeakPtr())); | |
105 power_manager_proxy_->ConnectToSignal( | |
106 chromium::kChromiumInterface, | |
107 chromium::kUnlockScreenSignal, | |
108 base::Bind(&PowerManagerClientImpl::ScreenLockSignalReceived, | |
109 weak_ptr_factory_.GetWeakPtr()), | |
110 base::Bind(&PowerManagerClientImpl::SignalConnected, | |
111 weak_ptr_factory_.GetWeakPtr())); | |
112 power_manager_proxy_->ConnectToSignal( | |
113 chromium::kChromiumInterface, | |
114 chromium::kUnlockScreenFailedSignal, | |
115 base::Bind(&PowerManagerClientImpl::ScreenLockSignalReceived, | |
116 weak_ptr_factory_.GetWeakPtr()), | |
117 base::Bind(&PowerManagerClientImpl::SignalConnected, | |
118 weak_ptr_factory_.GetWeakPtr())); | |
95 } | 119 } |
96 | 120 |
97 virtual ~PowerManagerClientImpl() { | 121 virtual ~PowerManagerClientImpl() { |
98 } | 122 } |
99 | 123 |
100 // PowerManagerClient override. | 124 // PowerManagerClient override. |
101 virtual void AddObserver(Observer* observer) { | 125 virtual void AddObserver(Observer* observer) { |
102 observers_.AddObserver(observer); | 126 observers_.AddObserver(observer); |
103 } | 127 } |
104 | 128 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 OVERRIDE { | 193 OVERRIDE { |
170 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | 194 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
171 power_manager::kGetIdleTime); | 195 power_manager::kGetIdleTime); |
172 power_manager_proxy_->CallMethod( | 196 power_manager_proxy_->CallMethod( |
173 &method_call, | 197 &method_call, |
174 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 198 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
175 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, | 199 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, |
176 weak_ptr_factory_.GetWeakPtr(), callback)); | 200 weak_ptr_factory_.GetWeakPtr(), callback)); |
177 } | 201 } |
178 | 202 |
203 virtual void NotifyScreenLockRequested() OVERRIDE { | |
204 SimpleMethodCallToPowerManager(power_manager::kRequestLockScreenMethod); | |
205 } | |
206 | |
207 virtual void NotifyScreenLockCompleted() OVERRIDE { | |
208 SimpleMethodCallToPowerManager(power_manager::kScreenIsLockedMethod); | |
209 } | |
210 | |
211 virtual void NotifyScreenUnlockRequested() OVERRIDE { | |
212 SimpleMethodCallToPowerManager(power_manager::kRequestUnlockScreenMethod); | |
213 } | |
214 | |
215 virtual void NotifyScreenUnlockCompleted() OVERRIDE { | |
216 SimpleMethodCallToPowerManager(power_manager::kScreenIsUnlockedMethod); | |
217 } | |
218 | |
179 private: | 219 private: |
180 // Called when a dbus signal is initially connected. | 220 // Called when a dbus signal is initially connected. |
181 void SignalConnected(const std::string& interface_name, | 221 void SignalConnected(const std::string& interface_name, |
182 const std::string& signal_name, | 222 const std::string& signal_name, |
183 bool success) { | 223 bool success) { |
184 LOG_IF(WARNING, !success) << "Failed to connect to signal " | 224 LOG_IF(WARNING, !success) << "Failed to connect to signal " |
185 << signal_name << "."; | 225 << signal_name << "."; |
186 } | 226 } |
187 | 227 |
228 // Make a method call to power manager with no arguments and no response. | |
229 void SimpleMethodCallToPowerManager(const std::string& method_name) { | |
230 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | |
231 method_name); | |
232 power_manager_proxy_->CallMethod( | |
233 &method_call, | |
234 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
235 dbus::ObjectProxy::EmptyResponseCallback()); | |
236 } | |
237 | |
188 // Called when a brightness change signal is received. | 238 // Called when a brightness change signal is received. |
189 void BrightnessChangedReceived(dbus::Signal* signal) { | 239 void BrightnessChangedReceived(dbus::Signal* signal) { |
190 dbus::MessageReader reader(signal); | 240 dbus::MessageReader reader(signal); |
191 int32 brightness_level = 0; | 241 int32 brightness_level = 0; |
192 bool user_initiated = 0; | 242 bool user_initiated = 0; |
193 if (!(reader.PopInt32(&brightness_level) && | 243 if (!(reader.PopInt32(&brightness_level) && |
194 reader.PopBool(&user_initiated))) { | 244 reader.PopBool(&user_initiated))) { |
195 LOG(ERROR) << "Brightness changed signal had incorrect parameters: " | 245 LOG(ERROR) << "Brightness changed signal had incorrect parameters: " |
196 << signal->ToString(); | 246 << signal->ToString(); |
197 return; | 247 return; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 return; | 331 return; |
282 } | 332 } |
283 if (idle_time_ms < 0) { | 333 if (idle_time_ms < 0) { |
284 LOG(ERROR) << "Power manager failed to calculate idle time."; | 334 LOG(ERROR) << "Power manager failed to calculate idle time."; |
285 callback.Run(-1); | 335 callback.Run(-1); |
286 return; | 336 return; |
287 } | 337 } |
288 callback.Run(idle_time_ms/1000); | 338 callback.Run(idle_time_ms/1000); |
289 } | 339 } |
290 | 340 |
341 void ScreenLockSignalReceived(dbus::Signal* signal) { | |
342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
satorux1
2011/11/29 01:29:12
Don't need this. We don't do this in other functio
Simon Que
2011/11/29 01:47:32
Done.
| |
343 std::string signal_name = signal->GetMember(); | |
344 if (signal_name == chromium::kLockScreenSignal) { | |
345 FOR_EACH_OBSERVER(Observer, observers_, LockScreen()); | |
346 } else if (signal_name == chromium::kUnlockScreenSignal) { | |
347 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen()); | |
348 } else if (signal_name == chromium::kUnlockScreenFailedSignal) { | |
349 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed()); | |
350 } else { | |
351 LOG(ERROR) << "Invalid signal name: " << signal_name; | |
352 NOTREACHED(); | |
353 } | |
satorux1
2011/11/29 01:29:12
What about defining a function for each signal, ra
Simon Que
2011/11/29 01:47:32
Done.
| |
354 } | |
355 | |
291 dbus::ObjectProxy* power_manager_proxy_; | 356 dbus::ObjectProxy* power_manager_proxy_; |
292 ObserverList<Observer> observers_; | 357 ObserverList<Observer> observers_; |
293 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 358 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
294 | 359 |
295 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 360 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
296 }; | 361 }; |
297 | 362 |
298 // The PowerManagerClient implementation used on Linux desktop, | 363 // The PowerManagerClient implementation used on Linux desktop, |
299 // which does nothing. | 364 // which does nothing. |
300 class PowerManagerClientStubImpl : public PowerManagerClient { | 365 class PowerManagerClientStubImpl : public PowerManagerClient { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 | 406 |
342 virtual void RequestRestart() OVERRIDE {} | 407 virtual void RequestRestart() OVERRIDE {} |
343 | 408 |
344 virtual void RequestShutdown() OVERRIDE {} | 409 virtual void RequestShutdown() OVERRIDE {} |
345 | 410 |
346 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) | 411 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) |
347 OVERRIDE { | 412 OVERRIDE { |
348 callback.Run(0); | 413 callback.Run(0); |
349 } | 414 } |
350 | 415 |
416 virtual void NotifyScreenLockRequested() OVERRIDE {} | |
417 | |
418 virtual void NotifyScreenLockCompleted() OVERRIDE {} | |
419 | |
420 virtual void NotifyScreenUnlockRequested() OVERRIDE {} | |
421 | |
422 virtual void NotifyScreenUnlockCompleted() OVERRIDE {} | |
423 | |
351 private: | 424 private: |
352 void Update() { | 425 void Update() { |
353 // We pause at 0 and 100% so that it's easier to check those conditions. | 426 // We pause at 0 and 100% so that it's easier to check those conditions. |
354 if (pause_count_ > 1) { | 427 if (pause_count_ > 1) { |
355 pause_count_--; | 428 pause_count_--; |
356 return; | 429 return; |
357 } | 430 } |
358 | 431 |
359 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 432 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
360 if (pause_count_) { | 433 if (pause_count_) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 | 472 |
400 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { | 473 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { |
401 if (system::runtime_environment::IsRunningOnChromeOS()) { | 474 if (system::runtime_environment::IsRunningOnChromeOS()) { |
402 return new PowerManagerClientImpl(bus); | 475 return new PowerManagerClientImpl(bus); |
403 } else { | 476 } else { |
404 return new PowerManagerClientStubImpl(); | 477 return new PowerManagerClientStubImpl(); |
405 } | 478 } |
406 } | 479 } |
407 | 480 |
408 } // namespace chromeos | 481 } // namespace chromeos |
OLD | NEW |