Index: chrome/browser/resources/bluetooth_internals/device_broker.js |
diff --git a/chrome/browser/resources/bluetooth_internals/device_broker.js b/chrome/browser/resources/bluetooth_internals/device_broker.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bec89282ea75235a50d0396c8c7520e18ab4270a |
--- /dev/null |
+++ b/chrome/browser/resources/bluetooth_internals/device_broker.js |
@@ -0,0 +1,52 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+/** |
+ * Javascript for device_broker, served from chrome://bluetooth-internals/. |
+ * Provides a single source to access DevicePtrs. DevicePtrs are cached for |
+ * for repeated use. Multiple connections may occur if subsequent requests are |
+ * made before the first request is resolved. In this case, the connection is |
+ * dropped. |
+ */ |
+ |
+// Expose for testing. |
+/** @type {!Map<string, !interfaces.BluetoothDevice.DevicePtr>} */ |
+var connectedDevices = null; |
+ |
+cr.define('device_broker', function() { |
+ connectedDevices = new Map(); |
+ |
+ /** |
+ * Creates a GATT connection to the device with |address|. If a connection to |
+ * the device already exists, the promise is resolved with the existing |
+ * DeviceBroker. |
+ * @param {string} address |
+ * @return {!Promise<!DeviceBroker>} |
+ */ |
+ function connectToDevice(address) { |
+ if (connectedDevices.has(address)) |
dpapad
2017/01/19 02:01:47
Is this function meant to call adapterBroker.conne
mbrunson
2017/01/19 03:22:20
I was less concerned about connection requests in
|
+ return Promise.resolve(connectedDevices.get(address)); |
+ |
+ return adapter_broker.getAdapterBroker().then(function(adapterBroker) { |
dpapad
2017/01/19 02:01:47
Chain instead of nesting, as follows
...
retu
mbrunson
2017/01/19 03:22:20
What function is "return adapter_broker.getAdapter
dpapad
2017/01/19 17:41:45
Ack. I misread the diff.
|
+ return adapterBroker.connectToDevice(address); |
+ }).then(function(device) { |
+ // If multiple connections occur, only use the first active connection. |
+ if (connectedDevices.has(address)) { |
+ device.disconnect(); |
+ return connectedDevices.get(address); |
+ } |
+ |
+ device.ptr.setConnectionErrorHandler(function() { |
+ connectedDevices.delete(address); |
+ }); |
+ |
+ connectedDevices.set(address, device); |
+ return device; |
+ }); |
+ } |
+ |
+ return { |
+ connectToDevice: connectToDevice |
+ }; |
+}); |