Environments.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibJS/Forward.h>
  10. #include <LibURL/Origin.h>
  11. #include <LibURL/URL.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  14. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  15. #include <LibWeb/HTML/Scripting/SerializedEnvironmentSettingsObject.h>
  16. namespace Web::HTML {
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#environment
  18. struct Environment : public JS::Cell {
  19. GC_CELL(Environment, JS::Cell);
  20. public:
  21. virtual ~Environment() override;
  22. // An id https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-id
  23. String id;
  24. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-creation-url
  25. URL::URL creation_url;
  26. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url
  27. URL::URL top_level_creation_url;
  28. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin
  29. URL::Origin top_level_origin;
  30. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  31. GC::Ptr<BrowsingContext> target_browsing_context;
  32. // FIXME: An active service worker https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
  33. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-execution-ready-flag
  34. bool execution_ready { false };
  35. protected:
  36. virtual void visit_edges(Cell::Visitor&) override;
  37. };
  38. enum class RunScriptDecision {
  39. Run,
  40. DoNotRun,
  41. };
  42. // https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
  43. struct EnvironmentSettingsObject : public Environment {
  44. GC_CELL(EnvironmentSettingsObject, Environment);
  45. public:
  46. virtual ~EnvironmentSettingsObject() override;
  47. virtual void initialize(JS::Realm&) override;
  48. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  49. JS::ExecutionContext& realm_execution_context();
  50. JS::ExecutionContext const& realm_execution_context() const;
  51. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-module-map
  52. ModuleMap& module_map();
  53. // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
  54. virtual GC::Ptr<DOM::Document> responsible_document() = 0;
  55. // https://html.spec.whatwg.org/multipage/webappapis.html#api-url-character-encoding
  56. virtual String api_url_character_encoding() const = 0;
  57. // https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
  58. virtual URL::URL api_base_url() const = 0;
  59. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
  60. virtual URL::Origin origin() const = 0;
  61. // A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
  62. virtual PolicyContainer policy_container() const = 0;
  63. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
  64. virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const = 0;
  65. URL::URL parse_url(StringView);
  66. URL::URL encoding_parse_url(StringView);
  67. Optional<String> encoding_parse_and_serialize_url(StringView);
  68. JS::Realm& realm();
  69. JS::Object& global_object();
  70. EventLoop& responsible_event_loop();
  71. // https://fetch.spec.whatwg.org/#concept-fetch-group
  72. Vector<GC::Ref<Fetch::Infrastructure::FetchRecord>>& fetch_group() { return m_fetch_group; }
  73. SerializedEnvironmentSettingsObject serialize();
  74. GC::Ref<StorageAPI::StorageManager> storage_manager();
  75. [[nodiscard]] bool discarded() const { return m_discarded; }
  76. void set_discarded(bool b) { m_discarded = b; }
  77. protected:
  78. explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);
  79. virtual void visit_edges(Cell::Visitor&) override;
  80. private:
  81. NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;
  82. GC::Ptr<ModuleMap> m_module_map;
  83. GC::Ptr<EventLoop> m_responsible_event_loop;
  84. // https://fetch.spec.whatwg.org/#concept-fetch-record
  85. // A fetch group holds an ordered list of fetch records
  86. Vector<GC::Ref<Fetch::Infrastructure::FetchRecord>> m_fetch_group;
  87. // https://storage.spec.whatwg.org/#api
  88. // Each environment settings object has an associated StorageManager object.
  89. GC::Ptr<StorageAPI::StorageManager> m_storage_manager;
  90. // https://w3c.github.io/ServiceWorker/#service-worker-client-discarded-flag
  91. // A service worker client has an associated discarded flag. It is initially unset.
  92. bool m_discarded { false };
  93. };
  94. JS::ExecutionContext const& execution_context_of_realm(JS::Realm const&);
  95. inline JS::ExecutionContext& execution_context_of_realm(JS::Realm& realm) { return const_cast<JS::ExecutionContext&>(execution_context_of_realm(const_cast<JS::Realm const&>(realm))); }
  96. RunScriptDecision can_run_script(JS::Realm const&);
  97. bool is_scripting_enabled(JS::Realm const&);
  98. bool is_scripting_disabled(JS::Realm const&);
  99. void prepare_to_run_script(JS::Realm&);
  100. void clean_up_after_running_script(JS::Realm const&);
  101. void prepare_to_run_callback(JS::Realm&);
  102. void clean_up_after_running_callback(JS::Realm const&);
  103. ModuleMap& module_map_of_realm(JS::Realm&);
  104. bool module_type_allowed(JS::Realm const&, StringView module_type);
  105. void add_module_to_resolved_module_set(JS::Realm&, String const& serialized_base_url, String const& normalized_specifier, Optional<URL::URL> const& as_url);
  106. EnvironmentSettingsObject& incumbent_settings_object();
  107. JS::Realm& incumbent_realm();
  108. JS::Object& incumbent_global_object();
  109. JS::Realm& current_principal_realm();
  110. EnvironmentSettingsObject& principal_realm_settings_object(JS::Realm&);
  111. EnvironmentSettingsObject& current_principal_settings_object();
  112. JS::Realm& principal_realm(GC::Ref<JS::Realm>);
  113. JS::Object& current_principal_global_object();
  114. JS::Realm& relevant_realm(JS::Object const&);
  115. JS::Realm& relevant_principal_realm(JS::Object const&);
  116. EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
  117. EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
  118. EnvironmentSettingsObject& relevant_principal_settings_object(JS::Object const&);
  119. JS::Object& relevant_global_object(JS::Object const&);
  120. JS::Object& relevant_principal_global_object(JS::Object const&);
  121. JS::Realm& entry_realm();
  122. EnvironmentSettingsObject& entry_settings_object();
  123. JS::Object& entry_global_object();
  124. JS::VM& relevant_agent(JS::Object const&);
  125. [[nodiscard]] bool is_secure_context(Environment const&);
  126. [[nodiscard]] bool is_non_secure_context(Environment const&);
  127. }