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 cr.exportPath('options');
8 * Copied from ash/system/chromeos/power/power_status.h.
11 options.PowerStatusDeviceType = {
19 * type: options.PowerStatusDeviceType,
25 cr.define('options', function() {
26 var Page = cr.ui.pageManager.Page;
27 var PageManager = cr.ui.pageManager.PageManager;
30 * Encapsulated handling of the power overlay.
32 * @extends {cr.ui.pageManager.Page}
34 function PowerOverlay() {
35 Page.call(this, 'power-overlay',
36 loadTimeData.getString('powerOverlayTabTitle'),
40 cr.addSingletonGetter(PowerOverlay);
42 PowerOverlay.prototype = {
43 __proto__: Page.prototype,
46 initializePage: function() {
47 Page.prototype.initializePage.call(this);
49 $('power-confirm').onclick =
50 PageManager.closeOverlay.bind(PageManager);
51 $('power-source-dropdown').onchange =
52 this.powerSourceChanged_.bind(this);
56 didShowPage: function() {
57 chrome.send('updatePowerStatus');
61 * @param {string} status
64 setBatteryStatusText_: function(status) {
65 $('battery-status-value').textContent = status;
69 * @param {Array<options.PowerSource>} sources External power sources.
70 * @param {string} selectedId The ID of the currently used power source.
71 * @param {boolean} isUsbCharger Whether the currently used power source
72 * is a USB (low-powered) charger.
73 * @param {boolean} isCalculating Whether the power info is still
77 setPowerSources_: function(sources, selectedId, isUsbCharger,
79 if (this.lastPowerSource_ != selectedId) {
80 this.lastPowerSource_ = selectedId;
81 if (selectedId && !isUsbCharger) {
82 // It can take a while to detect a USB charger, but triggering a
83 // power status update makes the determination faster.
84 setTimeout(chrome.send.bind(null, 'updatePowerStatus'), 1000);
88 var chargerRow = $('power-source-charger');
90 $('power-sources').hidden = chargerRow.hidden = true;
92 // If no power sources are available, only show the battery status text.
93 if (sources.length == 0)
96 // If we're still calculating battery time and seem to have an AC
97 // adapter, the charger information may be wrong.
98 if (isCalculating && selectedId && !isUsbCharger) {
99 $('power-source-charger-type').textContent =
100 loadTimeData.getString('calculatingPower');
101 chargerRow.hidden = false;
105 // Check if a dedicated charger is being used.
106 var usingDedicatedCharger = false;
108 usingDedicatedCharger = sources.some(function(source) {
109 return source.id == selectedId &&
110 source.type == options.PowerStatusDeviceType.DEDICATED_CHARGER;
114 if (usingDedicatedCharger) {
115 // Show charger information.
116 $('power-source-charger-type').textContent = loadTimeData.getString(
117 isUsbCharger ? 'powerSourceLowPowerCharger' :
118 'powerSourceAcAdapter');
119 chargerRow.hidden = false;
121 this.showPowerSourceList_(sources, selectedId);
126 * Populates and shows the dropdown of available power sources.
127 * @param {Array<options.PowerSource>} sources External power sources.
128 * @param {string} selectedId The ID of the currently used power source.
129 * The empty string indicates no external power source is in use
130 * (running on battery).
133 showPowerSourceList_: function(sources, selectedId) {
134 // Clear the dropdown.
135 var dropdown = $('power-source-dropdown');
136 dropdown.innerHTML = '';
138 // Add a battery option.
141 description: loadTimeData.getString('powerSourceBattery'),
144 // Build the power source list.
145 sources.forEach(function(source) {
146 var option = document.createElement('option');
147 option.value = source.id;
148 option.textContent = source.description;
149 option.selected = source.id == selectedId;
150 dropdown.appendChild(option);
153 // Show the power source list.
154 $('power-sources').hidden = false;
158 powerSourceChanged_: function() {
159 chrome.send('setPowerSource', [$('power-source-dropdown').value]);
163 cr.makePublic(PowerOverlay, [
164 'setBatteryStatusText',
170 PowerOverlay: PowerOverlay