| 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())); | |
| 118 } | 95 } |
| 119 | 96 |
| 120 virtual ~PowerManagerClientImpl() { | 97 virtual ~PowerManagerClientImpl() { |
| 121 } | 98 } |
| 122 | 99 |
| 123 // PowerManagerClient override. | 100 // PowerManagerClient override. |
| 124 virtual void AddObserver(Observer* observer) { | 101 virtual void AddObserver(Observer* observer) { |
| 125 observers_.AddObserver(observer); | 102 observers_.AddObserver(observer); |
| 126 } | 103 } |
| 127 | 104 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 OVERRIDE { | 169 OVERRIDE { |
| 193 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | 170 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, |
| 194 power_manager::kGetIdleTime); | 171 power_manager::kGetIdleTime); |
| 195 power_manager_proxy_->CallMethod( | 172 power_manager_proxy_->CallMethod( |
| 196 &method_call, | 173 &method_call, |
| 197 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 174 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 198 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, | 175 base::Bind(&PowerManagerClientImpl::OnGetIdleTime, |
| 199 weak_ptr_factory_.GetWeakPtr(), callback)); | 176 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 200 } | 177 } |
| 201 | 178 |
| 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 | |
| 218 private: | 179 private: |
| 219 // Called when a dbus signal is initially connected. | 180 // Called when a dbus signal is initially connected. |
| 220 void SignalConnected(const std::string& interface_name, | 181 void SignalConnected(const std::string& interface_name, |
| 221 const std::string& signal_name, | 182 const std::string& signal_name, |
| 222 bool success) { | 183 bool success) { |
| 223 LOG_IF(WARNING, !success) << "Failed to connect to signal " | 184 LOG_IF(WARNING, !success) << "Failed to connect to signal " |
| 224 << signal_name << "."; | 185 << signal_name << "."; |
| 225 } | 186 } |
| 226 | 187 |
| 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 | |
| 237 // Called when a brightness change signal is received. | 188 // Called when a brightness change signal is received. |
| 238 void BrightnessChangedReceived(dbus::Signal* signal) { | 189 void BrightnessChangedReceived(dbus::Signal* signal) { |
| 239 dbus::MessageReader reader(signal); | 190 dbus::MessageReader reader(signal); |
| 240 int32 brightness_level = 0; | 191 int32 brightness_level = 0; |
| 241 bool user_initiated = 0; | 192 bool user_initiated = 0; |
| 242 if (!(reader.PopInt32(&brightness_level) && | 193 if (!(reader.PopInt32(&brightness_level) && |
| 243 reader.PopBool(&user_initiated))) { | 194 reader.PopBool(&user_initiated))) { |
| 244 LOG(ERROR) << "Brightness changed signal had incorrect parameters: " | 195 LOG(ERROR) << "Brightness changed signal had incorrect parameters: " |
| 245 << signal->ToString(); | 196 << signal->ToString(); |
| 246 return; | 197 return; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 return; | 285 return; |
| 335 } | 286 } |
| 336 if (idle_time_ms < 0) { | 287 if (idle_time_ms < 0) { |
| 337 LOG(ERROR) << "Power manager failed to calculate idle time."; | 288 LOG(ERROR) << "Power manager failed to calculate idle time."; |
| 338 callback.Run(-1); | 289 callback.Run(-1); |
| 339 return; | 290 return; |
| 340 } | 291 } |
| 341 callback.Run(idle_time_ms/1000); | 292 callback.Run(idle_time_ms/1000); |
| 342 } | 293 } |
| 343 | 294 |
| 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 | |
| 356 dbus::ObjectProxy* power_manager_proxy_; | 295 dbus::ObjectProxy* power_manager_proxy_; |
| 357 ObserverList<Observer> observers_; | 296 ObserverList<Observer> observers_; |
| 358 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 297 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
| 359 | 298 |
| 360 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 299 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
| 361 }; | 300 }; |
| 362 | 301 |
| 363 // The PowerManagerClient implementation used on Linux desktop, | 302 // The PowerManagerClient implementation used on Linux desktop, |
| 364 // which does nothing. | 303 // which does nothing. |
| 365 class PowerManagerClientStubImpl : public PowerManagerClient { | 304 class PowerManagerClientStubImpl : public PowerManagerClient { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 | 345 |
| 407 virtual void RequestRestart() OVERRIDE {} | 346 virtual void RequestRestart() OVERRIDE {} |
| 408 | 347 |
| 409 virtual void RequestShutdown() OVERRIDE {} | 348 virtual void RequestShutdown() OVERRIDE {} |
| 410 | 349 |
| 411 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) | 350 virtual void CalculateIdleTime(const CalculateIdleTimeCallback& callback) |
| 412 OVERRIDE { | 351 OVERRIDE { |
| 413 callback.Run(0); | 352 callback.Run(0); |
| 414 } | 353 } |
| 415 | 354 |
| 416 virtual void NotifyScreenLockRequested() OVERRIDE {} | |
| 417 | |
| 418 virtual void NotifyScreenLockCompleted() OVERRIDE {} | |
| 419 | |
| 420 virtual void NotifyScreenUnlockRequested() OVERRIDE {} | |
| 421 | |
| 422 virtual void NotifyScreenUnlockCompleted() OVERRIDE {} | |
| 423 | |
| 424 private: | 355 private: |
| 425 void Update() { | 356 void Update() { |
| 426 // We pause at 0 and 100% so that it's easier to check those conditions. | 357 // We pause at 0 and 100% so that it's easier to check those conditions. |
| 427 if (pause_count_ > 1) { | 358 if (pause_count_ > 1) { |
| 428 pause_count_--; | 359 pause_count_--; |
| 429 return; | 360 return; |
| 430 } | 361 } |
| 431 | 362 |
| 432 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | 363 if (battery_percentage_ == 0 || battery_percentage_ == 100) { |
| 433 if (pause_count_) { | 364 if (pause_count_) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 | 403 |
| 473 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { | 404 PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) { |
| 474 if (system::runtime_environment::IsRunningOnChromeOS()) { | 405 if (system::runtime_environment::IsRunningOnChromeOS()) { |
| 475 return new PowerManagerClientImpl(bus); | 406 return new PowerManagerClientImpl(bus); |
| 476 } else { | 407 } else { |
| 477 return new PowerManagerClientStubImpl(); | 408 return new PowerManagerClientStubImpl(); |
| 478 } | 409 } |
| 479 } | 410 } |
| 480 | 411 |
| 481 } // namespace chromeos | 412 } // namespace chromeos |
| OLD | NEW |