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

Side by Side Diff: chrome/browser/resources/chromeos/network_ui/network_ui.js

Issue 260083007: Replace chrome://network implementation with networkConfig API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Error handling Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 var NetworkUI = function() { 5 var NetworkUI = (function() {
6 'use strict';
7
6 // Properties to display in the network state table. Each entry can be either 8 // Properties to display in the network state table. Each entry can be either
7 // a single state field or an array of state fields. If more than one is 9 // a single state field or an array of state fields. If more than one is
8 // specified then the first non empty value is used. 10 // specified then the first non empty value is used.
9 var NETWORK_STATE_FIELDS = [ 11 var NETWORK_STATE_FIELDS = [
10 'Name', 'Type', 'State', 'Profile', 'Connectable', 12 'GUID',
11 'Error', 'Security', 13 'Name',
12 ['Cellular.NetworkTechnology', 'EAP.EAP'], 14 'Type',
13 'Cellular.ActivationState', 'Cellular.RoamingState', 15 'ConnectionState',
14 'Cellular.OutOfCredits', 'Strength' 16 'ErrorState',
17 'WiFi.Security',
18 ['Cellular.NetworkTechnology',
19 'EAP.EAP'],
20 'Cellular.ActivationState',
21 'Cellular.RoamingState',
22 'Cellular.OutOfCredits',
23 'WiFi.SignalStrength'
15 ]; 24 ];
16 25
17 var FAVORITE_STATE_FIELDS = [ 26 var FAVORITE_STATE_FIELDS = [
18 'Name', 'Type', 'Profile', 'onc_source' 27 'GUID',
28 'Name',
29 'Type',
30 'Profile',
31 'OncSource'
pneubeck (no reviews) 2014/05/22 16:24:20 In network_util.cc you use 'onc_source' (I comment
stevenjb 2014/05/23 21:50:59 I went back and fort with this. I'll stick with 'o
19 ]; 32 ];
20 33
21 var LOG_LEVEL_CLASSNAME = { 34 var LOG_LEVEL_CLASSNAME = {
22 'Error': 'network-log-level-error', 35 'Error': 'network-log-level-error',
23 'User': 'network-log-level-user', 36 'User': 'network-log-level-user',
24 'Event': 'network-log-level-event', 37 'Event': 'network-log-level-event',
25 'Debug': 'network-log-level-debug' 38 'Debug': 'network-log-level-debug'
26 }; 39 };
27 40
28 var LOG_LEVEL_CHECKBOX = { 41 var LOG_LEVEL_CHECKBOX = {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 for (var i = 0; i < logEntries.length; ++i) { 103 for (var i = 0; i < logEntries.length; ++i) {
91 var entry = createLogEntryText(JSON.parse(logEntries[i])); 104 var entry = createLogEntryText(JSON.parse(logEntries[i]));
92 if (entry) 105 if (entry)
93 container.appendChild(entry); 106 container.appendChild(entry);
94 } 107 }
95 }; 108 };
96 109
97 /** 110 /**
98 * Create a cell with a button for expanding a network state table row. 111 * Create a cell with a button for expanding a network state table row.
99 * 112 *
100 * @param {dictionary} state Property values for the network or favorite. 113 * @param {dictionary} state Property values for the network or favorite.
xiyuan 2014/05/21 21:22:28 Update the doc here.
stevenjb 2014/05/23 21:50:59 Done.
101 * @return {DOMElement} The created td element that displays the given value. 114 * @return {DOMElement} The created td element that displays the given value.
102 */ 115 */
103 var createStateTableExpandButton = function(state) { 116 var createStateTableExpandButton = function(guid) {
104 var cell = document.createElement('td'); 117 var cell = document.createElement('td');
105 cell.className = 'state-table-expand-button-cell'; 118 cell.className = 'state-table-expand-button-cell';
106 var button = document.createElement('button'); 119 var button = document.createElement('button');
107 button.addEventListener('click', function(event) { 120 button.addEventListener('click', function(event) {
108 toggleExpandRow(event.target, state); 121 toggleExpandRow(event.target, guid);
109 }); 122 });
110 button.className = 'state-table-expand-button'; 123 button.className = 'state-table-expand-button';
124 button.innerHTML = '+';
xiyuan 2014/05/21 21:22:28 Use .textContent instead of .innerHTML Here and ot
stevenjb 2014/05/23 21:50:59 Done.
111 cell.appendChild(button); 125 cell.appendChild(button);
112 return cell; 126 return cell;
113 }; 127 };
114 128
115 /** 129 /**
116 * Create a cell in network state table. 130 * Create a cell in network state table.
117 * 131 *
118 * @param {string} value Content in the cell. 132 * @param {string} value Content in the cell.
119 * @return {DOMElement} The created td element that displays the given value. 133 * @return {DOMElement} The created td element that displays the given value.
120 */ 134 */
121 var createStateTableCell = function(value) { 135 var createStateTableCell = function(value) {
122 var cell = document.createElement('td'); 136 var cell = document.createElement('td');
123 cell.textContent = value || ''; 137 cell.textContent = value || '';
124 return cell; 138 return cell;
125 }; 139 };
126 140
127 /** 141 /**
128 * Create a row in the network state table. 142 * Create a row in the network state table.
129 * 143 *
130 * @param {string} stateFields The state fields to use for the row. 144 * @param {string} stateFields The state fields to use for the row.
pneubeck (no reviews) 2014/05/22 16:24:20 string -> Array
stevenjb 2014/05/23 21:50:59 Done.
131 * @param {string} path The network or favorite path.
132 * @param {dictionary} state Property values for the network or favorite. 145 * @param {dictionary} state Property values for the network or favorite.
133 * @return {DOMElement} The created tr element that contains the network 146 * @return {DOMElement} The created tr element that contains the network
134 * state information. 147 * state information.
135 */ 148 */
136 var createStateTableRow = function(stateFields, path, state) { 149 var createStateTableRow = function(stateFields, state) {
137 var row = document.createElement('tr'); 150 var row = document.createElement('tr');
138 row.className = 'state-table-row'; 151 row.className = 'state-table-row';
139 row.appendChild(createStateTableExpandButton(state)); 152 var guid = networkConfig.getValueFromProperties(state, 'GUID');
pneubeck (no reviews) 2014/05/22 16:24:20 equivalent to 'networkConfig.GUID' better use the
stevenjb 2014/05/23 21:50:59 Done.
140 row.appendChild(createStateTableCell(path)); 153 row.appendChild(createStateTableExpandButton(guid));
141 var guid = state['GUID'];
142 if (guid)
143 guid = guid.slice(1, 9);
144 row.appendChild(createStateTableCell(guid));
145 for (var i = 0; i < stateFields.length; ++i) { 154 for (var i = 0; i < stateFields.length; ++i) {
146 var field = stateFields[i]; 155 var field = stateFields[i];
147 var value = ''; 156 var value = '';
148 if (typeof field == 'string') { 157 if (typeof field == 'string') {
149 value = state[field]; 158 value = networkConfig.getValueFromProperties(state, field);
150 } else { 159 } else {
151 for (var j = 0; j < field.length; ++j) { 160 for (var j = 0; j < field.length; ++j) {
152 value = state[field[j]]; 161 value = networkConfig.getValueFromProperties(state, field[j]);
153 if (value) 162 if (value)
154 break; 163 break;
155 } 164 }
156 } 165 }
166 if (field == 'GUID' && field[0] != '/')
xiyuan 2014/05/21 21:22:28 Did you mean "||" rather than "&&"?
pneubeck (no reviews) 2014/05/22 16:24:20 what does the "field[0] != '/'" check mean? if cor
stevenjb 2014/05/23 21:50:59 This is leftover from when the GUID was sometimes
167 value = value.slice(0, 12);
157 row.appendChild(createStateTableCell(value)); 168 row.appendChild(createStateTableCell(value));
158 } 169 }
159 return row; 170 return row;
160 }; 171 };
161 172
162 /** 173 /**
163 * Create table for networks or favorites. 174 * Create table for networks or favorites.
164 * 175 *
165 * @param {string} tablename The name of the table to be created. 176 * @param {string} tablename The name of the table to be created.
166 * @param {Array.<Object>} stateFields The list of fields for the table. 177 * @param {Array.<Object>} stateFields The list of fields for the table.
pneubeck (no reviews) 2014/05/22 16:24:20 Array.<Object> -> Array or if it shall be precise:
stevenjb 2014/05/23 21:50:59 Done.
167 * @param {Array.<Object>} states An array of network or favorite states. 178 * @param {Array.<Object>} states An array of network or favorite states.
168 */ 179 */
169 var createStateTable = function(tablename, stateFields, states) { 180 var createStateTable = function(tablename, stateFields, states) {
170 var table = $(tablename); 181 var table = $(tablename);
171 var oldRows = table.querySelectorAll('.state-table-row'); 182 var oldRows = table.querySelectorAll('.state-table-row');
172 for (var i = 0; i < oldRows.length; ++i) 183 for (var i = 0; i < oldRows.length; ++i)
173 table.removeChild(oldRows[i]); 184 table.removeChild(oldRows[i]);
174 for (var path in states) 185 states.forEach(function(state) {
175 table.appendChild(createStateTableRow(stateFields, path, states[path])); 186 table.appendChild(createStateTableRow(stateFields, state));
187 });
176 }; 188 };
177 189
178 /** 190 /**
179 * This callback function is triggered when the data is received. 191 * This callback function is triggered when the network log is received.
180 * 192 *
181 * @param {dictionary} data A dictionary that contains network state 193 * @param {dictionary} data A JSON structure of event log entries.
xiyuan 2014/05/21 21:22:28 nit: dictionary -> Object
pneubeck (no reviews) 2014/05/22 16:24:20 and every other such occurrence
stevenjb 2014/05/23 21:50:59 Done.
182 * information.
183 */ 194 */
184 var onNetworkInfoReceived = function(data) { 195 var getNetworkLogCallback = function(data) {
185 createEventLog(JSON.parse(data.networkEventLog)); 196 createEventLog(JSON.parse(data));
186 createStateTable(
187 'network-state-table', NETWORK_STATE_FIELDS, data.networkStates);
188 createStateTable(
189 'favorite-state-table', FAVORITE_STATE_FIELDS, data.favoriteStates);
190 }; 197 };
191 198
192 /** 199 /**
200 * This callback function is triggered when visible networks are received.
201 *
202 * @param {dictionary} data A list of network state information for each
pneubeck (no reviews) 2014/05/22 16:24:20 dictionary -> Array.<Object>
stevenjb 2014/05/23 21:50:59 Done.
203 * visible network.
204 */
205 var onVisibleNetworksReceived = function(states) {
206 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states);
207 };
208
209 /**
210 * This callback function is triggered when favorite networks are received.
211 *
212 * @param {dictionary} data A list of network state information for each
pneubeck (no reviews) 2014/05/22 16:24:20 dictionary -> Array.<Object>
stevenjb 2014/05/23 21:50:59 Done.
213 * favorite network.
214 */
215 var onFavoriteNetworksReceived = function(states) {
216 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states);
217 };
218
219 /**
193 * Toggle the button state and add or remove a row displaying the complete 220 * Toggle the button state and add or remove a row displaying the complete
194 * state information for a row. 221 * state information for a row.
195 * 222 *
196 * @param {DOMElement} btn The button that was clicked. 223 * @param {DOMElement} btn The button that was clicked.
197 * @param {dictionary} state Property values for the network or favorite. 224 * @param {dictionary} state Property values for the network or favorite.
198 */ 225 */
199 var toggleExpandRow = function(btn, state) { 226 var toggleExpandRow = function(btn, guid) {
xiyuan 2014/05/21 21:22:28 Add doc for |guid|
stevenjb 2014/05/23 21:50:59 Done.
200 var cell = btn.parentNode; 227 var cell = btn.parentNode;
201 var row = cell.parentNode; 228 var row = cell.parentNode;
202 if (btn.classList.contains('state-table-expand-button-expanded')) { 229 if (btn.innerHTML == '-') {
203 btn.classList.remove('state-table-expand-button-expanded'); 230 btn.innerHTML = '+';
204 row.parentNode.removeChild(row.nextSibling); 231 row.parentNode.removeChild(row.nextSibling);
205 } else { 232 } else {
206 btn.classList.add('state-table-expand-button-expanded'); 233 btn.innerHTML = '-';
207 var expandedRow = createExpandedRow(state, row); 234 var expandedRow = createExpandedRow(guid, row);
208 row.parentNode.insertBefore(expandedRow, row.nextSibling); 235 row.parentNode.insertBefore(expandedRow, row.nextSibling);
209 } 236 }
210 }; 237 };
211 238
212 /** 239 /**
213 * Creates the expanded row for displaying the complete state as JSON. 240 * Creates the expanded row for displaying the complete state as JSON.
214 * 241 *
215 * @param {dictionary} state Property values for the network or favorite. 242 * @param {dictionary} state Property values for the network or favorite.
216 * @param {DOMElement} baseRow The unexpanded row associated with the new row. 243 * @param {DOMElement} baseRow The unexpanded row associated with the new row.
217 * @return {DOMElement} The created tr element for the expanded row. 244 * @return {DOMElement} The created tr element for the expanded row.
218 */ 245 */
219 var createExpandedRow = function(state, baseRow) { 246 var createExpandedRow = function(guid, baseRow) {
220 var expandedRow = document.createElement('tr'); 247 var expandedRow = document.createElement('tr');
221 expandedRow.className = 'state-table-row'; 248 expandedRow.className = 'state-table-row';
222 var emptyCell = document.createElement('td'); 249 var emptyCell = document.createElement('td');
223 emptyCell.style.border = 'none'; 250 emptyCell.style.border = 'none';
224 expandedRow.appendChild(emptyCell); 251 expandedRow.appendChild(emptyCell);
225 var detailCell = document.createElement('td'); 252 var detailCell = document.createElement('td');
226 detailCell.className = 'state-table-expanded-cell'; 253 detailCell.className = 'state-table-expanded-cell';
227 detailCell.colSpan = baseRow.childNodes.length - 1; 254 detailCell.colSpan = baseRow.childNodes.length - 1;
228 detailCell.innerHTML = JSON.stringify(state, null, '\t');
229 expandedRow.appendChild(detailCell); 255 expandedRow.appendChild(detailCell);
256 networkConfig.getProperties(guid, function(state) {
257 if (networkConfig.lastError)
258 detailCell.innerHTML = networkConfig.lastError;
259 else
260 detailCell.innerHTML = JSON.stringify(state, null, '\t');
261 });
230 return expandedRow; 262 return expandedRow;
231 }; 263 };
232 264
233 /** 265 /**
234 * Sends a refresh request. 266 * Requests a network log update.
235 */ 267 */
236 var sendRefresh = function() { 268 var requestLog = function() {
237 chrome.send('requestNetworkInfo'); 269 chrome.send('NetworkUI.getNetworkLog');
238 }; 270 };
239 271
240 /** 272 /**
273 * Requests an update of all network info.
274 */
275 var requestNetworks = function() {
276 networkConfig.getNetworks(
277 { 'type': 'All', 'visible': true },
278 onVisibleNetworksReceived);
279 networkConfig.getNetworks(
280 { 'type': 'All', 'configured': true },
281 onFavoriteNetworksReceived);
282 };
283
284 /**
241 * Sets refresh rate if the interval is found in the url. 285 * Sets refresh rate if the interval is found in the url.
242 */ 286 */
243 var setRefresh = function() { 287 var setRefresh = function() {
244 var interval = parseQueryParams(window.location)['refresh']; 288 var interval = parseQueryParams(window.location)['refresh'];
245 if (interval && interval != '') 289 if (interval && interval != '')
246 setInterval(sendRefresh, parseInt(interval) * 1000); 290 setInterval(requestNetworks, parseInt(interval) * 1000);
247 }; 291 };
248 292
249 /** 293 /**
250 * Get network information from WebUI. 294 * Get network information from WebUI.
251 */ 295 */
252 document.addEventListener('DOMContentLoaded', function() { 296 document.addEventListener('DOMContentLoaded', function() {
253 $('log-refresh').onclick = sendRefresh; 297 $('log-refresh').onclick = requestLog;
254 $('log-error').checked = true; 298 $('log-error').checked = true;
255 $('log-error').onclick = sendRefresh; 299 $('log-error').onclick = requestLog;
256 $('log-user').checked = true; 300 $('log-user').checked = true;
257 $('log-user').onclick = sendRefresh; 301 $('log-user').onclick = requestLog;
258 $('log-event').checked = true; 302 $('log-event').checked = true;
259 $('log-event').onclick = sendRefresh; 303 $('log-event').onclick = requestLog;
260 $('log-debug').checked = false; 304 $('log-debug').checked = false;
261 $('log-debug').onclick = sendRefresh; 305 $('log-debug').onclick = requestLog;
262 $('log-fileinfo').checked = false; 306 $('log-fileinfo').checked = false;
263 $('log-fileinfo').onclick = sendRefresh; 307 $('log-fileinfo').onclick = requestLog;
264 $('log-timedetail').checked = false; 308 $('log-timedetail').checked = false;
265 $('log-timedetail').onclick = sendRefresh; 309 $('log-timedetail').onclick = requestLog;
266 setRefresh(); 310 setRefresh();
267 sendRefresh(); 311 requestLog();
312 requestNetworks();
268 }); 313 });
269 314
270 return { 315 return {
271 onNetworkInfoReceived: onNetworkInfoReceived 316 getNetworkLogCallback: getNetworkLogCallback
272 }; 317 };
273 }(); 318 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698