Task.h 2.6 KB

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