VideoTrackList.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibJS/Runtime/VM.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/VideoTrackListPrototype.h>
  10. #include <LibWeb/HTML/EventNames.h>
  11. #include <LibWeb/HTML/VideoTrackList.h>
  12. namespace Web::HTML {
  13. VideoTrackList::VideoTrackList(JS::Realm& realm)
  14. : DOM::EventTarget(realm)
  15. , m_video_tracks(realm.heap())
  16. {
  17. }
  18. void VideoTrackList::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::VideoTrackListPrototype>(realm, "VideoTrackList"));
  22. }
  23. // https://html.spec.whatwg.org/multipage/media.html#dom-tracklist-item
  24. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> VideoTrackList::internal_get_own_property(JS::PropertyKey const& property_name) const
  25. {
  26. // To determine the value of an indexed property for a given index index in an AudioTrackList or VideoTrackList
  27. // object list, the user agent must return the AudioTrack or VideoTrack object that represents the indexth track
  28. // in list.
  29. if (property_name.is_number()) {
  30. if (auto index = property_name.as_number(); index < m_video_tracks.size()) {
  31. JS::PropertyDescriptor descriptor;
  32. descriptor.value = m_video_tracks.at(index);
  33. return descriptor;
  34. }
  35. }
  36. return Base::internal_get_own_property(property_name);
  37. }
  38. ErrorOr<void> VideoTrackList::add_track(Badge<HTMLMediaElement>, JS::NonnullGCPtr<VideoTrack> video_track)
  39. {
  40. TRY(m_video_tracks.try_append(video_track));
  41. video_track->set_video_track_list({}, this);
  42. return {};
  43. }
  44. void VideoTrackList::remove_all_tracks(Badge<HTMLMediaElement>)
  45. {
  46. m_video_tracks.clear();
  47. }
  48. // https://html.spec.whatwg.org/multipage/media.html#dom-videotracklist-gettrackbyid
  49. JS::GCPtr<VideoTrack> VideoTrackList::get_track_by_id(StringView id) const
  50. {
  51. // The AudioTrackList getTrackById(id) and VideoTrackList getTrackById(id) methods must return the first AudioTrack
  52. // or VideoTrack object (respectively) in the AudioTrackList or VideoTrackList object (respectively) whose identifier
  53. // is equal to the value of the id argument (in the natural order of the list, as defined above).
  54. auto it = m_video_tracks.find_if([&](auto const& video_track) {
  55. return video_track->id() == id;
  56. });
  57. // When no tracks match the given argument, the methods must return null.
  58. if (it == m_video_tracks.end())
  59. return nullptr;
  60. return *it;
  61. }
  62. // https://html.spec.whatwg.org/multipage/media.html#dom-videotracklist-selectedindex
  63. i32 VideoTrackList::selected_index() const
  64. {
  65. // The VideoTrackList selectedIndex attribute must return the index of the currently selected track, if any.
  66. auto it = m_video_tracks.find_if([&](auto const& video_track) {
  67. return video_track->selected();
  68. });
  69. // If the VideoTrackList object does not currently represent any tracks, or if none of the tracks are selected,
  70. // it must instead return −1.
  71. if (it == m_video_tracks.end())
  72. return -1;
  73. return static_cast<i32>(it.index());
  74. }
  75. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onchange
  76. void VideoTrackList::set_onchange(WebIDL::CallbackType* event_handler)
  77. {
  78. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  79. }
  80. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onchange
  81. WebIDL::CallbackType* VideoTrackList::onchange()
  82. {
  83. return event_handler_attribute(HTML::EventNames::change);
  84. }
  85. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onaddtrack
  86. void VideoTrackList::set_onaddtrack(WebIDL::CallbackType* event_handler)
  87. {
  88. set_event_handler_attribute(HTML::EventNames::addtrack, event_handler);
  89. }
  90. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onaddtrack
  91. WebIDL::CallbackType* VideoTrackList::onaddtrack()
  92. {
  93. return event_handler_attribute(HTML::EventNames::addtrack);
  94. }
  95. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onremovetrack
  96. void VideoTrackList::set_onremovetrack(WebIDL::CallbackType* event_handler)
  97. {
  98. set_event_handler_attribute(HTML::EventNames::removetrack, event_handler);
  99. }
  100. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onremovetrack
  101. WebIDL::CallbackType* VideoTrackList::onremovetrack()
  102. {
  103. return event_handler_attribute(HTML::EventNames::removetrack);
  104. }
  105. }