SessionHistoryTraversalQueue.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibCore/Timer.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibJS/SafeFunction.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web::HTML {
  13. struct SessionHistoryTraversalQueueEntry {
  14. JS::SafeFunction<void()> steps;
  15. JS::GCPtr<HTML::Navigable> target_navigable;
  16. };
  17. // https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-traversal-queue
  18. class SessionHistoryTraversalQueue {
  19. public:
  20. SessionHistoryTraversalQueue()
  21. {
  22. m_timer = Core::Timer::create_single_shot(0, [this] {
  23. while (m_queue.size() > 0) {
  24. auto entry = m_queue.take_first();
  25. entry.steps();
  26. }
  27. }).release_value_but_fixme_should_propagate_errors();
  28. }
  29. void append(JS::SafeFunction<void()> steps)
  30. {
  31. m_queue.append({ move(steps), nullptr });
  32. if (!m_timer->is_active()) {
  33. m_timer->start();
  34. }
  35. }
  36. void append_sync(JS::SafeFunction<void()> steps, JS::GCPtr<Navigable> target_navigable)
  37. {
  38. m_queue.append({ move(steps), target_navigable });
  39. if (!m_timer->is_active()) {
  40. m_timer->start();
  41. }
  42. }
  43. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#sync-navigations-jump-queue
  44. SessionHistoryTraversalQueueEntry first_synchronous_navigation_steps_with_target_navigable_not_contained_in(Vector<JS::GCPtr<Navigable>> const& list)
  45. {
  46. auto index = m_queue.find_first_index_if([&list](auto const& entry) -> bool {
  47. return (entry.target_navigable != nullptr) && !list.contains_slow(entry.target_navigable);
  48. });
  49. return index.has_value() ? m_queue.take(*index) : SessionHistoryTraversalQueueEntry {};
  50. }
  51. void process()
  52. {
  53. while (m_queue.size() > 0) {
  54. auto entry = m_queue.take_first();
  55. entry.steps();
  56. }
  57. }
  58. private:
  59. Vector<SessionHistoryTraversalQueueEntry> m_queue;
  60. RefPtr<Core::Timer> m_timer;
  61. };
  62. }