Created
July 17, 2012 18:56
-
-
Save fzzzy/3131272 to your computer and use it in GitHub Desktop.
next tcpsocket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/b2g/Makefile.in b/b2g/Makefile.in | |
--- a/b2g/Makefile.in | |
+++ b/b2g/Makefile.in | |
@@ -12,10 +12,12 @@ | |
DIRS = chrome components locales | |
ifeq ($(OS_ARCH),WINNT) | |
DIRS += $(DEPTH)/xulrunner/tools/redit | |
endif | |
DIRS += app | |
+TEST_DIRS += test | |
+ | |
include $(topsrcdir)/config/rules.mk | |
include $(topsrcdir)/testing/testsuite-targets.mk | |
diff --git a/b2g/chrome/content/shell.js b/b2g/chrome/content/shell.js | |
--- a/b2g/chrome/content/shell.js | |
+++ b/b2g/chrome/content/shell.js | |
@@ -51,17 +51,17 @@ | |
// until we have a proper security model, add some rights to | |
// the pre-installed web applications | |
// XXX never grant 'content-camera' to non-gaia apps | |
function addPermissions(urls) { | |
let permissions = [ | |
'indexedDB-unlimited', 'webapps-manage', 'offline-app', 'pin-app', | |
'websettings-read', 'websettings-readwrite', | |
'content-camera', 'webcontacts-manage', 'wifi-manage', 'desktop-notification', | |
- 'geolocation', 'device-storage', 'alarms' | |
+ 'geolocation', 'device-storage', 'alarms', 'tcp-socket' | |
]; | |
urls.forEach(function(url) { | |
url = url.trim(); | |
let uri = Services.io.newURI(url, null, null); | |
let allow = Ci.nsIPermissionManager.ALLOW_ACTION; | |
permissions.forEach(function(permission) { | |
Services.perms.add(uri, permission, allow); | |
diff --git a/b2g/components/B2GComponents.manifest b/b2g/components/B2GComponents.manifest | |
--- a/b2g/components/B2GComponents.manifest | |
+++ b/b2g/components/B2GComponents.manifest | |
@@ -25,12 +25,18 @@ | |
contract @mozilla.org/b2g-keyboard;1 {397a7fdf-2254-47be-b74e-76625a1a66d5} | |
category JavaScript-navigator-property mozKeyboard @mozilla.org/b2g-keyboard;1 | |
# DirectoryProvider.js | |
component {9181eb7c-6f87-11e1-90b1-4f59d80dd2e5} DirectoryProvider.js | |
contract @mozilla.org/browser/directory-provider;1 {9181eb7c-6f87-11e1-90b1-4f59d80dd2e5} | |
category xpcom-directory-providers browser-directory-provider @mozilla.org/browser/directory-provider;1 | |
+# TCPSocket.js | |
+component {cda91b22-6472-11e1-aa11-834fec09cd0a} TCPSocket.js | |
+contract @mozilla.org/tcp-socket;1 {cda91b22-6472-11e1-aa11-834fec09cd0a} | |
+category JavaScript-global-property MozTCPSocket @mozilla.org/tcp-socket;1 | |
+ | |
+ | |
# ProcessGlobal.js | |
component {1a94c87a-5ece-4d11-91e1-d29c29f21b28} ProcessGlobal.js | |
contract @mozilla.org/b2g-process-global;1 {1a94c87a-5ece-4d11-91e1-d29c29f21b28} | |
category app-startup ProcessGlobal service,@mozilla.org/b2g-process-global;1 | |
diff --git a/b2g/components/Makefile.in b/b2g/components/Makefile.in | |
--- a/b2g/components/Makefile.in | |
+++ b/b2g/components/Makefile.in | |
@@ -9,18 +9,27 @@ | |
include $(DEPTH)/config/autoconf.mk | |
MODULE = B2GComponents | |
XPIDL_MODULE = B2GComponents | |
XPIDLSRCS = \ | |
b2g.idl \ | |
+ nsIDOMTCPSocket.idl \ | |
$(NULL) | |
+EXTRA_COMPONENTS = \ | |
+ TCPSocket.js \ | |
+ $(NULL) | |
+ | |
+# Only put things in _PP_ if they need to be preprocessed. Preprocessing means | |
+# we can't just symlink to the source file so you have to run make every time | |
+# you update the files. | |
+ | |
EXTRA_PP_COMPONENTS = \ | |
AlertsService.js \ | |
B2GComponents.manifest \ | |
CameraContent.js \ | |
ContentPermissionPrompt.js \ | |
DirectoryProvider.js \ | |
MozKeyboard.js \ | |
ProcessGlobal.js \ | |
diff --git a/b2g/components/TCPSocket.js b/b2g/components/TCPSocket.js | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/components/TCPSocket.js | |
@@ -0,0 +1,397 @@ | |
+/* 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/. */ | |
+ | |
+"use strict"; | |
+ | |
+const Cc = Components.classes; | |
+const Ci = Components.interfaces; | |
+const Cu = Components.utils; | |
+const Cr = Components.results; | |
+const CC = Components.Constructor; | |
+ | |
+Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | |
+Cu.import("resource://gre/modules/Services.jsm"); | |
+ | |
+let debug = true; | |
+function LOG(msg) { | |
+ if (debug) | |
+ dump("TCPSocket: " + msg + "\n"); | |
+} | |
+ | |
+/* | |
+ * nsITCPSocketEvent object | |
+ */ | |
+ | |
+function TCPSocketEvent(type, sock, data) { | |
+ this.type = type; | |
+ this.socket = sock; | |
+ this.data = data; | |
+} | |
+ | |
+TCPSocketEvent.prototype = { | |
+ classID: Components.ID("{f29a577b-e831-431e-a540-1c4856721c82}"), | |
+ | |
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsITCPSocketEvent]), | |
+ | |
+ classInfo: XPCOMUtils.generateCI({ | |
+ classID: Components.ID("{f29a577b-e831-431e-a540-1c4856721c82}"), | |
+ contractID: "@mozilla.org/tcp-socket-event;1", | |
+ classDescription: "TCP Socket Event", | |
+ interfaces: [Ci.nsITCPSocketEvent], | |
+ flags: Ci.nsIClassInfo.DOM_OBJECT | |
+ }) | |
+}; | |
+ | |
+ | |
+const InputStreamPump = CC( | |
+ "@mozilla.org/network/input-stream-pump;1", "nsIInputStreamPump", "init"), | |
+ AsyncStreamCopier = CC( | |
+ "@mozilla.org/network/async-stream-copier;1", "nsIAsyncStreamCopier", "init"), | |
+ ScriptableInputStream = CC( | |
+ "@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init"), | |
+ BinaryInputStream = CC( | |
+ "@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream", "setInputStream"), | |
+ StringInputStream = CC( | |
+ '@mozilla.org/io/string-input-stream;1', 'nsIStringInputStream'), | |
+ MultiplexInputStream = CC( | |
+ '@mozilla.org/io/multiplex-input-stream;1', 'nsIMultiplexInputStream'); | |
+ | |
+const kCONNECTING = 'connecting'; | |
+const kOPEN = 'open'; | |
+const kCLOSING = 'closing'; | |
+const kCLOSED = 'closed'; | |
+ | |
+const BUFFER_SIZE = 65536; | |
+ | |
+/* | |
+ * nsIDOMTCPSocket object | |
+ */ | |
+ | |
+function TCPSocket() { | |
+ this.readyState = kCLOSED; | |
+ | |
+ this.onopen = null; | |
+ this.ondrain = null; | |
+ this.ondata = null; | |
+ this.onerror = null; | |
+ this.onclose = null; | |
+ | |
+ this.binaryType = "string"; | |
+ | |
+ this.host = ""; | |
+ this.port = 0; | |
+ this.ssl = false; | |
+}; | |
+ | |
+ | |
+TCPSocket.prototype = { | |
+ // Constants | |
+ CONNECTING: kCONNECTING, | |
+ OPEN: kOPEN, | |
+ CLOSING: kCLOSING, | |
+ CLOSED: kCLOSED, | |
+ | |
+ // The binary type, "string" or "arraybuffer" | |
+ binaryType: null, | |
+ | |
+ // Internal | |
+ _hasPrivileges: null, | |
+ _binaryType: "string", | |
+ | |
+ // Raw socket streams | |
+ _transport: null, | |
+ _socketInputStream: null, | |
+ _socketOutputStream: null, | |
+ | |
+ // Input stream machinery | |
+ _inputStreamPump: null, | |
+ _inputStreamScriptable: null, | |
+ _inputStreamBinary: null, | |
+ | |
+ // Output stream machinery | |
+ _outputMultiplexStream: null, | |
+ _outputStreamCopier: null, | |
+ | |
+ _asyncCopierActive: false, | |
+ _waitingForDrain: false, | |
+ _suspendCount: 0, | |
+ | |
+ _createTransport: function ts_createTransport(host, port, sslMode) { | |
+ let options, optlen; | |
+ if (sslMode) { | |
+ options = [sslMode]; | |
+ optlen = 1; | |
+ } else { | |
+ options = null; | |
+ optlen = 0; | |
+ } | |
+ return Cc["@mozilla.org/network/socket-transport-service;1"] | |
+ .getService(Ci.nsISocketTransportService) | |
+ .createTransport(options, optlen, host, port, null); | |
+ }, | |
+ | |
+ _ensureCopying: function ts_ensureCopying(that) { | |
+ if (that._asyncCopierActive) { | |
+ return; | |
+ } | |
+ that._asyncCopierActive = true; | |
+ that._outputStreamCopier.asyncCopy({ | |
+ onStartRequest: function ts_output_onStartRequest() { | |
+ }, | |
+ onStopRequest: function ts_output_onStopRequest() { | |
+ that._asyncCopierActive = false; | |
+ that._outputMultiplexStream.removeStream(0); | |
+ if (that._outputMultiplexStream.count) { | |
+ that._ensureCopying(that); | |
+ } else { | |
+ if (that._waitingForDrain) { | |
+ that._waitingForDrain = false; | |
+ that.callListener("ondrain"); | |
+ } | |
+ if (that.readyState === kCLOSING) { | |
+ that._socketOutputStream.close(); | |
+ } | |
+ } | |
+ } | |
+ }, null); | |
+ }, | |
+ | |
+ callListener: function ts_callListener(type, data) { | |
+ if (!this[type]) | |
+ return; | |
+ | |
+ this[type].call(this, new TCPSocketEvent(type, this, data || "")); | |
+ }, | |
+ | |
+ get bufferedAmount() { | |
+ return this._outputMultiplexStream.available(); | |
+ }, | |
+ | |
+ init: function ts_init(aWindow) { | |
+ // When the TCPSocket property is initialized for each window, | |
+ // we check to see if the tcp-socket permission is set for this | |
+ // domain. If not, open will refuse to create and open new sockets. | |
+ let principal = aWindow.document.nodePrincipal; | |
+ this._hasPrivileges = ( | |
+ Services.perms.testExactPermission(principal.URI, "tcp-socket") | |
+ === Ci.nsIPermissionManager.ALLOW_ACTION); | |
+ }, | |
+ | |
+ // nsIDOMTCPSocket | |
+ open: function ts_open(host, port, options) { | |
+ // in the testing case, init won't be called and | |
+ // hasPrivileges will be null. We want to proceed to test. | |
+ if (this._hasPrivileges !== true && this._hasPrivileges !== null) { | |
+ throw new Error("TCPSocket does not have permission in this context.\n"); | |
+ } | |
+ let that = new TCPSocket(); | |
+ | |
+ LOG("startup called\n"); | |
+ LOG("Host info: " + host + ":" + port + "\n"); | |
+ | |
+ that.readyState = kCONNECTING; | |
+ that.host = host; | |
+ that.port = port; | |
+ if (options !== undefined) { | |
+ if (options.useSSL) { | |
+ that.ssl = 'ssl'; | |
+ } else { | |
+ that.ssl = false; | |
+ } | |
+ that.binaryType = options.binaryType || that.binaryType; | |
+ } | |
+ that._binaryType = that.binaryType; | |
+ | |
+ LOG("SSL: " + that.ssl + "\n"); | |
+ | |
+ let transport = that._transport = this._createTransport(host, port, that.ssl); | |
+ transport.setEventSink(that, Services.tm.currentThread); | |
+ transport.securityCallbacks = new SecurityCallbacks(that); | |
+ | |
+ that._socketInputStream = transport.openInputStream(0, 0, 0); | |
+ that._socketOutputStream = transport.openOutputStream( | |
+ Ci.nsITransport.OPEN_UNBUFFERED, 0, 0); | |
+ | |
+ // If the other side is not listening, we will | |
+ // get an onInputStreamReady callback where available | |
+ // raises to indicate the connection was refused. | |
+ that._socketInputStream.asyncWait( | |
+ that, that._socketInputStream.WAIT_CLOSURE_ONLY, 0, Services.tm.currentThread); | |
+ | |
+ if (that.binaryType === "arraybuffer") { | |
+ that._inputStreamBinary = new BinaryInputStream(that._socketInputStream); | |
+ } else { | |
+ that._inputStreamScriptable = new ScriptableInputStream(that._socketInputStream); | |
+ } | |
+ | |
+ that._outputMultiplexStream = new MultiplexInputStream(); | |
+ | |
+ that._outputStreamCopier = new AsyncStreamCopier( | |
+ that._outputMultiplexStream, | |
+ that._socketOutputStream, | |
+ // (nsSocketTransport uses gSocketTransportService) | |
+ Cc["@mozilla.org/network/socket-transport-service;1"] | |
+ .getService(Ci.nsIEventTarget), | |
+ /* source buffered */ true, /* sink buffered */ false, | |
+ BUFFER_SIZE, /* close source*/ false, /* close sink */ false); | |
+ | |
+ return that; | |
+ }, | |
+ | |
+ close: function ts_close() { | |
+ if (this.readyState === kCLOSED || this.readyState === kCLOSING) | |
+ return; | |
+ | |
+ LOG("close called\n"); | |
+ this.readyState = kCLOSING; | |
+ | |
+ if (!this._outputMultiplexStream.count) { | |
+ this._socketOutputStream.close(); | |
+ } | |
+ this._socketInputStream.close(); | |
+ }, | |
+ | |
+ send: function ts_send(data) { | |
+ if (this.readyState !== kOPEN) { | |
+ throw new Error("Socket not open."); | |
+ } | |
+ | |
+ let new_stream = new StringInputStream(); | |
+ if (this._binaryType === "arraybuffer") { | |
+ // It would be really nice if there were an interface | |
+ // that took an ArrayBuffer like StringInputStream takes | |
+ // a string. There is one, but only in C++ and not exposed | |
+ // to js as far as I can tell | |
+ data = Array.map(data, function(el, i) { | |
+ return String.fromCharCode(el); | |
+ }).join(""); | |
+ } | |
+ new_stream.setData(data, data.length); | |
+ this._outputMultiplexStream.appendStream(new_stream); | |
+ | |
+ this._ensureCopying(this); | |
+ | |
+ if (this.bufferedAmount >= BUFFER_SIZE) { | |
+ // If we buffered more than some arbitrary amount of data, | |
+ // (65535 right now) we should tell the caller so they can | |
+ // wait until ondrain is called, once all the buffered data | |
+ // has been written to the socket. | |
+ this._waitingForDrain = true; | |
+ return false; | |
+ } | |
+ return true; | |
+ }, | |
+ | |
+ suspend: function ts_suspend() { | |
+ if (this._inputStreamPump) { | |
+ this._inputStreamPump.suspend(); | |
+ } else { | |
+ this._suspendCount++; | |
+ } | |
+ }, | |
+ | |
+ resume: function ts_resume() { | |
+ if (this._inputStreamPump) { | |
+ this._inputStreamPump.resume(); | |
+ } else { | |
+ this._suspendCount--; | |
+ } | |
+ }, | |
+ | |
+ // nsITransportEventSink (Triggered by transport.setEventSink) | |
+ onTransportStatus: function ts_onTransportStatus( | |
+ transport, status, progress, max) { | |
+ | |
+ if (status === Ci.nsISocketTransport.STATUS_CONNECTED_TO) { | |
+ this.readyState = kOPEN; | |
+ this.callListener("onopen"); | |
+ | |
+ this._inputStreamPump = new InputStreamPump( | |
+ this._socketInputStream, -1, -1, 0, 0, false | |
+ ) | |
+ while (this._suspendCount) { | |
+ this._inputStreamPump.suspend(); | |
+ this._suspendCount--; | |
+ } | |
+ | |
+ this._inputStreamPump.asyncRead(this, null); | |
+ } | |
+ }, | |
+ | |
+ // nsIAsyncInputStream (Triggered by _socketInputStream.asyncWait) | |
+ // Only used for detecting connection refused | |
+ onInputStreamReady: function ts_onInputStreamReady(input) { | |
+ try { | |
+ input.available(); | |
+ } catch (e) { | |
+ this.callListener("onerror", new Error("Connection refused")); | |
+ } | |
+ }, | |
+ | |
+ // nsIRequestObserver (Triggered by _inputStreamPump.asyncRead) | |
+ onStartRequest: function ts_onStartRequest(request, context) { | |
+ }, | |
+ | |
+ // nsIRequestObserver (Triggered by _inputStreamPump.asyncRead) | |
+ onStopRequest: function ts_onStopRequest(request, context, status) { | |
+ this.readyState = kCLOSED; | |
+ this._inputStreamPump = null; | |
+ | |
+ if (status) { | |
+ let err = new Error("Connection closed: " + status); | |
+ err.status = status; | |
+ this.callListener("onerror", err); | |
+ } | |
+ | |
+ this.callListener("onclose"); | |
+ }, | |
+ | |
+ // nsIStreamListener (Triggered by _inputStreamPump.asyncRead) | |
+ onDataAvailable: function ts_onDataAvailable(request, context, inputStream, offset, count) { | |
+ if (this._binaryType === "arraybuffer") { | |
+ let ua = new Uint8Array(count); | |
+ ua.set(this._inputStreamBinary.readByteArray(count)); | |
+ this.callListener("ondata", ua); | |
+ } else { | |
+ this.callListener("ondata", this._inputStreamScriptable.read(count)); | |
+ } | |
+ }, | |
+ | |
+ classID: Components.ID("{cda91b22-6472-11e1-aa11-834fec09cd0a}"), | |
+ | |
+ classInfo: XPCOMUtils.generateCI({ | |
+ classID: Components.ID("{cda91b22-6472-11e1-aa11-834fec09cd0a}"), | |
+ contractID: "@mozilla.org/tcp-socket;1", | |
+ classDescription: "Client TCP Socket", | |
+ interfaces: [Ci.nsIDOMTCPSocket], | |
+ flags: Ci.nsIClassInfo.DOM_OBJECT, | |
+ }), | |
+ | |
+ QueryInterface: XPCOMUtils.generateQI([ | |
+ Ci.nsIDOMTCPSocket, | |
+ ]) | |
+} | |
+ | |
+function SecurityCallbacks(socket) { | |
+ this._socket = socket; | |
+} | |
+SecurityCallbacks.prototype = { | |
+ notifySSLError: function sc_notifySSLError(socketInfo, error, targetSite) { | |
+ return true; | |
+ }, | |
+ | |
+ notifyCertProblem: function sc_notifyCertProblem(socketInfo, status, | |
+ targetSite) { | |
+ this._socket.callListener("onerror", status); | |
+ this._socket.close(); | |
+ return true; | |
+ }, | |
+ | |
+ getInterface: function sc_getInterface(iid) { | |
+ return this; | |
+ } | |
+}; | |
+ | |
+ | |
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPSocket]); | |
\ No newline at end of file | |
diff --git a/b2g/components/nsIDOMTCPSocket.idl b/b2g/components/nsIDOMTCPSocket.idl | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/components/nsIDOMTCPSocket.idl | |
@@ -0,0 +1,219 @@ | |
+/* 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/. */ | |
+ | |
+/** | |
+ * MozTCPSocket exposes a TCP client socket (no server sockets yet) | |
+ * to highly privileged apps. It provides a buffered, non-blocking | |
+ * interface for sending. For receiving, it uses an asynchronous, | |
+ * event handler based interface. | |
+ */ | |
+ | |
+#include "domstubs.idl" | |
+#include "nsIDOMEvent.idl" | |
+ | |
+// Bug 731746 - Allow chrome JS object to implement nsIDOMEventTarget | |
+// nsITCPSocket should be an nsIEventTarget but js objects | |
+// cannot be an nsIEventTarget yet | |
+// #include "nsIEventTarget.idl" | |
+ | |
+// Bug 723206 - Constructors implemented in JS from IDL should be | |
+// allowed to have arguments | |
+// | |
+// Once bug 723206 will be fixed, this method could be replaced by | |
+// arguments when instantiating a TCPSocket object. For example it will | |
+// be possible to do (similarly to the WebSocket API): | |
+// var s = new MozTCPSocket(host, port); | |
+ | |
+[scriptable, uuid(b82e17da-6476-11e1-8813-57a2ffe9e42c)] | |
+interface nsIDOMTCPSocket : nsISupports | |
+{ | |
+ /** | |
+ * Create and return a socket object which will attempt to connect to | |
+ * the given host and port. | |
+ * | |
+ * @param host The hostname of the server to connect to. | |
+ * @param port The port to connect to. | |
+ * @param options An object specifying one or more parameters which | |
+ * determine the details of the socket. | |
+ * | |
+ * useSSL: true to create an SSL socket. Defaults to false. | |
+ * | |
+ * binaryType: "arraybuffer" to use UInt8 array | |
+ * instances in the ondata callback and as the argument | |
+ * to send. Defaults to "string", to use JavaScript strings. | |
+ * | |
+ * @return The new TCPSocket instance. | |
+ */ | |
+ nsIDOMTCPSocket open(in DOMString host, in unsigned short port, [optional] in jsval options); | |
+ | |
+ /** | |
+ * The host of this socket object. | |
+ */ | |
+ readonly attribute DOMString host; | |
+ | |
+ /** | |
+ * The port of this socket object. | |
+ */ | |
+ readonly attribute unsigned short port; | |
+ | |
+ /** | |
+ * True if this socket object is an SSL socket. | |
+ */ | |
+ readonly attribute boolean ssl; | |
+ | |
+ /** | |
+ * The number of bytes which have previously been buffered by calls to | |
+ * send on this socket. | |
+ */ | |
+ readonly attribute unsigned long bufferedAmount; | |
+ | |
+ /** | |
+ * Pause reading incoming data and invocations of the ondata handler until | |
+ * resume is called. | |
+ */ | |
+ void suspend(); | |
+ | |
+ /** | |
+ * Resume reading incoming data and invoking ondata as usual. | |
+ */ | |
+ void resume(); | |
+ | |
+ /** | |
+ * Close the socket. | |
+ */ | |
+ void close(); | |
+ | |
+ /** | |
+ * Write data to the socket. | |
+ * | |
+ * @param data The data to write to the socket. If | |
+ * binaryType: "arraybuffer" was passed in the options | |
+ * object, then this object should be an Uint8Array instance. | |
+ * If binaryType: "string" was passed, or if no binaryType | |
+ * option was specified, then this object should be an | |
+ * ordinary JavaScript string. | |
+ * | |
+ * @return Send returns true or false as a hint to the caller that | |
+ * they may either continue sending more data immediately, or | |
+ * may want to wait until the other side has read some of the | |
+ * data which has already been written to the socket before | |
+ * buffering more. If send returns true, then less than 64k | |
+ * has been buffered and it's safe to immediately write more. | |
+ * If send returns false, then more than 64k has been buffered, | |
+ * and the caller may wish to wait until the ondrain event | |
+ * handler has been called before buffering more data by more | |
+ * calls to send. | |
+ */ | |
+ boolean send(in jsval data); | |
+ | |
+ /** | |
+ * The readyState attribute indicates which state the socket is currently | |
+ * in. The state will be either CONNECTING, OPEN, CLOSING, or CLOSED. | |
+ */ | |
+ readonly attribute DOMString readyState; | |
+ readonly attribute DOMString CONNECTING; | |
+ readonly attribute DOMString OPEN; | |
+ readonly attribute DOMString CLOSING; | |
+ readonly attribute DOMString CLOSED; | |
+ | |
+ /** | |
+ * The binaryType attribute indicates which mode this socket uses for | |
+ * sending and receiving data. If the binaryType: "arraybuffer" option | |
+ * was passed to the open method that created this socket, binaryType | |
+ * will be "arraybuffer". Otherwise, it will be "string". | |
+ */ | |
+ readonly attribute DOMString binaryType; | |
+ | |
+ /** | |
+ * The onopen event handler is called when the connection to the server | |
+ * has been established. If the connection is refused, onerror will be | |
+ * called, instead. | |
+ */ | |
+ attribute jsval onopen; | |
+ | |
+ /** | |
+ * After send has buffered more than 64k of data, it returns false to | |
+ * indicate that the client should pause before sending more data, to | |
+ * avoid accumulating large buffers. This is only advisory, and the client | |
+ * is free to ignore it and buffer as much data as desired, but if reducing | |
+ * the size of buffers is important (especially for a streaming application) | |
+ * ondrain will be called once the previously-buffered data has been written | |
+ * to the network, at which point the client can resume calling send again. | |
+ */ | |
+ attribute jsval ondrain; | |
+ | |
+ /** | |
+ * The ondata handler will be called repeatedly and asynchronously after | |
+ * onopen has been called, every time some data was available from the server | |
+ * and was read. If binaryType: "arraybuffer" was passed to open, the data | |
+ * attribute of the event object will be an Uint8Array. If not, it will be a | |
+ * normal JavaScript string. | |
+ * | |
+ * At any time, the client may choose to pause reading and receiving ondata | |
+ * callbacks, by calling the socket's suspend() method. Further invocations | |
+ * of ondata will be paused until resume() is called. | |
+ */ | |
+ attribute jsval ondata; | |
+ | |
+ /** | |
+ * The onerror handler will be called when there is an error. The data | |
+ * attribute of the event passed to the onerror handler will have a | |
+ * description of the kind of error. | |
+ * | |
+ * If onerror is called before onopen, the error was connection refused, | |
+ * and onclose will not be called. If onerror is called after onopen, | |
+ * the connection was lost, and onclose will be called after onerror. | |
+ */ | |
+ attribute jsval onerror; | |
+ | |
+ /** | |
+ * The onclose handler is called once the underlying network socket | |
+ * has been closed, either by the server, or by the client calling | |
+ * close. | |
+ * | |
+ * If onerror was not called before onclose, then either side cleanly | |
+ * closed the connection. | |
+ */ | |
+ attribute jsval onclose; | |
+}; | |
+ | |
+/** | |
+ * nsITCPSocketEvent is the event object which is passed as the | |
+ * first argument to all the event handler callbacks. It contains | |
+ * the socket that was associated with the event, the type of event, | |
+ * and the data associated with the event (if any). | |
+ */ | |
+ | |
+[scriptable, uuid(0f2abcca-b483-4539-a3e8-345707f75c44)] | |
+interface nsITCPSocketEvent : nsISupports { | |
+ /** | |
+ * The socket object which produced this event. | |
+ */ | |
+ readonly attribute nsIDOMTCPSocket socket; | |
+ | |
+ /** | |
+ * The type of this event. One of: | |
+ * | |
+ * onopen | |
+ * onerror | |
+ * ondata | |
+ * ondrain | |
+ * onclose | |
+ */ | |
+ readonly attribute DOMString type; | |
+ | |
+ /** | |
+ * The data related to this event, if any. In the ondata callback, | |
+ * data will be the bytes read from the network; if the binaryType | |
+ * of the socket was "arraybuffer", this value will be of type Uint8Array; | |
+ * otherwise, it will be a normal JavaScript string. | |
+ * | |
+ * In the onerror callback, data will be a string with a description | |
+ * of the error. | |
+ * | |
+ * In the other callbacks, data will be an empty string. | |
+ */ | |
+ readonly attribute jsval data; | |
+}; | |
+ | |
diff --git a/b2g/test/Makefile.in b/b2g/test/Makefile.in | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/test/Makefile.in | |
@@ -0,0 +1,17 @@ | |
+# 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/. | |
+ | |
+DEPTH = ../.. | |
+topsrcdir = @top_srcdir@ | |
+srcdir = @srcdir@ | |
+VPATH = @srcdir@ | |
+relativesrcdir = b2g/test | |
+ | |
+include $(DEPTH)/config/autoconf.mk | |
+ | |
+MODULE = test_b2g | |
+ | |
+XPCSHELL_TESTS = unit | |
+ | |
+include $(topsrcdir)/config/rules.mk | |
diff --git a/b2g/test/unit/test_tcpsocket.js b/b2g/test/unit/test_tcpsocket.js | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/test/unit/test_tcpsocket.js | |
@@ -0,0 +1,461 @@ | |
+/** | |
+ * Test TCPSocket.js by creating an XPCOM-style server socket, then sending | |
+ * data in both directions and making sure each side receives their data | |
+ * correctly and with the proper events. | |
+ * | |
+ * This test is derived from netwerk/test/unit/test_socks.js, except we don't | |
+ * involve a subprocess. | |
+ * | |
+ * Future work: | |
+ * - SSL. see https://bugzilla.mozilla.org/show_bug.cgi?id=466524 | |
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=662180 | |
+ * Alternatively, mochitests could be used. | |
+ * - Testing overflow logic. | |
+ * | |
+ **/ | |
+ | |
+const Cc = Components.classes; | |
+const Ci = Components.interfaces; | |
+const Cr = Components.results; | |
+const Cu = Components.utils; | |
+const CC = Components.Constructor; | |
+ | |
+/** | |
+ * | |
+ * Constants | |
+ * | |
+ */ | |
+ | |
+// Some binary data to send. | |
+const DATA_ARRAY = [0, 255, 254, 0, 1, 2, 3, 0, 255, 255, 254, 0], | |
+ TYPED_DATA_ARRAY = new Uint8Array(DATA_ARRAY), | |
+ HELLO_WORLD = "hlo wrld. ", | |
+ BIG_ARRAY = new Array(524288); | |
+ | |
+for (var i_big = 0; i_big < BIG_ARRAY.length; i_big++) { | |
+ BIG_ARRAY[i_big] = Math.floor(Math.random() * 256); | |
+} | |
+ | |
+const BIG_TYPED_ARRAY = new Uint8Array(BIG_ARRAY); | |
+ | |
+const ServerSocket = CC("@mozilla.org/network/server-socket;1", | |
+ "nsIServerSocket", | |
+ "init"), | |
+ InputStreamPump = CC("@mozilla.org/network/input-stream-pump;1", | |
+ "nsIInputStreamPump", | |
+ "init"), | |
+ BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", | |
+ "nsIBinaryInputStream", | |
+ "setInputStream"), | |
+ BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", | |
+ "nsIBinaryOutputStream", | |
+ "setOutputStream"), | |
+ TCPSocket = new (CC("@mozilla.org/tcp-socket;1", | |
+ "nsIDOMTCPSocket"))(); | |
+ | |
+/** | |
+ * | |
+ * Helper functions | |
+ * | |
+ */ | |
+ | |
+/** | |
+ * Spin up a listening socket and associate at most one live, accepted socket | |
+ * with ourselves. | |
+ */ | |
+function TestServer() { | |
+ this.listener = ServerSocket(-1, true, -1); | |
+ do_print('server: listening on', this.listener.port); | |
+ this.listener.asyncListen(this); | |
+ | |
+ this.binaryInput = null; | |
+ this.input = null; | |
+ this.binaryOutput = null; | |
+ this.output = null; | |
+ | |
+ this.onaccept = null; | |
+ this.ondata = null; | |
+ this.onclose = null; | |
+} | |
+ | |
+TestServer.prototype = { | |
+ onSocketAccepted: function(socket, trans) { | |
+ if (this.input) | |
+ do_throw("More than one live connection!?"); | |
+ | |
+ do_print('server: got client connection'); | |
+ this.input = trans.openInputStream(0, 0, 0); | |
+ this.binaryInput = new BinaryInputStream(this.input); | |
+ this.output = trans.openOutputStream(0, 0, 0); | |
+ this.binaryOutput = new BinaryOutputStream(this.output); | |
+ | |
+ new InputStreamPump(this.input, -1, -1, 0, 0, false).asyncRead(this, null); | |
+ | |
+ if (this.onaccept) | |
+ this.onaccept(); | |
+ else | |
+ do_throw("Received unexpected connection!"); | |
+ }, | |
+ | |
+ onStopListening: function(socket) { | |
+ }, | |
+ | |
+ onDataAvailable: function(request, context, inputStream, offset, count) { | |
+ var readData = this.binaryInput.readByteArray(count); | |
+ if (this.ondata) { | |
+ try { | |
+ this.ondata(readData); | |
+ } catch(ex) { | |
+ // re-throw if this is from do_throw | |
+ if (ex === Cr.NS_ERROR_ABORT) | |
+ throw ex; | |
+ // log if there was a test problem | |
+ do_print('Caught exception: ' + ex + '\n' + ex.stack); | |
+ do_throw('test is broken; bad ondata handler; see above'); | |
+ } | |
+ } else { | |
+ do_throw('Received ' + count + ' bytes of unexpected data!'); | |
+ } | |
+ }, | |
+ | |
+ waitForData: function(expectedData) { | |
+ this.expectedData = expectedData; | |
+ }, | |
+ | |
+ onStartRequest: function(request, context) { | |
+ }, | |
+ | |
+ onStopRequest: function(request, context, status) { | |
+ if (this.onclose) | |
+ this.onclose(); | |
+ else | |
+ do_throw("Received unexpected close!"); | |
+ }, | |
+ | |
+ close: function() { | |
+ this.binaryInput.close(); | |
+ this.binaryOutput.close(); | |
+ }, | |
+ | |
+ /** | |
+ * Forget about the socket we knew about before. | |
+ */ | |
+ reset: function() { | |
+ this.binaryInput = null; | |
+ this.input = null; | |
+ this.binaryOutput = null; | |
+ this.output = null; | |
+ }, | |
+}; | |
+ | |
+function makeSuccessCase(name) { | |
+ return function() { | |
+ do_print('got expected: ' + name); | |
+ run_next_test(); | |
+ }; | |
+} | |
+ | |
+function makeJointSuccess(names) { | |
+ let funcs = {}, successCount = 0; | |
+ names.forEach(function(name) { | |
+ funcs[name] = function() { | |
+ do_print('got expected: ' + name); | |
+ if (++successCount === names.length) | |
+ run_next_test(); | |
+ }; | |
+ }); | |
+ return funcs; | |
+} | |
+ | |
+function makeFailureCase(name) { | |
+ return function() { | |
+ let argstr; | |
+ if (arguments.length) { | |
+ argstr = '(args: ' + | |
+ Array.map(arguments, function(x) { return x + ""; }).join(" ") + ')'; | |
+ } | |
+ else { | |
+ argstr = '(no arguments)'; | |
+ } | |
+ do_throw('got unexpected: ' + name + ' ' + argstr); | |
+ }; | |
+} | |
+ | |
+function makeExpectData(name, expectedData, fromEvent) { | |
+ let dataBuffer = fromEvent ? null : [], done = false; | |
+ return function(receivedData) { | |
+ if (fromEvent) { | |
+ receivedData = receivedData.data; | |
+ if (dataBuffer) { | |
+ let newBuffer = new Uint8Array(dataBuffer.length + receivedData.length); | |
+ newBuffer.set(dataBuffer, 0); | |
+ newBuffer.set(receivedData, dataBuffer.length); | |
+ dataBuffer = newBuffer; | |
+ } | |
+ else { | |
+ dataBuffer = receivedData; | |
+ } | |
+ } | |
+ else { | |
+ dataBuffer = dataBuffer.concat(receivedData); | |
+ } | |
+ do_print(name + ' received ' + receivedData.length + ' bytes'); | |
+ | |
+ if (done) | |
+ do_throw(name + ' Received data event when already done!'); | |
+ | |
+ if (dataBuffer.length >= expectedData.length) { | |
+ // check the bytes are equivalent | |
+ for (let i = 0; i < expectedData.length; i++) { | |
+ do_check_eq(dataBuffer[i], expectedData[i]); | |
+ } | |
+ if (dataBuffer.length > expectedData.length) | |
+ do_throw(name + ' Received ' + dataBuffer.length + ' bytes but only expected ' + | |
+ expectedData.length + ' bytes.'); | |
+ | |
+ done = true; | |
+ run_next_test(); | |
+ } | |
+ }; | |
+} | |
+ | |
+var server = null, sock = null, failure_drain = null; | |
+ | |
+/** | |
+ * | |
+ * Test functions | |
+ * | |
+ */ | |
+ | |
+/** | |
+ * Connect the socket to the server. This test is added as the first | |
+ * test, and is also added after every test which results in the socket | |
+ * being closed. | |
+ */ | |
+ | |
+function connectSock() { | |
+ server.reset(); | |
+ var yayFuncs = makeJointSuccess(['serveropen', 'clientopen']); | |
+ | |
+ sock = TCPSocket.open( | |
+ '127.0.0.1', server.listener.port, | |
+ { binaryType: 'arraybuffer' }); | |
+ | |
+ sock.onopen = yayFuncs.clientopen; | |
+ sock.ondrain = null; | |
+ sock.ondata = makeFailureCase('data'); | |
+ sock.onerror = makeFailureCase('error'); | |
+ sock.onclose = makeFailureCase('close'); | |
+ | |
+ server.onaccept = yayFuncs.serveropen; | |
+ server.ondata = makeFailureCase('serverdata'); | |
+ server.onclose = makeFailureCase('serverclose'); | |
+} | |
+ | |
+/** | |
+ * Test that sending a small amount of data works, and that buffering | |
+ * does not take place for this small amount of data. | |
+ */ | |
+ | |
+function sendData() { | |
+ server.ondata = makeExpectData('serverdata', DATA_ARRAY); | |
+ if (!sock.send(TYPED_DATA_ARRAY)) { | |
+ do_throw("send should not have buffered such a small amount of data"); | |
+ } | |
+} | |
+ | |
+/** | |
+ * Test that sending a large amount of data works, that buffering | |
+ * takes place (send returns true), and that ondrain is called once | |
+ * the data has been sent. | |
+ */ | |
+ | |
+function sendBig() { | |
+ var yays = makeJointSuccess(['serverdata', 'clientdrain']), | |
+ amount = 0; | |
+ | |
+ server.ondata = function (data) { | |
+ amount += data.length; | |
+ if (amount === BIG_ARRAY.length) { | |
+ yays.serverdata(); | |
+ } | |
+ }; | |
+ sock.ondrain = function(evt) { | |
+ if (sock.bufferedAmount) { | |
+ do_throw("sock.bufferedAmount was > 0 in ondrain"); | |
+ } | |
+ yays.clientdrain(evt); | |
+ } | |
+ if (sock.send(BIG_TYPED_ARRAY)) { | |
+ do_throw("expected sock.send to return false on large buffer send"); | |
+ } | |
+} | |
+ | |
+/** | |
+ * Test that data sent from the server correctly fires the ondata | |
+ * callback on the client side. | |
+ */ | |
+ | |
+function receiveData() { | |
+ server.ondata = makeFailureCase('serverdata'); | |
+ sock.ondata = makeExpectData('data', DATA_ARRAY, true); | |
+ | |
+ server.binaryOutput.writeByteArray(DATA_ARRAY, DATA_ARRAY.length); | |
+} | |
+ | |
+/** | |
+ * Test that when the server closes the connection, the onclose callback | |
+ * is fired on the client side. | |
+ */ | |
+ | |
+function serverCloses() { | |
+ // we don't really care about the server's close event, but we do want to | |
+ // make sure it happened for sequencing purposes. | |
+ var yayFuncs = makeJointSuccess(['clientclose', 'serverclose']); | |
+ sock.ondata = makeFailureCase('data'); | |
+ sock.onclose = yayFuncs.clientclose; | |
+ server.onclose = yayFuncs.serverclose; | |
+ | |
+ server.close(); | |
+} | |
+ | |
+/** | |
+ * Test that when the client closes the connection, the onclose callback | |
+ * is fired on the server side. | |
+ */ | |
+ | |
+function clientCloses() { | |
+ // we want to make sure the server heard the close and also that the client's | |
+ // onclose event fired for consistency. | |
+ var yayFuncs = makeJointSuccess(['clientclose', 'serverclose']); | |
+ server.onclose = yayFuncs.serverclose; | |
+ sock.onclose = yayFuncs.clientclose; | |
+ | |
+ sock.close(); | |
+} | |
+ | |
+/** | |
+ * Send a large amount of data and immediately call close | |
+ */ | |
+ | |
+function bufferedClose() { | |
+ var yays = makeJointSuccess(['serverdata', 'clientclose', 'serverclose']); | |
+ server.ondata = yays.serverdata; | |
+ server.onclose = yays.serverclose; | |
+ sock.onclose = yays.clientclose; | |
+ sock.send(BIG_TYPED_ARRAY); | |
+ sock.close(); | |
+} | |
+ | |
+/** | |
+ * Connect to a port we know is not listening so an error is assured, | |
+ * and make sure that onerror and onclose are fired on the client side. | |
+ */ | |
+ | |
+function badConnect() { | |
+ // There's probably nothing listening on tcp port 2. | |
+ sock = TCPSocket.open('127.0.0.1', 2); | |
+ | |
+ sock.onopen = makeFailureCase('open'); | |
+ sock.ondata = makeFailureCase('data'); | |
+ sock.onclose = makeFailureCase('close'); | |
+ | |
+ sock.onerror = makeSuccessCase('error'); | |
+} | |
+ | |
+/** | |
+ * Test that calling send with enough data to buffer causes ondrain to | |
+ * be invoked once the data has been sent, and then test that calling send | |
+ * and buffering again causes ondrain to be fired again. | |
+ */ | |
+ | |
+function drainTwice() { | |
+ let yays = makeJointSuccess( | |
+ ['ondrain', 'ondrain2', 'serverclose', 'clientclose']); | |
+ | |
+ server.ondata = function() {} | |
+ server.onclose = yays.serverclose; | |
+ | |
+ sock.onclose = yays.clientclose; | |
+ sock.ondrain = function () { | |
+ sock.ondrain = function () { | |
+ yays.ondrain2(); | |
+ } | |
+ if (sock.send(BIG_ARRAY)) { | |
+ throw new Error("sock.send(BIG_ARRAY) did not return false to indicate buffering"); | |
+ } | |
+ sock.close(); | |
+ yays.ondrain(); | |
+ } | |
+ | |
+ if (sock.send(BIG_ARRAY)) { | |
+ throw new Error("sock.send(BIG_ARRAY) did not return false to indicate buffering"); | |
+ } | |
+} | |
+ | |
+function cleanup() { | |
+ sock.close(); | |
+ makeSuccessCase('cleanup')(); | |
+} | |
+ | |
+function bufferTwice() { | |
+ let yays = makeJointSuccess( | |
+ ['ondrain', 'serverclose', 'clientclose']); | |
+ | |
+ server.ondata = function() {} | |
+ server.onclose = yays.serverclose; | |
+ sock.onclose = yays.clientclose; | |
+ | |
+ sock.ondrain = function () { | |
+ sock.close(); | |
+ yays.ondrain(); | |
+ } | |
+ | |
+ if (sock.send(BIG_ARRAY)) { | |
+ throw new Error("sock.send(BIG_ARRAY) did not return false to indicate buffering"); | |
+ } | |
+ if (sock.send(BIG_ARRAY)) { | |
+ throw new Error("sock.send(BIG_ARRAY) did not return false to indicate buffering on second synchronous call to send"); | |
+ } | |
+} | |
+ | |
+// - connect, data and events work both ways | |
+add_test(connectSock); | |
+add_test(sendData); | |
+add_test(sendBig); | |
+add_test(receiveData); | |
+// - server closes on us | |
+add_test(serverCloses); | |
+ | |
+// - connect, we close on the server | |
+add_test(connectSock); | |
+add_test(clientCloses); | |
+ | |
+// - connect, buffer, close | |
+add_test(connectSock); | |
+add_test(bufferedClose); | |
+ | |
+// - get an error on an attempt to connect to a non-listening port | |
+add_test(badConnect); | |
+ | |
+// send a buffer, get a drain, send a buffer, get a drain | |
+add_test(connectSock); | |
+add_test(drainTwice); | |
+ | |
+// send a buffer, get a drain, send a buffer, get a drain | |
+add_test(connectSock); | |
+add_test(bufferTwice); | |
+ | |
+// clean up | |
+add_test(cleanup); | |
+ | |
+function run_test() { | |
+ server = new TestServer(); | |
+ | |
+ run_next_test(); | |
+ | |
+ do_timeout(10000, function() { | |
+ do_throw( | |
+ "The test should never take this long unless the system is hosed."); | |
+ }); | |
+} | |
diff --git a/b2g/test/unit/xpcshell.ini b/b2g/test/unit/xpcshell.ini | |
new file mode 100644 | |
--- /dev/null | |
+++ b/b2g/test/unit/xpcshell.ini | |
@@ -0,0 +1,5 @@ | |
+[DEFAULT] | |
+head = | |
+tail = | |
+ | |
+[test_tcpsocket.js] | |
diff --git a/testing/xpcshell/xpcshell.ini b/testing/xpcshell/xpcshell.ini | |
--- a/testing/xpcshell/xpcshell.ini | |
+++ b/testing/xpcshell/xpcshell.ini | |
@@ -122,9 +122,11 @@ | |
[include:netwerk/test/unit_ipc/xpcshell.ini] | |
[include:netwerk/cookie/test/unit_ipc/xpcshell.ini] | |
[include:toolkit/components/contentprefs/tests/unit_ipc/xpcshell.ini] | |
[include:uriloader/exthandler/tests/unit_ipc/xpcshell.ini] | |
[include:modules/libmar/tests/unit/xpcshell.ini] | |
skip-if = os == "android" | |
+[include:b2g/test/unit/xpcshell.ini] | |
+ | |
[include:tools/profiler/tests/xpcshell.ini] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment