Chromium Code Reviews| Index: chrome/browser/resources/bluetooth_internals/device_collection.js |
| diff --git a/chrome/browser/resources/bluetooth_internals/device_collection.js b/chrome/browser/resources/bluetooth_internals/device_collection.js |
| index 4d86d99c79d47e972aa809d210f54b8a59528384..b5981476cb46371077e09fb1147ee483b3a611bc 100644 |
| --- a/chrome/browser/resources/bluetooth_internals/device_collection.js |
| +++ b/chrome/browser/resources/bluetooth_internals/device_collection.js |
| @@ -8,12 +8,24 @@ |
| */ |
| cr.define('device_collection', function() { |
| + /** |
| + * Enum of connection status for a device. Used for |
| + * DeviceCollection.updateConnectionStatus which sets the connectionStatus |
| + * on the DeviceInfo object. New DeviceInfo objects have a DISCONNECTED status |
| + * by default. |
|
dpapad
2016/11/10 22:33:20
@enum {number}
mbrunson
2016/11/11 21:26:14
Done.
|
| + */ |
| + var ConnectionStatus = { |
| + DISCONNECTED: 0, |
| + CONNECTING: 1, |
| + CONNECTED: 2, |
| + }; |
| + |
| /* |
| * Collection of devices. Extends ArrayDataModel which provides a set of |
| * functions and events that notifies observers when the collection changes. |
| * @constructor |
| - * @param {!Array<device_collection.Device>} array The starting collection of |
| - * devices. |
| + * @param {!Array<!device_collection.DeviceInfo>} array The starting |
| + * collection of devices. |
| * @extends {cr.ui.ArrayDataModel} |
| */ |
| var DeviceCollection = function(array) { |
| @@ -29,7 +41,7 @@ cr.define('device_collection', function() { |
| getByAddress: function(address) { |
| for (var i = 0; i < this.length; i++) { |
| var device = this.item(i); |
| - if (address == device.info.address) |
| + if (address == device.address) |
| return device; |
| } |
| return null; |
| @@ -40,19 +52,23 @@ cr.define('device_collection', function() { |
| * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| */ |
| addOrUpdate: function(deviceInfo) { |
| - var oldDevice = this.getByAddress(deviceInfo.address); |
| - if (oldDevice) { |
| + deviceInfo.removed = false; |
| + var oldDeviceInfo = this.getByAddress(deviceInfo.address); |
| + |
| + if (oldDeviceInfo) { |
| // Update rssi if it's valid |
| var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
| - (oldDevice.info.rssi && oldDevice.info.rssi.value); |
| + (oldDeviceInfo.rssi && oldDeviceInfo.rssi.value); |
| - oldDevice.info = deviceInfo; |
| - oldDevice.info.rssi = { value: rssi }; |
| - oldDevice.removed = false; |
| - |
| - this.updateIndex(this.indexOf(oldDevice)); |
| + // The connectionStatus and connectionMessage properties may not exist |
| + // on |deviceInfo|. The rssi property may be null, so it must be |
| + // re-assigned. |
| + Object.assign(oldDeviceInfo, deviceInfo); |
| + oldDeviceInfo.rssi = { value: rssi }; |
| + this.updateIndex(this.indexOf(oldDeviceInfo)); |
| } else { |
| - this.push(new Device(deviceInfo)); |
| + deviceInfo.connectionStatus = ConnectionStatus.DISCONNECTED; |
| + this.push(deviceInfo); |
| } |
| }, |
| @@ -65,21 +81,29 @@ cr.define('device_collection', function() { |
| assert(device, 'Device does not exist.'); |
| device.removed = true; |
| this.updateIndex(this.indexOf(device)); |
| - } |
| - }; |
| + }, |
| - /* |
| - * Data model for a cached device. |
| - * @constructor |
| - * @param {!interfaces.BluetoothDevice.DeviceInfo} info |
| - */ |
| - var Device = function(info) { |
| - this.info = info; |
| - this.removed = false; |
| + /** |
| + * Updates the device connection status. |
| + * @param {string} address The address of the device. |
| + * @param {number} status . |
| + * @param {?Error} opt_error Optional Error from connection. |
| + */ |
| + updateConnectionStatus: function(address, status, opt_error) { |
| + var message = (opt_error && opt_error.message) || ''; |
| + |
| + var device = assert(this.getByAddress(address), 'Device does not exist'); |
| + device.connectionStatus = status; |
| + |
| + // TODO(crbug.com/663830): Replace connection error column with better |
| + // notification system. |
| + device.connectionMessage = message; |
| + this.updateIndex(this.indexOf(device)); |
| + }, |
| }; |
| return { |
| - Device: Device, |
| + ConnectionStatus: ConnectionStatus, |
| DeviceCollection: DeviceCollection, |
| }; |
| }); |