Truly independent web browser
LibWeb: Implement ProgressEvent in Rust
Shannon Boothcb9daa1a0b6d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/xhr/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/xhr/exports.rsindex 601b2835051..6b32768d298 100644--- a/Libraries/LibWeb/Bindings/Rust/src/xhr/exports.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/xhr/exports.rs@@ -13,8 +13,10 @@ use ak::Utf16String;use libjs_rust::api::{JSVM, vm_from_abi};use super::form_data::{Entry, FormData, FormDataEntryValue, StringOrBlob, create_an_entry};+use super::progress_event::{ProgressEvent, ProgressEventInit};use crate::bindings::conversions::UsvString;-use crate::bindings::interface::{Ref, allocate, cell_from_abi, cell_into_abi};+use crate::bindings::interface::{Ref, allocate, cell_from_abi, cell_into_abi, realm_arguments};+use crate::dom::event::EventInit;use crate::file_api::exports::any_blob_from_abi;#[repr(C)]@@ -171,3 +173,32 @@ pub unsafe extern "C" fn web_html_create_an_entry(};EntryAbi::from_entry(&create_an_entry(vm, &name, value, None))}++/// # Safety+///+/// `vm` must be the live VM, `realm` a live realm, and `event_name` a raw string that C++ owns a reference to.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn web_rust_xhr_create_progress_event(+ vm: *mut c_void,+ realm: *mut c_void,+ event_name: usize,+ length_computable: bool,+ loaded: f64,+ total: f64,+) -> *mut c_void {+ // SAFETY: The caller passes the live VM and a live realm, and hands over the name.+ let ((vm, realm), event_name) = unsafe { (realm_arguments(vm, realm), Utf16String::from_raw_owned(event_name)) };+ let event_init = ProgressEventInit {+ parent: EventInit {+ bubbles: false,+ cancelable: false,+ composed: false,+ },+ length_computable,+ loaded,+ total,+ };+ ProgressEvent::create(vm, realm, &event_name, event_init)+ .event()+ .as_ptr()+}diff --git a/Libraries/LibWeb/Bindings/Rust/src/xhr/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/xhr/mod.rsindex a32f847ec59..a5c96a46edc 100644--- a/Libraries/LibWeb/Bindings/Rust/src/xhr/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/xhr/mod.rs@@ -4,9 +4,11 @@* SPDX-License-Identifier: BSD-2-Clause*/-//! The FormData interface of the XMLHttpRequest Standard, https://xhr.spec.whatwg.org/#interface-formdata+//! The XMLHttpRequest Standard, https://xhr.spec.whatwg.org/#[cfg(not(test))]pub(crate) mod exports;#[cfg(not(test))]pub(crate) mod form_data;+#[cfg(not(test))]+pub(crate) mod progress_event;diff --git a/Libraries/LibWeb/Bindings/Rust/src/xhr/progress_event.rs b/Libraries/LibWeb/Bindings/Rust/src/xhr/progress_event.rsnew file mode 100644index 00000000000..4c19cd0ef09--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/xhr/progress_event.rs@@ -0,0 +1,94 @@+/*+ * Copyright (c) 2021, the SerenityOS developers.+ * Copyright (c) 2022, Andreas Kling <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! ProgressEvent, https://xhr.spec.whatwg.org/#interface-progressevent++use core::ffi::c_void;+use core::ptr::NonNull;++use ak::Utf16String;+use libjs_rust::api::{Gc, Realm, Trace, Vm};++use crate::bindings::event_target::CppEvent;+use crate::bindings::interface::{Ref, Wrappable, allocate};+use crate::bindings::{CallContext, WebIdlResult};+use crate::dom::event::EventInit;++// https://xhr.spec.whatwg.org/#dictdef-progresseventinit+pub(crate) struct ProgressEventInit {+ pub(crate) parent: EventInit,+ pub(crate) length_computable: bool,+ pub(crate) loaded: f64,+ pub(crate) total: f64,+}++// https://xhr.spec.whatwg.org/#interface-progressevent+#[derive(Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct ProgressEvent {+ event: CppEvent,+ length_computable: bool,+ loaded: f64,+ total: f64,+}++impl Wrappable for ProgressEvent {+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {+ self.event.as_cpp_parent()+ }+}++// https://xhr.spec.whatwg.org/#interface-progressevent+impl ProgressEvent {+ pub(crate) fn create(+ vm: &Vm,+ realm: Gc<Realm>,+ event_name: &Utf16String,+ event_init: ProgressEventInit,+ ) -> Ref<ProgressEvent> {+ let event = CppEvent::create_for_owner(+ realm,+ event_name,+ event_init.parent.bubbles,+ event_init.parent.cancelable,+ event_init.parent.composed,+ );+ let object = allocate(vm, |_| ProgressEvent {+ event,+ length_computable: event_init.length_computable,+ loaded: event_init.loaded,+ total: event_init.total,+ });+ object.event.set_owner(object);+ object+ }++ pub(crate) fn construct(+ cx: &mut CallContext,+ r#type: Utf16String,+ event_init_dict: ProgressEventInit,+ ) -> WebIdlResult<Ref<ProgressEvent>> {+ Ok(Self::create(cx.vm(), cx.realm(), &r#type, event_init_dict))+ }++ pub(crate) fn event(&self) -> &CppEvent {+ &self.event+ }++ pub(crate) fn length_computable(&self, _: &mut CallContext) -> WebIdlResult<bool> {+ Ok(self.length_computable)+ }++ pub(crate) fn loaded(&self, _: &mut CallContext) -> WebIdlResult<f64> {+ Ok(self.loaded)+ }++ pub(crate) fn total(&self, _: &mut CallContext) -> WebIdlResult<f64> {+ Ok(self.total)+ }+}diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex 64ea9a69b97..62af9d0fb45 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -1003,7 +1003,6 @@ set(SOURCESWebVTT/VTTRegion.cppXHR/EventNames.cppXHR/FormData.cpp- XHR/ProgressEvent.cppXHR/XMLHttpRequest.cppXHR/XMLHttpRequestEventTarget.cppXHR/XMLHttpRequestUpload.cppdiff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex db32cb8aa1d..720b8ce6eb1 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -1300,7 +1300,6 @@ class VTTRegion;namespace Web::XHR {class FormData;-class ProgressEvent;class XMLHttpRequest;class XMLHttpRequestEventTarget;class XMLHttpRequestUpload;diff --git a/Libraries/LibWeb/XHR/ProgressEvent.cpp b/Libraries/LibWeb/XHR/ProgressEvent.cppdeleted file mode 100644index 7166e669eb3..00000000000--- a/Libraries/LibWeb/XHR/ProgressEvent.cpp+++ /dev/null@@ -1,30 +0,0 @@-/*- * Copyright (c) 2022, Andreas Kling <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <LibGC/Heap.h>-#include <LibWeb/XHR/ProgressEvent.h>--namespace Web::XHR {--GC_DEFINE_ALLOCATOR(ProgressEvent);--GC::Ref<ProgressEvent> ProgressEvent::create(Utf16FlyString const& event_name, ProgressEventInit const& event_init,- HighResolutionTime::DOMHighResTimeStamp time_stamp)-{- return GC::Heap::the().allocate<ProgressEvent>(event_name, event_init, time_stamp);-}--ProgressEvent::ProgressEvent(Utf16FlyString const& event_name, ProgressEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)- : Event(event_name, event_init, time_stamp)- , m_length_computable(event_init.length_computable)- , m_loaded(event_init.loaded)- , m_total(event_init.total)-{-}--ProgressEvent::~ProgressEvent() = default;--}diff --git a/Libraries/LibWeb/XHR/ProgressEvent.h b/Libraries/LibWeb/XHR/ProgressEvent.hdeleted file mode 100644index 758c756fd0f..00000000000--- a/Libraries/LibWeb/XHR/ProgressEvent.h+++ /dev/null@@ -1,40 +0,0 @@-/*- * Copyright (c) 2021, the SerenityOS developers.- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/Utf16FlyString.h>-#include <LibWeb/Bindings/ProgressEvent.h>-#include <LibWeb/DOM/Event.h>-#include <LibWebCommon/WebIDL/Types.h>--namespace Web::XHR {--using ProgressEventInit = Bindings::ProgressEventInit;--class ProgressEvent final : public DOM::Event {- WEB_WRAPPABLE(ProgressEvent, DOM::Event);- GC_DECLARE_ALLOCATOR(ProgressEvent);--public:- [[nodiscard]] static GC::Ref<ProgressEvent> create(Utf16FlyString const& event_name, ProgressEventInit const&,- HighResolutionTime::DOMHighResTimeStamp);-- virtual ~ProgressEvent() override;-- bool length_computable() const { return m_length_computable; }- WebIDL::Double loaded() const { return m_loaded; }- WebIDL::Double total() const { return m_total; }--private:- ProgressEvent(Utf16FlyString const& event_name, ProgressEventInit const&, HighResolutionTime::DOMHighResTimeStamp);-- bool m_length_computable { false };- WebIDL::Double m_loaded { 0 };- WebIDL::Double m_total { 0 };-};--}diff --git a/Libraries/LibWeb/XHR/ProgressEvent.idl b/Libraries/LibWeb/XHR/ProgressEvent.idlindex d0b6f833769..88f45852bf1 100644--- a/Libraries/LibWeb/XHR/ProgressEvent.idl+++ b/Libraries/LibWeb/XHR/ProgressEvent.idl@@ -1,5 +1,5 @@// https://xhr.spec.whatwg.org/#interface-progressevent-[Exposed=(Window,Worker)]+[Exposed=(Window,Worker), ImplementedIn=Rust]interface ProgressEvent : Event {constructor(DOMString type, optional ProgressEventInit eventInitDict = {});diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cppindex 5f0fbe2d3d6..431333ce9d6 100644--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp@@ -14,6 +14,7 @@#include <AK/QuickSort.h>#include <LibGC/Heap.h>#include <LibHTTP/Method.h>+#include <LibJS/HostClassBuilder.h>#include <LibJS/Runtime/ArrayBuffer.h>#include <LibJS/Runtime/Completion.h>#include <LibJS/Runtime/FunctionObject.h>@@ -64,13 +65,18 @@#include <LibWeb/WebIDL/DOMException.h>#include <LibWeb/WebIDL/ExceptionOr.h>#include <LibWeb/XHR/EventNames.h>-#include <LibWeb/XHR/ProgressEvent.h>#include <LibWeb/XHR/XMLHttpRequest.h>#include <LibWeb/XHR/XMLHttpRequestUpload.h>#include <LibWeb/XML/XMLDocumentBuilder.h>namespace Web::XHR {+extern "C" {++// Defined in Bindings/Rust/src/xhr/exports.rs.+void* web_rust_xhr_create_progress_event(JSVM*, void* realm, FlatPtr event_name, bool length_computable, double loaded, double total);+}+GC_DEFINE_ALLOCATOR(XMLHttpRequest);GC::Ref<XMLHttpRequest> XMLHttpRequest::create(GC::Ref<DOM::EventTarget> relevant_global_object)@@ -184,12 +190,9 @@ static void fire_progress_event(JS::Object& relevant_global_object, XMLHttpReque// To fire a progress event named e at target, given transmitted and length, means to fire an event named e at target, using ProgressEvent,// with the loaded attribute initialized to transmitted, and if length is not 0, with the lengthComputable attribute initialized to true// and the total attribute initialized to length.- ProgressEventInit event_init {};- event_init.length_computable = length;- event_init.loaded = transmitted;- event_init.total = length;- target.dispatch_event(*ProgressEvent::create(event_name, event_init,- HighResolutionTime::current_high_resolution_time(relevant_global_object)));+ auto& realm = HTML::relevant_realm(relevant_global_object);+ auto* event = web_rust_xhr_create_progress_event(JS::HostABI::vm_to_abi(realm.vm()), &realm, event_name.to_utf16_string().into_raw(), length != 0, transmitted, length);+ target.dispatch_event(*static_cast<DOM::Event*>(static_cast<GC::Cell*>(event)));}// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext