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 var remoting = chrome.extension.getBackgroundPage().remoting; | 7 var remoting = chrome.extension.getBackgroundPage().remoting; |
8 | 8 |
9 XMPP_LOGIN_NAME = 'xmpp_login'; | 9 XMPP_LOGIN_NAME = 'xmpp_login'; |
10 XMPP_TOKEN_NAME = 'xmpp_token'; | 10 XMPP_TOKEN_NAME = 'xmpp_token'; |
(...skipping 13 matching lines...) Expand all Loading... |
24 document.getElementById('oauth2_code_button').style.display = 'inline'; | 24 document.getElementById('oauth2_code_button').style.display = 'inline'; |
25 document.getElementById('oauth2_clear_button').style.display = 'none'; | 25 document.getElementById('oauth2_clear_button').style.display = 'none'; |
26 document.getElementById('oauth2_form').style.display = 'inline'; | 26 document.getElementById('oauth2_form').style.display = 'inline'; |
27 } | 27 } |
28 var xmpp_status = document.getElementById('xmpp_status'); | 28 var xmpp_status = document.getElementById('xmpp_status'); |
29 if (remoting.getItem(XMPP_TOKEN_NAME) && remoting.getItem(XMPP_LOGIN_NAME)) { | 29 if (remoting.getItem(XMPP_TOKEN_NAME) && remoting.getItem(XMPP_LOGIN_NAME)) { |
30 document.getElementById('xmpp_clear').style.display = 'inline'; | 30 document.getElementById('xmpp_clear').style.display = 'inline'; |
31 document.getElementById('xmpp_form').style.display = 'none'; | 31 document.getElementById('xmpp_form').style.display = 'none'; |
32 xmpp_status.innerText = 'OK'; | 32 xmpp_status.innerText = 'OK'; |
33 xmpp_status.style.color = 'green'; | 33 xmpp_status.style.color = 'green'; |
34 remoting.xmppAuthToken = remoting.getItem(XMPP_TOKEN_NAME); | |
35 } else { | 34 } else { |
36 document.getElementById('xmpp_clear').style.display = 'none'; | 35 document.getElementById('xmpp_clear').style.display = 'none'; |
37 document.getElementById('xmpp_form').style.display = 'inline'; | 36 document.getElementById('xmpp_form').style.display = 'inline'; |
38 xmpp_status.innerText = 'Unauthorized'; | 37 xmpp_status.innerText = 'Unauthorized'; |
39 xmpp_status.style.color = 'red'; | 38 xmpp_status.style.color = 'red'; |
40 } | 39 } |
| 40 var current_email = document.getElementById('current_email'); |
| 41 if (remoting.getItem(XMPP_LOGIN_NAME)) { |
| 42 oauth2_status.style.color = 'green'; |
| 43 current_email.innerText = remoting.getItem(XMPP_LOGIN_NAME); |
| 44 } else { |
| 45 oauth2_status.style.color = 'red'; |
| 46 current_email.innerText = 'missing e-mail'; |
| 47 } |
41 } | 48 } |
42 | 49 |
43 function clientLoginError_(xhr) { | 50 function clientLoginError_(xhr) { |
44 // If there's an error URL, load it into an iframe. | 51 // If there's an error URL, load it into an iframe. |
45 var url_line = xhr.responseText.match('Url=.*'); | 52 var url_line = xhr.responseText.match('Url=.*'); |
46 if (url_line) { | 53 if (url_line) { |
47 url = url_line[0].substr(4); | 54 url = url_line[0].substr(4); |
48 var error_frame = document.getElementById('xmpp_error'); | 55 var error_frame = document.getElementById('xmpp_error'); |
49 error_frame.src = url; | 56 error_frame.src = url; |
50 error_frame.style.display = 'block'; | 57 error_frame.style.display = 'block'; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 '&Email=' + encodeURIComponent(form['xmpp_username'].value) + | 139 '&Email=' + encodeURIComponent(form['xmpp_username'].value) + |
133 '&Passwd=' + encodeURIComponent(form['xmpp_password'].value); | 140 '&Passwd=' + encodeURIComponent(form['xmpp_password'].value); |
134 | 141 |
135 if (captcha_result[0]) { | 142 if (captcha_result[0]) { |
136 post_data += '&logintoken=' + encodeURIComponent(captcha_result[0]) + | 143 post_data += '&logintoken=' + encodeURIComponent(captcha_result[0]) + |
137 '&logincaptcha=' + encodeURIComponent(captcha_result[1]); | 144 '&logincaptcha=' + encodeURIComponent(captcha_result[1]); |
138 } | 145 } |
139 xhr.send(post_data); | 146 xhr.send(post_data); |
140 } | 147 } |
141 | 148 |
| 149 function setEmail(form) { |
| 150 remoting.setItem(XMPP_LOGIN_NAME, form['new_email'].value); |
| 151 updateAuthStatus_(); |
| 152 } |
| 153 |
142 function authorizeOAuth2(code) { | 154 function authorizeOAuth2(code) { |
143 remoting.oauth2.exchangeCodeForToken(code, updateAuthStatus_); | 155 remoting.oauth2.exchangeCodeForToken(code, updateAuthStatus_); |
144 } | 156 } |
145 | 157 |
146 function clearOAuth2() { | 158 function clearOAuth2() { |
147 remoting.oauth2.clear(); | 159 remoting.oauth2.clear(); |
148 updateAuthStatus_(); | 160 updateAuthStatus_(); |
149 } | 161 } |
150 | 162 |
151 function clearXmpp() { | 163 function clearXmpp() { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 'preparing_to_share', | 199 'preparing_to_share', |
188 'ready_to_share', | 200 'ready_to_share', |
189 'shared']); | 201 'shared']); |
190 } | 202 } |
191 | 203 |
192 function setClientMode(mode) { | 204 function setClientMode(mode) { |
193 setMode_(mode, ['unconnected', 'connecting', 'connect_failed']); | 205 setMode_(mode, ['unconnected', 'connecting', 'connect_failed']); |
194 } | 206 } |
195 | 207 |
196 function tryShare() { | 208 function tryShare() { |
| 209 if (remoting.oauth2.needsNewAccessToken()) { |
| 210 remoting.oauth2.refreshAccessToken(function() { |
| 211 if (remoting.oauth2.needsNewAccessToken()) { |
| 212 // If we still need it, we're going to infinite loop. |
| 213 throw "Unable to get access token"; |
| 214 } |
| 215 tryShare(); |
| 216 }); |
| 217 return; |
| 218 } |
| 219 |
197 var div = document.getElementById('plugin_wrapper'); | 220 var div = document.getElementById('plugin_wrapper'); |
198 var plugin = document.createElement('embed'); | 221 var plugin = document.createElement('embed'); |
199 plugin.setAttribute('type', 'HOST_PLUGIN_MIMETYPE'); | 222 plugin.setAttribute('type', 'HOST_PLUGIN_MIMETYPE'); |
200 plugin.setAttribute('hidden', 'true'); | 223 plugin.setAttribute('hidden', 'true'); |
201 plugin.setAttribute('id', HOST_PLUGIN_ID); | 224 plugin.setAttribute('id', HOST_PLUGIN_ID); |
202 div.appendChild(plugin); | 225 div.appendChild(plugin); |
203 plugin.onStateChanged = onStateChanged_; | 226 plugin.onStateChanged = onStateChanged_; |
204 plugin.connect(remoting.getItem(XMPP_LOGIN_NAME), | 227 plugin.connect(remoting.getItem(XMPP_LOGIN_NAME), |
205 remoting.getItem(XMPP_TOKEN_NAME)); | 228 'oauth2:' + remoting.oauth2.getAccessToken()); |
206 } | 229 } |
207 | 230 |
208 function onStateChanged_() { | 231 function onStateChanged_() { |
209 var plugin = document.getElementById(HOST_PLUGIN_ID); | 232 var plugin = document.getElementById(HOST_PLUGIN_ID); |
210 var state = plugin.state; | 233 var state = plugin.state; |
211 if (state == plugin.REQUESTED_ACCESS_CODE) { | 234 if (state == plugin.REQUESTED_ACCESS_CODE) { |
212 setHostMode('preparing_to_share'); | 235 setHostMode('preparing_to_share'); |
213 } else if (state == plugin.RECEIVED_ACCESS_CODE) { | 236 } else if (state == plugin.RECEIVED_ACCESS_CODE) { |
214 var access_code = plugin.accessCode; | 237 var access_code = plugin.accessCode; |
215 var access_code_display = document.getElementById('access_code_display'); | 238 var access_code_display = document.getElementById('access_code_display'); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 | 303 |
281 xhr.open('GET', | 304 xhr.open('GET', |
282 'https://www.googleapis.com/chromoting/v1/support-hosts/' + | 305 'https://www.googleapis.com/chromoting/v1/support-hosts/' + |
283 encodeURIComponent(support_id), | 306 encodeURIComponent(support_id), |
284 true); | 307 true); |
285 xhr.setRequestHeader('Authorization', | 308 xhr.setRequestHeader('Authorization', |
286 'OAuth ' + remoting.oauth2.getAccessToken()); | 309 'OAuth ' + remoting.oauth2.getAccessToken()); |
287 xhr.send(null); | 310 xhr.send(null); |
288 } | 311 } |
289 | 312 |
290 function tryConnect(form) { | 313 function tryConnect(accessCode) { |
291 remoting.accessCode = normalizeAccessCode(form['access_code_entry'].value); | 314 if (remoting.oauth2.needsNewAccessToken()) { |
| 315 remoting.oauth2.refreshAccessToken(function() { |
| 316 if (remoting.oauth2.needsNewAccessToken()) { |
| 317 // If we still need it, we're going to infinite loop. |
| 318 throw "Unable to get access token"; |
| 319 } |
| 320 tryConnect(accessCode); |
| 321 }); |
| 322 return; |
| 323 } |
| 324 |
| 325 remoting.accessCode = accessCode; |
292 // TODO(jamiewalch): Since the mapping from (SupportId, HostSecret) to | 326 // TODO(jamiewalch): Since the mapping from (SupportId, HostSecret) to |
293 // AccessCode is not yet defined, assume it's hyphen-separated for now. | 327 // AccessCode is not yet defined, assume it's hyphen-separated for now. |
294 var parts = remoting.accessCode.split('-'); | 328 var parts = remoting.accessCode.split('-'); |
295 if (parts.length != 2) { | 329 if (parts.length != 2) { |
296 showConnectError_(404); | 330 showConnectError_(404); |
297 } else { | 331 } else { |
298 setClientMode('connecting'); | 332 setClientMode('connecting'); |
299 if (remoting.oauth2.needsNewAccessToken()) { | 333 resolveSupportId(parts[0]); |
300 remoting.oauth2.refreshAccessToken(function() { | |
301 resolveSupportId(parts[0]); | |
302 }); | |
303 return; | |
304 } else { | |
305 resolveSupportId(parts[0]); | |
306 } | |
307 } | 334 } |
308 } | 335 } |
309 | 336 |
310 function cancelConnect() { | 337 function cancelConnect() { |
311 remoting.accessCode = ''; | 338 remoting.accessCode = ''; |
312 setClientMode('unconnected'); | 339 setClientMode('unconnected'); |
313 } | 340 } |
OLD | NEW |