Environments.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/BrowsingContext.h>
  14. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  15. #include <LibWeb/HTML/Origin.h>
  16. namespace Web::HTML {
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#environment
  18. struct Environment {
  19. // FIXME: An id https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-id
  20. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-creation-url
  21. AK::URL creation_url;
  22. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url
  23. AK::URL top_level_creation_url;
  24. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin
  25. Origin top_level_origin;
  26. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  27. RefPtr<BrowsingContext> target_browsing_context;
  28. // FIXME: An active service worker https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
  29. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-execution-ready-flag
  30. bool execution_ready { false };
  31. };
  32. enum class CanUseCrossOriginIsolatedAPIs {
  33. No,
  34. Yes,
  35. };
  36. enum class RunScriptDecision {
  37. Run,
  38. DoNotRun,
  39. };
  40. // https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
  41. struct EnvironmentSettingsObject
  42. : public Environment
  43. , public JS::Realm::HostDefined {
  44. virtual ~EnvironmentSettingsObject() override;
  45. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  46. JS::ExecutionContext& realm_execution_context();
  47. // FIXME: A module map https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-module-map
  48. // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
  49. virtual RefPtr<DOM::Document> responsible_document() = 0;
  50. // https://html.spec.whatwg.org/multipage/webappapis.html#api-url-character-encoding
  51. virtual String api_url_character_encoding() = 0;
  52. // https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
  53. virtual AK::URL api_base_url() = 0;
  54. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
  55. virtual Origin origin() = 0;
  56. // FIXME: A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
  57. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
  58. virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() = 0;
  59. JS::Realm& realm();
  60. JS::GlobalObject& global_object();
  61. EventLoop& responsible_event_loop();
  62. RunScriptDecision can_run_script();
  63. void prepare_to_run_script();
  64. void clean_up_after_running_script();
  65. void prepare_to_run_callback();
  66. void clean_up_after_running_callback();
  67. void push_onto_outstanding_rejected_promises_weak_set(JS::Promise*);
  68. // Returns true if removed, false otherwise.
  69. bool remove_from_outstanding_rejected_promises_weak_set(JS::Promise*);
  70. void push_onto_about_to_be_notified_rejected_promises_list(JS::Handle<JS::Promise>);
  71. // Returns true if removed, false otherwise.
  72. bool remove_from_about_to_be_notified_rejected_promises_list(JS::Promise*);
  73. void notify_about_rejected_promises(Badge<EventLoop>);
  74. bool is_scripting_enabled() const;
  75. bool is_scripting_disabled() const;
  76. protected:
  77. explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);
  78. private:
  79. NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;
  80. EventLoop* m_responsible_event_loop { nullptr };
  81. // https://html.spec.whatwg.org/multipage/webappapis.html#outstanding-rejected-promises-weak-set
  82. // 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.
  83. Vector<JS::Promise*> m_outstanding_rejected_promises_weak_set;
  84. // https://html.spec.whatwg.org/multipage/webappapis.html#about-to-be-notified-rejected-promises-list
  85. Vector<JS::Handle<JS::Promise>> m_about_to_be_notified_rejected_promises_list;
  86. };
  87. EnvironmentSettingsObject& incumbent_settings_object();
  88. JS::Realm& incumbent_realm();
  89. JS::GlobalObject& incumbent_global_object();
  90. EnvironmentSettingsObject& current_settings_object();
  91. JS::GlobalObject& current_global_object();
  92. JS::Realm& relevant_realm(JS::Object const&);
  93. EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
  94. EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
  95. JS::GlobalObject& relevant_global_object(JS::Object const&);
  96. }