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 cr.addSingletonGetter(DiagPage); | |
14 | |
15 /* | |
16 * Remove all children nodes for an element. | |
17 * @param {element} parent of the elements to be removed. | |
18 */ | |
19 function removeChildren(element) { | |
20 element.textContent = ''; | |
21 } | |
22 | |
23 /** | |
24 * Creates an element named |elementName| containing the content |text|. | |
25 * @param {string} elementName Name of the new element to be created. | |
26 * @param {string} text Text to be contained in the new element. | |
27 * @return {HTMLElement} The newly created HTML element. | |
28 */ | |
29 function createElementFromText(elementName, text) { | |
xiyuan
2012/08/16 17:13:31
nit: This function seems no longer used.
hshi1
2012/08/16 18:40:41
Done.
| |
30 var element = document.createElement(elementName); | |
31 element.appendChild(document.createTextNode(text)); | |
32 return element; | |
33 } | |
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 // Initialize image elements. | |
69 this.failIconElement_ = document.createElement('img'); | |
70 this.tickIconElement_ = document.createElement('img'); | |
71 this.failIconElement_.setAttribute('src', 'fail.png'); | |
72 this.tickIconElement_.setAttribute('src', 'tick.png'); | |
xiyuan
2012/08/16 17:13:31
nit: Prefer to move this even further out of this
hshi1
2012/08/16 18:40:41
Done.
| |
73 | |
74 // Attempt to update. | |
75 chrome.send('pageLoaded'); | |
76 }, | |
77 | |
78 /** | |
79 * Updates the connectivity status with netif information. | |
80 * @param {String} netifStatus Dictionary of network adapter status. | |
81 */ | |
82 setNetifStatus_: function(netifStatus) { | |
83 // Hide the "loading" message. | |
84 $('loading').hidden = true; | |
85 | |
86 // Update netif state. | |
87 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
88 var adapterType = DiagPage.AdapterType[i]; | |
89 var status = netifStatus[adapterType.adapter]; | |
90 if (!status) | |
91 this.adapterStatus_[i] = DiagPage.AdapterStatus.NOT_FOUND; | |
92 else if (!status.flags || status.flags.indexOf('up') == -1) | |
93 this.adapterStatus_[i] = DiagPage.AdapterStatus.DISABLED; | |
94 else if (!status.ipv4) | |
95 this.adapterStatus_[i] = DiagPage.AdapterStatus.NO_IP; | |
96 else | |
97 this.adapterStatus_[i] = DiagPage.AdapterStatus.VALID_IP; | |
98 } | |
99 | |
100 // Update UI | |
101 this.updateAdapterSelection_(); | |
102 this.updateConnectivityStatus_(); | |
103 }, | |
104 | |
105 /** | |
106 * Gets the HTML radio input element id for a network adapter. | |
107 * @private | |
108 */ | |
109 getAdapterElementId_: function(adapter) { | |
110 return 'adapter-' + DiagPage.AdapterType[adapter].adapter; | |
111 }, | |
112 | |
113 /** | |
114 * Gets the most active adapter based on their status. | |
115 * @private | |
116 */ | |
117 getActiveAdapter_: function() { | |
118 var activeAdapter = -1; | |
119 var activeAdapterStatus = DiagPage.AdapterStatus.NOT_FOUND; | |
120 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
121 var status = this.adapterStatus_[i]; | |
122 if (status == DiagPage.AdapterStatus.NOT_FOUND) | |
123 continue; | |
124 if (activeAdapter == -1 || status > activeAdapterStatus) { | |
125 activeAdapter = i; | |
126 activeAdapterStatus = status; | |
127 } | |
128 } | |
129 return activeAdapter; | |
130 }, | |
131 | |
132 /** | |
133 * Update the adapter selection section. | |
134 * @private | |
135 */ | |
136 updateAdapterSelection_: function() { | |
137 // Determine active adapter. | |
138 if (this.activeAdapter_ == -1) | |
139 this.activeAdapter_ = this.getActiveAdapter_(); | |
140 // Create HTML radio input elements. | |
141 var adapterSelectionElement = $('adapter-selection'); | |
142 removeChildren(adapterSelectionElement); | |
143 for (var i = 0; i < DiagPage.AdapterType.length; i++) { | |
144 if (this.adapterStatus_[i] == DiagPage.AdapterStatus.NOT_FOUND) | |
145 continue; | |
146 var radioElement = document.createElement('input'); | |
147 var elementId = this.getAdapterElementId_(i); | |
148 radioElement.setAttribute('type', 'radio'); | |
149 radioElement.setAttribute('name', 'adapter'); | |
150 radioElement.setAttribute('id', elementId); | |
151 if (i == this.activeAdapter_) | |
152 radioElement.setAttribute('checked', 'true'); | |
153 radioElement.onclick = function(adapter) { | |
154 this.activeAdapter_ = adapter; | |
155 this.updateConnectivityStatus_(); | |
156 }.bind(this, i); | |
157 var labelElement = document.createElement('label'); | |
158 labelElement.setAttribute('for', elementId); | |
159 labelElement.appendChild(radioElement); | |
160 labelElement.appendChild( | |
161 document.createTextNode(DiagPage.AdapterType[i].name)); | |
162 adapterSelectionElement.appendChild(labelElement); | |
163 adapterSelectionElement.appendChild(document.createElement('br')); | |
164 } | |
165 }, | |
166 | |
167 /** | |
168 * Update the connectivity status for the specified network interface. | |
169 * @private | |
170 */ | |
171 updateConnectivityStatus_: function() { | |
172 var adapter = this.activeAdapter_; | |
173 var status = this.adapterStatus_[adapter]; | |
174 var name = DiagPage.AdapterType[adapter].name; | |
175 | |
176 // Status messages for individual tests. | |
177 var connectivityStatusElement = $('connectivity-status'); | |
178 var testStatusElements = new Array(); | |
179 removeChildren(connectivityStatusElement); | |
180 for (var i = 0; i < 3; i++) { | |
181 testStatusElements[i] = document.createElement('div'); | |
182 connectivityStatusElement.appendChild(testStatusElements[i]); | |
183 } | |
184 testStatusElements[0].innerHTML = | |
185 localStrings.getStringF('testing-hardware', name); | |
186 testStatusElements[1].innerHTML = | |
187 localStrings.getString('testing-connection-to-router'); | |
188 testStatusElements[2].innerHTML = | |
189 localStrings.getString('testing-connection-to-internet'); | |
190 | |
191 // Error and recommendation messages may be inserted in test status | |
192 // elements. | |
193 var errorElement = document.createElement('div'); | |
194 var recommendationElement = document.createElement('div'); | |
195 errorElement.className = 'test-error'; | |
196 recommendationElement.className = 'recommendation'; | |
197 testStatusElements[0].className = 'test-performed'; | |
198 if (status == DiagPage.AdapterStatus.DISABLED) { | |
199 errorElement.appendChild(this.failIconElement_.cloneNode()); | |
200 errorElement.appendChild(document.createTextNode( | |
201 localStrings.getStringF('adapter-disabled', name))); | |
202 recommendationElement.innerHTML = | |
203 localStrings.getStringF('enable-adapter', name); | |
204 connectivityStatusElement.insertBefore(errorElement, | |
205 testStatusElements[1]); | |
206 connectivityStatusElement.insertBefore(recommendationElement, | |
207 testStatusElements[1]); | |
208 testStatusElements[1].className = 'test-pending'; | |
209 testStatusElements[2].className = 'test-pending'; | |
210 } else { | |
211 testStatusElements[0].appendChild(this.tickIconElement_.cloneNode()); | |
212 testStatusElements[1].className = 'test-performed'; | |
213 if (status == DiagPage.AdapterStatus.NO_IP) { | |
214 errorElement.appendChild(this.failIconElement_.cloneNode()); | |
215 errorElement.appendChild(document.createTextNode( | |
216 localStrings.getStringF('adapter-no-ip', name))); | |
217 recommendationElement.innerHTML = | |
218 localStrings.getStringF('fix-connection-to-router'); | |
219 connectivityStatusElement.insertBefore(errorElement, | |
220 testStatusElements[2]); | |
221 connectivityStatusElement.insertBefore(recommendationElement, | |
222 testStatusElements[2]); | |
223 testStatusElements[2].className = 'test-pending'; | |
224 } else { | |
225 testStatusElements[1].appendChild(this.tickIconElement_.cloneNode()); | |
226 testStatusElements[2].className = 'test-performed'; | |
227 testStatusElements[2].appendChild(this.tickIconElement_.cloneNode()); | |
228 } | |
229 } | |
230 } | |
231 }; | |
232 | |
233 DiagPage.setNetifStatus = function(netifStatus) { | |
234 DiagPage.getInstance().setNetifStatus_(netifStatus); | |
235 } | |
236 | |
237 // Export | |
238 return { | |
239 DiagPage: DiagPage | |
240 }; | |
241 }); | |
242 | |
5 /** | 243 /** |
6 * Updates the Connectivity Status section. | 244 * Initialize the DiagPage upon DOM content loaded. |
7 * @param {String} connStatus Dictionary containing connectivity status. | |
8 */ | 245 */ |
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() { | 246 document.addEventListener('DOMContentLoaded', function() { |
38 chrome.send('pageLoaded'); | 247 diag.DiagPage.getInstance().initialize(); |
39 }); | 248 }); |
OLD | NEW |