Title: [249820] trunk/Source
Revision
249820
Author
ryanhad...@apple.com
Date
2019-09-12 16:59:32 -0700 (Thu, 12 Sep 2019)

Log Message

Unreviewed, rolling out r249801.

Caused two servier worker layout tests to become flaky.

Reverted changeset:

"Use WebProcess processIdentifier to identify Service Worker
connections"
https://bugs.webkit.org/show_bug.cgi?id=201459
https://trac.webkit.org/changeset/249801

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (249819 => 249820)


--- trunk/Source/WebCore/ChangeLog	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/ChangeLog	2019-09-12 23:59:32 UTC (rev 249820)
@@ -1,3 +1,16 @@
+2019-09-12  Ryan Haddad  <ryanhad...@apple.com>
+
+        Unreviewed, rolling out r249801.
+
+        Caused two servier worker layout tests to become flaky.
+
+        Reverted changeset:
+
+        "Use WebProcess processIdentifier to identify Service Worker
+        connections"
+        https://bugs.webkit.org/show_bug.cgi?id=201459
+        https://trac.webkit.org/changeset/249801
+
 2019-09-12  Chris Dumez  <cdu...@apple.com>
 
         [WKTR] Drop TestRunner.setPrivateBrowsingEnabled_DEPRECATED()

Modified: trunk/Source/WebCore/PAL/ChangeLog (249819 => 249820)


--- trunk/Source/WebCore/PAL/ChangeLog	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/PAL/ChangeLog	2019-09-12 23:59:32 UTC (rev 249820)
@@ -1,3 +1,16 @@
+2019-09-12  Ryan Haddad  <ryanhad...@apple.com>
+
+        Unreviewed, rolling out r249801.
+
+        Caused two servier worker layout tests to become flaky.
+
+        Reverted changeset:
+
+        "Use WebProcess processIdentifier to identify Service Worker
+        connections"
+        https://bugs.webkit.org/show_bug.cgi?id=201459
+        https://trac.webkit.org/changeset/249801
+
 2019-09-12  Youenn Fablet  <you...@apple.com>
 
         Use WebProcess processIdentifier to identify Service Worker connections

Modified: trunk/Source/WebCore/PAL/pal/SessionID.h (249819 => 249820)


--- trunk/Source/WebCore/PAL/pal/SessionID.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/PAL/pal/SessionID.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -55,11 +55,6 @@
     {
     }
 
-    explicit SessionID(uint64_t identifier)
-        : m_identifier(identifier)
-    {
-    }
-
     PAL_EXPORT static SessionID generateEphemeralSessionID();
     PAL_EXPORT static SessionID generatePersistentSessionID();
     PAL_EXPORT static void enableGenerationProtection();
@@ -81,6 +76,11 @@
     explicit operator bool() const { return m_identifier; }
 
 private:
+    explicit SessionID(uint64_t identifier)
+        : m_identifier(identifier)
+    {
+    }
+
     static bool isValidSessionIDValue(uint64_t sessionID) { return sessionID != HashTableEmptyValueID && sessionID != HashTableDeletedValueID; }
 
     uint64_t m_identifier;

Modified: trunk/Source/WebCore/workers/service/ServiceWorkerClient.cpp (249819 => 249820)


--- trunk/Source/WebCore/workers/service/ServiceWorkerClient.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/workers/service/ServiceWorkerClient.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -99,9 +99,9 @@
 
     MessageWithMessagePorts message = { messageData.releaseReturnValue(), portsOrException.releaseReturnValue() };
     auto sourceIdentifier = downcast<ServiceWorkerGlobalScope>(context).thread().identifier();
-    callOnMainThread([sessionID = context.sessionID(), message = WTFMove(message), destinationIdentifier = identifier(), sourceIdentifier, sourceOrigin = context.origin().isolatedCopy()] () mutable {
+    callOnMainThread([message = WTFMove(message), destinationIdentifier = identifier(), sourceIdentifier, sourceOrigin = context.origin().isolatedCopy()] () mutable {
         if (auto* connection = SWContextManager::singleton().connection())
-            connection->postMessageToServiceWorkerClient(sessionID, destinationIdentifier, message, sourceIdentifier, sourceOrigin);
+            connection->postMessageToServiceWorkerClient(destinationIdentifier, WTFMove(message), sourceIdentifier, sourceOrigin);
     });
 
     return { };

Modified: trunk/Source/WebCore/workers/service/ServiceWorkerTypes.h (249819 => 249820)


--- trunk/Source/WebCore/workers/service/ServiceWorkerTypes.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/workers/service/ServiceWorkerTypes.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -28,7 +28,6 @@
 #if ENABLE(SERVICE_WORKER)
 
 #include "DocumentIdentifier.h"
-#include "ProcessIdentifier.h"
 #include "ServiceWorkerIdentifier.h"
 #include <wtf/ObjectIdentifier.h>
 #include <wtf/Variant.h>
@@ -71,7 +70,7 @@
 enum SWServerToContextConnectionIdentifierType { };
 using SWServerToContextConnectionIdentifier = ObjectIdentifier<SWServerToContextConnectionIdentifierType>;
 
-using SWServerConnectionIdentifierType = ProcessIdentifierType;
+enum SWServerConnectionIdentifierType { };
 using SWServerConnectionIdentifier = ObjectIdentifier<SWServerConnectionIdentifierType>;
 
 using DocumentOrWorkerIdentifier = Variant<DocumentIdentifier, ServiceWorkerIdentifier>;

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


--- trunk/Source/WebCore/workers/service/context/SWContextManager.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/workers/service/context/SWContextManager.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -49,7 +49,7 @@
     public:
         virtual ~Connection() { }
 
-        virtual void postMessageToServiceWorkerClient(PAL::SessionID, const ServiceWorkerClientIdentifier& destinationIdentifier, const MessageWithMessagePorts&, ServiceWorkerIdentifier source, const String& sourceOrigin) = 0;
+        virtual void postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, MessageWithMessagePorts&&, ServiceWorkerIdentifier source, const String& sourceOrigin) = 0;
         virtual void serviceWorkerStarted(Optional<ServiceWorkerJobDataIdentifier>, ServiceWorkerIdentifier, bool doesHandleFetch) = 0;
         virtual void serviceWorkerFailedToStart(Optional<ServiceWorkerJobDataIdentifier>, ServiceWorkerIdentifier, const String& message) = 0;
         virtual void didFinishInstall(Optional<ServiceWorkerJobDataIdentifier>, ServiceWorkerIdentifier, bool wasSuccessful) = 0;

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


--- trunk/Source/WebCore/workers/service/server/SWServer.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/workers/service/server/SWServer.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -50,9 +50,9 @@
 
 static Seconds terminationDelay { 10_s };
 
-SWServer::Connection::Connection(SWServer& server, Identifier identifier)
+SWServer::Connection::Connection(SWServer& server)
     : m_server(server)
-    , m_identifier(identifier)
+    , m_identifier(SWServerConnectionIdentifier::generate())
 {
 }
 

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


--- trunk/Source/WebCore/workers/service/server/SWServer.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebCore/workers/service/server/SWServer.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -56,7 +56,6 @@
 enum class ServiceWorkerRegistrationState : uint8_t;
 enum class ServiceWorkerState : uint8_t;
 struct ExceptionData;
-struct MessageWithMessagePorts;
 struct ServiceWorkerClientQueryOptions;
 struct ServiceWorkerContextData;
 struct ServiceWorkerFetchResult;
@@ -87,7 +86,6 @@
         virtual void setRegistrationUpdateViaCache(ServiceWorkerRegistrationIdentifier, ServiceWorkerUpdateViaCache) = 0;
         virtual void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, const ServiceWorkerData& newController) = 0;
         virtual void registrationReady(uint64_t registrationReadyRequestIdentifier, ServiceWorkerRegistrationData&&) = 0;
-        virtual void postMessageToServiceWorkerClient(DocumentIdentifier, const MessageWithMessagePorts&, ServiceWorkerIdentifier, const String& sourceOrigin) = 0;
 
         virtual void serverToContextConnectionCreated(SWServerToContextConnection&) = 0;
 
@@ -95,7 +93,7 @@
         const SWServer& server() const { return m_server; }
 
     protected:
-        WEBCORE_EXPORT Connection(SWServer&, Identifier);
+        WEBCORE_EXPORT explicit Connection(SWServer&);
 
         WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
         WEBCORE_EXPORT void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier);

Modified: trunk/Source/WebKit/ChangeLog (249819 => 249820)


--- trunk/Source/WebKit/ChangeLog	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/ChangeLog	2019-09-12 23:59:32 UTC (rev 249820)
@@ -1,3 +1,16 @@
+2019-09-12  Ryan Haddad  <ryanhad...@apple.com>
+
+        Unreviewed, rolling out r249801.
+
+        Caused two servier worker layout tests to become flaky.
+
+        Reverted changeset:
+
+        "Use WebProcess processIdentifier to identify Service Worker
+        connections"
+        https://bugs.webkit.org/show_bug.cgi?id=201459
+        https://trac.webkit.org/changeset/249801
+
 2019-09-12  Chris Dumez  <cdu...@apple.com>
 
         Pass sessionID to WebProcess with other WebProcessDataStoreParameters

Modified: trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.cpp (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -213,22 +213,21 @@
     
 #if ENABLE(SERVICE_WORKER)
     if (decoder.messageReceiverName() == Messages::WebSWServerConnection::messageReceiverName()) {
-        if (auto swConnection = m_swConnections.get(PAL::SessionID { decoder.destinationID() }))
+        if (auto swConnection = m_swConnections.get(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID())))
             swConnection->didReceiveMessage(connection, decoder);
         return;
     }
+
     if (decoder.messageReceiverName() == Messages::WebSWServerToContextConnection::messageReceiverName()) {
-        ASSERT(m_swContextConnection);
-        if (m_swContextConnection) {
-            m_swContextConnection->didReceiveMessage(connection, decoder);
+        if (auto* contextConnection = m_networkProcess->connectionToContextProcessFromIPCConnection(connection)) {
+            contextConnection->didReceiveMessage(connection, decoder);
             return;
         }
     }
 
     if (decoder.messageReceiverName() == Messages::ServiceWorkerFetchTask::messageReceiverName()) {
-        ASSERT(m_swContextConnection);
-        if (m_swContextConnection) {
-            m_swContextConnection->didReceiveFetchTaskMessage(connection, decoder);
+        if (auto* contextConnection = m_networkProcess->connectionToContextProcessFromIPCConnection(connection)) {
+            contextConnection->didReceiveFetchTaskMessage(connection, decoder);
             return;
         }
     }
@@ -269,7 +268,7 @@
 
 #if ENABLE(SERVICE_WORKER)
     if (decoder.messageReceiverName() == Messages::WebSWServerConnection::messageReceiverName()) {
-        if (auto swConnection = m_swConnections.get(PAL::SessionID { decoder.destinationID() }))
+        if (auto swConnection = m_swConnections.get(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID())))
             swConnection->didReceiveSyncMessage(connection, decoder, reply);
         return;
     }
@@ -286,19 +285,10 @@
 void NetworkConnectionToWebProcess::didClose(IPC::Connection& connection)
 {
 #if ENABLE(SERVICE_WORKER)
-    if (m_swContextConnection) {
-        auto& registrableDomain = m_swContextConnection->registrableDomain();
-
-        m_swContextConnection->connectionClosed();
-
-        Optional<PAL::SessionID> sessionID;
-        m_networkProcess->forEachSWServer([&](auto& server) {
-            server.markAllWorkersForRegistrableDomainAsTerminated(registrableDomain);
-            if (server.needsServerToContextConnectionForRegistrableDomain(registrableDomain))
-                sessionID = server.sessionID();
-        });
-        if (sessionID)
-            m_networkProcess->createServerToContextConnection(registrableDomain, *sessionID);
+    if (RefPtr<WebSWServerToContextConnection> serverToContextConnection = m_networkProcess->connectionToContextProcessFromIPCConnection(connection)) {
+        // Service Worker process exited.
+        m_networkProcess->connectionToContextProcessWasClosed(serverToContextConnection.releaseNonNull());
+        return;
     }
 #else
     UNUSED_PARAM(connection);
@@ -873,24 +863,19 @@
     }
 }
 
-void NetworkConnectionToWebProcess::establishSWServerConnection(PAL::SessionID sessionID)
+void NetworkConnectionToWebProcess::establishSWServerConnection(PAL::SessionID sessionID, CompletionHandler<void(WebCore::SWServerConnectionIdentifier&&)>&& completionHandler)
 {
     auto& server = m_networkProcess->swServerForSession(sessionID);
-    auto connection = makeUnique<WebSWServerConnection>(m_networkProcess, server, m_connection.get(), m_webProcessIdentifier, sessionID);
+    auto connection = makeUnique<WebSWServerConnection>(m_networkProcess, server, m_connection.get(), sessionID);
+    
+    SWServerConnectionIdentifier serverConnectionIdentifier = connection->identifier();
+    LOG(ServiceWorker, "NetworkConnectionToWebProcess::establishSWServerConnection - %s", serverConnectionIdentifier.loggingString().utf8().data());
 
-    ASSERT(!m_swConnections.contains(sessionID));
-    m_swConnections.add(sessionID, makeWeakPtr(*connection));
+    ASSERT(!m_swConnections.contains(serverConnectionIdentifier));
+    m_swConnections.add(serverConnectionIdentifier, makeWeakPtr(*connection));
     server.addConnection(WTFMove(connection));
+    completionHandler(WTFMove(serverConnectionIdentifier));
 }
-
-void NetworkConnectionToWebProcess::establishSWContextConnection(RegistrableDomain&& registrableDomain)
-{
-    m_swContextConnection = WebSWServerToContextConnection::create(m_networkProcess, registrableDomain, m_connection.get());
-
-    m_networkProcess->forEachSWServer([&](auto& server) {
-        server.serverToContextConnectionCreated(*m_swContextConnection);
-    });
-}
 #endif
 
 void NetworkConnectionToWebProcess::createNewMessagePortChannel(const MessagePortIdentifier& port1, const MessagePortIdentifier& port2)

Modified: trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -68,7 +68,6 @@
 class NetworkSocketStream;
 class WebIDBConnectionToClient;
 class WebSWServerConnection;
-class WebSWServerToContextConnection;
 typedef uint64_t ResourceLoadIdentifier;
 
 namespace NetworkCache {
@@ -207,8 +206,7 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    void establishSWServerConnection(PAL::SessionID);
-    void establishSWContextConnection(WebCore::RegistrableDomain&&);
+    void establishSWServerConnection(PAL::SessionID, CompletionHandler<void(WebCore::SWServerConnectionIdentifier&&)>&&);
     void unregisterSWConnections();
 #endif
 
@@ -319,8 +317,7 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    HashMap<PAL::SessionID, WeakPtr<WebSWServerConnection>> m_swConnections;
-    RefPtr<WebSWServerToContextConnection> m_swContextConnection;
+    HashMap<WebCore::SWServerConnectionIdentifier, WeakPtr<WebSWServerConnection>> m_swConnections;
 #endif
 
 #if ENABLE(APPLE_PAY_REMOTE_UI)

Modified: trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in	2019-09-12 23:59:32 UTC (rev 249820)
@@ -79,8 +79,7 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    EstablishSWServerConnection(PAL::SessionID sessionID)
-    EstablishSWContextConnection(WebCore::RegistrableDomain domain)
+    EstablishSWServerConnection(PAL::SessionID sessionID) -> (WebCore::SWServerConnectionIdentifier serverConnectionIdentifier) Synchronous
 #endif
 
     CreateNewMessagePortChannel(struct WebCore::MessagePortIdentifier port1, struct WebCore::MessagePortIdentifier port2)

Modified: trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -228,6 +228,17 @@
     }
 #endif
 
+#if ENABLE(SERVICE_WORKER)
+    if (decoder.messageReceiverName() == Messages::WebSWServerToContextConnection::messageReceiverName()) {
+        ASSERT(parentProcessHasServiceWorkerEntitlement());
+        if (!parentProcessHasServiceWorkerEntitlement())
+            return;
+        if (auto* webSWConnection = connectionToContextProcessFromIPCConnection(connection)) {
+            webSWConnection->didReceiveMessage(connection, decoder);
+            return;
+        }
+    }
+#endif
     didReceiveNetworkProcessMessage(connection, decoder);
 }
 
@@ -418,7 +429,7 @@
 #endif
 }
 
-void NetworkProcess::createNetworkConnectionToWebProcess(ProcessIdentifier identifier, CompletionHandler<void(Optional<IPC::Attachment>&&)>&& completionHandler)
+void NetworkProcess::createNetworkConnectionToWebProcess(ProcessIdentifier identifier, bool isServiceWorkerProcess, WebCore::RegistrableDomain&& registrableDomain, CompletionHandler<void(Optional<IPC::Attachment>&&)>&& completionHandler)
 {
     auto ipcConnection = createIPCConnectionToWebProcess();
     if (!ipcConnection) {
@@ -435,7 +446,25 @@
     completionHandler(WTFMove(ipcConnection->second));
 
     connection.setOnLineState(NetworkStateNotifier::singleton().onLine());
+    
+#if ENABLE(SERVICE_WORKER)
+    if (isServiceWorkerProcess) {
+        ASSERT(parentProcessHasServiceWorkerEntitlement());
+        ASSERT(m_waitingForServerToContextProcessConnection);
+        auto contextConnection = WebSWServerToContextConnection::create(*this, registrableDomain, connection.connection());
+        auto addResult = m_serverToContextConnections.add(WTFMove(registrableDomain), contextConnection.copyRef());
+        ASSERT_UNUSED(addResult, addResult.isNewEntry);
 
+        m_waitingForServerToContextProcessConnection = false;
+
+        for (auto* server : SWServer::allServers())
+            server->serverToContextConnectionCreated(contextConnection);
+    }
+#else
+    UNUSED_PARAM(isServiceWorkerProcess);
+    UNUSED_PARAM(registrableDomain);
+#endif
+
     m_storageManagerSet->addConnection(connection.connection());
 }
 
@@ -2372,12 +2401,38 @@
 #endif // ENABLE(SANDBOX_EXTENSIONS)
 
 #if ENABLE(SERVICE_WORKER)
-void NetworkProcess::forEachSWServer(const Function<void(SWServer&)>& callback)
+WebSWServerToContextConnection* NetworkProcess::connectionToContextProcessFromIPCConnection(IPC::Connection& connection)
 {
-    for (auto& server : m_swServers.values())
-        callback(*server);
+    for (auto& serverToContextConnection : m_serverToContextConnections.values()) {
+        if (serverToContextConnection->ipcConnection() == &connection)
+            return serverToContextConnection.get();
+    }
+    return nullptr;
 }
 
+void NetworkProcess::connectionToContextProcessWasClosed(Ref<WebSWServerToContextConnection>&& serverToContextConnection)
+{
+    auto& registrableDomain = serverToContextConnection->registrableDomain();
+    
+    serverToContextConnection->connectionClosed();
+    m_serverToContextConnections.remove(registrableDomain);
+    
+    for (auto& swServer : m_swServers.values())
+        swServer->markAllWorkersForRegistrableDomainAsTerminated(registrableDomain);
+    
+    if (needsServerToContextConnectionForRegistrableDomain(registrableDomain)) {
+        RELEASE_LOG(ServiceWorker, "Connection to service worker process was closed but is still needed, relaunching it");
+        createServerToContextConnection(registrableDomain, WTF::nullopt);
+    }
+}
+
+bool NetworkProcess::needsServerToContextConnectionForRegistrableDomain(const RegistrableDomain& registrableDomain) const
+{
+    return WTF::anyOf(m_swServers.values(), [&](auto& swServer) {
+        return swServer->needsServerToContextConnectionForRegistrableDomain(registrableDomain);
+    });
+}
+
 SWServer& NetworkProcess::swServerForSession(PAL::SessionID sessionID)
 {
     auto result = m_swServers.ensure(sessionID, [&] {
@@ -2403,36 +2458,40 @@
     return &static_cast<WebSWOriginStore&>(swServer->originStore());
 }
 
-bool NetworkProcess::needsServerToContextConnectionForRegistrableDomain(const RegistrableDomain& registrableDomain) const
-{
-    return WTF::anyOf(m_swServers.values(), [&](auto& swServer) {
-        return swServer->needsServerToContextConnectionForRegistrableDomain(registrableDomain);
-    });
-}
-
 WebSWServerToContextConnection* NetworkProcess::serverToContextConnectionForRegistrableDomain(const RegistrableDomain& registrableDomain)
 {
     return m_serverToContextConnections.get(registrableDomain);
 }
 
-void NetworkProcess::createServerToContextConnection(const RegistrableDomain& registrableDomain, PAL::SessionID sessionID)
+void NetworkProcess::createServerToContextConnection(const RegistrableDomain& registrableDomain, Optional<PAL::SessionID> sessionID)
 {
-    if (m_pendingConnectionDomains.contains(registrableDomain))
+    if (m_waitingForServerToContextProcessConnection)
         return;
-
-    m_pendingConnectionDomains.add(registrableDomain);
-    parentProcessConnection()->send(Messages::NetworkProcessProxy::EstablishWorkerContextConnectionToNetworkProcess { registrableDomain, sessionID }, 0);
+    
+    m_waitingForServerToContextProcessConnection = true;
+    if (sessionID)
+        parentProcessConnection()->send(Messages::NetworkProcessProxy::EstablishWorkerContextConnectionToNetworkProcessForExplicitSession(registrableDomain, *sessionID), 0);
+    else
+        parentProcessConnection()->send(Messages::NetworkProcessProxy::EstablishWorkerContextConnectionToNetworkProcess(registrableDomain), 0);
 }
 
-void NetworkProcess::postMessageToServiceWorkerClient(PAL::SessionID sessionID, const ServiceWorkerClientIdentifier& destinationIdentifier, MessageWithMessagePorts&& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
+void NetworkProcess::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, MessageWithMessagePorts&& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
 {
-    if (auto* connection = swServerForSession(sessionID).connection(destinationIdentifier.serverConnectionIdentifier))
+    if (auto connection = m_swServerConnections.get(destinationIdentifier.serverConnectionIdentifier))
         connection->postMessageToServiceWorkerClient(destinationIdentifier.contextIdentifier, WTFMove(message), sourceIdentifier, sourceOrigin);
 }
 
+void NetworkProcess::postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&& message, const WebCore::ServiceWorkerOrClientIdentifier& source, SWServerConnectionIdentifier connectionIdentifier)
+{
+    if (auto connection = m_swServerConnections.get(connectionIdentifier))
+        connection->postMessageToServiceWorker(destination, WTFMove(message), source);
+}
+
 void NetworkProcess::registerSWServerConnection(WebSWServerConnection& connection)
 {
     ASSERT(parentProcessHasServiceWorkerEntitlement());
+    ASSERT(!m_swServerConnections.contains(connection.identifier()));
+    m_swServerConnections.add(connection.identifier(), makeWeakPtr(connection));
     auto* store = existingSWOriginStoreForSession(connection.sessionID());
     ASSERT(store);
     if (store)
@@ -2441,23 +2500,28 @@
 
 void NetworkProcess::unregisterSWServerConnection(WebSWServerConnection& connection)
 {
+    ASSERT(m_swServerConnections.get(connection.identifier()).get() == &connection);
+    m_swServerConnections.remove(connection.identifier());
     if (auto* store = existingSWOriginStoreForSession(connection.sessionID()))
         store->unregisterSWServerConnection(connection);
 }
 
-void NetworkProcess::registerSWContextConnection(WebSWServerToContextConnection& connection)
+void NetworkProcess::swContextConnectionMayNoLongerBeNeeded(WebSWServerToContextConnection& serverToContextConnection)
 {
-    ASSERT(!m_serverToContextConnections.contains(connection.registrableDomain()));
-    m_pendingConnectionDomains.remove(connection.registrableDomain());
-    m_serverToContextConnections.add(connection.registrableDomain(), &connection);
+    auto& registrableDomain = serverToContextConnection.registrableDomain();
+    if (needsServerToContextConnectionForRegistrableDomain(registrableDomain))
+        return;
+    
+    RELEASE_LOG(ServiceWorker, "Service worker process is no longer needed, terminating it");
+    serverToContextConnection.terminate();
+    
+    for (auto& swServer : m_swServers.values())
+        swServer->markAllWorkersForRegistrableDomainAsTerminated(registrableDomain);
+    
+    serverToContextConnection.connectionClosed();
+    m_serverToContextConnections.remove(registrableDomain);
 }
 
-void NetworkProcess::unregisterSWContextConnection(WebSWServerToContextConnection& connection)
-{
-    ASSERT(m_serverToContextConnections.get(connection.registrableDomain()) == &connection);
-    m_serverToContextConnections.remove(connection.registrableDomain());
-}
-
 void NetworkProcess::disableServiceWorkerProcessTerminationDelay()
 {
     if (m_shouldDisableServiceWorkerProcessTerminationDelay)

Modified: trunk/Source/WebKit/NetworkProcess/NetworkProcess.h (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/NetworkProcess.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/NetworkProcess.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -298,17 +298,16 @@
 
 #if ENABLE(SERVICE_WORKER)
     WebSWServerToContextConnection* serverToContextConnectionForRegistrableDomain(const WebCore::RegistrableDomain&);
-    void createServerToContextConnection(const WebCore::RegistrableDomain&, PAL::SessionID);
+    void createServerToContextConnection(const WebCore::RegistrableDomain&, Optional<PAL::SessionID>);
     
     WebCore::SWServer& swServerForSession(PAL::SessionID);
     void registerSWServerConnection(WebSWServerConnection&);
     void unregisterSWServerConnection(WebSWServerConnection&);
     
-    void registerSWContextConnection(WebSWServerToContextConnection&);
-    void unregisterSWContextConnection(WebSWServerToContextConnection&);
-
-    bool needsServerToContextConnectionForRegistrableDomain(const WebCore::RegistrableDomain&) const;
-    void forEachSWServer(const Function<void(WebCore::SWServer&)>&);
+    void swContextConnectionMayNoLongerBeNeeded(WebSWServerToContextConnection&);
+    
+    WebSWServerToContextConnection* connectionToContextProcessFromIPCConnection(IPC::Connection&);
+    void connectionToContextProcessWasClosed(Ref<WebSWServerToContextConnection>&&);
 #endif
 
 #if PLATFORM(IOS_FAMILY)
@@ -394,7 +393,7 @@
     // Message Handlers
     void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
     void initializeNetworkProcess(NetworkProcessCreationParameters&&);
-    void createNetworkConnectionToWebProcess(WebCore::ProcessIdentifier, CompletionHandler<void(Optional<IPC::Attachment>&&)>&&);
+    void createNetworkConnectionToWebProcess(WebCore::ProcessIdentifier, bool isServiceWorkerProcess, WebCore::RegistrableDomain&&, CompletionHandler<void(Optional<IPC::Attachment>&&)>&&);
 
     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, uint64_t callbackID);
@@ -463,12 +462,13 @@
 #if ENABLE(SERVICE_WORKER)
     void didCreateWorkerContextProcessConnection(const IPC::Attachment&);
     
-    void postMessageToServiceWorkerClient(PAL::SessionID, const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
-    void postMessageToServiceWorker(PAL::SessionID, WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source, WebCore::SWServerConnectionIdentifier);
+    void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
+    void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source, WebCore::SWServerConnectionIdentifier);
     
     void disableServiceWorkerProcessTerminationDelay();
     
     WebSWOriginStore* existingSWOriginStoreForSession(PAL::SessionID) const;
+    bool needsServerToContextConnectionForRegistrableDomain(const WebCore::RegistrableDomain&) const;
 
     void addServiceWorkerSession(PAL::SessionID, String& serviceWorkerRegistrationDirectory, const SandboxExtension::Handle&);
 #endif
@@ -539,11 +539,12 @@
     Lock m_storageTaskMutex;
     
 #if ENABLE(SERVICE_WORKER)
-    HashMap<WebCore::RegistrableDomain, WebSWServerToContextConnection*> m_serverToContextConnections;
+    HashMap<WebCore::RegistrableDomain, RefPtr<WebSWServerToContextConnection>> m_serverToContextConnections;
+    bool m_waitingForServerToContextProcessConnection { false };
     bool m_shouldDisableServiceWorkerProcessTerminationDelay { false };
     HashMap<PAL::SessionID, String> m_swDatabasePaths;
     HashMap<PAL::SessionID, std::unique_ptr<WebCore::SWServer>> m_swServers;
-    HashSet<WebCore::RegistrableDomain> m_pendingConnectionDomains;
+    HashMap<WebCore::SWServerConnectionIdentifier, WeakPtr<WebSWServerConnection>> m_swServerConnections;
 #endif
 
 #if PLATFORM(COCOA)

Modified: trunk/Source/WebKit/NetworkProcess/NetworkProcess.messages.in (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/NetworkProcess.messages.in	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/NetworkProcess.messages.in	2019-09-12 23:59:32 UTC (rev 249820)
@@ -25,7 +25,7 @@
     InitializeNetworkProcess(struct WebKit::NetworkProcessCreationParameters processCreationParameters)
 
     # Creates a connection for communication with a WebProcess
-    CreateNetworkConnectionToWebProcess(WebCore::ProcessIdentifier processIdentifier) -> (Optional<IPC::Attachment> connectionIdentifier) Async
+    CreateNetworkConnectionToWebProcess(WebCore::ProcessIdentifier processIdentifier, bool isServiceWorkerProcess, WebCore::RegistrableDomain registrableDomain) -> (Optional<IPC::Attachment> connectionIdentifier) Async
 
 #if USE(SOUP)
     SetIgnoreTLSErrors(bool ignoreTLSErrors)
@@ -151,7 +151,10 @@
     SetCacheStorageParameters(PAL::SessionID sessionID, String cacheStorageDirectory, WebKit::SandboxExtension::Handle handle);
 
 #if ENABLE(SERVICE_WORKER)
-    PostMessageToServiceWorkerClient(PAL::SessionID sessionID, struct WebCore::ServiceWorkerClientIdentifier destinationIdentifier, struct WebCore::MessageWithMessagePorts message, WebCore::ServiceWorkerIdentifier sourceIdentifier, String sourceOrigin)
+    PostMessageToServiceWorkerClient(struct WebCore::ServiceWorkerClientIdentifier destinationIdentifier, struct WebCore::MessageWithMessagePorts message, WebCore::ServiceWorkerIdentifier sourceIdentifier, String sourceOrigin)
+
+    PostMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, struct WebCore::MessageWithMessagePorts message, WebCore::ServiceWorkerOrClientIdentifier source, WebCore::SWServerConnectionIdentifier connectionIdentifier)
+
     DisableServiceWorkerProcessTerminationDelay()
 #endif
 

Modified: trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerConnection.cpp (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerConnection.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerConnection.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -61,8 +61,8 @@
 #define SWSERVERCONNECTION_RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(m_sessionID.isAlwaysOnLoggingAllowed(), ServiceWorker, "%p - WebSWServerConnection::" fmt, this, ##__VA_ARGS__)
 #define SWSERVERCONNECTION_RELEASE_LOG_ERROR_IF_ALLOWED(fmt, ...) RELEASE_LOG_ERROR_IF(m_sessionID.isAlwaysOnLoggingAllowed(), ServiceWorker, "%p - WebSWServerConnection::" fmt, this, ##__VA_ARGS__)
 
-WebSWServerConnection::WebSWServerConnection(NetworkProcess& networkProcess, SWServer& server, IPC::Connection& connection, ProcessIdentifier processIdentifier, SessionID sessionID)
-    : SWServer::Connection(server, processIdentifier)
+WebSWServerConnection::WebSWServerConnection(NetworkProcess& networkProcess, SWServer& server, IPC::Connection& connection, SessionID sessionID)
+    : SWServer::Connection(server)
     , m_sessionID(sessionID)
     , m_contentConnection(connection)
     , m_networkProcess(networkProcess)
@@ -253,13 +253,13 @@
     server().scheduleJob(WTFMove(jobData));
 }
 
-void WebSWServerConnection::postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, const MessageWithMessagePorts& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
+void WebSWServerConnection::postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, MessageWithMessagePorts&& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
 {
     auto* sourceServiceWorker = server().workerByID(sourceIdentifier);
     if (!sourceServiceWorker)
         return;
 
-    send(Messages::WebSWClientConnection::PostMessageToServiceWorkerClient { destinationContextIdentifier, message, sourceServiceWorker->data(), sourceOrigin });
+    send(Messages::WebSWClientConnection::PostMessageToServiceWorkerClient { destinationContextIdentifier, WTFMove(message), sourceServiceWorker->data(), sourceOrigin });
 }
 
 void WebSWServerConnection::matchRegistration(uint64_t registrationMatchRequestIdentifier, const SecurityOriginData& topOrigin, const URL& clientURL)

Modified: trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerConnection.h (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerConnection.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerConnection.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -31,7 +31,6 @@
 #include "MessageSender.h"
 #include "ServiceWorkerFetchTask.h"
 #include <WebCore/FetchIdentifier.h>
-#include <WebCore/ProcessIdentifier.h>
 #include <WebCore/SWServer.h>
 #include <pal/SessionID.h>
 #include <wtf/HashMap.h>
@@ -54,7 +53,7 @@
 
 class WebSWServerConnection : public WebCore::SWServer::Connection, public IPC::MessageSender, public IPC::MessageReceiver {
 public:
-    WebSWServerConnection(NetworkProcess&, WebCore::SWServer&, IPC::Connection&, WebCore::ProcessIdentifier, PAL::SessionID);
+    WebSWServerConnection(NetworkProcess&, WebCore::SWServer&, IPC::Connection&, PAL::SessionID);
     WebSWServerConnection(const WebSWServerConnection&) = delete;
     ~WebSWServerConnection() final;
 
@@ -65,6 +64,9 @@
 
     PAL::SessionID sessionID() const { return m_sessionID; }
 
+    void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceServiceWorkerIdentifier, const String& sourceOrigin);
+    void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source);
+
 private:
     // Implement SWServer::Connection (Messages to the client WebProcess)
     void rejectJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ExceptionData&) final;
@@ -92,8 +94,6 @@
     void unregisterServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier&);
     void syncTerminateWorkerFromClient(WebCore::ServiceWorkerIdentifier&&, CompletionHandler<void()>&&);
 
-    void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceServiceWorkerIdentifier, const String& sourceOrigin) final;
-
     void serverToContextConnectionCreated(WebCore::SWServerToContextConnection&) final;
 
     bool isThrottleable() const { return m_isThrottleable; }
@@ -102,10 +102,8 @@
     void setThrottleState(bool isThrottleable);
     void updateThrottleState();
 
-    void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source);
-
     IPC::Connection* messageSenderConnection() const final { return m_contentConnection.ptr(); }
-    uint64_t messageSenderDestinationID() const final { return m_sessionID.toUInt64(); }
+    uint64_t messageSenderDestinationID() const final { return identifier().toUInt64(); }
     
     template<typename U> static void sendToContextProcess(WebCore::SWServerToContextConnection&, U&& message);
 

Modified: trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.cpp (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -29,13 +29,11 @@
 #if ENABLE(SERVICE_WORKER)
 
 #include "FormDataReference.h"
-#include "Logging.h"
 #include "NetworkProcess.h"
 #include "ServiceWorkerFetchTask.h"
 #include "ServiceWorkerFetchTaskMessages.h"
 #include "WebCoreArgumentCoders.h"
 #include "WebSWContextManagerConnectionMessages.h"
-#include <WebCore/SWServer.h>
 #include <WebCore/ServiceWorkerContextData.h>
 
 namespace WebKit {
@@ -46,13 +44,9 @@
     , m_ipcConnection(WTFMove(connection))
     , m_networkProcess(networkProcess)
 {
-    m_networkProcess->registerSWContextConnection(*this);
 }
 
-WebSWServerToContextConnection::~WebSWServerToContextConnection()
-{
-    m_networkProcess->unregisterSWContextConnection(*this);
-}
+WebSWServerToContextConnection::~WebSWServerToContextConnection() = default;
 
 IPC::Connection* WebSWServerToContextConnection::messageSenderConnection() const
 {
@@ -71,12 +65,6 @@
         fetch->fail(ResourceError { errorDomainWebKitInternal, 0, { }, "Service Worker context closed"_s });
 }
 
-void WebSWServerToContextConnection::postMessageToServiceWorkerClient(PAL::SessionID sessionID, const ServiceWorkerClientIdentifier& destinationIdentifier, const MessageWithMessagePorts& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
-{
-    if (auto* connection = m_networkProcess->swServerForSession(sessionID).connection(destinationIdentifier.serverConnectionIdentifier))
-        connection->postMessageToServiceWorkerClient(destinationIdentifier.contextIdentifier, message, sourceIdentifier, sourceOrigin);
-}
-
 void WebSWServerToContextConnection::installServiceWorkerContext(const ServiceWorkerContextData& data, PAL::SessionID sessionID, const String& userAgent)
 {
     send(Messages::WebSWContextManagerConnection::InstallServiceWorker { data, sessionID, userAgent });
@@ -129,17 +117,7 @@
 
 void WebSWServerToContextConnection::connectionMayNoLongerBeNeeded()
 {
-    if (m_networkProcess->needsServerToContextConnectionForRegistrableDomain(registrableDomain()))
-        return;
-
-    RELEASE_LOG(ServiceWorker, "Service worker process is no longer needed, terminating it");
-    terminate();
-
-    m_networkProcess->forEachSWServer([&](auto& server) {
-        server.markAllWorkersForRegistrableDomainAsTerminated(registrableDomain());
-    });
-
-    connectionClosed();
+    m_networkProcess->swContextConnectionMayNoLongerBeNeeded(*this);
 }
 
 void WebSWServerToContextConnection::setThrottleState(bool isThrottleable)

Modified: trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.h (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -34,7 +34,6 @@
 
 namespace WebCore {
 struct FetchOptions;
-struct MessageWithMessagePorts;
 class ResourceRequest;
 }
 
@@ -83,8 +82,6 @@
     IPC::Connection* messageSenderConnection() const final;
     uint64_t messageSenderDestinationID() const final;
 
-    void postMessageToServiceWorkerClient(PAL::SessionID, const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
-
     // Messages to the SW host WebProcess
     void installServiceWorkerContext(const WebCore::ServiceWorkerContextData&, PAL::SessionID, const String& userAgent) final;
     void fireInstallEvent(WebCore::ServiceWorkerIdentifier) final;

Modified: trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.messages.in (249819 => 249820)


--- trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.messages.in	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/NetworkProcess/ServiceWorker/WebSWServerToContextConnection.messages.in	2019-09-12 23:59:32 UTC (rev 249820)
@@ -36,7 +36,6 @@
     MatchAll(uint64_t matchAllRequestIdentifier, WebCore::ServiceWorkerIdentifier serviceWorkerIdentifier, struct WebCore::ServiceWorkerClientQueryOptions options);
     Claim(uint64_t claimRequestIdentifier, WebCore::ServiceWorkerIdentifier serviceWorkerIdentifier);
     SetScriptResource(WebCore::ServiceWorkerIdentifier identifier, URL scriptURL, String script, URL responseURL, String mimeType);
-    PostMessageToServiceWorkerClient(PAL::SessionID sessionID, struct WebCore::ServiceWorkerClientIdentifier destination, struct WebCore::MessageWithMessagePorts message, WebCore::ServiceWorkerIdentifier source, String sourceOrigin)
 }
 
 #endif // ENABLE(SERVICE_WORKER)

Modified: trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -142,7 +142,16 @@
 
 void NetworkProcessProxy::openNetworkProcessConnection(uint64_t connectionRequestIdentifier, WebProcessProxy& webProcessProxy)
 {
-    connection()->sendWithAsyncReply(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess { webProcessProxy.coreProcessIdentifier() }, [this, weakThis = makeWeakPtr(this), webProcessProxy = makeWeakPtr(webProcessProxy), connectionRequestIdentifier](auto&& connectionIdentifier) mutable {
+    bool isServiceWorkerProcess = false;
+    RegistrableDomain registrableDomain;
+#if ENABLE(SERVICE_WORKER)
+    if (webProcessProxy.isRunningServiceWorkers()) {
+        isServiceWorkerProcess = true;
+        registrableDomain = webProcessProxy.registrableDomain();
+    }
+#endif
+
+    connection()->sendWithAsyncReply(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess { webProcessProxy.coreProcessIdentifier(), isServiceWorkerProcess, registrableDomain }, [this, weakThis = makeWeakPtr(this), webProcessProxy = makeWeakPtr(webProcessProxy), connectionRequestIdentifier](auto&& connectionIdentifier) mutable {
         if (!weakThis)
             return;
 
@@ -1200,8 +1209,13 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcess(RegistrableDomain&& registrableDomain, PAL::SessionID sessionID)
+void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcess(RegistrableDomain&& registrableDomain)
 {
+    m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(registrableDomain), WTF::nullopt);
+}
+
+void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcessForExplicitSession(RegistrableDomain&& registrableDomain, PAL::SessionID sessionID)
+{
     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(registrableDomain), sessionID);
 }
 #endif

Modified: trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -238,7 +238,8 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    void establishWorkerContextConnectionToNetworkProcess(WebCore::RegistrableDomain&&, PAL::SessionID);
+    void establishWorkerContextConnectionToNetworkProcess(WebCore::RegistrableDomain&&);
+    void establishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::RegistrableDomain&&, PAL::SessionID);
 #endif
 
     void requestStorageSpace(PAL::SessionID, const WebCore::ClientOrigin&, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t> quota)>&&);

Modified: trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.messages.in (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.messages.in	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.messages.in	2019-09-12 23:59:32 UTC (rev 249820)
@@ -61,7 +61,8 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    EstablishWorkerContextConnectionToNetworkProcess(WebCore::RegistrableDomain registrableDomain, PAL::SessionID sessionID)
+    EstablishWorkerContextConnectionToNetworkProcess(WebCore::RegistrableDomain registrableDomain)
+    EstablishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::RegistrableDomain registrableDomain, PAL::SessionID explicitSession)
 #endif
 
     RequestStorageSpace(PAL::SessionID sessionID, struct WebCore::ClientOrigin origin, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired) -> (Optional<uint64_t> newQuota) Async

Modified: trunk/Source/WebKit/UIProcess/WebProcessPool.cpp (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/WebProcessPool.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/WebProcessPool.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -687,13 +687,18 @@
 }
 
 #if ENABLE(SERVICE_WORKER)
-void WebProcessPool::establishWorkerContextConnectionToNetworkProcess(NetworkProcessProxy& proxy, RegistrableDomain&& registrableDomain, PAL::SessionID sessionID)
+void WebProcessPool::establishWorkerContextConnectionToNetworkProcess(NetworkProcessProxy& proxy, RegistrableDomain&& registrableDomain, Optional<PAL::SessionID> sessionID)
 {
     ASSERT_UNUSED(proxy, &proxy == m_networkProcess.get());
 
+    if (m_serviceWorkerProcesses.contains(registrableDomain))
+        return;
+
     m_mayHaveRegisteredServiceWorkers.clear();
 
-    auto* websiteDataStore = WebsiteDataStore::existingNonDefaultDataStoreForSessionID(sessionID);
+    WebsiteDataStore* websiteDataStore = nullptr;
+    if (sessionID)
+        websiteDataStore = WebsiteDataStore::existingNonDefaultDataStoreForSessionID(*sessionID);
 
     if (!websiteDataStore) {
         if (!m_websiteDataStore)
@@ -701,9 +706,6 @@
         websiteDataStore = &m_websiteDataStore->websiteDataStore();
     }
 
-    if (m_serviceWorkerProcesses.contains(registrableDomain))
-        return;
-
     if (m_serviceWorkerProcesses.isEmpty())
         sendToAllProcesses(Messages::WebProcess::RegisterServiceWorkerClients { });
 
@@ -716,7 +718,7 @@
     auto* serviceWorkerProcessProxyPtr = serviceWorkerProcessProxy.ptr();
     m_processes.append(WTFMove(serviceWorkerProcessProxy));
 
-    serviceWorkerProcessProxyPtr->establishServiceWorkerContext(m_serviceWorkerPreferences ? m_serviceWorkerPreferences.value() : m_defaultPageGroup->preferences().store());
+    serviceWorkerProcessProxyPtr->establishServiceWorkerContext(m_serviceWorkerPreferences ? m_serviceWorkerPreferences.value() : m_defaultPageGroup->preferences().store(), sessionID.valueOr(PAL::SessionID::defaultSessionID()));
     if (!m_serviceWorkerUserAgent.isNull())
         serviceWorkerProcessProxyPtr->setServiceWorkerUserAgent(m_serviceWorkerUserAgent);
 }

Modified: trunk/Source/WebKit/UIProcess/WebProcessPool.h (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/WebProcessPool.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/WebProcessPool.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -381,7 +381,7 @@
 
     bool isServiceWorkerPageID(WebPageProxyIdentifier) const;
 #if ENABLE(SERVICE_WORKER)
-    void establishWorkerContextConnectionToNetworkProcess(NetworkProcessProxy&, WebCore::RegistrableDomain&&, PAL::SessionID);
+    void establishWorkerContextConnectionToNetworkProcess(NetworkProcessProxy&, WebCore::RegistrableDomain&&, Optional<PAL::SessionID>);
     const HashMap<WebCore::RegistrableDomain, WebProcessProxy*>& serviceWorkerProxies() const { return m_serviceWorkerProcesses; }
     void setAllowsAnySSLCertificateForServiceWorker(bool allows) { m_allowsAnySSLCertificateForServiceWorker = allows; }
     bool allowsAnySSLCertificateForServiceWorker() const { return m_allowsAnySSLCertificateForServiceWorker; }

Modified: trunk/Source/WebKit/UIProcess/WebProcessProxy.cpp (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/WebProcessProxy.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/WebProcessProxy.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -1499,9 +1499,9 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-void WebProcessProxy::establishServiceWorkerContext(const WebPreferencesStore& store)
+void WebProcessProxy::establishServiceWorkerContext(const WebPreferencesStore& store, PAL::SessionID sessionID)
 {
-    send(Messages::WebProcess::EstablishWorkerContextConnectionToNetworkProcess { processPool().defaultPageGroup().pageGroupID(), m_serviceWorkerInformation->serviceWorkerPageProxyID, m_serviceWorkerInformation->serviceWorkerPageID, store, *m_registrableDomain, m_websiteDataStore->sessionID() }, 0);
+    send(Messages::WebProcess::EstablishWorkerContextConnectionToNetworkProcess { processPool().defaultPageGroup().pageGroupID(), m_serviceWorkerInformation->serviceWorkerPageProxyID, m_serviceWorkerInformation->serviceWorkerPageID, store, sessionID }, 0);
 }
 
 void WebProcessProxy::setServiceWorkerUserAgent(const String& userAgent)

Modified: trunk/Source/WebKit/UIProcess/WebProcessProxy.h (249819 => 249820)


--- trunk/Source/WebKit/UIProcess/WebProcessProxy.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/UIProcess/WebProcessProxy.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -313,7 +313,8 @@
     void deref() final { ThreadSafeRefCounted::deref(); }
 
 #if ENABLE(SERVICE_WORKER)
-    void establishServiceWorkerContext(const WebPreferencesStore&);
+    void establishServiceWorkerContext(const WebPreferencesStore&, PAL::SessionID);
+    void startForServiceWorkers(const WebPreferencesStore&, PAL::SessionID);
     void setServiceWorkerUserAgent(const String&);
     void updateServiceWorkerPreferencesStore(const WebPreferencesStore&);
     bool hasServiceWorkerPageProxy(WebPageProxyIdentifier pageProxyID) { return m_serviceWorkerInformation && m_serviceWorkerInformation->serviceWorkerPageProxyID == pageProxyID; }

Modified: trunk/Source/WebKit/WebProcess/Network/NetworkProcessConnection.cpp (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/Network/NetworkProcessConnection.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/Network/NetworkProcessConnection.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -137,7 +137,7 @@
 
 #if ENABLE(SERVICE_WORKER)
     if (decoder.messageReceiverName() == Messages::WebSWClientConnection::messageReceiverName()) {
-        auto serviceWorkerConnection = m_swConnectionsBySession.get(PAL::SessionID { decoder.destinationID() });
+        auto serviceWorkerConnection = m_swConnectionsByIdentifier.get(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID()));
         if (serviceWorkerConnection)
             serviceWorkerConnection->didReceiveMessage(connection, decoder);
         return;
@@ -200,7 +200,7 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    auto swConnections = std::exchange(m_swConnectionsBySession, { });
+    auto swConnections = std::exchange(m_swConnectionsByIdentifier, { });
     for (auto& connection : swConnections.values())
         connection->connectionToServerLost();
 #endif
@@ -264,6 +264,25 @@
         return WebSWClientConnection::create(sessionID);
     }).iterator->value;
 }
+
+void NetworkProcessConnection::removeSWClientConnection(WebSWClientConnection& connection)
+{
+    ASSERT(m_swConnectionsByIdentifier.contains(connection.serverConnectionIdentifier()));
+    m_swConnectionsByIdentifier.remove(connection.serverConnectionIdentifier());
+}
+
+SWServerConnectionIdentifier NetworkProcessConnection::initializeSWClientConnection(WebSWClientConnection& connection)
+{
+    ASSERT(connection.sessionID().isValid());
+    SWServerConnectionIdentifier identifier;
+    bool result = m_connection->sendSync(Messages::NetworkConnectionToWebProcess::EstablishSWServerConnection(connection.sessionID()), Messages::NetworkConnectionToWebProcess::EstablishSWServerConnection::Reply(identifier), 0);
+    ASSERT_UNUSED(result, result);
+
+    ASSERT(!m_swConnectionsByIdentifier.contains(identifier));
+    m_swConnectionsByIdentifier.add(identifier, &connection);
+
+    return identifier;
+}
 #endif
 
 void NetworkProcessConnection::messagesAvailableForPort(const WebCore::MessagePortIdentifier& messagePortIdentifier)

Modified: trunk/Source/WebKit/WebProcess/Network/NetworkProcessConnection.h (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/Network/NetworkProcessConnection.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/Network/NetworkProcessConnection.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -78,6 +78,9 @@
 #if ENABLE(SERVICE_WORKER)
     WebSWClientConnection* existingServiceWorkerConnectionForSession(PAL::SessionID sessionID) { return m_swConnectionsBySession.get(sessionID); }
     WebSWClientConnection& serviceWorkerConnectionForSession(PAL::SessionID);
+
+    WebCore::SWServerConnectionIdentifier initializeSWClientConnection(WebSWClientConnection&);
+    void removeSWClientConnection(WebSWClientConnection&);
 #endif
 
 private:
@@ -110,6 +113,7 @@
 
 #if ENABLE(SERVICE_WORKER)
     HashMap<PAL::SessionID, RefPtr<WebSWClientConnection>> m_swConnectionsBySession;
+    HashMap<WebCore::SWServerConnectionIdentifier, WebSWClientConnection*> m_swConnectionsByIdentifier;
 #endif
 };
 

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -31,7 +31,6 @@
 #include "DataReference.h"
 #include "FormDataReference.h"
 #include "Logging.h"
-#include "NetworkConnectionToWebProcessMessages.h"
 #include "NetworkProcessConnection.h"
 #include "NetworkProcessMessages.h"
 #include "ServiceWorkerClientFetch.h"
@@ -41,7 +40,6 @@
 #include "WebSWOriginTable.h"
 #include "WebSWServerConnectionMessages.h"
 #include <WebCore/Document.h>
-#include <WebCore/ProcessIdentifier.h>
 #include <WebCore/SecurityOrigin.h>
 #include <WebCore/SerializedScriptValue.h>
 #include <WebCore/ServiceWorkerClientData.h>
@@ -57,61 +55,77 @@
 
 WebSWClientConnection::WebSWClientConnection(SessionID sessionID)
     : m_sessionID(sessionID)
-    , m_identifier(Process::identifier())
     , m_swOriginTable(makeUniqueRef<WebSWOriginTable>())
 {
     ASSERT(m_sessionID.isValid());
-    send(Messages::NetworkConnectionToWebProcess::EstablishSWServerConnection { sessionID });
+    initializeConnectionIfNeeded();
 }
 
 WebSWClientConnection::~WebSWClientConnection()
 {
-    clear();
+    if (m_connection)
+        WebProcess::singleton().ensureNetworkProcessConnection().removeSWClientConnection(*this);
 }
 
-IPC::Connection* WebSWClientConnection::messageSenderConnection() const
+void WebSWClientConnection::initializeConnectionIfNeeded()
 {
-    return &WebProcess::singleton().ensureNetworkProcessConnection().connection();
+    if (m_connection)
+        return;
+
+    auto& networkProcessConnection = WebProcess::singleton().ensureNetworkProcessConnection();
+
+    m_connection = &networkProcessConnection.connection();
+    m_identifier = networkProcessConnection.initializeSWClientConnection(*this);
+
+    updateThrottleState();
 }
 
+template<typename U>
+void WebSWClientConnection::ensureConnectionAndSend(const U& message)
+{
+    initializeConnectionIfNeeded();
+    if (m_connection)
+        send(message);
+}
+
 void WebSWClientConnection::scheduleJobInServer(const ServiceWorkerJobData& jobData)
 {
-    send(Messages::WebSWServerConnection::ScheduleJobInServer { jobData });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::ScheduleJobInServer(jobData));
 }
 
 void WebSWClientConnection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
 {
-    send(Messages::WebSWServerConnection::FinishFetchingScriptInServer { result });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::FinishFetchingScriptInServer(result));
 }
 
 void WebSWClientConnection::addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
 {
-    send(Messages::WebSWServerConnection::AddServiceWorkerRegistrationInServer { identifier });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::AddServiceWorkerRegistrationInServer(identifier));
 }
 
 void WebSWClientConnection::removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
 {
-    send(Messages::WebSWServerConnection::RemoveServiceWorkerRegistrationInServer { identifier });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::RemoveServiceWorkerRegistrationInServer(identifier));
 }
 
 void WebSWClientConnection::postMessageToServiceWorker(ServiceWorkerIdentifier destinationIdentifier, MessageWithMessagePorts&& message, const ServiceWorkerOrClientIdentifier& sourceIdentifier)
 {
-    send(Messages::WebSWServerConnection::PostMessageToServiceWorker { destinationIdentifier, WTFMove(message), sourceIdentifier });
+    WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkProcess::PostMessageToServiceWorker { destinationIdentifier, WTFMove(message), sourceIdentifier, serverConnectionIdentifier() }, 0);
 }
 
 void WebSWClientConnection::registerServiceWorkerClient(const SecurityOrigin& topOrigin, const WebCore::ServiceWorkerClientData& data, const Optional<WebCore::ServiceWorkerRegistrationIdentifier>& controllingServiceWorkerRegistrationIdentifier, const String& userAgent)
 {
-    send(Messages::WebSWServerConnection::RegisterServiceWorkerClient { topOrigin.data(), data, controllingServiceWorkerRegistrationIdentifier, userAgent });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::RegisterServiceWorkerClient { topOrigin.data(), data, controllingServiceWorkerRegistrationIdentifier, userAgent });
 }
 
 void WebSWClientConnection::unregisterServiceWorkerClient(DocumentIdentifier contextIdentifier)
 {
-    send(Messages::WebSWServerConnection::UnregisterServiceWorkerClient { ServiceWorkerClientIdentifier { serverConnectionIdentifier(), contextIdentifier } });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::UnregisterServiceWorkerClient { ServiceWorkerClientIdentifier { serverConnectionIdentifier(), contextIdentifier } });
 }
 
 void WebSWClientConnection::didResolveRegistrationPromise(const ServiceWorkerRegistrationKey& key)
 {
-    send(Messages::WebSWServerConnection::DidResolveRegistrationPromise { key });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::DidResolveRegistrationPromise(key));
 }
 
 bool WebSWClientConnection::mayHaveServiceWorkerRegisteredForOrigin(const SecurityOriginData& origin) const
@@ -162,11 +176,11 @@
     runOrDelayTaskForImport([this, callback = WTFMove(callback), topOrigin = WTFMove(topOrigin), clientURL]() mutable {
         uint64_t callbackID = ++m_previousCallbackIdentifier;
         m_ongoingMatchRegistrationTasks.add(callbackID, WTFMove(callback));
-        send(Messages::WebSWServerConnection::MatchRegistration(callbackID, topOrigin, clientURL));
+        ensureConnectionAndSend(Messages::WebSWServerConnection::MatchRegistration(callbackID, topOrigin, clientURL));
     });
 }
 
-void WebSWClientConnection::runOrDelayTaskForImport(Function<void()>&& task)
+void WebSWClientConnection::runOrDelayTaskForImport(WTF::Function<void()>&& task)
 {
     if (m_swOriginTable->isImported()) {
         task();
@@ -173,6 +187,7 @@
         return;
     }
     m_tasksPendingOriginImport.append(WTFMove(task));
+    initializeConnectionIfNeeded();
 }
 
 void WebSWClientConnection::whenRegistrationReady(const SecurityOriginData& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&& callback)
@@ -179,7 +194,7 @@
 {
     uint64_t callbackID = ++m_previousCallbackIdentifier;
     m_ongoingRegistrationReadyTasks.add(callbackID, WTFMove(callback));
-    send(Messages::WebSWServerConnection::WhenRegistrationReady(callbackID, topOrigin, clientURL));
+    ensureConnectionAndSend(Messages::WebSWServerConnection::WhenRegistrationReady(callbackID, topOrigin, clientURL));
 }
 
 void WebSWClientConnection::registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&& registrationData)
@@ -201,32 +216,29 @@
     runOrDelayTaskForImport([this, callback = WTFMove(callback), topOrigin = WTFMove(topOrigin), clientURL]() mutable {
         uint64_t callbackID = ++m_previousCallbackIdentifier;
         m_ongoingGetRegistrationsTasks.add(callbackID, WTFMove(callback));
-        send(Messages::WebSWServerConnection::GetRegistrations { callbackID, topOrigin, clientURL });
+        ensureConnectionAndSend(Messages::WebSWServerConnection::GetRegistrations(callbackID, topOrigin, clientURL));
     });
 }
 
 void WebSWClientConnection::startFetch(FetchIdentifier fetchIdentifier, ServiceWorkerRegistrationIdentifier serviceWorkerRegistrationIdentifier, const ResourceRequest& request, const FetchOptions& options, const String& referrer)
 {
-    send(Messages::WebSWServerConnection::StartFetch { serviceWorkerRegistrationIdentifier, fetchIdentifier, request, options, IPC::FormDataReference { request.httpBody() }, referrer });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::StartFetch { serviceWorkerRegistrationIdentifier, fetchIdentifier, request, options, IPC::FormDataReference { request.httpBody() }, referrer });
 }
 
 void WebSWClientConnection::cancelFetch(FetchIdentifier fetchIdentifier, ServiceWorkerRegistrationIdentifier serviceWorkerRegistrationIdentifier)
 {
-    send(Messages::WebSWServerConnection::CancelFetch { serviceWorkerRegistrationIdentifier, fetchIdentifier });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::CancelFetch { serviceWorkerRegistrationIdentifier, fetchIdentifier });
 }
 
 void WebSWClientConnection::continueDidReceiveFetchResponse(FetchIdentifier fetchIdentifier, ServiceWorkerRegistrationIdentifier serviceWorkerRegistrationIdentifier)
 {
-    send(Messages::WebSWServerConnection::ContinueDidReceiveFetchResponse { serviceWorkerRegistrationIdentifier, fetchIdentifier });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::ContinueDidReceiveFetchResponse { serviceWorkerRegistrationIdentifier, fetchIdentifier });
 }
 
 void WebSWClientConnection::connectionToServerLost()
 {
-    clear();
-}
+    m_connection = nullptr;
 
-void WebSWClientConnection::clear()
-{
     auto registrationTasks = WTFMove(m_ongoingMatchRegistrationTasks);
     for (auto& callback : registrationTasks.values())
         callback(WTF::nullopt);
@@ -235,26 +247,31 @@
     for (auto& callback : getRegistrationTasks.values())
         callback({ });
 
-    auto registrationReadyTasks = WTFMove(m_ongoingRegistrationReadyTasks);
-    for (auto& callback : registrationReadyTasks.values())
-        callback({ });
-
     clearPendingJobs();
 }
 
 void WebSWClientConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
 {
-    sendSync(Messages::WebSWServerConnection::SyncTerminateWorkerFromClient { identifier }, Messages::WebSWServerConnection::SyncTerminateWorkerFromClient::Reply());
+    initializeConnectionIfNeeded();
+
+    sendSync(Messages::WebSWServerConnection::SyncTerminateWorkerFromClient(identifier), Messages::WebSWServerConnection::SyncTerminateWorkerFromClient::Reply());
 }
 
+WebCore::SWServerConnectionIdentifier WebSWClientConnection::serverConnectionIdentifier() const
+{
+    const_cast<WebSWClientConnection*>(this)->initializeConnectionIfNeeded();
+    return m_identifier;
+}
+
 void WebSWClientConnection::updateThrottleState()
 {
     m_isThrottleable = WebProcess::singleton().areAllPagesThrottleable();
-    send(Messages::WebSWServerConnection::SetThrottleState { m_isThrottleable });
+    ensureConnectionAndSend(Messages::WebSWServerConnection::SetThrottleState { m_isThrottleable });
 }
 
 void WebSWClientConnection::storeRegistrationsOnDiskForTesting(CompletionHandler<void()>&& callback)
 {
+    initializeConnectionIfNeeded();
     sendWithAsyncReply(Messages::WebSWServerConnection::StoreRegistrationsOnDisk { }, WTFMove(callback));
 }
 

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -52,7 +52,7 @@
     static Ref<WebSWClientConnection> create(PAL::SessionID sessionID) { return adoptRef(*new WebSWClientConnection { sessionID }); }
     ~WebSWClientConnection();
 
-    WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final { return m_identifier; }
+    WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final;
 
     void addServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
     void removeServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
@@ -74,6 +74,8 @@
 private:
     explicit WebSWClientConnection(PAL::SessionID);
 
+    void initializeConnectionIfNeeded();
+
     void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final;
     void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final;
     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source) final;
@@ -95,19 +97,20 @@
 
     void scheduleStorageJob(const WebCore::ServiceWorkerJobData&);
 
-    void runOrDelayTaskForImport(Function<void()>&& task);
+    void runOrDelayTaskForImport(WTF::Function<void()>&& task);
 
-    IPC::Connection* messageSenderConnection() const final;
-    uint64_t messageSenderDestinationID() const final { return m_sessionID.toUInt64(); }
+    IPC::Connection* messageSenderConnection() const final { return m_connection.get(); }
+    uint64_t messageSenderDestinationID() const final { return m_identifier.toUInt64(); }
 
     void setSWOriginTableSharedMemory(const SharedMemory::Handle&);
     void setSWOriginTableIsImported();
 
-    void clear();
+    template<typename U> void ensureConnectionAndSend(const U& message);
 
     PAL::SessionID m_sessionID;
     WebCore::SWServerConnectionIdentifier m_identifier;
 
+    RefPtr<IPC::Connection> m_connection;
     UniqueRef<WebSWOriginTable> m_swOriginTable;
 
     uint64_t m_previousCallbackIdentifier { 0 };
@@ -114,7 +117,7 @@
     HashMap<uint64_t, RegistrationCallback> m_ongoingMatchRegistrationTasks;
     HashMap<uint64_t, GetRegistrationsCallback> m_ongoingGetRegistrationsTasks;
     HashMap<uint64_t, WhenRegistrationReadyCallback> m_ongoingRegistrationReadyTasks;
-    Deque<Function<void()>> m_tasksPendingOriginImport;
+    Deque<WTF::Function<void()>> m_tasksPendingOriginImport;
     bool m_isThrottleable { true };
 }; // class WebSWServerConnection
 

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -31,7 +31,6 @@
 #include "DataReference.h"
 #include "FormDataReference.h"
 #include "Logging.h"
-#include "NetworkConnectionToWebProcessMessages.h"
 #include "NetworkProcessMessages.h"
 #include "ServiceWorkerFetchTaskMessages.h"
 #include "WebCacheStorageProvider.h"
@@ -87,9 +86,8 @@
     return WebDocumentLoader::create(request, substituteData);
 }
 
-WebSWContextManagerConnection::WebSWContextManagerConnection(Ref<IPC::Connection>&& connection, WebCore::RegistrableDomain&& registrableDomain, uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store)
+WebSWContextManagerConnection::WebSWContextManagerConnection(Ref<IPC::Connection>&& connection, uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store)
     : m_connectionToNetworkProcess(WTFMove(connection))
-    , m_registrableDomain(WTFMove(registrableDomain))
     , m_pageGroupID(pageGroupID)
     , m_webPageProxyID(webPageProxyID)
     , m_pageID(pageID)
@@ -100,7 +98,6 @@
 #endif
 {
     updatePreferencesStore(store);
-    m_connectionToNetworkProcess->send(Messages::NetworkConnectionToWebProcess::EstablishSWContextConnection { m_registrableDomain }, 0);
 }
 
 WebSWContextManagerConnection::~WebSWContextManagerConnection() = default;
@@ -263,9 +260,9 @@
     SWContextManager::singleton().terminateWorker(identifier, syncWorkerTerminationTimeout, WTFMove(reply));
 }
 
-void WebSWContextManagerConnection::postMessageToServiceWorkerClient(PAL::SessionID sessionID, const ServiceWorkerClientIdentifier& destinationIdentifier, const MessageWithMessagePorts& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
+void WebSWContextManagerConnection::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, MessageWithMessagePorts&& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
 {
-    m_connectionToNetworkProcess->send(Messages::WebSWServerToContextConnection::PostMessageToServiceWorkerClient(sessionID, destinationIdentifier, message, sourceIdentifier, sourceOrigin), 0);
+    m_connectionToNetworkProcess->send(Messages::NetworkProcess::PostMessageToServiceWorkerClient(destinationIdentifier, WTFMove(message), sourceIdentifier, sourceOrigin), 0);
 }
 
 void WebSWContextManagerConnection::didFinishInstall(Optional<ServiceWorkerJobDataIdentifier> jobDataIdentifier, ServiceWorkerIdentifier serviceWorkerIdentifier, bool wasSuccessful)

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -53,7 +53,7 @@
 
 class WebSWContextManagerConnection final : public WebCore::SWContextManager::Connection, public IPC::MessageReceiver {
 public:
-    WebSWContextManagerConnection(Ref<IPC::Connection>&&, WebCore::RegistrableDomain&&, uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&);
+    WebSWContextManagerConnection(Ref<IPC::Connection>&&, uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&);
     ~WebSWContextManagerConnection();
 
     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
@@ -65,7 +65,7 @@
     void updatePreferencesStore(const WebPreferencesStore&);
 
     // WebCore::SWContextManager::Connection.
-    void postMessageToServiceWorkerClient(PAL::SessionID, const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin) final;
+    void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin) final;
     void didFinishInstall(Optional<WebCore::ServiceWorkerJobDataIdentifier>, WebCore::ServiceWorkerIdentifier, bool wasSuccessful) final;
     void didFinishActivation(WebCore::ServiceWorkerIdentifier) final;
     void setServiceWorkerHasPendingEvents(WebCore::ServiceWorkerIdentifier, bool) final;
@@ -99,7 +99,6 @@
     void setThrottleState(bool isThrottleable);
 
     Ref<IPC::Connection> m_connectionToNetworkProcess;
-    WebCore::RegistrableDomain m_registrableDomain;
     uint64_t m_pageGroupID;
     WebPageProxyIdentifier m_webPageProxyID;
     WebCore::PageIdentifier m_pageID;

Modified: trunk/Source/WebKit/WebProcess/WebProcess.cpp (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/WebProcess.cpp	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/WebProcess.cpp	2019-09-12 23:59:32 UTC (rev 249820)
@@ -1812,13 +1812,13 @@
 }
 
 #if ENABLE(SERVICE_WORKER)
-void WebProcess::establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store, RegistrableDomain&& registrableDomain, PAL::SessionID initialSessionID)
+void WebProcess::establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store, PAL::SessionID initialSessionID)
 {
     // We are in the Service Worker context process and the call below establishes our connection to the Network Process
     // by calling ensureNetworkProcessConnection. SWContextManager needs to use the same underlying IPC::Connection as the
     // NetworkProcessConnection for synchronization purposes.
     auto& ipcConnection = ensureNetworkProcessConnection().connection();
-    SWContextManager::singleton().setConnection(makeUnique<WebSWContextManagerConnection>(ipcConnection, WTFMove(registrableDomain), pageGroupID, webPageProxyID, pageID, store));
+    SWContextManager::singleton().setConnection(makeUnique<WebSWContextManagerConnection>(ipcConnection, pageGroupID, webPageProxyID, pageID, store));
 }
 
 void WebProcess::registerServiceWorkerClients()

Modified: trunk/Source/WebKit/WebProcess/WebProcess.h (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/WebProcess.h	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/WebProcess.h	2019-09-12 23:59:32 UTC (rev 249820)
@@ -367,7 +367,7 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    void establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&, WebCore::RegistrableDomain&&, PAL::SessionID);
+    void establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&, PAL::SessionID);
     void registerServiceWorkerClients();
 #endif
 

Modified: trunk/Source/WebKit/WebProcess/WebProcess.messages.in (249819 => 249820)


--- trunk/Source/WebKit/WebProcess/WebProcess.messages.in	2019-09-12 22:55:38 UTC (rev 249819)
+++ trunk/Source/WebKit/WebProcess/WebProcess.messages.in	2019-09-12 23:59:32 UTC (rev 249820)
@@ -111,7 +111,7 @@
 #endif
 
 #if ENABLE(SERVICE_WORKER)
-    EstablishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebKit::WebPageProxyIdentifier webPageProxyID, WebCore::PageIdentifier pageID, struct WebKit::WebPreferencesStore store, WebCore::RegistrableDomain domain, PAL::SessionID initialSessionID)
+    EstablishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebKit::WebPageProxyIdentifier webPageProxyID, WebCore::PageIdentifier pageID, struct WebKit::WebPreferencesStore store, PAL::SessionID initialSessionID)
     RegisterServiceWorkerClients()
 #endif
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to