diff options
author | kelvinp <kelvinp@chromium.org> | 2015-03-06 19:48:53 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-07 03:49:39 +0000 |
commit | a78cad4e6288048d9a74eadf3dca66247f31fa62 (patch) | |
tree | e56782ed365ebad3df29cabcde702efaaeba29c9 /remoting/webapp | |
parent | 0e71dffca1885161e052d5c9fd3afed5c8aab5e1 (diff) | |
download | chromium_src-a78cad4e6288048d9a74eadf3dca66247f31fa62.zip chromium_src-a78cad4e6288048d9a74eadf3dca66247f31fa62.tar.gz chromium_src-a78cad4e6288048d9a74eadf3dca66247f31fa62.tar.bz2 |
[Webapp Refactor] Remove It2Me connection code from SessionConnectorImpl.
The SessionConnector is intended be a core component that is shared between
app remoting and desktop remoting. It2Me is desktop remoting specific and
should not be there.
This CL pulls out all the It2Me connection logic into It2MeConnectFlow.
BUG=464614
Review URL: https://codereview.chromium.org/984793003
Cr-Commit-Position: refs/heads/master@{#319552}
Diffstat (limited to 'remoting/webapp')
-rw-r--r-- | remoting/webapp/crd/js/crd_connect.js | 7 | ||||
-rw-r--r-- | remoting/webapp/crd/js/it2me_connect_flow.js | 149 | ||||
-rw-r--r-- | remoting/webapp/crd/js/session_connector.js | 10 | ||||
-rw-r--r-- | remoting/webapp/crd/js/session_connector_impl.js | 146 |
4 files changed, 175 insertions, 137 deletions
diff --git a/remoting/webapp/crd/js/crd_connect.js b/remoting/webapp/crd/js/crd_connect.js index ea4f7a8..e02d2d3 100644 --- a/remoting/webapp/crd/js/crd_connect.js +++ b/remoting/webapp/crd/js/crd_connect.js @@ -19,7 +19,12 @@ remoting.connectIT2Me = function() { var connector = remoting.app.getSessionConnector(); var accessCode = document.getElementById('access-code-entry').value; remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); - connector.connectIT2Me(accessCode); + remoting.It2MeConnectFlow.start(connector, accessCode). + catch(function(reason){ + var errorDiv = document.getElementById('connect-error-message'); + l10n.localizeElementFromTag(errorDiv, /** @type {string} */ (reason)); + remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME); + }); }; /** diff --git a/remoting/webapp/crd/js/it2me_connect_flow.js b/remoting/webapp/crd/js/it2me_connect_flow.js new file mode 100644 index 0000000..00dabf3 --- /dev/null +++ b/remoting/webapp/crd/js/it2me_connect_flow.js @@ -0,0 +1,149 @@ +// Copyright 2015 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. + +/** @suppress {duplicate} */ +var remoting = remoting || {}; + +(function() { + +'use strict'; + +// Length of the various components of the access code in number of digits. +var SUPPORT_ID_LENGTH = 7; +var HOST_SECRET_LENGTH = 5; +var ACCESS_CODE_LENGTH = SUPPORT_ID_LENGTH + HOST_SECRET_LENGTH; + +/** + * @param {remoting.SessionConnector} sessionConnector + * @constructor + * @private + */ +remoting.It2MeConnectFlow = function(sessionConnector) { + /** @private */ + this.sessionConnector_ = sessionConnector; + /** @private */ + this.hostId_ = ''; + /** @private */ + this.passCode_ = ''; +}; + +/** + * Initiates an IT2Me connection. + * + * @param {remoting.SessionConnector} connector + * @param {string} accessCode + * @return {Promise} Promise that resolves when the connection is initiated. + * Rejects with remoting.Error on failure. + */ +remoting.It2MeConnectFlow.start = function(connector, accessCode) { + var instance = new remoting.It2MeConnectFlow(connector); + return instance.connect_(accessCode); +}; + +/** + * @param {string} accessCode The access code as entered by the user. + * @return {Promise} Promise that resolves when the connection is initiated. + * Rejects with remoting.Error on failure. + * @private + */ +remoting.It2MeConnectFlow.prototype.connect_ = function(accessCode) { + var that = this; + + return this.verifyAccessCode_(accessCode).then(function() { + return remoting.identity.getToken(); + }).then(function(/** string */ token) { + return that.getHostInfo_(token); + }).then(function(/** XMLHttpRequest */ xhr) { + return that.onHostInfo_(xhr); + }).then(function(/** remoting.Host */ host) { + that.sessionConnector_.connect( + remoting.DesktopConnectedView.Mode.IT2ME, + host, + new remoting.CredentialsProvider({ accessCode: that.passCode_ })); + }).catch(function(error) { + return Promise.reject(/** remoting.Error */ error); + }); +}; + +/** + * @param {string} accessCode + * @return {Promise} Promise that resolves if the access code is valid. + * @private + */ +remoting.It2MeConnectFlow.prototype.verifyAccessCode_ = function(accessCode) { + var normalizedAccessCode = accessCode.replace(/\s/g, ''); + if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) { + return Promise.reject(remoting.Error.INVALID_ACCESS_CODE); + } + + this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH); + this.passCode_ = normalizedAccessCode; + + return Promise.resolve(); +}; + +/** + * Continues an IT2Me connection once an access token has been obtained. + * + * @param {string} token An OAuth2 access token. + * @return {Promise<XMLHttpRequest>} + * @private + */ +remoting.It2MeConnectFlow.prototype.getHostInfo_ = function(token) { + var that = this; + return new Promise(function(resolve) { + remoting.xhr.start({ + method: 'GET', + url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + + encodeURIComponent(that.hostId_), + onDone: resolve, + oauthToken: token + }); + }); +}; + +/** + * Continues an IT2Me connection once the host JID has been looked up. + * + * @param {XMLHttpRequest} xhr The server response to the support-hosts query. + * @return {!Promise<!remoting.Host>} Rejects on error. + * @private + */ +remoting.It2MeConnectFlow.prototype.onHostInfo_ = function(xhr) { + if (xhr.status == 200) { + var response = /** @type {{data: {jabberId: string, publicKey: string}}} */ + (base.jsonParseSafe(xhr.responseText)); + if (response && response.data && + response.data.jabberId && response.data.publicKey) { + var host = new remoting.Host(); + host.hostId = this.hostId_; + host.jabberId = response.data.jabberId; + host.publicKey = response.data.publicKey; + host.hostName = response.data.jabberId.split('/')[0]; + return Promise.resolve(host); + } else { + console.error('Invalid "support-hosts" response from server.'); + return Promise.reject(remoting.Error.UNEXPECTED); + } + } else { + return Promise.reject(translateSupportHostsError(xhr.status)); + } +}; + +/** + * @param {number} error An HTTP error code returned by the support-hosts + * endpoint. + * @return {remoting.Error} The equivalent remoting.Error code. + */ +function translateSupportHostsError(error) { + switch (error) { + case 0: return remoting.Error.NETWORK_FAILURE; + case 404: return remoting.Error.INVALID_ACCESS_CODE; + case 502: // No break + case 503: return remoting.Error.SERVICE_UNAVAILABLE; + default: return remoting.Error.UNEXPECTED; + } +} + +})(); diff --git a/remoting/webapp/crd/js/session_connector.js b/remoting/webapp/crd/js/session_connector.js index df6cf34..ea2abe6 100644 --- a/remoting/webapp/crd/js/session_connector.js +++ b/remoting/webapp/crd/js/session_connector.js @@ -74,13 +74,15 @@ remoting.SessionConnector.prototype.updatePairingInfo = function(clientId, sharedSecret) {}; /** - * Initiate an IT2Me connection. + * Initiates a remote connection. * - * @param {string} accessCode The access code as entered by the user. + * @param {remoting.DesktopConnectedView.Mode} mode + * @param {remoting.Host} host + * @param {remoting.CredentialsProvider} credentialsProvider * @return {void} Nothing. */ -remoting.SessionConnector.prototype.connectIT2Me = - function(accessCode) {}; +remoting.SessionConnector.prototype.connect = + function(mode, host, credentialsProvider) {}; /** * Reconnect a closed connection. diff --git a/remoting/webapp/crd/js/session_connector_impl.js b/remoting/webapp/crd/js/session_connector_impl.js index da0e9a8..4059396 100644 --- a/remoting/webapp/crd/js/session_connector_impl.js +++ b/remoting/webapp/crd/js/session_connector_impl.js @@ -55,9 +55,6 @@ remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError, /** @private {string} */ this.defaultRemapKeys_ = defaultRemapKeys; - /** @private {string} */ - this.clientJid_ = ''; - /** @private {remoting.DesktopConnectedView.Mode} */ this.connectionMode_ = remoting.DesktopConnectedView.Mode.ME2ME; @@ -90,10 +87,7 @@ remoting.SessionConnectorImpl.prototype.reset = function() { /** @private {remoting.ClientSession} */ this.clientSession_ = null; - /** @private {XMLHttpRequest} */ - this.pendingXhr_ = null; - - /** @type {remoting.CredentialsProvider} */ + /** @private {remoting.CredentialsProvider} */ this.credentialsProvider_ = null; }; @@ -115,14 +109,14 @@ remoting.SessionConnectorImpl.prototype.reset = function() { remoting.SessionConnectorImpl.prototype.connectMe2Me = function(host, fetchPin, fetchThirdPartyToken, clientPairingId, clientPairedSecret) { - this.connectionMode_ = remoting.DesktopConnectedView.Mode.ME2ME; this.logHostOfflineErrors_ = false; var credentialsProvider = new remoting.CredentialsProvider({ fetchPin: fetchPin, pairingInfo: { id: clientPairingId, secret: clientPairedSecret }, fetchThirdPartyToken: fetchThirdPartyToken }); - this.connectInternal_(host, credentialsProvider); + this.connect( + remoting.DesktopConnectedView.Mode.ME2ME, host, credentialsProvider); }; /** @@ -135,9 +129,9 @@ remoting.SessionConnectorImpl.prototype.connectMe2Me = * @return {void} Nothing. */ remoting.SessionConnectorImpl.prototype.retryConnectMe2Me = function(host) { - this.connectionMode_ = remoting.DesktopConnectedView.Mode.ME2ME; this.logHostOfflineErrors_ = true; - this.connectInternal_(host, this.credentialsProvider_); + this.connect(remoting.DesktopConnectedView.Mode.ME2ME, host, + this.credentialsProvider_); }; /** @@ -157,7 +151,9 @@ remoting.SessionConnectorImpl.prototype.connectMe2App = var credentialsProvider = new remoting.CredentialsProvider({ fetchThirdPartyToken : fetchThirdPartyToken }); - this.connectInternal_(host, credentialsProvider); + this.connect( + remoting.DesktopConnectedView.Mode.APP_REMOTING, host, + credentialsProvider); }; /** @@ -176,51 +172,23 @@ remoting.SessionConnectorImpl.prototype.updatePairingInfo = /** * Initiates a connection. * + * @param {remoting.DesktopConnectedView.Mode} mode * @param {remoting.Host} host the Host to connect to. * @param {remoting.CredentialsProvider} credentialsProvider * @return {void} Nothing. * @private */ -remoting.SessionConnectorImpl.prototype.connectInternal_ = - function(host, credentialsProvider) { +remoting.SessionConnectorImpl.prototype.connect = + function(mode, host, credentialsProvider) { // Cancel any existing connect operation. this.cancel(); - + this.connectionMode_ = mode; this.host_ = host; this.credentialsProvider_ = credentialsProvider; this.connectSignaling_(); }; /** - * Initiate an IT2Me connection. - * - * @param {string} accessCode The access code as entered by the user. - * @return {void} Nothing. - */ -remoting.SessionConnectorImpl.prototype.connectIT2Me = function(accessCode) { - var kSupportIdLen = 7; - var kHostSecretLen = 5; - var kAccessCodeLen = kSupportIdLen + kHostSecretLen; - - // Cancel any existing connect operation. - this.cancel(); - - var normalizedAccessCode = this.normalizeAccessCode_(accessCode); - if (normalizedAccessCode.length != kAccessCodeLen) { - this.onError_(remoting.Error.INVALID_ACCESS_CODE); - return; - } - var hostId = normalizedAccessCode.substring(0, kSupportIdLen); - this.credentialsProvider_ = new remoting.CredentialsProvider({ - accessCode: normalizedAccessCode - }); - this.connectionMode_ = remoting.DesktopConnectedView.Mode.IT2ME; - remoting.identity.getToken().then( - this.connectIT2MeWithToken_.bind(this, hostId), - remoting.Error.handler(this.onError_)); -}; - -/** * Reconnect a closed connection. * * @return {void} Nothing. @@ -231,7 +199,7 @@ remoting.SessionConnectorImpl.prototype.reconnect = function() { return; } this.logHostOfflineErrors_ = false; - this.connectInternal_(this.host_, this.credentialsProvider_); + this.connect(this.connectionMode_, this.host_, this.credentialsProvider_); }; /** @@ -242,10 +210,6 @@ remoting.SessionConnectorImpl.prototype.cancel = function() { this.clientSession_.removePlugin(); this.clientSession_ = null; } - if (this.pendingXhr_) { - this.pendingXhr_.abort(); - this.pendingXhr_ = null; - } this.reset(); }; @@ -327,56 +291,6 @@ remoting.SessionConnectorImpl.prototype.onSignalingState_ = function(state) { }; /** - * Continue an IT2Me connection once an access token has been obtained. - * - * @param {string} hostId - * @param {string} token An OAuth2 access token. - * @return {void} Nothing. - * @private - */ -remoting.SessionConnectorImpl.prototype.connectIT2MeWithToken_ = - function(hostId, token) { - // Resolve the host id to get the host JID. - this.pendingXhr_ = remoting.xhr.start({ - method: 'GET', - url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + - encodeURIComponent(hostId), - onDone: this.onIT2MeHostInfo_.bind(this, hostId), - oauthToken: token - }); -}; - -/** - * Continue an IT2Me connection once the host JID has been looked up. - * - * @param {string} hostId - * @param {XMLHttpRequest} xhr The server response to the support-hosts query. - * @return {void} Nothing. - * @private - */ -remoting.SessionConnectorImpl.prototype.onIT2MeHostInfo_ = - function(hostId, xhr) { - this.pendingXhr_ = null; - if (xhr.status == 200) { - var host = /** @type {{data: {jabberId: string, publicKey: string}}} */ - (base.jsonParseSafe(xhr.responseText)); - if (host && host.data && host.data.jabberId && host.data.publicKey) { - this.host_ = new remoting.Host(); - this.host_.hostId = hostId; - this.host_.jabberId = host.data.jabberId; - this.host_.publicKey = host.data.publicKey; - this.host_.hostName = host.data.jabberId.split('/')[0]; - this.connectSignaling_(); - return; - } else { - console.error('Invalid "support-hosts" response from server.'); - } - } else { - this.onError_(this.translateSupportHostsError_(xhr.status)); - } -}; - -/** * Creates ClientSession object. */ remoting.SessionConnectorImpl.prototype.createSession_ = function() { @@ -472,42 +386,10 @@ remoting.SessionConnectorImpl.prototype.onStateChange_ = function(event) { }; /** - * @param {number} error An HTTP error code returned by the support-hosts - * endpoint. - * @return {remoting.Error} The equivalent remoting.Error code. - * @private - */ -remoting.SessionConnectorImpl.prototype.translateSupportHostsError_ = - function(error) { - switch (error) { - case 0: return remoting.Error.NETWORK_FAILURE; - case 404: return remoting.Error.INVALID_ACCESS_CODE; - case 502: // No break - case 503: return remoting.Error.SERVICE_UNAVAILABLE; - default: return remoting.Error.UNEXPECTED; - } -}; - -/** - * Normalize the access code entered by the user. - * - * @param {string} accessCode The access code, as entered by the user. - * @return {string} The normalized form of the code (whitespace removed). - * @private - */ -remoting.SessionConnectorImpl.prototype.normalizeAccessCode_ = - function(accessCode) { - // Trim whitespace. - return accessCode.replace(/\s/g, ''); -}; - - -/** * @constructor * @implements {remoting.SessionConnectorFactory} */ -remoting.DefaultSessionConnectorFactory = function() { -}; +remoting.DefaultSessionConnectorFactory = function() {}; /** * @param {HTMLElement} clientContainer Container element for the client view. |