OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // TODO(ajwong): This seems like a bad idea to share the exact same object | 5 // TODO(ajwong): This seems like a bad idea to share the exact same object |
6 // with the background page. Why are we doing it like this? | 6 // with the background page. Why are we doing it like this? |
7 | |
8 "use strict"; | |
9 | |
7 var remoting = chrome.extension.getBackgroundPage().remoting; | 10 var remoting = chrome.extension.getBackgroundPage().remoting; |
11 remoting.CLIENT_MODE='client'; | |
12 remoting.HOST_MODE='host'; | |
8 | 13 |
9 XMPP_LOGIN_NAME = 'xmpp_login'; | 14 var XMPP_LOGIN_NAME = 'xmpp_login'; |
10 XMPP_TOKEN_NAME = 'xmpp_token'; | 15 var XMPP_TOKEN_NAME = 'xmpp_token'; |
11 HOST_PLUGIN_ID = 'host_plugin_id'; | 16 var HOST_PLUGIN_ID = 'host_plugin_id'; |
12 | 17 |
13 function updateAuthStatus_() { | 18 function updateAuthStatus_() { |
14 var oauth2_status = document.getElementById('oauth2_status'); | |
15 if (remoting.oauth2.isAuthenticated()) { | 19 if (remoting.oauth2.isAuthenticated()) { |
16 oauth2_status.innerText = 'OK'; | 20 document.getElementById('oauth2_code').style.display = 'none'; |
17 oauth2_status.style.color = 'green'; | 21 document.getElementById('oauth2_submit_button').style.display = 'none'; |
18 document.getElementById('oauth2_code_button').style.display = 'none'; | 22 document.getElementById('oauth2_code_button').style.display = 'none'; |
19 document.getElementById('oauth2_clear_button').style.display = 'inline'; | 23 document.getElementById('oauth2_clear_button').style.display = 'inline'; |
20 document.getElementById('oauth2_form').style.display = 'none'; | 24 document.getElementById('oauth2_form').style.display = 'inline'; |
21 } else { | 25 } else { |
22 oauth2_status.innerText = 'Unauthorized'; | 26 document.getElementById('oauth2_code').value = ""; |
23 oauth2_status.style.color = 'red'; | 27 document.getElementById('oauth2_code').style.display = 'inline'; |
28 document.getElementById('oauth2_submit_button').style.display = 'none'; | |
24 document.getElementById('oauth2_code_button').style.display = 'inline'; | 29 document.getElementById('oauth2_code_button').style.display = 'inline'; |
25 document.getElementById('oauth2_clear_button').style.display = 'none'; | 30 document.getElementById('oauth2_clear_button').style.display = 'none'; |
26 document.getElementById('oauth2_form').style.display = 'inline'; | 31 document.getElementById('oauth2_form').style.display = 'inline'; |
27 } | 32 } |
28 var xmpp_status = document.getElementById('xmpp_status'); | 33 var xmpp_status = document.getElementById('xmpp_status'); |
29 if (remoting.getItem(XMPP_TOKEN_NAME) && remoting.getItem(XMPP_LOGIN_NAME)) { | 34 if (remoting.getItem(XMPP_TOKEN_NAME) && remoting.getItem(XMPP_LOGIN_NAME)) { |
30 document.getElementById('xmpp_clear').style.display = 'inline'; | 35 document.getElementById('xmpp_clear').style.display = 'inline'; |
31 document.getElementById('xmpp_form').style.display = 'none'; | 36 document.getElementById('xmpp_form').style.display = 'none'; |
32 xmpp_status.innerText = 'OK'; | 37 xmpp_status.innerText = 'OK'; |
33 xmpp_status.style.color = 'green'; | 38 xmpp_status.style.color = 'green'; |
34 } else { | 39 } else { |
35 document.getElementById('xmpp_clear').style.display = 'none'; | 40 document.getElementById('xmpp_clear').style.display = 'none'; |
36 document.getElementById('xmpp_form').style.display = 'inline'; | 41 document.getElementById('xmpp_form').style.display = 'inline'; |
37 xmpp_status.innerText = 'Unauthorized'; | 42 xmpp_status.innerText = 'Unauthorized'; |
38 xmpp_status.style.color = 'red'; | 43 xmpp_status.style.color = 'red'; |
39 } | 44 } |
40 var current_email = document.getElementById('current_email'); | 45 var current_email = document.getElementById('current_email'); |
41 if (remoting.getItem(XMPP_LOGIN_NAME)) { | 46 if (remoting.getItem(XMPP_LOGIN_NAME)) { |
42 oauth2_status.style.color = 'green'; | |
43 current_email.innerText = remoting.getItem(XMPP_LOGIN_NAME); | 47 current_email.innerText = remoting.getItem(XMPP_LOGIN_NAME); |
48 current_email.style.display = 'inline'; | |
49 document.getElementById('new_email').style.display = 'none'; | |
50 document.getElementById('email_submit_button').style.display = 'none'; | |
51 document.getElementById('change_email_button').style.display = 'inline'; | |
44 } else { | 52 } else { |
45 oauth2_status.style.color = 'red'; | 53 current_email.style.display = 'none'; |
46 current_email.innerText = 'missing e-mail'; | 54 document.getElementById('new_email').style.display = 'inline'; |
55 document.getElementById('email_submit_button').style.display = 'inline'; | |
56 document.getElementById('change_email_button').style.display = 'none'; | |
47 } | 57 } |
48 } | 58 } |
49 | 59 |
50 function clientLoginError_(xhr) { | 60 function clientLoginError_(xhr) { |
51 // If there's an error URL, load it into an iframe. | 61 // If there's an error URL, load it into an iframe. |
52 var url_line = xhr.responseText.match('Url=.*'); | 62 var url_line = xhr.responseText.match('Url=.*'); |
53 if (url_line) { | 63 if (url_line) { |
54 url = url_line[0].substr(4); | 64 url = url_line[0].substr(4); |
55 var error_frame = document.getElementById('xmpp_error'); | 65 var error_frame = document.getElementById('xmpp_error'); |
56 error_frame.src = url; | 66 error_frame.src = url; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 '&Email=' + encodeURIComponent(form['xmpp_username'].value) + | 149 '&Email=' + encodeURIComponent(form['xmpp_username'].value) + |
140 '&Passwd=' + encodeURIComponent(form['xmpp_password'].value); | 150 '&Passwd=' + encodeURIComponent(form['xmpp_password'].value); |
141 | 151 |
142 if (captcha_result[0]) { | 152 if (captcha_result[0]) { |
143 post_data += '&logintoken=' + encodeURIComponent(captcha_result[0]) + | 153 post_data += '&logintoken=' + encodeURIComponent(captcha_result[0]) + |
144 '&logincaptcha=' + encodeURIComponent(captcha_result[1]); | 154 '&logincaptcha=' + encodeURIComponent(captcha_result[1]); |
145 } | 155 } |
146 xhr.send(post_data); | 156 xhr.send(post_data); |
147 } | 157 } |
148 | 158 |
149 function setEmail(form) { | 159 function setEmail(value) { |
150 remoting.setItem(XMPP_LOGIN_NAME, form['new_email'].value); | 160 remoting.setItem(XMPP_LOGIN_NAME, value); |
161 updateAuthStatus_(); | |
162 } | |
163 | |
164 function exchangedCodeForToken_() { | |
165 if (!remoting.oauth2.isAuthenticated()) { | |
166 alert("Your OAuth2 token was invalid. Please try again."); | |
167 } | |
151 updateAuthStatus_(); | 168 updateAuthStatus_(); |
152 } | 169 } |
153 | 170 |
154 function authorizeOAuth2(code) { | 171 function authorizeOAuth2(code) { |
155 remoting.oauth2.exchangeCodeForToken(code, updateAuthStatus_); | 172 remoting.oauth2.exchangeCodeForToken(code, exchangedCodeForToken_); |
156 } | 173 } |
157 | 174 |
158 function clearOAuth2() { | 175 function clearOAuth2() { |
159 remoting.oauth2.clear(); | 176 remoting.oauth2.clear(); |
160 updateAuthStatus_(); | 177 updateAuthStatus_(); |
161 } | 178 } |
162 | 179 |
180 function handleOAuth2CodeChange() { | |
181 if (document.getElementById('oauth2_code').value.length > 0) { | |
182 document.getElementById('oauth2_submit_button').style.display = 'inline'; | |
183 document.getElementById('oauth2_code_button').style.display = 'none'; | |
184 } else { | |
185 document.getElementById('oauth2_submit_button').style.display = 'none'; | |
186 document.getElementById('oauth2_code_button').style.display = 'inline'; | |
187 } | |
188 } | |
189 | |
163 function clearXmpp() { | 190 function clearXmpp() { |
164 remoting.removeItem(XMPP_TOKEN_NAME); | 191 remoting.removeItem(XMPP_TOKEN_NAME); |
165 updateAuthStatus_(); | 192 updateAuthStatus_(); |
166 } | 193 } |
167 | 194 |
168 // Show the div with id |mode| and hide those with other ids in |modes|. | 195 // Show the div with id |mode| and hide those with other ids in |modes|. |
169 function setMode_(mode, modes) { | 196 function setMode_(mode, modes) { |
170 for (var i = 0; i < modes.length; ++i) { | 197 for (var i = 0; i < modes.length; ++i) { |
171 var div = document.getElementById(modes[i]); | 198 var div = document.getElementById(modes[i]); |
172 if (mode == modes[i]) { | 199 if (mode == modes[i]) { |
173 div.style.display = 'block'; | 200 div.style.display = 'block'; |
174 } else { | 201 } else { |
175 div.style.display = 'none'; | 202 div.style.display = 'none'; |
176 } | 203 } |
177 } | 204 } |
178 } | 205 } |
179 | 206 |
180 function init() { | 207 function init() { |
181 initBackgroundFuncs_(); | 208 initBackgroundFuncs_(); |
182 initAuthPanel_(); | 209 initAuthPanel_(); |
183 setHostMode('unshared'); | 210 setHostMode('unshared'); |
184 setClientMode('unconnected'); | 211 setClientMode('unconnected'); |
185 setGlobalMode(remoting.getItem('startup-mode', 'host')); | 212 setGlobalMode(remoting.getItem('startup-mode', remoting.HOST_MODE)); |
186 } | 213 } |
187 | 214 |
188 function setGlobalMode(mode) { | 215 function setGlobalMode(mode) { |
189 setMode_(mode, ['host', 'client']); | 216 var elementsToShow = []; |
217 var elementsToHide = []; | |
218 var hostElements = [ | |
219 { id:'host_section', display:'block' }, | |
220 { id:'host_header', display:'inline' }, | |
221 { id:'host_footer', display:'block'} | |
222 ]; | |
223 var clientElements = [ | |
224 { id:'client_section', display:'block' }, | |
225 { id:'client_header', display:'inline' }, | |
226 { id:'client_footer', display:'block'} | |
227 ]; | |
228 if (mode == remoting.HOST_MODE) { | |
229 elementsToShow = hostElements; | |
230 elementsToHide = clientElements; | |
231 } else { | |
232 elementsToShow = clientElements; | |
233 elementsToHide = hostElements; | |
234 } | |
235 for (var i = 0; i < elementsToShow.length; ++i) { | |
236 var div = document.getElementById(elementsToShow[i].id); | |
237 div.style.display = elementsToShow[i].display; | |
238 } | |
239 for (var i = 0; i < elementsToHide.length; ++i) { | |
240 var div = document.getElementById(elementsToHide[i].id); | |
241 div.style.display = 'none'; | |
242 } | |
190 } | 243 } |
191 | 244 |
192 function setGlobalModePersistent(mode) { | 245 function setGlobalModePersistent(mode) { |
193 setGlobalMode(mode); | 246 setGlobalMode(mode); |
194 remoting.setItem('startup-mode', mode); | 247 remoting.setItem('startup-mode', mode); |
195 } | 248 } |
196 | 249 |
197 function setHostMode(mode) { | 250 function setHostMode(mode) { |
198 setMode_(mode, ['unshared', | 251 setMode_(mode, ['unshared', |
199 'preparing_to_share', | 252 'preparing_to_share', |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 | 356 |
304 xhr.open('GET', | 357 xhr.open('GET', |
305 'https://www.googleapis.com/chromoting/v1/support-hosts/' + | 358 'https://www.googleapis.com/chromoting/v1/support-hosts/' + |
306 encodeURIComponent(support_id), | 359 encodeURIComponent(support_id), |
307 true); | 360 true); |
308 xhr.setRequestHeader('Authorization', | 361 xhr.setRequestHeader('Authorization', |
309 'OAuth ' + remoting.oauth2.getAccessToken()); | 362 'OAuth ' + remoting.oauth2.getAccessToken()); |
310 xhr.send(null); | 363 xhr.send(null); |
311 } | 364 } |
312 | 365 |
313 function tryConnect(accessCode) { | 366 function tryConnect() { |
Jamie
2011/05/31 18:11:49
Why not pass the access code in as before? That wa
dmac
2011/06/01 23:01:58
Because currently it doesn't work with the "form"
| |
367 var accessCode = document.getElementById('access_code_entry').value; | |
314 if (remoting.oauth2.needsNewAccessToken()) { | 368 if (remoting.oauth2.needsNewAccessToken()) { |
315 remoting.oauth2.refreshAccessToken(function() { | 369 remoting.oauth2.refreshAccessToken(function() { |
316 if (remoting.oauth2.needsNewAccessToken()) { | 370 if (remoting.oauth2.needsNewAccessToken()) { |
317 // If we still need it, we're going to infinite loop. | 371 // If we still need it, we're going to infinite loop. |
318 throw "Unable to get access token"; | 372 throw "Unable to get access token"; |
319 } | 373 } |
320 tryConnect(accessCode); | 374 tryConnect(); |
321 }); | 375 }); |
322 return; | 376 return; |
323 } | 377 } |
324 | 378 |
325 remoting.accessCode = accessCode; | 379 remoting.accessCode = accessCode; |
326 // TODO(jamiewalch): Since the mapping from (SupportId, HostSecret) to | 380 // TODO(jamiewalch): Since the mapping from (SupportId, HostSecret) to |
327 // AccessCode is not yet defined, assume it's hyphen-separated for now. | 381 // AccessCode is not yet defined, assume it's hyphen-separated for now. |
328 var parts = remoting.accessCode.split('-'); | 382 var parts = remoting.accessCode.split('-'); |
329 if (parts.length != 2) { | 383 if (parts.length != 2) { |
330 showConnectError_(404); | 384 showConnectError_(404); |
331 } else { | 385 } else { |
332 setClientMode('connecting'); | 386 setClientMode('connecting'); |
333 resolveSupportId(parts[0]); | 387 resolveSupportId(parts[0]); |
334 } | 388 } |
335 } | 389 } |
336 | 390 |
337 function cancelConnect() { | 391 function cancelConnect() { |
338 remoting.accessCode = ''; | 392 remoting.accessCode = ''; |
339 setClientMode('unconnected'); | 393 setClientMode('unconnected'); |
340 } | 394 } |
OLD | NEW |