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 /** | |
11 * The implementation of AdapterClient in | |
12 * device/bluetooth/public/interfaces/adapter.mojom. | |
13 */ | |
14 var AdapterClient = function() {}; | |
15 AdapterClient.prototype = { | |
16 /** | |
17 * Prints added device to console. | |
18 * @param {!bluetoothDevice.DeviceInfo} device | |
19 */ | |
20 deviceAdded: function(device) { console.log('Device added', device); }, | |
21 | |
22 /** | |
23 * Prints removed device to console. | |
24 * @param {!bluetoothDevice.DeviceInfo} device | |
25 */ | |
26 deviceRemoved: function(device) { console.log('Device removed', device); } | |
27 }; | |
28 | |
29 (function() { | 10 (function() { |
30 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; |
31 | 12 |
| 13 /* |
| 14 * Data model for a cached device. |
| 15 * @constructor |
| 16 * @param {!bluetoothDevice.DeviceInfo} info |
| 17 */ |
| 18 var Device = function(info) { this.info = info; }; |
| 19 |
| 20 /** |
| 21 * The implementation of AdapterClient in |
| 22 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the |
| 23 * client-side collection of devices. |
| 24 * @constructor |
| 25 */ |
| 26 var AdapterClient = function() { this.devices_ = new Map(); }; |
| 27 AdapterClient.prototype = { |
| 28 /** |
| 29 * Logs added device to console and caches the device info. |
| 30 * @param {!bluetoothDevice.DeviceInfo} device |
| 31 */ |
| 32 deviceAdded: function(deviceInfo) { |
| 33 console.log('Device added', deviceInfo); |
| 34 this.devices_.set(deviceInfo.address, new Device(deviceInfo)); |
| 35 }, |
| 36 |
| 37 /** |
| 38 * Logs removed device to console and removes the cached device. |
| 39 * @param {!bluetoothDevice.DeviceInfo} device |
| 40 */ |
| 41 deviceRemoved: function(deviceInfo) { |
| 42 console.log('Device removed', deviceInfo); |
| 43 this.devices_.delete(deviceInfo.address); |
| 44 }, |
| 45 |
| 46 /** |
| 47 * Logs changed device info to console and updates the cached device. |
| 48 * @param {!bluetoothDevice.DeviceInfo} deviceInfo |
| 49 */ |
| 50 deviceChanged: function(deviceInfo) { |
| 51 console.log(new Date(), deviceInfo); |
| 52 if (this.devices_.has(deviceInfo.address)) { |
| 53 this.devices_.get(deviceInfo.address).info = deviceInfo; |
| 54 } |
| 55 } |
| 56 }; |
| 57 |
32 /** | 58 /** |
33 * TODO(crbug.com/652361): Move to shared location. | 59 * TODO(crbug.com/652361): Move to shared location. |
34 * Helper to convert callback-based define() API to a promise-based API. | 60 * Helper to convert callback-based define() API to a promise-based API. |
35 * @param {!Array<string>} moduleNames | 61 * @param {!Array<string>} moduleNames |
36 * @return {!Promise} | 62 * @return {!Promise} |
37 */ | 63 */ |
38 function importModules(moduleNames) { | 64 function importModules(moduleNames) { |
39 return new Promise(function(resolve, reject) { | 65 return new Promise(function(resolve, reject) { |
40 define(moduleNames, function(var_args) { | 66 define(moduleNames, function(var_args) { |
41 resolve(Array.prototype.slice.call(arguments, 0)); | 67 resolve(Array.prototype.slice.call(arguments, 0)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 adapter = connection.bindHandleToProxy(response.adapter, | 104 adapter = connection.bindHandleToProxy(response.adapter, |
79 bluetoothAdapter.Adapter); | 105 bluetoothAdapter.Adapter); |
80 | 106 |
81 // Create a message pipe and bind one end to client | 107 // Create a message pipe and bind one end to client |
82 // implementation and the other to the Adapter service. | 108 // implementation and the other to the Adapter service. |
83 adapterClient = new AdapterClient(); | 109 adapterClient = new AdapterClient(); |
84 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 110 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); |
85 }); | 111 }); |
86 } | 112 } |
87 | 113 |
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); | |
107 }); | |
108 } | |
109 | |
110 document.addEventListener('DOMContentLoaded', function() { | 114 document.addEventListener('DOMContentLoaded', function() { |
111 initializeProxies() | 115 initializeProxies() |
112 .then(function() { return adapter.getInfo(); }) | 116 .then(function() { return adapter.getInfo(); }) |
113 .then(function(response) { console.log('adapter', response.info); }) | 117 .then(function(response) { console.log('adapter', response.info); }) |
114 .then(function() { return adapter.getDevices(); }) | 118 .then(function() { return adapter.getDevices(); }) |
115 .then(function(response) { | 119 .then(function(response) { |
116 var devices = response.devices; | 120 console.log('devices', response.devices.length); |
117 console.log('devices', devices.length); | |
118 | 121 |
119 return Promise.all(devices.map(logDevice)); | 122 response.devices.forEach(function(deviceInfo) { |
| 123 adapterClient.deviceAdded(deviceInfo); |
| 124 }); |
120 }) | 125 }) |
121 .catch(function(error) { console.error(error); }); | 126 .catch(function(error) { console.error(error); }); |
122 }); | 127 }); |
123 })(); | 128 })(); |
OLD | NEW |