1/*
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
29#pragma once
30
31#include "DocumentIdentifier.h"
32#include "ReferrerPolicy.h"
33#include <wtf/text/WTFString.h>
34
35namespace WebCore {
36
37struct FetchOptions {
38 enum class Destination : uint8_t { EmptyString, Audio, Document, Embed, Font, Image, Manifest, Object, Report, Script, Serviceworker, Sharedworker, Style, Track, Video, Worker, Xslt };
39 enum class Mode : uint8_t { Navigate, SameOrigin, NoCors, Cors };
40 enum class Credentials : uint8_t { Omit, SameOrigin, Include };
41 enum class Cache : uint8_t { Default, NoStore, Reload, NoCache, ForceCache, OnlyIfCached };
42 enum class Redirect : uint8_t { Follow, Error, Manual };
43
44 FetchOptions() = default;
45 FetchOptions(Destination, Mode, Credentials, Cache, Redirect, ReferrerPolicy, String&&, bool);
46 FetchOptions isolatedCopy() const { return { destination, mode, credentials, cache, redirect, referrerPolicy, integrity.isolatedCopy(), keepAlive }; }
47
48 template<class Encoder> void encodePersistent(Encoder&) const;
49 template<class Decoder> static bool decodePersistent(Decoder&, FetchOptions&);
50 template<class Encoder> void encode(Encoder&) const;
51 template<class Decoder> static Optional<FetchOptions> decode(Decoder&);
52
53 Destination destination { Destination::EmptyString };
54 Mode mode { Mode::NoCors };
55 Credentials credentials { Credentials::Omit };
56 Cache cache { Cache::Default };
57 Redirect redirect { Redirect::Follow };
58 ReferrerPolicy referrerPolicy { ReferrerPolicy::EmptyString };
59 bool keepAlive { false };
60 String integrity;
61 Optional<DocumentIdentifier> clientIdentifier;
62};
63
64inline FetchOptions::FetchOptions(Destination destination, Mode mode, Credentials credentials, Cache cache, Redirect redirect, ReferrerPolicy referrerPolicy, String&& integrity, bool keepAlive)
65 : destination(destination)
66 , mode(mode)
67 , credentials(credentials)
68 , cache(cache)
69 , redirect(redirect)
70 , referrerPolicy(referrerPolicy)
71 , keepAlive(keepAlive)
72 , integrity(WTFMove(integrity))
73{
74}
75
76inline bool isPotentialNavigationOrSubresourceRequest(FetchOptions::Destination destination)
77{
78 return destination == FetchOptions::Destination::Object
79 || destination == FetchOptions::Destination::Embed;
80}
81
82inline bool isNonSubresourceRequest(FetchOptions::Destination destination)
83{
84 return destination == FetchOptions::Destination::Document
85 || destination == FetchOptions::Destination::Report
86 || destination == FetchOptions::Destination::Serviceworker
87 || destination == FetchOptions::Destination::Sharedworker
88 || destination == FetchOptions::Destination::Worker;
89}
90
91inline bool isScriptLikeDestination(FetchOptions::Destination destination)
92{
93 return destination == FetchOptions::Destination::Script
94 || destination == FetchOptions::Destination::Serviceworker
95 || destination == FetchOptions::Destination::Worker;
96}
97
98}
99
100namespace WTF {
101
102template<> struct EnumTraits<WebCore::FetchOptions::Destination> {
103 using values = EnumValues<
104 WebCore::FetchOptions::Destination,
105 WebCore::FetchOptions::Destination::EmptyString,
106 WebCore::FetchOptions::Destination::Audio,
107 WebCore::FetchOptions::Destination::Document,
108 WebCore::FetchOptions::Destination::Embed,
109 WebCore::FetchOptions::Destination::Font,
110 WebCore::FetchOptions::Destination::Image,
111 WebCore::FetchOptions::Destination::Manifest,
112 WebCore::FetchOptions::Destination::Object,
113 WebCore::FetchOptions::Destination::Report,
114 WebCore::FetchOptions::Destination::Script,
115 WebCore::FetchOptions::Destination::Serviceworker,
116 WebCore::FetchOptions::Destination::Sharedworker,
117 WebCore::FetchOptions::Destination::Style,
118 WebCore::FetchOptions::Destination::Track,
119 WebCore::FetchOptions::Destination::Video,
120 WebCore::FetchOptions::Destination::Worker,
121 WebCore::FetchOptions::Destination::Xslt
122 >;
123};
124
125template<> struct EnumTraits<WebCore::FetchOptions::Mode> {
126 using values = EnumValues<
127 WebCore::FetchOptions::Mode,
128 WebCore::FetchOptions::Mode::Navigate,
129 WebCore::FetchOptions::Mode::SameOrigin,
130 WebCore::FetchOptions::Mode::NoCors,
131 WebCore::FetchOptions::Mode::Cors
132 >;
133};
134
135template<> struct EnumTraits<WebCore::FetchOptions::Credentials> {
136 using values = EnumValues<
137 WebCore::FetchOptions::Credentials,
138 WebCore::FetchOptions::Credentials::Omit,
139 WebCore::FetchOptions::Credentials::SameOrigin,
140 WebCore::FetchOptions::Credentials::Include
141 >;
142};
143
144template<> struct EnumTraits<WebCore::FetchOptions::Cache> {
145 using values = EnumValues<
146 WebCore::FetchOptions::Cache,
147 WebCore::FetchOptions::Cache::Default,
148 WebCore::FetchOptions::Cache::NoStore,
149 WebCore::FetchOptions::Cache::Reload,
150 WebCore::FetchOptions::Cache::NoCache,
151 WebCore::FetchOptions::Cache::ForceCache,
152 WebCore::FetchOptions::Cache::OnlyIfCached
153 >;
154};
155
156template<> struct EnumTraits<WebCore::FetchOptions::Redirect> {
157 using values = EnumValues<
158 WebCore::FetchOptions::Redirect,
159 WebCore::FetchOptions::Redirect::Follow,
160 WebCore::FetchOptions::Redirect::Error,
161 WebCore::FetchOptions::Redirect::Manual
162 >;
163};
164
165}
166
167namespace WebCore {
168
169template<class Encoder> inline void FetchOptions::encodePersistent(Encoder& encoder) const
170{
171 // Changes to encoding here should bump NetworkCache Storage format version.
172 encoder << destination;
173 encoder << mode;
174 encoder << credentials;
175 encoder << cache;
176 encoder << redirect;
177 encoder << referrerPolicy;
178 encoder << integrity;
179 encoder << keepAlive;
180}
181
182template<class Decoder> inline bool FetchOptions::decodePersistent(Decoder& decoder, FetchOptions& options)
183{
184 FetchOptions::Destination destination;
185 if (!decoder.decode(destination))
186 return false;
187
188 FetchOptions::Mode mode;
189 if (!decoder.decode(mode))
190 return false;
191
192 FetchOptions::Credentials credentials;
193 if (!decoder.decode(credentials))
194 return false;
195
196 FetchOptions::Cache cache;
197 if (!decoder.decode(cache))
198 return false;
199
200 FetchOptions::Redirect redirect;
201 if (!decoder.decode(redirect))
202 return false;
203
204 ReferrerPolicy referrerPolicy;
205 if (!decoder.decode(referrerPolicy))
206 return false;
207
208 String integrity;
209 if (!decoder.decode(integrity))
210 return false;
211
212 bool keepAlive;
213 if (!decoder.decode(keepAlive))
214 return false;
215
216 options.destination = destination;
217 options.mode = mode;
218 options.credentials = credentials;
219 options.cache = cache;
220 options.redirect = redirect;
221 options.referrerPolicy = referrerPolicy;
222 options.integrity = WTFMove(integrity);
223 options.keepAlive = keepAlive;
224
225 return true;
226}
227
228template<class Encoder> inline void FetchOptions::encode(Encoder& encoder) const
229{
230 encodePersistent(encoder);
231 encoder << clientIdentifier;
232}
233
234template<class Decoder> inline Optional<FetchOptions> FetchOptions::decode(Decoder& decoder)
235{
236 FetchOptions options;
237 if (!decodePersistent(decoder, options))
238 return WTF::nullopt;
239
240 Optional<Optional<DocumentIdentifier>> clientIdentifier;
241 decoder >> clientIdentifier;
242 if (!clientIdentifier)
243 return WTF::nullopt;
244 options.clientIdentifier = WTFMove(clientIdentifier.value());
245
246 return options;
247}
248
249} // namespace WebCore
250