WebServiceWorkerProvider.cpp [plain text]
#include "config.h"
#include "WebServiceWorkerProvider.h"
#if ENABLE(SERVICE_WORKER)
#include "WebProcess.h"
#include "WebSWServerConnection.h"
#include "WebToStorageProcessConnection.h"
#include <WebCore/CachedResource.h>
#include <WebCore/Exception.h>
#include <WebCore/ExceptionCode.h>
#include <WebCore/SchemeRegistry.h>
#include <WebCore/ServiceWorkerJob.h>
#include <pal/SessionID.h>
#include <wtf/text/WTFString.h>
using namespace PAL;
using namespace WebCore;
namespace WebKit {
WebServiceWorkerProvider& WebServiceWorkerProvider::singleton()
{
static NeverDestroyed<WebServiceWorkerProvider> provider;
return provider;
}
WebServiceWorkerProvider::WebServiceWorkerProvider()
{
}
WebCore::SWClientConnection& WebServiceWorkerProvider::serviceWorkerConnectionForSession(SessionID sessionID)
{
ASSERT(sessionID.isValid());
return WebProcess::singleton().ensureWebToStorageProcessConnection(sessionID).serviceWorkerConnectionForSession(sessionID);
}
WebCore::SWClientConnection* WebServiceWorkerProvider::existingServiceWorkerConnectionForSession(SessionID sessionID)
{
ASSERT(sessionID.isValid());
auto* webToStorageProcessConnection = WebProcess::singleton().existingWebToStorageProcessConnection();
if (!webToStorageProcessConnection)
return nullptr;
return webToStorageProcessConnection->existingServiceWorkerConnectionForSession(sessionID);
}
static inline bool shouldHandleFetch(const ResourceLoaderOptions& options)
{
if (options.serviceWorkersMode == ServiceWorkersMode::None)
return false;
if (isPotentialNavigationOrSubresourceRequest(options.destination))
return false;
return !!options.serviceWorkerRegistrationIdentifier;
}
void WebServiceWorkerProvider::handleFetch(ResourceLoader& loader, CachedResource* resource, PAL::SessionID sessionID, bool shouldClearReferrerOnHTTPSToHTTPRedirect, ServiceWorkerClientFetch::Callback&& callback)
{
if (!SchemeRegistry::canServiceWorkersHandleURLScheme(loader.request().url().protocol().toStringWithoutCopying()) || !shouldHandleFetch(loader.options())) {
callback(ServiceWorkerClientFetch::Result::Unhandled);
return;
}
auto& connection = WebProcess::singleton().ensureWebToStorageProcessConnection(sessionID).serviceWorkerConnectionForSession(sessionID);
auto fetchIdentifier = makeObjectIdentifier<FetchIdentifierType>(loader.identifier());
m_ongoingFetchTasks.add(fetchIdentifier, ServiceWorkerClientFetch::create(*this, loader, fetchIdentifier, connection, shouldClearReferrerOnHTTPSToHTTPRedirect, WTFMove(callback)));
}
bool WebServiceWorkerProvider::cancelFetch(FetchIdentifier fetchIdentifier)
{
auto fetch = m_ongoingFetchTasks.take(fetchIdentifier);
if (fetch)
(*fetch)->cancel();
return !!fetch;
}
void WebServiceWorkerProvider::fetchFinished(FetchIdentifier fetchIdentifier)
{
m_ongoingFetchTasks.take(fetchIdentifier);
}
void WebServiceWorkerProvider::didReceiveServiceWorkerClientFetchMessage(IPC::Connection& connection, IPC::Decoder& decoder)
{
if (auto fetch = m_ongoingFetchTasks.get(makeObjectIdentifier<FetchIdentifierType>(decoder.destinationID())))
fetch->didReceiveMessage(connection, decoder);
}
}
#endif // ENABLE(SERVICE_WORKER)