ladybird/Userland/Libraries/LibWeb/HTML/EventLoop/Task.cpp
Timothy Flynn 807891c0df LibWeb: Support unique task sources
Some elements, like HTMLMediaElement, must have a unique task sources
for every instance of that element that is created. Support this with a
simple wrapper around IDAllocator.
2023-04-07 16:02:22 +02:00

51 lines
1.1 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/IDAllocator.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/EventLoop/Task.h>
namespace Web::HTML {
static IDAllocator s_unique_task_source_allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) };
Task::Task(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps)
: m_source(source)
, m_steps(move(steps))
, m_document(JS::make_handle(document))
{
}
Task::~Task() = default;
void Task::execute()
{
m_steps();
}
// https://html.spec.whatwg.org/#concept-task-runnable
bool Task::is_runnable() const
{
// A task is runnable if its document is either null or fully active.
return !m_document.ptr() || m_document->is_fully_active();
}
DOM::Document const* Task::document() const
{
return m_document.ptr();
}
UniqueTaskSource::UniqueTaskSource()
: source(static_cast<Task::Source>(s_unique_task_source_allocator.allocate()))
{
}
UniqueTaskSource::~UniqueTaskSource()
{
s_unique_task_source_allocator.deallocate(static_cast<int>(source));
}
}