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 * This class provides an interface between the HostSession and either the
8 * NativeMessaging Host or the Host NPAPI plugin, depending on whether or not
9 * NativeMessaging is supported. Since the test for NativeMessaging support is
10 * asynchronous, the connection is attemped on either the the NativeMessaging
11 * host or the NPAPI plugin once the test is complete.
13 * TODO(sergeyu): Remove this class once the NPAPI plugin is dropped.
18 /** @suppress {duplicate} */
19 var remoting = remoting || {};
24 remoting.HostIt2MeDispatcher = function() {
26 * @type {remoting.HostIt2MeNativeMessaging}
28 this.nativeMessagingHost_ = null;
31 * @type {remoting.HostPlugin}
33 this.npapiHost_ = null;
36 * @param {remoting.Error} error
38 this.onErrorHandler_ = function(error) {}
42 * @param {function():remoting.HostPlugin} createPluginCallback Callback to
43 * instantiate the NPAPI plugin when NativeMessaging is determined to be
45 * @param {function():void} onDone Callback to be called after initialization
46 * has finished successfully.
47 * @param {function(remoting.Error):void} onError Callback to invoke if neither
48 * the native messaging host nor the NPAPI plugin works.
50 remoting.HostIt2MeDispatcher.prototype.initialize =
51 function(createPluginCallback, onDone, onError) {
52 /** @type {remoting.HostIt2MeDispatcher} */
55 function onNativeMessagingStarted() {
56 console.log('Native Messaging supported.');
61 * @param {remoting.Error} error
63 function onNativeMessagingFailed(error) {
64 console.log('Native Messaging unsupported, falling back to NPAPI.');
66 that.nativeMessagingHost_ = null;
67 that.npapiHost_ = createPluginCallback();
69 // TODO(weitaosu): is there a better way to check whether NPAPI plugin is
71 if (that.npapiHost_) {
78 this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging();
79 this.nativeMessagingHost_.initialize(onNativeMessagingStarted,
80 onNativeMessagingFailed,
81 this.onNativeMessagingError_.bind(this));
85 * @param {remoting.Error} error
87 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ =
89 this.nativeMessagingHost_ = null;
90 this.onErrorHandler_(error);
94 * @param {string} email The user's email address.
95 * @param {string} authServiceWithToken Concatenation of the auth service
96 * (e.g. oauth2) and the access token.
97 * @param {function(remoting.HostSession.State):void} onStateChanged Callback to
98 * invoke when the host state changes.
99 * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when
100 * the nat traversal policy changes.
101 * @param {function(string):void} logDebugInfo Callback allowing the plugin
102 * to log messages to the debug log.
103 * @param {string} xmppServerAddress XMPP server host name (or IP address) and
105 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
107 * @param {string} directoryBotJid XMPP JID for the remoting directory server
109 * @param {function(remoting.Error):void} onError Callback to invoke in case of
112 remoting.HostIt2MeDispatcher.prototype.connect =
113 function(email, authServiceWithToken, onStateChanged,
114 onNatPolicyChanged, logDebugInfo, xmppServerAddress,
115 xmppServerUseTls, directoryBotJid, onError) {
116 this.onErrorHandler_ = onError;
117 if (this.nativeMessagingHost_) {
118 this.nativeMessagingHost_.connect(
119 email, authServiceWithToken, onStateChanged, onNatPolicyChanged,
120 xmppServerAddress, xmppServerUseTls, directoryBotJid);
121 } else if (this.npapiHost_) {
122 this.npapiHost_.xmppServerAddress = xmppServerAddress;
123 this.npapiHost_.xmppServerUseTls = xmppServerUseTls;
124 this.npapiHost_.directoryBotJid = directoryBotJid;
125 this.npapiHost_.onStateChanged = onStateChanged;
126 this.npapiHost_.onNatTraversalPolicyChanged = onNatPolicyChanged;
127 this.npapiHost_.logDebugInfo = logDebugInfo;
128 this.npapiHost_.localize(chrome.i18n.getMessage);
129 this.npapiHost_.connect(email, authServiceWithToken);
132 'remoting.HostIt2MeDispatcher.connect() without initialization.');
133 onError(remoting.Error.UNEXPECTED);
140 remoting.HostIt2MeDispatcher.prototype.disconnect = function() {
141 if (this.npapiHost_) {
142 this.npapiHost_.disconnect();
144 this.nativeMessagingHost_.disconnect();
149 * @return {string} The access code generated by the it2me host.
151 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() {
152 if (this.npapiHost_) {
153 return this.npapiHost_.accessCode;
155 return this.nativeMessagingHost_.getAccessCode();
160 * @return {number} The access code lifetime, in seconds.
162 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() {
163 if (this.npapiHost_) {
164 return this.npapiHost_.accessCodeLifetime;
166 return this.nativeMessagingHost_.getAccessCodeLifetime();
171 * @return {string} The client's email address.
173 remoting.HostIt2MeDispatcher.prototype.getClient = function() {
174 if (this.npapiHost_) {
175 return this.npapiHost_.client;
177 return this.nativeMessagingHost_.getClient();
184 remoting.HostIt2MeDispatcher.prototype.cleanup = function() {
185 if (this.npapiHost_) {
186 this.npapiHost_.parentNode.removeChild(this.npapiHost_);