Chromium Code Reviews| Index: chrome/browser/ui/webui/chromeos/power_ui.cc |
| diff --git a/chrome/browser/ui/webui/chromeos/power_ui.cc b/chrome/browser/ui/webui/chromeos/power_ui.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fb264c8e0adda73e81578bf12b2a7c065388e5f3 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/chromeos/power_ui.cc |
| @@ -0,0 +1,104 @@ |
| +// 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/chromeos/power_ui.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/time/time.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chromeos/power/power_data_collector.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "content/public/browser/web_ui_data_source.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "grit/browser_resources.h" |
| +#include "grit/generated_resources.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const char kStringsJsFile[] = "strings.js"; |
| +const char kRequestBatteryChargeDataCallback[] = "requestBatteryChargeData"; |
| +const char kOnRequestBatteryChargeDataFunction[] = |
| + "PowerUI.showBatteryChargeData"; |
| + |
| +class PowerMessageHandler : public content::WebUIMessageHandler { |
| + public: |
| + PowerMessageHandler(); |
| + virtual ~PowerMessageHandler(); |
| + |
| + // WebUIMessageHandler implementation. |
| + virtual void RegisterMessages() OVERRIDE; |
| + |
| + private: |
| + void OnGetBatteryChargeData(const base::ListValue* value); |
| +}; |
| + |
| +PowerMessageHandler::PowerMessageHandler() { |
| +} |
| + |
| +PowerMessageHandler::~PowerMessageHandler() { |
| +} |
| + |
| +void PowerMessageHandler::RegisterMessages() { |
| + web_ui()->RegisterMessageCallback( |
| + kRequestBatteryChargeDataCallback, |
| + base::Bind(&PowerMessageHandler::OnGetBatteryChargeData, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PowerMessageHandler::OnGetBatteryChargeData(const base::ListValue* value) { |
| + const std::vector<PowerDataCollector::PowerSupplySnapshot>& power_supply = |
| + PowerDataCollector::Get()->power_supply_data(); |
| + base::ListValue data; |
| + |
| + base::TimeTicks now = base::TimeTicks::Now(); |
| + for (unsigned int i = 0; i < power_supply.size(); ++i) { |
| + PowerDataCollector::PowerSupplySnapshot snapshot = power_supply[i]; |
|
Daniel Erat
2014/01/03 16:40:47
use "const PowerDataCollector::PowerSupplySnapshot
Siva Chandra
2014/01/03 23:34:59
Done.
|
| + base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds( |
| + now.ToInternalValue() - power_supply[i].time.ToInternalValue()); |
|
Daniel Erat
2014/01/03 16:40:47
you don't need ToInternalValue() here; just do "no
Siva Chandra
2014/01/03 23:34:59
Done.
|
| + |
| + base::DictionaryValue *element = new base::DictionaryValue; |
|
Daniel Erat
2014/01/03 16:40:47
nit: base::DictionaryValue*
also, safer to use:
Siva Chandra
2014/01/03 23:34:59
Done.
|
| + element->SetDouble("battery_percent", snapshot.battery_percent); |
| + element->SetInteger("time_past", time_delta.InSeconds()); |
|
Daniel Erat
2014/01/03 16:40:47
nit: seconds_past?
Siva Chandra
2014/01/03 23:34:59
Done.
|
| + element->SetBoolean("external_power", snapshot.external_power); |
| + |
| + data.Append(element); |
| + } |
| + |
| + web_ui()->CallJavascriptFunction(kOnRequestBatteryChargeDataFunction, data); |
| +} |
| + |
| +} // namespace |
| + |
| +PowerUI::PowerUI(content::WebUI* web_ui) : content::WebUIController(web_ui) { |
| + web_ui->AddMessageHandler(new PowerMessageHandler()); |
| + |
| + content::WebUIDataSource* html = |
| + content::WebUIDataSource::Create(chrome::kChromeUIPowerHost); |
| + html->SetUseJsonJSFormatV2(); |
| + |
| + html->AddLocalizedString("titleText", IDS_POWER_TITLE); |
| + html->AddLocalizedString("reloadButton", IDS_POWER_RELOAD_BUTTON); |
| + html->AddLocalizedString("batteryChargeHeader", |
| + IDS_POWER_BATTERY_CHARGE_HEADER); |
| + html->SetJsonPath(kStringsJsFile); |
| + |
| + html->AddResourcePath("power.css", IDR_POWER_CSS); |
| + html->AddResourcePath("power.js", IDR_POWER_JS); |
| + html->SetDefaultResource(IDR_POWER_HTML); |
| + |
| + Profile* profile = Profile::FromWebUI(web_ui); |
| + content::WebUIDataSource::Add(profile, html); |
| +} |
| + |
| +PowerUI::~PowerUI() { |
| +} |
| + |
| +} // namespace chromeos |