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() {} | |
|
xiyuan
2012/08/15 23:41:28
nit: Move this down to be with cr.addSingletonGett
hshi1
2012/08/16 00:29:17
Done.
| |
| 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)); | |
|
xiyuan
2012/08/15 23:41:28
nit: Maybe use a <label for="radio_element_id">xxx
hshi1
2012/08/16 00:29:17
Done.
| |
| 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 // Status messages for individual tests. | |
| 167 var connectivityStatusElement = $('connectivity-status'); | |
| 168 var testStatusElements = new Array(); | |
| 169 removeChildren(connectivityStatusElement); | |
| 170 for (var i = 0; i < 3; i++) { | |
| 171 testStatusElements[i] = document.createElement('div'); | |
| 172 connectivityStatusElement.appendChild(testStatusElements[i]); | |
| 173 } | |
| 174 testStatusElements[0].innerHTML = | |
| 175 localStrings.getStringF('testing-hardware', 1, name); | |
| 176 testStatusElements[1].innerHTML = | |
| 177 localStrings.getStringF('testing-connection-to-router', 2); | |
| 178 testStatusElements[2].innerHTML = | |
| 179 localStrings.getStringF('testing-connection-to-internet', 3); | |
|
xiyuan
2012/08/15 23:41:28
nit: It seems to be that your cuold make step numb
hshi1
2012/08/16 00:29:17
Done.
| |
| 180 | |
| 181 // Error and recommendation messages may be inserted in test status | |
| 182 // elements. | |
| 183 var errorElement = document.createElement('div'); | |
| 184 var recommendationElement = document.createElement('div'); | |
| 185 var failIconElement = document.createElement('img'); | |
| 186 var tickIconElement = document.createElement('img'); | |
|
xiyuan
2012/08/15 23:41:28
nit: Since we are cloneNode of the two image eleme
hshi1
2012/08/16 00:29:17
Done.
| |
| 187 errorElement.setAttribute('class', 'test-error'); | |
|
xiyuan
2012/08/15 23:41:28
nit: errorElement.className = 'test-error';
or err
hshi1
2012/08/16 00:29:17
Done.
| |
| 188 recommendationElement.setAttribute('class', 'recommendation'); | |
| 189 failIconElement.setAttribute('src', 'fail.png'); | |
| 190 tickIconElement.setAttribute('src', 'tick.png'); | |
| 191 | |
| 192 testStatusElements[0].setAttribute('class', 'test-performed'); | |
| 193 if (status == DiagPage.AdapterStatus.DISABLED) { | |
| 194 errorElement.appendChild(failIconElement.cloneNode()); | |
| 195 errorElement.appendChild(document.createTextNode( | |
| 196 localStrings.getStringF('adapter-disabled', name))); | |
| 197 recommendationElement.innerHTML = | |
| 198 localStrings.getStringF('enable-adapter', name); | |
| 199 connectivityStatusElement.insertBefore(errorElement, | |
| 200 testStatusElements[1]); | |
| 201 connectivityStatusElement.insertBefore(recommendationElement, | |
| 202 testStatusElements[1]); | |
| 203 testStatusElements[1].setAttribute('class', 'test-pending'); | |
| 204 testStatusElements[2].setAttribute('class', 'test-pending'); | |
| 205 } else { | |
| 206 testStatusElements[0].appendChild(tickIconElement.cloneNode()); | |
| 207 testStatusElements[1].setAttribute('class', 'test-performed'); | |
| 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 recommendationElement.innerHTML = | |
| 213 localStrings.getStringF('fix-connection-to-router'); | |
| 214 connectivityStatusElement.insertBefore(errorElement, | |
| 215 testStatusElements[2]); | |
| 216 connectivityStatusElement.insertBefore(recommendationElement, | |
| 217 testStatusElements[2]); | |
| 218 testStatusElements[2].setAttribute('class', 'test-pending'); | |
| 219 } else { | |
| 220 testStatusElements[1].appendChild(tickIconElement.cloneNode()); | |
| 221 testStatusElements[2].setAttribute('class', 'test-performed'); | |
| 222 testStatusElements[2].appendChild(tickIconElement.cloneNode()); | |
| 223 } | |
| 224 } | |
| 225 } | |
| 226 }; | |
| 227 | |
| 228 DiagPage.setNetifStatus = function(netifStatus) { | |
| 229 DiagPage.getInstance().setNetifStatus_(netifStatus); | |
| 230 } | |
| 231 | |
| 232 // Export | |
| 233 return { | |
| 234 DiagPage: DiagPage | |
| 235 }; | |
| 236 }); | |
| 237 | |
| 5 /** | 238 /** |
| 6 * Updates the Connectivity Status section. | 239 * Initialize the DiagPage upon DOM content loaded. |
| 7 * @param {String} connStatus Dictionary containing connectivity status. | |
| 8 */ | 240 */ |
| 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() { | 241 document.addEventListener('DOMContentLoaded', function() { |
| 38 chrome.send('pageLoaded'); | 242 diag.DiagPage.getInstance().initialize(); |
| 39 }); | 243 }); |
| OLD | NEW |