Truly independent web browser
LibWeb: Resume media resource selection after its source is removed
sideshowbarker53547a674e7c
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cppindex 6e829abd04d..f4ddda59aa4 100644--- a/Libraries/LibWeb/DOM/Node.cpp+++ b/Libraries/LibWeb/DOM/Node.cpp@@ -1675,7 +1675,7 @@ void Node::remove(bool suppress_observers)auto affects_elements = ChildrenChangedMetadata::AffectsElements::No;if (document().has_valid_html_collection_caches())affects_elements = mutation_affects_elements(*this);- ChildrenChangedMetadata metadata { ChildrenChangedMetadata::Type::Removal, *this, affects_elements };+ ChildrenChangedMetadata metadata { ChildrenChangedMetadata::Type::Removal, *this, affects_elements, old_previous_sibling };parent->children_changed(metadata);parent->invalidate_html_collection_caches_in_ancestors(affects_elements);diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.hindex af7c63f1cd1..9e8e5f3618d 100644--- a/Libraries/LibWeb/DOM/Node.h+++ b/Libraries/LibWeb/DOM/Node.h@@ -447,6 +447,8 @@ public:Type type {};GC::Ref<Node> node;AffectsElements affects_elements { AffectsElements::No };+ // For a removal, the sibling that was before the removed node, if any.+ GC::Ptr<Node> old_previous_sibling {};};// FIXME: It would be good if we could always provide this metadata for use in optimizations.virtual void children_changed(ChildrenChangedMetadata const&) { }diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex ddfab695863..079d469d3ed 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -1050,6 +1050,7 @@ public:SourceElementSelector(GC::Ref<HTMLMediaElement> media_element, GC::Ref<HTMLSourceElement> candidate): m_media_element(media_element), m_candidate(candidate)+ , m_node_before_pointer(candidate){}@@ -1058,7 +1059,7 @@ public:Base::visit_edges(visitor);visitor.visit(m_media_element);visitor.visit(m_candidate);- visitor.visit(m_previously_failed_candidate);+ visitor.visit(m_node_before_pointer);}void process_candidate()@@ -1116,10 +1117,23 @@ public:void process_next_candidate(){- if (!m_previously_failed_candidate)+ if (!m_waiting)return;- wait_for_next_candidate(*m_previously_failed_candidate);+ wait_for_next_candidate();+ }++ // 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.+ void node_removed(DOM::Node const& node, GC::Ptr<DOM::Node> old_previous_sibling)+ {+ if (m_node_before_pointer.ptr() == &node)+ m_node_before_pointer = old_previous_sibling;+ }++ void all_children_removed()+ {+ m_node_before_pointer = nullptr;}private:@@ -1138,40 +1152,43 @@ private:// 11. ⌛ Forget the media element's media-resource-specific tracks.selector.m_media_element->forget_media_resource_specific_tracks();- selector.find_next_candidate(selector.m_candidate);+ selector.find_next_candidate();}));}- void find_next_candidate(GC::Ref<DOM::Node> previous_candidate)+ GC::Ptr<DOM::Node> node_after(GC::Ptr<DOM::Node> node_before_pointer) const+ {+ return node_before_pointer ? node_before_pointer->next_sibling() : m_media_element->first_child();+ }++ void find_next_candidate(){// 12. ⌛ Find next candidate: Let candidate be null.GC::Ptr<HTMLSourceElement> candidate;- // 13. ⌛ Search loop: If the node after pointer is the end of the list, then jump to the waiting step below.- auto* next_sibling = previous_candidate->next_sibling();- if (!next_sibling) {- waiting(previous_candidate);- return;- }+ while (!candidate) {+ // 13. ⌛ Search loop: If the node after pointer is the end of the list, then jump to the waiting step below.+ auto node_after_pointer = node_after(m_node_before_pointer);+ if (!node_after_pointer) {+ waiting();+ return;+ }- // 14. ⌛ If the node after pointer is a source element, let candidate be that element.- if (auto* source_element = as_if<HTMLSourceElement>(next_sibling))- candidate = source_element;+ // 14. ⌛ If the node after pointer is a source element, let candidate be that element.+ candidate = as_if<HTMLSourceElement>(*node_after_pointer);- // 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.+ // 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.+ m_node_before_pointer = node_after_pointer;- // 16. ⌛ If candidate is null, jump back to the search loop step. Otherwise, jump back to the process candidate step.- if (!candidate) {- find_next_candidate(*next_sibling);- return;+ // 16. ⌛ If candidate is null, jump back to the search loop step. Otherwise, jump back to the process candidate step.}m_candidate = *candidate;process_candidate();}- void waiting(GC::Ref<DOM::Node> previous_candidate)+ void waiting(){// 17. ⌛ Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value.m_media_element->set_network_state(HTMLMediaElement::NetworkState::NoSource);@@ -1188,19 +1205,19 @@ private:// 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.)- wait_for_next_candidate(previous_candidate);+ wait_for_next_candidate();}- void wait_for_next_candidate(GC::Ref<DOM::Node> previous_candidate)+ void wait_for_next_candidate(){// 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 (previous_candidate->next_sibling() == nullptr) {- m_previously_failed_candidate = previous_candidate;+ if (!node_after(m_node_before_pointer)) {+ m_waiting = true;return;}- m_previously_failed_candidate = nullptr;+ m_waiting = false;// 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 ⌛.)@@ -1213,12 +1230,13 @@ private:m_media_element->set_network_state(HTMLMediaElement::NetworkState::Loading);// 25. ⌛ Jump back to the find next candidate step above.- find_next_candidate(previous_candidate);+ find_next_candidate();}GC::Ref<HTMLMediaElement> m_media_element;GC::Ref<HTMLSourceElement> m_candidate;- GC::Ptr<DOM::Node> m_previously_failed_candidate;+ bool m_waiting { false };+ GC::Ptr<DOM::Node> m_node_before_pointer;};GC_DEFINE_ALLOCATOR(SourceElementSelector);@@ -1227,8 +1245,13 @@ void HTMLMediaElement::children_changed(ChildrenChangedMetadata const& metadata){Base::children_changed(metadata);- if (m_source_element_selector)+ if (m_source_element_selector) {+ if (metadata.type == ChildrenChangedMetadata::Type::Removal)+ m_source_element_selector->node_removed(*metadata.node, metadata.old_previous_sibling);+ else if (metadata.type == ChildrenChangedMetadata::Type::AllChildrenRemoved)+ m_source_element_selector->all_children_removed();m_source_element_selector->process_next_candidate();+ }}// https://html.spec.whatwg.org/multipage/media.html#concept-media-load-algorithmdiff --git a/Tests/LibWeb/Text/expected/HTML/media-source-element-replaced-from-error-listener.txt b/Tests/LibWeb/Text/expected/HTML/media-source-element-replaced-from-error-listener.txtnew file mode 100644index 00000000000..a421ee0bae0--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/media-source-element-replaced-from-error-listener.txt@@ -0,0 +1,2 @@+error at the held source+loaded metadata: 160x120diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-element-replaced-while-waiting.txt b/Tests/LibWeb/Text/expected/HTML/media-source-element-replaced-while-waiting.txtnew file mode 100644index 00000000000..21ae7908b70--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/media-source-element-replaced-while-waiting.txt@@ -0,0 +1 @@+loaded metadata: 160x120diff --git a/Tests/LibWeb/Text/input/HTML/media-source-element-replaced-from-error-listener.html b/Tests/LibWeb/Text/input/HTML/media-source-element-replaced-from-error-listener.htmlnew file mode 100644index 00000000000..75431e1661e--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/media-source-element-replaced-from-error-listener.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ // The error event of a source that failed removes that source and appends a new one, before resource selection+ // looks for the next candidate.+ const video = document.createElement("video");+ const held = document.createElement("source");+ held.addEventListener("error", () => {+ println("error at the held source");+ held.remove();+ const source = document.createElement("source");+ source.src = "../../../Assets/h264-reordered-frames.mp4";+ video.appendChild(source);+ });+ video.appendChild(held);+ document.body.appendChild(video);+ video.addEventListener("loadedmetadata", () => {+ println(`loaded metadata: ${video.videoWidth}x${video.videoHeight}`);+ done();+ });+ });+</script>diff --git a/Tests/LibWeb/Text/input/HTML/media-source-element-replaced-while-waiting.html b/Tests/LibWeb/Text/input/HTML/media-source-element-replaced-while-waiting.htmlnew file mode 100644index 00000000000..0f584d0965f--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/media-source-element-replaced-while-waiting.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ // Resource selection fails on a source without a src and waits for another one. That source is then removed+ // and a new one appended, as consent scripts do while they hold a source back.+ const video = document.createElement("video");+ const held = document.createElement("source");+ video.appendChild(held);+ document.body.appendChild(video);+ video.addEventListener("loadedmetadata", () => {+ println(`loaded metadata: ${video.videoWidth}x${video.videoHeight}`);+ done();+ });+ setTimeout(() => {+ held.remove();+ const source = document.createElement("source");+ source.src = "../../../Assets/h264-reordered-frames.mp4";+ video.appendChild(source);+ }, 100);+ });+</script>