Skip to content

Truly independent web browser

LibWebView+UI/Qt: Let a crash report be canceled while it is sent

Jelle Raaijmakers3beea61ca1de
diff --git a/Documentation/CrashReports.md b/Documentation/CrashReports.md
index 93f89bdda16..47ebafe6c55 100644
--- a/Documentation/CrashReports.md
+++ b/Documentation/CrashReports.md
@@ -47,8 +47,9 @@ in place of the review. Submissions omit the website URL by default; when
the crashed page had one, the user can explicitly include it and edit it first.
Ladybird does not collect contact information. Network errors, timeouts, rate
limits and server errors are retried a few times, honoring the server's
-`Retry-After`; a report the server rejects is not. A report that changed on disk
-after it was reviewed is not sent. A successful submission removes the local
+`Retry-After`; a report the server rejects is not. Until a report is sent,
+**Cancel** stops sending it and returns to the review. A report that changed on
+disk after it was reviewed is not sent. A successful submission removes the local
copy. A report that is declined or could not be sent stays on the device, but is
not offered again.
diff --git a/Libraries/LibWebView/CrashReportReview.cpp b/Libraries/LibWebView/CrashReportReview.cpp
index ed57eb92c9b..352eefb8b58 100644
--- a/Libraries/LibWebView/CrashReportReview.cpp
+++ b/Libraries/LibWebView/CrashReportReview.cpp
@@ -40,6 +40,8 @@ ErrorOr<CrashReportReview::Report> CrashReportReview::open(Optional<ByteString>
m_report_name = {};
m_text_digest = {};
m_prepared_manifest = {};
+ m_prepared_description = {};
+ m_prepared_url = {};
auto report_name = name.value_or({});
if (report_name.is_empty()) {
@@ -141,6 +143,14 @@ Optional<String> CrashReportReview::send(String const& description, Optional<Str
if (auto error = validate(description, url_view); error.has_value())
return error;
+ // Sending the same choices again, after a failed or canceled attempt, sends the same submission, which the server
+ // recognizes if the earlier attempt reached it. Other choices make a new submission, carrying only those.
+ if (description != m_prepared_description || url != m_prepared_url) {
+ m_prepared_manifest = {};
+ m_prepared_description = description;
+ m_prepared_url = url;
+ }
+
auto submission = CrashReportSubmission::create(m_store,
{
.report_name = m_report_name,
@@ -182,4 +192,12 @@ Optional<String> CrashReportReview::send(String const& description, Optional<Str
return {};
}
+// The report stays on the device, and sending it again sends the same submission, which the server recognizes if the
+// canceled one reached it after all.
+void CrashReportReview::cancel_sending()
+{
+ if (auto submission = move(m_submission))
+ submission->cancel();
+}
+
}
diff --git a/Libraries/LibWebView/CrashReportReview.h b/Libraries/LibWebView/CrashReportReview.h
index e4ca6163a7f..33f197b4de4 100644
--- a/Libraries/LibWebView/CrashReportReview.h
+++ b/Libraries/LibWebView/CrashReportReview.h
@@ -54,6 +54,7 @@ public:
// the review is destroyed, which then only stops hearing about it.
Optional<String> send(String const& description, Optional<String> const& url);
bool is_sending() const { return m_submission; }
+ void cancel_sending();
Function<void(ReportSubmission::Stage)> on_progress;
Function<void(String reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds)> on_retry;
@@ -65,6 +66,8 @@ private:
ByteString m_report_name;
ByteString m_text_digest;
ByteString m_prepared_manifest;
+ String m_prepared_description;
+ Optional<String> m_prepared_url;
RefPtr<CrashReportSubmission> m_submission;
};
diff --git a/Libraries/LibWebView/ReportSubmission.cpp b/Libraries/LibWebView/ReportSubmission.cpp
index a3d66684571..65fad611094 100644
--- a/Libraries/LibWebView/ReportSubmission.cpp
+++ b/Libraries/LibWebView/ReportSubmission.cpp
@@ -183,6 +183,33 @@ void ReportSubmission::start()
begin_attempt();
}
+// A canceled submission stops wherever it is and reports nothing more. A report that reached the server before then may
+// still be stored there.
+void ReportSubmission::cancel()
+{
+ // Letting go of the submission can release the last reference to it.
+ NonnullRefPtr protector = *this;
+
+ on_progress = nullptr;
+ on_prepared = nullptr;
+ on_retry = nullptr;
+ on_sent = nullptr;
+ on_failed = nullptr;
+
+ m_proof_cancelled.store(true);
+ if (m_retry_timer) {
+ m_retry_timer->stop();
+ m_retry_timer = nullptr;
+ }
+ if (m_request_timeout) {
+ m_request_timeout->stop();
+ m_request_timeout = nullptr;
+ }
+ if (auto request = move(m_request))
+ request->stop();
+ m_self_while_in_flight = nullptr;
+}
+
void ReportSubmission::begin_attempt()
{
if (on_progress)
@@ -265,7 +292,7 @@ void ReportSubmission::solve_proof(ByteString token, u64 expected_work)
main_thread_event_loop.deferred_invoke(
[weak_this = move(weak_this), attempt, token = move(token), nonce]() mutable {
auto self = weak_this.strong_ref();
- if (!self || self->m_attempt != attempt)
+ if (!self || self->m_attempt != attempt || self->m_proof_cancelled.load())
return;
if (!nonce.has_value()) {
self->fail("Could not compute the report's proof of work."_string, ShouldRetry::No);
diff --git a/Libraries/LibWebView/ReportSubmission.h b/Libraries/LibWebView/ReportSubmission.h
index 5b40cb70f4a..308d65dfac6 100644
--- a/Libraries/LibWebView/ReportSubmission.h
+++ b/Libraries/LibWebView/ReportSubmission.h
@@ -78,6 +78,7 @@ public:
virtual ~ReportSubmission();
void start();
+ void cancel();
Function<void(Stage)> on_progress;
Function<void(ByteString const& manifest)> on_prepared;
diff --git a/Tests/LibWebView/TestCrashReportSubmission.cpp b/Tests/LibWebView/TestCrashReportSubmission.cpp
index dbc572d6578..86c5ec5d2c9 100644
--- a/Tests/LibWebView/TestCrashReportSubmission.cpp
+++ b/Tests/LibWebView/TestCrashReportSubmission.cpp
@@ -324,6 +324,29 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
VERIFY(!manifest_field(manifest, "description"sv).has_value());
VERIFY(!manifest_field(manifest, "url"sv).has_value());
+ // Other choices than those of a failed or canceled attempt make a new submission, which carries only those.
+ server.requests.clear();
+ track(review, outcome);
+ name = write_report(reports_directory, "2026-10-01T13-30-00Z-WebContent-abc123.txt"sv);
+ MUST(review.open(name));
+ server.respond_with({ 400, "{}", {} });
+ VERIFY(!review.send("I clicked a link"_string, "https://example.com/"_string).has_value());
+ Core::EventLoop::current().spin_until([&] { return outcome.is_finished(); });
+ VERIFY(outcome.failure == WebView::ReportSubmission::Failure::Sending);
+
+ track(review, outcome);
+ server.respond_with(challenge("changed"sv));
+ server.respond_with({ 200, R"({"receipt":"receipt-changed"})", {} });
+ VERIFY(!review.send("I opened a menu"_string, {}).has_value());
+ Core::EventLoop::current().spin_until([&] { return outcome.is_finished(); });
+ VERIFY(outcome.sent);
+ VERIFY(server.requests.size() == 3);
+ VERIFY(server.requests[0].body != server.requests[1].body);
+ manifest = JsonValue::from_string(multipart_part(server.requests[2].body, "manifest"sv)).release_value().as_object();
+ VERIFY(manifest_field(manifest, "description"sv)->as_string() == "I opened a menu"sv);
+ VERIFY(!manifest_field(manifest, "url"sv).has_value());
+ VERIFY(manifest.get_string("submission_id"sv).value() != submission_id);
+
// A report on its way keeps going when the screen that showed its review goes away, along with the store it was
// given.
server.requests.clear();
diff --git a/Tests/UI/Qt/TestCrashReportReviewWidget.cpp b/Tests/UI/Qt/TestCrashReportReviewWidget.cpp
index d5be0ce71cb..0b4514906c4 100644
--- a/Tests/UI/Qt/TestCrashReportReviewWidget.cpp
+++ b/Tests/UI/Qt/TestCrashReportReviewWidget.cpp
@@ -123,11 +123,42 @@ TEST_CASE(the_review_offers_to_exit_before_and_after_answering)
exit_button.click();
EXPECT_EQ(exits, 2);
- // A report that may still be sent offers both.
+ // A report that may still be sent offers to try again, or to go back to the review.
widget.show_failure(ReportSubmission::Failure::Sending, "Network error while contacting the report server."_string);
EXPECT(child<QPushButton>(widget, "CrashReportRetryButton").isVisible());
- EXPECT(exit_button.isVisible());
- EXPECT(!exit_button.isDefault());
+ EXPECT(child<QPushButton>(widget, "CrashReportCancelButton").isVisible());
+ EXPECT(!exit_button.isVisible());
+}
+
+TEST_CASE(sending_can_be_canceled_until_the_report_is_sent)
+{
+ cleanup();
+ ScopeGuard guard = cleanup;
+
+ auto name = write_report("2026-10-01T12-00-00Z"sv);
+ WebView::CrashReportStore store { test_directory() };
+ CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
+ auto answers = 0;
+ widget.on_answered = [&] { ++answers; };
+ MUST(widget.open_report(name));
+ widget.show();
+
+ auto& description = child<QPlainTextEdit>(widget, "CrashReportDescription");
+ description.setPlainText("Opening a menu");
+ widget.show_progress(ReportSubmission::Stage::SolvingProof);
+ auto& cancel = child<QPushButton>(widget, "CrashReportCancelButton");
+ EXPECT(cancel.isVisible());
+ EXPECT(!cancel.isDefault());
+ EXPECT(!child<QPushButton>(widget, "CrashReportExitButton").isVisible());
+
+ cancel.click();
+ EXPECT(child<QPushButton>(widget, "CrashReportSendButton").isVisible());
+ EXPECT_EQ(description.toPlainText(), "Opening a menu");
+ EXPECT_EQ(answers, 0);
+
+ widget.show_sent();
+ EXPECT_EQ(answers, 1);
+ EXPECT(!cancel.isVisible());
}
TEST_CASE(a_choice_that_cannot_be_sent_is_explained_in_place)
diff --git a/UI/Qt/CrashReportReviewWidget.cpp b/UI/Qt/CrashReportReviewWidget.cpp
index 435252fef4e..2757d8af6b8 100644
--- a/UI/Qt/CrashReportReviewWidget.cpp
+++ b/UI/Qt/CrashReportReviewWidget.cpp
@@ -140,6 +140,8 @@ CrashReportReviewWidget::CrashReportReviewWidget(QString exit_text, QWidget* par
m_retry_button = new QPushButton(tr("Try sending again"), m_status_page);
m_retry_button->setObjectName("CrashReportRetryButton");
+ m_cancel_button = new QPushButton(tr("Cancel"), m_status_page);
+ m_cancel_button->setObjectName("CrashReportCancelButton");
m_next_button = new QPushButton(tr("Review the next crash report"), m_status_page);
m_next_button->setObjectName("CrashReportNextButton");
m_exit_button = new QPushButton(exit_text, m_status_page);
@@ -147,6 +149,7 @@ CrashReportReviewWidget::CrashReportReviewWidget(QString exit_text, QWidget* par
auto* status_buttons = new QHBoxLayout;
status_buttons->setSpacing(button_spacing);
status_buttons->addWidget(m_retry_button);
+ status_buttons->addWidget(m_cancel_button);
status_buttons->addWidget(m_next_button);
status_buttons->addWidget(m_exit_button);
status_buttons->addStretch();
@@ -165,15 +168,16 @@ CrashReportReviewWidget::CrashReportReviewWidget(QString exit_text, QWidget* par
layout->addWidget(m_report_text_page);
QObject::connect(send_button, &QPushButton::clicked, this, [this] { send(); });
- QObject::connect(decline_button, &QPushButton::clicked, this, [this] {
- show_declined();
- if (on_answered)
- on_answered();
- });
+ QObject::connect(decline_button, &QPushButton::clicked, this, [this] { show_declined(); });
QObject::connect(m_retry_button, &QPushButton::clicked, this, [this] {
show_progress(WebView::ReportSubmission::Stage::Preparing);
VERIFY(!m_review.send(m_description, m_url).has_value());
});
+ QObject::connect(m_cancel_button, &QPushButton::clicked, this, [this] {
+ m_review.cancel_sending();
+ show_page(*m_review_page);
+ m_description_edit->setFocus(Qt::OtherFocusReason);
+ });
QObject::connect(m_next_button, &QPushButton::clicked, this, [this] {
if (auto result = open_report(); result.is_error()) {
warnln("Could not open the next crash report: {}", result.error());
@@ -264,8 +268,6 @@ void CrashReportReviewWidget::send()
m_validation->hide();
show_progress(WebView::ReportSubmission::Stage::Preparing);
- if (on_answered)
- on_answered();
VERIFY(!m_review.send(m_description, m_url).has_value());
}
@@ -298,20 +300,16 @@ void CrashReportReviewWidget::show_status(QString const& title, QString const& m
{
m_status_title->setText(title);
m_status_message->setText(message);
- for (auto* button : { m_retry_button, m_next_button, m_exit_button }) {
- button->setVisible(actions.contains_slow(button));
- button->setDefault(button == primary);
- }
show_page(*m_status_page);
- if (primary)
- primary->setFocus(Qt::OtherFocusReason);
+ QPushButton* all_actions[] = { m_retry_button, m_cancel_button, m_next_button, m_exit_button };
+ show_actions(all_actions, actions, primary);
}
void CrashReportReviewWidget::show_progress(WebView::ReportSubmission::Stage stage)
{
- // Leaving stays possible while the report is on its way, which carries on behind the screen that follows.
+ // Until the report is sent, the user can stop sending it and return to the review.
auto is_sending = stage == WebView::ReportSubmission::Stage::Sending;
- show_status(tr("Sending crash report"), is_sending ? tr("Sending crash report…") : tr("Preparing report…"), { m_exit_button });
+ show_status(tr("Sending crash report"), is_sending ? tr("Sending crash report…") : tr("Preparing report…"), { m_cancel_button });
m_progress->setValue(progress_percent(stage));
m_progress->show();
}
@@ -339,7 +337,7 @@ void CrashReportReviewWidget::show_failure(WebView::ReportSubmission::Failure fa
// Only sending can succeed on another attempt. A report that could not be prepared would fail the same way again.
if (failure == WebView::ReportSubmission::Failure::Sending) {
- show_status(tr("Couldn’t send report"), qstring_from_ak_string(reason), { m_retry_button, m_exit_button },
+ show_status(tr("Couldn’t send report"), qstring_from_ak_string(reason), { m_retry_button, m_cancel_button },
m_retry_button);
return;
}
@@ -349,6 +347,9 @@ void CrashReportReviewWidget::show_failure(WebView::ReportSubmission::Failure fa
// Once the report is answered, the review moves on to the next one waiting, or is left.
void CrashReportReviewWidget::show_outcome(QString const& title, QString const& message)
{
+ if (on_answered)
+ on_answered();
+
if (m_review.has_next_report())
show_status(title, message, { m_next_button, m_exit_button }, m_next_button);
else
diff --git a/UI/Qt/CrashReportReviewWidget.h b/UI/Qt/CrashReportReviewWidget.h
index 1440a09f4f0..90eaa75283c 100644
--- a/UI/Qt/CrashReportReviewWidget.h
+++ b/UI/Qt/CrashReportReviewWidget.h
@@ -45,7 +45,7 @@ public:
void show_failure(WebView::ReportSubmission::Failure, String const& reason);
Function<void()> on_exit;
- // The user chose to send the report or not, so the screen around the review no longer needs to ask.
+ // The report was sent, declined or cannot be sent, so the screen around the review no longer needs to ask.
Function<void()> on_answered;
Function<void(bool)> on_report_text_visibility_change;
@@ -86,6 +86,7 @@ private:
QProgressBar* m_progress { nullptr };
QLabel* m_status_message { nullptr };
QPushButton* m_retry_button { nullptr };
+ QPushButton* m_cancel_button { nullptr };
QPushButton* m_next_button { nullptr };
QPushButton* m_exit_button { nullptr };
diff --git a/UI/Qt/ReportReview.cpp b/UI/Qt/ReportReview.cpp
index 237755f6c59..148c9171cfa 100644
--- a/UI/Qt/ReportReview.cpp
+++ b/UI/Qt/ReportReview.cpp
@@ -206,6 +206,16 @@ int progress_percent(WebView::ReportSubmission::Stage stage)
VERIFY_NOT_REACHED();
}
+void show_actions(ReadonlySpan<QPushButton*> all_actions, ReadonlySpan<QPushButton*> actions, QPushButton* primary)
+{
+ for (auto* button : all_actions) {
+ button->setVisible(actions.contains_slow(button));
+ button->setDefault(button == primary);
+ }
+ if (auto* focused = primary ? primary : (actions.is_empty() ? nullptr : actions.first()))
+ focused->setFocus(Qt::OtherFocusReason);
+}
+
QString retry_message(String const& reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds)
{
return QObject::tr("%1 Trying again in %2 seconds (%3 of %4)…")
diff --git a/UI/Qt/ReportReview.h b/UI/Qt/ReportReview.h
index 8a626658298..fb72d5c879f 100644
--- a/UI/Qt/ReportReview.h
+++ b/UI/Qt/ReportReview.h
@@ -50,6 +50,7 @@ void apply_style(QWidget& review, ReadonlySpan<QWidget*> fields);
bool focus_next_control(QWidget& review, bool next);
int progress_percent(WebView::ReportSubmission::Stage);
+void show_actions(ReadonlySpan<QPushButton*> all_actions, ReadonlySpan<QPushButton*> actions, QPushButton* primary);
QString retry_message(String const& reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds);
}