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 * TODO: Move to shared location. See crbug.com/652361. |
| 42 * Helper to convert callback-based define() API to a promise-based API. |
| 43 * @param {!Array<string>} moduleNames |
| 44 * @return {!Promise} |
| 45 */ |
| 46 function importModules(moduleNames) { |
| 47 return new Promise(function(resolve, reject) { |
| 48 define(moduleNames, function(var_args) { |
| 49 resolve(Array.prototype.slice.call(arguments, 0)); |
| 50 }); |
| 51 }); |
| 52 } |
| 53 |
| 54 /** |
| 55 * Initializes Mojo proxies for page and Bluetooth services. |
| 56 * @return {!Promise} |
| 57 */ |
| 58 function initializeProxies() { |
| 59 return importModules([ |
| 60 'content/public/renderer/frame_interfaces', |
| 61 'device/bluetooth/public/interfaces/adapter.mojom', |
| 62 'mojo/public/js/connection', |
| 63 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { |
| 64 console.log('Loaded modules'); |
| 65 |
| 66 // Hook up the instance properties. |
| 67 AdapterClient.prototype.__proto__ = |
| 68 bluetoothAdapter.AdapterClient.stubClass.prototype; |
| 69 |
| 70 // Create a message pipe and bind one end to client |
| 71 // implementation. |
| 72 adapterClient = new AdapterClient(); |
| 73 adapterClientHandle = connection.bindStubDerivedImpl(adapterClient); |
| 74 |
| 75 adapter = connection.bindHandleToProxy( |
| 76 frameInterfaces.getInterface(bluetoothAdapter.Adapter.name), |
| 77 bluetoothAdapter.Adapter); |
| 78 |
| 79 adapter.setClient(adapterClientHandle); |
| 80 }); |
| 81 } |
| 82 |
| 83 document.addEventListener('DOMContentLoaded', function() { |
| 84 initializeProxies() |
| 85 .then(function() { return adapter.getDevices(); }) |
| 86 .then(function(response) { console.log(response.devices); }); |
| 87 }); |
| 88 })(); |
OLD | NEW |