EventLoop.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/WeakPtr.h>
  9. #include <LibCore/Forward.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibWeb/HTML/EventLoop/TaskQueue.h>
  12. namespace Web::HTML {
  13. class EventLoop : public JS::Cell {
  14. JS_CELL(EventLoop, JS::Cell);
  15. JS_DECLARE_ALLOCATOR(EventLoop);
  16. public:
  17. enum class Type {
  18. // https://html.spec.whatwg.org/multipage/webappapis.html#window-event-loop
  19. Window,
  20. // https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop
  21. Worker,
  22. // https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
  23. Worklet,
  24. };
  25. virtual ~EventLoop() override;
  26. Type type() const { return m_type; }
  27. TaskQueue& task_queue() { return *m_task_queue; }
  28. TaskQueue const& task_queue() const { return *m_task_queue; }
  29. TaskQueue& microtask_queue() { return *m_microtask_queue; }
  30. TaskQueue const& microtask_queue() const { return *m_microtask_queue; }
  31. void spin_until(JS::NonnullGCPtr<JS::HeapFunction<bool()>> goal_condition);
  32. void spin_processing_tasks_with_source_until(Task::Source, JS::NonnullGCPtr<JS::HeapFunction<bool()>> goal_condition);
  33. void process();
  34. void queue_task_to_update_the_rendering();
  35. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  36. size_t termination_nesting_level() const { return m_termination_nesting_level; }
  37. void increment_termination_nesting_level() { ++m_termination_nesting_level; }
  38. void decrement_termination_nesting_level() { --m_termination_nesting_level; }
  39. Task const* currently_running_task() const { return m_currently_running_task; }
  40. void schedule();
  41. void perform_a_microtask_checkpoint();
  42. void register_document(Badge<DOM::Document>, DOM::Document&);
  43. void unregister_document(Badge<DOM::Document>, DOM::Document&);
  44. Vector<JS::Handle<DOM::Document>> documents_in_this_event_loop() const;
  45. Vector<JS::Handle<HTML::Window>> same_loop_windows() const;
  46. void push_onto_backup_incumbent_realm_stack(JS::Realm&);
  47. void pop_backup_incumbent_realm_stack();
  48. JS::Realm& top_of_backup_incumbent_realm_stack();
  49. bool is_backup_incumbent_realm_stack_empty() const { return m_backup_incumbent_realm_stack.is_empty(); }
  50. void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  51. void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  52. double compute_deadline() const;
  53. // https://html.spec.whatwg.org/multipage/webappapis.html#pause
  54. void set_execution_paused(bool execution_paused) { m_execution_paused = execution_paused; }
  55. bool execution_paused() const { return m_execution_paused; }
  56. private:
  57. explicit EventLoop(Type);
  58. virtual void visit_edges(Visitor&) override;
  59. void update_the_rendering();
  60. Type m_type { Type::Window };
  61. JS::GCPtr<TaskQueue> m_task_queue;
  62. JS::GCPtr<TaskQueue> m_microtask_queue;
  63. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  64. JS::GCPtr<Task> m_currently_running_task { nullptr };
  65. // https://html.spec.whatwg.org/multipage/webappapis.html#last-render-opportunity-time
  66. double m_last_render_opportunity_time { 0 };
  67. // https://html.spec.whatwg.org/multipage/webappapis.html#last-idle-period-start-time
  68. double m_last_idle_period_start_time { 0 };
  69. JS::GCPtr<Platform::Timer> m_system_event_loop_timer;
  70. // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint
  71. bool m_performing_a_microtask_checkpoint { false };
  72. Vector<WeakPtr<DOM::Document>> m_documents;
  73. // Used to implement step 4 of "perform a microtask checkpoint".
  74. // NOTE: These are weak references! ESO registers and unregisters itself from the event loop manually.
  75. Vector<RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;
  76. // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
  77. // https://whatpr.org/html/9893/webappapis.html#backup-incumbent-realm-stack
  78. Vector<JS::NonnullGCPtr<JS::Realm>> m_backup_incumbent_realm_stack;
  79. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  80. size_t m_termination_nesting_level { 0 };
  81. bool m_execution_paused { false };
  82. bool m_skip_event_loop_processing_steps { false };
  83. bool m_is_running_rendering_task { false };
  84. JS::GCPtr<JS::HeapFunction<void()>> m_rendering_task_function;
  85. };
  86. EventLoop& main_thread_event_loop();
  87. TaskID queue_a_task(HTML::Task::Source, JS::GCPtr<EventLoop>, JS::GCPtr<DOM::Document>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  88. TaskID queue_global_task(HTML::Task::Source, JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  89. void queue_a_microtask(DOM::Document const*, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  90. void perform_a_microtask_checkpoint();
  91. }