| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/dbus/power_manager_client.h" | 5 #include "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/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "base/timer.h" | 19 #include "base/timer.h" |
| 20 #include "chromeos/dbus/power_manager/input_event.pb.h" | 20 #include "chromeos/dbus/power_manager/input_event.pb.h" |
| 21 #include "chromeos/dbus/power_manager/policy.pb.h" | 21 #include "chromeos/dbus/power_manager/policy.pb.h" |
| 22 #include "chromeos/dbus/power_manager/suspend.pb.h" | 22 #include "chromeos/dbus/power_manager/suspend.pb.h" |
| 23 #include "chromeos/dbus/power_state_control.pb.h" | |
| 24 #include "chromeos/dbus/power_supply_properties.pb.h" | 23 #include "chromeos/dbus/power_supply_properties.pb.h" |
| 25 #include "chromeos/dbus/video_activity_update.pb.h" | 24 #include "chromeos/dbus/video_activity_update.pb.h" |
| 26 #include "dbus/bus.h" | 25 #include "dbus/bus.h" |
| 27 #include "dbus/message.h" | 26 #include "dbus/message.h" |
| 28 #include "dbus/object_path.h" | 27 #include "dbus/object_path.h" |
| 29 #include "dbus/object_proxy.h" | 28 #include "dbus/object_proxy.h" |
| 30 #include "third_party/cros_system_api/dbus/service_constants.h" | 29 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 31 | 30 |
| 32 namespace chromeos { | 31 namespace chromeos { |
| 33 | 32 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 if (!writer.AppendProtoAsArrayOfBytes(policy)) { | 300 if (!writer.AppendProtoAsArrayOfBytes(policy)) { |
| 302 LOG(ERROR) << "Error calling " << power_manager::kSetPolicyMethod; | 301 LOG(ERROR) << "Error calling " << power_manager::kSetPolicyMethod; |
| 303 return; | 302 return; |
| 304 } | 303 } |
| 305 power_manager_proxy_->CallMethod( | 304 power_manager_proxy_->CallMethod( |
| 306 &method_call, | 305 &method_call, |
| 307 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 306 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 308 dbus::ObjectProxy::EmptyResponseCallback()); | 307 dbus::ObjectProxy::EmptyResponseCallback()); |
| 309 } | 308 } |
| 310 | 309 |
| 311 virtual void RequestPowerStateOverrides( | |
| 312 uint32 request_id, | |
| 313 base::TimeDelta duration, | |
| 314 int overrides, | |
| 315 const PowerStateRequestIdCallback& callback) OVERRIDE { | |
| 316 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | |
| 317 power_manager::kStateOverrideRequest); | |
| 318 dbus::MessageWriter writer(&method_call); | |
| 319 | |
| 320 PowerStateControl protobuf; | |
| 321 protobuf.set_request_id(request_id); | |
| 322 protobuf.set_duration(duration.InSeconds()); | |
| 323 protobuf.set_disable_idle_dim(overrides & DISABLE_IDLE_DIM); | |
| 324 protobuf.set_disable_idle_blank(overrides & DISABLE_IDLE_BLANK); | |
| 325 protobuf.set_disable_idle_suspend(overrides & DISABLE_IDLE_SUSPEND); | |
| 326 protobuf.set_disable_lid_suspend(overrides & DISABLE_LID_SUSPEND); | |
| 327 | |
| 328 if (!writer.AppendProtoAsArrayOfBytes(protobuf)) { | |
| 329 LOG(ERROR) << "Error calling " | |
| 330 << power_manager::kStateOverrideRequest; | |
| 331 return; | |
| 332 } | |
| 333 power_manager_proxy_->CallMethod( | |
| 334 &method_call, | |
| 335 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 336 base::Bind(&PowerManagerClientImpl::OnPowerStateOverride, | |
| 337 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 338 } | |
| 339 | |
| 340 virtual void CancelPowerStateOverrides(uint32 request_id) OVERRIDE { | |
| 341 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | |
| 342 power_manager::kStateOverrideCancel); | |
| 343 dbus::MessageWriter writer(&method_call); | |
| 344 writer.AppendInt32(request_id); | |
| 345 power_manager_proxy_->CallMethod( | |
| 346 &method_call, | |
| 347 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 348 dbus::ObjectProxy::EmptyResponseCallback()); | |
| 349 } | |
| 350 | |
| 351 virtual void SetIsProjecting(bool is_projecting) OVERRIDE { | 310 virtual void SetIsProjecting(bool is_projecting) OVERRIDE { |
| 352 dbus::MethodCall method_call( | 311 dbus::MethodCall method_call( |
| 353 power_manager::kPowerManagerInterface, | 312 power_manager::kPowerManagerInterface, |
| 354 power_manager::kSetIsProjectingMethod); | 313 power_manager::kSetIsProjectingMethod); |
| 355 dbus::MessageWriter writer(&method_call); | 314 dbus::MessageWriter writer(&method_call); |
| 356 writer.AppendBool(is_projecting); | 315 writer.AppendBool(is_projecting); |
| 357 power_manager_proxy_->CallMethod( | 316 power_manager_proxy_->CallMethod( |
| 358 &method_call, | 317 &method_call, |
| 359 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 318 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 360 dbus::ObjectProxy::EmptyResponseCallback()); | 319 dbus::ObjectProxy::EmptyResponseCallback()); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 status.battery_percentage = protobuf.battery_percentage(); | 421 status.battery_percentage = protobuf.battery_percentage(); |
| 463 status.battery_is_present = protobuf.battery_is_present(); | 422 status.battery_is_present = protobuf.battery_is_present(); |
| 464 status.battery_is_full = protobuf.battery_is_charged(); | 423 status.battery_is_full = protobuf.battery_is_charged(); |
| 465 status.is_calculating_battery_time = protobuf.is_calculating_battery_time(); | 424 status.is_calculating_battery_time = protobuf.is_calculating_battery_time(); |
| 466 status.battery_energy_rate = protobuf.battery_energy_rate(); | 425 status.battery_energy_rate = protobuf.battery_energy_rate(); |
| 467 | 426 |
| 468 VLOG(1) << "Power status: " << status.ToString(); | 427 VLOG(1) << "Power status: " << status.ToString(); |
| 469 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | 428 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
| 470 } | 429 } |
| 471 | 430 |
| 472 void OnPowerStateOverride(const PowerStateRequestIdCallback& callback, | |
| 473 dbus::Response* response) { | |
| 474 if (!response) { | |
| 475 LOG(ERROR) << "Error calling " << power_manager::kStateOverrideRequest; | |
| 476 return; | |
| 477 } | |
| 478 | |
| 479 dbus::MessageReader reader(response); | |
| 480 int32 request_id = 0; | |
| 481 if (!reader.PopInt32(&request_id)) { | |
| 482 LOG(ERROR) << "Error reading response from powerd: " | |
| 483 << response->ToString(); | |
| 484 callback.Run(0); | |
| 485 return; | |
| 486 } | |
| 487 | |
| 488 callback.Run(request_id); | |
| 489 } | |
| 490 | |
| 491 void OnGetScreenBrightnessPercent( | 431 void OnGetScreenBrightnessPercent( |
| 492 const GetScreenBrightnessPercentCallback& callback, | 432 const GetScreenBrightnessPercentCallback& callback, |
| 493 dbus::Response* response) { | 433 dbus::Response* response) { |
| 494 if (!response) { | 434 if (!response) { |
| 495 LOG(ERROR) << "Error calling " | 435 LOG(ERROR) << "Error calling " |
| 496 << power_manager::kGetScreenBrightnessPercent; | 436 << power_manager::kGetScreenBrightnessPercent; |
| 497 return; | 437 return; |
| 498 } | 438 } |
| 499 dbus::MessageReader reader(response); | 439 dbus::MessageReader reader(response); |
| 500 double percent = 0.0; | 440 double percent = 0.0; |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 }; | 702 }; |
| 763 | 703 |
| 764 // The PowerManagerClient implementation used on Linux desktop, | 704 // The PowerManagerClient implementation used on Linux desktop, |
| 765 // which does nothing. | 705 // which does nothing. |
| 766 class PowerManagerClientStubImpl : public PowerManagerClient { | 706 class PowerManagerClientStubImpl : public PowerManagerClient { |
| 767 public: | 707 public: |
| 768 PowerManagerClientStubImpl() | 708 PowerManagerClientStubImpl() |
| 769 : discharging_(true), | 709 : discharging_(true), |
| 770 battery_percentage_(40), | 710 battery_percentage_(40), |
| 771 brightness_(50.0), | 711 brightness_(50.0), |
| 772 pause_count_(2), | 712 pause_count_(2) { |
| 773 next_request_id_(1) { | |
| 774 } | 713 } |
| 775 | 714 |
| 776 virtual ~PowerManagerClientStubImpl() {} | 715 virtual ~PowerManagerClientStubImpl() {} |
| 777 | 716 |
| 778 // PowerManagerClient overrides: | 717 // PowerManagerClient overrides: |
| 779 | 718 |
| 780 virtual void AddObserver(Observer* observer) OVERRIDE { | 719 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 781 observers_.AddObserver(observer); | 720 observers_.AddObserver(observer); |
| 782 } | 721 } |
| 783 | 722 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 base::TimeDelta::FromMilliseconds(threshold)); | 786 base::TimeDelta::FromMilliseconds(threshold)); |
| 848 } | 787 } |
| 849 | 788 |
| 850 virtual void NotifyUserActivity( | 789 virtual void NotifyUserActivity( |
| 851 const base::TimeTicks& last_activity_time) OVERRIDE {} | 790 const base::TimeTicks& last_activity_time) OVERRIDE {} |
| 852 virtual void NotifyVideoActivity( | 791 virtual void NotifyVideoActivity( |
| 853 const base::TimeTicks& last_activity_time, | 792 const base::TimeTicks& last_activity_time, |
| 854 bool is_fullscreen) OVERRIDE {} | 793 bool is_fullscreen) OVERRIDE {} |
| 855 virtual void SetPolicy( | 794 virtual void SetPolicy( |
| 856 const power_manager::PowerManagementPolicy& policy) OVERRIDE {} | 795 const power_manager::PowerManagementPolicy& policy) OVERRIDE {} |
| 857 virtual void RequestPowerStateOverrides( | |
| 858 uint32 request_id, | |
| 859 base::TimeDelta duration, | |
| 860 int overrides, | |
| 861 const PowerStateRequestIdCallback& callback) OVERRIDE { | |
| 862 // Mimic the behavior of power manager w.r.t. the request_id. | |
| 863 if (request_id == 0) { | |
| 864 callback.Run(next_request_id_++); | |
| 865 } else { | |
| 866 callback.Run(request_id); | |
| 867 } | |
| 868 } | |
| 869 virtual void CancelPowerStateOverrides(uint32 request_id) OVERRIDE {} | |
| 870 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {} | 796 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {} |
| 871 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE { | 797 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE { |
| 872 return base::Closure(); | 798 return base::Closure(); |
| 873 } | 799 } |
| 874 | 800 |
| 875 private: | 801 private: |
| 876 void Update() { | 802 void Update() { |
| 877 if (pause_count_ > 0) { | 803 if (pause_count_ > 0) { |
| 878 pause_count_--; | 804 pause_count_--; |
| 879 } else { | 805 } else { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 915 FOR_EACH_OBSERVER(Observer, observers_, IdleNotify(threshold)); | 841 FOR_EACH_OBSERVER(Observer, observers_, IdleNotify(threshold)); |
| 916 } | 842 } |
| 917 | 843 |
| 918 bool discharging_; | 844 bool discharging_; |
| 919 int battery_percentage_; | 845 int battery_percentage_; |
| 920 double brightness_; | 846 double brightness_; |
| 921 int pause_count_; | 847 int pause_count_; |
| 922 ObserverList<Observer> observers_; | 848 ObserverList<Observer> observers_; |
| 923 base::RepeatingTimer<PowerManagerClientStubImpl> timer_; | 849 base::RepeatingTimer<PowerManagerClientStubImpl> timer_; |
| 924 PowerSupplyStatus status_; | 850 PowerSupplyStatus status_; |
| 925 uint32 next_request_id_; | |
| 926 }; | 851 }; |
| 927 | 852 |
| 928 PowerManagerClient::PowerManagerClient() { | 853 PowerManagerClient::PowerManagerClient() { |
| 929 } | 854 } |
| 930 | 855 |
| 931 PowerManagerClient::~PowerManagerClient() { | 856 PowerManagerClient::~PowerManagerClient() { |
| 932 } | 857 } |
| 933 | 858 |
| 859 // static |
| 934 PowerManagerClient* PowerManagerClient::Create( | 860 PowerManagerClient* PowerManagerClient::Create( |
| 935 DBusClientImplementationType type, | 861 DBusClientImplementationType type, |
| 936 dbus::Bus* bus) { | 862 dbus::Bus* bus) { |
| 937 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 863 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 938 return new PowerManagerClientImpl(bus); | 864 return new PowerManagerClientImpl(bus); |
| 939 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 865 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 940 return new PowerManagerClientStubImpl(); | 866 return new PowerManagerClientStubImpl(); |
| 941 } | 867 } |
| 942 | 868 |
| 943 } // namespace chromeos | 869 } // namespace chromeos |
| OLD | NEW |