OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * This class provides an interface between the HostSession and either the | 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 | 8 * NativeMessaging Host |
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 * | 9 * |
13 * TODO(sergeyu): Remove this class once the NPAPI plugin is dropped. | 10 * TODO(sergeyu): Remove this class. |
weitao
2014/06/17 23:20:02
Do you plan to deliver on this promise? :)
Sergey Ulanov
2014/06/17 23:30:01
TODO is not a promise. It just refers to a person
| |
14 */ | 11 */ |
15 | 12 |
16 'use strict'; | 13 'use strict'; |
17 | 14 |
18 /** @suppress {duplicate} */ | 15 /** @suppress {duplicate} */ |
19 var remoting = remoting || {}; | 16 var remoting = remoting || {}; |
20 | 17 |
21 /** | 18 /** |
22 * @constructor | 19 * @constructor |
23 */ | 20 */ |
24 remoting.HostIt2MeDispatcher = function() { | 21 remoting.HostIt2MeDispatcher = function() { |
25 /** | 22 /** |
26 * @type {remoting.HostIt2MeNativeMessaging} | 23 * @type {remoting.HostIt2MeNativeMessaging} |
27 * @private */ | 24 * @private */ |
28 this.nativeMessagingHost_ = null; | 25 this.nativeMessagingHost_ = null; |
29 | 26 |
30 /** | 27 /** |
31 * @type {remoting.HostPlugin} | |
32 * @private */ | |
33 this.npapiHost_ = null; | |
34 | |
35 /** | |
36 * @param {remoting.Error} error | 28 * @param {remoting.Error} error |
37 * @private */ | 29 * @private */ |
38 this.onErrorHandler_ = function(error) {} | 30 this.onErrorHandler_ = function(error) {} |
39 }; | 31 }; |
40 | 32 |
41 /** | 33 /** |
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 | 34 * @param {function():void} onDispatcherInitialized Callback to be called after |
46 * initialization has finished successfully. | 35 * initialization has finished successfully. |
47 * @param {function(remoting.Error):void} onDispatcherInitializationFailed | 36 * @param {function(remoting.Error):void} onDispatcherInitializationFailed |
48 * Callback to invoke if neither the native messaging host nor the NPAPI | 37 * Callback to invoke if neither the native messaging host nor the NPAPI |
49 * plugin works. | 38 * plugin works. |
50 */ | 39 */ |
51 remoting.HostIt2MeDispatcher.prototype.initialize = | 40 remoting.HostIt2MeDispatcher.prototype.initialize = |
52 function(createPluginCallback, onDispatcherInitialized, | 41 function(onDispatcherInitialized, |
53 onDispatcherInitializationFailed) { | 42 onDispatcherInitializationFailed) { |
54 /** @type {remoting.HostIt2MeDispatcher} */ | 43 /** @type {remoting.HostIt2MeDispatcher} */ |
55 var that = this; | 44 var that = this; |
56 | 45 |
57 function onNativeMessagingStarted() { | 46 function onNativeMessagingStarted() { |
58 console.log('Native Messaging supported.'); | |
59 | |
60 that.npapiHost_ = null; | |
61 onDispatcherInitialized(); | 47 onDispatcherInitialized(); |
62 } | 48 } |
63 | 49 |
64 function onNativeMessagingInitFailed() { | 50 function onNativeMessagingInitFailed() { |
65 console.log('Native Messaging unsupported, falling back to NPAPI.'); | |
66 | |
67 that.nativeMessagingHost_ = null; | 51 that.nativeMessagingHost_ = null; |
68 that.npapiHost_ = createPluginCallback(); | 52 onDispatcherInitializationFailed(remoting.Error.MISSING_PLUGIN); |
69 | |
70 // TODO(weitaosu): is there a better way to check whether NPAPI plugin is | |
71 // supported? | |
72 if (that.npapiHost_) { | |
73 onDispatcherInitialized(); | |
74 } else { | |
75 onDispatcherInitializationFailed(remoting.Error.MISSING_PLUGIN); | |
76 } | |
77 } | 53 } |
78 | 54 |
79 this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging(); | 55 this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging(); |
80 this.nativeMessagingHost_.initialize(onNativeMessagingStarted, | 56 this.nativeMessagingHost_.initialize(onNativeMessagingStarted, |
81 onNativeMessagingInitFailed, | 57 onNativeMessagingInitFailed, |
82 this.onNativeMessagingError_.bind(this)); | 58 this.onNativeMessagingError_.bind(this)); |
83 } | 59 } |
84 | 60 |
85 /** | 61 /** |
86 * @param {remoting.Error} error | 62 * @param {remoting.Error} error |
87 */ | 63 */ |
88 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ = | 64 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ = |
89 function(error) { | 65 function(error) { |
90 this.nativeMessagingHost_ = null; | 66 this.nativeMessagingHost_ = null; |
91 this.onErrorHandler_(error); | 67 this.onErrorHandler_(error); |
92 } | 68 } |
93 | 69 |
94 /** | 70 /** |
95 * @return {boolean} | |
96 */ | |
97 remoting.HostIt2MeDispatcher.prototype.usingNpapi = function() { | |
98 return this.npapiHost_ != null; | |
99 } | |
100 | |
101 /** | |
102 * @return {remoting.HostPlugin} | |
103 */ | |
104 remoting.HostIt2MeDispatcher.prototype.getNpapiHost = function() { | |
105 return this.npapiHost_; | |
106 } | |
107 | |
108 /** | |
109 * @param {string} email The user's email address. | 71 * @param {string} email The user's email address. |
110 * @param {string} authServiceWithToken Concatenation of the auth service | 72 * @param {string} authServiceWithToken Concatenation of the auth service |
111 * (e.g. oauth2) and the access token. | 73 * (e.g. oauth2) and the access token. |
112 * @param {function(remoting.HostSession.State):void} onStateChanged Callback to | 74 * @param {function(remoting.HostSession.State):void} onStateChanged Callback to |
113 * invoke when the host state changes. | 75 * invoke when the host state changes. |
114 * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when | 76 * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when |
115 * the nat traversal policy changes. | 77 * the nat traversal policy changes. |
116 * @param {function(string):void} logDebugInfo Callback allowing the plugin | 78 * @param {function(string):void} logDebugInfo Callback allowing the plugin |
117 * to log messages to the debug log. | 79 * to log messages to the debug log. |
118 * @param {string} xmppServerAddress XMPP server host name (or IP address) and | 80 * @param {string} xmppServerAddress XMPP server host name (or IP address) and |
119 * port. | 81 * port. |
120 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the | 82 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the |
121 * XMPP server | 83 * XMPP server |
122 * @param {string} directoryBotJid XMPP JID for the remoting directory server | 84 * @param {string} directoryBotJid XMPP JID for the remoting directory server |
123 * bot. | 85 * bot. |
124 * @param {function(remoting.Error):void} onError Callback to invoke in case of | 86 * @param {function(remoting.Error):void} onError Callback to invoke in case of |
125 * an error. | 87 * an error. |
126 */ | 88 */ |
127 remoting.HostIt2MeDispatcher.prototype.connect = | 89 remoting.HostIt2MeDispatcher.prototype.connect = |
128 function(email, authServiceWithToken, onStateChanged, | 90 function(email, authServiceWithToken, onStateChanged, |
129 onNatPolicyChanged, logDebugInfo, xmppServerAddress, | 91 onNatPolicyChanged, logDebugInfo, xmppServerAddress, |
130 xmppServerUseTls, directoryBotJid, onError) { | 92 xmppServerUseTls, directoryBotJid, onError) { |
131 this.onErrorHandler_ = onError; | 93 this.onErrorHandler_ = onError; |
132 if (this.nativeMessagingHost_) { | 94 if (this.nativeMessagingHost_) { |
133 this.nativeMessagingHost_.connect( | 95 this.nativeMessagingHost_.connect( |
134 email, authServiceWithToken, onStateChanged, onNatPolicyChanged, | 96 email, authServiceWithToken, onStateChanged, onNatPolicyChanged, |
135 xmppServerAddress, xmppServerUseTls, directoryBotJid); | 97 xmppServerAddress, xmppServerUseTls, directoryBotJid); |
136 } else if (this.npapiHost_) { | |
137 this.npapiHost_.xmppServerAddress = xmppServerAddress; | |
138 this.npapiHost_.xmppServerUseTls = xmppServerUseTls; | |
139 this.npapiHost_.directoryBotJid = directoryBotJid; | |
140 this.npapiHost_.onStateChanged = onStateChanged; | |
141 this.npapiHost_.onNatTraversalPolicyChanged = onNatPolicyChanged; | |
142 this.npapiHost_.logDebugInfo = logDebugInfo; | |
143 this.npapiHost_.localize(chrome.i18n.getMessage); | |
144 this.npapiHost_.connect(email, authServiceWithToken); | |
145 } else { | 98 } else { |
146 console.error( | 99 console.error( |
147 'remoting.HostIt2MeDispatcher.connect() without initialization.'); | 100 'remoting.HostIt2MeDispatcher.connect() without initialization.'); |
148 onError(remoting.Error.UNEXPECTED); | 101 onError(remoting.Error.UNEXPECTED); |
149 } | 102 } |
150 }; | 103 }; |
151 | 104 |
152 /** | 105 /** |
153 * @return {void} | 106 * @return {void} |
154 */ | 107 */ |
155 remoting.HostIt2MeDispatcher.prototype.disconnect = function() { | 108 remoting.HostIt2MeDispatcher.prototype.disconnect = function() { |
156 if (this.npapiHost_) { | 109 this.nativeMessagingHost_.disconnect(); |
157 this.npapiHost_.disconnect(); | |
158 } else { | |
159 this.nativeMessagingHost_.disconnect(); | |
160 } | |
161 }; | 110 }; |
162 | 111 |
163 /** | 112 /** |
164 * @return {string} The access code generated by the it2me host. | 113 * @return {string} The access code generated by the it2me host. |
165 */ | 114 */ |
166 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() { | 115 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() { |
167 if (this.npapiHost_) { | 116 return this.nativeMessagingHost_.getAccessCode(); |
168 return this.npapiHost_.accessCode; | |
169 } else { | |
170 return this.nativeMessagingHost_.getAccessCode(); | |
171 } | |
172 }; | 117 }; |
173 | 118 |
174 /** | 119 /** |
175 * @return {number} The access code lifetime, in seconds. | 120 * @return {number} The access code lifetime, in seconds. |
176 */ | 121 */ |
177 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() { | 122 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() { |
178 if (this.npapiHost_) { | 123 return this.nativeMessagingHost_.getAccessCodeLifetime(); |
179 return this.npapiHost_.accessCodeLifetime; | |
180 } else { | |
181 return this.nativeMessagingHost_.getAccessCodeLifetime(); | |
182 } | |
183 }; | 124 }; |
184 | 125 |
185 /** | 126 /** |
186 * @return {string} The client's email address. | 127 * @return {string} The client's email address. |
187 */ | 128 */ |
188 remoting.HostIt2MeDispatcher.prototype.getClient = function() { | 129 remoting.HostIt2MeDispatcher.prototype.getClient = function() { |
189 if (this.npapiHost_) { | 130 return this.nativeMessagingHost_.getClient(); |
190 return this.npapiHost_.client; | |
191 } else { | |
192 return this.nativeMessagingHost_.getClient(); | |
193 } | |
194 }; | 131 }; |
195 | |
196 /** | |
197 * @return {void} | |
198 */ | |
199 remoting.HostIt2MeDispatcher.prototype.cleanup = function() { | |
200 if (this.npapiHost_) { | |
201 this.npapiHost_.parentNode.removeChild(this.npapiHost_); | |
202 } | |
203 }; | |
OLD | NEW |