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

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: 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
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 var SUPPORT_ID_LENGTH = 7;
garykac 2015/03/06 22:27:27 Could you add a comment here describing what these
kelvinp 2015/03/06 23:33:51 Done.
13 var HOST_SECRET_LENGTH = 5;
14 var ACCESS_CODE_LENGTH = SUPPORT_ID_LENGTH + HOST_SECRET_LENGTH;
15
16 /**
17 * @param {remoting.SessionConnector} sessionConnector
18 * @constructor
19 * @private
20 */
21 remoting.It2MeConnectFlow = function(sessionConnector) {
22 /** @private */
23 this.sessionConnector_ = sessionConnector;
24 /** @private */
25 this.hostId_ = '';
26 /** @private */
27 this.passCode_ = '';
28 };
29
30 /**
31 * Initiates an IT2Me connection.
32 *
33 * @param {remoting.SessionConnector} connector
34 * @param {string} accessCode
35 * @return {Promise} Promise that resolves when the connection is initiated.
36 */
37 remoting.It2MeConnectFlow.start = function(connector, accessCode) {
38 var instance = new remoting.It2MeConnectFlow(connector);
39 return instance.connect_(accessCode);
40 };
41
42 /**
43 * @param {string} accessCode The access code as entered by the user.
44 * @return {Promise} Promise that resolves when the connection is initiated.
45 * @private
46 */
47 remoting.It2MeConnectFlow.prototype.connect_ = function(accessCode) {
48 var that = this;
49
50 return this.verifyAccessCode_(accessCode).then(function() {
51 return remoting.identity.getToken();
52 }).then(function(/** string */ token) {
53 return that.getHostInfo_(token);
54 }).then(function(/** XMLHttpRequest */ xhr) {
55 return that.onHostInfo_(xhr);
56 }).then(function(/** remoting.Host */ host) {
57 that.sessionConnector_.connect(
58 remoting.DesktopConnectedView.Mode.IT2ME,
59 host,
60 new remoting.CredentialsProvider({ accessCode: that.passCode_ }));
61 });
garykac 2015/03/06 22:27:27 If this rejects() is will pass a remoting.Error, b
kelvinp 2015/03/06 23:33:51 Done.
62 };
63
64 /**
65 * @param {string} accessCode
66 * @return {Promise} Promise that resolves if the access code is valid.
67 * @private
68 */
69 remoting.It2MeConnectFlow.prototype.verifyAccessCode_ = function(accessCode) {
70 var normalizedAccessCode = accessCode.replace(/\s/g, '');
71 if (normalizedAccessCode.length !== ACCESS_CODE_LENGTH) {
72 return Promise.reject(remoting.Error.INVALID_ACCESS_CODE);
73 }
74
75 this.hostId_ = normalizedAccessCode.substring(0, SUPPORT_ID_LENGTH);
76 this.passCode_ = normalizedAccessCode;
77
78 return Promise.resolve();
79 };
80
81 /**
82 * Continues an IT2Me connection once an access token has been obtained.
83 *
84 * @param {string} token An OAuth2 access token.
85 * @return {Promise<XMLHttpRequest>}
86 * @private
87 */
88 remoting.It2MeConnectFlow.prototype.getHostInfo_ = function(token) {
89 var that = this;
90 return new Promise(function(resolve) {
91 remoting.xhr.start({
92 method: 'GET',
93 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' +
94 encodeURIComponent(that.hostId_),
95 onDone: resolve,
96 oauthToken: token
97 });
98 });
99 };
100
101 /**
102 * Continues an IT2Me connection once the host JID has been looked up.
103 *
104 * @param {XMLHttpRequest} xhr The server response to the support-hosts query.
105 * @return {!Promise<!remoting.Host>} Rejects on error.
106 * @private
107 */
108 remoting.It2MeConnectFlow.prototype.onHostInfo_ = function(xhr) {
109 if (xhr.status == 200) {
110 var response = /** @type {{data: {jabberId: string, publicKey: string}}} */
111 (base.jsonParseSafe(xhr.responseText));
112 if (response && response.data &&
113 response.data.jabberId && response.data.publicKey) {
114 var host = new remoting.Host();
115 host.hostId = this.hostId_;
116 host.jabberId = response.data.jabberId;
117 host.publicKey = response.data.publicKey;
118 host.hostName = response.data.jabberId.split('/')[0];
119 return Promise.resolve(host);
120 } else {
121 console.error('Invalid "support-hosts" response from server.');
122 return Promise.reject(remoting.Error.UNEXPECTED);
123 }
124 } else {
125 return Promise.reject(translateSupportHostsError(xhr.status));
126 }
127 };
128
129 /**
130 * @param {number} error An HTTP error code returned by the support-hosts
131 * endpoint.
132 * @return {remoting.Error} The equivalent remoting.Error code.
133 */
134 function translateSupportHostsError(error) {
135 switch (error) {
136 case 0: return remoting.Error.NETWORK_FAILURE;
137 case 404: return remoting.Error.INVALID_ACCESS_CODE;
138 case 502: // No break
139 case 503: return remoting.Error.SERVICE_UNAVAILABLE;
140 default: return remoting.Error.UNEXPECTED;
141 }
142 }
143
144 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698