AudioTrackList.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/AudioTrackListPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/HTML/AudioTrackList.h>
  11. #include <LibWeb/HTML/EventNames.h>
  12. namespace Web::HTML {
  13. GC_DEFINE_ALLOCATOR(AudioTrackList);
  14. AudioTrackList::AudioTrackList(JS::Realm& realm)
  15. : DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes)
  16. {
  17. }
  18. void AudioTrackList::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioTrackList);
  22. }
  23. // https://html.spec.whatwg.org/multipage/media.html#dom-tracklist-item
  24. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> AudioTrackList::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_audio_tracks.size()) {
  31. JS::PropertyDescriptor descriptor;
  32. descriptor.value = m_audio_tracks.at(index);
  33. return descriptor;
  34. }
  35. }
  36. return Base::internal_get_own_property(property_name);
  37. }
  38. void AudioTrackList::add_track(Badge<HTMLMediaElement>, GC::Ref<AudioTrack> audio_track)
  39. {
  40. m_audio_tracks.append(audio_track);
  41. audio_track->set_audio_track_list({}, this);
  42. }
  43. void AudioTrackList::remove_all_tracks(Badge<HTMLMediaElement>)
  44. {
  45. m_audio_tracks.clear();
  46. }
  47. // https://html.spec.whatwg.org/multipage/media.html#dom-audiotracklist-gettrackbyid
  48. GC::Ptr<AudioTrack> AudioTrackList::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_audio_tracks.find_if([&](auto const& audio_track) {
  54. return audio_track->id() == id;
  55. });
  56. // When no tracks match the given argument, the methods must return null.
  57. if (it == m_audio_tracks.end())
  58. return nullptr;
  59. return *it;
  60. }
  61. bool AudioTrackList::has_enabled_track() const
  62. {
  63. auto it = m_audio_tracks.find_if([&](auto const& audio_track) {
  64. return audio_track->enabled();
  65. });
  66. return it != m_audio_tracks.end();
  67. }
  68. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onchange
  69. void AudioTrackList::set_onchange(WebIDL::CallbackType* event_handler)
  70. {
  71. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  72. }
  73. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onchange
  74. WebIDL::CallbackType* AudioTrackList::onchange()
  75. {
  76. return event_handler_attribute(HTML::EventNames::change);
  77. }
  78. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onaddtrack
  79. void AudioTrackList::set_onaddtrack(WebIDL::CallbackType* event_handler)
  80. {
  81. set_event_handler_attribute(HTML::EventNames::addtrack, event_handler);
  82. }
  83. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onaddtrack
  84. WebIDL::CallbackType* AudioTrackList::onaddtrack()
  85. {
  86. return event_handler_attribute(HTML::EventNames::addtrack);
  87. }
  88. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onremovetrack
  89. void AudioTrackList::set_onremovetrack(WebIDL::CallbackType* event_handler)
  90. {
  91. set_event_handler_attribute(HTML::EventNames::removetrack, event_handler);
  92. }
  93. // https://html.spec.whatwg.org/multipage/media.html#handler-tracklist-onremovetrack
  94. WebIDL::CallbackType* AudioTrackList::onremovetrack()
  95. {
  96. return event_handler_attribute(HTML::EventNames::removetrack);
  97. }
  98. void AudioTrackList::visit_edges(JS::Cell::Visitor& visitor)
  99. {
  100. Base::visit_edges(visitor);
  101. visitor.visit(m_audio_tracks);
  102. }
  103. }