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