WindowEnvironmentSettingsObject.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  8. #include <LibWeb/HTML/Window.h>
  9. namespace Web::HTML {
  10. WindowEnvironmentSettingsObject::WindowEnvironmentSettingsObject(Window& window, NonnullOwnPtr<JS::ExecutionContext> execution_context)
  11. : EnvironmentSettingsObject(move(execution_context))
  12. , m_window(window)
  13. {
  14. }
  15. WindowEnvironmentSettingsObject::~WindowEnvironmentSettingsObject() = default;
  16. void WindowEnvironmentSettingsObject::visit_edges(JS::Cell::Visitor& visitor)
  17. {
  18. EnvironmentSettingsObject::visit_edges(visitor);
  19. visitor.visit(m_window.ptr());
  20. }
  21. // https://html.spec.whatwg.org/multipage/window-object.html#set-up-a-window-environment-settings-object
  22. 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)
  23. {
  24. // 1. Let realm be the value of execution context's Realm component.
  25. auto* realm = execution_context->realm;
  26. VERIFY(realm);
  27. // 2. Let window be realm's global object.
  28. // NOTE: We want to store the Window impl rather than the WindowObject.
  29. auto& window = verify_cast<HTML::Window>(realm->global_object()).impl();
  30. // 3. Let settings object be a new environment settings object whose algorithms are defined as follows:
  31. // NOTE: See the functions defined for this class.
  32. auto settings_object = adopt_own(*new WindowEnvironmentSettingsObject(window, move(execution_context)));
  33. // 4. If reservedEnvironment is non-null, then:
  34. if (reserved_environment.has_value()) {
  35. // FIXME: 1. Set settings object's id to reservedEnvironment's id,
  36. // target browsing context to reservedEnvironment's target browsing context,
  37. // and active service worker to reservedEnvironment's active service worker.
  38. settings_object->target_browsing_context = reserved_environment->target_browsing_context;
  39. // FIXME: 2. Set reservedEnvironment's id to the empty string.
  40. }
  41. // 5. Otherwise, ...
  42. else {
  43. // FIXME: ...set settings object's id to a new unique opaque string,
  44. // settings object's target browsing context to null,
  45. // and settings object's active service worker to null.
  46. settings_object->target_browsing_context = nullptr;
  47. }
  48. // 6. Set settings object's creation URL to creationURL,
  49. // settings object's top-level creation URL to topLevelCreationURL,
  50. // and settings object's top-level origin to topLevelOrigin.
  51. settings_object->creation_url = creation_url;
  52. settings_object->top_level_creation_url = top_level_creation_url;
  53. settings_object->top_level_origin = top_level_origin;
  54. // 7. Set realm's [[HostDefined]] field to settings object.
  55. realm->set_host_defined(move(settings_object));
  56. }
  57. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:responsible-document
  58. JS::GCPtr<DOM::Document> WindowEnvironmentSettingsObject::responsible_document()
  59. {
  60. // Return window's associated Document.
  61. return m_window->associated_document();
  62. }
  63. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-url-character-encoding
  64. String WindowEnvironmentSettingsObject::api_url_character_encoding()
  65. {
  66. // Return the current character encoding of window's associated Document.
  67. return m_window->associated_document().encoding_or_default();
  68. }
  69. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-base-url
  70. AK::URL WindowEnvironmentSettingsObject::api_base_url()
  71. {
  72. // Return the current base URL of window's associated Document.
  73. return m_window->associated_document().base_url();
  74. }
  75. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-origin
  76. Origin WindowEnvironmentSettingsObject::origin()
  77. {
  78. // Return the origin of window's associated Document.
  79. return m_window->associated_document().origin();
  80. }
  81. // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-cross-origin-isolated-capability
  82. CanUseCrossOriginIsolatedAPIs WindowEnvironmentSettingsObject::cross_origin_isolated_capability()
  83. {
  84. // FIXME: Return true if both of the following hold, and false otherwise:
  85. // 1. realm's agent cluster's cross-origin-isolation mode is "concrete", and
  86. // 2. window's associated Document is allowed to use the "cross-origin-isolated" feature.
  87. TODO();
  88. }
  89. }