Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / remoting / webapp / background / it2me_helpee_channel.js
blob94001883bbbbed43d4b417d0d03214d8e3ef916d
1 // Copyright 2014 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.
5 /**
6 * @fileoverview
8 * It2MeHelpeeChannel relays messages between the Hangouts web page (Hangouts)
9 * and the It2Me Native Messaging Host (It2MeHost) for the helpee (the Hangouts
10 * participant who is receiving remoting assistance).
12 * It runs in the background page. It contains a chrome.runtime.Port object,
13 * representing a connection to Hangouts, and a remoting.It2MeHostFacade object,
14 * representing a connection to the IT2Me Native Messaging Host.
16 * Hangouts It2MeHelpeeChannel It2MeHost
17 * |---------runtime.connect()-------->| |
18 * |-----------hello message---------->| |
19 * |<-----helloResponse message------->| |
20 * |----------connect message--------->| |
21 * | |-----showConfirmDialog()----->|
22 * | |----------connect()---------->|
23 * | |<-------hostStateChanged------|
24 * | | (RECEIVED_ACCESS_CODE) |
25 * |<---connect response (access code)-| |
26 * | | |
28 * Hangouts will send the access code to the web app on the helper side.
29 * The helper will then connect to the It2MeHost using the access code.
31 * Hangouts It2MeHelpeeChannel It2MeHost
32 * | |<-------hostStateChanged------|
33 * | | (CONNECTED) |
34 * |<-- hostStateChanged(CONNECTED)----| |
35 * |-------disconnect message--------->| |
36 * |<--hostStateChanged(DISCONNECTED)--| |
39 * It also handles host downloads and install status queries:
41 * Hangouts It2MeHelpeeChannel
42 * |------isHostInstalled message----->|
43 * |<-isHostInstalled response(false)--|
44 * | |
45 * |--------downloadHost message------>|
46 * | |
47 * |------isHostInstalled message----->|
48 * |<-isHostInstalled response(false)--|
49 * | |
50 * |------isHostInstalled message----->|
51 * |<-isHostInstalled response(true)---|
54 'use strict';
56 /** @suppress {duplicate} */
57 var remoting = remoting || {};
59 /**
60 * @param {chrome.runtime.Port} hangoutPort
61 * @param {remoting.It2MeHostFacade} host
62 * @param {remoting.HostInstaller} hostInstaller
63 * @param {function()} onDisposedCallback Callback to notify the client when
64 * the connection is torn down.
66 * @constructor
67 * @implements {base.Disposable}
69 remoting.It2MeHelpeeChannel =
70 function(hangoutPort, host, hostInstaller, onDisposedCallback) {
71 /**
72 * @type {chrome.runtime.Port}
73 * @private
75 this.hangoutPort_ = hangoutPort;
77 /**
78 * @type {remoting.It2MeHostFacade}
79 * @private
81 this.host_ = host;
83 /**
84 * @type {?remoting.HostInstaller}
85 * @private
87 this.hostInstaller_ = hostInstaller;
89 /**
90 * @type {remoting.HostSession.State}
91 * @private
93 this.hostState_ = remoting.HostSession.State.UNKNOWN;
95 /**
96 * @type {?function()}
97 * @private
99 this.onDisposedCallback_ = onDisposedCallback;
101 this.onHangoutMessageRef_ = this.onHangoutMessage_.bind(this);
102 this.onHangoutDisconnectRef_ = this.onHangoutDisconnect_.bind(this);
105 /** @enum {string} */
106 remoting.It2MeHelpeeChannel.HangoutMessageTypes = {
107 CONNECT: 'connect',
108 CONNECT_RESPONSE: 'connectResponse',
109 DISCONNECT: 'disconnect',
110 DOWNLOAD_HOST: 'downloadHost',
111 ERROR: 'error',
112 HELLO: 'hello',
113 HELLO_RESPONSE: 'helloResponse',
114 HOST_STATE_CHANGED: 'hostStateChanged',
115 IS_HOST_INSTALLED: 'isHostInstalled',
116 IS_HOST_INSTALLED_RESPONSE: 'isHostInstalledResponse'
119 /** @enum {string} */
120 remoting.It2MeHelpeeChannel.Features = {
121 REMOTE_ASSISTANCE: 'remoteAssistance'
124 remoting.It2MeHelpeeChannel.prototype.init = function() {
125 this.hangoutPort_.onMessage.addListener(this.onHangoutMessageRef_);
126 this.hangoutPort_.onDisconnect.addListener(this.onHangoutDisconnectRef_);
129 remoting.It2MeHelpeeChannel.prototype.dispose = function() {
130 if (this.host_ !== null) {
131 this.host_.unhookCallbacks();
132 this.host_.disconnect();
133 this.host_ = null;
136 if (this.hangoutPort_ !== null) {
137 this.hangoutPort_.onMessage.removeListener(this.onHangoutMessageRef_);
138 this.hangoutPort_.onDisconnect.removeListener(this.onHangoutDisconnectRef_);
139 this.hostState_ = remoting.HostSession.State.DISCONNECTED;
141 try {
142 var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
143 this.hangoutPort_.postMessage({
144 method: MessageTypes.HOST_STATE_CHANGED,
145 state: this.hostState_
147 } catch (e) {
148 // |postMessage| throws if |this.hangoutPort_| is disconnected
149 // It is safe to ignore the exception.
151 this.hangoutPort_.disconnect();
152 this.hangoutPort_ = null;
155 if (this.onDisposedCallback_ !== null) {
156 this.onDisposedCallback_();
157 this.onDisposedCallback_ = null;
162 * Message Handler for incoming runtime messages from Hangouts.
164 * @param {{method:string, data:Object.<string,*>}} message
165 * @private
167 remoting.It2MeHelpeeChannel.prototype.onHangoutMessage_ = function(message) {
168 try {
169 var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
170 switch (message.method) {
171 case MessageTypes.HELLO:
172 this.hangoutPort_.postMessage({
173 method: MessageTypes.HELLO_RESPONSE,
174 supportedFeatures: base.values(remoting.It2MeHelpeeChannel.Features)
176 return true;
177 case MessageTypes.IS_HOST_INSTALLED:
178 this.handleIsHostInstalled_(message);
179 return true;
180 case MessageTypes.DOWNLOAD_HOST:
181 this.handleDownloadHost_(message);
182 return true;
183 case MessageTypes.CONNECT:
184 this.handleConnect_(message);
185 return true;
186 case MessageTypes.DISCONNECT:
187 this.dispose();
188 return true;
190 throw new Error('Unsupported message method=' + message.method);
191 } catch(e) {
192 var error = /** @type {Error} */ e;
193 this.sendErrorResponse_(message, error.message);
195 return false;
199 * Queries the |hostInstaller| for the installation status.
201 * @param {{method:string, data:Object.<string,*>}} message
202 * @private
204 remoting.It2MeHelpeeChannel.prototype.handleIsHostInstalled_ =
205 function(message) {
206 /** @type {remoting.It2MeHelpeeChannel} */
207 var that = this;
209 /** @param {boolean} installed */
210 function sendResponse(installed) {
211 var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
212 that.hangoutPort_.postMessage({
213 method: MessageTypes.IS_HOST_INSTALLED_RESPONSE,
214 result: installed
218 this.hostInstaller_.isInstalled().then(
219 sendResponse,
220 this.sendErrorResponse_.bind(this, message)
225 * @param {{method:string, data:Object.<string,*>}} message
226 * @private
228 remoting.It2MeHelpeeChannel.prototype.handleDownloadHost_ = function(message) {
229 try {
230 this.hostInstaller_.download();
231 } catch (e) {
232 var error = /** @type {Error} */ e;
233 this.sendErrorResponse_(message, error.message);
238 * Disconnect the session if the |hangoutPort| gets disconnected.
239 * @private
241 remoting.It2MeHelpeeChannel.prototype.onHangoutDisconnect_ = function() {
242 this.dispose();
246 * Connects to the It2Me Native messaging Host and retrieves the access code.
248 * @param {{method:string, data:Object.<string,*>}} message
249 * @private
251 remoting.It2MeHelpeeChannel.prototype.handleConnect_ =
252 function(message) {
253 var email = getStringAttr(message, 'email');
255 if (!email) {
256 throw new Error('Missing required parameter: email');
259 if (this.hostState_ !== remoting.HostSession.State.UNKNOWN) {
260 throw new Error('An existing connection is in progress.');
263 this.showConfirmDialog_().then(
264 this.initializeHost_.bind(this)
265 ).then(
266 this.fetchOAuthToken_.bind(this)
267 ).then(
268 this.connectToHost_.bind(this, email),
269 this.sendErrorResponse_.bind(this, message)
274 * Prompts the user before starting the It2Me Native Messaging Host. This
275 * ensures that even if Hangouts is compromised, an attacker cannot start the
276 * host without explicit user confirmation.
278 * @return {Promise} A promise that resolves when the user clicks accept on the
279 * dialog.
280 * @private
282 remoting.It2MeHelpeeChannel.prototype.showConfirmDialog_ = function() {
284 * @param {function(*=):void} resolve
285 * @param {function(*=):void} reject
287 return new Promise(function(resolve, reject) {
288 // TODO(kelvinp): The existing implementation doesn't work in the v2 app as
289 // window.confirm is blacklisted. Implement the dialog using
290 // chrome.app.window instead (crbug.com/405139).
291 if (base.isAppsV2()) {
292 resolve();
293 // The unlocalized string will be replaced in (crbug.com/405139).
294 } else if (window.confirm('Accept help?')) {
295 resolve();
296 } else {
297 reject(new Error(remoting.Error.CANCELLED));
303 * @return {Promise} A promise that resolves when the host is initialized.
304 * @private
306 remoting.It2MeHelpeeChannel.prototype.initializeHost_ = function() {
307 /** @type {remoting.It2MeHostFacade} */
308 var host = this.host_;
311 * @param {function(*=):void} resolve
312 * @param {function(*=):void} reject
314 return new Promise(function(resolve, reject) {
315 if (host.initialized()) {
316 resolve();
317 } else {
318 host.initialize(resolve, reject);
324 * TODO(kelvinp): The existing implementation only works in the v2 app
325 * We need to implement token fetching for the v1 app using remoting.OAuth2
326 * before launch (crbug.com/405130).
328 * @return {Promise} Promise that resolves with the OAuth token as the value.
330 remoting.It2MeHelpeeChannel.prototype.fetchOAuthToken_ = function() {
331 if (!base.isAppsV2()) {
332 throw new Error('fetchOAuthToken_ is not implemented in the v1 app.');
336 * @param {function(*=):void} resolve
338 return new Promise(function(resolve){
339 chrome.identity.getAuthToken({ 'interactive': false }, resolve);
344 * Connects to the It2Me Native Messaging Host and retrieves the access code
345 * in the |onHostStateChanged_| callback.
347 * @param {string} email
348 * @param {string} accessToken
349 * @private
351 remoting.It2MeHelpeeChannel.prototype.connectToHost_ =
352 function(email, accessToken) {
353 base.debug.assert(this.host_.initialized());
354 this.host_.connect(
355 email,
356 'oauth2:' + accessToken,
357 this.onHostStateChanged_.bind(this),
358 base.doNothing, // Ignore |onNatPolicyChanged|.
359 console.log.bind(console), // Forward logDebugInfo to console.log.
360 remoting.settings.XMPP_SERVER_ADDRESS,
361 remoting.settings.XMPP_SERVER_USE_TLS,
362 remoting.settings.DIRECTORY_BOT_JID,
363 this.onHostConnectError_);
367 * @param {remoting.Error} error
368 * @private
370 remoting.It2MeHelpeeChannel.prototype.onHostConnectError_ = function(error) {
371 this.sendErrorResponse_(null, error);
375 * @param {remoting.HostSession.State} state
376 * @private
378 remoting.It2MeHelpeeChannel.prototype.onHostStateChanged_ = function(state) {
379 this.hostState_ = state;
380 var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
381 var HostState = remoting.HostSession.State;
383 switch (state) {
384 case HostState.RECEIVED_ACCESS_CODE:
385 var accessCode = this.host_.getAccessCode();
386 this.hangoutPort_.postMessage({
387 method: MessageTypes.CONNECT_RESPONSE,
388 accessCode: accessCode
390 break;
391 case HostState.CONNECTED:
392 case HostState.DISCONNECTED:
393 this.hangoutPort_.postMessage({
394 method: MessageTypes.HOST_STATE_CHANGED,
395 state: state
397 break;
398 case HostState.ERROR:
399 this.sendErrorResponse_(null, remoting.Error.UNEXPECTED);
400 break;
401 case HostState.INVALID_DOMAIN_ERROR:
402 this.sendErrorResponse_(null, remoting.Error.INVALID_HOST_DOMAIN);
403 break;
404 default:
405 // It is safe to ignore other state changes.
410 * @param {?{method:string, data:Object.<string,*>}} incomingMessage
411 * @param {string|Error} error
412 * @private
414 remoting.It2MeHelpeeChannel.prototype.sendErrorResponse_ =
415 function(incomingMessage, error) {
416 if (error instanceof Error) {
417 error = error.message;
420 console.error('Error responding to message method:' +
421 (incomingMessage ? incomingMessage.method : 'null') +
422 ' error:' + error);
423 this.hangoutPort_.postMessage({
424 method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.ERROR,
425 message: error,
426 request: incomingMessage