Index: chrome/browser/resources/bluetooth_internals/interfaces.js |
diff --git a/chrome/browser/resources/bluetooth_internals/interfaces.js b/chrome/browser/resources/bluetooth_internals/interfaces.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dbf3ba46a1149c6cb98a7e9609263a58e7fc2c7e |
--- /dev/null |
+++ b/chrome/browser/resources/bluetooth_internals/interfaces.js |
@@ -0,0 +1,89 @@ |
+// Copyright 2016 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 Mojo interface helpers, served from |
+ * chrome://bluetooth-internals/. |
+ */ |
+ |
+cr.define('interfaces', function() { |
+ |
+ |
+ /** |
+ * TODO(crbug.com/652361): Move to shared location. |
+ * Helper to convert callback-based define() API to a promise-based API. |
+ * @param {!Array<string>} moduleNames |
+ * @return {!Promise} |
+ */ |
+ function importModules(moduleNames) { |
+ return new Promise(function(resolve, reject) { |
+ define(moduleNames, function(var_args) { |
+ resolve(Array.prototype.slice.call(arguments, 0)); |
+ }); |
+ }); |
+ } |
+ |
+ /** |
+ * Initializes Mojo proxies for page and Bluetooth services. |
+ * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
+ * is not supported. |
+ */ |
+ function initializeProxies() { |
+ return importModules([ |
+ 'content/public/renderer/frame_interfaces', |
+ 'device/bluetooth/public/interfaces/adapter.mojom', |
+ 'device/bluetooth/public/interfaces/device.mojom', |
+ 'mojo/public/js/connection', |
+ ]).then(function([frameInterfaces, bluetoothAdapter, bluetoothDevice, |
+ connection]) { |
+ Object.assign(interfaces, { |
+ BluetoothAdapter: bluetoothAdapter, |
+ BluetoothDevice: bluetoothDevice, |
+ Connection: connection, |
+ }); |
+ |
+ var adapterFactory = connection.bindHandleToProxy( |
+ frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
+ bluetoothAdapter.AdapterFactory); |
+ |
+ // Get an Adapter service. |
+ return adapterFactory.getAdapter(); |
+ }).then(function(response) { |
+ if (!response.adapter) { |
+ throw new Error('Bluetooth Not Supported on this platform.'); |
+ } |
+ |
+ adapter = interfaces.Connection.bindHandleToProxy( |
+ response.adapter, |
+ interfaces.BluetoothAdapter.Adapter); |
+ |
+ Object.assign(interfaces, { |
+ DefaultAdapter: adapter, |
+ }); |
+ }); |
+ } |
+ |
+ var initialized = false; |
+ var initializePromise = null; |
+ |
+ function initialize() { |
+ if (initialized) { |
+ return Promise.resolve(); |
+ } |
+ |
+ if (initializePromise) { |
+ return initializePromise; |
+ } |
+ |
+ initializePromise = initializeProxies().then(function() { |
+ initialized = true; |
+ }); |
+ |
+ return initializePromise; |
+ } |
+ |
+ return { |
+ initialize: initialize, |
+ }; |
+}); |