Fetching.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  8. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  9. #include <LibWeb/HTML/CORSSettingAttribute.h>
  10. #include <LibWeb/HTML/Scripting/ImportMap.h>
  11. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  12. #include <LibWeb/HTML/Scripting/ModuleScript.h>
  13. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  14. namespace Web::HTML {
  15. enum class IsTopLevel {
  16. No,
  17. Yes,
  18. };
  19. using OnFetchScriptComplete = JS::NonnullGCPtr<JS::HeapFunction<void(JS::GCPtr<Script>)>>;
  20. using PerformTheFetchHook = JS::GCPtr<JS::HeapFunction<WebIDL::ExceptionOr<void>(JS::NonnullGCPtr<Fetch::Infrastructure::Request>, IsTopLevel, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction)>>;
  21. OnFetchScriptComplete create_on_fetch_script_complete(JS::Heap& heap, Function<void(JS::GCPtr<Script>)> function);
  22. PerformTheFetchHook create_perform_the_fetch_hook(JS::Heap& heap, Function<WebIDL::ExceptionOr<void>(JS::NonnullGCPtr<Fetch::Infrastructure::Request>, IsTopLevel, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction)> function);
  23. // https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options
  24. struct ScriptFetchOptions {
  25. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-nonce
  26. String cryptographic_nonce {};
  27. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-integrity
  28. String integrity_metadata {};
  29. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-parser
  30. Fetch::Infrastructure::Request::ParserMetadata parser_metadata { Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted };
  31. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-credentials
  32. Fetch::Infrastructure::Request::CredentialsMode credentials_mode { Fetch::Infrastructure::Request::CredentialsMode::SameOrigin };
  33. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-referrer-policy
  34. Optional<ReferrerPolicy::ReferrerPolicy> referrer_policy;
  35. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-render-blocking
  36. bool render_blocking { false };
  37. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-fetch-priority
  38. Fetch::Infrastructure::Request::Priority fetch_priority {};
  39. };
  40. // https://html.spec.whatwg.org/multipage/webappapis.html#default-classic-script-fetch-options
  41. ScriptFetchOptions default_classic_script_fetch_options();
  42. class FetchContext : public JS::GraphLoadingState::HostDefined {
  43. JS_CELL(FetchContext, JS::GraphLoadingState::HostDefined);
  44. public:
  45. JS::Value parse_error; // [[ParseError]]
  46. Fetch::Infrastructure::Request::Destination destination; // [[Destination]]
  47. JS::GCPtr<JS::Promise> perform_fetch; // [[PerformFetch]]
  48. JS::NonnullGCPtr<EnvironmentSettingsObject> fetch_client; // [[FetchClient]]
  49. private:
  50. FetchContext(JS::Value parse_error, Fetch::Infrastructure::Request::Destination destination, JS::GCPtr<JS::Promise> perform_fetch, EnvironmentSettingsObject& fetch_client)
  51. : parse_error(parse_error)
  52. , destination(destination)
  53. , perform_fetch(perform_fetch)
  54. , fetch_client(fetch_client)
  55. {
  56. }
  57. void visit_edges(Visitor& visitor) override
  58. {
  59. Base::visit_edges(visitor);
  60. visitor.visit(parse_error);
  61. visitor.visit(perform_fetch);
  62. visitor.visit(fetch_client);
  63. }
  64. };
  65. ByteString module_type_from_module_request(JS::ModuleRequest const&);
  66. WebIDL::ExceptionOr<AK::URL> resolve_module_specifier(Optional<Script&> referring_script, ByteString const& specifier);
  67. WebIDL::ExceptionOr<Optional<AK::URL>> resolve_imports_match(ByteString const& normalized_specifier, Optional<AK::URL> as_url, ModuleSpecifierMap const&);
  68. Optional<AK::URL> resolve_url_like_module_specifier(ByteString const& specifier, AK::URL const& base_url);
  69. WebIDL::ExceptionOr<void> fetch_classic_script(JS::NonnullGCPtr<HTMLScriptElement>, AK::URL const&, EnvironmentSettingsObject& settings_object, ScriptFetchOptions options, CORSSettingAttribute cors_setting, String character_encoding, OnFetchScriptComplete on_complete);
  70. WebIDL::ExceptionOr<void> fetch_classic_worker_script(AK::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, EnvironmentSettingsObject& settings_object, PerformTheFetchHook, OnFetchScriptComplete);
  71. 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, OnFetchScriptComplete on_complete);
  72. void fetch_external_module_script_graph(JS::Realm&, AK::URL const&, EnvironmentSettingsObject& settings_object, ScriptFetchOptions const&, OnFetchScriptComplete on_complete);
  73. void fetch_inline_module_script_graph(JS::Realm&, ByteString const& filename, ByteString const& source_text, AK::URL const& base_url, EnvironmentSettingsObject& settings_object, OnFetchScriptComplete on_complete);
  74. void fetch_single_imported_module_script(JS::Realm&, AK::URL const&, EnvironmentSettingsObject& fetch_client, Fetch::Infrastructure::Request::Destination, ScriptFetchOptions const&, EnvironmentSettingsObject& settings_object, Fetch::Infrastructure::Request::Referrer, JS::ModuleRequest const&, OnFetchScriptComplete on_complete);
  75. 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, OnFetchScriptComplete callback);
  76. void fetch_descendants_of_and_link_a_module_script(JS::Realm&, JavaScriptModuleScript&, EnvironmentSettingsObject&, Fetch::Infrastructure::Request::Destination, OnFetchScriptComplete on_complete);
  77. enum class TopLevelModule {
  78. Yes,
  79. No
  80. };
  81. void fetch_single_module_script(JS::Realm&, AK::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, OnFetchScriptComplete callback);
  82. }