Environments.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/Runtime/ExecutionContext.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibJS/Runtime/Realm.h>
  13. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  14. #include <LibWeb/HTML/Origin.h>
  15. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  16. namespace Web::HTML {
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#environment
  18. struct Environment {
  19. virtual ~Environment() = default;
  20. // An id https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-id
  21. DeprecatedString id;
  22. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-creation-url
  23. AK::URL creation_url;
  24. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url
  25. AK::URL top_level_creation_url;
  26. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin
  27. Origin top_level_origin;
  28. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  29. JS::GCPtr<BrowsingContext> target_browsing_context;
  30. // FIXME: An active service worker https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
  31. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-execution-ready-flag
  32. bool execution_ready { false };
  33. };
  34. enum class CanUseCrossOriginIsolatedAPIs {
  35. No,
  36. Yes,
  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
  44. : public JS::Cell
  45. , public Environment {
  46. JS_CELL(EnvironmentSettingsObject, JS::Cell);
  47. virtual ~EnvironmentSettingsObject() override;
  48. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  49. JS::ExecutionContext& realm_execution_context();
  50. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-module-map
  51. ModuleMap& module_map();
  52. // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
  53. virtual JS::GCPtr<DOM::Document> responsible_document() = 0;
  54. // https://html.spec.whatwg.org/multipage/webappapis.html#api-url-character-encoding
  55. virtual DeprecatedString api_url_character_encoding() = 0;
  56. // https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
  57. virtual AK::URL api_base_url() = 0;
  58. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
  59. virtual Origin origin() = 0;
  60. // A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
  61. virtual PolicyContainer policy_container() = 0;
  62. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
  63. virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() = 0;
  64. AK::URL parse_url(StringView);
  65. JS::Realm& realm();
  66. JS::Object& global_object();
  67. EventLoop& responsible_event_loop();
  68. RunScriptDecision can_run_script();
  69. void prepare_to_run_script();
  70. void clean_up_after_running_script();
  71. void prepare_to_run_callback();
  72. void clean_up_after_running_callback();
  73. void push_onto_outstanding_rejected_promises_weak_set(JS::Promise*);
  74. // Returns true if removed, false otherwise.
  75. bool remove_from_outstanding_rejected_promises_weak_set(JS::Promise*);
  76. void push_onto_about_to_be_notified_rejected_promises_list(JS::NonnullGCPtr<JS::Promise>);
  77. // Returns true if removed, false otherwise.
  78. bool remove_from_about_to_be_notified_rejected_promises_list(JS::NonnullGCPtr<JS::Promise>);
  79. void notify_about_rejected_promises(Badge<EventLoop>);
  80. bool is_scripting_enabled() const;
  81. bool is_scripting_disabled() const;
  82. bool module_type_allowed(DeprecatedString const& module_type) const;
  83. void disallow_further_import_maps();
  84. protected:
  85. explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);
  86. virtual void visit_edges(Cell::Visitor&) override;
  87. private:
  88. NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;
  89. ModuleMap m_module_map;
  90. EventLoop* m_responsible_event_loop { nullptr };
  91. // https://html.spec.whatwg.org/multipage/webappapis.html#outstanding-rejected-promises-weak-set
  92. // 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.
  93. Vector<JS::Promise*> m_outstanding_rejected_promises_weak_set;
  94. // https://html.spec.whatwg.org/multipage/webappapis.html#about-to-be-notified-rejected-promises-list
  95. Vector<JS::Handle<JS::Promise>> m_about_to_be_notified_rejected_promises_list;
  96. };
  97. EnvironmentSettingsObject& incumbent_settings_object();
  98. JS::Realm& incumbent_realm();
  99. JS::Object& incumbent_global_object();
  100. EnvironmentSettingsObject& current_settings_object();
  101. JS::Object& current_global_object();
  102. JS::Realm& relevant_realm(JS::Object const&);
  103. EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
  104. EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
  105. JS::Object& relevant_global_object(JS::Object const&);
  106. JS::Realm& entry_realm();
  107. EnvironmentSettingsObject& entry_settings_object();
  108. JS::Object& entry_global_object();
  109. [[nodiscard]] bool is_secure_context(Environment const&);
  110. [[nodiscard]] bool is_non_secure_context(Environment const&);
  111. }