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 // Expose for testing. | 10 // Expose for testing. |
11 var adapterBroker = null; | 11 var adapterBroker = null; |
12 var devices = null; | 12 var devices = null; |
13 | 13 |
14 cr.define('bluetooth_internals', function() { | 14 cr.define('bluetooth_internals', function() { |
15 /** @const */ var DevicesPage = devices_page.DevicesPage; | |
16 /** @const */ var PageManager = cr.ui.pageManager.PageManager; | |
15 | 17 |
16 /** @type {!Map<string, !interfaces.BluetoothDevice.Device.proxyClass>} */ | 18 /** @type {!Map<string, !interfaces.BluetoothDevice.Device.proxyClass>} */ |
17 var deviceAddressToProxy = new Map(); | 19 var deviceAddressToProxy = new Map(); |
18 | 20 |
21 /** @type {!device_collection.DeviceCollection} */ | |
22 devices = new device_collection.DeviceCollection([]); | |
23 | |
24 function setupDeviceCollection(response) { | |
25 // Hook up device collection events. | |
26 adapterBroker.addEventListener('deviceadded', function(event) { | |
27 devices.addOrUpdate(event.detail.deviceInfo); | |
28 }); | |
29 adapterBroker.addEventListener('devicechanged', function(event) { | |
30 devices.addOrUpdate(event.detail.deviceInfo); | |
31 }); | |
32 adapterBroker.addEventListener('deviceremoved', function(event) { | |
33 devices.remove(event.detail.deviceInfo); | |
34 }); | |
35 | |
36 response.devices.forEach(devices.addOrUpdate, devices /* this */); | |
37 DevicesPage.getInstance().setDevices(devices); | |
38 } | |
39 | |
40 function setupPages() { | |
41 var sidebar = new window.sidebar.Sidebar('sidebar', 'overlay'); | |
42 PageManager.addObserver(sidebar); | |
43 | |
44 var devicesPage = DevicesPage.getInstance(); | |
45 devicesPage.pageDiv.addEventListener('inspectpressed', function() { | |
ortuno
2016/12/05 05:45:28
Seems a bit weird and flaky to be adding these bef
mbrunson
2016/12/06 00:25:45
I moved this event listener to the setupDeviceColl
| |
46 // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView | |
47 // when it's added in chrome://bluetooth-internals. | |
48 var address = event.detail.address; | |
49 var proxy = deviceAddressToProxy.get(address); | |
50 | |
51 if (proxy) { | |
52 // Device is already connected, so disconnect. | |
53 proxy.disconnect(); | |
54 deviceAddressToProxy.delete(address); | |
55 devices.updateConnectionStatus( | |
56 address, device_collection.ConnectionStatus.DISCONNECTED); | |
57 return; | |
58 } | |
59 | |
60 devices.updateConnectionStatus( | |
61 address, device_collection.ConnectionStatus.CONNECTING); | |
62 | |
63 adapterBroker.connectToDevice(address).then(function(deviceProxy) { | |
64 if (!devices.getByAddress(address)) { | |
65 // Device no longer in list, so drop the connection. | |
66 deviceProxy.disconnect(); | |
67 return; | |
68 } | |
69 | |
70 deviceAddressToProxy.set(address, deviceProxy); | |
71 devices.updateConnectionStatus( | |
72 address, device_collection.ConnectionStatus.CONNECTED); | |
73 | |
74 // Fetch services asynchronously. | |
75 return deviceProxy.getServices(); | |
76 }).then(function(response) { | |
77 if (!response) return; | |
78 | |
79 var deviceInfo = devices.getByAddress(address); | |
80 deviceInfo.services = response.services; | |
81 devices.addOrUpdate(deviceInfo); | |
82 }).catch(function(error) { | |
83 devices.updateConnectionStatus( | |
84 address, | |
85 device_collection.ConnectionStatus.DISCONNECTED, | |
86 error); | |
87 }); | |
88 }); | |
89 | |
90 devicesPage.pageDiv.addEventListener('menupressed', function() { | |
91 sidebar.open(); | |
92 }); | |
93 | |
94 PageManager.register(devicesPage); | |
95 PageManager.initialize(devicesPage); | |
96 PageManager.showDefaultPage(); | |
97 } | |
98 | |
19 function initializeViews() { | 99 function initializeViews() { |
100 setupPages(); | |
101 | |
20 adapter_broker.getAdapterBroker() | 102 adapter_broker.getAdapterBroker() |
21 .then(function(broker) { adapterBroker = broker; }) | 103 .then(function(broker) { adapterBroker = broker; }) |
22 .then(function() { return adapterBroker.getInfo(); }) | 104 .then(function() { return adapterBroker.getInfo(); }) |
23 .then(function(response) { console.log('adapter', response.info); }) | 105 .then(function(response) { console.log('adapter', response.info); }) |
24 .then(function() { return adapterBroker.getDevices(); }) | 106 .then(function() { return adapterBroker.getDevices(); }) |
25 .then(function(response) { | 107 .then(setupDeviceCollection) |
26 // Hook up device collection events. | |
27 devices = new device_collection.DeviceCollection([]); | |
28 adapterBroker.addEventListener('deviceadded', function(event) { | |
29 devices.addOrUpdate(event.detail.deviceInfo); | |
30 }); | |
31 adapterBroker.addEventListener('devicechanged', function(event) { | |
32 devices.addOrUpdate(event.detail.deviceInfo); | |
33 }); | |
34 adapterBroker.addEventListener('deviceremoved', function(event) { | |
35 devices.remove(event.detail.deviceInfo); | |
36 }); | |
37 | |
38 response.devices.forEach(devices.addOrUpdate, | |
39 devices /* this */); | |
40 | |
41 var deviceTable = new device_table.DeviceTable(); | |
42 | |
43 deviceTable.addEventListener('inspectpressed', function(event) { | |
44 // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView | |
45 // when it's added in chrome://bluetooth-internals. | |
46 var address = event.detail.address; | |
47 var proxy = deviceAddressToProxy.get(address); | |
48 | |
49 if (proxy) { | |
50 // Device is already connected, so disconnect. | |
51 proxy.disconnect(); | |
52 deviceAddressToProxy.delete(address); | |
53 devices.updateConnectionStatus( | |
54 address, device_collection.ConnectionStatus.DISCONNECTED); | |
55 return; | |
56 } | |
57 | |
58 devices.updateConnectionStatus( | |
59 address, device_collection.ConnectionStatus.CONNECTING); | |
60 adapterBroker.connectToDevice(address).then(function(deviceProxy) { | |
61 if (!devices.getByAddress(address)) { | |
62 // Device no longer in list, so drop the connection. | |
63 deviceProxy.disconnect(); | |
64 return; | |
65 } | |
66 | |
67 deviceAddressToProxy.set(address, deviceProxy); | |
68 devices.updateConnectionStatus( | |
69 address, device_collection.ConnectionStatus.CONNECTED); | |
70 | |
71 // Fetch services asynchronously. | |
72 return deviceProxy.getServices(); | |
73 }).then(function(response) { | |
74 var deviceInfo = devices.getByAddress(address); | |
75 deviceInfo.services = response.services; | |
76 devices.addOrUpdate(deviceInfo); | |
77 }).catch(function(error) { | |
78 devices.updateConnectionStatus( | |
79 address, | |
80 device_collection.ConnectionStatus.DISCONNECTED, | |
81 error); | |
82 }); | |
83 }); | |
84 | |
85 deviceTable.setDevices(devices); | |
86 deviceTable.id = 'device-table'; | |
87 | |
88 document.body.appendChild(deviceTable); | |
89 }) | |
90 .catch(function(error) { console.error(error); }); | 108 .catch(function(error) { console.error(error); }); |
91 } | 109 } |
92 | 110 |
93 return { | 111 return { |
94 initializeViews: initializeViews | 112 initializeViews: initializeViews |
95 }; | 113 }; |
96 }); | 114 }); |
97 | 115 |
98 document.addEventListener( | 116 document.addEventListener( |
99 'DOMContentLoaded', bluetooth_internals.initializeViews); | 117 'DOMContentLoaded', bluetooth_internals.initializeViews); |
OLD | NEW |