Environments.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/URL.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  11. #include <LibWeb/HTML/Origin.h>
  12. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  13. namespace Web::HTML {
  14. // https://html.spec.whatwg.org/multipage/webappapis.html#environment
  15. struct Environment {
  16. virtual ~Environment() = default;
  17. // An id https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-id
  18. DeprecatedString id;
  19. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-creation-url
  20. AK::URL creation_url;
  21. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url
  22. AK::URL top_level_creation_url;
  23. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin
  24. Origin top_level_origin;
  25. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  26. JS::GCPtr<BrowsingContext> target_browsing_context;
  27. // FIXME: An active service worker https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
  28. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-execution-ready-flag
  29. bool execution_ready { false };
  30. };
  31. enum class CanUseCrossOriginIsolatedAPIs {
  32. No,
  33. Yes,
  34. };
  35. enum class RunScriptDecision {
  36. Run,
  37. DoNotRun,
  38. };
  39. // https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
  40. struct EnvironmentSettingsObject
  41. : public JS::Cell
  42. , public Environment {
  43. JS_CELL(EnvironmentSettingsObject, JS::Cell);
  44. virtual ~EnvironmentSettingsObject() override;
  45. virtual void initialize(JS::Realm&) override;
  46. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  47. JS::ExecutionContext& realm_execution_context();
  48. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-module-map
  49. ModuleMap& module_map();
  50. // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
  51. virtual JS::GCPtr<DOM::Document> responsible_document() = 0;
  52. // https://html.spec.whatwg.org/multipage/webappapis.html#api-url-character-encoding
  53. virtual DeprecatedString api_url_character_encoding() = 0;
  54. // https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
  55. virtual AK::URL api_base_url() = 0;
  56. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
  57. virtual Origin origin() = 0;
  58. // A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
  59. virtual PolicyContainer policy_container() = 0;
  60. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
  61. virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() = 0;
  62. AK::URL parse_url(StringView);
  63. JS::Realm& realm();
  64. JS::Object& global_object();
  65. EventLoop& responsible_event_loop();
  66. RunScriptDecision can_run_script();
  67. void prepare_to_run_script();
  68. void clean_up_after_running_script();
  69. void prepare_to_run_callback();
  70. void clean_up_after_running_callback();
  71. void push_onto_outstanding_rejected_promises_weak_set(JS::Promise*);
  72. // Returns true if removed, false otherwise.
  73. bool remove_from_outstanding_rejected_promises_weak_set(JS::Promise*);
  74. void push_onto_about_to_be_notified_rejected_promises_list(JS::NonnullGCPtr<JS::Promise>);
  75. // Returns true if removed, false otherwise.
  76. bool remove_from_about_to_be_notified_rejected_promises_list(JS::NonnullGCPtr<JS::Promise>);
  77. void notify_about_rejected_promises(Badge<EventLoop>);
  78. bool is_scripting_enabled() const;
  79. bool is_scripting_disabled() const;
  80. bool module_type_allowed(DeprecatedString const& module_type) const;
  81. void disallow_further_import_maps();
  82. protected:
  83. explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);
  84. virtual void visit_edges(Cell::Visitor&) override;
  85. private:
  86. NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;
  87. JS::GCPtr<ModuleMap> m_module_map;
  88. EventLoop* m_responsible_event_loop { nullptr };
  89. // https://html.spec.whatwg.org/multipage/webappapis.html#outstanding-rejected-promises-weak-set
  90. // The outstanding rejected promises weak set must not create strong references to any of its members, and implementations are free to limit its size, e.g. by removing old entries from it when new ones are added.
  91. Vector<JS::GCPtr<JS::Promise>> m_outstanding_rejected_promises_weak_set;
  92. // https://html.spec.whatwg.org/multipage/webappapis.html#about-to-be-notified-rejected-promises-list
  93. Vector<JS::Handle<JS::Promise>> m_about_to_be_notified_rejected_promises_list;
  94. };
  95. EnvironmentSettingsObject& incumbent_settings_object();
  96. JS::Realm& incumbent_realm();
  97. JS::Object& incumbent_global_object();
  98. EnvironmentSettingsObject& current_settings_object();
  99. JS::Object& current_global_object();
  100. JS::Realm& relevant_realm(JS::Object const&);
  101. EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
  102. EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
  103. JS::Object& relevant_global_object(JS::Object const&);
  104. JS::Realm& entry_realm();
  105. EnvironmentSettingsObject& entry_settings_object();
  106. JS::Object& entry_global_object();
  107. JS::VM& relevant_agent(JS::Object const&);
  108. [[nodiscard]] bool is_secure_context(Environment const&);
  109. [[nodiscard]] bool is_non_secure_context(Environment const&);
  110. }