ServiceWorkerThreadProxy.cpp [plain text]
#include "config.h"
#include "ServiceWorkerThreadProxy.h"
#if ENABLE(SERVICE_WORKER)
#include "CacheStorageProvider.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "LoaderStrategy.h"
#include "PlatformStrategies.h"
#include "Settings.h"
#include <pal/SessionID.h>
#include <wtf/MainThread.h>
#include <wtf/RunLoop.h>
namespace WebCore {
URL static inline topOriginURL(const SecurityOrigin& origin)
{
URL url;
url.setProtocol(origin.protocol());
url.setHost(origin.host());
if (origin.port())
url.setPort(*origin.port());
return url;
}
static inline UniqueRef<Page> createPageForServiceWorker(PageConfiguration&& configuration, const ServiceWorkerContextData& data, SecurityOrigin::StorageBlockingPolicy storageBlockingPolicy, PAL::SessionID sessionID)
{
auto page = makeUniqueRef<Page>(WTFMove(configuration));
page->setSessionID(sessionID);
auto& mainFrame = page->mainFrame();
mainFrame.loader().initForSynthesizedDocument({ });
auto document = Document::createNonRenderedPlaceholder(&mainFrame, data.scriptURL);
document->createDOMWindow();
document->mutableSettings().setStorageBlockingPolicy(storageBlockingPolicy);
document->storageBlockingStateDidChange();
auto origin = data.registration.key.topOrigin().securityOrigin();
origin->setStorageBlockingPolicy(storageBlockingPolicy);
document->setFirstPartyForSameSiteCookies(topOriginURL(origin));
document->setFirstPartyForCookies(data.scriptURL);
document->setDomainForCachePartition(origin->domainForCachePartition());
mainFrame.setDocument(WTFMove(document));
return page;
}
static inline IDBClient::IDBConnectionProxy* idbConnectionProxy(Document& document)
{
#if ENABLE(INDEXED_DATABASE)
return document.idbConnectionProxy();
#else
return nullptr;
#endif
}
static HashSet<ServiceWorkerThreadProxy*>& allServiceWorkerThreadProxies()
{
static NeverDestroyed<HashSet<ServiceWorkerThreadProxy*>> set;
return set;
}
ServiceWorkerThreadProxy::ServiceWorkerThreadProxy(PageConfiguration&& pageConfiguration, const ServiceWorkerContextData& data, PAL::SessionID sessionID, String&& userAgent, CacheStorageProvider& cacheStorageProvider, SecurityOrigin::StorageBlockingPolicy storageBlockingPolicy)
: m_page(createPageForServiceWorker(WTFMove(pageConfiguration), data, storageBlockingPolicy, data.sessionID))
, m_document(*m_page->mainFrame().document())
, m_serviceWorkerThread(ServiceWorkerThread::create(data, sessionID, WTFMove(userAgent), *this, *this, idbConnectionProxy(m_document), m_document->socketProvider()))
, m_cacheStorageProvider(cacheStorageProvider)
, m_sessionID(sessionID)
, m_inspectorProxy(*this)
{
static bool addedListener;
if (!addedListener) {
platformStrategies()->loaderStrategy()->addOnlineStateChangeListener(&networkStateChanged);
addedListener = true;
}
ASSERT(!allServiceWorkerThreadProxies().contains(this));
allServiceWorkerThreadProxies().add(this);
#if ENABLE(REMOTE_INSPECTOR)
m_remoteDebuggable = std::make_unique<ServiceWorkerDebuggable>(*this, data);
m_remoteDebuggable->setRemoteDebuggingAllowed(true);
m_remoteDebuggable->init();
#endif
}
ServiceWorkerThreadProxy::~ServiceWorkerThreadProxy()
{
ASSERT(allServiceWorkerThreadProxies().contains(this));
allServiceWorkerThreadProxies().remove(this);
}
bool ServiceWorkerThreadProxy::postTaskForModeToWorkerGlobalScope(ScriptExecutionContext::Task&& task, const String& mode)
{
if (m_isTerminatingOrTerminated)
return false;
m_serviceWorkerThread->runLoop().postTaskForMode(WTFMove(task), mode);
return true;
}
void ServiceWorkerThreadProxy::postTaskToLoader(ScriptExecutionContext::Task&& task)
{
callOnMainThread([task = WTFMove(task), this, protectedThis = makeRef(*this)] () mutable {
task.performTask(m_document.get());
});
}
void ServiceWorkerThreadProxy::postMessageToDebugger(const String& message)
{
RunLoop::main().dispatch([this, protectedThis = makeRef(*this), message = message.isolatedCopy()] {
m_inspectorProxy.sendMessageFromWorkerToFrontend(message);
});
}
void ServiceWorkerThreadProxy::setResourceCachingDisabled(bool disabled)
{
postTaskToLoader([this, protectedThis = makeRef(*this), disabled] (ScriptExecutionContext&) {
ASSERT(isMainThread());
m_page->setResourceCachingDisabled(disabled);
});
}
Ref<CacheStorageConnection> ServiceWorkerThreadProxy::createCacheStorageConnection()
{
ASSERT(isMainThread());
if (!m_cacheStorageConnection)
m_cacheStorageConnection = m_cacheStorageProvider.createCacheStorageConnection(m_sessionID);
return *m_cacheStorageConnection;
}
std::unique_ptr<FetchLoader> ServiceWorkerThreadProxy::createBlobLoader(FetchLoaderClient& client, const URL& blobURL)
{
auto loader = std::make_unique<FetchLoader>(client, nullptr);
loader->startLoadingBlobURL(m_document, blobURL);
if (!loader->isStarted())
return nullptr;
return loader;
}
void ServiceWorkerThreadProxy::networkStateChanged(bool isOnLine)
{
for (auto* proxy : allServiceWorkerThreadProxies())
proxy->notifyNetworkStateChange(isOnLine);
}
void ServiceWorkerThreadProxy::notifyNetworkStateChange(bool isOnline)
{
if (m_isTerminatingOrTerminated)
return;
postTaskForModeToWorkerGlobalScope([isOnline] (ScriptExecutionContext& context) {
auto& globalScope = downcast<WorkerGlobalScope>(context);
globalScope.setIsOnline(isOnline);
globalScope.dispatchEvent(Event::create(isOnline ? eventNames().onlineEvent : eventNames().offlineEvent, false, false));
}, WorkerRunLoop::defaultMode());
}
void ServiceWorkerThreadProxy::startFetch(SWServerConnectionIdentifier connectionIdentifier, FetchIdentifier fetchIdentifier, Ref<ServiceWorkerFetch::Client>&& client, std::optional<ServiceWorkerClientIdentifier>&& clientId, ResourceRequest&& request, String&& referrer, FetchOptions&& options)
{
auto key = std::make_pair(connectionIdentifier, fetchIdentifier);
ASSERT(!m_ongoingFetchTasks.contains(key));
m_ongoingFetchTasks.add(key, client.copyRef());
thread().postFetchTask(WTFMove(client), WTFMove(clientId), WTFMove(request), WTFMove(referrer), WTFMove(options));
}
void ServiceWorkerThreadProxy::cancelFetch(SWServerConnectionIdentifier connectionIdentifier, FetchIdentifier fetchIdentifier)
{
if (auto client = m_ongoingFetchTasks.take(std::make_pair(connectionIdentifier, fetchIdentifier)))
client.value()->cancel();
}
void ServiceWorkerThreadProxy::removeFetch(SWServerConnectionIdentifier connectionIdentifier, FetchIdentifier fetchIdentifier)
{
m_ongoingFetchTasks.remove(std::make_pair(connectionIdentifier, fetchIdentifier));
}
}
#endif // ENABLE(SERVICE_WORKER)