Skip to content

Truly independent web browser

LibWeb+WebContent: Wake waiting media requests when focus comes back

Ali Mohammad Pur33c571546281
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
index b4075bf9fdb..b9923ee3036 100644
--- a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
@@ -183,6 +183,18 @@ bool MediaDevices::device_enumeration_can_proceed()
return is_in_view();
}
+// https://w3c.github.io/mediacapture-main/#dfn-has-system-focus
+void MediaDevices::system_focus_changed()
+{
+ // 11.7 While hasSystemFocus is false, the User Agent MUST wait to proceed to the next step until a task queued to set
+ // hasSystemFocus to the result of the has system focus algorithm, would set hasSystemFocus to true.
+ HTML::queue_global_task(HTML::Task::Source::UserInteraction, HTML::relevant_global_object(*m_window),
+ GC::create_function(heap(), [media_devices = GC::Ref(*this)] {
+ media_devices->process_pending_enumerate_devices_requests();
+ media_devices->process_pending_get_user_media_requests();
+ }));
+}
+
bool MediaDevices::get_user_media_can_proceed() const
{
return is_in_view() && m_window->associated_document().has_focus();
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.h b/Libraries/LibWeb/MediaCapture/MediaDevices.h
index f348464e5a7..a9f5ee0f8bf 100644
--- a/Libraries/LibWeb/MediaCapture/MediaDevices.h
+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.h
@@ -46,6 +46,8 @@ public:
// Ends every live track once the document may no longer use the microphone.
void end_tracks_without_permission();
void track_was_cloned(Badge<MediaStreamTrack>, MediaStreamTrack& clone);
+ // getUserMedia() waits while the page lacks system focus, and the Window sees no event when that changes.
+ void system_focus_changed();
[[nodiscard]] static GC::Ref<MediaDevices> create(HTML::Window&);
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 579a44b4d66..a713f82b021 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -34,6 +34,7 @@
#include <LibWeb/HTML/HistoryExecutor.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
#include <LibWeb/HTML/NavigableContainer.h>
+#include <LibWeb/HTML/Navigator.h>
#include <LibWeb/HTML/OffscreenCanvas.h>
#include <LibWeb/HTML/RemoteNavigable.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -42,6 +43,7 @@
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Loader/ContentBlocker.h>
+#include <LibWeb/MediaCapture/MediaDevices.h>
#include <LibWeb/NotificationsAPI/Notification.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Page/RenderingStats.h>
@@ -214,6 +216,22 @@ GC::Ptr<HTML::LocalNavigable> Page::hosted_focused_navigable() const
return as_if<HTML::LocalNavigable>(focused_navigable().ptr());
}
+void Page::system_focus_changed()
+{
+ for (auto const& navigable : hosted_navigables()) {
+ auto document = navigable->active_document();
+ if (!document)
+ continue;
+ auto window = document->window();
+ if (!window)
+ continue;
+ if (auto navigator = window->navigator_if_created(); navigator) {
+ if (auto media_devices = navigator->media_devices_if_created())
+ media_devices->system_focus_changed();
+ }
+ }
+}
+
void Page::set_focused_navigable(HTML::Navigable& navigable)
{
if (m_focused_navigable == &navigable)
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index f3f022c45bb..eeaf4dc3f1f 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -181,6 +181,7 @@ public:
GC::Ptr<HTML::Navigable> focused_navigable() const;
GC::Ptr<HTML::LocalNavigable> hosted_focused_navigable() const;
void set_focused_navigable(HTML::Navigable&);
+ void system_focus_changed();
void focused_navigable_changed_in_another_page(HTML::CrossProcessId);
void navigable_document_destroyed(Badge<DOM::Document>, HTML::LocalNavigable&);
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index de164ff25d4..bc7343ef282 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -225,6 +225,8 @@ void PageClient::set_has_focus(bool has_focus)
controller->dismiss();
Web::Painting::request_document_repaint(*document, Web::InvalidateDisplayList::PaintCommands);
}
+
+ page().system_focus_changed();
}
void PageClient::set_window_handle(Utf16String window_handle)