Coverage Report

Created: 2026-06-07 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HTML/Scripting/Fetching.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
10
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
11
#include <LibWeb/HTML/CORSSettingAttribute.h>
12
#include <LibWeb/HTML/Scripting/ImportMap.h>
13
#include <LibWeb/HTML/Scripting/ModuleMap.h>
14
#include <LibWeb/HTML/Scripting/ModuleScript.h>
15
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
16
17
namespace Web::HTML {
18
19
enum class TopLevelModule {
20
    Yes,
21
    No
22
};
23
24
using OnFetchScriptComplete = JS::NonnullGCPtr<JS::HeapFunction<void(JS::GCPtr<Script>)>>;
25
using PerformTheFetchHook = JS::GCPtr<JS::HeapFunction<WebIDL::ExceptionOr<void>(JS::NonnullGCPtr<Fetch::Infrastructure::Request>, TopLevelModule, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction)>>;
26
27
OnFetchScriptComplete create_on_fetch_script_complete(JS::Heap& heap, Function<void(JS::GCPtr<Script>)> function);
28
PerformTheFetchHook create_perform_the_fetch_hook(JS::Heap& heap, Function<WebIDL::ExceptionOr<void>(JS::NonnullGCPtr<Fetch::Infrastructure::Request>, TopLevelModule, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction)> function);
29
30
// https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options
31
struct ScriptFetchOptions {
32
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-nonce
33
    String cryptographic_nonce {};
34
35
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-integrity
36
    String integrity_metadata {};
37
38
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-parser
39
    Fetch::Infrastructure::Request::ParserMetadata parser_metadata { Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted };
40
41
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-credentials
42
    Fetch::Infrastructure::Request::CredentialsMode credentials_mode { Fetch::Infrastructure::Request::CredentialsMode::SameOrigin };
43
44
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-referrer-policy
45
    ReferrerPolicy::ReferrerPolicy referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };
46
47
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-render-blocking
48
    bool render_blocking { false };
49
50
    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-fetch-priority
51
    Fetch::Infrastructure::Request::Priority fetch_priority {};
52
};
53
54
// https://html.spec.whatwg.org/multipage/webappapis.html#default-classic-script-fetch-options
55
ScriptFetchOptions default_classic_script_fetch_options();
56
57
class FetchContext : public JS::GraphLoadingState::HostDefined {
58
    JS_CELL(FetchContext, JS::GraphLoadingState::HostDefined);
59
    JS_DECLARE_ALLOCATOR(FetchContext);
60
61
public:
62
    JS::Value parse_error;                                    // [[ParseError]]
63
    Fetch::Infrastructure::Request::Destination destination;  // [[Destination]]
64
    PerformTheFetchHook perform_fetch;                        // [[PerformFetch]]
65
    JS::NonnullGCPtr<EnvironmentSettingsObject> fetch_client; // [[FetchClient]]
66
67
private:
68
    FetchContext(JS::Value parse_error, Fetch::Infrastructure::Request::Destination destination, PerformTheFetchHook perform_fetch, EnvironmentSettingsObject& fetch_client)
69
0
        : parse_error(parse_error)
70
0
        , destination(destination)
71
0
        , perform_fetch(perform_fetch)
72
0
        , fetch_client(fetch_client)
73
0
    {
74
0
    }
75
76
    void visit_edges(Visitor& visitor) override
77
0
    {
78
0
        Base::visit_edges(visitor);
79
0
        visitor.visit(parse_error);
80
0
        visitor.visit(perform_fetch);
81
0
        visitor.visit(fetch_client);
82
0
    }
83
};
84
85
ByteString module_type_from_module_request(JS::ModuleRequest const&);
86
WebIDL::ExceptionOr<URL::URL> resolve_module_specifier(Optional<Script&> referring_script, ByteString const& specifier);
87
WebIDL::ExceptionOr<Optional<URL::URL>> resolve_imports_match(ByteString const& normalized_specifier, Optional<URL::URL> as_url, ModuleSpecifierMap const&);
88
Optional<URL::URL> resolve_url_like_module_specifier(ByteString const& specifier, URL::URL const& base_url);
89
WebIDL::ExceptionOr<ScriptFetchOptions> get_descendant_script_fetch_options(ScriptFetchOptions const& original_options, URL::URL const& url, EnvironmentSettingsObject& settings_object);
90
WebIDL::ExceptionOr<String> resolve_a_module_integrity_metadata(URL::URL const& url, EnvironmentSettingsObject& settings_object);
91
WebIDL::ExceptionOr<void> fetch_classic_script(JS::NonnullGCPtr<HTMLScriptElement>, URL::URL const&, EnvironmentSettingsObject& settings_object, ScriptFetchOptions options, CORSSettingAttribute cors_setting, String character_encoding, OnFetchScriptComplete on_complete);
92
WebIDL::ExceptionOr<void> fetch_classic_worker_script(URL::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook, OnFetchScriptComplete);
93
WebIDL::ExceptionOr<JS::NonnullGCPtr<ClassicScript>> fetch_a_classic_worker_imported_script(URL::URL const&, HTML::EnvironmentSettingsObject&, PerformTheFetchHook = nullptr);
94
WebIDL::ExceptionOr<void> fetch_module_worker_script_graph(URL::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook, OnFetchScriptComplete);
95
WebIDL::ExceptionOr<void> fetch_worklet_module_worker_script_graph(URL::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook, OnFetchScriptComplete);
96
void fetch_internal_module_script_graph(JS::Realm&, JS::ModuleRequest const& module_request, EnvironmentSettingsObject& fetch_client_settings_object, Fetch::Infrastructure::Request::Destination, ScriptFetchOptions const&, Script& referring_script, HashTable<ModuleLocationTuple> const& visited_set, PerformTheFetchHook, OnFetchScriptComplete on_complete);
97
void fetch_external_module_script_graph(JS::Realm&, URL::URL const&, EnvironmentSettingsObject& settings_object, ScriptFetchOptions const&, OnFetchScriptComplete on_complete);
98
void fetch_inline_module_script_graph(JS::Realm&, ByteString const& filename, ByteString const& source_text, URL::URL const& base_url, EnvironmentSettingsObject& settings_object, OnFetchScriptComplete on_complete);
99
void fetch_single_imported_module_script(JS::Realm&, URL::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, ScriptFetchOptions const&, EnvironmentSettingsObject& settings_object, Fetch::Infrastructure::Request::ReferrerType, JS::ModuleRequest const&, PerformTheFetchHook, OnFetchScriptComplete on_complete);
100
101
void fetch_descendants_of_a_module_script(JS::Realm&, JavaScriptModuleScript& module_script, EnvironmentSettingsObject& fetch_client_settings_object, Fetch::Infrastructure::Request::Destination, HashTable<ModuleLocationTuple> visited_set, PerformTheFetchHook, OnFetchScriptComplete callback);
102
void fetch_descendants_of_and_link_a_module_script(JS::Realm&, JavaScriptModuleScript&, EnvironmentSettingsObject&, Fetch::Infrastructure::Request::Destination, PerformTheFetchHook, OnFetchScriptComplete on_complete);
103
104
Fetch::Infrastructure::Request::Destination fetch_destination_from_module_type(Fetch::Infrastructure::Request::Destination, ByteString const&);
105
106
void fetch_single_module_script(JS::Realm&, URL::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, ScriptFetchOptions const&, EnvironmentSettingsObject& settings_object, Web::Fetch::Infrastructure::Request::ReferrerType const&, Optional<JS::ModuleRequest> const&, TopLevelModule, PerformTheFetchHook, OnFetchScriptComplete callback);
107
}