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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
// Copyright 2013 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 Host components via Native Messaging.
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* @constructor
*/
remoting.HostNativeMessaging = function() {
/**
* @type {number}
* @private
*/
this.nextId_ = 0;
/**
* @type {Object.<number, remoting.HostNativeMessaging.PendingReply>}
* @private
*/
this.pendingReplies_ = {};
/** @type {?chrome.extension.Port} @private */
this.port_ = null;
/** @type {string} @private */
this.version_ = '';
/** @type {Array.<remoting.HostController.Feature>} @private */
this.supportedFeatures_ = [];
};
/**
* Type used for entries of |pendingReplies_| list.
*
* @param {string} type Type of the originating request.
* @param {?function(...):void} onDone The callback, if any, to be triggered
* on response. The actual parameters depend on the original request type.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @constructor
*/
remoting.HostNativeMessaging.PendingReply = function(type, onDone, onError) {
this.type = type;
this.onDone = onDone;
this.onError = onError;
};
/**
* Sets up connection to the Native Messaging host process and exchanges
* 'hello' messages. If Native Messaging is not available or the host
* process is not installed, this returns false to the callback.
*
* @param {function(): void} onDone Called after successful initialization.
* @param {function(remoting.Error): void} onError Called if initialization
* failed.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.initialize = function(onDone, onError) {
if (!chrome.runtime.connectNative) {
console.log('Native Messaging API not available');
onError(remoting.Error.UNEXPECTED);
return;
}
// NativeMessaging API exists on Chrome 26.xxx but fails to notify
// onDisconnect in the case where the Host components are not installed. Need
// to blacklist these versions of Chrome.
var majorVersion = navigator.appVersion.match('Chrome/(\\d+)\.')[1];
if (!majorVersion || majorVersion <= 26) {
console.log('Native Messaging not supported on this version of Chrome');
onError(remoting.Error.UNEXPECTED);
return;
}
try {
this.port_ = chrome.runtime.connectNative(
'com.google.chrome.remote_desktop');
this.port_.onMessage.addListener(this.onIncomingMessage_.bind(this));
this.port_.onDisconnect.addListener(this.onDisconnect_.bind(this));
this.postMessage_({type: 'hello'}, onDone,
onError.bind(null, remoting.Error.UNEXPECTED));
} catch (err) {
console.log('Native Messaging initialization failed: ',
/** @type {*} */ (err));
onError(remoting.Error.UNEXPECTED);
return;
}
};
/**
* Verifies that |object| is of type |type|, logging an error if not.
*
* @param {string} name Name of the object, to be included in the error log.
* @param {*} object Object to test.
* @param {string} type Expected type of the object.
* @return {boolean} Result of test.
*/
function checkType_(name, object, type) {
if (typeof(object) !== type) {
console.error('NativeMessaging: "' + name + '" expected to be of type "' +
type + '", got: ' + object);
return false;
}
return true;
}
/**
* Returns |result| as an AsyncResult. If |result| is not valid, returns null
* and logs an error.
*
* @param {*} result
* @return {remoting.HostController.AsyncResult?} Converted result.
*/
function asAsyncResult_(result) {
if (!checkType_('result', result, 'string')) {
return null;
}
if (!remoting.HostController.AsyncResult.hasOwnProperty(result)) {
console.error('NativeMessaging: unexpected result code: ', result);
return null;
}
return remoting.HostController.AsyncResult[result];
}
/**
* Returns |result| as a HostController.State. If |result| is not valid,
* returns null and logs an error.
*
* @param {*} result
* @return {remoting.HostController.State?} Converted result.
*/
function asHostState_(result) {
if (!checkType_('result', result, 'string')) {
return null;
}
if (!remoting.HostController.State.hasOwnProperty(result)) {
console.error('NativeMessaging: unexpected result code: ', result);
return null;
}
return remoting.HostController.State[result];
}
/**
* @param {remoting.HostController.Feature} feature The feature to test for.
* @return {boolean} True if the implementation supports the named feature.
*/
remoting.HostNativeMessaging.prototype.hasFeature = function(feature) {
return this.supportedFeatures_.indexOf(feature) >= 0;
};
/**
* Attaches a new ID to the supplied message, and posts it to the Native
* Messaging port, adding |onDone| to the list of pending replies.
* |message| should have its 'type' field set, and any other fields set
* depending on the message type.
*
* @param {{type: string}} message The message to post.
* @param {?function(...):void} onDone The callback, if any, to be triggered
* on response.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
* @private
*/
remoting.HostNativeMessaging.prototype.postMessage_ =
function(message, onDone, onError) {
var id = this.nextId_++;
message['id'] = id;
this.pendingReplies_[id] = new remoting.HostNativeMessaging.PendingReply(
message.type + 'Response', onDone, onError);
this.port_.postMessage(message);
};
/**
* Handler for incoming Native Messages.
*
* @param {Object} message The received message.
* @return {void} Nothing.
* @private
*/
remoting.HostNativeMessaging.prototype.onIncomingMessage_ = function(message) {
/** @type {number} */
var id = message['id'];
if (typeof(id) != 'number') {
console.error('NativeMessaging: missing or non-numeric id');
return;
}
var reply = this.pendingReplies_[id];
if (!reply) {
console.error('NativeMessaging: unexpected id: ', id);
return;
}
delete this.pendingReplies_[id];
var onDone = reply.onDone;
var onError = reply.onError;
/** @type {string} */
var type = message['type'];
if (!checkType_('type', type, 'string')) {
onError(remoting.Error.UNEXPECTED);
return;
}
if (type != reply.type) {
console.error('NativeMessaging: expected reply type: ', reply.type,
', got: ', type);
onError(remoting.Error.UNEXPECTED);
return;
}
switch (type) {
case 'helloResponse':
/** @type {string} */
var version = message['version'];
if (checkType_('version', version, 'string')) {
this.version_ = version;
if (message['supportedFeatures'] instanceof Array) {
this.supportedFeatures_ = message['supportedFeatures'];
} else {
// Old versions of the native messaging host do not return this list.
// Those versions don't support any new feature.
this.supportedFeatures_ = [];
}
onDone();
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getHostNameResponse':
/** @type {*} */
var hostname = message['hostname'];
if (checkType_('hostname', hostname, 'string')) {
onDone(hostname);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getPinHashResponse':
/** @type {*} */
var hash = message['hash'];
if (checkType_('hash', hash, 'string')) {
onDone(hash);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'generateKeyPairResponse':
/** @type {*} */
var privateKey = message['privateKey'];
/** @type {*} */
var publicKey = message['publicKey'];
if (checkType_('privateKey', privateKey, 'string') &&
checkType_('publicKey', publicKey, 'string')) {
onDone(privateKey, publicKey);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'updateDaemonConfigResponse':
var result = asAsyncResult_(message['result']);
if (result != null) {
onDone(result);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getDaemonConfigResponse':
/** @type {*} */
var config = message['config'];
if (checkType_('config', config, 'object')) {
onDone(config);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getUsageStatsConsentResponse':
/** @type {*} */
var supported = message['supported'];
/** @type {*} */
var allowed = message['allowed'];
/** @type {*} */
var setByPolicy = message['setByPolicy'];
if (checkType_('supported', supported, 'boolean') &&
checkType_('allowed', allowed, 'boolean') &&
checkType_('setByPolicy', setByPolicy, 'boolean')) {
onDone(supported, allowed, setByPolicy);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'startDaemonResponse':
case 'stopDaemonResponse':
var result = asAsyncResult_(message['result']);
if (result != null) {
onDone(result);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getDaemonStateResponse':
var state = asHostState_(message['state']);
if (state != null) {
onDone(state);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getPairedClientsResponse':
var pairedClients = remoting.PairedClient.convertToPairedClientArray(
message['pairedClients']);
if (pairedClients != null) {
onDone(pairedClients);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'clearPairedClientsResponse':
case 'deletePairedClientResponse':
/** @type {boolean} */
var success = message['result'];
if (checkType_('success', success, 'boolean')) {
onDone(success);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getHostClientIdResponse':
/** @type {string} */
var clientId = message['clientId'];
if (checkType_('clientId', clientId, 'string')) {
onDone(clientId);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
case 'getCredentialsFromAuthCodeResponse':
/** @type {string} */
var userEmail = message['userEmail'];
/** @type {string} */
var refreshToken = message['refreshToken'];
if (checkType_('userEmail', userEmail, 'string') && userEmail &&
checkType_('refreshToken', refreshToken, 'string') && refreshToken) {
onDone(userEmail, refreshToken);
} else {
onError(remoting.Error.UNEXPECTED);
}
break;
default:
console.error('Unexpected native message: ', message);
onError(remoting.Error.UNEXPECTED);
}
};
/**
* @return {void} Nothing.
* @private
*/
remoting.HostNativeMessaging.prototype.onDisconnect_ = function() {
console.error('Native Message port disconnected');
// Notify the error-handlers of any requests that are still outstanding.
for (var id in this.pendingReplies_) {
this.pendingReplies_[/** @type {number} */(id)].onError(
remoting.Error.UNEXPECTED);
}
this.pendingReplies_ = {};
}
/**
* @param {function(string):void} onDone Callback to be called with the
* local hostname.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getHostName =
function(onDone, onError) {
this.postMessage_({type: 'getHostName'}, onDone, onError);
};
/**
* Calculates PIN hash value to be stored in the config, passing the resulting
* hash value base64-encoded to the callback.
*
* @param {string} hostId The host ID.
* @param {string} pin The PIN.
* @param {function(string):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getPinHash =
function(hostId, pin, onDone, onError) {
this.postMessage_({
type: 'getPinHash',
hostId: hostId,
pin: pin
}, onDone, onError);
};
/**
* Generates new key pair to use for the host. The specified callback is called
* when the key is generated. The key is returned in format understood by the
* host (PublicKeyInfo structure encoded with ASN.1 DER, and then BASE64).
*
* @param {function(string, string):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.generateKeyPair =
function(onDone, onError) {
this.postMessage_({type: 'generateKeyPair'}, onDone, onError);
};
/**
* Updates host config with the values specified in |config|. All
* fields that are not specified in |config| remain
* unchanged. Following parameters cannot be changed using this
* function: host_id, xmpp_login. Error is returned if |config|
* includes these parameters. Changes take effect before the callback
* is called.
*
* @param {Object} config The new config parameters.
* @param {function(remoting.HostController.AsyncResult):void} onDone
* Callback to be called when finished.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.updateDaemonConfig =
function(config, onDone, onError) {
this.postMessage_({
type: 'updateDaemonConfig',
config: config
}, onDone, onError);
};
/**
* Loads daemon config. The config is passed as a JSON formatted string to the
* callback.
*
* @param {function(Object):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getDaemonConfig =
function(onDone, onError) {
this.postMessage_({type: 'getDaemonConfig'}, onDone, onError);
};
/**
* Retrieves daemon version. The version is returned as a dotted decimal string
* of the form major.minor.build.patch.
* @return {string} The daemon version, or the empty string if not available.
*/
remoting.HostNativeMessaging.prototype.getDaemonVersion = function() {
// Return the cached version from the 'hello' exchange.
return this.version_;
};
/**
* Get the user's consent to crash reporting. The consent flags are passed to
* the callback as booleans: supported, allowed, set-by-policy.
*
* @param {function(boolean, boolean, boolean):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getUsageStatsConsent =
function(onDone, onError) {
this.postMessage_({type: 'getUsageStatsConsent'}, onDone, onError);
};
/**
* Starts the daemon process with the specified configuration.
*
* @param {Object} config Host configuration.
* @param {boolean} consent Consent to report crash dumps.
* @param {function(remoting.HostController.AsyncResult):void} onDone
* Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.startDaemon =
function(config, consent, onDone, onError) {
this.postMessage_({
type: 'startDaemon',
config: config,
consent: consent
}, onDone, onError);
};
/**
* Stops the daemon process.
*
* @param {function(remoting.HostController.AsyncResult):void} onDone
* Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.stopDaemon =
function(onDone, onError) {
this.postMessage_({type: 'stopDaemon'}, onDone, onError);
};
/**
* Gets the installed/running state of the Host process.
*
* @param {function(remoting.HostController.State):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getDaemonState =
function(onDone, onError) {
this.postMessage_({type: 'getDaemonState'}, onDone, onError);
}
/**
* Retrieves the list of paired clients.
*
* @param {function(Array.<remoting.PairedClient>):void} onDone Callback to be
* called with the result.
* @param {function(remoting.Error):void} onError Callback to be triggered
* on error.
*/
remoting.HostNativeMessaging.prototype.getPairedClients =
function(onDone, onError) {
this.postMessage_({type: 'getPairedClients'}, onDone, onError);
}
/**
* Clears all paired clients from the registry.
*
* @param {function(boolean):void} onDone Callback to be called when finished.
* @param {function(remoting.Error):void} onError Callback to be triggered
* on error.
*/
remoting.HostNativeMessaging.prototype.clearPairedClients =
function(onDone, onError) {
this.postMessage_({type: 'clearPairedClients'}, onDone, onError);
}
/**
* Deletes a paired client referenced by client id.
*
* @param {string} client Client to delete.
* @param {function(boolean):void} onDone Callback to be called when finished.
* @param {function(remoting.Error):void} onError Callback to be triggered
* on error.
*/
remoting.HostNativeMessaging.prototype.deletePairedClient =
function(client, onDone, onError) {
this.postMessage_({
type: 'deletePairedClient',
clientId: client
}, onDone, onError);
}
/**
* Gets the API keys to obtain/use service account credentials.
*
* @param {function(string):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getHostClientId =
function(onDone, onError) {
this.postMessage_({type: 'getHostClientId'}, onDone, onError);
};
/**
*
* @param {string} authorizationCode OAuth authorization code.
* @param {function(string, string):void} onDone Callback.
* @param {function(remoting.Error):void} onError The callback to be triggered
* on error.
* @return {void} Nothing.
*/
remoting.HostNativeMessaging.prototype.getCredentialsFromAuthCode =
function(authorizationCode, onDone, onError) {
this.postMessage_({
type: 'getCredentialsFromAuthCode',
authorizationCode: authorizationCode
}, onDone, onError);
};
|