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

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

Powered by Google App Engine
This is Rietveld 408576698