/src/serenity/Userland/Libraries/LibWeb/HTML/EventLoop/Task.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/IDAllocator.h> |
8 | | #include <LibWeb/DOM/Document.h> |
9 | | #include <LibWeb/HTML/EventLoop/Task.h> |
10 | | |
11 | | namespace Web::HTML { |
12 | | |
13 | | JS_DEFINE_ALLOCATOR(Task); |
14 | | |
15 | | static IDAllocator s_unique_task_source_allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) }; |
16 | | |
17 | | [[nodiscard]] static TaskID allocate_task_id() |
18 | 0 | { |
19 | 0 | static u64 next_task_id = 1; |
20 | 0 | return next_task_id++; |
21 | 0 | } |
22 | | |
23 | | JS::NonnullGCPtr<Task> Task::create(JS::VM& vm, Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps) |
24 | 0 | { |
25 | 0 | return vm.heap().allocate_without_realm<Task>(source, document, move(steps)); |
26 | 0 | } |
27 | | |
28 | | Task::Task(Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps) |
29 | 0 | : m_id(allocate_task_id()) |
30 | 0 | , m_source(source) |
31 | 0 | , m_steps(steps) |
32 | 0 | , m_document(document) |
33 | 0 | { |
34 | 0 | } |
35 | | |
36 | 0 | Task::~Task() = default; |
37 | | |
38 | | void Task::visit_edges(Visitor& visitor) |
39 | 0 | { |
40 | 0 | Base::visit_edges(visitor); |
41 | 0 | visitor.visit(m_steps); |
42 | 0 | visitor.visit(m_document); |
43 | 0 | } |
44 | | |
45 | | void Task::execute() |
46 | 0 | { |
47 | 0 | m_steps->function()(); |
48 | 0 | } |
49 | | |
50 | | // https://html.spec.whatwg.org/#concept-task-runnable |
51 | | bool Task::is_runnable() const |
52 | 0 | { |
53 | | // A task is runnable if its document is either null or fully active. |
54 | 0 | return !m_document.ptr() || m_document->is_fully_active(); |
55 | 0 | } |
56 | | |
57 | | DOM::Document const* Task::document() const |
58 | 0 | { |
59 | 0 | return m_document.ptr(); |
60 | 0 | } |
61 | | |
62 | | UniqueTaskSource::UniqueTaskSource() |
63 | 0 | : source(static_cast<Task::Source>(s_unique_task_source_allocator.allocate())) |
64 | 0 | { |
65 | 0 | } |
66 | | |
67 | | UniqueTaskSource::~UniqueTaskSource() |
68 | 0 | { |
69 | 0 | s_unique_task_source_allocator.deallocate(static_cast<int>(source)); |
70 | 0 | } |
71 | | |
72 | | } |