Task.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. static IDAllocator s_task_id_allocator;
  12. Task::Task(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps)
  13. : m_id(s_task_id_allocator.allocate())
  14. , m_source(source)
  15. , m_steps(move(steps))
  16. , m_document(JS::make_handle(document))
  17. {
  18. }
  19. Task::~Task()
  20. {
  21. s_unique_task_source_allocator.deallocate(m_id);
  22. }
  23. void Task::execute()
  24. {
  25. m_steps();
  26. }
  27. // https://html.spec.whatwg.org/#concept-task-runnable
  28. bool Task::is_runnable() const
  29. {
  30. // A task is runnable if its document is either null or fully active.
  31. return !m_document.ptr() || m_document->is_fully_active();
  32. }
  33. DOM::Document const* Task::document() const
  34. {
  35. return m_document.ptr();
  36. }
  37. UniqueTaskSource::UniqueTaskSource()
  38. : source(static_cast<Task::Source>(s_unique_task_source_allocator.allocate()))
  39. {
  40. }
  41. UniqueTaskSource::~UniqueTaskSource()
  42. {
  43. s_unique_task_source_allocator.deallocate(static_cast<int>(source));
  44. }
  45. }