Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HTML/AnimationFrameCallbackDriver.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2022, the SerenityOS developers.
3
 * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#pragma once
9
10
#include <AK/Function.h>
11
#include <AK/HashMap.h>
12
#include <LibCore/Timer.h>
13
#include <LibWeb/HTML/EventLoop/EventLoop.h>
14
#include <LibWeb/WebIDL/Types.h>
15
16
namespace Web::HTML {
17
18
struct AnimationFrameCallbackDriver {
19
    using Callback = Function<void(double)>;
20
21
    AnimationFrameCallbackDriver()
22
0
    {
23
0
        m_timer = Core::Timer::create_single_shot(16, [] {
24
0
            HTML::main_thread_event_loop().schedule();
25
0
        });
26
0
    }
27
28
    [[nodiscard]] WebIDL::UnsignedLong add(Callback handler)
29
0
    {
30
0
        auto id = ++m_animation_frame_callback_identifier;
31
0
        m_callbacks.set(id, move(handler));
32
0
        if (!m_timer->is_active())
33
0
            m_timer->start();
34
0
        return id;
35
0
    }
36
37
    bool remove(WebIDL::UnsignedLong id)
38
0
    {
39
0
        auto it = m_callbacks.find(id);
40
0
        if (it == m_callbacks.end())
41
0
            return false;
42
0
        m_callbacks.remove(it);
43
0
        return true;
44
0
    }
45
46
    void run(double now)
47
0
    {
48
0
        auto taken_callbacks = move(m_callbacks);
49
0
        for (auto& [id, callback] : taken_callbacks)
50
0
            callback(now);
51
0
    }
52
53
    bool has_callbacks() const
54
0
    {
55
0
        return !m_callbacks.is_empty();
56
0
    }
57
58
private:
59
    // https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#animation-frame-callback-identifier
60
    WebIDL::UnsignedLong m_animation_frame_callback_identifier { 0 };
61
62
    OrderedHashMap<WebIDL::UnsignedLong, Callback> m_callbacks;
63
    RefPtr<Core::Timer> m_timer;
64
};
65
66
}