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

Side by Side Diff: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js

Issue 2446823002: bluetooth: Componentize device list in chrome://bluetooth-internals. (Closed)
Patch Set: removeRow->deleteRow 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 unified diff | Download patch
OLDNEW
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 (function() { 10 cr.define('bluetooth_internals', function() {
11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
12
13 var REMOVED_CSS = 'removed';
14
15 /*
16 * Data model for a cached device.
17 * @constructor
18 * @param {!bluetoothDevice.DeviceInfo} info
19 */
20 var Device = function(info) { this.info = info; };
21
22 /** 11 /**
23 * The implementation of AdapterClient in 12 * The implementation of AdapterClient in
24 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the 13 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the
25 * client-side collection of devices. 14 * client-side collection of devices.
26 * @constructor 15 * @constructor
16 * @param {!AdapterBroker} adapterBroker Broker to dispatch events through.
27 */ 17 */
28 var AdapterClient = function() { this.devices_ = new Map(); }; 18 var AdapterClient = function(adapterBroker) {
19 this.adapterBroker_ = adapterBroker;
20 };
21
29 AdapterClient.prototype = { 22 AdapterClient.prototype = {
30 /** 23 /**
31 * Caches the device info and updates the device list. 24 * Fires deviceadded event.
32 * @param {!bluetoothDevice.DeviceInfo} deviceInfo 25 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
33 */ 26 */
34 deviceAdded: function(deviceInfo) { 27 deviceAdded: function(deviceInfo) {
35 if (this.devices_.has(deviceInfo.address)) { 28 var event = new Event('deviceadded');
36 var deviceElement = $(deviceInfo.address); 29 event.deviceInfo = deviceInfo;
37 deviceElement.classList.remove(REMOVED_CSS); 30 this.adapterBroker_.dispatchEvent(event);
38 } else {
39 this.devices_.set(deviceInfo.address, new Device(deviceInfo));
40
41 var deviceRowTemplate = $('device-row-template');
42 var deviceRow = document.importNode(
43 deviceRowTemplate.content.children[0], true /* deep */);
44 deviceRow.id = deviceInfo.address;
45
46 var deviceList = $('device-list');
47 deviceList.appendChild(deviceRow);
48 }
49
50 this.deviceChanged(deviceInfo);
51 }, 31 },
52 32
53 /** 33 /**
54 * Marks device as removed. 34 * Fires deviceremoved event.
55 * @param {!bluetoothDevice.DeviceInfo} deviceInfo 35 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
56 */ 36 */
57 deviceRemoved: function(deviceInfo) { 37 deviceRemoved: function(deviceInfo) {
58 $(deviceInfo.address).classList.add(REMOVED_CSS); 38 var event = new Event('deviceremoved');
39 event.deviceInfo = deviceInfo;
40 this.adapterBroker_.dispatchEvent(event);
59 }, 41 },
60 42
61 /** 43 /**
62 * Updates cached device and updates the device list. 44 * Fires devicechanged event.
63 * @param {!bluetoothDevice.DeviceInfo} deviceInfo 45 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
64 */ 46 */
65 deviceChanged: function(deviceInfo) { 47 deviceChanged: function(deviceInfo) {
66 console.log(new Date(), deviceInfo); 48 console.log(new Date(), deviceInfo);
67 49
68 assert(this.devices_.has(deviceInfo.address), 'Device does not exist.'); 50 var event = new Event('devicechanged');
69 51 event.deviceInfo = deviceInfo;
70 this.devices_.get(deviceInfo.address).info = deviceInfo; 52 this.adapterBroker_.dispatchEvent(event);
71
72 var deviceRow = $(deviceInfo.address);
73 deviceRow.querySelector('.device-name').textContent =
74 deviceInfo.name_for_display;
75 deviceRow.querySelector('.device-address').textContent =
76 deviceInfo.address;
77
78 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
79 deviceRow.querySelector('.device-rssi').textContent;
80 deviceRow.querySelector('.device-rssi').textContent = rssi;
81 } 53 }
82 }; 54 };
83 55
84 /** 56 /**
85 * Initializes Mojo proxies for page and Bluetooth services. 57 * The proxy class of an adapter and router of adapter events.
86 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth 58 * Exposes an EventTarget interface that allows other object to subscribe to
87 * is not supported. 59 * to specific AdapterClient events.
60 * Provides proxy access to Adapter functions. Converts parameters to Mojo
61 * handles and back when necessary.
62 * @constructor
63 * @extends {cr.EventTarget}
64 * @param {!interfaces.BluetoothAdapter.Adapter.proxyClass} adapter
88 */ 65 */
89 function initializeProxies() { 66 var AdapterBroker = function(adapter) {
90 return importModules([ 67 this.adapter_ = adapter;
91 'content/public/renderer/frame_interfaces', 68 this.adapterClient_ = new AdapterClient(this);
92 'device/bluetooth/public/interfaces/adapter.mojom', 69 this.setClient(this.adapterClient_);
93 'device/bluetooth/public/interfaces/device.mojom', 70 };
94 'mojo/public/js/connection',
95 ]).then(function([frameInterfaces, ...modules]) {
96 // Destructure here to assign global variables.
97 [bluetoothAdapter, bluetoothDevice, connection] = modules;
98 71
99 // Hook up the instance properties. 72 AdapterBroker.prototype = {
100 AdapterClient.prototype.__proto__ = 73 __proto__: cr.EventTarget.prototype,
101 bluetoothAdapter.AdapterClient.stubClass.prototype;
102 74
103 var adapterFactory = connection.bindHandleToProxy( 75 /**
104 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), 76 * Sets client of Adapter service.
105 bluetoothAdapter.AdapterFactory); 77 * @param {interfaces.BluetoothAdapter.AdapterClient} adapterClient
78 */
79 setClient: function(adapterClient) {
80 this.adapter_.setClient(interfaces.Connection.bindStubDerivedImpl(
81 adapterClient));
82 },
106 83
107 // Get an Adapter service. 84 /**
108 return adapterFactory.getAdapter(); 85 * Gets an array of currently detectable devices from the Adapter service.
109 }).then(function(response) { 86 * @return {Array<interfaces.BluetoothDevice.DeviceInfo>}
110 if (!response.adapter) { 87 */
111 throw new Error('Bluetooth Not Supported on this platform.'); 88 getDevices: function() {
112 } 89 return this.adapter_.getDevices();
90 },
113 91
114 adapter = connection.bindHandleToProxy(response.adapter, 92 /**
115 bluetoothAdapter.Adapter); 93 * Gets the current state of the Adapter.
94 * @return {interfaces.BluetoothAdapter.AdapterInfo}
95 */
96 getInfo: function() {
97 return this.adapter_.getInfo();
98 }
99 };
116 100
117 // Create a message pipe and bind one end to client 101 var adapterBroker = null;
118 // implementation and the other to the Adapter service. 102
119 adapterClient = new AdapterClient(); 103 function initialize() {
120 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); 104 interfaces.initialize()
121 }); 105 .then(function(adapter) {
106 // Hook up the instance properties.
107 AdapterClient.prototype.__proto__ =
108 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype;
109
110 adapterBroker = new AdapterBroker(adapter);
ortuno 2016/11/03 04:58:21 Would it make sense to: 1. Put AdapterBroker in i
mbrunson 2016/11/03 18:07:28 I can do 1 and 2 relatively easily. Not sure abou
111 })
112 .then(function() { return adapterBroker.getInfo(); })
113 .then(function(response) { console.log('adapter', response.info); })
114 .then(function() { return adapterBroker.getDevices(); })
115 .then(function(response) {
116 // Hook up device collection events.
117 var devices = new device_collection.DeviceCollection([]);
118 adapterBroker.addEventListener('deviceadded', function(event) {
119 devices.addOrUpdate(event.deviceInfo);
120 });
121 adapterBroker.addEventListener('devicechanged', function(event) {
122 devices.addOrUpdate(event.deviceInfo);
123 });
124 adapterBroker.addEventListener('deviceremoved', function(event) {
125 devices.remove(event.deviceInfo);
126 });
127
128 response.devices.forEach(devices.addOrUpdate,
129 devices /** this */);
ortuno 2016/11/03 04:58:21 nit: /** => /*
mbrunson 2016/11/03 18:07:28 Done.
130
131 var deviceTable = new device_table.DeviceTable();
132 deviceTable.setDevices(devices);
133 document.body.appendChild(deviceTable);
134 })
135 .catch(function(error) { console.error(error); });
122 } 136 }
123 137
124 document.addEventListener('DOMContentLoaded', function() { 138 return {
125 initializeProxies() 139 initialize: initialize
126 .then(function() { return adapter.getInfo(); }) 140 };
127 .then(function(response) { console.log('adapter', response.info); }) 141
128 .then(function() { return adapter.getDevices(); }) 142 });
129 .then(function(response) { 143
130 response.devices.forEach(adapterClient.deviceAdded, adapterClient); 144 document.addEventListener('DOMContentLoaded', bluetooth_internals.initialize);
131 })
132 .catch(function(error) { console.error(error); });
133 });
134 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698