Implement getting ServiceWorker registrations for the WKWebsiteDataStore API
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 16 Dec 2017 18:54:04 +0000 (18:54 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 16 Dec 2017 18:54:04 +0000 (18:54 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180886

Reviewed by Chris Dumez.

Source/WebCore:

No new tests (API test coming soon).

* workers/service/server/SWServer.cpp:
(WebCore::SWServer::registrationStoreImportComplete):
(WebCore::SWServer::SWServer):
(WebCore::SWServer::getOriginsWithRegistrations):
(WebCore::SWServer::performGetOriginsWithRegistrationsCallbacks):
* workers/service/server/SWServer.h:

Source/WebKit:

* StorageProcess/StorageProcess.cpp:
(WebKit::StorageProcess::fetchWebsiteData):

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

Source/WebCore/ChangeLog
Source/WebCore/workers/service/server/SWServer.cpp
Source/WebCore/workers/service/server/SWServer.h
Source/WebKit/ChangeLog
Source/WebKit/StorageProcess/StorageProcess.cpp

index 41b53cf..ba57c93 100644 (file)
@@ -1,3 +1,19 @@
+2017-12-16  Brady Eidson  <beidson@apple.com>
+
+        Implement getting ServiceWorker registrations for the WKWebsiteDataStore API
+        https://bugs.webkit.org/show_bug.cgi?id=180886
+
+        Reviewed by Chris Dumez.
+
+        No new tests (API test coming soon).
+
+        * workers/service/server/SWServer.cpp:
+        (WebCore::SWServer::registrationStoreImportComplete):
+        (WebCore::SWServer::SWServer):
+        (WebCore::SWServer::getOriginsWithRegistrations):
+        (WebCore::SWServer::performGetOriginsWithRegistrationsCallbacks):
+        * workers/service/server/SWServer.h:
+
 2017-12-16  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         Remove unnecessary boolean result of start() functions
index 7f16868..5b0d1a6 100644 (file)
@@ -108,7 +108,10 @@ SWServerRegistration* SWServer::getRegistration(const ServiceWorkerRegistrationK
 
 void SWServer::registrationStoreImportComplete()
 {
+    ASSERT(!m_importCompleted);
+    m_importCompleted = true;
     m_originStore->importComplete();
+    performGetOriginsWithRegistrationsCallbacks();
 }
 
 void SWServer::addRegistrationFromStore(ServiceWorkerContextData&& data)
@@ -228,7 +231,7 @@ void SWServer::Connection::syncTerminateWorker(ServiceWorkerIdentifier identifie
         m_server.syncTerminateWorker(*worker);
 }
 
-    SWServer::SWServer(UniqueRef<SWOriginStore>&& originStore, String&& registrationDatabaseDirectory, PAL::SessionID sessionID)
+SWServer::SWServer(UniqueRef<SWOriginStore>&& originStore, String&& registrationDatabaseDirectory, PAL::SessionID sessionID)
     : m_originStore(WTFMove(originStore))
     , m_registrationStore(*this, WTFMove(registrationDatabaseDirectory))
     , m_sessionID(sessionID)
@@ -768,6 +771,33 @@ void SWServer::Connection::resolveRegistrationReadyRequests(SWServerRegistration
     });
 }
 
+void SWServer::getOriginsWithRegistrations(WTF::Function<void(const HashSet<SecurityOriginData>&)> callback)
+{
+    m_getOriginsWithRegistrationsCallbacks.append(WTFMove(callback));
+
+    if (m_importCompleted)
+        performGetOriginsWithRegistrationsCallbacks();
+}
+
+void SWServer::performGetOriginsWithRegistrationsCallbacks()
+{
+    ASSERT(isMainThread());
+    ASSERT(m_importCompleted);
+
+    if (m_getOriginsWithRegistrationsCallbacks.isEmpty())
+        return;
+
+    HashSet<SecurityOriginData> originsWithRegistrations;
+    for (auto& key : m_registrations.keys()) {
+        originsWithRegistrations.add(key.topOrigin());
+        originsWithRegistrations.add(SecurityOriginData { key.scope().protocol().toString(), key.scope().host(), key.scope().port() });
+    }
+
+    auto callbacks = WTFMove(m_getOriginsWithRegistrationsCallbacks);
+    for (auto& callback : callbacks)
+        callback(originsWithRegistrations);
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(SERVICE_WORKER)
index 2773689..9a78b81 100644 (file)
@@ -174,6 +174,8 @@ public:
     void addRegistrationFromStore(ServiceWorkerContextData&&);
     void registrationStoreImportComplete();
 
+    WEBCORE_EXPORT void getOriginsWithRegistrations(WTF::Function<void(const HashSet<SecurityOriginData>&)>);
+
 private:
     void registerConnection(Connection&);
     void unregisterConnection(Connection&);
@@ -197,6 +199,8 @@ private:
     SWServerRegistration* registrationFromServiceWorkerIdentifier(ServiceWorkerIdentifier);
     void forEachClientForOrigin(const ClientOrigin&, const WTF::Function<void(ServiceWorkerClientData&)>&);
 
+    void performGetOriginsWithRegistrationsCallbacks();
+
     enum TerminationMode {
         Synchronous,
         Asynchronous,
@@ -231,6 +235,8 @@ private:
     Deque<ServiceWorkerContextData> m_pendingContextDatas;
     HashMap<ServiceWorkerIdentifier, Vector<RunServiceWorkerCallback>> m_serviceWorkerRunRequests;
     PAL::SessionID m_sessionID;
+    bool m_importCompleted { false };
+    Vector<WTF::Function<void(const HashSet<SecurityOriginData>&)>> m_getOriginsWithRegistrationsCallbacks;
 };
 
 } // namespace WebCore
index 8b5f00f..3266a13 100644 (file)
@@ -1,3 +1,13 @@
+2017-12-16  Brady Eidson  <beidson@apple.com>
+
+        Implement getting ServiceWorker registrations for the WKWebsiteDataStore API
+        https://bugs.webkit.org/show_bug.cgi?id=180886
+
+        Reviewed by Chris Dumez.
+
+        * StorageProcess/StorageProcess.cpp:
+        (WebKit::StorageProcess::fetchWebsiteData):
+
 2017-12-16  Brent Fulgham  <bfulgham@apple.com>
 
         Plugin processes are repeatedly spun up to do nothing
index 9e2590c..172e2ac 100644 (file)
@@ -265,33 +265,35 @@ void StorageProcess::createStorageToWebProcessConnection(bool isServiceWorkerPro
 
 void StorageProcess::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID)
 {
-    auto completionHandler = [this, callbackID](const WebsiteData& websiteData) {
-        parentProcessConnection()->send(Messages::StorageProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
-    };
+    auto websiteData = std::make_unique<WebsiteData>();
+    WebsiteData* rawWebsiteData = websiteData.get();
+    auto callbackAggregator = CallbackAggregator::create([this, websiteData = WTFMove(websiteData), callbackID]() {
+        parentProcessConnection()->send(Messages::StorageProcessProxy::DidFetchWebsiteData(callbackID, *websiteData), 0);
+    });
 
+    String path;
 #if ENABLE(SERVICE_WORKER)
-    if (websiteDataTypes.contains(WebsiteDataType::ServiceWorkerRegistrations))
-        notImplemented();
+    path = m_swDatabasePaths.get(sessionID);
+    if (!path.isEmpty() && websiteDataTypes.contains(WebsiteDataType::ServiceWorkerRegistrations)) {
+        swServerForSession(sessionID).getOriginsWithRegistrations([rawWebsiteData, callbackAggregator = callbackAggregator.copyRef()](const HashSet<SecurityOriginData>& origins) mutable {
+            for (auto& origin : origins)
+                rawWebsiteData->entries.append({ origin, WebsiteDataType::ServiceWorkerRegistrations, 0 });
+        });
+    }
 #endif
 
 #if ENABLE(INDEXED_DATABASE)
-    String path = m_idbDatabasePaths.get(sessionID);
+    path = m_idbDatabasePaths.get(sessionID);
     if (!path.isEmpty() && websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
         // FIXME: Pick the right database store based on the session ID.
-        postStorageTask(CrossThreadTask([this, completionHandler = WTFMove(completionHandler), path = WTFMove(path)]() mutable {
-            RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), securityOrigins = indexedDatabaseOrigins(path)] {
-                WebsiteData websiteData;
+        postStorageTask(CrossThreadTask([this, callbackAggregator = callbackAggregator.copyRef(), path = WTFMove(path), rawWebsiteData]() mutable {
+            RunLoop::main().dispatch([callbackAggregator = WTFMove(callbackAggregator), rawWebsiteData, securityOrigins = indexedDatabaseOrigins(path)] {
                 for (const auto& securityOrigin : securityOrigins)
-                    websiteData.entries.append({ securityOrigin, WebsiteDataType::IndexedDBDatabases, 0 });
-
-                completionHandler(websiteData);
+                    rawWebsiteData->entries.append({ securityOrigin, WebsiteDataType::IndexedDBDatabases, 0 });
             });
         }));
-        return;
     }
 #endif
-
-    completionHandler({ });
 }
 
 void StorageProcess::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID)