Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
|
Jamie
2015/04/07 21:20:44
Rename this file to match the class it defines.
kelvinp
2015/04/07 22:48:19
Done.
| |
| 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 /** @suppress {duplicate} */ | 5 /** @suppress {duplicate} */ |
| 6 var remoting = remoting || {}; | 6 var remoting = remoting || {}; |
| 7 | 7 |
| 8 (function() { | 8 (function() { |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 // Length of the various components of the access code in number of digits. | 12 // Length of the various components of the access code in number of digits. |
| 13 var SUPPORT_ID_LENGTH = 7; | 13 var SUPPORT_ID_LENGTH = 7; |
| 14 var HOST_SECRET_LENGTH = 5; | 14 var HOST_SECRET_LENGTH = 5; |
| 15 var ACCESS_CODE_LENGTH = SUPPORT_ID_LENGTH + HOST_SECRET_LENGTH; | 15 var ACCESS_CODE_LENGTH = SUPPORT_ID_LENGTH + HOST_SECRET_LENGTH; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * @param {remoting.SessionConnector} sessionConnector | 18 * @param {remoting.SessionConnector} sessionConnector |
| 19 * @constructor | 19 * @constructor |
| 20 * @implements {remoting.Activity} | |
| 20 */ | 21 */ |
| 21 remoting.It2MeConnectFlow = function(sessionConnector) { | 22 remoting.It2MeActivity = function(sessionConnector) { |
| 22 /** @private */ | 23 /** @private */ |
| 23 this.sessionConnector_ = sessionConnector; | 24 this.sessionConnector_ = sessionConnector; |
| 24 /** @private */ | 25 /** @private */ |
| 25 this.hostId_ = ''; | 26 this.hostId_ = ''; |
| 26 /** @private */ | 27 /** @private */ |
| 27 this.passCode_ = ''; | 28 this.passCode_ = ''; |
| 28 | 29 |
| 29 var form = document.getElementById('access-code-form'); | 30 var form = document.getElementById('access-code-form'); |
| 30 /** @private */ | 31 /** @private */ |
| 31 this.accessCodeDialog_ = new remoting.InputDialog( | 32 this.accessCodeDialog_ = new remoting.InputDialog( |
| 32 remoting.AppMode.CLIENT_UNCONNECTED, | 33 remoting.AppMode.CLIENT_UNCONNECTED, |
| 33 form, | 34 form, |
| 34 form.querySelector('#access-code-entry'), | 35 form.querySelector('#access-code-entry'), |
| 35 form.querySelector('#cancel-access-code-button')); | 36 form.querySelector('#cancel-access-code-button')); |
| 36 }; | 37 }; |
| 37 | 38 |
| 39 remoting.It2MeActivity.prototype.dispose = function() {}; | |
| 38 | 40 |
| 39 remoting.It2MeConnectFlow.prototype.start = function() { | 41 remoting.It2MeActivity.prototype.start = function() { |
| 40 var that = this; | 42 var that = this; |
| 41 | 43 |
| 42 this.accessCodeDialog_.show().then(function(/** string */ accessCode) { | 44 this.accessCodeDialog_.show().then(function(/** string */ accessCode) { |
| 43 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | 45 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| 44 return that.verifyAccessCode_(accessCode); | 46 return that.verifyAccessCode_(accessCode); |
| 45 }).then(function() { | 47 }).then(function() { |
| 46 return remoting.identity.getToken(); | 48 return remoting.identity.getToken(); |
| 47 }).then(function(/** string */ token) { | 49 }).then(function(/** string */ token) { |
| 48 return that.getHostInfo_(token); | 50 return that.getHostInfo_(token); |
| 49 }).then(function(/** !remoting.Xhr.Response */ response) { | 51 }).then(function(/** !remoting.Xhr.Response */ response) { |
| 50 return that.onHostInfo_(response); | 52 return that.onHostInfo_(response); |
| 51 }).then(function(/** remoting.Host */ host) { | 53 }).then(function(/** remoting.Host */ host) { |
| 52 that.sessionConnector_.connect( | 54 that.sessionConnector_.connect( |
| 53 remoting.Application.Mode.IT2ME, | 55 remoting.Application.Mode.IT2ME, |
| 54 host, | 56 host, |
| 55 new remoting.CredentialsProvider({ accessCode: that.passCode_ })); | 57 new remoting.CredentialsProvider({ accessCode: that.passCode_ })); |
| 56 }).catch(function(/** remoting.Error */ error) { | 58 }).catch(function(/** remoting.Error */ error) { |
| 57 if (error.hasTag(remoting.Error.Tag.CANCELLED)) { | 59 if (error.hasTag(remoting.Error.Tag.CANCELLED)) { |
| 58 remoting.setMode(remoting.AppMode.HOME); | 60 remoting.setMode(remoting.AppMode.HOME); |
| 59 } else { | 61 } else { |
| 60 var errorDiv = document.getElementById('connect-error-message'); | 62 var errorDiv = document.getElementById('connect-error-message'); |
| 61 l10n.localizeElementFromTag(errorDiv, error.getTag()); | 63 l10n.localizeElementFromTag(errorDiv, error.getTag()); |
| 62 remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME); | 64 remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME); |
| 63 } | 65 } |
| 64 }); | 66 }); |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 /** | 69 /** |
| 70 * @param {!remoting.Error} error | |
| 71 */ | |
| 72 remoting.It2MeActivity.prototype.onConnectionFailed = function(error) { | |
| 73 this.onError(error); | |
| 74 }; | |
| 75 | |
| 76 /** | |
| 77 * @param {!remoting.ConnectionInfo} connectionInfo | |
| 78 */ | |
| 79 remoting.It2MeActivity.prototype.onConnected = function(connectionInfo) { | |
| 80 this.accessCodeDialog_.inputField().value = ''; | |
| 81 }; | |
| 82 | |
| 83 remoting.It2MeActivity.prototype.onDisconnected = function() { | |
| 84 remoting.setMode(remoting.AppMode.CLIENT_SESSION_FINISHED_IT2ME); | |
| 85 }; | |
| 86 | |
| 87 /** | |
| 88 * @param {!remoting.Error} error | |
| 89 */ | |
| 90 remoting.It2MeActivity.prototype.onError = function(error) { | |
| 91 var errorDiv = document.getElementById('connect-error-message'); | |
| 92 l10n.localizeElementFromTag(errorDiv, error.getTag()); | |
| 93 remoting.setMode(remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME); | |
| 94 }; | |
| 95 | |
| 96 | |
| 97 /** | |
| 68 * @param {string} accessCode | 98 * @param {string} accessCode |
| 69 * @return {Promise} Promise that resolves if the access code is valid. | 99 * @return {Promise} Promise that resolves if the access code is valid. |
| 70 * @private | 100 * @private |
| 71 */ | 101 */ |
| 72 remoting.It2MeConnectFlow.prototype.verifyAccessCode_ = function(accessCode) { | 102 remoting.It2MeActivity.prototype.verifyAccessCode_ = function(accessCode) { |
| 73 var normalizedAccessCode = accessCode.replace(/\s/g, ''); | 103 var normalizedAccessCode = accessCode.replace(/\s/g, ''); |
| 74 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) { | 104 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) { |
| 75 return Promise.reject(new remoting.Error( | 105 return Promise.reject(new remoting.Error( |
| 76 remoting.Error.Tag.INVALID_ACCESS_CODE)); | 106 remoting.Error.Tag.INVALID_ACCESS_CODE)); |
| 77 } | 107 } |
| 78 | 108 |
| 79 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH); | 109 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH); |
| 80 this.passCode_ = normalizedAccessCode; | 110 this.passCode_ = normalizedAccessCode; |
| 81 | 111 |
| 82 return Promise.resolve(); | 112 return Promise.resolve(); |
| 83 }; | 113 }; |
| 84 | 114 |
| 85 /** | 115 /** |
| 86 * Continues an IT2Me connection once an access token has been obtained. | 116 * Continues an IT2Me connection once an access token has been obtained. |
| 87 * | 117 * |
| 88 * @param {string} token An OAuth2 access token. | 118 * @param {string} token An OAuth2 access token. |
| 89 * @return {Promise<!remoting.Xhr.Response>} | 119 * @return {Promise<!remoting.Xhr.Response>} |
| 90 * @private | 120 * @private |
| 91 */ | 121 */ |
| 92 remoting.It2MeConnectFlow.prototype.getHostInfo_ = function(token) { | 122 remoting.It2MeActivity.prototype.getHostInfo_ = function(token) { |
| 93 var that = this; | 123 var that = this; |
| 94 return new remoting.Xhr({ | 124 return new remoting.Xhr({ |
| 95 method: 'GET', | 125 method: 'GET', |
| 96 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + | 126 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + |
| 97 encodeURIComponent(that.hostId_), | 127 encodeURIComponent(that.hostId_), |
| 98 oauthToken: token | 128 oauthToken: token |
| 99 }).start(); | 129 }).start(); |
| 100 }; | 130 }; |
| 101 | 131 |
| 102 /** | 132 /** |
| 103 * Continues an IT2Me connection once the host JID has been looked up. | 133 * Continues an IT2Me connection once the host JID has been looked up. |
| 104 * | 134 * |
| 105 * @param {!remoting.Xhr.Response} xhrResponse The server response to the | 135 * @param {!remoting.Xhr.Response} xhrResponse The server response to the |
| 106 * support-hosts query. | 136 * support-hosts query. |
| 107 * @return {!Promise<!remoting.Host>} Rejects on error. | 137 * @return {!Promise<!remoting.Host>} Rejects on error. |
| 108 * @private | 138 * @private |
| 109 */ | 139 */ |
| 110 remoting.It2MeConnectFlow.prototype.onHostInfo_ = function(xhrResponse) { | 140 remoting.It2MeActivity.prototype.onHostInfo_ = function(xhrResponse) { |
| 111 if (xhrResponse.status == 200) { | 141 if (xhrResponse.status == 200) { |
| 112 var response = /** @type {{data: {jabberId: string, publicKey: string}}} */ | 142 var response = /** @type {{data: {jabberId: string, publicKey: string}}} */ |
| 113 (base.jsonParseSafe(xhrResponse.getText())); | 143 (base.jsonParseSafe(xhrResponse.getText())); |
| 114 if (response && response.data && | 144 if (response && response.data && |
| 115 response.data.jabberId && response.data.publicKey) { | 145 response.data.jabberId && response.data.publicKey) { |
| 116 var host = new remoting.Host(this.hostId_); | 146 var host = new remoting.Host(this.hostId_); |
| 117 host.jabberId = response.data.jabberId; | 147 host.jabberId = response.data.jabberId; |
| 118 host.publicKey = response.data.publicKey; | 148 host.publicKey = response.data.publicKey; |
| 119 host.hostName = response.data.jabberId.split('/')[0]; | 149 host.hostName = response.data.jabberId.split('/')[0]; |
| 120 return Promise.resolve(host); | 150 return Promise.resolve(host); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 137 switch (error) { | 167 switch (error) { |
| 138 case 0: return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | 168 case 0: return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
| 139 case 404: return new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | 169 case 404: return new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); |
| 140 case 502: // No break | 170 case 502: // No break |
| 141 case 503: return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | 171 case 503: return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
| 142 default: return remoting.Error.unexpected(); | 172 default: return remoting.Error.unexpected(); |
| 143 } | 173 } |
| 144 } | 174 } |
| 145 | 175 |
| 146 })(); | 176 })(); |
| OLD | NEW |