MediaSource.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/MediaSourcePrototype.h>
  8. #include <LibWeb/MediaSourceExtensions/EventNames.h>
  9. #include <LibWeb/MediaSourceExtensions/MediaSource.h>
  10. #include <LibWeb/MimeSniff/MimeType.h>
  11. namespace Web::MediaSourceExtensions {
  12. GC_DEFINE_ALLOCATOR(MediaSource);
  13. WebIDL::ExceptionOr<GC::Ref<MediaSource>> MediaSource::construct_impl(JS::Realm& realm)
  14. {
  15. return realm.create<MediaSource>(realm);
  16. }
  17. MediaSource::MediaSource(JS::Realm& realm)
  18. : DOM::EventTarget(realm)
  19. {
  20. }
  21. MediaSource::~MediaSource() = default;
  22. void MediaSource::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(MediaSource);
  26. }
  27. // https://w3c.github.io/media-source/#dom-mediasource-onsourceopen
  28. void MediaSource::set_onsourceopen(GC::Ptr<WebIDL::CallbackType> event_handler)
  29. {
  30. set_event_handler_attribute(EventNames::sourceopen, event_handler);
  31. }
  32. // https://w3c.github.io/media-source/#dom-mediasource-onsourceopen
  33. GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceopen()
  34. {
  35. return event_handler_attribute(EventNames::sourceopen);
  36. }
  37. // https://w3c.github.io/media-source/#dom-mediasource-onsourceended
  38. void MediaSource::set_onsourceended(GC::Ptr<WebIDL::CallbackType> event_handler)
  39. {
  40. set_event_handler_attribute(EventNames::sourceended, event_handler);
  41. }
  42. // https://w3c.github.io/media-source/#dom-mediasource-onsourceended
  43. GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceended()
  44. {
  45. return event_handler_attribute(EventNames::sourceended);
  46. }
  47. // https://w3c.github.io/media-source/#dom-mediasource-onsourceclose
  48. void MediaSource::set_onsourceclose(GC::Ptr<WebIDL::CallbackType> event_handler)
  49. {
  50. set_event_handler_attribute(EventNames::sourceclose, event_handler);
  51. }
  52. // https://w3c.github.io/media-source/#dom-mediasource-onsourceclose
  53. GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceclose()
  54. {
  55. return event_handler_attribute(EventNames::sourceclose);
  56. }
  57. // https://w3c.github.io/media-source/#dom-mediasource-istypesupported
  58. bool MediaSource::is_type_supported(JS::VM&, String const& type)
  59. {
  60. // 1. If type is an empty string, then return false.
  61. if (type.is_empty())
  62. return false;
  63. // 2. If type does not contain a valid MIME type string, then return false.
  64. auto mime_type = MimeSniff::MimeType::parse(type);
  65. if (!mime_type.has_value())
  66. return false;
  67. // FIXME: 3. If type contains a media type or media subtype that the MediaSource does not support, then
  68. // return false.
  69. // FIXME: 4. If type contains a codec that the MediaSource does not support, then return false.
  70. // FIXME: 5. If the MediaSource does not support the specified combination of media type, media
  71. // subtype, and codecs then return false.
  72. // 6. Return true.
  73. return true;
  74. }
  75. }