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} onDispatcherInitialized Callback to be called after
46 * initialization has finished successfully.
47 * @param {function(remoting.Error):void} onDispatcherInitializationFailed
48 * Callback to invoke if neither the native messaging host nor the NPAPI
51 remoting
.HostIt2MeDispatcher
.prototype.initialize
=
52 function(createPluginCallback
, onDispatcherInitialized
,
53 onDispatcherInitializationFailed
) {
54 /** @type {remoting.HostIt2MeDispatcher} */
57 function onNativeMessagingStarted() {
58 console
.log('Native Messaging supported.');
59 onDispatcherInitialized();
62 function onNativeMessagingInitFailed() {
63 console
.log('Native Messaging unsupported, falling back to NPAPI.');
65 that
.nativeMessagingHost_
= null;
66 that
.npapiHost_
= createPluginCallback();
68 // TODO(weitaosu): is there a better way to check whether NPAPI plugin is
70 if (that
.npapiHost_
) {
71 onDispatcherInitialized();
73 onDispatcherInitializationFailed(remoting
.Error
.MISSING_PLUGIN
);
77 this.nativeMessagingHost_
= new remoting
.HostIt2MeNativeMessaging();
78 this.nativeMessagingHost_
.initialize(onNativeMessagingStarted
,
79 onNativeMessagingInitFailed
,
80 this.onNativeMessagingError_
.bind(this));
84 * @param {remoting.Error} error
86 remoting
.HostIt2MeDispatcher
.prototype.onNativeMessagingError_
=
88 this.nativeMessagingHost_
= null;
89 this.onErrorHandler_(error
);
93 * @param {string} email The user's email address.
94 * @param {string} authServiceWithToken Concatenation of the auth service
95 * (e.g. oauth2) and the access token.
96 * @param {function(remoting.HostSession.State):void} onStateChanged Callback to
97 * invoke when the host state changes.
98 * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when
99 * the nat traversal policy changes.
100 * @param {function(string):void} logDebugInfo Callback allowing the plugin
101 * to log messages to the debug log.
102 * @param {string} xmppServerAddress XMPP server host name (or IP address) and
104 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
106 * @param {string} directoryBotJid XMPP JID for the remoting directory server
108 * @param {function(remoting.Error):void} onError Callback to invoke in case of
111 remoting
.HostIt2MeDispatcher
.prototype.connect
=
112 function(email
, authServiceWithToken
, onStateChanged
,
113 onNatPolicyChanged
, logDebugInfo
, xmppServerAddress
,
114 xmppServerUseTls
, directoryBotJid
, onError
) {
115 this.onErrorHandler_
= onError
;
116 if (this.nativeMessagingHost_
) {
117 this.nativeMessagingHost_
.connect(
118 email
, authServiceWithToken
, onStateChanged
, onNatPolicyChanged
,
119 xmppServerAddress
, xmppServerUseTls
, directoryBotJid
);
120 } else if (this.npapiHost_
) {
121 this.npapiHost_
.xmppServerAddress
= xmppServerAddress
;
122 this.npapiHost_
.xmppServerUseTls
= xmppServerUseTls
;
123 this.npapiHost_
.directoryBotJid
= directoryBotJid
;
124 this.npapiHost_
.onStateChanged
= onStateChanged
;
125 this.npapiHost_
.onNatTraversalPolicyChanged
= onNatPolicyChanged
;
126 this.npapiHost_
.logDebugInfo
= logDebugInfo
;
127 this.npapiHost_
.localize(chrome
.i18n
.getMessage
);
128 this.npapiHost_
.connect(email
, authServiceWithToken
);
131 'remoting.HostIt2MeDispatcher.connect() without initialization.');
132 onError(remoting
.Error
.UNEXPECTED
);
139 remoting
.HostIt2MeDispatcher
.prototype.disconnect = function() {
140 if (this.npapiHost_
) {
141 this.npapiHost_
.disconnect();
143 this.nativeMessagingHost_
.disconnect();
148 * @return {string} The access code generated by the it2me host.
150 remoting
.HostIt2MeDispatcher
.prototype.getAccessCode = function() {
151 if (this.npapiHost_
) {
152 return this.npapiHost_
.accessCode
;
154 return this.nativeMessagingHost_
.getAccessCode();
159 * @return {number} The access code lifetime, in seconds.
161 remoting
.HostIt2MeDispatcher
.prototype.getAccessCodeLifetime = function() {
162 if (this.npapiHost_
) {
163 return this.npapiHost_
.accessCodeLifetime
;
165 return this.nativeMessagingHost_
.getAccessCodeLifetime();
170 * @return {string} The client's email address.
172 remoting
.HostIt2MeDispatcher
.prototype.getClient = function() {
173 if (this.npapiHost_
) {
174 return this.npapiHost_
.client
;
176 return this.nativeMessagingHost_
.getClient();
183 remoting
.HostIt2MeDispatcher
.prototype.cleanup = function() {
184 if (this.npapiHost_
) {
185 this.npapiHost_
.parentNode
.removeChild(this.npapiHost_
);