From e4426ae30fce736981de9199dd67f9b9859ef811 Mon Sep 17 00:00:00 2001 From: Bill McCloskey Date: Sun, 13 Nov 2016 10:22:52 -0800 Subject: [PATCH] Bug 1318506 - Label AsyncEventDispatcher runnables with DocGroup (r=ehsan) I still don't completely understand why we sometimes need to use the global and sometimes the node. As far as I understand it: - Not all event targets are nodes, so the node code can't always be used. - The nsINode::GetOwnerGlobal implementation uses GetScriptHandlingObject, which returns null in some cases where GetScopeObject doesn't. Here is one: http://searchfox.org/mozilla-central/rev/62db1c9021cfbde9fa5e6e9601de16c21f4c7ce4/dom/base/nsDocument.cpp#4627 MozReview-Commit-ID: DdLWeQJIWZx --- dom/events/AsyncEventDispatcher.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dom/events/AsyncEventDispatcher.cpp b/dom/events/AsyncEventDispatcher.cpp index da36f7993900..a02d1a407ffb 100644 --- a/dom/events/AsyncEventDispatcher.cpp +++ b/dom/events/AsyncEventDispatcher.cpp @@ -66,6 +66,18 @@ nsresult AsyncEventDispatcher::PostDOMEvent() { RefPtr ensureDeletionWhenFailing = this; + if (NS_IsMainThread()) { + if (nsCOMPtr global = mTarget->GetOwnerGlobal()) { + return global->Dispatch("AsyncEvent", TaskCategory::Other, ensureDeletionWhenFailing.forget()); + } + + // Sometimes GetOwnerGlobal returns null because it uses + // GetScriptHandlingObject rather than GetScopeObject. + if (nsCOMPtr node = do_QueryInterface(mTarget)) { + nsCOMPtr doc = node->OwnerDoc(); + return doc->Dispatch("AsyncEvent", TaskCategory::Other, ensureDeletionWhenFailing.forget()); + } + } return NS_DispatchToCurrentThread(this); }