EventLoop.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.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 <LibJS/SafeFunction.h>
  12. #include <LibWeb/HTML/EventLoop/TaskQueue.h>
  13. namespace Web::HTML {
  14. class EventLoop : public JS::Cell {
  15. JS_CELL(EventLoop, Cell);
  16. JS_DECLARE_ALLOCATOR(EventLoop);
  17. public:
  18. enum class Type {
  19. // https://html.spec.whatwg.org/multipage/webappapis.html#window-event-loop
  20. Window,
  21. // https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop
  22. Worker,
  23. // https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
  24. Worklet,
  25. };
  26. virtual ~EventLoop() override;
  27. Type type() const { return m_type; }
  28. TaskQueue& task_queue() { return *m_task_queue; }
  29. TaskQueue const& task_queue() const { return *m_task_queue; }
  30. TaskQueue& microtask_queue() { return *m_microtask_queue; }
  31. TaskQueue const& microtask_queue() const { return *m_microtask_queue; }
  32. void spin_until(NOESCAPE JS::SafeFunction<bool()> goal_condition);
  33. void spin_processing_tasks_with_source_until(Task::Source, NOESCAPE JS::SafeFunction<bool()> goal_condition);
  34. void process();
  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_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object);
  47. void pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>);
  48. EnvironmentSettingsObject& top_of_backup_incumbent_settings_object_stack();
  49. bool is_backup_incumbent_settings_object_stack_empty() const { return m_backup_incumbent_settings_object_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. EventLoop();
  58. virtual void visit_edges(Visitor&) override;
  59. Type m_type { Type::Window };
  60. JS::GCPtr<TaskQueue> m_task_queue;
  61. JS::GCPtr<TaskQueue> m_microtask_queue;
  62. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  63. JS::GCPtr<Task> m_currently_running_task { nullptr };
  64. // https://html.spec.whatwg.org/multipage/webappapis.html#last-render-opportunity-time
  65. double m_last_render_opportunity_time { 0 };
  66. // https://html.spec.whatwg.org/multipage/webappapis.html#last-idle-period-start-time
  67. double m_last_idle_period_start_time { 0 };
  68. RefPtr<Platform::Timer> m_system_event_loop_timer;
  69. // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint
  70. bool m_performing_a_microtask_checkpoint { false };
  71. Vector<WeakPtr<DOM::Document>> m_documents;
  72. // Used to implement step 4 of "perform a microtask checkpoint".
  73. // NOTE: These are weak references! ESO registers and unregisters itself from the event loop manually.
  74. Vector<RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;
  75. // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
  76. Vector<JS::NonnullGCPtr<EnvironmentSettingsObject>> m_backup_incumbent_settings_object_stack;
  77. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  78. size_t m_termination_nesting_level { 0 };
  79. bool m_execution_paused { false };
  80. bool m_skip_event_loop_processing_steps { false };
  81. };
  82. EventLoop& main_thread_event_loop();
  83. int queue_global_task(HTML::Task::Source, JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  84. void queue_a_microtask(DOM::Document const*, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  85. void perform_a_microtask_checkpoint();
  86. }