AudioTrackList.cpp 4.0 KB

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