Fetching.h 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  8. #include <LibWeb/HTML/CORSSettingAttribute.h>
  9. #include <LibWeb/HTML/Scripting/ImportMap.h>
  10. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  11. #include <LibWeb/HTML/Scripting/ModuleScript.h>
  12. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  13. namespace Web::HTML {
  14. using OnFetchScriptComplete = JS::SafeFunction<void(JS::GCPtr<Script>)>;
  15. // https://html.spec.whatwg.org/multipage/webappapis.html#script-fetch-options
  16. struct ScriptFetchOptions {
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-nonce
  18. String cryptographic_nonce {};
  19. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-integrity
  20. String integrity_metadata {};
  21. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-parser
  22. Fetch::Infrastructure::Request::ParserMetadata parser_metadata { Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted };
  23. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-credentials
  24. Fetch::Infrastructure::Request::CredentialsMode credentials_mode { Fetch::Infrastructure::Request::CredentialsMode::SameOrigin };
  25. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-referrer-policy
  26. Optional<ReferrerPolicy::ReferrerPolicy> referrer_policy;
  27. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-render-blocking
  28. bool render_blocking { false };
  29. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-script-fetch-options-fetch-priority
  30. Fetch::Infrastructure::Request::Priority fetch_priority {};
  31. };
  32. class DescendantFetchingContext : public RefCounted<DescendantFetchingContext> {
  33. public:
  34. static NonnullRefPtr<DescendantFetchingContext> create() { return adopt_ref(*new DescendantFetchingContext); }
  35. ~DescendantFetchingContext() = default;
  36. size_t pending_count() const { return m_pending_count; }
  37. void set_pending_count(size_t count) { m_pending_count = count; }
  38. void decrement_pending_count() { --m_pending_count; }
  39. bool failed() const { return m_failed; }
  40. void set_failed(bool failed) { m_failed = failed; }
  41. void on_complete(JavaScriptModuleScript* module_script) { m_on_complete(module_script); }
  42. void set_on_complete(OnFetchScriptComplete on_complete) { m_on_complete = move(on_complete); }
  43. private:
  44. DescendantFetchingContext() = default;
  45. size_t m_pending_count { 0 };
  46. bool m_failed { false };
  47. OnFetchScriptComplete m_on_complete;
  48. };
  49. DeprecatedString module_type_from_module_request(JS::ModuleRequest const&);
  50. WebIDL::ExceptionOr<AK::URL> resolve_module_specifier(Optional<Script&> referring_script, DeprecatedString const& specifier);
  51. WebIDL::ExceptionOr<Optional<AK::URL>> resolve_imports_match(DeprecatedString const& normalized_specifier, Optional<AK::URL> as_url, ModuleSpecifierMap const&);
  52. Optional<AK::URL> resolve_url_like_module_specifier(DeprecatedString const& specifier, AK::URL const& base_url);
  53. 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);
  54. 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);
  55. void fetch_external_module_script_graph(JS::Realm&, AK::URL const&, EnvironmentSettingsObject& settings_object, ScriptFetchOptions const&, OnFetchScriptComplete on_complete);
  56. void fetch_inline_module_script_graph(JS::Realm& realm, DeprecatedString const& filename, DeprecatedString const& source_text, AK::URL const& base_url, EnvironmentSettingsObject& settings_object, OnFetchScriptComplete on_complete);
  57. 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);
  58. void fetch_descendants_of_and_link_a_module_script(JS::Realm&, JavaScriptModuleScript& module_script, EnvironmentSettingsObject& fetch_client_settings_object, Fetch::Infrastructure::Request::Destination, HashTable<ModuleLocationTuple> const& visited_set, OnFetchScriptComplete on_complete);
  59. enum class TopLevelModule {
  60. Yes,
  61. No
  62. };
  63. 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);
  64. }