2021-09-17 01:42:36 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2022-03-31 21:55:01 +02:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-09-17 01:42:36 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/IdleDeadlinePrototype.h>
|
2022-03-31 21:55:01 +02:00
|
|
|
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
2022-09-04 13:26:36 +02:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2022-10-04 21:30:29 +01:00
|
|
|
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
|
2021-09-17 01:42:36 +02:00
|
|
|
#include <LibWeb/RequestIdleCallback/IdleDeadline.h>
|
|
|
|
|
|
|
|
namespace Web::RequestIdleCallback {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(IdleDeadline);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<IdleDeadline> IdleDeadline::create(JS::Realm& realm, bool did_timeout)
|
2021-09-17 01:42:36 +02:00
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<IdleDeadline>(realm, did_timeout);
|
2021-09-17 01:42:36 +02:00
|
|
|
}
|
|
|
|
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
IdleDeadline::IdleDeadline(JS::Realm& realm, bool did_timeout)
|
|
|
|
: PlatformObject(realm)
|
2022-09-04 13:26:36 +02:00
|
|
|
, m_did_timeout(did_timeout)
|
2021-09-17 01:42:36 +02:00
|
|
|
{
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void IdleDeadline::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(IdleDeadline);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2021-09-17 01:42:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
IdleDeadline::~IdleDeadline() = default;
|
2021-09-17 01:42:36 +02:00
|
|
|
|
2022-03-31 21:55:01 +02:00
|
|
|
// https://w3c.github.io/requestidlecallback/#dom-idledeadline-timeremaining
|
|
|
|
double IdleDeadline::time_remaining() const
|
|
|
|
{
|
|
|
|
auto const& event_loop = HTML::main_thread_event_loop();
|
|
|
|
// 1. Let now be a DOMHighResTimeStamp representing current high resolution time in milliseconds.
|
2024-09-04 19:22:48 -06:00
|
|
|
auto now = HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this));
|
2022-03-31 21:55:01 +02:00
|
|
|
// 2. Let deadline be the result of calling IdleDeadline's get deadline time algorithm.
|
|
|
|
auto deadline = event_loop.compute_deadline();
|
|
|
|
// 3. Let timeRemaining be deadline - now.
|
|
|
|
auto time_remaining = deadline - now;
|
|
|
|
// 4. If timeRemaining is negative, set it to 0.
|
|
|
|
if (time_remaining < 0)
|
|
|
|
time_remaining = 0;
|
|
|
|
// 5. Return timeRemaining.
|
|
|
|
// NOTE: coarsening to milliseconds
|
|
|
|
return ceil(time_remaining);
|
|
|
|
}
|
|
|
|
|
2021-09-17 01:42:36 +02:00
|
|
|
}
|