-
Notifications
You must be signed in to change notification settings - Fork 8
/
it2me_host_facade.js
333 lines (292 loc) · 9.35 KB
/
it2me_host_facade.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* Class to communicate with the It2me Host component via Native Messaging.
*/
/** @suppress {duplicate} */
var remoting = remoting || {};
(function() {
'use strict';
/**
* @constructor
* @implements {base.Disposable}
*/
remoting.It2MeHostFacade = function() {
/** @private {number} */
this.nextId_ = 0;
/** @private {?chrome.runtime.Port} */
this.port_ = null;
/** @private {string} */
this.accessCode_ = '';
/** @private {number} */
this.accessCodeLifetime_ = 0;
/** @private {string} */
this.clientId_ = '';
/** @private {boolean} */
this.initialized_ = false;
/** @private {base.Disposables} */
this.eventHooks_ = null;
/**
* @type {?function():void}
* @private
*/
this.onInitialized_ = function() {};
/**
* Called if Native Messaging host has failed to start.
* @private
* */
this.onInitializationFailed_ = function() {};
/**
* Called if the It2Me Native Messaging host sends a malformed message:
* missing required attributes, attributes with incorrect types, etc.
* @type {?function(remoting.Error):void}
* @private
*/
this.onError_ = function(error) {};
/**
* @type {?function(remoting.HostSession.State):void}
* @private
*/
this.onStateChanged_ = function() {};
/**
* @type {?function(boolean):void}
* @private
*/
this.onNatPolicyChanged_ = function() {};
};
remoting.It2MeHostFacade.prototype.dispose = function() {
base.dispose(this.eventHooks_);
this.eventHooks_ = null;
if (this.port_) {
this.port_.disconnect();
this.port_ = null;
}
};
/**
* Sets up connection to the Native Messaging host process and exchanges
* 'hello' messages. If Native Messaging is not supported or if the it2me
* native messaging host is not installed, onInitializationFailed is invoked.
* Otherwise, onInitialized is invoked.
*
* @param {function(*=):void} onInitialized Called after successful
* initialization.
* @param {function(*=):void} onInitializationFailed Called if cannot connect to
* the native messaging host.
* @return {void}
*/
remoting.It2MeHostFacade.prototype.initialize =
function(onInitialized, onInitializationFailed) {
this.onInitialized_ = onInitialized;
this.onInitializationFailed_ = onInitializationFailed;
try {
this.port_ = chrome.runtime.connectNative(
'com.google.chrome.remote_assistance');
this.eventHooks_ = new base.Disposables(
new base.ChromeEventHook(this.port_.onMessage,
this.onIncomingMessage_.bind(this)),
new base.ChromeEventHook(this.port_.onDisconnect,
this.onHostDisconnect_.bind(this)));
this.port_.postMessage({type: 'hello'});
} catch (/** @type {*} */ err) {
console.log('Native Messaging initialization failed: ', err);
onInitializationFailed();
return;
}
};
/**
* @param {string} email The user's email address.
* @param {string} authServiceWithToken Concatenation of the auth service
* (e.g. oauth2) and the access token.
* @param {function(remoting.HostSession.State):void} onStateChanged Callback to
* invoke when the host state changes.
* @param {function(boolean):void} onNatPolicyChanged Callback to invoke when
* the nat traversal policy changes.
* @param {function(string):void} logDebugInfo Callback allowing the plugin
* to log messages to the debug log.
* @param {string} xmppServerAddress XMPP server host name (or IP address) and
* port.
* @param {boolean} xmppServerUseTls Whether to use TLS on connections to the
* XMPP server
* @param {string} directoryBotJid XMPP JID for the remoting directory server
* bot.
* @param {function(remoting.Error):void} onError Callback to invoke in case of
* an error.
* @return {void}
*/
remoting.It2MeHostFacade.prototype.connect =
function(email, authServiceWithToken, onStateChanged, onNatPolicyChanged,
logDebugInfo, xmppServerAddress, xmppServerUseTls, directoryBotJid,
onError) {
if (!this.port_) {
console.error(
'remoting.It2MeHostFacade.connect() without initialization.');
onError(remoting.Error.UNEXPECTED);
return;
}
this.onStateChanged_ = onStateChanged;
this.onNatPolicyChanged_ = onNatPolicyChanged;
this.onError_ = onError;
this.port_.postMessage({
type: 'connect',
userName: email,
authServiceWithToken: authServiceWithToken,
xmppServerAddress: xmppServerAddress,
xmppServerUseTls: xmppServerUseTls,
directoryBotJid: directoryBotJid
});
};
/**
* Unhooks the |onStateChanged|, |onError|, |onNatPolicyChanged| and
* |onInitalized| callbacks. This is called when the client shuts down so that
* the callbacks will not be invoked on a disposed client.
*
* @return {void}
*/
remoting.It2MeHostFacade.prototype.unhookCallbacks = function() {
this.onStateChanged_ = null;
this.onNatPolicyChanged_ = null;
this.onError_ = null;
this.onInitialized_ = null;
};
/**
* @return {void}
*/
remoting.It2MeHostFacade.prototype.disconnect = function() {
if (this.port_)
this.port_.postMessage({type: 'disconnect'});
};
/**
* @return {boolean}
*/
remoting.It2MeHostFacade.prototype.initialized = function() {
return this.initialized_;
};
/**
* @return {string}
*/
remoting.It2MeHostFacade.prototype.getAccessCode = function() {
return this.accessCode_;
};
/**
* @return {number}
*/
remoting.It2MeHostFacade.prototype.getAccessCodeLifetime = function() {
return this.accessCodeLifetime_;
};
/**
* @return {string}
*/
remoting.It2MeHostFacade.prototype.getClient = function() {
return this.clientId_;
};
/**
* Handler for incoming messages.
*
* @param {Object} message The received message.
* @return {void}
* @private
*/
remoting.It2MeHostFacade.prototype.onIncomingMessage_ =
function(message) {
var type = getStringAttr(message, 'type');
switch (type) {
case 'helloResponse':
var version = getStringAttr(message, 'version');
console.log('Host version: ', version);
this.initialized_ = true;
// A "hello" request is sent immediately after the native messaging host
// is started. We can proceed to the next task once we receive the
// "helloReponse".
if (this.onInitialized_) {
this.onInitialized_();
}
break;
case 'connectResponse':
console.log('connectResponse received');
// Response to the "connect" request. No action is needed until we
// receive the corresponding "hostStateChanged" message.
break;
case 'disconnectResponse':
console.log('disconnectResponse received');
// Response to the "disconnect" request. No action is needed until we
// receive the corresponding "hostStateChanged" message.
break;
case 'hostStateChanged':
var stateString = getStringAttr(message, 'state');
console.log('hostStateChanged received: ', stateString);
var state = remoting.HostSession.State.fromString(stateString);
switch (state) {
case remoting.HostSession.State.RECEIVED_ACCESS_CODE:
var accessCode = getStringAttr(message, 'accessCode');
var accessCodeLifetime = getNumberAttr(message, 'accessCodeLifetime');
this.onReceivedAccessCode_(accessCode, accessCodeLifetime);
break;
case remoting.HostSession.State.CONNECTED:
var client = getStringAttr(message, 'client');
this.onConnected_(client);
break;
}
if (this.onStateChanged_) {
this.onStateChanged_(state);
}
break;
case 'natPolicyChanged':
if (this.onNatPolicyChanged_) {
var natTraversalEnabled =
getBooleanAttr(message, 'natTraversalEnabled');
this.onNatPolicyChanged_(natTraversalEnabled);
}
break;
case 'error':
console.error(getStringAttr(message, 'description'));
if (this.onError_) {
this.onError_(remoting.Error.UNEXPECTED);
}
break;
default:
throw 'Unexpected native message: ' + message;
}
};
/**
* @param {string} accessCode
* @param {number} accessCodeLifetime
* @return {void}
* @private
*/
remoting.It2MeHostFacade.prototype.onReceivedAccessCode_ =
function(accessCode, accessCodeLifetime) {
this.accessCode_ = accessCode;
this.accessCodeLifetime_ = accessCodeLifetime;
};
/**
* @param {string} clientId
* @return {void}
* @private
*/
remoting.It2MeHostFacade.prototype.onConnected_ = function(clientId) {
this.clientId_ = clientId;
};
/**
* @return {void}
* @private
*/
remoting.It2MeHostFacade.prototype.onHostDisconnect_ = function() {
if (!this.initialized_) {
// If the host is disconnected before it is initialized, it probably means
// the host is not propertly installed (or not installed at all).
// E.g., if the host manifest is not present we get "Specified native
// messaging host not found" error. If the host manifest is present but
// the host binary cannot be found we get the "Native host has exited"
// error.
console.log('Native Messaging initialization failed: ' +
chrome.runtime.lastError.message);
this.onInitializationFailed_();
} else {
console.error('Native Messaging port disconnected.');
this.port_ = null;
this.onError_(remoting.Error.UNEXPECTED);
}
};
})();