| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/MediaSourcePrototype.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-18 12:13:48 +13:00
										 |  |  | #include <LibWeb/MediaSourceExtensions/EventNames.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | #include <LibWeb/MediaSourceExtensions/MediaSource.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-26 13:46:29 +01:00
										 |  |  | #include <LibWeb/MimeSniff/MimeType.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::MediaSourceExtensions { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(MediaSource); | 
					
						
							| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | WebIDL::ExceptionOr<GC::Ref<MediaSource>> MediaSource::construct_impl(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<MediaSource>(realm); | 
					
						
							| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MediaSource::MediaSource(JS::Realm& realm) | 
					
						
							|  |  |  |     : DOM::EventTarget(realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MediaSource::~MediaSource() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MediaSource::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::initialize(realm); | 
					
						
							|  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(MediaSource); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-18 12:13:48 +13:00
										 |  |  | // https://w3c.github.io/media-source/#dom-mediasource-onsourceopen
 | 
					
						
							|  |  |  | void MediaSource::set_onsourceopen(GC::Ptr<WebIDL::CallbackType> event_handler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_event_handler_attribute(EventNames::sourceopen, event_handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://w3c.github.io/media-source/#dom-mediasource-onsourceopen
 | 
					
						
							|  |  |  | GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceopen() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return event_handler_attribute(EventNames::sourceopen); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://w3c.github.io/media-source/#dom-mediasource-onsourceended
 | 
					
						
							|  |  |  | void MediaSource::set_onsourceended(GC::Ptr<WebIDL::CallbackType> event_handler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_event_handler_attribute(EventNames::sourceended, event_handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://w3c.github.io/media-source/#dom-mediasource-onsourceended
 | 
					
						
							|  |  |  | GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceended() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return event_handler_attribute(EventNames::sourceended); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://w3c.github.io/media-source/#dom-mediasource-onsourceclose
 | 
					
						
							|  |  |  | void MediaSource::set_onsourceclose(GC::Ptr<WebIDL::CallbackType> event_handler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_event_handler_attribute(EventNames::sourceclose, event_handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://w3c.github.io/media-source/#dom-mediasource-onsourceclose
 | 
					
						
							|  |  |  | GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceclose() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return event_handler_attribute(EventNames::sourceclose); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 13:46:29 +01:00
										 |  |  | // https://w3c.github.io/media-source/#dom-mediasource-istypesupported
 | 
					
						
							|  |  |  | bool MediaSource::is_type_supported(JS::VM&, String const& type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. If type is an empty string, then return false.
 | 
					
						
							|  |  |  |     if (type.is_empty()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. If type does not contain a valid MIME type string, then return false.
 | 
					
						
							|  |  |  |     auto mime_type = MimeSniff::MimeType::parse(type); | 
					
						
							|  |  |  |     if (!mime_type.has_value()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 3. If type contains a media type or media subtype that the MediaSource does not support, then
 | 
					
						
							|  |  |  |     //    return false.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 4. If type contains a codec that the MediaSource does not support, then return false.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 5. If the MediaSource does not support the specified combination of media type, media
 | 
					
						
							|  |  |  |     //    subtype, and codecs then return false.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Return true.
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-01 14:03:15 +01:00
										 |  |  | } |