diff options
author | jamiewalch@google.com <jamiewalch@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-09 21:50:51 +0000 |
---|---|---|
committer | jamiewalch@google.com <jamiewalch@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-09 21:50:51 +0000 |
commit | 480850718000d048a919695e41626be13ef16b71 (patch) | |
tree | dec734c53cd16eeafccd19cb9767b9cc831f243a /remoting | |
parent | dae3c887ca056ad62068222501ec6a3ade48e010 (diff) | |
download | chromium_src-480850718000d048a919695e41626be13ef16b71.zip chromium_src-480850718000d048a919695e41626be13ef16b71.tar.gz chromium_src-480850718000d048a919695e41626be13ef16b71.tar.bz2 |
Cleaned up WCS workflow.
Got rid of tokenRefresh parameter (invoke callWithToken explicitly instead).
BUG=108475
TEST=Manual
Review URL: http://codereview.chromium.org/9123012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116916 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/webapp/me2mom/client_screen.js | 98 | ||||
-rw-r--r-- | remoting/webapp/me2mom/event_handlers.js | 4 | ||||
-rw-r--r-- | remoting/webapp/me2mom/remoting.js | 4 | ||||
-rw-r--r-- | remoting/webapp/me2mom/wcs.js | 17 | ||||
-rw-r--r-- | remoting/webapp/me2mom/wcs_loader.js | 177 |
5 files changed, 111 insertions, 189 deletions
diff --git a/remoting/webapp/me2mom/client_screen.js b/remoting/webapp/me2mom/client_screen.js index c130247..3cbda0d 100644 --- a/remoting/webapp/me2mom/client_screen.js +++ b/remoting/webapp/me2mom/client_screen.js @@ -70,26 +70,13 @@ remoting.ConnectionType = { remoting.currentConnectionType = null; /** - * Entry point for the 'connect' functionality. This function checks for the - * existence of an OAuth2 token, and either requests one asynchronously, or - * calls through directly to tryConnectWithAccessToken_. + * Entry point for the 'connect' functionality. This function defers to the + * WCS loader to call it back with an access token. */ -remoting.tryConnect = function() { +remoting.connectIt2Me = function() { remoting.currentConnectionType = remoting.ConnectionType.It2Me; document.getElementById('cancel-button').disabled = false; - if (remoting.oauth2.needsNewAccessToken()) { - remoting.oauth2.refreshAccessToken(function(xhr) { - if (remoting.oauth2.needsNewAccessToken()) { - // Failed to get access token - remoting.debug.log('tryConnect: OAuth2 token fetch failed'); - showConnectError_(remoting.Error.AUTHENTICATION_FAILED); - return; - } - tryConnectWithAccessToken_(); - }); - } else { - tryConnectWithAccessToken_(); - } + remoting.WcsLoader.load(connectIt2MeWithAccessToken_); }; /** @@ -156,32 +143,14 @@ remoting.disconnect = function() { }; /** - * Second stage of the 'connect' functionality. Once an access token is - * available, load the WCS widget asynchronously and call through to - * tryConnectWithWcs_ when ready. - */ -function tryConnectWithAccessToken_() { - if (!remoting.wcsLoader) { - remoting.wcsLoader = new remoting.WcsLoader(); - } - /** @param {function(string):void} setToken The callback function. */ - var callWithToken = function(setToken) { - remoting.oauth2.callWithToken(setToken); - }; - remoting.wcsLoader.start( - remoting.oauth2.getAccessToken(), - callWithToken, - tryConnectWithWcs_); -} - -/** - * Final stage of the 'connect' functionality, called when the wcs widget has - * been loaded, or on error. + * If WCS was successfully loaded, proceed with the connection, otherwise + * report an error. * - * @param {boolean} success True if the script was loaded successfully. + * @param {string?} token The OAuth2 access token, or null if an error occurred. + * @return {void} Nothing. */ -function tryConnectWithWcs_(success) { - if (success) { +function connectIt2MeWithAccessToken_(token) { + if (token) { var accessCode = document.getElementById('access-code-entry').value; remoting.accessCode = normalizeAccessCode_(accessCode); // At present, only 12-digit access codes are supported, of which the first @@ -293,7 +262,7 @@ function retryConnectOrReportOffline_() { /** @param {boolean} success True if the refresh was successful. */ var onDone = function(success) { if (success) { - remoting.connectHost(remoting.hostId, false); + remoting.connectMe2Me(remoting.hostId, false); } else { showConnectError_(remoting.Error.HOST_IS_OFFLINE); } @@ -441,7 +410,7 @@ function updateStatistics_() { * work even if they reregister with Talk and get a different Jid. * @return {void} Nothing. */ -remoting.connectHost = function(hostId, retryIfOffline) { +remoting.connectMe2Me = function(hostId, retryIfOffline) { remoting.currentConnectionType = remoting.ConnectionType.Me2Me; remoting.hostId = hostId; remoting.retryIfOffline = retryIfOffline; @@ -455,7 +424,7 @@ remoting.connectHost = function(hostId, retryIfOffline) { * * @return {void} Nothing. */ -remoting.connectHostWithPin = function() { +remoting.connectMe2MeWithPin = function() { remoting.debug.log('Connecting to host...'); remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); @@ -469,37 +438,30 @@ remoting.connectHostWithPin = function() { document.getElementById('connected-to').innerText = host.hostName; document.title = document.title + ': ' + host.hostName; - if (!remoting.wcsLoader) { - remoting.wcsLoader = new remoting.WcsLoader(); - } - /** @param {function(string):void} setToken The callback function. */ - var callWithToken = function(setToken) { - remoting.oauth2.callWithToken(setToken); - }; - remoting.wcsLoader.startAsync(callWithToken, remoting.connectHostWithWcs); + remoting.WcsLoader.load(connectMe2MeWithAccessToken_); }; /** * Continue making the connection to a host, once WCS has initialized. * + * @param {string?} token The OAuth2 access token, or null if an error occurred. * @return {void} Nothing. */ -remoting.connectHostWithWcs = function() { - /** @type {string} */ - var pin = document.getElementById('pin-entry').value; - document.getElementById('pin-entry').value = ''; - - remoting.clientSession = - new remoting.ClientSession( - remoting.hostJid, remoting.hostPublicKey, - pin, /** @type {string} */ (remoting.oauth2.getCachedEmail()), - onClientStateChange_); - /** @param {string} token The auth token. */ - var createPluginAndConnect = function(token) { +function connectMe2MeWithAccessToken_(token) { + if (token) { + /** @type {string} */ + var pin = document.getElementById('pin-entry').value; + document.getElementById('pin-entry').value = ''; + + remoting.clientSession = + new remoting.ClientSession( + remoting.hostJid, remoting.hostPublicKey, + pin, /** @type {string} */ (remoting.oauth2.getCachedEmail()), + onClientStateChange_); remoting.clientSession.createPluginAndConnect( document.getElementById('session-mode'), token); - }; - - remoting.oauth2.callWithToken(createPluginAndConnect); -}; + } else { + showConnectError_(remoting.Error.AUTHENTICATION_FAILED); + } +} diff --git a/remoting/webapp/me2mom/event_handlers.js b/remoting/webapp/me2mom/event_handlers.js index 2baf133..bafb58e 100644 --- a/remoting/webapp/me2mom/event_handlers.js +++ b/remoting/webapp/me2mom/event_handlers.js @@ -16,12 +16,12 @@ function onLoad() { }; /** @param {Event} event */ var sendAccessCode = function(event) { - remoting.tryConnect(); + remoting.connectIt2Me(); event.preventDefault(); }; /** @param {Event} event */ var connectHostWithPin = function(event) { - remoting.connectHostWithPin(); + remoting.connectMe2MeWithPin(); event.preventDefault(); }; var doAuthRedirect = function() { diff --git a/remoting/webapp/me2mom/remoting.js b/remoting/webapp/me2mom/remoting.js index b06686f..e0d843d 100644 --- a/remoting/webapp/me2mom/remoting.js +++ b/remoting/webapp/me2mom/remoting.js @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -56,7 +56,7 @@ remoting.init = function() { if ('mode' in urlParams) { if (urlParams['mode'] == 'me2me') { var hostId = urlParams['hostId']; - remoting.connectHost(hostId, true); + remoting.connectMe2Me(hostId, true); return; } } diff --git a/remoting/webapp/me2mom/wcs.js b/remoting/webapp/me2mom/wcs.js index 5898733..a6e5049 100644 --- a/remoting/webapp/me2mom/wcs.js +++ b/remoting/webapp/me2mom/wcs.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2011 The Chromium Authors. All rights reserved. +/* Copyright (c) 2012 The Chromium Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -18,17 +18,12 @@ remoting.wcs = null; /** * @constructor - * * @param {remoting.WcsIqClient} wcsIqClient The WCS client. * @param {string} token An OAuth2 access token. * @param {function(boolean): void} onReady A function called when the WCS * client has received a full JID. - * @param {function(function(string): void): void} tokenRefresh A function - * called when this object wants to see whether an updated access token is - * available. The passed function will be called asynchronously with a - * (possibly updated) access token. */ -remoting.Wcs = function(wcsIqClient, token, onReady, tokenRefresh) { +remoting.Wcs = function(wcsIqClient, token, onReady) { /** * The WCS client. * @type {remoting.WcsIqClient} @@ -67,8 +62,10 @@ remoting.Wcs = function(wcsIqClient, token, onReady, tokenRefresh) { this.pollForUpdatedToken_ = setInterval( function() { /** @param {string} token */ - var setToken = function(token) { that.setToken_(token); } - tokenRefresh(setToken); + var updateAccessToken = function(token) { + that.updateAccessToken_(token); + } + remoting.oauth2.callWithToken(updateAccessToken); }, 60 * 1000); @@ -95,7 +92,7 @@ remoting.Wcs = function(wcsIqClient, token, onReady, tokenRefresh) { * @return {void} Nothing. * @private */ -remoting.Wcs.prototype.setToken_ = function(tokenNew) { +remoting.Wcs.prototype.updateAccessToken_ = function(tokenNew) { if (tokenNew != this.token_) { this.token_ = tokenNew; this.wcsIqClient_.updateAccessToken(this.token_); diff --git a/remoting/webapp/me2mom/wcs_loader.js b/remoting/webapp/me2mom/wcs_loader.js index 1d15924..54fd3cc 100644 --- a/remoting/webapp/me2mom/wcs_loader.js +++ b/remoting/webapp/me2mom/wcs_loader.js @@ -1,4 +1,4 @@ -/* Copyright (c) 2011 The Chromium Authors. All rights reserved. +/* Copyright (c) 2012 The Chromium Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -19,55 +19,36 @@ remoting.wcsLoader = null; /** * @constructor + * @private */ remoting.WcsLoader = function() { /** - * An OAuth2 access token. - * @type {string} - * @private - */ - this.token_ = ''; - - /** - * A callback that gets an updated access token asynchronously. - * @param {function(string): void} setToken The function to call when the - * token is available. - * @private - */ - this.tokenRefresh_ = function(setToken) {}; - - /** - * The function called when WCS is ready, or on error. - * @param {boolean} success Whether or not the load succeeded. - * @private - */ - this.onReady_ = function(success) {}; - - /** - * @enum {string} - * @private - */ - this.LoadState_ = { - NOT_STARTED: 'NOT_STARTED', - STARTED: 'STARTED', - READY: 'READY' - }; - - /** - * The state of WCS loading. - * @type {string} - * @private - */ - this.loadState_ = this.LoadState_.NOT_STARTED; - - /** - * The WCS client that will be downloaded. + * The WCS client that will be downloaded. This variable is initialized (via + * remoting.wcsLoader) by the downloaded Javascript. * @type {remoting.WcsIqClient} */ this.wcsIqClient = null; }; /** + * Load WCS if necessary, then invoke the callback with an access token. + * + * @param {function(string?): void} onReady The callback function, called with + * an OAuth2 access token when WCS has been loaded, or with null on error. + * @return {void} Nothing. + */ +remoting.WcsLoader.load = function(onReady) { + if (!remoting.wcsLoader) { + remoting.wcsLoader = new remoting.WcsLoader(); + } + /** @param {string} token The OAuth2 access token. */ + var start = function(token) { + remoting.wcsLoader.start_(token, onReady); + }; + remoting.oauth2.callWithToken(start); +}; + +/** * The URL of the GTalk gadget. * @type {string} * @private @@ -76,94 +57,76 @@ remoting.WcsLoader.prototype.TALK_GADGET_URL_ = 'https://talkgadget.google.com/talkgadget/'; /** - * Starts loading the WCS IQ client aynchronously. If an OAuth2 token is - * already available, this is equivalent to the start method; if not, then - * the client will be loaded after an OAuth token exchange has occurred. - * - * @param {function(function(string): void): void} tokenRefresh - * Gets a (possibly updated) access token asynchronously. - * @param {function(boolean): void} onReady If the WCS connection is not yet - * ready, then |onReady| will be called with a true parameter when it is - * ready, or with a false parameter on error. - * @return {void} Nothing. + * The id of the script node. + * @type {string} + * @private */ -remoting.WcsLoader.prototype.startAsync = function(tokenRefresh, onReady) { - /** @type {remoting.WcsLoader} */ - var that = this; - /** @param {string} token The OAuth2 access token. */ - var start = function(token) { - that.start(token, tokenRefresh, onReady); - }; - remoting.oauth2.callWithToken(start); -}; +remoting.WcsLoader.prototype.SCRIPT_NODE_ID_ = 'wcs-script-node'; + +/** + * The attribute name indicating that the WCS has finished loading. + * @type {string} + * @private + */ +remoting.WcsLoader.prototype.SCRIPT_NODE_LOADED_FLAG_ = 'wcs-script-loaded'; /** * Starts loading the WCS IQ client. * * When it's loaded, construct remoting.wcs as a wrapper for it. - * When the WCS connection is ready, call |onReady|. - * - * No guarantee is made about what will happen if this function is called more - * than once. + * When the WCS connection is ready, or on error, call |onReady|. * * @param {string} token An OAuth2 access token. - * @param {function(function(string): void): void} tokenRefresh - * Gets a (possibly updated) access token asynchronously. - * @param {function(boolean): void} onReady If the WCS connection is not yet - * ready, then |onReady| will be called with a true parameter when it is - * ready, or with a false parameter on error. + * @param {function(string?): void} onReady The callback function, called with + * an OAuth2 access token when WCS has been loaded, or with null on error. * @return {void} Nothing. + * @private */ -remoting.WcsLoader.prototype.start = function(token, tokenRefresh, onReady) { - this.token_ = token; - this.tokenRefresh_ = tokenRefresh; - this.onReady_ = onReady; - if (this.loadState_ == this.LoadState_.READY) { - this.onReady_(true); - return; - } - if (this.loadState_ == this.LoadState_.STARTED) { +remoting.WcsLoader.prototype.start_ = function(token, onReady) { + var node = document.getElementById(this.SCRIPT_NODE_ID_); + if (!node) { + // The first time, there will be no script node, so create one. + node = document.createElement('script'); + node.id = this.SCRIPT_NODE_ID_; + node.src = this.TALK_GADGET_URL_ + 'iq?access_token=' + token; + node.type = 'text/javascript'; + document.body.insertBefore(node, document.body.firstChild); + } else if (node.hasAttribute(this.SCRIPT_NODE_LOADED_FLAG_)) { + // Subsequently, explicitly invoke onReady if onload has already fired. + // TODO(jamiewalch): It's possible that the WCS client has not finished + // initializing. Add support for multiple callbacks to the remoting.Wcs + // class to address this. + onReady(token); return; } - this.loadState_ = this.LoadState_.STARTED; - var node = document.createElement('script'); - node.src = this.TALK_GADGET_URL_ + 'iq?access_token=' + this.token_; - node.type = 'text/javascript'; /** @type {remoting.WcsLoader} */ var that = this; - node.onload = function() { that.constructWcs_(); }; - node.onerror = function() { that.onReady_(false); }; - document.body.insertBefore(node, document.body.firstChild); - return; + var onLoad = function() { + var typedNode = /** @type {Element} */ (node); + typedNode.setAttribute(that.SCRIPT_NODE_LOADED_FLAG_, true); + that.constructWcs_(token, onReady); + }; + var onError = function() { + var typedNode = /** @type {Element} */ (node); + typedNode.parentNode.removeChild(node); + onReady(null); + }; + node.addEventListener('load', onLoad, false); + node.addEventListener('error', onError, false); }; /** * Constructs the remoting.wcs object. * + * @param {string} token An OAuth2 access token. + * @param {function(string?): void} onReady The callback function, called with + * an OAuth2 access token when WCS has been loaded, or with null on error. * @return {void} Nothing. * @private */ -remoting.WcsLoader.prototype.constructWcs_ = function() { - /** @type {remoting.WcsLoader} */ - var that = this; - /** @param {function(string): void} setToken The function to call when the - token is available. */ - var tokenRefresh = function(setToken) { that.tokenRefresh_(setToken); }; +remoting.WcsLoader.prototype.constructWcs_ = function(token, onReady) { remoting.wcs = new remoting.Wcs( remoting.wcsLoader.wcsIqClient, - this.token_, - function() { that.onWcsReady_(); }, - tokenRefresh); -}; - -/** - * Notifies this object that WCS is ready. - * - * @return {void} Nothing. - * @private - */ -remoting.WcsLoader.prototype.onWcsReady_ = function() { - this.loadState_ = this.LoadState_.READY; - this.onReady_(true); - this.onReady_ = function(success) {}; + token, + function() { onReady(token); }); }; |