| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 var NetworkUI = function() { | |
| 6 // 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 | |
| 8 // specified then the first non empty value is used. | |
| 9 var NETWORK_STATE_FIELDS = [ | |
| 10 'Name', 'Type', 'State', 'Profile', 'Connectable', | |
| 11 'Error', 'Security', | |
| 12 ['Cellular.NetworkTechnology', 'EAP.EAP'], | |
| 13 'Cellular.ActivationState', 'Cellular.RoamingState', | |
| 14 'Cellular.OutOfCredits', 'Strength' | |
| 15 ]; | |
| 16 | |
| 17 var FAVORITE_STATE_FIELDS = [ | |
| 18 'Name', 'Type', 'Profile', 'onc_source' | |
| 19 ]; | |
| 20 | |
| 21 var LOG_LEVEL_CLASSNAME = { | |
| 22 'Error': 'network-log-level-error', | |
| 23 'User': 'network-log-level-user', | |
| 24 'Event': 'network-log-level-event', | |
| 25 'Debug': 'network-log-level-debug' | |
| 26 }; | |
| 27 | |
| 28 var LOG_LEVEL_CHECKBOX = { | |
| 29 'Error': 'log-error', | |
| 30 'User': 'log-user', | |
| 31 'Event': 'log-event', | |
| 32 'Debug': 'log-debug' | |
| 33 }; | |
| 34 | |
| 35 /** | |
| 36 * Create a tag of log level. | |
| 37 * | |
| 38 * @param {string} level A string that represents log level. | |
| 39 * @return {DOMElement} The created span element. | |
| 40 */ | |
| 41 var createLevelTag = function(level) { | |
| 42 var tag = document.createElement('span'); | |
| 43 tag.className = 'network-level-tag'; | |
| 44 tag.textContent = level; | |
| 45 tag.classList.add(LOG_LEVEL_CLASSNAME[level]); | |
| 46 return tag; | |
| 47 }; | |
| 48 | |
| 49 /** | |
| 50 * Creates an element that contains the time, the event, the level and | |
| 51 * the description of the given log entry. | |
| 52 * | |
| 53 * @param {Object} logEntry An object that represents a single line of log. | |
| 54 * @return {DOMElement} The created p element that represents the log entry. | |
| 55 */ | |
| 56 var createLogEntryText = function(logEntry) { | |
| 57 var level = logEntry['level']; | |
| 58 if (!$(LOG_LEVEL_CHECKBOX[level]).checked) | |
| 59 return null; | |
| 60 var res = document.createElement('p'); | |
| 61 var textWrapper = document.createElement('span'); | |
| 62 var fileinfo = ''; | |
| 63 if ($('log-fileinfo').checked) | |
| 64 fileinfo = logEntry['file']; | |
| 65 var timestamp = ''; | |
| 66 if ($('log-timedetail').checked) | |
| 67 timestamp = logEntry['timestamp']; | |
| 68 else | |
| 69 timestamp = logEntry['timestampshort']; | |
| 70 textWrapper.textContent = loadTimeData.getStringF( | |
| 71 'logEntryFormat', | |
| 72 timestamp, | |
| 73 fileinfo, | |
| 74 logEntry['event'], | |
| 75 logEntry['description']); | |
| 76 res.appendChild(createLevelTag(level)); | |
| 77 res.appendChild(textWrapper); | |
| 78 return res; | |
| 79 }; | |
| 80 | |
| 81 /** | |
| 82 * Create event log entries. | |
| 83 * | |
| 84 * @param {Array.<string>} logEntries A array of strings that each string | |
| 85 * represents a log event in JSON format. | |
| 86 */ | |
| 87 var createEventLog = function(logEntries) { | |
| 88 var container = $('network-log-container'); | |
| 89 container.textContent = ''; | |
| 90 for (var i = 0; i < logEntries.length; ++i) { | |
| 91 var entry = createLogEntryText(JSON.parse(logEntries[i])); | |
| 92 if (entry) | |
| 93 container.appendChild(entry); | |
| 94 } | |
| 95 }; | |
| 96 | |
| 97 /** | |
| 98 * Create a cell with a button for expanding a network state table row. | |
| 99 * | |
| 100 * @param {dictionary} state Property values for the network or favorite. | |
| 101 * @return {DOMElement} The created td element that displays the given value. | |
| 102 */ | |
| 103 var createStateTableExpandButton = function(state) { | |
| 104 var cell = document.createElement('td'); | |
| 105 cell.className = 'state-table-expand-button-cell'; | |
| 106 var button = document.createElement('button'); | |
| 107 button.addEventListener('click', function(event) { | |
| 108 toggleExpandRow(event.target, state); | |
| 109 }); | |
| 110 button.className = 'state-table-expand-button'; | |
| 111 cell.appendChild(button); | |
| 112 return cell; | |
| 113 }; | |
| 114 | |
| 115 /** | |
| 116 * Create a cell in network state table. | |
| 117 * | |
| 118 * @param {string} value Content in the cell. | |
| 119 * @return {DOMElement} The created td element that displays the given value. | |
| 120 */ | |
| 121 var createStateTableCell = function(value) { | |
| 122 var cell = document.createElement('td'); | |
| 123 cell.textContent = value || ''; | |
| 124 return cell; | |
| 125 }; | |
| 126 | |
| 127 /** | |
| 128 * Create a row in the network state table. | |
| 129 * | |
| 130 * @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. | |
| 133 * @return {DOMElement} The created tr element that contains the network | |
| 134 * state information. | |
| 135 */ | |
| 136 var createStateTableRow = function(stateFields, path, state) { | |
| 137 var row = document.createElement('tr'); | |
| 138 row.className = 'state-table-row'; | |
| 139 row.appendChild(createStateTableExpandButton(state)); | |
| 140 row.appendChild(createStateTableCell(path)); | |
| 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) { | |
| 146 var field = stateFields[i]; | |
| 147 var value = ''; | |
| 148 if (typeof field == 'string') { | |
| 149 value = state[field]; | |
| 150 } else { | |
| 151 for (var j = 0; j < field.length; ++j) { | |
| 152 value = state[field[j]]; | |
| 153 if (value) | |
| 154 break; | |
| 155 } | |
| 156 } | |
| 157 row.appendChild(createStateTableCell(value)); | |
| 158 } | |
| 159 return row; | |
| 160 }; | |
| 161 | |
| 162 /** | |
| 163 * Create table for networks or favorites. | |
| 164 * | |
| 165 * @param {string} tablename The name of the table to be created. | |
| 166 * @param {Array.<Object>} stateFields The list of fields for the table. | |
| 167 * @param {Array.<Object>} states An array of network or favorite states. | |
| 168 */ | |
| 169 var createStateTable = function(tablename, stateFields, states) { | |
| 170 var table = $(tablename); | |
| 171 var oldRows = table.querySelectorAll('.state-table-row'); | |
| 172 for (var i = 0; i < oldRows.length; ++i) | |
| 173 table.removeChild(oldRows[i]); | |
| 174 for (var path in states) | |
| 175 table.appendChild(createStateTableRow(stateFields, path, states[path])); | |
| 176 }; | |
| 177 | |
| 178 /** | |
| 179 * This callback function is triggered when the data is received. | |
| 180 * | |
| 181 * @param {dictionary} data A dictionary that contains network state | |
| 182 * information. | |
| 183 */ | |
| 184 var onNetworkInfoReceived = function(data) { | |
| 185 createEventLog(JSON.parse(data.networkEventLog)); | |
| 186 createStateTable( | |
| 187 'network-state-table', NETWORK_STATE_FIELDS, data.networkStates); | |
| 188 createStateTable( | |
| 189 'favorite-state-table', FAVORITE_STATE_FIELDS, data.favoriteStates); | |
| 190 }; | |
| 191 | |
| 192 /** | |
| 193 * Toggle the button state and add or remove a row displaying the complete | |
| 194 * state information for a row. | |
| 195 * | |
| 196 * @param {DOMElement} btn The button that was clicked. | |
| 197 * @param {dictionary} state Property values for the network or favorite. | |
| 198 */ | |
| 199 var toggleExpandRow = function(btn, state) { | |
| 200 var cell = btn.parentNode; | |
| 201 var row = cell.parentNode; | |
| 202 if (btn.classList.contains('state-table-expand-button-expanded')) { | |
| 203 btn.classList.remove('state-table-expand-button-expanded'); | |
| 204 row.parentNode.removeChild(row.nextSibling); | |
| 205 } else { | |
| 206 btn.classList.add('state-table-expand-button-expanded'); | |
| 207 var expandedRow = createExpandedRow(state, row); | |
| 208 row.parentNode.insertBefore(expandedRow, row.nextSibling); | |
| 209 } | |
| 210 }; | |
| 211 | |
| 212 /** | |
| 213 * Creates the expanded row for displaying the complete state as JSON. | |
| 214 * | |
| 215 * @param {dictionary} state Property values for the network or favorite. | |
| 216 * @param {DOMElement} baseRow The unexpanded row associated with the new row. | |
| 217 * @return {DOMElement} The created tr element for the expanded row. | |
| 218 */ | |
| 219 var createExpandedRow = function(state, baseRow) { | |
| 220 var expandedRow = document.createElement('tr'); | |
| 221 expandedRow.className = 'state-table-row'; | |
| 222 var emptyCell = document.createElement('td'); | |
| 223 emptyCell.style.border = 'none'; | |
| 224 expandedRow.appendChild(emptyCell); | |
| 225 var detailCell = document.createElement('td'); | |
| 226 detailCell.className = 'state-table-expanded-cell'; | |
| 227 detailCell.colSpan = baseRow.childNodes.length - 1; | |
| 228 detailCell.innerHTML = JSON.stringify(state, null, '\t'); | |
| 229 expandedRow.appendChild(detailCell); | |
| 230 return expandedRow; | |
| 231 }; | |
| 232 | |
| 233 /** | |
| 234 * Sends a refresh request. | |
| 235 */ | |
| 236 var sendRefresh = function() { | |
| 237 chrome.send('requestNetworkInfo'); | |
| 238 }; | |
| 239 | |
| 240 /** | |
| 241 * Sets refresh rate if the interval is found in the url. | |
| 242 */ | |
| 243 var setRefresh = function() { | |
| 244 var interval = parseQueryParams(window.location)['refresh']; | |
| 245 if (interval && interval != '') | |
| 246 setInterval(sendRefresh, parseInt(interval) * 1000); | |
| 247 }; | |
| 248 | |
| 249 /** | |
| 250 * Get network information from WebUI. | |
| 251 */ | |
| 252 document.addEventListener('DOMContentLoaded', function() { | |
| 253 $('log-refresh').onclick = sendRefresh; | |
| 254 $('log-error').checked = true; | |
| 255 $('log-error').onclick = sendRefresh; | |
| 256 $('log-user').checked = true; | |
| 257 $('log-user').onclick = sendRefresh; | |
| 258 $('log-event').checked = true; | |
| 259 $('log-event').onclick = sendRefresh; | |
| 260 $('log-debug').checked = false; | |
| 261 $('log-debug').onclick = sendRefresh; | |
| 262 $('log-fileinfo').checked = false; | |
| 263 $('log-fileinfo').onclick = sendRefresh; | |
| 264 $('log-timedetail').checked = false; | |
| 265 $('log-timedetail').onclick = sendRefresh; | |
| 266 setRefresh(); | |
| 267 sendRefresh(); | |
| 268 }); | |
| 269 | |
| 270 return { | |
| 271 onNetworkInfoReceived: onNetworkInfoReceived | |
| 272 }; | |
| 273 }(); | |
| OLD | NEW |