summaryrefslogtreecommitdiffstats
path: root/dom/presentation/provider/LegacyPresentationControlService.js
blob: b27177b635bc4106763c771169c59914bb248b1c (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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* jshint esnext:true, globalstrict:true, moz:true, undef:true, unused:true */
/* globals Components, dump */
"use strict";

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;

/* globals XPCOMUtils */
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
/* globals Services */
Cu.import("resource://gre/modules/Services.jsm");
/* globals NetUtil */
Cu.import("resource://gre/modules/NetUtil.jsm");

const DEBUG = Services.prefs.getBoolPref("dom.presentation.tcp_server.debug");
function log(aMsg) {
  dump("-*- LegacyPresentationControlService.js: " + aMsg + "\n");
}

function LegacyPresentationControlService() {
  DEBUG && log("LegacyPresentationControlService - ctor"); //jshint ignore:line
  this._id = null;
}

LegacyPresentationControlService.prototype = {
  startServer: function() {
    DEBUG && log("LegacyPresentationControlService - doesn't support receiver mode"); //jshint ignore:line
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },

  get id() {
    return this._id;
  },

  set id(aId) {
    this._id = aId;
  },

  get port() {
    return 0;
  },

  get version() {
    return 0;
  },

  set listener(aListener) { //jshint ignore:line
    DEBUG && log("LegacyPresentationControlService - doesn't support receiver mode"); //jshint ignore:line
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },

  get listener() {
    return null;
  },

  connect: function(aDeviceInfo) {
    if (!this.id) {
      DEBUG && log("LegacyPresentationControlService - Id has not initialized; requestSession fails"); //jshint ignore:line
      return null;
    }
    DEBUG && log("LegacyPresentationControlService - requestSession to " + aDeviceInfo.id); //jshint ignore:line

    let sts = Cc["@mozilla.org/network/socket-transport-service;1"]
                .getService(Ci.nsISocketTransportService);

    let socketTransport;
    try {
      socketTransport = sts.createTransport(null,
                                            0,
                                            aDeviceInfo.address,
                                            aDeviceInfo.port,
                                            null);
    } catch (e) {
      DEBUG && log("LegacyPresentationControlService - createTransport throws: " + e); //jshint ignore:line
      // Pop the exception to |TCPDevice.establishControlChannel|
      throw Cr.NS_ERROR_FAILURE;
    }
    return new LegacyTCPControlChannel(this.id,
                                       socketTransport,
                                       aDeviceInfo);
  },

  close: function() {
    DEBUG && log("LegacyPresentationControlService - close"); //jshint ignore:line
  },

  classID: Components.ID("{b21816fe-8aff-4811-86d2-85a7444c557e}"),
  QueryInterface : XPCOMUtils.generateQI([Ci.nsIPresentationControlService]),
};

function ChannelDescription(aInit) {
  this._type = aInit.type;
  switch (this._type) {
    case Ci.nsIPresentationChannelDescription.TYPE_TCP:
      this._tcpAddresses = Cc["@mozilla.org/array;1"]
                           .createInstance(Ci.nsIMutableArray);
      for (let address of aInit.tcpAddress) {
        let wrapper = Cc["@mozilla.org/supports-cstring;1"]
                      .createInstance(Ci.nsISupportsCString);
        wrapper.data = address;
        this._tcpAddresses.appendElement(wrapper, false);
      }

      this._tcpPort = aInit.tcpPort;
      break;
    case Ci.nsIPresentationChannelDescription.TYPE_DATACHANNEL:
      this._dataChannelSDP = aInit.dataChannelSDP;
      break;
  }
}

ChannelDescription.prototype = {
  _type: 0,
  _tcpAddresses: null,
  _tcpPort: 0,
  _dataChannelSDP: "",

  get type() {
    return this._type;
  },

  get tcpAddress() {
    return this._tcpAddresses;
  },

  get tcpPort() {
    return this._tcpPort;
  },

  get dataChannelSDP() {
    return this._dataChannelSDP;
  },

  classID: Components.ID("{d69fc81c-4f40-47a3-97e6-b4cf5db2294e}"),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationChannelDescription]),
};

// Helper function: transfer nsIPresentationChannelDescription to json
function discriptionAsJson(aDescription) {
  let json = {};
  json.type = aDescription.type;
  switch(aDescription.type) {
    case Ci.nsIPresentationChannelDescription.TYPE_TCP:
      let addresses = aDescription.tcpAddress.QueryInterface(Ci.nsIArray);
      json.tcpAddress = [];
      for (let idx = 0; idx < addresses.length; idx++) {
        let address = addresses.queryElementAt(idx, Ci.nsISupportsCString);
        json.tcpAddress.push(address.data);
      }
      json.tcpPort = aDescription.tcpPort;
      break;
    case Ci.nsIPresentationChannelDescription.TYPE_DATACHANNEL:
      json.dataChannelSDP = aDescription.dataChannelSDP;
      break;
  }
  return json;
}

function LegacyTCPControlChannel(id,
                                 transport,
                                 deviceInfo) {
  DEBUG && log("create LegacyTCPControlChannel"); //jshint ignore:line
  this._deviceInfo = deviceInfo;
  this._transport = transport;

  this._id = id;

  let currentThread = Services.tm.currentThread;
  transport.setEventSink(this, currentThread);

  this._input = this._transport.openInputStream(0, 0, 0)
                               .QueryInterface(Ci.nsIAsyncInputStream);
  this._input.asyncWait(this.QueryInterface(Ci.nsIStreamListener),
                        Ci.nsIAsyncInputStream.WAIT_CLOSURE_ONLY,
                        0,
                        currentThread);

  this._output = this._transport
                     .openOutputStream(Ci.nsITransport.OPEN_UNBUFFERED, 0, 0);
}

LegacyTCPControlChannel.prototype = {
  _connected: false,
  _pendingOpen: false,
  _pendingAnswer: null,
  _pendingClose: null,
  _pendingCloseReason: null,

  _sendMessage: function(aJSONData, aOnThrow) {
    if (!aOnThrow) {
      aOnThrow = function(e) {throw e.result;};
    }

    if (!aJSONData) {
      aOnThrow();
      return;
    }

    if (!this._connected) {
      DEBUG && log("LegacyTCPControlChannel - send" + aJSONData.type + " fails"); //jshint ignore:line
      throw Cr.NS_ERROR_FAILURE;
    }

    try {
      this._send(aJSONData);
    } catch (e) {
      aOnThrow(e);
    }
  },

  _sendInit: function() {
    let msg = {
      type: "requestSession:Init",
      presentationId: this._presentationId,
      url: this._url,
      id: this._id,
    };

    this._sendMessage(msg, function(e) {
      this.disconnect();
      this._notifyDisconnected(e.result);
    });
  },

  launch: function(aPresentationId, aUrl) {
    this._presentationId = aPresentationId;
    this._url = aUrl;

    this._sendInit();
  },

  terminate: function() {
    // Legacy protocol doesn't support extra terminate protocol.
    // Trigger error handling for browser to shutdown all the resource locally.
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },

  sendOffer: function(aOffer) {
    let msg = {
      type: "requestSession:Offer",
      presentationId: this._presentationId,
      offer: discriptionAsJson(aOffer),
    };
    this._sendMessage(msg);
  },

  sendAnswer: function(aAnswer) { //jshint ignore:line
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },

  sendIceCandidate: function(aCandidate) {
    let msg = {
      type: "requestSession:IceCandidate",
      presentationId: this._presentationId,
      iceCandidate: aCandidate,
    };
    this._sendMessage(msg);
  },
  // may throw an exception
  _send: function(aMsg) {
    DEBUG && log("LegacyTCPControlChannel - Send: " + JSON.stringify(aMsg, null, 2)); //jshint ignore:line

    /**
     * XXX In TCP streaming, it is possible that more than one message in one
     * TCP packet. We use line delimited JSON to identify where one JSON encoded
     * object ends and the next begins. Therefore, we do not allow newline
     * characters whithin the whole message, and add a newline at the end.
     * Please see the parser code in |onDataAvailable|.
     */
    let message = JSON.stringify(aMsg).replace(["\n"], "") + "\n";
    try {
      this._output.write(message, message.length);
    } catch(e) {
      DEBUG && log("LegacyTCPControlChannel - Failed to send message: " + e.name); //jshint ignore:line
      throw e;
    }
  },

  // nsIAsyncInputStream (Triggered by nsIInputStream.asyncWait)
  // Only used for detecting connection refused
  onInputStreamReady: function(aStream) {
    try {
      aStream.available();
    } catch (e) {
      DEBUG && log("LegacyTCPControlChannel - onInputStreamReady error: " + e.name); //jshint ignore:line
      // NS_ERROR_CONNECTION_REFUSED
      this._listener.notifyDisconnected(e.result);
    }
  },

  // nsITransportEventSink (Triggered by nsISocketTransport.setEventSink)
  onTransportStatus: function(aTransport, aStatus, aProg, aProgMax) { //jshint ignore:line
    DEBUG && log("LegacyTCPControlChannel - onTransportStatus: "
                 + aStatus.toString(16)); //jshint ignore:line
    if (aStatus === Ci.nsISocketTransport.STATUS_CONNECTED_TO) {
      this._connected = true;

      if (!this._pump) {
        this._createInputStreamPump();
      }

      this._notifyConnected();
    }
  },

  // nsIRequestObserver (Triggered by nsIInputStreamPump.asyncRead)
  onStartRequest: function() {
    DEBUG && log("LegacyTCPControlChannel - onStartRequest"); //jshint ignore:line
  },

  // nsIRequestObserver (Triggered by nsIInputStreamPump.asyncRead)
  onStopRequest: function(aRequest, aContext, aStatus) {
    DEBUG && log("LegacyTCPControlChannel - onStopRequest: " + aStatus); //jshint ignore:line
    this.disconnect(aStatus);
    this._notifyDisconnected(aStatus);
  },

  // nsIStreamListener (Triggered by nsIInputStreamPump.asyncRead)
  onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) { //jshint ignore:line
    let data = NetUtil.readInputStreamToString(aInputStream,
                                               aInputStream.available());
    DEBUG && log("LegacyTCPControlChannel - onDataAvailable: " + data); //jshint ignore:line

    // Parser of line delimited JSON. Please see |_send| for more informaiton.
    let jsonArray = data.split("\n");
    jsonArray.pop();
    for (let json of jsonArray) {
      let msg;
      try {
        msg = JSON.parse(json);
      } catch (e) {
        DEBUG && log("LegacyTCPSignalingChannel - error in parsing json: " + e); //jshint ignore:line
      }

      this._handleMessage(msg);
    }
  },

  _createInputStreamPump: function() {
    DEBUG && log("LegacyTCPControlChannel - create pump"); //jshint ignore:line
    this._pump = Cc["@mozilla.org/network/input-stream-pump;1"].
               createInstance(Ci.nsIInputStreamPump);
    this._pump.init(this._input, -1, -1, 0, 0, false);
    this._pump.asyncRead(this, null);
  },

  // Handle command from remote side
  _handleMessage: function(aMsg) {
    DEBUG && log("LegacyTCPControlChannel - handleMessage from "
                 + JSON.stringify(this._deviceInfo) + ": " + JSON.stringify(aMsg)); //jshint ignore:line
    switch (aMsg.type) {
      case "requestSession:Answer": {
        this._onAnswer(aMsg.answer);
        break;
      }
      case "requestSession:IceCandidate": {
        this._listener.onIceCandidate(aMsg.iceCandidate);
        break;
      }
      case "requestSession:CloseReason": {
        this._pendingCloseReason = aMsg.reason;
        break;
      }
    }
  },

  get listener() {
    return this._listener;
  },

  set listener(aListener) {
    DEBUG && log("LegacyTCPControlChannel - set listener: " + aListener); //jshint ignore:line
    if (!aListener) {
      this._listener = null;
      return;
    }

    this._listener = aListener;
    if (this._pendingOpen) {
      this._pendingOpen = false;
      DEBUG && log("LegacyTCPControlChannel - notify pending opened"); //jshint ignore:line
      this._listener.notifyConnected();
    }

    if (this._pendingAnswer) {
      let answer = this._pendingAnswer;
      DEBUG && log("LegacyTCPControlChannel - notify pending answer: " +
                   JSON.stringify(answer)); // jshint ignore:line
      this._listener.onAnswer(new ChannelDescription(answer));
      this._pendingAnswer = null;
    }

    if (this._pendingClose) {
      DEBUG && log("LegacyTCPControlChannel - notify pending closed"); //jshint ignore:line
      this._notifyDisconnected(this._pendingCloseReason);
      this._pendingClose = null;
    }
  },

  /**
   * These functions are designed to handle the interaction with listener
   * appropriately. |_FUNC| is to handle |this._listener.FUNC|.
   */
  _onAnswer: function(aAnswer) {
    if (!this._connected) {
      return;
    }
    if (!this._listener) {
      this._pendingAnswer = aAnswer;
      return;
    }
    DEBUG && log("LegacyTCPControlChannel - notify answer: " + JSON.stringify(aAnswer)); //jshint ignore:line
    this._listener.onAnswer(new ChannelDescription(aAnswer));
  },

  _notifyConnected: function() {
    this._connected = true;
    this._pendingClose = false;
    this._pendingCloseReason = Cr.NS_OK;

    if (!this._listener) {
      this._pendingOpen = true;
      return;
    }

    DEBUG && log("LegacyTCPControlChannel - notify opened"); //jshint ignore:line
    this._listener.notifyConnected();
  },

  _notifyDisconnected: function(aReason) {
    this._connected = false;
    this._pendingOpen = false;
    this._pendingAnswer = null;

    // Remote endpoint closes the control channel with abnormal reason.
    if (aReason == Cr.NS_OK && this._pendingCloseReason != Cr.NS_OK) {
      aReason = this._pendingCloseReason;
    }

    if (!this._listener) {
      this._pendingClose = true;
      this._pendingCloseReason = aReason;
      return;
    }

    DEBUG && log("LegacyTCPControlChannel - notify closed"); //jshint ignore:line
    this._listener.notifyDisconnected(aReason);
  },

  disconnect: function(aReason) {
    DEBUG && log("LegacyTCPControlChannel - close with reason: " + aReason); //jshint ignore:line

    if (this._connected) {
      // default reason is NS_OK
      if (typeof aReason !== "undefined" && aReason !== Cr.NS_OK) {
        let msg = {
          type: "requestSession:CloseReason",
          presentationId: this._presentationId,
          reason: aReason,
        };
        this._sendMessage(msg);
        this._pendingCloseReason = aReason;
      }

      this._transport.setEventSink(null, null);
      this._pump = null;

      this._input.close();
      this._output.close();

      this._connected = false;
    }
  },

  reconnect: function() {
    // Legacy protocol doesn't support extra reconnect protocol.
    // Trigger error handling for browser to shutdown all the resource locally.
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },

  classID: Components.ID("{4027ce3d-06e3-4d06-a235-df329cb0d411}"),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationControlChannel,
                                         Ci.nsIStreamListener]),
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([LegacyPresentationControlService]); //jshint ignore:line