LibWeb: Support TrackEvent instances with an AudioTrack track type

This commit is contained in:
Timothy Flynn 2023-06-13 10:55:03 -04:00 committed by Andreas Kling
parent 11af5119b6
commit 6520a9a849
Notes: sideshowbarker 2024-07-17 05:58:46 +09:00
5 changed files with 33 additions and 16 deletions

View file

@ -27,6 +27,7 @@ static bool is_platform_object(Type const& type)
static constexpr Array types = {
"AbortSignal"sv,
"Attr"sv,
"AudioTrack"sv,
"Blob"sv,
"CanvasGradient"sv,
"CanvasPattern"sv,

View file

@ -1020,9 +1020,9 @@ WebIDL::ExceptionOr<void> HTMLMediaElement::process_media_data(Function<void(Str
// 7. Fire an event named addtrack at this VideoTrackList object, using TrackEvent, with the track attribute initialized to the new VideoTrack object.
TrackEventInit event_init {};
event_init.track = video_track;
event_init.track = JS::make_handle(video_track);
auto event = TRY(TrackEvent::create(realm, HTML::EventNames::addtrack, event_init));
auto event = TRY(TrackEvent::create(realm, HTML::EventNames::addtrack, move(event_init)));
m_video_tracks->dispatch_event(event);
}

View file

@ -10,19 +10,19 @@
namespace Web::HTML {
WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> TrackEvent::create(JS::Realm& realm, FlyString const& event_name, TrackEventInit const& event_init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> TrackEvent::create(JS::Realm& realm, FlyString const& event_name, TrackEventInit event_init)
{
return MUST_OR_THROW_OOM(realm.heap().allocate<TrackEvent>(realm, realm, event_name, event_init));
return MUST_OR_THROW_OOM(realm.heap().allocate<TrackEvent>(realm, realm, event_name, move(event_init)));
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> TrackEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, TrackEventInit const& event_init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> TrackEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, TrackEventInit event_init)
{
return create(realm, event_name, event_init);
return create(realm, event_name, move(event_init));
}
TrackEvent::TrackEvent(JS::Realm& realm, FlyString const& event_name, TrackEventInit const& event_init)
TrackEvent::TrackEvent(JS::Realm& realm, FlyString const& event_name, TrackEventInit event_init)
: DOM::Event(realm, event_name, event_init)
, m_track(event_init.track)
, m_track(move(event_init.track))
{
}
@ -34,4 +34,15 @@ JS::ThrowCompletionOr<void> TrackEvent::initialize(JS::Realm& realm)
return {};
}
Variant<Empty, JS::Handle<VideoTrack>, JS::Handle<AudioTrack>> TrackEvent::track() const
{
// FIXME: This is a bit awkward. When creating a nullable union, our IDL generator creates a type of
// Optional<Variant<...>>, using an empty Optional to represent null. But when retrieving the
// attribute, it expects a type of Variant<Empty, ...>, using Empty to represent null.
if (!m_track.has_value())
return Empty {};
return *m_track;
}
}

View file

@ -7,30 +7,34 @@
#pragma once
#include <AK/FlyString.h>
#include <AK/Optional.h>
#include <AK/Variant.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/DOM/Event.h>
namespace Web::HTML {
struct TrackEventInit : public DOM::EventInit {
JS::GCPtr<VideoTrack> track;
using TrackType = Optional<Variant<JS::Handle<VideoTrack>, JS::Handle<AudioTrack>>>;
TrackType track;
};
class TrackEvent : public DOM::Event {
WEB_PLATFORM_OBJECT(TrackEvent, DOM::Event);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> create(JS::Realm&, FlyString const& event_name, TrackEventInit const& event_init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> construct_impl(JS::Realm&, FlyString const& event_name, TrackEventInit const& event_init);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> create(JS::Realm&, FlyString const& event_name, TrackEventInit event_init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> construct_impl(JS::Realm&, FlyString const& event_name, TrackEventInit event_init);
// https://html.spec.whatwg.org/multipage/media.html#dom-trackevent-track
JS::GCPtr<VideoTrack> track() const { return m_track; }
Variant<Empty, JS::Handle<VideoTrack>, JS::Handle<AudioTrack>> track() const;
private:
TrackEvent(JS::Realm&, FlyString const& event_name, TrackEventInit const& event_init);
TrackEvent(JS::Realm&, FlyString const& event_name, TrackEventInit event_init);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
JS::GCPtr<VideoTrack> m_track;
TrackEventInit::TrackType m_track;
};
}

View file

@ -1,4 +1,5 @@
#import <DOM/Event.idl>
#import <HTML/AudioTrack.idl>
#import <HTML/VideoTrack.idl>
// https://html.spec.whatwg.org/multipage/media.html#trackevent
@ -7,10 +8,10 @@ interface TrackEvent : Event {
constructor(DOMString type, optional TrackEventInit eventInitDict = {});
// FIXME: Should be: (VideoTrack or AudioTrack or TextTrack)?
readonly attribute VideoTrack? track;
readonly attribute (VideoTrack or AudioTrack)? track;
};
dictionary TrackEventInit : EventInit {
// FIXME: Should be: (VideoTrack or AudioTrack or TextTrack)?
VideoTrack? track = null;
(VideoTrack or AudioTrack)? track = null;
};