History.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include <LibWeb/HTML/StructuredSerialize.h>
  10. namespace Web::HTML {
  11. JS::NonnullGCPtr<History> History::create(JS::Realm& realm, DOM::Document& document)
  12. {
  13. return realm.heap().allocate<History>(realm, realm, document);
  14. }
  15. History::History(JS::Realm& realm, DOM::Document& document)
  16. : PlatformObject(realm)
  17. , m_associated_document(document)
  18. {
  19. }
  20. History::~History() = default;
  21. void History::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::HistoryPrototype>(realm, "History"));
  25. }
  26. void History::visit_edges(Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(m_associated_document.ptr());
  30. }
  31. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  32. WebIDL::ExceptionOr<void> History::push_state(JS::Value data, DeprecatedString const&, DeprecatedString const& url)
  33. {
  34. // NOTE: The second parameter of this function is intentionally unused.
  35. return shared_history_push_replace_state(data, url, IsPush::Yes);
  36. }
  37. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  38. WebIDL::ExceptionOr<void> History::replace_state(JS::Value data, DeprecatedString const&, DeprecatedString const& url)
  39. {
  40. // NOTE: The second parameter of this function is intentionally unused.
  41. return shared_history_push_replace_state(data, url, IsPush::No);
  42. }
  43. // https://html.spec.whatwg.org/multipage/history.html#dom-history-length
  44. WebIDL::ExceptionOr<u64> History::length() const
  45. {
  46. // 1. If this's associated Document is not fully active, then throw a "SecurityError" DOMException.
  47. if (!m_associated_document->is_fully_active())
  48. return WebIDL::SecurityError::create(realm(), "Cannot perform length on a document that isn't fully active."sv);
  49. // 2. Return the number of entries in the top-level browsing context's joint session history.
  50. auto const* browsing_context = m_associated_document->browsing_context();
  51. // FIXME: We don't have the concept of "joint session history", this is an ad-hoc implementation.
  52. // See: https://html.spec.whatwg.org/multipage/history.html#joint-session-history
  53. return browsing_context->session_history().size();
  54. }
  55. // https://html.spec.whatwg.org/multipage/history.html#dom-history-go
  56. WebIDL::ExceptionOr<void> History::go(long delta = 0)
  57. {
  58. // 1. Let document be this's associated Document.
  59. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  60. if (!m_associated_document->is_fully_active())
  61. return WebIDL::SecurityError::create(realm(), "Cannot perform go on a document that isn't fully active."sv);
  62. // 3. If delta is 0, then act as if the location.reload() method was called, and return.
  63. auto* browsing_context = m_associated_document->browsing_context();
  64. auto current_entry_index = browsing_context->session_history_index();
  65. auto next_entry_index = current_entry_index + delta;
  66. auto const& sessions = browsing_context->session_history();
  67. if (next_entry_index < sessions.size()) {
  68. auto const& next_entry = sessions.at(next_entry_index);
  69. // FIXME: 4. Traverse the history by a delta with delta and document's browsing context.
  70. browsing_context->loader().load(next_entry->url, FrameLoader::Type::Reload);
  71. }
  72. return {};
  73. }
  74. // https://html.spec.whatwg.org/multipage/history.html#dom-history-back
  75. WebIDL::ExceptionOr<void> History::back()
  76. {
  77. // 1. Let document be this's associated Document.
  78. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  79. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  80. // 3. Traverse the history by a delta with −1 and document's browsing context.
  81. return go(-1);
  82. }
  83. // https://html.spec.whatwg.org/multipage/history.html#dom-history-forward
  84. WebIDL::ExceptionOr<void> History::forward()
  85. {
  86. // 1. Let document be this's associated Document.
  87. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  88. // NOTE: We already did this check in `go` method, so skip the fully active check here.
  89. // 3. Traverse the history by a delta with +1 and document's browsing context.
  90. return go(1);
  91. }
  92. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten
  93. static bool can_have_its_url_rewritten(DOM::Document const& document, AK::URL const& target_url)
  94. {
  95. // 1. Let documentURL be document's URL.
  96. auto document_url = document.url();
  97. // 2. If targetURL and documentURL differ in their scheme, username, password, host, or port components,
  98. // then return false.
  99. if (target_url.scheme() != document_url.scheme()
  100. || target_url.raw_username() != document_url.raw_username()
  101. || target_url.raw_password() != document_url.raw_password()
  102. || target_url.host() != document_url.host()
  103. || target_url.port() != document_url.port())
  104. return false;
  105. // 3. If targetURL's scheme is an HTTP(S) scheme, then return true.
  106. // (Differences in path, query, and fragment are allowed for http: and https: URLs.)
  107. if (target_url.scheme() == "http"sv || target_url.scheme() == "https"sv)
  108. return true;
  109. // 4. If targetURL's scheme is "file", and targetURL and documentURL differ in their path component,
  110. // then return false. (Differences in query and fragment are allowed for file: URLs.)
  111. // FIXME: Don't create temporary strings to compare paths
  112. auto target_url_path = target_url.serialize_path();
  113. auto document_url_path = document_url.serialize_path();
  114. if (target_url.scheme() == "file"sv
  115. && target_url_path != document_url_path)
  116. return false;
  117. // 5. If targetURL and documentURL differ in their path component or query components, then return false.
  118. // (Only differences in fragment are allowed for other types of URLs.)
  119. if (target_url_path != document_url_path
  120. || target_url.query() != document_url.query())
  121. return false;
  122. // 6. Return true.
  123. return true;
  124. }
  125. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  126. WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value value, DeprecatedString const& url, IsPush)
  127. {
  128. // 1. Let document be history's associated Document.
  129. auto& document = m_associated_document;
  130. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  131. if (!document->is_fully_active())
  132. return WebIDL::SecurityError::create(realm(), "Cannot perform pushState or replaceState on a document that isn't fully active."sv);
  133. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  134. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  135. // 4. Let serializedData be StructuredSerializeForStorage(data). Rethrow any exceptions.
  136. // FIXME: Actually rethrow exceptions here once we start using the serialized data.
  137. // Throwing here on data types we don't yet serialize will regress sites that use push/replaceState.
  138. [[maybe_unused]] auto serialized_data_or_error = structured_serialize_for_storage(vm(), value);
  139. // 5. Let newURL be document's URL.
  140. auto new_url = document->url();
  141. // 6. If url is not null or the empty string, then:
  142. if (!url.is_empty() && !url.is_null()) {
  143. // 1. Parse url, relative to the relevant settings object of history.
  144. auto parsed_url = relevant_settings_object(*this).parse_url(url);
  145. // 2. If that fails, then throw a "SecurityError" DOMException.
  146. if (!parsed_url.is_valid())
  147. return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"sv);
  148. // 3. Set newURL to the resulting URL record.
  149. new_url = parsed_url;
  150. // 4. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
  151. if (!can_have_its_url_rewritten(document, new_url))
  152. return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"sv);
  153. }
  154. // FIXME: 7. Let navigation be history's relevant global object's navigation API.
  155. // FIXME: 8. Let continue be the result of firing a push/replace/reload navigate event at navigation
  156. /// with navigationType set to historyHandling, isSameDocument set to true, destinationURL set to newURL,
  157. // and classicHistoryAPIState set to serializedData.
  158. // FIXME: 9. If continue is false, then return.
  159. // FIXME: 10. Run the URL and history update steps given document and newURL, with serializedData set to
  160. // serializedData and historyHandling set to historyHandling.
  161. dbgln("FIXME: Implement shared_history_push_replace_state.");
  162. return {};
  163. }
  164. }