diff options
author | psj@chromium.org <psj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-27 04:12:08 +0000 |
---|---|---|
committer | psj@chromium.org <psj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-27 04:12:08 +0000 |
commit | de101e124a81814fb7a0e183b6a3761f334b926a (patch) | |
tree | fe15f0a5c7287ca4e74dd8f2512b089c39cd1c89 /remoting/webapp | |
parent | 54c6efa65afd6484f304ed01786865e84bb11b1b (diff) | |
download | chromium_src-de101e124a81814fb7a0e183b6a3761f334b926a.zip chromium_src-de101e124a81814fb7a0e183b6a3761f334b926a.tar.gz chromium_src-de101e124a81814fb7a0e183b6a3761f334b926a.tar.bz2 |
Add gnubby auth handler to webapp
BUG=134250
Review URL: https://codereview.chromium.org/166273022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@253711 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/webapp')
-rw-r--r-- | remoting/webapp/client_plugin.js | 15 | ||||
-rw-r--r-- | remoting/webapp/client_session.js | 49 | ||||
-rw-r--r-- | remoting/webapp/gnubby_auth_handler.js | 89 | ||||
-rw-r--r-- | remoting/webapp/js_proto/chrome_proto.js | 8 | ||||
-rw-r--r-- | remoting/webapp/typecheck.js | 17 |
5 files changed, 173 insertions, 5 deletions
diff --git a/remoting/webapp/client_plugin.js b/remoting/webapp/client_plugin.js index 824098b..65db925 100644 --- a/remoting/webapp/client_plugin.js +++ b/remoting/webapp/client_plugin.js @@ -55,6 +55,8 @@ remoting.ClientPlugin = function(plugin, onExtensionMessage) { /** @param {!Array.<string>} capabilities The negotiated capabilities. */ this.onSetCapabilitiesHandler = function (capabilities) {}; this.fetchPinHandler = function (supportsPairing) {}; + /** @param {string} data Remote gnubbyd data. */ + this.onGnubbyAuthHandler = function(data) {}; /** @type {remoting.MediaSourceRenderer} */ this.mediaSourceRenderer_ = null; @@ -292,9 +294,12 @@ remoting.ClientPlugin.prototype.handleMessageMethod_ = function(message) { this.onPairingComplete_(clientId, sharedSecret); } else if (message.method == 'extensionMessage') { - var extMsgType = getStringAttr(message, 'type'); - var extMsgData = getStringAttr(message, 'data'); + var extMsgType = getStringAttr(message.data, 'type'); + var extMsgData = getStringAttr(message.data, 'data'); switch (extMsgType) { + case 'gnubby-auth': + this.onGnubbyAuthHandler(extMsgData); + break; case 'test-echo-reply': console.log('Got echo reply: ' + extMsgData); break; @@ -617,7 +622,7 @@ remoting.ClientPlugin.prototype.requestPairing = * Send an extension message to the host. * * @param {string} type The message type. - * @param {Object} message The message payload. + * @param {string} message The message payload. */ remoting.ClientPlugin.prototype.sendClientMessage = function(type, message) { @@ -625,8 +630,8 @@ remoting.ClientPlugin.prototype.sendClientMessage = return; } this.plugin.postMessage(JSON.stringify( - { method: 'extensionMessage', - data: { type: type, data: JSON.stringify(message) } })); + { method: 'extensionMessage', + data: { type: type, data: message } })); }; diff --git a/remoting/webapp/client_session.js b/remoting/webapp/client_session.js index a2b8af8..95da825 100644 --- a/remoting/webapp/client_session.js +++ b/remoting/webapp/client_session.js @@ -135,6 +135,9 @@ remoting.ClientSession = function(accessCode, fetchPin, fetchThirdPartyToken, /** @type {HTMLElement} @private */ this.fullScreenButton_ = document.getElementById('toggle-full-screen'); + /** @type {remoting.GnubbyAuthHandler} @private */ + this.gnubbyAuthHandler_ = null; + if (this.mode_ == remoting.ClientSession.Mode.IT2ME) { // Resize-to-client is not supported for IT2Me hosts. this.resizeToClientButton_.hidden = true; @@ -509,6 +512,8 @@ remoting.ClientSession.prototype.onPluginInitialized_ = function(initialized) { this.onDesktopSizeChanged_.bind(this); this.plugin_.onSetCapabilitiesHandler = this.onSetCapabilities_.bind(this); + this.plugin_.onGnubbyAuthHandler = + this.processGnubbyAuthMessage_.bind(this); this.initiateConnection_(); }; @@ -1002,6 +1007,9 @@ remoting.ClientSession.prototype.setState_ = function(newState) { state = remoting.ClientSession.State.CONNECTION_DROPPED; } this.logToServer.logClientSessionStateChange(state, this.error_, this.mode_); + if (this.state_ == remoting.ClientSession.State.CONNECTED) { + this.createGnubbyAuthHandler_(); + } if (this.onStateChange_) { this.onStateChange_(oldState, newState); } @@ -1383,3 +1391,44 @@ remoting.ClientSession.prototype.sendClipboardItem = function(mimeType, item) { return; this.plugin_.sendClipboardItem(mimeType, item); }; + +/** + * Send a gnubby-auth extension message to the host. + * @param {Object} data The gnubby-auth message data. + */ +remoting.ClientSession.prototype.sendGnubbyAuthMessage = function(data) { + if (!this.plugin_) + return; + this.plugin_.sendClientMessage('gnubby-auth', JSON.stringify(data)); +}; + +/** + * Process a remote gnubby auth request. + * @param {string} data Remote gnubby request data. + * @private + */ +remoting.ClientSession.prototype.processGnubbyAuthMessage_ = function(data) { + if (this.gnubbyAuthHandler_) { + try { + this.gnubbyAuthHandler_.onMessage(data); + } catch (err) { + console.error('Failed to process gnubby message: ', + /** @type {*} */ (err)); + } + } else { + console.error('Received unexpected gnubby message'); + } +}; + +/** + * Create a gnubby auth handler and inform the host that gnubby auth is + * supported. + * @private + */ +remoting.ClientSession.prototype.createGnubbyAuthHandler_ = function() { + if (this.mode_ == remoting.ClientSession.Mode.ME2ME) { + this.gnubbyAuthHandler_ = new remoting.GnubbyAuthHandler(this); + // TODO(psj): Move to more generic capabilities mechanism. + this.sendGnubbyAuthMessage({'type': 'control', 'option': 'auth-v1'}); + } +}; diff --git a/remoting/webapp/gnubby_auth_handler.js b/remoting/webapp/gnubby_auth_handler.js new file mode 100644 index 0000000..848ab1e --- /dev/null +++ b/remoting/webapp/gnubby_auth_handler.js @@ -0,0 +1,89 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview + * Class that routes gnubby-auth extension messages to and from the gnubbyd + * extension. + */ + +'use strict'; + +/** @suppress {duplicate} */ +var remoting = remoting || {}; + +/** + * @constructor + * @param {!remoting.ClientSession} clientSession The client session to send + * gnubby-auth response messages to. + */ +remoting.GnubbyAuthHandler = function(clientSession) { + this.clientSession_ = clientSession; +}; + +/** + * Processes gnubby-auth messages. + * @param {string} data The gnubby-auth message data. + */ +remoting.GnubbyAuthHandler.prototype.onMessage = function(data) { + var message = getJsonObjectFromString(data); + var messageType = getStringAttr(message, 'type'); + if (messageType == 'data') { + this.sendMessageToGnubbyd_( + getJsonObjectFromString(getStringAttr(message, 'jsonMessage')), + this.callback_.bind(this, getNumberAttr(message, 'connectionId'))); + } else { + console.error('Invalid gnubby-auth message: ' + messageType); + } +}; + +/** + * Callback invoked with data to be returned to the host. + * @param {number} connectionId The connection id. + * @param {Object} data The JSON object to send to the host. + * @private + */ +remoting.GnubbyAuthHandler.prototype.callback_ = + function(connectionId, data) { + var json_data = JSON.stringify(data); + this.clientSession_.sendGnubbyAuthMessage({'type': 'data', + 'connectionId': connectionId, + 'jsonMessage': json_data}); +}; + +/** + * Send data to the gnubbyd extension. + * @param {Object} jsonObject The JSON object to send to the gnubbyd extension. + * @param {function(Object)} callback The callback to invoke with reply data. + * @private + */ +remoting.GnubbyAuthHandler.prototype.sendMessageToGnubbyd_ = + function(jsonObject, callback) { + var kGnubbydDevExtensionId = 'dlfcjilkjfhdnfiecknlnddkmmiofjbg'; + + chrome.runtime.sendMessage( + kGnubbydDevExtensionId, + jsonObject, + onGnubbydDevReply_.bind(this, jsonObject, callback)); +}; + +/** + * Callback invoked as a result of sending a message to the gnubbyd-dev + * extension. If that extension is not installed, reply will be undefined; + * otherwise it will be the JSON response object. + * @param {Object} jsonObject The JSON object to send to the gnubbyd extension. + * @param {function(Object)} callback The callback to invoke with reply data. + * @param {Object} reply The reply from the extension (or Chrome, if the + * extension does not exist. + * @private + */ +function onGnubbydDevReply_(jsonObject, callback, reply) { + var kGnubbydStableExtensionId = 'beknehfpfkghjoafdifaflglpjkojoco'; + + if (reply) { + callback(reply); + } else { + chrome.runtime.sendMessage(kGnubbydStableExtensionId, jsonObject, callback); + } +}; diff --git a/remoting/webapp/js_proto/chrome_proto.js b/remoting/webapp/js_proto/chrome_proto.js index 7210df5..0e41606 100644 --- a/remoting/webapp/js_proto/chrome_proto.js +++ b/remoting/webapp/js_proto/chrome_proto.js @@ -51,6 +51,14 @@ chrome.runtime = { */ chrome.runtime.connectNative = function(name) {}; +/** + * @param {string} extensionId + * @param {*} message + * @param {Object=} opt_options + * @param {function(*)=} opt_callback + */ +chrome.runtime.sendMessage = function( + extensionId, message, opt_options, opt_callback) {}; /** @type {Object} */ chrome.extension = {}; diff --git a/remoting/webapp/typecheck.js b/remoting/webapp/typecheck.js index 4da66f7..5b5f865 100644 --- a/remoting/webapp/typecheck.js +++ b/remoting/webapp/typecheck.js @@ -126,3 +126,20 @@ function getStringAttr(dict, key, opt_default) { } return value; } + +/** + * Return a JSON object parsed from a string. + * + * If the string cannot be parsed, or does not result in an object, then an + * exception will be thrown. + * + * @param {string} jsonString The JSON string to parse. + * @return {Object} The JSON object created from the |jsonString|. + */ +function getJsonObjectFromString(jsonString) { + var value = /** @type {Object} */ JSON.parse(jsonString); + if (typeof value != 'object') { + throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')'; + } + return value; +} |