Chromium Code Reviews| 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 var deviceInfo = devices.getByAddress(address); | |
|
scheib
2016/11/29 04:33:44
can this fail, as previous promise block would?
mbrunson
2016/11/30 03:37:16
Yes. I'll check the response before running this.
| |
| 77 deviceInfo.services = response.services; | |
| 78 devices.addOrUpdate(deviceInfo); | |
| 79 }).catch(function(error) { | |
| 80 devices.updateConnectionStatus( | |
| 81 address, | |
| 82 device_collection.ConnectionStatus.DISCONNECTED, | |
| 83 error); | |
| 84 }); | |
| 85 }); | |
| 86 | |
| 87 PageManager.register(devicesView); | |
| 88 PageManager.initialize(devicesView); | |
| 89 PageManager.showDefaultPage(); | |
| 90 } | |
| 91 | |
| 19 function initializeViews() { | 92 function initializeViews() { |
| 20 adapter_broker.getAdapterBroker() | 93 adapter_broker.getAdapterBroker() |
| 21 .then(function(broker) { adapterBroker = broker; }) | 94 .then(function(broker) { adapterBroker = broker; }) |
| 22 .then(function() { return adapterBroker.getInfo(); }) | 95 .then(function() { return adapterBroker.getInfo(); }) |
| 23 .then(function(response) { console.log('adapter', response.info); }) | 96 .then(function(response) { console.log('adapter', response.info); }) |
| 24 .then(function() { return adapterBroker.getDevices(); }) | 97 .then(function() { return adapterBroker.getDevices(); }) |
| 25 .then(function(response) { | 98 .then(setupDeviceCollection) |
| 26 // Hook up device collection events. | 99 .then(setupPages) |
| 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); }); | 100 .catch(function(error) { console.error(error); }); |
| 91 } | 101 } |
| 92 | 102 |
| 93 return { | 103 return { |
| 94 initializeViews: initializeViews | 104 initializeViews: initializeViews |
| 95 }; | 105 }; |
| 96 }); | 106 }); |
| 97 | 107 |
| 98 document.addEventListener( | 108 document.addEventListener( |
| 99 'DOMContentLoaded', bluetooth_internals.initializeViews); | 109 'DOMContentLoaded', bluetooth_internals.initializeViews); |
| OLD | NEW |