Implement "UpdateWorkerState" and use it
[WebKit-https.git] / Source / WebCore / workers / service / server / SWServerRegistration.cpp
index 7b14857..e5bab02 100644 (file)
@@ -61,6 +61,8 @@ SWServerWorker* SWServerRegistration::getNewestWorker()
 
 void SWServerRegistration::updateRegistrationState(ServiceWorkerRegistrationState state, SWServerWorker* worker)
 {
+    LOG(ServiceWorker, "(%p) Updating registration state to %i with worker %p", this, (int)state, worker);
+    
     switch (state) {
     case ServiceWorkerRegistrationState::Installing:
         m_installingWorker = worker;
@@ -83,6 +85,18 @@ void SWServerRegistration::updateRegistrationState(ServiceWorkerRegistrationStat
     }
 }
 
+void SWServerRegistration::updateWorkerState(SWServerWorker& worker, ServiceWorkerState state)
+{
+    LOG(ServiceWorker, "Updating worker %p state to %i (%p)", &worker, (int)state, this);
+
+    worker.setState(state);
+
+    for (auto& connectionIdentifierWithClients : m_clientRegistrationsByConnection.keys()) {
+        if (auto* connection = m_server.getConnection(connectionIdentifierWithClients))
+            connection->updateWorkerStateInClient(worker.identifier(), state);
+    }
+}
+
 void SWServerRegistration::fireUpdateFoundEvent(uint64_t connectionIdentifier)
 {
     // No matter what, we send the event to the connection that scheduled the job. The client registration
@@ -115,7 +129,19 @@ void SWServerRegistration::firePostInstallEvents(uint64_t connectionIdentifier)
 
 ServiceWorkerRegistrationData SWServerRegistration::data() const
 {
-    return { m_registrationKey, identifier(), m_activeServiceWorkerIdentifier, m_scopeURL, m_scriptURL, m_updateViaCache };
+    std::optional<ServiceWorkerIdentifier> installingID;
+    if (m_installingWorker)
+        installingID = m_installingWorker->identifier();
+
+    std::optional<ServiceWorkerIdentifier> waitingID;
+    if (m_waitingWorker)
+        waitingID = m_waitingWorker->identifier();
+
+    std::optional<ServiceWorkerIdentifier> activeID;
+    if (m_activeWorker)
+        activeID = m_activeWorker->identifier();
+
+    return { m_registrationKey, identifier(), m_scopeURL, m_scriptURL, m_updateViaCache, installingID, waitingID, activeID };
 }
 
 void SWServerRegistration::addClientServiceWorkerRegistration(uint64_t connectionIdentifier, uint64_t clientRegistrationIdentifier)