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/chromeos/power_ui.h" |
| 6 |
| 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" |
| 21 |
| 22 namespace chromeos { |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kStringsJsFile[] = "strings.js"; |
| 27 const char kRequestBatteryChargeDataCallback[] = "requestBatteryChargeData"; |
| 28 const char kOnRequestBatteryChargeDataFunction[] = |
| 29 "powerUI.showBatteryChargeData"; |
| 30 |
| 31 class PowerMessageHandler : public content::WebUIMessageHandler { |
| 32 public: |
| 33 PowerMessageHandler(); |
| 34 virtual ~PowerMessageHandler(); |
| 35 |
| 36 // WebUIMessageHandler implementation. |
| 37 virtual void RegisterMessages() OVERRIDE; |
| 38 |
| 39 private: |
| 40 void OnGetBatteryChargeData(const base::ListValue* value); |
| 41 }; |
| 42 |
| 43 PowerMessageHandler::PowerMessageHandler() { |
| 44 } |
| 45 |
| 46 PowerMessageHandler::~PowerMessageHandler() { |
| 47 } |
| 48 |
| 49 void PowerMessageHandler::RegisterMessages() { |
| 50 web_ui()->RegisterMessageCallback( |
| 51 kRequestBatteryChargeDataCallback, |
| 52 base::Bind(&PowerMessageHandler::OnGetBatteryChargeData, |
| 53 base::Unretained(this))); |
| 54 } |
| 55 |
| 56 void PowerMessageHandler::OnGetBatteryChargeData(const base::ListValue* value) { |
| 57 const std::vector<PowerDataCollector::PowerSupplySnapshot>& power_supply = |
| 58 PowerDataCollector::Get()->power_supply_data(); |
| 59 base::ListValue data; |
| 60 |
| 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() - power_supply[i].time); |
| 65 scoped_ptr<base::DictionaryValue> element(new base::DictionaryValue); |
| 66 element->SetDouble("battery_percent", snapshot.battery_percent); |
| 67 element->SetBoolean("external_power", snapshot.external_power); |
| 68 element->SetDouble("time", time.ToJsTime()); |
| 69 |
| 70 data.Append(element.release()); |
| 71 } |
| 72 |
| 73 web_ui()->CallJavascriptFunction(kOnRequestBatteryChargeDataFunction, data); |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
| 78 PowerUI::PowerUI(content::WebUI* web_ui) : content::WebUIController(web_ui) { |
| 79 web_ui->AddMessageHandler(new PowerMessageHandler()); |
| 80 |
| 81 content::WebUIDataSource* html = |
| 82 content::WebUIDataSource::Create(chrome::kChromeUIPowerHost); |
| 83 html->SetUseJsonJSFormatV2(); |
| 84 |
| 85 html->AddLocalizedString("titleText", IDS_ABOUT_POWER_TITLE); |
| 86 html->AddLocalizedString("reloadButton", IDS_ABOUT_POWER_RELOAD_BUTTON); |
| 87 html->AddLocalizedString("batteryChargeHeader", |
| 88 IDS_ABOUT_POWER_BATTERY_CHARGE_HEADER); |
| 89 html->AddLocalizedString("notEnoughDataAvailableYet", |
| 90 IDS_ABOUT_POWER_NOT_ENOUGH_DATA); |
| 91 html->AddLocalizedString("timeAndPlotDataMismatch", |
| 92 IDS_ABOUT_POWER_TIME_AND_PLOT_DATA_MISMATCH); |
| 93 html->SetJsonPath(kStringsJsFile); |
| 94 |
| 95 html->AddResourcePath("power.css", IDR_ABOUT_POWER_CSS); |
| 96 html->AddResourcePath("power.js", IDR_ABOUT_POWER_JS); |
| 97 html->SetDefaultResource(IDR_ABOUT_POWER_HTML); |
| 98 |
| 99 Profile* profile = Profile::FromWebUI(web_ui); |
| 100 content::WebUIDataSource::Add(profile, html); |
| 101 } |
| 102 |
| 103 PowerUI::~PowerUI() { |
| 104 } |
| 105 |
| 106 } // namespace chromeos |
OLD | NEW |