WindowEnvironmentSettingsObject.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HostDefined.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  10. #include <LibWeb/HTML/Window.h>
  11. namespace Web::HTML {
  12. WindowEnvironmentSettingsObject::WindowEnvironmentSettingsObject(Window& window, NonnullOwnPtr<JS::ExecutionContext> execution_context)
  13. : EnvironmentSettingsObject(move(execution_context))
  14. , m_window(window)
  15. {
  16. }
  17. WindowEnvironmentSettingsObject::~WindowEnvironmentSettingsObject() = default;
  18. void WindowEnvironmentSettingsObject::visit_edges(JS::Cell::Visitor& visitor)
  19. {
  20. EnvironmentSettingsObject::visit_edges(visitor);
  21. visitor.visit(m_window.ptr());
  22. }
  23. // https://html.spec.whatwg.org/multipage/window-object.html#set-up-a-window-environment-settings-object
  24. WebIDL::ExceptionOr<void> WindowEnvironmentSettingsObject::setup(AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext> execution_context, Optional<Environment> reserved_environment, AK::URL top_level_creation_url, Origin top_level_origin)
  25. {
  26. // 1. Let realm be the value of execution context's Realm component.
  27. auto realm = execution_context->realm;
  28. VERIFY(realm);
  29. // 2. Let window be realm's global object.
  30. auto& window = verify_cast<HTML::Window>(realm->global_object());
  31. // 3. Let settings object be a new environment settings object whose algorithms are defined as follows:
  32. // NOTE: See the functions defined for this class.
  33. auto settings_object = MUST_OR_THROW_OOM(realm->heap().allocate<WindowEnvironmentSettingsObject>(*realm, window, move(execution_context)));
  34. // 4. If reservedEnvironment is non-null, then:
  35. if (reserved_environment.has_value()) {
  36. // FIXME: 1. Set settings object's id to reservedEnvironment's id,
  37. // target browsing context to reservedEnvironment's target browsing context,
  38. // and active service worker to reservedEnvironment's active service worker.
  39. settings_object->id = reserved_environment->id;
  40. settings_object->target_browsing_context = reserved_environment->target_browsing_context;
  41. // 2. Set reservedEnvironment's id to the empty string.
  42. reserved_environment->id = DeprecatedString::empty();
  43. }
  44. // 5. Otherwise, ...
  45. else {
  46. // FIXME: ...set settings object's id to a new unique opaque string,
  47. // settings object's target browsing context to null,
  48. // and settings object's active service worker to null.
  49. static i64 next_id = 1;
  50. settings_object->id = DeprecatedString::number(next_id++);
  51. settings_object->target_browsing_context = nullptr;
  52. }
  53. // 6. Set settings object's creation URL to creationURL,
  54. // settings object's top-level creation URL to topLevelCreationURL,
  55. // and settings object's top-level origin to topLevelOrigin.
  56. settings_object->creation_url = creation_url;
  57. settings_object->top_level_creation_url = top_level_creation_url;
  58. settings_object->top_level_origin = top_level_origin;
  59. // 7. Set realm's [[HostDefined]] field to settings object.
  60. // Non-Standard: We store the ESO next to the web intrinsics in a custom HostDefined object
  61. auto intrinsics = MUST_OR_THROW_OOM(realm->heap().allocate<Bindings::Intrinsics>(*realm, *realm));
  62. auto host_defined = make<Bindings::HostDefined>(settings_object, intrinsics);
  63. realm->set_host_defined(move(host_defined));
  64. // Non-Standard: We cannot fully initialize window object until *after* the we set up
  65. // the realm's [[HostDefined]] internal slot as the internal slot contains the web platform intrinsics
  66. TRY(window.initialize_web_interfaces({}));
  67. return {};
  68. }
  69. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:responsible-document
  70. JS::GCPtr<DOM::Document> WindowEnvironmentSettingsObject::responsible_document()
  71. {
  72. // Return window's associated Document.
  73. return m_window->associated_document();
  74. }
  75. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-url-character-encoding
  76. DeprecatedString WindowEnvironmentSettingsObject::api_url_character_encoding()
  77. {
  78. // Return the current character encoding of window's associated Document.
  79. return m_window->associated_document().encoding_or_default();
  80. }
  81. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-base-url
  82. AK::URL WindowEnvironmentSettingsObject::api_base_url()
  83. {
  84. // Return the current base URL of window's associated Document.
  85. return m_window->associated_document().base_url();
  86. }
  87. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-origin
  88. Origin WindowEnvironmentSettingsObject::origin()
  89. {
  90. // Return the origin of window's associated Document.
  91. return m_window->associated_document().origin();
  92. }
  93. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-policy-container
  94. PolicyContainer WindowEnvironmentSettingsObject::policy_container()
  95. {
  96. // Return the policy container of window's associated Document.
  97. return m_window->associated_document().policy_container();
  98. }
  99. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-cross-origin-isolated-capability
  100. CanUseCrossOriginIsolatedAPIs WindowEnvironmentSettingsObject::cross_origin_isolated_capability()
  101. {
  102. // FIXME: Return true if both of the following hold, and false otherwise:
  103. // 1. realm's agent cluster's cross-origin-isolation mode is "concrete", and
  104. // 2. window's associated Document is allowed to use the "cross-origin-isolated" feature.
  105. return CanUseCrossOriginIsolatedAPIs::Yes;
  106. }
  107. }