Move registration activation and clearing from SWServerJobQueue to SWServerRegistration
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 3 Dec 2017 05:32:33 +0000 (05:32 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 3 Dec 2017 05:32:33 +0000 (05:32 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180325

Patch by Youenn Fablet <youenn@apple.com> on 2017-12-02
Reviewed by Chris Dumez.

No change of behavior.

These algorithms fit naturally in SWServerRegistration and will be used as part of clients.claim.

* workers/service/server/SWServer.cpp:
(WebCore::SWServer::clearAll):
(WebCore::SWServer::didFinishActivation):
* workers/service/server/SWServerJobQueue.cpp:
(WebCore::SWServerJobQueue::scriptFetchFinished):
(WebCore::SWServerJobQueue::scriptContextFailedToStart):
(WebCore::SWServerJobQueue::didFinishInstall):
(WebCore::SWServerJobQueue::runUnregisterJob):
(WebCore::SWServerJobQueue::tryActivate): Deleted.
(WebCore::SWServerJobQueue::activate): Deleted.
(WebCore::SWServerJobQueue::didFinishActivation): Deleted.
(WebCore::SWServerJobQueue::tryClearRegistration): Deleted.
(WebCore::clearRegistrationWorker): Deleted.
(WebCore::SWServerJobQueue::clearRegistration): Deleted.
* workers/service/server/SWServerJobQueue.h:
* workers/service/server/SWServerRegistration.cpp:
(WebCore::SWServerRegistration::tryClear):
(WebCore::clearRegistrationWorker):
(WebCore::SWServerRegistration::clear):
(WebCore::SWServerRegistration::tryActivate):
(WebCore::SWServerRegistration::activate):
(WebCore::SWServerRegistration::didFinishActivation):
* workers/service/server/SWServerRegistration.h:

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

Source/WebCore/ChangeLog
Source/WebCore/workers/service/server/SWServer.cpp
Source/WebCore/workers/service/server/SWServerJobQueue.cpp
Source/WebCore/workers/service/server/SWServerJobQueue.h
Source/WebCore/workers/service/server/SWServerRegistration.cpp
Source/WebCore/workers/service/server/SWServerRegistration.h

index a59058b..beb74eb 100644 (file)
@@ -1,3 +1,38 @@
+2017-12-02  Youenn Fablet  <youenn@apple.com>
+
+        Move registration activation and clearing from SWServerJobQueue to SWServerRegistration
+        https://bugs.webkit.org/show_bug.cgi?id=180325
+
+        Reviewed by Chris Dumez.
+
+        No change of behavior.
+
+        These algorithms fit naturally in SWServerRegistration and will be used as part of clients.claim.
+
+        * workers/service/server/SWServer.cpp:
+        (WebCore::SWServer::clearAll):
+        (WebCore::SWServer::didFinishActivation):
+        * workers/service/server/SWServerJobQueue.cpp:
+        (WebCore::SWServerJobQueue::scriptFetchFinished):
+        (WebCore::SWServerJobQueue::scriptContextFailedToStart):
+        (WebCore::SWServerJobQueue::didFinishInstall):
+        (WebCore::SWServerJobQueue::runUnregisterJob):
+        (WebCore::SWServerJobQueue::tryActivate): Deleted.
+        (WebCore::SWServerJobQueue::activate): Deleted.
+        (WebCore::SWServerJobQueue::didFinishActivation): Deleted.
+        (WebCore::SWServerJobQueue::tryClearRegistration): Deleted.
+        (WebCore::clearRegistrationWorker): Deleted.
+        (WebCore::SWServerJobQueue::clearRegistration): Deleted.
+        * workers/service/server/SWServerJobQueue.h:
+        * workers/service/server/SWServerRegistration.cpp:
+        (WebCore::SWServerRegistration::tryClear):
+        (WebCore::clearRegistrationWorker):
+        (WebCore::SWServerRegistration::clear):
+        (WebCore::SWServerRegistration::tryActivate):
+        (WebCore::SWServerRegistration::activate):
+        (WebCore::SWServerRegistration::didFinishActivation):
+        * workers/service/server/SWServerRegistration.h:
+
 2017-12-02  Chris Dumez  <cdumez@apple.com>
 
         Support container.register() / registration.unregister() /  inside service workers
index bbcb4c8..0e23a0c 100644 (file)
@@ -146,7 +146,7 @@ void SWServer::clearAll()
 {
     m_jobQueues.clear();
     while (!m_registrations.isEmpty())
-        SWServerJobQueue::clearRegistration(*this, *m_registrations.begin()->value);
+        m_registrations.begin()->value->clear();
     ASSERT(m_registrationsByID.isEmpty());
     m_originStore->clearAll();
 }
@@ -310,7 +310,7 @@ void SWServer::didFinishInstall(const std::optional<ServiceWorkerJobDataIdentifi
 void SWServer::didFinishActivation(SWServerWorker& worker)
 {
     if (auto* registration = getRegistration(worker.registrationKey()))
-        SWServerJobQueue::didFinishActivation(*registration, worker.identifier());
+        registration->didFinishActivation(worker.identifier());
 }
 
 // https://w3c.github.io/ServiceWorker/#clients-get
index 69a9ebd..65e8469 100644 (file)
@@ -73,7 +73,7 @@ void SWServerJobQueue::scriptFetchFinished(SWServer::Connection& connection, con
 
         // If newestWorker is null, invoke Clear Registration algorithm passing registration as its argument.
         if (!newestWorker)
-            clearRegistration(m_server, *registration);
+            registration->clear();
 
         // Invoke Finish Job with job and abort these steps.
         finishCurrentJob();
@@ -105,7 +105,7 @@ void SWServerJobQueue::scriptContextFailedToStart(const ServiceWorkerJobDataIden
 
     // If newestWorker is null, invoke Clear Registration algorithm passing registration as its argument.
     if (!registration->getNewestWorker())
-        clearRegistration(m_server, *registration);
+        registration->clear();
 
     // Invoke Finish Job with job and abort these steps.
     finishCurrentJob();
@@ -175,7 +175,8 @@ void SWServerJobQueue::didFinishInstall(const ServiceWorkerJobDataIdentifier& jo
 
         // If newestWorker is null, invoke Clear Registration algorithm passing registration as its argument.
         if (!registration->getNewestWorker())
-            clearRegistration(m_server, *registration);
+            registration->clear();
+
         // Invoke Finish Job with job and abort these steps.
         finishCurrentJob();
         return;
@@ -196,70 +197,7 @@ void SWServerJobQueue::didFinishInstall(const ServiceWorkerJobDataIdentifier& jo
     finishCurrentJob();
 
     // FIXME: Wait for all the tasks queued by Update Worker State invoked in this algorithm have executed.
-    tryActivate(m_server, *registration);
-}
-
-// https://w3c.github.io/ServiceWorker/#try-activate-algorithm
-void SWServerJobQueue::tryActivate(SWServer& server, SWServerRegistration& registration)
-{
-    // If registration's waiting worker is null, return.
-    if (!registration.waitingWorker())
-        return;
-    // If registration's active worker is not null and registration's active worker's state is activating, return.
-    if (registration.activeWorker() && registration.activeWorker()->state() == ServiceWorkerState::Activating)
-        return;
-
-    // Invoke Activate with registration if either of the following is true:
-    // - registration's active worker is null.
-    // - The result of running Service Worker Has No Pending Events with registration's active worker is true,
-    //   and no service worker client is using registration
-    // FIXME: Check for the skip waiting flag.
-    if (!registration.activeWorker() || !registration.activeWorker()->hasPendingEvents())
-        activate(server, registration);
-}
-
-// https://w3c.github.io/ServiceWorker/#activate
-void SWServerJobQueue::activate(SWServer& server, SWServerRegistration& registration)
-{
-    // If registration's waiting worker is null, abort these steps.
-    if (!registration.waitingWorker())
-        return;
-
-    // If registration's active worker is not null, then:
-    if (auto* activeWorker = registration.activeWorker()) {
-        // Terminate registration's active worker.
-        activeWorker->terminate();
-        // Run the Update Worker State algorithm passing registration's active worker and redundant as the arguments.
-        registration.updateWorkerState(*activeWorker, ServiceWorkerState::Redundant);
-    }
-    // Run the Update Registration State algorithm passing registration, "active" and registration's waiting worker as the arguments.
-    registration.updateRegistrationState(ServiceWorkerRegistrationState::Active, registration.waitingWorker());
-    // Run the Update Registration State algorithm passing registration, "waiting" and null as the arguments.
-    registration.updateRegistrationState(ServiceWorkerRegistrationState::Waiting, nullptr);
-    // Run the Update Worker State algorithm passing registration's active worker and activating as the arguments.
-    registration.updateWorkerState(*registration.activeWorker(), ServiceWorkerState::Activating);
-    // FIXME: For each service worker client client whose creation URL matches registration's scope url...
-
-    // For each service worker client client who is using registration:
-    // - Set client's active worker to registration's active worker.
-    // - Invoke Notify Controller Change algorithm with client as the argument.
-    registration.notifyClientsOfControllerChange();
-
-    // FIXME: Invoke Run Service Worker algorithm with activeWorker as the argument.
-
-    // Queue a task to fire the activate event.
-    ASSERT(registration.activeWorker());
-    server.fireActivateEvent(*registration.activeWorker());
-}
-
-// https://w3c.github.io/ServiceWorker/#activate (post activate event steps).
-void SWServerJobQueue::didFinishActivation(SWServerRegistration& registration, ServiceWorkerIdentifier serviceWorkerIdentifier)
-{
-    if (!registration.activeWorker() || registration.activeWorker()->identifier() != serviceWorkerIdentifier)
-        return;
-
-    // Run the Update Worker State algorithm passing registration's active worker and activated as the arguments.
-    registration.updateWorkerState(*registration.activeWorker(), ServiceWorkerState::Activated);
+    registration->tryActivate();
 }
 
 // https://w3c.github.io/ServiceWorker/#run-job
@@ -346,48 +284,10 @@ void SWServerJobQueue::runUnregisterJob(const ServiceWorkerJobData& job)
     m_server.resolveUnregistrationJob(job, m_registrationKey, true);
 
     // Invoke Try Clear Registration with registration.
-    tryClearRegistration(*registration);
+    registration->tryClear();
     finishCurrentJob();
 }
 
-// https://w3c.github.io/ServiceWorker/#try-clear-registration-algorithm
-void SWServerJobQueue::tryClearRegistration(SWServerRegistration& registration)
-{
-    if (registration.hasClientsUsingRegistration())
-        return;
-
-    if (registration.installingWorker() && registration.installingWorker()->hasPendingEvents())
-        return;
-    if (registration.waitingWorker() && registration.waitingWorker()->hasPendingEvents())
-        return;
-    if (registration.activeWorker() && registration.activeWorker()->hasPendingEvents())
-        return;
-
-    clearRegistration(m_server, registration);
-}
-
-// https://w3c.github.io/ServiceWorker/#clear-registration
-static void clearRegistrationWorker(SWServerRegistration& registration, SWServerWorker* worker, ServiceWorkerRegistrationState state)
-{
-    if (!worker)
-        return;
-
-    worker->terminate();
-    registration.updateWorkerState(*worker, ServiceWorkerState::Redundant);
-    registration.updateRegistrationState(state, nullptr);
-}
-
-// https://w3c.github.io/ServiceWorker/#clear-registration
-void SWServerJobQueue::clearRegistration(SWServer& server, SWServerRegistration& registration)
-{
-    clearRegistrationWorker(registration, registration.installingWorker(), ServiceWorkerRegistrationState::Installing);
-    clearRegistrationWorker(registration, registration.waitingWorker(), ServiceWorkerRegistrationState::Waiting);
-    clearRegistrationWorker(registration, registration.activeWorker(), ServiceWorkerRegistrationState::Active);
-
-    // Remove scope to registration map[scopeString].
-    server.removeRegistration(registration.key());
-}
-
 // https://w3c.github.io/ServiceWorker/#update-algorithm
 void SWServerJobQueue::runUpdateJob(const ServiceWorkerJobData& job)
 {
index 23ba07a..810915d 100644 (file)
@@ -52,11 +52,8 @@ public:
     void scriptContextFailedToStart(const ServiceWorkerJobDataIdentifier&, ServiceWorkerIdentifier, const String& message);
     void scriptContextStarted(const ServiceWorkerJobDataIdentifier&, ServiceWorkerIdentifier);
     void didFinishInstall(const ServiceWorkerJobDataIdentifier&, ServiceWorkerIdentifier, bool wasSuccessful);
-    static void didFinishActivation(SWServerRegistration&, ServiceWorkerIdentifier);
     void didResolveRegistrationPromise();
 
-    static void clearRegistration(SWServer&, SWServerRegistration&);
-
 private:
     void jobTimerFired();
     void runNextJobSynchronously();
@@ -67,10 +64,7 @@ private:
     void runUnregisterJob(const ServiceWorkerJobData&);
     void runUpdateJob(const ServiceWorkerJobData&);
 
-    void tryClearRegistration(SWServerRegistration&);
     void install(SWServerRegistration&, ServiceWorkerIdentifier);
-    static void tryActivate(SWServer&, SWServerRegistration&);
-    static void activate(SWServer&, SWServerRegistration&);
 
     bool isCurrentlyProcessingJob(const ServiceWorkerJobDataIdentifier&) const;
 
index 2eccf58..82749e4 100644 (file)
@@ -180,6 +180,108 @@ void SWServerRegistration::unregisterServerConnection(SWServerConnectionIdentifi
     m_clientsUsingRegistration.remove(serverConnectionIdentifier);
 }
 
+// https://w3c.github.io/ServiceWorker/#try-clear-registration-algorithm
+bool SWServerRegistration::tryClear()
+{
+    if (hasClientsUsingRegistration())
+        return false;
+
+    if (installingWorker() && installingWorker()->hasPendingEvents())
+        return false;
+    if (waitingWorker() && waitingWorker()->hasPendingEvents())
+        return false;
+    if (activeWorker() && activeWorker()->hasPendingEvents())
+        return false;
+
+    clear();
+    return true;
+}
+
+// https://w3c.github.io/ServiceWorker/#clear-registration
+static void clearRegistrationWorker(SWServerRegistration& registration, SWServerWorker* worker, ServiceWorkerRegistrationState state)
+{
+    if (!worker)
+        return;
+
+    worker->terminate();
+    registration.updateWorkerState(*worker, ServiceWorkerState::Redundant);
+    registration.updateRegistrationState(state, nullptr);
+}
+
+// https://w3c.github.io/ServiceWorker/#clear-registration
+void SWServerRegistration::clear()
+{
+    clearRegistrationWorker(*this, installingWorker(), ServiceWorkerRegistrationState::Installing);
+    clearRegistrationWorker(*this, waitingWorker(), ServiceWorkerRegistrationState::Waiting);
+    clearRegistrationWorker(*this, activeWorker(), ServiceWorkerRegistrationState::Active);
+
+    // Remove scope to registration map[scopeString].
+    m_server.removeRegistration(key());
+}
+
+// https://w3c.github.io/ServiceWorker/#try-activate-algorithm
+void SWServerRegistration::tryActivate()
+{
+    // If registration's waiting worker is null, return.
+    if (!waitingWorker())
+        return;
+    // If registration's active worker is not null and registration's active worker's state is activating, return.
+    if (activeWorker() && activeWorker()->state() == ServiceWorkerState::Activating)
+        return;
+
+    // Invoke Activate with registration if either of the following is true:
+    // - registration's active worker is null.
+    // - The result of running Service Worker Has No Pending Events with registration's active worker is true,
+    //   and no service worker client is using registration
+    // FIXME: Check for the skip waiting flag.
+    if (!activeWorker() || !activeWorker()->hasPendingEvents())
+        activate();
+}
+
+// https://w3c.github.io/ServiceWorker/#activate
+void SWServerRegistration::activate()
+{
+    // If registration's waiting worker is null, abort these steps.
+    if (!waitingWorker())
+        return;
+
+    // If registration's active worker is not null, then:
+    if (auto* worker = activeWorker()) {
+        // Terminate registration's active worker.
+        worker->terminate();
+        // Run the Update Worker State algorithm passing registration's active worker and redundant as the arguments.
+        updateWorkerState(*worker, ServiceWorkerState::Redundant);
+    }
+    // Run the Update Registration State algorithm passing registration, "active" and registration's waiting worker as the arguments.
+    updateRegistrationState(ServiceWorkerRegistrationState::Active, waitingWorker());
+    // Run the Update Registration State algorithm passing registration, "waiting" and null as the arguments.
+    updateRegistrationState(ServiceWorkerRegistrationState::Waiting, nullptr);
+    // Run the Update Worker State algorithm passing registration's active worker and activating as the arguments.
+    updateWorkerState(*activeWorker(), ServiceWorkerState::Activating);
+    // FIXME: For each service worker client client whose creation URL matches registration's scope url...
+
+    // For each service worker client client who is using registration:
+    // - Set client's active worker to registration's active worker.
+    // - Invoke Notify Controller Change algorithm with client as the argument.
+    notifyClientsOfControllerChange();
+
+    // FIXME: Invoke Run Service Worker algorithm with activeWorker as the argument.
+
+    // Queue a task to fire the activate event.
+    ASSERT(activeWorker());
+    m_server.fireActivateEvent(*activeWorker());
+}
+
+// https://w3c.github.io/ServiceWorker/#activate (post activate event steps).
+void SWServerRegistration::didFinishActivation(ServiceWorkerIdentifier serviceWorkerIdentifier)
+{
+    if (!activeWorker() || activeWorker()->identifier() != serviceWorkerIdentifier)
+        return;
+
+    // Run the Update Worker State algorithm passing registration's active worker and activated as the arguments.
+    updateWorkerState(*activeWorker(), ServiceWorkerState::Activated);
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(SERVICE_WORKER)
index 8a10af3..20b27d2 100644 (file)
@@ -81,9 +81,16 @@ public:
 
     void notifyClientsOfControllerChange();
 
+    void clear();
+    bool tryClear();
+    void tryActivate();
+    void didFinishActivation(ServiceWorkerIdentifier);
+
 private:
     void forEachConnection(const WTF::Function<void(SWServer::Connection&)>&);
 
+    void activate();
+
     ServiceWorkerRegistrationIdentifier m_identifier;
     ServiceWorkerRegistrationKey m_registrationKey;
     ServiceWorkerUpdateViaCache m_updateViaCache;