Chromium Code Reviews| Index: remoting/webapp/crd/js/identity.js |
| diff --git a/remoting/webapp/crd/js/identity.js b/remoting/webapp/crd/js/identity.js |
| index aef0f2c6f984444844d7002e1f4d827a90dfb09b..ff695ec30055f96e79182c88c92ad82774ead43b 100644 |
| --- a/remoting/webapp/crd/js/identity.js |
| +++ b/remoting/webapp/crd/js/identity.js |
| @@ -21,20 +21,17 @@ var remoting = remoting || {}; |
| remoting.identity = null; |
| /** |
| - * @param {function(function():void):void} consentCallback Callback invoked if |
| - * user consent is required. The callback is passed a continuation function |
| - * which must be called from an interactive event handler (e.g. "click"). |
| * @constructor |
| */ |
| -remoting.Identity = function(consentCallback) { |
| - /** @private */ |
| - this.consentCallback_ = consentCallback; |
| +remoting.Identity = function() { |
| /** @type {string} @private */ |
| this.email_ = ''; |
| /** @type {string} @private */ |
| this.fullName_ = ''; |
| /** @type {Array.<remoting.Identity.Callbacks>} */ |
| this.pendingCallbacks_ = []; |
| + /** @type {Promise} */ |
| + this.handleAuthFailurePromise_ = null; |
| }; |
| /** |
| @@ -75,27 +72,75 @@ remoting.Identity.prototype.callWithNewToken = function(onOk, onError) { |
| chrome.identity.removeCachedAuthToken( |
| {'token': token }, |
| that.callWithToken.bind(that, onOk, onError)); |
| - }; |
| + } |
| this.callWithToken(revokeToken, onError); |
| }; |
| /** |
| + * Removes the cached token and prompts the user to get a new one. |
| + * @return {Promise} A promise that resolves when a new token is fetched. |
| + */ |
| +remoting.Identity.prototype.handleAuthFailure = function() { |
| + if (!this.handleAuthFailurePromise_) { |
| + var that = this; |
| + |
| + var revokeToken = this.removeCachedAuthToken_(); |
| + |
| + // Revoke the token. |
| + this.handleAuthFailurePromise_= revokeToken.then(function() { |
| + // Showing the auth dialog is required as chrome.identity.getAuthToken |
| + // must be initiated from user action when interactive is set to true. |
| + return remoting.AuthDialog.show(); |
| + }).then(function(){ |
| + // Fetch the new token using the identity API. |
| + return base.Promise.as( |
| + chrome.identity.getAuthToken, [{ 'interactive': true }]); |
| + }).then( |
| + /** @param {string} token */ |
| + function(token) { |
| + that.handleAuthFailurePromise_ = null; |
| + if (token) { |
| + return Promise.resolve(token); |
| + } |
| + return Promise.reject(remoting.Error.NOT_AUTHENTICATED); |
| + } |
| + ); |
| + } |
| + return this.handleAuthFailurePromise_; |
| +}; |
| + |
| +/** |
| + * Removes the cached token, prompts the user to get a new one and |
| + * relaunches the current window. |
| + * @return {void} Nothing. |
| + */ |
| +remoting.Identity.prototype.handleAuthFailureAndRelaunch = function() { |
| + this.handleAuthFailure().then(function() { |
| + base.IPC.invoke(remoting.ActivationHandler.IPC.RELAUNCH, |
|
kelvinp
2015/01/28 00:26:46
Will be fixed later after I address your feedback
|
| + chrome.app.window.current().id); |
| + }); |
| +}; |
| + |
| +/** |
| * Remove the cached auth token, if any. |
| * |
| - * @param {function():void} onDone Completion callback. |
| - * @return {void} Nothing. |
| + * @return {Promise} Promise that resolves when the cached token is removed. |
| + * @private |
| */ |
| -remoting.Identity.prototype.removeCachedAuthToken = function(onDone) { |
| - /** @param {string} token */ |
| - var onToken = function(token) { |
| - if (token) { |
| - chrome.identity.removeCachedAuthToken({ 'token': token }, onDone); |
| - } else { |
| - onDone(); |
| - } |
| - }; |
| - chrome.identity.getAuthToken({ 'interactive': false }, onToken); |
| +remoting.Identity.prototype.removeCachedAuthToken_ = function() { |
| + var getToken = |
| + base.Promise.as(chrome.identity.getAuthToken, [{'interactive': false}]); |
| + return getToken.then( |
| + /** @param {string} token */ |
| + function(token) { |
| + if (token) { |
| + return base.Promise.as(chrome.identity.removeCachedAuthToken, |
| + [{'token': token}]); |
| + } else { |
| + return Promise.resolve(); |
| + } |
| + }); |
| }; |
| /** |
| @@ -206,19 +251,8 @@ remoting.Identity.prototype.onAuthComplete_ = function(interactive, token) { |
| } |
| // If there's no token, but we haven't yet prompted for permission, do so |
| - // now. The consent callback is responsible for continuing the auth flow. |
| - this.consentCallback_(this.onAuthContinue_.bind(this)); |
| -}; |
| - |
| -/** |
| - * Called in response to the user signing in to the web-app. |
| - * |
| - * @private |
| - */ |
| -remoting.Identity.prototype.onAuthContinue_ = function() { |
| - chrome.identity.getAuthToken( |
| - { 'interactive': true }, |
| - this.onAuthComplete_.bind(this, true)); |
| + // now. |
| + this.handleAuthFailure().then(this.onAuthComplete_.bind(this, true)); |
| }; |
| /** |