Skip to content

Commit

Permalink
Move hello message from It2MeService to It2MeChannel
Browse files Browse the repository at this point in the history
Currently, Hangouts sends a hello message to the webapp before establishing a long-lived connection.
After this CL, Hangouts will establish the connection first and then send a hello message.  If the
webapp is not installed, Hangouts will receive a disconnect event on the port.  If the webapp is
installed, Hangouts will receive a hello response with the list of supported features.

Review URL: https://codereview.chromium.org/473073002

Cr-Commit-Position: refs/heads/master@{#290127}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@290127 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kelvinp@chromium.org committed Aug 16, 2014
1 parent 8003867 commit c4f1833
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 54 deletions.
27 changes: 24 additions & 3 deletions remoting/webapp/background/it2me_helper_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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------ |
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 0 additions & 38 deletions remoting/webapp/background/it2me_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand All @@ -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.<string,*>}} 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.
Expand Down
10 changes: 10 additions & 0 deletions remoting/webapp/unittests/it2me_helper_channel_unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions remoting/webapp/unittests/it2me_service_unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

})();

0 comments on commit c4f1833

Please sign in to comment.