Task.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. };
  35. static NonnullOwnPtr<Task> create(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps)
  36. {
  37. return adopt_own(*new Task(source, document, move(steps)));
  38. }
  39. ~Task();
  40. Source source() const { return m_source; }
  41. void execute();
  42. DOM::Document const* document() const;
  43. bool is_runnable() const;
  44. private:
  45. Task(Source, DOM::Document const*, JS::SafeFunction<void()> steps);
  46. Source m_source { Source::Unspecified };
  47. JS::SafeFunction<void()> m_steps;
  48. JS::Handle<DOM::Document const> m_document;
  49. };
  50. struct UniqueTaskSource {
  51. UniqueTaskSource();
  52. ~UniqueTaskSource();
  53. Task::Source const source;
  54. };
  55. }