Task.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <LibGC/CellAllocator.h>
  9. #include <LibJS/Heap/Cell.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. GC_CELL(Task, JS::Cell);
  16. GC_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. // https://w3c.github.io/IndexedDB/#database-access-task-source
  51. DatabaseAccess,
  52. // https://websockets.spec.whatwg.org/#websocket-task-source
  53. WebSocket,
  54. // !!! IMPORTANT: Keep this field last!
  55. // This serves as the base value of all unique task sources.
  56. // Some elements, such as the HTMLMediaElement, must have a unique task source per instance.
  57. UniqueTaskSourceStart
  58. };
  59. static GC::Ref<Task> create(JS::VM&, Source, GC::Ptr<DOM::Document const>, GC::Ref<GC::Function<void()>> steps);
  60. virtual ~Task() override;
  61. [[nodiscard]] TaskID id() const { return m_id; }
  62. Source source() const { return m_source; }
  63. void execute();
  64. DOM::Document const* document() const;
  65. bool is_runnable() const;
  66. private:
  67. Task(Source, GC::Ptr<DOM::Document const>, GC::Ref<GC::Function<void()>> steps);
  68. virtual void visit_edges(Visitor&) override;
  69. TaskID m_id {};
  70. Source m_source { Source::Unspecified };
  71. GC::Ref<GC::Function<void()>> m_steps;
  72. GC::Ptr<DOM::Document const> m_document;
  73. };
  74. struct UniqueTaskSource {
  75. UniqueTaskSource();
  76. ~UniqueTaskSource();
  77. Task::Source const source;
  78. };
  79. }