AudioTrackList.cpp 4.0 KB

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