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..ac6f4256f6aedea98aa2208f739387932e3c63f6 100644 |
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| @@ -7,27 +7,55 @@ |
| * 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 = {}; |
| + |
| + // Data model for a cached device. |
| + function Device() {}; |
| + Device.prototype = { info: null }; |
| /** |
| - * 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() {}; |
| + AdapterClient.prototype = { |
| + /** |
| + * Logs added device to console and caches the device info. |
| + * @param {!bluetoothDevice.DeviceInfo} device |
| + */ |
| + deviceAdded: function(device) { |
|
scheib
2016/10/19 23:49:24
function(deviceInfo) - so that we don't confuse th
mbrunson
2016/10/20 00:33:51
Done.
|
| + console.log('Device added', device); |
| + devices[device.address] = new Device(); |
| + devices[device.address].info = device; |
| + }, |
| + |
| + /** |
| + * Logs removed device to console and removes the cached device. |
| + * @param {!bluetoothDevice.DeviceInfo} device |
| + */ |
| + deviceRemoved: function(device) { |
| + console.log('Device removed', device); |
| + delete devices[device.address]; |
| + }, |
| + |
| + /** |
| + * Logs received advertising packet to console. caches the last valid |
|
ortuno
2016/10/19 23:19:19
nit: s/caches/Caches/
mbrunson
2016/10/20 00:33:51
This whole comment needs to be fixed. Done.
|
| + * RSSI, and refreshes the device list. |
| + * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the |
| + * advertising packet received from the device. |
| + */ |
| + deviceChanged: function(deviceInfo) { |
| + console.log(new Date(), deviceInfo); |
| + |
| + if (deviceInfo.rssi) { |
|
scheib
2016/10/19 23:49:24
Shouldn't this cache all the deviceInfo, always?
mbrunson
2016/10/20 00:33:51
I think it's fine if the data model has the update
|
| + devices[deviceInfo.address].info.rssi = deviceInfo.rssi; |
| + } |
| + } |
| + }; |
| /** |
| * TODO(crbug.com/652361): Move to shared location. |
| @@ -62,11 +90,11 @@ AdapterClient.prototype = { |
| // Hook up the instance properties. |
| AdapterClient.prototype.__proto__ = |
| - bluetoothAdapter.AdapterClient.stubClass.prototype; |
| + bluetoothAdapter.AdapterClient.stubClass.prototype; |
| var adapterFactory = connection.bindHandleToProxy( |
| - frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
| - bluetoothAdapter.AdapterFactory); |
| + frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
| + bluetoothAdapter.AdapterFactory); |
| // Get an Adapter service. |
| return adapterFactory.getAdapter(); |
| @@ -76,7 +104,7 @@ AdapterClient.prototype = { |
| } |
| adapter = connection.bindHandleToProxy(response.adapter, |
| - bluetoothAdapter.Adapter); |
| + bluetoothAdapter.Adapter); |
| // Create a message pipe and bind one end to client |
| // implementation and the other to the Adapter service. |
| @@ -85,38 +113,19 @@ 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[deviceInfo.address] = new Device(); |
| + devices[deviceInfo.address].info = deviceInfo; |
| + console.log(deviceInfo.name_for_display, deviceInfo); |
| + }); |
| }) |
| .catch(function(error) { console.error(error); }); |
| }); |