Title: [251792] trunk/Source/WebCore
Revision
251792
Author
[email protected]
Date
2019-10-30 12:37:55 -0700 (Wed, 30 Oct 2019)

Log Message

Introduce WorkerEventLoop and use it in FetchBodyOwner::runNetworkTaskWhenPossible
https://bugs.webkit.org/show_bug.cgi?id=203526

Reviewed by Chris Dumez.

Added WorkerEventLoop to implement the worker event loop:
https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop-2

Each ScriptExecutionContext now returns an AbstractEventLoop so the code that may run in either
main thread or a worker thread can simply get the event loop object via ScriptExecutionContext.

For now, WorkerEventLoop also implements the worklet event loop:
https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop

WorkerEventLoop is an active DOM object since it's used by at most one ScriptExecutionContext
unlike WindowEventLoop, which can be shared by multiple documents and needs a custom logic to
suspend and resume a subset of tasks enqueued to the event loop.

No new tests since there should be no behavioral change for now.

* Modules/fetch/FetchBodyOwner.cpp:
(WebCore::FetchBodyOwner::runNetworkTaskWhenPossible): Use WorkerEventLoop.
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* dom/AbstractEventLoop.h:
(WebCore::AbstractEventLoop::suspend): Removed since it's not needed for WorkerEventLoop.
(WebCore::AbstractEventLoop::resume): Ditto.
* dom/Document.cpp:
(WebCore::Document::eventLoop): Returns AbstractEventLoop as it now overrides the abstract
member function defined in AbstractEventLoop.
* dom/Document.h:
* dom/ScriptExecutionContext.h:
* dom/WindowEventLoop.cpp:
(WebCore::WindowEventLoop::suspend):
(WebCore::WindowEventLoop::resume):
* dom/WindowEventLoop.h:
* workers/WorkerEventLoop.cpp: Added.
(WebCore::WorkerEventLoop::create): Added.
(WebCore::WorkerEventLoop::WorkerEventLoop): Added.
(WebCore::WorkerEventLoop::queueTask): Added.
(WebCore::WorkerEventLoop::hasPendingActivity const): Added.
(WebCore::WorkerEventLoop::activeDOMObjectName const): Added.
(WebCore::WorkerEventLoop::suspend): Added.
(WebCore::WorkerEventLoop::resume): Added. Unlike WindowEventLoop, we only need to check
whether m_tasks is empty or not since WorkerEventLoop is associated with at most one
ScriptExecutionContext as noted above.
(WebCore::WorkerEventLoop::stop): Added. Clear m_tasks to free memory as early as possible.
(WebCore::WorkerEventLoop::scheduleToRunIfNeeded): Added.
(WebCore::WorkerEventLoop::run): Added.
* workers/WorkerEventLoop.h: Added.
* workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::eventLoop): Added.
* workers/WorkerGlobalScope.h:
* worklets/WorkletGlobalScope.cpp:
(WebCore::WorkletGlobalScope::eventLoop): Added. It returns WorkerEventLoop for now since
there is no implementation difference.
* worklets/WorkletGlobalScope.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (251791 => 251792)


--- trunk/Source/WebCore/ChangeLog	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/ChangeLog	2019-10-30 19:37:55 UTC (rev 251792)
@@ -1,3 +1,63 @@
+2019-10-30  Ryosuke Niwa  <[email protected]>
+
+        Introduce WorkerEventLoop and use it in FetchBodyOwner::runNetworkTaskWhenPossible
+        https://bugs.webkit.org/show_bug.cgi?id=203526
+
+        Reviewed by Chris Dumez.
+
+        Added WorkerEventLoop to implement the worker event loop:
+        https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop-2
+
+        Each ScriptExecutionContext now returns an AbstractEventLoop so the code that may run in either
+        main thread or a worker thread can simply get the event loop object via ScriptExecutionContext.
+
+        For now, WorkerEventLoop also implements the worklet event loop:
+        https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
+
+        WorkerEventLoop is an active DOM object since it's used by at most one ScriptExecutionContext
+        unlike WindowEventLoop, which can be shared by multiple documents and needs a custom logic to
+        suspend and resume a subset of tasks enqueued to the event loop.
+
+        No new tests since there should be no behavioral change for now.
+
+        * Modules/fetch/FetchBodyOwner.cpp:
+        (WebCore::FetchBodyOwner::runNetworkTaskWhenPossible): Use WorkerEventLoop.
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * dom/AbstractEventLoop.h:
+        (WebCore::AbstractEventLoop::suspend): Removed since it's not needed for WorkerEventLoop.
+        (WebCore::AbstractEventLoop::resume): Ditto.
+        * dom/Document.cpp:
+        (WebCore::Document::eventLoop): Returns AbstractEventLoop as it now overrides the abstract
+        member function defined in AbstractEventLoop.
+        * dom/Document.h:
+        * dom/ScriptExecutionContext.h:
+        * dom/WindowEventLoop.cpp:
+        (WebCore::WindowEventLoop::suspend):
+        (WebCore::WindowEventLoop::resume):
+        * dom/WindowEventLoop.h:
+        * workers/WorkerEventLoop.cpp: Added.
+        (WebCore::WorkerEventLoop::create): Added.
+        (WebCore::WorkerEventLoop::WorkerEventLoop): Added.
+        (WebCore::WorkerEventLoop::queueTask): Added.
+        (WebCore::WorkerEventLoop::hasPendingActivity const): Added.
+        (WebCore::WorkerEventLoop::activeDOMObjectName const): Added.
+        (WebCore::WorkerEventLoop::suspend): Added.
+        (WebCore::WorkerEventLoop::resume): Added. Unlike WindowEventLoop, we only need to check
+        whether m_tasks is empty or not since WorkerEventLoop is associated with at most one
+        ScriptExecutionContext as noted above.
+        (WebCore::WorkerEventLoop::stop): Added. Clear m_tasks to free memory as early as possible.
+        (WebCore::WorkerEventLoop::scheduleToRunIfNeeded): Added.
+        (WebCore::WorkerEventLoop::run): Added.
+        * workers/WorkerEventLoop.h: Added.
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::WorkerGlobalScope::eventLoop): Added.
+        * workers/WorkerGlobalScope.h:
+        * worklets/WorkletGlobalScope.cpp:
+        (WebCore::WorkletGlobalScope::eventLoop): Added. It returns WorkerEventLoop for now since
+        there is no implementation difference.
+        * worklets/WorkletGlobalScope.h:
+
 2019-10-30  Antoine Quint  <[email protected]>
 
         [ Mac Debug ] (r251706) webanimations/empty-keyframes-crash.html is crashing

Modified: trunk/Source/WebCore/Modules/fetch/FetchBodyOwner.cpp (251791 => 251792)


--- trunk/Source/WebCore/Modules/fetch/FetchBodyOwner.cpp	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/Modules/fetch/FetchBodyOwner.cpp	2019-10-30 19:37:55 UTC (rev 251792)
@@ -45,15 +45,9 @@
     if (!context || !scriptExecutionContext()->activeDOMObjectsAreSuspended())
         return task();
 
-    if (is<Document>(*context)) {
-        downcast<Document>(*context).eventLoop().queueTask(TaskSource::Networking, *context, [pendingActivity = makePendingActivity(*this), task = WTFMove(task)] {
-            task();
-        });
-    } else {
-        context->postTask([pendingActivity = makePendingActivity(*this), task = WTFMove(task)](ScriptExecutionContext&) {
-            task();
-        });
-    }
+    context->eventLoop().queueTask(TaskSource::Networking, *context, [pendingActivity = makePendingActivity(*this), task = WTFMove(task)] {
+        task();
+    });
 }
 
 FetchBodyOwner::FetchBodyOwner(ScriptExecutionContext& context, Optional<FetchBody>&& body, Ref<FetchHeaders>&& headers)

Modified: trunk/Source/WebCore/Sources.txt (251791 => 251792)


--- trunk/Source/WebCore/Sources.txt	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/Sources.txt	2019-10-30 19:37:55 UTC (rev 251792)
@@ -2500,6 +2500,7 @@
 workers/DedicatedWorkerThread.cpp
 workers/Worker.cpp
 workers/WorkerConsoleClient.cpp
+workers/WorkerEventLoop.cpp
 workers/WorkerEventQueue.cpp
 workers/WorkerGlobalScope.cpp
 workers/WorkerInspectorProxy.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (251791 => 251792)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2019-10-30 19:37:55 UTC (rev 251792)
@@ -11101,6 +11101,9 @@
 		9B098BE01F3D673D002DD562 /* JSDataTransferItemList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDataTransferItemList.h; sourceTree = "<group>"; };
 		9B098BE61F3D6AF6002DD562 /* JSStringCallback.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSStringCallback.cpp; sourceTree = "<group>"; };
 		9B098BE71F3D6AF6002DD562 /* JSStringCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSStringCallback.h; sourceTree = "<group>"; };
+		9B0ABCA123679AB300B45085 /* AbstractEventLoop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AbstractEventLoop.h; sourceTree = "<group>"; };
+		9B0ABCA323679ACF00B45085 /* WorkerEventLoop.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkerEventLoop.cpp; sourceTree = "<group>"; };
+		9B0ABCA423679ACF00B45085 /* WorkerEventLoop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerEventLoop.h; sourceTree = "<group>"; };
 		9B0FE8731D9E02DF004A8ACB /* DocumentOrShadowRoot.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = DocumentOrShadowRoot.idl; sourceTree = "<group>"; };
 		9B13257B1F3D2ABA00DAAB69 /* DataTransferItemList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DataTransferItemList.cpp; sourceTree = "<group>"; };
 		9B19B67E1B964E5200348745 /* ShadowRoot.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ShadowRoot.idl; sourceTree = "<group>"; };
@@ -11136,7 +11139,6 @@
 		9B742608234BEA6E00DB52E9 /* IdleDeadline.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IdleDeadline.h; sourceTree = "<group>"; };
 		9B742609234BEA6E00DB52E9 /* IdleDeadline.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = IdleDeadline.cpp; sourceTree = "<group>"; };
 		9B74260A234BEA6E00DB52E9 /* IdleDeadline.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = IdleDeadline.idl; sourceTree = "<group>"; };
-		9B8205A5235929B100C3BF40 /* AbstractEventLoop.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AbstractEventLoop.cpp; sourceTree = "<group>"; };
 		9B85530520E733B5009EEF4F /* EventTargetFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = EventTargetFactory.cpp; path = DerivedSources/WebCore/EventTargetFactory.cpp; sourceTree = BUILT_PRODUCTS_DIR; };
 		9B864F00234C028A006D10EB /* IdleRequestOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IdleRequestOptions.h; sourceTree = "<group>"; };
 		9B864F01234C028A006D10EB /* IdleRequestOptions.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = IdleRequestOptions.idl; sourceTree = "<group>"; };
@@ -17765,6 +17767,8 @@
 				A55639CF1C6EFD5900806D8E /* WorkerConsoleClient.cpp */,
 				A55639D01C6EFD5900806D8E /* WorkerConsoleClient.h */,
 				A52B349C1FA416F8008B6246 /* WorkerDebuggerProxy.h */,
+				9B0ABCA323679ACF00B45085 /* WorkerEventLoop.cpp */,
+				9B0ABCA423679ACF00B45085 /* WorkerEventLoop.h */,
 				A3E2642E14748991005A8588 /* WorkerEventQueue.cpp */,
 				A3E2642F14748991005A8588 /* WorkerEventQueue.h */,
 				2E4346360F546A8200B0F1BA /* WorkerGlobalScope.cpp */,
@@ -22116,8 +22120,8 @@
 				A1F76B0E1F44C0CF0014C318 /* paymentrequest */,
 				1DEF06A2233C32DB00EE228D /* pictureinpicture */,
 				072AE1DE183C0513000A5988 /* plugins */,
+				89F60B08157F68350075E157 /* quota */,
 				CDC312E022FCD0B0001204EC /* remoteplayback */,
-				89F60B08157F68350075E157 /* quota */,
 				AA2A5AB716A485A400975A25 /* speech */,
 				41A023EA1A39DB7900F722CF /* streams */,
 				FD315FA212B025B100C1A359 /* webaudio */,
@@ -27715,7 +27719,7 @@
 				7CD0E2BD1F80A56E0016A4CE /* AbortSignal.cpp */,
 				7CD0E2BC1F80A56E0016A4CE /* AbortSignal.h */,
 				7CD0E2BE1F80A56E0016A4CE /* AbortSignal.idl */,
-				9B8205A5235929B100C3BF40 /* AbstractEventLoop.cpp */,
+				9B0ABCA123679AB300B45085 /* AbstractEventLoop.h */,
 				7C5222971E1DAE16002CB8F7 /* ActiveDOMCallback.cpp */,
 				7C5222981E1DAE16002CB8F7 /* ActiveDOMCallback.h */,
 				7CD0BA021B8F79C9005CEBBE /* ActiveDOMCallbackMicrotask.cpp */,
@@ -29149,7 +29153,6 @@
 				E1C415DA0F655D6F0092D2FB /* CrossOriginPreflightResultCache.h in Headers */,
 				6B1F48112298A37E00DE8B82 /* CrossSiteNavigationDataTransfer.h in Headers */,
 				E169803D1133542D00894115 /* CRuntimeObject.h in Headers */,
-				CDC312E922FCD7C0001204EC /* RemotePlayback.h in Headers */,
 				975CA28B130365F800E99AD9 /* Crypto.h in Headers */,
 				7C9ACA981F3CCDAD00F3AA09 /* CryptoAesKeyAlgorithm.h in Headers */,
 				E172AF6E180F24C600FBADB9 /* CryptoAlgorithm.h in Headers */,
@@ -29848,7 +29851,6 @@
 				A8EA79FA0A1916DF00A8EF5F /* HTMLDirectoryElement.h in Headers */,
 				A8EA7CB70A192B9C00A8EF5F /* HTMLDivElement.h in Headers */,
 				A8EA79F70A1916DF00A8EF5F /* HTMLDListElement.h in Headers */,
-				CDC312EB22FCD7C9001204EC /* RemotePlaybackAvailabilityCallback.h in Headers */,
 				93F198E508245E59001E9ABC /* HTMLDocument.h in Headers */,
 				977B3867122883E900B81FF8 /* HTMLDocumentParser.h in Headers */,
 				93F198E608245E59001E9ABC /* HTMLElement.h in Headers */,
@@ -29891,6 +29893,7 @@
 				E44613A50CD6331000FADA75 /* HTMLMediaElement.h in Headers */,
 				CD5209E61B0BD9E10077184E /* HTMLMediaElementEnums.h in Headers */,
 				C937FE8D1B1F6821008ECC5D /* HTMLMediaElementMediaSession.h in Headers */,
+				CDC312EA22FCD7C6001204EC /* HTMLMediaElementRemotePlayback.h in Headers */,
 				A8EA79F40A1916DF00A8EF5F /* HTMLMenuElement.h in Headers */,
 				2F31E0751FA3B62B00E059BA /* HTMLMenuItemElement.h in Headers */,
 				2BE8E2C712A589EC00FAD550 /* HTMLMetaCharsetParser.h in Headers */,
@@ -31628,6 +31631,8 @@
 				CD8ACA891D237AA200ECC59E /* RemoteCommandListenerMac.h in Headers */,
 				46B95199207D634D00A7D2DD /* RemoteDOMWindow.h in Headers */,
 				46B9519A207D635400A7D2DD /* RemoteFrame.h in Headers */,
+				CDC312E922FCD7C0001204EC /* RemotePlayback.h in Headers */,
+				CDC312EB22FCD7C9001204EC /* RemotePlaybackAvailabilityCallback.h in Headers */,
 				073A15542177A42600EA08F2 /* RemoteVideoSample.h in Headers */,
 				D06C0D8F0CFD11460065F43F /* RemoveFormatCommand.h in Headers */,
 				93309E05099E64920056E581 /* RemoveNodeCommand.h in Headers */,
@@ -31894,7 +31899,6 @@
 				FD31603612B0267600C1A359 /* ScriptProcessorNode.h in Headers */,
 				8A413AE01207BBA50082016E /* ScriptRunner.h in Headers */,
 				934CC10A0EDB223900A658F2 /* ScriptSourceCode.h in Headers */,
-				CDC312EA22FCD7C6001204EC /* HTMLMediaElementRemotePlayback.h in Headers */,
 				41C760B10EDE03D300C1655F /* ScriptState.h in Headers */,
 				228C284510D82500009D0D0E /* ScriptWrappable.h in Headers */,
 				1400D7A817136EA70077CE05 /* ScriptWrappableInlines.h in Headers */,

Modified: trunk/Source/WebCore/dom/AbstractEventLoop.h (251791 => 251792)


--- trunk/Source/WebCore/dom/AbstractEventLoop.h	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/dom/AbstractEventLoop.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -46,9 +46,6 @@
     typedef WTF::Function<void ()> TaskFunction;
     virtual void queueTask(TaskSource, ScriptExecutionContext&, TaskFunction&&) = 0;
 
-    virtual void suspend(ScriptExecutionContext&) = 0;
-    virtual void resume(ScriptExecutionContext&) = 0;
-
 protected:
     AbstractEventLoop() = default;
 };

Modified: trunk/Source/WebCore/dom/Document.cpp (251791 => 251792)


--- trunk/Source/WebCore/dom/Document.cpp	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/dom/Document.cpp	2019-10-30 19:37:55 UTC (rev 251792)
@@ -6128,11 +6128,12 @@
         task.performTask(*this);
 }
 
-WindowEventLoop& Document::eventLoop()
+AbstractEventLoop& Document::eventLoop()
 {
+    ASSERT(isMainThread());
     if (!m_eventLoop) {
         if (m_contextDocument)
-            m_eventLoop = &m_contextDocument->eventLoop();
+            m_eventLoop = m_contextDocument->m_eventLoop;
         else // FIXME: Documents of similar origin should share the same event loop.
             m_eventLoop = WindowEventLoop::create();
     }

Modified: trunk/Source/WebCore/dom/Document.h (251791 => 251792)


--- trunk/Source/WebCore/dom/Document.h	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/dom/Document.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -88,6 +88,7 @@
 
 namespace WebCore {
 
+class AbstractEventLoop;
 class ApplicationStateChangeListener;
 class AXObjectCache;
 class Attr;
@@ -128,6 +129,7 @@
 class Frame;
 class FrameView;
 class FullscreenManager;
+class GPUCanvasContext;
 class HTMLAllCollection;
 class HTMLBodyElement;
 class HTMLCanvasElement;
@@ -205,7 +207,6 @@
 class WebAnimation;
 class WebGL2RenderingContext;
 class WebGLRenderingContext;
-class GPUCanvasContext;
 class WindowEventLoop;
 class WindowProxy;
 class Worklet;
@@ -1061,7 +1062,7 @@
 
     WEBCORE_EXPORT void postTask(Task&&) final; // Executes the task on context's thread asynchronously.
 
-    WindowEventLoop& eventLoop();
+    AbstractEventLoop& eventLoop() final;
     WindowEventLoop* eventLoopIfExists() { return m_eventLoop.get(); }
 
     ScriptedAnimationController* scriptedAnimationController() { return m_scriptedAnimationController.get(); }

Modified: trunk/Source/WebCore/dom/ScriptExecutionContext.h (251791 => 251792)


--- trunk/Source/WebCore/dom/ScriptExecutionContext.h	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/dom/ScriptExecutionContext.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -54,6 +54,7 @@
 
 namespace WebCore {
 
+class AbstractEventLoop;
 class CachedScript;
 class DatabaseContext;
 class EventQueue;
@@ -89,6 +90,8 @@
     virtual bool isContextThread() const { return true; }
     virtual bool isJSExecutionForbidden() const = 0;
 
+    virtual AbstractEventLoop& eventLoop() = 0;
+
     virtual const URL& url() const = 0;
     virtual URL completeURL(const String& url) const = 0;
 

Modified: trunk/Source/WebCore/dom/WindowEventLoop.cpp (251791 => 251792)


--- trunk/Source/WebCore/dom/WindowEventLoop.cpp	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/dom/WindowEventLoop.cpp	2019-10-30 19:37:55 UTC (rev 251792)
@@ -43,16 +43,14 @@
     m_tasks.append(Task { source, WTFMove(task), downcast<Document>(context).identifier() });
 }
 
-void WindowEventLoop::suspend(ScriptExecutionContext&)
+void WindowEventLoop::suspend(Document&)
 {
     ASSERT(isMainThread());
 }
 
-void WindowEventLoop::resume(ScriptExecutionContext& context)
+void WindowEventLoop::resume(Document& document)
 {
     ASSERT(isMainThread());
-    ASSERT(is<Document>(context));
-    auto& document = downcast<Document>(context);
     if (!m_documentIdentifiersForSuspendedTasks.contains(document.identifier()))
         return;
     scheduleToRunIfNeeded();

Modified: trunk/Source/WebCore/dom/WindowEventLoop.h (251791 => 251792)


--- trunk/Source/WebCore/dom/WindowEventLoop.h	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/dom/WindowEventLoop.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -40,8 +40,8 @@
 
     void queueTask(TaskSource, ScriptExecutionContext&, TaskFunction&&) override;
 
-    void suspend(ScriptExecutionContext&) override;
-    void resume(ScriptExecutionContext&) override;
+    void suspend(Document&);
+    void resume(Document&);
 
 private:
     WindowEventLoop() = default;

Copied: trunk/Source/WebCore/workers/WorkerEventLoop.cpp (from rev 251791, trunk/Source/WebCore/dom/WindowEventLoop.cpp) (0 => 251792)


--- trunk/Source/WebCore/workers/WorkerEventLoop.cpp	                        (rev 0)
+++ trunk/Source/WebCore/workers/WorkerEventLoop.cpp	2019-10-30 19:37:55 UTC (rev 251792)
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WorkerEventLoop.h"
+
+#include "WorkerGlobalScope.h"
+#include "WorkletGlobalScope.h"
+
+namespace WebCore {
+
+Ref<WorkerEventLoop> WorkerEventLoop::create(WorkerGlobalScope& context)
+{
+    return adoptRef(*new WorkerEventLoop(context));
+}
+
+#if ENABLE(CSS_PAINTING_API)
+Ref<WorkerEventLoop> WorkerEventLoop::create(WorkletGlobalScope& context)
+{
+    return adoptRef(*new WorkerEventLoop(context));
+}
+#endif
+
+WorkerEventLoop::WorkerEventLoop(ScriptExecutionContext& context)
+    : ActiveDOMObject(&context)
+{
+}
+
+void WorkerEventLoop::queueTask(TaskSource source, ScriptExecutionContext& context, TaskFunction&& function)
+{
+    if (!scriptExecutionContext())
+        return;
+    ASSERT(scriptExecutionContext()->isContextThread());
+    ASSERT_UNUSED(context, scriptExecutionContext() == &context);
+    m_tasks.append({ source, WTFMove(function) });
+    scheduleToRunIfNeeded();
+}
+
+const char* WorkerEventLoop::activeDOMObjectName() const
+{
+    return "WorkerEventLoop";
+}
+
+void WorkerEventLoop::suspend(ReasonForSuspension)
+{
+    ASSERT_NOT_REACHED();
+}
+
+void WorkerEventLoop::resume()
+{
+    ASSERT_NOT_REACHED();
+}
+
+void WorkerEventLoop::stop()
+{
+    m_tasks.clear();
+}
+
+void WorkerEventLoop::scheduleToRunIfNeeded()
+{
+    auto* context = scriptExecutionContext();
+    ASSERT(context);
+    if (m_isScheduledToRun || m_tasks.isEmpty())
+        return;
+
+    m_isScheduledToRun = true;
+    context->postTask([eventLoop = makeRef(*this)] (ScriptExecutionContext&) {
+        eventLoop->m_isScheduledToRun = false;
+        eventLoop->run();
+    });
+}
+
+void WorkerEventLoop::run()
+{
+    auto* context = scriptExecutionContext();
+    if (!context || context->activeDOMObjectsAreStopped() || context->activeDOMObjectsAreSuspended())
+        return;
+    for (auto& task : m_tasks)
+        task.task();
+}
+
+} // namespace WebCore
+

Copied: trunk/Source/WebCore/workers/WorkerEventLoop.h (from rev 251791, trunk/Source/WebCore/dom/WindowEventLoop.h) (0 => 251792)


--- trunk/Source/WebCore/workers/WorkerEventLoop.h	                        (rev 0)
+++ trunk/Source/WebCore/workers/WorkerEventLoop.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "AbstractEventLoop.h"
+#include "ActiveDOMObject.h"
+
+namespace WebCore {
+
+class WorkerGlobalScope;
+class WorkletGlobalScope;
+
+class WorkerEventLoop final : public AbstractEventLoop, private ActiveDOMObject {
+public:
+    // Explicitly take WorkerGlobalScope and WorkletGlobalScope for documentation purposes.
+    static Ref<WorkerEventLoop> create(WorkerGlobalScope&);
+
+#if ENABLE(CSS_PAINTING_API)
+    static Ref<WorkerEventLoop> create(WorkletGlobalScope&);
+#endif
+
+    void queueTask(TaskSource, ScriptExecutionContext&, TaskFunction&&) override;
+
+private:
+    explicit WorkerEventLoop(ScriptExecutionContext&);
+
+    void scheduleToRunIfNeeded();
+    void run();
+
+    // ActiveDOMObject;
+    const char* activeDOMObjectName() const override;
+    void suspend(ReasonForSuspension) override;
+    void resume() override;
+    void stop() override;
+
+    struct Task {
+        TaskSource source;
+        TaskFunction task;
+    };
+
+    Vector<Task> m_tasks;
+    bool m_isScheduledToRun { false };
+};
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.cpp (251791 => 251792)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2019-10-30 19:37:55 UTC (rev 251792)
@@ -42,6 +42,7 @@
 #include "SecurityOriginPolicy.h"
 #include "ServiceWorkerGlobalScope.h"
 #include "SocketProvider.h"
+#include "WorkerEventLoop.h"
 #include "WorkerInspectorController.h"
 #include "WorkerLoaderProxy.h"
 #include "WorkerLocation.h"
@@ -104,6 +105,14 @@
     thread().workerReportingProxy().workerGlobalScopeDestroyed();
 }
 
+AbstractEventLoop& WorkerGlobalScope::eventLoop()
+{
+    ASSERT(isContextThread());
+    if (!m_eventLoop)
+        m_eventLoop = WorkerEventLoop::create(*this);
+    return *m_eventLoop;
+}
+
 String WorkerGlobalScope::origin() const
 {
     auto* securityOrigin = this->securityOrigin();

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.h (251791 => 251792)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.h	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -42,11 +42,13 @@
 
 namespace WebCore {
 
+class AbstractEventLoop;
 class ContentSecurityPolicyResponseHeaders;
 class Crypto;
 class MicrotaskQueue;
 class Performance;
 class ScheduledAction;
+class WorkerEventLoop;
 class WorkerInspectorController;
 class WorkerLocation;
 class WorkerNavigator;
@@ -65,6 +67,8 @@
     virtual bool isDedicatedWorkerGlobalScope() const { return false; }
     virtual bool isServiceWorkerGlobalScope() const { return false; }
 
+    AbstractEventLoop& eventLoop() final;
+
     const URL& url() const final { return m_url; }
     String origin() const final;
     const String& identifier() const { return m_identifier; }
@@ -191,6 +195,8 @@
     bool m_isOnline;
     bool m_shouldBypassMainWorldContentSecurityPolicy;
 
+    RefPtr<WorkerEventLoop> m_eventLoop;
+
     mutable WorkerEventQueue m_eventQueue;
 
     Ref<SecurityOrigin> m_topOrigin;

Modified: trunk/Source/WebCore/worklets/WorkletGlobalScope.cpp (251791 => 251792)


--- trunk/Source/WebCore/worklets/WorkletGlobalScope.cpp	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/worklets/WorkletGlobalScope.cpp	2019-10-30 19:37:55 UTC (rev 251792)
@@ -35,6 +35,7 @@
 #include "PageConsoleClient.h"
 #include "SecurityOriginPolicy.h"
 #include "Settings.h"
+#include "WorkerEventLoop.h"
 #include "WorkletScriptController.h"
 #include <_javascript_Core/Exception.h>
 #include <_javascript_Core/JSLock.h>
@@ -89,6 +90,13 @@
     return scopes;
 }
 
+AbstractEventLoop& WorkletGlobalScope::eventLoop()
+{
+    if (!m_eventLoop)
+        m_eventLoop = WorkerEventLoop::create(*this);
+    return *m_eventLoop;
+}
+
 String WorkletGlobalScope::origin() const
 {
     return m_topOrigin->toString();

Modified: trunk/Source/WebCore/worklets/WorkletGlobalScope.h (251791 => 251792)


--- trunk/Source/WebCore/worklets/WorkletGlobalScope.h	2019-10-30 19:11:15 UTC (rev 251791)
+++ trunk/Source/WebCore/worklets/WorkletGlobalScope.h	2019-10-30 19:37:55 UTC (rev 251792)
@@ -28,11 +28,11 @@
 
 #if ENABLE(CSS_PAINTING_API)
 
-#include "Document.h"
 #include "EventTarget.h"
 #include "ExceptionOr.h"
 #include "ScriptExecutionContext.h"
 #include "ScriptSourceCode.h"
+#include "WorkerEventLoop.h"
 #include "WorkerEventQueue.h"
 #include <_javascript_Core/ConsoleMessage.h>
 #include <_javascript_Core/RuntimeFlags.h>
@@ -41,8 +41,11 @@
 #include <wtf/WeakPtr.h>
 
 namespace WebCore {
+
+class AbstractEventLoop;
+class Document;
+class WorkerEventLoop;
 class WorkletScriptController;
-class ScriptSourceCode;
 
 enum WorkletGlobalScopeIdentifierType { };
 using WorkletGlobalScopeIdentifier = ObjectIdentifier<WorkletGlobalScopeIdentifierType>;
@@ -57,6 +60,8 @@
 
     virtual bool isPaintWorkletGlobalScope() const { return false; }
 
+    AbstractEventLoop& eventLoop() final;
+
     const URL& url() const final { return m_code.url(); }
     String origin() const final;
 
@@ -129,6 +134,8 @@
 
     Ref<SecurityOrigin> m_topOrigin;
 
+    RefPtr<WorkerEventLoop> m_eventLoop;
+
     // FIXME: This is not implemented properly, it just satisfies the compiler.
     // https://bugs.webkit.org/show_bug.cgi?id=191136
     mutable WorkerEventQueue m_eventQueue;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to