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 | 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. |
9 * | 12 * |
10 * TODO(sergeyu): Remove this class. | 13 * TODO(sergeyu): Remove this class once the NPAPI plugin is dropped. |
11 */ | 14 */ |
12 | 15 |
13 'use strict'; | 16 'use strict'; |
14 | 17 |
15 /** @suppress {duplicate} */ | 18 /** @suppress {duplicate} */ |
16 var remoting = remoting || {}; | 19 var remoting = remoting || {}; |
17 | 20 |
18 /** | 21 /** |
19 * @constructor | 22 * @constructor |
20 */ | 23 */ |
21 remoting.HostIt2MeDispatcher = function() { | 24 remoting.HostIt2MeDispatcher = function() { |
22 /** | 25 /** |
23 * @type {remoting.HostIt2MeNativeMessaging} | 26 * @type {remoting.HostIt2MeNativeMessaging} |
24 * @private */ | 27 * @private */ |
25 this.nativeMessagingHost_ = null; | 28 this.nativeMessagingHost_ = null; |
26 | 29 |
27 /** | 30 /** |
| 31 * @type {remoting.HostPlugin} |
| 32 * @private */ |
| 33 this.npapiHost_ = null; |
| 34 |
| 35 /** |
28 * @param {remoting.Error} error | 36 * @param {remoting.Error} error |
29 * @private */ | 37 * @private */ |
30 this.onErrorHandler_ = function(error) {} | 38 this.onErrorHandler_ = function(error) {} |
31 }; | 39 }; |
32 | 40 |
33 /** | 41 /** |
| 42 * @param {function():remoting.HostPlugin} createPluginCallback Callback to |
| 43 * instantiate the NPAPI plugin when NativeMessaging is determined to be |
| 44 * unsupported. |
34 * @param {function():void} onDispatcherInitialized Callback to be called after | 45 * @param {function():void} onDispatcherInitialized Callback to be called after |
35 * initialization has finished successfully. | 46 * initialization has finished successfully. |
36 * @param {function(remoting.Error):void} onDispatcherInitializationFailed | 47 * @param {function(remoting.Error):void} onDispatcherInitializationFailed |
37 * Callback to invoke if neither the native messaging host nor the NPAPI | 48 * Callback to invoke if neither the native messaging host nor the NPAPI |
38 * plugin works. | 49 * plugin works. |
39 */ | 50 */ |
40 remoting.HostIt2MeDispatcher.prototype.initialize = | 51 remoting.HostIt2MeDispatcher.prototype.initialize = |
41 function(onDispatcherInitialized, | 52 function(createPluginCallback, onDispatcherInitialized, |
42 onDispatcherInitializationFailed) { | 53 onDispatcherInitializationFailed) { |
43 /** @type {remoting.HostIt2MeDispatcher} */ | 54 /** @type {remoting.HostIt2MeDispatcher} */ |
44 var that = this; | 55 var that = this; |
45 | 56 |
46 function onNativeMessagingStarted() { | 57 function onNativeMessagingStarted() { |
| 58 console.log('Native Messaging supported.'); |
| 59 |
| 60 that.npapiHost_ = null; |
47 onDispatcherInitialized(); | 61 onDispatcherInitialized(); |
48 } | 62 } |
49 | 63 |
50 function onNativeMessagingInitFailed() { | 64 function onNativeMessagingInitFailed() { |
| 65 console.log('Native Messaging unsupported, falling back to NPAPI.'); |
| 66 |
51 that.nativeMessagingHost_ = null; | 67 that.nativeMessagingHost_ = null; |
52 onDispatcherInitializationFailed(remoting.Error.MISSING_PLUGIN); | 68 that.npapiHost_ = createPluginCallback(); |
| 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 } |
53 } | 77 } |
54 | 78 |
55 this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging(); | 79 this.nativeMessagingHost_ = new remoting.HostIt2MeNativeMessaging(); |
56 this.nativeMessagingHost_.initialize(onNativeMessagingStarted, | 80 this.nativeMessagingHost_.initialize(onNativeMessagingStarted, |
57 onNativeMessagingInitFailed, | 81 onNativeMessagingInitFailed, |
58 this.onNativeMessagingError_.bind(this)); | 82 this.onNativeMessagingError_.bind(this)); |
59 } | 83 } |
60 | 84 |
61 /** | 85 /** |
62 * @param {remoting.Error} error | 86 * @param {remoting.Error} error |
63 */ | 87 */ |
64 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ = | 88 remoting.HostIt2MeDispatcher.prototype.onNativeMessagingError_ = |
65 function(error) { | 89 function(error) { |
66 this.nativeMessagingHost_ = null; | 90 this.nativeMessagingHost_ = null; |
67 this.onErrorHandler_(error); | 91 this.onErrorHandler_(error); |
68 } | 92 } |
69 | 93 |
70 /** | 94 /** |
| 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 /** |
71 * @param {string} email The user's email address. | 109 * @param {string} email The user's email address. |
72 * @param {string} authServiceWithToken Concatenation of the auth service | 110 * @param {string} authServiceWithToken Concatenation of the auth service |
73 * (e.g. oauth2) and the access token. | 111 * (e.g. oauth2) and the access token. |
74 * @param {function(remoting.HostSession.State):void} onStateChanged Callback to | 112 * @param {function(remoting.HostSession.State):void} onStateChanged Callback to |
75 * invoke when the host state changes. | 113 * invoke when the host state changes. |
76 * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when | 114 * @param {function(boolean):void} onNatPolicyChanged Callback to invoke when |
77 * the nat traversal policy changes. | 115 * the nat traversal policy changes. |
78 * @param {function(string):void} logDebugInfo Callback allowing the plugin | 116 * @param {function(string):void} logDebugInfo Callback allowing the plugin |
79 * to log messages to the debug log. | 117 * to log messages to the debug log. |
80 * @param {string} xmppServerAddress XMPP server host name (or IP address) and | 118 * @param {string} xmppServerAddress XMPP server host name (or IP address) and |
81 * port. | 119 * port. |
82 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the | 120 * @param {boolean} xmppServerUseTls Whether to use TLS on connections to the |
83 * XMPP server | 121 * XMPP server |
84 * @param {string} directoryBotJid XMPP JID for the remoting directory server | 122 * @param {string} directoryBotJid XMPP JID for the remoting directory server |
85 * bot. | 123 * bot. |
86 * @param {function(remoting.Error):void} onError Callback to invoke in case of | 124 * @param {function(remoting.Error):void} onError Callback to invoke in case of |
87 * an error. | 125 * an error. |
88 */ | 126 */ |
89 remoting.HostIt2MeDispatcher.prototype.connect = | 127 remoting.HostIt2MeDispatcher.prototype.connect = |
90 function(email, authServiceWithToken, onStateChanged, | 128 function(email, authServiceWithToken, onStateChanged, |
91 onNatPolicyChanged, logDebugInfo, xmppServerAddress, | 129 onNatPolicyChanged, logDebugInfo, xmppServerAddress, |
92 xmppServerUseTls, directoryBotJid, onError) { | 130 xmppServerUseTls, directoryBotJid, onError) { |
93 this.onErrorHandler_ = onError; | 131 this.onErrorHandler_ = onError; |
94 if (this.nativeMessagingHost_) { | 132 if (this.nativeMessagingHost_) { |
95 this.nativeMessagingHost_.connect( | 133 this.nativeMessagingHost_.connect( |
96 email, authServiceWithToken, onStateChanged, onNatPolicyChanged, | 134 email, authServiceWithToken, onStateChanged, onNatPolicyChanged, |
97 xmppServerAddress, xmppServerUseTls, directoryBotJid); | 135 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); |
98 } else { | 145 } else { |
99 console.error( | 146 console.error( |
100 'remoting.HostIt2MeDispatcher.connect() without initialization.'); | 147 'remoting.HostIt2MeDispatcher.connect() without initialization.'); |
101 onError(remoting.Error.UNEXPECTED); | 148 onError(remoting.Error.UNEXPECTED); |
102 } | 149 } |
103 }; | 150 }; |
104 | 151 |
105 /** | 152 /** |
106 * @return {void} | 153 * @return {void} |
107 */ | 154 */ |
108 remoting.HostIt2MeDispatcher.prototype.disconnect = function() { | 155 remoting.HostIt2MeDispatcher.prototype.disconnect = function() { |
109 this.nativeMessagingHost_.disconnect(); | 156 if (this.npapiHost_) { |
| 157 this.npapiHost_.disconnect(); |
| 158 } else { |
| 159 this.nativeMessagingHost_.disconnect(); |
| 160 } |
110 }; | 161 }; |
111 | 162 |
112 /** | 163 /** |
113 * @return {string} The access code generated by the it2me host. | 164 * @return {string} The access code generated by the it2me host. |
114 */ | 165 */ |
115 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() { | 166 remoting.HostIt2MeDispatcher.prototype.getAccessCode = function() { |
116 return this.nativeMessagingHost_.getAccessCode(); | 167 if (this.npapiHost_) { |
| 168 return this.npapiHost_.accessCode; |
| 169 } else { |
| 170 return this.nativeMessagingHost_.getAccessCode(); |
| 171 } |
117 }; | 172 }; |
118 | 173 |
119 /** | 174 /** |
120 * @return {number} The access code lifetime, in seconds. | 175 * @return {number} The access code lifetime, in seconds. |
121 */ | 176 */ |
122 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() { | 177 remoting.HostIt2MeDispatcher.prototype.getAccessCodeLifetime = function() { |
123 return this.nativeMessagingHost_.getAccessCodeLifetime(); | 178 if (this.npapiHost_) { |
| 179 return this.npapiHost_.accessCodeLifetime; |
| 180 } else { |
| 181 return this.nativeMessagingHost_.getAccessCodeLifetime(); |
| 182 } |
124 }; | 183 }; |
125 | 184 |
126 /** | 185 /** |
127 * @return {string} The client's email address. | 186 * @return {string} The client's email address. |
128 */ | 187 */ |
129 remoting.HostIt2MeDispatcher.prototype.getClient = function() { | 188 remoting.HostIt2MeDispatcher.prototype.getClient = function() { |
130 return this.nativeMessagingHost_.getClient(); | 189 if (this.npapiHost_) { |
| 190 return this.npapiHost_.client; |
| 191 } else { |
| 192 return this.nativeMessagingHost_.getClient(); |
| 193 } |
131 }; | 194 }; |
| 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 |