Environments.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <LibJS/Forward.h>
  9. #include <LibURL/Origin.h>
  10. #include <LibURL/URL.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  13. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  14. #include <LibWeb/HTML/Scripting/SerializedEnvironmentSettingsObject.h>
  15. namespace Web::HTML {
  16. // https://html.spec.whatwg.org/multipage/webappapis.html#environment
  17. struct Environment : public JS::Cell {
  18. JS_CELL(Environment, JS::Cell);
  19. public:
  20. virtual ~Environment() override;
  21. // An id https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-id
  22. String id;
  23. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-creation-url
  24. URL::URL creation_url;
  25. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url
  26. URL::URL top_level_creation_url;
  27. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin
  28. URL::Origin top_level_origin;
  29. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  30. JS::GCPtr<BrowsingContext> target_browsing_context;
  31. // FIXME: An active service worker https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
  32. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-execution-ready-flag
  33. bool execution_ready { false };
  34. protected:
  35. virtual void visit_edges(Cell::Visitor&) override;
  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 : public Environment {
  43. JS_CELL(EnvironmentSettingsObject, Environment);
  44. public:
  45. virtual ~EnvironmentSettingsObject() override;
  46. virtual void initialize(JS::Realm&) override;
  47. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
  48. JS::ExecutionContext& realm_execution_context();
  49. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-module-map
  50. ModuleMap& module_map();
  51. // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
  52. virtual JS::GCPtr<DOM::Document> responsible_document() = 0;
  53. // https://html.spec.whatwg.org/multipage/webappapis.html#api-url-character-encoding
  54. virtual String api_url_character_encoding() = 0;
  55. // https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
  56. virtual URL::URL api_base_url() = 0;
  57. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
  58. virtual URL::Origin origin() = 0;
  59. // A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
  60. virtual PolicyContainer policy_container() = 0;
  61. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
  62. virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() = 0;
  63. URL::URL parse_url(StringView);
  64. JS::Realm& realm();
  65. JS::Object& global_object();
  66. EventLoop& responsible_event_loop();
  67. // https://fetch.spec.whatwg.org/#concept-fetch-group
  68. Vector<JS::NonnullGCPtr<Fetch::Infrastructure::FetchRecord>>& fetch_group() { return m_fetch_group; }
  69. RunScriptDecision can_run_script();
  70. void prepare_to_run_script();
  71. void clean_up_after_running_script();
  72. void prepare_to_run_callback();
  73. void clean_up_after_running_callback();
  74. bool is_scripting_enabled() const;
  75. bool is_scripting_disabled() const;
  76. bool module_type_allowed(StringView module_type) const;
  77. void disallow_further_import_maps();
  78. SerializedEnvironmentSettingsObject serialize();
  79. JS::NonnullGCPtr<StorageAPI::StorageManager> storage_manager();
  80. [[nodiscard]] bool discarded() const { return m_discarded; }
  81. void set_discarded(bool b) { m_discarded = b; }
  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. JS::GCPtr<EventLoop> m_responsible_event_loop;
  89. // https://fetch.spec.whatwg.org/#concept-fetch-record
  90. // A fetch group holds an ordered list of fetch records
  91. Vector<JS::NonnullGCPtr<Fetch::Infrastructure::FetchRecord>> m_fetch_group;
  92. // https://storage.spec.whatwg.org/#api
  93. // Each environment settings object has an associated StorageManager object.
  94. JS::GCPtr<StorageAPI::StorageManager> m_storage_manager;
  95. // https://w3c.github.io/ServiceWorker/#service-worker-client-discarded-flag
  96. // A service worker client has an associated discarded flag. It is initially unset.
  97. bool m_discarded { false };
  98. };
  99. EnvironmentSettingsObject& incumbent_settings_object();
  100. JS::Realm& incumbent_realm();
  101. JS::Object& incumbent_global_object();
  102. JS::Realm& current_principal_realm();
  103. EnvironmentSettingsObject& current_principal_settings_object();
  104. JS::Object& current_global_object();
  105. JS::Realm& relevant_realm(JS::Object const&);
  106. EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
  107. EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
  108. JS::Object& relevant_global_object(JS::Object const&);
  109. JS::Realm& entry_realm();
  110. EnvironmentSettingsObject& entry_settings_object();
  111. JS::Object& entry_global_object();
  112. JS::VM& relevant_agent(JS::Object const&);
  113. [[nodiscard]] bool is_secure_context(Environment const&);
  114. [[nodiscard]] bool is_non_secure_context(Environment const&);
  115. }