/src/serenity/Userland/Libraries/LibWeb/RequestIdleCallback/IdleDeadline.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2022, the SerenityOS developers. |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <LibWeb/Bindings/IdleDeadlinePrototype.h> |
9 | | #include <LibWeb/HTML/EventLoop/EventLoop.h> |
10 | | #include <LibWeb/HTML/Window.h> |
11 | | #include <LibWeb/HighResolutionTime/TimeOrigin.h> |
12 | | #include <LibWeb/RequestIdleCallback/IdleDeadline.h> |
13 | | |
14 | | namespace Web::RequestIdleCallback { |
15 | | |
16 | | JS_DEFINE_ALLOCATOR(IdleDeadline); |
17 | | |
18 | | JS::NonnullGCPtr<IdleDeadline> IdleDeadline::create(JS::Realm& realm, bool did_timeout) |
19 | 0 | { |
20 | 0 | return realm.heap().allocate<IdleDeadline>(realm, realm, did_timeout); |
21 | 0 | } |
22 | | |
23 | | IdleDeadline::IdleDeadline(JS::Realm& realm, bool did_timeout) |
24 | 0 | : PlatformObject(realm) |
25 | 0 | , m_did_timeout(did_timeout) |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | void IdleDeadline::initialize(JS::Realm& realm) |
30 | 0 | { |
31 | 0 | Base::initialize(realm); |
32 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(IdleDeadline); |
33 | 0 | } |
34 | | |
35 | 0 | IdleDeadline::~IdleDeadline() = default; |
36 | | |
37 | | // https://w3c.github.io/requestidlecallback/#dom-idledeadline-timeremaining |
38 | | double IdleDeadline::time_remaining() const |
39 | 0 | { |
40 | 0 | auto const& event_loop = HTML::main_thread_event_loop(); |
41 | | // 1. Let now be a DOMHighResTimeStamp representing current high resolution time in milliseconds. |
42 | 0 | auto now = HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this)); |
43 | | // 2. Let deadline be the result of calling IdleDeadline's get deadline time algorithm. |
44 | 0 | auto deadline = event_loop.compute_deadline(); |
45 | | // 3. Let timeRemaining be deadline - now. |
46 | 0 | auto time_remaining = deadline - now; |
47 | | // 4. If timeRemaining is negative, set it to 0. |
48 | 0 | if (time_remaining < 0) |
49 | 0 | time_remaining = 0; |
50 | | // 5. Return timeRemaining. |
51 | | // NOTE: coarsening to milliseconds |
52 | 0 | return ceil(time_remaining); |
53 | 0 | } |
54 | | |
55 | | } |