Skip to content

Truly independent web browser

LibWeb: Stop a window's capture sources when its document unloads

Luke Wilde634a0d962256
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 37833ddb7a2..e48b5ea4a28 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -6744,6 +6744,15 @@ void Document::run_unloading_cleanup_steps()
fully_exit_fullscreen();
PictureInPicture::run_unloading_cleanup_steps(*this);
+ // https://w3c.github.io/mediacapture-main/#dfn-stop-all-sources
+ // The User Agent MUST stop all sources of a globalObject in the following conditions:
+ // 1. If globalObject is a Window object and the unloading document cleanup steps are executed for its associated
+ // document.
+ if (auto navigator = m_window ? m_window->navigator_if_created() : nullptr) {
+ if (auto media_devices = navigator->media_devices_if_created())
+ media_devices->stop_all_sources();
+ }
+
// https://w3c.github.io/webdriver-bidi/#event-script-realmDestroyed
if (WebDriver::BiDi::is_active())
WebDriver::BiDi::unloading_document_cleanup_steps(*this);
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
index 54cd54d9ebf..8f6339bc445 100644
--- a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
@@ -890,6 +890,21 @@ void MediaDevices::end_tracks(GC::RootVector<GC::Ref<MediaStreamTrack>> tracks_t
}));
}
+// https://w3c.github.io/mediacapture-main/#dfn-stop-all-sources
+void MediaDevices::stop_all_sources()
+{
+ // 1. For each MediaStreamTrack object track whose relevant global object is globalObject, set track's
+ // [[ReadyState]] to "ended".
+ // 2. If globalObject is a Window, then for each source in globalObject's associated
+ // MediaDevices.[[mediaStreamTrackSources]], stop source.
+ // NB: The tracks stand in for their sources, and stopping a track also ends it.
+ // FIXME: End this global object's tracks that no MediaDevices made too.
+ for (auto const& weak_track : exchange(m_media_stream_track_sources, {})) {
+ if (auto track = weak_track.ptr())
+ track->stop();
+ }
+}
+
static Optional<Vector<Utf16String>> dom_string_values_from_variant(Variant<Utf16String, Vector<Utf16String>> const& value)
{
Vector<Utf16String> values;
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.h b/Libraries/LibWeb/MediaCapture/MediaDevices.h
index 186f87be932..a601998aa83 100644
--- a/Libraries/LibWeb/MediaCapture/MediaDevices.h
+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.h
@@ -50,6 +50,7 @@ public:
void system_focus_changed();
[[nodiscard]] static GC::Ref<MediaDevices> create(HTML::Window&);
+ void stop_all_sources();
MediaTrackSupportedConstraints get_supported_constraints();
void enumerate_devices(GC::Ref<WebIDL::Promise>);
diff --git a/Tests/LibWeb/Text/expected/MediaCapture/tracks-end-when-their-document-unloads.txt b/Tests/LibWeb/Text/expected/MediaCapture/tracks-end-when-their-document-unloads.txt
new file mode 100644
index 00000000000..6fc22bae350
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/MediaCapture/tracks-end-when-their-document-unloads.txt
@@ -0,0 +1,3 @@
+before the iframe navigates: live
+once it has navigated: ended
+a clone made after: ended
diff --git a/Tests/LibWeb/Text/input/MediaCapture/tracks-end-when-their-document-unloads.html b/Tests/LibWeb/Text/input/MediaCapture/tracks-end-when-their-document-unloads.html
new file mode 100644
index 00000000000..e8bb585cc08
--- /dev/null
+++ b/Tests/LibWeb/Text/input/MediaCapture/tracks-end-when-their-document-unloads.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A window's tracks end when its document unloads, as its iframe navigates, even while another realm holds them.
+ asyncTest(async done => {
+ await internals.setPermission("microphone", "granted");
+
+ const iframe = document.createElement("iframe");
+ document.body.appendChild(iframe);
+ // getUserMedia() waits for its document to have focus.
+ iframe.contentWindow.focus();
+ const stream = await iframe.contentWindow.navigator.mediaDevices.getUserMedia({ audio: true });
+ const track = stream.getAudioTracks()[0];
+ println(`before the iframe navigates: ${track.readyState}`);
+
+ const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+ iframe.srcdoc = "<p>The next document</p>";
+ await loaded;
+ println(`once it has navigated: ${track.readyState}`);
+ println(`a clone made after: ${track.clone().readyState}`);
+ done();
+ });
+</script>