VideoTrackList.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. GC_DEFINE_ALLOCATOR(VideoTrackList);
  14. VideoTrackList::VideoTrackList(JS::Realm& realm)
  15. : DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes)
  16. {
  17. }
  18. void VideoTrackList::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(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. void VideoTrackList::add_track(Badge<HTMLMediaElement>, GC::Ref<VideoTrack> video_track)
  39. {
  40. m_video_tracks.append(video_track);
  41. video_track->set_video_track_list({}, this);
  42. }
  43. void VideoTrackList::remove_all_tracks(Badge<HTMLMediaElement>)
  44. {
  45. m_video_tracks.clear();
  46. }
  47. // https://html.spec.whatwg.org/multipage/media.html#dom-videotracklist-gettrackbyid
  48. GC::Ptr<VideoTrack> VideoTrackList::get_track_by_id(StringView id) const
  49. {
  50. // The AudioTrackList getTrackById(id) and VideoTrackList getTrackById(id) methods must return the first AudioTrack
  51. // or VideoTrack object (respectively) in the AudioTrackList or VideoTrackList object (respectively) whose identifier
  52. // is equal to the value of the id argument (in the natural order of the list, as defined above).
  53. auto it = m_video_tracks.find_if([&](auto const& video_track) {
  54. return video_track->id() == id;
  55. });
  56. // When no tracks match the given argument, the methods must return null.
  57. if (it == m_video_tracks.end())
  58. return nullptr;
  59. return *it;
  60. }
  61. // https://html.spec.whatwg.org/multipage/media.html#dom-videotracklist-selectedindex
  62. i32 VideoTrackList::selected_index() const
  63. {
  64. // The VideoTrackList selectedIndex attribute must return the index of the currently selected track, if any.
  65. auto it = m_video_tracks.find_if([&](auto const& video_track) {
  66. return video_track->selected();
  67. });
  68. // If the VideoTrackList object does not currently represent any tracks, or if none of the tracks are selected,
  69. // it must instead return −1.
  70. if (it == m_video_tracks.end())
  71. return -1;
  72. return static_cast<i32>(it.index());
  73. }
  74. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onchange
  75. void VideoTrackList::set_onchange(WebIDL::CallbackType* event_handler)
  76. {
  77. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  78. }
  79. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onchange
  80. WebIDL::CallbackType* VideoTrackList::onchange()
  81. {
  82. return event_handler_attribute(HTML::EventNames::change);
  83. }
  84. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onaddtrack
  85. void VideoTrackList::set_onaddtrack(WebIDL::CallbackType* event_handler)
  86. {
  87. set_event_handler_attribute(HTML::EventNames::addtrack, event_handler);
  88. }
  89. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onaddtrack
  90. WebIDL::CallbackType* VideoTrackList::onaddtrack()
  91. {
  92. return event_handler_attribute(HTML::EventNames::addtrack);
  93. }
  94. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onremovetrack
  95. void VideoTrackList::set_onremovetrack(WebIDL::CallbackType* event_handler)
  96. {
  97. set_event_handler_attribute(HTML::EventNames::removetrack, event_handler);
  98. }
  99. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onremovetrack
  100. WebIDL::CallbackType* VideoTrackList::onremovetrack()
  101. {
  102. return event_handler_attribute(HTML::EventNames::removetrack);
  103. }
  104. void VideoTrackList::visit_edges(JS::Cell::Visitor& visitor)
  105. {
  106. Base::visit_edges(visitor);
  107. visitor.visit(m_video_tracks);
  108. }
  109. }