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

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

Issue 2418343002: bluetooth: Add device list UI for chrome://bluetooth-internals. (Closed)
Patch Set: Add error for missing device Created 4 years, 2 months 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 (function() {
11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; 11 var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
12 12
13 /* 13 /*
14 * Data model for a cached device. 14 * Data model for a cached device.
15 * @constructor 15 * @constructor
16 * @param {!bluetoothDevice.DeviceInfo} info 16 * @param {!bluetoothDevice.DeviceInfo} info
17 */ 17 */
18 var Device = function(info) { this.info = info; }; 18 var Device = function(info) { this.info = info; };
19 19
20 /** 20 /**
21 * The implementation of AdapterClient in 21 * The implementation of AdapterClient in
22 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the 22 * device/bluetooth/public/interfaces/adapter.mojom. This also manages the
23 * client-side collection of devices. 23 * client-side collection of devices.
24 * @constructor 24 * @constructor
25 */ 25 */
26 var AdapterClient = function() { this.devices_ = new Map(); }; 26 var AdapterClient = function() { this.devices_ = new Map(); };
27 AdapterClient.prototype = { 27 AdapterClient.prototype = {
28 /** 28 /**
29 * Logs added device to console and caches the device info. 29 * Caches the device info and updates the device list.
30 * @param {!bluetoothDevice.DeviceInfo} device 30 * @param {!bluetoothDevice.DeviceInfo} deviceInfo
31 */ 31 */
32 deviceAdded: function(deviceInfo) { 32 deviceAdded: function(deviceInfo) {
33 console.log('Device added', deviceInfo); 33 if (this.devices_.has(deviceInfo.address)) {
dpapad 2016/10/21 22:46:21 How can device that was just added can already exi
mbrunson 2016/10/24 17:15:08 A device can be removed if it is not detected for
34 this.devices_.set(deviceInfo.address, new Device(deviceInfo)); 34 var deviceElement = $(deviceInfo.address);
35 deviceElement.classList.remove('removed');
dpapad 2016/10/21 22:46:21 Let's put all CSS string literals used in JS in an
mbrunson 2016/10/24 17:15:08 Done.
36 } else {
37 this.devices_.set(deviceInfo.address, new Device(deviceInfo));
38
39 var deviceRowTemplate = $('device-row-template');
40 var deviceRow = deviceRowTemplate.content.children[0].cloneNode(
41 true /* deep */);
dpapad 2016/10/21 22:46:21 The recommended way to instantiate templates is do
mbrunson 2016/10/24 17:15:08 Ok. I haven't used it before, but it seems to prod
42 deviceRow.id = deviceInfo.address;
43
44 var deviceList = $('device-list');
45 deviceList.appendChild(deviceRow);
46 }
47
48 this.deviceChanged(deviceInfo);
35 }, 49 },
36 50
37 /** 51 /**
38 * Logs removed device to console and removes the cached device. 52 * Removes the cached device and updates the device list.
39 * @param {!bluetoothDevice.DeviceInfo} device 53 * @param {!bluetoothDevice.DeviceInfo} deviceInfo
40 */ 54 */
41 deviceRemoved: function(deviceInfo) { 55 deviceRemoved: function(deviceInfo) {
42 console.log('Device removed', deviceInfo); 56 $(deviceInfo.address).classList.add('removed');
43 this.devices_.delete(deviceInfo.address);
44 }, 57 },
45 58
46 /** 59 /**
47 * Logs changed device info to console and updates the cached device. 60 * Updates cached device and updates the device list.
48 * @param {!bluetoothDevice.DeviceInfo} deviceInfo 61 * @param {!bluetoothDevice.DeviceInfo} deviceInfo
49 */ 62 */
50 deviceChanged: function(deviceInfo) { 63 deviceChanged: function(deviceInfo) {
51 console.log(new Date(), deviceInfo); 64 console.log(new Date(), deviceInfo);
52 if (this.devices_.has(deviceInfo.address)) { 65
53 this.devices_.get(deviceInfo.address).info = deviceInfo; 66 if (!this.devices_.has(deviceInfo.address))
54 } 67 throw new Error('Device does not exist.');
dpapad 2016/10/21 22:46:21 Can you use assert.js instead, see https://cs.chro
mbrunson 2016/10/24 17:15:08 Done.
68
69 this.devices_.get(deviceInfo.address).info = deviceInfo;
70
71 var deviceRow = $(deviceInfo.address);
72 deviceRow.querySelector('.device-name').innerText =
dpapad 2016/10/21 22:46:22 You probably want to use textContent (more context
mbrunson 2016/10/24 17:15:08 Done.
73 deviceInfo.name_for_display;
74 deviceRow.querySelector('.device-address').innerText =
75 deviceInfo.address;
76
77 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
78 deviceRow.querySelector('.device-rssi').innerText;
79 deviceRow.querySelector('.device-rssi').innerText = rssi;
55 } 80 }
56 }; 81 };
57 82
58 /** 83 /**
59 * TODO(crbug.com/652361): Move to shared location. 84 * TODO(crbug.com/652361): Move to shared location.
60 * Helper to convert callback-based define() API to a promise-based API. 85 * Helper to convert callback-based define() API to a promise-based API.
61 * @param {!Array<string>} moduleNames 86 * @param {!Array<string>} moduleNames
62 * @return {!Promise} 87 * @return {!Promise}
63 */ 88 */
64 function importModules(moduleNames) { 89 function importModules(moduleNames) {
65 return new Promise(function(resolve, reject) { 90 return new Promise(function(resolve, reject) {
66 define(moduleNames, function(var_args) { 91 define(moduleNames, function(var_args) {
67 resolve(Array.prototype.slice.call(arguments, 0)); 92 resolve(Array.prototype.slice.call(arguments, 0));
68 }); 93 });
69 }); 94 });
70 } 95 }
71 96
72 /** 97 /**
73 * Initializes Mojo proxies for page and Bluetooth services. 98 * Initializes Mojo proxies for page and Bluetooth services.
74 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth 99 * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth
75 * is not supported. 100 * is not supported.
76 */ 101 */
77 function initializeProxies() { 102 function initializeProxies() {
78 return importModules([ 103 return importModules([
79 'content/public/renderer/frame_interfaces', 104 'content/public/renderer/frame_interfaces',
80 'device/bluetooth/public/interfaces/adapter.mojom', 105 'device/bluetooth/public/interfaces/adapter.mojom',
81 'device/bluetooth/public/interfaces/device.mojom', 106 'device/bluetooth/public/interfaces/device.mojom',
82 'mojo/public/js/connection', 107 'mojo/public/js/connection',
83 ]).then(function([frameInterfaces, ...modules]) { 108 ]).then(function([frameInterfaces, ...modules]) {
84 console.log('Loaded modules');
85
86 // Destructure here to assign global variables. 109 // Destructure here to assign global variables.
87 [bluetoothAdapter, bluetoothDevice, connection] = modules; 110 [bluetoothAdapter, bluetoothDevice, connection] = modules;
88 111
89 // Hook up the instance properties. 112 // Hook up the instance properties.
90 AdapterClient.prototype.__proto__ = 113 AdapterClient.prototype.__proto__ =
91 bluetoothAdapter.AdapterClient.stubClass.prototype; 114 bluetoothAdapter.AdapterClient.stubClass.prototype;
92 115
93 var adapterFactory = connection.bindHandleToProxy( 116 var adapterFactory = connection.bindHandleToProxy(
94 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), 117 frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name),
95 bluetoothAdapter.AdapterFactory); 118 bluetoothAdapter.AdapterFactory);
(...skipping 14 matching lines...) Expand all
110 adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); 133 adapter.setClient(connection.bindStubDerivedImpl(adapterClient));
111 }); 134 });
112 } 135 }
113 136
114 document.addEventListener('DOMContentLoaded', function() { 137 document.addEventListener('DOMContentLoaded', function() {
115 initializeProxies() 138 initializeProxies()
116 .then(function() { return adapter.getInfo(); }) 139 .then(function() { return adapter.getInfo(); })
117 .then(function(response) { console.log('adapter', response.info); }) 140 .then(function(response) { console.log('adapter', response.info); })
118 .then(function() { return adapter.getDevices(); }) 141 .then(function() { return adapter.getDevices(); })
119 .then(function(response) { 142 .then(function(response) {
120 console.log('devices', response.devices.length); 143 response.devices.forEach(adapterClient.deviceAdded, adapterClient);
121
122 response.devices.forEach(function(deviceInfo) {
123 adapterClient.deviceAdded(deviceInfo);
124 });
125 }) 144 })
126 .catch(function(error) { console.error(error); }); 145 .catch(function(error) { console.error(error); });
127 }); 146 });
128 })(); 147 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698