gnubby_auth_handler.js revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @fileoverview 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Class that routes gnubby-auth extension messages to and from the gnubbyd 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * extension. 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)'use strict'; 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** @suppress {duplicate} */ 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)var remoting = remoting || {}; 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @constructor 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {!remoting.ClientSession} clientSession The client session to send 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * gnubby-auth response messages to. 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)remoting.GnubbyAuthHandler = function(clientSession) { 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) this.clientSession_ = clientSession; 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Processes gnubby-auth messages. 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {string} data The gnubby-auth message data. 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)remoting.GnubbyAuthHandler.prototype.onMessage = function(data) { 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) var message = getJsonObjectFromString(data); 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) var messageType = getStringAttr(message, 'type'); 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (messageType == 'data') { 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) this.sendMessageToGnubbyd_( 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) getJsonObjectFromString(getStringAttr(message, 'jsonMessage')), 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) this.callback_.bind(this, getNumberAttr(message, 'connectionId'))); 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } else { 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) console.error('Invalid gnubby-auth message: ' + messageType); 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Callback invoked with data to be returned to the host. 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {number} connectionId The connection id. 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {Object} data The JSON object to send to the host. 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @private 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)remoting.GnubbyAuthHandler.prototype.callback_ = 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) function(connectionId, data) { 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) var json_data = JSON.stringify(data); 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) this.clientSession_.sendGnubbyAuthMessage({'type': 'data', 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 'connectionId': connectionId, 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 'jsonMessage': json_data}); 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Send data to the gnubbyd extension. 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {Object} jsonObject The JSON object to send to the gnubbyd extension. 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {function(Object)} callback The callback to invoke with reply data. 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @private 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)remoting.GnubbyAuthHandler.prototype.sendMessageToGnubbyd_ = 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) function(jsonObject, callback) { 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) var kGnubbydDevExtensionId = 'dlfcjilkjfhdnfiecknlnddkmmiofjbg'; 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) chrome.runtime.sendMessage( 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) kGnubbydDevExtensionId, 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) jsonObject, 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) onGnubbydDevReply_.bind(this, jsonObject, callback)); 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Callback invoked as a result of sending a message to the gnubbyd-dev 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * extension. If that extension is not installed, reply will be undefined; 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * otherwise it will be the JSON response object. 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {Object} jsonObject The JSON object to send to the gnubbyd extension. 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {function(Object)} callback The callback to invoke with reply data. 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @param {Object} reply The reply from the extension (or Chrome, if the 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * extension does not exist. 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * @private 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)function onGnubbydDevReply_(jsonObject, callback, reply) { 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) var kGnubbydStableExtensionId = 'beknehfpfkghjoafdifaflglpjkojoco'; 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (reply) { 85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) callback(reply); 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } else { 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) chrome.runtime.sendMessage(kGnubbydStableExtensionId, jsonObject, callback); 88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 90