dom/network/src/TCPSocketParentIntermediary.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12
michael@0 13 function TCPSocketParentIntermediary() {
michael@0 14 }
michael@0 15
michael@0 16 TCPSocketParentIntermediary.prototype = {
michael@0 17 _setCallbacks: function(aParentSide, socket) {
michael@0 18 aParentSide.initJS(this);
michael@0 19 this._socket = socket;
michael@0 20
michael@0 21 // Create handlers for every possible callback that attempt to trigger
michael@0 22 // corresponding callbacks on the child object.
michael@0 23 // ondrain event is not forwarded, since the decision of firing ondrain
michael@0 24 // is made in child.
michael@0 25 ["open", "data", "error", "close"].forEach(
michael@0 26 function(p) {
michael@0 27 socket["on" + p] = function(data) {
michael@0 28 aParentSide.sendEvent(p, data.data, socket.readyState,
michael@0 29 socket.bufferedAmount);
michael@0 30 };
michael@0 31 }
michael@0 32 );
michael@0 33 },
michael@0 34
michael@0 35 _onUpdateBufferedAmountHandler: function(aParentSide, aBufferedAmount, aTrackingNumber) {
michael@0 36 aParentSide.sendUpdateBufferedAmount(aBufferedAmount, aTrackingNumber);
michael@0 37 },
michael@0 38
michael@0 39 open: function(aParentSide, aHost, aPort, aUseSSL, aBinaryType, aAppId) {
michael@0 40 let baseSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
michael@0 41 let socket = baseSocket.open(aHost, aPort, {useSecureTransport: aUseSSL, binaryType: aBinaryType});
michael@0 42 if (!socket)
michael@0 43 return null;
michael@0 44
michael@0 45 let socketInternal = socket.QueryInterface(Ci.nsITCPSocketInternal);
michael@0 46 if (socketInternal) {
michael@0 47 socketInternal.setAppId(aAppId);
michael@0 48 }
michael@0 49
michael@0 50 // Handle parent's request to update buffered amount.
michael@0 51 socketInternal.setOnUpdateBufferedAmountHandler(
michael@0 52 this._onUpdateBufferedAmountHandler.bind(this, aParentSide));
michael@0 53
michael@0 54 // Handlers are set to the JS-implemented socket object on the parent side.
michael@0 55 this._setCallbacks(aParentSide, socket);
michael@0 56 return socket;
michael@0 57 },
michael@0 58
michael@0 59 listen: function(aTCPServerSocketParent, aLocalPort, aBacklog, aBinaryType) {
michael@0 60 let baseSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
michael@0 61 let serverSocket = baseSocket.listen(aLocalPort, { binaryType: aBinaryType }, aBacklog);
michael@0 62 if (!serverSocket)
michael@0 63 return null;
michael@0 64
michael@0 65 let localPort = serverSocket.localPort;
michael@0 66
michael@0 67 serverSocket["onconnect"] = function(socket) {
michael@0 68 var socketParent = Cc["@mozilla.org/tcp-socket-parent;1"]
michael@0 69 .createInstance(Ci.nsITCPSocketParent);
michael@0 70 var intermediary = new TCPSocketParentIntermediary();
michael@0 71 // Handlers are set to the JS-implemented socket object on the parent side,
michael@0 72 // so that the socket parent object can communicate data
michael@0 73 // with the corresponding socket child object through IPC.
michael@0 74 intermediary._setCallbacks(socketParent, socket);
michael@0 75 // The members in the socket parent object are set with arguments,
michael@0 76 // so that the socket parent object can communicate data
michael@0 77 // with the JS socket object on the parent side via the intermediary object.
michael@0 78 socketParent.setSocketAndIntermediary(socket, intermediary);
michael@0 79 aTCPServerSocketParent.sendCallbackAccept(socketParent);
michael@0 80 };
michael@0 81
michael@0 82 serverSocket["onerror"] = function(data) {
michael@0 83 var error = data.data;
michael@0 84
michael@0 85 aTCPServerSocketParent.sendCallbackError(error.message, error.filename,
michael@0 86 error.lineNumber, error.columnNumber);
michael@0 87 };
michael@0 88
michael@0 89 return serverSocket;
michael@0 90 },
michael@0 91
michael@0 92 onRecvSendString: function(aData, aTrackingNumber) {
michael@0 93 let socketInternal = this._socket.QueryInterface(Ci.nsITCPSocketInternal);
michael@0 94 return socketInternal.onRecvSendFromChild(aData, 0, 0, aTrackingNumber);
michael@0 95 },
michael@0 96
michael@0 97 onRecvSendArrayBuffer: function(aData, aTrackingNumber) {
michael@0 98 let socketInternal = this._socket.QueryInterface(Ci.nsITCPSocketInternal);
michael@0 99 return socketInternal.onRecvSendFromChild(aData, 0, aData.byteLength,
michael@0 100 aTrackingNumber);
michael@0 101 },
michael@0 102
michael@0 103 classID: Components.ID("{afa42841-a6cb-4a91-912f-93099f6a3d18}"),
michael@0 104 QueryInterface: XPCOMUtils.generateQI([
michael@0 105 Ci.nsITCPSocketIntermediary
michael@0 106 ])
michael@0 107 };
michael@0 108
michael@0 109 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPSocketParentIntermediary]);

mercurial