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#pragma once
27
28#include "ContentSecurityPolicyResponseHeaders.h"
29#include "ServiceWorkerIdentifier.h"
30#include "ServiceWorkerJobDataIdentifier.h"
31#include "ServiceWorkerRegistrationData.h"
32#include "WorkerType.h"
33#include <pal/SessionID.h>
34#include <wtf/HashMap.h>
35#include <wtf/URL.h>
36#include <wtf/URLHash.h>
37
38#if ENABLE(SERVICE_WORKER)
39
40namespace WebCore {
41
42struct ServiceWorkerContextData {
43
44 struct ImportedScript {
45 String script;
46 URL responseURL;
47 String mimeType;
48
49 ImportedScript isolatedCopy() const { return { script.isolatedCopy(), responseURL.isolatedCopy(), mimeType.isolatedCopy() }; }
50
51 template<class Encoder> void encode(Encoder& encoder) const
52 {
53 encoder << script << responseURL << mimeType;
54 }
55
56 template<class Decoder> static bool decode(Decoder& decoder, ImportedScript& script)
57 {
58 ImportedScript importedScript;
59 if (!decoder.decode(importedScript.script))
60 return false;
61
62 if (!decoder.decode(importedScript.responseURL))
63 return false;
64
65 if (!decoder.decode(importedScript.mimeType))
66 return false;
67
68 script = WTFMove(importedScript);
69 return true;
70 }
71 };
72
73 Optional<ServiceWorkerJobDataIdentifier> jobDataIdentifier;
74 ServiceWorkerRegistrationData registration;
75 ServiceWorkerIdentifier serviceWorkerIdentifier;
76 String script;
77 ContentSecurityPolicyResponseHeaders contentSecurityPolicy;
78 String referrerPolicy;
79 URL scriptURL;
80 WorkerType workerType;
81 PAL::SessionID sessionID;
82 bool loadedFromDisk;
83 HashMap<URL, ImportedScript> scriptResourceMap;
84
85 template<class Encoder> void encode(Encoder&) const;
86 template<class Decoder> static Optional<ServiceWorkerContextData> decode(Decoder&);
87
88 ServiceWorkerContextData isolatedCopy() const;
89};
90
91template<class Encoder>
92void ServiceWorkerContextData::encode(Encoder& encoder) const
93{
94 encoder << jobDataIdentifier << registration << serviceWorkerIdentifier << script << contentSecurityPolicy << referrerPolicy << scriptURL << workerType << sessionID << loadedFromDisk;
95 encoder << scriptResourceMap;
96}
97
98template<class Decoder>
99Optional<ServiceWorkerContextData> ServiceWorkerContextData::decode(Decoder& decoder)
100{
101 Optional<Optional<ServiceWorkerJobDataIdentifier>> jobDataIdentifier;
102 decoder >> jobDataIdentifier;
103 if (!jobDataIdentifier)
104 return WTF::nullopt;
105
106 Optional<ServiceWorkerRegistrationData> registration;
107 decoder >> registration;
108 if (!registration)
109 return WTF::nullopt;
110
111 auto serviceWorkerIdentifier = ServiceWorkerIdentifier::decode(decoder);
112 if (!serviceWorkerIdentifier)
113 return WTF::nullopt;
114
115 String script;
116 if (!decoder.decode(script))
117 return WTF::nullopt;
118
119 ContentSecurityPolicyResponseHeaders contentSecurityPolicy;
120 if (!decoder.decode(contentSecurityPolicy))
121 return WTF::nullopt;
122
123 String referrerPolicy;
124 if (!decoder.decode(referrerPolicy))
125 return WTF::nullopt;
126
127 URL scriptURL;
128 if (!decoder.decode(scriptURL))
129 return WTF::nullopt;
130
131 WorkerType workerType;
132 if (!decoder.decodeEnum(workerType))
133 return WTF::nullopt;
134
135 PAL::SessionID sessionID;
136 if (!decoder.decode(sessionID))
137 return WTF::nullopt;
138
139 bool loadedFromDisk;
140 if (!decoder.decode(loadedFromDisk))
141 return WTF::nullopt;
142
143 HashMap<URL, ImportedScript> scriptResourceMap;
144 if (!decoder.decode(scriptResourceMap))
145 return WTF::nullopt;
146
147 return {{ WTFMove(*jobDataIdentifier), WTFMove(*registration), WTFMove(*serviceWorkerIdentifier), WTFMove(script), WTFMove(contentSecurityPolicy), WTFMove(referrerPolicy), WTFMove(scriptURL), workerType, sessionID, loadedFromDisk, WTFMove(scriptResourceMap) }};
148}
149
150} // namespace WebCore
151
152#endif // ENABLE(SERVICE_WORKER)
153