Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Javascript for bluetooth_internals.html, served from | 6 * Javascript for bluetooth_internals.html, served from |
| 7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * The implementation of AdapterClient in | 11 * The implementation of AdapterClient in |
| 12 * device/bluetooth/public/interfaces/adapter.mojom. | 12 * device/bluetooth/public/interfaces/adapter.mojom. |
| 13 */ | 13 */ |
| 14 var AdapterClient = function() {}; | 14 var AdapterClient = function() {}; |
| 15 AdapterClient.prototype = { | 15 AdapterClient.prototype = { |
| 16 /** | 16 /** |
| 17 * Prints added device to console. | 17 * Prints added device to console. |
| 18 * @param {!Object} device the device that was added | 18 * @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.
| |
| 19 */ | 19 */ |
| 20 deviceAdded: function(device) { | 20 deviceAdded: function(device) { console.log('Device added', device); }, |
| 21 console.log('Device added'); | |
| 22 console.log(device); | |
| 23 }, | |
| 24 | 21 |
| 25 /** | 22 /** |
| 26 * Prints removed device to console. | 23 * Prints removed device to console. |
| 27 * @param {!Object} device the device that was removed | 24 * @param {!bluetoothDevice.DeviceInfo} device the device that was removed |
| 28 */ | 25 */ |
| 29 deviceRemoved: function(device) { | 26 deviceRemoved: function(device) { console.log('Device removed', device); } |
| 30 console.log('Device removed'); | |
| 31 console.log(device); | |
| 32 } | |
| 33 }; | 27 }; |
| 34 | 28 |
| 35 (function() { | 29 (function() { |
| 36 var adapter, adapterClient; | 30 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
| 37 | 31 |
| 38 /** | 32 /** |
| 39 * TODO: Move to shared location. See http://crbug.com/652361. | 33 * TODO(crbug.com/652361): Move to shared location. |
| 40 * Helper to convert callback-based define() API to a promise-based API. | 34 * Helper to convert callback-based define() API to a promise-based API. |
| 41 * @param {!Array<string>} moduleNames | 35 * @param {!Array<string>} moduleNames |
| 42 * @return {!Promise} | 36 * @return {!Promise} |
| 43 */ | 37 */ |
| 44 function importModules(moduleNames) { | 38 function importModules(moduleNames) { |
| 45 return new Promise(function(resolve, reject) { | 39 return new Promise(function(resolve, reject) { |
| 46 define(moduleNames, function(var_args) { | 40 define(moduleNames, function(var_args) { |
| 47 resolve(Array.prototype.slice.call(arguments, 0)); | 41 resolve(Array.prototype.slice.call(arguments, 0)); |
| 48 }); | 42 }); |
| 49 }); | 43 }); |
| 50 } | 44 } |
| 51 | 45 |
| 52 /** | 46 /** |
| 53 * Initializes Mojo proxies for page and Bluetooth services. | 47 * Initializes Mojo proxies for page and Bluetooth services. |
| 54 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth | 48 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
| 55 * is not supported. | 49 * is not supported. |
| 56 */ | 50 */ |
| 57 function initializeProxies() { | 51 function initializeProxies() { |
| 58 return importModules([ | 52 return importModules([ |
| 59 'content/public/renderer/frame_interfaces', | 53 'content/public/renderer/frame_interfaces', |
| 60 'device/bluetooth/public/interfaces/adapter.mojom', | 54 'device/bluetooth/public/interfaces/adapter.mojom', |
| 55 'device/bluetooth/public/interfaces/device.mojom', | |
| 61 'mojo/public/js/connection', | 56 'mojo/public/js/connection', |
| 62 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { | 57 ]).then(function([frameInterfaces, ...modules]) { |
| 63 console.log('Loaded modules'); | 58 console.log('Loaded modules'); |
| 64 | 59 |
| 60 // Destructure here to assign global variables. | |
| 61 [bluetoothAdapter, bluetoothDevice, connection] = modules; | |
| 62 | |
| 65 // Hook up the instance properties. | 63 // Hook up the instance properties. |
| 66 AdapterClient.prototype.__proto__ = | 64 AdapterClient.prototype.__proto__ = |
| 67 bluetoothAdapter.AdapterClient.stubClass.prototype; | 65 bluetoothAdapter.AdapterClient.stubClass.prototype; |
| 68 | 66 |
| 69 var adapterFactory = connection.bindHandleToProxy( | 67 var adapterFactory = connection.bindHandleToProxy( |
| 70 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 68 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
| 71 bluetoothAdapter.AdapterFactory); | 69 bluetoothAdapter.AdapterFactory); |
| 72 | 70 |
| 73 // Get an Adapter service. | 71 // Get an Adapter service. |
| 74 return adapterFactory.getAdapter().then(function(response) { | 72 return adapterFactory.getAdapter(); |
| 75 if (!response.adapter) { | 73 }).then(function(response) { |
| 76 throw new Error('Bluetooth Not Supported on this platform.'); | 74 if (!response.adapter) { |
| 77 } | 75 throw new Error('Bluetooth Not Supported on this platform.'); |
| 76 } | |
| 78 | 77 |
| 79 adapter = connection.bindHandleToProxy(response.adapter, | 78 adapter = connection.bindHandleToProxy(response.adapter, |
| 80 bluetoothAdapter.Adapter); | 79 bluetoothAdapter.Adapter); |
|
dpapad
2016/10/11 21:58:32
Indentation off.
mbrunson
2016/10/11 23:15:33
Done.
| |
| 81 | 80 |
| 82 // Create a message pipe and bind one end to client | 81 // Create a message pipe and bind one end to client |
| 83 // implementation and the other to the Adapter service. | 82 // implementation and the other to the Adapter service. |
| 84 adapterClient = new AdapterClient(); | 83 adapterClient = new AdapterClient(); |
| 85 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
| 85 }); | |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * Prints device info from the device service. | |
| 90 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to | |
| 91 * get the information. | |
| 92 * @return {!Promise} resolves if device service is retrieved, rejects | |
| 93 * otherwise. | |
| 94 */ | |
| 95 function logDevice(deviceInfo) { | |
| 96 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.
| |
| 97 .then(function(response) { | |
| 98 var deviceHandle = response.device; | |
| 99 | |
| 100 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.
| |
| 101 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
| |
| 102 deviceHandle, bluetoothDevice.Device); | |
| 103 return device.getInfo(); | |
| 104 } | |
| 105 | |
| 106 throw new Error('Device cannot be found.'); | |
| 107 }) | |
| 108 .then(function(response) { | |
| 109 console.log(deviceInfo.name, response.info); | |
| 86 }); | 110 }); |
| 87 }); | |
| 88 } | 111 } |
| 89 | 112 |
| 90 document.addEventListener('DOMContentLoaded', function() { | 113 document.addEventListener('DOMContentLoaded', function() { |
| 91 initializeProxies() | 114 initializeProxies() |
| 92 .then(function() {return adapter.getInfo(); }) | 115 .then(function() { return adapter.getInfo(); }) |
| 93 .then(function(response) { console.log('info', response.info); }) | 116 .then(function(response) { console.log('adapter', response.info); }) |
| 94 .then(function() { return adapter.getDevices(); }) | 117 .then(function() { return adapter.getDevices(); }) |
| 95 .then(function(response) { console.log('devices', response.devices); }) | 118 .then(function(response) { |
| 119 var devices = response.devices; | |
| 120 console.log('devices', devices.length); | |
| 121 | |
| 122 devices.forEach(function(deviceInfo) { | |
| 123 logDevice(deviceInfo) | |
| 124 .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
| |
| 125 }); | |
| 126 }) | |
| 96 .catch(function(error) { console.error(error); }); | 127 .catch(function(error) { console.error(error); }); |
| 97 }); | 128 }); |
| 98 })(); | 129 })(); |
| OLD | NEW |