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 /** | |
| 11 * The implementation of AdapterClient in | |
| 12 * device/bluetooth/public/interfaces/adapter.mojom. | |
| 13 */ | |
| 14 var AdapterClient = function() {}; | |
| 15 AdapterClient.prototype = { | |
| 16 /** | |
| 17 * Prints added device to console. | |
| 18 * @param {!bluetoothDevice.DeviceInfo} device | |
| 19 */ | |
| 20 deviceAdded: function(device) { console.log('Device added', device); }, | |
| 21 | |
| 22 /** | |
| 23 * Prints removed device to console. | |
| 24 * @param {!bluetoothDevice.DeviceInfo} device | |
| 25 */ | |
| 26 deviceRemoved: function(device) { console.log('Device removed', device); } | |
| 27 }; | |
| 28 | |
| 29 (function() { | 10 (function() { |
| 30 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
| 31 | 12 |
| 13 // Dictionary for address->Device cache. | |
| 14 var devices = new Map(); | |
| 15 | |
| 16 // Data model for a cached device. | |
| 17 var Device = function(info) { this.info = info; }; | |
|
dpapad
2016/10/20 21:22:01
@constructor
@param {!bluetoothDevice.DeviceInfo}
mbrunson
2016/10/20 22:00:35
Done.
| |
| 18 | |
| 19 /** | |
| 20 * The implementation of AdapterClient in | |
| 21 * device/bluetooth/public/interfaces/adapter.mojom. | |
| 22 */ | |
| 23 var AdapterClient = function() {}; | |
|
dpapad
2016/10/20 21:22:01
Is there a good reason that the |devices| map is n
mbrunson
2016/10/20 22:00:35
Originally, I was thinking to just have the UI-rel
| |
| 24 AdapterClient.prototype = { | |
| 25 /** | |
| 26 * Logs added device to console and caches the device info. | |
| 27 * @param {!bluetoothDevice.DeviceInfo} device | |
| 28 */ | |
| 29 deviceAdded: function(deviceInfo) { | |
| 30 console.log('Device added', deviceInfo); | |
| 31 devices.set(deviceInfo.address, new Device(deviceInfo)); | |
| 32 }, | |
| 33 | |
| 34 /** | |
| 35 * Logs removed device to console and removes the cached device. | |
| 36 * @param {!bluetoothDevice.DeviceInfo} device | |
| 37 */ | |
| 38 deviceRemoved: function(deviceInfo) { | |
| 39 console.log('Device removed', deviceInfo); | |
| 40 devices.delete(deviceInfo.address); | |
| 41 }, | |
| 42 | |
| 43 /** | |
| 44 * Logs changed device info to console and updates the cached device. | |
| 45 * @param {!bluetoothDevice.DeviceInfo} deviceInfo | |
| 46 */ | |
| 47 deviceChanged: function(deviceInfo) { | |
| 48 console.log(new Date(), deviceInfo); | |
| 49 devices.get(deviceInfo.address).info = deviceInfo; | |
|
dpapad
2016/10/20 21:22:01
Should probably assert that the item was found in
mbrunson
2016/10/20 22:00:35
Done.
| |
| 50 } | |
| 51 }; | |
| 52 | |
| 32 /** | 53 /** |
| 33 * TODO(crbug.com/652361): Move to shared location. | 54 * TODO(crbug.com/652361): Move to shared location. |
| 34 * Helper to convert callback-based define() API to a promise-based API. | 55 * Helper to convert callback-based define() API to a promise-based API. |
| 35 * @param {!Array<string>} moduleNames | 56 * @param {!Array<string>} moduleNames |
| 36 * @return {!Promise} | 57 * @return {!Promise} |
| 37 */ | 58 */ |
| 38 function importModules(moduleNames) { | 59 function importModules(moduleNames) { |
| 39 return new Promise(function(resolve, reject) { | 60 return new Promise(function(resolve, reject) { |
| 40 define(moduleNames, function(var_args) { | 61 define(moduleNames, function(var_args) { |
| 41 resolve(Array.prototype.slice.call(arguments, 0)); | 62 resolve(Array.prototype.slice.call(arguments, 0)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 adapter = connection.bindHandleToProxy(response.adapter, | 99 adapter = connection.bindHandleToProxy(response.adapter, |
| 79 bluetoothAdapter.Adapter); | 100 bluetoothAdapter.Adapter); |
| 80 | 101 |
| 81 // Create a message pipe and bind one end to client | 102 // Create a message pipe and bind one end to client |
| 82 // implementation and the other to the Adapter service. | 103 // implementation and the other to the Adapter service. |
| 83 adapterClient = new AdapterClient(); | 104 adapterClient = new AdapterClient(); |
| 84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 105 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
| 85 }); | 106 }); |
| 86 } | 107 } |
| 87 | 108 |
| 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).then(function(response) { | |
| 97 var deviceHandle = response.device; | |
| 98 if (!deviceHandle) { | |
| 99 throw new Error(deviceInfo.name_for_display + ' cannot be found.'); | |
| 100 } | |
| 101 | |
| 102 var device = connection.bindHandleToProxy( | |
| 103 deviceHandle, bluetoothDevice.Device); | |
| 104 return device.getInfo(); | |
| 105 }).then(function(response) { | |
| 106 console.log(deviceInfo.name_for_display, response.info); | |
| 107 }); | |
| 108 } | |
| 109 | |
| 110 document.addEventListener('DOMContentLoaded', function() { | 109 document.addEventListener('DOMContentLoaded', function() { |
| 111 initializeProxies() | 110 initializeProxies() |
| 112 .then(function() { return adapter.getInfo(); }) | 111 .then(function() { return adapter.getInfo(); }) |
| 113 .then(function(response) { console.log('adapter', response.info); }) | 112 .then(function(response) { console.log('adapter', response.info); }) |
| 114 .then(function() { return adapter.getDevices(); }) | 113 .then(function() { return adapter.getDevices(); }) |
| 115 .then(function(response) { | 114 .then(function(response) { |
| 116 var devices = response.devices; | 115 console.log('devices', response.devices.length); |
| 117 console.log('devices', devices.length); | |
| 118 | 116 |
| 119 return Promise.all(devices.map(logDevice)); | 117 response.devices.forEach(function(deviceInfo) { |
| 118 devices.set(deviceInfo.address, new Device(deviceInfo)); | |
|
dpapad
2016/10/20 21:22:01
Is this necessary to initialize local state? If so
mbrunson
2016/10/20 22:00:35
Done.
| |
| 119 console.log(deviceInfo.name_for_display, deviceInfo); | |
| 120 }); | |
| 120 }) | 121 }) |
| 121 .catch(function(error) { console.error(error); }); | 122 .catch(function(error) { console.error(error); }); |
| 122 }); | 123 }); |
| 123 })(); | 124 })(); |
| OLD | NEW |