EventLoop.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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(NOESCAPE JS::SafeFunction<bool()> goal_condition);
  32. void spin_processing_tasks_with_source_until(Task::Source, NOESCAPE JS::SafeFunction<bool()> goal_condition);
  33. void process();
  34. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  35. size_t termination_nesting_level() const { return m_termination_nesting_level; }
  36. void increment_termination_nesting_level() { ++m_termination_nesting_level; }
  37. void decrement_termination_nesting_level() { --m_termination_nesting_level; }
  38. Task const* currently_running_task() const { return m_currently_running_task; }
  39. void schedule();
  40. void perform_a_microtask_checkpoint();
  41. void register_document(Badge<DOM::Document>, DOM::Document&);
  42. void unregister_document(Badge<DOM::Document>, DOM::Document&);
  43. Vector<JS::Handle<DOM::Document>> documents_in_this_event_loop() const;
  44. Vector<JS::Handle<HTML::Window>> same_loop_windows() const;
  45. void push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object);
  46. void pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>);
  47. EnvironmentSettingsObject& top_of_backup_incumbent_settings_object_stack();
  48. bool is_backup_incumbent_settings_object_stack_empty() const { return m_backup_incumbent_settings_object_stack.is_empty(); }
  49. void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  50. void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  51. double compute_deadline() const;
  52. // https://html.spec.whatwg.org/multipage/webappapis.html#pause
  53. void set_execution_paused(bool execution_paused) { m_execution_paused = execution_paused; }
  54. bool execution_paused() const { return m_execution_paused; }
  55. private:
  56. EventLoop();
  57. virtual void visit_edges(Visitor&) override;
  58. Type m_type { Type::Window };
  59. JS::GCPtr<TaskQueue> m_task_queue;
  60. JS::GCPtr<TaskQueue> m_microtask_queue;
  61. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  62. JS::GCPtr<Task> m_currently_running_task { nullptr };
  63. // https://html.spec.whatwg.org/multipage/webappapis.html#last-render-opportunity-time
  64. double m_last_render_opportunity_time { 0 };
  65. // https://html.spec.whatwg.org/multipage/webappapis.html#last-idle-period-start-time
  66. double m_last_idle_period_start_time { 0 };
  67. RefPtr<Platform::Timer> m_system_event_loop_timer;
  68. // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint
  69. bool m_performing_a_microtask_checkpoint { false };
  70. Vector<WeakPtr<DOM::Document>> m_documents;
  71. // Used to implement step 4 of "perform a microtask checkpoint".
  72. // NOTE: These are weak references! ESO registers and unregisters itself from the event loop manually.
  73. Vector<RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;
  74. // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
  75. Vector<JS::NonnullGCPtr<EnvironmentSettingsObject>> m_backup_incumbent_settings_object_stack;
  76. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  77. size_t m_termination_nesting_level { 0 };
  78. bool m_execution_paused { false };
  79. bool m_skip_event_loop_processing_steps { false };
  80. };
  81. EventLoop& main_thread_event_loop();
  82. int queue_global_task(HTML::Task::Source, JS::Object&, Function<void()> steps);
  83. void queue_a_microtask(DOM::Document const*, Function<void()> steps);
  84. void perform_a_microtask_checkpoint();
  85. }