Chromium Code Reviews| Index: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| diff --git a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| index 2826f2e4ad0ae6a37db2da5922cda04d730efbc4..050732258892c1b689c3b72cdfa2477814d5347e 100644 |
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
| @@ -12,81 +12,91 @@ var adapterBroker = null; |
| var devices = null; |
| cr.define('bluetooth_internals', function() { |
| + /** @const */ var DevicesView = devices_view.DevicesView; |
| + /** @const */ var PageManager = cr.ui.pageManager.PageManager; |
| /** @type {!Map<string, !interfaces.BluetoothDevice.Device.proxyClass>} */ |
| var deviceAddressToProxy = new Map(); |
| + function setupDeviceCollection(response) { |
| + // Hook up device collection events. |
| + devices = new device_collection.DeviceCollection([]); |
| + adapterBroker.addEventListener('deviceadded', function(event) { |
| + devices.addOrUpdate(event.detail.deviceInfo); |
| + }); |
| + adapterBroker.addEventListener('devicechanged', function(event) { |
| + devices.addOrUpdate(event.detail.deviceInfo); |
| + }); |
| + adapterBroker.addEventListener('deviceremoved', function(event) { |
| + devices.remove(event.detail.deviceInfo); |
| + }); |
| + |
| + response.devices.forEach(devices.addOrUpdate, devices /* this */); |
| + DevicesView.getInstance().setDevices(devices); |
| + } |
| + |
| + function setupPages() { |
| + var sidebar = new window.sidebar.Sidebar('sidebar', 'overlay'); |
| + $('menu-btn').addEventListener('click', function() { sidebar.open(); }); |
| + PageManager.addObserver(sidebar); |
| + |
| + var devicesView = DevicesView.getInstance(); |
| + devicesView.pageDiv.addEventListener('inspectpressed', function() { |
| + // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView |
| + // when it's added in chrome://bluetooth-internals. |
| + var address = event.detail.address; |
| + var proxy = deviceAddressToProxy.get(address); |
| + |
| + if (proxy) { |
| + // Device is already connected, so disconnect. |
| + proxy.disconnect(); |
| + deviceAddressToProxy.delete(address); |
| + devices.updateConnectionStatus( |
| + address, device_collection.ConnectionStatus.DISCONNECTED); |
| + return; |
| + } |
| + |
| + devices.updateConnectionStatus( |
| + address, device_collection.ConnectionStatus.CONNECTING); |
| + |
| + adapterBroker.connectToDevice(address).then(function(deviceProxy) { |
| + if (!devices.getByAddress(address)) { |
| + // Device no longer in list, so drop the connection. |
| + deviceProxy.disconnect(); |
| + return; |
| + } |
| + |
| + deviceAddressToProxy.set(address, deviceProxy); |
| + devices.updateConnectionStatus( |
| + address, device_collection.ConnectionStatus.CONNECTED); |
| + |
| + // Fetch services asynchronously. |
| + return deviceProxy.getServices(); |
| + }).then(function(response) { |
| + 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.
|
| + deviceInfo.services = response.services; |
| + devices.addOrUpdate(deviceInfo); |
| + }).catch(function(error) { |
| + devices.updateConnectionStatus( |
| + address, |
| + device_collection.ConnectionStatus.DISCONNECTED, |
| + error); |
| + }); |
| + }); |
| + |
| + PageManager.register(devicesView); |
| + PageManager.initialize(devicesView); |
| + PageManager.showDefaultPage(); |
| + } |
| + |
| function initializeViews() { |
| adapter_broker.getAdapterBroker() |
| .then(function(broker) { adapterBroker = broker; }) |
| .then(function() { return adapterBroker.getInfo(); }) |
| .then(function(response) { console.log('adapter', response.info); }) |
| .then(function() { return adapterBroker.getDevices(); }) |
| - .then(function(response) { |
| - // Hook up device collection events. |
| - devices = new device_collection.DeviceCollection([]); |
| - adapterBroker.addEventListener('deviceadded', function(event) { |
| - devices.addOrUpdate(event.detail.deviceInfo); |
| - }); |
| - adapterBroker.addEventListener('devicechanged', function(event) { |
| - devices.addOrUpdate(event.detail.deviceInfo); |
| - }); |
| - adapterBroker.addEventListener('deviceremoved', function(event) { |
| - devices.remove(event.detail.deviceInfo); |
| - }); |
| - |
| - response.devices.forEach(devices.addOrUpdate, |
| - devices /* this */); |
| - |
| - var deviceTable = new device_table.DeviceTable(); |
| - |
| - deviceTable.addEventListener('inspectpressed', function(event) { |
| - // TODO(crbug.com/663470): Move connection logic to DeviceDetailsView |
| - // when it's added in chrome://bluetooth-internals. |
| - var address = event.detail.address; |
| - var proxy = deviceAddressToProxy.get(address); |
| - |
| - if (proxy) { |
| - // Device is already connected, so disconnect. |
| - proxy.disconnect(); |
| - deviceAddressToProxy.delete(address); |
| - devices.updateConnectionStatus( |
| - address, device_collection.ConnectionStatus.DISCONNECTED); |
| - return; |
| - } |
| - |
| - devices.updateConnectionStatus( |
| - address, device_collection.ConnectionStatus.CONNECTING); |
| - adapterBroker.connectToDevice(address).then(function(deviceProxy) { |
| - if (!devices.getByAddress(address)) { |
| - // Device no longer in list, so drop the connection. |
| - deviceProxy.disconnect(); |
| - return; |
| - } |
| - |
| - deviceAddressToProxy.set(address, deviceProxy); |
| - devices.updateConnectionStatus( |
| - address, device_collection.ConnectionStatus.CONNECTED); |
| - |
| - // Fetch services asynchronously. |
| - return deviceProxy.getServices(); |
| - }).then(function(response) { |
| - var deviceInfo = devices.getByAddress(address); |
| - deviceInfo.services = response.services; |
| - devices.addOrUpdate(deviceInfo); |
| - }).catch(function(error) { |
| - devices.updateConnectionStatus( |
| - address, |
| - device_collection.ConnectionStatus.DISCONNECTED, |
| - error); |
| - }); |
| - }); |
| - |
| - deviceTable.setDevices(devices); |
| - deviceTable.id = 'device-table'; |
| - |
| - document.body.appendChild(deviceTable); |
| - }) |
| + .then(setupDeviceCollection) |
| + .then(setupPages) |
| .catch(function(error) { console.error(error); }); |
| } |