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 961b15d5a7ce860ba34b4c4ea103d7b054ed5725..1ee8f23b2f819941e75f27f34f831e58beebf62f 100644 |
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| @@ -15,28 +15,22 @@ var AdapterClient = function() {}; |
| AdapterClient.prototype = { |
| /** |
| * Prints added device to console. |
| - * @param {!Object} device the device that was added |
| + * @param {!bluetoothDevice.DeviceInfo} device the device that was added |
|
dpapad
2016/10/11 21:58:32
Nit: Per styleguide at https://google.github.io/st
mbrunson
2016/10/11 23:15:33
Done.
|
| */ |
| - deviceAdded: function(device) { |
| - console.log('Device added'); |
| - console.log(device); |
| - }, |
| + deviceAdded: function(device) { console.log('Device added', device); }, |
| /** |
| * Prints removed device to console. |
| - * @param {!Object} device the device that was removed |
| + * @param {!bluetoothDevice.DeviceInfo} device the device that was removed |
| */ |
| - deviceRemoved: function(device) { |
| - console.log('Device removed'); |
| - console.log(device); |
| - } |
| + deviceRemoved: function(device) { console.log('Device removed', device); } |
| }; |
| (function() { |
| - var adapter, adapterClient; |
| + var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
| /** |
| - * TODO: Move to shared location. See http://crbug.com/652361. |
| + * TODO(crbug.com/652361): Move to shared location. |
| * Helper to convert callback-based define() API to a promise-based API. |
| * @param {!Array<string>} moduleNames |
| * @return {!Promise} |
| @@ -58,10 +52,14 @@ AdapterClient.prototype = { |
| return importModules([ |
| 'content/public/renderer/frame_interfaces', |
| 'device/bluetooth/public/interfaces/adapter.mojom', |
| + 'device/bluetooth/public/interfaces/device.mojom', |
| 'mojo/public/js/connection', |
| - ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { |
| + ]).then(function([frameInterfaces, ...modules]) { |
| console.log('Loaded modules'); |
| + // Destructure here to assign global variables. |
| + [bluetoothAdapter, bluetoothDevice, connection] = modules; |
| + |
| // Hook up the instance properties. |
| AdapterClient.prototype.__proto__ = |
| bluetoothAdapter.AdapterClient.stubClass.prototype; |
| @@ -71,28 +69,61 @@ AdapterClient.prototype = { |
| bluetoothAdapter.AdapterFactory); |
| // Get an Adapter service. |
| - return adapterFactory.getAdapter().then(function(response) { |
| - if (!response.adapter) { |
| - throw new Error('Bluetooth Not Supported on this platform.'); |
| - } |
| - |
| - adapter = connection.bindHandleToProxy(response.adapter, |
| - bluetoothAdapter.Adapter); |
| - |
| - // Create a message pipe and bind one end to client |
| - // implementation and the other to the Adapter service. |
| - adapterClient = new AdapterClient(); |
| - adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
| - }); |
| + return adapterFactory.getAdapter(); |
| + }).then(function(response) { |
| + if (!response.adapter) { |
| + throw new Error('Bluetooth Not Supported on this platform.'); |
| + } |
| + |
| + adapter = connection.bindHandleToProxy(response.adapter, |
| + bluetoothAdapter.Adapter); |
|
dpapad
2016/10/11 21:58:32
Indentation off.
mbrunson
2016/10/11 23:15:33
Done.
|
| + |
| + // Create a message pipe and bind one end to client |
| + // implementation and the other to the Adapter service. |
| + adapterClient = new AdapterClient(); |
| + adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
| }); |
| } |
| + /** |
| + * 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) |
|
dpapad
2016/10/11 21:58:32
Regarding promise callback indentation style, I kn
mbrunson
2016/10/11 23:15:33
Done.
|
| + .then(function(response) { |
| + var deviceHandle = response.device; |
| + |
| + if (deviceHandle) { |
|
dpapad
2016/10/11 21:58:32
Can you reverse this logic? I think it is more rea
mbrunson
2016/10/11 23:15:32
Done.
|
| + var device = connection.bindHandleToProxy( |
|
dpapad
2016/10/11 21:58:32
I don't think this is meant to be called every tim
mbrunson
2016/10/11 23:15:32
adapter.GetDevice creates a new handle for each re
|
| + deviceHandle, bluetoothDevice.Device); |
| + return device.getInfo(); |
| + } |
| + |
| + throw new Error('Device cannot be found.'); |
| + }) |
| + .then(function(response) { |
| + console.log(deviceInfo.name, response.info); |
| + }); |
| + } |
| + |
| document.addEventListener('DOMContentLoaded', function() { |
| initializeProxies() |
| - .then(function() {return adapter.getInfo(); }) |
| - .then(function(response) { console.log('info', response.info); }) |
| + .then(function() { return adapter.getInfo(); }) |
| + .then(function(response) { console.log('adapter', response.info); }) |
| .then(function() { return adapter.getDevices(); }) |
| - .then(function(response) { console.log('devices', response.devices); }) |
| + .then(function(response) { |
| + var devices = response.devices; |
| + console.log('devices', devices.length); |
| + |
| + devices.forEach(function(deviceInfo) { |
| + logDevice(deviceInfo) |
| + .catch(function(error) { console.error(deviceInfo.name, error); }); |
|
dpapad
2016/10/11 21:58:32
Do you really want a dedicated error handler for e
mbrunson
2016/10/11 23:15:32
I could do this. I'll just move the device name to
|
| + }); |
| + }) |
| .catch(function(error) { console.error(error); }); |
| }); |
| })(); |