| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/ui/webui/settings/chromeos/device_power_handler.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "ash/resources/grit/ash_resources.h" |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "content/public/browser/web_ui.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/l10n/time_format.h" |
| 20 #include "ui/base/webui/web_ui_util.h" |
| 21 |
| 22 namespace chromeos { |
| 23 namespace settings { |
| 24 namespace { |
| 25 |
| 26 base::string16 GetBatteryTimeText(base::TimeDelta time_left) { |
| 27 int hour = 0; |
| 28 int min = 0; |
| 29 ash::PowerStatus::SplitTimeIntoHoursAndMinutes(time_left, &hour, &min); |
| 30 |
| 31 base::string16 time_text; |
| 32 if (hour == 0 || min == 0) { |
| 33 // Display only one unit ("2 hours" or "10 minutes"). |
| 34 return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION, |
| 35 ui::TimeFormat::LENGTH_LONG, time_left); |
| 36 } |
| 37 |
| 38 return ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION, |
| 39 ui::TimeFormat::LENGTH_LONG, |
| 40 -1, // force hour and minute output |
| 41 time_left); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 PowerHandler::PowerHandler() { |
| 47 power_status_ = ash::PowerStatus::Get(); |
| 48 } |
| 49 |
| 50 PowerHandler::~PowerHandler() {} |
| 51 |
| 52 void PowerHandler::RegisterMessages() { |
| 53 web_ui()->RegisterMessageCallback( |
| 54 "updatePowerStatus", base::Bind(&PowerHandler::HandleUpdatePowerStatus, |
| 55 base::Unretained(this))); |
| 56 web_ui()->RegisterMessageCallback( |
| 57 "setPowerSource", |
| 58 base::Bind(&PowerHandler::HandleSetPowerSource, base::Unretained(this))); |
| 59 } |
| 60 |
| 61 void PowerHandler::OnJavascriptAllowed() { |
| 62 power_status_->AddObserver(this); |
| 63 } |
| 64 |
| 65 void PowerHandler::OnJavascriptDisallowed() { |
| 66 power_status_->RemoveObserver(this); |
| 67 } |
| 68 |
| 69 void PowerHandler::OnPowerStatusChanged() { |
| 70 SendBatteryStatus(); |
| 71 SendPowerSources(); |
| 72 } |
| 73 |
| 74 void PowerHandler::HandleUpdatePowerStatus(const base::ListValue* args) { |
| 75 AllowJavascript(); |
| 76 power_status_->RequestStatusUpdate(); |
| 77 } |
| 78 |
| 79 void PowerHandler::HandleSetPowerSource(const base::ListValue* args) { |
| 80 AllowJavascript(); |
| 81 |
| 82 std::string id; |
| 83 CHECK(args->GetString(0, &id)); |
| 84 power_status_->SetPowerSource(id); |
| 85 } |
| 86 |
| 87 void PowerHandler::SendBatteryStatus() { |
| 88 bool charging = power_status_->IsBatteryCharging(); |
| 89 bool calculating = power_status_->IsBatteryTimeBeingCalculated(); |
| 90 int percent = power_status_->GetRoundedBatteryPercent(); |
| 91 base::TimeDelta time_left; |
| 92 bool show_time = false; |
| 93 |
| 94 if (!calculating) { |
| 95 time_left = charging ? power_status_->GetBatteryTimeToFull() |
| 96 : power_status_->GetBatteryTimeToEmpty(); |
| 97 show_time = ash::PowerStatus::ShouldDisplayBatteryTime(time_left); |
| 98 } |
| 99 |
| 100 base::string16 status_text; |
| 101 if (show_time) { |
| 102 status_text = l10n_util::GetStringFUTF16( |
| 103 charging ? IDS_OPTIONS_BATTERY_STATUS_CHARGING |
| 104 : IDS_OPTIONS_BATTERY_STATUS, |
| 105 base::IntToString16(percent), GetBatteryTimeText(time_left)); |
| 106 } else { |
| 107 status_text = l10n_util::GetStringFUTF16(IDS_OPTIONS_BATTERY_STATUS_SHORT, |
| 108 base::IntToString16(percent)); |
| 109 } |
| 110 |
| 111 base::DictionaryValue battery_dict; |
| 112 battery_dict.SetBoolean("charging", charging); |
| 113 battery_dict.SetBoolean("calculating", calculating); |
| 114 battery_dict.SetInteger("percent", percent); |
| 115 battery_dict.SetString("statusText", status_text); |
| 116 |
| 117 CallJavascriptFunction("cr.webUIListenerCallback", |
| 118 base::StringValue("battery-status-changed"), |
| 119 battery_dict); |
| 120 } |
| 121 |
| 122 void PowerHandler::SendPowerSources() { |
| 123 base::ListValue sources_list; |
| 124 for (const auto& source : power_status_->GetPowerSources()) { |
| 125 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 126 dict->SetString("id", source.id); |
| 127 dict->SetInteger("type", source.type); |
| 128 dict->SetString("description", |
| 129 l10n_util::GetStringUTF16(source.description_id)); |
| 130 sources_list.Append(std::move(dict)); |
| 131 } |
| 132 |
| 133 CallJavascriptFunction( |
| 134 "cr.webUIListenerCallback", base::StringValue("power-sources-changed"), |
| 135 sources_list, base::StringValue(power_status_->GetCurrentPowerSourceID()), |
| 136 base::FundamentalValue(power_status_->IsUsbChargerConnected())); |
| 137 } |
| 138 |
| 139 } // namespace settings |
| 140 } // namespace chromeos |
| OLD | NEW |