Environments.h 5.0 KB

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