-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathit2me_helpee_channel.js
494 lines (454 loc) · 15.3 KB
/
it2me_helpee_channel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2014 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.
/**
* @fileoverview
*
* It2MeHelpeeChannel relays messages between the Hangouts web page (Hangouts)
* and the It2Me Native Messaging Host (It2MeHost) for the helpee (the Hangouts
* participant who is receiving remoting assistance).
*
* It runs in the background page. It contains a chrome.runtime.Port object,
* representing a connection to Hangouts, and a remoting.It2MeHostFacade object,
* representing a connection to the IT2Me Native Messaging Host.
*
* Hangouts It2MeHelpeeChannel It2MeHost
* |---------runtime.connect()-------->| |
* |-----------hello message---------->| |
* |<-----helloResponse message------->| |
* |----------connect message--------->| |
* | |-----showConfirmDialog()----->|
* | |----------connect()---------->|
* | |<-------hostStateChanged------|
* | | (RECEIVED_ACCESS_CODE) |
* |<---connect response (access code)-| |
* | | |
*
* Hangouts will send the access code to the web app on the helper side.
* The helper will then connect to the It2MeHost using the access code.
*
* Hangouts It2MeHelpeeChannel It2MeHost
* | |<-------hostStateChanged------|
* | | (CONNECTED) |
* |<-- hostStateChanged(CONNECTED)----| |
* |-------disconnect message--------->| |
* |<--hostStateChanged(DISCONNECTED)--| |
*
*
* It also handles host downloads and install status queries:
*
* Hangouts It2MeHelpeeChannel
* |------isHostInstalled message----->|
* |<-isHostInstalled response(false)--|
* | |
* |--------downloadHost message------>|
* | |
* |------isHostInstalled message----->|
* |<-isHostInstalled response(false)--|
* | |
* |------isHostInstalled message----->|
* |<-isHostInstalled response(true)---|
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* @param {chrome.runtime.Port} hangoutPort
* @param {remoting.It2MeHostFacade} host
* @param {remoting.HostInstaller} hostInstaller
* @param {function()} onDisposedCallback Callback to notify the client when
* the connection is torn down.
*
* @constructor
* @implements {base.Disposable}
*/
remoting.It2MeHelpeeChannel =
function(hangoutPort, host, hostInstaller, onDisposedCallback) {
/**
* @type {chrome.runtime.Port}
* @private
*/
this.hangoutPort_ = hangoutPort;
/**
* @type {remoting.It2MeHostFacade}
* @private
*/
this.host_ = host;
/**
* @type {?remoting.HostInstaller}
* @private
*/
this.hostInstaller_ = hostInstaller;
/**
* @type {remoting.HostSession.State}
* @private
*/
this.hostState_ = remoting.HostSession.State.UNKNOWN;
/**
* @type {?function()}
* @private
*/
this.onDisposedCallback_ = onDisposedCallback;
this.onHangoutMessageRef_ = this.onHangoutMessage_.bind(this);
this.onHangoutDisconnectRef_ = this.onHangoutDisconnect_.bind(this);
};
/** @enum {string} */
remoting.It2MeHelpeeChannel.HangoutMessageTypes = {
CONNECT: 'connect',
CONNECT_RESPONSE: 'connectResponse',
DISCONNECT: 'disconnect',
DOWNLOAD_HOST: 'downloadHost',
ERROR: 'error',
HELLO: 'hello',
HELLO_RESPONSE: 'helloResponse',
HOST_STATE_CHANGED: 'hostStateChanged',
IS_HOST_INSTALLED: 'isHostInstalled',
IS_HOST_INSTALLED_RESPONSE: 'isHostInstalledResponse'
};
/** @enum {string} */
remoting.It2MeHelpeeChannel.Features = {
REMOTE_ASSISTANCE: 'remoteAssistance'
};
remoting.It2MeHelpeeChannel.prototype.init = function() {
this.hangoutPort_.onMessage.addListener(this.onHangoutMessageRef_);
this.hangoutPort_.onDisconnect.addListener(this.onHangoutDisconnectRef_);
};
remoting.It2MeHelpeeChannel.prototype.dispose = function() {
if (this.host_ !== null) {
this.host_.unhookCallbacks();
this.host_.disconnect();
this.host_ = null;
}
if (this.hangoutPort_ !== null) {
this.hangoutPort_.onMessage.removeListener(this.onHangoutMessageRef_);
this.hangoutPort_.onDisconnect.removeListener(this.onHangoutDisconnectRef_);
this.hostState_ = remoting.HostSession.State.DISCONNECTED;
try {
var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
this.hangoutPort_.postMessage({
method: MessageTypes.HOST_STATE_CHANGED,
state: this.hostState_
});
} catch (e) {
// |postMessage| throws if |this.hangoutPort_| is disconnected
// It is safe to ignore the exception.
}
this.hangoutPort_.disconnect();
this.hangoutPort_ = null;
}
if (this.onDisposedCallback_ !== null) {
this.onDisposedCallback_();
this.onDisposedCallback_ = null;
}
};
/**
* Message Handler for incoming runtime messages from Hangouts.
*
* @param {{method:string, data:Object<string,*>}} message
* @private
*/
remoting.It2MeHelpeeChannel.prototype.onHangoutMessage_ = function(message) {
try {
var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
switch (message.method) {
case MessageTypes.HELLO:
this.hangoutPort_.postMessage({
method: MessageTypes.HELLO_RESPONSE,
supportedFeatures: base.values(remoting.It2MeHelpeeChannel.Features)
});
return true;
case MessageTypes.IS_HOST_INSTALLED:
this.handleIsHostInstalled_(message);
return true;
case MessageTypes.DOWNLOAD_HOST:
this.handleDownloadHost_(message);
return true;
case MessageTypes.CONNECT:
this.handleConnect_(message);
return true;
case MessageTypes.DISCONNECT:
this.dispose();
return true;
}
throw new Error('Unsupported message method=' + message.method);
} catch(/** @type {Error} */ error) {
this.sendErrorResponse_(message, error.message);
}
return false;
};
/**
* Queries the |hostInstaller| for the installation status.
*
* @param {{method:string, data:Object<string,*>}} message
* @private
*/
remoting.It2MeHelpeeChannel.prototype.handleIsHostInstalled_ =
function(message) {
/** @type {remoting.It2MeHelpeeChannel} */
var that = this;
/** @param {boolean} installed */
function sendResponse(installed) {
var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
that.hangoutPort_.postMessage({
method: MessageTypes.IS_HOST_INSTALLED_RESPONSE,
result: installed
});
}
remoting.HostInstaller.isInstalled().then(
sendResponse,
/** @type {function(*):void} */(this.sendErrorResponse_.bind(this, message))
);
};
/**
* @param {{method:string, data:Object<string,*>}} message
* @private
*/
remoting.It2MeHelpeeChannel.prototype.handleDownloadHost_ = function(message) {
try {
this.hostInstaller_.download();
} catch (/** @type {*} */ e) {
var error = /** @type {Error} */ (e);
this.sendErrorResponse_(message, error.message);
}
};
/**
* Disconnect the session if the |hangoutPort| gets disconnected.
* @private
*/
remoting.It2MeHelpeeChannel.prototype.onHangoutDisconnect_ = function() {
this.dispose();
};
/**
* Connects to the It2Me Native messaging Host and retrieves the access code.
*
* @param {{method:string, data:Object<string,*>}} message
* @private
*/
remoting.It2MeHelpeeChannel.prototype.handleConnect_ =
function(message) {
var bounds =
/** @type {Bounds} */ (getObjectAttr(message, 'hangoutBounds', null));
if (this.hostState_ !== remoting.HostSession.State.UNKNOWN) {
throw new Error('An existing connection is in progress.');
}
var that = this;
this.showConfirmDialog_(bounds)
.then(this.initializeHost_.bind(this))
.then(this.fetchOAuthToken_.bind(this))
.then(this.fetchEmail_.bind(this))
/** @param {{email:string, token:string}|Promise} result */
.then(function(result) {
that.connectToHost_(result.email, result.token);
/** @param {*} reason */
}).catch(function(reason) {
that.sendErrorResponse_(message, /** @type {Error} */ (reason));
that.dispose();
});
};
/**
* Prompts the user before starting the It2Me Native Messaging Host. This
* ensures that even if Hangouts is compromised, an attacker cannot start the
* host without explicit user confirmation.
*
* @param {Bounds} bounds Bounds of the hangout window
* @return {Promise} A promise that will resolve if the user accepts remote
* assistance or reject otherwise.
* @private
*/
remoting.It2MeHelpeeChannel.prototype.showConfirmDialog_ = function(bounds) {
if (base.isAppsV2()) {
return this.showConfirmDialogV2_(bounds);
} else {
return this.showConfirmDialogV1_();
}
};
/**
* @return {Promise} A promise that will resolve if the user accepts remote
* assistance or reject otherwise.
* @private
*/
remoting.It2MeHelpeeChannel.prototype.showConfirmDialogV1_ = function() {
var messageHeader = l10n.getTranslationOrError(
/*i18n-content*/'HANGOUTS_CONFIRM_DIALOG_MESSAGE_1');
var message1 = l10n.getTranslationOrError(
/*i18n-content*/'HANGOUTS_CONFIRM_DIALOG_MESSAGE_2');
var message2 = l10n.getTranslationOrError(
/*i18n-content*/'HANGOUTS_CONFIRM_DIALOG_MESSAGE_3');
var message = base.escapeHTML(messageHeader) + '\n' +
'- ' + base.escapeHTML(message1) + '\n' +
'- ' + base.escapeHTML(message2) + '\n';
if(window.confirm(message)) {
return Promise.resolve();
} else {
return Promise.reject(new Error(remoting.Error.CANCELLED));
}
};
/**
* @param {Bounds} bounds the bounds of the Hangouts Window. If set, the
* confirm dialog will be centered within |bounds|.
* @return {Promise} A promise that will resolve if the user accepts remote
* assistance or reject otherwise.
* @private
*/
remoting.It2MeHelpeeChannel.prototype.showConfirmDialogV2_ = function(bounds) {
var getToken =
base.Promise.as(chrome.identity.getAuthToken, [{interactive: false}]);
return getToken.then(
/** @param {string} token */
function(token) {
return remoting.HangoutConsentDialog.getInstance().show(Boolean(token),
bounds);
});
};
/**
* @return {Promise} A promise that resolves when the host is initialized.
* @private
*/
remoting.It2MeHelpeeChannel.prototype.initializeHost_ = function() {
/** @type {remoting.It2MeHostFacade} */
var host = this.host_;
/**
* @param {function(*=):void} resolve
* @param {function(*=):void} reject
*/
return new Promise(function(resolve, reject) {
if (host.initialized()) {
resolve(true);
} else {
host.initialize(/** @type {function(*=):void} */ (resolve),
/** @type {function(*=):void} */ (reject));
}
});
};
/**
* @return {Promise<string>} Promise that resolves with the OAuth token as the
* value.
*/
remoting.It2MeHelpeeChannel.prototype.fetchOAuthToken_ = function() {
if (base.isAppsV2()) {
/**
* @param {function(*=):void} resolve
* @param {function(*=):void} reject
*/
return new Promise(function(resolve, reject){
remoting.identity.callWithToken(resolve, reject);
});
} else {
/**
* @param {function(*=):void} resolve
* @param {function(*=):void} reject
*/
return new Promise(function(resolve, reject) {
/** @param {remoting.Error} error */
var onError = function(error) {
if (error === remoting.Error.NOT_AUTHENTICATED) {
remoting.oauth2.doAuthRedirect(function() {
remoting.identity.callWithToken(resolve, reject);
});
return;
}
reject(new Error(remoting.Error.NOT_AUTHENTICATED));
};
remoting.identity.callWithToken(resolve, onError);
});
}
};
/**
* @param {string|Promise} token
* @return {Promise} Promise that resolves with the access token and the email
* of the user.
*/
remoting.It2MeHelpeeChannel.prototype.fetchEmail_ = function(token) {
/**
* @param {function(*=):void} resolve
* @param {function(*=):void} reject
*/
return new Promise(function(resolve, reject){
/** @param {string} email */
function onEmail (email) {
resolve({ email: email, token: token });
}
remoting.identity.getEmail(onEmail, reject);
});
};
/**
* Connects to the It2Me Native Messaging Host and retrieves the access code
* in the |onHostStateChanged_| callback.
*
* @param {string} email
* @param {string} accessToken
* @private
*/
remoting.It2MeHelpeeChannel.prototype.connectToHost_ =
function(email, accessToken) {
base.debug.assert(this.host_.initialized());
this.host_.connect(
email,
'oauth2:' + accessToken,
this.onHostStateChanged_.bind(this),
base.doNothing, // Ignore |onNatPolicyChanged|.
console.log.bind(console), // Forward logDebugInfo to console.log.
remoting.settings.XMPP_SERVER_FOR_IT2ME_HOST,
remoting.settings.XMPP_SERVER_USE_TLS,
remoting.settings.DIRECTORY_BOT_JID,
this.onHostConnectError_);
};
/**
* @param {remoting.Error} error
* @private
*/
remoting.It2MeHelpeeChannel.prototype.onHostConnectError_ = function(error) {
this.sendErrorResponse_(null, error);
};
/**
* @param {remoting.HostSession.State} state
* @private
*/
remoting.It2MeHelpeeChannel.prototype.onHostStateChanged_ = function(state) {
this.hostState_ = state;
var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
var HostState = remoting.HostSession.State;
switch (state) {
case HostState.RECEIVED_ACCESS_CODE:
var accessCode = this.host_.getAccessCode();
this.hangoutPort_.postMessage({
method: MessageTypes.CONNECT_RESPONSE,
accessCode: accessCode
});
break;
case HostState.CONNECTED:
case HostState.DISCONNECTED:
this.hangoutPort_.postMessage({
method: MessageTypes.HOST_STATE_CHANGED,
state: state
});
break;
case HostState.ERROR:
this.sendErrorResponse_(null, remoting.Error.UNEXPECTED);
break;
case HostState.INVALID_DOMAIN_ERROR:
this.sendErrorResponse_(null, remoting.Error.INVALID_HOST_DOMAIN);
break;
default:
// It is safe to ignore other state changes.
}
};
/**
* @param {?{method:string, data:Object<string,*>}} incomingMessage
* @param {string|Error} error
* @private
*/
remoting.It2MeHelpeeChannel.prototype.sendErrorResponse_ =
function(incomingMessage, error) {
if (error instanceof Error) {
error = error.message;
}
console.error('Error responding to message method:' +
(incomingMessage ? incomingMessage.method : 'null') +
' error:' + error);
this.hangoutPort_.postMessage({
method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.ERROR,
message: error,
request: incomingMessage
});
};