summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/host_native_messaging.js
blob: 64576a5194bc7f8f82ceb96a0008d74616b0d50f (plain)
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
// 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
 * @extends {remoting.HostPlugin}
 */
remoting.HostNativeMessaging = function() {
  /**
   * @type {number}
   * @private
   */
  this.nextId_ = 0;

  /**
   * @type {Object.<number, {callback:function(...):void, type:string}>}
   * @private
   */
  this.pendingReplies_ = {};

  /** @private */
  this.port_ = chrome.runtime.connectNative(
      'chrome-remote-desktop-native-host');
  this.port_.onMessage.addListener(this.onIncomingMessage_.bind(this));
};

/**
 * @return {boolean} True if native messaging is supported.
 */
remoting.HostNativeMessaging.isSupported = function() {
  // TODO(lambroslambrou): This needs to perform an asynchronous "hello"
  // exchange with the native host component (possibly getting its version
  // number at the same time), and report the result via a regular callback or
  // an error callback. For now, simply return false here.
  return false;
};

/**
 * 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, 'number')) {
    return null;
  }
  for (var i in remoting.HostController.AsyncResult) {
    if (remoting.HostController.AsyncResult[i] == result) {
      return remoting.HostController.AsyncResult[i];
    }
  }
  console.error('NativeMessaging: unexpected result code: ', result);
  return null;
}

/**
 * Attaches a new ID to the supplied message, and posts it to the Native
 * Messaging port, adding |callback| 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} callback The callback to be triggered on
 *     response.
 * @return {void} Nothing.
 * @private
 */
remoting.HostNativeMessaging.prototype.postMessage_ = function(message,
                                                               callback) {
  var id = this.nextId_++;
  message['id'] = id;
  this.pendingReplies_[id] = {
    callback: callback,
    type: message.type + 'Response'
  }
  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];

  /** @type {string} */
  var type = message['type'];
  if (!checkType_('type', type, 'string')) {
    return;
  }
  if (type != reply.type) {
    console.error('NativeMessaging: expected reply type: ', reply.type,
                  ', got: ', type);
    return;
  }

  var callback = reply.callback;

  // TODO(lambroslambrou): Errors here should be passed to an error-callback
  // supplied by the caller of this interface.
  switch (type) {
    case 'getHostNameResponse':
      /** @type {*} */
      var hostname = message['hostname'];
      if (checkType_('hostname', hostname, 'string')) {
        callback(hostname);
      }
      break;

    case 'getPinHashResponse':
      /** @type {*} */
      var hash = message['hash'];
      if (checkType_('hash', hash, 'string')) {
        callback(hash);
      }
      break;

    case 'generateKeyPairResponse':
      /** @type {*} */
      var private_key = message['private_key'];
      /** @type {*} */
      var public_key = message['public_key'];
      if (checkType_('private_key', private_key, 'string') &&
          checkType_('public_key', public_key, 'string')) {
        callback(private_key, public_key);
      }
      break;

    case 'updateDaemonConfigResponse':
      var result = asAsyncResult_(message['result']);
      if (result != null) {
        callback(result);
      }
      break;

    case 'getDaemonConfigResponse':
      /** @type {*} */
      var config = message['config'];
      if (checkType_('config', config, 'string')) {
        callback(config);
      }
      break;

    case 'getDaemonVersionResponse':
      /** @type {*} */
      var version = message['version'];
      if (checkType_('version', version, 'string')) {
        callback(version);
      }
      break;

    case 'getUsageStatsConsentResponse':
      /** @type {*} */
      var supported = message['supported'];
      /** @type {*} */
      var allowed = message['allowed'];
      /** @type {*} */
      var set_by_policy = message['set_by_policy'];
      if (checkType_('supported', supported, 'boolean') &&
          checkType_('allowed', allowed, 'boolean') &&
          checkType_('set_by_policy', set_by_policy, 'boolean')) {
        callback(supported, allowed, set_by_policy);
      }
      break;

    case 'startDaemonResponse':
    case 'stopDaemonResponse':
      var result = asAsyncResult_(message['result']);
      if (result != null) {
        callback(result);
      }
      break;

    default:
      console.error('Unexpected native message: ', message);
  }
};

/**
 * @param {string} email The email address of the connector.
 * @param {string} token The access token for the connector.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.connect = function(email, token) {
  console.error('NativeMessaging: connect() not implemented.');
};

/** @return {void} Nothing. */
remoting.HostNativeMessaging.prototype.disconnect = function() {
  console.error('NativeMessaging: disconnect() not implemented.');
};

/**
 * @param {function(string):string} callback Pointer to chrome.i18n.getMessage.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.localize = function(callback) {
  console.error('NativeMessaging: localize() not implemented.');
};

/**
 * @param {function(string):void} callback Callback to be called with the
 *     local hostname.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.getHostName = function(callback) {
  this.postMessage_({type: 'getHostName'}, callback);
};

/**
 * 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} callback Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.getPinHash = function(hostId, pin,
                                                             callback) {
  this.postMessage_({
      type: 'getPinHash',
      hostId: hostId,
      pin: pin
  }, callback);
};

/**
 * 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} callback Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.generateKeyPair = function(callback) {
  this.postMessage_({type: 'generateKeyPair'}, callback);
};

/**
 * 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 {string} config The new config parameters, JSON encoded dictionary.
 * @param {function(remoting.HostController.AsyncResult):void} callback
 *     Callback to be called when finished.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.updateDaemonConfig =
    function(config, callback) {
  this.postMessage_({
      type: 'updateDaemonConfig',
      config: config
  }, callback);
};

/**
 * Loads daemon config. The config is passed as a JSON formatted string to the
 * callback.
 *
 * @param {function(string):void} callback Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.getDaemonConfig = function(callback) {
  this.postMessage_({type: 'getDaemonConfig'}, callback);
};

/**
 * Retrieves daemon version. The version is passed to the callback as a dotted
 * decimal string of the form major.minor.build.patch.
 *
 * @param {function(string):void} callback Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.getDaemonVersion = function(callback) {
  this.postMessage_({type: 'getDaemonVersion'}, callback);
};

/**
 * 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} callback Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.getUsageStatsConsent =
    function(callback) {
  this.postMessage_({type: 'getUsageStatsConsent'}, callback);
};

/**
 * Starts the daemon process with the specified configuration.
 *
 * @param {string} config Host configuration.
 * @param {boolean} consent Consent to report crash dumps.
 * @param {function(remoting.HostController.AsyncResult):void} callback
 *     Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.startDaemon = function(
    config, consent, callback) {
  this.postMessage_({
      type: 'startDaemon',
      config: config,
      consent: consent
  }, callback);
};

/**
 * Stops the daemon process.
 *
 * @param {function(remoting.HostController.AsyncResult):void} callback
 *     Callback.
 * @return {void} Nothing.
 */
remoting.HostNativeMessaging.prototype.stopDaemon = function(callback) {
  this.postMessage_({type: 'stopDaemon'}, callback);
};