4f670b335a9dd8cf6b8963afb92b38cff3bee92c
[WebKit-https.git] / Source / WebCore / workers / service / server / SWServer.cpp
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "SWServer.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "ExceptionCode.h"
32 #include "ExceptionData.h"
33 #include "Logging.h"
34 #include "RegistrationStore.h"
35 #include "SWOriginStore.h"
36 #include "SWServerJobQueue.h"
37 #include "SWServerRegistration.h"
38 #include "SWServerToContextConnection.h"
39 #include "SWServerWorker.h"
40 #include "SecurityOrigin.h"
41 #include "ServiceWorkerClientType.h"
42 #include "ServiceWorkerContextData.h"
43 #include "ServiceWorkerFetchResult.h"
44 #include "ServiceWorkerJobData.h"
45 #include <wtf/CompletionHandler.h>
46 #include <wtf/NeverDestroyed.h>
47 #include <wtf/text/WTFString.h>
48
49 namespace WebCore {
50
51 static Seconds terminationDelay { 10_s };
52
53 SWServer::Connection::Connection(SWServer& server)
54     : m_server(server)
55     , m_identifier(SWServerConnectionIdentifier::generate())
56 {
57 }
58
59 HashSet<SWServer*>& SWServer::allServers()
60 {
61     static NeverDestroyed<HashSet<SWServer*>> servers;
62     return servers;
63 }
64
65 SWServer::~SWServer()
66 {
67     // Destroy the remaining connections before the SWServer gets destroyed since they have a raw pointer
68     // to the server and since they try to unregister clients from the server in their destructor.
69     auto connections = WTFMove(m_connections);
70     connections.clear();
71
72     allServers().remove(this);
73 }
74
75 SWServerWorker* SWServer::workerByID(ServiceWorkerIdentifier identifier) const
76 {
77     auto* worker = SWServerWorker::existingWorkerForIdentifier(identifier);
78     ASSERT(!worker || &worker->server() == this);
79     return worker;
80 }
81
82 Optional<ServiceWorkerClientData> SWServer::serviceWorkerClientWithOriginByID(const ClientOrigin& clientOrigin, const ServiceWorkerClientIdentifier& clientIdentifier) const
83 {
84     auto iterator = m_clientIdentifiersPerOrigin.find(clientOrigin);
85     if (iterator == m_clientIdentifiersPerOrigin.end())
86         return WTF::nullopt;
87
88     if (!iterator->value.identifiers.contains(clientIdentifier))
89         return WTF::nullopt;
90
91     auto clientIterator = m_clientsById.find(clientIdentifier);
92     ASSERT(clientIterator != m_clientsById.end());
93     return clientIterator->value;
94 }
95
96 String SWServer::serviceWorkerClientUserAgent(const ClientOrigin& clientOrigin) const
97 {
98     auto iterator = m_clientIdentifiersPerOrigin.find(clientOrigin);
99     if (iterator == m_clientIdentifiersPerOrigin.end())
100         return String();
101     return iterator->value.userAgent;
102 }
103
104 SWServerWorker* SWServer::activeWorkerFromRegistrationID(ServiceWorkerRegistrationIdentifier identifier)
105 {
106     auto* registration = m_registrationsByID.get(identifier);
107     return registration ? registration->activeWorker() : nullptr;
108 }
109
110 SWServerRegistration* SWServer::getRegistration(const ServiceWorkerRegistrationKey& registrationKey)
111 {
112     return m_registrations.get(registrationKey);
113 }
114
115 void SWServer::registrationStoreImportComplete()
116 {
117     ASSERT(!m_importCompleted);
118     m_importCompleted = true;
119     m_originStore->importComplete();
120
121     auto clearCallbacks = WTFMove(m_clearCompletionCallbacks);
122     for (auto& callback : clearCallbacks)
123         callback();
124
125     performGetOriginsWithRegistrationsCallbacks();
126 }
127
128 void SWServer::registrationStoreDatabaseFailedToOpen()
129 {
130     if (!m_importCompleted)
131         registrationStoreImportComplete();
132 }
133
134 void SWServer::addRegistrationFromStore(ServiceWorkerContextData&& data)
135 {
136     // Pages should not have been able to make a new registration to this key while the import was still taking place.
137     ASSERT(!m_registrations.contains(data.registration.key));
138
139     auto registration = std::make_unique<SWServerRegistration>(*this, data.registration.key, data.registration.updateViaCache, data.registration.scopeURL, data.scriptURL);
140     registration->setLastUpdateTime(data.registration.lastUpdateTime);
141     auto registrationPtr = registration.get();
142     addRegistration(WTFMove(registration));
143
144     auto worker = SWServerWorker::create(*this, *registrationPtr, data.scriptURL, data.script, data.contentSecurityPolicy, WTFMove(data.referrerPolicy), data.workerType, data.serviceWorkerIdentifier, HashMap<URL, ServiceWorkerContextData::ImportedScript> { data.scriptResourceMap });
145     registrationPtr->updateRegistrationState(ServiceWorkerRegistrationState::Active, worker.ptr());
146     worker->setState(ServiceWorkerState::Activated);
147 }
148
149 void SWServer::addRegistration(std::unique_ptr<SWServerRegistration>&& registration)
150 {
151     auto key = registration->key();
152     auto* registrationPtr = registration.get();
153     auto addResult1 = m_registrations.add(key, WTFMove(registration));
154     ASSERT_UNUSED(addResult1, addResult1.isNewEntry);
155
156     auto addResult2 = m_registrationsByID.add(registrationPtr->identifier(), registrationPtr);
157     ASSERT_UNUSED(addResult2, addResult2.isNewEntry);
158
159     m_originStore->add(key.topOrigin());
160 }
161
162 void SWServer::removeRegistration(const ServiceWorkerRegistrationKey& key)
163 {
164     auto topOrigin = key.topOrigin();
165     auto registration = m_registrations.take(key);
166     ASSERT(registration);
167     bool wasRemoved = m_registrationsByID.remove(registration->identifier());
168     ASSERT_UNUSED(wasRemoved, wasRemoved);
169
170     m_originStore->remove(topOrigin);
171     if (m_registrationStore)
172         m_registrationStore->removeRegistration(*registration);
173 }
174
175 Vector<ServiceWorkerRegistrationData> SWServer::getRegistrations(const SecurityOriginData& topOrigin, const URL& clientURL)
176 {
177     Vector<SWServerRegistration*> matchingRegistrations;
178     for (auto& item : m_registrations) {
179         if (!item.value->isUninstalling() && item.key.originIsMatching(topOrigin, clientURL))
180             matchingRegistrations.append(item.value.get());
181     }
182     // The specification mandates that registrations are returned in the insertion order.
183     std::sort(matchingRegistrations.begin(), matchingRegistrations.end(), [](auto& a, auto& b) {
184         return a->creationTime() < b->creationTime();
185     });
186     Vector<ServiceWorkerRegistrationData> matchingRegistrationDatas;
187     matchingRegistrationDatas.reserveInitialCapacity(matchingRegistrations.size());
188     for (auto* registration : matchingRegistrations)
189         matchingRegistrationDatas.uncheckedAppend(registration->data());
190     return matchingRegistrationDatas;
191 }
192
193 void SWServer::clearAll(CompletionHandler<void()>&& completionHandler)
194 {
195     if (!m_importCompleted) {
196         m_clearCompletionCallbacks.append([this, completionHandler = WTFMove(completionHandler)] () mutable {
197             ASSERT(m_importCompleted);
198             clearAll(WTFMove(completionHandler));
199         });
200         return;
201     }
202
203     m_jobQueues.clear();
204     while (!m_registrations.isEmpty())
205         m_registrations.begin()->value->clear();
206     ASSERT(m_registrationsByID.isEmpty());
207     m_pendingContextDatas.clear();
208     m_originStore->clearAll();
209     if (m_registrationStore)
210         m_registrationStore->clearAll(WTFMove(completionHandler));
211 }
212
213 void SWServer::clear(const SecurityOriginData& securityOrigin, CompletionHandler<void()>&& completionHandler)
214 {
215     if (!m_importCompleted) {
216         m_clearCompletionCallbacks.append([this, securityOrigin, completionHandler = WTFMove(completionHandler)] () mutable {
217             ASSERT(m_importCompleted);
218             clear(securityOrigin, WTFMove(completionHandler));
219         });
220         return;
221     }
222
223     m_jobQueues.removeIf([&](auto& keyAndValue) {
224         return keyAndValue.key.relatesToOrigin(securityOrigin);
225     });
226
227     Vector<SWServerRegistration*> registrationsToRemove;
228     for (auto& keyAndValue : m_registrations) {
229         if (keyAndValue.key.relatesToOrigin(securityOrigin))
230             registrationsToRemove.append(keyAndValue.value.get());
231     }
232
233     for (auto& contextDatas : m_pendingContextDatas.values()) {
234         contextDatas.removeAllMatching([&](auto& contextData) {
235             return contextData.registration.key.relatesToOrigin(securityOrigin);
236         });
237     }
238
239     if (registrationsToRemove.isEmpty()) {
240         completionHandler();
241         return;
242     }
243
244     // Calling SWServerRegistration::clear() takes care of updating m_registrations, m_originStore and m_registrationStore.
245     for (auto* registration : registrationsToRemove)
246         registration->clear();
247
248     if (m_registrationStore)
249         m_registrationStore->flushChanges(WTFMove(completionHandler));
250 }
251
252 void SWServer::Connection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
253 {
254     m_server.scriptFetchFinished(*this, result);
255 }
256
257 void SWServer::Connection::didResolveRegistrationPromise(const ServiceWorkerRegistrationKey& key)
258 {
259     m_server.didResolveRegistrationPromise(*this, key);
260 }
261
262 void SWServer::Connection::addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
263 {
264     m_server.addClientServiceWorkerRegistration(*this, identifier);
265 }
266
267 void SWServer::Connection::removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
268 {
269     m_server.removeClientServiceWorkerRegistration(*this, identifier);
270 }
271
272 void SWServer::Connection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
273 {
274     if (auto* worker = m_server.workerByID(identifier))
275         m_server.syncTerminateWorker(*worker);
276 }
277
278 SWServer::SWServer(UniqueRef<SWOriginStore>&& originStore, String&& registrationDatabaseDirectory, PAL::SessionID sessionID)
279     : m_originStore(WTFMove(originStore))
280     , m_sessionID(sessionID)
281 {
282     ASSERT(!registrationDatabaseDirectory.isEmpty() || m_sessionID.isEphemeral());
283     if (!m_sessionID.isEphemeral())
284         m_registrationStore = std::make_unique<RegistrationStore>(*this, WTFMove(registrationDatabaseDirectory));
285     else
286         registrationStoreImportComplete();
287
288     UNUSED_PARAM(registrationDatabaseDirectory);
289     allServers().add(this);
290 }
291
292 // https://w3c.github.io/ServiceWorker/#schedule-job-algorithm
293 void SWServer::scheduleJob(ServiceWorkerJobData&& jobData)
294 {
295     ASSERT(m_connections.contains(jobData.connectionIdentifier()));
296
297     // FIXME: Per the spec, check if this job is equivalent to the last job on the queue.
298     // If it is, stack it along with that job.
299
300     auto& jobQueue = *m_jobQueues.ensure(jobData.registrationKey(), [this, &jobData] {
301         return std::make_unique<SWServerJobQueue>(*this, jobData.registrationKey());
302     }).iterator->value;
303
304     jobQueue.enqueueJob(jobData);
305     if (jobQueue.size() == 1)
306         jobQueue.runNextJob();
307 }
308
309 void SWServer::rejectJob(const ServiceWorkerJobData& jobData, const ExceptionData& exceptionData)
310 {
311     LOG(ServiceWorker, "Rejected ServiceWorker job %s in server", jobData.identifier().loggingString().utf8().data());
312     auto* connection = m_connections.get(jobData.connectionIdentifier());
313     if (!connection)
314         return;
315
316     connection->rejectJobInClient(jobData.identifier().jobIdentifier, exceptionData);
317 }
318
319 void SWServer::resolveRegistrationJob(const ServiceWorkerJobData& jobData, const ServiceWorkerRegistrationData& registrationData, ShouldNotifyWhenResolved shouldNotifyWhenResolved)
320 {
321     LOG(ServiceWorker, "Resolved ServiceWorker job %s in server with registration %s", jobData.identifier().loggingString().utf8().data(), registrationData.identifier.loggingString().utf8().data());
322     auto* connection = m_connections.get(jobData.connectionIdentifier());
323     if (!connection)
324         return;
325
326     connection->resolveRegistrationJobInClient(jobData.identifier().jobIdentifier, registrationData, shouldNotifyWhenResolved);
327 }
328
329 void SWServer::resolveUnregistrationJob(const ServiceWorkerJobData& jobData, const ServiceWorkerRegistrationKey& registrationKey, bool unregistrationResult)
330 {
331     auto* connection = m_connections.get(jobData.connectionIdentifier());
332     if (!connection)
333         return;
334
335     connection->resolveUnregistrationJobInClient(jobData.identifier().jobIdentifier, registrationKey, unregistrationResult);
336 }
337
338 void SWServer::startScriptFetch(const ServiceWorkerJobData& jobData, FetchOptions::Cache cachePolicy)
339 {
340     LOG(ServiceWorker, "Server issuing startScriptFetch for current job %s in client", jobData.identifier().loggingString().utf8().data());
341     auto* connection = m_connections.get(jobData.connectionIdentifier());
342     ASSERT_WITH_MESSAGE(connection, "If the connection was lost, this job should have been cancelled");
343     if (connection)
344         connection->startScriptFetchInClient(jobData.identifier().jobIdentifier, jobData.registrationKey(), cachePolicy);
345 }
346
347 void SWServer::scriptFetchFinished(Connection& connection, const ServiceWorkerFetchResult& result)
348 {
349     LOG(ServiceWorker, "Server handling scriptFetchFinished for current job %s in client", result.jobDataIdentifier.loggingString().utf8().data());
350
351     ASSERT(m_connections.contains(result.jobDataIdentifier.connectionIdentifier));
352
353     auto jobQueue = m_jobQueues.get(result.registrationKey);
354     if (!jobQueue)
355         return;
356
357     jobQueue->scriptFetchFinished(connection, result);
358 }
359
360 void SWServer::scriptContextFailedToStart(const Optional<ServiceWorkerJobDataIdentifier>& jobDataIdentifier, SWServerWorker& worker, const String& message)
361 {
362     if (!jobDataIdentifier)
363         return;
364
365     RELEASE_LOG_ERROR(ServiceWorker, "%p - SWServer::scriptContextFailedToStart: Failed to start SW for job %s, error: %s", this, jobDataIdentifier->loggingString().utf8().data(), message.utf8().data());
366
367     auto* jobQueue = m_jobQueues.get(worker.registrationKey());
368     if (!jobQueue || !jobQueue->isCurrentlyProcessingJob(*jobDataIdentifier)) {
369         // The job which started this worker has been canceled, terminate this worker.
370         terminatePreinstallationWorker(worker);
371         return;
372     }
373     jobQueue->scriptContextFailedToStart(*jobDataIdentifier, worker.identifier(), message);
374 }
375
376 void SWServer::scriptContextStarted(const Optional<ServiceWorkerJobDataIdentifier>& jobDataIdentifier, SWServerWorker& worker)
377 {
378     if (!jobDataIdentifier)
379         return;
380
381     auto* jobQueue = m_jobQueues.get(worker.registrationKey());
382     if (!jobQueue || !jobQueue->isCurrentlyProcessingJob(*jobDataIdentifier)) {
383         // The job which started this worker has been canceled, terminate this worker.
384         terminatePreinstallationWorker(worker);
385         return;
386     }
387     jobQueue->scriptContextStarted(*jobDataIdentifier, worker.identifier());
388 }
389
390 void SWServer::terminatePreinstallationWorker(SWServerWorker& worker)
391 {
392     worker.terminate();
393     auto* registration = getRegistration(worker.registrationKey());
394     if (registration && registration->preInstallationWorker() == &worker)
395         registration->setPreInstallationWorker(nullptr);
396 }
397
398 void SWServer::didFinishInstall(const Optional<ServiceWorkerJobDataIdentifier>& jobDataIdentifier, SWServerWorker& worker, bool wasSuccessful)
399 {
400     if (!jobDataIdentifier)
401         return;
402
403     if (wasSuccessful)
404         RELEASE_LOG(ServiceWorker, "%p - SWServer::didFinishInstall: Successfuly finished SW install for job %s", this, jobDataIdentifier->loggingString().utf8().data());
405     else
406         RELEASE_LOG_ERROR(ServiceWorker, "%p - SWServer::didFinishInstall: Failed SW install for job %s", this, jobDataIdentifier->loggingString().utf8().data());
407
408     if (auto* jobQueue = m_jobQueues.get(worker.registrationKey()))
409         jobQueue->didFinishInstall(*jobDataIdentifier, worker.identifier(), wasSuccessful);
410 }
411
412 void SWServer::didFinishActivation(SWServerWorker& worker)
413 {
414     RELEASE_LOG(ServiceWorker, "%p - SWServer::didFinishActivation: Finished activation for service worker %llu", this, worker.identifier().toUInt64());
415
416     auto* registration = getRegistration(worker.registrationKey());
417     if (!registration)
418         return;
419
420     if (m_registrationStore)
421         m_registrationStore->updateRegistration(worker.contextData());
422     registration->didFinishActivation(worker.identifier());
423 }
424
425 // https://w3c.github.io/ServiceWorker/#clients-getall
426 void SWServer::matchAll(SWServerWorker& worker, const ServiceWorkerClientQueryOptions& options, ServiceWorkerClientsMatchAllCallback&& callback)
427 {
428     // FIXME: Support reserved client filtering.
429     // FIXME: Support WindowClient additional properties.
430
431     Vector<ServiceWorkerClientData> matchingClients;
432     forEachClientForOrigin(worker.origin(), [&](auto& clientData) {
433         if (!options.includeUncontrolled) {
434             auto registrationIdentifier = m_clientToControllingRegistration.get(clientData.identifier);
435             if (worker.data().registrationIdentifier != registrationIdentifier)
436                 return;
437             if (&worker != this->activeWorkerFromRegistrationID(registrationIdentifier))
438                 return;
439         }
440         if (options.type != ServiceWorkerClientType::All && options.type != clientData.type)
441             return;
442         matchingClients.append(clientData);
443     });
444     callback(WTFMove(matchingClients));
445 }
446
447 void SWServer::forEachClientForOrigin(const ClientOrigin& origin, const WTF::Function<void(ServiceWorkerClientData&)>& apply)
448 {
449     auto iterator = m_clientIdentifiersPerOrigin.find(origin);
450     if (iterator == m_clientIdentifiersPerOrigin.end())
451         return;
452
453     for (auto& clientIdentifier : iterator->value.identifiers) {
454         auto clientIterator = m_clientsById.find(clientIdentifier);
455         ASSERT(clientIterator != m_clientsById.end());
456         apply(clientIterator->value);
457     }
458 }
459
460 void SWServer::claim(SWServerWorker& worker)
461 {
462     auto& origin = worker.origin();
463     forEachClientForOrigin(origin, [&](auto& clientData) {
464         auto* registration = this->doRegistrationMatching(origin.topOrigin, clientData.url);
465         if (!(registration && registration->key() == worker.registrationKey()))
466             return;
467
468         auto result = m_clientToControllingRegistration.add(clientData.identifier, registration->identifier());
469         if (!result.isNewEntry) {
470             auto previousIdentifier = result.iterator->value;
471             if (previousIdentifier == registration->identifier())
472                 return;
473             result.iterator->value = registration->identifier();
474             if (auto* controllingRegistration = m_registrationsByID.get(previousIdentifier))
475                 controllingRegistration->removeClientUsingRegistration(clientData.identifier);
476         }
477         registration->controlClient(clientData.identifier);
478     });
479 }
480
481 void SWServer::didResolveRegistrationPromise(Connection& connection, const ServiceWorkerRegistrationKey& registrationKey)
482 {
483     ASSERT_UNUSED(connection, m_connections.contains(connection.identifier()));
484
485     if (auto* jobQueue = m_jobQueues.get(registrationKey))
486         jobQueue->didResolveRegistrationPromise();
487 }
488
489 void SWServer::addClientServiceWorkerRegistration(Connection& connection, ServiceWorkerRegistrationIdentifier identifier)
490 {
491     auto* registration = m_registrationsByID.get(identifier);
492     if (!registration) {
493         LOG_ERROR("Request to add client-side ServiceWorkerRegistration to non-existent server-side registration");
494         return;
495     }
496     
497     registration->addClientServiceWorkerRegistration(connection.identifier());
498 }
499
500 void SWServer::removeClientServiceWorkerRegistration(Connection& connection, ServiceWorkerRegistrationIdentifier identifier)
501 {
502     if (auto* registration = m_registrationsByID.get(identifier))
503         registration->removeClientServiceWorkerRegistration(connection.identifier());
504 }
505
506 void SWServer::updateWorker(Connection&, const ServiceWorkerJobDataIdentifier& jobDataIdentifier, SWServerRegistration& registration, const URL& url, const String& script, const ContentSecurityPolicyResponseHeaders& contentSecurityPolicy, const String& referrerPolicy, WorkerType type, HashMap<URL, ServiceWorkerContextData::ImportedScript>&& scriptResourceMap)
507 {
508     tryInstallContextData({ jobDataIdentifier, registration.data(), ServiceWorkerIdentifier::generate(), script, contentSecurityPolicy, referrerPolicy, url, type, sessionID(), false, WTFMove(scriptResourceMap) });
509 }
510
511 void SWServer::tryInstallContextData(ServiceWorkerContextData&& data)
512 {
513     RegistrableDomain registrableDomain(data.scriptURL);
514     auto* connection = SWServerToContextConnection::connectionForRegistrableDomain(registrableDomain);
515     if (!connection) {
516         m_pendingContextDatas.ensure(WTFMove(registrableDomain), [] {
517             return Vector<ServiceWorkerContextData> { };
518         }).iterator->value.append(WTFMove(data));
519         return;
520     }
521     
522     installContextData(data);
523 }
524
525 void SWServer::serverToContextConnectionCreated(SWServerToContextConnection& contextConnection)
526 {
527     auto pendingContextDatas = m_pendingContextDatas.take(contextConnection.registrableDomain());
528     for (auto& data : pendingContextDatas)
529         installContextData(data);
530
531     auto serviceWorkerRunRequests = m_serviceWorkerRunRequests.take(contextConnection.registrableDomain());
532     for (auto& item : serviceWorkerRunRequests) {
533         bool success = runServiceWorker(item.key);
534         for (auto& callback : item.value)
535             callback(success ? &contextConnection : nullptr);
536     }
537 }
538
539 void SWServer::installContextData(const ServiceWorkerContextData& data)
540 {
541     ASSERT_WITH_MESSAGE(!data.loadedFromDisk, "Workers we just read from disk should only be launched as needed");
542
543     if (data.jobDataIdentifier) {
544         // Abort if the job that scheduled this has been cancelled.
545         auto* jobQueue = m_jobQueues.get(data.registration.key);
546         if (!jobQueue || !jobQueue->isCurrentlyProcessingJob(*data.jobDataIdentifier))
547             return;
548     }
549
550     auto* registration = m_registrations.get(data.registration.key);
551     RELEASE_ASSERT(registration);
552
553     auto worker = SWServerWorker::create(*this, *registration, data.scriptURL, data.script, data.contentSecurityPolicy, String { data.referrerPolicy }, data.workerType, data.serviceWorkerIdentifier, HashMap<URL, ServiceWorkerContextData::ImportedScript> { data.scriptResourceMap });
554
555     auto* connection = worker->contextConnection();
556     ASSERT(connection);
557
558     registration->setPreInstallationWorker(worker.ptr());
559     worker->setState(SWServerWorker::State::Running);
560     auto userAgent = worker->userAgent();
561     auto result = m_runningOrTerminatingWorkers.add(data.serviceWorkerIdentifier, WTFMove(worker));
562     ASSERT_UNUSED(result, result.isNewEntry);
563
564     connection->installServiceWorkerContext(data, m_sessionID, userAgent);
565 }
566
567 void SWServer::runServiceWorkerIfNecessary(ServiceWorkerIdentifier identifier, RunServiceWorkerCallback&& callback)
568 {
569     auto* worker = workerByID(identifier);
570     if (!worker) {
571         callback(nullptr);
572         return;
573     }
574
575     auto* contextConnection = worker->contextConnection();
576     if (worker->isRunning()) {
577         ASSERT(contextConnection);
578         callback(contextConnection);
579         return;
580     }
581
582     if (!contextConnection) {
583         auto& serviceWorkerRunRequestsForOrigin = m_serviceWorkerRunRequests.ensure(worker->registrableDomain(), [] {
584             return HashMap<ServiceWorkerIdentifier, Vector<RunServiceWorkerCallback>> { };
585         }).iterator->value;
586         serviceWorkerRunRequestsForOrigin.ensure(identifier, [&] {
587             return Vector<RunServiceWorkerCallback> { };
588         }).iterator->value.append(WTFMove(callback));
589         return;
590     }
591
592     bool success = runServiceWorker(identifier);
593     callback(success ? contextConnection : nullptr);
594 }
595
596 bool SWServer::runServiceWorker(ServiceWorkerIdentifier identifier)
597 {
598     auto* worker = workerByID(identifier);
599     if (!worker)
600         return false;
601
602     // If the registration for a working has been removed then the request to run
603     // the worker is moot.
604     if (!getRegistration(worker->registrationKey()))
605         return false;
606
607     auto addResult = m_runningOrTerminatingWorkers.add(identifier, *worker);
608     ASSERT_UNUSED(addResult, addResult.isNewEntry || worker->isTerminating());
609
610     worker->setState(SWServerWorker::State::Running);
611
612     auto* contextConnection = worker->contextConnection();
613     ASSERT(contextConnection);
614
615     contextConnection->installServiceWorkerContext(worker->contextData(), m_sessionID, worker->userAgent());
616
617     return true;
618 }
619
620 void SWServer::terminateWorker(SWServerWorker& worker)
621 {
622     terminateWorkerInternal(worker, Asynchronous);
623 }
624
625 void SWServer::syncTerminateWorker(SWServerWorker& worker)
626 {
627     terminateWorkerInternal(worker, Synchronous);
628 }
629
630 void SWServer::terminateWorkerInternal(SWServerWorker& worker, TerminationMode mode)
631 {
632     ASSERT(m_runningOrTerminatingWorkers.get(worker.identifier()) == &worker);
633     ASSERT(worker.isRunning());
634
635     RELEASE_LOG(ServiceWorker, "%p - SWServer::terminateWorkerInternal: Terminating service worker %llu", this, worker.identifier().toUInt64());
636
637     worker.setState(SWServerWorker::State::Terminating);
638
639     auto* contextConnection = worker.contextConnection();
640     ASSERT(contextConnection);
641     if (!contextConnection) {
642         LOG_ERROR("Request to terminate a worker whose context connection does not exist");
643         workerContextTerminated(worker);
644         return;
645     }
646
647     switch (mode) {
648     case Asynchronous:
649         contextConnection->terminateWorker(worker.identifier());
650         break;
651     case Synchronous:
652         contextConnection->syncTerminateWorker(worker.identifier());
653         break;
654     };
655 }
656
657 void SWServer::markAllWorkersForRegistrableDomainAsTerminated(const RegistrableDomain& registrableDomain)
658 {
659     Vector<SWServerWorker*> terminatedWorkers;
660     for (auto& worker : m_runningOrTerminatingWorkers.values()) {
661         if (worker->registrableDomain() == registrableDomain)
662             terminatedWorkers.append(worker.ptr());
663     }
664     for (auto& terminatedWorker : terminatedWorkers)
665         workerContextTerminated(*terminatedWorker);
666 }
667
668 void SWServer::workerContextTerminated(SWServerWorker& worker)
669 {
670     worker.setState(SWServerWorker::State::NotRunning);
671
672     if (auto* jobQueue = m_jobQueues.get(worker.registrationKey()))
673         jobQueue->cancelJobsFromServiceWorker(worker.identifier());
674
675     // At this point if no registrations are referencing the worker then it will be destroyed,
676     // removing itself from the m_workersByID map.
677     auto result = m_runningOrTerminatingWorkers.take(worker.identifier());
678     ASSERT_UNUSED(result, result && result->ptr() == &worker);
679 }
680
681 void SWServer::fireInstallEvent(SWServerWorker& worker)
682 {
683     auto* contextConnection = worker.contextConnection();
684     if (!contextConnection) {
685         LOG_ERROR("Request to fire install event on a worker whose context connection does not exist");
686         return;
687     }
688
689     contextConnection->fireInstallEvent(worker.identifier());
690 }
691
692 void SWServer::fireActivateEvent(SWServerWorker& worker)
693 {
694     auto* contextConnection = worker.contextConnection();
695     if (!contextConnection) {
696         LOG_ERROR("Request to fire install event on a worker whose context connection does not exist");
697         return;
698     }
699
700     contextConnection->fireActivateEvent(worker.identifier());
701 }
702
703 void SWServer::addConnection(std::unique_ptr<Connection>&& connection)
704 {
705     auto identifier = connection->identifier();
706     ASSERT(!m_connections.contains(identifier));
707     m_connections.add(identifier, WTFMove(connection));
708 }
709
710 void SWServer::removeConnection(SWServerConnectionIdentifier connectionIdentifier)
711 {
712     ASSERT(m_connections.contains(connectionIdentifier));
713     m_connections.remove(connectionIdentifier);
714
715     for (auto& registration : m_registrations.values())
716         registration->unregisterServerConnection(connectionIdentifier);
717
718     for (auto& jobQueue : m_jobQueues.values())
719         jobQueue->cancelJobsFromConnection(connectionIdentifier);
720 }
721
722 SWServerRegistration* SWServer::doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL)
723 {
724     SWServerRegistration* selectedRegistration = nullptr;
725     for (auto& registration : m_registrations.values()) {
726         if (!registration->key().isMatching(topOrigin, clientURL))
727             continue;
728         if (!selectedRegistration || selectedRegistration->key().scopeLength() < registration->key().scopeLength())
729             selectedRegistration = registration.get();
730     }
731
732     return (selectedRegistration && !selectedRegistration->isUninstalling()) ? selectedRegistration : nullptr;
733 }
734
735 SWServerRegistration* SWServer::registrationFromServiceWorkerIdentifier(ServiceWorkerIdentifier identifier)
736 {
737     auto iterator = m_runningOrTerminatingWorkers.find(identifier);
738     if (iterator == m_runningOrTerminatingWorkers.end())
739         return nullptr;
740
741     return m_registrations.get(iterator->value->registrationKey());
742 }
743
744 void SWServer::registerServiceWorkerClient(ClientOrigin&& clientOrigin, ServiceWorkerClientData&& data, const Optional<ServiceWorkerRegistrationIdentifier>& controllingServiceWorkerRegistrationIdentifier, String&& userAgent)
745 {
746     auto clientIdentifier = data.identifier;
747
748     ASSERT(!m_clientsById.contains(clientIdentifier));
749     m_clientsById.add(clientIdentifier, WTFMove(data));
750
751     auto& clientIdentifiersForOrigin = m_clientIdentifiersPerOrigin.ensure(clientOrigin, [] {
752         return Clients { };
753     }).iterator->value;
754
755     ASSERT(!clientIdentifiersForOrigin.identifiers.contains(clientIdentifier));
756     clientIdentifiersForOrigin.identifiers.append(clientIdentifier);
757
758     if (!clientIdentifiersForOrigin.userAgent.isNull() && clientIdentifiersForOrigin.userAgent != userAgent)
759         RELEASE_LOG_ERROR(ServiceWorker, "%p - SWServer::registerServiceWorkerClient: Service worker has clients using different user agents", this);
760     clientIdentifiersForOrigin.userAgent = WTFMove(userAgent);
761
762     clientIdentifiersForOrigin.terminateServiceWorkersTimer = nullptr;
763
764     m_clientsByRegistrableDomain.ensure(clientOrigin.clientRegistrableDomain(), [] {
765         return HashSet<ServiceWorkerClientIdentifier> { };
766     }).iterator->value.add(clientIdentifier);
767
768     if (!controllingServiceWorkerRegistrationIdentifier)
769         return;
770
771     auto* controllingRegistration = m_registrationsByID.get(*controllingServiceWorkerRegistrationIdentifier);
772     if (!controllingRegistration || !controllingRegistration->activeWorker())
773         return;
774
775     controllingRegistration->addClientUsingRegistration(clientIdentifier);
776     ASSERT(!m_clientToControllingRegistration.contains(clientIdentifier));
777     m_clientToControllingRegistration.add(clientIdentifier, *controllingServiceWorkerRegistrationIdentifier);
778 }
779
780 void SWServer::unregisterServiceWorkerClient(const ClientOrigin& clientOrigin, ServiceWorkerClientIdentifier clientIdentifier)
781 {
782     auto clientRegistrableDomain = clientOrigin.clientRegistrableDomain();
783
784     bool wasRemoved = m_clientsById.remove(clientIdentifier);
785     ASSERT_UNUSED(wasRemoved, wasRemoved);
786
787     auto iterator = m_clientIdentifiersPerOrigin.find(clientOrigin);
788     ASSERT(iterator != m_clientIdentifiersPerOrigin.end());
789
790     auto& clientIdentifiers = iterator->value.identifiers;
791     clientIdentifiers.removeFirstMatching([&] (const auto& identifier) {
792         return clientIdentifier == identifier;
793     });
794
795     if (clientIdentifiers.isEmpty()) {
796         ASSERT(!iterator->value.terminateServiceWorkersTimer);
797         iterator->value.terminateServiceWorkersTimer = std::make_unique<Timer>([clientOrigin, clientRegistrableDomain, this] {
798             Vector<SWServerWorker*> workersToTerminate;
799             for (auto& worker : m_runningOrTerminatingWorkers.values()) {
800                 if (worker->isRunning() && worker->origin() == clientOrigin)
801                     workersToTerminate.append(worker.ptr());
802             }
803             for (auto* worker : workersToTerminate)
804                 terminateWorker(*worker);
805
806             if (!m_clientsByRegistrableDomain.contains(clientRegistrableDomain)) {
807                 if (auto* connection = SWServerToContextConnection::connectionForRegistrableDomain(clientRegistrableDomain))
808                     connection->connectionMayNoLongerBeNeeded();
809             }
810
811             m_clientIdentifiersPerOrigin.remove(clientOrigin);
812         });
813         iterator->value.terminateServiceWorkersTimer->startOneShot(m_shouldDisableServiceWorkerProcessTerminationDelay ? 0_s : terminationDelay);
814     }
815
816     auto clientsByRegistrableDomainIterator = m_clientsByRegistrableDomain.find(clientRegistrableDomain);
817     ASSERT(clientsByRegistrableDomainIterator != m_clientsByRegistrableDomain.end());
818     auto& clientsForRegistrableDomain = clientsByRegistrableDomainIterator->value;
819     clientsForRegistrableDomain.remove(clientIdentifier);
820     if (clientsForRegistrableDomain.isEmpty())
821         m_clientsByRegistrableDomain.remove(clientsByRegistrableDomainIterator);
822
823     auto registrationIterator = m_clientToControllingRegistration.find(clientIdentifier);
824     if (registrationIterator == m_clientToControllingRegistration.end())
825         return;
826
827     if (auto* registration = m_registrationsByID.get(registrationIterator->value))
828         registration->removeClientUsingRegistration(clientIdentifier);
829
830     m_clientToControllingRegistration.remove(registrationIterator);
831 }
832
833 bool SWServer::needsServerToContextConnectionForRegistrableDomain(const RegistrableDomain& registrableDomain) const
834 {
835     return m_clientsByRegistrableDomain.contains(registrableDomain);
836 }
837
838 void SWServer::resolveRegistrationReadyRequests(SWServerRegistration& registration)
839 {
840     for (auto& connection : m_connections.values())
841         connection->resolveRegistrationReadyRequests(registration);
842 }
843
844 void SWServer::Connection::whenRegistrationReady(uint64_t registrationReadyRequestIdentifier, const SecurityOriginData& topOrigin, const URL& clientURL)
845 {
846     if (auto* registration = doRegistrationMatching(topOrigin, clientURL)) {
847         if (registration->activeWorker()) {
848             registrationReady(registrationReadyRequestIdentifier, registration->data());
849             return;
850         }
851     }
852     m_registrationReadyRequests.append({ topOrigin, clientURL, registrationReadyRequestIdentifier });
853 }
854
855 void SWServer::Connection::resolveRegistrationReadyRequests(SWServerRegistration& registration)
856 {
857     m_registrationReadyRequests.removeAllMatching([&](auto& request) {
858         if (!registration.key().isMatching(request.topOrigin, request.clientURL))
859             return false;
860
861         this->registrationReady(request.identifier, registration.data());
862         return true;
863     });
864 }
865
866 void SWServer::getOriginsWithRegistrations(Function<void(const HashSet<SecurityOriginData>&)>&& callback)
867 {
868     m_getOriginsWithRegistrationsCallbacks.append(WTFMove(callback));
869
870     if (m_importCompleted)
871         performGetOriginsWithRegistrationsCallbacks();
872 }
873
874 void SWServer::performGetOriginsWithRegistrationsCallbacks()
875 {
876     ASSERT(isMainThread());
877     ASSERT(m_importCompleted);
878
879     if (m_getOriginsWithRegistrationsCallbacks.isEmpty())
880         return;
881
882     HashSet<SecurityOriginData> originsWithRegistrations;
883     for (auto& key : m_registrations.keys()) {
884         originsWithRegistrations.add(key.topOrigin());
885         originsWithRegistrations.add(SecurityOriginData { key.scope().protocol().toString(), key.scope().host().toString(), key.scope().port() });
886     }
887
888     auto callbacks = WTFMove(m_getOriginsWithRegistrationsCallbacks);
889     for (auto& callback : callbacks)
890         callback(originsWithRegistrations);
891 }
892
893 } // namespace WebCore
894
895 #endif // ENABLE(SERVICE_WORKER)