SWContextManager.cpp [plain text]
#include "config.h"
#include "SWContextManager.h"
#if ENABLE(SERVICE_WORKER)
#include "FrameLoaderClient.h"
#include "Logging.h"
#include "MessageWithMessagePorts.h"
#include "ServiceWorkerClientIdentifier.h"
#include "ServiceWorkerGlobalScope.h"
namespace WebCore {
SWContextManager& SWContextManager::singleton()
{
static SWContextManager* sharedManager = new SWContextManager;
return *sharedManager;
}
void SWContextManager::setConnection(std::unique_ptr<Connection>&& connection)
{
ASSERT(!m_connection || m_connection->isClosed());
m_connection = WTFMove(connection);
}
auto SWContextManager::connection() const -> Connection*
{
return m_connection.get();
}
void SWContextManager::registerServiceWorkerThreadForInstall(Ref<ServiceWorkerThreadProxy>&& serviceWorkerThreadProxy)
{
auto serviceWorkerIdentifier = serviceWorkerThreadProxy->identifier();
auto jobDataIdentifier = serviceWorkerThreadProxy->thread().contextData().jobDataIdentifier;
auto* threadProxy = serviceWorkerThreadProxy.ptr();
auto result = m_workerMap.add(serviceWorkerIdentifier, WTFMove(serviceWorkerThreadProxy));
ASSERT_UNUSED(result, result.isNewEntry);
threadProxy->thread().start([jobDataIdentifier, serviceWorkerIdentifier](const String& exceptionMessage, bool doesHandleFetch) {
SWContextManager::singleton().startedServiceWorker(jobDataIdentifier, serviceWorkerIdentifier, exceptionMessage, doesHandleFetch);
});
}
void SWContextManager::startedServiceWorker(Optional<ServiceWorkerJobDataIdentifier> jobDataIdentifier, ServiceWorkerIdentifier serviceWorkerIdentifier, const String& exceptionMessage, bool doesHandleFetch)
{
if (m_serviceWorkerCreationCallback)
m_serviceWorkerCreationCallback(serviceWorkerIdentifier.toUInt64());
if (!exceptionMessage.isEmpty()) {
connection()->serviceWorkerFailedToStart(jobDataIdentifier, serviceWorkerIdentifier, exceptionMessage);
return;
}
connection()->serviceWorkerStarted(jobDataIdentifier, serviceWorkerIdentifier, doesHandleFetch);
}
ServiceWorkerThreadProxy* SWContextManager::serviceWorkerThreadProxy(ServiceWorkerIdentifier identifier) const
{
return m_workerMap.get(identifier);
}
void SWContextManager::postMessageToServiceWorker(ServiceWorkerIdentifier destination, MessageWithMessagePorts&& message, ServiceWorkerOrClientData&& sourceData)
{
auto* serviceWorker = m_workerMap.get(destination);
ASSERT(serviceWorker);
ASSERT(!serviceWorker->isTerminatingOrTerminated());
serviceWorker->postMessageToServiceWorker(WTFMove(message), WTFMove(sourceData));
}
void SWContextManager::fireInstallEvent(ServiceWorkerIdentifier identifier)
{
auto* serviceWorker = m_workerMap.get(identifier);
if (!serviceWorker)
return;
serviceWorker->fireInstallEvent();
}
void SWContextManager::fireActivateEvent(ServiceWorkerIdentifier identifier)
{
auto* serviceWorker = m_workerMap.get(identifier);
if (!serviceWorker)
return;
serviceWorker->fireActivateEvent();
}
void SWContextManager::terminateWorker(ServiceWorkerIdentifier identifier, Seconds timeout, Function<void()>&& completionHandler)
{
auto serviceWorker = m_workerMap.take(identifier);
if (!serviceWorker) {
if (completionHandler)
completionHandler();
return;
}
stopWorker(*serviceWorker, timeout, WTFMove(completionHandler));
}
void SWContextManager::stopWorker(ServiceWorkerThreadProxy& serviceWorker, Seconds timeout, Function<void()>&& completionHandler)
{
auto identifier = serviceWorker.identifier();
serviceWorker.setAsTerminatingOrTerminated();
m_pendingServiceWorkerTerminationRequests.add(identifier, makeUnique<ServiceWorkerTerminationRequest>(*this, identifier, timeout));
auto& thread = serviceWorker.thread();
thread.stop([this, identifier, serviceWorker = makeRef(serviceWorker), completionHandler = WTFMove(completionHandler)]() mutable {
m_pendingServiceWorkerTerminationRequests.remove(identifier);
if (auto* connection = SWContextManager::singleton().connection())
connection->workerTerminated(identifier);
if (completionHandler)
completionHandler();
callOnMainThread([serviceWorker = WTFMove(serviceWorker)] { });
});
}
void SWContextManager::forEachServiceWorkerThread(const WTF::Function<void(ServiceWorkerThreadProxy&)>& apply)
{
for (auto& workerThread : m_workerMap.values())
apply(workerThread);
}
bool SWContextManager::postTaskToServiceWorker(ServiceWorkerIdentifier identifier, WTF::Function<void(ServiceWorkerGlobalScope&)>&& task)
{
auto* serviceWorker = m_workerMap.get(identifier);
if (!serviceWorker)
return false;
serviceWorker->thread().runLoop().postTask([task = WTFMove(task)] (auto& context) {
task(downcast<ServiceWorkerGlobalScope>(context));
});
return true;
}
void SWContextManager::serviceWorkerFailedToTerminate(ServiceWorkerIdentifier serviceWorkerIdentifier)
{
UNUSED_PARAM(serviceWorkerIdentifier);
RELEASE_LOG_ERROR(ServiceWorker, "Failed to terminate service worker with identifier %s, killing the service worker process", serviceWorkerIdentifier.loggingString().utf8().data());
ASSERT_NOT_REACHED();
_exit(EXIT_FAILURE);
}
SWContextManager::ServiceWorkerTerminationRequest::ServiceWorkerTerminationRequest(SWContextManager& manager, ServiceWorkerIdentifier serviceWorkerIdentifier, Seconds timeout)
: m_timeoutTimer([&manager, serviceWorkerIdentifier] { manager.serviceWorkerFailedToTerminate(serviceWorkerIdentifier); })
{
m_timeoutTimer.startOneShot(timeout);
}
void SWContextManager::stopAllServiceWorkers()
{
auto serviceWorkers = WTFMove(m_workerMap);
for (auto& serviceWorker : serviceWorkers.values())
stopWorker(serviceWorker, workerTerminationTimeout, [] { });
}
}
#endif