Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Side by Side Diff: remoting/webapp/crd/js/it2me_connect_flow.js

Issue 984793003: [Webapp Refactor] Remove It2Me connection code from SessionConnectorImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Ready for Checkin Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/webapp/crd/js/crd_connect.js ('k') | remoting/webapp/crd/js/session_connector.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 /** @suppress {duplicate} */
6 var remoting = remoting || {};
7
8 (function() {
9
10 'use strict';
11
12 // Length of the various components of the access code in number of digits.
13 var SUPPORT_ID_LENGTH = 7;
14 var HOST_SECRET_LENGTH = 5;
15 var ACCESS_CODE_LENGTH = SUPPORT_ID_LENGTH + HOST_SECRET_LENGTH;
16
17 /**
18 * @param {remoting.SessionConnector} sessionConnector
19 * @constructor
20 * @private
21 */
22 remoting.It2MeConnectFlow = function(sessionConnector) {
23 /** @private */
24 this.sessionConnector_ = sessionConnector;
25 /** @private */
26 this.hostId_ = '';
27 /** @private */
28 this.passCode_ = '';
29 };
30
31 /**
32 * Initiates an IT2Me connection.
33 *
34 * @param {remoting.SessionConnector} connector
35 * @param {string} accessCode
36 * @return {Promise} Promise that resolves when the connection is initiated.
37 * Rejects with remoting.Error on failure.
38 */
39 remoting.It2MeConnectFlow.start = function(connector, accessCode) {
40 var instance = new remoting.It2MeConnectFlow(connector);
41 return instance.connect_(accessCode);
42 };
43
44 /**
45 * @param {string} accessCode The access code as entered by the user.
46 * @return {Promise} Promise that resolves when the connection is initiated.
47 * Rejects with remoting.Error on failure.
48 * @private
49 */
50 remoting.It2MeConnectFlow.prototype.connect_ = function(accessCode) {
51 var that = this;
52
53 return this.verifyAccessCode_(accessCode).then(function() {
54 return remoting.identity.getToken();
55 }).then(function(/** string */ token) {
56 return that.getHostInfo_(token);
57 }).then(function(/** XMLHttpRequest */ xhr) {
58 return that.onHostInfo_(xhr);
59 }).then(function(/** remoting.Host */ host) {
60 that.sessionConnector_.connect(
61 remoting.DesktopConnectedView.Mode.IT2ME,
62 host,
63 new remoting.CredentialsProvider({ accessCode: that.passCode_ }));
64 }).catch(function(error) {
65 return Promise.reject(/** remoting.Error */ error);
66 });
67 };
68
69 /**
70 * @param {string} accessCode
71 * @return {Promise} Promise that resolves if the access code is valid.
72 * @private
73 */
74 remoting.It2MeConnectFlow.prototype.verifyAccessCode_ = function(accessCode) {
75 var normalizedAccessCode = accessCode.replace(/\s/g, '');
76 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) {
77 return Promise.reject(remoting.Error.INVALID_ACCESS_CODE);
78 }
79
80 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
81 this.passCode_ = normalizedAccessCode;
82
83 return Promise.resolve();
84 };
85
86 /**
87 * Continues an IT2Me connection once an access token has been obtained.
88 *
89 * @param {string} token An OAuth2 access token.
90 * @return {Promise<XMLHttpRequest>}
91 * @private
92 */
93 remoting.It2MeConnectFlow.prototype.getHostInfo_ = function(token) {
94 var that = this;
95 return new Promise(function(resolve) {
96 remoting.xhr.start({
97 method: 'GET',
98 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' +
99 encodeURIComponent(that.hostId_),
100 onDone: resolve,
101 oauthToken: token
102 });
103 });
104 };
105
106 /**
107 * Continues an IT2Me connection once the host JID has been looked up.
108 *
109 * @param {XMLHttpRequest} xhr The server response to the support-hosts query.
110 * @return {!Promise<!remoting.Host>} Rejects on error.
111 * @private
112 */
113 remoting.It2MeConnectFlow.prototype.onHostInfo_ = function(xhr) {
114 if (xhr.status == 200) {
115 var response = /** @type {{data: {jabberId: string, publicKey: string}}} */
116 (base.jsonParseSafe(xhr.responseText));
117 if (response && response.data &&
118 response.data.jabberId && response.data.publicKey) {
119 var host = new remoting.Host();
120 host.hostId = this.hostId_;
121 host.jabberId = response.data.jabberId;
122 host.publicKey = response.data.publicKey;
123 host.hostName = response.data.jabberId.split('/')[0];
124 return Promise.resolve(host);
125 } else {
126 console.error('Invalid "support-hosts" response from server.');
127 return Promise.reject(remoting.Error.UNEXPECTED);
128 }
129 } else {
130 return Promise.reject(translateSupportHostsError(xhr.status));
131 }
132 };
133
134 /**
135 * @param {number} error An HTTP error code returned by the support-hosts
136 * endpoint.
137 * @return {remoting.Error} The equivalent remoting.Error code.
138 */
139 function translateSupportHostsError(error) {
140 switch (error) {
141 case 0: return remoting.Error.NETWORK_FAILURE;
142 case 404: return remoting.Error.INVALID_ACCESS_CODE;
143 case 502: // No break
144 case 503: return remoting.Error.SERVICE_UNAVAILABLE;
145 default: return remoting.Error.UNEXPECTED;
146 }
147 }
148
149 })();
OLDNEW
« no previous file with comments | « remoting/webapp/crd/js/crd_connect.js ('k') | remoting/webapp/crd/js/session_connector.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698