OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Javascript for Mojo interface helpers, served from |
| 7 * chrome://bluetooth-internals/. |
| 8 */ |
| 9 |
| 10 cr.define('interfaces', function() { |
| 11 |
| 12 |
| 13 /** |
| 14 * TODO(crbug.com/652361): Move to shared location. |
| 15 * Helper to convert callback-based define() API to a promise-based API. |
| 16 * @param {!Array<string>} moduleNames |
| 17 * @return {!Promise} |
| 18 */ |
| 19 function importModules(moduleNames) { |
| 20 return new Promise(function(resolve, reject) { |
| 21 define(moduleNames, function(var_args) { |
| 22 resolve(Array.prototype.slice.call(arguments, 0)); |
| 23 }); |
| 24 }); |
| 25 } |
| 26 |
| 27 /** |
| 28 * Initializes Mojo proxies for page and Bluetooth services. |
| 29 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
| 30 * is not supported. |
| 31 */ |
| 32 function initializeProxies() { |
| 33 return importModules([ |
| 34 'content/public/renderer/frame_interfaces', |
| 35 'device/bluetooth/public/interfaces/adapter.mojom', |
| 36 'device/bluetooth/public/interfaces/device.mojom', |
| 37 'mojo/public/js/connection', |
| 38 ]).then(function([frameInterfaces, bluetoothAdapter, bluetoothDevice, |
| 39 connection]) { |
| 40 Object.assign(interfaces, { |
| 41 BluetoothAdapter: bluetoothAdapter, |
| 42 BluetoothDevice: bluetoothDevice, |
| 43 Connection: connection, |
| 44 }); |
| 45 |
| 46 var adapterFactory = connection.bindHandleToProxy( |
| 47 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
| 48 bluetoothAdapter.AdapterFactory); |
| 49 |
| 50 // Get an Adapter service. |
| 51 return adapterFactory.getAdapter(); |
| 52 }).then(function(response) { |
| 53 if (!response.adapter) { |
| 54 throw new Error('Bluetooth Not Supported on this platform.'); |
| 55 } |
| 56 |
| 57 adapter = interfaces.Connection.bindHandleToProxy( |
| 58 response.adapter, |
| 59 interfaces.BluetoothAdapter.Adapter); |
| 60 |
| 61 Object.assign(interfaces, { |
| 62 DefaultAdapter: adapter, |
| 63 }); |
| 64 }); |
| 65 } |
| 66 |
| 67 var initialized = false; |
| 68 var initializePromise = null; |
| 69 |
| 70 function initialize() { |
| 71 if (initialized) { |
| 72 return Promise.resolve(); |
| 73 } |
| 74 |
| 75 if (initializePromise) { |
| 76 return initializePromise; |
| 77 } |
| 78 |
| 79 initializePromise = initializeProxies().then(function() { |
| 80 initialized = true; |
| 81 }); |
| 82 |
| 83 return initializePromise; |
| 84 } |
| 85 |
| 86 return { |
| 87 initialize: initialize, |
| 88 }; |
| 89 }); |
OLD | NEW |