Commit 06408c1d authored by kelvinp's avatar kelvinp Committed by Commit bot

[Webapp Refactor] Move key injection related functionality into the plugin layer.

This CL moves key injection logic setRemapKey() and injectKeyCombination() into
the plugin layer.

With this change, we no longer need to pass the default remap keys from the
ApplicationDelegate through the SessionConnector to the DesktopConnectedView
in order to setup a key remapping.

Instead, we can simply call plugin.setRemapKeys('a->b,b->c') on the delegate.

BUG=465878

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

Cr-Commit-Position: refs/heads/master@{#321419}
parent a8190bfa
......@@ -210,18 +210,6 @@ remoting.AppRemoting.prototype.runApplicationUrl = function() {
remoting.settings.getAppRemotingApplicationId() + '/run';
};
/**
* @return {string} The default remap keys for the current platform.
*/
remoting.AppRemoting.prototype.getDefaultRemapKeys = function() {
// Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard
// shortcuts, but we want them to act as natively as possible.
if (remoting.platformIsMac()) {
return '0x0700e3>0x0700e0,0x0700e7>0x0700e4';
}
return '';
};
/**
* Called when a new session has been connected.
*
......@@ -249,8 +237,13 @@ remoting.AppRemoting.prototype.handleConnected = function(connectionInfo) {
// TODO(kelvinp): Move all app remoting specific logic into
// remoting.AppRemotingView.
this.connectedView_ = new remoting.DesktopConnectedView(
document.getElementById('client-container'), connectionInfo,
this.getDefaultRemapKeys());
document.getElementById('client-container'), connectionInfo);
// Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard
// shortcuts, but we want them to act as natively as possible.
if (remoting.platformIsMac()) {
connectionInfo.plugin().setRemapKeys('0x0700e3>0x0700e0,0x0700e7>0x0700e4');
}
};
/**
......
......@@ -180,8 +180,7 @@ remoting.Application.prototype.getSessionConnector = function() {
this.onConnected.bind(this),
this.onError.bind(this),
this.onConnectionFailed.bind(this),
this.appCapabilities_,
this.delegate_.getDefaultRemapKeys());
this.appCapabilities_);
}
return this.sessionConnector_;
};
......@@ -266,11 +265,6 @@ remoting.Application.Delegate.prototype.signInFailed = function(error) {};
*/
remoting.Application.Delegate.prototype.getApplicationName = function() {};
/**
* @return {string} The default remap keys for the current platform.
*/
remoting.Application.Delegate.prototype.getDefaultRemapKeys = function() {};
/**
* Called when a new session has been connected.
*
......
......@@ -48,6 +48,22 @@ remoting.ClientPlugin.prototype.connect =
remoting.ClientPlugin.prototype.injectKeyEvent =
function(key, down) {};
/**
* Sends a key combination to the host, by sending down events for
* the given keys, followed by up events in reverse order.
*
* @param {Array<number>} keys Key codes to be sent.
* @return {void} Nothing.
*/
remoting.ClientPlugin.prototype.injectKeyCombination = function(keys) {};
/**
* Sets and stores the key remapping setting for the current host.
*
* @param {string} remappings Comma separated list of key remappings.
*/
remoting.ClientPlugin.prototype.setRemapKeys = function(remappings) {};
/**
* @param {number} from
* @param {number} to
......
......@@ -98,6 +98,9 @@ remoting.ClientPluginImpl = function(container,
/** @private {remoting.CredentialsProvider} */
this.credentials_ = null;
/** @private {string} */
this.keyRemappings_ = '';
};
/**
......@@ -496,6 +499,73 @@ remoting.ClientPluginImpl.prototype.releaseAllKeys = function() {
{ method: 'releaseAllKeys', data: {} }));
};
/**
* Sets and stores the key remapping setting for the current host.
*
* @param {string} remappings Comma separated list of key remappings.
*/
remoting.ClientPluginImpl.prototype.setRemapKeys =
function(remappings) {
// Cancel any existing remappings and apply the new ones.
this.applyRemapKeys_(this.keyRemappings_, false);
this.applyRemapKeys_(remappings, true);
this.keyRemappings_ = remappings;
};
/**
* Applies the configured key remappings to the session, or resets them.
*
* @param {string} remapKeys
* @param {boolean} apply True to apply remappings, false to cancel them.
* @private
*/
remoting.ClientPluginImpl.prototype.applyRemapKeys_ =
function(remapKeys, apply) {
if (remapKeys == '') {
return;
}
var remappings = remapKeys.split(',');
for (var i = 0; i < remappings.length; ++i) {
var keyCodes = remappings[i].split('>');
if (keyCodes.length != 2) {
console.log('bad remapKey: ' + remappings[i]);
continue;
}
var fromKey = parseInt(keyCodes[0], 0);
var toKey = parseInt(keyCodes[1], 0);
if (!fromKey || !toKey) {
console.log('bad remapKey code: ' + remappings[i]);
continue;
}
if (apply) {
console.log('remapKey 0x' + fromKey.toString(16) +
'>0x' + toKey.toString(16));
this.remapKey(fromKey, toKey);
} else {
console.log('cancel remapKey 0x' + fromKey.toString(16));
this.remapKey(fromKey, fromKey);
}
}
};
/**
* Sends a key combination to the remoting host, by sending down events for
* the given keys, followed by up events in reverse order.
*
* @param {Array<number>} keys Key codes to be sent.
* @return {void} Nothing.
*/
remoting.ClientPluginImpl.prototype.injectKeyCombination =
function(keys) {
for (var i = 0; i < keys.length; i++) {
this.injectKeyEvent(keys[i], true);
}
for (var i = 0; i < keys.length; i++) {
this.injectKeyEvent(keys[i], false);
}
};
/**
* Send a key event to the host.
*
......
......@@ -15,14 +15,11 @@ var remoting = remoting || {};
/**
* @param {HTMLElement} container
* @param {remoting.ConnectionInfo} connectionInfo
* @param {string} defaultRemapKeys The default set of remap keys, to use
* when the client doesn't define any.
* @constructor
* @extends {base.EventSourceImpl}
* @implements {base.Disposable}
*/
remoting.DesktopConnectedView = function(container, connectionInfo,
defaultRemapKeys) {
remoting.DesktopConnectedView = function(container, connectionInfo) {
/** @private {HTMLElement} */
this.container_ = container;
......@@ -39,9 +36,6 @@ remoting.DesktopConnectedView = function(container, connectionInfo,
/** @private */
this.mode_ = connectionInfo.mode();
/** @private {string} */
this.defaultRemapKeys_ = defaultRemapKeys;
/** @private {remoting.DesktopViewport} */
this.viewport_ = null;
......@@ -156,11 +150,6 @@ remoting.DesktopConnectedView.prototype.initPlugin_ = function() {
sendCadElement.hidden = true;
}
// Apply customized key remappings if the plugin supports remapKeys.
if (this.plugin_.hasFeature(remoting.ClientPlugin.Feature.REMAP_KEY)) {
this.applyRemapKeys_(true);
}
if (this.session_.hasCapability(
remoting.ClientSession.Capability.VIDEO_RECORDER)) {
this.videoFrameRecorder_ = new remoting.VideoFrameRecorder(this.plugin_);
......@@ -253,76 +242,6 @@ remoting.DesktopConnectedView.prototype.onFullScreenChanged_ = function (
}
};
/**
* Sets and stores the key remapping setting for the current host.
*
* @param {string} remappings Comma separated list of key remappings.
*/
remoting.DesktopConnectedView.prototype.setRemapKeys = function(remappings) {
// Cancel any existing remappings and apply the new ones.
this.applyRemapKeys_(false);
this.host_.options.remapKeys = remappings;
this.applyRemapKeys_(true);
// Save the new remapping setting.
this.host_.options.save();
};
/**
* Applies the configured key remappings to the session, or resets them.
*
* @param {boolean} apply True to apply remappings, false to cancel them.
*/
remoting.DesktopConnectedView.prototype.applyRemapKeys_ = function(apply) {
var remapKeys = this.host_.options.remapKeys;
if (remapKeys == '') {
remapKeys = this.defaultRemapKeys_;
if (remapKeys == '') {
return;
}
}
var remappings = remapKeys.split(',');
for (var i = 0; i < remappings.length; ++i) {
var keyCodes = remappings[i].split('>');
if (keyCodes.length != 2) {
console.log('bad remapKey: ' + remappings[i]);
continue;
}
var fromKey = parseInt(keyCodes[0], 0);
var toKey = parseInt(keyCodes[1], 0);
if (!fromKey || !toKey) {
console.log('bad remapKey code: ' + remappings[i]);
continue;
}
if (apply) {
console.log('remapKey 0x' + fromKey.toString(16) +
'>0x' + toKey.toString(16));
this.plugin_.remapKey(fromKey, toKey);
} else {
console.log('cancel remapKey 0x' + fromKey.toString(16));
this.plugin_.remapKey(fromKey, fromKey);
}
}
};
/**
* Sends a key combination to the remoting client, by sending down events for
* the given keys, followed by up events in reverse order.
*
* @param {Array<number>} keys Key codes to be sent.
* @return {void} Nothing.
* @private
*/
remoting.DesktopConnectedView.prototype.sendKeyCombination_ = function(keys) {
for (var i = 0; i < keys.length; i++) {
this.plugin_.injectKeyEvent(keys[i], true);
}
for (var i = 0; i < keys.length; i++) {
this.plugin_.injectKeyEvent(keys[i], false);
}
};
/**
* Sends a Ctrl-Alt-Del sequence to the remoting client.
*
......@@ -330,7 +249,7 @@ remoting.DesktopConnectedView.prototype.sendKeyCombination_ = function(keys) {
*/
remoting.DesktopConnectedView.prototype.sendCtrlAltDel = function() {
console.log('Sending Ctrl-Alt-Del.');
this.sendKeyCombination_([0x0700e0, 0x0700e2, 0x07004c]);
this.plugin_.injectKeyCombination([0x0700e0, 0x0700e2, 0x07004c]);
};
/**
......@@ -340,7 +259,7 @@ remoting.DesktopConnectedView.prototype.sendCtrlAltDel = function() {
*/
remoting.DesktopConnectedView.prototype.sendPrintScreen = function() {
console.log('Sending Print Screen.');
this.sendKeyCombination_([0x070046]);
this.plugin_.injectKeyCombination([0x070046]);
};
/**
......
......@@ -155,19 +155,6 @@ remoting.DesktopRemoting.prototype.getApplicationName = function() {
return chrome.i18n.getMessage(/*i18n-content*/'PRODUCT_NAME');
};
/**
* @return {string} The default remap keys for the current platform.
*/
remoting.DesktopRemoting.prototype.getDefaultRemapKeys = function() {
var remapKeys = '';
// By default, under ChromeOS, remap the right Control key to the right
// Win / Cmd key.
if (remoting.platformIsChromeOS()) {
remapKeys = '0x0700e4>0x0700e7';
}
return remapKeys;
};
/**
* Called when a new session has been connected.
*
......@@ -198,8 +185,13 @@ remoting.DesktopRemoting.prototype.handleConnected = function(connectionInfo) {
}
this.connectedView_ = new remoting.DesktopConnectedView(
document.getElementById('client-container'), connectionInfo,
this.getDefaultRemapKeys());
document.getElementById('client-container'), connectionInfo);
// By default, under ChromeOS, remap the right Control key to the right
// Win / Cmd key.
if (remoting.platformIsChromeOS()) {
connectionInfo.plugin().setRemapKeys('0x0700e4>0x0700e7');
}
if (connectionInfo.mode() === remoting.DesktopConnectedView.Mode.ME2ME) {
var sessionConnector = remoting.app.getSessionConnector();
......
......@@ -129,13 +129,11 @@ remoting.SessionConnectorFactory = function() {};
* the connection fails.
* @param {Array<string>} requiredCapabilities Connector capabilities
* required by this application.
* @param {string} defaultRemapKeys The default set of key mappings to use
* in the client session.
* @return {remoting.SessionConnector}
*/
remoting.SessionConnectorFactory.prototype.createConnector =
function(clientContainer, onConnected, onError,
onConnectionFailed, requiredCapabilities, defaultRemapKeys) {};
onConnectionFailed, requiredCapabilities) {};
/**
* @type {remoting.SessionConnectorFactory}
......
......@@ -28,15 +28,12 @@ remoting.clientSession = null;
* the connection fails.
* @param {Array<string>} requiredCapabilities Connector capabilities
* required by this application.
* @param {string} defaultRemapKeys The default set of key mappings for the
* client session to use.
* @constructor
* @implements {remoting.SessionConnector}
*/
remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError,
onConnectionFailed,
requiredCapabilities,
defaultRemapKeys) {
requiredCapabilities) {
/** @private {HTMLElement} */
this.clientContainer_ = clientContainer;
......@@ -52,9 +49,6 @@ remoting.SessionConnectorImpl = function(clientContainer, onConnected, onError,
/** @private {Array<string>} */
this.requiredCapabilities_ = requiredCapabilities;
/** @private {string} */
this.defaultRemapKeys_ = defaultRemapKeys;
/** @private {remoting.DesktopConnectedView.Mode} */
this.connectionMode_ = remoting.DesktopConnectedView.Mode.ME2ME;
......@@ -555,16 +549,13 @@ remoting.DefaultSessionConnectorFactory = function() {};
* the connection fails.
* @param {Array<string>} requiredCapabilities Connector capabilities
* required by this application.
* @param {string} defaultRemapKeys The default set of key mappings to use
* in the client session.
* @return {remoting.SessionConnector}
*/
remoting.DefaultSessionConnectorFactory.prototype.createConnector =
function(clientContainer, onConnected, onError,
onConnectionFailed, requiredCapabilities, defaultRemapKeys) {
onConnectionFailed, requiredCapabilities) {
return new remoting.SessionConnectorImpl(clientContainer, onConnected,
onError,
onConnectionFailed,
requiredCapabilities,
defaultRemapKeys);
requiredCapabilities);
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment