Bug 1627533 - Create LoadGroups for docshells using the BrowsingContext's request context id. r=dragana,necko-reviewers
☠☠ backed out by 6d55c17a5a85 ☠ ☠
authorMatt Woodrow <mwoodrow@mozilla.com>
Wed, 20 May 2020 23:41:36 +0000
changeset 531351 70fa5e075269720bf83a452698b78abbfeafe5ba
parent 531350 0d10466705340f8a95cb8b64222f611315b5b068
child 531352 3b35a1852a6011c8094e2d3d538db7093ab6f86c
push id37438
push userabutkovits@mozilla.com
push dateThu, 21 May 2020 09:36:57 +0000
treeherdermozilla-central@2d00a1a6495c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdragana, necko-reviewers
bugs1627533
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1627533 - Create LoadGroups for docshells using the BrowsingContext's request context id. r=dragana,necko-reviewers Differential Revision: https://phabricator.services.mozilla.com/D75885
docshell/base/nsDocShell.cpp
netwerk/base/nsLoadGroup.cpp
netwerk/base/nsLoadGroup.h
uriloader/base/moz.build
uriloader/base/nsDocLoader.cpp
uriloader/base/nsDocLoader.h
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -459,17 +459,17 @@ nsDocShell::~nsDocShell() {
 already_AddRefed<nsDocShell> nsDocShell::Create(
     BrowsingContext* aBrowsingContext, uint64_t aContentWindowID) {
   MOZ_ASSERT(aBrowsingContext, "DocShell without a BrowsingContext!");
 
   nsresult rv;
   RefPtr<nsDocShell> ds = new nsDocShell(aBrowsingContext, aContentWindowID);
 
   // Initialize the underlying nsDocLoader.
-  rv = ds->nsDocLoader::Init();
+  rv = ds->nsDocLoader::InitWithBrowsingContext(aBrowsingContext);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return nullptr;
   }
 
   // Create our ContentListener
   ds->mContentListener = new nsDSURIContentListener(ds);
 
   // If parent intercept is not enabled then we must forward to
--- a/netwerk/base/nsLoadGroup.cpp
+++ b/netwerk/base/nsLoadGroup.cpp
@@ -89,28 +89,29 @@ nsLoadGroup::nsLoadGroup()
       mLoadFlags(LOAD_NORMAL),
       mDefaultLoadFlags(0),
       mPriority(PRIORITY_NORMAL),
       mRequests(&sRequestHashOps, sizeof(RequestMapEntry)),
       mStatus(NS_OK),
       mIsCanceling(false),
       mDefaultLoadIsTimed(false),
       mBrowsingContextDiscarded(false),
+      mExternalRequestContext(false),
       mTimedRequests(0),
       mCachedRequests(0) {
   LOG(("LOADGROUP [%p]: Created.\n", this));
 }
 
 nsLoadGroup::~nsLoadGroup() {
   DebugOnly<nsresult> rv = Cancel(NS_BINDING_ABORTED);
   NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed");
 
   mDefaultLoadRequest = nullptr;
 
-  if (mRequestContext) {
+  if (mRequestContext && !mExternalRequestContext) {
     mRequestContextService->RemoveRequestContext(mRequestContext->GetID());
     if (IsNeckoChild() && gNeckoChild) {
       gNeckoChild->SendRemoveRequestContext(mRequestContext->GetID());
     }
   }
 
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
   if (os) {
@@ -973,16 +974,33 @@ nsresult nsLoadGroup::Init() {
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
   NS_ENSURE_STATE(os);
 
   Unused << os->AddObserver(this, "last-pb-context-exited", true);
 
   return NS_OK;
 }
 
+nsresult nsLoadGroup::InitWithRequestContextId(
+    const uint64_t& aRequestContextId) {
+  mRequestContextService = RequestContextService::GetOrCreate();
+  if (mRequestContextService) {
+    Unused << mRequestContextService->GetRequestContext(
+        aRequestContextId, getter_AddRefs(mRequestContext));
+  }
+  mExternalRequestContext = true;
+
+  nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+  NS_ENSURE_STATE(os);
+
+  Unused << os->AddObserver(this, "last-pb-context-exited", true);
+
+  return NS_OK;
+}
+
 NS_IMETHODIMP
 nsLoadGroup::Observe(nsISupports* aSubject, const char* aTopic,
                      const char16_t* aData) {
   MOZ_ASSERT(!strcmp(aTopic, "last-pb-context-exited"));
 
   OriginAttributes attrs;
   StoragePrincipalHelper::GetRegularPrincipalOriginAttributes(this, attrs);
   if (attrs.mPrivateBrowsingId == 0) {
--- a/netwerk/base/nsLoadGroup.h
+++ b/netwerk/base/nsLoadGroup.h
@@ -51,16 +51,17 @@ class nsLoadGroup : public nsILoadGroup,
   NS_DECL_NSIOBSERVER
 
   ////////////////////////////////////////////////////////////////////////////
   // nsLoadGroup methods:
 
   nsLoadGroup();
 
   nsresult Init();
+  nsresult InitWithRequestContextId(const uint64_t& aRequestContextId);
 
  protected:
   virtual ~nsLoadGroup();
 
   nsresult MergeLoadFlags(nsIRequest* aRequest, nsLoadFlags& flags);
   nsresult MergeDefaultLoadFlags(nsIRequest* aRequest, nsLoadFlags& flags);
 
  private:
@@ -87,16 +88,17 @@ class nsLoadGroup : public nsILoadGroup,
 
   nsWeakPtr mObserver;
   nsWeakPtr mParentLoadGroup;
 
   nsresult mStatus;
   bool mIsCanceling;
   bool mDefaultLoadIsTimed;
   bool mBrowsingContextDiscarded;
+  bool mExternalRequestContext;
 
   /* Telemetry */
   mozilla::TimeStamp mDefaultRequestCreationTime;
   uint32_t mTimedRequests;
   uint32_t mCachedRequests;
 };
 
 }  // namespace net
--- a/uriloader/base/moz.build
+++ b/uriloader/base/moz.build
@@ -25,9 +25,13 @@ EXPORTS += [
     'nsURILoader.h',
 ]
 
 UNIFIED_SOURCES += [
     'nsDocLoader.cpp',
     'nsURILoader.cpp',
 ]
 
+LOCAL_INCLUDES += [
+    '/netwerk/base',
+]
+
 FINAL_LIBRARY = 'xul'
--- a/uriloader/base/nsDocLoader.cpp
+++ b/uriloader/base/nsDocLoader.cpp
@@ -10,16 +10,17 @@
 #include "mozilla/Components.h"
 #include "mozilla/EventDispatcher.h"
 #include "mozilla/Logging.h"
 #include "mozilla/IntegerPrintfMacros.h"
 #include "mozilla/PresShell.h"
 
 #include "nsDocLoader.h"
 #include "nsDocShell.h"
+#include "nsLoadGroup.h"
 #include "nsNetUtil.h"
 #include "nsIHttpChannel.h"
 #include "nsIWebNavigation.h"
 #include "nsIWebProgressListener2.h"
 
 #include "nsString.h"
 
 #include "nsCOMPtr.h"
@@ -134,16 +135,34 @@ nsresult nsDocLoader::Init() {
   if (NS_FAILED(rv)) return rv;
 
   MOZ_LOG(gDocLoaderLog, LogLevel::Debug,
           ("DocLoader:%p: load group %p.\n", this, mLoadGroup.get()));
 
   return NS_OK;
 }
 
+nsresult nsDocLoader::InitWithBrowsingContext(
+    BrowsingContext* aBrowsingContext) {
+  RefPtr<net::nsLoadGroup> loadGroup = new net::nsLoadGroup();
+  nsresult rv = loadGroup->InitWithRequestContextId(
+      aBrowsingContext->GetRequestContextId());
+  if (NS_FAILED(rv)) return rv;
+
+  rv = loadGroup->SetGroupObserver(this);
+  if (NS_FAILED(rv)) return rv;
+
+  mLoadGroup = loadGroup;
+
+  MOZ_LOG(gDocLoaderLog, LogLevel::Debug,
+          ("DocLoader:%p: load group %p.\n", this, mLoadGroup.get()));
+
+  return NS_OK;
+}
+
 nsDocLoader::~nsDocLoader() {
   /*
           |ClearWeakReferences()| here is intended to prevent people holding
      weak references from re-entering this destructor since |QueryReferent()|
      will |AddRef()| me, and the subsequent |Release()| will try to destroy me.
      At this point there should be only weak references remaining (otherwise, we
      wouldn't be getting destroyed).
 
--- a/uriloader/base/nsDocLoader.h
+++ b/uriloader/base/nsDocLoader.h
@@ -26,16 +26,17 @@
 #include "nsCycleCollectionParticipant.h"
 
 #include "mozilla/LinkedList.h"
 #include "mozilla/UniquePtr.h"
 
 namespace mozilla {
 namespace dom {
 class BrowserBridgeChild;
+class BrowsingContext;
 }  // namespace dom
 }  // namespace mozilla
 
 /****************************************************************************
  * nsDocLoader implementation...
  ****************************************************************************/
 
 #define NS_THIS_DOCLOADER_IMPL_CID                   \
@@ -56,16 +57,18 @@ class nsDocLoader : public nsIDocumentLo
  public:
   using BrowserBridgeChild = mozilla::dom::BrowserBridgeChild;
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_THIS_DOCLOADER_IMPL_CID)
 
   nsDocLoader();
 
   [[nodiscard]] virtual nsresult Init();
+  [[nodiscard]] nsresult InitWithBrowsingContext(
+      mozilla::dom::BrowsingContext* aBrowsingContext);
 
   static already_AddRefed<nsDocLoader> GetAsDocLoader(nsISupports* aSupports);
   // Needed to deal with ambiguous inheritance from nsISupports...
   static nsISupports* GetAsSupports(nsDocLoader* aDocLoader) {
     return static_cast<nsIDocumentLoader*>(aDocLoader);
   }
 
   // Add aDocLoader as a child to the docloader service.