mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 20:17:42 +00:00
LibWeb: Move timer implementations to WindowOrWorkerGlobalScopeMixin
This is where it belongs according to the spec, and where these methods' IDL will be placed. This forces us to implement a few steps closer to the spec as well.
This commit is contained in:
parent
b579093ad0
commit
dd992e7dad
5 changed files with 164 additions and 146 deletions
|
@ -41,11 +41,9 @@
|
||||||
#include <LibWeb/HTML/Navigator.h>
|
#include <LibWeb/HTML/Navigator.h>
|
||||||
#include <LibWeb/HTML/Origin.h>
|
#include <LibWeb/HTML/Origin.h>
|
||||||
#include <LibWeb/HTML/PageTransitionEvent.h>
|
#include <LibWeb/HTML/PageTransitionEvent.h>
|
||||||
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
|
||||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||||
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
||||||
#include <LibWeb/HTML/Storage.h>
|
#include <LibWeb/HTML/Storage.h>
|
||||||
#include <LibWeb/HTML/Timer.h>
|
|
||||||
#include <LibWeb/HTML/Window.h>
|
#include <LibWeb/HTML/Window.h>
|
||||||
#include <LibWeb/HTML/WindowProxy.h>
|
#include <LibWeb/HTML/WindowProxy.h>
|
||||||
#include <LibWeb/HighResolutionTime/Performance.h>
|
#include <LibWeb/HighResolutionTime/Performance.h>
|
||||||
|
@ -98,6 +96,8 @@ Window::Window(JS::Realm& realm)
|
||||||
void Window::visit_edges(JS::Cell::Visitor& visitor)
|
void Window::visit_edges(JS::Cell::Visitor& visitor)
|
||||||
{
|
{
|
||||||
Base::visit_edges(visitor);
|
Base::visit_edges(visitor);
|
||||||
|
WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
|
||||||
|
|
||||||
visitor.visit(m_associated_document.ptr());
|
visitor.visit(m_associated_document.ptr());
|
||||||
visitor.visit(m_current_event.ptr());
|
visitor.visit(m_current_event.ptr());
|
||||||
visitor.visit(m_performance.ptr());
|
visitor.visit(m_performance.ptr());
|
||||||
|
@ -105,8 +105,6 @@ void Window::visit_edges(JS::Cell::Visitor& visitor)
|
||||||
visitor.visit(m_location);
|
visitor.visit(m_location);
|
||||||
visitor.visit(m_crypto);
|
visitor.visit(m_crypto);
|
||||||
visitor.visit(m_navigator);
|
visitor.visit(m_navigator);
|
||||||
for (auto& it : m_timers)
|
|
||||||
visitor.visit(it.value.ptr());
|
|
||||||
for (auto& plugin_object : m_pdf_viewer_plugin_objects)
|
for (auto& plugin_object : m_pdf_viewer_plugin_objects)
|
||||||
visitor.visit(plugin_object);
|
visitor.visit(plugin_object);
|
||||||
for (auto& mime_type_object : m_pdf_viewer_mime_type_objects)
|
for (auto& mime_type_object : m_pdf_viewer_mime_type_objects)
|
||||||
|
@ -415,124 +413,6 @@ WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> Window::open_impl(StringView url, St
|
||||||
return target_browsing_context->window_proxy();
|
return target_browsing_context->window_proxy();
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
|
|
||||||
i32 Window::set_timeout_impl(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
|
|
||||||
{
|
|
||||||
return run_timer_initialization_steps(move(handler), timeout, move(arguments), Repeat::No);
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
|
|
||||||
i32 Window::set_interval_impl(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
|
|
||||||
{
|
|
||||||
return run_timer_initialization_steps(move(handler), timeout, move(arguments), Repeat::Yes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-cleartimeout
|
|
||||||
void Window::clear_timeout_impl(i32 id)
|
|
||||||
{
|
|
||||||
m_timers.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-clearinterval
|
|
||||||
void Window::clear_interval_impl(i32 id)
|
|
||||||
{
|
|
||||||
m_timers.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::deallocate_timer_id(Badge<Timer>, i32 id)
|
|
||||||
{
|
|
||||||
m_timer_id_allocator.deallocate(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps
|
|
||||||
i32 Window::run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id)
|
|
||||||
{
|
|
||||||
// 1. Let thisArg be global if that is a WorkerGlobalScope object; otherwise let thisArg be the WindowProxy that corresponds to global.
|
|
||||||
|
|
||||||
// 2. If previousId was given, let id be previousId; otherwise, let id be an implementation-defined integer that is greater than zero and does not already exist in global's map of active timers.
|
|
||||||
auto id = previous_id.has_value() ? previous_id.value() : m_timer_id_allocator.allocate();
|
|
||||||
|
|
||||||
// FIXME: 3. If the surrounding agent's event loop's currently running task is a task that was created by this algorithm, then let nesting level be the task's timer nesting level. Otherwise, let nesting level be zero.
|
|
||||||
|
|
||||||
// 4. If timeout is less than 0, then set timeout to 0.
|
|
||||||
if (timeout < 0)
|
|
||||||
timeout = 0;
|
|
||||||
|
|
||||||
// FIXME: 5. If nesting level is greater than 5, and timeout is less than 4, then set timeout to 4.
|
|
||||||
|
|
||||||
// 6. Let callerRealm be the current Realm Record, and calleeRealm be global's relevant Realm.
|
|
||||||
// FIXME: Implement this when step 9.2 is implemented.
|
|
||||||
|
|
||||||
// 7. Let initiating script be the active script.
|
|
||||||
// 8. Assert: initiating script is not null, since this algorithm is always called from some script.
|
|
||||||
|
|
||||||
// 9. Let task be a task that runs the following substeps:
|
|
||||||
JS::SafeFunction<void()> task = [this, handler = move(handler), timeout, arguments = move(arguments), repeat, id] {
|
|
||||||
// 1. If id does not exist in global's map of active timers, then abort these steps.
|
|
||||||
if (!m_timers.contains(id))
|
|
||||||
return;
|
|
||||||
|
|
||||||
handler.visit(
|
|
||||||
// 2. If handler is a Function, then invoke handler given arguments with the callback this value set to thisArg. If this throws an exception, catch it, and report the exception.
|
|
||||||
[&](JS::Handle<WebIDL::CallbackType> callback) {
|
|
||||||
if (auto result = WebIDL::invoke_callback(*callback, this, arguments); result.is_error())
|
|
||||||
report_exception(result, realm());
|
|
||||||
},
|
|
||||||
// 3. Otherwise:
|
|
||||||
[&](DeprecatedString const& source) {
|
|
||||||
// 1. Assert: handler is a string.
|
|
||||||
// FIXME: 2. Perform HostEnsureCanCompileStrings(callerRealm, calleeRealm). If this throws an exception, catch it, report the exception, and abort these steps.
|
|
||||||
|
|
||||||
// 3. Let settings object be global's relevant settings object.
|
|
||||||
auto& settings_object = associated_document().relevant_settings_object();
|
|
||||||
|
|
||||||
// 4. Let base URL be initiating script's base URL.
|
|
||||||
auto url = associated_document().url();
|
|
||||||
|
|
||||||
// 5. Assert: base URL is not null, as initiating script is a classic script or a JavaScript module script.
|
|
||||||
|
|
||||||
// 6. Let fetch options be a script fetch options whose cryptographic nonce is initiating script's fetch options's cryptographic nonce, integrity metadata is the empty string, parser metadata is "not-parser-inserted", credentials mode is initiating script's fetch options's credentials mode, and referrer policy is initiating script's fetch options's referrer policy.
|
|
||||||
// 7. Let script be the result of creating a classic script given handler, settings object, base URL, and fetch options.
|
|
||||||
auto script = ClassicScript::create(url.basename(), source, settings_object, url);
|
|
||||||
|
|
||||||
// 8. Run the classic script script.
|
|
||||||
(void)script->run();
|
|
||||||
});
|
|
||||||
|
|
||||||
// 4. If id does not exist in global's map of active timers, then abort these steps.
|
|
||||||
if (!m_timers.contains(id))
|
|
||||||
return;
|
|
||||||
|
|
||||||
switch (repeat) {
|
|
||||||
// 5. If repeat is true, then perform the timer initialization steps again, given global, handler, timeout, arguments, true, and id.
|
|
||||||
case Repeat::Yes:
|
|
||||||
run_timer_initialization_steps(handler, timeout, move(arguments), repeat, id);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// 6. Otherwise, remove global's map of active timers[id].
|
|
||||||
case Repeat::No:
|
|
||||||
m_timers.remove(id);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// FIXME: 10. Increment nesting level by one.
|
|
||||||
// FIXME: 11. Set task's timer nesting level to nesting level.
|
|
||||||
|
|
||||||
// 12. Let completionStep be an algorithm step which queues a global task on the timer task source given global to run task.
|
|
||||||
JS::SafeFunction<void()> completion_step = [this, task = move(task)]() mutable {
|
|
||||||
queue_global_task(Task::Source::TimerTask, *this, move(task));
|
|
||||||
};
|
|
||||||
|
|
||||||
// 13. Run steps after a timeout given global, "setTimeout/setInterval", timeout, completionStep, and id.
|
|
||||||
auto timer = Timer::create(*this, timeout, move(completion_step), id);
|
|
||||||
m_timers.set(id, timer);
|
|
||||||
timer->start();
|
|
||||||
|
|
||||||
// 14. Return id.
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::did_set_location_href(Badge<Location>, AK::URL const& new_href)
|
void Window::did_set_location_href(Badge<Location>, AK::URL const& new_href)
|
||||||
{
|
{
|
||||||
auto* browsing_context = associated_document().browsing_context();
|
auto* browsing_context = associated_document().browsing_context();
|
||||||
|
@ -1511,7 +1391,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::set_timeout)
|
||||||
for (size_t i = 2; i < vm.argument_count(); ++i)
|
for (size_t i = 2; i < vm.argument_count(); ++i)
|
||||||
arguments.append(vm.argument(i));
|
arguments.append(vm.argument(i));
|
||||||
|
|
||||||
auto id = impl->set_timeout_impl(move(handler), timeout, move(arguments));
|
auto id = static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->set_timeout(move(handler), timeout, move(arguments));
|
||||||
return JS::Value(id);
|
return JS::Value(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1533,7 +1413,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::set_interval)
|
||||||
for (size_t i = 2; i < vm.argument_count(); ++i)
|
for (size_t i = 2; i < vm.argument_count(); ++i)
|
||||||
arguments.append(vm.argument(i));
|
arguments.append(vm.argument(i));
|
||||||
|
|
||||||
auto id = impl->set_interval_impl(move(handler), timeout, move(arguments));
|
auto id = static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->set_interval(move(handler), timeout, move(arguments));
|
||||||
return JS::Value(id);
|
return JS::Value(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1546,7 +1426,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::clear_timeout)
|
||||||
if (vm.argument_count())
|
if (vm.argument_count())
|
||||||
id = TRY(vm.argument(0).to_i32(vm));
|
id = TRY(vm.argument(0).to_i32(vm));
|
||||||
|
|
||||||
impl->clear_timeout_impl(id);
|
static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->clear_timeout(id);
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1559,7 +1439,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::clear_interval)
|
||||||
if (vm.argument_count())
|
if (vm.argument_count())
|
||||||
id = TRY(vm.argument(0).to_i32(vm));
|
id = TRY(vm.argument(0).to_i32(vm));
|
||||||
|
|
||||||
impl->clear_interval_impl(id);
|
static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->clear_interval(id);
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Badge.h>
|
#include <AK/Badge.h>
|
||||||
#include <AK/IDAllocator.h>
|
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/TypeCasts.h>
|
#include <AK/TypeCasts.h>
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
|
@ -31,9 +30,6 @@ namespace Web::HTML {
|
||||||
|
|
||||||
class IdleCallback;
|
class IdleCallback;
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/#timerhandler
|
|
||||||
using TimerHandler = Variant<JS::Handle<WebIDL::CallbackType>, DeprecatedString>;
|
|
||||||
|
|
||||||
// https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolloptions
|
// https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolloptions
|
||||||
struct ScrollOptions {
|
struct ScrollOptions {
|
||||||
Bindings::ScrollBehavior behavior { Bindings::ScrollBehavior::Auto };
|
Bindings::ScrollBehavior behavior { Bindings::ScrollBehavior::Auto };
|
||||||
|
@ -96,17 +92,10 @@ public:
|
||||||
WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
|
WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
|
||||||
bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
|
bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
|
||||||
|
|
||||||
i32 set_timeout_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
|
||||||
i32 set_interval_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
|
||||||
void clear_timeout_impl(i32);
|
|
||||||
void clear_interval_impl(i32);
|
|
||||||
|
|
||||||
void did_set_location_href(Badge<Location>, AK::URL const& new_href);
|
void did_set_location_href(Badge<Location>, AK::URL const& new_href);
|
||||||
void did_call_location_reload(Badge<Location>);
|
void did_call_location_reload(Badge<Location>);
|
||||||
void did_call_location_replace(Badge<Location>, DeprecatedString url);
|
void did_call_location_replace(Badge<Location>, DeprecatedString url);
|
||||||
|
|
||||||
void deallocate_timer_id(Badge<Timer>, i32);
|
|
||||||
|
|
||||||
DOM::Event* current_event() { return m_current_event.ptr(); }
|
DOM::Event* current_event() { return m_current_event.ptr(); }
|
||||||
DOM::Event const* current_event() const { return m_current_event.ptr(); }
|
DOM::Event const* current_event() const { return m_current_event.ptr(); }
|
||||||
void set_current_event(DOM::Event* event);
|
void set_current_event(DOM::Event* event);
|
||||||
|
@ -204,12 +193,6 @@ private:
|
||||||
// ^HTML::WindowEventHandlers
|
// ^HTML::WindowEventHandlers
|
||||||
virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }
|
virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }
|
||||||
|
|
||||||
enum class Repeat {
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
};
|
|
||||||
i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
|
|
||||||
|
|
||||||
void invoke_idle_callbacks();
|
void invoke_idle_callbacks();
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
|
// https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
|
||||||
|
@ -217,9 +200,6 @@ private:
|
||||||
|
|
||||||
JS::GCPtr<DOM::Event> m_current_event;
|
JS::GCPtr<DOM::Event> m_current_event;
|
||||||
|
|
||||||
IDAllocator m_timer_id_allocator;
|
|
||||||
HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
|
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
|
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
|
||||||
ImportMap m_import_map;
|
ImportMap m_import_map;
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,15 @@
|
||||||
#include <AK/Utf8View.h>
|
#include <AK/Utf8View.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibTextCodec/Decoder.h>
|
#include <LibTextCodec/Decoder.h>
|
||||||
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||||
#include <LibWeb/Fetch/FetchMethod.h>
|
#include <LibWeb/Fetch/FetchMethod.h>
|
||||||
#include <LibWeb/Forward.h>
|
#include <LibWeb/Forward.h>
|
||||||
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
||||||
|
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
||||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||||
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
||||||
#include <LibWeb/HTML/StructuredSerialize.h>
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
||||||
|
#include <LibWeb/HTML/Timer.h>
|
||||||
#include <LibWeb/HTML/Window.h>
|
#include <LibWeb/HTML/Window.h>
|
||||||
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
|
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
|
||||||
#include <LibWeb/Infra/Base64.h>
|
#include <LibWeb/Infra/Base64.h>
|
||||||
|
@ -27,6 +30,12 @@ namespace Web::HTML {
|
||||||
|
|
||||||
WindowOrWorkerGlobalScopeMixin::~WindowOrWorkerGlobalScopeMixin() = default;
|
WindowOrWorkerGlobalScopeMixin::~WindowOrWorkerGlobalScopeMixin() = default;
|
||||||
|
|
||||||
|
void WindowOrWorkerGlobalScopeMixin::visit_edges(JS::Cell::Visitor& visitor)
|
||||||
|
{
|
||||||
|
for (auto& it : m_timers)
|
||||||
|
visitor.visit(it.value);
|
||||||
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
|
// https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
|
||||||
WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::origin() const
|
WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::origin() const
|
||||||
{
|
{
|
||||||
|
@ -132,4 +141,127 @@ JS::NonnullGCPtr<JS::Promise> WindowOrWorkerGlobalScopeMixin::fetch(Fetch::Reque
|
||||||
return Fetch::fetch(vm, input, init);
|
return Fetch::fetch(vm, input, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
|
||||||
|
i32 WindowOrWorkerGlobalScopeMixin::set_timeout(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
|
||||||
|
{
|
||||||
|
return run_timer_initialization_steps(move(handler), timeout, move(arguments), Repeat::No);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
|
||||||
|
i32 WindowOrWorkerGlobalScopeMixin::set_interval(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments)
|
||||||
|
{
|
||||||
|
return run_timer_initialization_steps(move(handler), timeout, move(arguments), Repeat::Yes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-cleartimeout
|
||||||
|
void WindowOrWorkerGlobalScopeMixin::clear_timeout(i32 id)
|
||||||
|
{
|
||||||
|
m_timers.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-clearinterval
|
||||||
|
void WindowOrWorkerGlobalScopeMixin::clear_interval(i32 id)
|
||||||
|
{
|
||||||
|
m_timers.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps
|
||||||
|
i32 WindowOrWorkerGlobalScopeMixin::run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id, Optional<AK::URL> base_url)
|
||||||
|
{
|
||||||
|
// 1. Let thisArg be global if that is a WorkerGlobalScope object; otherwise let thisArg be the WindowProxy that corresponds to global.
|
||||||
|
|
||||||
|
// 2. If previousId was given, let id be previousId; otherwise, let id be an implementation-defined integer that is greater than zero and does not already exist in global's map of active timers.
|
||||||
|
auto id = previous_id.has_value() ? previous_id.value() : m_timer_id_allocator.allocate();
|
||||||
|
|
||||||
|
// FIXME: 3. If the surrounding agent's event loop's currently running task is a task that was created by this algorithm, then let nesting level be the task's timer nesting level. Otherwise, let nesting level be zero.
|
||||||
|
|
||||||
|
// 4. If timeout is less than 0, then set timeout to 0.
|
||||||
|
if (timeout < 0)
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
|
// FIXME: 5. If nesting level is greater than 5, and timeout is less than 4, then set timeout to 4.
|
||||||
|
|
||||||
|
// 6. Let callerRealm be the current Realm Record, and calleeRealm be global's relevant Realm.
|
||||||
|
// FIXME: Implement this when step 9.3.2 is implemented.
|
||||||
|
|
||||||
|
// FIXME: The active script becomes null on repeated setInterval callbacks. In JS::VM::get_active_script_or_module,
|
||||||
|
// the execution context stack is empty on the repeated invocations, thus it returns null. We will need
|
||||||
|
// to figure out why it becomes empty. But all we need from the active script is the base URL, so we
|
||||||
|
// grab it on the first invocation an reuse it on repeated invocations.
|
||||||
|
if (!base_url.has_value()) {
|
||||||
|
// 7. Let initiating script be the active script.
|
||||||
|
auto const* initiating_script = Web::Bindings::active_script();
|
||||||
|
|
||||||
|
// 8. Assert: initiating script is not null, since this algorithm is always called from some script.
|
||||||
|
VERIFY(initiating_script);
|
||||||
|
|
||||||
|
base_url = initiating_script->base_url();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9. Let task be a task that runs the following substeps:
|
||||||
|
JS::SafeFunction<void()> task = [this, handler = move(handler), timeout, arguments = move(arguments), repeat, id, base_url = move(base_url)]() mutable {
|
||||||
|
// 1. If id does not exist in global's map of active timers, then abort these steps.
|
||||||
|
if (!m_timers.contains(id))
|
||||||
|
return;
|
||||||
|
|
||||||
|
handler.visit(
|
||||||
|
// 2. If handler is a Function, then invoke handler given arguments with the callback this value set to thisArg. If this throws an exception, catch it, and report the exception.
|
||||||
|
[&](JS::Handle<WebIDL::CallbackType> const& callback) {
|
||||||
|
if (auto result = WebIDL::invoke_callback(*callback, &this_impl(), arguments); result.is_error())
|
||||||
|
report_exception(result, this_impl().realm());
|
||||||
|
},
|
||||||
|
// 3. Otherwise:
|
||||||
|
[&](DeprecatedString const& source) {
|
||||||
|
// 1. Assert: handler is a string.
|
||||||
|
// FIXME: 2. Perform HostEnsureCanCompileStrings(callerRealm, calleeRealm). If this throws an exception, catch it, report the exception, and abort these steps.
|
||||||
|
|
||||||
|
// 3. Let settings object be global's relevant settings object.
|
||||||
|
auto& settings_object = relevant_settings_object(this_impl());
|
||||||
|
|
||||||
|
// 4. Let base URL be initiating script's base URL.
|
||||||
|
// 5. Assert: base URL is not null, as initiating script is a classic script or a JavaScript module script.
|
||||||
|
VERIFY(base_url.has_value());
|
||||||
|
|
||||||
|
// 6. Let fetch options be a script fetch options whose cryptographic nonce is initiating script's fetch options's cryptographic nonce, integrity metadata is the empty string, parser metadata is "not-parser-inserted", credentials mode is initiating script's fetch options's credentials mode, and referrer policy is initiating script's fetch options's referrer policy.
|
||||||
|
// 7. Let script be the result of creating a classic script given handler, settings object, base URL, and fetch options.
|
||||||
|
auto script = ClassicScript::create(base_url->basename(), source, settings_object, *base_url);
|
||||||
|
|
||||||
|
// 8. Run the classic script script.
|
||||||
|
(void)script->run();
|
||||||
|
});
|
||||||
|
|
||||||
|
// 4. If id does not exist in global's map of active timers, then abort these steps.
|
||||||
|
if (!m_timers.contains(id))
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (repeat) {
|
||||||
|
// 5. If repeat is true, then perform the timer initialization steps again, given global, handler, timeout, arguments, true, and id.
|
||||||
|
case Repeat::Yes:
|
||||||
|
run_timer_initialization_steps(handler, timeout, move(arguments), repeat, id, move(base_url));
|
||||||
|
break;
|
||||||
|
|
||||||
|
// 6. Otherwise, remove global's map of active timers[id].
|
||||||
|
case Repeat::No:
|
||||||
|
m_timers.remove(id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// FIXME: 10. Increment nesting level by one.
|
||||||
|
// FIXME: 11. Set task's timer nesting level to nesting level.
|
||||||
|
|
||||||
|
// 12. Let completionStep be an algorithm step which queues a global task on the timer task source given global to run task.
|
||||||
|
JS::SafeFunction<void()> completion_step = [this, task = move(task)]() mutable {
|
||||||
|
queue_global_task(Task::Source::TimerTask, this_impl(), move(task));
|
||||||
|
};
|
||||||
|
|
||||||
|
// 13. Run steps after a timeout given global, "setTimeout/setInterval", timeout, completionStep, and id.
|
||||||
|
auto timer = Timer::create(this_impl(), timeout, move(completion_step), id);
|
||||||
|
m_timers.set(id, timer);
|
||||||
|
timer->start();
|
||||||
|
|
||||||
|
// 14. Return id.
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/IDAllocator.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <LibWeb/Bindings/PlatformObject.h>
|
#include <LibWeb/Bindings/PlatformObject.h>
|
||||||
#include <LibWeb/Fetch/Request.h>
|
#include <LibWeb/Fetch/Request.h>
|
||||||
#include <LibWeb/Forward.h>
|
#include <LibWeb/Forward.h>
|
||||||
|
@ -14,6 +17,9 @@
|
||||||
|
|
||||||
namespace Web::HTML {
|
namespace Web::HTML {
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/#timerhandler
|
||||||
|
using TimerHandler = Variant<JS::Handle<WebIDL::CallbackType>, DeprecatedString>;
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
|
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
|
||||||
class WindowOrWorkerGlobalScopeMixin {
|
class WindowOrWorkerGlobalScopeMixin {
|
||||||
public:
|
public:
|
||||||
|
@ -31,6 +37,24 @@ public:
|
||||||
void queue_microtask(WebIDL::CallbackType&);
|
void queue_microtask(WebIDL::CallbackType&);
|
||||||
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Value, StructuredSerializeOptions const&) const;
|
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Value, StructuredSerializeOptions const&) const;
|
||||||
JS::NonnullGCPtr<JS::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
|
JS::NonnullGCPtr<JS::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
|
||||||
|
|
||||||
|
i32 set_timeout(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
||||||
|
i32 set_interval(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
||||||
|
void clear_timeout(i32);
|
||||||
|
void clear_interval(i32);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void visit_edges(JS::Cell::Visitor&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class Repeat {
|
||||||
|
Yes,
|
||||||
|
No,
|
||||||
|
};
|
||||||
|
i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {}, Optional<AK::URL> base_url = {});
|
||||||
|
|
||||||
|
IDAllocator m_timer_id_allocator;
|
||||||
|
HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,8 @@ JS::ThrowCompletionOr<void> WorkerGlobalScope::initialize(JS::Realm& realm)
|
||||||
void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
|
void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
|
||||||
{
|
{
|
||||||
Base::visit_edges(visitor);
|
Base::visit_edges(visitor);
|
||||||
|
WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
|
||||||
|
|
||||||
visitor.visit(m_location.ptr());
|
visitor.visit(m_location.ptr());
|
||||||
visitor.visit(m_navigator.ptr());
|
visitor.visit(m_navigator.ptr());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue