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 (function() { | |
10 let adapterService = null; | |
9 | 11 |
10 document.addEventListener('DOMContentLoaded', function() { | 12 /** |
11 console.log('Welcome to Bluetooth Internals.'); | 13 * Retrieves a list of devices from the Bluetooth Adapter service. |
12 }); | 14 * @return {!Promise} promise with array of devices |
15 */ | |
16 function getDevices() { | |
17 return adapterService.getDevices(0, 5).then((response) => response.devices); | |
18 } | |
19 | |
20 /** | |
21 * Helper to convert callback-based define() API to a promise-based API. | |
22 * @param {!Array<string>} moduleNames | |
23 * @return {!Promise} | |
24 */ | |
25 function importModules(moduleNames) { | |
26 return new Promise(function(resolve, reject) { | |
ortuno
2016/09/22 08:32:53
q: Does this need to be a function? Could it be an
scheib
2016/09/23 20:53:51
These are good suggestions, but ortuno FYI this is
mbrunson
2016/09/24 01:05:46
The define callback has to be a function. I've run
ortuno
2016/09/26 01:57:55
Ah got it. Thanks for the explanation.
| |
27 define(moduleNames, function(var_args) { | |
ortuno
2016/09/22 08:32:53
var_args seems unused?
mbrunson
2016/09/24 01:05:46
Done.
ortuno
2016/09/26 01:57:55
Did you mean to delete the argument or are you lea
mbrunson
2016/09/28 02:18:21
I'm just going to leave this entire function alone
| |
28 resolve(Array.prototype.slice.call(arguments, 0)); | |
ortuno
2016/09/22 08:32:53
I believe you can use the spread operator here. So
ortuno
2016/09/22 08:32:53
I believe you can use the spread operator here. So
mbrunson
2016/09/24 01:05:46
I believe the promise spec only allows a single va
ortuno
2016/09/26 01:57:55
Right. In ES2015 you can use either the spread ope
| |
29 }); | |
30 }); | |
31 } | |
32 | |
33 /** | |
34 * Initializes Mojo proxies for page and Bluetooth services. | |
35 * @return {!Promise} | |
36 */ | |
37 function initializeProxies() { | |
38 return importModules([ | |
39 'content/public/renderer/frame_interfaces', | |
40 'chrome/browser/ui/webui/bluetooth_internals/' + | |
41 'bluetooth_internals.mojom', | |
42 'device/bluetooth/public/interfaces/bluetooth.mojom', | |
43 'mojo/public/js/connection', | |
44 ]) | |
45 .then((modules) => { | |
ortuno
2016/09/22 08:32:53
.then(([frameInterfaces, mojom, bluetooth, connect
mbrunson
2016/09/24 01:05:46
In which case this has to be one variable; however
| |
46 let frameInterfaces = modules[0]; | |
47 let mojom = modules[1]; | |
48 let bluetooth = modules[2]; | |
49 let connection = modules[3]; | |
50 | |
51 let pageHandler = connection.bindHandleToProxy( | |
52 frameInterfaces.getInterface(mojom.InternalsPageHandler.name), | |
53 mojom.InternalsPageHandler); | |
54 | |
55 let AdapterClientImpl = function() {}; | |
56 | |
57 // Create adapter client implementation | |
58 AdapterClientImpl.prototype = { | |
ortuno
2016/09/22 08:32:53
Is it possible to use ES6 classes?
https://cs.chr
mbrunson
2016/09/24 01:05:46
Done.
| |
59 __proto__: bluetooth.AdapterClient.stubClass.prototype, | |
60 /** | |
61 * Prints added device to console | |
62 * @param {!Object} device the device that was added | |
63 */ | |
64 deviceAdded(device) { | |
65 console.log('Device added'); | |
66 console.log(device); | |
67 }, | |
68 | |
69 /** | |
70 * Prints removed device to console | |
71 * @param {!Object} device the device that was removed | |
72 */ | |
73 deviceRemoved(device) { | |
74 console.log('Device added'); | |
75 console.log(device); | |
76 } | |
77 }; | |
78 | |
79 let adapterClient = new AdapterClientImpl(); | |
80 | |
81 // Create adapter service interface request | |
82 let adapter = connection.bindProxy2(bluetooth.Adapter); | |
83 | |
84 adapterService = adapter.proxy; | |
85 let adapterHandle = adapter.handle; | |
86 | |
87 // Create a message pipe and bind one end to client implementation | |
88 let clientHandle = connection.bindStubDerivedImpl(adapterClient); | |
89 | |
90 // Send interface request and other end of client message pipe to C++ | |
91 pageHandler.getAdapterService(adapterHandle, clientHandle); | |
92 }); | |
93 } | |
94 | |
95 document.addEventListener('DOMContentLoaded', function() { | |
96 initializeProxies() | |
97 .then(() => getDevices()) | |
98 .then((devices) => console.log(devices)); | |
99 }); | |
100 })(); | |
OLD | NEW |