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 document.addEventListener('DOMContentLoaded', function() { | 10 /** |
11 console.log('Welcome to Bluetooth Internals.'); | 11 * The implementation of AdapterClient in |
12 }); | 12 * device/bluetooth/public/interfaces/adapter.mojom. |
| 13 */ |
| 14 class AdapterClient { |
| 15 /** |
| 16 * Prints added device to console. |
| 17 * @param {!Object} device the device that was added |
| 18 */ |
| 19 deviceAdded(device) { |
| 20 console.log('Device added'); |
| 21 console.log(device); |
| 22 } |
| 23 |
| 24 /** |
| 25 * Prints removed device to console. |
| 26 * @param {!Object} device the device that was removed |
| 27 */ |
| 28 deviceRemoved(device) { |
| 29 console.log('Device removed'); |
| 30 console.log(device); |
| 31 } |
| 32 }; |
| 33 |
| 34 (function() { |
| 35 let adapter = null; |
| 36 let adapterClient = null; |
| 37 let adapterClientHandle = null; |
| 38 |
| 39 /** |
| 40 * Helper to convert callback-based define() API to a promise-based API. |
| 41 * @param {!Array<string>} moduleNames |
| 42 * @return {!Promise} |
| 43 */ |
| 44 function importModules(moduleNames) { |
| 45 return new Promise(function(resolve, reject) { |
| 46 define(moduleNames, function(var_args) { |
| 47 resolve(Array.prototype.slice.call(arguments, 0)); |
| 48 }); |
| 49 }); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Initializes Mojo proxies for page and Bluetooth services. |
| 54 * @return {!Promise} |
| 55 */ |
| 56 function initializeProxies() { |
| 57 return importModules([ |
| 58 'content/public/renderer/frame_interfaces', |
| 59 'device/bluetooth/public/interfaces/adapter.mojom', |
| 60 'mojo/public/js/connection', |
| 61 ]) |
| 62 .then(([frameInterfaces, bluetoothAdapter, connection]) => { |
| 63 console.log('Loaded modules'); |
| 64 |
| 65 // Hook up the instance properties |
| 66 Object.setPrototypeOf( |
| 67 AdapterClient.prototype, |
| 68 bluetoothAdapter.AdapterClient.stubClass.prototype); |
| 69 |
| 70 |
| 71 // Create a message pipe and bind one end to client |
| 72 // implementation |
| 73 adapterClient = new AdapterClient(); |
| 74 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); |
| 75 |
| 76 adapter = connection.bindHandleToProxy( |
| 77 frameInterfaces.getInterface(bluetoothAdapter.Adapter.name), |
| 78 bluetoothAdapter.Adapter); |
| 79 |
| 80 adapter.setClient(adapterClientHandle); |
| 81 }); |
| 82 } |
| 83 |
| 84 document.addEventListener('DOMContentLoaded', function() { |
| 85 initializeProxies() |
| 86 .then(() => adapter.getDevices()) |
| 87 .then(response => console.log(response.devices)); |
| 88 }); |
| 89 })(); |
OLD | NEW |