1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/network/src/TCPSocketParentIntermediary.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const Cc = Components.classes; 1.11 +const Ci = Components.interfaces; 1.12 +const Cu = Components.utils; 1.13 + 1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.15 + 1.16 +function TCPSocketParentIntermediary() { 1.17 +} 1.18 + 1.19 +TCPSocketParentIntermediary.prototype = { 1.20 + _setCallbacks: function(aParentSide, socket) { 1.21 + aParentSide.initJS(this); 1.22 + this._socket = socket; 1.23 + 1.24 + // Create handlers for every possible callback that attempt to trigger 1.25 + // corresponding callbacks on the child object. 1.26 + // ondrain event is not forwarded, since the decision of firing ondrain 1.27 + // is made in child. 1.28 + ["open", "data", "error", "close"].forEach( 1.29 + function(p) { 1.30 + socket["on" + p] = function(data) { 1.31 + aParentSide.sendEvent(p, data.data, socket.readyState, 1.32 + socket.bufferedAmount); 1.33 + }; 1.34 + } 1.35 + ); 1.36 + }, 1.37 + 1.38 + _onUpdateBufferedAmountHandler: function(aParentSide, aBufferedAmount, aTrackingNumber) { 1.39 + aParentSide.sendUpdateBufferedAmount(aBufferedAmount, aTrackingNumber); 1.40 + }, 1.41 + 1.42 + open: function(aParentSide, aHost, aPort, aUseSSL, aBinaryType, aAppId) { 1.43 + let baseSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket); 1.44 + let socket = baseSocket.open(aHost, aPort, {useSecureTransport: aUseSSL, binaryType: aBinaryType}); 1.45 + if (!socket) 1.46 + return null; 1.47 + 1.48 + let socketInternal = socket.QueryInterface(Ci.nsITCPSocketInternal); 1.49 + if (socketInternal) { 1.50 + socketInternal.setAppId(aAppId); 1.51 + } 1.52 + 1.53 + // Handle parent's request to update buffered amount. 1.54 + socketInternal.setOnUpdateBufferedAmountHandler( 1.55 + this._onUpdateBufferedAmountHandler.bind(this, aParentSide)); 1.56 + 1.57 + // Handlers are set to the JS-implemented socket object on the parent side. 1.58 + this._setCallbacks(aParentSide, socket); 1.59 + return socket; 1.60 + }, 1.61 + 1.62 + listen: function(aTCPServerSocketParent, aLocalPort, aBacklog, aBinaryType) { 1.63 + let baseSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket); 1.64 + let serverSocket = baseSocket.listen(aLocalPort, { binaryType: aBinaryType }, aBacklog); 1.65 + if (!serverSocket) 1.66 + return null; 1.67 + 1.68 + let localPort = serverSocket.localPort; 1.69 + 1.70 + serverSocket["onconnect"] = function(socket) { 1.71 + var socketParent = Cc["@mozilla.org/tcp-socket-parent;1"] 1.72 + .createInstance(Ci.nsITCPSocketParent); 1.73 + var intermediary = new TCPSocketParentIntermediary(); 1.74 + // Handlers are set to the JS-implemented socket object on the parent side, 1.75 + // so that the socket parent object can communicate data 1.76 + // with the corresponding socket child object through IPC. 1.77 + intermediary._setCallbacks(socketParent, socket); 1.78 + // The members in the socket parent object are set with arguments, 1.79 + // so that the socket parent object can communicate data 1.80 + // with the JS socket object on the parent side via the intermediary object. 1.81 + socketParent.setSocketAndIntermediary(socket, intermediary); 1.82 + aTCPServerSocketParent.sendCallbackAccept(socketParent); 1.83 + }; 1.84 + 1.85 + serverSocket["onerror"] = function(data) { 1.86 + var error = data.data; 1.87 + 1.88 + aTCPServerSocketParent.sendCallbackError(error.message, error.filename, 1.89 + error.lineNumber, error.columnNumber); 1.90 + }; 1.91 + 1.92 + return serverSocket; 1.93 + }, 1.94 + 1.95 + onRecvSendString: function(aData, aTrackingNumber) { 1.96 + let socketInternal = this._socket.QueryInterface(Ci.nsITCPSocketInternal); 1.97 + return socketInternal.onRecvSendFromChild(aData, 0, 0, aTrackingNumber); 1.98 + }, 1.99 + 1.100 + onRecvSendArrayBuffer: function(aData, aTrackingNumber) { 1.101 + let socketInternal = this._socket.QueryInterface(Ci.nsITCPSocketInternal); 1.102 + return socketInternal.onRecvSendFromChild(aData, 0, aData.byteLength, 1.103 + aTrackingNumber); 1.104 + }, 1.105 + 1.106 + classID: Components.ID("{afa42841-a6cb-4a91-912f-93099f6a3d18}"), 1.107 + QueryInterface: XPCOMUtils.generateQI([ 1.108 + Ci.nsITCPSocketIntermediary 1.109 + ]) 1.110 +}; 1.111 + 1.112 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPSocketParentIntermediary]);