EventLoop.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 {
  15. public:
  16. enum class Type {
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#window-event-loop
  18. Window,
  19. // https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop
  20. Worker,
  21. // https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
  22. Worklet,
  23. };
  24. EventLoop();
  25. ~EventLoop();
  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(Function<bool()> goal_condition);
  32. void process();
  33. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
  34. size_t termination_nesting_level() const { return m_termination_nesting_level; }
  35. void increment_termination_nesting_level() { ++m_termination_nesting_level; }
  36. void decrement_termination_nesting_level() { --m_termination_nesting_level; }
  37. Task const* currently_running_task() const { return m_currently_running_task; }
  38. JS::VM& vm() { return *m_vm; }
  39. JS::VM const& vm() const { return *m_vm; }
  40. void set_vm(JS::VM&);
  41. void schedule();
  42. void perform_a_microtask_checkpoint();
  43. void register_document(Badge<DOM::Document>, DOM::Document&);
  44. void unregister_document(Badge<DOM::Document>, DOM::Document&);
  45. Vector<JS::Handle<DOM::Document>> documents_in_this_event_loop() const;
  46. Vector<JS::Handle<HTML::Window>> same_loop_windows() const;
  47. void push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object);
  48. void pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>);
  49. EnvironmentSettingsObject& top_of_backup_incumbent_settings_object_stack();
  50. bool is_backup_incumbent_settings_object_stack_empty() const { return m_backup_incumbent_settings_object_stack.is_empty(); }
  51. void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  52. void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
  53. double compute_deadline() const;
  54. // https://html.spec.whatwg.org/multipage/webappapis.html#pause
  55. void set_execution_paused(bool execution_paused) { m_execution_paused = execution_paused; }
  56. bool execution_paused() const { return m_execution_paused; }
  57. private:
  58. Type m_type { Type::Window };
  59. TaskQueue m_task_queue;
  60. TaskQueue m_microtask_queue;
  61. // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
  62. 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. JS::VM* m_vm { nullptr };
  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. Vector<EnvironmentSettingsObject&> m_related_environment_settings_objects;
  74. // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
  75. Vector<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. };
  80. EventLoop& main_thread_event_loop();
  81. void old_queue_global_task_with_document(HTML::Task::Source, DOM::Document&, JS::SafeFunction<void()> steps);
  82. void queue_global_task(HTML::Task::Source, JS::Object&, JS::SafeFunction<void()> steps);
  83. void queue_a_microtask(DOM::Document const*, JS::SafeFunction<void()> steps);
  84. void perform_a_microtask_checkpoint();
  85. }