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. 4 5/** 6 * @fileoverview 7 * Class that routes gnubby-auth extension messages to and from the gnubbyd 8 * extension. 9 */ 10 11'use strict'; 12 13/** @suppress {duplicate} */ 14var remoting = remoting || {}; 15 16/** 17 * @constructor 18 * @param {!remoting.ClientSession} clientSession The client session to send 19 * gnubby-auth response messages to. 20 */ 21remoting.GnubbyAuthHandler = function(clientSession) { 22 this.clientSession_ = clientSession; 23}; 24 25/** 26 * Processes gnubby-auth messages. 27 * @param {string} data The gnubby-auth message data. 28 */ 29remoting.GnubbyAuthHandler.prototype.onMessage = function(data) { 30 var message = getJsonObjectFromString(data); 31 var messageType = getStringAttr(message, 'type'); 32 if (messageType == 'data') { 33 this.sendMessageToGnubbyd_({ 34 'type': 'auth-agent@openssh.com', 35 'data': getArrayAttr(message, 'data') 36 }, this.callback_.bind(this, getNumberAttr(message, 'connectionId'))); 37 } else { 38 console.error('Invalid gnubby-auth message: ' + messageType); 39 } 40}; 41 42/** 43 * Callback invoked with data to be returned to the host. 44 * @param {number} connectionId The connection id. 45 * @param {Object} response The JSON response with the data to send to the host. 46 * @private 47 */ 48remoting.GnubbyAuthHandler.prototype.callback_ = 49 function(connectionId, response) { 50 try { 51 this.clientSession_.sendGnubbyAuthMessage({ 52 'type': 'data', 53 'connectionId': connectionId, 54 'data': getArrayAttr(response, 'data') 55 }); 56 } catch (err) { 57 console.error('gnubby callback failed: ', /** @type {*} */ (err)); 58 this.clientSession_.sendGnubbyAuthMessage({ 59 'type': 'error', 60 'connectionId': connectionId 61 }); 62 return; 63 } 64}; 65 66/** 67 * Send data to the gnubbyd extension. 68 * @param {Object} jsonObject The JSON object to send to the gnubbyd extension. 69 * @param {function(Object)} callback The callback to invoke with reply data. 70 * @private 71 */ 72remoting.GnubbyAuthHandler.prototype.sendMessageToGnubbyd_ = 73 function(jsonObject, callback) { 74 var kGnubbydDevExtensionId = 'dlfcjilkjfhdnfiecknlnddkmmiofjbg'; 75 76 chrome.runtime.sendMessage( 77 kGnubbydDevExtensionId, 78 jsonObject, 79 onGnubbydDevReply_.bind(this, jsonObject, callback)); 80}; 81 82/** 83 * Callback invoked as a result of sending a message to the gnubbyd-dev 84 * extension. If that extension is not installed, reply will be undefined; 85 * otherwise it will be the JSON response object. 86 * @param {Object} jsonObject The JSON object to send to the gnubbyd extension. 87 * @param {function(Object)} callback The callback to invoke with reply data. 88 * @param {Object} reply The reply from the extension (or Chrome, if the 89 * extension does not exist. 90 * @private 91 */ 92function onGnubbydDevReply_(jsonObject, callback, reply) { 93 var kGnubbydStableExtensionId = 'beknehfpfkghjoafdifaflglpjkojoco'; 94 95 if (reply) { 96 callback(reply); 97 } else { 98 chrome.runtime.sendMessage(kGnubbydStableExtensionId, jsonObject, callback); 99 } 100} 101