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 setupDeviceSystem(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 | |
38 var devicesPage = DevicesPage.getInstance(); | |
39 devicesPage.setDevices(devices); | |
40 devicesPage.pageDiv.addEventListener('inspectpressed', function() { | |
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 | |
58 adapterBroker.connectToDevice(address).then(function(deviceProxy) { | |
59 if (!devices.getByAddress(address)) { | |
60 // Device no longer in list, so drop the connection. | |
61 deviceProxy.disconnect(); | |
62 return; | |
63 } | |
64 | |
65 deviceAddressToProxy.set(address, deviceProxy); | |
66 devices.updateConnectionStatus( | |
67 address, device_collection.ConnectionStatus.CONNECTED); | |
68 | |
69 // Fetch services asynchronously. | |
70 return deviceProxy.getServices(); | |
71 }).then(function(response) { | |
72 if (!response) return; | |
73 | |
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 | |
86 function setupPages() { | |
87 var sidebar = new window.sidebar.Sidebar('sidebar', 'overlay'); | |
ortuno
2016/12/06 10:16:05
optional nit: I think passing the element would ma
mbrunson
2016/12/07 01:12:14
Ah. I agree. Done.
| |
88 PageManager.addObserver(sidebar); | |
89 $('menu-btn').addEventListener('click', function() { sidebar.open(); }); | |
90 | |
91 var devicesPage = DevicesPage.getInstance(); | |
ortuno
2016/12/06 10:16:05
I think we should ask someone from WebUI if there
mbrunson
2016/12/07 01:12:14
There doesn't seem to be a technical reason. It wo
| |
92 | |
93 PageManager.register(devicesPage); | |
94 PageManager.initialize(devicesPage); | |
95 PageManager.showDefaultPage(); | |
96 } | |
97 | |
19 function initializeViews() { | 98 function initializeViews() { |
99 setupPages(); | |
100 | |
20 adapter_broker.getAdapterBroker() | 101 adapter_broker.getAdapterBroker() |
21 .then(function(broker) { adapterBroker = broker; }) | 102 .then(function(broker) { adapterBroker = broker; }) |
22 .then(function() { return adapterBroker.getInfo(); }) | 103 .then(function() { return adapterBroker.getInfo(); }) |
23 .then(function(response) { console.log('adapter', response.info); }) | 104 .then(function(response) { console.log('adapter', response.info); }) |
24 .then(function() { return adapterBroker.getDevices(); }) | 105 .then(function() { return adapterBroker.getDevices(); }) |
25 .then(function(response) { | 106 .then(setupDeviceSystem) |
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); }); | 107 .catch(function(error) { console.error(error); }); |
91 } | 108 } |
92 | 109 |
93 return { | 110 return { |
94 initializeViews: initializeViews | 111 initializeViews: initializeViews |
95 }; | 112 }; |
96 }); | 113 }); |
97 | 114 |
98 document.addEventListener( | 115 document.addEventListener( |
99 'DOMContentLoaded', bluetooth_internals.initializeViews); | 116 'DOMContentLoaded', bluetooth_internals.initializeViews); |
OLD | NEW |