Introduce ServiceWorkerContainer::ensureSWClientConnection()
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 29 Nov 2017 19:41:22 +0000 (19:41 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 29 Nov 2017 19:41:22 +0000 (19:41 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180146

Reviewed by Youenn Fablet.

Introduce ServiceWorkerContainer::ensureSWClientConnection() to reduce
code duplication. Also use callOnMainThread() in preparation for this
getting called from a service worker thread (now that ServiceWorkerContainer
is exposed to service workers). This is needed because constructing the
SWClientConnection initializes the IPC connection from the WebProcess to
the StorageProcess, which involves a synchronous IPC with the UIProcess.
Doing a synchronous IPC from a background thread is unsupported.

* workers/service/ServiceWorkerContainer.cpp:
(WebCore::ServiceWorkerContainer::addRegistration):
(WebCore::ServiceWorkerContainer::getRegistration):
(WebCore::ServiceWorkerContainer::getRegistrations):
(WebCore::ServiceWorkerContainer::ensureSWClientConnection):
* workers/service/ServiceWorkerContainer.h:

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

Source/WebCore/ChangeLog
Source/WebCore/workers/service/ServiceWorkerContainer.cpp
Source/WebCore/workers/service/ServiceWorkerContainer.h

index c27f07e..ef89fb4 100644 (file)
@@ -1,3 +1,25 @@
+2017-11-29  Chris Dumez  <cdumez@apple.com>
+
+        Introduce ServiceWorkerContainer::ensureSWClientConnection()
+        https://bugs.webkit.org/show_bug.cgi?id=180146
+
+        Reviewed by Youenn Fablet.
+
+        Introduce ServiceWorkerContainer::ensureSWClientConnection() to reduce
+        code duplication. Also use callOnMainThread() in preparation for this
+        getting called from a service worker thread (now that ServiceWorkerContainer
+        is exposed to service workers). This is needed because constructing the
+        SWClientConnection initializes the IPC connection from the WebProcess to
+        the StorageProcess, which involves a synchronous IPC with the UIProcess.
+        Doing a synchronous IPC from a background thread is unsupported.
+
+        * workers/service/ServiceWorkerContainer.cpp:
+        (WebCore::ServiceWorkerContainer::addRegistration):
+        (WebCore::ServiceWorkerContainer::getRegistration):
+        (WebCore::ServiceWorkerContainer::getRegistrations):
+        (WebCore::ServiceWorkerContainer::ensureSWClientConnection):
+        * workers/service/ServiceWorkerContainer.h:
+
 2017-11-29  Antoine Quint  <graouts@apple.com>
 
         [iOS] Media controls should stop updating while media is playing in fullscreen
index f679309..1dfb049 100644 (file)
@@ -102,10 +102,7 @@ void ServiceWorkerContainer::addRegistration(const String& relativeScriptURL, co
         return;
     }
 
-    if (!m_swConnection)
-        m_swConnection = &ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(scriptExecutionContext()->sessionID());
-
-    ServiceWorkerJobData jobData(m_swConnection->serverConnectionIdentifier());
+    ServiceWorkerJobData jobData(ensureSWClientConnection().serverConnectionIdentifier());
 
     jobData.scriptURL = context->completeURL(relativeScriptURL);
     if (!jobData.scriptURL.isValid()) {
@@ -231,10 +228,7 @@ void ServiceWorkerContainer::getRegistration(const String& clientURL, Ref<Deferr
         return;
     }
 
-    if (!m_swConnection)
-        m_swConnection = &ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(context->sessionID());
-
-    return m_swConnection->matchRegistration(context->topOrigin(), parsedURL, [promise = WTFMove(promise), protectingThis = makePendingActivity(*this), this] (auto&& result) mutable {
+    return ensureSWClientConnection().matchRegistration(context->topOrigin(), parsedURL, [promise = WTFMove(promise), protectingThis = makePendingActivity(*this), this] (auto&& result) mutable {
         if (m_isStopped)
             return;
 
@@ -278,10 +272,7 @@ void ServiceWorkerContainer::getRegistrations(RegistrationsPromise&& promise)
         return;
     }
 
-    if (!m_swConnection)
-        m_swConnection = &ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(context->sessionID());
-
-    return m_swConnection->getRegistrations(context->topOrigin(), context->url(), [this, pendingActivity = makePendingActivity(*this), promise = WTFMove(promise)] (auto&& registrationDatas) mutable {
+    return ensureSWClientConnection().getRegistrations(context->topOrigin(), context->url(), [this, pendingActivity = makePendingActivity(*this), promise = WTFMove(promise)] (auto&& registrationDatas) mutable {
         if (m_isStopped)
             return;
 
@@ -418,6 +409,17 @@ bool ServiceWorkerContainer::canSuspendForDocumentSuspension() const
     return !hasPendingActivity();
 }
 
+SWClientConnection& ServiceWorkerContainer::ensureSWClientConnection()
+{
+    if (!m_swConnection) {
+        ASSERT(scriptExecutionContext());
+        callOnMainThreadAndWait([this, sessionID = scriptExecutionContext()->sessionID()]() {
+            m_swConnection = &ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(sessionID);
+        });
+    }
+    return *m_swConnection;
+}
+
 void ServiceWorkerContainer::addRegistration(ServiceWorkerRegistration& registration)
 {
     m_swConnection->addServiceWorkerRegistrationInServer(registration.identifier());
index cc73fa9..a0a8e75 100644 (file)
@@ -97,6 +97,7 @@ private:
     void jobDidFinish(ServiceWorkerJob&);
 
     SWServerConnectionIdentifier connectionIdentifier() final;
+    SWClientConnection& ensureSWClientConnection();
 
     const char* activeDOMObjectName() const final;
     bool canSuspendForDocumentSuspension() const final;