diff --git a/remoting/webapp/background/it2me_helper_channel.js b/remoting/webapp/background/it2me_helper_channel.js index e25c443b6a64cd..e039783273b6c4 100644 --- a/remoting/webapp/background/it2me_helper_channel.js +++ b/remoting/webapp/background/it2me_helper_channel.js @@ -10,13 +10,20 @@ * assistance). * * It runs in the background page and contains two chrome.runtime.Port objects, - * respresenting connections to the webapp and hangout, respectively. + * representing connections to the webapp and hangout, respectively. * - * Connection is always initiated from Hangouts. + * Connection is always initiated from Hangouts by calling + * var port = chrome.runtime.connect({name:'it2me.helper.hangout'}, extId). + * port.postMessage('hello') + * If the webapp is not installed, |port.onDisconnect| will fire. + * If the webapp is installed, Hangouts will receive a hello response with the + * list of supported features. * * Hangout It2MeHelperChannel Chrome Remote Desktop * |-----runtime.connect() ------>| | - * |------connect message-------->| | + * |--------hello message-------->| | + * | |<-----helloResponse message-----| + * |-------connect message------->| | * | |-------appLauncher.launch()---->| * | |<------runtime.connect()------- | * | |<-----sessionStateChanged------ | @@ -109,10 +116,17 @@ remoting.It2MeHelperChannel = /** @enum {string} */ remoting.It2MeHelperChannel.HangoutMessageTypes = { + HELLO: 'hello', + HELLO_RESPONSE: 'helloResponse', CONNECT: 'connect', DISCONNECT: 'disconnect' }; +/** @enum {string} */ +remoting.It2MeHelperChannel.Features = { + REMOTE_ASSISTANCE: 'remoteAssistance' +}; + /** @enum {string} */ remoting.It2MeHelperChannel.WebappMessageTypes = { SESSION_STATE_CHANGED: 'sessionStateChanged' @@ -143,7 +157,14 @@ remoting.It2MeHelperChannel.prototype.onHangoutMessage_ = function(message) { case MessageTypes.DISCONNECT: this.closeWebapp_(message); return true; + case MessageTypes.HELLO: + this.hangoutPort_.postMessage({ + method: MessageTypes.HELLO_RESPONSE, + supportedFeatures: base.values(remoting.It2MeHelperChannel.Features) + }); + return true; } + throw new Error('Unknown message method=' + message.method); } catch(e) { var error = /** @type {Error} */ e; console.error(error); diff --git a/remoting/webapp/background/it2me_service.js b/remoting/webapp/background/it2me_service.js index 9656864577fa8d..93cb9a9e132354 100644 --- a/remoting/webapp/background/it2me_service.js +++ b/remoting/webapp/background/it2me_service.js @@ -35,7 +35,6 @@ remoting.It2MeService = function(appLauncher) { this.helpee_ = null; this.onWebappConnectRef_ = this.onWebappConnect_.bind(this); - this.onMessageExternalRef_ = this.onMessageExternal_.bind(this); this.onConnectExternalRef_ = this.onConnectExternal_.bind(this); }; @@ -51,52 +50,15 @@ remoting.It2MeService.ConnectionTypes = { */ remoting.It2MeService.prototype.init = function() { chrome.runtime.onConnect.addListener(this.onWebappConnectRef_); - chrome.runtime.onMessageExternal.addListener(this.onMessageExternalRef_); chrome.runtime.onConnectExternal.addListener(this.onConnectExternalRef_); }; remoting.It2MeService.prototype.dispose = function() { chrome.runtime.onConnect.removeListener(this.onWebappConnectRef_); - chrome.runtime.onMessageExternal.removeListener( - this.onMessageExternalRef_); chrome.runtime.onConnectExternal.removeListener( this.onConnectExternalRef_); }; -/** - * This function is called when a runtime message is received from an external - * web page (hangout) or extension. - * - * @param {{method:string, data:Object.}} message - * @param {chrome.runtime.MessageSender} sender - * @param {function(*):void} sendResponse - * @private - */ -remoting.It2MeService.prototype.onMessageExternal_ = - function(message, sender, sendResponse) { - try { - var method = message.method; - if (method == 'hello') { - // The hello message is used by hangouts to detect whether the app is - // installed and what features are supported. - sendResponse({ - method: 'helloResponse', - supportedFeatures: ['it2me'] - }); - return true; - } - throw new Error('Unknown method: ' + method); - } catch (e) { - var error = /** @type {Error} */ e; - console.error(error); - sendResponse({ - method: message.method + 'Response', - error: error.message - }); - } - return false; -}; - /** * This function is called when Hangouts connects via chrome.runtime.connect. * Only web pages that are white-listed in the manifest are allowed to connect. diff --git a/remoting/webapp/unittests/it2me_helper_channel_unittest.js b/remoting/webapp/unittests/it2me_helper_channel_unittest.js index b5511c33058c9c..f0a0fa230ea088 100644 --- a/remoting/webapp/unittests/it2me_helper_channel_unittest.js +++ b/remoting/webapp/unittests/it2me_helper_channel_unittest.js @@ -61,6 +61,16 @@ function promiseResolveSynchronous(value) { }; } +test('onHangoutMessage_("hello") should return supportedFeatures', function() { + hangoutPort.onMessage.mock$fire( + { method: remoting.It2MeHelperChannel.HangoutMessageTypes.HELLO }); + + sinon.assert.calledWith(hangoutPort.postMessage, { + method: remoting.It2MeHelperChannel.HangoutMessageTypes.HELLO_RESPONSE, + supportedFeatures: base.values(remoting.It2MeHelperChannel.Features) + }); +}); + test('onHangoutMessage_(|connect|) should launch the webapp', function() { sinon.assert.called(appLauncher.launch); diff --git a/remoting/webapp/unittests/it2me_service_unittest.js b/remoting/webapp/unittests/it2me_service_unittest.js index a5924262d1a282..1bd66f5c7f9485 100644 --- a/remoting/webapp/unittests/it2me_service_unittest.js +++ b/remoting/webapp/unittests/it2me_service_unittest.js @@ -129,17 +129,4 @@ test('should reject unknown connection', function() { sinon.assert.called(randomPort.disconnect); }); -test('messageExternal("hello") should return supportedFeatures', function() { - var response = null; - function callback(msg) { - response = msg; - } - - it2meService.onMessageExternal_({ - method: 'hello' - }, null, callback); - - QUnit.ok(response.supportedFeatures instanceof Array); -}); - })(); \ No newline at end of file