Skip to content

Truly independent web browser

LibWeb: Implement BufferedChangeEvent in Rust

Shannon Booth4268dbe49e0c
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index af1720a297f..2dbb3daf8fc 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -253,6 +253,16 @@ pub(crate) mod largest_contentful_paint;
clippy::unnecessary_wraps,
reason = "the bindings call every attribute and operation as one that can throw"
)]
+pub(crate) mod media_source_extensions;
+#[cfg(not(test))]
+#[cfg_attr(
+ not(web_idl_bindings),
+ allow(dead_code, reason = "only the generated bindings use the interfaces")
+)]
+#[allow(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every attribute and operation as one that can throw"
+)]
pub(crate) mod navigation_timing;
#[cfg(not(test))]
#[cfg_attr(
diff --git a/Libraries/LibWeb/Bindings/Rust/src/media_source_extensions/buffered_change_event.rs b/Libraries/LibWeb/Bindings/Rust/src/media_source_extensions/buffered_change_event.rs
new file mode 100644
index 00000000000..47d9d05df39
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/media_source_extensions/buffered_change_event.rs
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2024, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! BufferedChangeEvent, https://w3c.github.io/media-source/#bufferedchangeevent-interface
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::Trace;
+
+use crate::bindings::conversions::CppInterface;
+use crate::bindings::event_target::CppEvent;
+use crate::bindings::interface::{Ref, Wrappable, allocate};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::dom::event::EventInit;
+
+/// The dictionary BufferedChangeEventInit of MediaSourceExtensions/BufferedChangeEvent.idl.
+pub(crate) struct BufferedChangeEventInit {
+ pub(crate) parent: EventInit,
+ #[allow(dead_code, reason = "the attributes that return the ranges are not implemented")]
+ pub(crate) added_ranges: Option<CppInterface>,
+ #[allow(dead_code, reason = "the attributes that return the ranges are not implemented")]
+ pub(crate) removed_ranges: Option<CppInterface>,
+}
+
+// https://w3c.github.io/media-source/#bufferedchangeevent-interface
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct BufferedChangeEvent {
+ event: CppEvent,
+}
+
+impl Wrappable for BufferedChangeEvent {
+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {
+ self.event.as_cpp_parent()
+ }
+}
+
+impl BufferedChangeEvent {
+ pub(crate) fn construct(
+ cx: &mut CallContext,
+ r#type: Utf16String,
+ event_init_dict: BufferedChangeEventInit,
+ ) -> WebIdlResult<Ref<BufferedChangeEvent>> {
+ let event_init = event_init_dict.parent;
+ let event = CppEvent::create_for_owner(
+ cx.realm(),
+ &r#type,
+ event_init.bubbles,
+ event_init.cancelable,
+ event_init.composed,
+ );
+ let object = allocate(cx.vm(), |_| BufferedChangeEvent { event });
+ object.event.set_owner(object);
+ Ok(object)
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/media_source_extensions/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/media_source_extensions/mod.rs
new file mode 100644
index 00000000000..70c7de4f828
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/media_source_extensions/mod.rs
@@ -0,0 +1,9 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Media Source Extensions, https://w3c.github.io/media-source/
+
+pub(crate) mod buffered_change_event;
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index fdbd2110c5e..2f374fc42ff 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -664,7 +664,6 @@ set(SOURCES
MediaSession/ChapterInformation.cpp
MediaSession/MediaMetadata.cpp
MediaSession/MediaSession.cpp
- MediaSourceExtensions/BufferedChangeEvent.cpp
MediaSourceExtensions/EventNames.cpp
MediaSourceExtensions/ManagedMediaSource.cpp
MediaSourceExtensions/ManagedSourceBuffer.cpp
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index bc332d21a74..6914183547c 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -946,7 +946,6 @@ class MediaSession;
namespace Web::MediaSourceExtensions {
-class BufferedChangeEvent;
class ManagedMediaSource;
class ManagedSourceBuffer;
class MediaSource;
diff --git a/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.cpp b/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.cpp
deleted file mode 100644
index 03b0ddbae29..00000000000
--- a/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (c) 2024, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/MediaSourceExtensions/BufferedChangeEvent.h>
-
-namespace Web::MediaSourceExtensions {
-
-GC_DEFINE_ALLOCATOR(BufferedChangeEvent);
-
-GC::Ref<BufferedChangeEvent> BufferedChangeEvent::create(AK::FlyString const& type, BufferedChangeEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
-{
- return GC::Heap::the().allocate<BufferedChangeEvent>(type, event_init, time_stamp);
-}
-
-GC::Ref<BufferedChangeEvent> BufferedChangeEvent::create(Utf16String const& type, BufferedChangeEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
-{
- return GC::Heap::the().allocate<BufferedChangeEvent>(Utf16FlyString::from_utf16(type.utf16_view()), event_init, time_stamp);
-}
-
-BufferedChangeEvent::BufferedChangeEvent(AK::FlyString const& type, BufferedChangeEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
- : DOM::Event(type, event_init, time_stamp)
-{
-}
-
-BufferedChangeEvent::BufferedChangeEvent(Utf16FlyString const& type, BufferedChangeEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
- : DOM::Event(type, event_init, time_stamp)
-{
-}
-
-BufferedChangeEvent::~BufferedChangeEvent() = default;
-
-}
diff --git a/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.h b/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.h
deleted file mode 100644
index da6192ab0cd..00000000000
--- a/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2024, Jelle Raaijmakers <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Utf16FlyString.h>
-#include <AK/Utf16String.h>
-#include <LibJS/Forward.h>
-#include <LibWeb/Bindings/BufferedChangeEvent.h>
-#include <LibWeb/DOM/Event.h>
-#include <LibWeb/HTML/TimeRanges.h>
-#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>
-
-namespace Web::MediaSourceExtensions {
-
-using BufferedChangeEventInit = Bindings::BufferedChangeEventInit;
-
-// https://w3c.github.io/media-source/#bufferedchangeevent-interface
-class BufferedChangeEvent : public DOM::Event {
- WEB_WRAPPABLE(BufferedChangeEvent, DOM::Event);
- GC_DECLARE_ALLOCATOR(BufferedChangeEvent);
-
-public:
- [[nodiscard]] static GC::Ref<BufferedChangeEvent> create(FlyString const& type, BufferedChangeEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
- [[nodiscard]] static GC::Ref<BufferedChangeEvent> create(Utf16String const& type, BufferedChangeEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
-private:
- BufferedChangeEvent(FlyString const& type, BufferedChangeEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
- BufferedChangeEvent(Utf16FlyString const& type, BufferedChangeEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual ~BufferedChangeEvent() override;
-};
-
-}
diff --git a/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.idl b/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.idl
index 5115262f5a9..6ccac3a26c8 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.idl
+++ b/Libraries/LibWeb/MediaSourceExtensions/BufferedChangeEvent.idl
@@ -1,5 +1,5 @@
// https://w3c.github.io/media-source/#dom-bufferedchangeevent
-[Exposed=(Window,DedicatedWorker)]
+[Exposed=(Window,DedicatedWorker), ImplementedIn=Rust]
interface BufferedChangeEvent : Event {
constructor(DOMString type, optional BufferedChangeEventInit eventInitDict = {});