History.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HistoryPrototype.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/History.h>
  9. namespace Web::HTML {
  10. JS::NonnullGCPtr<History> History::create(HTML::Window& window, DOM::Document& document)
  11. {
  12. return *window.heap().allocate<History>(window.realm(), window, document);
  13. }
  14. History::History(HTML::Window& window, DOM::Document& document)
  15. : PlatformObject(window.realm())
  16. , m_associated_document(document)
  17. {
  18. set_prototype(&window.ensure_web_prototype<Bindings::HistoryPrototype>("History"));
  19. }
  20. History::~History() = default;
  21. void History::visit_edges(Cell::Visitor& visitor)
  22. {
  23. Base::visit_edges(visitor);
  24. visitor.visit(m_associated_document.ptr());
  25. }
  26. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  27. DOM::ExceptionOr<void> History::push_state(JS::Value data, String const&, String const& url)
  28. {
  29. // NOTE: The second parameter of this function is intentionally unused.
  30. return shared_history_push_replace_state(data, url, IsPush::Yes);
  31. }
  32. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  33. DOM::ExceptionOr<void> History::replace_state(JS::Value data, String const&, String const& url)
  34. {
  35. // NOTE: The second parameter of this function is intentionally unused.
  36. return shared_history_push_replace_state(data, url, IsPush::No);
  37. }
  38. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  39. DOM::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value, String const&, IsPush)
  40. {
  41. // 1. Let document be history's associated Document. (NOTE: Not necessary)
  42. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  43. if (!m_associated_document->is_fully_active())
  44. return DOM::SecurityError::create("Cannot perform pushState or replaceState on a document that isn't fully active.");
  45. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  46. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  47. dbgln("FIXME: Implement shared_history_push_replace_state.");
  48. return {};
  49. // FIXME: Add the rest of the spec steps once they're added.
  50. }
  51. }