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

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

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Add comment for SetCallbackRan function in device connection helper 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 DeviceTable UI, served from 6 * Javascript for DeviceTable UI, served from
7 * chrome://bluetooth-internals/. 7 * chrome://bluetooth-internals/.
8 */ 8 */
9 9
10 cr.define('device_table', function() { 10 cr.define('device_table', function() {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 /** 54 /**
55 * Updates table row on change event of the device collection. 55 * Updates table row on change event of the device collection.
56 * @private 56 * @private
57 * @param {!CustomEvent} event 57 * @param {!CustomEvent} event
58 */ 58 */
59 handleChange_: function(event) { 59 handleChange_: function(event) {
60 this.updateRow_(this.devices_.item(event.index), event.index); 60 this.updateRow_(this.devices_.item(event.index), event.index);
61 }, 61 },
62 62
63 /** 63 /**
64 * Generates a function to handle click events on the connect button for the
65 * given |row|.
66 * @private
67 * @param {HTMLTableRowElement} row The table row that was clicked.
68 * @return {function(Event)}
69 */
70 handleConnectBtn_: function(row) {
ortuno 2016/11/08 04:38:02 So currently our code is split in two UI (Device T
mbrunson 2016/11/08 22:45:13 Ahh. I see your point and agree with the event-bas
71 return function(event) {
72 var cellCount = row.cells.length;
73 var connectCell = row.cells[cellCount - 2];
74 var connectButton = connectCell.children[0];
75 var connectErrorCell = row.cells[cellCount - 1];
76
77 connectErrorCell.textContent = '';
78
79 var device = this.devices_.getByAddress(row.id);
80 if (device.proxy) {
81 connectButton.textContent = 'Disconnecting...';
82 connectButton.disabled = true;
83 device.disconnect();
84 } else {
85 connectButton.textContent = 'Connecting...';
86 connectButton.disabled = true;
87
88 this.handleConnect_(device).catch(function(error) {
89 connectErrorCell.textContent = error.message;
90 connectButton.textContent = 'Connect';
91 connectButton.disabled = false;
92 });
93 }
94
95 }.bind(this);
96 },
97
98 /**
99 * Creates a connection to the device and updates the device collection.
100 * @private
101 * @param {device_collection.Device} device
102 * @return {Promise} rejects if connection failed, resolves otherwise.
103 */
104 handleConnect_: function(device) {
105 return device.connect().then(function() {
106 this.devices_.addOrUpdate(device.info);
107 }.bind(this));
108 },
109
110 /**
64 * Updates table row on splice event of the device collection. 111 * Updates table row on splice event of the device collection.
65 * @private 112 * @private
66 * @param {!CustomEvent} event 113 * @param {!CustomEvent} event
67 */ 114 */
68 handleSplice_: function(event) { 115 handleSplice_: function(event) {
69 event.removed.forEach(function() { 116 event.removed.forEach(function() {
70 this.body_.deleteRow(event.index); 117 this.body_.deleteRow(event.index);
71 }, this); 118 }, this);
72 119
73 event.added.forEach(function(device, index) { 120 event.added.forEach(function(device, index) {
74 this.insertRow_(device, event.index + index); 121 this.insertRow_(device, event.index + index);
75 }, this); 122 }, this);
76 }, 123 },
77 124
78 /** 125 /**
79 * Inserts a new row at |index| and updates it with info from |device|. 126 * Inserts a new row at |index| and updates it with info from |device|.
80 * @private 127 * @private
81 * @param {!device_collection.Device} device 128 * @param {!device_collection.Device} device
82 * @param {?number} index 129 * @param {?number} index
83 */ 130 */
84 insertRow_: function(device, index) { 131 insertRow_: function(device, index) {
85 var row = this.body_.insertRow(index); 132 var row = this.body_.insertRow(index);
86 row.id = device.info.address; 133 row.id = device.info.address;
87 134
88 for (var i = 0; i < this.headers_.length; i++) { 135 for (var i = 0; i < this.headers_.length; i++) {
89 row.insertCell(); 136 row.insertCell();
90 } 137 }
91 138
139 // Make two extra cells for the connect button and connect errors.
140 var connectCell = row.insertCell();
141 var connectErrorCell = row.insertCell();
142
143 var connectButton = document.createElement('button');
144 connectCell.appendChild(connectButton);
145
146 connectButton.textContent = 'Connect';
147 connectButton.addEventListener('click',
148 this.handleConnectBtn_(row).bind(this));
149
92 this.updateRow_(device, row.sectionRowIndex); 150 this.updateRow_(device, row.sectionRowIndex);
93 }, 151 },
94 152
95 /** 153 /**
96 * Deletes and recreates the table using the cached |devices_|. 154 * Deletes and recreates the table using the cached |devices_|.
97 * @private 155 * @private
98 */ 156 */
99 redraw_: function() { 157 redraw_: function() {
100 this.removeChild(this.body_); 158 this.removeChild(this.body_);
101 this.appendChild(document.createElement('tbody')); 159 this.appendChild(document.createElement('tbody'));
(...skipping 14 matching lines...) Expand all
116 updateRow_: function(device, index) { 174 updateRow_: function(device, index) {
117 assert(this.body_.rows[index], 'Row ' + index + ' is not in the table.'); 175 assert(this.body_.rows[index], 'Row ' + index + ' is not in the table.');
118 var row = this.body_.rows[index]; 176 var row = this.body_.rows[index];
119 177
120 if (device.removed) { 178 if (device.removed) {
121 row.classList.add(REMOVED_CSS); 179 row.classList.add(REMOVED_CSS);
122 } else { 180 } else {
123 row.classList.remove(REMOVED_CSS); 181 row.classList.remove(REMOVED_CSS);
124 } 182 }
125 183
184 var cellCount = row.cells.length;
185 var connectCell = row.cells[cellCount - 2];
ortuno 2016/11/08 04:38:02 Is there a better way to retrieve this? Would it m
mbrunson 2016/11/08 22:45:13 The connection-related columns are always at the e
ortuno 2016/11/09 03:24:03 Right, but getting a cell by counting from the end
mbrunson 2016/11/09 23:39:37 Done.
186 var connectButton = connectCell.children[0];
187 var connectErrorCell = row.cells[cellCount - 1];
188
189 if (!device.connectionPending) {
190 if (device.info.is_gatt_connected) {
ortuno 2016/11/08 04:38:02 This is a little tricky because there are two diff
mbrunson 2016/11/08 22:45:13 I'll use inspect/forget for now. Done.
191 connectButton.textContent = 'Disconnect';
192 } else if (device.proxy) {
193 connectButton.textContent = 'Connect';
194 connectErrorCell.textContent = 'Lost connection';
195 device.proxy = null;
196 } else {
197 connectButton.textContent = 'Connect';
198 }
199
200 connectButton.disabled = false;
201 }
202
203
126 // Update the properties based on the header field path. 204 // Update the properties based on the header field path.
127 for (var i = 0; i < this.headers_.length; i++) { 205 for (var i = 0; i < this.headers_.length; i++) {
128 var header = this.headers_[i]; 206 var header = this.headers_[i];
129 var propName = header.dataset.field; 207 var propName = header.dataset.field;
130 208
131 var parts = propName.split('.'); 209 var parts = propName.split('.');
132 var obj = device.info; 210 var obj = device.info;
133 while (obj != null && parts.length > 0) { 211 while (obj != null && parts.length > 0) {
134 var part = parts.shift(); 212 var part = parts.shift();
135 obj = obj[part]; 213 obj = obj[part];
136 } 214 }
137 215
138 var cell = row.cells[i]; 216 var cell = row.cells[i];
139 cell.textContent = obj || 'Unknown'; 217 cell.textContent = (obj == null || obj == undefined) ? 'Unknown' : obj;
140 cell.dataset.label = header.textContent; 218 cell.dataset.label = header.textContent;
141 } 219 }
142 }, 220 },
143 }; 221 };
144 222
145 return { 223 return {
146 DeviceTable: DeviceTable, 224 DeviceTable: DeviceTable,
147 }; 225 };
148 }); 226 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698