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 DevicesView = devices_view.DevicesView; | |
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 function setupDeviceCollection(response) { | |
22 // Hook up device collection events. | |
23 devices = new device_collection.DeviceCollection([]); | |
24 adapterBroker.addEventListener('deviceadded', function(event) { | |
25 devices.addOrUpdate(event.detail.deviceInfo); | |
26 }); | |
27 adapterBroker.addEventListener('devicechanged', function(event) { | |
28 devices.addOrUpdate(event.detail.deviceInfo); | |
29 }); | |
30 adapterBroker.addEventListener('deviceremoved', function(event) { | |
31 devices.remove(event.detail.deviceInfo); | |
32 }); | |
33 | |
34 response.devices.forEach(devices.addOrUpdate, devices /* this */); | |
35 DevicesView.getInstance().setDevices(devices); | |
36 } | |
37 | |
38 function setupPages() { | |
39 var sidebar = new window.sidebar.Sidebar('sidebar', 'overlay'); | |
40 $('menu-btn').addEventListener('click', function() { sidebar.open(); }); | |
41 PageManager.addObserver(sidebar); | |
42 | |
43 var devicesView = DevicesView.getInstance(); | |
44 devicesView.pageDiv.addEventListener('inspectpressed', function() { | |
45 // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView | |
46 // when it's added in chrome://bluetooth-internals. | |
47 var address = event.detail.address; | |
48 var proxy = deviceAddressToProxy.get(address); | |
49 | |
50 if (proxy) { | |
51 // Device is already connected, so disconnect. | |
52 proxy.disconnect(); | |
53 deviceAddressToProxy.delete(address); | |
54 devices.updateConnectionStatus( | |
55 address, device_collection.ConnectionStatus.DISCONNECTED); | |
56 return; | |
57 } | |
58 | |
59 devices.updateConnectionStatus( | |
60 address, device_collection.ConnectionStatus.CONNECTING); | |
61 | |
62 adapterBroker.connectToDevice(address).then(function(deviceProxy) { | |
63 if (!devices.getByAddress(address)) { | |
64 // Device no longer in list, so drop the connection. | |
65 deviceProxy.disconnect(); | |
66 return; | |
67 } | |
68 | |
69 deviceAddressToProxy.set(address, deviceProxy); | |
70 devices.updateConnectionStatus( | |
71 address, device_collection.ConnectionStatus.CONNECTED); | |
72 | |
73 // Fetch services asynchronously. | |
74 return deviceProxy.getServices(); | |
75 }).then(function(response) { | |
76 if (!response) return; | |
77 | |
78 var deviceInfo = devices.getByAddress(address); | |
79 deviceInfo.services = response.services; | |
80 devices.addOrUpdate(deviceInfo); | |
81 }).catch(function(error) { | |
82 devices.updateConnectionStatus( | |
83 address, | |
84 device_collection.ConnectionStatus.DISCONNECTED, | |
85 error); | |
86 }); | |
87 }); | |
88 | |
89 PageManager.register(devicesView); | |
90 PageManager.initialize(devicesView); | |
91 PageManager.showDefaultPage(); | |
92 } | |
93 | |
19 function initializeViews() { | 94 function initializeViews() { |
20 adapter_broker.getAdapterBroker() | 95 adapter_broker.getAdapterBroker() |
21 .then(function(broker) { adapterBroker = broker; }) | 96 .then(function(broker) { adapterBroker = broker; }) |
22 .then(function() { return adapterBroker.getInfo(); }) | 97 .then(function() { return adapterBroker.getInfo(); }) |
23 .then(function(response) { console.log('adapter', response.info); }) | 98 .then(function(response) { console.log('adapter', response.info); }) |
24 .then(function() { return adapterBroker.getDevices(); }) | 99 .then(function() { return adapterBroker.getDevices(); }) |
25 .then(function(response) { | 100 .then(setupDeviceCollection) |
26 // Hook up device collection events. | 101 .then(setupPages) |
ortuno
2016/12/01 06:27:03
Is the order correct here? If there is any error g
mbrunson
2016/12/02 02:31:45
Yes. That's a possibility. I'll initialize the pag
| |
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); }); | 102 .catch(function(error) { console.error(error); }); |
91 } | 103 } |
92 | 104 |
93 return { | 105 return { |
94 initializeViews: initializeViews | 106 initializeViews: initializeViews |
95 }; | 107 }; |
96 }); | 108 }); |
97 | 109 |
98 document.addEventListener( | 110 document.addEventListener( |
99 'DOMContentLoaded', bluetooth_internals.initializeViews); | 111 'DOMContentLoaded', bluetooth_internals.initializeViews); |
OLD | NEW |