Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4912)

Unified Diff: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js

Issue 2446823002: bluetooth: Componentize device list in chrome://bluetooth-internals. (Closed)
Patch Set: Fix issues, add comments, move Adapter code to AdapterBroker Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 93a73935c52d3cabcc24309e489e4b00cc7de649..767c35548d44ab801e0cb45cf37c7cd4e4f24e94 100644
--- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
+++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
@@ -7,128 +7,41 @@
* chrome://bluetooth-internals/.
*/
-(function() {
- var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
-
- var REMOVED_CSS = 'removed';
-
- /*
- * Data model for a cached device.
- * @constructor
- * @param {!bluetoothDevice.DeviceInfo} info
- */
- var Device = function(info) { this.info = info; };
-
- /**
- * The implementation of AdapterClient in
- * device/bluetooth/public/interfaces/adapter.mojom. This also manages the
- * client-side collection of devices.
- * @constructor
- */
- var AdapterClient = function() { this.devices_ = new Map(); };
- AdapterClient.prototype = {
- /**
- * Caches the device info and updates the device list.
- * @param {!bluetoothDevice.DeviceInfo} deviceInfo
- */
- deviceAdded: function(deviceInfo) {
- if (this.devices_.has(deviceInfo.address)) {
- var deviceElement = $(deviceInfo.address);
- deviceElement.classList.remove(REMOVED_CSS);
- } else {
- this.devices_.set(deviceInfo.address, new Device(deviceInfo));
-
- var deviceRowTemplate = $('device-row-template');
- var deviceRow = document.importNode(
- deviceRowTemplate.content.children[0], true /* deep */);
- deviceRow.id = deviceInfo.address;
-
- var deviceList = $('device-list');
- deviceList.appendChild(deviceRow);
- }
-
- this.deviceChanged(deviceInfo);
- },
-
- /**
- * Marks device as removed.
- * @param {!bluetoothDevice.DeviceInfo} deviceInfo
- */
- deviceRemoved: function(deviceInfo) {
- $(deviceInfo.address).classList.add(REMOVED_CSS);
- },
-
- /**
- * Updates cached device and updates the device list.
- * @param {!bluetoothDevice.DeviceInfo} deviceInfo
- */
- deviceChanged: function(deviceInfo) {
- console.log(new Date(), deviceInfo);
-
- assert(this.devices_.has(deviceInfo.address), 'Device does not exist.');
-
- this.devices_.get(deviceInfo.address).info = deviceInfo;
-
- var deviceRow = $(deviceInfo.address);
- deviceRow.querySelector('.device-name').textContent =
- deviceInfo.name_for_display;
- deviceRow.querySelector('.device-address').textContent =
- deviceInfo.address;
-
- var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
- deviceRow.querySelector('.device-rssi').textContent;
- deviceRow.querySelector('.device-rssi').textContent = rssi;
- }
- };
-
- /**
- * Initializes Mojo proxies for page and Bluetooth services.
- * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth
- * is not supported.
- */
- function initializeProxies() {
- return importModules([
- 'content/public/renderer/frame_interfaces',
- 'device/bluetooth/public/interfaces/adapter.mojom',
- 'device/bluetooth/public/interfaces/device.mojom',
- 'mojo/public/js/connection',
- ]).then(function([frameInterfaces, ...modules]) {
- // Destructure here to assign global variables.
- [bluetoothAdapter, bluetoothDevice, connection] = modules;
-
- // Hook up the instance properties.
- AdapterClient.prototype.__proto__ =
- bluetoothAdapter.AdapterClient.stubClass.prototype;
-
- var adapterFactory = connection.bindHandleToProxy(
- frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name),
- bluetoothAdapter.AdapterFactory);
-
- // Get an Adapter service.
- return adapterFactory.getAdapter();
- }).then(function(response) {
- if (!response.adapter) {
- throw new Error('Bluetooth Not Supported on this platform.');
- }
-
- adapter = connection.bindHandleToProxy(response.adapter,
- bluetoothAdapter.Adapter);
-
- // Create a message pipe and bind one end to client
- // implementation and the other to the Adapter service.
- adapterClient = new AdapterClient();
- adapter.setClient(connection.bindStubDerivedImpl(adapterClient));
- });
- }
-
- document.addEventListener('DOMContentLoaded', function() {
- initializeProxies()
- .then(function() { return adapter.getInfo(); })
+cr.define('bluetooth_internals', function() {
+ function initialize() {
+ var adapterBroker = null;
+ adapter_broker.getAdapterBroker()
+ .then(function(broker) { adapterBroker = broker; })
+ .then(function() { return adapterBroker.getInfo(); })
.then(function(response) { console.log('adapter', response.info); })
- .then(function() { return adapter.getDevices(); })
+ .then(function() { return adapterBroker.getDevices(); })
.then(function(response) {
- response.devices.forEach(adapterClient.deviceAdded, adapterClient);
+ // Hook up device collection events.
+ var devices = new device_collection.DeviceCollection([]);
+ adapterBroker.addEventListener('deviceadded', function(event) {
+ devices.addOrUpdate(event.deviceInfo);
+ });
+ adapterBroker.addEventListener('devicechanged', function(event) {
+ devices.addOrUpdate(event.deviceInfo);
+ });
+ adapterBroker.addEventListener('deviceremoved', function(event) {
+ devices.remove(event.deviceInfo);
+ });
+
+ response.devices.forEach(devices.addOrUpdate,
+ devices /* this */);
+
+ var deviceTable = new device_table.DeviceTable();
+ deviceTable.setDevices(devices);
+ document.body.appendChild(deviceTable);
})
.catch(function(error) { console.error(error); });
- });
-})();
+ }
+
+ return {
+ initialize: initialize
ortuno 2016/11/04 01:44:09 Maybe initializeViews
mbrunson 2016/11/04 02:37:40 Done.
+ };
+
+});
+
+document.addEventListener('DOMContentLoaded', bluetooth_internals.initialize);

Powered by Google App Engine
This is Rietveld 408576698