Port ServiceWorkerContainer to the HTML5 event loop
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Nov 2019 22:36:45 +0000 (22:36 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Nov 2019 22:36:45 +0000 (22:36 +0000)
https://bugs.webkit.org/show_bug.cgi?id=203963

Reviewed by Ryosuke Niwa.

* workers/service/ServiceWorkerContainer.cpp:
(WebCore::ServiceWorkerContainer::ServiceWorkerContainer):
(WebCore::ServiceWorkerContainer::startMessages):
(WebCore::ServiceWorkerContainer::postMessage):
* workers/service/ServiceWorkerContainer.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@252206 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/workers/service/ServiceWorkerContainer.cpp
Source/WebCore/workers/service/ServiceWorkerContainer.h

index 11ebeeb..d508c26 100644 (file)
@@ -1,3 +1,16 @@
+2019-11-07  Chris Dumez  <cdumez@apple.com>
+
+        Port ServiceWorkerContainer to the HTML5 event loop
+        https://bugs.webkit.org/show_bug.cgi?id=203963
+
+        Reviewed by Ryosuke Niwa.
+
+        * workers/service/ServiceWorkerContainer.cpp:
+        (WebCore::ServiceWorkerContainer::ServiceWorkerContainer):
+        (WebCore::ServiceWorkerContainer::startMessages):
+        (WebCore::ServiceWorkerContainer::postMessage):
+        * workers/service/ServiceWorkerContainer.h:
+
 2019-11-06  Ryosuke Niwa  <rniwa@webkit.org>
 
         Integrate scroll event into HTML5 event loop
index cf3a37b..39a71d8 100644 (file)
@@ -73,13 +73,12 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(ServiceWorkerContainer);
 ServiceWorkerContainer::ServiceWorkerContainer(ScriptExecutionContext* context, NavigatorBase& navigator)
     : ActiveDOMObject(context)
     , m_navigator(navigator)
-    , m_messageQueue(GenericEventQueue::create(*this))
 {
     suspendIfNeeded();
     
     // We should queue messages until the DOMContentLoaded event has fired or startMessages() has been called.
     if (is<Document>(context) && downcast<Document>(*context).parsing())
-        m_messageQueue->setPaused(true);
+        m_shouldDeferMessageEvents = true;
 }
 
 ServiceWorkerContainer::~ServiceWorkerContainer()
@@ -314,7 +313,10 @@ void ServiceWorkerContainer::getRegistrations(Ref<DeferredPromise>&& promise)
 
 void ServiceWorkerContainer::startMessages()
 {
-    m_messageQueue->setPaused(false);
+    m_shouldDeferMessageEvents = false;
+    auto deferredMessageEvents = WTFMove(m_deferredMessageEvents);
+    for (auto& messageEvent : deferredMessageEvents)
+        queueTaskToDispatchEvent(*this, TaskSource::DOMManipulation, WTFMove(messageEvent));
 }
 
 void ServiceWorkerContainer::jobFailedWithException(ServiceWorkerJob& job, const Exception& exception)
@@ -404,8 +406,12 @@ void ServiceWorkerContainer::postMessage(MessageWithMessagePorts&& message, Serv
     MessageEventSource source = RefPtr<ServiceWorker> { ServiceWorker::getOrCreate(context, WTFMove(sourceData)) };
 
     auto messageEvent = MessageEvent::create(MessagePort::entanglePorts(context, WTFMove(message.transferredPorts)), message.message.releaseNonNull(), sourceOrigin, { }, WTFMove(source));
-    
-    m_messageQueue->enqueueEvent(WTFMove(messageEvent));
+    if (m_shouldDeferMessageEvents)
+        m_deferredMessageEvents.append(WTFMove(messageEvent));
+    else {
+        ASSERT(m_deferredMessageEvents.isEmpty());
+        queueTaskToDispatchEvent(*this, TaskSource::DOMManipulation, WTFMove(messageEvent));
+    }
 }
 
 void ServiceWorkerContainer::notifyRegistrationIsSettled(const ServiceWorkerRegistrationKey& registrationKey)
index d3a6973..8c419bd 100644 (file)
@@ -29,7 +29,6 @@
 
 #include "ActiveDOMObject.h"
 #include "EventTarget.h"
-#include "GenericEventQueue.h"
 #include "IDLTypes.h"
 #include "SWClientConnection.h"
 #include "SWServer.h"
@@ -142,7 +141,8 @@ private:
 
     uint64_t m_lastOngoingSettledRegistrationIdentifier { 0 };
     HashMap<uint64_t, ServiceWorkerRegistrationKey> m_ongoingSettledRegistrations;
-    UniqueRef<GenericEventQueue> m_messageQueue;
+    bool m_shouldDeferMessageEvents { false };
+    Vector<Ref<Event>> m_deferredMessageEvents;
 };
 
 } // namespace WebCore