VideoTrackList.cpp 4.4 KB

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