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