blob: 395df046d46a5d5bd85667ef0d7b2cd4493358a0 [file] [log] [blame]
youenn.fablet@crf.canon.frc28be402016-02-01 11:05:39 +00001/*
2 * Copyright (C) 2016 Canon Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted, provided that the following conditions
6 * are required to be met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Canon Inc. nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY CANON INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL CANON INC. AND ITS CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
darin@apple.comb3202b32016-05-02 00:17:55 +000029#pragma once
youenn.fablet@crf.canon.frc28be402016-02-01 11:05:39 +000030
cdumez@apple.com5a21cd72021-11-18 18:27:28 +000031#include "ProcessQualified.h"
cdumez@apple.com9870b2e2017-08-03 17:19:44 +000032#include "ReferrerPolicy.h"
cdumez@apple.com5a21cd72021-11-18 18:27:28 +000033#include "ScriptExecutionContextIdentifier.h"
ysuzuki@apple.com157c6402020-02-12 22:51:19 +000034#include <wtf/Markable.h>
weinig@apple.comb50adaa2017-05-09 22:53:13 +000035#include <wtf/text/WTFString.h>
36
youenn.fablet@crf.canon.frc28be402016-02-01 11:05:39 +000037namespace WebCore {
38
darin@apple.comb3202b32016-05-02 00:17:55 +000039struct FetchOptions {
cdumez@apple.com8d990a22021-09-16 21:58:32 +000040 enum class Destination : uint8_t { EmptyString, Audio, Audioworklet, Document, Embed, Font, Image, Iframe, Manifest, Model, Object, Paintworklet, Report, Script, Serviceworker, Sharedworker, Style, Track, Video, Worker, Xslt };
simon.fraser@apple.com90e96252018-07-09 23:54:18 +000041 enum class Mode : uint8_t { Navigate, SameOrigin, NoCors, Cors };
42 enum class Credentials : uint8_t { Omit, SameOrigin, Include };
43 enum class Cache : uint8_t { Default, NoStore, Reload, NoCache, ForceCache, OnlyIfCached };
44 enum class Redirect : uint8_t { Follow, Error, Manual };
commit-queue@webkit.org1e15ad62017-08-16 22:09:34 +000045
46 FetchOptions() = default;
youenn@apple.com3c0a3e22022-04-14 08:48:21 +000047 FetchOptions(Destination, Mode, Credentials, Cache, Redirect, ReferrerPolicy, String&&, bool, std::optional<ScriptExecutionContextIdentifier>);
48 FetchOptions isolatedCopy() const & { return { destination, mode, credentials, cache, redirect, referrerPolicy, integrity.isolatedCopy(), keepAlive, clientIdentifier }; }
49 FetchOptions isolatedCopy() && { return { destination, mode, credentials, cache, redirect, referrerPolicy, WTFMove(integrity).isolatedCopy(), keepAlive, clientIdentifier }; }
commit-queue@webkit.org1e15ad62017-08-16 22:09:34 +000050
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +000051 template<class Encoder> void encodePersistent(Encoder&) const;
ddkilzer@apple.comcca5c3d2020-04-14 17:03:25 +000052 template<class Decoder> static WARN_UNUSED_RETURN bool decodePersistent(Decoder&, FetchOptions&);
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +000053 template<class Encoder> void encode(Encoder&) const;
darin@apple.coma4ddc782021-05-30 16:11:40 +000054 template<class Decoder> static std::optional<FetchOptions> decode(Decoder&);
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +000055
commit-queue@webkit.org1e15ad62017-08-16 22:09:34 +000056 Destination destination { Destination::EmptyString };
57 Mode mode { Mode::NoCors };
58 Credentials credentials { Credentials::Omit };
59 Cache cache { Cache::Default };
darin@apple.com53438e92016-05-03 05:47:34 +000060 Redirect redirect { Redirect::Follow };
darin@apple.comb3202b32016-05-02 00:17:55 +000061 ReferrerPolicy referrerPolicy { ReferrerPolicy::EmptyString };
simon.fraser@apple.com90e96252018-07-09 23:54:18 +000062 String integrity;
youenn@apple.com3c0a3e22022-04-14 08:48:21 +000063 bool keepAlive { false };
cdumez@apple.com5a21cd72021-11-18 18:27:28 +000064 std::optional<ScriptExecutionContextIdentifier> clientIdentifier;
youenn.fablet@crf.canon.frc28be402016-02-01 11:05:39 +000065};
66
youenn@apple.com3c0a3e22022-04-14 08:48:21 +000067inline FetchOptions::FetchOptions(Destination destination, Mode mode, Credentials credentials, Cache cache, Redirect redirect, ReferrerPolicy referrerPolicy, String&& integrity, bool keepAlive, std::optional<ScriptExecutionContextIdentifier> clientIdentifier)
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +000068 : destination(destination)
commit-queue@webkit.org1e15ad62017-08-16 22:09:34 +000069 , mode(mode)
70 , credentials(credentials)
71 , cache(cache)
72 , redirect(redirect)
73 , referrerPolicy(referrerPolicy)
simon.fraser@apple.com90e96252018-07-09 23:54:18 +000074 , integrity(WTFMove(integrity))
youenn@apple.com3c0a3e22022-04-14 08:48:21 +000075 , keepAlive(keepAlive)
76 , clientIdentifier(clientIdentifier)
commit-queue@webkit.org1e15ad62017-08-16 22:09:34 +000077{
78}
79
cdumez@apple.com6bfc41d2017-10-19 22:44:57 +000080inline bool isPotentialNavigationOrSubresourceRequest(FetchOptions::Destination destination)
81{
82 return destination == FetchOptions::Destination::Object
83 || destination == FetchOptions::Destination::Embed;
84}
85
youenn@apple.com59c7bc92021-11-30 10:48:04 +000086// https://fetch.spec.whatwg.org/#navigation-request
87inline bool isNavigationRequest(FetchOptions::Destination destination)
88{
89 return destination == FetchOptions::Destination::Document
90 || destination == FetchOptions::Destination::Iframe
91 || destination == FetchOptions::Destination::Object
92 || destination == FetchOptions::Destination::Embed;
93}
94
cdumez@apple.com8d990a22021-09-16 21:58:32 +000095// https://fetch.spec.whatwg.org/#non-subresource-request
cdumez@apple.com6bfc41d2017-10-19 22:44:57 +000096inline bool isNonSubresourceRequest(FetchOptions::Destination destination)
97{
98 return destination == FetchOptions::Destination::Document
cdumez@apple.com8d990a22021-09-16 21:58:32 +000099 || destination == FetchOptions::Destination::Iframe
cdumez@apple.com6bfc41d2017-10-19 22:44:57 +0000100 || destination == FetchOptions::Destination::Report
101 || destination == FetchOptions::Destination::Serviceworker
102 || destination == FetchOptions::Destination::Sharedworker
103 || destination == FetchOptions::Destination::Worker;
104}
105
dbates@webkit.org85ea93d2018-04-12 22:32:40 +0000106inline bool isScriptLikeDestination(FetchOptions::Destination destination)
107{
youenn@apple.com3a5896c2020-10-22 18:06:44 +0000108 return destination == FetchOptions::Destination::Audioworklet
109 || destination == FetchOptions::Destination::Paintworklet
110 || destination == FetchOptions::Destination::Script
dbates@webkit.org85ea93d2018-04-12 22:32:40 +0000111 || destination == FetchOptions::Destination::Serviceworker
cdumez@apple.com8fe27572022-02-11 22:57:33 +0000112 || destination == FetchOptions::Destination::Sharedworker
dbates@webkit.org85ea93d2018-04-12 22:32:40 +0000113 || destination == FetchOptions::Destination::Worker;
114}
115
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000116}
117
118namespace WTF {
119
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000120template<> struct EnumTraits<WebCore::FetchOptions::Destination> {
121 using values = EnumValues<
122 WebCore::FetchOptions::Destination,
123 WebCore::FetchOptions::Destination::EmptyString,
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +0000124 WebCore::FetchOptions::Destination::Audio,
youenn@apple.com3a5896c2020-10-22 18:06:44 +0000125 WebCore::FetchOptions::Destination::Audioworklet,
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000126 WebCore::FetchOptions::Destination::Document,
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +0000127 WebCore::FetchOptions::Destination::Embed,
128 WebCore::FetchOptions::Destination::Font,
129 WebCore::FetchOptions::Destination::Image,
cdumez@apple.com8d990a22021-09-16 21:58:32 +0000130 WebCore::FetchOptions::Destination::Iframe,
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +0000131 WebCore::FetchOptions::Destination::Manifest,
graouts@webkit.org57f79042021-02-03 15:08:38 +0000132 WebCore::FetchOptions::Destination::Model,
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +0000133 WebCore::FetchOptions::Destination::Object,
youenn@apple.com3a5896c2020-10-22 18:06:44 +0000134 WebCore::FetchOptions::Destination::Paintworklet,
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +0000135 WebCore::FetchOptions::Destination::Report,
136 WebCore::FetchOptions::Destination::Script,
137 WebCore::FetchOptions::Destination::Serviceworker,
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000138 WebCore::FetchOptions::Destination::Sharedworker,
commit-queue@webkit.orgaa0494c2017-10-16 21:57:27 +0000139 WebCore::FetchOptions::Destination::Style,
140 WebCore::FetchOptions::Destination::Track,
141 WebCore::FetchOptions::Destination::Video,
142 WebCore::FetchOptions::Destination::Worker,
143 WebCore::FetchOptions::Destination::Xslt
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000144 >;
145};
146
147template<> struct EnumTraits<WebCore::FetchOptions::Mode> {
148 using values = EnumValues<
149 WebCore::FetchOptions::Mode,
150 WebCore::FetchOptions::Mode::Navigate,
151 WebCore::FetchOptions::Mode::SameOrigin,
152 WebCore::FetchOptions::Mode::NoCors,
153 WebCore::FetchOptions::Mode::Cors
154 >;
155};
156
157template<> struct EnumTraits<WebCore::FetchOptions::Credentials> {
158 using values = EnumValues<
159 WebCore::FetchOptions::Credentials,
160 WebCore::FetchOptions::Credentials::Omit,
161 WebCore::FetchOptions::Credentials::SameOrigin,
162 WebCore::FetchOptions::Credentials::Include
163 >;
164};
165
166template<> struct EnumTraits<WebCore::FetchOptions::Cache> {
167 using values = EnumValues<
168 WebCore::FetchOptions::Cache,
169 WebCore::FetchOptions::Cache::Default,
170 WebCore::FetchOptions::Cache::NoStore,
171 WebCore::FetchOptions::Cache::Reload,
172 WebCore::FetchOptions::Cache::NoCache,
173 WebCore::FetchOptions::Cache::ForceCache,
174 WebCore::FetchOptions::Cache::OnlyIfCached
175 >;
176};
177
178template<> struct EnumTraits<WebCore::FetchOptions::Redirect> {
179 using values = EnumValues<
180 WebCore::FetchOptions::Redirect,
181 WebCore::FetchOptions::Redirect::Follow,
182 WebCore::FetchOptions::Redirect::Error,
183 WebCore::FetchOptions::Redirect::Manual
184 >;
185};
186
187}
188
189namespace WebCore {
190
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000191template<class Encoder>
192inline void FetchOptions::encodePersistent(Encoder& encoder) const
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000193{
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000194 // Changes to encoding here should bump NetworkCache Storage format version.
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000195 encoder << destination;
196 encoder << mode;
197 encoder << credentials;
198 encoder << cache;
199 encoder << redirect;
200 encoder << referrerPolicy;
201 encoder << integrity;
202 encoder << keepAlive;
203}
204
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000205template<class Decoder>
ddkilzer@apple.comcca5c3d2020-04-14 17:03:25 +0000206inline bool FetchOptions::decodePersistent(Decoder& decoder, FetchOptions& options)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000207{
darin@apple.coma4ddc782021-05-30 16:11:40 +0000208 std::optional<FetchOptions::Destination> destination;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000209 decoder >> destination;
210 if (!destination)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000211 return false;
212
darin@apple.coma4ddc782021-05-30 16:11:40 +0000213 std::optional<FetchOptions::Mode> mode;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000214 decoder >> mode;
215 if (!mode)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000216 return false;
217
darin@apple.coma4ddc782021-05-30 16:11:40 +0000218 std::optional<FetchOptions::Credentials> credentials;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000219 decoder >> credentials;
220 if (!credentials)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000221 return false;
222
darin@apple.coma4ddc782021-05-30 16:11:40 +0000223 std::optional<FetchOptions::Cache> cache;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000224 decoder >> cache;
225 if (!cache)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000226 return false;
227
darin@apple.coma4ddc782021-05-30 16:11:40 +0000228 std::optional<FetchOptions::Redirect> redirect;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000229 decoder >> redirect;
230 if (!redirect)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000231 return false;
232
darin@apple.coma4ddc782021-05-30 16:11:40 +0000233 std::optional<ReferrerPolicy> referrerPolicy;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000234 decoder >> referrerPolicy;
235 if (!referrerPolicy)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000236 return false;
237
darin@apple.coma4ddc782021-05-30 16:11:40 +0000238 std::optional<String> integrity;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000239 decoder >> integrity;
240 if (!integrity)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000241 return false;
242
darin@apple.coma4ddc782021-05-30 16:11:40 +0000243 std::optional<bool> keepAlive;
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000244 decoder >> keepAlive;
245 if (!keepAlive)
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000246 return false;
247
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000248 options.destination = *destination;
249 options.mode = *mode;
250 options.credentials = *credentials;
251 options.cache = *cache;
252 options.redirect = *redirect;
253 options.referrerPolicy = *referrerPolicy;
254 options.integrity = WTFMove(*integrity);
255 options.keepAlive = *keepAlive;
commit-queue@webkit.orga480fc02017-08-23 17:54:03 +0000256
257 return true;
258}
259
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000260template<class Encoder>
261inline void FetchOptions::encode(Encoder& encoder) const
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000262{
263 encodePersistent(encoder);
cdumez@apple.com5a21cd72021-11-18 18:27:28 +0000264 encoder << clientIdentifier;
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000265}
266
commit-queue@webkit.org26bdece2020-04-11 06:39:36 +0000267template<class Decoder>
darin@apple.coma4ddc782021-05-30 16:11:40 +0000268inline std::optional<FetchOptions> FetchOptions::decode(Decoder& decoder)
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000269{
270 FetchOptions options;
271 if (!decodePersistent(decoder, options))
darin@apple.com7c840b62021-05-28 01:26:23 +0000272 return std::nullopt;
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000273
cdumez@apple.com5a21cd72021-11-18 18:27:28 +0000274 std::optional<std::optional<ScriptExecutionContextIdentifier>> clientIdentifier;
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000275 decoder >> clientIdentifier;
276 if (!clientIdentifier)
darin@apple.com7c840b62021-05-28 01:26:23 +0000277 return std::nullopt;
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000278 options.clientIdentifier = WTFMove(clientIdentifier.value());
279
mcatanzaro@igalia.coma7ade272019-03-19 20:04:34 +0000280 return options;
commit-queue@webkit.org27605fe2017-11-29 21:47:37 +0000281}
282
youenn.fablet@crf.canon.frc28be402016-02-01 11:05:39 +0000283} // namespace WebCore