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'); | |
dpapad
2016/10/03 17:51:03
Are those console.logs temporary?
mbrunson
2016/10/03 18:56:03
Yes. They are temporary.
| |
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) { | |
dpapad
2016/10/03 17:51:03
This function is repeated 3 times (and now 4), acr
mbrunson
2016/10/03 18:56:02
I've made a bug for this and added a TODO: crbug.c
| |
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', | |
dpapad
2016/10/03 17:51:03
Indentation seems quite a bit off here. Shouldn't
mbrunson
2016/10/03 18:56:02
Done.
| |
60 'device/bluetooth/public/interfaces/adapter.mojom', | |
61 'mojo/public/js/connection', | |
62 ]) | |
63 .then(function(modules) { | |
64 var frameInterfaces = modules[0]; | |
dpapad
2016/10/03 17:51:03
Nit: I believe that you can use destructuring assi
mbrunson
2016/10/03 18:56:03
Done.
| |
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 | |
dpapad
2016/10/03 17:51:03
Nit: Comments should have proper punctuation.
mbrunson
2016/10/03 18:56:02
Done.
| |
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 |