Skip to content

Truly independent web browser

LibWeb: Let Rust ask for activation, focus and settled promises

Shannon Booth7b8a1c890b0d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
index fbe96788d8b..3c0b50d3269 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
@@ -21,9 +21,11 @@ unsafe extern "C" {
fn web_bindings_for_each_active_window(ctx: *mut c_void, on_window: unsafe extern "C" fn(*mut c_void, *mut c_void));
fn web_bindings_window_associated_document(window: *mut c_void) -> *mut c_void;
fn web_bindings_window_page_id(window: *mut c_void) -> u64;
+ fn web_bindings_window_has_transient_activation(window: *mut c_void) -> bool;
fn web_bindings_window_as_event_target(window: *mut c_void) -> *mut c_void;
fn web_bindings_document_has_navigable(document: *mut c_void) -> bool;
fn web_bindings_document_is_fully_active(document: *mut c_void) -> bool;
+ fn web_bindings_document_has_focus(document: *mut c_void) -> bool;
fn web_bindings_document_has_been_destroyed(document: *mut c_void) -> bool;
fn web_bindings_document_url(document: *mut c_void) -> liburl_rust::RustUrl;
fn web_bindings_worker_global_scope_from_global_object(global_object: *mut c_void) -> *mut c_void;
@@ -110,6 +112,12 @@ impl CppWindow {
unsafe { web_bindings_window_page_id(self.0.as_ptr()) }
}
+ /// https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
+ pub(crate) fn has_transient_activation(&self) -> bool {
+ // SAFETY: The Window is live.
+ unsafe { web_bindings_window_has_transient_activation(self.0.as_ptr()) }
+ }
+
/// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-document-window
pub(crate) fn associated_document(&self) -> CppDocument {
// SAFETY: The Window is live, and LibWeb returns its live associated Document.
@@ -124,6 +132,12 @@ impl CppDocument {
unsafe { web_bindings_document_is_fully_active(self.0.as_ptr()) }
}
+ /// https://html.spec.whatwg.org/multipage/interaction.html#has-focus-steps
+ pub(crate) fn has_focus(&self) -> bool {
+ // SAFETY: The Document is live.
+ unsafe { web_bindings_document_has_focus(self.0.as_ptr()) }
+ }
+
/// https://html.spec.whatwg.org/multipage/document-lifecycle.html#destroy-a-document
pub(crate) fn has_been_destroyed(&self) -> bool {
// SAFETY: The Document is live.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
index 9a1df63abe7..47601215040 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
@@ -62,6 +62,9 @@ unsafe extern "C" {
/// The HTML::TaskID of the task that the event loop of the realm's agent is running.
fn web_bindings_currently_running_task(realm: *mut c_void) -> u64;
+
+ /// Spins the event loop until the promise, a Promise, is no longer pending.
+ fn web_bindings_spin_until_promise_is_settled(promise: *mut c_void);
}
/// The task sources of HTML::Task::Source that the bindings queue tasks on.
@@ -74,6 +77,8 @@ pub(crate) enum TaskSource {
PostedMessage = 6,
FileReading = 13,
PerformanceTimeline = 15,
+ Clipboard = 17,
+ Permissions = 18,
FontLoading = 19,
Crypto = 27,
Storage = 29,
@@ -640,6 +645,12 @@ pub(crate) fn currently_running_task(realm: Gc<Realm>) -> TaskId {
TaskId(unsafe { web_bindings_currently_running_task(realm.as_ptr().cast()) })
}
+/// Spins the event loop until the promise is fulfilled or rejected.
+pub(crate) fn spin_until_promise_is_settled(promise: WebIdlPromise) {
+ // SAFETY: The promise is live.
+ unsafe { web_bindings_spin_until_promise_is_settled(promise_object(promise).as_ptr().cast()) };
+}
+
/// The [[Promise]] of a promise capability that LibWeb created, which is a Promise.
fn promise_object(promise: WebIdlPromise) -> Gc<Promise> {
promise
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 144e56acbdc..b2642e61b42 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -6,6 +6,7 @@
#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/HostObject.h>
+#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/Realm.h>
#include <LibURL/RustIntegration.h>
#include <LibWeb/Bindings/DOMException.h>
@@ -89,6 +90,7 @@ void web_bindings_queue_a_task(void* realm, u8 task_source, void* cell, void (*s
u64 web_bindings_queue_a_global_task(JSObject* global_object, u8 task_source, void* cell, void (*steps)(JSVM*, void*));
void web_bindings_remove_tasks(void* realm, ReadonlySpan<u64> task_ids);
u64 web_bindings_currently_running_task(void* realm);
+void web_bindings_spin_until_promise_is_settled(JSObject* promise);
void* web_bindings_create_callback_function(JSObject* callable, bool returns_promise);
JSCompletion web_bindings_invoke_callback_function(void* callback, bool has_this_value, JSValue this_value, ReadonlySpan<JSValue> arguments);
void* web_bindings_invoke_promise_callback_function(void* callback, bool has_this_value, JSValue this_value, ReadonlySpan<JSValue> arguments);
@@ -101,9 +103,11 @@ HTML::Window* web_bindings_window_from_global_object(JSObject* global_object);
void web_bindings_for_each_active_window(void* ctx, void (*on_window)(void*, HTML::Window*));
DOM::Document* web_bindings_window_associated_document(HTML::Window*);
u64 web_bindings_window_page_id(HTML::Window*);
+bool web_bindings_window_has_transient_activation(HTML::Window*);
DOM::EventTarget* web_bindings_window_as_event_target(HTML::Window*);
bool web_bindings_document_has_navigable(DOM::Document*);
bool web_bindings_document_is_fully_active(DOM::Document*);
+bool web_bindings_document_has_focus(DOM::Document*);
bool web_bindings_document_has_been_destroyed(DOM::Document*);
URL::FFI::RustUrl web_bindings_document_url(DOM::Document*);
HTML::WorkerGlobalScope* web_bindings_worker_global_scope_from_global_object(JSObject* global_object);
@@ -205,6 +209,11 @@ u64 web_bindings_window_page_id(HTML::Window* window)
return window->page().client().id().value();
}
+bool web_bindings_window_has_transient_activation(HTML::Window* window)
+{
+ return window->has_transient_activation();
+}
+
DOM::EventTarget* web_bindings_window_as_event_target(HTML::Window* window)
{
return window;
@@ -220,6 +229,11 @@ bool web_bindings_document_is_fully_active(DOM::Document* document)
return document->is_fully_active();
}
+bool web_bindings_document_has_focus(DOM::Document* document)
+{
+ return document->has_focus();
+}
+
bool web_bindings_document_has_been_destroyed(DOM::Document* document)
{
return document->has_been_destroyed();
@@ -355,11 +369,21 @@ void web_bindings_run_in_parallel(void* realm, void* cell, void (*steps)(JSVM*,
}));
}
+void web_bindings_spin_until_promise_is_settled(JSObject* promise)
+{
+ auto& js_promise = as<JS::Promise>(*JS::HostABI::object_from_abi(promise));
+ HTML::main_thread_event_loop().spin_until(GC::create_function(GC::Heap::the(), GC::captures(js_promise), [](auto& js_promise) {
+ return js_promise.state() != JS::Promise::State::Pending;
+ }));
+}
+
// NB: The task sources of the Rust bindings' TaskSource.
static_assert(to_underlying(HTML::Task::Source::Unspecified) == 0);
static_assert(to_underlying(HTML::Task::Source::Networking) == 3);
static_assert(to_underlying(HTML::Task::Source::FileReading) == 13);
static_assert(to_underlying(HTML::Task::Source::PerformanceTimeline) == 15);
+static_assert(to_underlying(HTML::Task::Source::Clipboard) == 17);
+static_assert(to_underlying(HTML::Task::Source::Permissions) == 18);
static_assert(to_underlying(HTML::Task::Source::Crypto) == 27);
static_assert(to_underlying(HTML::Task::Source::Storage) == 29);