diff options
author | garykac <garykac@chromium.org> | 2015-03-16 19:40:01 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-17 02:40:42 +0000 |
commit | 1d092a46beaf5a11cf85460782debf48e831d103 (patch) | |
tree | bb5e9d7356dc5882bdaba9010fee461158f5d5c8 /remoting | |
parent | a8cd0cdb5970a9aa14a5c407e05760d07f677226 (diff) | |
download | chromium_src-1d092a46beaf5a11cf85460782debf48e831d103.zip chromium_src-1d092a46beaf5a11cf85460782debf48e831d103.tar.gz chromium_src-1d092a46beaf5a11cf85460782debf48e831d103.tar.bz2 |
[Chromoting] Handle AppRemoting extension messages via ProtocolExtension.
This removes the special handling for 'application' extension messages
and updates AppRemoting to use the recently-added extension mechanism.
BUG=465878
Review URL: https://codereview.chromium.org/1002373002
Cr-Commit-Position: refs/heads/master@{#320850}
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/webapp/app_remoting/js/app_remoting.js | 25 | ||||
-rw-r--r-- | remoting/webapp/base/js/application.js | 36 | ||||
-rw-r--r-- | remoting/webapp/base/js/protocol_extension.js | 18 | ||||
-rw-r--r-- | remoting/webapp/crd/js/cast_extension_handler.js | 26 | ||||
-rw-r--r-- | remoting/webapp/crd/js/desktop_remoting.js | 10 | ||||
-rw-r--r-- | remoting/webapp/crd/js/gnubby_auth_handler.js | 26 | ||||
-rw-r--r-- | remoting/webapp/crd/js/session_connector.js | 5 | ||||
-rw-r--r-- | remoting/webapp/crd/js/session_connector_impl.js | 90 |
8 files changed, 125 insertions, 111 deletions
diff --git a/remoting/webapp/app_remoting/js/app_remoting.js b/remoting/webapp/app_remoting/js/app_remoting.js index 80bb4427..03d5468 100644 --- a/remoting/webapp/app_remoting/js/app_remoting.js +++ b/remoting/webapp/app_remoting/js/app_remoting.js @@ -17,6 +17,7 @@ var remoting = remoting || {}; * @param {remoting.Application} app The main app that owns this delegate. * @constructor * @implements {remoting.Application.Delegate} + * @implements {remoting.ProtocolExtension} */ remoting.AppRemoting = function(app) { app.setDelegate(this); @@ -232,6 +233,8 @@ remoting.AppRemoting.prototype.handleConnected = function(connectionInfo) { JSON.stringify({fullName: userInfo.name})); }); + remoting.app.getSessionConnector().registerProtocolExtension(this); + var clientSession = connectionInfo.session(); // Set up a ping at 10-second intervals to test the connection speed. var CONNECTION_SPEED_PING_INTERVAL = 10 * 1000; @@ -276,15 +279,25 @@ remoting.AppRemoting.prototype.handleVideoStreamingStarted = function() { remoting.LoadingWindow.close(); }; + +/** @return {Array<string>} */ +remoting.AppRemoting.prototype.getExtensionTypes = function() { + return ['openURL', 'onWindowRemoved', 'onWindowAdded', + 'onAllWindowsMinimized', 'setKeyboardLayouts', 'pingResponse']; +}; + /** - * Called when an extension message needs to be handled. - * - * @param {string} type The type of the extension message. + * @param {function(string,string)} sendMessageToHost Callback to send a message + * to the host. + */ +remoting.AppRemoting.prototype.startExtension = function(sendMessageToHost) { +}; + +/** + * @param {string} type The message type. * @param {Object} message The parsed extension message data. - * @return {boolean} True if the extension message was recognized. */ -remoting.AppRemoting.prototype.handleExtensionMessage = function( - type, message) { +remoting.AppRemoting.prototype.onExtensionMessage = function(type, message) { switch (type) { case 'openURL': diff --git a/remoting/webapp/base/js/application.js b/remoting/webapp/base/js/application.js index 008a277..82b6453 100644 --- a/remoting/webapp/base/js/application.js +++ b/remoting/webapp/base/js/application.js @@ -159,31 +159,6 @@ remoting.Application.prototype.onVideoStreamingStarted = function() { }; /** - * Called when an extension message needs to be handled. - * - * @param {string} type The type of the extension message. - * @param {string} data The payload of the extension message. - * @return {boolean} Return true if the extension message was recognized. - */ -remoting.Application.prototype.onExtensionMessage = function(type, data) { - var message = /** @type {Object} */ (base.jsonParseSafe(data)); - if (typeof message != 'object') { - return false; - } - - // Give the delegate a chance to handle this extension message first. - if (this.delegate_.handleExtensionMessage(type, message)) { - return true; - } - - if (remoting.desktopConnectedView) { - return remoting.desktopConnectedView.handleExtensionMessage(type, message); - } - - return false; -}; - -/** * Called when an error needs to be displayed to the user. * * @param {!remoting.Error} errorTag The error to be localized and displayed. @@ -204,7 +179,6 @@ remoting.Application.prototype.getSessionConnector = function() { document.getElementById('client-container'), this.onConnected.bind(this), this.onError.bind(this), - this.onExtensionMessage.bind(this), this.onConnectionFailed.bind(this), this.appCapabilities_, this.delegate_.getDefaultRemapKeys()); @@ -334,16 +308,6 @@ remoting.Application.Delegate.prototype.handleVideoStreamingStarted = function() {}; /** - * Called when an extension message needs to be handled. - * - * @param {string} type The type of the extension message. - * @param {Object} message The parsed extension message data. - * @return {boolean} Return true if the extension message was recognized. - */ -remoting.Application.Delegate.prototype.handleExtensionMessage = - function(type, message) {}; - -/** * Called when an error needs to be displayed to the user. * * @param {!remoting.Error} errorTag The error to be localized and displayed. diff --git a/remoting/webapp/base/js/protocol_extension.js b/remoting/webapp/base/js/protocol_extension.js index d745172..28103f7 100644 --- a/remoting/webapp/base/js/protocol_extension.js +++ b/remoting/webapp/base/js/protocol_extension.js @@ -21,12 +21,12 @@ var remoting = remoting || {}; remoting.ProtocolExtension = function() {}; /** - * The string that identifies the type of the extension. - * All extension messages with this type will be sent to the extension. + * Return a list of the extension message types that this class can handle. + * All extension messages that match these types will be sent to the extension. * - * @return {string} + * @return {Array<string>} */ -remoting.ProtocolExtension.prototype.getType = function() {}; +remoting.ProtocolExtension.prototype.getExtensionTypes = function() {}; /** * Called when the connection has been established to start the extension. @@ -34,13 +34,15 @@ remoting.ProtocolExtension.prototype.getType = function() {}; * @param {function(string,string)} sendMessageToHost Callback to send a message * to the host. */ -remoting.ProtocolExtension.prototype.start = +remoting.ProtocolExtension.prototype.startExtension = function(sendMessageToHost) {}; /** * Called when an extension message of a matching type is received. * - * @param {string} message + * @param {string} type The message type. + * @param {Object} message The parsed extension message data. + * @return {boolean} True if the extension message was handled. */ -remoting.ProtocolExtension.prototype.onMessage = - function(message) {}; +remoting.ProtocolExtension.prototype.onExtensionMessage = + function(type, message) {}; diff --git a/remoting/webapp/crd/js/cast_extension_handler.js b/remoting/webapp/crd/js/cast_extension_handler.js index ab0bde2..b8e2621 100644 --- a/remoting/webapp/crd/js/cast_extension_handler.js +++ b/remoting/webapp/crd/js/cast_extension_handler.js @@ -41,9 +41,12 @@ remoting.CastExtensionHandler = function() { this.sendMessageToHostCallback_ = null; }; -/** @return {string} */ -remoting.CastExtensionHandler.prototype.getType = function() { - return 'cast_message'; +/** @private {string} */ +remoting.CastExtensionHandler.EXTENSION_TYPE = 'cast_message'; + +/** @return {Array<string>} */ +remoting.CastExtensionHandler.prototype.getExtensionTypes = function() { + return [remoting.CastExtensionHandler.EXTENSION_TYPE]; }; /** @@ -58,7 +61,8 @@ remoting.CastExtensionHandler.prototype.SCRIPT_NODE_ID_ = 'cast-script-node'; * @param {function(string,string)} sendMessageToHost Callback to send a message * to the host. */ -remoting.CastExtensionHandler.prototype.start = function(sendMessageToHost) { +remoting.CastExtensionHandler.prototype.startExtension = + function(sendMessageToHost) { this.sendMessageToHostCallback_ = sendMessageToHost; var node = document.getElementById(this.SCRIPT_NODE_ID_); @@ -90,11 +94,13 @@ remoting.CastExtensionHandler.prototype.start = function(sendMessageToHost) { /** * Process Cast Extension Messages from the Chromoting host. - * @param {string} msgString The extension message's data. + * + * @param {string} type The message type. + * @param {Object} message The parsed extension message data. + * @return {boolean} True if the extension message was handled. */ -remoting.CastExtensionHandler.prototype.onMessage = function(msgString) { - var message = getJsonObjectFromString(msgString); - +remoting.CastExtensionHandler.prototype.onExtensionMessage = + function(type, message) { // Save messages to send after a session is established. this.messageQueue_.push(message); // Trigger the sending of pending messages, followed by the one just @@ -102,6 +108,7 @@ remoting.CastExtensionHandler.prototype.onMessage = function(msgString) { if (this.session_) { this.sendPendingMessages_(); } + return true; }; /** @@ -111,7 +118,8 @@ remoting.CastExtensionHandler.prototype.onMessage = function(msgString) { * @private */ remoting.CastExtensionHandler.prototype.sendMessageToHost_ = function(data) { - this.sendMessageToHostCallback_(this.getType(), JSON.stringify(data)); + this.sendMessageToHostCallback_(remoting.CastExtensionHandler.EXTENSION_TYPE, + JSON.stringify(data)); }; /** diff --git a/remoting/webapp/crd/js/desktop_remoting.js b/remoting/webapp/crd/js/desktop_remoting.js index a2ea18c..0ef6bea 100644 --- a/remoting/webapp/crd/js/desktop_remoting.js +++ b/remoting/webapp/crd/js/desktop_remoting.js @@ -294,16 +294,6 @@ remoting.DesktopRemoting.prototype.handleVideoStreamingStarted = function() { }; /** - * @param {string} type The type of the extension message. - * @param {Object} message The parsed extension message data. - * @return {boolean} Return true if the extension message was recognized. - */ -remoting.DesktopRemoting.prototype.handleExtensionMessage = function( - type, message) { - return false; -}; - -/** * Called when an error needs to be displayed to the user. * * @param {!remoting.Error} error The error to be localized and displayed. diff --git a/remoting/webapp/crd/js/gnubby_auth_handler.js b/remoting/webapp/crd/js/gnubby_auth_handler.js index 4d414da..f3514dd 100644 --- a/remoting/webapp/crd/js/gnubby_auth_handler.js +++ b/remoting/webapp/crd/js/gnubby_auth_handler.js @@ -22,16 +22,20 @@ remoting.GnubbyAuthHandler = function() { this.sendMessageToHostCallback_ = null; }; -/** @return {string} */ -remoting.GnubbyAuthHandler.prototype.getType = function() { - return 'gnubby-auth'; +/** @private {string} */ +remoting.GnubbyAuthHandler.EXTENSION_TYPE = 'gnubby-auth'; + +/** @return {Array<string>} */ +remoting.GnubbyAuthHandler.prototype.getExtensionTypes = function() { + return [remoting.GnubbyAuthHandler.EXTENSION_TYPE]; }; /** * @param {function(string,string)} sendMessageToHost Callback to send a message * to the host. */ -remoting.GnubbyAuthHandler.prototype.start = function(sendMessageToHost) { +remoting.GnubbyAuthHandler.prototype.startExtension = + function(sendMessageToHost) { this.sendMessageToHostCallback_ = sendMessageToHost; this.sendMessageToHost_({ @@ -45,15 +49,19 @@ remoting.GnubbyAuthHandler.prototype.start = function(sendMessageToHost) { * @private */ remoting.GnubbyAuthHandler.prototype.sendMessageToHost_ = function(data) { - this.sendMessageToHostCallback_(this.getType(), JSON.stringify(data)); + this.sendMessageToHostCallback_(remoting.GnubbyAuthHandler.EXTENSION_TYPE, + JSON.stringify(data)); } /** * Processes gnubby-auth messages. - * @param {string} data The gnubby-auth message data. + * + * @param {string} type The message type. + * @param {Object} message The parsed extension message data. + * @return {boolean} True if the extension message was handled. */ -remoting.GnubbyAuthHandler.prototype.onMessage = function(data) { - var message = getJsonObjectFromString(data); +remoting.GnubbyAuthHandler.prototype.onExtensionMessage = + function(type, message) { var messageType = getStringAttr(message, 'type'); if (messageType == 'data') { this.sendMessageToGnubbyd_({ @@ -62,7 +70,9 @@ remoting.GnubbyAuthHandler.prototype.onMessage = function(data) { }, this.callback_.bind(this, getNumberAttr(message, 'connectionId'))); } else { console.error('Invalid gnubby-auth message: ' + messageType); + return false; } + return true; }; /** diff --git a/remoting/webapp/crd/js/session_connector.js b/remoting/webapp/crd/js/session_connector.js index 3da3f65..fce0bef 100644 --- a/remoting/webapp/crd/js/session_connector.js +++ b/remoting/webapp/crd/js/session_connector.js @@ -121,9 +121,6 @@ remoting.SessionConnectorFactory = function() {}; * @param {function(remoting.ConnectionInfo):void} onConnected Callback on * success. * @param {function(!remoting.Error):void} onError Callback on error. - * @param {function(string, string):boolean} appProtocolExtensionHandler The - * handler for the application's protocol extension messages. Returns true - * if a message is recognized; false otherwise. * @param {function(!remoting.Error):void} onConnectionFailed Callback for when * the connection fails. * @param {Array<string>} requiredCapabilities Connector capabilities @@ -133,7 +130,7 @@ remoting.SessionConnectorFactory = function() {}; * @return {remoting.SessionConnector} */ remoting.SessionConnectorFactory.prototype.createConnector = - function(clientContainer, onConnected, onError, appProtocolExtensionHandler, + function(clientContainer, onConnected, onError, onConnectionFailed, requiredCapabilities, defaultRemapKeys) {}; /** diff --git a/remoting/webapp/crd/js/session_connector_impl.js b/remoting/webapp/crd/js/session_connector_impl.js index 4f8c934..13def39 100644 --- a/remoting/webapp/crd/js/session_connector_impl.js +++ b/remoting/webapp/crd/js/session_connector_impl.js @@ -31,9 +31,6 @@ remoting.desktopConnectedView = null; * @param {function(remoting.ConnectionInfo):void} onConnected Callback on * success. * @param {function(!remoting.Error):void} onError Callback on error. - * @param {function(string, string):boolean} appProtocolExtensionHandler The - * handler for the application's protocol extension messages. Returns true - * if a message is recognized; false otherwise. * @param {function(!remoting.Error):void} onConnectionFailed Callback for when * the connection fails. * @param {Array<string>} requiredCapabilities Connector capabilities @@ -44,7 +41,6 @@ remoting.desktopConnectedView = null; * @implements {remoting.SessionConnector} */ remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError, - appProtocolExtensionHandler, onConnectionFailed, requiredCapabilities, defaultRemapKeys) { @@ -57,9 +53,6 @@ remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError, /** @private {function(!remoting.Error):void} */ this.onError_ = onError; - /** @private {function(string, string):boolean} */ - this.appProtocolExtensionHandler_ = appProtocolExtensionHandler; - /** @private {function(!remoting.Error):void} */ this.onConnectionFailed_ = onConnectionFailed; @@ -118,6 +111,14 @@ remoting.SessionConnectorImpl.prototype.resetConnection_ = function() { /** @private {Object<string,remoting.ProtocolExtension>} */ this.protocolExtensions_ = {}; + + /** + * True once a session has been created and we've started the extensions. + * This is used to immediately start any extensions that are registered + * after the CONNECTED state change. + * @private {boolean} + */ + this.protocolExtensionsStarted_ = false; }; /** @@ -405,22 +406,43 @@ remoting.SessionConnectorImpl.prototype.removePlugin_ = function() { */ remoting.SessionConnectorImpl.prototype.registerProtocolExtension = function(extension) { - var type = extension.getType(); - if (type in this.protocolExtensions_) { - console.error( - 'Attempt to register multiple extensions with the same type: ', type); - return; + var types = extension.getExtensionTypes(); + + // Make sure we don't have an extension of that type already registered. + for (var i=0, len=types.length; i < len; i++) { + if (types[i] in this.protocolExtensions_) { + console.error( + 'Attempt to register multiple extensions of the same type: ', type); + return; + } + } + + for (var i=0, len=types.length; i < len; i++) { + var type = types[i]; + this.protocolExtensions_[type] = extension; + if (this.protocolExtensionsStarted_) { + this.startProtocolExtension_(type); + } } - this.protocolExtensions_[type] = extension; }; /** @private */ remoting.SessionConnectorImpl.prototype.initProtocolExtensions_ = function() { + base.debug.assert(!this.protocolExtensionsStarted_); for (var type in this.protocolExtensions_) { - /** @type {remoting.ProtocolExtension} */ - var extension = this.protocolExtensions_[type]; - extension.start(this.plugin_.sendClientMessage.bind(this.plugin_)); + this.startProtocolExtension_(type); } + this.protocolExtensionsStarted_ = true; +}; + +/** + * @param {string} type + * @private + */ +remoting.SessionConnectorImpl.prototype.startProtocolExtension_ = + function(type) { + var extension = this.protocolExtensions_[type]; + extension.startExtension(this.plugin_.sendClientMessage.bind(this.plugin_)); }; /** @@ -437,20 +459,33 @@ remoting.SessionConnectorImpl.prototype.onProtocolExtensionMessage_ = console.log('Got echo reply: ' + data); return true; } - for (var type in this.protocolExtensions_) { + + var message = base.jsonParseSafe(data); + if (typeof message != 'object') { + console.error('Error parsing extension json data: ' + data); + return false; + } + + if (type in this.protocolExtensions_) { /** @type {remoting.ProtocolExtension} */ var extension = this.protocolExtensions_[type]; - if (type == extension.getType()) { - try { - extension.onMessage(data); - } catch (/** @type {*} */ err) { - console.error('Failed to process protocol extension ', type, - ' message: ', err); - } + var handled = false; + try { + handled = extension.onExtensionMessage(type, message); + } catch (/** @type {*} */ err) { + console.error('Failed to process protocol extension ' + type + + ' message: ' + err); + } + if (handled) { return true; } } - return this.appProtocolExtensionHandler_(type, data); + + if (remoting.desktopConnectedView) { + return remoting.desktopConnectedView.handleExtensionMessage(type, message); + } + + return false; }; /** @@ -542,9 +577,6 @@ remoting.DefaultSessionConnectorFactory = function() {}; * @param {function(remoting.ConnectionInfo):void} onConnected Callback on * success. * @param {function(!remoting.Error):void} onError Callback on error. - * @param {function(string, string):boolean} appProtocolExtensionHandler The - * handler for the application's protocol extension messages. Returns true - * if a message is recognized; false otherwise. * @param {function(!remoting.Error):void} onConnectionFailed Callback for when * the connection fails. * @param {Array<string>} requiredCapabilities Connector capabilities @@ -555,11 +587,9 @@ remoting.DefaultSessionConnectorFactory = function() {}; */ remoting.DefaultSessionConnectorFactory.prototype.createConnector = function(clientContainer, onConnected, onError, - appProtocolExtensionHandler, onConnectionFailed, requiredCapabilities, defaultRemapKeys) { return new remoting.SessionConnectorImpl(clientContainer, onConnected, onError, - appProtocolExtensionHandler, onConnectionFailed, requiredCapabilities, defaultRemapKeys); |