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 {!Object} device the device that was added |
| 19 */ | 19 */ |
| 20 deviceAdded: function(device) { | 20 deviceAdded: function(device) { |
| 21 console.log('Device added'); | 21 console.log('Device added', device); |
| 22 console.log(device); | |
| 23 }, | 22 }, |
| 24 | 23 |
| 25 /** | 24 /** |
| 26 * Prints removed device to console. | 25 * Prints removed device to console. |
| 27 * @param {!Object} device the device that was removed | 26 * @param {!Object} device the device that was removed |
| 28 */ | 27 */ |
| 29 deviceRemoved: function(device) { | 28 deviceRemoved: function(device) { |
| 30 console.log('Device removed'); | 29 console.log('Device removed', device); |
| 31 console.log(device); | |
| 32 } | 30 } |
| 33 }; | 31 }; |
| 34 | 32 |
| 35 (function() { | 33 (function() { |
| 36 var adapterFactory = null; | 34 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
| 37 var adapter = null; | |
| 38 var adapterClient = null; | |
| 39 | 35 |
| 40 /** | 36 /** |
| 41 * TODO: Move to shared location. See crbug.com/652361. | 37 * TODO: Move to shared location. See http://crbug.com/652361. |
|
ortuno
2016/10/10 00:11:20
Please follow TODO format of the style guide[1] e.
mbrunson
2016/10/10 19:21:28
Done.
| |
| 42 * Logs basic information retrieved from the adapter. | |
| 43 */ | |
| 44 function logAdapterInfo() { | |
| 45 console.log('Getting adapter info'); | |
| 46 | |
| 47 adapter.getInfo().then(function(response) { console.log(response.info); }); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Helper to convert callback-based define() API to a promise-based API. | 38 * Helper to convert callback-based define() API to a promise-based API. |
| 52 * @param {!Array<string>} moduleNames | 39 * @param {!Array<string>} moduleNames |
| 53 * @return {!Promise} | 40 * @return {!Promise} |
| 54 */ | 41 */ |
| 55 function importModules(moduleNames) { | 42 function importModules(moduleNames) { |
| 56 return new Promise(function(resolve, reject) { | 43 return new Promise(function(resolve, reject) { |
| 57 define(moduleNames, function(var_args) { | 44 define(moduleNames, function(var_args) { |
| 58 resolve(Array.prototype.slice.call(arguments, 0)); | 45 resolve(Array.prototype.slice.call(arguments, 0)); |
| 59 }); | 46 }); |
| 60 }); | 47 }); |
| 61 } | 48 } |
| 62 | 49 |
| 63 /** | 50 /** |
| 64 * Initializes Mojo proxies for page and Bluetooth services. | 51 * Initializes Mojo proxies for page and Bluetooth services. |
| 65 * @return {!Promise} resolves if adapter is acquired | 52 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
| 53 * is not supported. | |
| 66 */ | 54 */ |
| 67 function initializeProxies() { | 55 function initializeProxies() { |
| 68 return importModules([ | 56 return importModules([ |
| 69 'content/public/renderer/frame_interfaces', | 57 'content/public/renderer/frame_interfaces', |
| 70 'device/bluetooth/public/interfaces/adapter.mojom', | 58 'device/bluetooth/public/interfaces/adapter.mojom', |
| 59 'device/bluetooth/public/interfaces/le_device.mojom', | |
| 71 'mojo/public/js/connection', | 60 'mojo/public/js/connection', |
| 72 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { | 61 ]).then(function([frameInterfaces, ...modules]) { |
|
ortuno
2016/10/10 00:11:20
I thought you weren't allowed to use ES6 features?
mbrunson
2016/10/10 19:21:27
I was told destructuring is supported.
| |
| 73 console.log('Loaded modules'); | 62 console.log('Loaded modules'); |
| 74 | 63 |
| 64 [bluetoothAdapter, bluetoothDevice, connection] = modules; | |
| 65 | |
| 75 // Hook up the instance properties. | 66 // Hook up the instance properties. |
| 76 AdapterClient.prototype.__proto__ = | 67 AdapterClient.prototype.__proto__ = |
| 77 bluetoothAdapter.AdapterClient.stubClass.prototype; | 68 bluetoothAdapter.AdapterClient.stubClass.prototype; |
| 78 | 69 |
| 79 adapterFactory = connection.bindHandleToProxy( | 70 var adapterFactory = connection.bindHandleToProxy( |
|
ortuno
2016/10/10 00:11:20
I think you have to rebase; I remember you making
mbrunson
2016/10/10 19:21:28
Yeah. My repo is in a weird state right now. I'll
| |
| 80 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 71 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
| 81 bluetoothAdapter.AdapterFactory); | 72 bluetoothAdapter.AdapterFactory); |
| 82 | 73 |
| 83 // Get an Adapter service. | 74 // Get an Adapter service. |
| 84 return adapterFactory.getAdapter().then(function(response) { | 75 return adapterFactory.getAdapter(); |
| 85 if (!response.adapter) { | 76 }).then(function(response) { |
| 86 throw new Error('Bluetooth Not Supported on this platform.'); | 77 if (!response.adapter) { |
| 87 } | 78 throw new Error('Bluetooth Not Supported on this platform.'); |
| 79 } | |
| 88 | 80 |
| 89 adapter = connection.bindHandleToProxy(response.adapter, | 81 adapter = connection.bindHandleToProxy(response.adapter, |
| 90 bluetoothAdapter.Adapter); | 82 bluetoothAdapter.Adapter); |
| 91 | 83 |
| 92 // Create a message pipe and bind one end to client | 84 // Create a message pipe and bind one end to client |
| 93 // implementation and the other to the Adapter service. | 85 // implementation and the other to the Adapter service. |
| 94 adapterClient = new AdapterClient(); | 86 adapterClient = new AdapterClient(); |
| 95 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 87 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
| 88 }); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Prints device info from the device service. | |
| 93 * @param {!bluetoothDevice.LEDeviceInfo} deviceInfo the device for which to | |
| 94 * get the information. | |
| 95 * @return {!Promise} resolves if device service is retrieved, rejects | |
| 96 * otherwise. | |
| 97 */ | |
| 98 function logDeviceService(deviceInfo) { | |
| 99 return adapter.getDeviceService(deviceInfo.address) | |
| 100 .then(function(response) { | |
| 101 var deviceHandle = response.device; | |
| 102 | |
| 103 if (deviceHandle) { | |
| 104 var device = connection.bindHandleToProxy( | |
| 105 deviceHandle, bluetoothDevice.LEDevice); | |
| 106 return device.getInfo(); | |
| 107 } | |
| 108 | |
| 109 throw new Error('Device cannot be found.'); | |
| 110 }) | |
| 111 .then(function(response) { | |
| 112 console.log(deviceInfo.name, response.info); | |
| 96 }); | 113 }); |
| 97 }); | |
| 98 } | 114 } |
| 99 | 115 |
| 100 document.addEventListener('DOMContentLoaded', function() { | 116 document.addEventListener('DOMContentLoaded', function() { |
| 101 initializeProxies() | 117 initializeProxies() |
| 102 .then(function() { logAdapterInfo(); }) | 118 .then(function() { return adapter.getInfo(); }) |
| 119 .then(function(response) { console.log('adapter', response.info); }) | |
| 103 .then(function() { return adapter.getDevices(); }) | 120 .then(function() { return adapter.getDevices(); }) |
| 104 .then(function(response) { console.log(response.devices); }) | 121 .then(function(response) { |
| 122 var devices = response.devices; | |
| 123 console.log('devices', devices.length); | |
| 124 | |
| 125 for (var i = 0; i < devices.length; i++) { | |
|
ortuno
2016/10/10 00:11:20
nit:
devices.forEach(function(deviceInfo) {
log
mbrunson
2016/10/10 19:21:28
Done.
| |
| 126 logDeviceService(devices[i]) | |
| 127 .catch(function(error) { console.error(devices[i].name, error); }); | |
| 128 } | |
| 129 }) | |
| 105 .catch(function(error) { console.error(error); }); | 130 .catch(function(error) { console.error(error); }); |
| 106 }); | 131 }); |
| 107 })(); | 132 })(); |
| OLD | NEW |