Bug 1436743 - Dispatch events via the docgroup rather than the tabgroup when possible. r=mystor
authorJosh Matthews <josh@joshmatthews.net>
Thu, 08 Feb 2018 15:54:00 -0500
changeset 455580 d9da3f3bf43203a0ad3de87adfd8298c5e5e85a2
parent 455579 1c858a85df47bb57ba54d4b5cc84cccb1a2350c6
child 455581 4afd35f217c1bc90ecdd6b010d564852a59c5eca
push id8799
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 16:46:23 +0000
treeherdermozilla-beta@15334014dc67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmystor
bugs1436743
milestone60.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 1436743 - Dispatch events via the docgroup rather than the tabgroup when possible. r=mystor
docshell/base/nsDocShell.cpp
docshell/shistory/nsSHistory.cpp
dom/jsurl/nsJSProtocolHandler.cpp
dom/performance/Performance.cpp
dom/script/ScriptLoader.cpp
image/imgRequestProxy.cpp
toolkit/components/satchel/nsFormFillController.cpp
uriloader/base/nsDocLoader.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -1196,16 +1196,19 @@ nsDocShell::DispatchToTabGroup(TaskCateg
   nsCOMPtr<nsIRunnable> runnable(aRunnable);
   nsCOMPtr<nsPIDOMWindowOuter> win = GetWindow();
   if (!win) {
     // Window should only be unavailable after destroyed.
     MOZ_ASSERT(mIsBeingDestroyed);
     return NS_ERROR_FAILURE;
   }
 
+  if (win->GetDocGroup()) {
+    return win->GetDocGroup()->Dispatch(aCategory, runnable.forget());
+  }
   RefPtr<mozilla::dom::TabGroup> tabGroup = win->TabGroup();
   return tabGroup->Dispatch(aCategory, runnable.forget());
 }
 
 NS_IMETHODIMP
 nsDocShell::DispatchLocationChangeEvent()
 {
   return DispatchToTabGroup(
--- a/docshell/shistory/nsSHistory.cpp
+++ b/docshell/shistory/nsSHistory.cpp
@@ -2078,22 +2078,23 @@ nsSHistory::SetRootDocShell(nsIDocShell*
     // Seamonkey moves shistory between <xul:browser>s when restoring a tab.
     // Let's try not to break our friend too badly...
     if (mHistoryTracker) {
       NS_WARNING("Change the root docshell of a shistory is unsafe and "
                  "potentially problematic.");
       mHistoryTracker->AgeAllGenerations();
     }
 
-    RefPtr<mozilla::dom::TabGroup> tabGroup = win->TabGroup();
+    nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(win);
+
     mHistoryTracker = mozilla::MakeUnique<HistoryTracker>(
       this,
       mozilla::Preferences::GetUint(CONTENT_VIEWER_TIMEOUT_SECONDS,
                                     CONTENT_VIEWER_TIMEOUT_SECONDS_DEFAULT),
-      tabGroup->EventTargetFor(mozilla::TaskCategory::Other));
+      global->EventTargetFor(mozilla::TaskCategory::Other));
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSHistory::GetSHistoryEnumerator(nsISimpleEnumerator** aEnumerator)
 {
--- a/dom/jsurl/nsJSProtocolHandler.cpp
+++ b/dom/jsurl/nsJSProtocolHandler.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsCOMPtr.h"
 #include "jsapi.h"
 #include "jswrapper.h"
 #include "nsCRT.h"
 #include "nsError.h"
 #include "nsString.h"
+#include "nsGlobalWindowInner.h"
 #include "nsReadableUtils.h"
 #include "nsJSProtocolHandler.h"
 #include "nsStringStream.h"
 #include "nsNetUtil.h"
 
 #include "nsIStreamListener.h"
 #include "nsIComponentManager.h"
 #include "nsIServiceManager.h"
@@ -650,18 +651,19 @@ nsJSChannel::AsyncOpen(nsIStreamListener
 
         // We're returning success from asyncOpen2(), but we didn't open a
         // stream channel.  We'll have to notify ourselves, but make sure to do
         // it asynchronously.
         method = &nsJSChannel::NotifyListener;
         name = "nsJSChannel::NotifyListener";
     }
 
-    nsresult rv = NS_DispatchToCurrentThread(
-      mozilla::NewRunnableMethod(name, this, method));
+    nsCOMPtr<nsIRunnable> runnable = mozilla::NewRunnableMethod(name, this, method);
+    nsGlobalWindowInner* window = nsGlobalWindowInner::Cast(mOriginalInnerWindow);
+    nsresult rv = window->Dispatch(mozilla::TaskCategory::Other, runnable.forget());
 
     if (NS_FAILED(rv)) {
         loadGroup->RemoveRequest(this, nullptr, rv);
         mIsActive = false;
         CleanupStrongRefs();
     }
     return rv;
 }
--- a/dom/performance/Performance.cpp
+++ b/dom/performance/Performance.cpp
@@ -508,17 +508,22 @@ private:
   RefPtr<Performance> mPerformance;
 };
 
 void
 Performance::RunNotificationObserversTask()
 {
   mPendingNotificationObserversTask = true;
   nsCOMPtr<nsIRunnable> task = new NotifyObserversTask(this);
-  nsresult rv = NS_DispatchToCurrentThread(task);
+  nsresult rv;
+  if (GetOwnerGlobal()) {
+    rv = GetOwnerGlobal()->Dispatch(TaskCategory::Other, task.forget());
+  } else {
+    rv = NS_DispatchToCurrentThread(task);
+  }
   if (NS_WARN_IF(NS_FAILED(rv))) {
     mPendingNotificationObserversTask = false;
   }
 }
 
 void
 Performance::QueueEntry(PerformanceEntry* aEntry)
 {
--- a/dom/script/ScriptLoader.cpp
+++ b/dom/script/ScriptLoader.cpp
@@ -1371,20 +1371,25 @@ ScriptLoader::ProcessExternalScript(nsIS
     // keep request->mScriptFromHead to false so we don't treat non preloaded
     // scripts as blockers for full page load. See bug 792438.
 
     nsresult rv = StartLoad(request);
     if (NS_FAILED(rv)) {
       ReportErrorToConsole(request, rv);
 
       // Asynchronously report the load failure
-      NS_DispatchToCurrentThread(
+      nsCOMPtr<nsIRunnable> runnable =
         NewRunnableMethod("nsIScriptElement::FireErrorEvent",
                           aElement,
-                          &nsIScriptElement::FireErrorEvent));
+                          &nsIScriptElement::FireErrorEvent);
+      if (mDocument) {
+        mDocument->Dispatch(TaskCategory::Other, runnable.forget());
+      } else {
+        NS_DispatchToCurrentThread(runnable);
+      }
       return false;
     }
   }
 
   // Should still be in loading stage of script.
   NS_ASSERTION(!request->InCompilingStage(),
                "Request should not yet be in compiling stage.");
 
--- a/image/imgRequestProxy.cpp
+++ b/image/imgRequestProxy.cpp
@@ -352,17 +352,17 @@ imgRequestProxy::AddToOwner(nsIDocument*
   // scheduler group is valid with or without a document, but that means
   // we will use the most generic event target possible on dispatch.
   if (aLoadingDocument) {
     RefPtr<dom::DocGroup> docGroup = aLoadingDocument->GetDocGroup();
     if (docGroup) {
       mTabGroup = docGroup->GetTabGroup();
       MOZ_ASSERT(mTabGroup);
 
-      mEventTarget = mTabGroup->EventTargetFor(mozilla::TaskCategory::Other);
+      mEventTarget = docGroup->EventTargetFor(mozilla::TaskCategory::Other);
       MOZ_ASSERT(mEventTarget);
     }
   }
 
   if (mListener && !mEventTarget) {
     mEventTarget = do_GetMainThread();
   }
 
--- a/toolkit/components/satchel/nsFormFillController.cpp
+++ b/toolkit/components/satchel/nsFormFillController.cpp
@@ -153,17 +153,17 @@ nsFormFillController::AttributeChanged(n
     // to avoid ending up in an endless loop due to re-registering our
     // mutation observer (which would notify us again for *this* event).
     nsCOMPtr<nsIRunnable> event =
       mozilla::NewRunnableMethod<RefPtr<HTMLInputElement>>(
         "nsFormFillController::MaybeStartControllingInput",
         this,
         &nsFormFillController::MaybeStartControllingInput,
         focusedInput);
-    NS_DispatchToCurrentThread(event);
+    aDocument->Dispatch(TaskCategory::Other, event.forget());
   }
 
   if (mListNode && mListNode->Contains(aElement)) {
     RevalidateDataList();
   }
 }
 
 void
--- a/uriloader/base/nsDocLoader.cpp
+++ b/uriloader/base/nsDocLoader.cpp
@@ -1,15 +1,14 @@
 /* -*- Mode: C++; 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/. */
 
 #include "nspr.h"
-#include "mozilla/dom/TabGroup.h"
 #include "mozilla/Logging.h"
 #include "mozilla/IntegerPrintfMacros.h"
 
 #include "nsDocLoader.h"
 #include "nsCURILoader.h"
 #include "nsNetUtil.h"
 #include "nsIHttpChannel.h"
 #include "nsIWebProgressListener2.h"
@@ -982,20 +981,20 @@ nsDocLoader::GetLoadType(uint32_t *aLoad
 
 NS_IMETHODIMP
 nsDocLoader::GetTarget(nsIEventTarget** aTarget)
 {
   nsCOMPtr<mozIDOMWindowProxy> window;
   nsresult rv = GetDOMWindow(getter_AddRefs(window));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsPIDOMWindowOuter> piwindow = nsPIDOMWindowOuter::From(window);
-  NS_ENSURE_STATE(piwindow);
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(window);
+  NS_ENSURE_STATE(global);
 
-  nsCOMPtr<nsIEventTarget> target = piwindow->TabGroup()->EventTargetFor(mozilla::TaskCategory::Other);
+  nsCOMPtr<nsIEventTarget> target = global->EventTargetFor(mozilla::TaskCategory::Other);
   target.forget(aTarget);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDocLoader::SetTarget(nsIEventTarget* aTarget)
 {
   return NS_ERROR_NOT_IMPLEMENTED;