Bug 1115502 - Expose inBrowser information to TCPSocket. r=honzab
authorEthan Tseng <ettseng@mozilla.com>
Mon, 29 Dec 2014 16:12:17 +0800
changeset 238433 b035d2c566ad7552410eff92ea9acac1aede59a0
parent 238432 1ca17114e06a366ff64b8de99b26a0297ad33b7c
child 238434 09c23f8dea868b7b93d7c23a3e1d1bc589b565cf
push id4610
push userjlund@mozilla.com
push dateMon, 30 Mar 2015 18:32:55 +0000
treeherdermozilla-esr52@4df54044d9ef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershonzab
bugs1115502
milestone38.0a1
Bug 1115502 - Expose inBrowser information to TCPSocket. r=honzab
dom/network/TCPServerSocketParent.cpp
dom/network/TCPServerSocketParent.h
dom/network/TCPSocket.js
dom/network/TCPSocketParent.cpp
dom/network/TCPSocketParent.h
dom/network/TCPSocketParentIntermediary.js
dom/network/interfaces/nsIDOMTCPSocket.idl
dom/network/interfaces/nsITCPSocketParent.idl
--- a/dom/network/TCPServerSocketParent.cpp
+++ b/dom/network/TCPServerSocketParent.cpp
@@ -56,17 +56,17 @@ TCPServerSocketParent::Init(PNeckoParent
   nsresult rv;
   mIntermediary = do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv);
   if (NS_FAILED(rv)) {
     FireInteralError(this, __LINE__);
     return true;
   }
 
   rv = mIntermediary->Listen(this, aLocalPort, aBacklog, aBinaryType, GetAppId(),
-                             getter_AddRefs(mServerSocket));
+                             GetInBrowser(), getter_AddRefs(mServerSocket));
   if (NS_FAILED(rv) || !mServerSocket) {
     FireInteralError(this, __LINE__);
     return true;
   }
   return true;
 }
 
 uint32_t
@@ -77,16 +77,29 @@ TCPServerSocketParent::GetAppId()
   const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
   if (browsers.Length() > 0) {
     TabParent *tab = static_cast<TabParent*>(browsers[0]);
     appId = tab->OwnAppId();
   }
   return appId;
 };
 
+bool
+TCPServerSocketParent::GetInBrowser()
+{
+  bool inBrowser = false;
+  const PContentParent *content = Manager()->Manager();
+  const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
+  if (browsers.Length() > 0) {
+    TabParent *tab = static_cast<TabParent*>(browsers[0]);
+    inBrowser = tab->IsBrowserElement();
+  }
+  return inBrowser;
+}
+
 NS_IMETHODIMP
 TCPServerSocketParent::SendCallbackAccept(nsITCPSocketParent *socket)
 {
   TCPSocketParent* _socket = static_cast<TCPSocketParent*>(socket);
   PTCPSocketParent* _psocket = static_cast<PTCPSocketParent*>(_socket);
 
   _socket->AddIPDLReference();
 
--- a/dom/network/TCPServerSocketParent.h
+++ b/dom/network/TCPServerSocketParent.h
@@ -27,16 +27,17 @@ public:
 
   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() {}
 
   virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE;
--- a/dom/network/TCPSocket.js
+++ b/dom/network/TCPSocket.js
@@ -179,16 +179,17 @@ TCPSocket.prototype = {
   _onUpdateBufferedAmount: null,
   _trackingNumber: 0,
 
 #ifdef MOZ_WIDGET_GONK
   // Network statistics (Gonk-specific feature)
   _txBytes: 0,
   _rxBytes: 0,
   _appId: Ci.nsIScriptSecurityManager.NO_APP_ID,
+  _inBrowser: false,
   _activeNetwork: null,
 #endif
 
   // Public accessors.
   get readyState() {
     return this._readyState;
   },
   get binaryType() {
@@ -476,16 +477,24 @@ TCPSocket.prototype = {
   setAppId: function ts_setAppId(appId) {
 #ifdef MOZ_WIDGET_GONK
     this._appId = appId;
 #else
     // Do nothing because _appId only exists on Gonk-specific platform.
 #endif
   },
 
+  setInBrowser: function ts_setInBrowser(inBrowser) {
+#ifdef MOZ_WIDGET_GONK
+    this._inBrowser = inBrowser;
+#else
+    // Do nothing.
+#endif
+  },
+
   setOnUpdateBufferedAmountHandler: function(aFunction) {
     if (typeof(aFunction) == 'function') {
       this._onUpdateBufferedAmount = aFunction;
     } else {
       throw new Error("only function can be passed to " +
                       "setOnUpdateBufferedAmountHandler");
     }
   },
--- a/dom/network/TCPSocketParent.cpp
+++ b/dom/network/TCPSocketParent.cpp
@@ -88,16 +88,29 @@ TCPSocketParent::GetAppId()
   const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
   if (browsers.Length() > 0) {
     TabParent *tab = static_cast<TabParent*>(browsers[0]);
     appId = tab->OwnAppId();
   }
   return appId;
 };
 
+bool
+TCPSocketParent::GetInBrowser()
+{
+  bool inBrowser = false;
+  const PContentParent *content = Manager()->Manager();
+  const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
+  if (browsers.Length() > 0) {
+    TabParent *tab = static_cast<TabParent*>(browsers[0]);
+    inBrowser = tab->IsBrowserElement();
+  }
+  return inBrowser;
+}
+
 nsresult
 TCPSocketParent::OfflineNotification(nsISupports *aSubject)
 {
   nsCOMPtr<nsIAppOfflineInfo> info(do_QueryInterface(aSubject));
   if (!info) {
     return NS_OK;
   }
 
@@ -157,32 +170,33 @@ TCPSocketParent::RecvOpen(const nsString
   if (net::UsingNeckoIPCSecurity() &&
       !AssertAppProcessPermission(Manager()->Manager(), "tcp-socket")) {
     FireInteralError(this, __LINE__);
     return true;
   }
 
   // Obtain App ID
   uint32_t appId = GetAppId();
+  bool     inBrowser = GetInBrowser();
 
   if (NS_IsAppOffline(appId)) {
     NS_ERROR("Can't open socket because app is offline");
     FireInteralError(this, __LINE__);
     return true;
   }
 
   nsresult rv;
   mIntermediary = do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv);
   if (NS_FAILED(rv)) {
     FireInteralError(this, __LINE__);
     return true;
   }
 
   rv = mIntermediary->Open(this, aHost, aPort, aUseSSL, aBinaryType, appId,
-                           getter_AddRefs(mSocket));
+                           inBrowser, getter_AddRefs(mSocket));
   if (NS_FAILED(rv) || !mSocket) {
     FireInteralError(this, __LINE__);
     return true;
   }
 
   return true;
 }
 
--- a/dom/network/TCPSocketParent.h
+++ b/dom/network/TCPSocketParent.h
@@ -58,16 +58,17 @@ public:
   virtual bool RecvSuspend() MOZ_OVERRIDE;
   virtual bool RecvResume() MOZ_OVERRIDE;
   virtual bool RecvClose() MOZ_OVERRIDE;
   virtual bool RecvData(const SendableData& aData,
                         const uint32_t& aTrackingNumber) MOZ_OVERRIDE;
   virtual bool RecvRequestDelete() MOZ_OVERRIDE;
   virtual nsresult OfflineNotification(nsISupports *) MOZ_OVERRIDE;
   virtual uint32_t GetAppId() MOZ_OVERRIDE;
+  bool GetInBrowser();
 
 private:
   virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE;
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/network/TCPSocketParentIntermediary.js
+++ b/dom/network/TCPSocketParentIntermediary.js
@@ -31,49 +31,53 @@ TCPSocketParentIntermediary.prototype = 
       }
     );
   },
 
   _onUpdateBufferedAmountHandler: function(aParentSide, aBufferedAmount, aTrackingNumber) {
     aParentSide.sendUpdateBufferedAmount(aBufferedAmount, aTrackingNumber);
   },
 
-  open: function(aParentSide, aHost, aPort, aUseSSL, aBinaryType, aAppId) {
+  open: function(aParentSide, aHost, aPort, aUseSSL, aBinaryType,
+                 aAppId, aInBrowser) {
     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.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 socket;
   },
 
-  listen: function(aTCPServerSocketParent, aLocalPort, aBacklog, aBinaryType, aAppId) {
+  listen: function(aTCPServerSocketParent, aLocalPort, aBacklog, aBinaryType,
+                   aAppId, aInBrowser) {
     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(socket) {
       var socketParent = Cc["@mozilla.org/tcp-socket-parent;1"]
                             .createInstance(Ci.nsITCPSocketParent);
       var intermediary = new TCPSocketParentIntermediary();
 
       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, socket);
       // The members in the socket parent object are set with arguments,
--- a/dom/network/interfaces/nsIDOMTCPSocket.idl
+++ b/dom/network/interfaces/nsIDOMTCPSocket.idl
@@ -211,17 +211,17 @@ interface nsIDOMTCPSocket : nsISupports
 };
 
 /*
  * 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(b1235064-9a08-4714-ad03-1212e4562803)]
+[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
@@ -271,16 +271,19 @@ interface nsITCPSocketInternal : nsISupp
   //        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
--- a/dom/network/interfaces/nsITCPSocketParent.idl
+++ b/dom/network/interfaces/nsITCPSocketParent.idl
@@ -60,28 +60,30 @@ interface nsITCPSocketParent : nsISuppor
   readonly attribute unsigned short port;
 };
 
 // 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(0bc14635-c586-4046-b82f-27ff45e6c39c)]
+[scriptable, uuid(aa9bd46d-26bf-4ba8-9c18-ba02482c02f0)]
 interface nsITCPSocketIntermediary : nsISupports {
   // Open the connection to the server with the given parameters
   nsIDOMTCPSocket open(in nsITCPSocketParent parent,
                        in DOMString host, in unsigned short port,
                        in boolean useSSL, in DOMString binaryType,
-                       in unsigned long appId);
+                       in unsigned long appId,
+                       in boolean inBrowser);
 
   // Listen on a port
   nsIDOMTCPServerSocket listen(in nsITCPServerSocketParent parent,
                                in unsigned short port, in unsigned short backlog,
                                in DOMString binaryType,
-                               in unsigned long appId);
+                               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);
 };