Bug 1497235 - P1: Pass request context id to HttpTransactionChild, r=JuniorHsu
authorKershaw Chang <kershaw@mozilla.com>
Tue, 29 Jan 2019 18:09:36 +0100
changeset 448034 eaf43966becc3854ecf2dae70f763fc69c281ac8
parent 448033 e846f91aa280453dbe1df103da5ee5722ec7c986
child 448035 41d3a13f5782ead12d6db9e94bf1a647f9734414
push id28
push userkjang@mozilla.com
push dateTue, 29 Jan 2019 17:10:05 +0000
reviewersJuniorHsu
bugs1497235
milestone66.0a1
Bug 1497235 - P1: Pass request context id to HttpTransactionChild, r=JuniorHsu
netwerk/protocol/http/HttpTransactionChild.cpp
netwerk/protocol/http/HttpTransactionChild.h
netwerk/protocol/http/HttpTransactionParent.cpp
netwerk/protocol/http/PHttpTransaction.ipdl
netwerk/protocol/http/nsAHttpTransactionShell.h
netwerk/protocol/http/nsHttpChannel.cpp
netwerk/protocol/http/nsHttpTransaction.cpp
netwerk/protocol/http/nsHttpTransaction.h
--- a/netwerk/protocol/http/HttpTransactionChild.cpp
+++ b/netwerk/protocol/http/HttpTransactionChild.cpp
@@ -35,25 +35,44 @@ HttpTransactionChild::HttpTransactionChi
   mPeerAddr.raw.family = PR_AF_UNSPEC;
 }
 
 HttpTransactionChild::~HttpTransactionChild()
 {
   LOG(("Destroying HttpTransactionChild @%p\n", this));
 }
 
+static already_AddRefed<nsIRequestContext>
+CreateRequestContext(uint64_t aRequestContextID)
+{
+  if (!aRequestContextID) {
+    return nullptr;
+  }
+
+  nsIRequestContextService* rcsvc = gHttpHandler->GetRequestContextService();
+  if (!rcsvc) {
+    return nullptr;
+  }
+
+  nsCOMPtr<nsIRequestContext> requestContext;
+  rcsvc->GetRequestContext(aRequestContextID, getter_AddRefs(requestContext));
+
+  return requestContext.forget();
+}
+
 nsresult
 HttpTransactionChild::InitInternal(uint32_t caps,
                                    const HttpConnectionInfoCloneArgs &infoArgs,
                                    nsHttpRequestHead *requestHead,
                                    nsIInputStream *requestBody,
                                    uint64_t requestContentLength,
                                    bool requestBodyHasHeaders,
                                    nsIEventTarget *target,
-                                   uint64_t topLevelOuterContentWindowId)
+                                   uint64_t topLevelOuterContentWindowId,
+                                   uint64_t requestContextID)
 {
   LOG(("HttpTransactionChild::InitInternal [this=%p caps=%x]\n", this, caps));
 
   RefPtr<nsHttpConnectionInfo> cinfo;
   if (infoArgs.routedHost().IsEmpty()) {
       cinfo = new nsHttpConnectionInfo(infoArgs.host(), infoArgs.port(), infoArgs.npnToken(), infoArgs.username(), nullptr,
                                        infoArgs.originAttributes(), infoArgs.endToEndSSL());
   } else {
@@ -66,22 +85,25 @@ HttpTransactionChild::InitInternal(uint3
   cinfo->SetPrivate(infoArgs.aPrivate());
   cinfo->SetInsecureScheme(infoArgs.insecureScheme());
   cinfo->SetNoSpdy(infoArgs.noSpdy());
   cinfo->SetBeConservative(infoArgs.beConservative());
   cinfo->SetTlsFlags(infoArgs.tlsFlags());
   cinfo->SetTrrUsed(infoArgs.trrUsed());
   cinfo->SetTrrDisabled(infoArgs.trrDisabled());
 
+  nsCOMPtr<nsIRequestContext> rc = CreateRequestContext(requestContextID);
+  LOG(("  CreateRequestContext this=%p id=%" PRIx64, this, requestContextID));
+
   nsresult rv =
    mTransaction->Init(caps, cinfo, requestHead, requestBody,
                       requestContentLength, requestBodyHasHeaders,
                       target, nullptr, // TODO: security callback
-                      this, topLevelOuterContentWindowId);
-  if (NS_FAILED(rv)) {
+                      this, topLevelOuterContentWindowId, rc);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
     mTransaction = nullptr;
   }
 
   return rv;
 }
 
 mozilla::ipc::IPCResult
 HttpTransactionChild::RecvCancelPump(const nsresult& aStatus)
@@ -119,31 +141,32 @@ HttpTransactionChild::RecvResumePump()
 
 mozilla::ipc::IPCResult
 HttpTransactionChild::RecvInit(const uint32_t& aCaps,
                                const HttpConnectionInfoCloneArgs& aArgs,
                                const nsHttpRequestHead& aReqHeaders,
                                const OptionalIPCStream& aRequestBody,
                                const uint64_t& aReqContentLength,
                                const bool& aReqBodyIncludesHeaders,
-                               const uint64_t& aTopLevelOuterContentWindowId)
+                               const uint64_t& aTopLevelOuterContentWindowId,
+                               const uint64_t& aRequestContextID)
 {
   mRequestHead = aReqHeaders;
   mUploadStream = mozilla::ipc::DeserializeIPCStream(aRequestBody);
   // TODO: let parent process know about the failure
   if (NS_FAILED(InitInternal(aCaps, aArgs, &mRequestHead, mUploadStream,
                              aReqContentLength, aReqBodyIncludesHeaders,
                              GetCurrentThreadEventTarget(),
-                             aTopLevelOuterContentWindowId))) {
+                             aTopLevelOuterContentWindowId,
+                             aRequestContextID))) {
     LOG(("HttpTransactionChild::RecvInit: [this=%p] InitInternal failed!\n", this));
   }
   return IPC_OK();
 }
 
-
 mozilla::ipc::IPCResult
 HttpTransactionChild::RecvRead(const int32_t& priority)
 {
   LOG(("HttpTransactionChild::RecvRead start [this=%p]\n", this));
   MOZ_ASSERT(mTransaction, "should SentInit first");
   if (mTransaction) {
     Unused << mTransaction->AsyncRead(this, priority, getter_AddRefs(mTransactionPump));
   }
--- a/netwerk/protocol/http/HttpTransactionChild.h
+++ b/netwerk/protocol/http/HttpTransactionChild.h
@@ -35,17 +35,18 @@ public:
 
   mozilla::ipc::IPCResult
   RecvInit(const uint32_t& aCaps,
            const HttpConnectionInfoCloneArgs& aArgs,
            const nsHttpRequestHead& aReqHeaders,
            const OptionalIPCStream& aRequestBody,
            const uint64_t& aReqContentLength,
            const bool& aReqBodyIncludesHeaders,
-           const uint64_t& aTopLevelOuterContentWindowId) override;
+           const uint64_t& aTopLevelOuterContentWindowId,
+           const uint64_t& aRequestContextID) override;
   mozilla::ipc::IPCResult RecvRead(const int32_t& priority) override;
   mozilla::ipc::IPCResult RecvReschedule(const int32_t& priority) override;
   mozilla::ipc::IPCResult RecvUpdateClassOfService(const uint32_t& classOfService) override;
   mozilla::ipc::IPCResult RecvCancel(const nsresult& reason) override;
   mozilla::ipc::IPCResult RecvCancelPump(const nsresult& aStatus) override;
   mozilla::ipc::IPCResult RecvSuspendPump() override;
   mozilla::ipc::IPCResult RecvResumePump() override;
   mozilla::ipc::IPCResult RecvSetDNSWasRefreshed() override;
@@ -56,17 +57,18 @@ private:
   // the parameters.
   MOZ_MUST_USE nsresult InitInternal(uint32_t               caps,
                                      const HttpConnectionInfoCloneArgs &aArgs, // remove proxyInfo first
                                      nsHttpRequestHead     *reqHeaders,
                                      nsIInputStream        *reqBody, // use the trick in bug 1277681
                                      uint64_t               reqContentLength,
                                      bool                   reqBodyIncludesHeaders,
                                      nsIEventTarget        *consumerTarget, // Will remove
-                                     uint64_t               topLevelOuterContentWindowId);
+                                     uint64_t               topLevelOuterContentWindowId,
+                                     uint64_t               requestContextID);
 
   nsHttpRequestHead mRequestHead;
   nsCOMPtr<nsIInputStream> mUploadStream;
   RefPtr<nsHttpTransaction> mTransaction;
   nsCOMPtr<nsIRequest> mTransactionPump;
   NetAddr mSelfAddr;
   NetAddr mPeerAddr;
 };
--- a/netwerk/protocol/http/HttpTransactionParent.cpp
+++ b/netwerk/protocol/http/HttpTransactionParent.cpp
@@ -74,34 +74,39 @@ HttpTransactionParent::Init(uint32_t cap
                             nsHttpConnectionInfo *cinfo,
                             nsHttpRequestHead *requestHead,
                             nsIInputStream *requestBody,
                             uint64_t requestContentLength,
                             bool requestBodyHasHeaders,
                             nsIEventTarget *target,
                             nsIInterfaceRequestor *callbacks,
                             nsITransportEventSink *eventsink,
-                            uint64_t topLevelOuterContentWindowId)
+                            uint64_t topLevelOuterContentWindowId,
+                            nsIRequestContext *requestContext)
 {
   LOG(("HttpTransactionParent::Init [this=%p caps=%x]\n", this, caps));
 
   mEventsink = eventsink;
 
   HttpConnectionInfoCloneArgs infoArgs;
   GetStructFromInfo(cinfo, infoArgs);
 
   mozilla::ipc::AutoIPCStream autoStream;
   if (requestBody && !autoStream.Serialize(requestBody, SocketProcessParent::GetSingleton())) {
     return NS_ERROR_FAILURE;
   }
 
+  uint64_t requestContextID = requestContext
+    ? requestContext->GetID()
+    : 0;
+
   // TODO: handle |target| later
   if (!SendInit(caps, infoArgs, *requestHead, autoStream.TakeOptionalValue(),
                 requestContentLength, requestBodyHasHeaders,
-                topLevelOuterContentWindowId)) {
+                topLevelOuterContentWindowId, requestContextID)) {
     return NS_ERROR_FAILURE;
   }
 
   mTargetThread = GetCurrentThreadEventTarget();
   return NS_OK;
 }
 
 nsresult
--- a/netwerk/protocol/http/PHttpTransaction.ipdl
+++ b/netwerk/protocol/http/PHttpTransaction.ipdl
@@ -46,17 +46,18 @@ parent:
 child:
   async __delete__();
   async Init(uint32_t caps,
              HttpConnectionInfoCloneArgs aArgs,
              nsHttpRequestHead reqHeaders,
              OptionalIPCStream requestBody,
              uint64_t reqContentLength,
              bool reqBodyIncludesHeaders,
-             uint64_t topLevelOuterContentWindowId);
+             uint64_t topLevelOuterContentWindowId,
+             uint64_t requestContextID);
   async Read(int32_t priority);
   async Reschedule(int32_t priority);
   async UpdateClassOfService(uint32_t classOfService);
   async Cancel(nsresult reason);
 
   async CancelPump(nsresult status);
   async SuspendPump();
   async ResumePump();
--- a/netwerk/protocol/http/nsAHttpTransactionShell.h
+++ b/netwerk/protocol/http/nsAHttpTransactionShell.h
@@ -6,16 +6,17 @@
 #define nsAHttpTransactionShell_h__
 
 #include "nsISupports.h"
 
 class nsIEventTraget;
 class nsIInputStream;
 class nsIInterfaceRequestor;
 class nsIRequest;
+class nsIRequestContext;
 class nsITransportEventSink;
 
 namespace mozilla { namespace net {
 
 class nsHttpConnectionInfo;
 class nsHttpRequestHead;
 
 //----------------------------------------------------------------------------
@@ -58,17 +59,18 @@ public:
        nsHttpConnectionInfo  *connInfo,
        nsHttpRequestHead     *reqHeaders,
        nsIInputStream        *reqBody,
        uint64_t               reqContentLength,
        bool                   reqBodyIncludesHeaders,
        nsIEventTarget        *consumerTarget,
        nsIInterfaceRequestor *callbacks,
        nsITransportEventSink *eventsink,
-       uint64_t               topLevelOuterContentWindowId) = 0;
+       uint64_t               topLevelOuterContentWindowId,
+       nsIRequestContext     *requestContext) = 0;
 
   // @param aListener
   //        receives notifications.
   // @param pump
   //        the pump that will contain the response data. async wait on this
   //        input stream for data. On first notification, headers should be
   //        available (check transaction status).
   virtual nsresult AsyncRead(nsIStreamListener *listener,
@@ -116,17 +118,18 @@ NS_DEFINE_STATIC_IID_ACCESSOR(nsAHttpTra
        nsHttpConnectionInfo  *connInfo, \
        nsHttpRequestHead     *reqHeaders, \
        nsIInputStream        *reqBody, \
        uint64_t               reqContentLength, \
        bool                   reqBodyIncludesHeaders, \
        nsIEventTarget        *consumerTarget, \
        nsIInterfaceRequestor *callbacks, \
        nsITransportEventSink *eventsink, \
-       uint64_t               topLevelOuterContentWindowId) override; \
+       uint64_t               topLevelOuterContentWindowId, \
+       nsIRequestContext     *requestContext) override; \
   virtual nsresult AsyncRead(nsIStreamListener *listener, \
                              int32_t priority, \
                              nsIRequest **pump) override; \
   virtual nsresult AsyncReschedule(int32_t priority) override; \
   virtual void AsyncUpdateClassOfService(uint32_t classOfService) override; \
   virtual nsresult AsyncCancel(nsresult reason) override; \
   virtual nsHttpResponseHead* TakeResponseHead() override; \
   virtual nsISupports* SecurityInfo() override; \
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -1275,37 +1275,34 @@ nsresult nsHttpChannel::SetupTransaction
   NS_QueryNotificationCallbacks(mCallbacks, mLoadGroup,
                                 NS_GET_IID(nsIHttpPushListener),
                                 getter_AddRefs(pushListener));
   if (pushListener) {
     mCaps |= NS_HTTP_ONPUSH_LISTENER;
   }
 
   EnsureTopLevelOuterContentWindowId();
+  EnsureRequestContext();
 
   nsCOMPtr<nsIAsyncInputStream> responseStream;
   rv = mTransaction->Init(mCaps, mConnectionInfo, &mRequestHead, mUploadStream,
                           mReqContentLength, mUploadStreamHasHeaders,
                           GetCurrentThreadEventTarget(), callbacks, this,
-                          mTopLevelOuterContentWindowId);
+                          mTopLevelOuterContentWindowId, mRequestContext);
   if (NS_FAILED(rv)) {
     mTransaction = nullptr;
     return rv;
   }
 
   // TODO: this line is added for security callback workaround
   // mTransaction->SetSecurityCallbacks(callbacks);
 
   // TODO: make throttling work again
   // mTransaction->SetClassOfService(mClassOfService);
 
-  // TODO: make request context service work agin
-  // if (EnsureRequestContext()) {
-  //     mTransaction->SetRequestContext(mRequestContext);
-  // }
   return NS_OK;
 }
 
 enum class Report { Error, Warning };
 
 // Helper Function to report messages to the console when the loaded
 // script had a wrong MIME type.
 void ReportMimeTypeMismatch(nsHttpChannel *aChannel, const char *aMessageName,
--- a/netwerk/protocol/http/nsHttpTransaction.cpp
+++ b/netwerk/protocol/http/nsHttpTransaction.cpp
@@ -254,29 +254,32 @@ nsHttpTransaction::~nsHttpTransaction() 
   }
 }
 
 nsresult nsHttpTransaction::Init(
     uint32_t caps, nsHttpConnectionInfo *cinfo, nsHttpRequestHead *requestHead,
     nsIInputStream *requestBody, uint64_t requestContentLength,
     bool requestBodyHasHeaders, nsIEventTarget *target,
     nsIInterfaceRequestor *callbacks, nsITransportEventSink *eventsink,
-    uint64_t topLevelOuterContentWindowId) {
+    uint64_t topLevelOuterContentWindowId, nsIRequestContext *requestContext) {
   nsresult rv;
 
   LOG1(("nsHttpTransaction::Init [this=%p caps=%x]\n", this, caps));
 
   MOZ_ASSERT(cinfo);
   MOZ_ASSERT(requestHead);
   MOZ_ASSERT(target);
   MOZ_ASSERT(NS_IsMainThread());
 
   mTopLevelOuterContentWindowId = topLevelOuterContentWindowId;
   LOG(("  window-id = %" PRIx64, mTopLevelOuterContentWindowId));
 
+  mRequestContext = requestContext;
+  LOG1(("nsHttpTransaction %p SetRequestContext %p\n", this, mRequestContext.get()));
+
   mActivityDistributor = services::GetActivityDistributor();
   if (!mActivityDistributor) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   bool activityDistributorActive;
   rv = mActivityDistributor->GetIsActive(&activityDistributorActive);
   if (NS_SUCCEEDED(rv) && activityDistributorActive) {
@@ -1886,21 +1889,16 @@ nsresult nsHttpTransaction::ProcessData(
       mContentDecoding = mResponseHead->HasHeader(nsHttp::Content_Encoding);
       mContentDecodingCheck = true;
     }
   }
 
   return NS_OK;
 }
 
-void nsHttpTransaction::SetRequestContext(nsIRequestContext *aRequestContext) {
-  LOG1(("nsHttpTransaction %p SetRequestContext %p\n", this, aRequestContext));
-  mRequestContext = aRequestContext;
-}
-
 // Called when the transaction marked for blocking is associated with a
 // connection (i.e. added to a new h1 conn, an idle http connection, etc..) It
 // is safe to call this multiple times with it only having an effect once.
 void nsHttpTransaction::DispatchedAsBlocking() {
   if (mDispatchedAsBlocking) return;
 
   LOG(("nsHttpTransaction %p dispatched as blocking\n", this));
 
--- a/netwerk/protocol/http/nsHttpTransaction.h
+++ b/netwerk/protocol/http/nsHttpTransaction.h
@@ -94,17 +94,16 @@ class nsHttpTransaction final : public n
   // is false)
   void SetPendingTime(bool now = true) {
     mPendingTime = now ? TimeStamp::Now() : TimeStamp();
   }
   const TimeStamp GetPendingTime() { return mPendingTime; }
 
   // overload of nsAHttpTransaction::RequestContext()
   nsIRequestContext *RequestContext() override { return mRequestContext.get(); }
-  void SetRequestContext(nsIRequestContext *aRequestContext);
   void DispatchedAsBlocking();
   void RemoveDispatchedAsBlocking();
 
   void DisableSpdy() override;
 
   nsHttpTransaction *QueryHttpTransaction() override { return this; }
 
   Http2PushedStream *GetPushedStream() { return mPushedStream; }