summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Fetch/Enums.cpp
blob: fc9779a83c1c000bec57ec06bf456035164e6459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/Bindings/RequestPrototype.h>
#include <LibWeb/Bindings/ResponsePrototype.h>
#include <LibWeb/Fetch/Enums.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>

namespace Web::Fetch {

// We have a handful of enums that have both a generated and a handwritten version, and need to
// convert between some of them. This has three reasons:
// - Some enums have more internal values in the spec than what is exposed to JS. An example of
//   this is Request::Destination's ServiceWorker member and Request::Mode's WebSocket member,
//   both of which are not present in the IDL-defined enums.
// - The generated enums are not perfect, e.g. "no-cors" becomes NoCors, not NoCORS. This is fine
//   for the generated constructor/prototype code, but not great for the remaining handwritten
//   code.
// - Fetch has use-cases beyond its JS interface, so having to refer to the 'Bindings' namespace
//   constantly is irritating.

Optional<ReferrerPolicy::ReferrerPolicy> from_bindings_enum(Bindings::ReferrerPolicy referrer_policy)
{
    switch (referrer_policy) {
    case Bindings::ReferrerPolicy::Empty:
        return {};
    case Bindings::ReferrerPolicy::NoReferrer:
        return ReferrerPolicy::ReferrerPolicy::NoReferrer;
    case Bindings::ReferrerPolicy::NoReferrerWhenDowngrade:
        return ReferrerPolicy::ReferrerPolicy::NoReferrerWhenDowngrade;
    case Bindings::ReferrerPolicy::SameOrigin:
        return ReferrerPolicy::ReferrerPolicy::SameOrigin;
    case Bindings::ReferrerPolicy::Origin:
        return ReferrerPolicy::ReferrerPolicy::Origin;
    case Bindings::ReferrerPolicy::StrictOrigin:
        return ReferrerPolicy::ReferrerPolicy::StrictOrigin;
    case Bindings::ReferrerPolicy::OriginWhenCrossOrigin:
        return ReferrerPolicy::ReferrerPolicy::OriginWhenCrossOrigin;
    case Bindings::ReferrerPolicy::StrictOriginWhenCrossOrigin:
        return ReferrerPolicy::ReferrerPolicy::StrictOriginWhenCrossOrigin;
    case Bindings::ReferrerPolicy::UnsafeUrl:
        return ReferrerPolicy::ReferrerPolicy::UnsafeURL;
    default:
        VERIFY_NOT_REACHED();
    }
}

Infrastructure::Request::Mode from_bindings_enum(Bindings::RequestMode mode)
{
    switch (mode) {
    case Bindings::RequestMode::SameOrigin:
        return Infrastructure::Request::Mode::SameOrigin;
    case Bindings::RequestMode::Cors:
        return Infrastructure::Request::Mode::CORS;
    case Bindings::RequestMode::NoCors:
        return Infrastructure::Request::Mode::NoCORS;
    case Bindings::RequestMode::Navigate:
        return Infrastructure::Request::Mode::Navigate;
    default:
        VERIFY_NOT_REACHED();
    }
}

Infrastructure::Request::CredentialsMode from_bindings_enum(Bindings::RequestCredentials request_credentials)
{
    switch (request_credentials) {
    case Bindings::RequestCredentials::Omit:
        return Infrastructure::Request::CredentialsMode::Omit;
    case Bindings::RequestCredentials::SameOrigin:
        return Infrastructure::Request::CredentialsMode::SameOrigin;
    case Bindings::RequestCredentials::Include:
        return Infrastructure::Request::CredentialsMode::Include;
    default:
        VERIFY_NOT_REACHED();
    }
}

Infrastructure::Request::CacheMode from_bindings_enum(Bindings::RequestCache request_cache)
{
    switch (request_cache) {
    case Bindings::RequestCache::Default:
        return Infrastructure::Request::CacheMode::Default;
    case Bindings::RequestCache::NoStore:
        return Infrastructure::Request::CacheMode::NoStore;
    case Bindings::RequestCache::Reload:
        return Infrastructure::Request::CacheMode::Reload;
    case Bindings::RequestCache::NoCache:
        return Infrastructure::Request::CacheMode::NoCache;
    case Bindings::RequestCache::ForceCache:
        return Infrastructure::Request::CacheMode::ForceCache;
    case Bindings::RequestCache::OnlyIfCached:
        return Infrastructure::Request::CacheMode::OnlyIfCached;
    default:
        VERIFY_NOT_REACHED();
    }
}

Infrastructure::Request::RedirectMode from_bindings_enum(Bindings::RequestRedirect request_redirect)
{
    switch (request_redirect) {
    case Bindings::RequestRedirect::Follow:
        return Infrastructure::Request::RedirectMode::Follow;
    case Bindings::RequestRedirect::Error:
        return Infrastructure::Request::RedirectMode::Error;
    case Bindings::RequestRedirect::Manual:
        return Infrastructure::Request::RedirectMode::Manual;
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::ReferrerPolicy to_bindings_enum(Optional<ReferrerPolicy::ReferrerPolicy> const& referrer_policy)
{
    if (!referrer_policy.has_value())
        return Bindings::ReferrerPolicy::Empty;
    switch (*referrer_policy) {
    case ReferrerPolicy::ReferrerPolicy::NoReferrer:
        return Bindings::ReferrerPolicy::NoReferrer;
    case ReferrerPolicy::ReferrerPolicy::NoReferrerWhenDowngrade:
        return Bindings::ReferrerPolicy::NoReferrerWhenDowngrade;
    case ReferrerPolicy::ReferrerPolicy::SameOrigin:
        return Bindings::ReferrerPolicy::SameOrigin;
    case ReferrerPolicy::ReferrerPolicy::Origin:
        return Bindings::ReferrerPolicy::Origin;
    case ReferrerPolicy::ReferrerPolicy::StrictOrigin:
        return Bindings::ReferrerPolicy::StrictOrigin;
    case ReferrerPolicy::ReferrerPolicy::OriginWhenCrossOrigin:
        return Bindings::ReferrerPolicy::OriginWhenCrossOrigin;
    case ReferrerPolicy::ReferrerPolicy::StrictOriginWhenCrossOrigin:
        return Bindings::ReferrerPolicy::StrictOriginWhenCrossOrigin;
    case ReferrerPolicy::ReferrerPolicy::UnsafeURL:
        return Bindings::ReferrerPolicy::UnsafeUrl;
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::RequestDestination to_bindings_enum(Optional<Infrastructure::Request::Destination> const& destination)
{
    if (!destination.has_value())
        return Bindings::RequestDestination::Empty;
    switch (*destination) {
    case Infrastructure::Request::Destination::Audio:
        return Bindings::RequestDestination::Audio;
    case Infrastructure::Request::Destination::AudioWorklet:
        return Bindings::RequestDestination::Audioworklet;
    case Infrastructure::Request::Destination::Document:
        return Bindings::RequestDestination::Document;
    case Infrastructure::Request::Destination::Embed:
        return Bindings::RequestDestination::Embed;
    case Infrastructure::Request::Destination::Font:
        return Bindings::RequestDestination::Font;
    case Infrastructure::Request::Destination::Frame:
        return Bindings::RequestDestination::Frame;
    case Infrastructure::Request::Destination::IFrame:
        return Bindings::RequestDestination::Iframe;
    case Infrastructure::Request::Destination::Image:
        return Bindings::RequestDestination::Image;
    case Infrastructure::Request::Destination::Manifest:
        return Bindings::RequestDestination::Manifest;
    case Infrastructure::Request::Destination::Object:
        return Bindings::RequestDestination::Object;
    case Infrastructure::Request::Destination::PaintWorklet:
        return Bindings::RequestDestination::Paintworklet;
    case Infrastructure::Request::Destination::Report:
        return Bindings::RequestDestination::Report;
    case Infrastructure::Request::Destination::Script:
        return Bindings::RequestDestination::Script;
    case Infrastructure::Request::Destination::ServiceWorker:
        // NOTE: "serviceworker" is omitted from RequestDestination as it cannot be observed from JavaScript.
        //       Implementations will still need to support it as a destination.
        VERIFY_NOT_REACHED();
    case Infrastructure::Request::Destination::SharedWorker:
        return Bindings::RequestDestination::Sharedworker;
    case Infrastructure::Request::Destination::Style:
        return Bindings::RequestDestination::Style;
    case Infrastructure::Request::Destination::Track:
        return Bindings::RequestDestination::Track;
    case Infrastructure::Request::Destination::Video:
        return Bindings::RequestDestination::Video;
    case Infrastructure::Request::Destination::Worker:
        return Bindings::RequestDestination::Worker;
    case Infrastructure::Request::Destination::XSLT:
        return Bindings::RequestDestination::Xslt;
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::RequestMode to_bindings_enum(Infrastructure::Request::Mode mode)
{
    switch (mode) {
    case Infrastructure::Request::Mode::SameOrigin:
        return Bindings::RequestMode::SameOrigin;
    case Infrastructure::Request::Mode::CORS:
        return Bindings::RequestMode::Cors;
    case Infrastructure::Request::Mode::NoCORS:
        return Bindings::RequestMode::NoCors;
    case Infrastructure::Request::Mode::Navigate:
        return Bindings::RequestMode::Navigate;
    case Infrastructure::Request::Mode::WebSocket:
        // NOTE: "websocket" is omitted from RequestMode as it cannot be used nor observed from JavaScript.
        VERIFY_NOT_REACHED();
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::RequestCredentials to_bindings_enum(Infrastructure::Request::CredentialsMode credentials_mode)
{
    switch (credentials_mode) {
    case Infrastructure::Request::CredentialsMode::Omit:
        return Bindings::RequestCredentials::Omit;
    case Infrastructure::Request::CredentialsMode::SameOrigin:
        return Bindings::RequestCredentials::SameOrigin;
    case Infrastructure::Request::CredentialsMode::Include:
        return Bindings::RequestCredentials::Include;
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::RequestCache to_bindings_enum(Infrastructure::Request::CacheMode cache_mode)
{
    switch (cache_mode) {
    case Infrastructure::Request::CacheMode::Default:
        return Bindings::RequestCache::Default;
    case Infrastructure::Request::CacheMode::NoStore:
        return Bindings::RequestCache::NoStore;
    case Infrastructure::Request::CacheMode::Reload:
        return Bindings::RequestCache::Reload;
    case Infrastructure::Request::CacheMode::NoCache:
        return Bindings::RequestCache::NoCache;
    case Infrastructure::Request::CacheMode::ForceCache:
        return Bindings::RequestCache::ForceCache;
    case Infrastructure::Request::CacheMode::OnlyIfCached:
        return Bindings::RequestCache::OnlyIfCached;
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::RequestRedirect to_bindings_enum(Infrastructure::Request::RedirectMode redirect_mode)
{
    switch (redirect_mode) {
    case Infrastructure::Request::RedirectMode::Follow:
        return Bindings::RequestRedirect::Follow;
    case Infrastructure::Request::RedirectMode::Error:
        return Bindings::RequestRedirect::Error;
    case Infrastructure::Request::RedirectMode::Manual:
        return Bindings::RequestRedirect::Manual;
    default:
        VERIFY_NOT_REACHED();
    }
}

Bindings::ResponseType to_bindings_enum(Infrastructure::Response::Type type)
{
    switch (type) {
    case Infrastructure::Response::Type::Basic:
        return Bindings::ResponseType::Basic;
    case Infrastructure::Response::Type::CORS:
        return Bindings::ResponseType::Cors;
    case Infrastructure::Response::Type::Default:
        return Bindings::ResponseType::Default;
    case Infrastructure::Response::Type::Error:
        return Bindings::ResponseType::Error;
    case Infrastructure::Response::Type::Opaque:
        return Bindings::ResponseType::Opaque;
    case Infrastructure::Response::Type::OpaqueRedirect:
        return Bindings::ResponseType::Opaqueredirect;
    default:
        VERIFY_NOT_REACHED();
    }
}

}