1 // Copyright 2014 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.
5 #include "chrome/browser/ui/webui/options/chromeos/power_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "content/public/browser/web_ui.h"
15 #include "grit/ash_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/l10n/time_format.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/webui/web_ui_util.h"
21 using ash::PowerStatus
;
26 // Returns the message ID corresponding to the port location of a source.
27 int GetPowerSourceDescription(const PowerStatus::PowerSource
& source
) {
28 switch (source
.port
) {
29 case PowerStatus::UNKNOWN_PORT
:
30 return IDS_OPTIONS_POWER_SOURCE_PORT_UNKNOWN
;
31 case PowerStatus::LEFT_PORT
:
32 return IDS_OPTIONS_POWER_SOURCE_PORT_LEFT
;
33 case PowerStatus::RIGHT_PORT
:
34 return IDS_OPTIONS_POWER_SOURCE_PORT_RIGHT
;
35 case PowerStatus::BACK_PORT
:
36 return IDS_OPTIONS_POWER_SOURCE_PORT_BACK
;
37 case PowerStatus::FRONT_PORT
:
38 return IDS_OPTIONS_POWER_SOURCE_PORT_FRONT
;
39 case PowerStatus::LEFT_FRONT_PORT
:
40 return IDS_OPTIONS_POWER_SOURCE_PORT_LEFT_FRONT
;
41 case PowerStatus::LEFT_BACK_PORT
:
42 return IDS_OPTIONS_POWER_SOURCE_PORT_LEFT_BACK
;
43 case PowerStatus::RIGHT_FRONT_PORT
:
44 return IDS_OPTIONS_POWER_SOURCE_PORT_RIGHT_FRONT
;
45 case PowerStatus::RIGHT_BACK_PORT
:
46 return IDS_OPTIONS_POWER_SOURCE_PORT_RIGHT_BACK
;
47 case PowerStatus::BACK_LEFT_PORT
:
48 return IDS_OPTIONS_POWER_SOURCE_PORT_BACK_LEFT
;
49 case PowerStatus::BACK_RIGHT_PORT
:
50 return IDS_OPTIONS_POWER_SOURCE_PORT_BACK_RIGHT
;
53 return IDS_OPTIONS_POWER_SOURCE_PORT_UNKNOWN
;
60 PowerHandler::PowerHandler() {
61 this->show_power_status_
= switches::PowerOverlayEnabled() ||
62 (PowerStatus::Get()->IsBatteryPresent() &&
63 PowerStatus::Get()->SupportsDualRoleDevices());
66 PowerHandler::~PowerHandler() {
67 if (this->show_power_status_
)
68 PowerStatus::Get()->RemoveObserver(this);
71 void PowerHandler::GetLocalizedValues(
72 base::DictionaryValue
* localized_strings
) {
73 DCHECK(localized_strings
);
74 RegisterTitle(localized_strings
, "powerOverlay",
75 IDS_OPTIONS_POWER_OVERLAY_TITLE
);
76 localized_strings
->SetString(
78 l10n_util::GetStringUTF16(IDS_OPTIONS_BATTERY_STATUS_LABEL
));
79 localized_strings
->SetBoolean(
80 "showPowerStatus", this->show_power_status_
);
81 localized_strings
->SetString(
83 l10n_util::GetStringUTF16(IDS_OPTIONS_POWER_SOURCE_LABEL
));
84 localized_strings
->SetString(
86 l10n_util::GetStringUTF16(IDS_OPTIONS_POWER_SOURCE_BATTERY
));
87 localized_strings
->SetString(
88 "powerSourceAcAdapter",
89 l10n_util::GetStringUTF16(IDS_OPTIONS_POWER_SOURCE_AC_ADAPTER
));
90 localized_strings
->SetString(
91 "powerSourceLowPowerCharger",
92 l10n_util::GetStringUTF16(IDS_OPTIONS_POWER_SOURCE_LOW_POWER_CHARGER
));
93 localized_strings
->SetString(
95 l10n_util::GetStringUTF16(IDS_OPTIONS_POWER_OVERLAY_CALCULATING
));
98 void PowerHandler::InitializePage() {
99 if (this->show_power_status_
)
100 PowerStatus::Get()->RequestStatusUpdate();
103 void PowerHandler::RegisterMessages() {
104 if (this->show_power_status_
)
105 PowerStatus::Get()->AddObserver(this);
106 // Callback to fetch the power info.
107 web_ui()->RegisterMessageCallback(
109 base::Bind(&PowerHandler::UpdatePowerStatus
, base::Unretained(this)));
110 // Callback to set the power source.
111 web_ui()->RegisterMessageCallback(
113 base::Bind(&PowerHandler::SetPowerSource
, base::Unretained(this)));
116 void PowerHandler::OnPowerStatusChanged() {
117 web_ui()->CallJavascriptFunction(
118 "options.PowerOverlay.setBatteryStatusText",
119 base::StringValue(GetStatusValue()));
120 UpdatePowerSources();
123 base::string16
PowerHandler::GetStatusValue() const {
124 PowerStatus
* status
= PowerStatus::Get();
125 if (!status
->IsBatteryPresent())
126 return base::string16();
128 bool charging
= status
->IsBatteryCharging();
129 bool calculating
= status
->IsBatteryTimeBeingCalculated();
130 int percent
= status
->GetRoundedBatteryPercent();
131 base::TimeDelta time_left
;
132 bool show_time
= false;
135 time_left
= charging
? status
->GetBatteryTimeToFull() :
136 status
->GetBatteryTimeToEmpty();
137 show_time
= PowerStatus::ShouldDisplayBatteryTime(time_left
);
141 return l10n_util::GetStringFUTF16(IDS_OPTIONS_BATTERY_STATUS_SHORT
,
142 base::IntToString16(percent
));
146 PowerStatus::SplitTimeIntoHoursAndMinutes(time_left
, &hour
, &min
);
148 base::string16 time_text
;
149 if (hour
== 0 || min
== 0) {
150 // Display only one unit ("2 hours" or "10 minutes").
151 time_text
= ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION
,
152 ui::TimeFormat::LENGTH_LONG
,
155 time_text
= ui::TimeFormat::Detailed(ui::TimeFormat::FORMAT_DURATION
,
156 ui::TimeFormat::LENGTH_LONG
,
157 -1, // force hour and minute output
161 return l10n_util::GetStringFUTF16(
162 charging
? IDS_OPTIONS_BATTERY_STATUS_CHARGING
:
163 IDS_OPTIONS_BATTERY_STATUS
,
164 base::IntToString16(percent
),
169 void PowerHandler::UpdatePowerStatus(const base::ListValue
* args
) {
170 PowerStatus::Get()->RequestStatusUpdate();
173 void PowerHandler::SetPowerSource(const base::ListValue
* args
) {
175 if (!args
->GetString(0, &id
)) {
179 PowerStatus::Get()->SetPowerSource(id
);
182 void PowerHandler::UpdatePowerSources() {
183 PowerStatus
* status
= PowerStatus::Get();
185 base::ListValue sources_list
;
186 for (const auto& source
: status
->GetPowerSources()) {
187 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
188 dict
->SetString("id", source
.id
);
189 dict
->SetInteger("type", source
.type
);
190 int message_id
= GetPowerSourceDescription(source
);
191 dict
->SetString("description", l10n_util::GetStringUTF16(message_id
));
192 sources_list
.Append(dict
.release());
195 web_ui()->CallJavascriptFunction(
196 "options.PowerOverlay.setPowerSources",
198 base::StringValue(status
->GetCurrentPowerSourceID()),
199 base::FundamentalValue(status
->IsUsbChargerConnected()),
200 base::FundamentalValue(status
->IsBatteryTimeBeingCalculated()));
203 } // namespace options
204 } // namespace chromeos