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 cr.define('bluetooth_internals', function() { | 10 cr.define('bluetooth_internals', function() { |
11 | |
12 /** @type {!Map<string, !interfaces.BluetoothDevice.Device.proxyClass>} */ | |
13 var deviceAddressToProxy = new Map(); | |
14 | |
11 function initializeViews() { | 15 function initializeViews() { |
12 var adapterBroker = null; | 16 var adapterBroker = null; |
13 adapter_broker.getAdapterBroker() | 17 adapter_broker.getAdapterBroker() |
14 .then(function(broker) { adapterBroker = broker; }) | 18 .then(function(broker) { adapterBroker = broker; }) |
15 .then(function() { return adapterBroker.getInfo(); }) | 19 .then(function() { return adapterBroker.getInfo(); }) |
16 .then(function(response) { console.log('adapter', response.info); }) | 20 .then(function(response) { console.log('adapter', response.info); }) |
17 .then(function() { return adapterBroker.getDevices(); }) | 21 .then(function() { return adapterBroker.getDevices(); }) |
18 .then(function(response) { | 22 .then(function(response) { |
19 // Hook up device collection events. | 23 // Hook up device collection events. |
20 var devices = new device_collection.DeviceCollection([]); | 24 var devices = new device_collection.DeviceCollection([]); |
21 adapterBroker.addEventListener('deviceadded', function(event) { | 25 adapterBroker.addEventListener('deviceadded', function(event) { |
22 devices.addOrUpdate(event.detail.deviceInfo); | 26 devices.addOrUpdate(event.detail.deviceInfo); |
23 }); | 27 }); |
24 adapterBroker.addEventListener('devicechanged', function(event) { | 28 adapterBroker.addEventListener('devicechanged', function(event) { |
25 devices.addOrUpdate(event.detail.deviceInfo); | 29 devices.addOrUpdate(event.detail.deviceInfo); |
26 }); | 30 }); |
27 adapterBroker.addEventListener('deviceremoved', function(event) { | 31 adapterBroker.addEventListener('deviceremoved', function(event) { |
28 devices.remove(event.detail.deviceInfo); | 32 devices.remove(event.detail.deviceInfo); |
29 }); | 33 }); |
30 | 34 |
31 response.devices.forEach(devices.addOrUpdate, | 35 response.devices.forEach(devices.addOrUpdate, |
32 devices /* this */); | 36 devices /* this */); |
33 | 37 |
34 var deviceTable = new device_table.DeviceTable(); | 38 var deviceTable = new device_table.DeviceTable(); |
39 | |
40 deviceTable.addEventListener('inspectpressed', function(event) { | |
41 // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView | |
42 // when it's added in chrome://bluetooth-internals. | |
43 var address = event.detail.address; | |
44 var proxy = deviceAddressToProxy.get(address); | |
45 | |
46 if (proxy) { | |
47 // Device is already connected, so disconnect. | |
48 proxy.disconnect(); | |
49 deviceAddressToProxy.delete(address); | |
50 devices.updateConnectionStatus( | |
51 address, device_collection.ConnectionStatus.DISCONNECTED); | |
52 return; | |
53 } | |
54 | |
55 devices.updateConnectionStatus( | |
56 address, device_collection.ConnectionStatus.CONNECTING); | |
57 adapterBroker.connectToDevice(address).then(function(deviceProxy) { | |
dpapad
2016/11/10 22:33:20
Can you chain instead of nest Promise callbacks? C
dpapad
2016/11/10 22:33:59
Correction:
*nested callbacks are less readable
mbrunson
2016/11/11 21:26:14
Done.
| |
58 if (!devices.getByAddress(address)) { | |
59 // Device no longer in list, so drop the connection. | |
60 deviceProxy.disconnect(); | |
61 return; | |
62 } | |
63 | |
64 deviceAddressToProxy.set(address, deviceProxy); | |
65 devices.updateConnectionStatus( | |
66 address, device_collection.ConnectionStatus.CONNECTED); | |
67 | |
68 // Fetch services asynchronously. | |
69 deviceProxy.getServices().then(function(response) { | |
70 var deviceInfo = devices.getByAddress(address); | |
71 deviceInfo.services = response.services; | |
72 devices.addOrUpdate(deviceInfo); | |
73 }); | |
74 }).catch(function(error) { | |
75 devices.updateConnectionStatus( | |
76 address, | |
77 device_collection.ConnectionStatus.DISCONNECTED, | |
78 error); | |
79 }); | |
80 }); | |
81 | |
35 deviceTable.setDevices(devices); | 82 deviceTable.setDevices(devices); |
36 document.body.appendChild(deviceTable); | 83 document.body.appendChild(deviceTable); |
37 }) | 84 }) |
38 .catch(function(error) { console.error(error); }); | 85 .catch(function(error) { console.error(error); }); |
39 } | 86 } |
40 | 87 |
41 return { | 88 return { |
42 initializeViews: initializeViews | 89 initializeViews: initializeViews |
43 }; | 90 }; |
44 | 91 |
45 }); | 92 }); |
46 | 93 |
47 document.addEventListener( | 94 document.addEventListener( |
48 'DOMContentLoaded', bluetooth_internals.initializeViews); | 95 'DOMContentLoaded', bluetooth_internals.initializeViews); |
OLD | NEW |