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 /** | 10 /** |
11 * The implementation of AdapterClient in | 11 * The implementation of AdapterClient in |
12 * device/bluetooth/public/interfaces/adapter.mojom. | 12 * device/bluetooth/public/interfaces/adapter.mojom. |
13 */ | 13 */ |
14 var AdapterClient = function() {}; | 14 var AdapterClient = function() {}; |
15 AdapterClient.prototype = { | 15 AdapterClient.prototype = { |
16 /** | 16 /** |
17 * Prints added device to console. | 17 * Prints added device to console. |
18 * @param {!Object} device the device that was added | 18 * @param {!bluetoothDevice.DeviceInfo} device |
19 */ | 19 */ |
20 deviceAdded: function(device) { | 20 deviceAdded: function(device) { console.log('Device added', device); }, |
21 console.log('Device added'); | |
22 console.log(device); | |
23 }, | |
24 | 21 |
25 /** | 22 /** |
26 * Prints removed device to console. | 23 * Prints removed device to console. |
27 * @param {!Object} device the device that was removed | 24 * @param {!bluetoothDevice.DeviceInfo} device |
28 */ | 25 */ |
29 deviceRemoved: function(device) { | 26 deviceRemoved: function(device) { console.log('Device removed', device); } |
30 console.log('Device removed'); | |
31 console.log(device); | |
32 } | |
33 }; | 27 }; |
34 | 28 |
35 (function() { | 29 (function() { |
36 var adapter, adapterClient; | 30 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
37 | 31 |
38 /** | 32 /** |
39 * TODO: Move to shared location. See http://crbug.com/652361. | 33 * TODO(crbug.com/652361): Move to shared location. |
40 * Helper to convert callback-based define() API to a promise-based API. | 34 * Helper to convert callback-based define() API to a promise-based API. |
41 * @param {!Array<string>} moduleNames | 35 * @param {!Array<string>} moduleNames |
42 * @return {!Promise} | 36 * @return {!Promise} |
43 */ | 37 */ |
44 function importModules(moduleNames) { | 38 function importModules(moduleNames) { |
45 return new Promise(function(resolve, reject) { | 39 return new Promise(function(resolve, reject) { |
46 define(moduleNames, function(var_args) { | 40 define(moduleNames, function(var_args) { |
47 resolve(Array.prototype.slice.call(arguments, 0)); | 41 resolve(Array.prototype.slice.call(arguments, 0)); |
48 }); | 42 }); |
49 }); | 43 }); |
50 } | 44 } |
51 | 45 |
52 /** | 46 /** |
53 * Initializes Mojo proxies for page and Bluetooth services. | 47 * Initializes Mojo proxies for page and Bluetooth services. |
54 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth | 48 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth |
55 * is not supported. | 49 * is not supported. |
56 */ | 50 */ |
57 function initializeProxies() { | 51 function initializeProxies() { |
58 return importModules([ | 52 return importModules([ |
59 'content/public/renderer/frame_interfaces', | 53 'content/public/renderer/frame_interfaces', |
60 'device/bluetooth/public/interfaces/adapter.mojom', | 54 'device/bluetooth/public/interfaces/adapter.mojom', |
| 55 'device/bluetooth/public/interfaces/device.mojom', |
61 'mojo/public/js/connection', | 56 'mojo/public/js/connection', |
62 ]).then(function([frameInterfaces, bluetoothAdapter, connection]) { | 57 ]).then(function([frameInterfaces, ...modules]) { |
63 console.log('Loaded modules'); | 58 console.log('Loaded modules'); |
64 | 59 |
| 60 // Destructure here to assign global variables. |
| 61 [bluetoothAdapter, bluetoothDevice, connection] = modules; |
| 62 |
65 // Hook up the instance properties. | 63 // Hook up the instance properties. |
66 AdapterClient.prototype.__proto__ = | 64 AdapterClient.prototype.__proto__ = |
67 bluetoothAdapter.AdapterClient.stubClass.prototype; | 65 bluetoothAdapter.AdapterClient.stubClass.prototype; |
68 | 66 |
69 var adapterFactory = connection.bindHandleToProxy( | 67 var adapterFactory = connection.bindHandleToProxy( |
70 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 68 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), |
71 bluetoothAdapter.AdapterFactory); | 69 bluetoothAdapter.AdapterFactory); |
72 | 70 |
73 // Get an Adapter service. | 71 // Get an Adapter service. |
74 return adapterFactory.getAdapter().then(function(response) { | 72 return adapterFactory.getAdapter(); |
75 if (!response.adapter) { | 73 }).then(function(response) { |
76 throw new Error('Bluetooth Not Supported on this platform.'); | 74 if (!response.adapter) { |
77 } | 75 throw new Error('Bluetooth Not Supported on this platform.'); |
| 76 } |
78 | 77 |
79 adapter = connection.bindHandleToProxy(response.adapter, | 78 adapter = connection.bindHandleToProxy(response.adapter, |
80 bluetoothAdapter.Adapter); | 79 bluetoothAdapter.Adapter); |
81 | 80 |
82 // Create a message pipe and bind one end to client | 81 // Create a message pipe and bind one end to client |
83 // implementation and the other to the Adapter service. | 82 // implementation and the other to the Adapter service. |
84 adapterClient = new AdapterClient(); | 83 adapterClient = new AdapterClient(); |
85 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
| 85 }); |
| 86 } |
| 87 |
| 88 /** |
| 89 * Prints device info from the device service. |
| 90 * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to |
| 91 * get the information. |
| 92 * @return {!Promise} resolves if device service is retrieved, rejects |
| 93 * otherwise. |
| 94 */ |
| 95 function logDevice(deviceInfo) { |
| 96 return adapter.getDevice(deviceInfo.address).then(function(response) { |
| 97 var deviceHandle = response.device; |
| 98 if (!deviceHandle) { |
| 99 throw new Error(deviceInfo.name_for_display + ' cannot be found.'); |
| 100 } |
| 101 |
| 102 var device = connection.bindHandleToProxy( |
| 103 deviceHandle, bluetoothDevice.Device); |
| 104 return device.getInfo(); |
| 105 }).then(function(response) { |
| 106 console.log(deviceInfo.name_for_display, response.info); |
86 }); | 107 }); |
87 }); | |
88 } | 108 } |
89 | 109 |
90 document.addEventListener('DOMContentLoaded', function() { | 110 document.addEventListener('DOMContentLoaded', function() { |
91 initializeProxies() | 111 initializeProxies() |
92 .then(function() {return adapter.getInfo(); }) | 112 .then(function() { return adapter.getInfo(); }) |
93 .then(function(response) { console.log('info', response.info); }) | 113 .then(function(response) { console.log('adapter', response.info); }) |
94 .then(function() { return adapter.getDevices(); }) | 114 .then(function() { return adapter.getDevices(); }) |
95 .then(function(response) { console.log('devices', response.devices); }) | 115 .then(function(response) { |
| 116 var devices = response.devices; |
| 117 console.log('devices', devices.length); |
| 118 |
| 119 return Promise.all(devices.map(logDevice)); |
| 120 }) |
96 .catch(function(error) { console.error(error); }); | 121 .catch(function(error) { console.error(error); }); |
97 }); | 122 }); |
98 })(); | 123 })(); |
OLD | NEW |