Truly independent web browser
LibWebView+LibWeb+WebContent: Show which tabs hold a capture stream
Luke Wilde1b4f221904b4
diff --git a/Libraries/LibAudio/ClientConnection.cpp b/Libraries/LibAudio/ClientConnection.cppindex 6385d695155..bf5c9cade7b 100644--- a/Libraries/LibAudio/ClientConnection.cpp+++ b/Libraries/LibAudio/ClientConnection.cpp@@ -51,6 +51,18 @@ bool ClientConnection::capture_is_pending_for_testing()return any_of(connection->m_capture_sources, [](auto const& entry) { return !entry.value.specification.has_value(); });}+size_t ClientConnection::capture_stream_count_for_testing()+{+ auto const& connection = *s_connection;+ if (!connection)+ return 0;++ size_t stream_count = 0;+ for (auto const& [stream_id, source] : connection->m_capture_sources)+ stream_count += source.stream_count;+ return stream_count;+}+ClientConnection::ClientConnection(NonnullOwnPtr<IPC::Transport> transport): IPC::ConnectionToServer<AudioClientEndpoint, AudioServerEndpoint>(*this, move(transport)), m_event_loop(Core::EventLoop::current())diff --git a/Libraries/LibAudio/ClientConnection.h b/Libraries/LibAudio/ClientConnection.hindex 4c0da6bcd95..027ce6c1a5c 100644--- a/Libraries/LibAudio/ClientConnection.h+++ b/Libraries/LibAudio/ClientConnection.h@@ -48,6 +48,7 @@ public:static ErrorOr<NonnullRefPtr<ClientConnection>> acquire();// Whether a capture waits on the server for its device, which another capture of the same device would join.static bool capture_is_pending_for_testing();+ static size_t capture_stream_count_for_testing();explicit ClientConnection(NonnullOwnPtr<IPC::Transport>);virtual ~ClientConnection() override;diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex d9086840676..f87b8c59337 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -2680,6 +2680,43 @@ bool Internals::tab_is_heard_playing_audio()return window().associated_document().page().client().page_did_request_tab_is_heard_playing_audio_for_testing();}+bool Internals::tab_is_using_microphone()+{+ return window().associated_document().page().client().page_did_request_tab_is_using_microphone_for_testing();+}++bool Internals::tab_is_capturing_audio()+{+ return window().associated_document().page().client().page_did_request_tab_is_capturing_audio_for_testing();+}++void Internals::report_audio_capture_of_unknown_page(bool capturing)+{+ report_audio_capture_of_page(NumericLimits<u64>::max(), capturing);+}++WebIDL::UnsignedLongLong Internals::picture_in_picture_window_page_id()+{+ auto window = page().picture_in_picture_controller().open_window();+ VERIFY(window);+ return window->traversable().active_document()->page().client().id().value();+}++void Internals::report_audio_capture_of_page(WebIDL::UnsignedLongLong page_id, bool capturing)+{+ page().client().page_did_report_audio_capture_for_testing(Web::PageId { page_id }, capturing);+}++void Internals::retire_picture_in_picture_window()+{+ page().client().page_did_retire_picture_in_picture_window_for_testing();+}++WebIDL::UnsignedLongLong Internals::audio_capture_stream_count()+{+ return Audio::ClientConnection::capture_stream_count_for_testing();+}+WebIDL::UnsignedLongLong Internals::tab_audibility_change_count(){return window().associated_document().page().client().page_did_request_tab_audibility_change_count_for_testing();diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex 99d2247bca8..3f5bea5d362 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -378,6 +378,13 @@ public:bool document_is_playing_audio(DOM::Document&);bool tab_is_playing_audio();bool tab_is_heard_playing_audio();+ bool tab_is_using_microphone();+ bool tab_is_capturing_audio();+ void report_audio_capture_of_unknown_page(bool capturing);+ WebIDL::UnsignedLongLong picture_in_picture_window_page_id();+ void report_audio_capture_of_page(WebIDL::UnsignedLongLong page_id, bool capturing);+ void retire_picture_in_picture_window();+ WebIDL::UnsignedLongLong audio_capture_stream_count();WebIDL::UnsignedLongLong tab_audibility_change_count();bool media_element_video_sink_is_ticking(HTML::HTMLMediaElement&);void set_media_element_ready_state(HTML::HTMLMediaElement&, u16 ready_state);diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex b4dbc29330c..d31538bbb47 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -508,6 +508,21 @@ interface Internals {boolean tabIsPlayingAudio();// Whether the AudioServer has recently heard this window's tab, whatever the tab shows.boolean tabIsHeardPlayingAudio();+ // Whether the UI process shows this window's tab as using the microphone: while the AudioServer holds a capture stream+ // for the tab, and for a while after.+ boolean tabIsUsingMicrophone();+ // Whether the AudioServer holds a capture stream for this window's tab, whatever the tab shows.+ boolean tabIsCapturingAudio();+ // Has the UI process hear of a capture stream of this process whose label names none of its pages.+ undefined reportAudioCaptureOfUnknownPage(boolean capturing);+ // The ID of the page that shows this page's Picture-in-Picture window.+ unsigned long long pictureInPictureWindowPageId();+ // Has the UI process hear of a capture stream of this process labelled for the page, as this process chose to.+ undefined reportAudioCaptureOfPage(unsigned long long pageId, boolean capturing);+ // Has the UI process retire this page's Picture-in-Picture window, as if this process went on to ignore that.+ undefined retirePictureInPictureWindow();+ // How many streams this process's tracks hold of the capture sources it has open.+ unsigned long long audioCaptureStreamCount();// How many times the UI process has changed whether this window's tab is shown as playing audio.unsigned long long tabAudibilityChangeCount();boolean mediaElementVideoSinkIsTicking(HTMLMediaElement element);diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 3fb01963114..1e7ee374472 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -918,6 +918,10 @@ public:virtual bool page_did_request_has_populated_document_for_testing([[maybe_unused]] HTML::CrossProcessId navigable_id) { return false; }virtual bool page_did_request_tab_is_playing_audio_for_testing() { return false; }virtual bool page_did_request_tab_is_heard_playing_audio_for_testing() { return false; }+ virtual bool page_did_request_tab_is_using_microphone_for_testing() { return false; }+ virtual bool page_did_request_tab_is_capturing_audio_for_testing() { return false; }+ virtual void page_did_report_audio_capture_for_testing([[maybe_unused]] Web::PageId labelled_page_id, [[maybe_unused]] bool capturing) { }+ virtual void page_did_retire_picture_in_picture_window_for_testing() { }virtual u64 page_did_request_tab_audibility_change_count_for_testing() { return 0; }virtual bool page_did_request_capture_session_history_snapshot_for_testing() { return false; }virtual bool page_did_request_restore_session_history_snapshot_for_testing() { return false; }diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.hindex 634ad1f8607..93779745ee4 100644--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h@@ -39,6 +39,7 @@ public:bool is_waiting_for_window_to_open() const { return m_window_awaiting_open.has_value(); }bool has_open_window() const { return m_open_window != nullptr; }+ GC::Ptr<PictureInPictureWindow> open_window() const { return m_open_window; }private:explicit PictureInPictureController(Page&);diff --git a/Libraries/LibWebView/AudioServerActivity.cpp b/Libraries/LibWebView/AudioServerActivity.cppnew file mode 100644index 00000000000..3576a69864b--- /dev/null+++ b/Libraries/LibWebView/AudioServerActivity.cpp@@ -0,0 +1,87 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibWebView/AudioServerActivity.h>+#include <LibWebView/AudioServerControlClient.h>++namespace WebView {++AudioServerActivity::AudioServerActivity(OnAudibilityChange on_audibility_change, OnCaptureChange on_capture_change)+ : m_on_audibility_change(move(on_audibility_change))+ , m_on_capture_change(move(on_capture_change))+{+}++AudioServerActivity::~AudioServerActivity()+{+ stop_watching();+}++void AudioServerActivity::watch(AudioServerControlClient& controller, AudibilityFilter audibility_filter, CaptureFilter capture_filter)+{+ stop_watching();+ forget_everything();+ m_controller = controller.make_weak_ptr<AudioServerControlClient>();+ controller.on_playback_group_audibility_changed = [this, filter = move(audibility_filter)](u64 tab_id, Audio::PlaybackGroupId group, Audio::Audibility audibility) {+ if (filter(tab_id, group, audibility))+ set_audibility(group, audibility);+ };+ controller.on_capture_activity_changed = [this, filter = move(capture_filter)](u64 tab_id, Audio::PlaybackGroupId group, bool capturing) {+ if (filter(tab_id))+ set_capturing(group, capturing);+ };+ controller.on_death = [this] {+ forget_everything();+ };+}++void AudioServerActivity::forget_audibility(Audio::PlaybackGroupId group)+{+ set_audibility(group, Audio::Audibility::Inaudible);+}++void AudioServerActivity::stop_watching()+{+ // A replaced AudioServer's death can be noticed after its replacement has begun reporting.+ if (auto controller = m_controller.strong_ref()) {+ controller->on_playback_group_audibility_changed = nullptr;+ controller->on_capture_activity_changed = nullptr;+ controller->on_death = nullptr;+ }+ m_controller.clear();+}++void AudioServerActivity::set_audibility(Audio::PlaybackGroupId group, Audio::Audibility audibility)+{+ switch (audibility) {+ case Audio::Audibility::Audible:+ if (m_audible_groups.set(group) != HashSetResult::InsertedNewEntry)+ return;+ break;+ case Audio::Audibility::Inaudible:+ if (!m_audible_groups.remove(group))+ return;+ break;+ }+ m_on_audibility_change(group, audibility);+}++void AudioServerActivity::set_capturing(Audio::PlaybackGroupId group, bool capturing)+{+ auto changed = capturing ? m_capturing_groups.set(group) == HashSetResult::InsertedNewEntry : m_capturing_groups.remove(group);+ if (changed)+ m_on_capture_change();+}++void AudioServerActivity::forget_everything()+{+ for (auto group : exchange(m_audible_groups, {}))+ m_on_audibility_change(group, Audio::Audibility::Inaudible);+ if (!exchange(m_capturing_groups, {}).is_empty())+ m_on_capture_change();+}++}diff --git a/Libraries/LibWebView/AudioServerActivity.h b/Libraries/LibWebView/AudioServerActivity.hnew file mode 100644index 00000000000..b120e672a0a--- /dev/null+++ b/Libraries/LibWebView/AudioServerActivity.h@@ -0,0 +1,53 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Function.h>+#include <AK/HashTable.h>+#include <AK/WeakPtr.h>+#include <LibAudio/Audibility.h>+#include <LibAudio/PlaybackGroupId.h>+#include <LibWebView/Export.h>+#include <LibWebView/Forward.h>++namespace WebView {++// Which groups a process's AudioServer has recently heard, and which hold capture streams. Only the watched AudioServer+// is heard: what it reported is forgotten once it dies or another is watched instead, and one that has been replaced is+// heard from no more.+class WEBVIEW_API AudioServerActivity {+public:+ using OnAudibilityChange = Function<void(Audio::PlaybackGroupId, Audio::Audibility)>;+ using OnCaptureChange = Function<void()>;+ // Whether to take the AudioServer's report.+ using AudibilityFilter = Function<bool(u64 tab_id, Audio::PlaybackGroupId, Audio::Audibility)>;+ using CaptureFilter = Function<bool(u64 tab_id)>;++ AudioServerActivity(OnAudibilityChange, OnCaptureChange);+ ~AudioServerActivity();++ void watch(AudioServerControlClient&, AudibilityFilter, CaptureFilter);+ void forget_audibility(Audio::PlaybackGroupId);++ // The renderer labels its capture streams, so these name the pages it says it captures for.+ HashTable<Audio::PlaybackGroupId> const& capturing_groups() const { return m_capturing_groups; }+ void set_capturing_for_testing(Audio::PlaybackGroupId group, bool capturing) { set_capturing(group, capturing); }++private:+ void stop_watching();+ void set_audibility(Audio::PlaybackGroupId, Audio::Audibility);+ void set_capturing(Audio::PlaybackGroupId, bool);+ void forget_everything();++ OnAudibilityChange m_on_audibility_change;+ OnCaptureChange m_on_capture_change;+ WeakPtr<AudioServerControlClient> m_controller;+ HashTable<Audio::PlaybackGroupId> m_audible_groups;+ HashTable<Audio::PlaybackGroupId> m_capturing_groups;+};++}diff --git a/Libraries/LibWebView/AudioServerAudibility.cpp b/Libraries/LibWebView/AudioServerAudibility.cppdeleted file mode 100644index 435167f5545..00000000000--- a/Libraries/LibWebView/AudioServerAudibility.cpp+++ /dev/null@@ -1,72 +0,0 @@-/*- * Copyright (c) 2026-present, the Ladybird developers.- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <LibWebView/AudioServerAudibility.h>-#include <LibWebView/AudioServerControlClient.h>--namespace WebView {--AudioServerAudibility::AudioServerAudibility(OnChange on_change)- : m_on_change(move(on_change))-{-}--AudioServerAudibility::~AudioServerAudibility()-{- stop_watching();-}--void AudioServerAudibility::watch(AudioServerControlClient& controller, Filter filter)-{- stop_watching();- forget_everything();- m_controller = controller.make_weak_ptr<AudioServerControlClient>();- controller.on_playback_group_audibility_changed = [this, filter = move(filter)](u64 tab_id, Audio::PlaybackGroupId group, Audio::Audibility audibility) {- if (filter(tab_id, group, audibility))- set(group, audibility);- };- controller.on_death = [this] {- forget_everything();- };-}--void AudioServerAudibility::forget(Audio::PlaybackGroupId group)-{- set(group, Audio::Audibility::Inaudible);-}--void AudioServerAudibility::stop_watching()-{- // A replaced AudioServer's death can be noticed after its replacement has begun reporting.- if (auto controller = m_controller.strong_ref()) {- controller->on_playback_group_audibility_changed = nullptr;- controller->on_death = nullptr;- }- m_controller.clear();-}--void AudioServerAudibility::set(Audio::PlaybackGroupId group, Audio::Audibility audibility)-{- switch (audibility) {- case Audio::Audibility::Audible:- if (m_audible_groups.set(group) != HashSetResult::InsertedNewEntry)- return;- break;- case Audio::Audibility::Inaudible:- if (!m_audible_groups.remove(group))- return;- break;- }- m_on_change(group, audibility);-}--void AudioServerAudibility::forget_everything()-{- for (auto group : exchange(m_audible_groups, {}))- m_on_change(group, Audio::Audibility::Inaudible);-}--}diff --git a/Libraries/LibWebView/AudioServerAudibility.h b/Libraries/LibWebView/AudioServerAudibility.hdeleted file mode 100644index 4866ba1e13e..00000000000--- a/Libraries/LibWebView/AudioServerAudibility.h+++ /dev/null@@ -1,43 +0,0 @@-/*- * Copyright (c) 2026-present, the Ladybird developers.- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/Function.h>-#include <AK/HashTable.h>-#include <AK/WeakPtr.h>-#include <LibAudio/Audibility.h>-#include <LibAudio/PlaybackGroupId.h>-#include <LibWebView/Export.h>-#include <LibWebView/Forward.h>--namespace WebView {--// Which playback groups a process's AudioServer has recently heard. Only the watched AudioServer is heard: what it heard-// is forgotten once it dies or another is watched instead, and one that has been replaced is heard from no more.-class WEBVIEW_API AudioServerAudibility {-public:- using OnChange = Function<void(Audio::PlaybackGroupId, Audio::Audibility)>;- // Whether to take the AudioServer's report.- using Filter = Function<bool(u64 tab_id, Audio::PlaybackGroupId, Audio::Audibility)>;-- explicit AudioServerAudibility(OnChange);- ~AudioServerAudibility();-- void watch(AudioServerControlClient&, Filter);- void forget(Audio::PlaybackGroupId);--private:- void stop_watching();- void set(Audio::PlaybackGroupId, Audio::Audibility);- void forget_everything();-- OnChange m_on_change;- WeakPtr<AudioServerControlClient> m_controller;- HashTable<Audio::PlaybackGroupId> m_audible_groups;-};--}diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txtindex f29b0103a8b..ff5c0bc1752 100644--- a/Libraries/LibWebView/CMakeLists.txt+++ b/Libraries/LibWebView/CMakeLists.txt@@ -2,7 +2,7 @@ set(SOURCESAccessibilityTreeManager.cppApplication.cppApplyHistoryStep.cpp- AudioServerAudibility.cpp+ AudioServerActivity.cppAudioServerControlClient.cppAutocomplete.cppAutocompleteMuxer.cppdiff --git a/Libraries/LibWebView/PictureInPictureManager.cpp b/Libraries/LibWebView/PictureInPictureManager.cppindex 0674f0509f9..5c78589ca6f 100644--- a/Libraries/LibWebView/PictureInPictureManager.cpp+++ b/Libraries/LibWebView/PictureInPictureManager.cpp@@ -41,6 +41,7 @@ String PictureInPictureManager::open_window(WebContentPage& requesting_page, Canauto window = Application::the().create_picture_in_picture_window(requesting_page, traversable, video_size);if (!window)return {};+ window->act_for_document(requesting_navigable.active_document());window->on_resize = [this, window = window.ptr()](Gfx::IntSize size) {if (auto index = index_of(*window); index.has_value() && m_windows[*index].page->is_open())@@ -122,11 +123,12 @@ Optional<size_t> PictureInPictureManager::index_of(PictureInPictureWindow const&}// The page that a window shows outlives the window's place as another page's window, so a retired window stays hidden-// until the page that it shows has closed.+// until the page that it shows has closed. Its process decides when that is, so the window leaves the tab now.void PictureInPictureManager::retire_window(size_t index){auto window = m_windows.take(index).window;window->hide();+ window->leave_owners_tab();m_retired_windows.append(move(window));}diff --git a/Libraries/LibWebView/PictureInPictureWindow.cpp b/Libraries/LibWebView/PictureInPictureWindow.cppindex 9cd70f0642a..badcf14cea0 100644--- a/Libraries/LibWebView/PictureInPictureWindow.cpp+++ b/Libraries/LibWebView/PictureInPictureWindow.cpp@@ -77,6 +77,18 @@ void PictureInPictureWindow::close_page()page.async_request_close_without_prompting();}+void PictureInPictureWindow::act_for_document(CanonicalDocument const& document)+{+ if (m_page_view)+ m_page_view->set_owner_document({}, document);+}++void PictureInPictureWindow::leave_owners_tab()+{+ if (m_page_view)+ m_page_view->did_leave_owners_tab({});+}+void PictureInPictureWindow::report_page_close_of(ViewImplementation& view){m_page_view = &view;diff --git a/Libraries/LibWebView/PictureInPictureWindow.h b/Libraries/LibWebView/PictureInPictureWindow.hindex 4b595108a25..969528b6432 100644--- a/Libraries/LibWebView/PictureInPictureWindow.h+++ b/Libraries/LibWebView/PictureInPictureWindow.h@@ -35,6 +35,8 @@ public:virtual void set_video_size(Gfx::IntSize) = 0;void close_page();+ void act_for_document(CanonicalDocument const&);+ void leave_owners_tab();// The user closes the window to stop watching its video, or to go back to the video's tab and watch it there.enum class VideoKeepsPlaying : bool {diff --git a/Libraries/LibWebView/TabIndicators.h b/Libraries/LibWebView/TabIndicators.hindex 910afd72b20..a997291ce66 100644--- a/Libraries/LibWebView/TabIndicators.h+++ b/Libraries/LibWebView/TabIndicators.h@@ -21,6 +21,7 @@ namespace WebView {enum class TabIndicator : u8 {PlayingAudio,UsingLocation,+ UsingMicrophone,};// Each indicator lingers on a deadline of its own once what it shows stops, however it stopped.@@ -54,9 +55,10 @@ private:Entry& entry(TabIndicator);Entry const& entry(TabIndicator indicator) const { return const_cast<TabIndicators&>(*this).entry(indicator); }- Array<Entry, 2> m_entries {+ Array<Entry, 3> m_entries {Entry { .indicator = TabIndicator::PlayingAudio },Entry { .indicator = TabIndicator::UsingLocation },+ Entry { .indicator = TabIndicator::UsingMicrophone },};NonnullRefPtr<Core::Timer> m_timer;};diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 900b2b29bad..b3b50aefa67 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -87,6 +87,25 @@ Optional<ViewImplementation&> ViewImplementation::find_view_by_id(u64 id)return {};}+Optional<ViewImplementation&> ViewImplementation::tab_view()+{+ if (!m_owner_view_id.has_value())+ return *this;+ if (!m_owner_document)+ return {};+ return find_view_by_id(*m_owner_view_id);+}++void ViewImplementation::did_leave_owners_tab(Badge<PictureInPictureWindow>)+{+ m_owner_document = nullptr;++ // Whether a process may capture audio depends on the tabs its pages are part of.+ traversable().for_each_hosting_page([](WebContentPage& page) {+ page.client().update_audio_capture_permission();+ });+}+ViewImplementation::ViewImplementation(IsPrivate is_private): m_is_private(is_private), m_session(Application::session_for_new_view(is_private))@@ -115,8 +134,8 @@ ViewImplementation::ViewImplementation(IsPrivate is_private)ViewImplementation::~ViewImplementation(){// The frontend may be torn down already, so clearing the ongoing navigation must not report a URL change to it, nor- // cancelling the view's location requests or closing the view's page (which revokes its audio playback) a change- // in what the tab shows.+ // cancelling the view's location requests or closing the view's page (which revokes its audio playback and capture)+ // a change in what the tab shows.on_url_change = nullptr;on_tab_indicator_change = nullptr;answer_pending_console_input_completions();@@ -2975,9 +2994,42 @@ void ViewImplementation::update_audibility()m_tab_indicators.set_active(TabIndicator::PlayingAudio, audible, MonotonicTime::now());}+void ViewImplementation::update_microphone_capture()+{+ // An owned view has no tab of its own, so its capture shows on its owner's, with one linger for them all.+ if (m_owner_view_id.has_value()) {+ if (auto owner = find_view_by_id(*m_owner_view_id); owner.has_value())+ owner->update_microphone_capture();+ return;+ }++ auto capturing = false;+ auto check_pages_of = [&](ViewImplementation const& view) {+ view.traversable().for_each_hosting_page([&](WebContentPage& page) {+ if (page.is_capturing_audio())+ capturing = true;+ });+ };++ check_pages_of(*this);+ for_each_view([&](ViewImplementation& view) {+ if (view.m_owner_view_id == m_view_id && view.has_traversable())+ check_pages_of(view);+ return IterationDecision::Continue;+ });+ m_tab_indicators.set_active(TabIndicator::UsingMicrophone, capturing, MonotonicTime::now());+}++void ViewImplementation::did_release_page(Badge<CanonicalTraversable>)+{+ update_audibility();+ update_microphone_capture();+}+void ViewImplementation::reset_page_media_state(){update_audibility();+ update_microphone_capture();if (m_screen_wake_lock_state != Web::ScreenWakeLockState::Released) {m_screen_wake_lock_state = Web::ScreenWakeLockState::Released;diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex a885f06fa95..37fd7622864 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -192,6 +192,12 @@ public:// A view that shows a page on behalf of another view, such as a Picture-in-Picture window, is part of that view's// tab rather than a tab or window of its own.Optional<u64> owner_view_id() const { return m_owner_view_id; }+ // The view whose tab this view is part of, which is none for a view whose owner is gone or that has left its tab.+ Optional<ViewImplementation&> tab_view();+ // The owner's document that this view acts for, which limits what the view is allowed, until it leaves the tab.+ CanonicalDocument const* owner_document() const { return m_owner_document.ptr(); }+ void set_owner_document(Badge<PictureInPictureWindow>, CanonicalDocument const& document) { m_owner_document = document; }+ void did_leave_owners_tab(Badge<PictureInPictureWindow>);CanonicalTraversable& traversable() const;bool has_traversable() const { return m_top_level_traversable != nullptr; }@@ -512,9 +518,13 @@ public:// The tab plays audio while the AudioServer has recently heard any page of it.void update_audibility(Badge<WebContentPage>) { update_audibility(); }- void did_release_page(Badge<CanonicalTraversable>) { update_audibility(); }+ void did_release_page(Badge<CanonicalTraversable>);bool is_playing_audio() const { return m_tab_indicators.is_active(TabIndicator::PlayingAudio); }+ // The tab captures while the AudioServer holds a capture stream for any page of it.+ void update_microphone_capture(Badge<WebContentPage>) { update_microphone_capture(); }+ bool is_capturing_audio() const { return m_tab_indicators.is_active(TabIndicator::UsingMicrophone); }+bool tab_indicator_visible(TabIndicator indicator) const { return m_tab_indicators.is_visible(indicator); }u64 playing_audio_indicator_change_count() const { return m_playing_audio_indicator_change_count; }void did_change_screen_wake_lock_state(Badge<WebContentPage>, Web::ScreenWakeLockState);@@ -824,6 +834,7 @@ protected:WebContentPage& focused_navigable_host() const;void reset_page_media_state();void update_audibility();+ void update_microphone_capture();// A navigation to make again on reload: one the user stopped before its document was activated, or one a crash cut// short. It's made again from the source it was first made from, when it has one (a javascript: URL has none).@@ -1222,6 +1233,7 @@ protected:// is required to be globally unique for Firefox DevTools.u64 m_view_id { 0 };Optional<u64> m_owner_view_id;+ WeakPtr<CanonicalDocument const> m_owner_document;HashMap<u64, NavigationListener> m_navigation_listeners;u64 m_next_navigation_listener_id { 1 };diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex e6d731b52c5..2dfab976fdc 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -980,13 +980,19 @@ ErrorOr<void> WebContentClient::initialize_audio_server_controller()if (self && tab_id == self->m_audio_tab_id)self->decide_audio_capture_request(request_id, group);};- m_audio_server_audibility.watch(*m_audio_server_client, [weak_self = make_weak_ptr<WebContentClient>()](u64 tab_id, Audio::PlaybackGroupId group, Audio::Audibility audibility) {- auto self = weak_self.strong_ref();- if (!self || tab_id != self->m_audio_tab_id)- return false;- // A report of sound can trail the revocation of the page's authorization.- return audibility == Audio::Audibility::Inaudible || self->m_authorized_audio_playback_groups.contains(group);- });+ m_audio_server_activity.watch(+ *m_audio_server_client,+ [weak_self = make_weak_ptr<WebContentClient>()](u64 tab_id, Audio::PlaybackGroupId group, Audio::Audibility audibility) {+ auto self = weak_self.strong_ref();+ if (!self || tab_id != self->m_audio_tab_id)+ return false;+ // A report of sound can trail the revocation of the page's authorization.+ return audibility == Audio::Audibility::Inaudible || self->m_authorized_audio_playback_groups.contains(group);+ },+ [weak_self = make_weak_ptr<WebContentClient>()](u64 tab_id) {+ auto self = weak_self.strong_ref();+ return self && tab_id == self->m_audio_tab_id;+ });if (m_audio_capture_permitted)TRY(tell_audio_server_of_capture_permission());// A muted group is never heard: the mutes land before the authorizations.@@ -1017,6 +1023,9 @@ ErrorOr<void> WebContentClient::grant_audio_capture_to_file(URL::URL const& url)void WebContentClient::update_audio_capture_permission(){+ // A page that comes to permit capture, or stops, can take over a stream labelled for no permitting page.+ update_audio_capture_indicators();+auto permitted = audio_capture_permitted();if (permitted == m_audio_capture_permitted)return;@@ -1052,22 +1061,39 @@ Messages::WebContentClient::DidRequestPermissionSnapshotResponse WebContentClienbool WebContentClient::page_may_capture_audio(WebContentPage const& page) const{+ // A new page has no view until the chrome makes one, and replacing a window can get here before that.+ auto view = page.traversable().view();+ if (!view.has_value())+ return false;++ // A window that is part of a tab has the tab's grant, whichever document its process has it show.+ auto tab_view = view->tab_view();+ if (!tab_view.has_value())+ return false;+// The top-level document's origin, which a popup on about:blank has from its opener; only a file page is left with// an opaque one, and reads its own grant by URL.- auto origin = page.view().permission_origin();+ auto origin = tab_view->permission_origin();if (origin.is_opaque()) {// NB: The global default binds a file: page too; its own grant only matters while sites may ask.if (session().permission_manager->global_default(Web::Permissions::PermissionName::Microphone) == Web::Permissions::EffectiveState::Denied)return false;- return m_audio_capture_grants.contains(audio_capture_permission_key(page.view().url()));+ return m_audio_capture_grants.contains(audio_capture_permission_key(tab_view->url()));}return session().permission_manager->effective_state(Web::Permissions::PermissionName::Microphone, origin) == Web::Permissions::EffectiveState::Granted;}-// A tab's grant reaches only the documents whose permissions policy allows them the microphone.+// A tab's grant reaches only the documents whose permissions policy allows them the microphone. A window that is part of+// the tab is held to the document it acts for too, or an iframe could get past its policy by opening a window.bool WebContentClient::page_permits_audio_capture(WebContentPage const& page) const{- return page.is_open() && page_may_capture_audio(page) && page.hosts_a_document_allowed_to_use(Web::PermissionsPolicy::PolicyControlledFeature::Microphone);+ if (!page.is_open() || !page_may_capture_audio(page))+ return false;++ auto microphone = Web::PermissionsPolicy::PolicyControlledFeature::Microphone;+ if (auto const* owner_document = page.view().owner_document(); owner_document && !owner_document->is_allowed_to_use(microphone))+ return false;+ return page.hosts_a_document_allowed_to_use(microphone);}// The process may capture if it hosts a page that permits it, as it can act for any document it hosts.@@ -1104,8 +1130,11 @@ void WebContentClient::decide_audio_capture_request(u64 request_id, Audio::Playbif (auto* named_page = this->page(page_id_of_audio_playback_group(group)); named_page && page_permits_audio_capture(*named_page))page = named_page;+ // A page that permits capture is part of a tab, which is where the user hears of the answer.+ auto tab_view = page->view().tab_view();+// Only a site the user allowed gets the system to ask the user, if it never has.- page->view().ensure_microphone_authorization([weak_self = make_weak_ptr<WebContentClient>(), request_id](bool authorized) {+ tab_view->ensure_microphone_authorization([weak_self = make_weak_ptr<WebContentClient>(), request_id](bool authorized) {auto self = weak_self.strong_ref();if (!self || !self->m_audio_server_client || !self->m_audio_server_client->is_open())return;@@ -1122,6 +1151,31 @@ void WebContentClient::send_audio_capture_decision(u64 request_id, bool allowed)m_audio_server_client->async_capture_decided(request_id, allowed);}+// The AudioServer counts every capture stream of the process, but the renderer labels them, so a label counts only for a+// page that permits capture. A stream with any other label is shown on every such page, as the process could be+// capturing for any of them.+void WebContentClient::update_audio_capture_indicators()+{+ HashTable<Web::PageId> labelled_pages;+ auto shown_on_every_permitting_page = false;+ for (auto group : m_audio_server_activity.capturing_groups()) {+ if (auto* page = this->page(page_id_of_audio_playback_group(group)); page && page_permits_audio_capture(*page))+ labelled_pages.set(page->id());+ else+ shown_on_every_permitting_page = true;+ }++ for (auto const& [page_id, page] : m_pages) {+ auto capturing = labelled_pages.contains(page_id) || (shown_on_every_permitting_page && page_permits_audio_capture(*page));+ page->set_capturing_audio(capturing);+ }+}++void WebContentClient::report_audio_capture_for_testing(Web::PageId labelled_page_id, bool capturing)+{+ m_audio_server_activity.set_capturing_for_testing(audio_playback_group(labelled_page_id), capturing);+}+ErrorOr<void> WebContentClient::tell_audio_server_of_capture_permission(){auto response = m_audio_server_client->send_sync_but_allow_failure<Messages::AudioServerControl::SetCapturePermission>(m_audio_tab_id, m_audio_capture_permitted);@@ -1155,7 +1209,7 @@ void WebContentClient::revoke_audio_playback(Web::PageId page_id)return;if (m_audio_server_client && m_audio_server_client->is_open())m_audio_server_client->async_revoke_playback_group(m_audio_tab_id, group);- m_audio_server_audibility.forget(group);+ m_audio_server_activity.forget_audibility(group);}void WebContentClient::set_audio_playback_mute_state(Web::PageId page_id, Web::HTML::MuteState mute_state)diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex 526ee0980d2..df0579baf6b 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -58,7 +58,7 @@#include <LibWebCommon/Page/ViewportIsFullscreen.h>#include <LibWebCommon/StorageAPI/StorageEndpoint.h>#include <LibWebCommon/WebView/Debugger.h>-#include <LibWebView/AudioServerAudibility.h>+#include <LibWebView/AudioServerActivity.h>#include <LibWebView/BlobURLStore.h>#include <LibWebView/BrowsingSession.h>#include <LibWebView/ChildIdentity.h>@@ -183,6 +183,8 @@ public:void update_audio_capture_permission();u64 audio_capture_decision_count() const { return m_audio_capture_decision_count; }u64 audio_capture_refusal_count() const { return m_audio_capture_refusal_count; }+ // Stands in for the AudioServer reporting a capture stream with the given label, which the renderer chooses.+ void report_audio_capture_for_testing(Web::PageId labelled_page_id, bool capturing);// The process's AudioServer plays the page's sound while the page is authorized. It hears of an authorization// before this returns, so a document the page goes on to run is heard from its start.void authorize_audio_playback(Web::PageId);@@ -297,6 +299,7 @@ private:bool page_may_capture_audio(WebContentPage const&) const;void decide_audio_capture_request(u64 request_id, Audio::PlaybackGroupId);void send_audio_capture_decision(u64 request_id, bool allowed);+ void update_audio_capture_indicators();// The controller connections to the MediaServer and AudioServer spawned for this process, from its first media use// until it exits. The AudioServer mixes for this process alone, so a compromised one hears only its own site.@@ -314,7 +317,10 @@ private:// Likewise, the AudioServer is told again of the groups it may play, and of those the user has muted.HashTable<Audio::PlaybackGroupId> m_authorized_audio_playback_groups;HashTable<Audio::PlaybackGroupId> m_muted_audio_playback_groups;- AudioServerAudibility m_audio_server_audibility { [this](Audio::PlaybackGroupId group, Audio::Audibility audibility) { did_change_audio_playback_audibility(group, audibility); } };+ AudioServerActivity m_audio_server_activity {+ [this](Audio::PlaybackGroupId group, Audio::Audibility audibility) { did_change_audio_playback_audibility(group, audibility); },+ [this] { update_audio_capture_indicators(); },+ };RefPtr<Core::Timer> m_detached_page_close_timer;// When this process last got a WebRTCClient, so that it can't make us spawn them in a loop.diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 1fb6f787916..47ce16ead8c 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -2126,6 +2126,17 @@ void WebContentPage::set_audibility(Audio::Audibility audibility)}}+void WebContentPage::set_capturing_audio(bool capturing)+{+ if (m_capturing_audio == capturing)+ return;+ m_capturing_audio = capturing;+ if (m_traversable) {+ if (auto view = m_traversable->view(); view.has_value())+ view->update_microphone_capture({});+ }+}+void WebContentPage::did_change_screen_wake_lock_state(Web::ScreenWakeLockState wake_lock_state){if (displays_tab())@@ -3428,6 +3439,28 @@ Messages::WebContentTestClient::DidRequestTabIsHeardPlayingAudioForTestingResponreturn view.has_value() && view->is_playing_audio();}+Messages::WebContentTestClient::DidRequestTabIsUsingMicrophoneForTestingResponse WebContentPage::did_request_tab_is_using_microphone_for_testing()+{+ auto view = traversable().view();+ return view.has_value() && view->tab_indicator_visible(TabIndicator::UsingMicrophone);+}++Messages::WebContentTestClient::DidRequestTabIsCapturingAudioForTestingResponse WebContentPage::did_request_tab_is_capturing_audio_for_testing()+{+ auto view = traversable().view();+ return view.has_value() && view->is_capturing_audio();+}++void WebContentPage::report_audio_capture_for_testing(Web::PageId labelled_page_id, bool capturing)+{+ client().report_audio_capture_for_testing(labelled_page_id, capturing);+}++void WebContentPage::retire_picture_in_picture_window_for_testing()+{+ PictureInPictureManager::the().close_window(*this);+}+Messages::WebContentTestClient::DidRequestTabAudibilityChangeCountForTestingResponse WebContentPage::did_request_tab_audibility_change_count_for_testing(){auto view = traversable().view();diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 6b095404ad9..301218c74d7 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -111,6 +111,9 @@ public:// Whether the AudioServer has recently heard the page, muted or not.Audio::Audibility audibility() const { return m_audibility; }void set_audibility(Audio::Audibility);+ // Whether the AudioServer holds a capture stream for the page.+ bool is_capturing_audio() const { return m_capturing_audio; }+ void set_capturing_audio(bool);// The channel to the WebUI this page shows, if it shows one. Each page has its own, so two WebUI pages in one// process each keep theirs.@@ -404,6 +407,10 @@ private:Messages::WebContentTestClient::DidRequestSiteIsolationProcessTreeForTestingResponse did_request_site_isolation_process_tree_for_testing();Messages::WebContentTestClient::DidRequestTabIsPlayingAudioForTestingResponse did_request_tab_is_playing_audio_for_testing();Messages::WebContentTestClient::DidRequestTabIsHeardPlayingAudioForTestingResponse did_request_tab_is_heard_playing_audio_for_testing();+ Messages::WebContentTestClient::DidRequestTabIsUsingMicrophoneForTestingResponse did_request_tab_is_using_microphone_for_testing();+ Messages::WebContentTestClient::DidRequestTabIsCapturingAudioForTestingResponse did_request_tab_is_capturing_audio_for_testing();+ void report_audio_capture_for_testing(Web::PageId labelled_page_id, bool capturing);+ void retire_picture_in_picture_window_for_testing();Messages::WebContentTestClient::DidRequestTabAudibilityChangeCountForTestingResponse did_request_tab_audibility_change_count_for_testing();virtual void did_request_crash_of_remote_frame_processes_for_testing() override;virtual void did_request_close_tab_for_testing() override;@@ -424,6 +431,7 @@ private:bool m_is_open { true };RefPtr<WebUI> m_web_ui;Audio::Audibility m_audibility { Audio::Audibility::Inaudible };+ bool m_capturing_audio { false };Optional<PresentedBackingStores> m_presented_backing_stores;bool m_needs_beforeunload_check { true };bool m_detached_close_pending { false };diff --git a/Libraries/LibWebView/WebContentTestClient.cpp b/Libraries/LibWebView/WebContentTestClient.cppindex 12284557e61..55bd9d0116e 100644--- a/Libraries/LibWebView/WebContentTestClient.cpp+++ b/Libraries/LibWebView/WebContentTestClient.cpp@@ -137,6 +137,34 @@ Messages::WebContentTestClient::DidRequestTabIsHeardPlayingAudioForTestingResponreturn false;}+Messages::WebContentTestClient::DidRequestTabIsUsingMicrophoneForTestingResponse WebContentTestClient::did_request_tab_is_using_microphone_for_testing(Web::PageId page_id)+{+ if (auto* page = m_client.page(page_id))+ return page->did_request_tab_is_using_microphone_for_testing();++ return false;+}++Messages::WebContentTestClient::DidRequestTabIsCapturingAudioForTestingResponse WebContentTestClient::did_request_tab_is_capturing_audio_for_testing(Web::PageId page_id)+{+ if (auto* page = m_client.page(page_id))+ return page->did_request_tab_is_capturing_audio_for_testing();++ return false;+}++void WebContentTestClient::report_audio_capture_for_testing(Web::PageId page_id, Web::PageId labelled_page_id, bool capturing)+{+ if (auto* page = m_client.page(page_id))+ page->report_audio_capture_for_testing(labelled_page_id, capturing);+}++void WebContentTestClient::retire_picture_in_picture_window_for_testing(Web::PageId page_id)+{+ if (auto* page = m_client.page(page_id))+ page->retire_picture_in_picture_window_for_testing();+}+Messages::WebContentTestClient::DidRequestTabAudibilityChangeCountForTestingResponse WebContentTestClient::did_request_tab_audibility_change_count_for_testing(Web::PageId page_id){if (auto* page = m_client.page(page_id))diff --git a/Libraries/LibWebView/WebContentTestClient.h b/Libraries/LibWebView/WebContentTestClient.hindex facad6ff59d..9165e5e1ef2 100644--- a/Libraries/LibWebView/WebContentTestClient.h+++ b/Libraries/LibWebView/WebContentTestClient.h@@ -39,6 +39,10 @@ private:virtual Messages::WebContentTestClient::DidRequestSiteIsolationProcessTreeForTestingResponse did_request_site_isolation_process_tree_for_testing(Web::PageId page_id) override;virtual Messages::WebContentTestClient::DidRequestTabIsPlayingAudioForTestingResponse did_request_tab_is_playing_audio_for_testing(Web::PageId page_id) override;virtual Messages::WebContentTestClient::DidRequestTabIsHeardPlayingAudioForTestingResponse did_request_tab_is_heard_playing_audio_for_testing(Web::PageId page_id) override;+ virtual Messages::WebContentTestClient::DidRequestTabIsUsingMicrophoneForTestingResponse did_request_tab_is_using_microphone_for_testing(Web::PageId page_id) override;+ virtual Messages::WebContentTestClient::DidRequestTabIsCapturingAudioForTestingResponse did_request_tab_is_capturing_audio_for_testing(Web::PageId page_id) override;+ virtual void report_audio_capture_for_testing(Web::PageId page_id, Web::PageId labelled_page_id, bool capturing) override;+ virtual void retire_picture_in_picture_window_for_testing(Web::PageId page_id) override;virtual Messages::WebContentTestClient::DidRequestTabAudibilityChangeCountForTestingResponse did_request_tab_audibility_change_count_for_testing(Web::PageId page_id) override;virtual Messages::WebContentTestClient::DidRequestHasPopulatedDocumentForTestingResponse did_request_has_populated_document_for_testing(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) override;virtual Messages::WebContentTestClient::DidRequestCaptureSessionHistorySnapshotForTestingResponse did_request_capture_session_history_snapshot_for_testing(Web::PageId page_id) override;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex 5f8f2b30bb3..ad260cd0e30 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -1956,6 +1956,32 @@ bool PageClient::page_did_request_tab_is_heard_playing_audio_for_testing()return false;}+bool PageClient::page_did_request_tab_is_using_microphone_for_testing()+{+ if (auto* test_connection = client().test_connection())+ return test_connection->did_request_tab_is_using_microphone_for_testing(m_id);+ return false;+}++bool PageClient::page_did_request_tab_is_capturing_audio_for_testing()+{+ if (auto* test_connection = client().test_connection())+ return test_connection->did_request_tab_is_capturing_audio_for_testing(m_id);+ return false;+}++void PageClient::page_did_report_audio_capture_for_testing(Web::PageId labelled_page_id, bool capturing)+{+ if (auto* test_connection = client().test_connection())+ (void)test_connection->send_sync_but_allow_failure<Messages::WebContentTestClient::ReportAudioCaptureForTesting>(m_id, labelled_page_id, capturing);+}++void PageClient::page_did_retire_picture_in_picture_window_for_testing()+{+ if (auto* test_connection = client().test_connection())+ (void)test_connection->send_sync_but_allow_failure<Messages::WebContentTestClient::RetirePictureInPictureWindowForTesting>(m_id);+}+u64 PageClient::page_did_request_tab_audibility_change_count_for_testing(){if (auto* test_connection = client().test_connection())diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex 8aac09d64cc..8b61464718b 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -346,6 +346,10 @@ private:virtual bool page_did_request_has_populated_document_for_testing(Web::HTML::CrossProcessId navigable_id) override;virtual bool page_did_request_tab_is_playing_audio_for_testing() override;virtual bool page_did_request_tab_is_heard_playing_audio_for_testing() override;+ virtual bool page_did_request_tab_is_using_microphone_for_testing() override;+ virtual bool page_did_request_tab_is_capturing_audio_for_testing() override;+ virtual void page_did_report_audio_capture_for_testing(Web::PageId labelled_page_id, bool capturing) override;+ virtual void page_did_retire_picture_in_picture_window_for_testing() override;virtual u64 page_did_request_tab_audibility_change_count_for_testing() override;virtual bool page_did_request_capture_session_history_snapshot_for_testing() override;virtual bool page_did_request_restore_session_history_snapshot_for_testing() override;diff --git a/Services/WebContent/WebContentTestClient.ipc b/Services/WebContent/WebContentTestClient.ipcindex 6585d142c1d..6b7551cfa75 100644--- a/Services/WebContent/WebContentTestClient.ipc+++ b/Services/WebContent/WebContentTestClient.ipc@@ -32,6 +32,10 @@ endpoint WebContentTestClient routed by Web::PageId page_id[NotRouted] did_request_site_isolation_process_tree_for_testing(Web::PageId page_id) => (String process_tree)[NotRouted] did_request_tab_is_playing_audio_for_testing(Web::PageId page_id) => (bool is_playing_audio)[NotRouted] did_request_tab_is_heard_playing_audio_for_testing(Web::PageId page_id) => (bool is_heard_playing_audio)+ [NotRouted] did_request_tab_is_using_microphone_for_testing(Web::PageId page_id) => (bool is_using_microphone)+ [NotRouted] did_request_tab_is_capturing_audio_for_testing(Web::PageId page_id) => (bool is_capturing_audio)+ [NotRouted] report_audio_capture_for_testing(Web::PageId page_id, Web::PageId labelled_page_id, bool capturing) => ()+ [NotRouted] retire_picture_in_picture_window_for_testing(Web::PageId page_id) => ()[NotRouted] did_request_tab_audibility_change_count_for_testing(Web::PageId page_id) => (u64 change_count)[NotRouted] did_request_has_populated_document_for_testing(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id) => (bool has_populated_document)did_request_crash_of_remote_frame_processes_for_testing(Web::PageId page_id) =|diff --git a/Tests/LibWeb/Text/expected/MediaCapture/getUserMedia-revoked-while-the-system-asks.txt b/Tests/LibWeb/Text/expected/MediaCapture/getUserMedia-revoked-while-the-system-asks.txtindex fb3498e55ec..e8bcbff41dd 100644--- a/Tests/LibWeb/Text/expected/MediaCapture/getUserMedia-revoked-while-the-system-asks.txt+++ b/Tests/LibWeb/Text/expected/MediaCapture/getUserMedia-revoked-while-the-system-asks.txt@@ -1 +1,2 @@the request settles with: NotAllowedError+no stream of it stays behind: truediff --git a/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-has-the-grant-of-its-tab.txt b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-has-the-grant-of-its-tab.txtnew file mode 100644index 00000000000..505fb5dbc91--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-has-the-grant-of-its-tab.txt@@ -0,0 +1,4 @@+without the grant, this tab: false+with the grant, this tab: true+with the grant, the other tab: false+once it's taken away, this tab: falsediff --git a/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-is-allowed-no-more-than-its-opener.txt b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-is-allowed-no-more-than-its-opener.txtnew file mode 100644index 00000000000..fa7f8dd346d--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-is-allowed-no-more-than-its-opener.txt@@ -0,0 +1,2 @@+opened by an iframe allowed the microphone: true+opened by an iframe not allowed the microphone: falsediff --git a/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-leaves-its-tab-with-its-document.txt b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-leaves-its-tab-with-its-document.txtnew file mode 100644index 00000000000..b8a3e977cdc--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-leaves-its-tab-with-its-document.txt@@ -0,0 +1,8 @@+labelled for the window, its tab: true+labelled for the window, this tab: false+its tab closed, this tab: true+stream gone: false+labelled for the window, its tab: true+labelled for the window, this tab: false+its tab navigated, this tab: true+stream gone: falsediff --git a/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-replaced-by-another.txt b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-replaced-by-another.txtnew file mode 100644index 00000000000..0daf359990b--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/picture-in-picture-window-replaced-by-another.txt@@ -0,0 +1,4 @@+labelled for the first window, the other tab: false+first window replaced, the other tab: true+labelled for the second window, this tab: true+labelled for the second window, the other tab: falsediff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-a-cross-site-navigation.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-a-cross-site-navigation.txtnew file mode 100644index 00000000000..97d859a7fe7--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-a-cross-site-navigation.txt@@ -0,0 +1,3 @@+the popup's tab shows capture in another process+stops showing it a while later: true+went on showing it for at least two and a half seconds after navigating: truediff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-its-picture-in-picture-window-closes.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-its-picture-in-picture-window-closes.txtnew file mode 100644index 00000000000..ef924bb2fdd--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-its-picture-in-picture-window-closes.txt@@ -0,0 +1,8 @@+before the window closes, this tab: true+before the window closes, the other tab: false+the window closed: none+once the window closed, this tab: true+once the window closed, the other tab: true+stops capturing: true+stops showing it a while later: true+went on showing it for at least two and a half seconds: truediff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-its-process-is-lost.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-its-process-is-lost.txtnew file mode 100644index 00000000000..9063d7c8a10--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-its-process-is-lost.txt@@ -0,0 +1,4 @@+captures in the isolated iframe: true+stops capturing once the iframe's process is gone: true+stops showing it a while later: true+went on showing it for at least two and a half seconds after the crash: truediff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-the-permission-is-taken-away.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-the-permission-is-taken-away.txtnew file mode 100644index 00000000000..8f1b7225d38--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-a-while-after-the-permission-is-taken-away.txt@@ -0,0 +1,5 @@+capturing: true+stops capturing once the permission is taken away: true+stops showing it a while later: true+went on showing it for at least two and a half seconds: true+the track ended: endeddiff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-labelled-for-no-page.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-labelled-for-no-page.txtnew file mode 100644index 00000000000..39a3b042ec7--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-labelled-for-no-page.txt@@ -0,0 +1,3 @@+without the permission: false+with the permission: true+once the stream is gone: falsediff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-of-its-picture-in-picture-window.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-of-its-picture-in-picture-window.txtnew file mode 100644index 00000000000..7b1ac831e6d--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-of-its-picture-in-picture-window.txt@@ -0,0 +1,5 @@+labelled for the window, this tab: true+labelled for the window, the other tab: false+stream gone, this tab: false+window retired, this tab: true+window retired, the other tab: truediff --git a/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-while-a-track-lives.txt b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-while-a-track-lives.txtnew file mode 100644index 00000000000..dae3cdfbc52--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/tab-shows-capture-while-a-track-lives.txt@@ -0,0 +1,8 @@+before: false+capturing while the track lives, unconsumed: true+the clone holds a stream: true+the clone's stream outlives the original's: true+capturing after the original stops: true+stops capturing once the clone stops: true+stops showing it a while later: true+went on showing it for at least two and a half seconds: truediff --git a/Tests/LibWeb/Text/input/MediaCapture/getUserMedia-revoked-while-the-system-asks.html b/Tests/LibWeb/Text/input/MediaCapture/getUserMedia-revoked-while-the-system-asks.htmlindex 5aa410c9f4d..b20e26ab07d 100644--- a/Tests/LibWeb/Text/input/MediaCapture/getUserMedia-revoked-while-the-system-asks.html+++ b/Tests/LibWeb/Text/input/MediaCapture/getUserMedia-revoked-while-the-system-asks.html@@ -6,6 +6,7 @@promiseTest(async () => {internals.setTestTimeout(30_000);await internals.setPermission("microphone", "granted");+ const streamsBefore = internals.audioCaptureStreamCount();const url = new URL(await httpTestServer().createEcho("GET", "/getUserMedia-revoked-while-the-system-asks", {@@ -31,6 +32,7 @@await internals.setPermission("microphone", "denied");internals.releaseSystemMicrophoneAnswer();println(`the request settles with: ${await request.then(stream => "a live stream", error => error.name)}`);+ println(`no stream of it stays behind: ${await waitForCondition(() => internals.audioCaptureStreamCount() === streamsBefore)}`);popup.close();});</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-has-the-grant-of-its-tab.html b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-has-the-grant-of-its-tab.htmlnew file mode 100644index 00000000000..dbbd00a6bcb--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-has-the-grant-of-its-tab.html@@ -0,0 +1,33 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video src="../../../Assets/test-webm.webm"></video>+<script>+ // A Picture-in-Picture window has the grant of the tab it's part of, for as long as the tab has it. The other tab tells+ // whether the window counted, as a stream labelled for a page that doesn't shows on every page that may capture.+ asyncTest(async done => {+ const video = document.querySelector("video");+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));++ const otherTab = window.open("about:blank");++ internals.grantTransientActivation();+ await video.requestPictureInPicture();+ const windowPageId = internals.pictureInPictureWindowPageId();++ internals.reportAudioCaptureOfPage(windowPageId, true);+ println(`without the grant, this tab: ${internals.tabIsCapturingAudio()}`);++ await internals.setPermission("microphone", "granted");+ println(`with the grant, this tab: ${internals.tabIsCapturingAudio()}`);+ println(`with the grant, the other tab: ${otherTab.internals.tabIsCapturingAudio()}`);++ await internals.setPermission("microphone", "denied");+ println(`once it's taken away, this tab: ${internals.tabIsCapturingAudio()}`);++ internals.reportAudioCaptureOfPage(windowPageId, false);+ otherTab.close();+ await document.exitPictureInPicture();+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-is-allowed-no-more-than-its-opener.html b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-is-allowed-no-more-than-its-opener.htmlnew file mode 100644index 00000000000..5032b860988--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-is-allowed-no-more-than-its-opener.html@@ -0,0 +1,50 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A Picture-in-Picture window acts for the document that opened it, so an isolated iframe that the top-level document+ // doesn't allow the microphone can't reach the tab's grant by opening a window.+ asyncTest(async done => {+ await internals.setPermission("microphone", "granted");++ const videoUrl = new URL("../../../Assets/test-webm.webm", location.href).href;+ const url = new URL(+ await httpTestServer().createEcho("GET", `/picture-in-picture-opener-${crypto.randomUUID()}`, {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><video src="${videoUrl}"></video><script>+ (async () => {+ const video = document.querySelector("video");+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));++ internals.grantTransientActivation();+ await video.requestPictureInPicture();++ const windowPageId = internals.pictureInPictureWindowPageId();+ internals.reportAudioCaptureOfPage(windowPageId, true);+ const capturing = internals.tabIsCapturingAudio();+ internals.reportAudioCaptureOfPage(windowPageId, false);+ await document.exitPictureInPicture();+ parent.postMessage(capturing, "*");+ })();+ <\/script>`,+ })+ );+ url.hostname = uniqueLocalhostHostname("picture-in-picture-opener");++ const capturesInIframe = async allow => {+ const iframe = document.createElement("iframe");+ iframe.allow = allow;+ iframe.src = url;+ const capturing = new Promise(resolve => addEventListener("message", event => resolve(event.data), { once: true }));+ document.body.appendChild(iframe);+ const result = await capturing;+ iframe.remove();+ return result;+ };++ println(`opened by an iframe allowed the microphone: ${await capturesInIframe("microphone")}`);+ println(`opened by an iframe not allowed the microphone: ${await capturesInIframe("")}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-leaves-its-tab-with-its-document.html b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-leaves-its-tab-with-its-document.htmlnew file mode 100644index 00000000000..8239f63f1a3--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-leaves-its-tab-with-its-document.html@@ -0,0 +1,46 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A Picture-in-Picture window goes when the tab it's part of closes or navigates, and a stream that was labelled for+ // it then counts for no page, so it shows on the tabs that remain.+ asyncTest(async done => {+ const httpServer = httpTestServer();+ await internals.setPermission("microphone", "granted");++ const openWindowInTab = async () => {+ const tab = window.open("about:blank");+ const video = tab.document.createElement("video");+ video.src = new URL("../../../Assets/test-webm.webm", location.href).href;+ const loaded = new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));+ tab.document.body.appendChild(video);+ await loaded;++ tab.internals.grantTransientActivation();+ await video.requestPictureInPicture();+ const windowPageId = tab.internals.pictureInPictureWindowPageId();+ tab.internals.reportAudioCaptureOfPage(windowPageId, true);+ println(`labelled for the window, its tab: ${tab.internals.tabIsCapturingAudio()}`);+ println(`labelled for the window, this tab: ${internals.tabIsCapturingAudio()}`);+ return { tab, windowPageId };+ };++ const first = await openWindowInTab();+ first.tab.close();+ println(`its tab closed, this tab: ${await waitForCondition(() => internals.tabIsCapturingAudio())}`);+ internals.reportAudioCaptureOfPage(first.windowPageId, false);+ println(`stream gone: ${internals.tabIsCapturingAudio()}`);++ const second = await openWindowInTab();+ second.tab.location.href = await httpServer.createEcho("GET", `/next-document-${crypto.randomUUID()}`, {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: "<!DOCTYPE html>",+ });+ println(`its tab navigated, this tab: ${await waitForCondition(() => internals.tabIsCapturingAudio())}`);+ internals.reportAudioCaptureOfPage(second.windowPageId, false);+ println(`stream gone: ${internals.tabIsCapturingAudio()}`);++ second.tab.close();+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-replaced-by-another.html b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-replaced-by-another.htmlnew file mode 100644index 00000000000..69ec15ce6eb--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/picture-in-picture-window-replaced-by-another.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video src="../../../Assets/test-webm.webm"></video>+<video src="../../../Assets/test-webm.webm"></video>+<script>+ // A window that replaces another has the page's next stream labelled for it, while the replaced window leaves the tab+ // at a point where the new window's page has no view yet.+ asyncTest(async done => {+ const [firstVideo, secondVideo] = document.querySelectorAll("video");+ for (const video of [firstVideo, secondVideo]) {+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));+ }++ await internals.setPermission("microphone", "granted");+ const otherTab = window.open("about:blank");++ internals.grantTransientActivation();+ await firstVideo.requestPictureInPicture();+ const firstWindowPageId = internals.pictureInPictureWindowPageId();+ internals.reportAudioCaptureOfPage(firstWindowPageId, true);+ println(`labelled for the first window, the other tab: ${otherTab.internals.tabIsCapturingAudio()}`);++ internals.grantTransientActivation();+ await secondVideo.requestPictureInPicture();+ println(`first window replaced, the other tab: ${await waitForCondition(() => otherTab.internals.tabIsCapturingAudio())}`);+ internals.reportAudioCaptureOfPage(firstWindowPageId, false);++ const secondWindowPageId = internals.pictureInPictureWindowPageId();+ internals.reportAudioCaptureOfPage(secondWindowPageId, true);+ println(`labelled for the second window, this tab: ${internals.tabIsCapturingAudio()}`);+ println(`labelled for the second window, the other tab: ${otherTab.internals.tabIsCapturingAudio()}`);++ internals.reportAudioCaptureOfPage(secondWindowPageId, false);+ otherTab.close();+ await document.exitPictureInPicture();+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-a-cross-site-navigation.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-a-cross-site-navigation.htmlnew file mode 100644index 00000000000..d073af75fe4--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-a-cross-site-navigation.html@@ -0,0 +1,42 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ internals.setTestTimeout(30_000);+ const httpServer = httpTestServer();+ const nextMessage = () => new Promise(resolve => addEventListener("message", event => resolve(event.data), { once: true }));++ const echo = async (name, body) => httpServer.createEcho("GET", `/${name}`, {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>${body}<\/script>`,+ });++ // The popup's document captures in another process, and says so once its tab shows it.+ const crossSiteUrl = new URL(await echo("tab-shows-capture-cross-site", `+ (async () => {+ window.stream = await navigator.mediaDevices.getUserMedia({ audio: true });+ while (!internals.tabIsCapturingAudio())+ await new Promise(resolve => setTimeout(resolve, 25));+ opener.postMessage("capturing", "*");+ })();+ `));+ crossSiteUrl.hostname = uniqueLocalhostHostname("tab-shows-capture-cross-site");+ const sameSiteUrl = await echo("tab-shows-capture-same-site", `opener.postMessage("arrived", "*");`);++ const capturing = nextMessage();+ const popup = window.open(crossSiteUrl.href);+ await capturing;+ println("the popup's tab shows capture in another process");++ // The popup's page leaves its tab while it captures, as the popup's next document is displayed by this process.+ const arrived = nextMessage();+ const navigatedAt = performance.now();+ popup.location.href = sameSiteUrl;+ await arrived;+ println(`stops showing it a while later: ${await waitForCondition(() => !popup.internals.tabIsUsingMicrophone())}`);+ const shownFor = performance.now() - navigatedAt;+ println(`went on showing it for at least two and a half seconds after navigating: ${shownFor >= 2500}`);+ popup.close();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-its-picture-in-picture-window-closes.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-its-picture-in-picture-window-closes.htmlnew file mode 100644index 00000000000..7cf7d0197db--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-its-picture-in-picture-window-closes.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video src="../../../Assets/test-webm.webm"></video>+<script>+ // A stream labelled for a Picture-in-Picture window outlives the window in the AudioServer's report. Its label then+ // names no page, so it shows on every tab of the process that may capture until it ends, and for one linger after.+ asyncTest(async done => {+ const video = document.querySelector("video");+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));++ await internals.setPermission("microphone", "granted");+ const otherTab = window.open("about:blank");++ internals.grantTransientActivation();+ await video.requestPictureInPicture();+ const windowPageId = internals.pictureInPictureWindowPageId();++ internals.reportAudioCaptureOfPage(windowPageId, true);+ println(`before the window closes, this tab: ${internals.tabIsCapturingAudio()}`);+ println(`before the window closes, the other tab: ${otherTab.internals.tabIsCapturingAudio()}`);++ await document.exitPictureInPicture();+ println(`the window closed: ${internals.pictureInPictureWindowState}`);+ println(`once the window closed, this tab: ${internals.tabIsCapturingAudio()}`);+ println(`once the window closed, the other tab: ${await waitForCondition(() => otherTab.internals.tabIsCapturingAudio())}`);++ const endedAt = performance.now();+ internals.reportAudioCaptureOfPage(windowPageId, false);+ otherTab.close();+ println(`stops capturing: ${await waitForCondition(() => !internals.tabIsCapturingAudio())}`);+ println(`stops showing it a while later: ${await waitForCondition(() => !internals.tabIsUsingMicrophone())}`);+ println(`went on showing it for at least two and a half seconds: ${performance.now() - endedAt >= 2500}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-its-process-is-lost.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-its-process-is-lost.htmlnew file mode 100644index 00000000000..a59319dc876--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-its-process-is-lost.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ internals.setTestTimeout(30_000);+ // The tab can still be remembered as using the microphone for a previous test.+ await waitForCondition(() => !internals.tabIsUsingMicrophone());++ // The tab's grant is what reaches the iframe.+ await internals.setPermission("microphone", "granted");++ const url = new URL(+ await httpTestServer().createEcho("GET", "/tab-shows-capture-a-while-after-its-process-is-lost", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>+ // getUserMedia() waits for its document to have focus.+ window.focus();+ navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => window.stream = stream);+ <\/script>`,+ })+ );+ url.hostname = uniqueLocalhostHostname("tab-shows-capture-a-while-after-its-process-is-lost");++ const iframe = document.createElement("iframe");+ iframe.allow = "microphone";+ iframe.src = url;+ document.body.appendChild(iframe);+ println(`captures in the isolated iframe: ${await waitForCondition(() => internals.tabIsCapturingAudio())}`);++ // A page lost along with its process stops capturing at once, and the tab goes on showing it for a while, as it+ // does however capture stops.+ const crashedAt = performance.now();+ internals.crashRemoteFrameProcesses();+ println(`stops capturing once the iframe's process is gone: ${await waitForCondition(() => !internals.tabIsCapturingAudio())}`);+ println(`stops showing it a while later: ${await waitForCondition(() => !internals.tabIsUsingMicrophone())}`);+ println(`went on showing it for at least two and a half seconds after the crash: ${performance.now() - crashedAt >= 2500}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-the-permission-is-taken-away.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-the-permission-is-taken-away.htmlnew file mode 100644index 00000000000..8927421b701--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-a-while-after-the-permission-is-taken-away.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // Taking the permission away ends the capture, and the tab goes on showing it for the linger, as after any other end.+ asyncTest(async done => {+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });+ println(`capturing: ${await waitForCondition(() => internals.tabIsCapturingAudio())}`);++ const takenAwayAt = performance.now();+ await internals.setPermission("microphone", "denied");+ println(`stops capturing once the permission is taken away: ${await waitForCondition(() => !internals.tabIsCapturingAudio())}`);+ println(`stops showing it a while later: ${await waitForCondition(() => !internals.tabIsUsingMicrophone())}`);+ println(`went on showing it for at least two and a half seconds: ${performance.now() - takenAwayAt >= 2500}`);+ println(`the track ended: ${stream.getTracks()[0].readyState}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-labelled-for-no-page.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-labelled-for-no-page.htmlnew file mode 100644index 00000000000..d87e5c600bb--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-labelled-for-no-page.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The renderer labels its capture streams, so a stream labelled for none of its pages still shows on every page of+ // the process that may capture.+ asyncTest(async done => {+ internals.reportAudioCaptureOfUnknownPage(true);+ println(`without the permission: ${internals.tabIsCapturingAudio()}`);++ await internals.setPermission("microphone", "granted");+ println(`with the permission: ${internals.tabIsCapturingAudio()}`);++ internals.reportAudioCaptureOfUnknownPage(false);+ println(`once the stream is gone: ${internals.tabIsCapturingAudio()}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-of-its-picture-in-picture-window.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-of-its-picture-in-picture-window.htmlnew file mode 100644index 00000000000..e0a26f91f33--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-of-its-picture-in-picture-window.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<video src="../../../Assets/test-webm.webm"></video>+<script>+ // A Picture-in-Picture window is part of its tab, so a stream labelled for it shows on that tab and no other. Once the+ // window is retired it's part of no tab, even while its process keeps its page open, so the label counts for nothing.+ asyncTest(async done => {+ const video = document.querySelector("video");+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));++ await internals.setPermission("microphone", "granted");+ const otherTab = window.open("about:blank");++ internals.grantTransientActivation();+ await video.requestPictureInPicture();+ const windowPageId = internals.pictureInPictureWindowPageId();++ internals.reportAudioCaptureOfPage(windowPageId, true);+ println(`labelled for the window, this tab: ${internals.tabIsCapturingAudio()}`);+ println(`labelled for the window, the other tab: ${otherTab.internals.tabIsCapturingAudio()}`);++ internals.reportAudioCaptureOfPage(windowPageId, false);+ println(`stream gone, this tab: ${internals.tabIsCapturingAudio()}`);++ internals.reportAudioCaptureOfPage(windowPageId, true);+ internals.retirePictureInPictureWindow();+ println(`window retired, this tab: ${internals.tabIsCapturingAudio()}`);+ println(`window retired, the other tab: ${otherTab.internals.tabIsCapturingAudio()}`);++ internals.reportAudioCaptureOfPage(windowPageId, false);+ otherTab.close();+ await document.exitPictureInPicture();+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-while-a-track-lives.html b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-while-a-track-lives.htmlnew file mode 100644index 00000000000..41b347ec3fc--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/tab-shows-capture-while-a-track-lives.html@@ -0,0 +1,28 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ // The tab can still be remembered as using the microphone for a previous test.+ await waitForCondition(() => !internals.tabIsUsingMicrophone());+ println(`before: ${internals.tabIsCapturingAudio()}`);++ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });+ println(`capturing while the track lives, unconsumed: ${await waitForCondition(() => internals.tabIsCapturingAudio())}`);++ // Each track holds a stream of the source, which stays open for the clone once the original stops. An earlier+ // test's tracks can hold streams in this process too.+ const streamsBefore = internals.audioCaptureStreamCount();+ const clone = stream.clone().getTracks()[0];+ println(`the clone holds a stream: ${internals.audioCaptureStreamCount() === streamsBefore + 1}`);+ stream.getTracks()[0].stop();+ println(`the clone's stream outlives the original's: ${await waitForCondition(() => internals.audioCaptureStreamCount() === streamsBefore)}`);+ println(`capturing after the original stops: ${internals.tabIsCapturingAudio()}`);++ const stoppedAt = performance.now();+ clone.stop();+ println(`stops capturing once the clone stops: ${await waitForCondition(() => !internals.tabIsCapturingAudio())}`);+ println(`stops showing it a while later: ${await waitForCondition(() => !internals.tabIsUsingMicrophone())}`);+ println(`went on showing it for at least two and a half seconds: ${performance.now() - stoppedAt >= 2500}`);+ done();+ });+</script>diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex 7a7e140c477..8479be61ac8 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -50,7 +50,7 @@ ladybird_test_suite(LibWebView)set(TEST_SOURCESTestAccessibilityTreeManager.cpp- TestAudioServerAudibility.cpp+ TestAudioServerActivity.cppTestAutocompleteMuxer.cppTestAutocompleteRanker.cppTestBackgroundNetworking.cppdiff --git a/Tests/LibWebView/TestAudioServerAudibility.cpp b/Tests/LibWebView/TestAudioServerActivity.cppsimilarity index 59%rename from Tests/LibWebView/TestAudioServerAudibility.cpprename to Tests/LibWebView/TestAudioServerActivity.cppindex b8ff311d29c..0cedc3d6726 100644--- a/Tests/LibWebView/TestAudioServerAudibility.cpp+++ b/Tests/LibWebView/TestAudioServerActivity.cpp@@ -8,7 +8,7 @@#include <LibCore/EventLoop.h>#include <LibIPC/Transport.h>#include <LibTest/TestCase.h>-#include <LibWebView/AudioServerAudibility.h>+#include <LibWebView/AudioServerActivity.h>#include <LibWebView/AudioServerControlClient.h>static constexpr u64 TAB_ID = 1;@@ -40,25 +40,35 @@ struct Controller {EXPECT(!stub.handle(make<Messages::AudioServerControlClient::PlaybackGroupAudibilityChanged>(TAB_ID, group, audibility)).is_error());}+ void report_capture(Audio::PlaybackGroupId group, bool capturing, u64 tab_id = TAB_ID)+ {+ auto& stub = static_cast<AudioServerControlClientStub&>(*client);+ EXPECT(!stub.handle(make<Messages::AudioServerControlClient::CaptureActivityChanged>(tab_id, group, capturing)).is_error());+ }+void die() { client->shutdown(); }};-struct AudibilityFixture {+struct ActivityFixture {Core::EventLoop loop;Vector<Change> changes;- WebView::AudioServerAudibility audibility { [this](Audio::PlaybackGroupId group, Audio::Audibility audibility) {- changes.append({ group, audibility });- } };+ size_t capture_changes { 0 };+ WebView::AudioServerActivity activity;++ ActivityFixture()+ : activity([this](Audio::PlaybackGroupId group, Audio::Audibility audibility) { changes.append({ group, audibility }); }, [this] { ++capture_changes; })+ {+ }void watch(Controller& controller){- audibility.watch(*controller.client, [](u64, Audio::PlaybackGroupId, Audio::Audibility) { return true; });+ activity.watch(*controller.client, [](u64, Audio::PlaybackGroupId, Audio::Audibility) { return true; }, [](u64 tab_id) { return tab_id == TAB_ID; });}};TEST_CASE(a_replaced_audio_server_is_forgotten_at_once_and_heard_from_no_more){- AudibilityFixture fixture;+ ActivityFixture fixture;auto replaced = Controller::create();fixture.watch(replaced);replaced.report(GROUP, AUDIBLE);@@ -80,7 +90,7 @@ TEST_CASE(a_replaced_audio_server_is_forgotten_at_once_and_heard_from_no_more)TEST_CASE(a_dead_audio_servers_late_reports_are_ignored){- AudibilityFixture fixture;+ ActivityFixture fixture;auto controller = Controller::create();fixture.watch(controller);controller.report(GROUP, AUDIBLE);@@ -94,12 +104,12 @@ TEST_CASE(a_dead_audio_servers_late_reports_are_ignored)TEST_CASE(a_forgotten_group_is_inaudible_until_reported_audible_again){- AudibilityFixture fixture;+ ActivityFixture fixture;auto controller = Controller::create();fixture.watch(controller);controller.report(GROUP, AUDIBLE);- fixture.audibility.forget(GROUP);- fixture.audibility.forget(GROUP);+ fixture.activity.forget_audibility(GROUP);+ fixture.activity.forget_audibility(GROUP);EXPECT(fixture.changes == (Vector<Change> { { GROUP, AUDIBLE }, { GROUP, INAUDIBLE } }));controller.report(GROUP, AUDIBLE);@@ -108,10 +118,43 @@ TEST_CASE(a_forgotten_group_is_inaudible_until_reported_audible_again)TEST_CASE(reports_the_filter_turns_away_are_not_heard){- AudibilityFixture fixture;+ ActivityFixture fixture;auto controller = Controller::create();- fixture.audibility.watch(*controller.client, [](u64, Audio::PlaybackGroupId group, Audio::Audibility) { return group == GROUP; });+ fixture.activity.watch(*controller.client, [](u64, Audio::PlaybackGroupId group, Audio::Audibility) { return group == GROUP; }, [](u64) { return true; });controller.report(OTHER_GROUP, AUDIBLE);controller.report(GROUP, AUDIBLE);EXPECT(fixture.changes == Vector<Change> { { GROUP, AUDIBLE } });}++TEST_CASE(capturing_groups_follow_the_reports_of_the_watched_tab)+{+ ActivityFixture fixture;+ auto controller = Controller::create();+ fixture.watch(controller);+ controller.report_capture(GROUP, true);+ controller.report_capture(GROUP, true);+ controller.report_capture(OTHER_GROUP, true, TAB_ID + 1);+ EXPECT(fixture.activity.capturing_groups().contains(GROUP));+ EXPECT_EQ(fixture.activity.capturing_groups().size(), 1u);+ EXPECT_EQ(fixture.capture_changes, 1u);++ controller.report_capture(GROUP, false);+ EXPECT(fixture.activity.capturing_groups().is_empty());+ EXPECT_EQ(fixture.capture_changes, 2u);+}++TEST_CASE(capture_goes_with_the_audio_server_but_not_with_a_forgotten_sound)+{+ ActivityFixture fixture;+ auto controller = Controller::create();+ fixture.watch(controller);+ controller.report_capture(GROUP, true);+ fixture.activity.forget_audibility(GROUP);+ EXPECT(fixture.activity.capturing_groups().contains(GROUP));++ controller.die();+ EXPECT(fixture.activity.capturing_groups().is_empty());+ EXPECT_EQ(fixture.capture_changes, 2u);+ controller.report_capture(GROUP, true);+ EXPECT(fixture.activity.capturing_groups().is_empty());+}diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex ad36ae5a1a0..cf122b064c2 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -592,6 +592,8 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)case WebView::TabIndicator::UsingLocation:m_location_edit->set_location_in_use(visible);break;+ case WebView::TabIndicator::UsingMicrophone:+ break;}};