Skip to content

Truly independent web browser

LibWebView+UI/Qt: Keep crash reports per profile

Andreas Klingcd895ceacdd2
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 2d666ba3efe..953e94cc6d9 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -674,6 +674,19 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)
}
}
+ // Reports belong to the profile they came from, whose own setting decides what happens to them. A temporary
+ // profile takes nothing over, since it would take it to its grave.
+ auto& crash_report_store = CrashReportStore::the();
+ crash_report_store.set_directory(LexicalPath::join(profile().paths().data, "CrashReports"sv).string());
+ if (!profile().is_temporary()) {
+ if (auto result = crash_report_store.adopt_reports_from(CrashReportStore::default_directory()); result.is_error())
+ warnln("Could not move crash reports into the profile: {}", result.error());
+ }
+ if (captures_browser_crashes()) {
+ if (auto result = crash_report_store.initialize_browser_crash_handler(); result.is_error())
+ warnln("Could not prepare Browser crash reporting: {}", result.error());
+ }
+
// Test results must not depend on the languages of the machine running the tests.
auto platform_languages = m_web_content_options.is_test_mode == IsTestMode::Yes ? Vector<String> {} : platform_preferred_languages();
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 5500e8f72f9..ff54e7df6f0 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -458,6 +458,7 @@ protected:
virtual NonnullOwnPtr<MicrophoneAuthorizer> create_microphone_authorizer();
virtual bool should_coordinate_browser_process() const { return true; }
+ virtual bool captures_browser_crashes() const { return false; }
virtual bool should_use_os_geolocation() const { return true; }
virtual SiteIsolationMode default_site_isolation_mode() const { return SiteIsolationMode::IFrame; }
diff --git a/Libraries/LibWebView/CrashReport.h b/Libraries/LibWebView/CrashReport.h
index 6e8138d98be..56a627e9547 100644
--- a/Libraries/LibWebView/CrashReport.h
+++ b/Libraries/LibWebView/CrashReport.h
@@ -40,7 +40,7 @@ public:
static ByteString describe_current_build(ProcessType);
ErrorOr<void> record_build_description(StringView description);
- ErrorOr<void> save(int wait_status, ByteString const& directory = CrashReportStore::default_directory(),
+ ErrorOr<void> save(int wait_status, ByteString const& directory = CrashReportStore::the().directory(),
Optional<UnixDateTime> crashed_at = {});
explicit CrashReport(NonnullOwnPtr<Core::File> file, ProcessType process_type)
diff --git a/Libraries/LibWebView/CrashReportStore.cpp b/Libraries/LibWebView/CrashReportStore.cpp
index fc4045c0d51..a4e6efe2e68 100644
--- a/Libraries/LibWebView/CrashReportStore.cpp
+++ b/Libraries/LibWebView/CrashReportStore.cpp
@@ -20,6 +20,7 @@
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
+#include <LibFileSystem/FileSystem.h>
#include <LibWebView/CrashReport.h>
#include <LibWebView/CrashReportStore.h>
#include <LibWebView/ProcessManager.h>
@@ -167,11 +168,37 @@ static ErrorOr<Optional<Core::Directory>> open_seen_directory_if_exists(Core::Di
}
// Moving is atomic, so a report is always in exactly one of the two directories. Moving it where it already is succeeds.
+static timespec modified_time(struct stat const&);
+
+// A profile can live on another file system than the place reports used to be kept, which rename cannot reach.
+static ErrorOr<void> copy_report_across_file_systems(Core::Directory const& from, Core::Directory const& to, ByteString const& name, struct stat const& status)
+{
+ auto text = TRY(read_owned_file(from, name));
+ auto file = TRY(to.open(name, Core::File::OpenMode::Write | Core::File::OpenMode::MustBeNew | Core::File::OpenMode::NoFollow, 0400));
+ ArmedScopeGuard remove_incomplete_copy = [&] { (void)unlinkat(to.fd(), name.characters(), 0); };
+ TRY(file->write_until_depleted(text.bytes()));
+
+ // A report's age is based on this, so the copy has to expire when the report would have.
+ auto modified = modified_time(status);
+ timespec times[2] { modified, modified };
+ if (futimens(file->fd(), times) < 0 || fsync(file->fd()) < 0)
+ return Error::from_errno(errno);
+ remove_incomplete_copy.disarm();
+
+ if (unlinkat(from.fd(), name.characters(), 0) < 0)
+ return Error::from_errno(errno);
+ return {};
+}
+
static ErrorOr<void> move_report(Core::Directory const& from, Core::Directory const& to, ByteString const& name)
{
struct stat status {};
if (is_owned_regular_file_at(from, name, status)) {
- TRY(Core::System::renameat(from.fd(), name, to.fd(), name));
+ if (auto result = Core::System::renameat(from.fd(), name, to.fd(), name); result.is_error()) {
+ if (result.error().code() != EXDEV)
+ return result.release_error();
+ TRY(copy_report_across_file_systems(from, to, name, status));
+ }
// The move is only durable once both directories are, or a report could be offered again.
if (fsync(to.fd()) < 0 || fsync(from.fd()) < 0)
return Error::from_errno(errno);
@@ -182,6 +209,18 @@ static ErrorOr<void> move_report(Core::Directory const& from, Core::Directory co
return Error::from_string_literal("Crash report no longer exists");
}
+static void move_all_reports(Core::Directory const& from, Core::Directory const& to)
+{
+ Core::DirIterator iterator(from.path().string(), Core::DirIterator::SkipDots);
+ while (iterator.has_next()) {
+ auto name = iterator.next_path();
+ if (!CrashReportStore::is_saved_report_name(name))
+ continue;
+ if (auto result = move_report(from, to, name); result.is_error())
+ warnln("Could not move crash report {}: {}", name, result.error());
+ }
+}
+
static timespec modified_time(struct stat const& status)
{
# if defined(AK_OS_MACOS)
@@ -309,6 +348,21 @@ ErrorOr<void> CrashReportStore::mark_seen(ByteString const& name) const
return {};
}
+ErrorOr<void> CrashReportStore::adopt_reports_from(ByteString const& directory) const
+{
+ if (directory == m_directory || !FileSystem::exists(directory))
+ return {};
+
+ auto from = TRY(open_report_directory(directory, Core::Directory::CreateDirectories::No));
+ auto to = TRY(open_report_directory(m_directory));
+ move_all_reports(from, to);
+ if (auto seen_from = TRY(open_seen_directory_if_exists(from)); seen_from.has_value()) {
+ auto seen_to = TRY(open_seen_directory(to));
+ move_all_reports(*seen_from, seen_to);
+ }
+ return {};
+}
+
ErrorOr<void> CrashReportStore::remove_all_reports() const
{
auto directory = TRY(open_report_directory(m_directory));
@@ -495,6 +549,7 @@ bool CrashReportStore::has_pending_reports(UnixDateTime) const { return false; }
ErrorOr<void> CrashReportStore::mark_seen(ByteString const&) const { return {}; }
ErrorOr<void> CrashReportStore::remove_report(ByteString const&) const { return {}; }
ErrorOr<void> CrashReportStore::remove_all_reports() const { return {}; }
+ErrorOr<void> CrashReportStore::adopt_reports_from(ByteString const&) const { return {}; }
ErrorOr<ByteString> CrashReportStore::store_report(ProcessType, StringView, UnixDateTime) const
{
diff --git a/Libraries/LibWebView/CrashReportStore.h b/Libraries/LibWebView/CrashReportStore.h
index 23c880a7020..75d85f8791d 100644
--- a/Libraries/LibWebView/CrashReportStore.h
+++ b/Libraries/LibWebView/CrashReportStore.h
@@ -31,6 +31,10 @@ public:
}
ByteString const& directory() const { return m_directory; }
+ void set_directory(ByteString directory) { m_directory = move(directory); }
+
+ // Moves in every saved report of another store, which is how reports from before they belonged to a profile arrive.
+ ErrorOr<void> adopt_reports_from(ByteString const& directory) const;
ErrorOr<SavedReport> saved_report(ByteString const& name) const;
diff --git a/Tests/LibWebView/test-webdriver-browser-crash-recovery.py b/Tests/LibWebView/test-webdriver-browser-crash-recovery.py
index de68cde0b48..ee852ee6f00 100644
--- a/Tests/LibWebView/test-webdriver-browser-crash-recovery.py
+++ b/Tests/LibWebView/test-webdriver-browser-crash-recovery.py
@@ -16,8 +16,10 @@ from pathlib import Path
webdriver = importlib.import_module("test-webdriver-delete-session")
-def launch(webdriver_binary, environment):
- process, port = webdriver.launch_webdriver(webdriver_binary, "--headless", env=environment)
+def launch(webdriver_binary, profile, environment):
+ process, port = webdriver.launch_webdriver(
+ webdriver_binary, "--headless", f"--profile-path={profile}", env=environment
+ )
status, payload, response = webdriver.request(
port, "POST", "/session", {"capabilities": {"alwaysMatch": {"ladybird:headless": True}}}
)
@@ -63,10 +65,11 @@ def run_test(webdriver_binary):
("XDG_CACHE_HOME", "cache"),
):
environment[variable] = str(Path(temporary) / directory)
- reports_directory = Path(temporary) / "data/Ladybird/CrashReports"
+ profile = Path(temporary) / "profile"
+ reports_directory = profile / "data/CrashReports"
report = write_browser_report(reports_directory)
- process, port, session = launch(webdriver_binary, environment)
+ process, port, session = launch(webdriver_binary, profile, environment)
try:
# A running browser keeps a signal-safe record, which the next launch recovers if a fatal signal ends it.
deadline = time.monotonic() + webdriver.EVENT_TIMEOUT_SECONDS
diff --git a/Tests/LibWebView/test-webdriver-crash-report.py b/Tests/LibWebView/test-webdriver-crash-report.py
index 823ce9fbe00..8019915996c 100755
--- a/Tests/LibWebView/test-webdriver-crash-report.py
+++ b/Tests/LibWebView/test-webdriver-crash-report.py
@@ -65,7 +65,10 @@ def run_test(webdriver_binary, process_name):
("XDG_CACHE_HOME", "cache"),
):
environment[variable] = str(Path(temporary) / directory)
- webdriver, port = webdriver_helpers.launch_webdriver(webdriver_binary, "--headless", env=environment)
+ profile = Path(temporary) / "profile"
+ webdriver, port = webdriver_helpers.launch_webdriver(
+ webdriver_binary, "--headless", f"--profile-path={profile}", env=environment
+ )
try:
status, payload, body = webdriver_helpers.request(
port,
@@ -127,7 +130,7 @@ def run_test(webdriver_binary, process_name):
elif process_name == "Compositor":
wait_for_compositor(port, session)
crash_helper(webdriver.pid, process_name)
- directory = Path(temporary) / "data/Ladybird/CrashReports"
+ directory = profile / "data/CrashReports"
deadline = time.monotonic() + webdriver_helpers.EVENT_TIMEOUT_SECONDS
while True:
reports = list(directory.glob(f"*-{process_name}-*.txt"))
diff --git a/Tests/LibWebView/test-webdriver-worker-terminated-while-starting.py b/Tests/LibWebView/test-webdriver-worker-terminated-while-starting.py
index c82789cfd29..f39c19fac27 100755
--- a/Tests/LibWebView/test-webdriver-worker-terminated-while-starting.py
+++ b/Tests/LibWebView/test-webdriver-worker-terminated-while-starting.py
@@ -51,7 +51,10 @@ def run_test(webdriver_binary):
("XDG_CACHE_HOME", "cache"),
):
environment[variable] = str(Path(temporary) / directory)
- webdriver, port = webdriver_helpers.launch_webdriver(webdriver_binary, "--headless", env=environment)
+ profile = Path(temporary) / "profile"
+ webdriver, port = webdriver_helpers.launch_webdriver(
+ webdriver_binary, "--headless", f"--profile-path={profile}", env=environment
+ )
try:
session = webdriver_helpers.create_session(port)
status, _, body = webdriver_helpers.request(
@@ -95,7 +98,7 @@ def run_test(webdriver_binary):
time.sleep(0.05)
time.sleep(CRASH_REPORT_GRACE_SECONDS)
- reports = list((Path(temporary) / "data/Ladybird/CrashReports").glob("*-WebWorker-*.txt"))
+ reports = list((profile / "data/CrashReports").glob("*-WebWorker-*.txt"))
assert not reports, reports[0].read_text()
webdriver_helpers.request(port, "DELETE", f"/session/{session}")
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index 646b3f8be1d..ecd49508471 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -174,6 +174,7 @@ private:
virtual void show_download_in_folder(WebView::FileDownloader::Download const&) const override;
virtual void crash_reports_changed() override;
+ virtual bool captures_browser_crashes() const override { return true; }
virtual bool supports_clipboard_type(ClipboardType) const override;
virtual Utf16String clipboard_text(ClipboardType) const override;
diff --git a/UI/Qt/main.cpp b/UI/Qt/main.cpp
index 79111cb68e2..ef758ebb605 100644
--- a/UI/Qt/main.cpp
+++ b/UI/Qt/main.cpp
@@ -9,7 +9,6 @@
#include <LibMain/Main.h>
#include <LibWebView/Application.h>
#include <LibWebView/BrowserProcess.h>
-#include <LibWebView/CrashReportStore.h>
#include <LibWebView/URL.h>
#include <LibWebView/Utilities.h>
#include <UI/Qt/Application.h>
@@ -40,9 +39,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
- if (auto result = WebView::CrashReportStore::the().initialize_browser_crash_handler(); result.is_error())
- warnln("Could not prepare Browser crash reporting: {}", result.error());
-
// The web content view is presented in a native child window. Without this attribute, a widget
// gaining a native window would force every other widget in the browser window to become native
// as well.