NavigationHistoryEntry.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/NavigationHistoryEntryPrototype.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/DocumentState.h>
  12. #include <LibWeb/HTML/Navigation.h>
  13. #include <LibWeb/HTML/NavigationHistoryEntry.h>
  14. #include <LibWeb/HTML/Window.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(NavigationHistoryEntry);
  17. JS::NonnullGCPtr<NavigationHistoryEntry> NavigationHistoryEntry::create(JS::Realm& realm, JS::NonnullGCPtr<SessionHistoryEntry> she)
  18. {
  19. return realm.heap().allocate<NavigationHistoryEntry>(realm, realm, she);
  20. }
  21. NavigationHistoryEntry::NavigationHistoryEntry(JS::Realm& realm, JS::NonnullGCPtr<SessionHistoryEntry> she)
  22. : DOM::EventTarget(realm)
  23. , m_session_history_entry(she)
  24. {
  25. }
  26. NavigationHistoryEntry::~NavigationHistoryEntry() = default;
  27. void NavigationHistoryEntry::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(NavigationHistoryEntry);
  31. }
  32. void NavigationHistoryEntry::visit_edges(JS::Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_session_history_entry);
  36. }
  37. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-url
  38. WebIDL::ExceptionOr<Optional<String>> NavigationHistoryEntry::url() const
  39. {
  40. // The url getter steps are:
  41. // 1. Let document be this's relevant global object's associated Document.
  42. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  43. // 2. If document is not fully active, then return the empty string.
  44. if (!document.is_fully_active())
  45. return String {};
  46. // 3. Let she be this's session history entry.
  47. auto const& she = this->m_session_history_entry;
  48. // 4. If she's document does not equal document, and she's document state's request referrer policy
  49. // is "no-referrer" or "origin", then return null.
  50. if ((she->document_state->document() != &document)
  51. && (she->document_state->request_referrer_policy() == ReferrerPolicy::ReferrerPolicy::NoReferrer
  52. || she->document_state->request_referrer_policy() == ReferrerPolicy::ReferrerPolicy::Origin))
  53. return OptionalNone {};
  54. // 5. Return she's URL, serialized.
  55. return TRY_OR_THROW_OOM(vm(), String::from_byte_string(she->url.serialize()));
  56. }
  57. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-key
  58. String NavigationHistoryEntry::key() const
  59. {
  60. // The key of a NavigationHistoryEntry nhe is given by the return value of the following algorithm:
  61. // 1. If nhe's relevant global object's associated Document is not fully active, then return the empty string.
  62. auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  63. if (!associated_document.is_fully_active())
  64. return {};
  65. // 2. Return nhe's session history entry's navigation API key.
  66. return m_session_history_entry->navigation_api_key;
  67. }
  68. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-id
  69. String NavigationHistoryEntry::id() const
  70. {
  71. // The ID of a NavigationHistoryEntry nhe is given by the return value of the following algorithm:
  72. // 1. If nhe's relevant global object's associated Document is not fully active, then return the empty string.
  73. auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  74. if (!associated_document.is_fully_active())
  75. return {};
  76. // 2. Return nhe's session history entry's navigation API ID.
  77. return m_session_history_entry->navigation_api_id;
  78. }
  79. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-index
  80. i64 NavigationHistoryEntry::index() const
  81. {
  82. // The index of a NavigationHistoryEntry nhe is given by the return value of the following algorithm:
  83. // 1. If nhe's relevant global object's associated Document is not fully active, then return −1.
  84. auto& this_relevant_global_object = verify_cast<HTML::Window>(relevant_global_object(*this));
  85. if (!this_relevant_global_object.associated_document().is_fully_active())
  86. return -1;
  87. // 2. Return the result of getting the navigation API entry index of this's session history entry
  88. // within this's relevant global object's navigation API.
  89. return this_relevant_global_object.navigation()->get_the_navigation_api_entry_index(*m_session_history_entry);
  90. }
  91. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-samedocument
  92. bool NavigationHistoryEntry::same_document() const
  93. {
  94. // The sameDocument getter steps are:
  95. // 1. Let document be this's relevant global object's associated Document.
  96. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  97. // 2. If document is not fully active, then return false.
  98. if (!document.is_fully_active())
  99. return false;
  100. // 3. Return true if this's session history entry's document equals document, and false otherwise.
  101. return m_session_history_entry->document_state->document() == &document;
  102. }
  103. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-getstate
  104. WebIDL::ExceptionOr<JS::Value> NavigationHistoryEntry::get_state()
  105. {
  106. // The getState() method steps are:
  107. // 1. If this's relevant global object's associated Document is not fully active, then return undefined.
  108. auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  109. if (!associated_document.is_fully_active())
  110. return JS::js_undefined();
  111. // 2. Return StructuredDeserialize(this's session history entry's navigation API state). Rethrow any exceptions.
  112. // NOTE: This can in theory throw an exception, if attempting to deserialize a large ArrayBuffer
  113. // when not enough memory is available.
  114. return structured_deserialize(vm(), m_session_history_entry->navigation_api_state, realm(), {});
  115. }
  116. void NavigationHistoryEntry::set_ondispose(WebIDL::CallbackType* event_handler)
  117. {
  118. set_event_handler_attribute(HTML::EventNames::dispose, event_handler);
  119. }
  120. WebIDL::CallbackType* NavigationHistoryEntry::ondispose()
  121. {
  122. return event_handler_attribute(HTML::EventNames::dispose);
  123. }
  124. }