/src/serenity/Userland/Libraries/LibWeb/Fetch/Enums.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/RequestPrototype.h> |
8 | | #include <LibWeb/Bindings/ResponsePrototype.h> |
9 | | #include <LibWeb/Fetch/Enums.h> |
10 | | #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> |
11 | | #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h> |
12 | | |
13 | | namespace Web::Fetch { |
14 | | |
15 | | // We have a handful of enums that have both a generated and a handwritten version, and need to |
16 | | // convert between some of them. This has three reasons: |
17 | | // - Some enums have more internal values in the spec than what is exposed to JS. An example of |
18 | | // this is Request::Destination's ServiceWorker member and Request::Mode's WebSocket member, |
19 | | // both of which are not present in the IDL-defined enums. |
20 | | // - The generated enums are not perfect, e.g. "no-cors" becomes NoCors, not NoCORS. This is fine |
21 | | // for the generated constructor/prototype code, but not great for the remaining handwritten |
22 | | // code. |
23 | | // - Fetch has use-cases beyond its JS interface, so having to refer to the 'Bindings' namespace |
24 | | // constantly is irritating. |
25 | | |
26 | | ReferrerPolicy::ReferrerPolicy from_bindings_enum(Bindings::ReferrerPolicy referrer_policy) |
27 | 0 | { |
28 | 0 | switch (referrer_policy) { |
29 | 0 | case Bindings::ReferrerPolicy::Empty: |
30 | 0 | return ReferrerPolicy::ReferrerPolicy::EmptyString; |
31 | 0 | case Bindings::ReferrerPolicy::NoReferrer: |
32 | 0 | return ReferrerPolicy::ReferrerPolicy::NoReferrer; |
33 | 0 | case Bindings::ReferrerPolicy::NoReferrerWhenDowngrade: |
34 | 0 | return ReferrerPolicy::ReferrerPolicy::NoReferrerWhenDowngrade; |
35 | 0 | case Bindings::ReferrerPolicy::SameOrigin: |
36 | 0 | return ReferrerPolicy::ReferrerPolicy::SameOrigin; |
37 | 0 | case Bindings::ReferrerPolicy::Origin: |
38 | 0 | return ReferrerPolicy::ReferrerPolicy::Origin; |
39 | 0 | case Bindings::ReferrerPolicy::StrictOrigin: |
40 | 0 | return ReferrerPolicy::ReferrerPolicy::StrictOrigin; |
41 | 0 | case Bindings::ReferrerPolicy::OriginWhenCrossOrigin: |
42 | 0 | return ReferrerPolicy::ReferrerPolicy::OriginWhenCrossOrigin; |
43 | 0 | case Bindings::ReferrerPolicy::StrictOriginWhenCrossOrigin: |
44 | 0 | return ReferrerPolicy::ReferrerPolicy::StrictOriginWhenCrossOrigin; |
45 | 0 | case Bindings::ReferrerPolicy::UnsafeUrl: |
46 | 0 | return ReferrerPolicy::ReferrerPolicy::UnsafeURL; |
47 | 0 | default: |
48 | 0 | VERIFY_NOT_REACHED(); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | Infrastructure::Request::Mode from_bindings_enum(Bindings::RequestMode mode) |
53 | 0 | { |
54 | 0 | switch (mode) { |
55 | 0 | case Bindings::RequestMode::SameOrigin: |
56 | 0 | return Infrastructure::Request::Mode::SameOrigin; |
57 | 0 | case Bindings::RequestMode::Cors: |
58 | 0 | return Infrastructure::Request::Mode::CORS; |
59 | 0 | case Bindings::RequestMode::NoCors: |
60 | 0 | return Infrastructure::Request::Mode::NoCORS; |
61 | 0 | case Bindings::RequestMode::Navigate: |
62 | 0 | return Infrastructure::Request::Mode::Navigate; |
63 | 0 | default: |
64 | 0 | VERIFY_NOT_REACHED(); |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | Infrastructure::Request::CredentialsMode from_bindings_enum(Bindings::RequestCredentials request_credentials) |
69 | 0 | { |
70 | 0 | switch (request_credentials) { |
71 | 0 | case Bindings::RequestCredentials::Omit: |
72 | 0 | return Infrastructure::Request::CredentialsMode::Omit; |
73 | 0 | case Bindings::RequestCredentials::SameOrigin: |
74 | 0 | return Infrastructure::Request::CredentialsMode::SameOrigin; |
75 | 0 | case Bindings::RequestCredentials::Include: |
76 | 0 | return Infrastructure::Request::CredentialsMode::Include; |
77 | 0 | default: |
78 | 0 | VERIFY_NOT_REACHED(); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | Infrastructure::Request::CacheMode from_bindings_enum(Bindings::RequestCache request_cache) |
83 | 0 | { |
84 | 0 | switch (request_cache) { |
85 | 0 | case Bindings::RequestCache::Default: |
86 | 0 | return Infrastructure::Request::CacheMode::Default; |
87 | 0 | case Bindings::RequestCache::NoStore: |
88 | 0 | return Infrastructure::Request::CacheMode::NoStore; |
89 | 0 | case Bindings::RequestCache::Reload: |
90 | 0 | return Infrastructure::Request::CacheMode::Reload; |
91 | 0 | case Bindings::RequestCache::NoCache: |
92 | 0 | return Infrastructure::Request::CacheMode::NoCache; |
93 | 0 | case Bindings::RequestCache::ForceCache: |
94 | 0 | return Infrastructure::Request::CacheMode::ForceCache; |
95 | 0 | case Bindings::RequestCache::OnlyIfCached: |
96 | 0 | return Infrastructure::Request::CacheMode::OnlyIfCached; |
97 | 0 | default: |
98 | 0 | VERIFY_NOT_REACHED(); |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | Infrastructure::Request::RedirectMode from_bindings_enum(Bindings::RequestRedirect request_redirect) |
103 | 0 | { |
104 | 0 | switch (request_redirect) { |
105 | 0 | case Bindings::RequestRedirect::Follow: |
106 | 0 | return Infrastructure::Request::RedirectMode::Follow; |
107 | 0 | case Bindings::RequestRedirect::Error: |
108 | 0 | return Infrastructure::Request::RedirectMode::Error; |
109 | 0 | case Bindings::RequestRedirect::Manual: |
110 | 0 | return Infrastructure::Request::RedirectMode::Manual; |
111 | 0 | default: |
112 | 0 | VERIFY_NOT_REACHED(); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | Infrastructure::Request::Priority from_bindings_enum(Bindings::RequestPriority request_priority) |
117 | 0 | { |
118 | 0 | switch (request_priority) { |
119 | 0 | case Bindings::RequestPriority::High: |
120 | 0 | return Infrastructure::Request::Priority::High; |
121 | 0 | case Bindings::RequestPriority::Low: |
122 | 0 | return Infrastructure::Request::Priority::Low; |
123 | 0 | case Bindings::RequestPriority::Auto: |
124 | 0 | return Infrastructure::Request::Priority::Auto; |
125 | 0 | default: |
126 | 0 | VERIFY_NOT_REACHED(); |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | Bindings::ReferrerPolicy to_bindings_enum(ReferrerPolicy::ReferrerPolicy referrer_policy) |
131 | 0 | { |
132 | 0 | switch (referrer_policy) { |
133 | 0 | case ReferrerPolicy::ReferrerPolicy::EmptyString: |
134 | 0 | return Bindings::ReferrerPolicy::Empty; |
135 | 0 | case ReferrerPolicy::ReferrerPolicy::NoReferrer: |
136 | 0 | return Bindings::ReferrerPolicy::NoReferrer; |
137 | 0 | case ReferrerPolicy::ReferrerPolicy::NoReferrerWhenDowngrade: |
138 | 0 | return Bindings::ReferrerPolicy::NoReferrerWhenDowngrade; |
139 | 0 | case ReferrerPolicy::ReferrerPolicy::SameOrigin: |
140 | 0 | return Bindings::ReferrerPolicy::SameOrigin; |
141 | 0 | case ReferrerPolicy::ReferrerPolicy::Origin: |
142 | 0 | return Bindings::ReferrerPolicy::Origin; |
143 | 0 | case ReferrerPolicy::ReferrerPolicy::StrictOrigin: |
144 | 0 | return Bindings::ReferrerPolicy::StrictOrigin; |
145 | 0 | case ReferrerPolicy::ReferrerPolicy::OriginWhenCrossOrigin: |
146 | 0 | return Bindings::ReferrerPolicy::OriginWhenCrossOrigin; |
147 | 0 | case ReferrerPolicy::ReferrerPolicy::StrictOriginWhenCrossOrigin: |
148 | 0 | return Bindings::ReferrerPolicy::StrictOriginWhenCrossOrigin; |
149 | 0 | case ReferrerPolicy::ReferrerPolicy::UnsafeURL: |
150 | 0 | return Bindings::ReferrerPolicy::UnsafeUrl; |
151 | 0 | default: |
152 | 0 | VERIFY_NOT_REACHED(); |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | | Bindings::RequestDestination to_bindings_enum(Optional<Infrastructure::Request::Destination> const& destination) |
157 | 0 | { |
158 | 0 | if (!destination.has_value()) |
159 | 0 | return Bindings::RequestDestination::Empty; |
160 | 0 | switch (*destination) { |
161 | 0 | case Infrastructure::Request::Destination::Audio: |
162 | 0 | return Bindings::RequestDestination::Audio; |
163 | 0 | case Infrastructure::Request::Destination::AudioWorklet: |
164 | 0 | return Bindings::RequestDestination::Audioworklet; |
165 | 0 | case Infrastructure::Request::Destination::Document: |
166 | 0 | return Bindings::RequestDestination::Document; |
167 | 0 | case Infrastructure::Request::Destination::Embed: |
168 | 0 | return Bindings::RequestDestination::Embed; |
169 | 0 | case Infrastructure::Request::Destination::Font: |
170 | 0 | return Bindings::RequestDestination::Font; |
171 | 0 | case Infrastructure::Request::Destination::Frame: |
172 | 0 | return Bindings::RequestDestination::Frame; |
173 | 0 | case Infrastructure::Request::Destination::IFrame: |
174 | 0 | return Bindings::RequestDestination::Iframe; |
175 | 0 | case Infrastructure::Request::Destination::Image: |
176 | 0 | return Bindings::RequestDestination::Image; |
177 | 0 | case Infrastructure::Request::Destination::JSON: |
178 | 0 | return Bindings::RequestDestination::Json; |
179 | 0 | case Infrastructure::Request::Destination::Manifest: |
180 | 0 | return Bindings::RequestDestination::Manifest; |
181 | 0 | case Infrastructure::Request::Destination::Object: |
182 | 0 | return Bindings::RequestDestination::Object; |
183 | 0 | case Infrastructure::Request::Destination::PaintWorklet: |
184 | 0 | return Bindings::RequestDestination::Paintworklet; |
185 | 0 | case Infrastructure::Request::Destination::Report: |
186 | 0 | return Bindings::RequestDestination::Report; |
187 | 0 | case Infrastructure::Request::Destination::Script: |
188 | 0 | return Bindings::RequestDestination::Script; |
189 | 0 | case Infrastructure::Request::Destination::ServiceWorker: |
190 | | // NOTE: "serviceworker" is omitted from RequestDestination as it cannot be observed from JavaScript. |
191 | | // Implementations will still need to support it as a destination. |
192 | 0 | VERIFY_NOT_REACHED(); |
193 | 0 | case Infrastructure::Request::Destination::SharedWorker: |
194 | 0 | return Bindings::RequestDestination::Sharedworker; |
195 | 0 | case Infrastructure::Request::Destination::Style: |
196 | 0 | return Bindings::RequestDestination::Style; |
197 | 0 | case Infrastructure::Request::Destination::Track: |
198 | 0 | return Bindings::RequestDestination::Track; |
199 | 0 | case Infrastructure::Request::Destination::Video: |
200 | 0 | return Bindings::RequestDestination::Video; |
201 | 0 | case Infrastructure::Request::Destination::Worker: |
202 | 0 | return Bindings::RequestDestination::Worker; |
203 | 0 | case Infrastructure::Request::Destination::XSLT: |
204 | 0 | return Bindings::RequestDestination::Xslt; |
205 | 0 | default: |
206 | 0 | VERIFY_NOT_REACHED(); |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | Bindings::RequestMode to_bindings_enum(Infrastructure::Request::Mode mode) |
211 | 0 | { |
212 | 0 | switch (mode) { |
213 | 0 | case Infrastructure::Request::Mode::SameOrigin: |
214 | 0 | return Bindings::RequestMode::SameOrigin; |
215 | 0 | case Infrastructure::Request::Mode::CORS: |
216 | 0 | return Bindings::RequestMode::Cors; |
217 | 0 | case Infrastructure::Request::Mode::NoCORS: |
218 | 0 | return Bindings::RequestMode::NoCors; |
219 | 0 | case Infrastructure::Request::Mode::Navigate: |
220 | 0 | return Bindings::RequestMode::Navigate; |
221 | 0 | case Infrastructure::Request::Mode::WebSocket: |
222 | | // NOTE: "websocket" is omitted from RequestMode as it cannot be used nor observed from JavaScript. |
223 | 0 | VERIFY_NOT_REACHED(); |
224 | 0 | default: |
225 | 0 | VERIFY_NOT_REACHED(); |
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | | Bindings::RequestCredentials to_bindings_enum(Infrastructure::Request::CredentialsMode credentials_mode) |
230 | 0 | { |
231 | 0 | switch (credentials_mode) { |
232 | 0 | case Infrastructure::Request::CredentialsMode::Omit: |
233 | 0 | return Bindings::RequestCredentials::Omit; |
234 | 0 | case Infrastructure::Request::CredentialsMode::SameOrigin: |
235 | 0 | return Bindings::RequestCredentials::SameOrigin; |
236 | 0 | case Infrastructure::Request::CredentialsMode::Include: |
237 | 0 | return Bindings::RequestCredentials::Include; |
238 | 0 | default: |
239 | 0 | VERIFY_NOT_REACHED(); |
240 | 0 | } |
241 | 0 | } |
242 | | |
243 | | Bindings::RequestCache to_bindings_enum(Infrastructure::Request::CacheMode cache_mode) |
244 | 0 | { |
245 | 0 | switch (cache_mode) { |
246 | 0 | case Infrastructure::Request::CacheMode::Default: |
247 | 0 | return Bindings::RequestCache::Default; |
248 | 0 | case Infrastructure::Request::CacheMode::NoStore: |
249 | 0 | return Bindings::RequestCache::NoStore; |
250 | 0 | case Infrastructure::Request::CacheMode::Reload: |
251 | 0 | return Bindings::RequestCache::Reload; |
252 | 0 | case Infrastructure::Request::CacheMode::NoCache: |
253 | 0 | return Bindings::RequestCache::NoCache; |
254 | 0 | case Infrastructure::Request::CacheMode::ForceCache: |
255 | 0 | return Bindings::RequestCache::ForceCache; |
256 | 0 | case Infrastructure::Request::CacheMode::OnlyIfCached: |
257 | 0 | return Bindings::RequestCache::OnlyIfCached; |
258 | 0 | default: |
259 | 0 | VERIFY_NOT_REACHED(); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | Bindings::RequestRedirect to_bindings_enum(Infrastructure::Request::RedirectMode redirect_mode) |
264 | 0 | { |
265 | 0 | switch (redirect_mode) { |
266 | 0 | case Infrastructure::Request::RedirectMode::Follow: |
267 | 0 | return Bindings::RequestRedirect::Follow; |
268 | 0 | case Infrastructure::Request::RedirectMode::Error: |
269 | 0 | return Bindings::RequestRedirect::Error; |
270 | 0 | case Infrastructure::Request::RedirectMode::Manual: |
271 | 0 | return Bindings::RequestRedirect::Manual; |
272 | 0 | default: |
273 | 0 | VERIFY_NOT_REACHED(); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | Bindings::ResponseType to_bindings_enum(Infrastructure::Response::Type type) |
278 | 0 | { |
279 | 0 | switch (type) { |
280 | 0 | case Infrastructure::Response::Type::Basic: |
281 | 0 | return Bindings::ResponseType::Basic; |
282 | 0 | case Infrastructure::Response::Type::CORS: |
283 | 0 | return Bindings::ResponseType::Cors; |
284 | 0 | case Infrastructure::Response::Type::Default: |
285 | 0 | return Bindings::ResponseType::Default; |
286 | 0 | case Infrastructure::Response::Type::Error: |
287 | 0 | return Bindings::ResponseType::Error; |
288 | 0 | case Infrastructure::Response::Type::Opaque: |
289 | 0 | return Bindings::ResponseType::Opaque; |
290 | 0 | case Infrastructure::Response::Type::OpaqueRedirect: |
291 | 0 | return Bindings::ResponseType::Opaqueredirect; |
292 | 0 | default: |
293 | 0 | VERIFY_NOT_REACHED(); |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | | } |