Title: [252723] trunk/Source/WebCore
Revision
252723
Author
[email protected]
Date
2019-11-20 17:47:56 -0800 (Wed, 20 Nov 2019)

Log Message

MicrotaskQueue should be accessed via EventLoop
https://bugs.webkit.org/show_bug.cgi?id=204397

Reviewed by Antti Koivisto.

This patch refactors the existing code so that a microtask is always queued via EventLoopTaskGroup.
It preserves all other (broken) semantics and behavior like all origins sharing a single microtask queue.

The singleton MicrotaskQueue for the main thread has been moved from MicrotaskQueue::mainThreadQueue
to WindowEventLoop, and an instance of MicrotaskQueue for each worker has been moved from WorkerGlobalScope
to WorkerEventLoop.

* animation/DocumentTimeline.cpp:
(WebCore::DocumentTimeline::internalUpdateAnimationsAndSendEvents):
* animation/WebAnimation.cpp:
(WebCore::WebAnimation::updateFinishedState):
* bindings/js/JSDOMGlobalObjectTask.cpp:
(WebCore::JSGlobalObjectTask::JSGlobalObjectTask):
* bindings/js/JSDOMWindowBase.cpp:
(WebCore::JSDOMWindowBase::queueMicrotaskToEventLoop): Renamed from queueTaskToEventLoop.
* bindings/js/JSDOMWindowBase.h:
* bindings/js/JSExecState.cpp:
(WebCore::JSExecState::didLeaveScriptContext):
* bindings/js/JSRemoteDOMWindowBase.cpp:
* bindings/js/JSWorkerGlobalScopeBase.cpp:
(WebCore::JSWorkerGlobalScopeBase::queueMicrotaskToEventLoop): Renamed from queueTaskToEventLoop.
* bindings/js/JSWorkerGlobalScopeBase.h:
* bindings/js/JSWorkletGlobalScopeBase.cpp:
* bindings/js/JSWorkletGlobalScopeBase.h:
* dom/CustomElementReactionQueue.cpp:
(WebCore::BackupElementQueueMicrotask): Deleted.
(WebCore::CustomElementReactionQueue::enqueueElementOnAppropriateElementQueue):
(WebCore::CustomElementReactionQueue::ensureBackupQueue):
* dom/CustomElementReactionQueue.h:
* dom/Document.cpp:
(WebCore::Document::finishedParsing):
* dom/DocumentStorageAccess.cpp:
(WebCore::DocumentStorageAccess::requestStorageAccess):
* dom/EventLoop.cpp:
(WebCore::EventLoop::queueMicrotask): Added.
(WebCore::EventLoop::performMicrotaskCheckpoint): Added.
(WebCore::EventLoopTaskGroup::queueMicrotaskCallback): Added.
(WebCore::EventLoopTaskGroup::queueMicrotask): Added.
(WebCore::EventLoopTaskGroup::performMicrotaskCheckpoint): Added.
* dom/EventLoop.h:
(WebCore::EventLoopTaskGroup::microtaskQueue):
* dom/Microtasks.cpp:
(WebCore::MicrotaskQueue::mainThreadQueue): Deleted.
(WebCore::MicrotaskQueue::contextQueue): Deleted.
* dom/Microtasks.h:
* dom/MutationObserver.cpp:
(WebCore::MutationObserverMicrotask): Deleted.
(WebCore::MutationObserver::queueMutationObserverCompoundMicrotask): Made this a member function
so that it can call notifyMutationObservers in its lambda.
(WebCore::MutationObserver::enqueueMutationRecord):
(WebCore::MutationObserver::enqueueSlotChangeEvent):
(WebCore::MutationObserver::setHasTransientRegistration):
* dom/MutationObserver.h:
* dom/MutationObserverRegistration.cpp:
(WebCore::MutationObserverRegistration::observedSubtreeNodeWillDetach):
* dom/WindowEventLoop.cpp:
(WebCore::WindowEventLoop::microtaskQueue):
* dom/WindowEventLoop.h:
* html/parser/HTMLDocumentParser.cpp:
(WebCore::HTMLDocumentParser::runScriptsForPausedTreeBuilder):
* html/parser/HTMLScriptRunner.cpp:
(WebCore::HTMLScriptRunner::executePendingScriptAndDispatchEvent):
(WebCore::HTMLScriptRunner::runScript):
* inspector/agents/InspectorCanvasAgent.cpp:
(WebCore::InspectorCanvasAgent::recordCanvasAction):
* testing/Internals.cpp:
(WebCore::Internals::queueMicroTask):
* workers/WorkerEventLoop.cpp:
(WebCore::WorkerEventLoop::~WorkerEventLoop):
(WebCore::WorkerEventLoop::microtaskQueue):
(WebCore::WorkerEventLoop::clearMicrotaskQueue):
* workers/WorkerEventLoop.h:
* workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::WorkerGlobalScope):
(WebCore::WorkerGlobalScope::prepareForTermination):
* workers/WorkerGlobalScope.h:
(WebCore::WorkerGlobalScope::microtaskQueue const): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (252722 => 252723)


--- trunk/Source/WebCore/ChangeLog	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/ChangeLog	2019-11-21 01:47:56 UTC (rev 252723)
@@ -1,3 +1,88 @@
+2019-11-19  Ryosuke Niwa  <[email protected]>
+
+        MicrotaskQueue should be accessed via EventLoop
+        https://bugs.webkit.org/show_bug.cgi?id=204397
+
+        Reviewed by Antti Koivisto.
+
+        This patch refactors the existing code so that a microtask is always queued via EventLoopTaskGroup.
+        It preserves all other (broken) semantics and behavior like all origins sharing a single microtask queue.
+
+        The singleton MicrotaskQueue for the main thread has been moved from MicrotaskQueue::mainThreadQueue
+        to WindowEventLoop, and an instance of MicrotaskQueue for each worker has been moved from WorkerGlobalScope
+        to WorkerEventLoop.
+
+        * animation/DocumentTimeline.cpp:
+        (WebCore::DocumentTimeline::internalUpdateAnimationsAndSendEvents):
+        * animation/WebAnimation.cpp:
+        (WebCore::WebAnimation::updateFinishedState):
+        * bindings/js/JSDOMGlobalObjectTask.cpp:
+        (WebCore::JSGlobalObjectTask::JSGlobalObjectTask):
+        * bindings/js/JSDOMWindowBase.cpp:
+        (WebCore::JSDOMWindowBase::queueMicrotaskToEventLoop): Renamed from queueTaskToEventLoop.
+        * bindings/js/JSDOMWindowBase.h:
+        * bindings/js/JSExecState.cpp:
+        (WebCore::JSExecState::didLeaveScriptContext):
+        * bindings/js/JSRemoteDOMWindowBase.cpp:
+        * bindings/js/JSWorkerGlobalScopeBase.cpp:
+        (WebCore::JSWorkerGlobalScopeBase::queueMicrotaskToEventLoop): Renamed from queueTaskToEventLoop.
+        * bindings/js/JSWorkerGlobalScopeBase.h:
+        * bindings/js/JSWorkletGlobalScopeBase.cpp:
+        * bindings/js/JSWorkletGlobalScopeBase.h:
+        * dom/CustomElementReactionQueue.cpp:
+        (WebCore::BackupElementQueueMicrotask): Deleted.
+        (WebCore::CustomElementReactionQueue::enqueueElementOnAppropriateElementQueue):
+        (WebCore::CustomElementReactionQueue::ensureBackupQueue):
+        * dom/CustomElementReactionQueue.h:
+        * dom/Document.cpp:
+        (WebCore::Document::finishedParsing):
+        * dom/DocumentStorageAccess.cpp:
+        (WebCore::DocumentStorageAccess::requestStorageAccess):
+        * dom/EventLoop.cpp:
+        (WebCore::EventLoop::queueMicrotask): Added.
+        (WebCore::EventLoop::performMicrotaskCheckpoint): Added.
+        (WebCore::EventLoopTaskGroup::queueMicrotaskCallback): Added.
+        (WebCore::EventLoopTaskGroup::queueMicrotask): Added.
+        (WebCore::EventLoopTaskGroup::performMicrotaskCheckpoint): Added.
+        * dom/EventLoop.h:
+        (WebCore::EventLoopTaskGroup::microtaskQueue):
+        * dom/Microtasks.cpp:
+        (WebCore::MicrotaskQueue::mainThreadQueue): Deleted.
+        (WebCore::MicrotaskQueue::contextQueue): Deleted.
+        * dom/Microtasks.h:
+        * dom/MutationObserver.cpp:
+        (WebCore::MutationObserverMicrotask): Deleted.
+        (WebCore::MutationObserver::queueMutationObserverCompoundMicrotask): Made this a member function
+        so that it can call notifyMutationObservers in its lambda.
+        (WebCore::MutationObserver::enqueueMutationRecord):
+        (WebCore::MutationObserver::enqueueSlotChangeEvent):
+        (WebCore::MutationObserver::setHasTransientRegistration):
+        * dom/MutationObserver.h:
+        * dom/MutationObserverRegistration.cpp:
+        (WebCore::MutationObserverRegistration::observedSubtreeNodeWillDetach):
+        * dom/WindowEventLoop.cpp:
+        (WebCore::WindowEventLoop::microtaskQueue):
+        * dom/WindowEventLoop.h:
+        * html/parser/HTMLDocumentParser.cpp:
+        (WebCore::HTMLDocumentParser::runScriptsForPausedTreeBuilder):
+        * html/parser/HTMLScriptRunner.cpp:
+        (WebCore::HTMLScriptRunner::executePendingScriptAndDispatchEvent):
+        (WebCore::HTMLScriptRunner::runScript):
+        * inspector/agents/InspectorCanvasAgent.cpp:
+        (WebCore::InspectorCanvasAgent::recordCanvasAction):
+        * testing/Internals.cpp:
+        (WebCore::Internals::queueMicroTask):
+        * workers/WorkerEventLoop.cpp:
+        (WebCore::WorkerEventLoop::~WorkerEventLoop):
+        (WebCore::WorkerEventLoop::microtaskQueue):
+        (WebCore::WorkerEventLoop::clearMicrotaskQueue):
+        * workers/WorkerEventLoop.h:
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::WorkerGlobalScope::WorkerGlobalScope):
+        (WebCore::WorkerGlobalScope::prepareForTermination):
+        * workers/WorkerGlobalScope.h:
+        (WebCore::WorkerGlobalScope::microtaskQueue const): Deleted.
+
 2019-11-20  Wenson Hsieh  <[email protected]>
 
         Unreviewed, remove an unnecessary null check after r252561

Modified: trunk/Source/WebCore/animation/DocumentTimeline.cpp (252722 => 252723)


--- trunk/Source/WebCore/animation/DocumentTimeline.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/animation/DocumentTimeline.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -32,6 +32,7 @@
 #include "DOMWindow.h"
 #include "DeclarativeAnimation.h"
 #include "Document.h"
+#include "EventLoop.h"
 #include "EventNames.h"
 #include "GraphicsLayer.h"
 #include "KeyframeEffect.h"
@@ -388,7 +389,8 @@
     removeReplacedAnimations();
 
     // 3. Perform a microtask checkpoint.
-    MicrotaskQueue::mainThreadQueue().performMicrotaskCheckpoint();
+    if (auto document = makeRefPtr(this->document()))
+        document->eventLoop().performMicrotaskCheckpoint();
 
     // 4. Let events to dispatch be a copy of doc's pending animation event queue.
     // 5. Clear doc's pending animation event queue.

Modified: trunk/Source/WebCore/animation/WebAnimation.cpp (252722 => 252723)


--- trunk/Source/WebCore/animation/WebAnimation.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/animation/WebAnimation.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -796,12 +796,14 @@
             // Otherwise, if synchronously notify is false, queue a microtask to run finish notification steps for animation unless there
             // is already a microtask queued to run those steps for animation.
             m_finishNotificationStepsMicrotaskPending = true;
-            MicrotaskQueue::mainThreadQueue().append(makeUnique<VoidMicrotask>([this, protectedThis = makeRef(*this)] () {
-                if (m_finishNotificationStepsMicrotaskPending) {
-                    m_finishNotificationStepsMicrotaskPending = false;
-                    finishNotificationSteps();
-                }
-            }));
+            if (auto* context = scriptExecutionContext()) {
+                context->eventLoop().queueMicrotask([this, protectedThis = makeRef(*this)] {
+                    if (m_finishNotificationStepsMicrotaskPending) {
+                        m_finishNotificationStepsMicrotaskPending = false;
+                        finishNotificationSteps();
+                    }
+                });
+            }
         }
     }
 

Modified: trunk/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSDOMGlobalObjectTask.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -38,7 +38,7 @@
 
 class JSGlobalObjectCallback final : public RefCounted<JSGlobalObjectCallback>, private ActiveDOMCallback {
 public:
-    static Ref<JSGlobalObjectCallback> create(JSDOMGlobalObject& globalObject, Ref<Microtask>&& task)
+    static Ref<JSGlobalObjectCallback> create(JSDOMGlobalObject& globalObject, Ref<JSC::Microtask>&& task)
     {
         return adoptRef(*new JSGlobalObjectCallback(globalObject, WTFMove(task)));
     }
@@ -64,7 +64,7 @@
     }
 
 private:
-    JSGlobalObjectCallback(JSDOMGlobalObject& globalObject, Ref<Microtask>&& task)
+    JSGlobalObjectCallback(JSDOMGlobalObject& globalObject, Ref<JSC::Microtask>&& task)
         : ActiveDOMCallback { globalObject.scriptExecutionContext() }
         , m_globalObject { globalObject.vm(), &globalObject }
         , m_task { WTFMove(task) }
@@ -72,10 +72,10 @@
     }
 
     Strong<JSDOMGlobalObject> m_globalObject;
-    Ref<Microtask> m_task;
+    Ref<JSC::Microtask> m_task;
 };
 
-JSGlobalObjectTask::JSGlobalObjectTask(JSDOMGlobalObject& globalObject, Ref<Microtask>&& task)
+JSGlobalObjectTask::JSGlobalObjectTask(JSDOMGlobalObject& globalObject, Ref<JSC::Microtask>&& task)
     : ScriptExecutionContext::Task({ })
 {
     auto callback = JSGlobalObjectCallback::create(globalObject, WTFMove(task));

Modified: trunk/Source/WebCore/bindings/js/JSDOMWindowBase.cpp (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSDOMWindowBase.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSDOMWindowBase.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -29,6 +29,7 @@
 #include "CommonVM.h"
 #include "DOMWindow.h"
 #include "Document.h"
+#include "EventLoop.h"
 #include "FetchResponse.h"
 #include "Frame.h"
 #include "InspectorController.h"
@@ -70,7 +71,7 @@
     &supportsRichSourceInfo,
     &shouldInterruptScript,
     &_javascript_RuntimeFlags,
-    &queueTaskToEventLoop,
+    &queueMicrotaskToEventLoop,
     &shouldInterruptScriptBeforeTimeout,
     &moduleLoaderImportModule,
     &moduleLoaderResolve,
@@ -204,16 +205,16 @@
     return frame->settings()._javascript_RuntimeFlags();
 }
 
-void JSDOMWindowBase::queueTaskToEventLoop(JSGlobalObject& object, Ref<JSC::Microtask>&& task)
+void JSDOMWindowBase::queueMicrotaskToEventLoop(JSGlobalObject& object, Ref<JSC::Microtask>&& task)
 {
     JSDOMWindowBase& thisObject = static_cast<JSDOMWindowBase&>(object);
 
     auto callback = JSMicrotaskCallback::create(thisObject, WTFMove(task));
-    auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(MicrotaskQueue::mainThreadQueue(), *thisObject.scriptExecutionContext(), [callback = WTFMove(callback)]() mutable {
+    auto& eventLoop = thisObject.scriptExecutionContext()->eventLoop();
+    auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(eventLoop.microtaskQueue(), *thisObject.scriptExecutionContext(), [callback = WTFMove(callback)]() mutable {
         callback->call();
     });
-
-    MicrotaskQueue::mainThreadQueue().append(WTFMove(microtask));
+    eventLoop.queueMicrotaskCallback(WTFMove(microtask));
 }
 
 void JSDOMWindowBase::willRemoveFromWindowProxy()

Modified: trunk/Source/WebCore/bindings/js/JSDOMWindowBase.h (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSDOMWindowBase.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSDOMWindowBase.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -77,7 +77,7 @@
     static bool shouldInterruptScript(const JSC::JSGlobalObject*);
     static bool shouldInterruptScriptBeforeTimeout(const JSC::JSGlobalObject*);
     static JSC::RuntimeFlags _javascript_RuntimeFlags(const JSC::JSGlobalObject*);
-    static void queueTaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
+    static void queueMicrotaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
 
     void printErrorMessage(const String&) const;
 

Modified: trunk/Source/WebCore/bindings/js/JSExecState.cpp (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSExecState.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSExecState.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "JSExecState.h"
 
+#include "EventLoop.h"
 #include "Microtasks.h"
 #include "RejectedPromiseTracker.h"
 #include "ScriptExecutionContext.h"
@@ -39,7 +40,7 @@
     ScriptExecutionContext* context = scriptExecutionContextFromExecState(lexicalGlobalObject);
     if (!context)
         return;
-    MicrotaskQueue::contextQueue(*context).performMicrotaskCheckpoint();
+    context->eventLoop().performMicrotaskCheckpoint();
     context->ensureRejectedPromiseTracker().processQueueSoon();
 }
 

Modified: trunk/Source/WebCore/bindings/js/JSRemoteDOMWindowBase.cpp (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSRemoteDOMWindowBase.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSRemoteDOMWindowBase.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -39,7 +39,7 @@
     nullptr, // shellSupportsRichSourceInfo
     nullptr, // shouldInterruptScript
     &_javascript_RuntimeFlags,
-    nullptr, // queueTaskToEventLoop
+    nullptr, // queueMicrotaskToEventLoop
     nullptr, // shouldInterruptScriptBeforeTimeout
     nullptr, // moduleLoaderImportModule
     nullptr, // moduleLoaderResolve

Modified: trunk/Source/WebCore/bindings/js/JSWorkerGlobalScopeBase.cpp (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSWorkerGlobalScopeBase.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSWorkerGlobalScopeBase.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -30,6 +30,7 @@
 
 #include "ActiveDOMCallbackMicrotask.h"
 #include "DOMWrapperWorld.h"
+#include "EventLoop.h"
 #include "JSDOMGlobalObjectTask.h"
 #include "JSDOMGuardedObject.h"
 #include "JSDedicatedWorkerGlobalScope.h"
@@ -57,7 +58,7 @@
     &supportsRichSourceInfo,
     &shouldInterruptScript,
     &_javascript_RuntimeFlags,
-    &queueTaskToEventLoop,
+    &queueMicrotaskToEventLoop,
     &shouldInterruptScriptBeforeTimeout,
     nullptr, // moduleLoaderImportModule
     nullptr, // moduleLoaderResolve
@@ -130,17 +131,16 @@
     return thisObject->m_wrapped->thread().runtimeFlags();
 }
 
-void JSWorkerGlobalScopeBase::queueTaskToEventLoop(JSGlobalObject& object, Ref<JSC::Microtask>&& task)
+void JSWorkerGlobalScopeBase::queueMicrotaskToEventLoop(JSGlobalObject& object, Ref<JSC::Microtask>&& task)
 {
     JSWorkerGlobalScopeBase& thisObject = static_cast<JSWorkerGlobalScopeBase&>(object);
 
     auto callback = JSMicrotaskCallback::create(thisObject, WTFMove(task));
     auto& context = thisObject.wrapped();
-    auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(context.microtaskQueue(), context, [callback = WTFMove(callback)]() mutable {
+    auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(context.eventLoop().microtaskQueue(), context, [callback = WTFMove(callback)]() mutable {
         callback->call();
     });
-
-    context.microtaskQueue().append(WTFMove(microtask));
+    context.eventLoop().queueMicrotaskCallback(WTFMove(microtask));
 }
 
 JSValue toJS(JSGlobalObject* lexicalGlobalObject, JSDOMGlobalObject*, WorkerGlobalScope& workerGlobalScope)

Modified: trunk/Source/WebCore/bindings/js/JSWorkerGlobalScopeBase.h (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSWorkerGlobalScopeBase.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSWorkerGlobalScopeBase.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -69,7 +69,7 @@
     static bool shouldInterruptScript(const JSC::JSGlobalObject*);
     static bool shouldInterruptScriptBeforeTimeout(const JSC::JSGlobalObject*);
     static JSC::RuntimeFlags _javascript_RuntimeFlags(const JSC::JSGlobalObject*);
-    static void queueTaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
+    static void queueMicrotaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
 
     void clearDOMGuardedObjects();
 

Modified: trunk/Source/WebCore/bindings/js/JSWorkletGlobalScopeBase.cpp (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSWorkletGlobalScopeBase.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSWorkletGlobalScopeBase.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -49,7 +49,7 @@
     &supportsRichSourceInfo,
     &shouldInterruptScript,
     &_javascript_RuntimeFlags,
-    nullptr, // queueTaskToEventLoop
+    nullptr, // queueMicrotaskToEventLoop
     &shouldInterruptScriptBeforeTimeout,
     nullptr, // moduleLoaderImportModule
     nullptr, // moduleLoaderResolve

Modified: trunk/Source/WebCore/bindings/js/JSWorkletGlobalScopeBase.h (252722 => 252723)


--- trunk/Source/WebCore/bindings/js/JSWorkletGlobalScopeBase.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/bindings/js/JSWorkletGlobalScopeBase.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -59,7 +59,7 @@
     static bool shouldInterruptScript(const JSC::JSGlobalObject*);
     static bool shouldInterruptScriptBeforeTimeout(const JSC::JSGlobalObject*);
     static JSC::RuntimeFlags _javascript_RuntimeFlags(const JSC::JSGlobalObject*);
-    static void queueTaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
+    static void queueMicrotaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
 
     void clearDOMGuardedObjects();
 

Modified: trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/CustomElementReactionQueue.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -30,6 +30,7 @@
 #include "DOMWindow.h"
 #include "Document.h"
 #include "Element.h"
+#include "EventLoop.h"
 #include "HTMLNames.h"
 #include "JSCustomElementInterface.h"
 #include "JSDOMBinding.h"
@@ -293,7 +294,7 @@
 {
     ASSERT(element.reactionQueue());
     if (!CustomElementReactionStack::s_currentProcessingStack) {
-        auto& queue = ensureBackupQueue();
+        auto& queue = ensureBackupQueue(element.document());
         queue.add(element);
         return;
     }
@@ -318,23 +319,16 @@
     m_queue = nullptr;
 }
 
-class BackupElementQueueMicrotask final : public Microtask {
-    WTF_MAKE_FAST_ALLOCATED;
-private:
-    Result run() final
-    {
-        CustomElementReactionQueue::processBackupQueue();
-        return Result::Done;
-    }
-};
-
 static bool s_processingBackupElementQueue = false;
 
-CustomElementReactionQueue::ElementQueue& CustomElementReactionQueue::ensureBackupQueue()
+// FIXME: BackupQueue must be per event loop.
+CustomElementReactionQueue::ElementQueue& CustomElementReactionQueue::ensureBackupQueue(Document& document)
 {
     if (!s_processingBackupElementQueue) {
         s_processingBackupElementQueue = true;
-        MicrotaskQueue::mainThreadQueue().append(makeUnique<BackupElementQueueMicrotask>());
+        document.eventLoop().queueMicrotask([] {
+            CustomElementReactionQueue::processBackupQueue();
+        });
     }
     return backupElementQueue();
 }

Modified: trunk/Source/WebCore/dom/CustomElementReactionQueue.h (252722 => 252723)


--- trunk/Source/WebCore/dom/CustomElementReactionQueue.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/CustomElementReactionQueue.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -80,7 +80,7 @@
 
 private:
     static void enqueueElementOnAppropriateElementQueue(Element&);
-    static ElementQueue& ensureBackupQueue();
+    static ElementQueue& ensureBackupQueue(Document&);
     static ElementQueue& backupElementQueue();
 
     Ref<JSCustomElementInterface> m_interface;

Modified: trunk/Source/WebCore/dom/Document.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/Document.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/Document.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -5793,7 +5793,7 @@
 
     if (!page() || !page()->isForSanitizingWebContent()) {
         // FIXME: Schedule a task to fire DOMContentLoaded event instead. See webkit.org/b/82931
-        MicrotaskQueue::mainThreadQueue().performMicrotaskCheckpoint();
+        eventLoop().performMicrotaskCheckpoint();
     }
 
     dispatchEvent(Event::create(eventNames().DOMContentLoadedEvent, Event::CanBubble::Yes, Event::IsCancelable::No));

Modified: trunk/Source/WebCore/dom/DocumentStorageAccess.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/DocumentStorageAccess.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/DocumentStorageAccess.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -31,11 +31,11 @@
 #include "Chrome.h"
 #include "ChromeClient.h"
 #include "Document.h"
+#include "EventLoop.h"
 #include "Frame.h"
 #include "FrameLoader.h"
 #include "FrameLoaderClient.h"
 #include "JSDOMPromiseDeferred.h"
-#include "Microtasks.h"
 #include "Page.h"
 #include "RegistrableDomain.h"
 #include "SecurityOrigin.h"
@@ -182,10 +182,10 @@
         bool shouldPreserveUserGesture = wasGranted == StorageAccessWasGranted::Yes || promptWasShown == StorageAccessPromptWasShown::No;
 
         if (shouldPreserveUserGesture) {
-            MicrotaskQueue::mainThreadQueue().append(makeUnique<VoidMicrotask>([this, weakThis] () {
+            m_document.eventLoop().queueMicrotask([this, weakThis = makeWeakPtr(*this)] {
                 if (weakThis)
                     enableTemporaryTimeUserGesture();
-            }));
+            });
         }
 
         if (wasGranted == StorageAccessWasGranted::Yes)
@@ -197,10 +197,10 @@
         }
 
         if (shouldPreserveUserGesture) {
-            MicrotaskQueue::mainThreadQueue().append(makeUnique<VoidMicrotask>([this, weakThis = WTFMove(weakThis)] () {
+            m_document.eventLoop().queueMicrotask([this, weakThis = makeWeakPtr(*this)] {
                 if (weakThis)
                     consumeTemporaryTimeUserGesture();
-            }));
+            });
         }
     });
 }

Modified: trunk/Source/WebCore/dom/EventLoop.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/EventLoop.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/EventLoop.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -26,6 +26,9 @@
 #include "config.h"
 #include "EventLoop.h"
 
+#include "ActiveDOMCallbackMicrotask.h"
+#include "Microtasks.h"
+
 namespace WebCore {
 
 void EventLoop::queueTask(std::unique_ptr<EventLoopTask>&& task)
@@ -36,6 +39,16 @@
     m_tasks.append(WTFMove(task));
 }
 
+void EventLoop::queueMicrotask(std::unique_ptr<Microtask>&& microtask)
+{
+    microtaskQueue().append(WTFMove(microtask));
+}
+
+void EventLoop::performMicrotaskCheckpoint()
+{
+    microtaskQueue().performMicrotaskCheckpoint();
+}
+
 void EventLoop::resumeGroup(EventLoopTaskGroup& group)
 {
     ASSERT(isContextThread());
@@ -120,4 +133,41 @@
     return queueTask(makeUnique<EventLoopFunctionDispatchTask>(source, *this, WTFMove(function)));
 }
 
+void EventLoopTaskGroup::queueMicrotaskCallback(std::unique_ptr<ActiveDOMCallbackMicrotask>&& microtask)
+{
+    if (m_state == State::Stopped || !m_eventLoop)
+        return;
+    m_eventLoop->queueMicrotask(WTFMove(microtask));
+}
+
+class VoidMicrotask final : public Microtask {
+public:
+    explicit VoidMicrotask(Function<void()>&& function)
+        : m_function(WTFMove(function))
+    {
+    }
+
+private:
+    Result run() final
+    {
+        m_function();
+        return Result::Done;
+    }
+
+    Function<void()> m_function;
+};
+
+void EventLoopTaskGroup::queueMicrotask(EventLoop::TaskFunction&& function)
+{
+    if (m_state == State::Stopped || !m_eventLoop)
+        return;
+    m_eventLoop->queueMicrotask(makeUnique<VoidMicrotask>(WTFMove(function)));
+}
+
+void EventLoopTaskGroup::performMicrotaskCheckpoint()
+{
+    if (m_eventLoop)
+        m_eventLoop->performMicrotaskCheckpoint();
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/dom/EventLoop.h (252722 => 252723)


--- trunk/Source/WebCore/dom/EventLoop.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/EventLoop.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -34,8 +34,11 @@
 
 namespace WebCore {
 
+class ActiveDOMCallbackMicrotask;
 class EventLoopTaskGroup;
 class EventTarget;
+class Microtask;
+class MicrotaskQueue;
 class ScriptExecutionContext;
 
 class EventLoopTask {
@@ -66,6 +69,13 @@
     typedef Function<void ()> TaskFunction;
     void queueTask(std::unique_ptr<EventLoopTask>&&);
 
+    // https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-microtask
+    void queueMicrotask(std::unique_ptr<Microtask>&&);
+
+    // https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint
+    void performMicrotaskCheckpoint();
+    virtual MicrotaskQueue& microtaskQueue() = 0;
+
     void resumeGroup(EventLoopTaskGroup&);
     void stopGroup(EventLoopTaskGroup&);
 
@@ -130,6 +140,16 @@
     void queueTask(std::unique_ptr<EventLoopTask>&&);
     WEBCORE_EXPORT void queueTask(TaskSource, EventLoop::TaskFunction&&);
 
+    // https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-microtask
+    void queueMicrotask(EventLoop::TaskFunction&&);
+    MicrotaskQueue& microtaskQueue() { return m_eventLoop->microtaskQueue(); }
+
+    // FIXME: This function and ActiveDOMCallbackMicrotask should go away.
+    WEBCORE_EXPORT void queueMicrotaskCallback(std::unique_ptr<ActiveDOMCallbackMicrotask>&&);
+
+    // https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint
+    void performMicrotaskCheckpoint();
+
 private:
     enum class State : uint8_t { Running, Suspended, Stopped };
 

Modified: trunk/Source/WebCore/dom/Microtasks.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/Microtasks.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/Microtasks.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -43,24 +43,6 @@
 
 MicrotaskQueue::~MicrotaskQueue() = default;
 
-MicrotaskQueue& MicrotaskQueue::mainThreadQueue()
-{
-    ASSERT(isMainThread());
-    static NeverDestroyed<MicrotaskQueue> queue(commonVM());
-    return queue;
-}
-
-MicrotaskQueue& MicrotaskQueue::contextQueue(ScriptExecutionContext& context)
-{
-    // While main thread has many ScriptExecutionContexts, WorkerGlobalScope and worker thread have
-    // one on one correspondence. The lifetime of MicrotaskQueue is aligned to this semantics.
-    // While main thread MicrotaskQueue is persistently held, worker's MicrotaskQueue is held by
-    // WorkerGlobalScope.
-    if (isMainThread())
-        return mainThreadQueue();
-    return downcast<WorkerGlobalScope>(context).microtaskQueue();
-}
-
 void MicrotaskQueue::append(std::unique_ptr<Microtask>&& task)
 {
     m_microtaskQueue.append(WTFMove(task));

Modified: trunk/Source/WebCore/dom/Microtasks.h (252722 => 252723)


--- trunk/Source/WebCore/dom/Microtasks.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/Microtasks.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -52,31 +52,11 @@
     void removeSelfFromQueue(MicrotaskQueue&);
 };
 
-class VoidMicrotask final : public Microtask {
-public:
-    explicit VoidMicrotask(Function<void()>&& function)
-        : m_function(WTFMove(function))
-    {
-    }
-
-private:
-    Result run() final
-    {
-        m_function();
-        return Result::Done;
-    }
-
-    Function<void()> m_function;
-};
-
 class MicrotaskQueue final {
     WTF_MAKE_FAST_ALLOCATED;
     friend NeverDestroyed<MicrotaskQueue>;
     friend class Microtask;
 public:
-    WEBCORE_EXPORT static MicrotaskQueue& mainThreadQueue();
-    WEBCORE_EXPORT static MicrotaskQueue& contextQueue(ScriptExecutionContext&);
-
     WEBCORE_EXPORT MicrotaskQueue(JSC::VM&);
     WEBCORE_EXPORT ~MicrotaskQueue();
 

Modified: trunk/Source/WebCore/dom/MutationObserver.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/MutationObserver.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/MutationObserver.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -34,6 +34,7 @@
 #include "MutationObserver.h"
 
 #include "Document.h"
+#include "EventLoop.h"
 #include "GCReachableRef.h"
 #include "HTMLSlotElement.h"
 #include "InspectorInstrumentation.h"
@@ -158,24 +159,17 @@
     return list;
 }
 
-static bool mutationObserverCompoundMicrotaskQueuedFlag;
+// This state must be per event loop.
+static bool mutationObserverCompoundMicrotaskQueuedFlag = false;
 
-class MutationObserverMicrotask final : public Microtask {
-    WTF_MAKE_FAST_ALLOCATED;
-private:
-    Result run() final
-    {
-        MutationObserver::notifyMutationObservers();
-        return Result::Done;
-    }
-};
-
-static void queueMutationObserverCompoundMicrotask()
+void MutationObserver::queueMutationObserverCompoundMicrotask(Document& document)
 {
     if (mutationObserverCompoundMicrotaskQueuedFlag)
         return;
     mutationObserverCompoundMicrotaskQueuedFlag = true;
-    MicrotaskQueue::mainThreadQueue().append(makeUnique<MutationObserverMicrotask>());
+    document.eventLoop().queueMicrotask([] {
+        notifyMutationObservers();
+    });
 }
 
 void MutationObserver::enqueueMutationRecord(Ref<MutationRecord>&& mutation)
@@ -182,11 +176,13 @@
 {
     ASSERT(isMainThread());
     ASSERT(mutation->target());
+    auto document = makeRef(mutation->target()->document());
+
     m_pendingTargets.add(*mutation->target());
     m_records.append(WTFMove(mutation));
     activeMutationObservers().add(this);
 
-    queueMutationObserverCompoundMicrotask();
+    queueMutationObserverCompoundMicrotask(document.get());
 }
 
 void MutationObserver::enqueueSlotChangeEvent(HTMLSlotElement& slot)
@@ -195,15 +191,15 @@
     ASSERT(signalSlotList().findMatching([&slot](auto& entry) { return entry.ptr() == &slot; }) == notFound);
     signalSlotList().append(slot);
 
-    queueMutationObserverCompoundMicrotask();
+    queueMutationObserverCompoundMicrotask(slot.document());
 }
 
-void MutationObserver::setHasTransientRegistration()
+void MutationObserver::setHasTransientRegistration(Document& document)
 {
     ASSERT(isMainThread());
     activeMutationObservers().add(this);
 
-    queueMutationObserverCompoundMicrotask();
+    queueMutationObserverCompoundMicrotask(document);
 }
 
 HashSet<Node*> MutationObserver::observedNodes() const

Modified: trunk/Source/WebCore/dom/MutationObserver.h (252722 => 252723)


--- trunk/Source/WebCore/dom/MutationObserver.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/MutationObserver.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -40,6 +40,7 @@
 
 namespace WebCore {
 
+class Document;
 class HTMLSlotElement;
 class MutationCallback;
 class MutationObserverRegistration;
@@ -51,7 +52,6 @@
 
 class MutationObserver final : public RefCounted<MutationObserver> {
     WTF_MAKE_ISO_ALLOCATED(MutationObserver);
-    friend class MutationObserverMicrotask;
 public:
     enum MutationType {
         ChildList = 1 << 0,
@@ -97,7 +97,7 @@
     void observationStarted(MutationObserverRegistration&);
     void observationEnded(MutationObserverRegistration&);
     void enqueueMutationRecord(Ref<MutationRecord>&&);
-    void setHasTransientRegistration();
+    void setHasTransientRegistration(Document&);
     bool canDeliver();
 
     HashSet<Node*> observedNodes() const;
@@ -110,6 +110,7 @@
     explicit MutationObserver(Ref<MutationCallback>&&);
     void deliver();
 
+    static void queueMutationObserverCompoundMicrotask(Document&);
     static void notifyMutationObservers();
     static bool validateOptions(MutationObserverOptions);
 

Modified: trunk/Source/WebCore/dom/MutationObserverRegistration.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/MutationObserverRegistration.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/MutationObserverRegistration.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -65,7 +65,7 @@
         return;
 
     node.registerTransientMutationObserver(*this);
-    m_observer->setHasTransientRegistration();
+    m_observer->setHasTransientRegistration(node.document());
 
     if (!m_transientRegistrationNodes) {
         m_transientRegistrationNodes = makeUnique<HashSet<GCReachableRef<Node>>>();

Modified: trunk/Source/WebCore/dom/WindowEventLoop.cpp (252722 => 252723)


--- trunk/Source/WebCore/dom/WindowEventLoop.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/WindowEventLoop.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -26,7 +26,9 @@
 #include "config.h"
 #include "WindowEventLoop.h"
 
+#include "CommonVM.h"
 #include "Document.h"
+#include "Microtasks.h"
 
 namespace WebCore {
 
@@ -71,4 +73,11 @@
     return isMainThread();
 }
 
+MicrotaskQueue& WindowEventLoop::microtaskQueue()
+{
+    // MicrotaskQueue must be one per event loop.
+    static NeverDestroyed<MicrotaskQueue> queue(commonVM());
+    return queue;
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/dom/WindowEventLoop.h (252722 => 252723)


--- trunk/Source/WebCore/dom/WindowEventLoop.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/dom/WindowEventLoop.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -39,7 +39,7 @@
 public:
     static Ref<WindowEventLoop> ensureForRegistrableDomain(const RegistrableDomain&);
 
-    ~WindowEventLoop();
+    virtual ~WindowEventLoop();
 
 private:
     WindowEventLoop(const RegistrableDomain&);
@@ -46,6 +46,7 @@
 
     void scheduleToRun() final;
     bool isContextThread() const final;
+    MicrotaskQueue& microtaskQueue() final;
 
     RegistrableDomain m_domain;
 };

Modified: trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp (252722 => 252723)


--- trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -31,6 +31,7 @@
 #include "CustomElementReactionQueue.h"
 #include "DocumentFragment.h"
 #include "DocumentLoader.h"
+#include "EventLoop.h"
 #include "Frame.h"
 #include "HTMLDocument.h"
 #include "HTMLParserScheduler.h"
@@ -40,7 +41,6 @@
 #include "HTMLUnknownElement.h"
 #include "JSCustomElementInterface.h"
 #include "LinkLoader.h"
-#include "Microtasks.h"
 #include "NavigationScheduler.h"
 #include "ScriptElement.h"
 #include "ThrowOnDynamicMarkupInsertionCountIncrementer.h"
@@ -216,7 +216,7 @@
             // Prevent document.open/write during reactions by allocating the incrementer before the reactions stack.
             ThrowOnDynamicMarkupInsertionCountIncrementer incrementer(*document());
 
-            MicrotaskQueue::mainThreadQueue().performMicrotaskCheckpoint();
+            document()->eventLoop().performMicrotaskCheckpoint();
 
             CustomElementReactionStack reactionStack(document()->execState());
             auto& elementInterface = constructionData->elementInterface.get();

Modified: trunk/Source/WebCore/html/parser/HTMLScriptRunner.cpp (252722 => 252723)


--- trunk/Source/WebCore/html/parser/HTMLScriptRunner.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/html/parser/HTMLScriptRunner.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -29,6 +29,7 @@
 
 #include "Element.h"
 #include "Event.h"
+#include "EventLoop.h"
 #include "EventNames.h"
 #include "Frame.h"
 #include "HTMLInputStream.h"
@@ -36,7 +37,6 @@
 #include "HTMLScriptRunnerHost.h"
 #include "IgnoreDestructiveWriteCountIncrementer.h"
 #include "InlineClassicScript.h"
-#include "Microtasks.h"
 #include "MutationObserver.h"
 #include "NestingLevelIncrementer.h"
 #include "ScriptElement.h"
@@ -106,8 +106,8 @@
     if (pendingScript.watchingForLoad())
         stopWatchingForLoad(pendingScript);
 
-    if (!isExecutingScript())
-        MicrotaskQueue::mainThreadQueue().performMicrotaskCheckpoint();
+    if (!isExecutingScript() && m_document)
+        m_document->eventLoop().performMicrotaskCheckpoint();
 
     {
         NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
@@ -241,8 +241,8 @@
     // every script element, even if it's not ready to execute yet. There's
     // unfortunately no obvious way to tell if prepareScript is going to
     // execute the script before calling it.
-    if (!isExecutingScript())
-        MicrotaskQueue::mainThreadQueue().performMicrotaskCheckpoint();
+    if (!isExecutingScript() && m_document)
+        m_document->eventLoop().performMicrotaskCheckpoint();
 
     InsertionPointRecord insertionPointRecord(m_host.inputStream());
     NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);

Modified: trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp (252722 => 252723)


--- trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -31,6 +31,7 @@
 #include "CanvasRenderingContext2D.h"
 #include "Document.h"
 #include "Element.h"
+#include "EventLoop.h"
 #include "Frame.h"
 #include "HTMLCanvasElement.h"
 #include "ImageBitmap.h"
@@ -453,8 +454,8 @@
     // covered by the initial microtask until the next frame.
     if (!inspectorCanvas->currentFrameHasData()) {
         if (auto* scriptExecutionContext = inspectorCanvas->scriptExecutionContext()) {
-            auto& queue = MicrotaskQueue::mainThreadQueue();
-            queue.append(makeUnique<ActiveDOMCallbackMicrotask>(queue, *scriptExecutionContext, [&, protectedInspectorCanvas = inspectorCanvas.copyRef()] {
+            auto& eventLoop = scriptExecutionContext->eventLoop();
+            auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(eventLoop.microtaskQueue(), *scriptExecutionContext, [&, protectedInspectorCanvas = inspectorCanvas.copyRef()] {
                 if (auto* canvasElement = protectedInspectorCanvas->canvasElement()) {
                     if (canvasElement->isDescendantOf(canvasElement->document()))
                         return;
@@ -462,7 +463,8 @@
 
                 if (canvasRenderingContext.callTracingActive())
                     didFinishRecordingCanvasFrame(canvasRenderingContext);
-            }));
+            });
+            eventLoop.queueMicrotaskCallback(WTFMove(microtask));
         }
     }
 

Modified: trunk/Source/WebCore/testing/Internals.cpp (252722 => 252723)


--- trunk/Source/WebCore/testing/Internals.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/testing/Internals.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -4327,11 +4327,12 @@
     if (!document)
         return;
 
-    auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(MicrotaskQueue::mainThreadQueue(), *document, [document, testNumber]() {
+    ScriptExecutionContext* context = document;
+    auto& eventLoop = context->eventLoop();
+    auto microtask = makeUnique<ActiveDOMCallbackMicrotask>(eventLoop.microtaskQueue(), *document, [document, testNumber]() {
         document->addConsoleMessage(MessageSource::JS, MessageLevel::Debug, makeString("MicroTask #", testNumber, " has run."));
     });
-
-    MicrotaskQueue::mainThreadQueue().append(WTFMove(microtask));
+    eventLoop.queueMicrotaskCallback(WTFMove(microtask));
 }
 
 #if ENABLE(CONTENT_FILTERING)

Modified: trunk/Source/WebCore/workers/WorkerEventLoop.cpp (252722 => 252723)


--- trunk/Source/WebCore/workers/WorkerEventLoop.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/workers/WorkerEventLoop.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "WorkerEventLoop.h"
 
+#include "Microtasks.h"
 #include "WorkerGlobalScope.h"
 #include "WorkletGlobalScope.h"
 
@@ -48,6 +49,10 @@
 {
 }
 
+WorkerEventLoop::~WorkerEventLoop()
+{
+}
+
 void WorkerEventLoop::scheduleToRun()
 {
     ASSERT(scriptExecutionContext());
@@ -61,5 +66,18 @@
     return scriptExecutionContext()->isContextThread();
 }
 
+MicrotaskQueue& WorkerEventLoop::microtaskQueue()
+{
+    ASSERT(scriptExecutionContext());
+    if (!m_microtaskQueue)
+        m_microtaskQueue = makeUnique<MicrotaskQueue>(scriptExecutionContext()->vm());
+    return *m_microtaskQueue;
+}
+
+void WorkerEventLoop::clearMicrotaskQueue()
+{
+    m_microtaskQueue = nullptr;
+}
+
 } // namespace WebCore
 

Modified: trunk/Source/WebCore/workers/WorkerEventLoop.h (252722 => 252723)


--- trunk/Source/WebCore/workers/WorkerEventLoop.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/workers/WorkerEventLoop.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -42,11 +42,19 @@
     static Ref<WorkerEventLoop> create(WorkletGlobalScope&);
 #endif
 
+    virtual ~WorkerEventLoop();
+
+    // FIXME: This should be removed once MicrotaskQueue is integrated with EventLoopTaskGroup.
+    void clearMicrotaskQueue();
+
 private:
     explicit WorkerEventLoop(ScriptExecutionContext&);
 
     void scheduleToRun() final;
     bool isContextThread() const;
+    MicrotaskQueue& microtaskQueue() final;
+
+    std::unique_ptr<MicrotaskQueue> m_microtaskQueue;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.cpp (252722 => 252723)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -68,7 +68,6 @@
     , m_thread(thread)
     , m_script(makeUnique<WorkerScriptController>(this))
     , m_inspectorController(makeUnique<WorkerInspectorController>(*this))
-    , m_microtaskQueue(makeUnique<MicrotaskQueue>(m_script->vm()))
     , m_isOnline(isOnline)
     , m_shouldBypassMainWorldContentSecurityPolicy(shouldBypassMainWorldContentSecurityPolicy)
     , m_eventQueue(*this)
@@ -143,7 +142,8 @@
     removeAllEventListeners();
 
     // MicrotaskQueue and RejectedPromiseTracker reference Heap.
-    m_microtaskQueue = nullptr;
+    if (m_eventLoop)
+        m_eventLoop->clearMicrotaskQueue();
     removeRejectedPromiseTracker();
 }
 

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.h (252722 => 252723)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.h	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.h	2019-11-21 01:47:56 UTC (rev 252723)
@@ -45,7 +45,6 @@
 class ContentSecurityPolicyResponseHeaders;
 class Crypto;
 class EventLoopTaskGroup;
-class MicrotaskQueue;
 class Performance;
 class ScheduledAction;
 class WorkerEventLoop;
@@ -90,8 +89,6 @@
 
     WorkerInspectorController& inspectorController() const { return *m_inspectorController; }
 
-    MicrotaskQueue& microtaskQueue() const { return *m_microtaskQueue; }
-
     WorkerThread& thread() const { return m_thread; }
 
     using ScriptExecutionContext::hasPendingActivity;
@@ -191,7 +188,6 @@
     WorkerThread& m_thread;
     std::unique_ptr<WorkerScriptController> m_script;
     std::unique_ptr<WorkerInspectorController> m_inspectorController;
-    std::unique_ptr<MicrotaskQueue> m_microtaskQueue;
 
     bool m_closing { false };
     bool m_isOnline;

Modified: trunk/Source/WebCore/worklets/WorkletGlobalScope.cpp (252722 => 252723)


--- trunk/Source/WebCore/worklets/WorkletGlobalScope.cpp	2019-11-21 01:25:19 UTC (rev 252722)
+++ trunk/Source/WebCore/worklets/WorkletGlobalScope.cpp	2019-11-21 01:47:56 UTC (rev 252723)
@@ -80,8 +80,10 @@
     if (m_defaultTaskGroup)
         m_defaultTaskGroup->stopAndDiscardAllTasks();
     stopActiveDOMObjects();
+    removeAllEventListeners();
+    if (m_eventLoop)
+        m_eventLoop->clearMicrotaskQueue();
     removeRejectedPromiseTracker();
-    removeAllEventListeners();
     m_script->vm().notifyNeedTermination();
     m_script = nullptr;
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to