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" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 weak_ptr_factory_.GetWeakPtr())); | 85 weak_ptr_factory_.GetWeakPtr())); |
86 | 86 |
87 // Monitor the D-Bus signal for power state changed signals. | 87 // Monitor the D-Bus signal for power state changed signals. |
88 power_manager_proxy_->ConnectToSignal( | 88 power_manager_proxy_->ConnectToSignal( |
89 power_manager::kPowerManagerInterface, | 89 power_manager::kPowerManagerInterface, |
90 power_manager::kPowerStateChangedSignal, | 90 power_manager::kPowerStateChangedSignal, |
91 base::Bind(&PowerManagerClientImpl::PowerStateChangedSignalReceived, | 91 base::Bind(&PowerManagerClientImpl::PowerStateChangedSignalReceived, |
92 weak_ptr_factory_.GetWeakPtr()), | 92 weak_ptr_factory_.GetWeakPtr()), |
93 base::Bind(&PowerManagerClientImpl::SignalConnected, | 93 base::Bind(&PowerManagerClientImpl::SignalConnected, |
94 weak_ptr_factory_.GetWeakPtr())); | 94 weak_ptr_factory_.GetWeakPtr())); |
| 95 |
| 96 // Monitor the D-Bus signal for screen lock and unlock signals. |
| 97 power_manager_proxy_->ConnectToSignal( |
| 98 chromium::kChromiumInterface, |
| 99 chromium::kLockScreenSignal, |
| 100 base::Bind(&PowerManagerClientImpl::ScreenLockSignalReceived, |
| 101 weak_ptr_factory_.GetWeakPtr()), |
| 102 base::Bind(&PowerManagerClientImpl::SignalConnected, |
| 103 weak_ptr_factory_.GetWeakPtr())); |
| 104 power_manager_proxy_->ConnectToSignal( |
| 105 chromium::kChromiumInterface, |
| 106 chromium::kUnlockScreenSignal, |
| 107 base::Bind(&PowerManagerClientImpl::ScreenUnlockSignalReceived, |
| 108 weak_ptr_factory_.GetWeakPtr()), |
| 109 base::Bind(&PowerManagerClientImpl::SignalConnected, |
| 110 weak_ptr_factory_.GetWeakPtr())); |
| 111 power_manager_proxy_->ConnectToSignal( |
| 112 chromium::kChromiumInterface, |
| 113 chromium::kUnlockScreenFailedSignal, |
| 114 base::Bind(&PowerManagerClientImpl::ScreenUnlockFailedSignalReceived, |
| 115 weak_ptr_factory_.GetWeakPtr()), |
| 116 base::Bind(&PowerManagerClientImpl::SignalConnected, |
| 117 weak_ptr_factory_.GetWeakPtr())); |
95 } | 118 } |
96 | 119 |
97 virtual ~PowerManagerClientImpl() { | 120 virtual ~PowerManagerClientImpl() { |
98 } | 121 } |
99 | 122 |
100 // PowerManagerClient override. | 123 // PowerManagerClient override. |
101 virtual void AddObserver(Observer* observer) { | 124 virtual void AddObserver(Observer* observer) { |
102 observers_.AddObserver(observer); | 125 observers_.AddObserver(observer); |
103 } | 126 } |
104 | 127 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 OVERRIDE { | 192 OVERRIDE { |
170 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | 193 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
171 power_manager::kGetIdleTime); | 194 power_manager::kGetIdleTime); |
172 power_manager_proxy_->CallMethod( | 195 power_manager_proxy_->CallMethod( |
173 &method_call, | 196 &method_call, |
174 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 197 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
175 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, | 198 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, |
176 weak_ptr_factory_.GetWeakPtr(), callback)); | 199 weak_ptr_factory_.GetWeakPtr(), callback)); |
177 } | 200 } |
178 | 201 |
| 202 virtual void NotifyScreenLockRequested() OVERRIDE { |
| 203 SimpleMethodCallToPowerManager(power_manager::kRequestLockScreenMethod); |
| 204 } |
| 205 |
| 206 virtual void NotifyScreenLockCompleted() OVERRIDE { |
| 207 SimpleMethodCallToPowerManager(power_manager::kScreenIsLockedMethod); |
| 208 } |
| 209 |
| 210 virtual void NotifyScreenUnlockRequested() OVERRIDE { |
| 211 SimpleMethodCallToPowerManager(power_manager::kRequestUnlockScreenMethod); |
| 212 } |
| 213 |
| 214 virtual void NotifyScreenUnlockCompleted() OVERRIDE { |
| 215 SimpleMethodCallToPowerManager(power_manager::kScreenIsUnlockedMethod); |
| 216 } |
| 217 |
179 private: | 218 private: |
180 // Called when a dbus signal is initially connected. | 219 // Called when a dbus signal is initially connected. |
181 void SignalConnected(const std::string& interface_name, | 220 void SignalConnected(const std::string& interface_name, |
182 const std::string& signal_name, | 221 const std::string& signal_name, |
183 bool success) { | 222 bool success) { |
184 LOG_IF(WARNING, !success) << "Failed to connect to signal " | 223 LOG_IF(WARNING, !success) << "Failed to connect to signal " |
185 << signal_name << "."; | 224 << signal_name << "."; |
186 } | 225 } |
187 | 226 |
| 227 // Make a method call to power manager with no arguments and no response. |
| 228 void SimpleMethodCallToPowerManager(const std::string& method_name) { |
| 229 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
| 230 method_name); |
| 231 power_manager_proxy_->CallMethod( |
| 232 &method_call, |
| 233 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 234 dbus::ObjectProxy::EmptyResponseCallback()); |
| 235 } |
| 236 |
188 // Called when a brightness change signal is received. | 237 // Called when a brightness change signal is received. |
189 void BrightnessChangedReceived(dbus::Signal* signal) { | 238 void BrightnessChangedReceived(dbus::Signal* signal) { |
190 dbus::MessageReader reader(signal); | 239 dbus::MessageReader reader(signal); |
191 int32 brightness_level = 0; | 240 int32 brightness_level = 0; |
192 bool user_initiated = 0; | 241 bool user_initiated = 0; |
193 if (!(reader.PopInt32(&brightness_level) && | 242 if (!(reader.PopInt32(&brightness_level) && |
194 reader.PopBool(&user_initiated))) { | 243 reader.PopBool(&user_initiated))) { |
195 LOG(ERROR) << "Brightness changed signal had incorrect parameters: " | 244 LOG(ERROR) << "Brightness changed signal had incorrect parameters: " |
196 << signal->ToString(); | 245 << signal->ToString(); |
197 return; | 246 return; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 return; | 334 return; |
286 } | 335 } |
287 if (idle_time_ms < 0) { | 336 if (idle_time_ms < 0) { |
288 LOG(ERROR) << "Power manager failed to calculate idle time."; | 337 LOG(ERROR) << "Power manager failed to calculate idle time."; |
289 callback.Run(-1); | 338 callback.Run(-1); |
290 return; | 339 return; |
291 } | 340 } |
292 callback.Run(idle_time_ms/1000); | 341 callback.Run(idle_time_ms/1000); |
293 } | 342 } |
294 | 343 |
| 344 void ScreenLockSignalReceived(dbus::Signal* signal) { |
| 345 FOR_EACH_OBSERVER(Observer, observers_, LockScreen()); |
| 346 } |
| 347 |
| 348 void ScreenUnlockSignalReceived(dbus::Signal* signal) { |
| 349 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen()); |
| 350 } |
| 351 |
| 352 void ScreenUnlockFailedSignalReceived(dbus::Signal* signal) { |
| 353 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed()); |
| 354 } |
| 355 |
295 dbus::ObjectProxy* power_manager_proxy_; | 356 dbus::ObjectProxy* power_manager_proxy_; |
296 ObserverList<Observer> observers_; | 357 ObserverList<Observer> observers_; |
297 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 358 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
298 | 359 |
299 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 360 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
300 }; | 361 }; |
301 | 362 |
302 // The PowerManagerClient implementation used on Linux desktop, | 363 // The PowerManagerClient implementation used on Linux desktop, |
303 // which does nothing. | 364 // which does nothing. |
304 class PowerManagerClientStubImpl : public PowerManagerClient { | 365 class PowerManagerClientStubImpl : public PowerManagerClient { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 406 |
346 virtual void RequestRestart() OVERRIDE {} | 407 virtual void RequestRestart() OVERRIDE {} |
347 | 408 |
348 virtual void RequestShutdown() OVERRIDE {} | 409 virtual void RequestShutdown() OVERRIDE {} |
349 | 410 |
350 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) | 411 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) |
351 OVERRIDE { | 412 OVERRIDE { |
352 callback.Run(0); | 413 callback.Run(0); |
353 } | 414 } |
354 | 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 |
355 private: | 424 private: |
356 void Update() { | 425 void Update() { |
357 // 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. |
358 if (pause_count_ > 1) { | 427 if (pause_count_ > 1) { |
359 pause_count_--; | 428 pause_count_--; |
360 return; | 429 return; |
361 } | 430 } |
362 | 431 |
363 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 432 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
364 if (pause_count_) { | 433 if (pause_count_) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 | 472 |
404 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { | 473 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { |
405 if (system::runtime_environment::IsRunningOnChromeOS()) { | 474 if (system::runtime_environment::IsRunningOnChromeOS()) { |
406 return new PowerManagerClientImpl(bus); | 475 return new PowerManagerClientImpl(bus); |
407 } else { | 476 } else { |
408 return new PowerManagerClientStubImpl(); | 477 return new PowerManagerClientStubImpl(); |
409 } | 478 } |
410 } | 479 } |
411 | 480 |
412 } // namespace chromeos | 481 } // namespace chromeos |
OLD | NEW |