TrackEvent.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/TrackEventPrototype.h>
  8. #include <LibWeb/HTML/TrackEvent.h>
  9. namespace Web::HTML {
  10. JS::NonnullGCPtr<TrackEvent> TrackEvent::create(JS::Realm& realm, FlyString const& event_name, TrackEventInit event_init)
  11. {
  12. return realm.heap().allocate<TrackEvent>(realm, realm, event_name, move(event_init));
  13. }
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<TrackEvent>> TrackEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, TrackEventInit event_init)
  15. {
  16. return create(realm, event_name, move(event_init));
  17. }
  18. TrackEvent::TrackEvent(JS::Realm& realm, FlyString const& event_name, TrackEventInit event_init)
  19. : DOM::Event(realm, event_name, event_init)
  20. , m_track(move(event_init.track))
  21. {
  22. }
  23. void TrackEvent::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::TrackEventPrototype>(realm, "TrackEvent"));
  27. }
  28. Variant<Empty, JS::Handle<VideoTrack>, JS::Handle<AudioTrack>> TrackEvent::track() const
  29. {
  30. // FIXME: This is a bit awkward. When creating a nullable union, our IDL generator creates a type of
  31. // Optional<Variant<...>>, using an empty Optional to represent null. But when retrieving the
  32. // attribute, it expects a type of Variant<Empty, ...>, using Empty to represent null.
  33. if (!m_track.has_value())
  34. return Empty {};
  35. return *m_track;
  36. }
  37. }