Back out 3 changesets (bug 885982, bug 1118063) for b2g mochitest-6 bustage
authorPhil Ringnalda <philringnalda@gmail.com>
Sat, 17 Jan 2015 13:26:10 -0800
changeset 253203 0c454540fc2b709e1b32456be700ebcfa8713093
parent 253202 ee6ddbf938c7ee1137173b3861071d9225c2380d
child 253204 63f9aed866c05a8844f480bb6680ade5440c2f27
child 253208 25eb0e4b088633ad99e0bd1226e8e35bf494a7fb
child 253215 b8059c92cd31218e7bb79c2efe7d7ac5988c8c78
push id721
push userjlund@mozilla.com
push dateTue, 21 Apr 2015 23:03:33 +0000
treeherdermozilla-release@d27c9211ebb3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs885982, 1118063
milestone38.0a1
backs out865e7bc208dfde6b92fdfd074995728de78189f7
9ede577f5ada3f664d7a9bc13d2f617901349e00
6ccc86f7429e38dec4343a43f7d994c4c553e91e
first release with
nightly linux32
0c454540fc2b / 38.0a1 / 20150118030202 / files
nightly linux64
0c454540fc2b / 38.0a1 / 20150118030202 / files
nightly mac
0c454540fc2b / 38.0a1 / 20150118030202 / files
nightly win32
0c454540fc2b / 38.0a1 / 20150118030202 / files
nightly win64
0c454540fc2b / 38.0a1 / 20150118030202 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Back out 3 changesets (bug 885982, bug 1118063) for b2g mochitest-6 bustage CLOSED TREE Backed out changeset 865e7bc208df (bug 885982) Backed out changeset 9ede577f5ada (bug 885982) Backed out changeset 6ccc86f7429e (bug 1118063)
CLOBBER
dom/bindings/Bindings.conf
dom/network/TCPServerSocket.js
dom/network/TCPServerSocketChild.cpp
dom/network/TCPServerSocketParent.cpp
dom/network/TCPServerSocketParent.h
dom/network/TCPSocket.js
dom/network/TCPSocket.manifest
dom/network/TCPSocketChild.cpp
dom/network/TCPSocketParent.cpp
dom/network/TCPSocketParent.h
dom/network/TCPSocketParentIntermediary.js
dom/network/TCPSocketUtils.cpp
dom/network/TCPSocketUtils.h
dom/network/interfaces/moz.build
dom/network/interfaces/nsIDOMTCPServerSocket.idl
dom/network/interfaces/nsIDOMTCPSocket.idl
dom/network/interfaces/nsITCPServerSocketChild.idl
dom/network/interfaces/nsITCPServerSocketInternal.idl
dom/network/interfaces/nsITCPServerSocketParent.idl
dom/network/interfaces/nsITCPSocketInternal.idl
dom/network/interfaces/nsITCPSocketParent.idl
dom/network/moz.build
dom/network/tests/mochitest.ini
dom/network/tests/test_tcpsocket_client_and_server_basics.js
dom/network/tests/test_tcpsocket_default_permissions.html
dom/network/tests/test_tcpsocket_enabled_no_perm.html
dom/network/tests/test_tcpsocket_enabled_with_perm.html
dom/network/tests/unit/test_tcpsocket.js
dom/network/tests/unit_ipc/test_tcpsocket_ipc.js
dom/permission/tests/test_tcp-socket.html
dom/webidl/TCPServerSocket.webidl
dom/webidl/TCPServerSocketEvent.webidl
dom/webidl/TCPSocket.webidl
dom/webidl/TCPSocketEvent.webidl
dom/webidl/moz.build
--- a/CLOBBER
+++ b/CLOBBER
@@ -17,9 +17,9 @@
 #
 # Modifying this file will now automatically clobber the buildbot machines \o/
 #
 
 # Are you updating CLOBBER because you think it's needed for your WebIDL
 # changes to stick? As of bug 928195, this shouldn't be necessary! Please
 # don't change CLOBBER for WebIDL changes any more.
 
-Renaming nsIDOMTCPSocket.idl and nsIDOMTCPServerSocket.idl for bug 885982 and bug 1118063.
+Bug 1121297 - Converted VolatileBuffer's CPP tests to GTests
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -1873,18 +1873,16 @@ addExternalIface('nsIMessageBroadcaster'
                  headerFile='nsIMessageManager.h', notflattened=True)
 addExternalIface('nsISelectionListener', nativeType='nsISelectionListener')
 addExternalIface('nsIStreamListener', nativeType='nsIStreamListener', notflattened=True)
 addExternalIface('nsISupports', nativeType='nsISupports')
 addExternalIface('nsIDocShell', nativeType='nsIDocShell', notflattened=True)
 addExternalIface('nsIEditor', nativeType='nsIEditor', notflattened=True)
 addExternalIface('nsIVariant', nativeType='nsIVariant', notflattened=True)
 addExternalIface('nsIScriptableRegion', nativeType='nsIScriptableRegion', notflattened=True)
-addExternalIface('nsITCPServerSocketInternal', nativeType='nsITCPServerSocketInternal', notflattened=True)
-addExternalIface('nsITCPSocketInternal', nativeType='nsITCPSocketInternal', notflattened=True)
 addExternalIface('OutputStream', nativeType='nsIOutputStream',
                  notflattened=True)
 addExternalIface('Principal', nativeType='nsIPrincipal',
                  headerFile='nsIPrincipal.h', notflattened=True)
 addExternalIface('StackFrame', nativeType='nsIStackFrame',
                  headerFile='nsIException.h', notflattened=True)
 addExternalIface('URI', nativeType='nsIURI', headerFile='nsIURI.h',
                  notflattened=True)
--- a/dom/network/TCPServerSocket.js
+++ b/dom/network/TCPServerSocket.js
@@ -6,122 +6,91 @@
 
 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");
 
 const ServerSocket = CC(
         '@mozilla.org/network/server-socket;1', 'nsIServerSocket', 'init'),
       TCPSocketInternal = Cc[
         '@mozilla.org/tcp-socket;1'].createInstance(Ci.nsITCPSocketInternal);
 
 /*
  * Debug logging function
  */
 
-let debug = false;
+let debug = true;
 function LOG(msg) {
   if (debug) {
     dump("TCPServerSocket: " + msg + "\n");
   }
 }
 
 /*
  * nsIDOMTCPServerSocket object
  */
 
 function TCPServerSocket() {
-  this.makeGetterSetterEH("onconnect");
-  this.makeGetterSetterEH("onerror");
-
   this._localPort = 0;
   this._binaryType = null;
 
+  this._onconnect = null;
+  this._onerror = null;
+
   this._inChild = false;
   this._neckoTCPServerSocket = null;
   this._serverBridge = null;
+  this.useWin = null;
 }
 
+// When this API moves to WebIDL and these __exposedProps__ go away, remove
+// this call here and remove the API from XPConnect.
+Cu.skipCOWCallableChecks();
+
 TCPServerSocket.prototype = {
+  __exposedProps__: {
+    localPort: 'r',
+    onconnect: 'rw',
+    onerror: 'rw'
+  },
   get localPort() {
     return this._localPort;
   },
-
-  getEH: function(type) {
-    return this.__DOM_IMPL__.getEventHandler(type);
+  get onconnect() {
+    return this._onconnect;
   },
-  setEH: function(type, handler) {
-    this.__DOM_IMPL__.setEventHandler(type, handler);
+  set onconnect(f) {
+    this._onconnect = f;
   },
-  makeGetterSetterEH: function(name) {
-    Object.defineProperty(this, name,
-                          {
-                            get:function()  { return this.getEH(name); },
-                            set:function(h) { return this.setEH(name, h); }
-                          });
+  get onerror() {
+    return this._onerror;
+  },
+  set onerror(f) {
+    this._onerror = f;
   },
 
   _callListenerAcceptCommon: function tss_callListenerAcceptCommon(socket) {
-    try {
-      this.__DOM_IMPL__.dispatchEvent(new this.useGlobal.TCPServerSocketEvent("connect", {socket: socket}));
-    } catch(e) {
-      LOG('exception in _callListenerAcceptCommon: ' + e);
+    if (this._onconnect) {
+      try {
+        this["onconnect"].call(null, socket);
+      } catch (e) {
+        socket.close();
+      }
+    }
+    else {
       socket.close();
+      dump("Received unexpected connection!");
     }
   },
   init: function tss_init(aWindowObj) {
-    this.useGlobal = aWindowObj;
-
-    if (aWindowObj) {
-      Services.obs.addObserver(this, "inner-window-destroyed", true);
-
-      let util = aWindowObj.QueryInterface(
-        Ci.nsIInterfaceRequestor
-      ).getInterface(Ci.nsIDOMWindowUtils);
-      this.innerWindowID = util.currentInnerWindowID;
-
-      LOG("window init: " + this.innerWindowID);
-    }
-  },
-
-  __init: function(port, options, backlog) {
-    this.listen(port, options, backlog);
-  },
-
-  initWithGlobal: function(global) {
-    this.useGlobal = global;
-  },
-
-  observe: function(aSubject, aTopic, aData) {
-    let wId;
-    try {
-      wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
-    } catch(x) {
-      wId = 0;
-    }
-
-    if (wId == this.innerWindowID) {
-      LOG("inner-window-destroyed: " + this.innerWindowID);
-
-      // This window is now dead, so we want to clear the callbacks
-      // so that we don't get a "can't access dead object" when the
-      // underlying stream goes to tell us that we are closed
-      this.onconnect = null;
-      this.onerror = null;
-
-      this.useGlobal = null;
-
-      // Clean up our socket
-      this.close();
-    }
+    this.useWin = aWindowObj;
   },
 
   /* nsITCPServerSocketInternal method */
   listen: function tss_listen(localPort, options, backlog) {
     this._inChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
                        .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
     this._binaryType = options.binaryType;
 
@@ -144,29 +113,27 @@ TCPServerSocket.prototype = {
       else {
         throw new Error("Parent TCPServerSocket has already listening. \n");
       }
     }
   },
 
   callListenerAccept: function tss_callListenerSocket(socketChild) {
     // this method is called at child process when the socket is accepted at parent process.
-    let socket = new this.useGlobal.mozTCPSocket("", 0, {doNotConnect: true});
-    socket.getInternalSocket().initAcceptedChild(socketChild, this._binaryType, this.useGlobal);
+    let socket = TCPSocketInternal.createAcceptedChild(socketChild, this._binaryType, this.useWin);
     this._callListenerAcceptCommon(socket);
   },
 
   callListenerError: function tss_callListenerError(message, filename, lineNumber, columnNumber) {
-    let init = {
-      message: message,
-      filename: filename,
-      lineno: lineNumber,
-      colno: columnNumber,
-    };
-    this.__DOM_IMPL__.dispatchEvent(new this.useGlobal.ErrorEvent("error", init));
+    if (this._onerror) {
+      var type = "error";
+      var error = new Error(message, filename, lineNumber, columnNumber);
+
+      this["onerror"].call(null, new TCPSocketEvent(type, this, error));
+    }
   },
   /* end nsITCPServerSocketInternal method */
 
   close: function tss_close() {
     if (this._inChild) {
       this._serverBridge.close();
       return;
     }
@@ -176,42 +143,45 @@ TCPServerSocket.prototype = {
       this._neckoTCPServerSocket.close();
     }
   },
 
   // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
   onSocketAccepted: function tss_onSocketAccepted(server, trans) {
     // precondition: this._inChild == false
     try {
-      let that = new this.useGlobal.mozTCPSocket("", 0, {doNotConnect: true});
-      that.getInternalSocket().initAcceptedParent(trans, this._binaryType, this.useGlobal);
+      let that = TCPSocketInternal.createAcceptedParent(trans, this._binaryType,
+                                                        this.useWin);
       this._callListenerAcceptCommon(that);
     }
     catch(e) {
-      LOG('exception in onSocketAccepted: ' + e);
       trans.close(Cr.NS_BINDING_ABORTED);
     }
   },
 
   // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
   onStopListening: function tss_onStopListening(server, status) {
     if (status != Cr.NS_BINDING_ABORTED) {
       throw new Error("Server socket was closed by unexpected reason.");
     }
     this._neckoTCPServerSocket = null;
   },
 
-  getInternalSocket: function() {
-    return this.QueryInterface(Ci.nsITCPServerSocketInternal);
-  },
-
   classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
 
-  contractID: "@mozilla.org/tcp-server-socket;1",
+  classInfo: XPCOMUtils.generateCI({
+    classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
+    classDescription: "Server TCP Socket",
+    interfaces: [
+      Ci.nsIDOMTCPServerSocket,
+      Ci.nsISupportsWeakReference
+    ],
+    flags: Ci.nsIClassInfo.DOM_OBJECT,
+  }),
+
   QueryInterface: XPCOMUtils.generateQI([
+    Ci.nsIDOMTCPServerSocket,
     Ci.nsITCPServerSocketInternal,
-    Ci.nsIDOMGlobalPropertyInitializer,
-    Ci.nsISupportsWeakReference,
-    Ci.nsIObserver
+    Ci.nsISupportsWeakReference
   ])
 }
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPServerSocket]);
--- a/dom/network/TCPServerSocketChild.cpp
+++ b/dom/network/TCPServerSocketChild.cpp
@@ -2,17 +2,17 @@
  * 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/. */
 
 #include "TCPServerSocketChild.h"
 #include "TCPSocketChild.h"
 #include "mozilla/net/NeckoChild.h"
 #include "mozilla/dom/PBrowserChild.h"
 #include "mozilla/dom/TabChild.h"
-#include "nsITCPServerSocketInternal.h"
+#include "nsIDOMTCPSocket.h"
 #include "nsJSUtils.h"
 #include "jsfriendapi.h"
 
 using mozilla::net::gNeckoChild;
 
 namespace mozilla {
 namespace dom {
 
--- a/dom/network/TCPServerSocketParent.cpp
+++ b/dom/network/TCPServerSocketParent.cpp
@@ -2,21 +2,18 @@
  * 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/. */
 
 #include "TCPServerSocketParent.h"
 #include "nsJSUtils.h"
 #include "TCPSocketParent.h"
 #include "mozilla/unused.h"
 #include "mozilla/AppProcessChecker.h"
-#include "mozilla/dom/BindingUtils.h"
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/TabParent.h"
-#include "nsGlobalWindow.h"
-#include "nsITCPServerSocketInternal.h"
 
 namespace mozilla {
 namespace dom {
 
 static void
 FireInteralError(mozilla::net::PTCPServerSocketParent* aActor,
                  uint32_t aLineNo)
 {
@@ -29,26 +26,16 @@ NS_IMPL_CYCLE_COLLECTION(TCPServerSocket
 NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent)
   NS_INTERFACE_MAP_ENTRY(nsITCPServerSocketParent)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
-TCPServerSocketParent::TCPServerSocketParent()
-: mNeckoParent(nullptr)
-, mIPCOpen(false)
-{
-}
-
-TCPServerSocketParent::~TCPServerSocketParent()
-{
-}
-
 void
 TCPServerSocketParent::ReleaseIPDLReference()
 {
   MOZ_ASSERT(mIPCOpen);
   mIPCOpen = false;
   this->Release();
 }
 
--- a/dom/network/TCPServerSocketParent.h
+++ b/dom/network/TCPServerSocketParent.h
@@ -1,58 +1,52 @@
 /* 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/. */
 
-#ifndef mozilla_dom_TCPServerSocketParent_h
-#define mozilla_dom_TCPServerSocketParent_h
-
 #include "mozilla/net/PNeckoParent.h"
 #include "mozilla/net/PTCPServerSocketParent.h"
 #include "nsITCPSocketParent.h"
 #include "nsITCPServerSocketParent.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsCOMPtr.h"
-
-class nsITCPServerSocketInternal;
+#include "nsIDOMTCPSocket.h"
 
 namespace mozilla {
 namespace dom {
 
 class PBrowserParent;
 
 class TCPServerSocketParent : public mozilla::net::PTCPServerSocketParent
                             , public nsITCPServerSocketParent
 {
 public:
   NS_DECL_CYCLE_COLLECTION_CLASS(TCPServerSocketParent)
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_NSITCPSERVERSOCKETPARENT
 
-  TCPServerSocketParent();
+  TCPServerSocketParent() : mNeckoParent(nullptr), mIPCOpen(false) {}
 
   bool Init(PNeckoParent* neckoParent, const uint16_t& aLocalPort, const uint16_t& aBacklog,
             const nsString& aBinaryType);
 
   virtual bool RecvClose() MOZ_OVERRIDE;
   virtual bool RecvRequestDelete() MOZ_OVERRIDE;
 
   uint32_t GetAppId();
   bool GetInBrowser();
 
   void AddIPDLReference();
   void ReleaseIPDLReference();
 
 private:
-  ~TCPServerSocketParent();
+  ~TCPServerSocketParent() {}
 
   virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE;
 
   PNeckoParent* mNeckoParent;
   nsCOMPtr<nsITCPSocketIntermediary> mIntermediary;
-  nsCOMPtr<nsITCPServerSocketInternal> mServerSocket;
+  nsCOMPtr<nsIDOMTCPServerSocket> mServerSocket;
   bool mIPCOpen;
 };
 
 } // namespace dom
 } // namespace mozilla
-
-#endif // mozilla_dom_TCPServerSocketParent_h
--- a/dom/network/TCPSocket.js
+++ b/dom/network/TCPSocket.js
@@ -8,20 +8,16 @@ 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");
 
-XPCOMUtils.defineLazyServiceGetter(this, "gSocketTransportService",
-                                   "@mozilla.org/network/socket-transport-service;1",
-                                   "nsISocketTransportService");
-
 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"),
@@ -44,54 +40,118 @@ const BUFFER_SIZE = 65536;
 const NETWORK_STATS_THRESHOLD = 65536;
 
 // XXX we have no TCPError implementation right now because it's really hard to
 // do on b2g18.  On mozilla-central we want a proper TCPError that ideally
 // sub-classes DOMError.  Bug 867872 has been filed to implement this and
 // contains a documented TCPError.webidl that maps all the error codes we use in
 // this file to slightly more readable explanations.
 function createTCPError(aWindow, aErrorName, aErrorType) {
-  return new DOMError(aErrorName);
+  return new (aWindow ? aWindow.DOMError : DOMError)(aErrorName);
 }
 
 
 /*
  * Debug logging function
  */
 
 let debug = false;
 function LOG(msg) {
   if (debug)
     dump("TCPSocket: " + msg + "\n");
 }
 
 /*
+ * nsITCPSocketEvent object
+ */
+
+function TCPSocketEvent(type, sock, data) {
+  this._type = type;
+  this._target = sock;
+  this._data = data;
+}
+
+// When this API moves to WebIDL and these __exposedProps__ go away, remove
+// this call here and remove the API from XPConnect.
+Cu.skipCOWCallableChecks();
+
+TCPSocketEvent.prototype = {
+  __exposedProps__: {
+    type: 'r',
+    target: 'r',
+    data: 'r',
+    // Promise::ResolveInternal tries to check if the thing being resolved is
+    // itself a promise through the presence of "then".  Accordingly, we list
+    // it as an exposed property, although we return undefined for it.
+    // Bug 882123 covers making TCPSocket be a proper event target with proper
+    // events.
+    then: 'r'
+  },
+  get type() {
+    return this._type;
+  },
+  get target() {
+    return this._target;
+  },
+  get data() {
+    return this._data;
+  },
+  get then() {
+    return undefined;
+  }
+}
+
+/*
  * nsIDOMTCPSocket object
  */
 
 function TCPSocket() {
-  this.makeGetterSetterEH("onopen");
-  this.makeGetterSetterEH("onclose");
-  this.makeGetterSetterEH("ondrain");
-  this.makeGetterSetterEH("ondata");
-  this.makeGetterSetterEH("onerror");
+  this._readyState = kCLOSED;
 
-  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;
+
+  this.useWin = null;
 }
 
 TCPSocket.prototype = {
+  __exposedProps__: {
+    open: 'r',
+    host: 'r',
+    port: 'r',
+    ssl: 'r',
+    bufferedAmount: 'r',
+    suspend: 'r',
+    resume: 'r',
+    close: 'r',
+    send: 'r',
+    readyState: 'r',
+    binaryType: 'r',
+    listen: 'r',
+    onopen: 'rw',
+    ondrain: 'rw',
+    ondata: 'rw',
+    onerror: 'rw',
+    onclose: 'rw'
+  },
   // The binary type, "string" or "arraybuffer"
   _binaryType: null,
 
+  // Internal
+  _hasPrivileges: null,
+
   // Raw socket streams
   _transport: null,
   _socketInputStream: null,
   _socketOutputStream: null,
 
   // Input stream machinery
   _inputStreamPump: null,
   _inputStreamScriptable: null,
@@ -145,47 +205,64 @@ TCPSocket.prototype = {
     return this._ssl;
   },
   get bufferedAmount() {
     if (this._inChild) {
       return this._bufferedAmount;
     }
     return this._multiplexStream.available();
   },
-
-  getEH: function(type) {
-    return this.__DOM_IMPL__.getEventHandler(type);
-   },
-  setEH: function(type, handler) {
-    this.__DOM_IMPL__.setEventHandler(type, handler);
-   },
-  makeGetterSetterEH: function(name) {
-    Object.defineProperty(this, name,
-                          {
-                            get:function()  { return this.getEH(name); },
-                            set:function(h) { return this.setEH(name, h); }
-                          });
-   },
-
+  get onopen() {
+    return this._onopen;
+  },
+  set onopen(f) {
+    this._onopen = f;
+  },
+  get ondrain() {
+    return this._ondrain;
+  },
+  set ondrain(f) {
+    this._ondrain = f;
+  },
+  get ondata() {
+    return this._ondata;
+  },
+  set ondata(f) {
+    this._ondata = f;
+  },
+  get onerror() {
+    return this._onerror;
+  },
+  set onerror(f) {
+    this._onerror = f;
+  },
+  get onclose() {
+    return this._onclose;
+  },
+  set onclose(f) {
+    this._onclose = f;
+  },
 
   _activateTLS: function() {
     let securityInfo = this._transport.securityInfo
           .QueryInterface(Ci.nsISSLSocketControl);
     securityInfo.StartTLS();
   },
 
   // Helper methods.
   _createTransport: function ts_createTransport(host, port, sslMode) {
     let options;
     if (sslMode === 'ssl') {
       options = ['ssl'];
     } else {
       options = ['starttls'];
     }
-    return gSocketTransportService.createTransport(options, 1, host, port, null);
+    return Cc["@mozilla.org/network/socket-transport-service;1"]
+             .getService(Ci.nsISocketTransportService)
+             .createTransport(options, 1, host, port, null);
   },
 
   _sendBufferedAmount: function ts_sendBufferedAmount() {
     if (this._onUpdateBufferedAmount) {
       this._onUpdateBufferedAmount(this.bufferedAmount, this._trackingNumber);
     }
   },
 
@@ -302,17 +379,20 @@ TCPSocket.prototype = {
 
     // Reset the counters once the statistics is saved to NetworkStatsServiceProxy.
     this._txBytes = this._rxBytes = 0;
   },
   // End of helper method for network statistics.
 #endif
 
   callListener: function ts_callListener(type, data) {
-    this.__DOM_IMPL__.dispatchEvent(new this.useWin.TCPSocketEvent(type, {data: data || ""}));
+    if (!this["on" + type])
+      return;
+
+    this["on" + type].call(null, new TCPSocketEvent(type, this, data || ""));
   },
 
   /* nsITCPSocketInternal methods */
   callListenerError: function ts_callListenerError(type, name) {
     // XXX we're not really using TCPError at this time, so there's only a name
     // attribute to pass.
     this.callListener(type, createTCPError(this.useWin, name));
   },
@@ -356,16 +436,49 @@ TCPSocket.prototype = {
         this.callListener("drain");
       }
     } else {
       LOG("bufferedAmount is updated but haven't reaches zero. bufferedAmount: " +
           bufferedAmount);
     }
   },
 
+  createAcceptedParent: function ts_createAcceptedParent(transport, binaryType, windowObject) {
+    let that = new TCPSocket();
+    that._transport = transport;
+    that._initStream(binaryType);
+
+    // ReadyState is kOpen since accepted transport stream has already been connected
+    that._readyState = kOPEN;
+    that._inputStreamPump = new InputStreamPump(that._socketInputStream, -1, -1, 0, 0, false);
+    that._inputStreamPump.asyncRead(that, null);
+
+    // Grab host/port from SocketTransport.
+    that._host = transport.host;
+    that._port = transport.port;
+    that.useWin = windowObject;
+
+    return that;
+  },
+
+  createAcceptedChild: function ts_createAcceptedChild(socketChild, binaryType, windowObject) {
+    let that = new TCPSocket();
+
+    that._binaryType = binaryType;
+    that._inChild = true;
+    that._readyState = kOPEN;
+    socketChild.setSocketAndWindow(that, windowObject);
+    that._socketBridge = socketChild;
+    that._host = socketChild.host;
+    that._port = socketChild.port;
+    that.useWin = windowObject;
+
+    return that;
+  },
+
   setAppId: function ts_setAppId(appId) {
 #ifdef MOZ_WIDGET_GONK
     this._appId = appId;
 #else
     // Do nothing because _appId only exists on Gonk-specific platform.
 #endif
   },
 
@@ -393,78 +506,46 @@ TCPSocket.prototype = {
    */
   onRecvSendFromChild: function(data, byteOffset, byteLength, trackingNumber) {
     this._trackingNumber = trackingNumber;
     this.send(data, byteOffset, byteLength);
   },
 
   /* end nsITCPSocketInternal methods */
 
-  init: function(aWindow) {
-    this._inChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
-                       .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
-    LOG("content process: " + (this._inChild ? "true" : "false"));
-
-    if (aWindow) {
-      let util = aWindow.QueryInterface(
-        Ci.nsIInterfaceRequestor
-      ).getInterface(Ci.nsIDOMWindowUtils);
-
-      this.useWin = aWindow;
-      this.innerWindowID = util.currentInnerWindowID;
-      LOG("window init: " + this.innerWindowID);
-      Services.obs.addObserver(this, "inner-window-destroyed", true);
+  initWindowless: function ts_initWindowless() {
+    try {
+      return Services.prefs.getBoolPref("dom.mozTCPSocket.enabled");
+    } catch (e) {
+      // no pref means return false
+      return false;
     }
   },
 
-  __init: function(host, port, options) {
-    if (options.doNotConnect) {
-	return;
-    }
+  init: function ts_init(aWindow) {
+    if (!this.initWindowless())
+      return null;
 
-    LOG("Host info: " + host + ":" + port);
-    this._readyState = kCONNECTING;
-    this._host = host;
-    this._port = port;
-    if (options) {
-      if (options.useSSL) {
-          this._ssl = 'ssl';
-      } else {
-          this._ssl = false;
-      }
-      this._binaryType = options.binaryType || this._binaryType;
-    }
-
-    LOG("SSL: " + this.ssl);
+    let principal = aWindow.document.nodePrincipal;
+    let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
+                   .getService(Ci.nsIScriptSecurityManager);
 
-    if (this._inChild) {
-      this._socketBridge = Cc["@mozilla.org/tcp-socket-child;1"]
-                             .createInstance(Ci.nsITCPSocketChild);
-      this._socketBridge.sendOpen(this, host, port, !!this._ssl,
-                                  this._binaryType, this.useWin, this.useWin || this);
-      return;
-    }
+    let perm = principal == secMan.getSystemPrincipal()
+                 ? Ci.nsIPermissionManager.ALLOW_ACTION
+                 : Services.perms.testExactPermissionFromPrincipal(principal, "tcp-socket");
 
-    let transport = this._transport = this._createTransport(host, port, this._ssl);
-    transport.setEventSink(this, Services.tm.currentThread);
-    this._initStream(this._binaryType);
+    this._hasPrivileges = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
 
-#ifdef MOZ_WIDGET_GONK
-    // Set _activeNetwork, which is only required for network statistics.
-    // Note that nsINetworkManager, as well as nsINetworkStatsServiceProxy, is
-    // Gonk-specific.
-    let networkManager = Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
-    if (networkManager) {
-      this._activeNetwork = networkManager.active;
-    }
-#endif
-  },
+    let util = aWindow.QueryInterface(
+      Ci.nsIInterfaceRequestor
+    ).getInterface(Ci.nsIDOMWindowUtils);
 
-  initWithGlobal: function(global) {
-    this.useWin = global;
+    this.useWin = XPCNativeWrapper.unwrap(aWindow);
+    this.innerWindowID = util.currentInnerWindowID;
+    LOG("window init: " + this.innerWindowID);
   },
 
   observe: function(aSubject, aTopic, aData) {
     if (aTopic == "inner-window-destroyed") {
       let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
       if (wId == this.innerWindowID) {
         LOG("inner-window-destroyed: " + this.innerWindowID);
 
@@ -480,43 +561,78 @@ TCPSocket.prototype = {
         this.useWin = null;
 
         // Clean up our socket
         this.close();
       }
     }
   },
 
-  initAcceptedParent: function(transport, binaryType, global) {
-    this.useWin = global;
-    this._transport = transport;
-    this._initStream(binaryType);
+  // nsIDOMTCPSocket
+  open: function ts_open(host, port, options) {
+    this._inChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
+                       .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
+    LOG("content process: " + (this._inChild ? "true" : "false"));
+
+    // 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();
+
+    that.useWin = this.useWin;
+    that.innerWindowID = this.innerWindowID;
+    that._inChild = this._inChild;
+
+    LOG("window init: " + that.innerWindowID);
+    Services.obs.addObserver(that, "inner-window-destroyed", true);
+
+    LOG("startup called");
+    LOG("Host info: " + host + ":" + port);
 
-    // ReadyState is kOpen since accepted transport stream has already been connected
-    this._readyState = kOPEN;
-    this._inputStreamPump = new InputStreamPump(this._socketInputStream, -1, -1, 0, 0, false);
-    this._inputStreamPump.asyncRead(this, null);
+    that._readyState = kCONNECTING;
+    that._host = host;
+    that._port = port;
+    if (options !== undefined) {
+      if (options.useSecureTransport) {
+          that._ssl = 'ssl';
+      } else {
+          that._ssl = false;
+      }
+      that._binaryType = options.binaryType || that._binaryType;
+    }
+
+    LOG("SSL: " + that.ssl);
 
-    // Grab host/port from SocketTransport.
-    this._host = transport.host;
-    this._port = transport.port;
+    if (this._inChild) {
+      that._socketBridge = Cc["@mozilla.org/tcp-socket-child;1"]
+                             .createInstance(Ci.nsITCPSocketChild);
+      that._socketBridge.sendOpen(that, host, port, !!that._ssl,
+                                  that._binaryType, this.useWin, this.useWin || this);
+      return that;
+    }
+
+    let transport = that._transport = this._createTransport(host, port, that._ssl);
+    transport.setEventSink(that, Services.tm.currentThread);
+    that._initStream(that._binaryType);
+
+#ifdef MOZ_WIDGET_GONK
+    // Set _activeNetwork, which is only required for network statistics.
+    // Note that nsINetworkManager, as well as nsINetworkStatsServiceProxy, is
+    // Gonk-specific.
+    let networkManager = Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
+    if (networkManager) {
+      that._activeNetwork = networkManager.active;
+    }
+#endif
+
+    return that;
   },
 
-  initAcceptedChild: function(socketChild, binaryType, global) {
-    this.useWin = global;
-    this._binaryType = binaryType;
-    this._inChild = true;
-    this._readyState = kOPEN;
-    socketChild.setSocketAndWindow(this.getInternalSocket(), this.useWin);
-    this._socketBridge = socketChild;
-    this._host = socketChild.host;
-    this._port = socketChild.port;
-  },
-
-  // nsIDOMTCPSocket
   upgradeToSecure: function ts_upgradeToSecure() {
     if (this._readyState !== kOPEN) {
       throw new Error("Socket not open.");
     }
     if (this._ssl == 'ssl') {
       // Already SSL
       return;
     }
@@ -530,16 +646,30 @@ TCPSocket.prototype = {
 
     if (this._multiplexStream.count == 0) {
       this._activateTLS();
     } else {
       this._waitingForStartTLS = true;
     }
   },
 
+  listen: function ts_listen(localPort, options, backlog) {
+    // 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 TCPServerSocket(this.useWin);
+
+    options = options || { binaryType : this.binaryType };
+    backlog = backlog || -1;
+    that.listen(localPort, options, backlog);
+    return that;
+  },
+
   close: function ts_close() {
     if (this._readyState === kCLOSED || this._readyState === kCLOSING)
       return;
 
     LOG("close called");
     this._readyState = kCLOSING;
 
     if (this._inChild) {
@@ -555,25 +685,24 @@ TCPSocket.prototype = {
 
   send: function ts_send(data, byteOffset, byteLength) {
     if (this._readyState !== kOPEN) {
       throw new Error("Socket not open.");
     }
 
     if (this._binaryType === "arraybuffer") {
       byteLength = byteLength || data.byteLength;
-    } else {
-      byteLength = data.length;
     }
 
     if (this._inChild) {
       this._socketBridge.sendSend(data, byteOffset, byteLength, ++this._trackingNumber);
     }
 
-    let newBufferedAmount = this.bufferedAmount + byteLength;
+    let length = this._binaryType === "arraybuffer" ? byteLength : data.length;
+    let newBufferedAmount = this.bufferedAmount + length;
     let bufferFull = newBufferedAmount >= BUFFER_SIZE;
 
     if (bufferFull) {
       // 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 if they so desire. Once all the
       // buffered data has been written to the socket, ondrain is
       // called.
@@ -588,17 +717,17 @@ TCPSocket.prototype = {
     }
 
     let new_stream;
     if (this._binaryType === "arraybuffer") {
       new_stream = new ArrayBufferInputStream();
       new_stream.setData(data, byteOffset, byteLength);
     } else {
       new_stream = new StringInputStream();
-      new_stream.setData(data, byteLength);
+      new_stream.setData(data, length);
     }
 
     if (this._waitingForStartTLS) {
       // When we are waiting for starttls, new_stream is added to pendingData
       // and will be appended to multiplexStream after tls had been set up.
       this._pendingDataAfterStartTLS.push(new_stream);
     } else {
       this._multiplexStream.appendStream(new_stream);
@@ -831,38 +960,44 @@ TCPSocket.prototype = {
 
     // We call this even if there is no error.
     this._maybeReportErrorAndCloseIfOpen(status);
   },
 
   // nsIStreamListener (Triggered by _inputStreamPump.asyncRead)
   onDataAvailable: function ts_onDataAvailable(request, context, inputStream, offset, count) {
     if (this._binaryType === "arraybuffer") {
-      let buffer = new (this.useWin.ArrayBuffer)(count);
+      let buffer = new (this.useWin ? this.useWin.ArrayBuffer : ArrayBuffer)(count);
       this._inputStreamBinary.readArrayBuffer(count, buffer);
       this.callListener("data", buffer);
     } else {
       this.callListener("data", this._inputStreamScriptable.read(count));
     }
 
 #ifdef MOZ_WIDGET_GONK
     // Collect received amount for network statistics.
     this._rxBytes += count;
     this._saveNetworkStats(false);
 #endif
   },
 
-  getInternalSocket: function() {
-    return this.QueryInterface(Ci.nsITCPSocketInternal);
-  },
-
   classID: Components.ID("{cda91b22-6472-11e1-aa11-834fec09cd0a}"),
 
-  contractID: "@mozilla.org/tcp-socket;1",
+  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,
     Ci.nsITCPSocketInternal,
     Ci.nsIDOMGlobalPropertyInitializer,
     Ci.nsIObserver,
     Ci.nsISupportsWeakReference
   ])
-};
+}
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPSocket]);
--- a/dom/network/TCPSocket.manifest
+++ b/dom/network/TCPSocket.manifest
@@ -1,11 +1,12 @@
 # TCPSocket.js
 component {cda91b22-6472-11e1-aa11-834fec09cd0a} TCPSocket.js
 contract @mozilla.org/tcp-socket;1 {cda91b22-6472-11e1-aa11-834fec09cd0a}
+category JavaScript-navigator-property mozTCPSocket @mozilla.org/tcp-socket;1
 
 # TCPSocketParentIntermediary.js
 component {afa42841-a6cb-4a91-912f-93099f6a3d18} TCPSocketParentIntermediary.js
 contract @mozilla.org/tcp-socket-intermediary;1 {afa42841-a6cb-4a91-912f-93099f6a3d18}
 
 # TCPServerSocket.js
 component {73065eae-27dc-11e2-895a-000c29987aa2} TCPServerSocket.js
 contract @mozilla.org/tcp-server-socket;1 {73065eae-27dc-11e2-895a-000c29987aa2}
--- a/dom/network/TCPSocketChild.cpp
+++ b/dom/network/TCPSocketChild.cpp
@@ -4,17 +4,17 @@
 
 #include <algorithm>
 #include "TCPSocketChild.h"
 #include "mozilla/unused.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/net/NeckoChild.h"
 #include "mozilla/dom/PBrowserChild.h"
 #include "mozilla/dom/TabChild.h"
-#include "nsITCPSocketInternal.h"
+#include "nsIDOMTCPSocket.h"
 #include "nsJSUtils.h"
 #include "nsContentUtils.h"
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "jswrapper.h"
 
 using mozilla::net::gNeckoChild;
 
--- a/dom/network/TCPSocketParent.cpp
+++ b/dom/network/TCPSocketParent.cpp
@@ -1,17 +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/. */
 
 #include "TCPSocketParent.h"
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "nsJSUtils.h"
-#include "nsITCPSocketInternal.h"
+#include "nsIDOMTCPSocket.h"
 #include "mozilla/unused.h"
 #include "mozilla/AppProcessChecker.h"
 #include "mozilla/net/NeckoCommon.h"
 #include "mozilla/net/PNeckoParent.h"
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/dom/TabParent.h"
 #include "mozilla/HoldDropJSObjects.h"
@@ -345,21 +345,22 @@ TCPSocketParent::SendEvent(const nsAStri
   }
   mozilla::unused <<
       PTCPSocketParent::SendCallback(nsString(aType), data,
                                      nsString(aReadyState));
   return NS_OK;
 }
 
 NS_IMETHODIMP
-TCPSocketParent::SetSocketAndIntermediary(nsITCPSocketInternal *aSocket,
-                                          nsITCPSocketIntermediary *aIntermediary)
+TCPSocketParent::SetSocketAndIntermediary(nsIDOMTCPSocket *socket,
+                                          nsITCPSocketIntermediary *intermediary,
+                                          JSContext* cx)
 {
-  mSocket = aSocket;
-  mIntermediary = aIntermediary;
+  mSocket = socket;
+  mIntermediary = intermediary;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 TCPSocketParent::SendUpdateBufferedAmount(uint32_t aBufferedAmount,
                                           uint32_t aTrackingNumber)
 {
   mozilla::unused << PTCPSocketParent::SendUpdateBufferedAmount(aBufferedAmount,
--- a/dom/network/TCPSocketParent.h
+++ b/dom/network/TCPSocketParent.h
@@ -4,24 +4,23 @@
 
 #ifndef mozilla_dom_TCPSocketParent_h
 #define mozilla_dom_TCPSocketParent_h
 
 #include "mozilla/net/PTCPSocketParent.h"
 #include "nsITCPSocketParent.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsCOMPtr.h"
+#include "nsIDOMTCPSocket.h"
 #include "js/TypeDecls.h"
 #include "mozilla/net/OfflineObserver.h"
 
 #define TCPSOCKETPARENT_CID \
   { 0x4e7246c6, 0xa8b3, 0x426d, { 0x9c, 0x17, 0x76, 0xda, 0xb1, 0xe1, 0xe1, 0x4a } }
 
-class nsITCPSocketInternal;
-
 namespace mozilla {
 namespace dom {
 
 class PBrowserParent;
 
 class TCPSocketParentBase : public nsITCPSocketParent
                           , public mozilla::net::DisconnectableParent
 {
@@ -33,17 +32,17 @@ public:
   void ReleaseIPDLReference();
 
 protected:
   TCPSocketParentBase();
   virtual ~TCPSocketParentBase();
 
   JS::Heap<JSObject*> mIntermediaryObj;
   nsCOMPtr<nsITCPSocketIntermediary> mIntermediary;
-  nsCOMPtr<nsITCPSocketInternal> mSocket;
+  nsCOMPtr<nsIDOMTCPSocket> mSocket;
   nsRefPtr<mozilla::net::OfflineObserver> mObserver;
   bool mIPCOpen;
 };
 
 class TCPSocketParent : public mozilla::net::PTCPSocketParent
                       , public TCPSocketParentBase
 {
 public:
--- a/dom/network/TCPSocketParentIntermediary.js
+++ b/dom/network/TCPSocketParentIntermediary.js
@@ -4,27 +4,24 @@
 
 "use strict";
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/Services.jsm");
-
-let global = this;
 
 function TCPSocketParentIntermediary() {
 }
 
 TCPSocketParentIntermediary.prototype = {
   _setCallbacks: function(aParentSide, socket) {
     aParentSide.initJS(this);
-    this._socket = socket.getInternalSocket();
+    this._socket = socket;
 
     // Create handlers for every possible callback that attempt to trigger
     // corresponding callbacks on the child object.
     // ondrain event is not forwarded, since the decision of firing ondrain
     // is made in child.
     ["open", "data", "error", "close"].forEach(
       function(p) {
         socket["on" + p] = function(data) {
@@ -36,70 +33,73 @@ TCPSocketParentIntermediary.prototype = 
   },
 
   _onUpdateBufferedAmountHandler: function(aParentSide, aBufferedAmount, aTrackingNumber) {
     aParentSide.sendUpdateBufferedAmount(aBufferedAmount, aTrackingNumber);
   },
 
   open: function(aParentSide, aHost, aPort, aUseSSL, aBinaryType,
                  aAppId, aInBrowser) {
-    let socket = new global.mozTCPSocket(aHost, aPort, {useSecureTransport: aUseSSL, binaryType: aBinaryType});
+    let baseSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
+    let socket = baseSocket.open(aHost, aPort, {useSecureTransport: aUseSSL, binaryType: aBinaryType});
+    if (!socket)
+      return null;
 
-    let socketInternal = socket.getInternalSocket();
-    socketInternal.initWithGlobal(global);
+    let socketInternal = socket.QueryInterface(Ci.nsITCPSocketInternal);
     socketInternal.setAppId(aAppId);
     socketInternal.setInBrowser(aInBrowser);
 
     // Handle parent's request to update buffered amount.
     socketInternal.setOnUpdateBufferedAmountHandler(
       this._onUpdateBufferedAmountHandler.bind(this, aParentSide));
 
     // Handlers are set to the JS-implemented socket object on the parent side.
     this._setCallbacks(aParentSide, socket);
-    return socketInternal;
+    return socket;
   },
 
   listen: function(aTCPServerSocketParent, aLocalPort, aBacklog, aBinaryType,
                    aAppId, aInBrowser) {
-    let serverSocket = new global.mozTCPServerSocket(aLocalPort, { binaryType: aBinaryType }, aBacklog);
-    let serverSocketInternal = serverSocket.getInternalSocket();
-    serverSocketInternal.initWithGlobal(global);
+    let baseSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
+    let serverSocket = baseSocket.listen(aLocalPort, { binaryType: aBinaryType }, aBacklog);
+    if (!serverSocket)
+      return null;
 
     let localPort = serverSocket.localPort;
 
-    serverSocket["onconnect"] = function(event) {
+    serverSocket["onconnect"] = function(socket) {
       var socketParent = Cc["@mozilla.org/tcp-socket-parent;1"]
                             .createInstance(Ci.nsITCPSocketParent);
       var intermediary = new TCPSocketParentIntermediary();
 
-      let socketInternal = event.socket.getInternalSocket();
+      let socketInternal = socket.QueryInterface(Ci.nsITCPSocketInternal);
       socketInternal.setAppId(aAppId);
       socketInternal.setInBrowser(aInBrowser);
       socketInternal.setOnUpdateBufferedAmountHandler(
         intermediary._onUpdateBufferedAmountHandler.bind(intermediary, socketParent));
 
       // Handlers are set to the JS-implemented socket object on the parent side,
       // so that the socket parent object can communicate data
       // with the corresponding socket child object through IPC.
-      intermediary._setCallbacks(socketParent, event.socket);
+      intermediary._setCallbacks(socketParent, socket);
       // The members in the socket parent object are set with arguments,
       // so that the socket parent object can communicate data
       // with the JS socket object on the parent side via the intermediary object.
-      socketParent.setSocketAndIntermediary(socketInternal, intermediary);
+      socketParent.setSocketAndIntermediary(socket, intermediary);
       aTCPServerSocketParent.sendCallbackAccept(socketParent);
     };
 
     serverSocket["onerror"] = function(data) {
         var error = data.data;
 
         aTCPServerSocketParent.sendCallbackError(error.message, error.filename,
                                                  error.lineNumber, error.columnNumber);
     };
 
-    return serverSocketInternal;
+    return serverSocket;
   },
 
   onRecvSendString: function(aData, aTrackingNumber) {
     let socketInternal = this._socket.QueryInterface(Ci.nsITCPSocketInternal);
     return socketInternal.onRecvSendFromChild(aData, 0, 0, aTrackingNumber);
   },
 
   onRecvSendArrayBuffer: function(aData, aTrackingNumber) {
deleted file mode 100644
--- a/dom/network/TCPSocketUtils.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-/* 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/. */
-
-#include "TCPSocketUtils.h"
-#include "mozilla/dom/BindingUtils.h"
-#include "mozilla/Preferences.h"
-#include "nsGlobalWindow.h"
-
-namespace TCPSocketUtils {
-
-using mozilla::Preferences;
-using mozilla::dom::CheckPermissions;
-
-bool
-SocketEnabled(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
-{
-  if (!Preferences::GetBool("dom.mozTCPSocket.enabled")) {
-    return false;
-  }
-
-  nsPIDOMWindow* window = xpc::WindowGlobalOrNull(aGlobal);
-  if (!window) {
-    return true;
-  }
-
-  const char* permissions[] = {"tcp-socket", nullptr};
-  return CheckPermissions(aCx, aGlobal, permissions);
-}
-
-}
deleted file mode 100644
--- a/dom/network/TCPSocketUtils.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/* 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/. */
-
-#ifndef TCPSocketUtils_h
-#define TCPSocketUtils_h
-
-#include "js/RootingAPI.h"
-
-namespace TCPSocketUtils {
-
-extern bool
-SocketEnabled(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
-
-}
-
-#endif // TCPSocketUtils_h
--- a/dom/network/interfaces/moz.build
+++ b/dom/network/interfaces/moz.build
@@ -1,21 +1,21 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
 XPIDL_SOURCES += [
+    'nsIDOMTCPServerSocket.idl',
+    'nsIDOMTCPSocket.idl',
     'nsIMozNavigatorNetwork.idl',
     'nsITCPServerSocketChild.idl',
-    'nsITCPServerSocketInternal.idl',
     'nsITCPServerSocketParent.idl',
     'nsITCPSocketChild.idl',
-    'nsITCPSocketInternal.idl',
     'nsITCPSocketParent.idl',
     'nsIUDPSocketChild.idl',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
     XPIDL_SOURCES += [
         'nsIDOMNetworkStatsManager.idl',
         'nsINetworkStatsServiceProxy.idl',
rename from dom/network/interfaces/nsITCPServerSocketInternal.idl
rename to dom/network/interfaces/nsIDOMTCPServerSocket.idl
--- a/dom/network/interfaces/nsITCPServerSocketInternal.idl
+++ b/dom/network/interfaces/nsIDOMTCPServerSocket.idl
@@ -1,32 +1,68 @@
 /* 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/. */
 
 #include "domstubs.idl"
+#include "nsITCPSocketChild.idl"
 
-interface nsITCPSocketChild;
+// Bug 797561 - Expose a server tcp socket API to web applications
+/**
+ * nsIDOMTCPServerSocket
+ *
+ * An interface to a server socket that can accept incoming connections for gaia apps.
+ */
+[scriptable, uuid(821638a1-5327-416d-8031-668764f2ec04)]
+interface nsIDOMTCPServerSocket : nsISupports
+{
+  /**
+   * The port of this server socket object.
+   */
+  readonly attribute unsigned short localPort;
+
+  /**
+   * The onconnect event handler is called when a client connection is accepted.
+   * The data attribute of the event passed to the onconnect handler will be a TCPSocket
+   * instance, which is used for communication between client and server. 
+   */
+  attribute jsval onconnect;
+
+  /**
+   * The onerror handler will be called when the listen of a server socket is aborted.
+   * The data attribute of the event passed to the onerror handler will have a
+   * description of the kind of error.
+   */
+  attribute jsval onerror;
+
+  /**
+   * Close the server socket.
+   */
+  void close();
+};
 
 /**
  * Internal interfaces for use in cross-process server-socket implementation.
  * Needed to account for multiple possible types that can be provided to
  * the socket callbacks as arguments.
  *
  * These interfaces are for calling each method from the server socket object
  * on the parent and child side for an IPC protocol implementation.
  */
 
-[scriptable, uuid(03520F9E-7989-4604-870A-A67DFFD846DC)]
-interface nsITCPServerSocketInternal : nsISupports
+[scriptable, uuid(b64b1e68-4efa-497c-b0d8-69f067ad5ec8)]
+interface nsITCPServerSocketInternal : nsISupports 
 {
   /**
-   * Close the server socket.
+   * Initialization after creating a TCP server socket object.
+   *
+   * @param windowVal
+   *        An object to create ArrayBuffer for this window. See Bug 831107.
    */
-  void close();
+  void init(in jsval windowVal);
 
   /** 
    * Listen on a port
    *
    * @param localPort 
    *        The port of the server socket. Pass -1 to indicate no preference,
    *        and a port will be selected automatically.
    * @param options 
@@ -48,14 +84,9 @@ interface nsITCPServerSocketInternal : n
    */
   void callListenerAccept(in nsITCPSocketChild socketChild);
 
   /**
    * Listener for handling an error caused in chrome process.
    */
   void callListenerError(in DOMString message, in DOMString filename,
                          in uint32_t lineNumber, in uint32_t columnNumber);
-
-  /**
-   * Called by the parent process when there is no DOM window available.
-   */
-  void initWithGlobal(in nsISupports global);
 };
new file mode 100644
--- /dev/null
+++ b/dom/network/interfaces/nsIDOMTCPSocket.idl
@@ -0,0 +1,333 @@
+/* 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 and server sockets
+ * 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"
+#include "nsITCPSocketChild.idl"
+#include "nsIDOMTCPServerSocket.idl"
+
+interface nsISocketTransport;
+
+// 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);
+
+// Bug 797561 - Expose a server tcp socket API to web applications
+
+
+[scriptable, uuid(65f6d2c8-4be6-4695-958d-0735e8935289)]
+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.
+   *
+   *        useSecureTransport: true to create an SSL socket. Defaults to false.
+   *
+   *        binaryType: "arraybuffer" to use ArrayBuffer
+   *          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);
+
+  /**
+   * Listen on a port
+   *
+   * @param localPort The port of the server socket. Pass -1 to indicate no preference,
+   *                  and a port will be selected automatically.
+   * @param options An object specifying one or more parameters which
+   *                determine the details of the socket.
+   *
+   *        binaryType: "arraybuffer" to use ArrayBuffer
+   *          instances in the ondata callback and as the argument
+   *          to send. Defaults to "string", to use JavaScript strings.
+   * @param backlog The maximum length the queue of pending connections may grow to.
+   *                This parameter may be silently limited by the operating system.
+   *                Pass -1 to use the default value.
+   *
+   * @return The new TCPServerSocket instance.
+   */
+  nsIDOMTCPServerSocket listen(in unsigned short localPort, [optional] in jsval options,
+                               [optional] in unsigned short backlog);
+
+  /**
+   * Enable secure on channel.
+   */
+  void upgradeToSecure();
+
+  /**
+   * 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 ArrayBuffer instance.
+   *             If binaryType: "string" was passed, or if no binaryType
+   *             option was specified, then this object should be an
+   *             ordinary JavaScript string.
+   * @param byteOffset The offset within the data from which to begin writing.
+   *                   Has no effect on non-ArrayBuffer data.
+   * @param byteLength The number of bytes to write. Has no effect on
+   *                   non-ArrayBuffer data.
+   *
+   * @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, [optional] in unsigned long byteOffset, [optional] in unsigned long byteLength);
+
+  /**
+   * 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;
+
+  /**
+   * 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 ArrayBuffer. 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;
+};
+
+/*
+ * This interface is implemented in TCPSocket.js as an internal interfaces
+ * for use in cross-process socket implementation.
+ * Needed to account for multiple possible types that can be provided to
+ * the socket callbacks as arguments.
+ */
+[scriptable, uuid(ac2c4b69-cb79-4767-b1ce-bcf62945cd39)]
+interface nsITCPSocketInternal : nsISupports {
+  // Trigger the callback for |type| and provide a DOMError() object with the given data
+  void callListenerError(in DOMString type, in DOMString name);
+
+  // Trigger the callback for |type| and provide a string argument
+  void callListenerData(in DOMString type, in DOMString data);
+
+  // Trigger the callback for |type| and provide an ArrayBuffer argument
+  void callListenerArrayBuffer(in DOMString type, in jsval data);
+
+  // Trigger the callback for |type| with no argument
+  void callListenerVoid(in DOMString type);
+
+  // Update the DOM object's readyState.
+  // @param readyState
+  //        new ready state to be set to TCPSocket.
+  void updateReadyState(in DOMString readyState);
+
+  // Update the DOM object's bufferedAmount value with a tracking number to
+  // ensure the update request is sent after child's send() invocation.
+  // @param bufferedAmount
+  //        TCPSocket parent's bufferedAmount.
+  // @param trackingNumber
+  //        A number to ensure the bufferedAmount is updated after data
+  //        from child are sent to parent.
+  void updateBufferedAmount(in uint32_t bufferedAmount,
+                            in uint32_t trackingNumber);
+
+  // Create a socket object on the parent side.
+  // This is called in accepting any open request on the parent side.
+  // 
+  // @param transport
+  //        The accepted socket transport.
+  // @param binaryType
+  //        "arraybuffer" to use ArrayBuffer instances 
+  //        in the ondata callback and as the argument to send.
+  // @param window
+  //        An object to create ArrayBuffer for this window. See Bug 831107.
+  nsIDOMTCPSocket createAcceptedParent(in nsISocketTransport transport,
+                                       in DOMString binaryType,
+                                       in nsIDOMWindow window);
+
+  // Create a DOM socket on the child side
+  // This is called when the socket is accepted on the parent side.
+  //
+  // @param socketChild
+  //        The socket child object for the IPC implementation.
+  // @param binaryType
+  //        "arraybuffer" to use ArrayBuffer instances
+  //        in the ondata callback and as the argument to send.
+  // @param window
+  //        An object to create ArrayBuffer for this window. See Bug 831107.
+  nsIDOMTCPSocket createAcceptedChild(in nsITCPSocketChild socketChild,
+                                      in DOMString binaryType,
+                                      in nsIDOMWindow window);
+
+  // Set App ID.
+  void setAppId(in unsigned long appId);
+
+  // Set inBrowser.
+  void setInBrowser(in boolean inBrowser);
+
+  // Set a callback that handles the request from a TCP socket parent when that
+  // socket parent wants to notify that its bufferedAmount is updated.
+  void setOnUpdateBufferedAmountHandler(in jsval handler);
+
+  // Providing child process with ability to pass more arguments to parent's
+  // send() function.
+  // @param trackingNumber
+  //        To ensure the request to update bufferedAmount in child is after
+  //        lastest send() invocation from child.
+  void onRecvSendFromChild(in jsval data, in unsigned long byteOffset,
+                           in unsigned long byteLength, in unsigned long trackingNumber);
+};
+
+/**
+ * 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 target;
+
+  /**
+   * The type of this event. One of:
+   *
+   * open
+   * error
+   * data
+   * drain
+   * close
+   */
+  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 ArrayBuffer;
+   * 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;
+};
+
--- a/dom/network/interfaces/nsITCPServerSocketChild.idl
+++ b/dom/network/interfaces/nsITCPServerSocketChild.idl
@@ -1,13 +1,14 @@
 /* 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/. */
 
 #include "domstubs.idl"
+#include "nsIDOMTCPServerSocket.idl"
 
 interface nsITCPServerSocketInternal;
 
 /**
  * Interface to allow the content process server socket to reach the IPC bridge.
  * It is used in the server socket implementation on the child side.
  */
 
--- a/dom/network/interfaces/nsITCPServerSocketParent.idl
+++ b/dom/network/interfaces/nsITCPServerSocketParent.idl
@@ -1,17 +1,18 @@
 /* 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/. */
 
 #include "domstubs.idl"
+#include "nsITCPSocketParent.idl"
 
-interface nsITCPSocketParent;
+interface nsIDOMTCPServerSocket;
 
-/**
+/** 
  * Interface required to allow the TCP server-socket object in the parent process
  * to talk to the parent IPC actor.
  * It is used in the server socket implementation on the parent side.
  */
 [scriptable, uuid(161ffc9f-54d3-4f21-a536-4166003d0e1d)]
 interface nsITCPServerSocketParent : nsISupports
 {
   /**
deleted file mode 100644
--- a/dom/network/interfaces/nsITCPSocketInternal.idl
+++ /dev/null
@@ -1,133 +0,0 @@
-/* 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 and server sockets
- * 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"
-
-interface nsISocketTransport;
-interface nsITCPSocketChild;
-
-/*
- * This interface is implemented in TCPSocket.js as an internal interfaces
- * for use in cross-process socket implementation.
- * Needed to account for multiple possible types that can be provided to
- * the socket callbacks as arguments.
- */
-[scriptable, uuid(E79C5F3C-0B54-44B9-B0D7-6930EF9126DC)]
-interface nsITCPSocketInternal : nsISupports {
-  /**
-   * Enable secure on channel.
-   */
-  void upgradeToSecure();
-
-  /**
-   * The host of this socket object.
-   */
-  readonly attribute DOMString host;
-
-  /**
-   * The port of this socket object.
-   */
-  readonly attribute unsigned short port;
-
-  /**
-   * 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();
-
-  // Trigger the callback for |type| and provide a DOMError() object with the given data
-  void callListenerError(in DOMString type, in DOMString name);
-
-  // Trigger the callback for |type| and provide a string argument
-  void callListenerData(in DOMString type, in DOMString data);
-
-  // Trigger the callback for |type| and provide an ArrayBuffer argument
-  void callListenerArrayBuffer(in DOMString type, in jsval data);
-
-  // Trigger the callback for |type| with no argument
-  void callListenerVoid(in DOMString type);
-
-  // Update the DOM object's readyState.
-  // @param readyState
-  //        new ready state to be set to TCPSocket.
-  void updateReadyState(in DOMString readyState);
-
-  // Update the DOM object's bufferedAmount value with a tracking number to
-  // ensure the update request is sent after child's send() invocation.
-  // @param bufferedAmount
-  //        TCPSocket parent's bufferedAmount.
-  // @param trackingNumber
-  //        A number to ensure the bufferedAmount is updated after data
-  //        from child are sent to parent.
-  void updateBufferedAmount(in uint32_t bufferedAmount,
-                            in uint32_t trackingNumber);
-
-  // Initialize a socket object on the parent side.
-  // This is called in accepting any open request on the parent side.
-  //
-  // @param transport
-  //        The accepted socket transport.
-  // @param binaryType
-  //        "arraybuffer" to use ArrayBuffer instances
-  //        in the ondata callback and as the argument to send.
-  // @param global
-  //        An object to create ArrayBuffer for this global. See Bug 831107.
-  void initAcceptedParent(in nsISocketTransport transport,
-                          in DOMString binaryType,
-                          in nsISupports global);
-
-  // Initialize a DOM socket on the child side
-  // This is called when the socket is accepted on the parent side.
-  //
-  // @param socketChild
-  //        The socket child object for the IPC implementation.
-  // @param binaryType
-  //        "arraybuffer" to use ArrayBuffer instances
-  //        in the ondata callback and as the argument to send.
-  // @param global
-  //        An object to create ArrayBuffer for this global. See Bug 831107.
-  void initAcceptedChild(in nsITCPSocketChild socketChild,
-                         in DOMString binaryType,
-                         in nsISupports global);
-
-  // Set App ID.
-  void setAppId(in unsigned long appId);
-
-  // Set inBrowser.
-  void setInBrowser(in boolean inBrowser);
-
-  // Set a callback that handles the request from a TCP socket parent when that
-  // socket parent wants to notify that its bufferedAmount is updated.
-  void setOnUpdateBufferedAmountHandler(in jsval handler);
-
-  // Providing child process with ability to pass more arguments to parent's
-  // send() function.
-  // @param trackingNumber
-  //        To ensure the request to update bufferedAmount in child is after
-  //        lastest send() invocation from child.
-  void onRecvSendFromChild(in jsval data, in unsigned long byteOffset,
-                           in unsigned long byteLength, in unsigned long trackingNumber);
-
-  /**
-   * Called by the parent process when there is no DOM window available.
-   */
-  void initWithGlobal(in nsISupports global);
-};
--- a/dom/network/interfaces/nsITCPSocketParent.idl
+++ b/dom/network/interfaces/nsITCPSocketParent.idl
@@ -1,23 +1,23 @@
 /* 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/. */
 
 #include "domstubs.idl"
 
-interface nsITCPSocketInternal;
-interface nsITCPServerSocketInternal;
+interface nsIDOMTCPSocket;
+interface nsIDOMTCPServerSocket;
 interface nsITCPServerSocketParent;
 interface nsITCPSocketIntermediary;
 
 // Interface required to allow the TCP socket object (TCPSocket.js) in the
 // parent process to talk to the parent IPC actor, TCPSocketParent, which
 // is written in C++.
-[scriptable, uuid(03B864C8-AAA9-4062-943E-AE6397070E0D)]
+[scriptable, uuid(6f040bf0-6852-11e3-949a-0800200c9a66)]
 interface nsITCPSocketParent : nsISupports
 {
   [implicit_jscontext] void initJS(in jsval intermediary);
 
   // Trigger a callback in the content process for |type|, providing a serialized
   // argument of |data|, and update the child's readyState value with the given
   // values.
   //
@@ -35,18 +35,18 @@ interface nsITCPSocketParent : nsISuppor
   // Initialize a parent socket object. It is called from the parent side socket,
   // which is generated in accepting any open request on the parent side.
   // The socket after being initialized will be established.
   //
   // @param socket
   //        The socket on the parent side.
   // @param intermediary
   //        Intermediate class object. See nsITCPSocketIntermediary.
-  void setSocketAndIntermediary(in nsITCPSocketInternal socket,
-                                in nsITCPSocketIntermediary intermediary);
+  [implicit_jscontext] void setSocketAndIntermediary(in nsIDOMTCPSocket socket,
+                                                     in nsITCPSocketIntermediary intermediary);
 
   // When parent's buffered amount is updated and it wants to inform child to
   // update the bufferedAmount as well.
   //
   // @param bufferedAmount
   //        The new value of bufferedAmount that is going to be set to child's
   //        bufferedAmount.
   // @param trackingNumber
@@ -63,27 +63,27 @@ interface nsITCPSocketParent : nsISuppor
 // Intermediate class to handle sending multiple possible data types
 // and kicking off the chrome process socket object's connection.
 // This interface is the bridge of TCPSocketParent, which is written in C++,
 // and TCPSocket, which is written in Javascript. TCPSocketParentIntermediary
 // implements nsITCPSocketIntermediary in Javascript.
 [scriptable, uuid(aa9bd46d-26bf-4ba8-9c18-ba02482c02f0)]
 interface nsITCPSocketIntermediary : nsISupports {
   // Open the connection to the server with the given parameters
-  nsITCPSocketInternal open(in nsITCPSocketParent parent,
-                            in DOMString host, in unsigned short port,
-                            in boolean useSSL, in DOMString binaryType,
-                            in unsigned long appId,
-                            in boolean inBrowser);
+  nsIDOMTCPSocket open(in nsITCPSocketParent parent,
+                       in DOMString host, in unsigned short port,
+                       in boolean useSSL, in DOMString binaryType,
+                       in unsigned long appId,
+                       in boolean inBrowser);
 
   // Listen on a port
-  nsITCPServerSocketInternal listen(in nsITCPServerSocketParent parent,
-                                    in unsigned short port, in unsigned short backlog,
-                                    in DOMString binaryType,
-                                    in unsigned long appId,
-                                    in boolean inBrowser);
+  nsIDOMTCPServerSocket listen(in nsITCPServerSocketParent parent,
+                               in unsigned short port, in unsigned short backlog,
+                               in DOMString binaryType,
+                               in unsigned long appId,
+                               in boolean inBrowser);
 
   // Called when received a child request to send a string.
   void onRecvSendString(in DOMString data, in uint32_t trackingNumber);
 
   // Called when received a child request to send an array buffer.
   void onRecvSendArrayBuffer(in jsval data, in uint32_t trackingNumber);
 };
--- a/dom/network/moz.build
+++ b/dom/network/moz.build
@@ -11,20 +11,16 @@ XPCSHELL_TESTS_MANIFESTS += [
     'tests/unit_ipc/xpcshell.ini',
 ]
 
 if CONFIG['MOZ_B2G_RIL']:
     XPCSHELL_TESTS_MANIFESTS += ['tests/unit_stats/xpcshell.ini']
 
 MOCHITEST_MANIFESTS += ['tests/mochitest.ini']
 
-EXPORTS += [
-    'TCPSocketUtils.h',
-]
-
 EXPORTS.mozilla.dom += [
     'UDPSocket.h',
 ]
 
 EXPORTS.mozilla.dom.network += [
     'Connection.h',
     'Constants.h',
     'TCPServerSocketChild.h',
@@ -37,17 +33,16 @@ EXPORTS.mozilla.dom.network += [
 ]
 
 UNIFIED_SOURCES += [
     'Connection.cpp',
     'TCPServerSocketChild.cpp',
     'TCPServerSocketParent.cpp',
     'TCPSocketChild.cpp',
     'TCPSocketParent.cpp',
-    'TCPSocketUtils.cpp',
     'UDPSocket.cpp',
     'UDPSocketChild.cpp',
     'UDPSocketParent.cpp',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
     EXTRA_JS_MODULES += [
         'NetworkStatsDB.jsm',
@@ -81,13 +76,11 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk
 IPDL_SOURCES += [
     'PTCPServerSocket.ipdl',
     'PTCPSocket.ipdl',
     'PUDPSocket.ipdl',
 ]
 
 FAIL_ON_WARNINGS = True
 
-LOCAL_INCLUDES += ['/dom/base']
-
 include('/ipc/chromium/chromium-config.mozbuild')
 
 FINAL_LIBRARY = 'xul'
--- a/dom/network/tests/mochitest.ini
+++ b/dom/network/tests/mochitest.ini
@@ -4,16 +4,18 @@ support-files =
   file_udpsocket_iframe.html
   test_tcpsocket_client_and_server_basics.js
 
 [test_network_basics.html]
 skip-if = toolkit == "gonk" || toolkit == 'android'
 [test_tcpsocket_client_and_server_basics.html]
 [test_tcpsocket_default_permissions.html]
 skip-if = toolkit == "gonk"
+[test_tcpsocket_enabled_no_perm.html]
+skip-if = toolkit == "gonk"
 [test_tcpsocket_enabled_with_perm.html]
 skip-if = toolkit == "gonk" || e10s
 [test_networkstats_alarms.html]
 skip-if = true # Bug 958689
 [test_networkstats_basics.html]
 skip-if = true # Bug 958689, bug 858005
 [test_networkstats_disabled.html]
 skip-if = toolkit != "gonk"
--- a/dom/network/tests/test_tcpsocket_client_and_server_basics.js
+++ b/dom/network/tests/test_tcpsocket_client_and_server_basics.js
@@ -122,32 +122,28 @@ function listenForEventsOnSocket(socket,
  * calling listenForEventsOnSocket(socket).  This must be done because we need
  * to add the event listener during the connection.
  */
 function waitForConnection(listeningServer) {
   return new Promise(function(resolve, reject) {
     // Because of the event model of sockets, we can't use the
     // listenForEventsOnSocket mechanism; we need to hook up listeners during
     // the connect event.
-    listeningServer.onconnect = function(ev) {
+    listeningServer.onconnect = function(socket) {
       // Clobber the listener to get upset if it receives any more connections
       // after this.
       listeningServer.onconnect = function() {
         ok(false, 'Received a connection when not expecting one.');
       };
       ok(true, 'Listening server accepted socket');
       resolve({
-        socket: ev.socket,
-        queue: listenForEventsOnSocket(ev.socket, 'server')
+        socket: socket,
+        queue: listenForEventsOnSocket(socket, 'server')
       });
     };
-    listeningServer.onerror = function(ev) {
-      ok(false, 'Received an error when not expecting one.');
-      reject();
-    };
   });
 }
 
 function defer() {
   var deferred = {};
   deferred.promise = new Promise(function(resolve, reject) {
     deferred.resolve = resolve;
     deferred.reject = reject;
@@ -170,26 +166,27 @@ function* test_basics() {
     permDeferred.resolve);
   yield permDeferred.promise;
 
   // See bug 903830; in e10s mode we never get to find out the localPort if we
   // let it pick a free port by choosing 0.  This is the same port the xpcshell
   // test was using.
   let serverPort = 8085;
 
+  let TCPSocket = navigator.mozTCPSocket;
   // - Start up a listening socket.
-  let listeningServer = new mozTCPServerSocket(serverPort,
-                                               { binaryType: 'arraybuffer' },
-                                               SERVER_BACKLOG);
+  let listeningServer = TCPSocket.listen(serverPort,
+                                         { binaryType: 'arraybuffer' },
+                                         SERVER_BACKLOG);
 
   let connectedPromise = waitForConnection(listeningServer);
 
   // -- Open a connection to the server
-  let clientSocket = new mozTCPSocket('127.0.0.1', serverPort,
-                                      { binaryType: 'arraybuffer' });
+  let clientSocket = TCPSocket.open('127.0.0.1', serverPort,
+                                    { binaryType: 'arraybuffer' });
   let clientQueue = listenForEventsOnSocket(clientSocket, 'client');
 
   // (the client connects)
   is((yield clientQueue.waitForEvent()).type, 'open', 'got open event');
   is(clientSocket.readyState, 'open', 'client readyState is open');
 
   // (the server connected)
   let { socket: serverSocket, queue: serverQueue } = yield connectedPromise;
@@ -285,18 +282,18 @@ function* test_basics() {
      'client readyState should be closed after close event');
   is((yield serverQueue.waitForEvent()).type, 'close',
      'The server should get a close event when it closes itself.');
   is(serverSocket.readyState, 'closed',
      'server readyState should be closed after close event');
 
   // -- Re-establish connection
   connectedPromise = waitForConnection(listeningServer);
-  clientSocket = new mozTCPSocket('127.0.0.1', serverPort,
-                                  { binaryType: 'arraybuffer' });
+  clientSocket = TCPSocket.open('127.0.0.1', serverPort,
+                                { binaryType: 'arraybuffer' });
   clientQueue = listenForEventsOnSocket(clientSocket, 'client');
   is((yield clientQueue.waitForEvent()).type, 'open', 'got open event');
 
   let connectedResult = yield connectedPromise;
   // destructuring assignment is not yet ES6 compliant, must manually unpack
   serverSocket = connectedResult.socket;
   serverQueue = connectedResult.queue;
 
@@ -312,18 +309,18 @@ function* test_basics() {
   is((yield serverQueue.waitForEvent()).type, 'close',
      'The server should get a close event when the client closes.');
   is(serverSocket.readyState, 'closed',
      'server readyState should be closed after the close event is received');
 
 
   // -- Re-establish connection
   connectedPromise = waitForConnection(listeningServer);
-  clientSocket = new mozTCPSocket('127.0.0.1', serverPort,
-                                  { binaryType: 'arraybuffer' });
+  clientSocket = TCPSocket.open('127.0.0.1', serverPort,
+                                { binaryType: 'arraybuffer' });
   clientQueue = listenForEventsOnSocket(clientSocket, 'client');
   is((yield clientQueue.waitForEvent()).type, 'open', 'got open event');
 
   connectedResult = yield connectedPromise;
   // destructuring assignment is not yet ES6 compliant, must manually unpack
   serverSocket = connectedResult.socket;
   serverQueue = connectedResult.queue;
 
@@ -341,26 +338,26 @@ function* test_basics() {
 
   // The server will get its data
   serverReceived = yield serverQueue.waitForDataWithAtLeastLength(
     bigUint8Array.length);
   assertUint8ArraysEqual(serverReceived, bigUint8Array,
                          'server received/client sent');
   // And a close.
   is((yield serverQueue.waitForEvent()).type, 'close',
-     'The close event should fire after a large send that returned true.');
+     'The drain event should fire after a large send that returned true.');
 
 
   // -- Close the listening server (and try to connect)
   // We want to verify that the server actually closes / stops listening when
   // we tell it to.
   listeningServer.close();
 
   // - try and connect, get an error
-  clientSocket = new mozTCPSocket('127.0.0.1', serverPort,
-                                  { binaryType: 'arraybuffer' });
+  clientSocket = TCPSocket.open('127.0.0.1', serverPort,
+                                { binaryType: 'arraybuffer' });
   clientQueue = listenForEventsOnSocket(clientSocket, 'client');
   is((yield clientQueue.waitForEvent()).type, 'error', 'fail to connect');
   is(clientSocket.readyState, 'closed',
      'client readyState should be closed after the failure to connect');
 }
 
 add_task(test_basics);
--- a/dom/network/tests/test_tcpsocket_default_permissions.html
+++ b/dom/network/tests/test_tcpsocket_default_permissions.html
@@ -9,14 +9,19 @@
 <p id="display"></p>
 <div id="content" style="display: none">
 </div>
 <pre id="test">
 <script type="application/javascript">
 
 /** Test to ensure TCPSocket permission is disabled by default **/
 
-ok(!("mozTCPSocket" in window), "mozTCPSocket should not exist by default");
+try {
+  navigator.mozTCPSocket;
+  throw new Error("Error: navigator.mozTCPSocket should not exist by default");
+} catch (e) {
+  ok(true, "navigator.mozTCPSocket should not exist by default");  
+}
 
 </script>
 </pre>
 </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/dom/network/tests/test_tcpsocket_enabled_no_perm.html
@@ -0,0 +1,35 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test to ensure TCPSocket permission enabled and no tcp-socket perm does not allow open</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test to ensure TCPSocket permission being turned on enables 
+  navigator.mozTCPSocket, but mozTCPSocket.open does not work
+  in content.
+**/
+SpecialPowers.setBoolPref("dom.mozTCPSocket.enabled", true);
+
+ok('mozTCPSocket' in navigator, "navigator.mozTCPSocket should be accessible if dom.mozTCPSocket.enabled is true");
+
+try {
+  navigator.mozTCPSocket.open('localhost', 80);
+  throw new Error("Error: navigator.mozTCPSocket.open should raise for content that does not have the tcp-socket permission");
+} catch (e) {
+  ok(true, "navigator.mozTCPSocket.open should raise for content that does not have the tcp-socket permission");
+}
+
+SpecialPowers.setBoolPref("dom.mozTCPSocket.enabled", false);
+
+</script>
+</pre>
+</body>
+</html>
--- a/dom/network/tests/test_tcpsocket_enabled_with_perm.html
+++ b/dom/network/tests/test_tcpsocket_enabled_with_perm.html
@@ -7,25 +7,25 @@
 </head>
 <body>
 <p id="display"></p>
 <div id="content" style="display: none">
 </div>
 <pre id="test">
 <script type="application/javascript">
 
-/** Test to ensure TCPSocket permission being turned on enables
-  mozTCPSocket, and mozTCPSocket constructor works when
+/** Test to ensure TCPSocket permission being turned on enables 
+  navigator.mozTCPSocket, and mozTCPSocket.open works when
   the tcp-socket permission has been granted.
 **/
 SpecialPowers.setBoolPref("dom.mozTCPSocket.enabled", true);
 SpecialPowers.addPermission("tcp-socket", true, document);
 
-ok('mozTCPSocket' in window, "window.mozTCPSocket should be accessible if dom.mozTCPSocket.enabled is true");
+ok('mozTCPSocket' in navigator, "navigator.mozTCPSocket should be accessible if dom.mozTCPSocket.enabled is true");
 
-ok(new mozTCPSocket('localhost', 80), "window.mozTCPSocket ctor should work for content that has the tcp-socket permission");
+ok(navigator.mozTCPSocket.open('localhost', 80), "navigator.mozTCPSocket.open should work for content that has the tcp-socket permission");
 
 SpecialPowers.setBoolPref("dom.mozTCPSocket.enabled", false);
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/network/tests/unit/test_tcpsocket.js
+++ b/dom/network/tests/unit/test_tcpsocket.js
@@ -56,17 +56,17 @@ const ServerSocket = CC("@mozilla.org/ne
                            "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",
-                     "nsITCPSocketInternal"))();
+                     "nsIDOMTCPSocket"))();
 
 const gInChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
                   .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
 
 Cu.import("resource://gre/modules/Services.jsm");
 
 /**
  *
@@ -264,20 +264,19 @@ var server = null, sock = null, failure_
  * 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 = new mozTCPSocket(
+  sock = TCPSocket.open(
     '127.0.0.1', server.listener.port,
     { binaryType: 'arraybuffer' });
-  sock.getInternalSocket().initWithGlobal(this);
 
   sock.onopen = yayFuncs.clientopen;
   sock.ondrain = null;
   sock.ondata = makeFailureCase('data');
   sock.onerror = makeFailureCase('error');
   sock.onclose = makeFailureCase('close');
 
   server.onconnect = yayFuncs.serveropen;
@@ -306,17 +305,17 @@ function childbuffered() {
   sock.ondrain = function() {
     yays.ondrain();
     sock.close();
   };
 
   server.ondata = makeExpectData(
     'ondata', DATA_ARRAY, false, yays.serverdata);
 
-  let internalSocket = sock.getInternalSocket();
+  let internalSocket = sock.QueryInterface(Ci.nsITCPSocketInternal);
   internalSocket.updateBufferedAmount(65535, // almost reach buffering threshold
                                       0);
   if (sock.send(DATA_ARRAY_BUFFER)) {
     do_throw("expected sock.send to return false.");
   }
 
   sock.onclose = yays.clientclose;
   server.onclose = yays.serverclose;
@@ -331,17 +330,17 @@ function childbuffered() {
 // 3. wait for 1 second, to make sure there's no ondrain event dispatched in
 //    child.
 function childnotbuffered() {
   let yays = makeJointSuccess(['serverdata', 'clientclose', 'serverclose']);
   server.ondata = makeExpectData('ondata', BIG_ARRAY, false, yays.serverdata);
   if (sock.send(BIG_ARRAY_BUFFER)) {
     do_throw("sock.send(BIG_TYPED_ARRAY) did not return false to indicate buffering");
   }
-  let internalSocket = sock.getInternalSocket();
+  let internalSocket = sock.QueryInterface(Ci.nsITCPSocketInternal);
   internalSocket.updateBufferedAmount(0, // setting zero will clear waitForDrain in sock.
                                       1);
 
   // shouldn't get ondrain, even after parent have cleared its buffer.
   sock.ondrain = makeFailureCase('drain');
   sock.onclose = yays.clientclose;
   server.onclose = yays.serverclose;
   do_timeout(1000, function() {
--- a/dom/network/tests/unit_ipc/test_tcpsocket_ipc.js
+++ b/dom/network/tests/unit_ipc/test_tcpsocket_ipc.js
@@ -1,9 +1,9 @@
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 function run_test() {
   Services.prefs.setBoolPref('dom.mozTCPSocket.enabled', true);
   run_test_in_child("../unit/test_tcpsocket.js", function() {
     Services.prefs.clearUserPref('dom.mozTCPSocket.enabled');
     do_test_finished();
   });
-}
+}
\ No newline at end of file
--- a/dom/permission/tests/test_tcp-socket.html
+++ b/dom/permission/tests/test_tcp-socket.html
@@ -16,28 +16,34 @@ https://bugzilla.mozilla.org/show_bug.cg
 <pre id="test">
 <script type="application/javascript;version=1.8" src="file_framework.js"></script>
 <script type="application/javascript;version=1.8">
 /* mozTCPSocket only returns null on window init
  * if the permission isn't set
  */
 function verifier(success, failure) {
   try {
-    var conn = new mozTCPSocket("http://mochi.test/", 80);
-    success("Opened connection");
+    var conn = this.getObj().open("http://mochi.test/", 80);
+
+    if (conn) {
+      success("Opened connection");
+    } else {
+      failure("failed to open connection");
+    }
   } catch (e) {
     failure("Got an exception " + e);
   }
 }
 
 var gData = [
   {
     perm: ["tcp-socket"],
     needParentPerm: true,
-    webidl: "mozTCPSocket",
+    obj: "mozTCPSocket",
+    idl: "nsIDOMTCPSocket",
     settings: [["dom.mozTCPSocket.enabled", true]],
     verifier: verifier.toSource(),
   }
 ]
 </script>
 </pre>
 </body>
 </html>
deleted file mode 100644
--- a/dom/webidl/TCPServerSocket.webidl
+++ /dev/null
@@ -1,53 +0,0 @@
-/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* 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/. */
-
-interface nsITCPServerSocketInternal;
-
-/**
- * mozTCPServerSocket
- *
- * An interface to a server socket that can accept incoming connections for gaia apps.
- */
-
-enum TCPServerSocketBinaryType {
-  "arraybuffer",
-  "string"
-};
-
-dictionary ServerSocketOptions {
-  TCPServerSocketBinaryType binaryType = "string";
-};
-
-[Constructor(unsigned short port, optional ServerSocketOptions options, optional unsigned short backlog),
- JSImplementation="@mozilla.org/tcp-server-socket;1", Func="TCPSocketUtils::SocketEnabled",
- Exposed=(Window,System)]
-interface mozTCPServerSocket : EventTarget {
-  /**
-   * The port of this server socket object.
-   */
-  readonly attribute unsigned short localPort;
-
-  /**
-   * The onconnect event handler is called when a client connection is accepted.
-   * The socket attribute of the event passed to the onconnect handler will be a TCPSocket
-   * instance, which is used for communication between client and server.
-   */
-  attribute EventHandler onconnect;
-
-  /**
-   * The onerror handler will be called when the listen of a server socket is aborted.
-   * The data attribute of the event passed to the onerror handler will have a
-   * description of the kind of error.
-   */
-  attribute EventHandler onerror;
-
-  /**
-   * Close the server socket.
-   */
-  void close();
-
-  [ChromeOnly]
-  nsITCPServerSocketInternal getInternalSocket();
-};
deleted file mode 100644
--- a/dom/webidl/TCPServerSocketEvent.webidl
+++ /dev/null
@@ -1,15 +0,0 @@
-/* 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/. */
-
-interface nsIDOMTCPSocket;
-
-[Constructor(DOMString type, optional TCPServerSocketEventInit eventInitDict),
- Func="TCPSocketUtils::SocketEnabled", Exposed=(Window,System)]
-interface TCPServerSocketEvent : Event {
-  readonly attribute mozTCPSocket socket;
-};
-
-dictionary TCPServerSocketEventInit : EventInit {
-  mozTCPSocket? socket = null;
-};
deleted file mode 100644
--- a/dom/webidl/TCPSocket.webidl
+++ /dev/null
@@ -1,162 +0,0 @@
-/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* 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/. */
-
-interface nsITCPSocketInternal;
-
-/**
- * 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.
- */
-
-enum TCPSocketBinaryType {
-  "arraybuffer",
-  "string"
-};
-
-dictionary SocketOptions {
-  boolean useSSL = false;
-  TCPSocketBinaryType binaryType = "string";
-  boolean doNotConnect = false;
-};
-
-[Constructor(DOMString host, unsigned short port, optional SocketOptions options),
- Func="TCPSocketUtils::SocketEnabled",
- JSImplementation="@mozilla.org/tcp-socket;1", Exposed=(Window,System)]
-interface mozTCPSocket : EventTarget {
-  /**
-   * The host of this socket object.
-   */
-  readonly attribute USVString 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 ArrayBuffer instance.
-   *             If binaryType: "string" was passed, or if no binaryType
-   *             option was specified, then this object should be an
-   *             ordinary JavaScript string.
-   * @param byteOffset The offset within the data from which to begin writing.
-   *                   Has no effect on non-ArrayBuffer data.
-   * @param byteLength The number of bytes to write. Has no effect on
-   *                   non-ArrayBuffer data.
-   *
-   * @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((USVString or ArrayBuffer) data, optional unsigned long byteOffset, optional unsigned long byteLength);
-
-  /**
-   * 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;
-
-  /**
-   * 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 TCPSocketBinaryType 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 EventHandler 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 EventHandler 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 ArrayBuffer. 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 EventHandler 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 EventHandler 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 EventHandler onclose;
-
-  [ChromeOnly]
-  nsITCPSocketInternal getInternalSocket();
-};
deleted file mode 100644
--- a/dom/webidl/TCPSocketEvent.webidl
+++ /dev/null
@@ -1,34 +0,0 @@
-/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* 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/. */
-
-/**
- * TCPSocketEvent 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).
- */
-
-[Constructor(DOMString type, optional TCPSocketEventInit eventInitDict),
- Func="TCPSocketUtils::SocketEnabled", Exposed=(Window,System)]
-interface TCPSocketEvent : Event {
-  /**
-   * 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 ArrayBuffer;
-   * 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.
-   */
-  //TODO: make this (ArrayBuffer or DOMString) after sorting out the rooting required. bug 1121634
-  readonly attribute any data;
-};
-
-dictionary TCPSocketEventInit : EventInit {
-  //TODO: make this (ArrayBuffer or DOMString) after sorting out the rooting required. bug 1121634
-  any data = null;
-};
--- a/dom/webidl/moz.build
+++ b/dom/webidl/moz.build
@@ -488,20 +488,16 @@ WEBIDL_FILES = [
     'SVGTransformList.webidl',
     'SVGTSpanElement.webidl',
     'SVGUnitTypes.webidl',
     'SVGURIReference.webidl',
     'SVGUseElement.webidl',
     'SVGViewElement.webidl',
     'SVGZoomAndPan.webidl',
     'SVGZoomEvent.webidl',
-    'TCPServerSocket.webidl',
-    'TCPServerSocketEvent.webidl',
-    'TCPSocket.webidl',
-    'TCPSocketEvent.webidl',
     'Telephony.webidl',
     'TelephonyCall.webidl',
     'TelephonyCallGroup.webidl',
     'TelephonyCallId.webidl',
     'Text.webidl',
     'TextDecoder.webidl',
     'TextEncoder.webidl',
     'TextTrack.webidl',
@@ -735,18 +731,16 @@ GENERATED_EVENTS_WEBIDL_FILES = [
     'RTCPeerConnectionIceEvent.webidl',
     'RTCPeerConnectionIdentityErrorEvent.webidl',
     'RTCPeerConnectionIdentityEvent.webidl',
     'ScrollViewChangeEvent.webidl',
     'SelectionStateChangedEvent.webidl',
     'StyleRuleChangeEvent.webidl',
     'StyleSheetApplicableStateChangeEvent.webidl',
     'StyleSheetChangeEvent.webidl',
-    'TCPServerSocketEvent.webidl',
-    'TCPSocketEvent.webidl',
     'TrackEvent.webidl',
     'TVCurrentChannelChangedEvent.webidl',
     'TVCurrentSourceChangedEvent.webidl',
     'TVEITBroadcastedEvent.webidl',
     'TVScanningStateChangedEvent.webidl',
     'UDPMessageEvent.webidl',
     'UserProximityEvent.webidl',
     'USSDReceivedEvent.webidl',