ServiceWorkerContextData.h [plain text]
#pragma once
#include "ContentSecurityPolicyResponseHeaders.h"
#include "ServiceWorkerIdentifier.h"
#include "ServiceWorkerJobDataIdentifier.h"
#include "ServiceWorkerRegistrationData.h"
#include "URL.h"
#include "URLHash.h"
#include "WorkerType.h"
#include <pal/SessionID.h>
#include <wtf/HashMap.h>
#if ENABLE(SERVICE_WORKER)
namespace WebCore {
struct ServiceWorkerContextData {
struct ImportedScript {
String script;
URL responseURL;
String mimeType;
ImportedScript isolatedCopy() const { return { script.isolatedCopy(), responseURL.isolatedCopy(), mimeType.isolatedCopy() }; }
template<class Encoder> void encode(Encoder& encoder) const
{
encoder << script << responseURL << mimeType;
}
template<class Decoder> static bool decode(Decoder& decoder, ImportedScript& script)
{
ImportedScript importedScript;
if (!decoder.decode(importedScript.script))
return false;
if (!decoder.decode(importedScript.responseURL))
return false;
if (!decoder.decode(importedScript.mimeType))
return false;
script = WTFMove(importedScript);
return true;
}
};
std::optional<ServiceWorkerJobDataIdentifier> jobDataIdentifier;
ServiceWorkerRegistrationData registration;
ServiceWorkerIdentifier serviceWorkerIdentifier;
String script;
ContentSecurityPolicyResponseHeaders contentSecurityPolicy;
URL scriptURL;
WorkerType workerType;
PAL::SessionID sessionID;
bool loadedFromDisk;
HashMap<URL, ImportedScript> scriptResourceMap;
template<class Encoder> void encode(Encoder&) const;
template<class Decoder> static std::optional<ServiceWorkerContextData> decode(Decoder&);
ServiceWorkerContextData isolatedCopy() const;
};
template<class Encoder>
void ServiceWorkerContextData::encode(Encoder& encoder) const
{
encoder << jobDataIdentifier << registration << serviceWorkerIdentifier << script << contentSecurityPolicy << scriptURL << workerType << sessionID << loadedFromDisk;
encoder << scriptResourceMap;
}
template<class Decoder>
std::optional<ServiceWorkerContextData> ServiceWorkerContextData::decode(Decoder& decoder)
{
std::optional<std::optional<ServiceWorkerJobDataIdentifier>> jobDataIdentifier;
decoder >> jobDataIdentifier;
if (!jobDataIdentifier)
return std::nullopt;
std::optional<ServiceWorkerRegistrationData> registration;
decoder >> registration;
if (!registration)
return std::nullopt;
auto serviceWorkerIdentifier = ServiceWorkerIdentifier::decode(decoder);
if (!serviceWorkerIdentifier)
return std::nullopt;
String script;
if (!decoder.decode(script))
return std::nullopt;
ContentSecurityPolicyResponseHeaders contentSecurityPolicy;
if (!decoder.decode(contentSecurityPolicy))
return std::nullopt;
URL scriptURL;
if (!decoder.decode(scriptURL))
return std::nullopt;
WorkerType workerType;
if (!decoder.decodeEnum(workerType))
return std::nullopt;
PAL::SessionID sessionID;
if (!decoder.decode(sessionID))
return std::nullopt;
bool loadedFromDisk;
if (!decoder.decode(loadedFromDisk))
return std::nullopt;
HashMap<URL, ImportedScript> scriptResourceMap;
if (!decoder.decode(scriptResourceMap))
return std::nullopt;
return {{ WTFMove(*jobDataIdentifier), WTFMove(*registration), WTFMove(*serviceWorkerIdentifier), WTFMove(script), WTFMove(contentSecurityPolicy), WTFMove(scriptURL), workerType, sessionID, loadedFromDisk, WTFMove(scriptResourceMap) }};
}
}
#endif // ENABLE(SERVICE_WORKER)