Skip to content

Truly independent web browser

LibWeb: Keep media pending activity alive from the global

Shannon Boothbf7dc173c7f1
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index 86f80e340ef..27e6309ec51 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -257,7 +257,7 @@ void HTMLMediaElement::initialize_element()
m_remote_fetch_data->stream->set_may_idle(true);
detach_video_sink_edge();
- update_activity_root();
+ update_pending_activity();
});
m_document_observer->set_document_became_active([this]() {
@@ -267,7 +267,7 @@ void HTMLMediaElement::initialize_element()
m_remote_fetch_data->stream->set_may_idle(m_remote_fetch_data->download_is_suspended);
add_current_video_sink();
- update_activity_root();
+ update_pending_activity();
});
m_document_observer->set_document_visibility_state_observer([this](VisibilityState visibility_state) {
@@ -384,7 +384,7 @@ void HTMLMediaElement::attribute_changed(Utf16FlyString const& name, Optional<Ut
continue_fetching_the_resource_after_an_implementation_defined_event();
}
update_download_suspension();
- update_activity_root();
+ update_pending_activity();
}
}
@@ -430,6 +430,9 @@ void HTMLMediaElement::adopted_from(DOM::Document& old_document)
{
Base::adopted_from(old_document);
+ // NB: The element's relevant global follows its node document, so the old document's global stops keeping it alive.
+ relevant_window_or_worker_global_scope(old_document).set_has_pending_activity(*this, false);
+
// NB: The became-inactive transition synthesized when moving into a document that is not fully active stops any
// in-flight fetch, and the became-active transition synthesized when moving back into a fully active document
// resumes it. Without the former, the latter would find a fetch controller that is still live and start a
@@ -481,6 +484,8 @@ void HTMLMediaElement::adopted_from(DOM::Document& old_document)
m_screen_wake_lock.clear();
update_screen_wake_lock();
}
+
+ update_pending_activity();
}
void HTMLMediaElement::cancel_the_fetching_process()
@@ -597,7 +602,7 @@ void HTMLMediaElement::set_seeking(bool seeking)
m_seeking = seeking;
CSS::Invalidation::invalidate_style_after_media_seeking_state_change(*this, seeking);
update_download_suspension();
- update_activity_root();
+ update_pending_activity();
}
// https://html.spec.whatwg.org/multipage/media.html#dom-media-load
@@ -1871,7 +1876,7 @@ void HTMLMediaElement::load_local_resource(MediaProvider const& media_provider,
media_source->set_assigned_to_media_element({}, *this);
m_attached_media_source = media_source;
- update_activity_root();
+ update_pending_activity();
set_up_playback_manager_for_local(move(failure_callback));
// 4. Set the readyState attribute to "open".
@@ -2716,7 +2721,7 @@ void HTMLMediaElement::detach_attached_media_source()
return;
m_attached_media_source->detach_from_media_element({});
m_attached_media_source = nullptr;
- update_activity_root();
+ update_pending_activity();
}
// https://html.spec.whatwg.org/multipage/media.html#forget-the-media-element's-media-resource-specific-tracks
@@ -2771,7 +2776,7 @@ void HTMLMediaElement::set_ready_state(ReadyState ready_state)
update_screen_wake_lock();
update_representation();
update_download_suspension();
- update_activity_root();
+ update_pending_activity();
} };
// When the ready state of a media element whose networkState is not NETWORK_EMPTY changes, the user agent must
@@ -3401,7 +3406,7 @@ void HTMLMediaElement::set_paused(bool paused)
update_representation();
CSS::Invalidation::invalidate_style_after_media_paused_state_change(*this, paused);
- update_activity_root();
+ update_pending_activity();
}
void HTMLMediaElement::set_ended(bool ended)
@@ -3412,7 +3417,7 @@ void HTMLMediaElement::set_ended(bool ended)
m_ended = ended;
update_screen_wake_lock();
- update_activity_root();
+ update_pending_activity();
}
// https://html.spec.whatwg.org/multipage/media.html#dom-media-defaultplaybackrate
@@ -3516,13 +3521,13 @@ bool HTMLMediaElement::is_eligible_for_autoplay() const
void HTMLMediaElement::set_network_state(NetworkState network_state)
{
m_network_state = network_state;
- update_activity_root();
+ update_pending_activity();
}
void HTMLMediaElement::set_can_autoplay(bool can_autoplay)
{
m_can_autoplay = can_autoplay;
- update_activity_root();
+ update_pending_activity();
}
void HTMLMediaElement::set_delaying_the_load_event(bool delaying)
@@ -3531,7 +3536,7 @@ void HTMLMediaElement::set_delaying_the_load_event(bool delaying)
m_delaying_the_load_event.emplace(document());
else
m_delaying_the_load_event.clear();
- update_activity_root();
+ update_pending_activity();
}
// https://html.spec.whatwg.org/multipage/media.html#playing-the-media-resource
@@ -3570,12 +3575,9 @@ bool HTMLMediaElement::should_be_kept_alive() const
return is_eligible_for_autoplay() && m_ready_state != ReadyState::HaveEnoughData && m_network_state != NetworkState::Empty;
}
-void HTMLMediaElement::update_activity_root()
+void HTMLMediaElement::update_pending_activity()
{
- if (should_be_kept_alive())
- m_activity_root.take(*this);
- else
- m_activity_root.release();
+ relevant_window_or_worker_global_scope(*this).set_has_pending_activity(*this, should_be_kept_alive());
}
// https://html.spec.whatwg.org/multipage/media.html#allowed-to-play
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.h
index 43a3d16a6a9..b516901b22a 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -17,7 +17,6 @@
#include <LibCompositing/DisplayList/DisplayListResourceIds.h>
#include <LibCompositing/Video/VideoFrameHold.h>
#include <LibCore/Forward.h>
-#include <LibGC/ActivityRoot.h>
#include <LibGC/RootVector.h>
#include <LibGfx/Rect.h>
#include <LibJS/Forward.h>
@@ -344,7 +343,7 @@ private:
void set_can_autoplay(bool);
void set_delaying_the_load_event(bool);
bool should_be_kept_alive() const;
- void update_activity_root();
+ void update_pending_activity();
enum class PlaybackDirection : u8 {
Forwards,
@@ -475,8 +474,6 @@ private:
// https://html.spec.whatwg.org/multipage/media.html#delaying-the-load-event-flag
Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
- GC::ActivityRoot m_activity_root;
-
bool m_running_time_update_event_handler { false };
Optional<MonotonicTime> m_last_time_update_event_time;
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 36d75db25f7..025bfdb0f84 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -495,10 +495,14 @@ bool Internals::wrapper_is_preserved(JS::Object& object)
return Bindings::wrapper_is_preserved(*platform_object);
}
-bool Internals::has_pending_activity(JS::Object& object)
+bool Internals::has_pending_activity(JS::Object& object, GC::Ptr<DOM::Document> document)
{
auto* target = Bindings::impl_from<DOM::EventTarget>(&object);
- return target && HTML::relevant_window_or_worker_global_scope(object).has_pending_activity(*target);
+ if (!target)
+ return false;
+ if (document)
+ return document->window() && document->window()->has_pending_activity(*target);
+ return HTML::relevant_window_or_worker_global_scope(object).has_pending_activity(*target);
}
WebIDL::UnsignedLongLong Internals::message_port_pending_outgoing_message_count(HTML::MessagePort& port)
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index b9314a7e420..5e1cc718670 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -77,7 +77,7 @@ public:
WebIDL::UnsignedLongLong discard_decoded_image_frames();
WebIDL::ExceptionOr<void> mark_as_garbage(Utf16String const& variable_name);
bool wrapper_is_preserved(JS::Object&);
- bool has_pending_activity(JS::Object&);
+ bool has_pending_activity(JS::Object&, GC::Ptr<DOM::Document>);
WebIDL::UnsignedLongLong message_port_pending_outgoing_message_count(HTML::MessagePort&);
WebIDL::UnsignedLongLong html_collection_cache_generation(DOM::HTMLCollection&);
void fail_next_message_port_transfer(HTML::MessagePort&);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 0711406bcd3..8084ec7648c 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -36,7 +36,8 @@ interface Internals {
// This does not work for values stored only in optimized locals or registers.
undefined markAsGarbage(DOMString variableName);
boolean wrapperIsPreserved(object object);
- boolean hasPendingActivity(object object);
+ // Asks the global of the given document, or else the global of the object's realm.
+ boolean hasPendingActivity(object object, optional Document document);
unsigned long long messagePortPendingOutgoingMessageCount(MessagePort port);
unsigned long long htmlCollectionCacheGeneration(HTMLCollection collection);
undefined failNextMessagePortTransfer(MessagePort port);
diff --git a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp
index 02e5f62624f..c1c6e0d2b30 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp
+++ b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.cpp
@@ -18,6 +18,7 @@
#include <LibWeb/HTML/TimeRanges.h>
#include <LibWeb/HTML/TrackEvent.h>
#include <LibWeb/HTML/VideoTrackList.h>
+#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/MediaSourceExtensions/EventNames.h>
#include <LibWeb/MediaSourceExtensions/MediaSource.h>
@@ -336,10 +337,10 @@ bool SourceBuffer::updating() const
void SourceBuffer::set_updating(bool updating)
{
m_updating = updating;
- if (updating)
- m_activity_root.take(*this);
- else
- m_activity_root.release();
+
+ // NB: The operation's events may still begin playback, so they keep this, its MediaSource and the media element
+ // alive.
+ HTML::relevant_window_or_worker_global_scope(m_media_source->relevant_global_object()).set_has_pending_activity(*this, updating);
}
// https://w3c.github.io/media-source/#dom-sourcebuffer-buffered
diff --git a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h
index 106a63b02c3..8c239ec0a7b 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h
+++ b/Libraries/LibWeb/MediaSourceExtensions/SourceBuffer.h
@@ -9,7 +9,6 @@
#include <AK/NonnullRefPtr.h>
#include <AK/Types.h>
-#include <LibGC/ActivityRoot.h>
#include <LibMedia/Track.h>
#include <LibMediaClient/Forward.h>
#include <LibWeb/Bindings/SourceBuffer.h>
@@ -125,10 +124,6 @@ private:
// https://w3c.github.io/media-source/#dom-sourcebuffer-updating
bool m_updating { false };
- // NB: The operation's events may still begin playback, so they keep this, its MediaSource and the media element
- // alive.
- GC::ActivityRoot m_activity_root;
-
// https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset
double m_timestamp_offset_assigned_by_script { 0 };
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-adoption-moves-pending-activity.txt b/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-adoption-moves-pending-activity.txt
new file mode 100644
index 00000000000..98fddfde6fc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-adoption-moves-pending-activity.txt
@@ -0,0 +1,3 @@
+frame's global before adoption: true
+frame's global after adoption: false
+top-level global after adoption: true
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-adoption-moves-pending-activity.html b/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-adoption-moves-pending-activity.html
new file mode 100644
index 00000000000..ccf187c9c4a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-adoption-moves-pending-activity.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A loading media element's pending activity belongs to its relevant global, which follows its node document.
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const token = `adoption-moves-pending-activity-${crypto.randomUUID()}`;
+ const mediaURL = await server.createEcho("GET", `/${token}.webm`, { status: 404, wait_for_unblock: token });
+ const unblockURL = new URL(`/unblock/${token}`, mediaURL);
+ unblockURL.hostname = uniqueLocalhostHostname("adoption-moves-pending-activity-unblock");
+
+ const frame = document.createElement("iframe");
+ document.body.append(frame);
+ const video = frame.contentDocument.createElement("video");
+ video.src = mediaURL;
+ await server.waitForRequest(mediaURL);
+ println(`frame's global before adoption: ${internals.hasPendingActivity(video, frame.contentDocument)}`);
+
+ document.adoptNode(video);
+ println(`frame's global after adoption: ${internals.hasPendingActivity(video, frame.contentDocument)}`);
+ println(`top-level global after adoption: ${internals.hasPendingActivity(video, document)}`);
+
+ await fetch(unblockURL);
+ });
+</script>