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 cr.define('diag', function() { | |
| 6 /** | |
| 7 * Encapsulated handling of the diagnostics page. | |
| 8 */ | |
| 9 function DiagPage() {} | |
| 10 | |
| 11 cr.addSingletonGetter(DiagPage); | |
| 12 | |
| 13 /** | |
| 14 * List of network adapter types. | |
| 15 */ | |
| 16 DiagPage.AdapterType = [ | |
| 17 {adapter: 'wlan0', name: 'Wi-Fi'}, | |
| 18 {adapter: 'eth0', name: 'Ethernet 1'}, | |
| 19 {adapter: 'eth1', name: 'Ethernet 2'}, | |
| 20 {adapter: 'wwan0', name: '3G'}, | |
|
xiyuan
2012/08/14 17:00:01
Do we need to worry about i18n for the names?
hshi1
2012/08/14 20:09:28
Yes I think all strings should be localized.
Can
xiyuan
2012/08/14 23:00:39
Include local_strings.js and you can get i18n stri
hshi1
2012/08/15 00:44:32
Done.
| |
| 21 ]; | |
| 22 | |
| 23 /** | |
| 24 * List of network adapter status. | |
| 25 * The numeric value assigned to each status reflects how healthy the network | |
| 26 * adapter is. | |
| 27 * | |
| 28 * @enum {int} | |
| 29 */ | |
| 30 DiagPage.AdapterStatus = { | |
| 31 NOT_FOUND: 0, | |
| 32 DISABLED: 1, | |
| 33 NO_IP: 2, | |
| 34 VALID_IP: 3 | |
| 35 }; | |
| 36 | |
| 37 DiagPage.prototype = { | |
| 38 /** | |
| 39 * Perform initial setup. | |
| 40 */ | |
| 41 initialize: function() { | |
| 42 // Initialize member variables. | |
| 43 this.activeAdapter_ = -1; | |
| 44 this.adapterStatus_ = new Array(); | |
| 45 | |
| 46 // Attempt to update. | |
| 47 chrome.send('pageLoaded'); | |
| 48 }, | |
| 49 | |
| 50 /** | |
| 51 * Updates the connectivity status with netif information. | |
| 52 * @param {String} netifStatus Dictionary of network adapter status. | |
| 53 */ | |
| 54 setNetifStatus_: function(netifStatus) { | |
| 55 // Hide the "loading" message. | |
| 56 $('loading').setAttribute('style', 'display:none'); | |
|
xiyuan
2012/08/14 17:00:01
$('loading').hidden = true;
hshi1
2012/08/14 20:09:28
Done.
| |
| 57 | |
| 58 // Update netif state. | |
| 59 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 60 var adapterType = DiagPage.AdapterType[i]; | |
| 61 var status = netifStatus[adapterType.adapter]; | |
| 62 if (!status) | |
| 63 this.adapterStatus_[i] = DiagPage.AdapterStatus.NOT_FOUND; | |
| 64 else if (!status.flags || status.flags.indexOf('up') == -1) | |
| 65 this.adapterStatus_[i] = DiagPage.AdapterStatus.DISABLED; | |
| 66 else if (!status.ipv4) | |
| 67 this.adapterStatus_[i] = DiagPage.AdapterStatus.NO_IP; | |
| 68 else | |
| 69 this.adapterStatus_[i] = DiagPage.AdapterStatus.VALID_IP; | |
| 70 } | |
| 71 | |
| 72 // Update UI | |
| 73 this.updateAdapterSelection_(); | |
| 74 this.updateConnectivityStatus_(); | |
| 75 }, | |
| 76 | |
| 77 /** | |
| 78 * Gets the HTML radio input element id for a network adapter. | |
| 79 * @private | |
| 80 */ | |
| 81 getAdapterElementId_: function(adapter) { | |
| 82 return 'adapter-' + DiagPage.AdapterType[adapter].adapter; | |
| 83 }, | |
| 84 | |
| 85 /** | |
| 86 * Gets the most active adapter based on their status. | |
| 87 * @private | |
| 88 */ | |
| 89 getActiveAdapter_: function() { | |
| 90 var activeAdapter = -1; | |
| 91 var activeAdapterStatus = DiagPage.AdapterStatus.NOT_FOUND; | |
| 92 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 93 var status = this.adapterStatus_[i]; | |
| 94 if (status == DiagPage.AdapterStatus.NOT_FOUND) | |
| 95 continue; | |
| 96 if (activeAdapter == -1 || status > activeAdapterStatus) { | |
| 97 activeAdapter = i; | |
| 98 activeAdapterStatus = status; | |
| 99 } | |
| 100 } | |
| 101 return activeAdapter; | |
| 102 }, | |
| 103 | |
| 104 /** | |
| 105 * Update the adapter selection section. | |
| 106 * @private | |
| 107 */ | |
| 108 updateAdapterSelection_: function() { | |
| 109 // Create HTML radio input elements. | |
| 110 var adapterSelectionHTML = ''; | |
| 111 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 112 if (this.adapterStatus_[i] != DiagPage.AdapterStatus.NOT_FOUND) { | |
| 113 adapterSelectionHTML += | |
| 114 '<input type="radio" name="adapter" id="' + | |
| 115 this.getAdapterElementId_(i) + '">' + DiagPage.AdapterType[i].name + | |
| 116 '</input><br />'; | |
| 117 } | |
| 118 } | |
| 119 // Set click handlers for adapter selection. | |
| 120 $('adapter-selection').innerHTML = adapterSelectionHTML; | |
|
xiyuan
2012/08/14 17:00:01
Is it possible to use our js list instead of doing
hshi1
2012/08/14 20:09:28
Can you please elaborate what does it mean to use
xiyuan
2012/08/14 23:00:39
Search for "cr.ui.List" in js files and you could
hshi1
2012/08/15 00:44:32
I'll explicitly create the DOM elements and append
| |
| 121 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
| 122 var radioElement = $(this.getAdapterElementId_(i)); | |
| 123 if (radioElement) { | |
| 124 radioElement.onclick = function(adapter) { | |
| 125 this.activeAdapter_ = adapter; | |
| 126 this.updateConnectivityStatus_(); | |
| 127 }.bind(this, i); | |
| 128 } | |
| 129 } | |
| 130 if (this.activeAdapter_ == -1) { | |
| 131 this.activeAdapter_ = this.getActiveAdapter_(); | |
| 132 $(this.getAdapterElementId_(this.activeAdapter_)).setAttribute( | |
| 133 'checked', 'true'); | |
| 134 } | |
| 135 }, | |
| 136 | |
| 137 /** | |
| 138 * Update the connectivity status for the specified network interface. | |
| 139 * @private | |
| 140 */ | |
| 141 updateConnectivityStatus_: function() { | |
| 142 var adapter = this.activeAdapter_; | |
| 143 var status = this.adapterStatus_[adapter]; | |
| 144 var name = DiagPage.AdapterType[adapter].name; | |
| 145 var statusHTML = '<div class="test-performed">' + | |
| 146 '<b>1. </b>Testing ' + name + ' hardware...'; | |
| 147 if (status == DiagPage.AdapterStatus.DISABLED) { | |
| 148 statusHTML += '</div><div class="test-error"><img src="fail.png" />' + | |
| 149 'The ' + name + ' adapter was not enabled.</div>' + | |
| 150 '<div class="recommendation"><b>Recommendation: </b>' + | |
| 151 'Enable the ' + name + ' adapter.</div>' + | |
| 152 '<div class="test-pending">' + | |
| 153 '<b>2. </b>Testing connection to router<br />' + | |
| 154 '<b>3. </b>Testing connection to internet</div>'; | |
| 155 } else { | |
| 156 statusHTML += '<img src="tick.png" /><br />' + | |
| 157 '<b>2. </b>Testing connection to router...'; | |
| 158 if (status == DiagPage.AdapterStatus.NO_IP) { | |
| 159 statusHTML += | |
| 160 '</div><div class="test-error"><img src="fail.png" />' + | |
| 161 'The ' + name + ' adapter does not have an IP address and cannot ' + | |
| 162 'connect to the internet.</div>' + | |
| 163 '<div class="recommendation"><b>Recommendation: </b>' + | |
| 164 '<i>Please ensure that</i><br />' + | |
| 165 '1) you are trying to connect to the right network<br />' + | |
| 166 '2) you are using the right authentication method<br />' + | |
| 167 '3) your router is configured correctly<br />' + | |
| 168 '<i>Try to</i><br />' + | |
| 169 '1) connect to a different network<br />' + | |
| 170 '2) reboot your router<br /></div>' + | |
| 171 '<div class="test-pending"><b>3. </b>Testing connection to ' + | |
| 172 'internet</div>'; | |
| 173 } else { | |
| 174 statusHTML += | |
| 175 '<img src="tick.png" /><br />' + | |
| 176 '<b>3. </b>Testing connection to internet...' + | |
| 177 '<img src="tick.png" /></div>'; | |
| 178 } | |
| 179 } | |
| 180 $('connectivity-status').innerHTML = statusHTML; | |
|
xiyuan
2012/08/14 17:00:01
i18n?
And in general, we should avoid manipulatin
| |
| 181 } | |
| 182 }; | |
| 183 | |
| 184 DiagPage.setNetifStatus = function(netifStatus) { | |
| 185 DiagPage.getInstance().setNetifStatus_(netifStatus); | |
| 186 } | |
| 187 | |
| 188 // Export | |
| 189 return { | |
| 190 DiagPage: DiagPage | |
| 191 }; | |
| 192 }); | |
| 193 | |
| 5 /** | 194 /** |
| 6 * Updates the Connectivity Status section. | 195 * Initialize the DiagPage upon DOM content loaded. |
| 7 * @param {String} connStatus Dictionary containing connectivity status. | |
| 8 */ | 196 */ |
| 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() { | 197 document.addEventListener('DOMContentLoaded', function() { |
| 38 chrome.send('pageLoaded'); | 198 diag.DiagPage.getInstance().initialize(); |
| 39 }); | 199 }); |
| OLD | NEW |