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..4398c2474b9a5b41518b514f999b90a7c9b554b6 100644 |
--- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
+++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js |
@@ -12,81 +12,99 @@ var adapterBroker = null; |
var devices = null; |
cr.define('bluetooth_internals', function() { |
+ /** @const */ var DevicesPage = devices_page.DevicesPage; |
+ /** @const */ var PageManager = cr.ui.pageManager.PageManager; |
/** @type {!Map<string, !interfaces.BluetoothDevice.Device.proxyClass>} */ |
var deviceAddressToProxy = new Map(); |
+ /** @type {!device_collection.DeviceCollection} */ |
+ devices = new device_collection.DeviceCollection([]); |
+ |
+ function setupDeviceCollection(response) { |
+ // Hook up device collection events. |
+ 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 */); |
+ DevicesPage.getInstance().setDevices(devices); |
+ } |
+ |
+ function setupPages() { |
+ var sidebar = new window.sidebar.Sidebar('sidebar', 'overlay'); |
+ PageManager.addObserver(sidebar); |
+ |
+ var devicesPage = DevicesPage.getInstance(); |
+ 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
|
+ // 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) { |
+ if (!response) return; |
+ |
+ var deviceInfo = devices.getByAddress(address); |
+ deviceInfo.services = response.services; |
+ devices.addOrUpdate(deviceInfo); |
+ }).catch(function(error) { |
+ devices.updateConnectionStatus( |
+ address, |
+ device_collection.ConnectionStatus.DISCONNECTED, |
+ error); |
+ }); |
+ }); |
+ |
+ devicesPage.pageDiv.addEventListener('menupressed', function() { |
+ sidebar.open(); |
+ }); |
+ |
+ PageManager.register(devicesPage); |
+ PageManager.initialize(devicesPage); |
+ PageManager.showDefaultPage(); |
+ } |
+ |
function initializeViews() { |
+ setupPages(); |
+ |
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) |
.catch(function(error) { console.error(error); }); |
} |