Task.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DistinctNumeric.h>
  8. #include <LibJS/Heap/Cell.h>
  9. #include <LibJS/Heap/CellAllocator.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::HTML {
  12. struct UniqueTaskSource;
  13. AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, TaskID, Comparison);
  14. class Task final : public JS::Cell {
  15. JS_CELL(Task, JS::Cell);
  16. JS_DECLARE_ALLOCATOR(Task);
  17. public:
  18. // https://html.spec.whatwg.org/multipage/webappapis.html#generic-task-sources
  19. enum class Source {
  20. Unspecified,
  21. DOMManipulation,
  22. UserInteraction,
  23. Networking,
  24. HistoryTraversal,
  25. IdleTask,
  26. PostedMessage,
  27. Microtask,
  28. TimerTask,
  29. JavaScriptEngine,
  30. // https://html.spec.whatwg.org/multipage/webappapis.html#navigation-and-traversal-task-source
  31. NavigationAndTraversal,
  32. // https://w3c.github.io/FileAPI/#fileReadingTaskSource
  33. FileReading,
  34. // https://www.w3.org/TR/intersection-observer/#intersectionobserver-task-source
  35. IntersectionObserver,
  36. // https://w3c.github.io/performance-timeline/#dfn-performance-timeline-task-source
  37. PerformanceTimeline,
  38. // https://html.spec.whatwg.org/multipage/canvas.html#canvas-blob-serialisation-task-source
  39. CanvasBlobSerializationTask,
  40. // https://w3c.github.io/clipboard-apis/#clipboard-task-source
  41. Clipboard,
  42. // https://w3c.github.io/permissions/#permissions-task-source
  43. Permissions,
  44. // https://drafts.csswg.org/css-font-loading/#task-source
  45. FontLoading,
  46. // https://html.spec.whatwg.org/multipage/server-sent-events.html#remote-event-task-source
  47. RemoteEvent,
  48. // https://html.spec.whatwg.org/multipage/webappapis.html#rendering-task-source
  49. Rendering,
  50. // !!! IMPORTANT: Keep this field last!
  51. // This serves as the base value of all unique task sources.
  52. // Some elements, such as the HTMLMediaElement, must have a unique task source per instance.
  53. UniqueTaskSourceStart
  54. };
  55. static JS::NonnullGCPtr<Task> create(JS::VM&, Source, JS::GCPtr<DOM::Document const>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  56. virtual ~Task() override;
  57. [[nodiscard]] TaskID id() const { return m_id; }
  58. Source source() const { return m_source; }
  59. void execute();
  60. DOM::Document const* document() const;
  61. bool is_runnable() const;
  62. private:
  63. Task(Source, JS::GCPtr<DOM::Document const>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
  64. virtual void visit_edges(Visitor&) override;
  65. TaskID m_id {};
  66. Source m_source { Source::Unspecified };
  67. JS::NonnullGCPtr<JS::HeapFunction<void()>> m_steps;
  68. JS::GCPtr<DOM::Document const> m_document;
  69. };
  70. struct UniqueTaskSource {
  71. UniqueTaskSource();
  72. ~UniqueTaskSource();
  73. Task::Source const source;
  74. };
  75. }