Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 localStrings = new LocalStrings(); | |
| 6 | |
| 7 cr.define('diag', function() { | |
| 8 /** | |
| 9 * Encapsulated handling of the diagnostics page. | |
| 10 */ | |
| 11 function DiagPage() {} | |
| 12 | |
| 13 /* | |
| 14 * Remove all children nodes for an element. | |
| 15 * @param {element} parent of the elements to be removed. | |
| 16 */ | |
| 17 function removeChildren(element) { | |
| 18 element.textContent = ''; | |
| 19 } | |
| 20 | |
| 21 /** | |
| 22 * Creates an element named |elementName| containing the content |text|. | |
| 23 * @param {string} elementName Name of the new element to be created. | |
| 24 * @param {string} text Text to be contained in the new element. | |
| 25 * @return {HTMLElement} The newly created HTML element. | |
| 26 */ | |
| 27 function createElementFromText(elementName, text) { | |
| 28 var element = document.createElement(elementName); | |
| 29 element.appendChild(document.createTextNode(text)); | |
| 30 return element; | |
| 31 } | |
| 32 | |
| 33 cr.addSingletonGetter(DiagPage); | |
| 34 | |
| 35 /** | |
| 36 * List of network adapter types. | |
| 37 */ | |
| 38 DiagPage.AdapterType = [ | |
| 39 {adapter: 'wlan0', name: localStrings.getString('wlan0')}, | |
| 40 {adapter: 'eth0', name: localStrings.getString('eth0')}, | |
| 41 {adapter: 'eth1', name: localStrings.getString('eth1')}, | |
| 42 {adapter: 'wwan0', name: localStrings.getString('wwan0')}, | |
| 43 ]; | |
| 44 | |
| 45 /** | |
| 46 * List of network adapter status. | |
| 47 * The numeric value assigned to each status reflects how healthy the network | |
| 48 * adapter is. | |
| 49 * | |
| 50 * @enum {int} | |
| 51 */ | |
| 52 DiagPage.AdapterStatus = { | |
| 53 NOT_FOUND: 0, | |
| 54 DISABLED: 1, | |
| 55 NO_IP: 2, | |
| 56 VALID_IP: 3 | |
| 57 }; | |
| 58 | |
| 59 DiagPage.prototype = { | |
| 60 /** | |
| 61 * Perform initial setup. | |
| 62 */ | |
| 63 initialize: function() { | |
| 64 // Initialize member variables. | |
| 65 this.activeAdapter_ = -1; | |
| 66 this.adapterStatus_ = new Array(); | |
| 67 | |
| 68 // Attempt to update. | |
| 69 chrome.send('pageLoaded'); | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * Updates the connectivity status with netif information. | |
| 74 * @param {String} netifStatus Dictionary of network adapter status. | |
| 75 */ | |
| 76 setNetifStatus_: function(netifStatus) { | |
| 77 // Hide the "loading" message. | |
| 78 $('loading').hidden = true; | |
| 79 | |
| 80 // Update netif state. | |
| 81 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 82 var adapterType = DiagPage.AdapterType[i]; | |
| 83 var status = netifStatus[adapterType.adapter]; | |
| 84 if (!status) | |
| 85 this.adapterStatus_[i] = DiagPage.AdapterStatus.NOT_FOUND; | |
| 86 else if (!status.flags || status.flags.indexOf('up') == -1) | |
| 87 this.adapterStatus_[i] = DiagPage.AdapterStatus.DISABLED; | |
| 88 else if (!status.ipv4) | |
| 89 this.adapterStatus_[i] = DiagPage.AdapterStatus.NO_IP; | |
| 90 else | |
| 91 this.adapterStatus_[i] = DiagPage.AdapterStatus.VALID_IP; | |
| 92 } | |
| 93 | |
| 94 // Update UI | |
| 95 this.updateAdapterSelection_(); | |
| 96 this.updateConnectivityStatus_(); | |
| 97 }, | |
| 98 | |
| 99 /** | |
| 100 * Gets the HTML radio input element id for a network adapter. | |
| 101 * @private | |
| 102 */ | |
| 103 getAdapterElementId_: function(adapter) { | |
| 104 return 'adapter-' + DiagPage.AdapterType[adapter].adapter; | |
| 105 }, | |
| 106 | |
| 107 /** | |
| 108 * Gets the most active adapter based on their status. | |
| 109 * @private | |
| 110 */ | |
| 111 getActiveAdapter_: function() { | |
| 112 var activeAdapter = -1; | |
| 113 var activeAdapterStatus = DiagPage.AdapterStatus.NOT_FOUND; | |
| 114 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 115 var status = this.adapterStatus_[i]; | |
| 116 if (status == DiagPage.AdapterStatus.NOT_FOUND) | |
| 117 continue; | |
| 118 if (activeAdapter == -1 || status > activeAdapterStatus) { | |
| 119 activeAdapter = i; | |
| 120 activeAdapterStatus = status; | |
| 121 } | |
| 122 } | |
| 123 return activeAdapter; | |
| 124 }, | |
| 125 | |
| 126 /** | |
| 127 * Update the adapter selection section. | |
| 128 * @private | |
| 129 */ | |
| 130 updateAdapterSelection_: function() { | |
| 131 // Determine active adapter. | |
| 132 if (this.activeAdapter_ == -1) | |
| 133 this.activeAdapter_ = this.getActiveAdapter_(); | |
| 134 // Create HTML radio input elements. | |
| 135 var adapterSelectionElement = $('adapter-selection'); | |
| 136 removeChildren(adapterSelectionElement); | |
| 137 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 138 if (this.adapterStatus_[i] == DiagPage.AdapterStatus.NOT_FOUND) | |
| 139 continue; | |
| 140 var radioElement = document.createElement('input'); | |
| 141 radioElement.setAttribute('type', 'radio'); | |
| 142 radioElement.setAttribute('name', 'adapter'); | |
| 143 radioElement.setAttribute('id', this.getAdapterElementId_(i)); | |
| 144 if (i == this.activeAdapter_) | |
| 145 radioElement.setAttribute('checked', 'true'); | |
| 146 radioElement.onclick = function(adapter) { | |
| 147 this.activeAdapter_ = adapter; | |
| 148 this.updateConnectivityStatus_(); | |
| 149 }.bind(this, i); | |
| 150 adapterSelectionElement.appendChild(radioElement); | |
| 151 adapterSelectionElement.appendChild( | |
| 152 document.createTextNode(DiagPage.AdapterType[i].name)); | |
| 153 adapterSelectionElement.appendChild(document.createElement('br')); | |
| 154 } | |
| 155 }, | |
| 156 | |
| 157 /** | |
| 158 * Update the connectivity status for the specified network interface. | |
| 159 * @private | |
| 160 */ | |
| 161 updateConnectivityStatus_: function() { | |
| 162 var adapter = this.activeAdapter_; | |
| 163 var status = this.adapterStatus_[adapter]; | |
| 164 var name = DiagPage.AdapterType[adapter].name; | |
| 165 | |
| 166 var performedTestsElement = document.createElement('div'); | |
| 167 var pendingTestsElement = document.createElement('div'); | |
| 168 var errorElement = document.createElement('div'); | |
| 169 var recommendationElement = document.createElement('div'); | |
| 170 var failIconElement = document.createElement('img'); | |
| 171 var tickIconElement = document.createElement('img'); | |
| 172 | |
| 173 performedTestsElement.setAttribute('class', 'test-performed'); | |
| 174 pendingTestsElement.setAttribute('class', 'test-pending'); | |
| 175 errorElement.setAttribute('class', 'test-error'); | |
| 176 recommendationElement.setAttribute('class', 'recommendation'); | |
| 177 failIconElement.setAttribute('src', 'fail.png'); | |
| 178 tickIconElement.setAttribute('src', 'tick.png'); | |
| 179 | |
| 180 performedTestsElement.appendChild(createElementFromText('b', '1. ')); | |
| 181 performedTestsElement.appendChild(document.createTextNode( | |
| 182 localStrings.getStringF('testing-hardware', name))); | |
| 183 | |
| 184 if (status == DiagPage.AdapterStatus.DISABLED) { | |
| 185 errorElement.appendChild(failIconElement.cloneNode()); | |
| 186 errorElement.appendChild(document.createTextNode( | |
| 187 localStrings.getStringF('adapter-disabled', name))); | |
| 188 | |
| 189 recommendationElement.appendChild(createElementFromText( | |
| 190 'b', localStrings.getString('recommendation'))); | |
| 191 recommendationElement.appendChild(document.createTextNode( | |
| 192 localStrings.getStringF('enable-adapter', name))); | |
| 193 | |
| 194 pendingTestsElement.appendChild(createElementFromText('b', '2. ')); | |
| 195 pendingTestsElement.appendChild(document.createTextNode( | |
| 196 localStrings.getString('testing-connection-to-router'))); | |
| 197 pendingTestsElement.appendChild(document.createElement('br')); | |
| 198 pendingTestsElement.appendChild(createElementFromText('b', '3. ')); | |
| 199 pendingTestsElement.appendChild(document.createTextNode( | |
| 200 localStrings.getString('testing-connection-to-internet'))); | |
| 201 } else { | |
| 202 performedTestsElement.appendChild(tickIconElement.cloneNode()); | |
| 203 performedTestsElement.appendChild(document.createElement('br')); | |
| 204 performedTestsElement.appendChild(createElementFromText('b', '2. ')); | |
| 205 performedTestsElement.appendChild(document.createTextNode( | |
| 206 localStrings.getString('testing-connection-to-router'))); | |
| 207 | |
| 208 if (status == DiagPage.AdapterStatus.NO_IP) { | |
| 209 errorElement.appendChild(failIconElement.cloneNode()); | |
| 210 errorElement.appendChild(document.createTextNode( | |
| 211 localStrings.getStringF('adapter-no-ip', name))); | |
| 212 | |
| 213 recommendationElement.appendChild(createElementFromText( | |
| 214 'b', localStrings.getString('recommendation'))); | |
| 215 // TODO: i18n string | |
| 216 recommendationElement.innerHTML += | |
| 217 '<i>Please ensure that</i><br />' + | |
| 218 '1) you are trying to connect to the right network<br />' + | |
| 219 '2) you are using the right authentication method<br />' + | |
| 220 '3) your router is configured correctly<br />' + | |
| 221 '<i>Try to</i><br />' + | |
| 222 '1) connect to a different network<br />' + | |
| 223 '2) reboot your router<br /></div>'; | |
|
hshi1
2012/08/15 00:44:33
How would you suggest to handle i18n strings like
xiyuan
2012/08/15 16:36:07
Sorry that I was not clear enough. What I suggeste
hshi1
2012/08/15 22:59:25
Done.
| |
| 224 | |
| 225 pendingTestsElement.appendChild(createElementFromText('b', '3. ')); | |
| 226 pendingTestsElement.appendChild(document.createTextNode( | |
| 227 localStrings.getString('testing-connection-to-internet'))); | |
| 228 } else { | |
| 229 performedTestsElement.appendChild(tickIconElement.cloneNode()); | |
| 230 performedTestsElement.appendChild(document.createElement('br')); | |
| 231 performedTestsElement.appendChild(createElementFromText('b', '3. ')); | |
| 232 performedTestsElement.appendChild(document.createTextNode( | |
| 233 localStrings.getString('testing-connection-to-internet'))); | |
| 234 performedTestsElement.appendChild(tickIconElement.cloneNode()); | |
| 235 | |
| 236 errorElement.hidden = true; | |
| 237 recommendationElement.hidden = true; | |
| 238 pendingTestsElement.hidden = true; | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 var connectivityStatusElement = $('connectivity-status'); | |
| 243 removeChildren(connectivityStatusElement); | |
| 244 connectivityStatusElement.appendChild(performedTestsElement); | |
| 245 connectivityStatusElement.appendChild(errorElement); | |
| 246 connectivityStatusElement.appendChild(recommendationElement); | |
| 247 connectivityStatusElement.appendChild(pendingTestsElement); | |
| 248 } | |
| 249 }; | |
| 250 | |
| 251 DiagPage.setNetifStatus = function(netifStatus) { | |
| 252 DiagPage.getInstance().setNetifStatus_(netifStatus); | |
| 253 } | |
| 254 | |
| 255 // Export | |
| 256 return { | |
| 257 DiagPage: DiagPage | |
| 258 }; | |
| 259 }); | |
| 260 | |
| 5 /** | 261 /** |
| 6 * Updates the Connectivity Status section. | 262 * Initialize the DiagPage upon DOM content loaded. |
| 7 * @param {String} connStatus Dictionary containing connectivity status. | |
| 8 */ | 263 */ |
| 9 function updateConnectivityStatus(connStatus) { | |
| 10 var deviceTypes = ['wlan0', 'wwan0', 'eth0', 'eth1']; | |
| 11 var deviceNames = ['Wi-Fi', '3G', 'Ethernet0', 'Ethernet1']; | |
| 12 for (var i = 0; i < deviceTypes.length; i++) { | |
| 13 var deviceName = deviceNames[i]; | |
| 14 var nameElement = document.createElement('h2'); | |
| 15 nameElement.appendChild(document.createTextNode(deviceName)); | |
| 16 $('connectivity-status').appendChild(nameElement); | |
| 17 | |
| 18 var deviceType = deviceTypes[i]; | |
| 19 var deviceStatus = connStatus[deviceType]; | |
| 20 var statusMessage; | |
| 21 if (!deviceStatus) { | |
| 22 statusMessage = 'Device not found.'; | |
| 23 } else if (!deviceStatus.flags || | |
| 24 deviceStatus.flags.indexOf('up') == -1) { | |
| 25 statusMessage = 'Device disabled.'; | |
| 26 } else if (!deviceStatus.ipv4) { | |
| 27 statusMessage = 'IPv4 address unavailable.'; | |
| 28 } else { | |
| 29 statusMessage = 'IPv4 address: ' + deviceStatus.ipv4.addrs; | |
| 30 } | |
| 31 var statusElement = document.createElement('p'); | |
| 32 statusElement.appendChild(document.createTextNode(statusMessage)); | |
| 33 $('connectivity-status').appendChild(statusElement); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 document.addEventListener('DOMContentLoaded', function() { | 264 document.addEventListener('DOMContentLoaded', function() { |
| 38 chrome.send('pageLoaded'); | 265 diag.DiagPage.getInstance().initialize(); |
| 39 }); | 266 }); |
| OLD | NEW |