Chromium Code Reviews| Index: ash/system/chromeos/emulator/battery_emulation_controller.cc |
| diff --git a/ash/system/chromeos/emulator/battery_emulation_controller.cc b/ash/system/chromeos/emulator/battery_emulation_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..51cdb1e8add7714c2cb16c5a8125160b36523e2b |
| --- /dev/null |
| +++ b/ash/system/chromeos/emulator/battery_emulation_controller.cc |
| @@ -0,0 +1,210 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/system/chromeos/emulator/battery_emulation_controller.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/observer_list.h" |
| +#include "base/power_monitor/power_monitor_device_source.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "chromeos/chromeos_switches.h" |
| +#include "chromeos/dbus/power_manager/input_event.pb.h" |
| +#include "chromeos/dbus/power_manager/peripheral_battery_status.pb.h" |
| +#include "chromeos/dbus/power_manager/policy.pb.h" |
| +#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
| +#include "chromeos/dbus/power_manager/suspend.pb.h" |
| +#include "chromeos/dbus/power_manager_client.h" |
| + |
| +namespace ash { |
| + |
| +BatteryEmulationController::BatteryEmulationController() |
| + : discharging_(true), |
| + brightness_(50), |
| + battery_percentage_(50), |
| + is_calculating_(false), |
| + power_source_(0), |
| + num_pending_suspend_readiness_callbacks_(0), |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +BatteryEmulationController::~BatteryEmulationController() { |
| +} |
| + |
| +void BatteryEmulationController::Init(dbus::Bus* bus) { |
| + ParseCommandLineSwitch(); |
| + if (power_cycle_delay_ != base::TimeDelta()) { |
| + update_timer_.Start(FROM_HERE, power_cycle_delay_, this, |
| + &BatteryEmulationController::UpdateStatus); |
| + } |
| +} |
| + |
| +void BatteryEmulationController::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void BatteryEmulationController::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +bool BatteryEmulationController::HasObserver(const Observer* observer) const { |
| + return observers_.HasObserver(observer); |
| +} |
| + |
| +void BatteryEmulationController::SetBatteryCharging(bool charge) { |
| + discharging_ = charge; |
| +} |
| + |
| +void BatteryEmulationController::SetBatteryPercentage(int percentage) { |
| + battery_percentage_ = percentage; |
| +} |
| + |
| +void BatteryEmulationController::SetBatteryFullTimeToEmpty(int time_in_hours) { |
| + hours_to_empty_full_battery = time_in_hours * 60 * 60; |
| +} |
| + |
| +void BatteryEmulationController::SetExternalPower(int power_source) { |
| + power_source_ = power_source; |
| +} |
| + |
| +void BatteryEmulationController::SetIsCalculatingBatteryTime(bool calculating) { |
| + is_calculating_ = calculating; |
| +} |
| + |
| +void BatteryEmulationController::DecreaseScreenBrightness(bool allow_off) { |
|
mozartalouis
2015/06/24 00:50:18
Will we be needing Brightness features for the emu
|
| + // POWER_LOG(USER) << "Requested to descrease screen brightness"; |
| + SetBrightness(brightness_ - 5.0, true); |
| +} |
| + |
| +void BatteryEmulationController::IncreaseScreenBrightness() { |
| + // POWER_LOG(USER) << "Requested to increase screen brightness"; |
| + SetBrightness(brightness_ + 5.0, true); |
| +} |
| + |
| +void BatteryEmulationController::SetScreenBrightnessPercent(double percent, |
| + bool gradual) { |
| + // POWER_LOG(USER) << "Requested to set screen brightness to " << percent << |
| + // "% " |
| + // << (gradual ? "gradually" : "instantaneously"); |
| + SetBrightness(percent, false); |
| +} |
| + |
| +void BatteryEmulationController::GetScreenBrightnessPercent( |
| + const chromeos::GetScreenBrightnessPercentCallback& callback) { |
| + // POWER_LOG(USER) << "Requested to get screen brightness"; |
| + callback.Run(brightness_); |
| +} |
| + |
| +void BatteryEmulationController::DecreaseKeyboardBrightness() { |
| + // POWER_LOG(USER) << "Requested to descrease keyboard brightness"; |
| +} |
| + |
| +void BatteryEmulationController::IncreaseKeyboardBrightness() { |
| + // POWER_LOG(USER) << "Requested to increase keyboard brightness"; |
| +} |
| + |
| +void BatteryEmulationController::RequestStatusUpdate() { |
| + // POWER_LOG(USER) << "Requested status update"; |
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, base::Bind(&BatteryEmulationController::UpdateStatus, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +base::Closure BatteryEmulationController::GetSuspendReadinessCallback() { |
| + num_pending_suspend_readiness_callbacks_++; |
| + return base::Bind(&BatteryEmulationController::HandleSuspendReadiness, |
| + weak_ptr_factory_.GetWeakPtr()); |
| +} |
| + |
| +int BatteryEmulationController::GetNumPendingSuspendReadinessCallbacks() { |
| + return num_pending_suspend_readiness_callbacks_; |
| +} |
| + |
| +void BatteryEmulationController::HandleSuspendReadiness() { |
| + num_pending_suspend_readiness_callbacks_--; |
| +} |
| + |
| +void BatteryEmulationController::UpdateStatus() { |
| + int64 remaining_battery_time = |
| + std::max(1, battery_percentage_ * hours_to_empty_full_battery / 100); |
| + |
| + props_.Clear(); |
| + |
| + switch (power_source_) { |
| + case 0: |
| + // Say that the system is charging with AC connected and |
| + // discharging without any charger connected. |
| + props_.set_external_power( |
| + discharging_ |
| + ? power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED |
| + : power_manager::PowerSupplyProperties_ExternalPower_AC); |
| + break; |
| + case 1: |
| + // Say that the system is both charging and discharging on USB |
| + // (i.e. a low-power charger). |
| + props_.set_external_power( |
| + power_manager::PowerSupplyProperties_ExternalPower_USB); |
| + break; |
| + case 2: |
| + // Say that the system is both charging and discharging on AC. |
| + props_.set_external_power( |
| + power_manager::PowerSupplyProperties_ExternalPower_AC); |
| + break; |
| + default: |
| + NOTREACHED() << "Unhandled power source " << power_source_; |
| + } |
| + |
| + if (battery_percentage_ == 100 && !discharging_) { |
| + props_.set_battery_state( |
| + power_manager::PowerSupplyProperties_BatteryState_FULL); |
| + } else if (!discharging_) { |
| + props_.set_battery_state( |
| + power_manager::PowerSupplyProperties_BatteryState_CHARGING); |
| + props_.set_battery_time_to_full_sec( |
| + std::max(static_cast<int64>(1), |
| + hours_to_empty_full_battery - remaining_battery_time)); |
| + } else { |
| + props_.set_battery_state( |
| + power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); |
| + props_.set_battery_time_to_empty_sec(remaining_battery_time); |
| + } |
| + |
| + props_.set_battery_percent(battery_percentage_); |
| + props_.set_is_calculating_battery_time(is_calculating_); |
| + |
| + FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_)); |
| +} |
| + |
| +void BatteryEmulationController::SetBrightness(double percent, |
| + bool user_initiated) { |
| + brightness_ = std::min(std::max(0.0, percent), 100.0); |
| + int brightness_level = static_cast<int>(brightness_); |
| + FOR_EACH_OBSERVER(Observer, observers_, |
| + BrightnessChanged(brightness_level, user_initiated)); |
| +} |
| + |
| +void BatteryEmulationController::ParseCommandLineSwitch() { |
| + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| + if (!command_line || !command_line->HasSwitch(chromeos::switches::kPowerStub)) |
| + return; |
| + std::string option_str = |
| + command_line->GetSwitchValueASCII(chromeos::switches::kPowerStub); |
| + base::StringPairs string_pairs; |
| + base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs); |
| + for (base::StringPairs::iterator iter = string_pairs.begin(); |
| + iter != string_pairs.end(); ++iter) { |
| + ParseOption((*iter).first, (*iter).second); |
| + } |
| +} |
| + |
| +void BatteryEmulationController::ParseOption(const std::string& arg0, |
| + const std::string& arg1) { |
| + if (arg0 == "cycle" || arg0 == "interactive") { |
| + int seconds = 1; |
| + if (!arg1.empty()) |
| + base::StringToInt(arg1, &seconds); |
| + power_cycle_delay_ = base::TimeDelta::FromSeconds(seconds); |
| + } |
| +} |
| +} // namespace ash |