Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/system/chromeos/emulator/battery_emulation_controller.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/observer_list.h" | |
| 9 #include "base/power_monitor/power_monitor_device_source.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_split.h" | |
| 12 #include "chromeos/chromeos_switches.h" | |
| 13 #include "chromeos/dbus/power_manager/input_event.pb.h" | |
| 14 #include "chromeos/dbus/power_manager/peripheral_battery_status.pb.h" | |
| 15 #include "chromeos/dbus/power_manager/policy.pb.h" | |
| 16 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
| 17 #include "chromeos/dbus/power_manager/suspend.pb.h" | |
| 18 #include "chromeos/dbus/power_manager_client.h" | |
| 19 | |
| 20 namespace ash { | |
| 21 | |
| 22 BatteryEmulationController::BatteryEmulationController() | |
| 23 : discharging_(true), | |
| 24 brightness_(50), | |
| 25 battery_percentage_(50), | |
| 26 is_calculating_(false), | |
| 27 power_source_(0), | |
| 28 num_pending_suspend_readiness_callbacks_(0), | |
| 29 weak_ptr_factory_(this) { | |
| 30 } | |
| 31 | |
| 32 BatteryEmulationController::~BatteryEmulationController() { | |
| 33 } | |
| 34 | |
| 35 void BatteryEmulationController::Init(dbus::Bus* bus) { | |
| 36 ParseCommandLineSwitch(); | |
| 37 if (power_cycle_delay_ != base::TimeDelta()) { | |
| 38 update_timer_.Start(FROM_HERE, power_cycle_delay_, this, | |
| 39 &BatteryEmulationController::UpdateStatus); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 void BatteryEmulationController::AddObserver(Observer* observer) { | |
| 44 observers_.AddObserver(observer); | |
| 45 } | |
| 46 | |
| 47 void BatteryEmulationController::RemoveObserver(Observer* observer) { | |
| 48 observers_.RemoveObserver(observer); | |
| 49 } | |
| 50 | |
| 51 bool BatteryEmulationController::HasObserver(const Observer* observer) const { | |
| 52 return observers_.HasObserver(observer); | |
| 53 } | |
| 54 | |
| 55 void BatteryEmulationController::SetBatteryCharging(bool charge) { | |
| 56 discharging_ = charge; | |
| 57 } | |
| 58 | |
| 59 void BatteryEmulationController::SetBatteryPercentage(int percentage) { | |
| 60 battery_percentage_ = percentage; | |
| 61 } | |
| 62 | |
| 63 void BatteryEmulationController::SetBatteryFullTimeToEmpty(int time_in_hours) { | |
| 64 hours_to_empty_full_battery = time_in_hours * 60 * 60; | |
| 65 } | |
| 66 | |
| 67 void BatteryEmulationController::SetExternalPower(int power_source) { | |
| 68 power_source_ = power_source; | |
| 69 } | |
| 70 | |
| 71 void BatteryEmulationController::SetIsCalculatingBatteryTime(bool calculating) { | |
| 72 is_calculating_ = calculating; | |
| 73 } | |
| 74 | |
| 75 void BatteryEmulationController::DecreaseScreenBrightness(bool allow_off) { | |
|
mozartalouis
2015/06/24 00:50:18
Will we be needing Brightness features for the emu
| |
| 76 // POWER_LOG(USER) << "Requested to descrease screen brightness"; | |
| 77 SetBrightness(brightness_ - 5.0, true); | |
| 78 } | |
| 79 | |
| 80 void BatteryEmulationController::IncreaseScreenBrightness() { | |
| 81 // POWER_LOG(USER) << "Requested to increase screen brightness"; | |
| 82 SetBrightness(brightness_ + 5.0, true); | |
| 83 } | |
| 84 | |
| 85 void BatteryEmulationController::SetScreenBrightnessPercent(double percent, | |
| 86 bool gradual) { | |
| 87 // POWER_LOG(USER) << "Requested to set screen brightness to " << percent << | |
| 88 // "% " | |
| 89 // << (gradual ? "gradually" : "instantaneously"); | |
| 90 SetBrightness(percent, false); | |
| 91 } | |
| 92 | |
| 93 void BatteryEmulationController::GetScreenBrightnessPercent( | |
| 94 const chromeos::GetScreenBrightnessPercentCallback& callback) { | |
| 95 // POWER_LOG(USER) << "Requested to get screen brightness"; | |
| 96 callback.Run(brightness_); | |
| 97 } | |
| 98 | |
| 99 void BatteryEmulationController::DecreaseKeyboardBrightness() { | |
| 100 // POWER_LOG(USER) << "Requested to descrease keyboard brightness"; | |
| 101 } | |
| 102 | |
| 103 void BatteryEmulationController::IncreaseKeyboardBrightness() { | |
| 104 // POWER_LOG(USER) << "Requested to increase keyboard brightness"; | |
| 105 } | |
| 106 | |
| 107 void BatteryEmulationController::RequestStatusUpdate() { | |
| 108 // POWER_LOG(USER) << "Requested status update"; | |
| 109 base::MessageLoop::current()->PostTask( | |
| 110 FROM_HERE, base::Bind(&BatteryEmulationController::UpdateStatus, | |
| 111 weak_ptr_factory_.GetWeakPtr())); | |
| 112 } | |
| 113 | |
| 114 base::Closure BatteryEmulationController::GetSuspendReadinessCallback() { | |
| 115 num_pending_suspend_readiness_callbacks_++; | |
| 116 return base::Bind(&BatteryEmulationController::HandleSuspendReadiness, | |
| 117 weak_ptr_factory_.GetWeakPtr()); | |
| 118 } | |
| 119 | |
| 120 int BatteryEmulationController::GetNumPendingSuspendReadinessCallbacks() { | |
| 121 return num_pending_suspend_readiness_callbacks_; | |
| 122 } | |
| 123 | |
| 124 void BatteryEmulationController::HandleSuspendReadiness() { | |
| 125 num_pending_suspend_readiness_callbacks_--; | |
| 126 } | |
| 127 | |
| 128 void BatteryEmulationController::UpdateStatus() { | |
| 129 int64 remaining_battery_time = | |
| 130 std::max(1, battery_percentage_ * hours_to_empty_full_battery / 100); | |
| 131 | |
| 132 props_.Clear(); | |
| 133 | |
| 134 switch (power_source_) { | |
| 135 case 0: | |
| 136 // Say that the system is charging with AC connected and | |
| 137 // discharging without any charger connected. | |
| 138 props_.set_external_power( | |
| 139 discharging_ | |
| 140 ? power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED | |
| 141 : power_manager::PowerSupplyProperties_ExternalPower_AC); | |
| 142 break; | |
| 143 case 1: | |
| 144 // Say that the system is both charging and discharging on USB | |
| 145 // (i.e. a low-power charger). | |
| 146 props_.set_external_power( | |
| 147 power_manager::PowerSupplyProperties_ExternalPower_USB); | |
| 148 break; | |
| 149 case 2: | |
| 150 // Say that the system is both charging and discharging on AC. | |
| 151 props_.set_external_power( | |
| 152 power_manager::PowerSupplyProperties_ExternalPower_AC); | |
| 153 break; | |
| 154 default: | |
| 155 NOTREACHED() << "Unhandled power source " << power_source_; | |
| 156 } | |
| 157 | |
| 158 if (battery_percentage_ == 100 && !discharging_) { | |
| 159 props_.set_battery_state( | |
| 160 power_manager::PowerSupplyProperties_BatteryState_FULL); | |
| 161 } else if (!discharging_) { | |
| 162 props_.set_battery_state( | |
| 163 power_manager::PowerSupplyProperties_BatteryState_CHARGING); | |
| 164 props_.set_battery_time_to_full_sec( | |
| 165 std::max(static_cast<int64>(1), | |
| 166 hours_to_empty_full_battery - remaining_battery_time)); | |
| 167 } else { | |
| 168 props_.set_battery_state( | |
| 169 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); | |
| 170 props_.set_battery_time_to_empty_sec(remaining_battery_time); | |
| 171 } | |
| 172 | |
| 173 props_.set_battery_percent(battery_percentage_); | |
| 174 props_.set_is_calculating_battery_time(is_calculating_); | |
| 175 | |
| 176 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_)); | |
| 177 } | |
| 178 | |
| 179 void BatteryEmulationController::SetBrightness(double percent, | |
| 180 bool user_initiated) { | |
| 181 brightness_ = std::min(std::max(0.0, percent), 100.0); | |
| 182 int brightness_level = static_cast<int>(brightness_); | |
| 183 FOR_EACH_OBSERVER(Observer, observers_, | |
| 184 BrightnessChanged(brightness_level, user_initiated)); | |
| 185 } | |
| 186 | |
| 187 void BatteryEmulationController::ParseCommandLineSwitch() { | |
| 188 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 189 if (!command_line || !command_line->HasSwitch(chromeos::switches::kPowerStub)) | |
| 190 return; | |
| 191 std::string option_str = | |
| 192 command_line->GetSwitchValueASCII(chromeos::switches::kPowerStub); | |
| 193 base::StringPairs string_pairs; | |
| 194 base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs); | |
| 195 for (base::StringPairs::iterator iter = string_pairs.begin(); | |
| 196 iter != string_pairs.end(); ++iter) { | |
| 197 ParseOption((*iter).first, (*iter).second); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 void BatteryEmulationController::ParseOption(const std::string& arg0, | |
| 202 const std::string& arg1) { | |
| 203 if (arg0 == "cycle" || arg0 == "interactive") { | |
| 204 int seconds = 1; | |
| 205 if (!arg1.empty()) | |
| 206 base::StringToInt(arg1, &seconds); | |
| 207 power_cycle_delay_ = base::TimeDelta::FromSeconds(seconds); | |
| 208 } | |
| 209 } | |
| 210 } // namespace ash | |
| OLD | NEW |