EventLoop.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/WeakPtr.h>
  10. #include <LibCore/Forward.h>
  11. #include <LibGC/Ptr.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibWeb/HTML/EventLoop/TaskQueue.h>
  14. #include <LibWeb/HighResolutionTime/DOMHighResTimeStamp.h>
  15. namespace Web::HTML {
  16. class EventLoop : public JS::Cell {
  17. GC_CELL(EventLoop, JS::Cell);
  18. GC_DECLARE_ALLOCATOR(EventLoop);
  19. struct PauseHandle {
  20. PauseHandle(EventLoop&, JS::Object const& global, HighResolutionTime::DOMHighResTimeStamp);
  21. ~PauseHandle();
  22. AK_MAKE_NONCOPYABLE(PauseHandle);
  23. AK_MAKE_NONMOVABLE(PauseHandle);
  24. GC::Ref<EventLoop> event_loop;
  25. GC::Ref<JS::Object const> global;
  26. HighResolutionTime::DOMHighResTimeStamp const time_before_pause;
  27. };
  28. public:
  29. enum class Type {
  30. // https://html.spec.whatwg.org/multipage/webappapis.html#window-event-loop
  31. Window,
  32. // https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop
  33. Worker,
  34. // https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
  35. Worklet,
  36. };
  37. virtual ~EventLoop() override;
  38. Type type() const { return m_type; }
  39. TaskQueue& task_queue() { return *m_task_queue; }
  40. TaskQueue const& task_queue() const { return *m_task_queue; }
  41. TaskQueue& microtask_queue() { return *m_microtask_queue; }
  42. TaskQueue const& microtask_queue() const { return *m_microtask_queue; }
  43. void spin_until(GC::Ref<GC::Function<bool()>> goal_condition);
  44. void spin_processing_tasks_with_source_until(Task::Source, GC::Ref<GC::Function<bool()>> goal_condition);
  45. void process();
  46. void queue_task_to_update_the_rendering();
  47. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  48. size_t termination_nesting_level() const { return m_termination_nesting_level; }
  49. void increment_termination_nesting_level() { ++m_termination_nesting_level; }
  50. void decrement_termination_nesting_level() { --m_termination_nesting_level; }
  51. Task const* currently_running_task() const { return m_currently_running_task; }
  52. void schedule();
  53. void perform_a_microtask_checkpoint();
  54. void register_document(Badge<DOM::Document>, DOM::Document&);
  55. void unregister_document(Badge<DOM::Document>, DOM::Document&);
  56. Vector<GC::Root<DOM::Document>> documents_in_this_event_loop() const;
  57. [[nodiscard]] Vector<GC::Root<DOM::Document>> documents_in_this_event_loop_matching(Function<bool(DOM::Document&)> callback) const;
  58. Vector<GC::Root<HTML::Window>> same_loop_windows() const;
  59. void push_onto_backup_incumbent_realm_stack(JS::Realm&);
  60. void pop_backup_incumbent_realm_stack();
  61. JS::Realm& top_of_backup_incumbent_realm_stack();
  62. bool is_backup_incumbent_realm_stack_empty() const { return m_backup_incumbent_realm_stack.is_empty(); }
  63. void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  64. void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  65. double compute_deadline() const;
  66. [[nodiscard]] PauseHandle pause();
  67. void unpause(Badge<PauseHandle>, JS::Object const& global, HighResolutionTime::DOMHighResTimeStamp);
  68. bool execution_paused() const { return m_execution_paused; }
  69. private:
  70. explicit EventLoop(Type);
  71. virtual void visit_edges(Visitor&) override;
  72. void update_the_rendering();
  73. Type m_type { Type::Window };
  74. GC::Ptr<TaskQueue> m_task_queue;
  75. GC::Ptr<TaskQueue> m_microtask_queue;
  76. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  77. GC::Ptr<Task> m_currently_running_task { nullptr };
  78. // https://html.spec.whatwg.org/multipage/webappapis.html#last-render-opportunity-time
  79. double m_last_render_opportunity_time { 0 };
  80. // https://html.spec.whatwg.org/multipage/webappapis.html#last-idle-period-start-time
  81. double m_last_idle_period_start_time { 0 };
  82. GC::Ptr<Platform::Timer> m_system_event_loop_timer;
  83. // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint
  84. bool m_performing_a_microtask_checkpoint { false };
  85. Vector<WeakPtr<DOM::Document>> m_documents;
  86. // Used to implement step 4 of "perform a microtask checkpoint".
  87. // NOTE: These are weak references! ESO registers and unregisters itself from the event loop manually.
  88. Vector<RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;
  89. // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
  90. // https://whatpr.org/html/9893/webappapis.html#backup-incumbent-realm-stack
  91. Vector<GC::Ref<JS::Realm>> m_backup_incumbent_realm_stack;
  92. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  93. size_t m_termination_nesting_level { 0 };
  94. bool m_execution_paused { false };
  95. bool m_skip_event_loop_processing_steps { false };
  96. bool m_is_running_rendering_task { false };
  97. GC::Ptr<GC::Function<void()>> m_rendering_task_function;
  98. };
  99. EventLoop& main_thread_event_loop();
  100. TaskID queue_a_task(HTML::Task::Source, GC::Ptr<EventLoop>, GC::Ptr<DOM::Document>, GC::Ref<GC::Function<void()>> steps);
  101. TaskID queue_global_task(HTML::Task::Source, JS::Object&, GC::Ref<GC::Function<void()>> steps);
  102. void queue_a_microtask(DOM::Document const*, GC::Ref<GC::Function<void()>> steps);
  103. void perform_a_microtask_checkpoint();
  104. }