Chromium Code Reviews| Index: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| diff --git a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| index b9af7fb9c83e1c876d8d6555e259545d87b986da..c87a0a8aef02cafabefab0ac44d9b65a5408bbe6 100644 |
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| @@ -7,27 +7,48 @@ |
| * chrome://bluetooth-internals/. |
| */ |
| -/** |
| - * The implementation of AdapterClient in |
| - * device/bluetooth/public/interfaces/adapter.mojom. |
| - */ |
| -var AdapterClient = function() {}; |
| -AdapterClient.prototype = { |
| - /** |
| - * Prints added device to console. |
| - * @param {!bluetoothDevice.DeviceInfo} device |
| - */ |
| - deviceAdded: function(device) { console.log('Device added', device); }, |
| +(function() { |
| + var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
| + |
| + // Dictionary for address->Device cache. |
| + var devices = new Map(); |
| + |
| + // Data model for a cached device. |
| + var Device = function(info) { this.info = info; }; |
|
dpapad
2016/10/20 21:22:01
@constructor
@param {!bluetoothDevice.DeviceInfo}
mbrunson
2016/10/20 22:00:35
Done.
|
| /** |
| - * Prints removed device to console. |
| - * @param {!bluetoothDevice.DeviceInfo} device |
| + * The implementation of AdapterClient in |
| + * device/bluetooth/public/interfaces/adapter.mojom. |
| */ |
| - deviceRemoved: function(device) { console.log('Device removed', device); } |
| -}; |
| - |
| -(function() { |
| - var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
| + var AdapterClient = function() {}; |
|
dpapad
2016/10/20 21:22:01
Is there a good reason that the |devices| map is n
mbrunson
2016/10/20 22:00:35
Originally, I was thinking to just have the UI-rel
|
| + AdapterClient.prototype = { |
| + /** |
| + * Logs added device to console and caches the device info. |
| + * @param {!bluetoothDevice.DeviceInfo} device |
| + */ |
| + deviceAdded: function(deviceInfo) { |
| + console.log('Device added', deviceInfo); |
| + devices.set(deviceInfo.address, new Device(deviceInfo)); |
| + }, |
| + |
| + /** |
| + * Logs removed device to console and removes the cached device. |
| + * @param {!bluetoothDevice.DeviceInfo} device |
| + */ |
| + deviceRemoved: function(deviceInfo) { |
| + console.log('Device removed', deviceInfo); |
| + devices.delete(deviceInfo.address); |
| + }, |
| + |
| + /** |
| + * Logs changed device info to console and updates the cached device. |
| + * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
| + */ |
| + deviceChanged: function(deviceInfo) { |
| + console.log(new Date(), deviceInfo); |
| + devices.get(deviceInfo.address).info = deviceInfo; |
|
dpapad
2016/10/20 21:22:01
Should probably assert that the item was found in
mbrunson
2016/10/20 22:00:35
Done.
|
| + } |
| + }; |
| /** |
| * TODO(crbug.com/652361): Move to shared location. |
| @@ -85,38 +106,18 @@ AdapterClient.prototype = { |
| }); |
| } |
| - /** |
| - * Prints device info from the device service. |
| - * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to |
| - * get the information. |
| - * @return {!Promise} resolves if device service is retrieved, rejects |
| - * otherwise. |
| - */ |
| - function logDevice(deviceInfo) { |
| - return adapter.getDevice(deviceInfo.address).then(function(response) { |
| - var deviceHandle = response.device; |
| - if (!deviceHandle) { |
| - throw new Error(deviceInfo.name_for_display + ' cannot be found.'); |
| - } |
| - |
| - var device = connection.bindHandleToProxy( |
| - deviceHandle, bluetoothDevice.Device); |
| - return device.getInfo(); |
| - }).then(function(response) { |
| - console.log(deviceInfo.name_for_display, response.info); |
| - }); |
| - } |
| - |
| document.addEventListener('DOMContentLoaded', function() { |
| initializeProxies() |
| .then(function() { return adapter.getInfo(); }) |
| .then(function(response) { console.log('adapter', response.info); }) |
| .then(function() { return adapter.getDevices(); }) |
| .then(function(response) { |
| - var devices = response.devices; |
| - console.log('devices', devices.length); |
| + console.log('devices', response.devices.length); |
| - return Promise.all(devices.map(logDevice)); |
| + response.devices.forEach(function(deviceInfo) { |
| + devices.set(deviceInfo.address, new Device(deviceInfo)); |
|
dpapad
2016/10/20 21:22:01
Is this necessary to initialize local state? If so
mbrunson
2016/10/20 22:00:35
Done.
|
| + console.log(deviceInfo.name_for_display, deviceInfo); |
| + }); |
| }) |
| .catch(function(error) { console.error(error); }); |
| }); |