History.cpp 5.3 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/Intrinsics.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/History.h>
  9. namespace Web::HTML {
  10. WebIDL::ExceptionOr<JS::NonnullGCPtr<History>> History::create(JS::Realm& realm, DOM::Document& document)
  11. {
  12. return MUST_OR_THROW_OOM(realm.heap().allocate<History>(realm, realm, document));
  13. }
  14. History::History(JS::Realm& realm, DOM::Document& document)
  15. : PlatformObject(realm)
  16. , m_associated_document(document)
  17. {
  18. }
  19. History::~History() = default;
  20. void History::initialize(JS::Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. set_prototype(&Bindings::ensure_web_prototype<Bindings::HistoryPrototype>(realm, "History"));
  24. }
  25. void History::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_associated_document.ptr());
  29. }
  30. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  31. WebIDL::ExceptionOr<void> History::push_state(JS::Value data, DeprecatedString const&, DeprecatedString const& url)
  32. {
  33. // NOTE: The second parameter of this function is intentionally unused.
  34. return shared_history_push_replace_state(data, url, IsPush::Yes);
  35. }
  36. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  37. WebIDL::ExceptionOr<void> History::replace_state(JS::Value data, DeprecatedString const&, DeprecatedString const& url)
  38. {
  39. // NOTE: The second parameter of this function is intentionally unused.
  40. return shared_history_push_replace_state(data, url, IsPush::No);
  41. }
  42. // https://html.spec.whatwg.org/multipage/history.html#dom-history-length
  43. WebIDL::ExceptionOr<u64> History::length() const
  44. {
  45. // 1. If this's associated Document is not fully active, then throw a "SecurityError" DOMException.
  46. if (!m_associated_document->is_fully_active())
  47. return WebIDL::SecurityError::create(realm(), "Cannot perform length on a document that isn't fully active."sv);
  48. // 2. Return the number of entries in the top-level browsing context's joint session history.
  49. auto const* browsing_context = m_associated_document->browsing_context();
  50. // FIXME: We don't have the concept of "joint session history", this is an ad-hoc implementation.
  51. // See: https://html.spec.whatwg.org/multipage/history.html#joint-session-history
  52. return browsing_context->session_history().size();
  53. }
  54. // https://html.spec.whatwg.org/multipage/history.html#dom-history-go
  55. WebIDL::ExceptionOr<void> History::go(long delta = 0)
  56. {
  57. // 1. Let document be this's associated Document.
  58. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  59. if (!m_associated_document->is_fully_active())
  60. return WebIDL::SecurityError::create(realm(), "Cannot perform go on a document that isn't fully active."sv);
  61. // 3. If delta is 0, then act as if the location.reload() method was called, and return.
  62. auto* browsing_context = m_associated_document->browsing_context();
  63. auto current_entry_index = browsing_context->session_history_index();
  64. auto next_entry_index = current_entry_index + delta;
  65. auto const& sessions = browsing_context->session_history();
  66. if (next_entry_index < sessions.size()) {
  67. auto const& next_entry = sessions.at(next_entry_index);
  68. // FIXME: 4. Traverse the history by a delta with delta and document's browsing context.
  69. browsing_context->loader().load(next_entry->url, FrameLoader::Type::Reload);
  70. }
  71. return {};
  72. }
  73. // https://html.spec.whatwg.org/multipage/history.html#dom-history-back
  74. WebIDL::ExceptionOr<void> History::back()
  75. {
  76. // 1. Let document be this's associated Document.
  77. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  78. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  79. // 3. Traverse the history by a delta with −1 and document's browsing context.
  80. return go(-1);
  81. }
  82. // https://html.spec.whatwg.org/multipage/history.html#dom-history-forward
  83. WebIDL::ExceptionOr<void> History::forward()
  84. {
  85. // 1. Let document be this's associated Document.
  86. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  87. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  88. // 3. Traverse the history by a delta with +1 and document's browsing context.
  89. return go(1);
  90. }
  91. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  92. WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value, DeprecatedString const&, IsPush)
  93. {
  94. // 1. Let document be history's associated Document. (NOTE: Not necessary)
  95. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  96. if (!m_associated_document->is_fully_active())
  97. return WebIDL::SecurityError::create(realm(), "Cannot perform pushState or replaceState on a document that isn't fully active."sv);
  98. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  99. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  100. dbgln("FIXME: Implement shared_history_push_replace_state.");
  101. return {};
  102. // FIXME: Add the rest of the spec steps once they're added.
  103. }
  104. }