Task.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021-2022, 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/NonnullOwnPtr.h>
  9. #include <AK/RefPtr.h>
  10. #include <LibJS/Heap/Handle.h>
  11. #include <LibJS/SafeFunction.h>
  12. #include <LibWeb/Forward.h>
  13. namespace Web::HTML {
  14. struct UniqueTaskSource;
  15. class 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. // Some elements, such as the HTMLMediaElement, must have a unique task source per instance.
  30. // Keep this field last, to serve as the base value of all unique task sources.
  31. UniqueTaskSourceStart,
  32. // https://html.spec.whatwg.org/multipage/webappapis.html#navigation-and-traversal-task-source
  33. NavigationAndTraversal,
  34. // https://w3c.github.io/FileAPI/#fileReadingTaskSource
  35. FileReading,
  36. };
  37. static NonnullOwnPtr<Task> create(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps)
  38. {
  39. return adopt_own(*new Task(source, document, move(steps)));
  40. }
  41. ~Task();
  42. Source source() const { return m_source; }
  43. void execute();
  44. DOM::Document const* document() const;
  45. bool is_runnable() const;
  46. private:
  47. Task(Source, DOM::Document const*, JS::SafeFunction<void()> steps);
  48. Source m_source { Source::Unspecified };
  49. JS::SafeFunction<void()> m_steps;
  50. JS::Handle<DOM::Document const> m_document;
  51. };
  52. struct UniqueTaskSource {
  53. UniqueTaskSource();
  54. ~UniqueTaskSource();
  55. Task::Source const source;
  56. };
  57. }