Skip to content

Truly independent web browser

LibWebView: Don’t report audibility to a frontend being torn down

sideshowbarkerc60d9b515cad
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 5debc82f17b..24f170b492a 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -113,9 +113,11 @@ 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 that the location is no longer in use.
+ // cancelling the view's location requests that the location is no longer in use, nor closing the view's page (which
+ // revokes its audio playback) that the tab went quiet.
on_url_change = nullptr;
on_location_in_use_change = nullptr;
+ on_audibility_changed = nullptr;
answer_pending_console_input_completions();
TabPerformanceMonitor::forget_view(view_id());
// Leave the views before the permission manager hears: a one-time grant that ends with this view has it revoke
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 3b1aeccbb1a..bb594a03554 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -259,6 +259,7 @@ if (NOT WIN32)
TestShadowTreeTooltipsAndLinks.cpp
TestTabPerformanceMonitor.cpp
TestUserActivation.cpp
+ TestViewDestruction.cpp
TestWebCompatReport.cpp
TestWebCompatReportPage.cpp
)
diff --git a/Tests/LibWebView/TestViewDestruction.cpp b/Tests/LibWebView/TestViewDestruction.cpp
new file mode 100644
index 00000000000..aca563aeda5
--- /dev/null
+++ b/Tests/LibWebView/TestViewDestruction.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "HeadlessTest.h"
+#include <AK/LexicalPath.h>
+#include <LibCore/EventLoop.h>
+#include <LibGfx/SystemTheme.h>
+#include <LibTest/TestCase.h>
+#include <LibWebView/CanonicalTraversable.h>
+#include <LibWebView/HeadlessWebView.h>
+#include <LibWebView/Utilities.h>
+#include <LibWebView/WebContentPage.h>
+
+// A frontend destroys its widgets before the view they hold: the view's teardown, which closes its page, must not call
+// back into the frontend.
+TEST_CASE(a_view_being_destroyed_does_not_report_its_audibility)
+{
+ Test::HeadlessTestEnvironment environment { "TestViewDestruction"sv };
+
+ auto app = TRY_OR_FAIL(environment.create_application());
+
+ auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);
+ auto theme = TRY_OR_FAIL(Gfx::load_system_theme(theme_path.string()));
+ OwnPtr<WebView::HeadlessWebView> view = WebView::HeadlessWebView::create(move(theme), { 800, 600 });
+
+ size_t loads_finished = 0;
+ view->on_load_finish = [&](auto const&) { ++loads_finished; };
+ Core::EventLoop::current().spin_until([&] { return loads_finished == 1; });
+
+ Optional<Audio::Audibility> reported_audibility;
+ view->on_audibility_changed = [&](auto audibility) { reported_audibility = audibility; };
+ view->traversable().for_each_hosting_page([](WebView::WebContentPage& page) {
+ page.set_audibility(Audio::Audibility::Audible);
+ });
+ EXPECT(reported_audibility == Audio::Audibility::Audible);
+
+ reported_audibility.clear();
+ view = nullptr;
+ EXPECT(!reported_audibility.has_value());
+}