Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(394)

Side by Side Diff: remoting/webapp/oauth2.js

Issue 10207028: Fixed callWithToken error handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewer feedback. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/webapp/host_list.js ('k') | remoting/webapp/wcs.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * OAuth2 class that handles retrieval/storage of an OAuth2 token. 7 * OAuth2 class that handles retrieval/storage of an OAuth2 token.
8 * 8 *
9 * Uses a content script to trampoline the OAuth redirect page back into the 9 * Uses a content script to trampoline the OAuth redirect page back into the
10 * extension context. This works around the lack of native support for 10 * extension context. This works around the lack of native support for
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 }; 332 };
333 333
334 /** 334 /**
335 * Call myfunc with an access token as the only parameter. 335 * Call myfunc with an access token as the only parameter.
336 * 336 *
337 * This will refresh the access token if necessary. If the access token 337 * This will refresh the access token if necessary. If the access token
338 * cannot be refreshed, an error is thrown. 338 * cannot be refreshed, an error is thrown.
339 * 339 *
340 * The access token will remain valid for at least 2 minutes. 340 * The access token will remain valid for at least 2 minutes.
341 * 341 *
342 * @param {function(string):void} myfunc 342 * @param {function(string?):void} myfunc Function to invoke with access token.
343 * Function to invoke with access token.
344 * @return {void} Nothing. 343 * @return {void} Nothing.
345 */ 344 */
346 remoting.OAuth2.prototype.callWithToken = function(myfunc) { 345 remoting.OAuth2.prototype.callWithToken = function(myfunc) {
347 /** @type {remoting.OAuth2} */ 346 /** @type {remoting.OAuth2} */
348 var that = this; 347 var that = this;
349 if (remoting.oauth2.needsNewAccessToken()) { 348 if (this.needsNewAccessToken()) {
350 remoting.oauth2.refreshAccessToken(function() { 349 var onRefresh = function() {
351 if (remoting.oauth2.needsNewAccessToken()) { 350 if (that.needsNewAccessToken()) {
352 // If we still need it, we're going to infinite loop. 351 myfunc(null);
353 throw 'Unable to get access token.'; 352 } else {
353 myfunc(that.getAccessToken());
354 } 354 }
355 myfunc(that.getAccessToken()); 355 };
356 }); 356 this.refreshAccessToken(onRefresh);
357 return; 357 } else {
358 myfunc(this.getAccessToken());
358 } 359 }
359
360 myfunc(this.getAccessToken());
361 }; 360 };
362 361
363 /** 362 /**
364 * Get the user's email address. 363 * Get the user's email address.
365 * 364 *
366 * @param {function(?string):void} setEmail Callback invoked when the email 365 * @param {function(?string):void} setEmail Callback invoked when the email
367 * address is available, or on error. 366 * address is available, or on error.
368 * @return {void} Nothing. 367 * @return {void} Nothing.
369 */ 368 */
370 remoting.OAuth2.prototype.getEmail = function(setEmail) { 369 remoting.OAuth2.prototype.getEmail = function(setEmail) {
371 /** @type {remoting.OAuth2} */ 370 /** @type {remoting.OAuth2} */
372 var that = this; 371 var that = this;
373 /** @param {XMLHttpRequest} xhr The XHR response. */ 372 /** @param {XMLHttpRequest} xhr The XHR response. */
374 var onResponse = function(xhr) { 373 var onResponse = function(xhr) {
375 that.email = null; 374 that.email = null;
376 if (xhr.status == 200) { 375 if (xhr.status == 200) {
377 // TODO(ajwong): See if we can't find a JSON endpoint. 376 // TODO(ajwong): See if we can't find a JSON endpoint.
378 that.email = xhr.responseText.split('&')[0].split('=')[1]; 377 that.email = xhr.responseText.split('&')[0].split('=')[1];
379 } 378 }
380 window.localStorage.setItem(that.KEY_EMAIL_, that.email); 379 window.localStorage.setItem(that.KEY_EMAIL_, that.email);
381 setEmail(that.email); 380 setEmail(that.email);
382 }; 381 };
383 382
384 /** @param {string} token The access token. */ 383 /** @param {string?} token The access token. */
385 var getEmailFromToken = function(token) { 384 var getEmailFromToken = function(token) {
386 var headers = { 'Authorization': 'OAuth ' + token }; 385 if (token) {
387 // TODO(ajwong): Update to new v2 API. 386 var headers = { 'Authorization': 'OAuth ' + token };
388 remoting.xhr.get('https://www.googleapis.com/userinfo/email', 387 // TODO(ajwong): Update to new v2 API.
389 onResponse, '', headers); 388 remoting.xhr.get('https://www.googleapis.com/userinfo/email',
389 onResponse, '', headers);
390 } else {
391 setEmail(null);
392 }
390 }; 393 };
391 394
392 this.callWithToken(getEmailFromToken); 395 this.callWithToken(getEmailFromToken);
393 }; 396 };
394 397
395 /** 398 /**
396 * If the user's email address is cached, return it, otherwise return null. 399 * If the user's email address is cached, return it, otherwise return null.
397 * 400 *
398 * @return {?string} The email address, if it has been cached by a previous call 401 * @return {?string} The email address, if it has been cached by a previous call
399 * to getEmail, otherwise null. 402 * to getEmail, otherwise null.
400 */ 403 */
401 remoting.OAuth2.prototype.getCachedEmail = function() { 404 remoting.OAuth2.prototype.getCachedEmail = function() {
402 var value = window.localStorage.getItem(this.KEY_EMAIL_); 405 var value = window.localStorage.getItem(this.KEY_EMAIL_);
403 if (typeof value == 'string') { 406 if (typeof value == 'string') {
404 return value; 407 return value;
405 } 408 }
406 return null; 409 return null;
407 }; 410 };
OLDNEW
« no previous file with comments | « remoting/webapp/host_list.js ('k') | remoting/webapp/wcs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698