| OLD | NEW | 
|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** @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'; | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 47  *     Rejects with remoting.Error on failure. | 47  *     Rejects with remoting.Error on failure. | 
| 48  * @private | 48  * @private | 
| 49  */ | 49  */ | 
| 50 remoting.It2MeConnectFlow.prototype.connect_ = function(accessCode) { | 50 remoting.It2MeConnectFlow.prototype.connect_ = function(accessCode) { | 
| 51   var that = this; | 51   var that = this; | 
| 52 | 52 | 
| 53   return this.verifyAccessCode_(accessCode).then(function() { | 53   return this.verifyAccessCode_(accessCode).then(function() { | 
| 54     return remoting.identity.getToken(); | 54     return remoting.identity.getToken(); | 
| 55   }).then(function(/** string */ token) { | 55   }).then(function(/** string */ token) { | 
| 56     return that.getHostInfo_(token); | 56     return that.getHostInfo_(token); | 
| 57   }).then(function(/** XMLHttpRequest */ xhr) { | 57   }).then(function(/** !remoting.Xhr.Response */ response) { | 
| 58     return that.onHostInfo_(xhr); | 58     return that.onHostInfo_(response); | 
| 59   }).then(function(/** remoting.Host */ host) { | 59   }).then(function(/** remoting.Host */ host) { | 
| 60     that.sessionConnector_.connect( | 60     that.sessionConnector_.connect( | 
| 61         remoting.DesktopConnectedView.Mode.IT2ME, | 61         remoting.DesktopConnectedView.Mode.IT2ME, | 
| 62         host, | 62         host, | 
| 63         new remoting.CredentialsProvider({ accessCode: that.passCode_ })); | 63         new remoting.CredentialsProvider({ accessCode: that.passCode_ })); | 
| 64   }).catch(function(error) { | 64   }).catch(function(error) { | 
| 65     return Promise.reject(/** remoting.Error */ error); | 65     return Promise.reject(/** remoting.Error */ error); | 
| 66   }); | 66   }); | 
| 67 }; | 67 }; | 
| 68 | 68 | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 81   this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH); | 81   this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH); | 
| 82   this.passCode_ = normalizedAccessCode; | 82   this.passCode_ = normalizedAccessCode; | 
| 83 | 83 | 
| 84   return Promise.resolve(); | 84   return Promise.resolve(); | 
| 85 }; | 85 }; | 
| 86 | 86 | 
| 87 /** | 87 /** | 
| 88  * Continues an IT2Me connection once an access token has been obtained. | 88  * Continues an IT2Me connection once an access token has been obtained. | 
| 89  * | 89  * | 
| 90  * @param {string} token An OAuth2 access token. | 90  * @param {string} token An OAuth2 access token. | 
| 91  * @return {Promise<XMLHttpRequest>} | 91  * @return {Promise<!remoting.Xhr.Response>} | 
| 92  * @private | 92  * @private | 
| 93  */ | 93  */ | 
| 94 remoting.It2MeConnectFlow.prototype.getHostInfo_ = function(token) { | 94 remoting.It2MeConnectFlow.prototype.getHostInfo_ = function(token) { | 
| 95   var that = this; | 95   var that = this; | 
| 96   return new Promise(function(resolve) { | 96   return new remoting.Xhr({ | 
| 97     remoting.xhr.start({ | 97     method: 'GET', | 
| 98       method: 'GET', | 98     url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + | 
| 99       url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + | 99         encodeURIComponent(that.hostId_), | 
| 100            encodeURIComponent(that.hostId_), | 100     oauthToken: token | 
| 101       onDone: resolve, | 101   }).start(); | 
| 102       oauthToken: token |  | 
| 103     }); |  | 
| 104   }); |  | 
| 105 }; | 102 }; | 
| 106 | 103 | 
| 107 /** | 104 /** | 
| 108  * Continues an IT2Me connection once the host JID has been looked up. | 105  * Continues an IT2Me connection once the host JID has been looked up. | 
| 109  * | 106  * | 
| 110  * @param {XMLHttpRequest} xhr The server response to the support-hosts query. | 107  * @param {!remoting.Xhr.Response} xhrResponse The server response to the | 
|  | 108  *     support-hosts query. | 
| 111  * @return {!Promise<!remoting.Host>} Rejects on error. | 109  * @return {!Promise<!remoting.Host>} Rejects on error. | 
| 112  * @private | 110  * @private | 
| 113  */ | 111  */ | 
| 114 remoting.It2MeConnectFlow.prototype.onHostInfo_ = function(xhr) { | 112 remoting.It2MeConnectFlow.prototype.onHostInfo_ = function(xhrResponse) { | 
| 115   if (xhr.status == 200) { | 113   if (xhrResponse.status == 200) { | 
| 116     var response = /** @type {{data: {jabberId: string, publicKey: string}}} */ | 114     var response = /** @type {{data: {jabberId: string, publicKey: string}}} */ | 
| 117         (base.jsonParseSafe(xhr.responseText)); | 115         (base.jsonParseSafe(xhrResponse.getText())); | 
| 118     if (response && response.data && | 116     if (response && response.data && | 
| 119         response.data.jabberId && response.data.publicKey) { | 117         response.data.jabberId && response.data.publicKey) { | 
| 120       var host = new remoting.Host(); | 118       var host = new remoting.Host(); | 
| 121       host.hostId = this.hostId_; | 119       host.hostId = this.hostId_; | 
| 122       host.jabberId = response.data.jabberId; | 120       host.jabberId = response.data.jabberId; | 
| 123       host.publicKey = response.data.publicKey; | 121       host.publicKey = response.data.publicKey; | 
| 124       host.hostName = response.data.jabberId.split('/')[0]; | 122       host.hostName = response.data.jabberId.split('/')[0]; | 
| 125       return Promise.resolve(host); | 123       return Promise.resolve(host); | 
| 126     } else { | 124     } else { | 
| 127       console.error('Invalid "support-hosts" response from server.'); | 125       console.error('Invalid "support-hosts" response from server.'); | 
| 128       return Promise.reject(remoting.Error.unexpected()); | 126       return Promise.reject(remoting.Error.unexpected()); | 
| 129     } | 127     } | 
| 130   } else { | 128   } else { | 
| 131     return Promise.reject(translateSupportHostsError(xhr.status)); | 129     return Promise.reject(translateSupportHostsError(xhrResponse.status)); | 
| 132   } | 130   } | 
| 133 }; | 131 }; | 
| 134 | 132 | 
| 135 /** | 133 /** | 
|  | 134  * TODO(jrw): Replace with remoting.Error.fromHttpStatus. | 
| 136  * @param {number} error An HTTP error code returned by the support-hosts | 135  * @param {number} error An HTTP error code returned by the support-hosts | 
| 137  *     endpoint. | 136  *     endpoint. | 
| 138  * @return {remoting.Error} The equivalent remoting.Error code. | 137  * @return {remoting.Error} The equivalent remoting.Error code. | 
| 139  */ | 138  */ | 
| 140 function translateSupportHostsError(error) { | 139 function translateSupportHostsError(error) { | 
| 141   switch (error) { | 140   switch (error) { | 
| 142     case 0: return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | 141     case 0: return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | 
| 143     case 404: return new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | 142     case 404: return new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | 
| 144     case 502: // No break | 143     case 502: // No break | 
| 145     case 503: return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | 144     case 503: return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | 
| 146     default: return remoting.Error.unexpected(); | 145     default: return remoting.Error.unexpected(); | 
| 147   } | 146   } | 
| 148 } | 147 } | 
| 149 | 148 | 
| 150 })(); | 149 })(); | 
| OLD | NEW | 
|---|