dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_media_element_3.lucb
#==============================================================================================
#
# html_media_element_3 - HTML::HTMLMediaElement: the resource selection algorithm
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLMediaElement.cpp lines 775-1118: SourceElementSelector (the
# "children" mode of the resource selection algorithm walking the media element's source
# element children), the children changed steps and the resource selection algorithm
# itself.
#
#==============================================================================================
# mark: SourceElementSelector ===================================================================
## Ported from HTMLMediaElement.cpp:786 Web::HTML::SourceElementSelector::SourceElementSelector.
pub func html_source_element_selector_construct(this: HtmlSourceElementSelector*, media_element: HtmlMediaElement*, candidate: HtmlSourceElement*):
this.m_media_element = media_element
this.m_candidate = candidate
## Ported from HTMLMediaElement.cpp:792 Web::HTML::SourceElementSelector::visit_edges (override).
pub func html_source_element_selector_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlSourceElementSelector*)base
gc.cell_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2[HtmlMediaElement](visitor, this.m_media_element)
gc.cell_visitor_visit_t_2[HtmlSourceElement](visitor, this.m_candidate)
gc.cell_visitor_visit_t[DomNode](visitor, this.m_previously_failed_candidate)
## process_candidate's failure callback: `[self = GC::make_root(this)](auto const&) { self->failed_with_elements(); }`.
func html_source_element_selector_load_failed(context: void*?, error_message: ak.String):
html_source_element_selector_failed_with_elements((HtmlSourceElementSelector*)(context else trap("failure callback: no selector")))
## Ported from HTMLMediaElement.cpp:800 Web::HTML::SourceElementSelector::process_candidate.
pub func html_source_element_selector_process_candidate(this: HtmlSourceElementSelector*):
# 2. ⌛ Process candidate: If candidate does not have a src attribute, or if its src attribute's value is the
# empty string, then end the synchronous section, and jump down to the failed with elements step below.
var candidate_src = ak.string_new()
if let maybe_src = dom_element_get_attribute(this.m_candidate.dom_element(), html_attribute_names_src):
candidate_src = maybe_src
if ak.string_is_empty(&candidate_src):
html_source_element_selector_failed_with_elements(this)
return
# FIXME: 3: ⌛ If candidate has a media attribute whose value does not match the environment,
# then end the synchronous section, and jump down to the failed with elements step below.
# 4. ⌛ Let urlRecord be the result of encoding-parsing a URL given candidate's src attribute's value, relative to candidate's node document when the src attribute was last changed.
let url_record = dom_document_encoding_parse_url(dom_node_document(this.m_candidate.dom_node()), ak.string_bytes_as_string_view(&candidate_src))
# 5. ⌛ If urlRecord is failure, then end the synchronous section, and jump down to the failed with elements step below.
if url_record == none:
html_source_element_selector_failed_with_elements(this)
return
let url = url_record else trap("unreachable")
# FIXME: 6. ⌛ If candidate has a type attribute whose value, when parsed as a MIME type (including any codecs described
# by the codecs parameter, for types that define that parameter), represents a type that the user agent knows
# it cannot render, then end the synchronous section, and jump down to the failed with elements step below.
# 7. ⌛ Set the currentSrc attribute to the result of applying the URL serializer to urlRecord.
this.m_media_element.m_current_src = web_url.url_serialize(&url)
# 8. End the synchronous section, continuing the remaining steps in parallel.
# 9. Run the resource fetch algorithm with urlRecord. If that algorithm returns without aborting this one, then
# the load failed.
html_media_element_load_url_resource(this.m_media_element, url, ak.Function1[ak.String, unit](call = html_source_element_selector_load_failed, context = this))
## Ported from HTMLMediaElement.cpp:839 Web::HTML::SourceElementSelector::process_next_candidate.
pub func html_source_element_selector_process_next_candidate(this: HtmlSourceElementSelector*):
let previously_failed_candidate = this.m_previously_failed_candidate else return
html_source_element_selector_wait_for_next_candidate(this, previously_failed_candidate)
## failed_with_elements' task: `[this] { ... }`.
func html_source_element_selector_failed_with_elements_task(context: void*?):
let this = (HtmlSourceElementSelector*)(context else trap("failed with elements task: no selector"))
_ = dom_event_target_dispatch_event(this.m_candidate.dom_event_target(), dom_event_create(html_element_realm(this.m_candidate.html_element()), html_event_names_error))
# 10. Await a stable state. The synchronous section consists of all the remaining steps of this algorithm until
# the algorithm says the synchronous section has ended. (Steps in synchronous sections are marked with ⌛.)
# FIXME: Also run the next steps within this task, instead of waiting for a stable state.
# This seems to match the intent in https://github.com/whatwg/html/issues/2882#issuecomment-1108531815.
# Update this if the spec is clarified in regard to this step.
# 11. ⌛ Forget the media element's media-resource-specific tracks.
html_media_element_forget_media_resource_specific_tracks(this.m_media_element)
html_source_element_selector_find_next_candidate(this, this.m_candidate.dom_node())
## Ported from HTMLMediaElement.cpp:848 Web::HTML::SourceElementSelector::failed_with_elements.
pub func html_source_element_selector_failed_with_elements(this: HtmlSourceElementSelector*):
# 9. Failed with elements: Queue a media element task given the media element to fire an event named error at candidate.
html_media_element_queue_a_media_element_task(this.m_media_element, ak.Function0[unit](call = html_source_element_selector_failed_with_elements_task, context = this))
## Ported from HTMLMediaElement.cpp:867 Web::HTML::SourceElementSelector::find_next_candidate.
pub func html_source_element_selector_find_next_candidate(this: HtmlSourceElementSelector*, previous_candidate: DomNode*):
# 12. ⌛ Find next candidate: Let candidate be null.
var candidate: HtmlSourceElement*? = none
# 13. ⌛ Search loop: If the node after pointer is the end of the list, then jump to the waiting step below.
let next_sibling = tree_node_next_sibling(previous_candidate)
if next_sibling == none:
html_source_element_selector_waiting(this, previous_candidate)
return
let next = next_sibling else trap("unreachable")
# 14. ⌛ If the node after pointer is a source element, let candidate be that element.
if let source_element = as_if_html_source_element(next):
candidate = source_element
# 15. ⌛ Advance pointer so that the node before pointer is now the node that was after pointer, and the node
# after pointer is the node after the node that used to be after pointer, if any.
# 16. ⌛ If candidate is null, jump back to the search loop step. Otherwise, jump back to the process candidate step.
if candidate == none:
html_source_element_selector_find_next_candidate(this, next)
return
this.m_candidate = candidate else trap("unreachable")
html_source_element_selector_process_candidate(this)
## waiting's task: `[this] { m_media_element->m_delaying_the_load_event.clear(); }`.
func html_source_element_selector_stop_delaying_the_load_event(context: void*?):
let this = (HtmlSourceElementSelector*)(context else trap("waiting task: no selector"))
html_media_element_clear_delaying_the_load_event(this.m_media_element)
## Ported from HTMLMediaElement.cpp:896 Web::HTML::SourceElementSelector::waiting.
pub func html_source_element_selector_waiting(this: HtmlSourceElementSelector*, previous_candidate: DomNode*):
# 17. ⌛ Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
this.m_media_element.m_network_state = HtmlMediaElementNetworkState.no_source
# 18. ⌛ Set the element's show poster flag to true.
html_media_element_set_show_poster(this.m_media_element, true)
# 19. ⌛ Queue a media element task given the media element to set the element's delaying-the-load-event flag
# to false. This stops delaying the load event.
html_media_element_queue_a_media_element_task(this.m_media_element, ak.Function0[unit](call = html_source_element_selector_stop_delaying_the_load_event, context = this))
# 20. End the synchronous section, continuing the remaining steps in parallel.
# 21. Wait until the node after pointer is a node other than the end of the list. (This step might wait forever.)
html_source_element_selector_wait_for_next_candidate(this, previous_candidate)
## Ported from HTMLMediaElement.cpp:916 Web::HTML::SourceElementSelector::wait_for_next_candidate.
pub func html_source_element_selector_wait_for_next_candidate(this: HtmlSourceElementSelector*, previous_candidate: DomNode*):
# NOTE: If there isn't another candidate to check, we implement the "waiting" step by returning until the media
# element's children have changed.
if tree_node_next_sibling(previous_candidate) == none:
this.m_previously_failed_candidate = previous_candidate
return
this.m_previously_failed_candidate = none
# FIXME: 22. Await a stable state. The synchronous section consists of all the remaining steps of this algorithm until
# the algorithm says the synchronous section has ended. (Steps in synchronous sections are marked with ⌛.)
# 23. ⌛ Set the element's delaying-the-load-event flag back to true (this delays the load event again, in case
# it hasn't been fired yet).
html_media_element_emplace_delaying_the_load_event(this.m_media_element, html_media_element_document(this.m_media_element))
# 24. ⌛ Set the networkState back to NETWORK_LOADING.
this.m_media_element.m_network_state = HtmlMediaElementNetworkState.loading
# 25. ⌛ Jump back to the find next candidate step above.
html_source_element_selector_find_next_candidate(this, previous_candidate)
# mark: Children changed and the resource selection algorithm ===================================
## Ported from HTMLMediaElement.cpp:948 Web::HTML::HTMLMediaElement::children_changed (override).
pub func html_media_element_children_changed_impl(base: DomNode*, metadata: DomNodeChildrenChangedMetadata):
let this = (HtmlMediaElement*)base
dom_element_children_changed_impl(base, metadata)
if let selector = this.m_source_element_selector:
html_source_element_selector_process_next_candidate(selector)
## The captures of the dedicated media source failure task that failed_with_media_provider and
## failed_with_attribute queue: `[this, error_message = move(error_message)]`.
struct HtmlMediaElementMediaSourceFailureTask:
var this: HtmlMediaElement*
var error_message: ak.String
## The task's steps: take pending play promises and run the dedicated media source failure steps.
static func run(context: void*?):
let captures = (HtmlMediaElementMediaSourceFailureTask*)(context else trap("media source failure task: no captures"))
var promises = html_media_element_take_pending_play_promises(captures.this)
html_media_element_handle_media_source_failure(captures.this, ak.vector_span(&promises), captures.error_message)
## select_resource's `failed_with_media_provider` lambda (mode object): `[this](auto error_message) { ... }`.
func html_media_element_failed_with_media_provider(context: void*?, error_message: ak.String):
let this = (HtmlMediaElement*)(context else trap("failed with media provider: no element"))
# 4. Failed with media provider: Reaching this step indicates that the media resource failed to load. Take pending play promises and queue
# a media element task given the media element to run the dedicated media source failure steps with the result.
let captures = new HtmlMediaElementMediaSourceFailureTask(this = this, error_message = error_message) else trap("out of memory")
html_media_element_queue_a_media_element_task(this, ak.Function0[unit](call = HtmlMediaElementMediaSourceFailureTask.run, context = captures))
# 5. Wait for the task queued by the previous step to have executed.
# AD-HOC: All calls to the failure steps immediately return, so we do not actually need to wait here.
## select_resource's `failed_with_attribute` lambda (mode attribute): `[this](auto error_message) { ... }`.
func html_media_element_failed_with_attribute(context: void*?, error_message: ak.String):
let this = (HtmlMediaElement*)(context else trap("failed with attribute: no element"))
# 6. Failed with attribute: Reaching this step indicates that the media resource failed to load or that the given URL could not be parsed. Take
# pending play promises and queue a media element task given the media element to run the dedicated media source failure steps with the result.
let captures = new HtmlMediaElementMediaSourceFailureTask(this = this, error_message = error_message) else trap("out of memory")
html_media_element_queue_a_media_element_task(this, ak.Function0[unit](call = HtmlMediaElementMediaSourceFailureTask.run, context = captures))
# 7. Wait for the task queued by the previous step to have executed.
# AD-HOC: All calls to the failure steps immediately return, so we do not actually need to wait here.
## Mode object's fetch task: `[this, failed_with_media_provider] { load_local_resource(assigned_media_provider_object(), ...); }`.
func html_media_element_select_resource_load_local_task(context: void*?):
let this = (HtmlMediaElement*)(context else trap("load local resource task: no element"))
html_media_element_load_local_resource(this, html_media_element_assigned_media_provider_object_const(this), ak.Function1[ak.String, unit](call = html_media_element_failed_with_media_provider, context = this))
## The captures of mode attribute's fetch task: `[this, url_record = move(url_record), failed_with_attribute]`.
struct HtmlMediaElementSelectResourceLoadUrlTask:
var this: HtmlMediaElement*
var url_record: web_url.Url?
## The task's steps: run the resource fetch algorithm with urlRecord, if it was obtained.
static func run(context: void*?):
let captures = (HtmlMediaElementSelectResourceLoadUrlTask*)(context else trap("load url resource task: no captures"))
if let url_record = captures.url_record:
html_media_element_load_url_resource(captures.this, url_record, ak.Function1[ak.String, unit](call = html_media_element_failed_with_attribute, context = captures.this))
return
## select_resource's task: the steps after "await a stable state" (`[this, &realm]() { ... }`).
func html_media_element_select_resource_task(context: void*?):
let this = (HtmlMediaElement*)(context else trap("select resource task: no element"))
let realm = html_media_element_realm(this)
# FIXME: 5. ⌛ If the media element's blocked-on-parser flag is false, then populate the list of pending text tracks.
var mode: HtmlSelectMode? = none
var candidate: HtmlSourceElement*? = none
# 6. ⌛ If the media element has an assigned media provider object, then let mode be object.
var has_assigned_media_provider_object = true
match html_media_element_assigned_media_provider_object_const(this):
.empty:
has_assigned_media_provider_object = false
_:
has_assigned_media_provider_object = true
let first_source_element = tree_node_first_child_of_type(this.dom_node(), &html_source_element_class)
if has_assigned_media_provider_object:
mode = HtmlSelectMode.object
# ⌛ Otherwise, if the media element has no assigned media provider object but has a src attribute, then let mode be attribute.
elif dom_element_has_attribute(this.dom_element(), html_attribute_names_src):
mode = HtmlSelectMode.attribute
# ⌛ Otherwise, if the media element does not have an assigned media provider object and does not have a src attribute, but does have
# a source element child, then let mode be children and let candidate be the first such source element child in tree order.
elif first_source_element != none:
mode = HtmlSelectMode.children
candidate = (HtmlSourceElement*?)first_source_element
# ⌛ Otherwise the media element has no assigned media provider object and has neither a src attribute nor a source element child:
else:
# 1. ⌛ Set the networkState to NETWORK_EMPTY.
this.m_network_state = HtmlMediaElementNetworkState.empty
# 2. ⌛ Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
html_media_element_clear_delaying_the_load_event(this)
# 3. End the synchronous section and return.
return
# 7. ⌛ Set the media element's networkState to NETWORK_LOADING.
this.m_network_state = HtmlMediaElementNetworkState.loading
# 8. ⌛ Queue a media element task given the media element to fire an event named loadstart at the media element.
html_media_element_queue_firing_an_event(this, html_event_names_loadstart)
# 9. Run the appropriate steps from the following list:
let selected_mode = mode else trap("unreachable")
match selected_mode:
# -> If mode is object
.object:
# 1. ⌛ Set the currentSrc attribute to the empty string.
this.m_current_src = ak.string_new()
# 2. End the synchronous section, continuing the remaining steps in parallel.
# 3. Run the resource fetch algorithm with the assigned media provider object. If that algorithm returns without aborting this one,
# then the load failed.
assert(has_assigned_media_provider_object)
html_media_element_queue_a_media_element_task(this, ak.Function0[unit](call = html_media_element_select_resource_load_local_task, context = this))
# 6. Return. The element won't attempt to load another resource until this algorithm is triggered again.
return
# -> If mode is attribute
.attribute:
# 1. ⌛ If the src attribute's value is the empty string, then end the synchronous section, and jump down to the failed with attribute step below.
let source = dom_element_get_attribute_value(this.dom_element(), html_attribute_names_src)
if ak.string_is_empty(&source):
html_media_element_failed_with_attribute(this, ak.string_from_str("The 'src' attribute is empty"))
return
# 2. ⌛ Let urlRecord be the result of encoding-parsing a URL given the src attribute's value,
# relative to the media element's node document when the src attribute was last changed.
let url_record = dom_document_encoding_parse_url(html_media_element_document(this), ak.string_bytes_as_string_view(&source))
# 3. ⌛ If urlRecord is not failure, then set the currentSrc attribute to the result of applying the URL serializer to urlRecord.
if let url = url_record:
this.m_current_src = web_url.url_serialize(&url)
# 4. End the synchronous section, continuing the remaining steps in parallel.
# 5. If urlRecord was obtained successfully, run the resource fetch algorithm with urlRecord. If that algorithm returns without aborting this one,
# then the load failed.
let captures = new HtmlMediaElementSelectResourceLoadUrlTask(this = this, url_record = url_record) else trap("out of memory")
html_media_element_queue_a_media_element_task(this, ak.Function0[unit](call = HtmlMediaElementSelectResourceLoadUrlTask.run, context = captures))
# 8. Return. The element won't attempt to load another resource until this algorithm is triggered again.
return
# -> Otherwise (mode is children)
.children:
let first_candidate = candidate else trap("VERIFY: candidate")
# 1. ⌛ Let pointer be a position defined by two adjacent nodes in the media element's child list, treating the start of the list (before the
# first child in the list, if any) and end of the list (after the last child in the list, if any) as nodes in their own right. One node is
# the node before pointer, and the other node is the node after pointer. Initially, let pointer be the position between the candidate node
# and the next node, if there are any, or the end of the list, if it is the last node.
#
# As nodes are inserted and removed into the media element, pointer must be updated as follows:
#
# If a new node is inserted between the two nodes that define pointer
# Let pointer be the point between the node before pointer and the new node. In other words, insertions at pointer go after pointer.
# If the node before pointer is removed
# Let pointer be the point between the node after pointer and the node before the node after pointer. In other words, pointer doesn't
# move relative to the remaining nodes.
# If the node after pointer is removed
# Let pointer be the point between the node before pointer and the node after the node before pointer. Just as with the previous case,
# pointer doesn't move relative to the remaining nodes.
# Other changes don't affect pointer.
# NOTE: We do not bother with maintaining this pointer. We inspect the DOM tree on the fly, rather than dealing
# with the headache of auto-updating this pointer as the DOM changes.
let selector = realm_create_html_source_element_selector(realm, this, first_candidate)
this.m_source_element_selector = selector
html_source_element_selector_process_candidate(selector)
_:
trap("VERIFY_NOT_REACHED: SelectMode")
## Ported from HTMLMediaElement.cpp:957 Web::HTML::HTMLMediaElement::select_resource.
# https://html.spec.whatwg.org/multipage/media.html#concept-media-load-algorithm
pub func html_media_element_select_resource(this: HtmlMediaElement*):
# 1. Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
this.m_network_state = HtmlMediaElementNetworkState.no_source
# 2. Set the element's show poster flag to true.
html_media_element_set_show_poster(this, true)
# 3. Set the media element's delaying-the-load-event flag to true (this delays the load event).
html_media_element_emplace_delaying_the_load_event(this, html_media_element_document(this))
# 4. Await a stable state, allowing the task that invoked this algorithm to continue. The synchronous section consists of all the remaining
# steps of this algorithm until the algorithm says the synchronous section has ended. (Steps in synchronous sections are marked with ⌛.)
html_media_element_queue_a_media_element_task(this, ak.Function0[unit](call = html_media_element_select_resource_task, context = this))