Title: [249526] trunk
Revision
249526
Author
you...@apple.com
Date
2019-09-05 02:01:51 -0700 (Thu, 05 Sep 2019)

Log Message

Introduce WorkerSWClientConnection to make SWClientConnection mono-thread
https://bugs.webkit.org/show_bug.cgi?id=201430

Reviewed by Alex Christensen.

LayoutTests/imported/w3c:

Remove inserted iframes to make test outputs more stable.

* web-platform-tests/service-workers/service-worker/activation.https-expected.txt:
* web-platform-tests/service-workers/service-worker/activation.https.html:
* web-platform-tests/service-workers/service-worker/detached-context.https-expected.txt:
* web-platform-tests/service-workers/service-worker/detached-context.https.html:

Source/WebCore:

Make SWClientConnection RefCounted.
Add a WorkerSWClientConnection dedicated to be used by worker/service worker contexts.
Simplify ServiceWorkerContainer logic based on this refactoring.

Covered by existing tests.

* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::swClientConnection):
* workers/WorkerGlobalScope.h:
* workers/service/SWClientConnection.cpp:
(WebCore::SWClientConnection::scheduleJob):
(WebCore::SWClientConnection::failedFetchingScript):
(WebCore::SWClientConnection::postTaskForJob):
* workers/service/SWClientConnection.h:
* workers/service/ServiceWorkerContainer.cpp:
(WebCore::mainThreadConnection):
(WebCore::ServiceWorkerContainer::ready):
(WebCore::ServiceWorkerContainer::scheduleJob):
(WebCore::ServiceWorkerContainer::getRegistration):
(WebCore::ServiceWorkerContainer::updateRegistrationState):
(WebCore::ServiceWorkerContainer::getRegistrations):
(WebCore::ServiceWorkerContainer::notifyRegistrationIsSettled):
(WebCore::ServiceWorkerContainer::jobFinishedLoadingScript):
(WebCore::ServiceWorkerContainer::notifyFailedFetchingScript):
(WebCore::ServiceWorkerContainer::ensureSWClientConnection):
(WebCore::ServiceWorkerContainer::stop):
* workers/service/ServiceWorkerContainer.h:
* workers/service/ServiceWorkerFetchResult.h:
(WebCore::ServiceWorkerFetchResult::isolatedCopy const):
* workers/service/WorkerSWClientConnection.cpp: Added.
(WebCore::WorkerSWClientConnection::WorkerSWClientConnection):
(WebCore::WorkerSWClientConnection::~WorkerSWClientConnection):
(WebCore::WorkerSWClientConnection::matchRegistration):
(WebCore::WorkerSWClientConnection::getRegistrations):
(WebCore::WorkerSWClientConnection::whenRegistrationReady):
(WebCore::WorkerSWClientConnection::addServiceWorkerRegistrationInServer):
(WebCore::WorkerSWClientConnection::removeServiceWorkerRegistrationInServer):
(WebCore::WorkerSWClientConnection::didResolveRegistrationPromise):
(WebCore::WorkerSWClientConnection::postMessageToServiceWorker):
(WebCore::WorkerSWClientConnection::serverConnectionIdentifier const):
(WebCore::WorkerSWClientConnection::mayHaveServiceWorkerRegisteredForOrigin const):
(WebCore::WorkerSWClientConnection::syncTerminateWorker):
(WebCore::WorkerSWClientConnection::registerServiceWorkerClient):
(WebCore::WorkerSWClientConnection::unregisterServiceWorkerClient):
(WebCore::WorkerSWClientConnection::finishFetchingScriptInServer):
(WebCore::WorkerSWClientConnection::isThrottleable const):
(WebCore::WorkerSWClientConnection::updateThrottleState):
(WebCore::WorkerSWClientConnection::scheduleJob):
(WebCore::WorkerSWClientConnection::scheduleJobInServer):
* workers/service/WorkerSWClientConnection.h: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (249525 => 249526)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2019-09-05 09:01:51 UTC (rev 249526)
@@ -1,3 +1,17 @@
+2019-09-05  Youenn Fablet  <you...@apple.com>
+
+        Introduce WorkerSWClientConnection to make SWClientConnection mono-thread
+        https://bugs.webkit.org/show_bug.cgi?id=201430
+
+        Reviewed by Alex Christensen.
+
+        Remove inserted iframes to make test outputs more stable.
+
+        * web-platform-tests/service-workers/service-worker/activation.https-expected.txt:
+        * web-platform-tests/service-workers/service-worker/activation.https.html:
+        * web-platform-tests/service-workers/service-worker/detached-context.https-expected.txt:
+        * web-platform-tests/service-workers/service-worker/detached-context.https.html:
+
 2019-09-04  Yusuke Suzuki  <ysuz...@apple.com>
 
         [JSC] Make Promise implementation faster

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/activation.https-expected.txt (249525 => 249526)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/activation.https-expected.txt	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/activation.https-expected.txt	2019-09-05 09:01:51 UTC (rev 249526)
@@ -1,5 +1,4 @@
 
-
 PASS loss of controllees triggers activation 
 PASS finishing a request triggers activation 
 PASS skipWaiting bypasses no controllee requirement 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/activation.https.html (249525 => 249526)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/activation.https.html	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/activation.https.html	2019-09-05 09:01:51 UTC (rev 249526)
@@ -113,10 +113,12 @@
     var scope = 'resources/skip-waiting';
     var worker_url = 'resources/mint-new-worker.py?skip-waiting';
     var registration;
+    var iframe;
     var new_worker;
     return setup_activation_test(t, scope, worker_url)
       .then(result => {
           registration = result.registration;
+          iframe = result.iframe;
           // Finish the request. The iframe does not need to be removed because
           // skipWaiting() was called.
           new_worker = registration.waiting;
@@ -126,6 +128,8 @@
         })
       .then(() => {
           assert_equals(registration.active, new_worker);
+          // Remove the iframe.
+          iframe.remove();
         });
   }, 'skipWaiting bypasses no controllee requirement');
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/detached-context.https-expected.txt (249525 => 249526)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/detached-context.https-expected.txt	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/detached-context.https-expected.txt	2019-09-05 09:01:51 UTC (rev 249526)
@@ -1,5 +1,4 @@
 
-
 PASS accessing a ServiceWorkerRegistration from a removed iframe 
 PASS accessing a ServiceWorker object from a removed iframe 
 PASS accessing navigator.serviceWorker on a detached iframe 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/detached-context.https.html (249525 => 249526)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/detached-context.https.html	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/detached-context.https.html	2019-09-05 09:01:51 UTC (rev 249526)
@@ -59,6 +59,7 @@
       .then(() => {
           assert_equals(resolvedCount, 0,
                         'methods called on a detached registration should not resolve');
+          frame.remove();
         })
   }, 'accessing a ServiceWorkerRegistration from a removed iframe');
 
@@ -102,6 +103,7 @@
         assert_equals(
             get_navigator().serviceWorker,
             iframe.contentWindow.navigator.serviceWorker);
+        iframe.remove();
       });
   }, 'accessing navigator.serviceWorker on a detached iframe');
 

Modified: trunk/Source/WebCore/ChangeLog (249525 => 249526)


--- trunk/Source/WebCore/ChangeLog	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/ChangeLog	2019-09-05 09:01:51 UTC (rev 249526)
@@ -1,3 +1,63 @@
+2019-09-05  Youenn Fablet  <you...@apple.com>
+
+        Introduce WorkerSWClientConnection to make SWClientConnection mono-thread
+        https://bugs.webkit.org/show_bug.cgi?id=201430
+
+        Reviewed by Alex Christensen.
+
+        Make SWClientConnection RefCounted.
+        Add a WorkerSWClientConnection dedicated to be used by worker/service worker contexts.
+        Simplify ServiceWorkerContainer logic based on this refactoring.
+
+        Covered by existing tests.
+
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::WorkerGlobalScope::swClientConnection):
+        * workers/WorkerGlobalScope.h:
+        * workers/service/SWClientConnection.cpp:
+        (WebCore::SWClientConnection::scheduleJob):
+        (WebCore::SWClientConnection::failedFetchingScript):
+        (WebCore::SWClientConnection::postTaskForJob):
+        * workers/service/SWClientConnection.h:
+        * workers/service/ServiceWorkerContainer.cpp:
+        (WebCore::mainThreadConnection):
+        (WebCore::ServiceWorkerContainer::ready):
+        (WebCore::ServiceWorkerContainer::scheduleJob):
+        (WebCore::ServiceWorkerContainer::getRegistration):
+        (WebCore::ServiceWorkerContainer::updateRegistrationState):
+        (WebCore::ServiceWorkerContainer::getRegistrations):
+        (WebCore::ServiceWorkerContainer::notifyRegistrationIsSettled):
+        (WebCore::ServiceWorkerContainer::jobFinishedLoadingScript):
+        (WebCore::ServiceWorkerContainer::notifyFailedFetchingScript):
+        (WebCore::ServiceWorkerContainer::ensureSWClientConnection):
+        (WebCore::ServiceWorkerContainer::stop):
+        * workers/service/ServiceWorkerContainer.h:
+        * workers/service/ServiceWorkerFetchResult.h:
+        (WebCore::ServiceWorkerFetchResult::isolatedCopy const):
+        * workers/service/WorkerSWClientConnection.cpp: Added.
+        (WebCore::WorkerSWClientConnection::WorkerSWClientConnection):
+        (WebCore::WorkerSWClientConnection::~WorkerSWClientConnection):
+        (WebCore::WorkerSWClientConnection::matchRegistration):
+        (WebCore::WorkerSWClientConnection::getRegistrations):
+        (WebCore::WorkerSWClientConnection::whenRegistrationReady):
+        (WebCore::WorkerSWClientConnection::addServiceWorkerRegistrationInServer):
+        (WebCore::WorkerSWClientConnection::removeServiceWorkerRegistrationInServer):
+        (WebCore::WorkerSWClientConnection::didResolveRegistrationPromise):
+        (WebCore::WorkerSWClientConnection::postMessageToServiceWorker):
+        (WebCore::WorkerSWClientConnection::serverConnectionIdentifier const):
+        (WebCore::WorkerSWClientConnection::mayHaveServiceWorkerRegisteredForOrigin const):
+        (WebCore::WorkerSWClientConnection::syncTerminateWorker):
+        (WebCore::WorkerSWClientConnection::registerServiceWorkerClient):
+        (WebCore::WorkerSWClientConnection::unregisterServiceWorkerClient):
+        (WebCore::WorkerSWClientConnection::finishFetchingScriptInServer):
+        (WebCore::WorkerSWClientConnection::isThrottleable const):
+        (WebCore::WorkerSWClientConnection::updateThrottleState):
+        (WebCore::WorkerSWClientConnection::scheduleJob):
+        (WebCore::WorkerSWClientConnection::scheduleJobInServer):
+        * workers/service/WorkerSWClientConnection.h: Added.
+
 2019-09-04  Fujii Hironori  <hironori.fu...@sony.com>
 
         [Win][Clang] InspectorNetworkAgent.cpp(1122,20): error: cannot decompose this type; 'std::tuple_size<const WTF::KeyValuePair<WTF::String, WTF::RefPtr<WTF::JSONImpl::Value, WTF::DumbPtrTraits<WTF::JSONImpl::Value> > >>::value' is not a valid integral const

Modified: trunk/Source/WebCore/Sources.txt (249525 => 249526)


--- trunk/Source/WebCore/Sources.txt	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/Sources.txt	2019-09-05 09:01:51 UTC (rev 249526)
@@ -2490,6 +2490,7 @@
 workers/service/ServiceWorkerRegistrationKey.cpp
 workers/service/ServiceWorkerRegistrationOptions.cpp
 workers/service/ServiceWorkerWindowClient.cpp
+workers/service/WorkerSWClientConnection.cpp
 
 workers/service/context/ServiceWorkerDebuggable.cpp
 workers/service/context/ServiceWorkerFetch.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (249525 => 249526)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2019-09-05 09:01:51 UTC (rev 249526)
@@ -7323,6 +7323,8 @@
 		41A023ED1A39DB7900F722DF /* WritableStream.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = WritableStream.idl; sourceTree = "<group>"; };
 		41A0829922932EF4008426E0 /* FeaturePolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeaturePolicy.h; sourceTree = "<group>"; };
 		41A0829B22932EF4008426E0 /* FeaturePolicy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FeaturePolicy.cpp; sourceTree = "<group>"; };
+		41A098BC231E91D100F4FFF0 /* WorkerSWClientConnection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkerSWClientConnection.cpp; sourceTree = "<group>"; };
+		41A098BE231E91D100F4FFF0 /* WorkerSWClientConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerSWClientConnection.h; sourceTree = "<group>"; };
 		41A1B00D1E52656E007F3769 /* LibWebRTCProvider.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LibWebRTCProvider.cpp; path = libwebrtc/LibWebRTCProvider.cpp; sourceTree = "<group>"; };
 		41A1B01A1E542396007F3769 /* JSDOMGuardedObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDOMGuardedObject.h; sourceTree = "<group>"; };
 		41A1B01B1E542396007F3769 /* JSDOMGuardedObject.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSDOMGuardedObject.cpp; sourceTree = "<group>"; };
@@ -18934,6 +18936,8 @@
 				46EF14261F97B7BA00C2A524 /* ServiceWorkerWindowClient.idl */,
 				46ECB6361FD0A3BB0088E5AD /* SWClientConnection.cpp */,
 				46ECB6371FD0A3BB0088E5AD /* SWClientConnection.h */,
+				41A098BC231E91D100F4FFF0 /* WorkerSWClientConnection.cpp */,
+				41A098BE231E91D100F4FFF0 /* WorkerSWClientConnection.h */,
 			);
 			path = service;
 			sourceTree = "<group>";

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.cpp (249525 => 249526)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2019-09-05 09:01:51 UTC (rev 249526)
@@ -47,6 +47,7 @@
 #include "WorkerLocation.h"
 #include "WorkerNavigator.h"
 #include "WorkerReportingProxy.h"
+#include "WorkerSWClientConnection.h"
 #include "WorkerScriptLoader.h"
 #include "WorkerThread.h"
 #include <_javascript_Core/ScriptArguments.h>
@@ -470,6 +471,15 @@
     return *m_messagePortChannelProvider;
 }
 
+#if ENABLE(SERVICE_WORKER)
+WorkerSWClientConnection& WorkerGlobalScope::swClientConnection()
+{
+    if (!m_swClientConnection)
+        m_swClientConnection = WorkerSWClientConnection::create(*this);
+    return *m_swClientConnection;
+}
+#endif
+
 void WorkerGlobalScope::createImageBitmap(ImageBitmap::Source&& source, ImageBitmapOptions&& options, ImageBitmap::Promise&& promise)
 {
     ImageBitmap::createPromise(*this, WTFMove(source), WTFMove(options), WTFMove(promise));

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.h (249525 => 249526)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.h	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.h	2019-09-05 09:01:51 UTC (rev 249526)
@@ -51,6 +51,7 @@
 class WorkerInspectorController;
 class WorkerLocation;
 class WorkerNavigator;
+class WorkerSWClientConnection;
 class WorkerThread;
 
 namespace IDBClient {
@@ -75,6 +76,9 @@
 
     WorkerCacheStorageConnection& cacheStorageConnection();
     MessagePortChannelProvider& messagePortChannelProvider();
+#if ENABLE(SERVICE_WORKER)
+    WorkerSWClientConnection& swClientConnection();
+#endif
 
     WorkerScriptController* script() { return m_script.get(); }
     void clearScript() { m_script = nullptr; }
@@ -214,6 +218,9 @@
     RefPtr<WorkerCacheStorageConnection> m_cacheStorageConnection;
     std::unique_ptr<WorkerMessagePortChannelProvider> m_messagePortChannelProvider;
     unsigned long m_uniqueIdentifier { 1 };
+#if ENABLE(SERVICE_WORKER)
+    RefPtr<WorkerSWClientConnection> m_swClientConnection;
+#endif
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/workers/service/SWClientConnection.cpp (249525 => 249526)


--- trunk/Source/WebCore/workers/service/SWClientConnection.cpp	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/service/SWClientConnection.cpp	2019-09-05 09:01:51 UTC (rev 249526)
@@ -47,8 +47,6 @@
 
 void SWClientConnection::scheduleJob(DocumentOrWorkerIdentifier contextIdentifier, const ServiceWorkerJobData& jobData)
 {
-    ASSERT(isMainThread());
-
     auto addResult = m_scheduledJobSources.add(jobData.identifier().jobIdentifier, contextIdentifier);
     ASSERT_UNUSED(addResult, addResult.isNewEntry);
 
@@ -62,7 +60,7 @@
     finishFetchingScriptInServer({ { serverConnectionIdentifier(), jobIdentifier }, registrationKey, { }, { }, { },  error });
 }
 
-bool SWClientConnection::postTaskForJob(ServiceWorkerJobIdentifier jobIdentifier, IsJobComplete isJobComplete, WTF::Function<void(ServiceWorkerJob&)>&& task)
+bool SWClientConnection::postTaskForJob(ServiceWorkerJobIdentifier jobIdentifier, IsJobComplete isJobComplete, Function<void(ServiceWorkerJob&)>&& task)
 {
     ASSERT(isMainThread());
 

Modified: trunk/Source/WebCore/workers/service/SWClientConnection.h (249525 => 249526)


--- trunk/Source/WebCore/workers/service/SWClientConnection.h	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/service/SWClientConnection.h	2019-09-05 09:01:51 UTC (rev 249526)
@@ -32,7 +32,7 @@
 #include "ServiceWorkerTypes.h"
 #include <wtf/CompletionHandler.h>
 #include <wtf/HashMap.h>
-#include <wtf/ThreadSafeRefCounted.h>
+#include <wtf/RefCounted.h>
 
 namespace WebCore {
 
@@ -53,23 +53,23 @@
 struct ServiceWorkerFetchResult;
 struct ServiceWorkerRegistrationData;
 
-class SWClientConnection : public ThreadSafeRefCounted<SWClientConnection> {
+class SWClientConnection : public RefCounted<SWClientConnection> {
 public:
     WEBCORE_EXPORT virtual ~SWClientConnection();
 
-    using RegistrationCallback = WTF::CompletionHandler<void(Optional<ServiceWorkerRegistrationData>&&)>;
+    using RegistrationCallback = CompletionHandler<void(Optional<ServiceWorkerRegistrationData>&&)>;
     virtual void matchRegistration(SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) = 0;
 
-    using GetRegistrationsCallback = WTF::CompletionHandler<void(Vector<ServiceWorkerRegistrationData>&&)>;
+    using GetRegistrationsCallback = CompletionHandler<void(Vector<ServiceWorkerRegistrationData>&&)>;
     virtual void getRegistrations(SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) = 0;
 
-    using WhenRegistrationReadyCallback = WTF::Function<void(ServiceWorkerRegistrationData&&)>;
-    virtual void whenRegistrationReady(const SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) = 0;
+    using WhenRegistrationReadyCallback = Function<void(ServiceWorkerRegistrationData&&)>;
+    virtual void whenRegistrationReady(const SecurityOriginData& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) = 0;
 
     virtual void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0;
     virtual void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0;
 
-    void scheduleJob(DocumentOrWorkerIdentifier, const ServiceWorkerJobData&);
+    WEBCORE_EXPORT virtual void scheduleJob(DocumentOrWorkerIdentifier, const ServiceWorkerJobData&);
     void failedFetchingScript(ServiceWorkerJobIdentifier, const ServiceWorkerRegistrationKey&, const ResourceError&);
 
     virtual void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&) = 0;
@@ -111,7 +111,7 @@
     virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0;
 
     enum class IsJobComplete { No, Yes };
-    bool postTaskForJob(ServiceWorkerJobIdentifier, IsJobComplete, WTF::Function<void(ServiceWorkerJob&)>&&);
+    bool postTaskForJob(ServiceWorkerJobIdentifier, IsJobComplete, Function<void(ServiceWorkerJob&)>&&);
 
     HashMap<ServiceWorkerJobIdentifier, DocumentOrWorkerIdentifier> m_scheduledJobSources;
 };

Modified: trunk/Source/WebCore/workers/service/ServiceWorkerContainer.cpp (249525 => 249526)


--- trunk/Source/WebCore/workers/service/ServiceWorkerContainer.cpp	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/service/ServiceWorkerContainer.cpp	2019-09-05 09:01:51 UTC (rev 249526)
@@ -49,6 +49,7 @@
 #include "ServiceWorkerJobData.h"
 #include "ServiceWorkerProvider.h"
 #include "ServiceWorkerThread.h"
+#include "WorkerSWClientConnection.h"
 #include <wtf/IsoMallocInlines.h>
 #include <wtf/RunLoop.h>
 #include <wtf/Scope.h>
@@ -59,6 +60,11 @@
 
 namespace WebCore {
 
+static inline SWClientConnection& mainThreadConnection(PAL::SessionID sessionID)
+{
+    return ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+}
+
 WTF_MAKE_ISO_ALLOCATED_IMPL(ServiceWorkerContainer);
 
 ServiceWorkerContainer::ServiceWorkerContainer(ScriptExecutionContext* context, NavigatorBase& navigator)
@@ -95,24 +101,16 @@
     if (!m_readyPromise) {
         m_readyPromise = makeUnique<ReadyPromise>();
 
-        if (m_isStopped || !scriptExecutionContext()->sessionID().isValid())
+        if (m_isStopped)
             return *m_readyPromise;
 
         auto& context = *scriptExecutionContext();
-        auto contextIdentifier = this->contextIdentifier();
-        callOnMainThread([connection = makeRef(ensureSWClientConnection()), topOrigin = context.topOrigin().isolatedCopy(), clientURL = context.url().isolatedCopy(), contextIdentifier]() mutable {
-            connection->whenRegistrationReady(topOrigin, clientURL, [contextIdentifier](auto&& registrationData) {
-                ScriptExecutionContext::postTaskTo(contextIdentifier, [registrationData = crossThreadCopy(registrationData)](auto& context) mutable {
-                    auto* serviceWorkerContainer = context.serviceWorkerContainer();
-                    if (!serviceWorkerContainer)
-                        return;
-                    if (serviceWorkerContainer->m_isStopped || !context.sessionID().isValid())
-                        return;
+        ensureSWClientConnection().whenRegistrationReady(context.topOrigin().data(), context.url(), [this, protectedThis = makeRef(*this)](auto&& registrationData) mutable {
+            if (m_isStopped)
+                return;
 
-                    auto registration = ServiceWorkerRegistration::getOrCreate(context, *serviceWorkerContainer, WTFMove(registrationData));
-                    serviceWorkerContainer->m_readyPromise->resolve(WTFMove(registration));
-                });
-            });
+            auto registration = ServiceWorkerRegistration::getOrCreate(*scriptExecutionContext(), *this, WTFMove(registrationData));
+            m_readyPromise->resolve(WTFMove(registration));
         });
     }
     return *m_readyPromise;
@@ -254,69 +252,35 @@
     ASSERT(!m_jobMap.contains(jobIdentifier));
     m_jobMap.add(jobIdentifier, OngoingJob { WTFMove(job), makePendingActivity(*this) });
 
-    callOnMainThread([connection = m_swConnection, contextIdentifier = this->contextIdentifier(), jobData = jobData.isolatedCopy()] {
-        connection->scheduleJob(contextIdentifier, jobData);
-    });
+    m_swConnection->scheduleJob(contextIdentifier(), jobData);
 }
 
 void ServiceWorkerContainer::getRegistration(const String& clientURL, Ref<DeferredPromise>&& promise)
 {
-    auto* context = scriptExecutionContext();
-    if (m_isStopped || !context->sessionID().isValid()) {
+    if (m_isStopped) {
         promise->reject(Exception { InvalidStateError });
         return;
     }
 
-    URL parsedURL = context->completeURL(clientURL);
-    if (!protocolHostAndPortAreEqual(parsedURL, context->url())) {
+    auto& context = *scriptExecutionContext();
+    URL parsedURL = context.completeURL(clientURL);
+    if (!protocolHostAndPortAreEqual(parsedURL, context.url())) {
         promise->reject(Exception { SecurityError, "Origin of clientURL is not client's origin"_s });
         return;
     }
 
-    uint64_t pendingPromiseIdentifier = ++m_lastPendingPromiseIdentifier;
-    auto pendingPromise = makeUnique<PendingPromise>(WTFMove(promise), makePendingActivity(*this));
-    m_pendingPromises.add(pendingPromiseIdentifier, WTFMove(pendingPromise));
+    ensureSWClientConnection().matchRegistration(SecurityOriginData { context.topOrigin().data() }, parsedURL, [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](auto&& result) mutable {
+        if (m_isStopped)
+            return;
 
-    auto contextIdentifier = this->contextIdentifier();
-    callOnMainThread([connection = makeRef(ensureSWClientConnection()), topOrigin = context->topOrigin().data().isolatedCopy(), parsedURL = parsedURL.isolatedCopy(), contextIdentifier, pendingPromiseIdentifier]() mutable {
-        connection->matchRegistration(WTFMove(topOrigin), parsedURL, [contextIdentifier, pendingPromiseIdentifier] (auto&& result) mutable {
-            ScriptExecutionContext::postTaskTo(contextIdentifier, [pendingPromiseIdentifier, result = crossThreadCopy(result)](auto& context) mutable {
-                auto* serviceWorkerContainer = context.serviceWorkerContainer();
-                if (!serviceWorkerContainer)
-                    return;
-                if (serviceWorkerContainer->m_isStopped || !context.sessionID().isValid())
-                    return;
-
-                serviceWorkerContainer->didFinishGetRegistrationRequest(pendingPromiseIdentifier, WTFMove(result));
-            });
-        });
+        if (!result) {
+            promise->resolve();
+            return;
+        }
+        promise->resolve<IDLInterface<ServiceWorkerRegistration>>(ServiceWorkerRegistration::getOrCreate(*scriptExecutionContext(), *this, WTFMove(result.value())));
     });
 }
 
-void ServiceWorkerContainer::didFinishGetRegistrationRequest(uint64_t pendingPromiseIdentifier, Optional<ServiceWorkerRegistrationData>&& result)
-{
-#ifndef NDEBUG
-    ASSERT(m_creationThread.ptr() == &Thread::current());
-#endif
-
-    auto pendingPromise = m_pendingPromises.take(pendingPromiseIdentifier);
-    if (!pendingPromise)
-        return;
-
-    if (m_isStopped || !scriptExecutionContext()->sessionID().isValid()) {
-        pendingPromise->promise->reject(Exception { InvalidStateError });
-        return;
-    }
-
-    if (!result) {
-        pendingPromise->promise->resolve();
-        return;
-    }
-
-    auto registration = ServiceWorkerRegistration::getOrCreate(*scriptExecutionContext(), *this, WTFMove(result.value()));
-    pendingPromise->promise->resolve<IDLInterface<ServiceWorkerRegistration>>(WTFMove(registration));
-}
-
 void ServiceWorkerContainer::updateRegistrationState(ServiceWorkerRegistrationIdentifier identifier, ServiceWorkerRegistrationState state, const Optional<ServiceWorkerData>& serviceWorkerData)
 {
     if (m_isStopped)
@@ -332,55 +296,23 @@
 
 void ServiceWorkerContainer::getRegistrations(Ref<DeferredPromise>&& promise)
 {
-    auto* context = scriptExecutionContext();
-    if (m_isStopped || !context->sessionID().isValid()) {
+    if (m_isStopped) {
         promise->reject(Exception { InvalidStateError });
         return;
     }
 
-    uint64_t pendingPromiseIdentifier = ++m_lastPendingPromiseIdentifier;
-    auto pendingPromise = makeUnique<PendingPromise>(WTFMove(promise), makePendingActivity(*this));
-    m_pendingPromises.add(pendingPromiseIdentifier, WTFMove(pendingPromise));
+    auto& context = *scriptExecutionContext();
+    ensureSWClientConnection().getRegistrations(SecurityOriginData { context.topOrigin().data() }, context.url(), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)] (auto&& registrationDatas) mutable {
+        if (m_isStopped)
+            return;
 
-    auto contextIdentifier = this->contextIdentifier();
-    auto contextURL = context->url();
-    callOnMainThread([connection = makeRef(ensureSWClientConnection()), topOrigin = context->topOrigin().data().isolatedCopy(), contextURL = contextURL.isolatedCopy(), contextIdentifier, pendingPromiseIdentifier]() mutable {
-        connection->getRegistrations(WTFMove(topOrigin), contextURL, [contextIdentifier, pendingPromiseIdentifier] (auto&& registrationDatas) mutable {
-            ScriptExecutionContext::postTaskTo(contextIdentifier, [pendingPromiseIdentifier, registrationDatas = crossThreadCopy(registrationDatas)](auto& context) mutable {
-                auto* serviceWorkerContainer = context.serviceWorkerContainer();
-                if (!serviceWorkerContainer)
-                    return;
-                if (serviceWorkerContainer->m_isStopped || !context.sessionID().isValid())
-                    return;
-
-                serviceWorkerContainer->didFinishGetRegistrationsRequest(pendingPromiseIdentifier, WTFMove(registrationDatas));
-            });
+        auto registrations = WTF::map(WTFMove(registrationDatas), [&](auto&& registrationData) {
+            return ServiceWorkerRegistration::getOrCreate(*scriptExecutionContext(), *this, WTFMove(registrationData));
         });
+        promise->resolve<IDLSequence<IDLInterface<ServiceWorkerRegistration>>>(WTFMove(registrations));
     });
 }
 
-void ServiceWorkerContainer::didFinishGetRegistrationsRequest(uint64_t pendingPromiseIdentifier, Vector<ServiceWorkerRegistrationData>&& registrationDatas)
-{
-#ifndef NDEBUG
-    ASSERT(m_creationThread.ptr() == &Thread::current());
-#endif
-
-    auto pendingPromise = m_pendingPromises.take(pendingPromiseIdentifier);
-    if (!pendingPromise)
-        return;
-
-    if (m_isStopped || !scriptExecutionContext()->sessionID().isValid()) {
-        pendingPromise->promise->reject(Exception { InvalidStateError });
-        return;
-    }
-
-    auto registrations = WTF::map(WTFMove(registrationDatas), [&] (auto&& registrationData) {
-        return ServiceWorkerRegistration::getOrCreate(*this->scriptExecutionContext(), *this, WTFMove(registrationData));
-    });
-
-    pendingPromise->promise->resolve<IDLSequence<IDLInterface<ServiceWorkerRegistration>>>(WTFMove(registrations));
-}
-
 void ServiceWorkerContainer::startMessages()
 {
     m_messageQueue.resume();
@@ -487,8 +419,8 @@
 
 void ServiceWorkerContainer::notifyRegistrationIsSettled(const ServiceWorkerRegistrationKey& registrationKey)
 {
-    callOnMainThread([connection = m_swConnection, registrationKey = registrationKey.isolatedCopy()] {
-        connection->didResolveRegistrationPromise(registrationKey);
+    callOnMainThread([sessionID = scriptExecutionContext()->sessionID(), registrationKey = registrationKey.isolatedCopy()] {
+        mainThreadConnection(sessionID).didResolveRegistrationPromise(registrationKey);
     });
 }
 
@@ -544,8 +476,8 @@
 
     CONTAINER_RELEASE_LOG_IF_ALLOWED("jobFinishedLoadingScript: Successfuly finished fetching script for job %" PRIu64, job.identifier().toUInt64());
 
-    callOnMainThread([connection = m_swConnection, jobDataIdentifier = job.data().identifier(), registrationKey = job.data().registrationKey().isolatedCopy(), script = script.isolatedCopy(), contentSecurityPolicy = contentSecurityPolicy.isolatedCopy(), referrerPolicy = referrerPolicy.isolatedCopy()] {
-        connection->finishFetchingScriptInServer({ jobDataIdentifier, registrationKey, script, contentSecurityPolicy, referrerPolicy, { } });
+    callOnMainThread([sessionID = scriptExecutionContext()->sessionID(), jobDataIdentifier = job.data().identifier(), registrationKey = job.data().registrationKey().isolatedCopy(), script = script.isolatedCopy(), contentSecurityPolicy = contentSecurityPolicy.isolatedCopy(), referrerPolicy = referrerPolicy.isolatedCopy()] {
+        mainThreadConnection(sessionID).finishFetchingScriptInServer({ jobDataIdentifier, registrationKey, script, contentSecurityPolicy, referrerPolicy, { } });
     });
 }
 
@@ -567,8 +499,8 @@
 
 void ServiceWorkerContainer::notifyFailedFetchingScript(ServiceWorkerJob& job, const ResourceError& error)
 {
-    callOnMainThread([connection = m_swConnection, jobIdentifier = job.identifier(), registrationKey = job.data().registrationKey().isolatedCopy(), error = error.isolatedCopy()] {
-        connection->failedFetchingScript(jobIdentifier, registrationKey, error);
+    callOnMainThread([sessionID = scriptExecutionContext()->sessionID(), jobIdentifier = job.identifier(), registrationKey = job.data().registrationKey().isolatedCopy(), error = error.isolatedCopy()] {
+        mainThreadConnection(sessionID).failedFetchingScript(jobIdentifier, registrationKey, error);
     });
 }
 
@@ -607,10 +539,11 @@
     ASSERT(scriptExecutionContext());
     ASSERT(scriptExecutionContext()->sessionID().isValid());
     if (!m_swConnection) {
-        ASSERT(scriptExecutionContext());
-        callOnMainThreadAndWait([this, sessionID = scriptExecutionContext()->sessionID()]() {
-            m_swConnection = &ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
-        });
+        auto& context = *scriptExecutionContext();
+        if (is<WorkerGlobalScope>(context))
+            m_swConnection = &downcast<WorkerGlobalScope>(context).swClientConnection();
+        else
+            m_swConnection = &mainThreadConnection(context.sessionID());
     }
     return *m_swConnection;
 }
@@ -651,7 +584,6 @@
 {
     m_isStopped = true;
     removeAllEventListeners();
-    m_pendingPromises.clear();
     m_readyPromise = nullptr;
     m_messageQueue.close();
     auto jobMap = WTFMove(m_jobMap);

Modified: trunk/Source/WebCore/workers/service/ServiceWorkerContainer.h (249525 => 249526)


--- trunk/Source/WebCore/workers/service/ServiceWorkerContainer.h	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/service/ServiceWorkerContainer.h	2019-09-05 09:01:51 UTC (rev 249526)
@@ -105,9 +105,6 @@
     void notifyFailedFetchingScript(ServiceWorkerJob&, const ResourceError&);
     void destroyJob(ServiceWorkerJob&);
 
-    void didFinishGetRegistrationRequest(uint64_t requestIdentifier, Optional<ServiceWorkerRegistrationData>&&);
-    void didFinishGetRegistrationsRequest(uint64_t requestIdentifier, Vector<ServiceWorkerRegistrationData>&&);
-
     DocumentOrWorkerIdentifier contextIdentifier() final;
 
     SWClientConnection& ensureSWClientConnection();
@@ -145,20 +142,6 @@
     Ref<Thread> m_creationThread { Thread::current() };
 #endif
 
-    struct PendingPromise {
-        WTF_MAKE_STRUCT_FAST_ALLOCATED;
-        PendingPromise(Ref<DeferredPromise>&& promise, Ref<PendingActivity<ServiceWorkerContainer>>&& pendingActivity)
-            : promise(WTFMove(promise))
-            , pendingActivity(WTFMove(pendingActivity))
-        { }
-
-        Ref<DeferredPromise> promise;
-        Ref<PendingActivity<ServiceWorkerContainer>> pendingActivity;
-    };
-
-    uint64_t m_lastPendingPromiseIdentifier { 0 };
-    HashMap<uint64_t, std::unique_ptr<PendingPromise>> m_pendingPromises;
-
     uint64_t m_lastOngoingSettledRegistrationIdentifier { 0 };
     HashMap<uint64_t, ServiceWorkerRegistrationKey> m_ongoingSettledRegistrations;
     GenericEventQueue m_messageQueue;

Modified: trunk/Source/WebCore/workers/service/ServiceWorkerFetchResult.h (249525 => 249526)


--- trunk/Source/WebCore/workers/service/ServiceWorkerFetchResult.h	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebCore/workers/service/ServiceWorkerFetchResult.h	2019-09-05 09:01:51 UTC (rev 249526)
@@ -42,6 +42,8 @@
     String referrerPolicy;
     ResourceError scriptError;
 
+    ServiceWorkerFetchResult isolatedCopy() const { return { jobDataIdentifier, registrationKey.isolatedCopy(), script.isolatedCopy(), contentSecurityPolicy.isolatedCopy(), referrerPolicy.isolatedCopy(), scriptError.isolatedCopy() }; }
+
     template<class Encoder> void encode(Encoder&) const;
     template<class Decoder> static bool decode(Decoder&, ServiceWorkerFetchResult&);
 };

Added: trunk/Source/WebCore/workers/service/WorkerSWClientConnection.cpp (0 => 249526)


--- trunk/Source/WebCore/workers/service/WorkerSWClientConnection.cpp	                        (rev 0)
+++ trunk/Source/WebCore/workers/service/WorkerSWClientConnection.cpp	2019-09-05 09:01:51 UTC (rev 249526)
@@ -0,0 +1,211 @@
+/*
+ * 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.
+ */
+
+#include "config.h"
+#include "WorkerSWClientConnection.h"
+
+#if ENABLE(SERVICE_WORKER)
+
+#include "SecurityOrigin.h"
+#include "ServiceWorkerFetchResult.h"
+#include "ServiceWorkerJobData.h"
+#include "ServiceWorkerProvider.h"
+#include "ServiceWorkerRegistration.h"
+#include "WorkerGlobalScope.h"
+#include "WorkerThread.h"
+
+namespace WebCore {
+
+WorkerSWClientConnection::WorkerSWClientConnection(WorkerGlobalScope& scope)
+    : m_sessionID(scope.sessionID())
+    , m_thread(scope.thread())
+{
+}
+
+WorkerSWClientConnection::~WorkerSWClientConnection()
+{
+    auto matchRegistrations = WTFMove(m_matchRegistrationRequests);
+    for (auto& callback : matchRegistrations.values())
+        callback({ });
+
+    auto getRegistrationsRequests = WTFMove(m_getRegistrationsRequests);
+    for (auto& callback : getRegistrationsRequests.values())
+        callback({ });
+
+    auto whenRegistrationReadyRequests = WTFMove(m_whenRegistrationReadyRequests);
+    for (auto& callback : whenRegistrationReadyRequests.values())
+        callback({ });
+}
+
+void WorkerSWClientConnection::matchRegistration(SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&& callback)
+{
+    uint64_t requestIdentifier = ++m_lastRequestIdentifier;
+    m_matchRegistrationRequests.add(requestIdentifier, WTFMove(callback));
+
+    callOnMainThread([sessionID = m_sessionID, thread = m_thread.copyRef(), requestIdentifier, topOrigin = crossThreadCopy(WTFMove(topOrigin)), clientURL = crossThreadCopy(clientURL)]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.matchRegistration(WTFMove(topOrigin), clientURL, [thread = WTFMove(thread), requestIdentifier](auto&& result) mutable {
+            thread->runLoop().postTaskForMode([requestIdentifier, result = WTFMove(result)] (auto& scope) mutable {
+                auto callback = downcast<WorkerGlobalScope>(scope).swClientConnection().m_matchRegistrationRequests.take(requestIdentifier);
+                callback(WTFMove(result));
+            }, WorkerRunLoop::defaultMode());
+        });
+    });
+}
+
+void WorkerSWClientConnection::getRegistrations(SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&& callback)
+{
+    uint64_t requestIdentifier = ++m_lastRequestIdentifier;
+    m_getRegistrationsRequests.add(requestIdentifier, WTFMove(callback));
+
+    callOnMainThread([sessionID = m_sessionID, thread = m_thread.copyRef(), requestIdentifier, topOrigin = crossThreadCopy(WTFMove(topOrigin)), clientURL = crossThreadCopy(clientURL)]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.getRegistrations(WTFMove(topOrigin), clientURL, [thread = WTFMove(thread), requestIdentifier](auto&& data) mutable {
+            thread->runLoop().postTaskForMode([requestIdentifier, data = "" (auto& scope) mutable {
+                auto callback = downcast<WorkerGlobalScope>(scope).swClientConnection().m_getRegistrationsRequests.take(requestIdentifier);
+                callback(WTFMove(data));
+            }, WorkerRunLoop::defaultMode());
+        });
+    });
+}
+
+void WorkerSWClientConnection::whenRegistrationReady(const SecurityOriginData& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&& callback)
+{
+    uint64_t requestIdentifier = ++m_lastRequestIdentifier;
+    m_whenRegistrationReadyRequests.add(requestIdentifier, WTFMove(callback));
+
+    callOnMainThread([sessionID = m_sessionID, thread = m_thread.copyRef(), requestIdentifier, topOrigin = topOrigin.isolatedCopy(), clientURL = crossThreadCopy(clientURL)]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.whenRegistrationReady(topOrigin, clientURL, [thread = WTFMove(thread), requestIdentifier](auto&& result) mutable {
+            thread->runLoop().postTaskForMode([requestIdentifier, result = crossThreadCopy(WTFMove(result))] (auto& scope) mutable {
+                auto callback = downcast<WorkerGlobalScope>(scope).swClientConnection().m_whenRegistrationReadyRequests.take(requestIdentifier);
+                callback(WTFMove(result));
+            }, WorkerRunLoop::defaultMode());
+        });
+    });
+}
+
+void WorkerSWClientConnection::addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
+{
+    callOnMainThread([sessionID = m_sessionID, identifier]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.addServiceWorkerRegistrationInServer(identifier);
+    });
+}
+
+void WorkerSWClientConnection::removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
+{
+    callOnMainThread([sessionID = m_sessionID, identifier]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.removeServiceWorkerRegistrationInServer(identifier);
+    });
+}
+
+void WorkerSWClientConnection::didResolveRegistrationPromise(const ServiceWorkerRegistrationKey& key)
+{
+    callOnMainThread([sessionID = m_sessionID, key = crossThreadCopy(key)]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.didResolveRegistrationPromise(key);
+    });
+}
+
+void WorkerSWClientConnection::postMessageToServiceWorker(ServiceWorkerIdentifier destination, MessageWithMessagePorts&& ports, const ServiceWorkerOrClientIdentifier& source)
+{
+    callOnMainThreadAndWait([&] {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(m_sessionID);
+        connection.postMessageToServiceWorker(destination, WTFMove(ports), source);
+    });
+}
+
+SWServerConnectionIdentifier WorkerSWClientConnection::serverConnectionIdentifier() const
+{
+    SWServerConnectionIdentifier identifier;
+    callOnMainThreadAndWait([&] {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(m_sessionID);
+        identifier = connection.serverConnectionIdentifier();
+    });
+    return identifier;
+}
+
+bool WorkerSWClientConnection::mayHaveServiceWorkerRegisteredForOrigin(const SecurityOriginData&) const
+{
+    ASSERT_NOT_REACHED();
+    return true;
+}
+
+void WorkerSWClientConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
+{
+    callOnMainThread([sessionID = m_sessionID, identifier]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.syncTerminateWorker(identifier);
+    });
+}
+
+void WorkerSWClientConnection::registerServiceWorkerClient(const SecurityOrigin& topOrigin, const ServiceWorkerClientData& data, const Optional<ServiceWorkerRegistrationIdentifier>& identifier, const String& userAgent)
+{
+    callOnMainThread([sessionID = m_sessionID, topOrigin = topOrigin.isolatedCopy(), data = "" identifier, userAgent = crossThreadCopy(userAgent)] {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.registerServiceWorkerClient(topOrigin, data, identifier, userAgent);
+    });
+}
+
+void WorkerSWClientConnection::unregisterServiceWorkerClient(DocumentIdentifier)
+{
+    ASSERT_NOT_REACHED();
+}
+
+void WorkerSWClientConnection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
+{
+    callOnMainThread([sessionID = m_sessionID, result = crossThreadCopy(result)]() mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.finishFetchingScriptInServer(result);
+    });
+}
+
+bool WorkerSWClientConnection::isThrottleable() const
+{
+    return true;
+}
+
+void WorkerSWClientConnection::updateThrottleState()
+{
+}
+
+void WorkerSWClientConnection::scheduleJob(DocumentOrWorkerIdentifier identifier, const ServiceWorkerJobData& data)
+{
+    callOnMainThread([sessionID = m_sessionID, identifier, data = "" mutable {
+        auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        connection.scheduleJob(identifier, data);
+    });
+}
+
+void WorkerSWClientConnection::scheduleJobInServer(const ServiceWorkerJobData&)
+{
+    ASSERT_NOT_REACHED();
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(SERVICE_WORKER)

Added: trunk/Source/WebCore/workers/service/WorkerSWClientConnection.h (0 => 249526)


--- trunk/Source/WebCore/workers/service/WorkerSWClientConnection.h	                        (rev 0)
+++ trunk/Source/WebCore/workers/service/WorkerSWClientConnection.h	2019-09-05 09:01:51 UTC (rev 249526)
@@ -0,0 +1,74 @@
+/*
+ * 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
+
+#if ENABLE(SERVICE_WORKER)
+
+#include "SWClientConnection.h"
+
+namespace WebCore {
+
+class WorkerGlobalScope;
+class WorkerThread;
+
+class WorkerSWClientConnection final : public SWClientConnection {
+public:
+    static Ref<WorkerSWClientConnection> create(WorkerGlobalScope& scope) { return adoptRef(*new WorkerSWClientConnection { scope }); }
+    ~WorkerSWClientConnection();
+
+private:
+    explicit WorkerSWClientConnection(WorkerGlobalScope&);
+
+    void matchRegistration(SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) final;
+    void getRegistrations(SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) final;
+    void whenRegistrationReady(const SecurityOriginData& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) final;
+    void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) final;
+    void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) final;
+    void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&) final;
+    void postMessageToServiceWorker(ServiceWorkerIdentifier destination, MessageWithMessagePorts&&, const ServiceWorkerOrClientIdentifier& source) final;
+    SWServerConnectionIdentifier serverConnectionIdentifier() const final;
+    bool mayHaveServiceWorkerRegisteredForOrigin(const SecurityOriginData&) const final;
+    void syncTerminateWorker(ServiceWorkerIdentifier) final;
+    void registerServiceWorkerClient(const SecurityOrigin& topOrigin, const ServiceWorkerClientData&, const Optional<ServiceWorkerRegistrationIdentifier>&, const String& userAgent) final;
+    void unregisterServiceWorkerClient(DocumentIdentifier) final;
+    void finishFetchingScriptInServer(const ServiceWorkerFetchResult&) final;
+    bool isThrottleable() const final;
+    void updateThrottleState() final;
+    void scheduleJobInServer(const ServiceWorkerJobData&) final;
+    void scheduleJob(DocumentOrWorkerIdentifier, const ServiceWorkerJobData&) final;
+
+    PAL::SessionID m_sessionID;
+    Ref<WorkerThread> m_thread;
+
+    uint64_t m_lastRequestIdentifier { 0 };
+    HashMap<uint64_t, RegistrationCallback> m_matchRegistrationRequests;
+    HashMap<uint64_t, GetRegistrationsCallback> m_getRegistrationsRequests;
+    HashMap<uint64_t, WhenRegistrationReadyCallback> m_whenRegistrationReadyRequests;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(SERVICE_WORKER)

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp (249525 => 249526)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp	2019-09-05 09:01:51 UTC (rev 249526)
@@ -190,11 +190,11 @@
     initializeConnectionIfNeeded();
 }
 
-void WebSWClientConnection::whenRegistrationReady(const SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&& callback)
+void WebSWClientConnection::whenRegistrationReady(const SecurityOriginData& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&& callback)
 {
     uint64_t callbackID = ++m_previousCallbackIdentifier;
     m_ongoingRegistrationReadyTasks.add(callbackID, WTFMove(callback));
-    ensureConnectionAndSend(Messages::WebSWServerConnection::WhenRegistrationReady(callbackID, topOrigin.data(), clientURL));
+    ensureConnectionAndSend(Messages::WebSWServerConnection::WhenRegistrationReady(callbackID, topOrigin, clientURL));
 }
 
 void WebSWClientConnection::registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&& registrationData)

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h (249525 => 249526)


--- trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2019-09-05 07:47:22 UTC (rev 249525)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h	2019-09-05 09:01:51 UTC (rev 249526)
@@ -85,7 +85,7 @@
     void matchRegistration(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) final;
     void didMatchRegistration(uint64_t matchRequestIdentifier, Optional<WebCore::ServiceWorkerRegistrationData>&&);
     void didGetRegistrations(uint64_t matchRequestIdentifier, Vector<WebCore::ServiceWorkerRegistrationData>&&);
-    void whenRegistrationReady(const WebCore::SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) final;
+    void whenRegistrationReady(const WebCore::SecurityOriginData& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) final;
     void registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&&);
 
     void getRegistrations(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) final;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to