Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Javascript for device_broker, served from chrome://bluetooth-internals/. | |
| 7 * Provides a single source to access DevicePtrs. DevicePtrs are cached for | |
| 8 * for repeated use. Multiple connection requests will result in the same | |
| 9 * DevicePtr being shared among all requesters. | |
| 10 */ | |
| 11 | |
| 12 // Expose for testing. | |
| 13 /** @type {!Map<string, !interfaces.BluetoothDevice.DevicePtr|!Promise} */ | |
| 14 var connectedDevices = null; | |
| 15 | |
| 16 cr.define('device_broker', function() { | |
| 17 connectedDevices = new Map(); | |
| 18 | |
| 19 /** | |
| 20 * Creates a GATT connection to the device with |address|. If a connection to | |
| 21 * the device already exists, the promise is resolved with the existing | |
| 22 * DevicePtr. If a connection is in progress, the promise resolves when | |
| 23 * the existing connection request promise is fulfilled. | |
| 24 * @param {string} address | |
| 25 * @return {!Promise<!interfaces.BluetoothDevice.DevicePtr>} | |
| 26 */ | |
| 27 function connectToDevice(address) { | |
|
dpapad
2017/01/19 17:41:45
Nit (optional): You can avoid querying the map twi
mbrunson
2017/01/19 23:34:10
Done.
| |
| 28 if (connectedDevices.has(address)) | |
| 29 return Promise.resolve(connectedDevices.get(address)); | |
| 30 | |
| 31 var promise = adapter_broker.getAdapterBroker().then( | |
| 32 function(adapterBroker) { | |
| 33 return adapterBroker.connectToDevice(address); | |
| 34 }).then(function(device) { | |
| 35 connectedDevices.set(address, device); | |
| 36 | |
| 37 device.ptr.setConnectionErrorHandler(function() { | |
| 38 connectedDevices.delete(address); | |
| 39 }); | |
| 40 | |
| 41 return device; | |
| 42 }).catch(function(error) { | |
| 43 connectedDevices.delete(address); | |
| 44 throw error; | |
| 45 }); | |
| 46 | |
| 47 connectedDevices.set(address, promise); | |
| 48 return promise; | |
| 49 } | |
| 50 | |
| 51 return { | |
| 52 connectToDevice: connectToDevice | |
| 53 }; | |
| 54 }); | |
| OLD | NEW |