Skip to content

Truly independent web browser

LibWebView: Initialize every AudioServer before connecting clients to it

Luke Wilded74daea7b0d7
diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cpp
index e75b1ba4aad..c0fd0de7891 100644
--- a/Libraries/LibWebView/HelperProcess.cpp
+++ b/Libraries/LibWebView/HelperProcess.cpp
@@ -694,11 +694,12 @@ ErrorOr<void> ensure_audio_server_is_running(RefPtr<AudioServerControlClient>& c
return {};
}
-ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(RefPtr<AudioServerControlClient>& controller, u64 owner_child_id, u64 audio_tab_id)
+ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(AudioServerControlClient& controller, u64 audio_tab_id)
{
- TRY(ensure_audio_server_is_running(controller, owner_child_id));
+ if (!controller.is_open())
+ return Error::from_string_literal("The AudioServer is gone");
- auto response = controller->send_sync_but_allow_failure<Messages::AudioServerControl::ConnectNewClient>(audio_tab_id);
+ auto response = controller.send_sync_but_allow_failure<Messages::AudioServerControl::ConnectNewClient>(audio_tab_id);
if (!response || !response->handle().has_value())
return Error::from_string_literal("Failed to connect to AudioServer");
return response->take_handle().release_value();
diff --git a/Libraries/LibWebView/HelperProcess.h b/Libraries/LibWebView/HelperProcess.h
index 627d135da07..0636ad4615b 100644
--- a/Libraries/LibWebView/HelperProcess.h
+++ b/Libraries/LibWebView/HelperProcess.h
@@ -62,8 +62,9 @@ WEBVIEW_API u64 allocate_audio_tab_id();
// Launches the AudioServer for a renderer if it has none, keeping its controller connection in the given slot. The
// server is listed with the renderer's process, which the owner child ID names.
WEBVIEW_API ErrorOr<void> ensure_audio_server_is_running(RefPtr<AudioServerControlClient>& controller, u64 owner_child_id);
-// Likewise, and connects a new client to it as a member of the given tab's mix.
-WEBVIEW_API ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(RefPtr<AudioServerControlClient>& controller, u64 owner_child_id, u64 audio_tab_id);
+// Connects a new client to a running AudioServer as a member of the given tab's mix, or fails without launching
+// another, so that whoever launched the server can tell its replacement what it needs to know first.
+WEBVIEW_API ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(AudioServerControlClient& controller, u64 audio_tab_id);
// Launches the MediaServer for a renderer if it has none, keeping its controller connection in the given slot, and
// connects a new client to it. The server gets its AudioServer connection through connect_audio_server, and its
// VideoDecoderServer from the Browser, which launches one for it on request. Both servers it launches are listed with
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index eecd53939a7..43e3b70ae98 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -881,9 +881,17 @@ void WebContentClient::request_video_presentation_channel()
ErrorOr<IPC::TransportHandle> WebContentClient::connect_audio_server_client()
{
- // The server must know of the permission before any client gets its connection.
- TRY(ensure_audio_server_controller());
- return connect_new_audio_server_client(m_audio_server_client, child_id(), m_audio_tab_id);
+ // The server must know of the permission before any client gets its connection. A failed request can reveal that the
+ // existing server has died, so the connection is tried once more, letting a closed controller be replaced and
+ // initialized first.
+ auto connect = [this]() -> ErrorOr<IPC::TransportHandle> {
+ TRY(ensure_audio_server_controller());
+ return connect_new_audio_server_client(*m_audio_server_client, m_audio_tab_id);
+ };
+ auto handle = connect();
+ if (handle.is_error())
+ handle = connect();
+ return handle;
}
ErrorOr<void> WebContentClient::ensure_audio_server_controller()
@@ -892,6 +900,16 @@ ErrorOr<void> WebContentClient::ensure_audio_server_controller()
TRY(ensure_audio_server_is_running(m_audio_server_client, child_id()));
if (m_audio_server_client.ptr() == previous_controller)
return {};
+ // A server that isn't told everything gets no clients; the next attempt starts over with another.
+ if (auto result = initialize_audio_server_controller(); result.is_error()) {
+ m_audio_server_client->shutdown();
+ return result.release_error();
+ }
+ return {};
+}
+
+ErrorOr<void> WebContentClient::initialize_audio_server_controller()
+{
m_audio_server_client->on_capture_requested = [weak_self = make_weak_ptr<WebContentClient>()](u64 tab_id, u64 request_id, ByteString const&) {
auto self = weak_self.strong_ref();
if (self && tab_id == self->m_audio_tab_id)
diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.h
index 055582d426c..b883f0af24a 100644
--- a/Libraries/LibWebView/WebContentClient.h
+++ b/Libraries/LibWebView/WebContentClient.h
@@ -244,6 +244,7 @@ private:
ErrorOr<IPC::TransportHandle> connect_audio_server_client();
ErrorOr<void> ensure_audio_server_controller();
+ ErrorOr<void> initialize_audio_server_controller();
ErrorOr<void> tell_audio_server_of_capture_permission();
static String audio_capture_permission_key(URL::URL const&);
bool audio_capture_permitted() const;
diff --git a/Libraries/LibWebView/WebWorkerClient.cpp b/Libraries/LibWebView/WebWorkerClient.cpp
index d7cf896e180..5e5b20f101d 100644
--- a/Libraries/LibWebView/WebWorkerClient.cpp
+++ b/Libraries/LibWebView/WebWorkerClient.cpp
@@ -167,7 +167,8 @@ Messages::WebWorkerClient::RequestMediaServerConnectionResponse WebWorkerClient:
auto self = weak_self.strong_ref();
if (!self)
return Error::from_string_literal("The worker process is gone");
- return connect_new_audio_server_client(self->m_audio_server_client, self->child_id(), self->m_audio_tab_id);
+ TRY(ensure_audio_server_is_running(self->m_audio_server_client, self->child_id()));
+ return connect_new_audio_server_client(*self->m_audio_server_client, self->m_audio_tab_id);
});
if (connection.is_error()) {
warnln("Unable to connect a MediaServer client: {}", connection.error());