 Chromium Code Reviews
 Chromium Code Reviews Issue 2401193002:
  bluetooth: Add Device service for chrome://bluetooth-internals.  (Closed)
    
  
    Issue 2401193002:
  bluetooth: Add Device service for chrome://bluetooth-internals.  (Closed) 
  | 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..a4443904a6581a0e27a5ac8f2c3d70afb618d8d9 100644 | 
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js | 
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js | 
| @@ -18,8 +18,7 @@ AdapterClient.prototype = { | 
| * @param {!Object} device the device that was added | 
| */ | 
| deviceAdded: function(device) { | 
| - console.log('Device added'); | 
| - console.log(device); | 
| + console.log('Device added', device); | 
| }, | 
| /** | 
| @@ -27,16 +26,15 @@ AdapterClient.prototype = { | 
| * @param {!Object} device the device that was removed | 
| */ | 
| deviceRemoved: function(device) { | 
| - console.log('Device removed'); | 
| - console.log(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 +56,13 @@ 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'); | 
| + [bluetoothAdapter, bluetoothDevice, connection] = modules; | 
| 
scheib
2016/10/10 20:23:51
Why destructure twice? Why not just all above?
 
mbrunson
2016/10/10 22:01:09
Destructuring in the parameter list hides the glob
 | 
| + | 
| // Hook up the instance properties. | 
| AdapterClient.prototype.__proto__ = | 
| bluetoothAdapter.AdapterClient.stubClass.prototype; | 
| @@ -71,28 +72,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); | 
| + | 
| + // 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 logDeviceService(deviceInfo) { | 
| + return adapter.getDeviceService(deviceInfo.address) | 
| + .then(function(response) { | 
| + var deviceHandle = response.device; | 
| + | 
| + if (deviceHandle) { | 
| + var device = connection.bindHandleToProxy( | 
| + 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) { | 
| + logDeviceService(deviceInfo) | 
| + .catch(function(error) { console.error(deviceInfo.name, error); }); | 
| + }); | 
| + }) | 
| .catch(function(error) { console.error(error); }); | 
| }); | 
| })(); |