Task.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/IDAllocator.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/EventLoop/Task.h>
  9. namespace Web::HTML {
  10. static IDAllocator s_unique_task_source_allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) };
  11. Task::Task(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps)
  12. : m_source(source)
  13. , m_steps(move(steps))
  14. , m_document(JS::make_handle(document))
  15. {
  16. }
  17. Task::~Task() = default;
  18. void Task::execute()
  19. {
  20. m_steps();
  21. }
  22. // https://html.spec.whatwg.org/#concept-task-runnable
  23. bool Task::is_runnable() const
  24. {
  25. // A task is runnable if its document is either null or fully active.
  26. return !m_document.ptr() || m_document->is_fully_active();
  27. }
  28. DOM::Document const* Task::document() const
  29. {
  30. return m_document.ptr();
  31. }
  32. UniqueTaskSource::UniqueTaskSource()
  33. : source(static_cast<Task::Source>(s_unique_task_source_allocator.allocate()))
  34. {
  35. }
  36. UniqueTaskSource::~UniqueTaskSource()
  37. {
  38. s_unique_task_source_allocator.deallocate(static_cast<int>(source));
  39. }
  40. }