Title: [225403] trunk
Revision
225403
Author
beid...@apple.com
Date
2017-12-01 11:45:41 -0800 (Fri, 01 Dec 2017)

Log Message

Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
https://bugs.webkit.org/show_bug.cgi?id=180170

Reviewed by Chris Dumez.

Source/WebCore:

Test: http/tests/workers/service/postmessage-after-terminate.https.html

* dom/ActiveDOMObject.cpp:
(WebCore::ActiveDOMObject::~ActiveDOMObject):

* testing/Internals.cpp:
(WebCore::Internals::terminateServiceWorker):
* testing/Internals.h:
* testing/Internals.idl:

* workers/service/SWClientConnection.h:

* workers/service/ServiceWorker.idl:

* workers/service/context/SWContextManager.cpp:
(WebCore::SWContextManager::postMessageToServiceWorkerGlobalScope):
(WebCore::SWContextManager::terminateWorker):
* workers/service/context/SWContextManager.h:

* workers/service/context/ServiceWorkerThreadProxy.h:

* workers/service/server/SWServer.cpp:
(WebCore::SWServer::workerByID const):
(WebCore::SWServer::Connection::syncTerminateWorker):
(WebCore::SWServer::installContextData):
(WebCore::SWServer::invokeRunServiceWorker):
(WebCore::SWServer::terminateWorker):
(WebCore::SWServer::syncTerminateWorker):
(WebCore::SWServer::terminateWorkerInternal):
(WebCore::SWServer::workerContextTerminated):
* workers/service/server/SWServer.h:
(WebCore::SWServer::workerByID const): Deleted.

* workers/service/server/SWServerToContextConnection.h:

* workers/service/server/SWServerWorker.cpp:
(WebCore::SWServerWorker::contextData const):
* workers/service/server/SWServerWorker.h:
(WebCore::SWServerWorker::isRunning const):
(WebCore::SWServerWorker::isTerminating const):
(WebCore::SWServerWorker::setState):
(WebCore::SWServerWorker::server):

Source/WebKit:

* StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
(WebKit::WebSWServerConnection::startFetch):
(WebKit::WebSWServerConnection::postMessageToServiceWorkerGlobalScope):
* StorageProcess/ServiceWorker/WebSWServerConnection.h:
* StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:

* StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp:
(WebKit::WebSWServerToContextConnection::syncTerminateWorker):
* StorageProcess/ServiceWorker/WebSWServerToContextConnection.h:

* StorageProcess/StorageToWebProcessConnection.cpp:
(WebKit::StorageToWebProcessConnection::didReceiveSyncMessage):

* WebProcess/Storage/WebSWClientConnection.cpp:
(WebKit::WebSWClientConnection::syncTerminateWorker):
* WebProcess/Storage/WebSWClientConnection.h:

* WebProcess/Storage/WebSWContextManagerConnection.cpp:
(WebKit::WebSWContextManagerConnection::terminateWorker):
(WebKit::WebSWContextManagerConnection::syncTerminateWorker):
* WebProcess/Storage/WebSWContextManagerConnection.h:
* WebProcess/Storage/WebSWContextManagerConnection.messages.in:

* WebProcess/Storage/WebToStorageProcessConnection.cpp:
(WebKit::WebToStorageProcessConnection::didReceiveSyncMessage):
* WebProcess/Storage/WebToStorageProcessConnection.h:

Source/WTF:

* wtf/CompletionHandler.h:
(WTF::CompletionHandler<Out):

LayoutTests:

* http/tests/workers/service/postmessage-after-terminate.https-expected.txt: Added.
* http/tests/workers/service/postmessage-after-terminate.https.html: Added.
* http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js:
(then):
* http/tests/workers/service/resources/postmessage-after-terminate.js: Copied from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js.
(then):
* http/tests/workers/service/resources/postmessage-echo-worker.js: Renamed from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js.

Modified Paths

Added Paths

Removed Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (225402 => 225403)


--- trunk/LayoutTests/ChangeLog	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/LayoutTests/ChangeLog	2017-12-01 19:45:41 UTC (rev 225403)
@@ -1,3 +1,18 @@
+2017-12-01  Brady Eidson  <beid...@apple.com>
+
+        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
+        https://bugs.webkit.org/show_bug.cgi?id=180170
+
+        Reviewed by Chris Dumez.
+
+        * http/tests/workers/service/postmessage-after-terminate.https-expected.txt: Added.
+        * http/tests/workers/service/postmessage-after-terminate.https.html: Added.
+        * http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js:
+        (then):
+        * http/tests/workers/service/resources/postmessage-after-terminate.js: Copied from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js.
+        (then):
+        * http/tests/workers/service/resources/postmessage-echo-worker.js: Renamed from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js.
+
 2017-12-01  Zalan Bujtas  <za...@apple.com>
 
         Nullptr deref in WebCore::RenderTableCaption::containingBlockLogicalWidthForContent

Added: trunk/LayoutTests/http/tests/workers/service/postmessage-after-terminate.https-expected.txt (0 => 225403)


--- trunk/LayoutTests/http/tests/workers/service/postmessage-after-terminate.https-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/postmessage-after-terminate.https-expected.txt	2017-12-01 19:45:41 UTC (rev 225403)
@@ -0,0 +1,5 @@
+PASS: Client received message from service worker, origin: https://127.0.0.1:8443
+PASS: Service worker received message 'Message 1' from origin 'https://127.0.0.1:8443'
+PASS: Client received message from service worker, origin: https://127.0.0.1:8443
+PASS: Service worker received message 'Message 2' from origin 'https://127.0.0.1:8443'
+

Added: trunk/LayoutTests/http/tests/workers/service/postmessage-after-terminate.https.html (0 => 225403)


--- trunk/LayoutTests/http/tests/workers/service/postmessage-after-terminate.https.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/postmessage-after-terminate.https.html	2017-12-01 19:45:41 UTC (rev 225403)
@@ -0,0 +1,9 @@
+<html>
+<head>
+<script src=""
+</head>
+<body>
+
+<script src=""
+</body>
+</html>

Deleted: trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js (225402 => 225403)


--- trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js	2017-12-01 19:45:41 UTC (rev 225403)
@@ -1,15 +0,0 @@
-var client = null;
-self.addEventListener("message", (event) => {
-    if (!client) {
-        client = event.source;
-        if (!(client instanceof WindowClient)) {
-            event.source.postMessage("FAIL: client source is not a WindowClient");
-            return;
-        }
-    } else if (client !== event.source) {
-        event.source.postMessage("FAIL: client source of the second message is not the same as the first message");
-        return;
-    }
-    event.source.postMessage("PASS: Service worker received message '" + event.data + "' from origin '" + event.origin + "'");
-});
-

Modified: trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js (225402 => 225403)


--- trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js	2017-12-01 19:45:41 UTC (rev 225403)
@@ -9,6 +9,6 @@
         finishSWTest();
 });
 
-navigator.serviceWorker.register("resources/basic-ServiceWorker-postMessage-worker.js", { }).then(function(registration) {
+navigator.serviceWorker.register("resources/postmessage-echo-worker.js", { }).then(function(registration) {
     registration.installing.postMessage("Message 1");
 });

Copied: trunk/LayoutTests/http/tests/workers/service/resources/postmessage-after-terminate.js (from rev 225402, trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js) (0 => 225403)


--- trunk/LayoutTests/http/tests/workers/service/resources/postmessage-after-terminate.js	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/resources/postmessage-after-terminate.js	2017-12-01 19:45:41 UTC (rev 225403)
@@ -0,0 +1,15 @@
+var messageNumber = 1;
+navigator.serviceWorker.addEventListener("message", function(event) {
+    log("PASS: Client received message from service worker, origin: " + event.origin);
+    log(event.data);
+    if (messageNumber == 1) {
+        window.internals.terminateServiceWorker(event.source);
+        event.source.postMessage("Message 2");
+        messageNumber++;
+    } else
+        finishSWTest();
+});
+
+navigator.serviceWorker.register("resources/postmessage-echo-worker.js", { }).then(function(registration) {
+    registration.installing.postMessage("Message 1");
+});

Copied: trunk/LayoutTests/http/tests/workers/service/resources/postmessage-echo-worker.js (from rev 225402, trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js) (0 => 225403)


--- trunk/LayoutTests/http/tests/workers/service/resources/postmessage-echo-worker.js	                        (rev 0)
+++ trunk/LayoutTests/http/tests/workers/service/resources/postmessage-echo-worker.js	2017-12-01 19:45:41 UTC (rev 225403)
@@ -0,0 +1,15 @@
+var client = null;
+self.addEventListener("message", (event) => {
+    if (!client) {
+        client = event.source;
+        if (!(client instanceof WindowClient)) {
+            event.source.postMessage("FAIL: client source is not a WindowClient");
+            return;
+        }
+    } else if (client !== event.source) {
+        event.source.postMessage("FAIL: client source of the second message is not the same as the first message");
+        return;
+    }
+    event.source.postMessage("PASS: Service worker received message '" + event.data + "' from origin '" + event.origin + "'");
+});
+

Modified: trunk/Source/WTF/ChangeLog (225402 => 225403)


--- trunk/Source/WTF/ChangeLog	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WTF/ChangeLog	2017-12-01 19:45:41 UTC (rev 225403)
@@ -1,3 +1,13 @@
+2017-12-01  Brady Eidson  <beid...@apple.com>
+
+        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
+        https://bugs.webkit.org/show_bug.cgi?id=180170
+
+        Reviewed by Chris Dumez.
+
+        * wtf/CompletionHandler.h:
+        (WTF::CompletionHandler<Out):
+
 2017-11-30  Yusuke Suzuki  <utatane....@gmail.com>
 
         [JSC] Remove easy toRemove & map.remove() use in OAS phase

Modified: trunk/Source/WTF/wtf/CompletionHandler.h (225402 => 225403)


--- trunk/Source/WTF/wtf/CompletionHandler.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WTF/wtf/CompletionHandler.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -53,7 +53,7 @@
 
     explicit operator bool() const { return !!m_function; }
 
-    Out operator()(In... in)
+    Out operator()(In... in) const
     {
         ASSERT_WITH_MESSAGE(m_function, "Completion handler should not be called more than once");
         auto function = WTFMove(m_function);
@@ -61,7 +61,7 @@
     }
 
 private:
-    WTF::Function<Out(In...)> m_function;
+    mutable WTF::Function<Out(In...)> m_function;
 };
 
 } // namespace WTF

Modified: trunk/Source/WebCore/ChangeLog (225402 => 225403)


--- trunk/Source/WebCore/ChangeLog	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/ChangeLog	2017-12-01 19:45:41 UTC (rev 225403)
@@ -1,3 +1,53 @@
+2017-12-01  Brady Eidson  <beid...@apple.com>
+
+        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
+        https://bugs.webkit.org/show_bug.cgi?id=180170
+
+        Reviewed by Chris Dumez.
+
+        Test: http/tests/workers/service/postmessage-after-terminate.https.html
+
+        * dom/ActiveDOMObject.cpp:
+        (WebCore::ActiveDOMObject::~ActiveDOMObject):
+
+        * testing/Internals.cpp:
+        (WebCore::Internals::terminateServiceWorker):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
+        * workers/service/SWClientConnection.h:
+
+        * workers/service/ServiceWorker.idl:
+
+        * workers/service/context/SWContextManager.cpp:
+        (WebCore::SWContextManager::postMessageToServiceWorkerGlobalScope):
+        (WebCore::SWContextManager::terminateWorker):
+        * workers/service/context/SWContextManager.h:
+
+        * workers/service/context/ServiceWorkerThreadProxy.h:
+
+        * workers/service/server/SWServer.cpp:
+        (WebCore::SWServer::workerByID const):
+        (WebCore::SWServer::Connection::syncTerminateWorker):
+        (WebCore::SWServer::installContextData):
+        (WebCore::SWServer::invokeRunServiceWorker):
+        (WebCore::SWServer::terminateWorker):
+        (WebCore::SWServer::syncTerminateWorker):
+        (WebCore::SWServer::terminateWorkerInternal):
+        (WebCore::SWServer::workerContextTerminated):
+        * workers/service/server/SWServer.h:
+        (WebCore::SWServer::workerByID const): Deleted.
+
+        * workers/service/server/SWServerToContextConnection.h:
+
+        * workers/service/server/SWServerWorker.cpp:
+        (WebCore::SWServerWorker::contextData const):
+        * workers/service/server/SWServerWorker.h:
+        (WebCore::SWServerWorker::isRunning const):
+        (WebCore::SWServerWorker::isTerminating const):
+        (WebCore::SWServerWorker::setState):
+        (WebCore::SWServerWorker::server):
+
 2017-12-01  Zalan Bujtas  <za...@apple.com>
 
         Nullptr deref in WebCore::RenderTableCaption::containingBlockLogicalWidthForContent

Modified: trunk/Source/WebCore/dom/ActiveDOMObject.cpp (225402 => 225403)


--- trunk/Source/WebCore/dom/ActiveDOMObject.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/dom/ActiveDOMObject.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -47,11 +47,6 @@
 
 ActiveDOMObject::~ActiveDOMObject()
 {
-    if (!m_scriptExecutionContext)
-        return;
-
-    ASSERT(m_suspendIfNeededWasCalled);
-
     // ActiveDOMObject may be inherited by a sub-class whose life-cycle
     // exceeds that of the associated ScriptExecutionContext. In those cases,
     // m_scriptExecutionContext would/should have been nullified by
@@ -58,10 +53,12 @@
     // ContextDestructionObserver::contextDestroyed() (which we implement /
     // inherit). Hence, we should ensure that this is not 0 before use it
     // here.
-    if (m_scriptExecutionContext) {
-        ASSERT(m_scriptExecutionContext->isContextThread());
-        m_scriptExecutionContext->willDestroyActiveDOMObject(*this);
-    }
+    if (!m_scriptExecutionContext)
+        return;
+
+    ASSERT(m_suspendIfNeededWasCalled);
+    ASSERT(m_scriptExecutionContext->isContextThread());
+    m_scriptExecutionContext->willDestroyActiveDOMObject(*this);
 }
 
 void ActiveDOMObject::suspendIfNeeded()

Modified: trunk/Source/WebCore/testing/Internals.cpp (225402 => 225403)


--- trunk/Source/WebCore/testing/Internals.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/testing/Internals.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -132,6 +132,7 @@
 #include "ScrollingMomentumCalculator.h"
 #include "SecurityOrigin.h"
 #include "SerializedScriptValue.h"
+#include "ServiceWorker.h"
 #include "ServiceWorkerProvider.h"
 #include "ServiceWorkerRegistrationData.h"
 #include "Settings.h"
@@ -4270,6 +4271,14 @@
         promise.resolve(!!result);
     });
 }
+
+void Internals::terminateServiceWorker(ServiceWorker& worker)
+{
+    if (!contextDocument())
+        return;
+
+    ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(contextDocument()->sessionID()).syncTerminateWorker(worker.identifier());
+}
 #endif
 
 String Internals::timelineDescription(AnimationTimeline& timeline)

Modified: trunk/Source/WebCore/testing/Internals.h (225402 => 225403)


--- trunk/Source/WebCore/testing/Internals.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/testing/Internals.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -90,6 +90,10 @@
 class WebGLRenderingContext;
 class XMLHttpRequest;
 
+#if ENABLE(SERVICE_WORKER)
+class ServiceWorker;
+#endif
+
 class Internals final : public RefCounted<Internals>, private ContextDestructionObserver
 #if ENABLE(MEDIA_STREAM)
     , private RealtimeMediaSource::Observer
@@ -623,6 +627,7 @@
     Ref<FetchEvent> createBeingDispatchedFetchEvent(ScriptExecutionContext&);
     using HasRegistrationPromise = DOMPromiseDeferred<IDLBoolean>;
     void hasServiceWorkerRegistration(const String& clientURL, HasRegistrationPromise&&);
+    void terminateServiceWorker(ServiceWorker&);
 #endif
 
 #if ENABLE(APPLE_PAY)

Modified: trunk/Source/WebCore/testing/Internals.idl (225402 => 225403)


--- trunk/Source/WebCore/testing/Internals.idl	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/testing/Internals.idl	2017-12-01 19:45:41 UTC (rev 225403)
@@ -567,6 +567,7 @@
     [Conditional=SERVICE_WORKER] Promise<Response> waitForFetchEventToFinish(FetchEvent event);
     [Conditional=SERVICE_WORKER, CallWith=ScriptExecutionContext] FetchEvent createBeingDispatchedFetchEvent();
     [Conditional=SERVICE_WORKER] Promise<boolean> hasServiceWorkerRegistration(DOMString scopeURL);
+    [Conditional=SERVICE_WORKER] void terminateServiceWorker(ServiceWorker worker);
 
     [EnabledAtRuntime=WebAnimations] DOMString timelineDescription(AnimationTimeline timeline);
     [EnabledAtRuntime=WebAnimations] void pauseTimeline(AnimationTimeline timeline);

Modified: trunk/Source/WebCore/workers/service/SWClientConnection.h (225402 => 225403)


--- trunk/Source/WebCore/workers/service/SWClientConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/SWClientConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -73,6 +73,7 @@
     virtual void postMessageToServiceWorkerGlobalScope(ServiceWorkerIdentifier destinationIdentifier, Ref<SerializedScriptValue>&&, DocumentIdentifier sourceContextIdentifier, ServiceWorkerClientData&& source) = 0;
     virtual SWServerConnectionIdentifier serverConnectionIdentifier() const = 0;
     virtual bool mayHaveServiceWorkerRegisteredForOrigin(const SecurityOrigin&) const = 0;
+    virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0;
 
     virtual void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;
     virtual void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;

Modified: trunk/Source/WebCore/workers/service/ServiceWorker.idl (225402 => 225403)


--- trunk/Source/WebCore/workers/service/ServiceWorker.idl	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/ServiceWorker.idl	2017-12-01 19:45:41 UTC (rev 225403)
@@ -31,7 +31,8 @@
     SecureContext,
     Exposed=(Window,ServiceWorker),
     Conditional=SERVICE_WORKER,
-    EnabledAtRuntime=ServiceWorker
+    EnabledAtRuntime=ServiceWorker,
+    ExportMacro=WEBCORE_EXPORT
 ] interface ServiceWorker : EventTarget {
     readonly attribute USVString scriptURL;
     readonly attribute ServiceWorkerState state;

Modified: trunk/Source/WebCore/workers/service/context/SWContextManager.cpp (225402 => 225403)


--- trunk/Source/WebCore/workers/service/context/SWContextManager.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/context/SWContextManager.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -69,8 +69,8 @@
 void SWContextManager::postMessageToServiceWorkerGlobalScope(ServiceWorkerIdentifier destination, Ref<SerializedScriptValue>&& message, ServiceWorkerClientIdentifier sourceIdentifier, ServiceWorkerClientData&& sourceData)
 {
     auto* serviceWorker = m_workerMap.get(destination);
-    if (!serviceWorker)
-        return;
+    ASSERT(serviceWorker);
+    ASSERT(!serviceWorker->isTerminatingOrTerminated());
 
     // FIXME: We should pass valid MessagePortChannels.
     serviceWorker->thread().postMessageToServiceWorkerGlobalScope(WTFMove(message), nullptr, sourceIdentifier, WTFMove(sourceData));
@@ -94,15 +94,27 @@
     serviceWorker->thread().fireActivateEvent();
 }
 
-void SWContextManager::terminateWorker(ServiceWorkerIdentifier identifier)
+void SWContextManager::terminateWorker(ServiceWorkerIdentifier identifier, Function<void()>&& completionHandler)
 {
     auto* serviceWorker = m_workerMap.get(identifier);
     if (!serviceWorker)
         return;
 
-    serviceWorker->thread().stop([identifier] {
+    serviceWorker->setTerminatingOrTerminated(true);
+
+    serviceWorker->thread().stop([this, identifier, completionHandler = WTFMove(completionHandler)] {
         if (auto* connection = SWContextManager::singleton().connection())
             connection->workerTerminated(identifier);
+
+        if (completionHandler)
+            completionHandler();
+        
+        auto worker = m_workerMap.take(identifier);
+        ASSERT(worker);
+        
+        // Spin the runloop before releasing the worker thread proxy, as there would otherwise be
+        // a race towards its destruction.
+        callOnMainThread([worker = WTFMove(worker)] { });
     });
 }
 

Modified: trunk/Source/WebCore/workers/service/context/SWContextManager.h (225402 => 225403)


--- trunk/Source/WebCore/workers/service/context/SWContextManager.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/context/SWContextManager.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -61,7 +61,7 @@
     WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(ServiceWorkerIdentifier destination, Ref<SerializedScriptValue>&& message, ServiceWorkerClientIdentifier sourceIdentifier, ServiceWorkerClientData&& sourceData);
     WEBCORE_EXPORT void fireInstallEvent(ServiceWorkerIdentifier);
     WEBCORE_EXPORT void fireActivateEvent(ServiceWorkerIdentifier);
-    WEBCORE_EXPORT void terminateWorker(ServiceWorkerIdentifier);
+    WEBCORE_EXPORT void terminateWorker(ServiceWorkerIdentifier, Function<void()>&&);
 
     void forEachServiceWorkerThread(const WTF::Function<void(ServiceWorkerThreadProxy&)>&);
 

Modified: trunk/Source/WebCore/workers/service/context/ServiceWorkerThreadProxy.h (225402 => 225403)


--- trunk/Source/WebCore/workers/service/context/ServiceWorkerThreadProxy.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/context/ServiceWorkerThreadProxy.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -56,6 +56,9 @@
     ServiceWorkerThread& thread() { return m_serviceWorkerThread.get(); }
     ServiceWorkerInspectorProxy& inspectorProxy() { return m_inspectorProxy; }
 
+    bool isTerminatingOrTerminated() const { return m_isTerminatingOrTerminated; }
+    void setTerminatingOrTerminated(bool terminating) { m_isTerminatingOrTerminated = terminating; }
+
 private:
     WEBCORE_EXPORT ServiceWorkerThreadProxy(PageConfiguration&&, const ServiceWorkerContextData&, PAL::SessionID, CacheStorageProvider&);
 
@@ -74,6 +77,8 @@
     CacheStorageProvider& m_cacheStorageProvider;
     RefPtr<CacheStorageConnection> m_cacheStorageConnection;
     PAL::SessionID m_sessionID;
+    bool m_isTerminatingOrTerminated { false };
+
     ServiceWorkerInspectorProxy m_inspectorProxy;
 #if ENABLE(REMOTE_INSPECTOR)
     std::unique_ptr<ServiceWorkerDebuggable> m_remoteDebuggable;

Modified: trunk/Source/WebCore/workers/service/server/SWServer.cpp (225402 => 225403)


--- trunk/Source/WebCore/workers/service/server/SWServer.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/server/SWServer.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -87,6 +87,13 @@
     allServers().remove(this);
 }
 
+SWServerWorker* SWServer::workerByID(ServiceWorkerIdentifier identifier) const
+{
+    auto* worker = SWServerWorker::existingWorkerForIdentifier(identifier);
+    ASSERT(!worker || &worker->server() == this);
+    return worker;
+}
+
 SWServerRegistration* SWServer::getRegistration(const ServiceWorkerRegistrationKey& registrationKey)
 {
     return m_registrations.get(registrationKey);
@@ -188,6 +195,12 @@
     m_server.serviceWorkerStoppedControllingClient(*this, serviceWorkerIdentifier, registrationIdentifier, contextIdentifier);
 }
 
+void SWServer::Connection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
+{
+    if (auto* worker = m_server.workerByID(identifier))
+        m_server.syncTerminateWorker(*worker);
+}
+
 SWServer::SWServer(UniqueRef<SWOriginStore>&& originStore)
     : m_originStore(WTFMove(originStore))
 {
@@ -299,12 +312,6 @@
         SWServerJobQueue::didFinishActivation(*registration, worker.identifier());
 }
 
-void SWServer::workerContextTerminated(SWServerWorker& worker)
-{
-    auto result = m_workersByID.remove(worker.identifier());
-    ASSERT_UNUSED(result, result);
-}
-
 void SWServer::didResolveRegistrationPromise(Connection& connection, const ServiceWorkerRegistrationKey& registrationKey)
 {
     ASSERT_UNUSED(connection, m_connections.contains(connection.identifier()));
@@ -387,22 +394,85 @@
     auto* registration = m_registrations.get(data.registration.key);
     RELEASE_ASSERT(registration);
 
-    auto result = m_workersByID.add(data.serviceWorkerIdentifier, SWServerWorker::create(*this, *registration, connection->identifier(), data.scriptURL, data.script, data.workerType, data.serviceWorkerIdentifier));
-    ASSERT_UNUSED(result, result.isNewEntry);
+    auto result = m_runningOrTerminatingWorkers.add(data.serviceWorkerIdentifier, SWServerWorker::create(*this, *registration, connection->identifier(), data.scriptURL, data.script, data.workerType, data.serviceWorkerIdentifier));
+    ASSERT(result.isNewEntry);
 
+    result.iterator->value->setState(SWServerWorker::State::Running);
+
     connection->installServiceWorkerContext(data);
 }
 
+bool SWServer::invokeRunServiceWorker(ServiceWorkerIdentifier identifier)
+{
+    if (auto* worker = m_runningOrTerminatingWorkers.get(identifier)) {
+        if (worker->isRunning())
+            return true;
+    }
 
+    // Nobody should have a ServiceWorkerIdentifier for a SWServerWorker that doesn't exist.
+    auto* worker = workerByID(identifier);
+    ASSERT(worker);
+
+    // If the registration for a working has been removed then the request to run
+    // the worker is moot.
+    if (!getRegistration(worker->registrationKey()))
+        return false;
+
+    m_runningOrTerminatingWorkers.add(identifier, *worker);
+    worker->setState(SWServerWorker::State::Running);
+
+    auto* connection = SWServerToContextConnection::globalServerToContextConnection();
+    ASSERT(connection);
+    connection->installServiceWorkerContext(worker->contextData());
+    
+    return true;
+}
+
 void SWServer::terminateWorker(SWServerWorker& worker)
 {
+    terminateWorkerInternal(worker, Asynchronous);
+}
+
+void SWServer::syncTerminateWorker(SWServerWorker& worker)
+{
+    terminateWorkerInternal(worker, Synchronous);
+}
+
+void SWServer::terminateWorkerInternal(SWServerWorker& worker, TerminationMode mode)
+{
     auto* connection = SWServerToContextConnection::connectionForIdentifier(worker.contextConnectionIdentifier());
-    if (connection)
+    if (!connection) {
+        LOG_ERROR("Request to terminate a worker whose context connection does not exist");
+        return;
+    }
+
+    ASSERT(m_runningOrTerminatingWorkers.get(worker.identifier()) == &worker);
+    ASSERT(!worker.isTerminating());
+
+    worker.setState(SWServerWorker::State::Terminating);
+
+    switch (mode) {
+    case Asynchronous:
         connection->terminateWorker(worker.identifier());
-    else
-        LOG_ERROR("Request to terminate a worker whose context connection does not exist");
+        break;
+    case Synchronous:
+        connection->syncTerminateWorker(worker.identifier());
+        break;
+    };
 }
 
+void SWServer::workerContextTerminated(SWServerWorker& worker)
+{
+    ASSERT(worker.isTerminating());
+
+    worker.setState(SWServerWorker::State::NotRunning);
+
+    // At this point if no registrations are referencing the worker then it will be destroyed,
+    // removing itself from the m_workersByID map.
+    auto result = m_runningOrTerminatingWorkers.take(worker.identifier());
+    ASSERT_UNUSED(result, result && result->ptr() == &worker);
+}
+
 void SWServer::fireInstallEvent(SWServerWorker& worker)
 {
     auto* connection = SWServerToContextConnection::connectionForIdentifier(worker.contextConnectionIdentifier());

Modified: trunk/Source/WebCore/workers/service/server/SWServer.h (225402 => 225403)


--- trunk/Source/WebCore/workers/service/server/SWServer.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/server/SWServer.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -90,6 +90,7 @@
         WEBCORE_EXPORT void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier);
         WEBCORE_EXPORT void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
         WEBCORE_EXPORT void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
+        WEBCORE_EXPORT void syncTerminateWorker(ServiceWorkerIdentifier);
 
     private:
         // Messages to the client WebProcess
@@ -108,7 +109,6 @@
     WEBCORE_EXPORT void clearAll();
     WEBCORE_EXPORT void clear(const SecurityOrigin&);
 
-
     SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
     void addRegistration(std::unique_ptr<SWServerRegistration>&&);
     void removeRegistration(const ServiceWorkerRegistrationKey&);
@@ -125,9 +125,10 @@
 
     void updateWorker(Connection&, const ServiceWorkerJobDataIdentifier&, SWServerRegistration&, const URL&, const String& script, WorkerType);
     void terminateWorker(SWServerWorker&);
+    void syncTerminateWorker(SWServerWorker&);
     void fireInstallEvent(SWServerWorker&);
     void fireActivateEvent(SWServerWorker&);
-    SWServerWorker* workerByID(ServiceWorkerIdentifier identifier) const { return m_workersByID.get(identifier); }
+    WEBCORE_EXPORT SWServerWorker* workerByID(ServiceWorkerIdentifier) const;
     
     Connection* getConnection(SWServerConnectionIdentifier identifier) { return m_connections.get(identifier); }
     SWOriginStore& originStore() { return m_originStore; }
@@ -145,6 +146,8 @@
     WEBCORE_EXPORT void registerServiceWorkerClient(ClientOrigin&&, ServiceWorkerClientIdentifier, ServiceWorkerClientData&&);
     WEBCORE_EXPORT void unregisterServiceWorkerClient(const ClientOrigin&, ServiceWorkerClientIdentifier);
 
+    WEBCORE_EXPORT bool invokeRunServiceWorker(ServiceWorkerIdentifier);
+
 private:
     void registerConnection(Connection&);
     void unregisterConnection(Connection&);
@@ -165,12 +168,18 @@
 
     void installContextData(const ServiceWorkerContextData&);
 
+    enum TerminationMode {
+        Synchronous,
+        Asynchronous,
+    };
+    void terminateWorkerInternal(SWServerWorker&, TerminationMode);
+
     HashMap<SWServerConnectionIdentifier, Connection*> m_connections;
     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
     HashMap<ServiceWorkerRegistrationIdentifier, SWServerRegistration*> m_registrationsByID;
     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerJobQueue>> m_jobQueues;
 
-    HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_workersByID;
+    HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_runningOrTerminatingWorkers;
 
     struct ClientInformation {
         ServiceWorkerClientIdentifier identifier;

Modified: trunk/Source/WebCore/workers/service/server/SWServerToContextConnection.h (225402 => 225403)


--- trunk/Source/WebCore/workers/service/server/SWServerToContextConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/server/SWServerToContextConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -48,6 +48,7 @@
     virtual void fireInstallEvent(ServiceWorkerIdentifier) = 0;
     virtual void fireActivateEvent(ServiceWorkerIdentifier) = 0;
     virtual void terminateWorker(ServiceWorkerIdentifier) = 0;
+    virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0;
 
     // Messages back from the SW host process
     WEBCORE_EXPORT void scriptContextFailedToStart(const std::optional<ServiceWorkerJobDataIdentifier>&, ServiceWorkerIdentifier, const String& message);

Modified: trunk/Source/WebCore/workers/service/server/SWServerWorker.cpp (225402 => 225403)


--- trunk/Source/WebCore/workers/service/server/SWServerWorker.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/server/SWServerWorker.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -60,6 +60,14 @@
     ASSERT_UNUSED(taken, taken == this);
 }
 
+ServiceWorkerContextData SWServerWorker::contextData() const
+{
+    auto* registration = m_server.getRegistration(m_registrationKey);
+    ASSERT(registration);
+
+    return { std::nullopt, registration->data(), m_data.identifier, m_script, m_data.scriptURL, m_data.type };
+}
+
 void SWServerWorker::terminate()
 {
     m_server.terminateWorker(*this);

Modified: trunk/Source/WebCore/workers/service/server/SWServerWorker.h (225402 => 225403)


--- trunk/Source/WebCore/workers/service/server/SWServerWorker.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebCore/workers/service/server/SWServerWorker.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -39,6 +39,7 @@
 class SWServer;
 class SWServerRegistration;
 enum class WorkerType;
+struct ServiceWorkerContextData;
 struct ServiceWorkerJobDataIdentifier;
 
 class SWServerWorker : public RefCounted<SWServerWorker> {
@@ -53,7 +54,16 @@
 
     void terminate();
 
-    SWServer& server();
+    enum class State {
+        Running,
+        Terminating,
+        NotRunning,
+    };
+    bool isRunning() const { return m_state == State::Running; }
+    bool isTerminating() const { return m_state == State::Terminating; }
+    void setState(State state) { m_state = state; }
+
+    SWServer& server() { return m_server; }
     const ServiceWorkerRegistrationKey& registrationKey() const { return m_registrationKey; }
     const URL& scriptURL() const { return m_data.scriptURL; }
     const String& script() const { return m_script; }
@@ -77,6 +87,7 @@
     WEBCORE_EXPORT static SWServerWorker* existingWorkerForIdentifier(ServiceWorkerIdentifier);
 
     const ServiceWorkerData& data() const { return m_data; }
+    ServiceWorkerContextData contextData() const;
 
 private:
     SWServerWorker(SWServer&, SWServerRegistration&, SWServerToContextConnectionIdentifier, const URL&, const String& script, WorkerType, ServiceWorkerIdentifier);
@@ -87,6 +98,7 @@
     ServiceWorkerData m_data;
     String m_script;
     bool m_hasPendingEvents { false };
+    State m_state { State::NotRunning };
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebKit/ChangeLog (225402 => 225403)


--- trunk/Source/WebKit/ChangeLog	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/ChangeLog	2017-12-01 19:45:41 UTC (rev 225403)
@@ -1,3 +1,37 @@
+2017-12-01  Brady Eidson  <beid...@apple.com>
+
+        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
+        https://bugs.webkit.org/show_bug.cgi?id=180170
+
+        Reviewed by Chris Dumez.
+
+        * StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
+        (WebKit::WebSWServerConnection::startFetch):
+        (WebKit::WebSWServerConnection::postMessageToServiceWorkerGlobalScope):
+        * StorageProcess/ServiceWorker/WebSWServerConnection.h:
+        * StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
+
+        * StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp:
+        (WebKit::WebSWServerToContextConnection::syncTerminateWorker):
+        * StorageProcess/ServiceWorker/WebSWServerToContextConnection.h:
+
+        * StorageProcess/StorageToWebProcessConnection.cpp:
+        (WebKit::StorageToWebProcessConnection::didReceiveSyncMessage):
+
+        * WebProcess/Storage/WebSWClientConnection.cpp:
+        (WebKit::WebSWClientConnection::syncTerminateWorker):
+        * WebProcess/Storage/WebSWClientConnection.h:
+
+        * WebProcess/Storage/WebSWContextManagerConnection.cpp:
+        (WebKit::WebSWContextManagerConnection::terminateWorker):
+        (WebKit::WebSWContextManagerConnection::syncTerminateWorker):
+        * WebProcess/Storage/WebSWContextManagerConnection.h:
+        * WebProcess/Storage/WebSWContextManagerConnection.messages.in:
+
+        * WebProcess/Storage/WebToStorageProcessConnection.cpp:
+        (WebKit::WebToStorageProcessConnection::didReceiveSyncMessage):
+        * WebProcess/Storage/WebToStorageProcessConnection.h:
+
 2017-12-01  Youenn Fablet  <you...@apple.com>
 
         Clear WebSWClientConnection in case storage process IPC connection is closing

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp (225402 => 225403)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -118,11 +118,22 @@
 
 void WebSWServerConnection::startFetch(uint64_t fetchIdentifier, std::optional<ServiceWorkerIdentifier> serviceWorkerIdentifier, const ResourceRequest& request, const FetchOptions& options, const IPC::FormDataReference& formData)
 {
+    // It's possible this specific worker cannot be re-run (e.g. its registration has been removed)
+    if (serviceWorkerIdentifier && !server().invokeRunServiceWorker(*serviceWorkerIdentifier))
+        return;
+
+    // FIXME: If we don't have a ServiceWorkerIdentifier here then we need to create and run the appropriate Service Worker,
+    // but it's unclear that we have enough information here to do that.
+
     sendToContextProcess(Messages::WebSWContextManagerConnection::StartFetch { identifier(), fetchIdentifier, serviceWorkerIdentifier, request, options, formData });
 }
 
 void WebSWServerConnection::postMessageToServiceWorkerGlobalScope(ServiceWorkerIdentifier destinationServiceWorkerIdentifier, const IPC::DataReference& message, DocumentIdentifier sourceContextIdentifier, ServiceWorkerClientData&& sourceData)
 {
+    // It's possible this specific worker cannot be re-run (e.g. its registration has been removed)
+    if (!server().invokeRunServiceWorker(destinationServiceWorkerIdentifier))
+        return;
+
     ServiceWorkerClientIdentifier sourceIdentifier { identifier(), sourceContextIdentifier };
     sendToContextProcess(Messages::WebSWContextManagerConnection::PostMessageToServiceWorkerGlobalScope { destinationServiceWorkerIdentifier, message, sourceIdentifier, WTFMove(sourceData) });
 }

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h (225402 => 225403)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -54,6 +54,7 @@
 
     void disconnectedFromWebProcess();
     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
+    void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
 
     PAL::SessionID sessionID() const { return m_sessionID; }
 

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in (225402 => 225403)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in	2017-12-01 19:45:41 UTC (rev 225403)
@@ -40,6 +40,8 @@
     GetRegistrations(uint64_t serviceRegistrationMatchRequestIdentifier, struct WebCore::SecurityOriginData topOrigin, WebCore::URL clientURL)
     RegisterServiceWorkerClient(struct WebCore::SecurityOriginData topOrigin, WebCore::DocumentIdentifier identifier, struct WebCore::ServiceWorkerClientData data)
     UnregisterServiceWorkerClient(WebCore::DocumentIdentifier identifier)
+
+    SyncTerminateWorker(WebCore::ServiceWorkerIdentifier workerIdentifier) -> ()
 }
 
 #endif // ENABLE(SERVICE_WORKER)

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp (225402 => 225403)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -76,6 +76,11 @@
     send(Messages::WebSWContextManagerConnection::TerminateWorker(serviceWorkerIdentifier));
 }
 
+void WebSWServerToContextConnection::syncTerminateWorker(ServiceWorkerIdentifier serviceWorkerIdentifier)
+{
+    sendSync(Messages::WebSWContextManagerConnection::SyncTerminateWorker(serviceWorkerIdentifier), Messages::WebSWContextManagerConnection::SyncTerminateWorker::Reply());
+}
+
 } // namespace WebKit
 
 #endif // ENABLE(SERVICE_WORKER)

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.h (225402 => 225403)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -59,6 +59,7 @@
     void fireInstallEvent(WebCore::ServiceWorkerIdentifier) final;
     void fireActivateEvent(WebCore::ServiceWorkerIdentifier) final;
     void terminateWorker(WebCore::ServiceWorkerIdentifier) final;
+    void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
 
     Ref<IPC::Connection> m_ipcConnection;
     

Modified: trunk/Source/WebKit/StorageProcess/StorageToWebProcessConnection.cpp (225402 => 225403)


--- trunk/Source/WebKit/StorageProcess/StorageToWebProcessConnection.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/StorageProcess/StorageToWebProcessConnection.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -108,6 +108,15 @@
         return;
     }
 
+#if ENABLE(SERVICE_WORKER)
+    if (decoder.messageReceiverName() == Messages::WebSWServerConnection::messageReceiverName()) {
+        auto iterator = m_swConnections.find(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID()));
+        if (iterator != m_swConnections.end())
+            iterator->value->didReceiveSyncMessage(connection, decoder, replyEncoder);
+        return;
+    }
+#endif
+
     ASSERT_NOT_REACHED();
 }
 

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -190,6 +190,11 @@
     clearPendingJobs();
 }
 
+void WebSWClientConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
+{
+    sendSync(Messages::WebSWServerConnection::SyncTerminateWorker(identifier), Messages::WebSWServerConnection::SyncTerminateWorker::Reply());
+}
+
 } // namespace WebKit
 
 #endif // ENABLE(SERVICE_WORKER)

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -66,6 +66,8 @@
 
     void connectionToServerLost();
 
+    void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
+
 private:
     WebSWClientConnection(IPC::Connection&, PAL::SessionID);
 

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -166,9 +166,16 @@
 
 void WebSWContextManagerConnection::terminateWorker(ServiceWorkerIdentifier identifier)
 {
-    SWContextManager::singleton().terminateWorker(identifier);
+    SWContextManager::singleton().terminateWorker(identifier, nullptr);
 }
 
+void WebSWContextManagerConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier, Ref<Messages::WebSWContextManagerConnection::SyncTerminateWorker::DelayedReply>&& reply)
+{
+    SWContextManager::singleton().terminateWorker(identifier, [reply = WTFMove(reply)] {
+        reply->send();
+    });
+}
+
 void WebSWContextManagerConnection::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, Ref<SerializedScriptValue>&& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
 {
     m_connectionToStorageProcess->send(Messages::StorageProcess::PostMessageToServiceWorkerClient(destinationIdentifier, IPC::DataReference { message->data() }, sourceIdentifier, sourceOrigin), 0);

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -29,6 +29,7 @@
 
 #include "Connection.h"
 #include "MessageReceiver.h"
+#include "WebSWContextManagerConnectionMessages.h"
 #include <WebCore/SWContextManager.h>
 #include <WebCore/ServiceWorkerTypes.h>
 
@@ -51,6 +52,7 @@
     WebSWContextManagerConnection(Ref<IPC::Connection>&&, uint64_t pageID, const WebPreferencesStore&);
 
     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
+    void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) final;
 
 private:
     void updatePreferences(const WebPreferencesStore&);
@@ -70,6 +72,7 @@
     void fireInstallEvent(WebCore::ServiceWorkerIdentifier);
     void fireActivateEvent(WebCore::ServiceWorkerIdentifier);
     void terminateWorker(WebCore::ServiceWorkerIdentifier);
+    void syncTerminateWorker(WebCore::ServiceWorkerIdentifier, Ref<Messages::WebSWContextManagerConnection::SyncTerminateWorker::DelayedReply>&&);
 
     Ref<IPC::Connection> m_connectionToStorageProcess;
     uint64_t m_pageID { 0 };

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.messages.in (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.messages.in	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.messages.in	2017-12-01 19:45:41 UTC (rev 225403)
@@ -29,6 +29,7 @@
     FireInstallEvent(WebCore::ServiceWorkerIdentifier identifier)
     FireActivateEvent(WebCore::ServiceWorkerIdentifier identifier)
     TerminateWorker(WebCore::ServiceWorkerIdentifier identifier)
+    SyncTerminateWorker(WebCore::ServiceWorkerIdentifier identifier) -> () Delayed
 }
 
 #endif

Modified: trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.cpp (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.cpp	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.cpp	2017-12-01 19:45:41 UTC (rev 225403)
@@ -86,6 +86,19 @@
     ASSERT_NOT_REACHED();
 }
 
+void WebToStorageProcessConnection::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
+{
+#if ENABLE(SERVICE_WORKER)
+    if (decoder.messageReceiverName() == Messages::WebSWContextManagerConnection::messageReceiverName()) {
+        ASSERT(SWContextManager::singleton().connection());
+        if (auto* contextManagerConnection = SWContextManager::singleton().connection())
+            static_cast<WebSWContextManagerConnection&>(*contextManagerConnection).didReceiveSyncMessage(connection, decoder, replyEncoder);
+        return;
+    }
+#endif
+    ASSERT_NOT_REACHED();
+}
+
 void WebToStorageProcessConnection::didClose(IPC::Connection& connection)
 {
 #if ENABLE(INDEXED_DATABASE)

Modified: trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.h (225402 => 225403)


--- trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.h	2017-12-01 19:15:33 UTC (rev 225402)
+++ trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.h	2017-12-01 19:45:41 UTC (rev 225403)
@@ -62,6 +62,7 @@
 
     // IPC::Connection::Client
     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
+    void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
     void didClose(IPC::Connection&) override;
     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to