Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Unified Diff: chrome/browser/ui/webui/options/chromeos/power_handler.cc

Issue 677983004: Power overlay in Settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/options/chromeos/power_handler.cc
diff --git a/chrome/browser/ui/webui/options/chromeos/power_handler.cc b/chrome/browser/ui/webui/options/chromeos/power_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8032975d4c3db9aaa66ff9588be88dae8a5ba145
--- /dev/null
+++ b/chrome/browser/ui/webui/options/chromeos/power_handler.cc
@@ -0,0 +1,122 @@
+// Copyright 2014 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 "chrome/browser/ui/webui/options/chromeos/power_handler.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/grit/generated_resources.h"
+#include "content/public/browser/web_ui.h"
+#include "grit/ash_resources.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/base/webui/web_ui_util.h"
+#include "ui/gfx/image/image_skia.h"
+
+using ash::PowerStatus;
+
+namespace chromeos {
+namespace options {
+
+PowerHandler::PowerHandler() {
+}
+
+PowerHandler::~PowerHandler() {
+ if (switches::PowerOverlayEnabled())
+ PowerStatus::Get()->RemoveObserver(this);
+}
+
+void PowerHandler::GetLocalizedValues(
+ base::DictionaryValue* localized_strings) {
+ DCHECK(localized_strings);
+ RegisterTitle(localized_strings, "powerOverlay",
+ IDS_OPTIONS_POWER_OVERLAY_TITLE);
+ localized_strings->SetString(
+ "batteryStatusLabel",
+ l10n_util::GetStringUTF16(IDS_OPTIONS_BATTERY_STATUS_LABEL));
+}
+
+void PowerHandler::InitializePage() {
+ if (switches::PowerOverlayEnabled())
+ PowerStatus::Get()->RequestStatusUpdate();
+}
+
+void PowerHandler::RegisterMessages() {
+ if (switches::PowerOverlayEnabled())
+ PowerStatus::Get()->AddObserver(this);
+
+ // Callback to fetch the battery icon data.
+ web_ui()->RegisterMessageCallback(
+ "requestBatteryIcon",
+ base::Bind(&PowerHandler::GetBatteryIcon, base::Unretained(this)));
+}
+
+void PowerHandler::OnPowerStatusChanged() {
+ web_ui()->CallJavascriptFunction(
+ "options.PowerOverlay.setBatteryStatusText",
+ base::StringValue(GetStatusValue()));
+ web_ui()->CallJavascriptFunction(
+ "options.BrowserOptions.setBatteryStatusText",
+ base::StringValue(GetFullStatusText()));
+}
+
+base::string16 PowerHandler::GetStatusValue() const {
+ PowerStatus* status = PowerStatus::Get();
+ if (!status->IsBatteryPresent())
+ return base::string16();
+
+ bool charging = status->IsBatteryCharging();
+ bool calculating = status->IsBatteryTimeBeingCalculated();
+ int percent = status->GetRoundedBatteryPercent();
+ base::TimeDelta time_left;
+ bool show_time = false;
+
+ if (!calculating) {
+ time_left = charging ? status->GetBatteryTimeToFull() :
+ status->GetBatteryTimeToEmpty();
+ show_time = PowerStatus::ShouldDisplayBatteryTime(time_left);
+ }
+
+ if (!show_time) {
+ return l10n_util::GetStringFUTF16(IDS_OPTIONS_BATTERY_STATUS_SHORT,
+ base::IntToString16(percent));
+ } else {
+ int hour = 0;
+ int min = 0;
+ PowerStatus::SplitTimeIntoHoursAndMinutes(time_left, &hour, &min);
+ base::string16 minute_text = base::ASCIIToUTF16(min < 10 ? "0" : "") +
+ base::IntToString16(min);
+ return l10n_util::GetStringFUTF16(
+ charging ? IDS_OPTIONS_BATTERY_STATUS_CHARGING :
+ IDS_OPTIONS_BATTERY_STATUS,
+ base::IntToString16(percent),
+ base::IntToString16(hour),
+ minute_text);
+ }
+}
+
+base::string16 PowerHandler::GetFullStatusText() const {
+ base::string16 status = GetStatusValue();
+ if (status.empty())
+ return base::string16();
+ return l10n_util::GetStringFUTF16(
+ IDS_OPTIONS_DEVICE_GROUP_BATTERY_STATUS_LABEL, status);
+}
+
+void PowerHandler::GetBatteryIcon(const base::ListValue* args) {
+ gfx::ImageSkia icon = PowerStatus::Get()->GetBatteryImage(
+ ash::PowerStatus::ICON_DARK);
+ gfx::ImageSkiaRep image_rep = icon.GetRepresentation(
+ web_ui()->GetDeviceScaleFactor());
+ web_ui()->CallJavascriptFunction(
+ "options.BrowserOptions.setBatteryIcon",
+ base::StringValue(webui::GetBitmapDataUrl(image_rep.sk_bitmap())));
+}
+
+} // namespace options
+} // namespace chromeos
« no previous file with comments | « chrome/browser/ui/webui/options/chromeos/power_handler.h ('k') | chrome/browser/ui/webui/options/options_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698