Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / power_ui.cc
blobc0178cb7059f26c9d7affbc3028609b2d294bcae
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/chromeos/power_ui.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/url_constants.h"
15 #include "chromeos/power/power_data_collector.h"
16 #include "content/public/browser/web_ui.h"
17 #include "content/public/browser/web_ui_data_source.h"
18 #include "content/public/browser/web_ui_message_handler.h"
19 #include "grit/browser_resources.h"
20 #include "grit/generated_resources.h"
22 namespace chromeos {
24 namespace {
26 const char kStringsJsFile[] = "strings.js";
27 const char kRequestBatteryChargeDataCallback[] = "requestBatteryChargeData";
28 const char kOnRequestBatteryChargeDataFunction[] =
29 "powerUI.showBatteryChargeData";
31 class PowerMessageHandler : public content::WebUIMessageHandler {
32 public:
33 PowerMessageHandler();
34 virtual ~PowerMessageHandler();
36 // WebUIMessageHandler implementation.
37 virtual void RegisterMessages() OVERRIDE;
39 private:
40 void OnGetBatteryChargeData(const base::ListValue* value);
43 PowerMessageHandler::PowerMessageHandler() {
46 PowerMessageHandler::~PowerMessageHandler() {
49 void PowerMessageHandler::RegisterMessages() {
50 web_ui()->RegisterMessageCallback(
51 kRequestBatteryChargeDataCallback,
52 base::Bind(&PowerMessageHandler::OnGetBatteryChargeData,
53 base::Unretained(this)));
56 void PowerMessageHandler::OnGetBatteryChargeData(const base::ListValue* value) {
57 const std::deque<PowerDataCollector::PowerSupplySnapshot>& power_supply =
58 PowerDataCollector::Get()->power_supply_data();
59 base::ListValue data;
61 for (unsigned int i = 0; i < power_supply.size(); ++i) {
62 const PowerDataCollector::PowerSupplySnapshot& snapshot = power_supply[i];
63 base::Time time = base::Time::Now() -
64 (base::TimeTicks::Now() - snapshot.time);
65 scoped_ptr<base::DictionaryValue> element(new base::DictionaryValue);
66 element->SetDouble("battery_percent", snapshot.battery_percent);
67 element->SetDouble("battery_discharge_rate",
68 snapshot.battery_discharge_rate);
69 element->SetBoolean("external_power", snapshot.external_power);
70 element->SetDouble("time", time.ToJsTime());
72 data.Append(element.release());
75 web_ui()->CallJavascriptFunction(kOnRequestBatteryChargeDataFunction, data);
78 } // namespace
80 PowerUI::PowerUI(content::WebUI* web_ui) : content::WebUIController(web_ui) {
81 web_ui->AddMessageHandler(new PowerMessageHandler());
83 content::WebUIDataSource* html =
84 content::WebUIDataSource::Create(chrome::kChromeUIPowerHost);
85 html->SetUseJsonJSFormatV2();
87 html->AddLocalizedString("titleText", IDS_ABOUT_POWER_TITLE);
88 html->AddLocalizedString("reloadButton", IDS_ABOUT_POWER_RELOAD_BUTTON);
89 html->AddLocalizedString("batteryChargePercentageHeader",
90 IDS_ABOUT_POWER_BATTERY_CHARGE_PERCENTAGE_HEADER);
91 html->AddLocalizedString("batteryDischargeRateHeader",
92 IDS_ABOUT_POWER_BATTERY_DISCHARGE_RATE_HEADER);
93 html->AddLocalizedString("negativeDischargeRateInfo",
94 IDS_ABOUT_POWER_NEGATIVE_DISCHARGE_RATE_INFO);
95 html->AddLocalizedString("notEnoughDataAvailableYet",
96 IDS_ABOUT_POWER_NOT_ENOUGH_DATA);
97 html->AddLocalizedString("timeAndPlotDataMismatch",
98 IDS_ABOUT_POWER_TIME_AND_PLOT_DATA_MISMATCH);
99 html->SetJsonPath(kStringsJsFile);
101 html->AddResourcePath("power.css", IDR_ABOUT_POWER_CSS);
102 html->AddResourcePath("power.js", IDR_ABOUT_POWER_JS);
103 html->SetDefaultResource(IDR_ABOUT_POWER_HTML);
105 Profile* profile = Profile::FromWebUI(web_ui);
106 content::WebUIDataSource::Add(profile, html);
109 PowerUI::~PowerUI() {
112 } // namespace chromeos