Title: [225251] trunk/Source
Revision
225251
Author
[email protected]
Date
2017-11-28 17:50:03 -0800 (Tue, 28 Nov 2017)

Log Message

Register Documents as ServiceWorker clients to the StorageProcess
https://bugs.webkit.org/show_bug.cgi?id=180047

Patch by Youenn Fablet <[email protected]> on 2017-11-28
Reviewed by Brady Eidson.

Source/WebCore:

No change of behavior.
These changes will be covered when implementing ServiceWorker Clients API.

Registering a document when being created by DocumentLoader.
In the future, we may restrict registration to only documents related to origins that have some ongoing service worker activity.
This would require to keep track of which documents are registered so that we unregister these ones.
This would also require to register existing documents when one of the document with the same origin starts registering a service worker.
Unregistering a document inside prepareForDestruction.

Storing all clients in SWServer as a HasMap keyed by ClientOrigin.
Processing will then iterate through the list of clients scoped by the client origin.

Adding a ClientOrigin class as a pair of top origin and frame origin since service workers will be related based on that information.

* WebCore.xcodeproj/project.pbxproj:
* dom/Document.cpp:
(WebCore::Document::prepareForDestruction):
* loader/DocumentLoader.cpp:
(WebCore::DocumentLoader::commitData):
* page/ClientOrigin.h: Added.
(WebCore::ClientOrigin::emptyKey):
(WebCore::ClientOrigin::hash const):
(WebCore::ClientOrigin::operator== const):
(WTF::ClientOriginKeyHash::hash):
(WTF::ClientOriginKeyHash::equal):
(WTF::HashTraits<WebCore::ClientOrigin>::emptyValue):
(WTF::HashTraits<WebCore::ClientOrigin>::constructDeletedValue):
(WTF::HashTraits<WebCore::ClientOrigin>::isDeletedValue):
* workers/service/ServiceWorkerClientIdentifier.h:
(WebCore::ServiceWorkerClientIdentifier::operator== const):
* workers/service/server/SWClientConnection.h:
* workers/service/server/SWServer.cpp:
(WebCore::SWServer::registerServiceWorkerClient):
(WebCore::SWServer::unregisterServiceWorkerClient):
* workers/service/server/SWServer.h:

Source/WebKit:

Adding IPC to register/unregister clients to the storage process.
This will be used to enable service worker termination, and implementation of Clients API.

WebSWServerConnection keeps a hash map of all its related clients.
This allows unregistering these clients if the corresponding web process crashes.

* StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
(WebKit::WebSWServerConnection::~WebSWServerConnection):
(WebKit::WebSWServerConnection::registerServiceWorkerClient):
(WebKit::WebSWServerConnection::unregisterServiceWorkerClient):
* StorageProcess/ServiceWorker/WebSWServerConnection.h:
* StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
* StorageProcess/ServiceWorker/WebSWServerToContextConnection.h:
* WebProcess/Storage/WebSWClientConnection.cpp:
(WebKit::WebSWClientConnection::registerServiceWorkerClient):
(WebKit::WebSWClientConnection::unregisterServiceWorkerClient):
* WebProcess/Storage/WebSWClientConnection.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (225250 => 225251)


--- trunk/Source/WebCore/ChangeLog	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/ChangeLog	2017-11-29 01:50:03 UTC (rev 225251)
@@ -1,3 +1,46 @@
+2017-11-28  Youenn Fablet  <[email protected]>
+
+        Register Documents as ServiceWorker clients to the StorageProcess
+        https://bugs.webkit.org/show_bug.cgi?id=180047
+
+        Reviewed by Brady Eidson.
+
+        No change of behavior.
+        These changes will be covered when implementing ServiceWorker Clients API.
+
+        Registering a document when being created by DocumentLoader.
+        In the future, we may restrict registration to only documents related to origins that have some ongoing service worker activity.
+        This would require to keep track of which documents are registered so that we unregister these ones.
+        This would also require to register existing documents when one of the document with the same origin starts registering a service worker.
+        Unregistering a document inside prepareForDestruction.
+
+        Storing all clients in SWServer as a HasMap keyed by ClientOrigin.
+        Processing will then iterate through the list of clients scoped by the client origin.
+
+        Adding a ClientOrigin class as a pair of top origin and frame origin since service workers will be related based on that information.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * dom/Document.cpp:
+        (WebCore::Document::prepareForDestruction):
+        * loader/DocumentLoader.cpp:
+        (WebCore::DocumentLoader::commitData):
+        * page/ClientOrigin.h: Added.
+        (WebCore::ClientOrigin::emptyKey):
+        (WebCore::ClientOrigin::hash const):
+        (WebCore::ClientOrigin::operator== const):
+        (WTF::ClientOriginKeyHash::hash):
+        (WTF::ClientOriginKeyHash::equal):
+        (WTF::HashTraits<WebCore::ClientOrigin>::emptyValue):
+        (WTF::HashTraits<WebCore::ClientOrigin>::constructDeletedValue):
+        (WTF::HashTraits<WebCore::ClientOrigin>::isDeletedValue):
+        * workers/service/ServiceWorkerClientIdentifier.h:
+        (WebCore::ServiceWorkerClientIdentifier::operator== const):
+        * workers/service/server/SWClientConnection.h:
+        * workers/service/server/SWServer.cpp:
+        (WebCore::SWServer::registerServiceWorkerClient):
+        (WebCore::SWServer::unregisterServiceWorkerClient):
+        * workers/service/server/SWServer.h:
+
 2017-11-28  Chris Dumez  <[email protected]>
 
         ServiceWorkerGlobalScope.clients should always return the same object

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (225250 => 225251)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2017-11-29 01:50:03 UTC (rev 225251)
@@ -963,6 +963,7 @@
 		3FF813A71DBA8640009BF001 /* PointerLockController.h in Headers */ = {isa = PBXBuildFile; fileRef = 5CFC434F192406A900A0D3B5 /* PointerLockController.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		3FFFF9A9159D9A550020BBD5 /* WebKitCSSViewportRule.h in Headers */ = {isa = PBXBuildFile; fileRef = 3FFFF9A7159D9A550020BBD5 /* WebKitCSSViewportRule.h */; };
 		3FFFF9AE159D9B060020BBD5 /* ViewportStyleResolver.h in Headers */ = {isa = PBXBuildFile; fileRef = 3FFFF9AC159D9B060020BBD5 /* ViewportStyleResolver.h */; };
+		4107908C1FC3F0330061B27A /* ClientOrigin.h in Headers */ = {isa = PBXBuildFile; fileRef = 4107908A1FC3E4F20061B27A /* ClientOrigin.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		41103AAC1E39791000769F03 /* RealtimeOutgoingAudioSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 41103AA81E39790A00769F03 /* RealtimeOutgoingAudioSource.h */; };
 		41103AAC1E39791000769F14 /* RealtimeOutgoingAudioSourceCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = 41103AA81E39790A00769F14 /* RealtimeOutgoingAudioSourceCocoa.h */; };
 		41103AAD1E39791000769F03 /* RealtimeIncomingAudioSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 41103AA91E39790A00769F03 /* RealtimeIncomingAudioSource.h */; };
@@ -6880,6 +6881,7 @@
 		3FFFF9A7159D9A550020BBD5 /* WebKitCSSViewportRule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebKitCSSViewportRule.h; sourceTree = "<group>"; };
 		3FFFF9AB159D9B060020BBD5 /* ViewportStyleResolver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ViewportStyleResolver.cpp; sourceTree = "<group>"; };
 		3FFFF9AC159D9B060020BBD5 /* ViewportStyleResolver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewportStyleResolver.h; sourceTree = "<group>"; };
+		4107908A1FC3E4F20061B27A /* ClientOrigin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ClientOrigin.h; sourceTree = "<group>"; };
 		410B7E711045FAB000D8224F /* JSMessageEventCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSMessageEventCustom.cpp; sourceTree = "<group>"; };
 		41103AA71E39790A00769F03 /* RealtimeOutgoingAudioSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RealtimeOutgoingAudioSource.cpp; sourceTree = "<group>"; };
 		41103AA71E39790A00769F14 /* RealtimeOutgoingAudioSourceCocoa.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RealtimeOutgoingAudioSourceCocoa.cpp; sourceTree = "<group>"; };
@@ -18400,6 +18402,7 @@
 				14D8238A0AF92DF60004F057 /* Chrome.cpp */,
 				14D823500AF92A790004F057 /* Chrome.h */,
 				14D824060AF93AEB0004F057 /* ChromeClient.h */,
+				4107908A1FC3E4F20061B27A /* ClientOrigin.h */,
 				065AD4F20B0C2EDA005A2B1D /* ContextMenuClient.h */,
 				5106D7BB18BDB76F000AB166 /* ContextMenuContext.cpp */,
 				5106D7BC18BDB76F000AB166 /* ContextMenuContext.h */,
@@ -26265,6 +26268,7 @@
 				14D824080AF93AEB0004F057 /* ChromeClient.h in Headers */,
 				E4A814D41C6DEC4000BF85AC /* ClassChangeInvalidation.h in Headers */,
 				8348BFAC1B85729800912F36 /* ClassCollection.h in Headers */,
+				4107908C1FC3F0330061B27A /* ClientOrigin.h in Headers */,
 				7C3A91E61C963B8800D1A7E3 /* ClipboardAccessPolicy.h in Headers */,
 				85031B400A44EFC700F992E0 /* ClipboardEvent.h in Headers */,
 				FB92DF4B15FED08700994433 /* ClipPathOperation.h in Headers */,

Modified: trunk/Source/WebCore/dom/Document.cpp (225250 => 225251)


--- trunk/Source/WebCore/dom/Document.cpp	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/dom/Document.cpp	2017-11-29 01:50:03 UTC (rev 225251)
@@ -160,6 +160,7 @@
 #include "SVGSVGElement.h"
 #include "SVGTitleElement.h"
 #include "SVGZoomEvent.h"
+#include "SWClientConnection.h"
 #include "SchemeRegistry.h"
 #include "ScopedEventQueue.h"
 #include "ScriptController.h"
@@ -174,6 +175,8 @@
 #include "SecurityPolicy.h"
 #include "SegmentedString.h"
 #include "SelectorQuery.h"
+#include "ServiceWorkerClientData.h"
+#include "ServiceWorkerProvider.h"
 #include "Settings.h"
 #include "ShadowRoot.h"
 #include "SocketProvider.h"
@@ -2327,6 +2330,7 @@
 
 #if ENABLE(SERVICE_WORKER)
     setActiveServiceWorker(nullptr);
+    setServiceWorkerConnection(nullptr);
 #endif
 
 #if ENABLE(IOS_TOUCH_EVENTS)
@@ -7581,4 +7585,17 @@
     });
 }
 
+#if ENABLE(SERVICE_WORKER)
+void Document::setServiceWorkerConnection(SWClientConnection* serviceWorkerConnection)
+{
+    if (m_serviceWorkerConnection)
+        m_serviceWorkerConnection->unregisterServiceWorkerClient(identifier());
+
+    m_serviceWorkerConnection = serviceWorkerConnection;
+
+    if (m_serviceWorkerConnection)
+        m_serviceWorkerConnection->registerServiceWorkerClient(topOrigin(), identifier(), ServiceWorkerClientData::from(*this));
+}
+#endif
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/dom/Document.h (225250 => 225251)


--- trunk/Source/WebCore/dom/Document.h	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/dom/Document.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -154,6 +154,7 @@
 class RequestAnimationFrameCallback;
 class SVGDocumentExtensions;
 class SVGSVGElement;
+class SWClientConnection;
 class ScriptElementData;
 class ScriptModuleLoader;
 class ScriptRunner;
@@ -1388,6 +1389,10 @@
     WEBCORE_EXPORT RefPtr<HTMLAttachmentElement> attachmentForIdentifier(const String& identifier) const;
 #endif
 
+#if ENABLE(SERVICE_WORKER)
+    void setServiceWorkerConnection(SWClientConnection*);
+#endif
+
 protected:
     enum ConstructionFlags { Synthesized = 1, NonRenderedPlaceholder = 1 << 1 };
     Document(Frame*, const URL&, unsigned = DefaultDocumentClass, unsigned constructionFlags = 0);
@@ -1860,6 +1865,10 @@
 
     RefPtr<DocumentTimeline> m_timeline;
     DocumentIdentifier m_identifier;
+
+#if ENABLE(SERVICE_WORKER)
+    SWClientConnection* m_serviceWorkerConnection { nullptr };
+#endif
 };
 
 Element* eventTargetElementForDocument(Document*);

Modified: trunk/Source/WebCore/loader/DocumentLoader.cpp (225250 => 225251)


--- trunk/Source/WebCore/loader/DocumentLoader.cpp	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/loader/DocumentLoader.cpp	2017-11-29 01:50:03 UTC (rev 225251)
@@ -871,9 +871,14 @@
             m_frame->document()->setBaseURLOverride(m_archive->mainResource()->url());
 #endif
 #if ENABLE(SERVICE_WORKER)
-        if (m_serviceWorkerRegistrationData && m_serviceWorkerRegistrationData->activeWorker) {
-            m_frame->document()->setActiveServiceWorker(ServiceWorker::getOrCreate(*m_frame->document(), WTFMove(m_serviceWorkerRegistrationData->activeWorker.value())));
-            m_serviceWorkerRegistrationData = { };
+        if (RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled()) {
+            // FIXME: We should probably register the client as soon as we do the related navigation fetch.
+            // We can probably implement this when supporting FetchEvent.reservedClientId.
+            m_frame->document()->setServiceWorkerConnection(&ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(m_frame->page()->sessionID()));
+            if (m_serviceWorkerRegistrationData && m_serviceWorkerRegistrationData->activeWorker) {
+                m_frame->document()->setActiveServiceWorker(ServiceWorker::getOrCreate(*m_frame->document(), WTFMove(m_serviceWorkerRegistrationData->activeWorker.value())));
+                m_serviceWorkerRegistrationData = { };
+            }
         }
 #endif
         // Call receivedFirstData() exactly once per load. We should only reach this point multiple times

Added: trunk/Source/WebCore/page/ClientOrigin.h (0 => 225251)


--- trunk/Source/WebCore/page/ClientOrigin.h	                        (rev 0)
+++ trunk/Source/WebCore/page/ClientOrigin.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "SecurityOriginData.h"
+#include "URL.h"
+
+namespace WebCore {
+
+struct ClientOrigin {
+    static ClientOrigin emptyKey() { return { }; }
+
+    unsigned hash() const;
+    bool operator==(const ClientOrigin&) const;
+
+    SecurityOriginData topOrigin;
+    SecurityOriginData clientOrigin;
+};
+
+inline unsigned ClientOrigin::hash() const
+{
+    unsigned hashes[2];
+    hashes[0] = SecurityOriginDataHash::hash(topOrigin);
+    hashes[1] = SecurityOriginDataHash::hash(clientOrigin);
+
+    return StringHasher::hashMemory(hashes, sizeof(hashes));
+}
+
+inline bool ClientOrigin::operator==(const ClientOrigin& other) const
+{
+    return topOrigin == other.topOrigin && clientOrigin == other.clientOrigin;
+}
+
+} // namespace WebCore
+
+namespace WTF {
+
+struct ClientOriginKeyHash {
+    static unsigned hash(const WebCore::ClientOrigin& key) { return key.hash(); }
+    static bool equal(const WebCore::ClientOrigin& a, const WebCore::ClientOrigin& b) { return a == b; }
+    static const bool safeToCompareToEmptyOrDeleted = false;
+};
+
+template<> struct HashTraits<WebCore::ClientOrigin> : GenericHashTraits<WebCore::ClientOrigin> {
+    static WebCore::ClientOrigin emptyValue() { return WebCore::ClientOrigin::emptyKey(); }
+
+    static void constructDeletedValue(WebCore::ClientOrigin& slot) { slot.topOrigin = WebCore::SecurityOriginData(HashTableDeletedValue); }
+    static bool isDeletedValue(const WebCore::ClientOrigin& slot) { return slot.topOrigin.isHashTableDeletedValue(); }
+};
+
+template<> struct DefaultHash<WebCore::ClientOrigin> {
+    typedef ClientOriginKeyHash Hash;
+};
+
+} // namespace WTF

Modified: trunk/Source/WebCore/workers/service/ServiceWorkerClientIdentifier.h (225250 => 225251)


--- trunk/Source/WebCore/workers/service/ServiceWorkerClientIdentifier.h	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/workers/service/ServiceWorkerClientIdentifier.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -43,6 +43,11 @@
     template<class Decoder> static std::optional<ServiceWorkerClientIdentifier> decode(Decoder&);
 };
 
+inline bool operator==(const ServiceWorkerClientIdentifier& a, const ServiceWorkerClientIdentifier& b)
+{
+    return a.serverConnectionIdentifier == b.serverConnectionIdentifier &&  a.contextIdentifier == b.contextIdentifier;
+}
+
 template<class Encoder>
 void ServiceWorkerClientIdentifier::encode(Encoder& encoder) const
 {

Modified: trunk/Source/WebCore/workers/service/server/SWClientConnection.h (225250 => 225251)


--- trunk/Source/WebCore/workers/service/server/SWClientConnection.h	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/workers/service/server/SWClientConnection.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -76,6 +76,9 @@
     virtual void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;
     virtual void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;
 
+    virtual void registerServiceWorkerClient(const SecurityOrigin& topOrigin, DocumentIdentifier, const ServiceWorkerClientData&) = 0;
+    virtual void unregisterServiceWorkerClient(DocumentIdentifier) = 0;
+
 protected:
     WEBCORE_EXPORT SWClientConnection();
 

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


--- trunk/Source/WebCore/workers/service/server/SWServer.cpp	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/workers/service/server/SWServer.cpp	2017-11-29 01:50:03 UTC (rev 225251)
@@ -37,6 +37,7 @@
 #include "SWServerToContextConnection.h"
 #include "SWServerWorker.h"
 #include "SecurityOrigin.h"
+#include "ServiceWorkerClientType.h"
 #include "ServiceWorkerContextData.h"
 #include "ServiceWorkerFetchResult.h"
 #include "ServiceWorkerJobData.h"
@@ -485,6 +486,28 @@
     return (selectedRegistration && !selectedRegistration->isUninstalling()) ? selectedRegistration : nullptr;
 }
 
+void SWServer::registerServiceWorkerClient(ClientOrigin&& clientOrigin, ServiceWorkerClientIdentifier identifier, ServiceWorkerClientData&& data)
+{
+    m_clients.ensure(WTFMove(clientOrigin), [] {
+        return Vector<ClientInformation> { };
+    }).iterator->value.append(ClientInformation { identifier, WTFMove(data) });
+}
+
+void SWServer::unregisterServiceWorkerClient(const ClientOrigin& clientOrigin, ServiceWorkerClientIdentifier identifier)
+{
+    auto iterator = m_clients.find(clientOrigin);
+    ASSERT(iterator != m_clients.end());
+
+    auto& clients = iterator->value;
+    clients.removeFirstMatching([&] (const auto& item) {
+        return identifier == item.identifier;
+    });
+    if (clients.isEmpty()) {
+        // FIXME: We might want to terminate any clientOrigin related service worker.
+        m_clients.remove(iterator);
+    }
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(SERVICE_WORKER)

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


--- trunk/Source/WebCore/workers/service/server/SWServer.h	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebCore/workers/service/server/SWServer.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -27,8 +27,10 @@
 
 #if ENABLE(SERVICE_WORKER)
 
+#include "ClientOrigin.h"
 #include "DocumentIdentifier.h"
 #include "SWServerWorker.h"
+#include "ServiceWorkerClientData.h"
 #include "ServiceWorkerIdentifier.h"
 #include "ServiceWorkerJob.h"
 #include "ServiceWorkerRegistrationData.h"
@@ -138,6 +140,9 @@
     
     WEBCORE_EXPORT static HashSet<SWServer*>& allServers();
 
+    WEBCORE_EXPORT void registerServiceWorkerClient(ClientOrigin&&, ServiceWorkerClientIdentifier, ServiceWorkerClientData&&);
+    WEBCORE_EXPORT void unregisterServiceWorkerClient(const ClientOrigin&, ServiceWorkerClientIdentifier);
+
 private:
     void registerConnection(Connection&);
     void unregisterConnection(Connection&);
@@ -165,6 +170,12 @@
 
     HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_workersByID;
 
+    struct ClientInformation {
+        ServiceWorkerClientIdentifier identifier;
+        ServiceWorkerClientData data;
+    };
+    HashMap<ClientOrigin, Vector<ClientInformation>> m_clients;
+
     RefPtr<Thread> m_taskThread;
     Lock m_taskThreadLock;
 

Modified: trunk/Source/WebKit/ChangeLog (225250 => 225251)


--- trunk/Source/WebKit/ChangeLog	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebKit/ChangeLog	2017-11-29 01:50:03 UTC (rev 225251)
@@ -1,3 +1,28 @@
+2017-11-28  Youenn Fablet  <[email protected]>
+
+        Register Documents as ServiceWorker clients to the StorageProcess
+        https://bugs.webkit.org/show_bug.cgi?id=180047
+
+        Reviewed by Brady Eidson.
+
+        Adding IPC to register/unregister clients to the storage process.
+        This will be used to enable service worker termination, and implementation of Clients API.
+
+        WebSWServerConnection keeps a hash map of all its related clients.
+        This allows unregistering these clients if the corresponding web process crashes.
+
+        * StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
+        (WebKit::WebSWServerConnection::~WebSWServerConnection):
+        (WebKit::WebSWServerConnection::registerServiceWorkerClient):
+        (WebKit::WebSWServerConnection::unregisterServiceWorkerClient):
+        * StorageProcess/ServiceWorker/WebSWServerConnection.h:
+        * StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
+        * StorageProcess/ServiceWorker/WebSWServerToContextConnection.h:
+        * WebProcess/Storage/WebSWClientConnection.cpp:
+        (WebKit::WebSWClientConnection::registerServiceWorkerClient):
+        (WebKit::WebSWClientConnection::unregisterServiceWorkerClient):
+        * WebProcess/Storage/WebSWClientConnection.h:
+
 2017-11-28  Said Abou-Hallawa  <[email protected]>
 
         [CG] PostScript images should be supported if they are sub-resource images

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp (225250 => 225251)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp	2017-11-29 01:50:03 UTC (rev 225251)
@@ -67,6 +67,8 @@
 WebSWServerConnection::~WebSWServerConnection()
 {
     StorageProcess::singleton().unregisterSWServerConnection(*this);
+    for (auto keyValue : m_clientOrigins)
+        server().unregisterServiceWorkerClient(keyValue.value, ServiceWorkerClientIdentifier { identifier(), keyValue.key });
 }
 
 void WebSWServerConnection::disconnectedFromWebProcess()
@@ -179,6 +181,23 @@
     send(Messages::WebSWClientConnection::DidGetRegistrations { registrationMatchRequestIdentifier, registrations });
 }
 
+void WebSWServerConnection::registerServiceWorkerClient(SecurityOriginData&& topOrigin, DocumentIdentifier contextIdentifier, ServiceWorkerClientData&& data)
+{
+    auto clientOrigin = ClientOrigin { WTFMove(topOrigin), SecurityOriginData::fromSecurityOrigin(SecurityOrigin::create(data.url)) };
+    m_clientOrigins.add(contextIdentifier, clientOrigin);
+    server().registerServiceWorkerClient(WTFMove(clientOrigin), ServiceWorkerClientIdentifier { this->identifier(), contextIdentifier } , WTFMove(data));
+}
+
+void WebSWServerConnection::unregisterServiceWorkerClient(DocumentIdentifier contextIdentifier)
+{
+    auto iterator = m_clientOrigins.find(contextIdentifier);
+    if (iterator == m_clientOrigins.end())
+        return;
+
+    server().unregisterServiceWorkerClient(iterator->value, ServiceWorkerClientIdentifier { this->identifier(), contextIdentifier });
+    m_clientOrigins.remove(iterator);
+}
+
 template<typename U> void WebSWServerConnection::sendToContextProcess(U&& message)
 {
     if (auto* connection = StorageProcess::singleton().globalServerToContextConnection())

Modified: trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h (225250 => 225251)


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -31,6 +31,7 @@
 #include "MessageSender.h"
 #include <WebCore/SWServer.h>
 #include <pal/SessionID.h>
+#include <wtf/HashMap.h>
 
 namespace IPC {
 class FormDataReference;
@@ -37,6 +38,7 @@
 }
 
 namespace WebCore {
+struct ClientOrigin;
 struct ExceptionData;
 struct ServiceWorkerClientData;
 class ServiceWorkerRegistrationKey;
@@ -82,6 +84,9 @@
     void matchRegistration(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::URL& clientURL);
     void getRegistrations(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::URL& clientURL);
 
+    void registerServiceWorkerClient(WebCore::SecurityOriginData&& topOrigin, WebCore::DocumentIdentifier, WebCore::ServiceWorkerClientData&&);
+    void unregisterServiceWorkerClient(WebCore::DocumentIdentifier);
+
     IPC::Connection* messageSenderConnection() final { return m_contentConnection.ptr(); }
     uint64_t messageSenderDestinationID() final { return identifier().toUInt64(); }
     
@@ -91,6 +96,7 @@
 
     Ref<IPC::Connection> m_contentConnection;
     RefPtr<IPC::Connection> m_contextConnection;
+    HashMap<WebCore::DocumentIdentifier, WebCore::ClientOrigin> m_clientOrigins;
 }; // class WebSWServerConnection
 
 } // namespace WebKit

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


--- trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in	2017-11-29 01:50:03 UTC (rev 225251)
@@ -38,6 +38,8 @@
 
     MatchRegistration(uint64_t serviceRegistrationMatchRequestIdentifier, struct WebCore::SecurityOriginData topOrigin, WebCore::URL clientURL)
     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)
 }
 
 #endif // ENABLE(SERVICE_WORKER)

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp (225250 => 225251)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2017-11-29 01:50:03 UTC (rev 225251)
@@ -97,6 +97,16 @@
     send(Messages::WebSWServerConnection::ServiceWorkerStoppedControllingClient(serviceWorkerIdentifier, registrationIdentifier, contextIdentifier));
 }
 
+void WebSWClientConnection::registerServiceWorkerClient(const SecurityOrigin& topOrigin, WebCore::DocumentIdentifier contextIdentifier, const WebCore::ServiceWorkerClientData& data)
+{
+    send(Messages::WebSWServerConnection::RegisterServiceWorkerClient { SecurityOriginData::fromSecurityOrigin(topOrigin), contextIdentifier, data });
+}
+
+void WebSWClientConnection::unregisterServiceWorkerClient(WebCore::DocumentIdentifier contextIdentifier)
+{
+    send(Messages::WebSWServerConnection::UnregisterServiceWorkerClient { contextIdentifier });
+}
+
 void WebSWClientConnection::didResolveRegistrationPromise(const ServiceWorkerRegistrationKey& key)
 {
     send(Messages::WebSWServerConnection::DidResolveRegistrationPromise(key));

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h (225250 => 225251)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2017-11-29 01:47:01 UTC (rev 225250)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2017-11-29 01:50:03 UTC (rev 225251)
@@ -71,6 +71,8 @@
     void postMessageToServiceWorkerGlobalScope(WebCore::ServiceWorkerIdentifier destinationIdentifier, Ref<WebCore::SerializedScriptValue>&&, WebCore::DocumentIdentifier sourceContextIdentifier, WebCore::ServiceWorkerClientData&& source) final;
     void serviceWorkerStartedControllingClient(WebCore::ServiceWorkerIdentifier, WebCore::ServiceWorkerRegistrationIdentifier, WebCore::DocumentIdentifier) final;
     void serviceWorkerStoppedControllingClient(WebCore::ServiceWorkerIdentifier, WebCore::ServiceWorkerRegistrationIdentifier, WebCore::DocumentIdentifier) final;
+    void registerServiceWorkerClient(const WebCore::SecurityOrigin& topOrigin, WebCore::DocumentIdentifier, const WebCore::ServiceWorkerClientData&) final;
+    void unregisterServiceWorkerClient(WebCore::DocumentIdentifier) final;
 
     void matchRegistration(const WebCore::SecurityOrigin& topOrigin, const WebCore::URL& clientURL, RegistrationCallback&&) final;
     void didMatchRegistration(uint64_t matchRequestIdentifier, std::optional<WebCore::ServiceWorkerRegistrationData>&&);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to