Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / remoting / webapp / host_it2me_dispatcher.js
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 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.
12  *
13  * TODO(sergeyu): Remove this class once the NPAPI plugin is dropped.
14  */
15
16 'use strict';
17
18 /** @suppress {duplicate} */
19 var remoting = remoting || {};
20
21 /**
22  * @constructor
23  */
24 remoting.HostIt2MeDispatcher = function() {
25   /**
26    * @type {remoting.HostIt2MeNativeMessaging}
27    * @private */
28   this.nativeMessagingHost_ = null;
29
30   /**
31    * @type {remoting.HostPlugin}
32    * @private */
33   this.npapiHost_ = null;
34
35   /**
36    * @param {remoting.Error} error
37    * @private */
38   this.onErrorHandler_ = function(error) {}
39 };
40
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} 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.
49  */
50 remoting.HostIt2MeDispatcher.prototype.initialize =
51     function(createPluginCallback, onDone, onError) {
52   /** @type {remoting.HostIt2MeDispatcher} */
53   var that = this;
54
55   function onNativeMessagingStarted() {
56     console.log('Native Messaging supported.');
57     onDone();
58   }
59
60   /**
61    * @param {remoting.Error} error
62    */
63   function onNativeMessagingFailed(error) {
64     console.log('Native Messaging unsupported, falling back to NPAPI.');
65
66     that.nativeMessagingHost_ = null;
67     that.npapiHost_ = createPluginCallback();
68
69     // TODO(weitaosu): is there a better way to check whether NPAPI plugin is
70     // supported?
71     if (that.npapiHost_) {
72       onDone();
73     } else {
74       onError(error);
75     }
76   }
77
78   this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging();
79   this.nativeMessagingHost_.initialize(onNativeMessagingStarted,
80                                        onNativeMessagingFailed,
81                                        this.onNativeMessagingError_.bind(this));
82 }
83
84 /**
85  * @param {remoting.Error} error
86  */
87 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ =
88     function(error) {
89   this.nativeMessagingHost_ = null;
90   this.onErrorHandler_(error);
91 }
92
93 /**
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
104  *     port.
105  * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
106  *     XMPP server
107  * @param {string} directoryBotJid XMPP JID for the remoting directory server
108  *     bot.
109  * @param {function(remoting.Error):void} onError Callback to invoke in case of
110  *     an error.
111  */
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);
130   } else {
131     console.error(
132         'remoting.HostIt2MeDispatcher.connect() without initialization.');
133     onError(remoting.Error.UNEXPECTED);
134   }
135 };
136
137 /**
138  * @return {void}
139  */
140 remoting.HostIt2MeDispatcher.prototype.disconnect = function() {
141   if (this.npapiHost_) {
142     this.npapiHost_.disconnect();
143   } else {
144     this.nativeMessagingHost_.disconnect();
145   }
146 };
147
148 /**
149  * @return {string} The access code generated by the it2me host.
150  */
151 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() {
152   if (this.npapiHost_) {
153     return this.npapiHost_.accessCode;
154   } else {
155     return this.nativeMessagingHost_.getAccessCode();
156   }
157 };
158
159 /**
160  * @return {number} The access code lifetime, in seconds.
161  */
162 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() {
163   if (this.npapiHost_) {
164     return this.npapiHost_.accessCodeLifetime;
165   } else {
166     return this.nativeMessagingHost_.getAccessCodeLifetime();
167   }
168 };
169
170 /**
171  * @return {string} The client's email address.
172  */
173 remoting.HostIt2MeDispatcher.prototype.getClient = function() {
174   if (this.npapiHost_) {
175     return this.npapiHost_.client;
176   } else {
177     return this.nativeMessagingHost_.getClient();
178   }
179 };
180
181 /**
182  * @return {void}
183  */
184 remoting.HostIt2MeDispatcher.prototype.cleanup = function() {
185   if (this.npapiHost_) {
186     this.npapiHost_.parentNode.removeChild(this.npapiHost_);
187   }
188 };