Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: chromeos/dbus/power_manager_client.cc

Issue 1206733002: ChromeOs Power Emulation Impl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 754 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 // invalidate its weak pointers before any other members are destroyed. 765 // invalidate its weak pointers before any other members are destroyed.
766 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; 766 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_;
767 767
768 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); 768 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl);
769 }; 769 };
770 770
771 // The PowerManagerClient implementation used on Linux desktop, 771 // The PowerManagerClient implementation used on Linux desktop,
772 // which does nothing. 772 // which does nothing.
773 class PowerManagerClientStubImpl : public PowerManagerClient { 773 class PowerManagerClientStubImpl : public PowerManagerClient {
774 public: 774 public:
775 PowerManagerClientStubImpl() 775 PowerManagerClientStubImpl()
mozartalouis 2015/06/24 00:50:18 Not sure if we should keep this class any longer s
776 : discharging_(true), 776 : brightness_(50.0),
777 battery_percentage_(40),
778 brightness_(50.0),
779 pause_count_(2),
780 cycle_count_(0),
781 num_pending_suspend_readiness_callbacks_(0), 777 num_pending_suspend_readiness_callbacks_(0),
782 weak_ptr_factory_(this) {} 778 weak_ptr_factory_(this) {}
783 779
784 ~PowerManagerClientStubImpl() override {} 780 ~PowerManagerClientStubImpl() override {}
785 781
786 int num_pending_suspend_readiness_callbacks() const { 782 int num_pending_suspend_readiness_callbacks() const {
787 return num_pending_suspend_readiness_callbacks_; 783 return num_pending_suspend_readiness_callbacks_;
788 } 784 }
789 785
790 // PowerManagerClient overrides: 786 // PowerManagerClient overrides:
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
866 int GetNumPendingSuspendReadinessCallbacks() override { 862 int GetNumPendingSuspendReadinessCallbacks() override {
867 return num_pending_suspend_readiness_callbacks_; 863 return num_pending_suspend_readiness_callbacks_;
868 } 864 }
869 865
870 private: 866 private:
871 void HandleSuspendReadiness() { 867 void HandleSuspendReadiness() {
872 num_pending_suspend_readiness_callbacks_--; 868 num_pending_suspend_readiness_callbacks_--;
873 } 869 }
874 870
875 void UpdateStatus() { 871 void UpdateStatus() {
876 if (pause_count_ > 0) {
mozartalouis 2015/06/24 00:50:18 Removed the current implementation of changing the
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. 872 const int kSecondsToEmptyFullBattery = 3 * 60 * 60; // 3 hours.
895 int64 remaining_battery_time = 873 props_.set_external_power(
896 std::max(1, battery_percentage_ * kSecondsToEmptyFullBattery / 100); 874 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
897 875 props_.set_battery_state(
898 props_.Clear(); 876 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
899 877 props_.set_battery_time_to_empty_sec(kSecondsToEmptyFullBattery);
900 switch (cycle_count_) { 878 props_.set_battery_percent(50);
901 case 0: 879 props_.set_is_calculating_battery_time(false);
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 880
940 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_)); 881 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_));
941 } 882 }
942 883
943 void SetBrightness(double percent, bool user_initiated) { 884 void SetBrightness(double percent, bool user_initiated) {
944 brightness_ = std::min(std::max(0.0, percent), 100.0); 885 brightness_ = std::min(std::max(0.0, percent), 100.0);
945 int brightness_level = static_cast<int>(brightness_); 886 int brightness_level = static_cast<int>(brightness_);
946 FOR_EACH_OBSERVER(Observer, observers_, 887 FOR_EACH_OBSERVER(Observer, observers_,
947 BrightnessChanged(brightness_level, user_initiated)); 888 BrightnessChanged(brightness_level, user_initiated));
948 } 889 }
(...skipping 15 matching lines...) Expand all
964 void ParseOption(const std::string& arg0, const std::string& arg1) { 905 void ParseOption(const std::string& arg0, const std::string& arg1) {
965 if (arg0 == "cycle" || arg0 == "interactive") { 906 if (arg0 == "cycle" || arg0 == "interactive") {
966 int seconds = 1; 907 int seconds = 1;
967 if (!arg1.empty()) 908 if (!arg1.empty())
968 base::StringToInt(arg1, &seconds); 909 base::StringToInt(arg1, &seconds);
969 power_cycle_delay_ = base::TimeDelta::FromSeconds(seconds); 910 power_cycle_delay_ = base::TimeDelta::FromSeconds(seconds);
970 } 911 }
971 } 912 }
972 913
973 base::TimeDelta power_cycle_delay_; // Time over which to cycle power state 914 base::TimeDelta power_cycle_delay_; // Time over which to cycle power state
974 bool discharging_;
975 int battery_percentage_;
976 double brightness_; 915 double brightness_;
977 int pause_count_;
978 int cycle_count_;
979 base::ObserverList<Observer> observers_; 916 base::ObserverList<Observer> observers_;
980 base::RepeatingTimer<PowerManagerClientStubImpl> update_timer_; 917 base::RepeatingTimer<PowerManagerClientStubImpl> update_timer_;
981 power_manager::PowerSupplyProperties props_; 918 power_manager::PowerSupplyProperties props_;
982 919
983 // Number of callbacks returned by GetSuspendReadinessCallback() but not yet 920 // Number of callbacks returned by GetSuspendReadinessCallback() but not yet
984 // invoked. 921 // invoked.
985 int num_pending_suspend_readiness_callbacks_; 922 int num_pending_suspend_readiness_callbacks_;
986 923
987 // Note: This should remain the last member so it'll be destroyed and 924 // Note: This should remain the last member so it'll be destroyed and
988 // invalidate its weak pointers before any other members are destroyed. 925 // invalidate its weak pointers before any other members are destroyed.
989 base::WeakPtrFactory<PowerManagerClientStubImpl> weak_ptr_factory_; 926 base::WeakPtrFactory<PowerManagerClientStubImpl> weak_ptr_factory_;
990 }; 927 };
991 928
992 PowerManagerClient::PowerManagerClient() { 929 PowerManagerClient::PowerManagerClient() {
993 } 930 }
994 931
995 PowerManagerClient::~PowerManagerClient() { 932 PowerManagerClient::~PowerManagerClient() {
996 } 933 }
997 934
998 // static 935 // static
999 PowerManagerClient* PowerManagerClient::Create( 936 PowerManagerClient* PowerManagerClient::Create(
1000 DBusClientImplementationType type) { 937 DBusClientImplementationType type) {
1001 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 938 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
1002 return new PowerManagerClientImpl(); 939 return new PowerManagerClientImpl();
1003 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 940 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
1004 return new PowerManagerClientStubImpl(); 941 return new PowerManagerClientStubImpl();
1005 } 942 }
1006 943
1007 } // namespace chromeos 944 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698