Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / webapp / host_it2me_dispatcher.js
blobf2a7e90c146d9d46d5ac4a4020238ec2bbfc9e31
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.
5 /**
6 * @fileoverview
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.
16 'use strict';
18 /** @suppress {duplicate} */
19 var remoting = remoting || {};
21 /**
22 * @constructor
24 remoting.HostIt2MeDispatcher = function() {
25 /**
26 * @type {remoting.HostIt2MeNativeMessaging}
27 * @private */
28 this.nativeMessagingHost_ = null;
30 /**
31 * @type {remoting.HostPlugin}
32 * @private */
33 this.npapiHost_ = null;
35 /**
36 * @param {remoting.Error} error
37 * @private */
38 this.onErrorHandler_ = function(error) {}
41 /**
42 * @param {function():remoting.HostPlugin} createPluginCallback Callback to
43 * instantiate the NPAPI plugin when NativeMessaging is determined to be
44 * unsupported.
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
49 * plugin works.
51 remoting.HostIt2MeDispatcher.prototype.initialize =
52 function(createPluginCallback, onDispatcherInitialized,
53 onDispatcherInitializationFailed) {
54 /** @type {remoting.HostIt2MeDispatcher} */
55 var that = this;
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
69 // supported?
70 if (that.npapiHost_) {
71 onDispatcherInitialized();
72 } else {
73 onDispatcherInitializationFailed(remoting.Error.MISSING_PLUGIN);
77 this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging();
78 this.nativeMessagingHost_.initialize(onNativeMessagingStarted,
79 onNativeMessagingInitFailed,
80 this.onNativeMessagingError_.bind(this));
83 /**
84 * @param {remoting.Error} error
86 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ =
87 function(error) {
88 this.nativeMessagingHost_ = null;
89 this.onErrorHandler_(error);
92 /**
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
103 * port.
104 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
105 * XMPP server
106 * @param {string} directoryBotJid XMPP JID for the remoting directory server
107 * bot.
108 * @param {function(remoting.Error):void} onError Callback to invoke in case of
109 * an error.
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);
129 } else {
130 console.error(
131 'remoting.HostIt2MeDispatcher.connect() without initialization.');
132 onError(remoting.Error.UNEXPECTED);
137 * @return {void}
139 remoting.HostIt2MeDispatcher.prototype.disconnect = function() {
140 if (this.npapiHost_) {
141 this.npapiHost_.disconnect();
142 } else {
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;
153 } else {
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;
164 } else {
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;
175 } else {
176 return this.nativeMessagingHost_.getClient();
181 * @return {void}
183 remoting.HostIt2MeDispatcher.prototype.cleanup = function() {
184 if (this.npapiHost_) {
185 this.npapiHost_.parentNode.removeChild(this.npapiHost_);