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/command_line.h" | 10 #include "base/command_line.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/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/power_monitor/power_monitor_device_source.h" | 16 #include "base/power_monitor/power_monitor_device_source.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
21 #include "base/timer/timer.h" | 21 #include "base/timer/timer.h" |
22 #include "chromeos/chromeos_switches.h" | 22 #include "chromeos/chromeos_switches.h" |
| 23 #include "chromeos/dbus/fake_power_manager_client.h" |
23 #include "chromeos/dbus/power_manager/input_event.pb.h" | 24 #include "chromeos/dbus/power_manager/input_event.pb.h" |
24 #include "chromeos/dbus/power_manager/peripheral_battery_status.pb.h" | 25 #include "chromeos/dbus/power_manager/peripheral_battery_status.pb.h" |
25 #include "chromeos/dbus/power_manager/policy.pb.h" | 26 #include "chromeos/dbus/power_manager/policy.pb.h" |
26 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | 27 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
27 #include "chromeos/dbus/power_manager/suspend.pb.h" | 28 #include "chromeos/dbus/power_manager/suspend.pb.h" |
28 #include "components/device_event_log/device_event_log.h" | 29 #include "components/device_event_log/device_event_log.h" |
29 #include "dbus/bus.h" | 30 #include "dbus/bus.h" |
30 #include "dbus/message.h" | 31 #include "dbus/message.h" |
31 #include "dbus/object_path.h" | 32 #include "dbus/object_path.h" |
32 #include "dbus/object_proxy.h" | 33 #include "dbus/object_proxy.h" |
(...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 // processes. | 762 // processes. |
762 base::WeakPtr<RenderProcessManagerDelegate> render_process_manager_delegate_; | 763 base::WeakPtr<RenderProcessManagerDelegate> render_process_manager_delegate_; |
763 | 764 |
764 // Note: This should remain the last member so it'll be destroyed and | 765 // Note: This should remain the last member so it'll be destroyed and |
765 // invalidate its weak pointers before any other members are destroyed. | 766 // invalidate its weak pointers before any other members are destroyed. |
766 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; | 767 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; |
767 | 768 |
768 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); | 769 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); |
769 }; | 770 }; |
770 | 771 |
771 // The PowerManagerClient implementation used on Linux desktop, | |
772 // which does nothing. | |
773 class PowerManagerClientStubImpl : public PowerManagerClient { | |
774 public: | |
775 PowerManagerClientStubImpl() | |
776 : discharging_(true), | |
777 battery_percentage_(40), | |
778 brightness_(50.0), | |
779 pause_count_(2), | |
780 cycle_count_(0), | |
781 num_pending_suspend_readiness_callbacks_(0), | |
782 weak_ptr_factory_(this) {} | |
783 | |
784 ~PowerManagerClientStubImpl() override {} | |
785 | |
786 int num_pending_suspend_readiness_callbacks() const { | |
787 return num_pending_suspend_readiness_callbacks_; | |
788 } | |
789 | |
790 // PowerManagerClient overrides: | |
791 void Init(dbus::Bus* bus) override { | |
792 ParseCommandLineSwitch(); | |
793 if (power_cycle_delay_ != base::TimeDelta()) { | |
794 update_timer_.Start(FROM_HERE, | |
795 power_cycle_delay_, | |
796 this, | |
797 &PowerManagerClientStubImpl::UpdateStatus); | |
798 } | |
799 } | |
800 | |
801 void AddObserver(Observer* observer) override { | |
802 observers_.AddObserver(observer); | |
803 } | |
804 | |
805 void RemoveObserver(Observer* observer) override { | |
806 observers_.RemoveObserver(observer); | |
807 } | |
808 | |
809 bool HasObserver(const Observer* observer) const override { | |
810 return observers_.HasObserver(observer); | |
811 } | |
812 | |
813 void SetRenderProcessManagerDelegate( | |
814 base::WeakPtr<RenderProcessManagerDelegate> delegate) override {} | |
815 | |
816 void DecreaseScreenBrightness(bool allow_off) override { | |
817 POWER_LOG(USER) << "Requested to descrease screen brightness"; | |
818 SetBrightness(brightness_ - 5.0, true); | |
819 } | |
820 | |
821 void IncreaseScreenBrightness() override { | |
822 POWER_LOG(USER) << "Requested to increase screen brightness"; | |
823 SetBrightness(brightness_ + 5.0, true); | |
824 } | |
825 | |
826 void SetScreenBrightnessPercent(double percent, bool gradual) override { | |
827 POWER_LOG(USER) << "Requested to set screen brightness to " << percent | |
828 << "% " << (gradual ? "gradually" : "instantaneously"); | |
829 SetBrightness(percent, false); | |
830 } | |
831 | |
832 void GetScreenBrightnessPercent( | |
833 const GetScreenBrightnessPercentCallback& callback) override { | |
834 POWER_LOG(USER) << "Requested to get screen brightness"; | |
835 callback.Run(brightness_); | |
836 } | |
837 | |
838 void DecreaseKeyboardBrightness() override { | |
839 POWER_LOG(USER) << "Requested to descrease keyboard brightness"; | |
840 } | |
841 | |
842 void IncreaseKeyboardBrightness() override { | |
843 POWER_LOG(USER) << "Requested to increase keyboard brightness"; | |
844 } | |
845 | |
846 void RequestStatusUpdate() override { | |
847 POWER_LOG(USER) << "Requested status update"; | |
848 base::MessageLoop::current()->PostTask( | |
849 FROM_HERE, base::Bind(&PowerManagerClientStubImpl::UpdateStatus, | |
850 weak_ptr_factory_.GetWeakPtr())); | |
851 } | |
852 | |
853 void RequestSuspend() override {} | |
854 void RequestRestart() override {} | |
855 void RequestShutdown() override {} | |
856 | |
857 void NotifyUserActivity(power_manager::UserActivityType type) override {} | |
858 void NotifyVideoActivity(bool is_fullscreen) override {} | |
859 void SetPolicy(const power_manager::PowerManagementPolicy& policy) override {} | |
860 void SetIsProjecting(bool is_projecting) override {} | |
861 base::Closure GetSuspendReadinessCallback() override { | |
862 num_pending_suspend_readiness_callbacks_++; | |
863 return base::Bind(&PowerManagerClientStubImpl::HandleSuspendReadiness, | |
864 weak_ptr_factory_.GetWeakPtr()); | |
865 } | |
866 int GetNumPendingSuspendReadinessCallbacks() override { | |
867 return num_pending_suspend_readiness_callbacks_; | |
868 } | |
869 | |
870 private: | |
871 void HandleSuspendReadiness() { | |
872 num_pending_suspend_readiness_callbacks_--; | |
873 } | |
874 | |
875 void UpdateStatus() { | |
876 if (pause_count_ > 0) { | |
877 pause_count_--; | |
878 if (pause_count_ == 2) | |
879 discharging_ = !discharging_; | |
880 } else { | |
881 if (discharging_) | |
882 battery_percentage_ -= (battery_percentage_ <= 10 ? 1 : 10); | |
883 else | |
884 battery_percentage_ += (battery_percentage_ >= 10 ? 10 : 1); | |
885 battery_percentage_ = std::min(std::max(battery_percentage_, 0), 100); | |
886 // We pause at 0 and 100% so that it's easier to check those conditions. | |
887 if (battery_percentage_ == 0 || battery_percentage_ == 100) { | |
888 pause_count_ = 4; | |
889 if (battery_percentage_ == 100) | |
890 cycle_count_ = (cycle_count_ + 1) % 3; | |
891 } | |
892 } | |
893 | |
894 const int kSecondsToEmptyFullBattery = 3 * 60 * 60; // 3 hours. | |
895 int64 remaining_battery_time = | |
896 std::max(1, battery_percentage_ * kSecondsToEmptyFullBattery / 100); | |
897 | |
898 props_.Clear(); | |
899 | |
900 switch (cycle_count_) { | |
901 case 0: | |
902 // Say that the system is charging with AC connected and | |
903 // discharging without any charger connected. | |
904 props_.set_external_power(discharging_ ? | |
905 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED : | |
906 power_manager::PowerSupplyProperties_ExternalPower_AC); | |
907 break; | |
908 case 1: | |
909 // Say that the system is both charging and discharging on USB | |
910 // (i.e. a low-power charger). | |
911 props_.set_external_power( | |
912 power_manager::PowerSupplyProperties_ExternalPower_USB); | |
913 break; | |
914 case 2: | |
915 // Say that the system is both charging and discharging on AC. | |
916 props_.set_external_power( | |
917 power_manager::PowerSupplyProperties_ExternalPower_AC); | |
918 break; | |
919 default: | |
920 NOTREACHED() << "Unhandled cycle " << cycle_count_; | |
921 } | |
922 | |
923 if (battery_percentage_ == 100 && !discharging_) { | |
924 props_.set_battery_state( | |
925 power_manager::PowerSupplyProperties_BatteryState_FULL); | |
926 } else if (!discharging_) { | |
927 props_.set_battery_state( | |
928 power_manager::PowerSupplyProperties_BatteryState_CHARGING); | |
929 props_.set_battery_time_to_full_sec(std::max(static_cast<int64>(1), | |
930 kSecondsToEmptyFullBattery - remaining_battery_time)); | |
931 } else { | |
932 props_.set_battery_state( | |
933 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); | |
934 props_.set_battery_time_to_empty_sec(remaining_battery_time); | |
935 } | |
936 | |
937 props_.set_battery_percent(battery_percentage_); | |
938 props_.set_is_calculating_battery_time(pause_count_ > 1); | |
939 | |
940 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_)); | |
941 } | |
942 | |
943 void SetBrightness(double percent, bool user_initiated) { | |
944 brightness_ = std::min(std::max(0.0, percent), 100.0); | |
945 int brightness_level = static_cast<int>(brightness_); | |
946 FOR_EACH_OBSERVER(Observer, observers_, | |
947 BrightnessChanged(brightness_level, user_initiated)); | |
948 } | |
949 | |
950 void ParseCommandLineSwitch() { | |
951 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
952 if (!command_line || !command_line->HasSwitch(switches::kPowerStub)) | |
953 return; | |
954 std::string option_str = | |
955 command_line->GetSwitchValueASCII(switches::kPowerStub); | |
956 base::StringPairs string_pairs; | |
957 base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs); | |
958 for (base::StringPairs::iterator iter = string_pairs.begin(); | |
959 iter != string_pairs.end(); ++iter) { | |
960 ParseOption((*iter).first, (*iter).second); | |
961 } | |
962 } | |
963 | |
964 void ParseOption(const std::string& arg0, const std::string& arg1) { | |
965 if (arg0 == "cycle" || arg0 == "interactive") { | |
966 int seconds = 1; | |
967 if (!arg1.empty()) | |
968 base::StringToInt(arg1, &seconds); | |
969 power_cycle_delay_ = base::TimeDelta::FromSeconds(seconds); | |
970 } | |
971 } | |
972 | |
973 base::TimeDelta power_cycle_delay_; // Time over which to cycle power state | |
974 bool discharging_; | |
975 int battery_percentage_; | |
976 double brightness_; | |
977 int pause_count_; | |
978 int cycle_count_; | |
979 base::ObserverList<Observer> observers_; | |
980 base::RepeatingTimer<PowerManagerClientStubImpl> update_timer_; | |
981 power_manager::PowerSupplyProperties props_; | |
982 | |
983 // Number of callbacks returned by GetSuspendReadinessCallback() but not yet | |
984 // invoked. | |
985 int num_pending_suspend_readiness_callbacks_; | |
986 | |
987 // Note: This should remain the last member so it'll be destroyed and | |
988 // invalidate its weak pointers before any other members are destroyed. | |
989 base::WeakPtrFactory<PowerManagerClientStubImpl> weak_ptr_factory_; | |
990 }; | |
991 | |
992 PowerManagerClient::PowerManagerClient() { | 772 PowerManagerClient::PowerManagerClient() { |
993 } | 773 } |
994 | 774 |
995 PowerManagerClient::~PowerManagerClient() { | 775 PowerManagerClient::~PowerManagerClient() { |
996 } | 776 } |
997 | 777 |
998 // static | 778 // static |
999 PowerManagerClient* PowerManagerClient::Create( | 779 PowerManagerClient* PowerManagerClient::Create( |
1000 DBusClientImplementationType type) { | 780 DBusClientImplementationType type) { |
1001 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 781 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
1002 return new PowerManagerClientImpl(); | 782 return new PowerManagerClientImpl(); |
1003 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 783 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
1004 return new PowerManagerClientStubImpl(); | 784 return new FakePowerManagerClient(); |
1005 } | 785 } |
1006 | 786 |
1007 } // namespace chromeos | 787 } // namespace chromeos |
OLD | NEW |