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..24799d6f3927783a2104a7b1afe987ee75a4da3d 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 |
| */ |
| - 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 |
| */ |
| - 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,57 @@ 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); |
| + |
| + // 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).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('info', response.info); }) |
| + .then(function() { return adapter.getInfo(); }) |
| + .then(function(response) { console.log('adapter', response.info); }) |
| .then(function() { return adapter.getDevices(); }) |
|
dpapad
2016/10/11 23:51:04
I am confused about the purpose of these calls her
mbrunson
2016/10/12 01:37:42
No. You're not missing anything. This is the minim
dpapad
2016/10/12 17:39:02
Thanks for the explanation. It still seems odd to
mbrunson
2016/10/12 18:06:52
Design doc is a WIP: go/fizzbluetoothinternals. Yo
|
| - .then(function(response) { console.log('devices', response.devices); }) |
| + .then(function(response) { |
|
dpapad
2016/10/11 23:51:04
The error will propagate to the already existing e
mbrunson
2016/10/12 01:37:42
Done.
|
| + var devices = response.devices; |
| + console.log('devices', devices.length); |
| + |
| + Promise.all(devices.map(logDevice)).catch(function(error) { |
| + console.error(error); |
| + }); |
| + }) |
| .catch(function(error) { console.error(error); }); |
| }); |
| })(); |