OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/webui/options/chromeos/power_handler.h" |
| 6 |
| 7 #include "base/bind.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/resource/resource_bundle.h" |
| 18 #include "ui/base/webui/web_ui_util.h" |
| 19 #include "ui/gfx/image/image_skia.h" |
| 20 |
| 21 using ash::PowerStatus; |
| 22 |
| 23 namespace chromeos { |
| 24 namespace options { |
| 25 |
| 26 PowerHandler::PowerHandler() { |
| 27 } |
| 28 |
| 29 PowerHandler::~PowerHandler() { |
| 30 if (switches::PowerOverlayEnabled()) |
| 31 PowerStatus::Get()->RemoveObserver(this); |
| 32 } |
| 33 |
| 34 void PowerHandler::GetLocalizedValues( |
| 35 base::DictionaryValue* localized_strings) { |
| 36 DCHECK(localized_strings); |
| 37 RegisterTitle(localized_strings, "powerOverlay", |
| 38 IDS_OPTIONS_POWER_OVERLAY_TITLE); |
| 39 localized_strings->SetString( |
| 40 "batteryStatusLabel", |
| 41 l10n_util::GetStringUTF16(IDS_OPTIONS_BATTERY_STATUS_LABEL)); |
| 42 } |
| 43 |
| 44 void PowerHandler::InitializePage() { |
| 45 if (switches::PowerOverlayEnabled()) |
| 46 PowerStatus::Get()->RequestStatusUpdate(); |
| 47 } |
| 48 |
| 49 void PowerHandler::RegisterMessages() { |
| 50 if (switches::PowerOverlayEnabled()) |
| 51 PowerStatus::Get()->AddObserver(this); |
| 52 |
| 53 // Callback to fetch the battery icon data. |
| 54 web_ui()->RegisterMessageCallback( |
| 55 "requestBatteryIcon", |
| 56 base::Bind(&PowerHandler::GetBatteryIcon, base::Unretained(this))); |
| 57 } |
| 58 |
| 59 void PowerHandler::OnPowerStatusChanged() { |
| 60 web_ui()->CallJavascriptFunction( |
| 61 "options.PowerOverlay.setBatteryStatusText", |
| 62 base::StringValue(GetStatusValue())); |
| 63 web_ui()->CallJavascriptFunction( |
| 64 "options.BrowserOptions.setBatteryStatusText", |
| 65 base::StringValue(GetFullStatusText())); |
| 66 } |
| 67 |
| 68 base::string16 PowerHandler::GetStatusValue() const { |
| 69 PowerStatus* status = PowerStatus::Get(); |
| 70 if (!status->IsBatteryPresent()) |
| 71 return base::string16(); |
| 72 |
| 73 bool charging = status->IsBatteryCharging(); |
| 74 bool calculating = status->IsBatteryTimeBeingCalculated(); |
| 75 int percent = status->GetRoundedBatteryPercent(); |
| 76 base::TimeDelta time_left; |
| 77 bool show_time = false; |
| 78 |
| 79 if (!calculating) { |
| 80 time_left = charging ? status->GetBatteryTimeToFull() : |
| 81 status->GetBatteryTimeToEmpty(); |
| 82 show_time = PowerStatus::ShouldDisplayBatteryTime(time_left); |
| 83 } |
| 84 |
| 85 if (!show_time) { |
| 86 return l10n_util::GetStringFUTF16(IDS_OPTIONS_BATTERY_STATUS_SHORT, |
| 87 base::IntToString16(percent)); |
| 88 } else { |
| 89 int hour = 0; |
| 90 int min = 0; |
| 91 PowerStatus::SplitTimeIntoHoursAndMinutes(time_left, &hour, &min); |
| 92 base::string16 minute_text = base::ASCIIToUTF16(min < 10 ? "0" : "") + |
| 93 base::IntToString16(min); |
| 94 return l10n_util::GetStringFUTF16( |
| 95 charging ? IDS_OPTIONS_BATTERY_STATUS_CHARGING : |
| 96 IDS_OPTIONS_BATTERY_STATUS, |
| 97 base::IntToString16(percent), |
| 98 base::IntToString16(hour), |
| 99 minute_text); |
| 100 } |
| 101 } |
| 102 |
| 103 base::string16 PowerHandler::GetFullStatusText() const { |
| 104 base::string16 status = GetStatusValue(); |
| 105 if (status.empty()) |
| 106 return base::string16(); |
| 107 return l10n_util::GetStringFUTF16( |
| 108 IDS_OPTIONS_DEVICE_GROUP_BATTERY_STATUS_LABEL, status); |
| 109 } |
| 110 |
| 111 void PowerHandler::GetBatteryIcon(const base::ListValue* args) { |
| 112 gfx::ImageSkia icon = PowerStatus::Get()->GetBatteryImage( |
| 113 ash::PowerStatus::ICON_DARK); |
| 114 gfx::ImageSkiaRep image_rep = icon.GetRepresentation( |
| 115 web_ui()->GetDeviceScaleFactor()); |
| 116 web_ui()->CallJavascriptFunction( |
| 117 "options.BrowserOptions.setBatteryIcon", |
| 118 base::StringValue(webui::GetBitmapDataUrl(image_rep.sk_bitmap()))); |
| 119 } |
| 120 |
| 121 } // namespace options |
| 122 } // namespace chromeos |
OLD | NEW |