1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * Class that routes gnubby-auth extension messages to and from the gnubbyd
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
18 * @implements {remoting.ProtocolExtension}
20 remoting.GnubbyAuthHandler = function() {
21 /** @private {?function(string,string)} */
22 this.sendMessageToHostCallback_ = null;
25 /** @private {string} */
26 remoting.GnubbyAuthHandler.EXTENSION_TYPE = 'gnubby-auth';
28 /** @return {Array<string>} */
29 remoting.GnubbyAuthHandler.prototype.getExtensionTypes = function() {
30 return [remoting.GnubbyAuthHandler.EXTENSION_TYPE];
34 * @param {function(string,string)} sendMessageToHost Callback to send a message
37 remoting.GnubbyAuthHandler.prototype.startExtension =
38 function(sendMessageToHost) {
39 this.sendMessageToHostCallback_ = sendMessageToHost;
41 this.sendMessageToHost_({
48 * @param {Object} data The data to send.
51 remoting.GnubbyAuthHandler.prototype.sendMessageToHost_ = function(data) {
52 this.sendMessageToHostCallback_(remoting.GnubbyAuthHandler.EXTENSION_TYPE,
53 JSON.stringify(data));
57 * Processes gnubby-auth messages.
59 * @param {string} type The message type.
60 * @param {Object} message The parsed extension message data.
61 * @return {boolean} True if the extension message was handled.
63 remoting.GnubbyAuthHandler.prototype.onExtensionMessage =
64 function(type, message) {
65 var messageType = base.getStringAttr(message, 'type');
66 if (messageType == 'data') {
67 this.sendMessageToGnubbyd_({
68 'type': 'auth-agent@openssh.com',
69 'data': base.getArrayAttr(message, 'data')
70 }, this.callback_.bind(this, base.getNumberAttr(message, 'connectionId')));
72 console.error('Invalid gnubby-auth message: ' + messageType);
79 * Callback invoked with data to be returned to the host.
80 * @param {number} connectionId The connection id.
81 * @param {Object} response The JSON response with the data to send to the host.
84 remoting.GnubbyAuthHandler.prototype.callback_ =
85 function(connectionId, response) {
87 this.sendMessageToHost_({
89 'connectionId': connectionId,
90 'data': base.getArrayAttr(response, 'data')
92 } catch (/** @type {*} */ err) {
93 console.error('gnubby callback failed: ', err);
94 this.sendMessageToHost_({
96 'connectionId': connectionId
103 * Send data to the gnubbyd extension.
104 * @param {Object} jsonObject The JSON object to send to the gnubbyd extension.
105 * @param {function(Object)} callback The callback to invoke with reply data.
108 remoting.GnubbyAuthHandler.prototype.sendMessageToGnubbyd_ =
109 function(jsonObject, callback) {
110 var kGnubbydDevExtensionId = 'dlfcjilkjfhdnfiecknlnddkmmiofjbg';
112 chrome.runtime.sendMessage(
113 kGnubbydDevExtensionId,
115 onGnubbydDevReply_.bind(this, jsonObject, callback));
119 * Callback invoked as a result of sending a message to the gnubbyd-dev
120 * extension. If that extension is not installed, reply will be undefined;
121 * otherwise it will be the JSON response object.
122 * @param {Object} jsonObject The JSON object to send to the gnubbyd extension.
123 * @param {function(Object)} callback The callback to invoke with reply data.
124 * @param {Object} reply The reply from the extension (or Chrome, if the
125 * extension does not exist.
128 function onGnubbydDevReply_(jsonObject, callback, reply) {
129 var kGnubbydStableExtensionId = 'beknehfpfkghjoafdifaflglpjkojoco';
134 chrome.runtime.sendMessage(kGnubbydStableExtensionId, jsonObject, callback);