Index: chrome/browser/resources/bluetooth_internals/device.js |
diff --git a/chrome/browser/resources/bluetooth_internals/device.js b/chrome/browser/resources/bluetooth_internals/device.js |
index f64ffcf3e634526b0d427158f85943ae4c1bef9f..f200ec0fb7ed8ab8895f6755125eb12b1ce2e61d 100644 |
--- a/chrome/browser/resources/bluetooth_internals/device.js |
+++ b/chrome/browser/resources/bluetooth_internals/device.js |
@@ -7,8 +7,12 @@ |
* chrome://bluetooth-internals/. |
*/ |
+/** @typedef {function(device.Device): Promise} */ |
+var ConnectionHandler; |
+ |
cr.define('device', function() { |
+ |
var REMOVED_CSS = 'removed'; |
/** |
@@ -19,9 +23,11 @@ cr.define('device', function() { |
*/ |
var DeviceTable = cr.ui.define(function() { |
this.devices_ = null; |
+ this.connectHandler_ = null; |
+ this.disconnectHandler_ = null; |
return document.importNode($('table-template').content.children[0], |
- true /* deep */); |
+ true /* deep */); |
}); |
DeviceTable.prototype = { |
__proto__: HTMLTableElement.prototype, |
@@ -36,6 +42,20 @@ cr.define('device', function() { |
}, |
/** |
+ * Sets the connection handlers for this device table. Handlers are expected |
+ * to return a Promise which resolves or throws an Error. The message |
+ * given in the error is displayed in the last column of the table. |
+ * @param {ConnectionHandler} connectHandler Handler that is called |
+ * when a connection needs to be created. |
+ * @param {ConnectionHandler} disconnectHandler Handler that is |
+ * called when a connection needs to be closed. |
+ */ |
+ setConnectionHandlers: function(connectHandler, disconnectHandler) { |
+ this.connectHandler_ = connectHandler; |
+ this.disconnectHandler_ = disconnectHandler; |
+ }, |
+ |
+ /** |
* Sets the tables device collection. |
* @param {!DeviceCollection} deviceCollection |
*/ |
@@ -65,6 +85,40 @@ cr.define('device', function() { |
}, |
/** |
+ * Generates a function to handle click events on the connect button for the |
+ * given |row|. |
+ * @param {HTMLTableRowElement} row The table row that was clicked. |
+ * @return {function(Event)} |
+ */ |
+ handleConnect_: function(row) { |
+ return function(event) { |
+ var cellCount = row.cells.length; |
+ var connectCell = row.cells[cellCount - 2]; |
+ var connectButton = connectCell.children[0]; |
+ var connectErrorCell = row.cells[cellCount - 1]; |
+ |
+ connectErrorCell.textContent = ''; |
+ |
+ var device = this.devices_.getByAddress(row.id); |
+ if (this.connectHandler_ && !device.proxy) { |
+ connectButton.textContent = 'Connecting...'; |
+ connectButton.disabled = true; |
+ |
+ this.connectHandler_(device).catch(function(error) { |
+ connectErrorCell.textContent = error.message; |
+ connectButton.textContent = 'Connect'; |
+ connectButton.disabled = false; |
+ }); |
+ } else if (this.disconnectHandler_ && device.proxy) { |
+ connectButton.textContent = 'Disconnecting...'; |
+ connectButton.disabled = true; |
+ |
+ this.disconnectHandler_(device); |
+ } |
+ }.bind(this); |
+ }, |
+ |
+ /** |
* Updates table row on splice event of the device collection. |
* @param {!Event} event |
*/ |
@@ -91,6 +145,17 @@ cr.define('device', function() { |
row.insertCell(); |
} |
+ // Make two extra cells for the connect button and connect errors. |
+ var connectCell = row.insertCell(); |
+ var connectErrorCell = row.insertCell(); |
+ |
+ var connectButton = document.createElement('button'); |
+ connectCell.appendChild(connectButton); |
+ |
+ connectButton.textContent = 'Connect'; |
+ connectButton.addEventListener('click', |
+ this.handleConnect_(row).bind(this)); |
+ |
this.updateRow_(device, row.sectionRowIndex); |
}, |
@@ -122,6 +187,23 @@ cr.define('device', function() { |
row.classList.remove(REMOVED_CSS); |
} |
+ var cellCount = row.cells.length; |
+ var connectCell = row.cells[cellCount - 2]; |
+ var connectButton = connectCell.children[0]; |
+ var connectErrorCell = row.cells[cellCount - 1]; |
+ |
+ if (device.info.connected) { |
+ connectButton.textContent = 'Disconnect'; |
+ } else if (device.proxy) { |
+ connectButton.textContent = 'Connect'; |
+ connectErrorCell.textContent = 'Lost connection'; |
+ device.proxy = null; |
+ } else { |
+ connectButton.textContent = 'Connect'; |
+ } |
+ |
+ connectButton.disabled = false; |
+ |
// Update the properties based on the header field path. |
for (var i = 0; i < this.headers_.length; i++) { |
var header = this.headers_[i]; |
@@ -136,12 +218,12 @@ cr.define('device', function() { |
var cell = row.cells[i]; |
cell.textContent = obj || 'Unknown'; |
- cell.dataset.label = header.innerText; |
+ cell.dataset.label = header.textContent; |
} |
}, |
}; |
- /* |
+ /** |
* Collection of devices with helper functions for searching and updating by |
* device address. |
* @constructor |
@@ -171,13 +253,14 @@ cr.define('device', function() { |
} |
}; |
- /* |
+ /** |
* Data model for a cached device. |
* @constructor |
* @param {!bluetoothDevice.DeviceInfo} info |
*/ |
var Device = function(info) { |
this.info = info; |
+ this.proxy = null; |
this.removed = false; |
}; |