Home | History | Annotate | Download | only in webapp
      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  * This class provides an interface between the HostSession and either the
      8  * NativeMessaging Host
      9  *
     10  * TODO(sergeyu): Remove this class.
     11  */
     12 
     13 'use strict';
     14 
     15 /** @suppress {duplicate} */
     16 var remoting = remoting || {};
     17 
     18 /**
     19  * @constructor
     20  */
     21 remoting.HostIt2MeDispatcher = function() {
     22   /**
     23    * @type {remoting.HostIt2MeNativeMessaging}
     24    * @private */
     25   this.nativeMessagingHost_ = null;
     26 
     27   /**
     28    * @param {remoting.Error} error
     29    * @private */
     30   this.onErrorHandler_ = function(error) {}
     31 };
     32 
     33 /**
     34  * @param {function():void} onDispatcherInitialized Callback to be called after
     35  *     initialization has finished successfully.
     36  * @param {function(remoting.Error):void} onDispatcherInitializationFailed
     37  *     Callback to invoke if neither the native messaging host nor the NPAPI
     38  *     plugin works.
     39  */
     40 remoting.HostIt2MeDispatcher.prototype.initialize =
     41     function(onDispatcherInitialized,
     42              onDispatcherInitializationFailed) {
     43   /** @type {remoting.HostIt2MeDispatcher} */
     44   var that = this;
     45 
     46   function onNativeMessagingStarted() {
     47     onDispatcherInitialized();
     48   }
     49 
     50   function onNativeMessagingInitFailed() {
     51     that.nativeMessagingHost_ = null;
     52     onDispatcherInitializationFailed(remoting.Error.MISSING_PLUGIN);
     53   }
     54 
     55   this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging();
     56   this.nativeMessagingHost_.initialize(onNativeMessagingStarted,
     57                                        onNativeMessagingInitFailed,
     58                                        this.onNativeMessagingError_.bind(this));
     59 }
     60 
     61 /**
     62  * @param {remoting.Error} error
     63  */
     64 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ =
     65     function(error) {
     66   this.nativeMessagingHost_ = null;
     67   this.onErrorHandler_(error);
     68 }
     69 
     70 /**
     71  * @param {string} email The user's email address.
     72  * @param {string} authServiceWithToken Concatenation of the auth service
     73  *     (e.g. oauth2) and the access token.
     74  * @param {function(remoting.HostSession.State):void} onStateChanged Callback to
     75  *     invoke when the host state changes.
     76  * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when
     77  *     the nat traversal policy changes.
     78  * @param {function(string):void} logDebugInfo Callback allowing the plugin
     79  *     to log messages to the debug log.
     80  * @param {string} xmppServerAddress XMPP server host name (or IP address) and
     81  *     port.
     82  * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
     83  *     XMPP server
     84  * @param {string} directoryBotJid XMPP JID for the remoting directory server
     85  *     bot.
     86  * @param {function(remoting.Error):void} onError Callback to invoke in case of
     87  *     an error.
     88  */
     89 remoting.HostIt2MeDispatcher.prototype.connect =
     90     function(email, authServiceWithToken, onStateChanged,
     91              onNatPolicyChanged, logDebugInfo, xmppServerAddress,
     92              xmppServerUseTls, directoryBotJid, onError) {
     93   this.onErrorHandler_ = onError;
     94   if (this.nativeMessagingHost_) {
     95     this.nativeMessagingHost_.connect(
     96         email, authServiceWithToken, onStateChanged, onNatPolicyChanged,
     97         xmppServerAddress, xmppServerUseTls, directoryBotJid);
     98   } else {
     99     console.error(
    100         'remoting.HostIt2MeDispatcher.connect() without initialization.');
    101     onError(remoting.Error.UNEXPECTED);
    102   }
    103 };
    104 
    105 /**
    106  * @return {void}
    107  */
    108 remoting.HostIt2MeDispatcher.prototype.disconnect = function() {
    109   this.nativeMessagingHost_.disconnect();
    110 };
    111 
    112 /**
    113  * @return {string} The access code generated by the it2me host.
    114  */
    115 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() {
    116   return this.nativeMessagingHost_.getAccessCode();
    117 };
    118 
    119 /**
    120  * @return {number} The access code lifetime, in seconds.
    121  */
    122 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() {
    123   return this.nativeMessagingHost_.getAccessCodeLifetime();
    124 };
    125 
    126 /**
    127  * @return {string} The client's email address.
    128  */
    129 remoting.HostIt2MeDispatcher.prototype.getClient = function() {
    130   return this.nativeMessagingHost_.getClient();
    131 };
    132