Environments.h 4.5 KB

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