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"
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/chromeos/power/power_data_collector.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/url_constants.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"
26 const char kStringsJsFile
[] = "strings.js";
27 const char kRequestBatteryChargeDataCallback
[] = "requestBatteryChargeData";
28 const char kOnRequestBatteryChargeDataFunction
[] =
29 "powerUI.showBatteryChargeData";
31 class PowerMessageHandler
: public content::WebUIMessageHandler
{
33 PowerMessageHandler();
34 virtual ~PowerMessageHandler();
36 // WebUIMessageHandler implementation.
37 virtual void RegisterMessages() OVERRIDE
;
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::PowerSupplySample
>& power_supply
=
58 PowerDataCollector::Get()->power_supply_data();
59 base::ListValue js_power_supply_data
;
60 for (size_t i
= 0; i
< power_supply
.size(); ++i
) {
61 const PowerDataCollector::PowerSupplySample
& sample
= power_supply
[i
];
62 scoped_ptr
<base::DictionaryValue
> element(new base::DictionaryValue
);
63 element
->SetDouble("batteryPercent", sample
.battery_percent
);
64 element
->SetDouble("batteryDischargeRate", sample
.battery_discharge_rate
);
65 element
->SetBoolean("externalPower", sample
.external_power
);
66 element
->SetDouble("time", sample
.time
.ToJsTime());
68 js_power_supply_data
.Append(element
.release());
71 const std::deque
<PowerDataCollector::SystemResumedSample
>& system_resumed
=
72 PowerDataCollector::Get()->system_resumed_data();
73 base::ListValue js_system_resumed_data
;
74 for (size_t i
= 0; i
< system_resumed
.size(); ++i
) {
75 const PowerDataCollector::SystemResumedSample
& sample
= system_resumed
[i
];
76 scoped_ptr
<base::DictionaryValue
> element(new base::DictionaryValue
);
77 element
->SetDouble("sleepDuration",
78 sample
.sleep_duration
.InMillisecondsF());
79 element
->SetDouble("time", sample
.time
.ToJsTime());
81 js_system_resumed_data
.Append(element
.release());
84 web_ui()->CallJavascriptFunction(kOnRequestBatteryChargeDataFunction
,
86 js_system_resumed_data
);
91 PowerUI::PowerUI(content::WebUI
* web_ui
) : content::WebUIController(web_ui
) {
92 web_ui
->AddMessageHandler(new PowerMessageHandler());
94 content::WebUIDataSource
* html
=
95 content::WebUIDataSource::Create(chrome::kChromeUIPowerHost
);
96 html
->SetUseJsonJSFormatV2();
98 html
->AddLocalizedString("titleText", IDS_ABOUT_POWER_TITLE
);
99 html
->AddLocalizedString("reloadButton", IDS_ABOUT_POWER_RELOAD_BUTTON
);
100 html
->AddLocalizedString("batteryChargePercentageHeader",
101 IDS_ABOUT_POWER_BATTERY_CHARGE_PERCENTAGE_HEADER
);
102 html
->AddLocalizedString("batteryDischargeRateHeader",
103 IDS_ABOUT_POWER_BATTERY_DISCHARGE_RATE_HEADER
);
104 html
->AddLocalizedString("negativeDischargeRateInfo",
105 IDS_ABOUT_POWER_NEGATIVE_DISCHARGE_RATE_INFO
);
106 html
->AddLocalizedString("notEnoughDataAvailableYet",
107 IDS_ABOUT_POWER_NOT_ENOUGH_DATA
);
108 html
->AddLocalizedString("systemSuspended",
109 IDS_ABOUT_POWER_SYSTEM_SUSPENDED
);
110 html
->SetJsonPath(kStringsJsFile
);
112 html
->AddResourcePath("power.css", IDR_ABOUT_POWER_CSS
);
113 html
->AddResourcePath("power.js", IDR_ABOUT_POWER_JS
);
114 html
->SetDefaultResource(IDR_ABOUT_POWER_HTML
);
116 Profile
* profile
= Profile::FromWebUI(web_ui
);
117 content::WebUIDataSource::Add(profile
, html
);
120 PowerUI::~PowerUI() {
123 } // namespace chromeos