Skip to content

Truly independent web browser

LibWebView: Split report uploads out of CrashReportSubmission

Jelle Raaijmakersa597b18a5884
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index f99e99b0a94..121d45b3d5d 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -52,6 +52,7 @@ set(SOURCES
TabPerformanceMonitor.cpp
ProcessMonitor.cpp
Profile.cpp
+ ReportSubmission.cpp
RequestServerManager.cpp
RequestServerSiteBindings.cpp
SearchEngine.cpp
diff --git a/Libraries/LibWebView/CrashReportReview.cpp b/Libraries/LibWebView/CrashReportReview.cpp
index 54d71b26ff3..4465f53bfd6 100644
--- a/Libraries/LibWebView/CrashReportReview.cpp
+++ b/Libraries/LibWebView/CrashReportReview.cpp
@@ -158,9 +158,9 @@ Optional<String> CrashReportReview::send(String const& description, Optional<Str
if (auto* self = weak_this.ptr())
self->m_prepared_manifest = manifest;
};
- submission->on_retry = [weak_this = make_weak_ptr()](String reason, u32 retry_number, u32 delay_seconds) {
+ submission->on_retry = [weak_this = make_weak_ptr()](String reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds) {
if (auto* self = weak_this.ptr(); self && self->on_retry)
- self->on_retry(move(reason), retry_number, CrashReportSubmission::maximum_attempts - 1, delay_seconds);
+ self->on_retry(move(reason), retry_number, maximum_retries, delay_seconds);
};
submission->on_sent = [weak_this = make_weak_ptr()] {
if (auto* self = weak_this.ptr()) {
diff --git a/Libraries/LibWebView/CrashReportReview.h b/Libraries/LibWebView/CrashReportReview.h
index 37a47593ff8..451fc09172a 100644
--- a/Libraries/LibWebView/CrashReportReview.h
+++ b/Libraries/LibWebView/CrashReportReview.h
@@ -17,6 +17,8 @@
#include <LibWebView/CrashReportStore.h>
#include <LibWebView/CrashReportSubmission.h>
#include <LibWebView/Forward.h>
+#include <LibWebView/ReportDetail.h>
+#include <LibWebView/ReportSubmission.h>
namespace WebView {
@@ -28,17 +30,10 @@ public:
static constexpr size_t maximum_description_bytes = 16384;
static constexpr size_t maximum_url_bytes = 4096;
- struct Field {
- String label;
- String value;
- // Values such as a commit hash or a source location, which read best in a fixed-width font.
- bool is_code { false };
- };
-
struct Report {
ByteString name;
// The fields that tell crashes apart. The rest is in the report itself, which show_report() opens.
- Vector<Field> fields;
+ Vector<ReportDetail> fields;
};
explicit CrashReportReview(CrashReportStore& = CrashReportStore::the());
@@ -60,10 +55,10 @@ public:
Optional<String> send(String const& description, Optional<String> const& url);
bool is_sending() const { return m_submission; }
- Function<void(CrashReportSubmission::Stage)> on_progress;
+ Function<void(ReportSubmission::Stage)> on_progress;
Function<void(String reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds)> on_retry;
Function<void()> on_sent;
- Function<void(CrashReportSubmission::Failure, String reason)> on_failed;
+ Function<void(ReportSubmission::Failure, String reason)> on_failed;
private:
CrashReportStore& m_store;
diff --git a/Libraries/LibWebView/CrashReportSubmission.cpp b/Libraries/LibWebView/CrashReportSubmission.cpp
index 7cb2cc3ee19..9928b613251 100644
--- a/Libraries/LibWebView/CrashReportSubmission.cpp
+++ b/Libraries/LibWebView/CrashReportSubmission.cpp
@@ -4,179 +4,56 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/Array.h>
#include <AK/Hex.h>
-#include <AK/JsonArray.h>
-#include <AK/JsonObject.h>
-#include <AK/JsonValue.h>
-#include <AK/NumericLimits.h>
-#include <AK/Random.h>
-#include <AK/StdLibExtras.h>
-#include <AK/StringBuilder.h>
-#include <LibCore/Environment.h>
-#include <LibCore/EventLoop.h>
-#include <LibCore/Platform/ThreadQoS.h>
-#include <LibCore/Timer.h>
+#include <LibCore/ImmutableBytes.h>
#include <LibCrypto/Hash/SHA2.h>
-#include <LibHTTP/HeaderList.h>
-#include <LibRequests/Request.h>
-#include <LibRequests/RequestClient.h>
-#include <LibThreading/Thread.h>
-#include <LibURL/Parser.h>
-#include <LibWebView/Application.h>
#include <LibWebView/CrashReportDiagnostics.h>
#include <LibWebView/CrashReportStore.h>
#include <LibWebView/CrashReportSubmission.h>
namespace WebView {
-static constexpr auto default_endpoint = "https://reports.app.ladybird.org"sv;
-static constexpr auto challenge_path = "/api/v1/challenges"sv;
-static constexpr auto report_path = "/api/v1/reports"sv;
-static constexpr auto challenge_algorithm = "sha256-seed-nonce-le-v1"sv;
static constexpr auto diagnostics_reference = "diagnostics"sv;
static constexpr auto diagnostics_name = "crash-diagnostics.txt"sv;
-static constexpr size_t maximum_challenge_token_length = 4096;
-static constexpr u64 maximum_expected_work = 100'000'000;
-static constexpr int request_timeout_ms = 30'000;
-static constexpr int retry_delay_ms = 3'000;
-static constexpr int maximum_retry_delay_ms = 60'000;
-
-static String to_string(StringView text)
-{
- return String::from_utf8_with_replacement_character(text);
-}
-
-static Optional<JsonObject> parse_json_object(StringView text)
-{
- auto parsed = JsonValue::from_string(text);
- if (parsed.is_error() || !parsed.value().is_object())
- return {};
- return parsed.value().as_object();
-}
static ByteString sha256_hex(ReadonlyBytes bytes)
{
return encode_hex(Crypto::Hash::SHA256::hash(bytes).bytes());
}
-static ByteString build_manifest(CrashReportSubmission::Options const& options, StringView text)
+static ReportSubmission::Contents describe_report(CrashReportSubmission::Options const& options, StringView text)
{
auto diagnostics = CrashReportDiagnostics::parse(text);
- JsonArray fields;
- auto add = [&fields](StringView key, StringView type, JsonValue value) {
- JsonObject typed;
- typed.set("type"sv, to_string(type));
- typed.set("value"sv, move(value));
- JsonObject field;
- field.set("key"sv, to_string(key));
- field.set("value"sv, move(typed));
- fields.must_append(move(field));
- };
- auto add_text = [&add](StringView key, StringView type, StringView value) {
- if (!value.is_empty())
- add(key, type, to_string(value));
- };
+ ReportSubmission::Contents contents;
+ contents.kind = "crash"sv;
- add_text("stack"sv, "stack_trace"sv, diagnostics.stack);
- add_text("process"sv, "text"sv, diagnostics.header("Process"sv));
- add_text("platform"sv, "text"sv, diagnostics.header("Platform"sv));
- add_text("architecture"sv, "text"sv, diagnostics.header("Architecture"sv));
- add_text("git_commit"sv, "text"sv, diagnostics.header("Git commit"sv));
- add_text("build_configuration"sv, "text"sv, diagnostics.header("Build configuration"sv));
- add_text("cpp_compiler"sv, "text"sv, diagnostics.header("C++ compiler"sv));
+ contents.add_text("stack"sv, "stack_trace"sv, diagnostics.stack);
+ contents.add_text("process"sv, "text"sv, diagnostics.header("Process"sv));
+ contents.add_text("platform"sv, "text"sv, diagnostics.header("Platform"sv));
+ contents.add_text("architecture"sv, "text"sv, diagnostics.header("Architecture"sv));
+ contents.add_text("git_commit"sv, "text"sv, diagnostics.header("Git commit"sv));
+ contents.add_text("build_configuration"sv, "text"sv, diagnostics.header("Build configuration"sv));
+ contents.add_text("cpp_compiler"sv, "text"sv, diagnostics.header("C++ compiler"sv));
for (auto label : { "Verification failed"sv, "Assertion failed"sv, "Rust panic"sv }) {
auto value = diagnostics.header(label);
if (value.is_empty())
continue;
- add_text("failure_reason"sv, "text"sv, ByteString::formatted("{}: {}", label, value));
+ auto failure_reason = ByteString::formatted("{}: {}", label, value);
+ contents.add_text("failure_reason"sv, "text"sv, failure_reason);
break;
}
- add_text("signal"sv, "text"sv, diagnostics.signal.name);
+ contents.add_text("signal"sv, "text"sv, diagnostics.signal.name);
if (diagnostics.signal.number.has_value())
- add("signal_number"sv, "number"sv, *diagnostics.signal.number);
- add_text("description"sv, "multiline"sv, options.description);
- add_text("url"sv, "text"sv, options.url);
-
- JsonObject attachment;
- attachment.set("id"sv, to_string(diagnostics_reference));
- attachment.set("name"sv, to_string(diagnostics_name));
- attachment.set("media_type"sv, "text/plain"_string);
- attachment.set("size"sv, text.length());
- attachment.set("sha256"sv, to_string(sha256_hex(text.bytes())));
- JsonArray attachments;
- attachments.must_append(move(attachment));
+ contents.add("signal_number"sv, "number"sv, *diagnostics.signal.number);
+ contents.add_text("description"sv, "multiline"sv, options.description);
+ contents.add_text("url"sv, "text"sv, options.url);
auto version = diagnostics.header("Version"sv);
auto build = diagnostics.header("Build configuration"sv);
-
- JsonObject manifest;
- manifest.set("protocol"sv, 1);
- manifest.set("submission_id"sv, generate_random_uuid());
- manifest.set("kind"sv, "crash"_string);
- manifest.set("client_version"sv, version.is_empty() ? "Unknown Ladybird version"_string : to_string(version));
- manifest.set("build"sv, build.is_empty() ? "Unknown build"_string : to_string(build));
- manifest.set("fields"sv, move(fields));
- manifest.set("attachments"sv, move(attachments));
- return manifest.serialized().to_byte_string();
-}
-
-static ByteString build_multipart_body(StringView manifest, StringView report_text, StringView boundary)
-{
- StringBuilder builder;
- auto append_part = [&](StringView disposition, StringView content_type, StringView content) {
- builder.appendff("--{}\r\nContent-Disposition: form-data; {}\r\nContent-Type: {}\r\n\r\n{}\r\n",
- boundary, disposition, content_type, content);
- };
-
- append_part("name=\"manifest\""sv, "application/json"sv, manifest);
- append_part(ByteString::formatted("name=\"{}\"; filename=\"{}\"", diagnostics_reference, diagnostics_name),
- "text/plain; charset=utf-8"sv, report_text);
-
- builder.appendff("--{}--\r\n", boundary);
- return builder.to_byte_string();
-}
-
-static Optional<u64> solve_challenge(ByteString const& token, u64 expected_work, Atomic<bool> const& cancelled)
-{
- auto seed = Crypto::Hash::SHA256::hash(token.bytes());
- Array<u8, 40> candidate {};
- seed.bytes().copy_to(candidate.span().slice(0, 32));
- auto threshold = NumericLimits<u64>::max() / expected_work;
-
- for (u64 nonce = 0; nonce < NumericLimits<u64>::max(); ++nonce) {
- if (nonce % 4096 == 0 && cancelled.load())
- return {};
- for (size_t i = 0; i < 8; ++i)
- candidate[32 + i] = static_cast<u8>(nonce >> (i * 8));
- auto digest = Crypto::Hash::SHA256::hash(candidate.span());
- u64 prefix = 0;
- for (size_t i = 0; i < 8; ++i)
- prefix = prefix << 8 | digest.bytes()[i];
- if (prefix <= threshold)
- return nonce;
- }
- return {};
-}
-
-// The server would reject anything else again, so only these are worth another attempt. A conflict means the challenge
-// expired or was already used, which the fresh challenge of the next attempt resolves.
-static bool is_transient_status(u32 status)
-{
- return status == 408 || status == 409 || status == 429 || (status >= 500 && status < 600);
-}
-
-// Only a delay in seconds is honored. A date falls back to the regular delay between attempts.
-static Optional<int> retry_after_ms(HTTP::HeaderList const& headers)
-{
- auto value = headers.get("Retry-After"sv);
- if (!value.has_value())
- return {};
- auto seconds = value->view().trim_whitespace().to_number<u32>();
- if (!seconds.has_value())
- return {};
- return static_cast<int>(min<u64>(*seconds * 1000ull, maximum_retry_delay_ms));
+ contents.client_version = version.is_empty() ? ByteString { "Unknown Ladybird version"sv } : move(version);
+ contents.build = build.is_empty() ? ByteString { "Unknown build"sv } : move(build);
+ return contents;
}
NonnullRefPtr<CrashReportSubmission> CrashReportSubmission::create(CrashReportStore const& store, Options options)
@@ -184,275 +61,45 @@ NonnullRefPtr<CrashReportSubmission> CrashReportSubmission::create(CrashReportSt
return adopt_ref(*new CrashReportSubmission(store, move(options)));
}
-// A submission builds its manifest, solves the proof-of-work challenge off the main thread, uploads
-// the report, and removes the local copy after the server acknowledges it. It can outlive the review that started it,
-// and so the store that review was given, which is why it keeps a store of its own.
+// A crash report submission removes the local copy after the server acknowledges it. It can outlive the review that
+// started it, and so the store that review was given, which is why it keeps a store of its own.
CrashReportSubmission::CrashReportSubmission(CrashReportStore const& store, Options options)
: m_store(store)
, m_options(move(options))
{
}
-CrashReportSubmission::~CrashReportSubmission()
-{
- m_proof_cancelled.store(true);
- if (m_proof_thread && m_proof_thread->needs_to_be_joined())
- (void)m_proof_thread->join();
-}
-
-// A submission keeps itself alive until it is sent or fails for good, so it outlives the review that started it.
-void CrashReportSubmission::start()
-{
- m_self_while_in_flight = *this;
- begin_attempt();
-}
-
-void CrashReportSubmission::begin_attempt()
-{
- if (on_progress)
- on_progress(Stage::Preparing);
-
- if (m_manifest.is_empty()) {
- auto report = m_store.saved_report(m_options.report_name);
- if (report.is_error()) {
- warnln("Could not read the crash report for sending: {}", report.error());
- fail_preparation("Could not prepare this report."_string);
- return;
- }
- if (sha256_hex(report.value().text.bytes()) != m_options.reviewed_text_digest) {
- warnln("Crash report {} changed after it was reviewed", m_options.report_name);
- fail_preparation("This report changed after it was reviewed, so it was not sent."_string);
- return;
- }
- m_report_text = move(report.value().text);
- m_manifest = m_options.manifest.is_empty() ? build_manifest(m_options, m_report_text) : move(m_options.manifest);
- }
-
- if (on_prepared)
- on_prepared(m_manifest);
- request_challenge();
-}
-
-void CrashReportSubmission::fail_preparation(String reason)
-{
- // Reporting the failure can release the last reference to this submission.
- NonnullRefPtr protector = *this;
-
- m_self_while_in_flight = nullptr;
- if (on_failed)
- on_failed(Failure::Preparation, move(reason));
-}
-
-void CrashReportSubmission::request_challenge()
-{
- if (on_progress)
- on_progress(Stage::RequestingChallenge);
-
- JsonObject body;
- body.set("manifest_digest"sv, to_string(sha256_hex(m_manifest.bytes())));
-
- post(challenge_path, "requesting a challenge"sv, "application/json"sv, body.serialized().to_byte_string(), {},
- [this](ByteString response) {
- auto challenge = parse_json_object(response);
- if (!challenge.has_value()) {
- fail("The report server sent an unreadable challenge."_string, ShouldRetry::No);
- return;
- }
-
- auto algorithm = challenge->get_string("algorithm"sv);
- if (!algorithm.has_value() || *algorithm != challenge_algorithm) {
- fail("Unsupported report challenge."_string, ShouldRetry::No);
- return;
- }
-
- auto token = challenge->get_string("token"sv);
- auto expected_work = challenge->get_integer<u64>("expected_work"sv);
- if (!token.has_value() || token->bytes().size() > maximum_challenge_token_length
- || !expected_work.has_value() || *expected_work == 0 || *expected_work > maximum_expected_work) {
- fail("The report server supplied an invalid challenge."_string, ShouldRetry::No);
- return;
- }
-
- solve_proof(token->to_byte_string(), *expected_work);
- });
-}
-
-void CrashReportSubmission::solve_proof(ByteString token, u64 expected_work)
-{
- if (on_progress)
- on_progress(Stage::SolvingProof);
-
- // A previous attempt's search has already delivered its result.
- if (m_proof_thread && m_proof_thread->needs_to_be_joined())
- (void)m_proof_thread->join();
-
- // The server chooses the difficulty, so the search runs on its own thread, at the priority of work that the user
- // follows through a progress indicator.
- auto& main_thread_event_loop = Core::EventLoop::current();
- m_proof_thread = Threading::Thread::construct("Crash report proof"sv,
- [weak_this = make_weak_ptr<CrashReportSubmission>(), &cancelled = m_proof_cancelled, &main_thread_event_loop,
- attempt = m_attempt, token = move(token), expected_work]() mutable -> intptr_t {
- auto nonce = solve_challenge(token, expected_work, cancelled);
- 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)
- return;
- if (!nonce.has_value()) {
- self->fail("Could not compute the report's proof of work."_string, ShouldRetry::No);
- return;
- }
- self->send_report(ProofOfWork { move(token), *nonce });
- });
- return 0;
- });
- m_proof_thread->set_qos(Core::Platform::ThreadQoS::Utility);
- m_proof_thread->start();
-}
-
-void CrashReportSubmission::send_report(ProofOfWork proof)
-{
- if (on_progress)
- on_progress(Stage::Sending);
-
- auto boundary = ByteString::formatted("ladybird-{}", generate_random_uuid());
- auto body = build_multipart_body(m_manifest, m_report_text, boundary);
- auto content_type = ByteString::formatted("multipart/form-data; boundary={}", boundary);
-
- post(report_path, "sending the report"sv, content_type, move(body), move(proof), [this](ByteString response) {
- auto body = parse_json_object(response);
- auto receipt = body.has_value() ? body->get_string("receipt"sv) : Optional<String const&> {};
- if (!receipt.has_value() || receipt->is_empty()) {
- fail("Report server did not provide a receipt."_string, ShouldRetry::No);
- return;
- }
- finish();
- });
-}
+CrashReportSubmission::~CrashReportSubmission() = default;
-// A response outside the 2xx range fails this attempt, and the activity names what was going on in that message.
-void CrashReportSubmission::post(StringView path, StringView activity, StringView content_type, ByteString body,
- Optional<ProofOfWork> proof, Function<void(ByteString)> on_success)
+ErrorOr<ReportSubmission::Payload, String> CrashReportSubmission::prepare()
{
- auto endpoint = Core::Environment::get("LADYBIRD_REPORTS_ENDPOINT"sv).value_or(default_endpoint);
- auto url = URL::Parser::basic_parse(MUST(String::formatted("{}{}{}", endpoint,
- endpoint.ends_with('/') ? ""sv : "/"sv, path.substring_view(1))));
- if (!url.has_value() || (url->scheme() != "https"sv && url->scheme() != "http"sv)) {
- fail("Invalid report server configuration."_string, ShouldRetry::No);
- return;
- }
-
- auto headers = HTTP::HeaderList::create();
- headers->set({ "Content-Type"sv, ByteString { content_type } });
- if (proof.has_value()) {
- headers->set({ "X-Ladybird-Challenge"sv, proof->token });
- headers->set({ "X-Ladybird-Nonce"sv, ByteString::number(proof->nonce) });
- }
-
- auto request_client = Application::request_server_client(IsPrivate::No);
- if (request_client.is_error()) {
- fail("Could not start the report request."_string, ShouldRetry::Yes);
- return;
+ auto report = m_store.saved_report(m_options.report_name);
+ if (report.is_error()) {
+ warnln("Could not read the crash report for sending: {}", report.error());
+ return "Could not prepare this report."_string;
}
- m_request = request_client.value()->start_request("POST"sv, *url, *headers, body.bytes(),
- HTTP::CacheMode::NoStore, HTTP::Cookie::IncludeCredentials::No);
- if (!m_request) {
- fail("Could not connect to the report server."_string, ShouldRetry::Yes);
- return;
+ if (sha256_hex(report.value().text.bytes()) != m_options.reviewed_text_digest) {
+ warnln("Crash report {} changed after it was reviewed", m_options.report_name);
+ return "This report changed after it was reviewed, so it was not sent."_string;
}
- m_request_timeout = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(request_timeout_ms),
- [weak_this = make_weak_ptr<CrashReportSubmission>()] {
- auto self = weak_this.strong_ref();
- if (!self)
- return;
- auto request = move(self->m_request);
- self->m_request_timeout = nullptr;
- if (request)
- request->stop();
- self->fail("The report server did not respond in time."_string, ShouldRetry::Yes);
- });
- m_request_timeout->start();
-
- m_request->set_buffered_request_finished_callback(
- [weak_this = make_weak_ptr<CrashReportSubmission>(), activity = ByteString { activity },
- on_success = move(on_success)](u64, Requests::RequestTimingInfo const&,
- Optional<Requests::NetworkError> const& network_error, HTTP::HeaderList const& response_headers,
- Optional<u32> response_code, Optional<String> const&,
- Optional<Core::ImmutableBytes>, Optional<u64>, Requests::CacheState,
- Core::ImmutableBytes payload) {
- auto self = weak_this.strong_ref();
- if (!self || !self->m_request)
- return;
- if (self->m_request_timeout)
- self->m_request_timeout->stop();
- self->m_request_timeout = nullptr;
-
- // A request cannot be destroyed from inside its own callback.
- Core::deferred_invoke([weak_this] {
- if (auto self = weak_this.strong_ref())
- self->m_request = nullptr;
- });
+ auto const& text = report.value().text;
+ Vector<Attachment> attachments;
+ attachments.append({
+ .id = diagnostics_reference,
+ .name = diagnostics_name,
+ .media_type = "text/plain"sv,
+ .content = MUST(Core::ImmutableBytes::copy(text.bytes())),
+ });
- if (network_error.has_value() || !response_code.has_value()) {
- self->fail("Network error while contacting the report server."_string, ShouldRetry::Yes);
- return;
- }
- if (*response_code < 200 || *response_code >= 300) {
- self->fail(MUST(String::formatted("Report server returned {} while {}.", *response_code, activity)),
- is_transient_status(*response_code) ? ShouldRetry::Yes : ShouldRetry::No,
- retry_after_ms(response_headers));
- return;
- }
- on_success(ByteString::copy(payload.bytes()));
- });
+ auto manifest = m_options.manifest.is_empty() ? build_manifest(describe_report(m_options, text), attachments) : move(m_options.manifest);
+ return Payload { move(manifest), move(attachments) };
}
-void CrashReportSubmission::finish()
+void CrashReportSubmission::did_send()
{
- // Reporting success can release the last reference to this submission.
- NonnullRefPtr protector = *this;
-
- if (m_request_timeout) {
- m_request_timeout->stop();
- m_request_timeout = nullptr;
- }
if (auto result = m_store.remove_sent_report(m_options.report_name); result.is_error())
warnln("Could not remove the sent crash report: {}", result.error());
- m_self_while_in_flight = nullptr;
- if (on_sent)
- on_sent();
-}
-
-void CrashReportSubmission::fail(String reason, ShouldRetry should_retry, Optional<int> retry_after_ms)
-{
- // Reporting the final failure can release the last reference to this submission.
- NonnullRefPtr protector = *this;
-
- if (m_request_timeout) {
- m_request_timeout->stop();
- m_request_timeout = nullptr;
- }
-
- if (should_retry == ShouldRetry::No || m_attempt >= maximum_attempts) {
- m_self_while_in_flight = nullptr;
- if (on_failed)
- on_failed(Failure::Sending, move(reason));
- return;
- }
-
- // A server that asks for more time gets it, but it cannot keep the user waiting for long.
- auto delay_ms = clamp(retry_after_ms.value_or(retry_delay_ms), retry_delay_ms, maximum_retry_delay_ms);
- auto retry_number = m_attempt++;
- if (on_retry)
- on_retry(move(reason), retry_number, static_cast<u32>(delay_ms / 1000));
-
- m_retry_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(delay_ms),
- [weak_this = make_weak_ptr<CrashReportSubmission>()] {
- if (auto self = weak_this.strong_ref())
- self->begin_attempt();
- });
- m_retry_timer->start();
}
}
diff --git a/Libraries/LibWebView/CrashReportSubmission.h b/Libraries/LibWebView/CrashReportSubmission.h
index 718f38772f8..a8f69b50a94 100644
--- a/Libraries/LibWebView/CrashReportSubmission.h
+++ b/Libraries/LibWebView/CrashReportSubmission.h
@@ -6,41 +6,16 @@
#pragma once
-#include <AK/Atomic.h>
#include <AK/ByteString.h>
-#include <AK/Function.h>
-#include <AK/RefCounted.h>
-#include <AK/RefPtr.h>
-#include <AK/String.h>
-#include <AK/WeakPtr.h>
-#include <LibCore/Forward.h>
-#include <LibRequests/Forward.h>
-#include <LibThreading/Forward.h>
+#include <AK/NonnullRefPtr.h>
#include <LibWebView/CrashReportStore.h>
#include <LibWebView/Forward.h>
+#include <LibWebView/ReportSubmission.h>
namespace WebView {
-class WEBVIEW_API CrashReportSubmission
- : public RefCounted<CrashReportSubmission>
- , public Weakable<CrashReportSubmission> {
+class WEBVIEW_API CrashReportSubmission final : public ReportSubmission {
public:
- AK_ALLOC_WITH_KMALLOC;
-
- enum class Stage : u8 {
- Preparing,
- RequestingChallenge,
- SolvingProof,
- Sending,
- };
-
- enum class Failure : u8 {
- Preparation,
- Sending,
- };
-
- static constexpr u32 maximum_attempts = 6;
-
struct Options {
ByteString report_name;
ByteString description;
@@ -53,54 +28,16 @@ public:
};
static NonnullRefPtr<CrashReportSubmission> create(CrashReportStore const&, Options);
- ~CrashReportSubmission();
-
- void start();
-
- Function<void(Stage)> on_progress;
- Function<void(ByteString const& manifest)> on_prepared;
- Function<void(String reason, u32 retry_number, u32 delay_seconds)> on_retry;
- Function<void()> on_sent;
- Function<void(Failure, String reason)> on_failed;
+ virtual ~CrashReportSubmission() override;
private:
- struct ProofOfWork {
- ByteString token;
- u64 nonce { 0 };
- };
-
- enum class ShouldRetry {
- No,
- Yes,
- };
-
CrashReportSubmission(CrashReportStore const&, Options);
- void begin_attempt();
- void fail_preparation(String reason);
- void request_challenge();
- void solve_proof(ByteString token, u64 expected_work);
- void send_report(ProofOfWork);
- void finish();
- void fail(String reason, ShouldRetry, Optional<int> retry_after_ms = {});
-
- void post(StringView path, StringView activity, StringView content_type, ByteString body, Optional<ProofOfWork>,
- Function<void(ByteString body)> on_success);
+ virtual ErrorOr<Payload, String> prepare() override;
+ virtual void did_send() override;
CrashReportStore m_store;
Options m_options;
- ByteString m_report_text;
- ByteString m_manifest;
- u32 m_attempt { 1 };
-
- RefPtr<Requests::Request> m_request;
- RefPtr<Core::Timer> m_request_timeout;
- RefPtr<Core::Timer> m_retry_timer;
-
- RefPtr<CrashReportSubmission> m_self_while_in_flight;
-
- RefPtr<Threading::Thread> m_proof_thread;
- Atomic<bool> m_proof_cancelled { false };
};
}
diff --git a/Libraries/LibWebView/ReportDetail.h b/Libraries/LibWebView/ReportDetail.h
new file mode 100644
index 00000000000..4390f648eb0
--- /dev/null
+++ b/Libraries/LibWebView/ReportDetail.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/String.h>
+
+namespace WebView {
+
+struct ReportDetail {
+ String label;
+ String value;
+ // Values such as a commit hash or a source location, which read best in a fixed-width font.
+ bool is_code { false };
+};
+
+}
diff --git a/Libraries/LibWebView/ReportSubmission.cpp b/Libraries/LibWebView/ReportSubmission.cpp
new file mode 100644
index 00000000000..a3d66684571
--- /dev/null
+++ b/Libraries/LibWebView/ReportSubmission.cpp
@@ -0,0 +1,426 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Array.h>
+#include <AK/Hex.h>
+#include <AK/JsonArray.h>
+#include <AK/JsonObject.h>
+#include <AK/NumericLimits.h>
+#include <AK/Random.h>
+#include <AK/StdLibExtras.h>
+#include <AK/StringBuilder.h>
+#include <LibCore/Environment.h>
+#include <LibCore/EventLoop.h>
+#include <LibCore/Platform/ThreadQoS.h>
+#include <LibCore/Timer.h>
+#include <LibCrypto/Hash/SHA2.h>
+#include <LibHTTP/HeaderList.h>
+#include <LibRequests/Request.h>
+#include <LibRequests/RequestClient.h>
+#include <LibThreading/Thread.h>
+#include <LibURL/Parser.h>
+#include <LibWebView/Application.h>
+#include <LibWebView/ReportSubmission.h>
+
+namespace WebView {
+
+static constexpr auto default_endpoint = "https://reports.app.ladybird.org"sv;
+static constexpr auto challenge_path = "/api/v1/challenges"sv;
+static constexpr auto report_path = "/api/v1/reports"sv;
+static constexpr auto challenge_algorithm = "sha256-seed-nonce-le-v1"sv;
+static constexpr size_t maximum_challenge_token_length = 4096;
+static constexpr u64 maximum_expected_work = 100'000'000;
+static constexpr int request_timeout_ms = 30'000;
+static constexpr int retry_delay_ms = 3'000;
+static constexpr int maximum_retry_delay_ms = 60'000;
+
+static String to_string(StringView text)
+{
+ return String::from_utf8_with_replacement_character(text);
+}
+
+static Optional<JsonObject> parse_json_object(StringView text)
+{
+ auto parsed = JsonValue::from_string(text);
+ if (parsed.is_error() || !parsed.value().is_object())
+ return {};
+ return parsed.value().as_object();
+}
+
+static ByteString sha256_hex(ReadonlyBytes bytes)
+{
+ return encode_hex(Crypto::Hash::SHA256::hash(bytes).bytes());
+}
+
+void ReportSubmission::Contents::add(StringView key, StringView type, JsonValue value)
+{
+ fields.append({ key, type, move(value) });
+}
+
+// Empty values are left out, as the server has nothing to show for them.
+void ReportSubmission::Contents::add_text(StringView key, StringView type, StringView value)
+{
+ if (!value.is_empty())
+ add(key, type, to_string(value));
+}
+
+ByteString ReportSubmission::build_manifest(Contents const& contents, ReadonlySpan<Attachment> attachments)
+{
+ JsonArray fields;
+ for (auto const& field : contents.fields) {
+ JsonObject typed;
+ typed.set("type"sv, to_string(field.type));
+ typed.set("value"sv, field.value);
+ JsonObject entry;
+ entry.set("key"sv, to_string(field.key));
+ entry.set("value"sv, move(typed));
+ fields.must_append(move(entry));
+ }
+
+ JsonArray described_attachments;
+ for (auto const& attachment : attachments) {
+ JsonObject described;
+ described.set("id"sv, to_string(attachment.id));
+ described.set("name"sv, to_string(attachment.name));
+ described.set("media_type"sv, to_string(attachment.media_type));
+ described.set("size"sv, attachment.content.size());
+ described.set("sha256"sv, to_string(sha256_hex(attachment.content.bytes())));
+ described_attachments.must_append(move(described));
+ }
+
+ JsonObject manifest;
+ manifest.set("protocol"sv, 1);
+ manifest.set("submission_id"sv, generate_random_uuid());
+ manifest.set("kind"sv, to_string(contents.kind));
+ manifest.set("client_version"sv, to_string(contents.client_version));
+ manifest.set("build"sv, to_string(contents.build));
+ manifest.set("fields"sv, move(fields));
+ manifest.set("attachments"sv, move(described_attachments));
+ return manifest.serialized().to_byte_string();
+}
+
+static ByteString build_multipart_body(StringView manifest, ReadonlySpan<ReportSubmission::Attachment> attachments,
+ StringView boundary)
+{
+ StringBuilder builder;
+ auto append_part = [&](StringView disposition, StringView content_type, ReadonlyBytes content) {
+ builder.appendff("--{}\r\nContent-Disposition: form-data; {}\r\nContent-Type: {}\r\n\r\n",
+ boundary, disposition, content_type);
+ builder.append(StringView { content });
+ builder.append("\r\n"sv);
+ };
+
+ append_part("name=\"manifest\""sv, "application/json"sv, manifest.bytes());
+ for (auto const& attachment : attachments) {
+ auto content_type = attachment.media_type == "text/plain"sv ? "text/plain; charset=utf-8"sv : attachment.media_type.view();
+ append_part(ByteString::formatted("name=\"{}\"; filename=\"{}\"", attachment.id, attachment.name),
+ content_type, attachment.content.bytes());
+ }
+
+ builder.appendff("--{}--\r\n", boundary);
+ return builder.to_byte_string();
+}
+
+static Optional<u64> solve_challenge(ByteString const& token, u64 expected_work, Atomic<bool> const& cancelled)
+{
+ auto seed = Crypto::Hash::SHA256::hash(token.bytes());
+ Array<u8, 40> candidate {};
+ seed.bytes().copy_to(candidate.span().slice(0, 32));
+ auto threshold = NumericLimits<u64>::max() / expected_work;
+
+ for (u64 nonce = 0; nonce < NumericLimits<u64>::max(); ++nonce) {
+ if (nonce % 4096 == 0 && cancelled.load())
+ return {};
+ for (size_t i = 0; i < 8; ++i)
+ candidate[32 + i] = static_cast<u8>(nonce >> (i * 8));
+ auto digest = Crypto::Hash::SHA256::hash(candidate.span());
+ u64 prefix = 0;
+ for (size_t i = 0; i < 8; ++i)
+ prefix = prefix << 8 | digest.bytes()[i];
+ if (prefix <= threshold)
+ return nonce;
+ }
+ return {};
+}
+
+// The server would reject anything else again, so only these are worth another attempt. A conflict means the challenge
+// expired or was already used, which the fresh challenge of the next attempt resolves.
+static bool is_transient_status(u32 status)
+{
+ return status == 408 || status == 409 || status == 429 || (status >= 500 && status < 600);
+}
+
+// Only a delay in seconds is honored. A date falls back to the regular delay between attempts.
+static Optional<int> retry_after_ms(HTTP::HeaderList const& headers)
+{
+ auto value = headers.get("Retry-After"sv);
+ if (!value.has_value())
+ return {};
+ auto seconds = value->view().trim_whitespace().to_number<u32>();
+ if (!seconds.has_value())
+ return {};
+ return static_cast<int>(min<u64>(*seconds * 1000ull, maximum_retry_delay_ms));
+}
+
+// A submission prepares its payload, solves the proof-of-work challenge off the main thread, and uploads the report.
+// It can outlive whatever started it.
+ReportSubmission::ReportSubmission() = default;
+
+ReportSubmission::~ReportSubmission()
+{
+ m_proof_cancelled.store(true);
+ if (m_proof_thread && m_proof_thread->needs_to_be_joined())
+ (void)m_proof_thread->join();
+}
+
+// A submission keeps itself alive until it is sent or fails for good, so it outlives the review that started it.
+void ReportSubmission::start()
+{
+ m_self_while_in_flight = *this;
+ begin_attempt();
+}
+
+void ReportSubmission::begin_attempt()
+{
+ if (on_progress)
+ on_progress(Stage::Preparing);
+
+ if (!m_payload.has_value()) {
+ auto payload = prepare();
+ if (payload.is_error()) {
+ fail_preparation(payload.release_error());
+ return;
+ }
+ m_payload = payload.release_value();
+ }
+
+ if (on_prepared)
+ on_prepared(m_payload->manifest);
+ request_challenge();
+}
+
+void ReportSubmission::fail_preparation(String reason)
+{
+ // Reporting the failure can release the last reference to this submission.
+ NonnullRefPtr protector = *this;
+
+ m_self_while_in_flight = nullptr;
+ if (on_failed)
+ on_failed(Failure::Preparation, move(reason));
+}
+
+void ReportSubmission::request_challenge()
+{
+ if (on_progress)
+ on_progress(Stage::RequestingChallenge);
+
+ JsonObject body;
+ body.set("manifest_digest"sv, to_string(sha256_hex(m_payload->manifest.bytes())));
+
+ post(challenge_path, "requesting a challenge"sv, "application/json"sv, body.serialized().to_byte_string(), {},
+ [this](ByteString response) {
+ auto challenge = parse_json_object(response);
+ if (!challenge.has_value()) {
+ fail("The report server sent an unreadable challenge."_string, ShouldRetry::No);
+ return;
+ }
+
+ auto algorithm = challenge->get_string("algorithm"sv);
+ if (!algorithm.has_value() || *algorithm != challenge_algorithm) {
+ fail("Unsupported report challenge."_string, ShouldRetry::No);
+ return;
+ }
+
+ auto token = challenge->get_string("token"sv);
+ auto expected_work = challenge->get_integer<u64>("expected_work"sv);
+ if (!token.has_value() || token->bytes().size() > maximum_challenge_token_length
+ || !expected_work.has_value() || *expected_work == 0 || *expected_work > maximum_expected_work) {
+ fail("The report server supplied an invalid challenge."_string, ShouldRetry::No);
+ return;
+ }
+
+ solve_proof(token->to_byte_string(), *expected_work);
+ });
+}
+
+void ReportSubmission::solve_proof(ByteString token, u64 expected_work)
+{
+ if (on_progress)
+ on_progress(Stage::SolvingProof);
+
+ // A previous attempt's search has already delivered its result.
+ if (m_proof_thread && m_proof_thread->needs_to_be_joined())
+ (void)m_proof_thread->join();
+
+ // The server chooses the difficulty, so the search runs on its own thread, at the priority of work that the user
+ // follows through a progress indicator.
+ auto& main_thread_event_loop = Core::EventLoop::current();
+ m_proof_thread = Threading::Thread::construct("Report proof"sv,
+ [weak_this = make_weak_ptr<ReportSubmission>(), &cancelled = m_proof_cancelled, &main_thread_event_loop,
+ attempt = m_attempt, token = move(token), expected_work]() mutable -> intptr_t {
+ auto nonce = solve_challenge(token, expected_work, cancelled);
+ 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)
+ return;
+ if (!nonce.has_value()) {
+ self->fail("Could not compute the report's proof of work."_string, ShouldRetry::No);
+ return;
+ }
+ self->send_report(ProofOfWork { move(token), *nonce });
+ });
+ return 0;
+ });
+ m_proof_thread->set_qos(Core::Platform::ThreadQoS::Utility);
+ m_proof_thread->start();
+}
+
+void ReportSubmission::send_report(ProofOfWork proof)
+{
+ if (on_progress)
+ on_progress(Stage::Sending);
+
+ auto boundary = ByteString::formatted("ladybird-{}", generate_random_uuid());
+ auto body = build_multipart_body(m_payload->manifest, m_payload->attachments, boundary);
+ auto content_type = ByteString::formatted("multipart/form-data; boundary={}", boundary);
+
+ post(report_path, "sending the report"sv, content_type, move(body), move(proof), [this](ByteString response) {
+ auto body = parse_json_object(response);
+ auto receipt = body.has_value() ? body->get_string("receipt"sv) : Optional<String const&> {};
+ if (!receipt.has_value() || receipt->is_empty()) {
+ fail("Report server did not provide a receipt."_string, ShouldRetry::No);
+ return;
+ }
+ finish();
+ });
+}
+
+// A response outside the 2xx range fails this attempt, and the activity names what was going on in that message.
+void ReportSubmission::post(StringView path, StringView activity, StringView content_type, ByteString body,
+ Optional<ProofOfWork> proof, Function<void(ByteString)> on_success)
+{
+ auto endpoint = Core::Environment::get("LADYBIRD_REPORTS_ENDPOINT"sv).value_or(default_endpoint);
+ auto url = URL::Parser::basic_parse(MUST(String::formatted("{}{}{}", endpoint,
+ endpoint.ends_with('/') ? ""sv : "/"sv, path.substring_view(1))));
+ if (!url.has_value() || (url->scheme() != "https"sv && url->scheme() != "http"sv)) {
+ fail("Invalid report server configuration."_string, ShouldRetry::No);
+ return;
+ }
+
+ auto headers = HTTP::HeaderList::create();
+ headers->set({ "Content-Type"sv, ByteString { content_type } });
+ if (proof.has_value()) {
+ headers->set({ "X-Ladybird-Challenge"sv, proof->token });
+ headers->set({ "X-Ladybird-Nonce"sv, ByteString::number(proof->nonce) });
+ }
+
+ auto request_client = Application::request_server_client(IsPrivate::No);
+ if (request_client.is_error()) {
+ fail("Could not start the report request."_string, ShouldRetry::Yes);
+ return;
+ }
+ m_request = request_client.value()->start_request("POST"sv, *url, *headers, body.bytes(),
+ HTTP::CacheMode::NoStore, HTTP::Cookie::IncludeCredentials::No);
+ if (!m_request) {
+ fail("Could not connect to the report server."_string, ShouldRetry::Yes);
+ return;
+ }
+
+ m_request_timeout = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(request_timeout_ms),
+ [weak_this = make_weak_ptr<ReportSubmission>()] {
+ auto self = weak_this.strong_ref();
+ if (!self)
+ return;
+ auto request = move(self->m_request);
+ self->m_request_timeout = nullptr;
+ if (request)
+ request->stop();
+ self->fail("The report server did not respond in time."_string, ShouldRetry::Yes);
+ });
+ m_request_timeout->start();
+
+ m_request->set_buffered_request_finished_callback(
+ [weak_this = make_weak_ptr<ReportSubmission>(), activity = ByteString { activity },
+ on_success = move(on_success)](u64, Requests::RequestTimingInfo const&,
+ Optional<Requests::NetworkError> const& network_error, HTTP::HeaderList const& response_headers,
+ Optional<u32> response_code, Optional<String> const&,
+ Optional<Core::ImmutableBytes>, Optional<u64>, Requests::CacheState,
+ Core::ImmutableBytes payload) {
+ auto self = weak_this.strong_ref();
+ if (!self || !self->m_request)
+ return;
+ if (self->m_request_timeout)
+ self->m_request_timeout->stop();
+ self->m_request_timeout = nullptr;
+
+ // A request cannot be destroyed from inside its own callback.
+ Core::deferred_invoke([weak_this] {
+ if (auto self = weak_this.strong_ref())
+ self->m_request = nullptr;
+ });
+
+ if (network_error.has_value() || !response_code.has_value()) {
+ self->fail("Network error while contacting the report server."_string, ShouldRetry::Yes);
+ return;
+ }
+ if (*response_code < 200 || *response_code >= 300) {
+ self->fail(MUST(String::formatted("Report server returned {} while {}.", *response_code, activity)),
+ is_transient_status(*response_code) ? ShouldRetry::Yes : ShouldRetry::No,
+ retry_after_ms(response_headers));
+ return;
+ }
+ on_success(ByteString::copy(payload.bytes()));
+ });
+}
+
+void ReportSubmission::finish()
+{
+ // Reporting success can release the last reference to this submission.
+ NonnullRefPtr protector = *this;
+
+ if (m_request_timeout) {
+ m_request_timeout->stop();
+ m_request_timeout = nullptr;
+ }
+ did_send();
+ m_self_while_in_flight = nullptr;
+ if (on_sent)
+ on_sent();
+}
+
+void ReportSubmission::fail(String reason, ShouldRetry should_retry, Optional<int> retry_after_ms)
+{
+ // Reporting the final failure can release the last reference to this submission.
+ NonnullRefPtr protector = *this;
+
+ if (m_request_timeout) {
+ m_request_timeout->stop();
+ m_request_timeout = nullptr;
+ }
+
+ if (should_retry == ShouldRetry::No || m_attempt >= maximum_attempts) {
+ m_self_while_in_flight = nullptr;
+ if (on_failed)
+ on_failed(Failure::Sending, move(reason));
+ return;
+ }
+
+ // A server that asks for more time gets it, but it cannot keep the user waiting for long.
+ auto delay_ms = clamp(retry_after_ms.value_or(retry_delay_ms), retry_delay_ms, maximum_retry_delay_ms);
+ auto retry_number = m_attempt++;
+ if (on_retry)
+ on_retry(move(reason), retry_number, maximum_attempts - 1, static_cast<u32>(delay_ms / 1000));
+
+ m_retry_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(delay_ms),
+ [weak_this = make_weak_ptr<ReportSubmission>()] {
+ if (auto self = weak_this.strong_ref())
+ self->begin_attempt();
+ });
+ m_retry_timer->start();
+}
+
+}
diff --git a/Libraries/LibWebView/ReportSubmission.h b/Libraries/LibWebView/ReportSubmission.h
new file mode 100644
index 00000000000..5b40cb70f4a
--- /dev/null
+++ b/Libraries/LibWebView/ReportSubmission.h
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Atomic.h>
+#include <AK/ByteString.h>
+#include <AK/Error.h>
+#include <AK/Function.h>
+#include <AK/JsonValue.h>
+#include <AK/RefCounted.h>
+#include <AK/RefPtr.h>
+#include <AK/String.h>
+#include <AK/Vector.h>
+#include <AK/WeakPtr.h>
+#include <AK/Weakable.h>
+#include <LibCore/Forward.h>
+#include <LibCore/ImmutableBytes.h>
+#include <LibRequests/Forward.h>
+#include <LibThreading/Forward.h>
+#include <LibWebView/Forward.h>
+
+namespace WebView {
+
+class WEBVIEW_API ReportSubmission
+ : public RefCounted<ReportSubmission>
+ , public Weakable<ReportSubmission> {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ enum class Stage : u8 {
+ Preparing,
+ RequestingChallenge,
+ SolvingProof,
+ Sending,
+ };
+
+ enum class Failure : u8 {
+ Preparation,
+ Sending,
+ };
+
+ static constexpr u32 maximum_attempts = 6;
+
+ struct Field {
+ StringView key;
+ StringView type;
+ JsonValue value;
+ };
+
+ struct Attachment {
+ ByteString id;
+ ByteString name;
+ ByteString media_type;
+ Core::ImmutableBytes content;
+ };
+
+ struct Contents {
+ void add(StringView key, StringView type, JsonValue value);
+ void add_text(StringView key, StringView type, StringView value);
+
+ StringView kind;
+ ByteString client_version;
+ ByteString build;
+ Vector<Field> fields;
+ };
+
+ struct Payload {
+ ByteString manifest;
+ Vector<Attachment> attachments;
+ };
+
+ static ByteString build_manifest(Contents const&, ReadonlySpan<Attachment>);
+
+ virtual ~ReportSubmission();
+
+ void start();
+
+ Function<void(Stage)> on_progress;
+ Function<void(ByteString const& manifest)> on_prepared;
+ Function<void(String reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds)> on_retry;
+ Function<void()> on_sent;
+ Function<void(Failure, String reason)> on_failed;
+
+protected:
+ ReportSubmission();
+
+ virtual ErrorOr<Payload, String> prepare() = 0;
+ virtual void did_send() { }
+
+private:
+ struct ProofOfWork {
+ ByteString token;
+ u64 nonce { 0 };
+ };
+
+ enum class ShouldRetry {
+ No,
+ Yes,
+ };
+
+ void begin_attempt();
+ void fail_preparation(String reason);
+ void request_challenge();
+ void solve_proof(ByteString token, u64 expected_work);
+ void send_report(ProofOfWork);
+ void finish();
+ void fail(String reason, ShouldRetry, Optional<int> retry_after_ms = {});
+
+ void post(StringView path, StringView activity, StringView content_type, ByteString body, Optional<ProofOfWork>,
+ Function<void(ByteString body)> on_success);
+
+ Optional<Payload> m_payload;
+ u32 m_attempt { 1 };
+
+ RefPtr<Requests::Request> m_request;
+ RefPtr<Core::Timer> m_request_timeout;
+ RefPtr<Core::Timer> m_retry_timer;
+
+ RefPtr<ReportSubmission> m_self_while_in_flight;
+
+ RefPtr<Threading::Thread> m_proof_thread;
+ Atomic<bool> m_proof_cancelled { false };
+};
+
+}
diff --git a/Tests/LibWebView/TestCrashReportReview.cpp b/Tests/LibWebView/TestCrashReportReview.cpp
index 275418891a8..ab64bd6bd7d 100644
--- a/Tests/LibWebView/TestCrashReportReview.cpp
+++ b/Tests/LibWebView/TestCrashReportReview.cpp
@@ -18,7 +18,7 @@
#include <unistd.h>
using WebView::CrashReportReview;
-using WebView::CrashReportSubmission;
+using WebView::ReportSubmission;
static constexpr auto report_text = "Ladybird crash report, format 1\n"
"Process: WebContent\n"
@@ -231,7 +231,7 @@ TEST_CASE(a_report_changed_after_it_was_reviewed_is_not_sent)
auto file = MUST(directory.open(name, Core::File::OpenMode::Write | Core::File::OpenMode::Truncate));
MUST(file->write_until_depleted("Ladybird crash report, format 1\nProcess: Browser\n"sv.bytes()));
- Optional<CrashReportSubmission::Failure> failure;
+ Optional<ReportSubmission::Failure> failure;
String reason;
review.on_failed = [&](auto kind, String message) {
failure = kind;
@@ -239,7 +239,7 @@ TEST_CASE(a_report_changed_after_it_was_reviewed_is_not_sent)
};
EXPECT(!review.send(""_string, {}).has_value());
- EXPECT(failure == CrashReportSubmission::Failure::Preparation);
+ EXPECT(failure == ReportSubmission::Failure::Preparation);
EXPECT_EQ(reason, "This report changed after it was reviewed, so it was not sent."sv);
EXPECT(!review.is_sending());
}
diff --git a/Tests/LibWebView/TestCrashReportSubmission.cpp b/Tests/LibWebView/TestCrashReportSubmission.cpp
index 48d828b31c8..dbc572d6578 100644
--- a/Tests/LibWebView/TestCrashReportSubmission.cpp
+++ b/Tests/LibWebView/TestCrashReportSubmission.cpp
@@ -181,7 +181,7 @@ Optional<JsonValue> manifest_field(JsonObject const& manifest, StringView key)
struct Outcome {
bool sent { false };
- Optional<WebView::CrashReportSubmission::Failure> failure;
+ Optional<WebView::ReportSubmission::Failure> failure;
String reason;
Vector<u32> retry_delays;
@@ -193,7 +193,7 @@ void track(WebView::CrashReportReview& review, Outcome& outcome)
outcome = {};
review.on_retry = [&outcome](String const&, u32 retry_number, u32 maximum_retries, u32 delay_seconds) {
VERIFY(retry_number == outcome.retry_delays.size() + 1);
- VERIFY(maximum_retries == WebView::CrashReportSubmission::maximum_attempts - 1);
+ VERIFY(maximum_retries == WebView::ReportSubmission::maximum_attempts - 1);
outcome.retry_delays.append(delay_seconds);
};
review.on_sent = [&outcome] { outcome.sent = true; };
@@ -306,7 +306,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
VERIFY(!review.send(""_string, {}).has_value());
Core::EventLoop::current().spin_until([&] { return outcome.is_finished(); });
- VERIFY(outcome.failure == WebView::CrashReportSubmission::Failure::Sending);
+ VERIFY(outcome.failure == WebView::ReportSubmission::Failure::Sending);
VERIFY(outcome.reason == "Report server returned 400 while requesting a challenge."sv);
VERIFY(outcome.retry_delays.is_empty());
VERIFY(server.requests.size() == 1);
diff --git a/Tests/UI/Qt/TestCrashReportReviewWidget.cpp b/Tests/UI/Qt/TestCrashReportReviewWidget.cpp
index 79d867e0dce..9e720651fa4 100644
--- a/Tests/UI/Qt/TestCrashReportReviewWidget.cpp
+++ b/Tests/UI/Qt/TestCrashReportReviewWidget.cpp
@@ -26,7 +26,7 @@
#include <unistd.h>
using Ladybird::CrashReportReviewWidget;
-using WebView::CrashReportSubmission;
+using WebView::ReportSubmission;
static constexpr auto report_text = "Ladybird crash report, format 1\n"
"Process: WebContent\n"
@@ -123,7 +123,7 @@ TEST_CASE(the_review_offers_to_exit_before_and_after_answering)
EXPECT_EQ(exits, 2);
// A report that may still be sent offers both.
- widget.show_failure(CrashReportSubmission::Failure::Sending, "Network error while contacting the report server."_string);
+ 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());
@@ -198,7 +198,7 @@ TEST_CASE(sending_shows_its_progress_and_outcome)
auto& message = child<QLabel>(widget, "CrashReportStatusMessage");
auto& retry = child<QPushButton>(widget, "CrashReportRetryButton");
- widget.show_progress(CrashReportSubmission::Stage::SolvingProof);
+ widget.show_progress(ReportSubmission::Stage::SolvingProof);
EXPECT_EQ(status_title(widget), "Sending crash report");
EXPECT_EQ(message.text(), "Preparing report…");
@@ -206,11 +206,11 @@ TEST_CASE(sending_shows_its_progress_and_outcome)
EXPECT_EQ(message.text(), "Report server returned 503 while sending the report. Trying again in 3 seconds (2 of 5)…");
// Only a report that failed to reach the server can be sent again.
- widget.show_failure(CrashReportSubmission::Failure::Sending, "Network error while contacting the report server."_string);
+ widget.show_failure(ReportSubmission::Failure::Sending, "Network error while contacting the report server."_string);
EXPECT_EQ(status_title(widget), "Couldn’t send report");
EXPECT(retry.isVisible());
- widget.show_failure(CrashReportSubmission::Failure::Preparation, "Could not prepare this report."_string);
+ widget.show_failure(ReportSubmission::Failure::Preparation, "Could not prepare this report."_string);
EXPECT(!retry.isVisible());
EXPECT(child<QPushButton>(widget, "CrashReportExitButton").isVisible());
diff --git a/UI/Qt/CrashReportReviewWidget.cpp b/UI/Qt/CrashReportReviewWidget.cpp
index 7eb9fe04a87..5fec1a0057f 100644
--- a/UI/Qt/CrashReportReviewWidget.cpp
+++ b/UI/Qt/CrashReportReviewWidget.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibWebView/ReportDetail.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/CrashReportReviewWidget.h>
#include <UI/Qt/StringUtils.h>
@@ -94,7 +95,7 @@ static void clear_form(QFormLayout& form)
form.removeRow(0);
}
-static void add_field(QFormLayout& form, WebView::CrashReportReview::Field const& field, QWidget* parent)
+static void add_field(QFormLayout& form, WebView::ReportDetail const& field, QWidget* parent)
{
// Labels stay on one line, so each value starts level with its label however far it wraps.
auto* label = create_label(qstring_from_ak_string(field.label), parent);
@@ -256,7 +257,7 @@ CrashReportReviewWidget::CrashReportReviewWidget(QString exit_text, QWidget* par
on_answered();
});
QObject::connect(m_retry_button, &QPushButton::clicked, this, [this] {
- show_progress(WebView::CrashReportSubmission::Stage::Preparing);
+ show_progress(WebView::ReportSubmission::Stage::Preparing);
VERIFY(!m_review.send(m_description, m_url).has_value());
});
QObject::connect(m_next_button, &QPushButton::clicked, this, [this] {
@@ -379,7 +380,7 @@ void CrashReportReviewWidget::send()
}
m_validation->hide();
- show_progress(WebView::CrashReportSubmission::Stage::Preparing);
+ show_progress(WebView::ReportSubmission::Stage::Preparing);
if (on_answered)
on_answered();
VERIFY(!m_review.send(m_description, m_url).has_value());
@@ -405,9 +406,9 @@ void CrashReportReviewWidget::show_status(QString const& title, QString const& m
primary->setFocus(Qt::OtherFocusReason);
}
-void CrashReportReviewWidget::show_progress(WebView::CrashReportSubmission::Stage stage)
+void CrashReportReviewWidget::show_progress(WebView::ReportSubmission::Stage stage)
{
- using Stage = WebView::CrashReportSubmission::Stage;
+ using Stage = WebView::ReportSubmission::Stage;
auto percent = [&] {
switch (stage) {
@@ -451,12 +452,12 @@ void CrashReportReviewWidget::show_declined()
show_outcome(tr("Report not sent"), tr("This report stays on your device and is not sent."));
}
-void CrashReportReviewWidget::show_failure(WebView::CrashReportSubmission::Failure failure, String const& reason)
+void CrashReportReviewWidget::show_failure(WebView::ReportSubmission::Failure failure, String const& reason)
{
m_progress->hide();
// Only sending can succeed on another attempt. A report that could not be prepared would fail the same way again.
- if (failure == WebView::CrashReportSubmission::Failure::Sending) {
+ if (failure == WebView::ReportSubmission::Failure::Sending) {
show_status(tr("Couldn’t send report"), qstring_from_ak_string(reason), { m_retry_button, m_exit_button },
m_retry_button);
return;
diff --git a/UI/Qt/CrashReportReviewWidget.h b/UI/Qt/CrashReportReviewWidget.h
index 38c1e21115b..3fef032b8f2 100644
--- a/UI/Qt/CrashReportReviewWidget.h
+++ b/UI/Qt/CrashReportReviewWidget.h
@@ -14,6 +14,7 @@
#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <LibWebView/CrashReportReview.h>
+#include <LibWebView/ReportSubmission.h>
#include <QWidget>
@@ -43,11 +44,11 @@ public:
ErrorOr<void> open_report(Optional<ByteString> const& name = {}, Optional<String> const& website = {});
- void show_progress(WebView::CrashReportSubmission::Stage);
+ void show_progress(WebView::ReportSubmission::Stage);
void show_retry(String const& reason, u32 retry_number, u32 maximum_retries, u32 delay_seconds);
void show_sent();
void show_declined();
- void show_failure(WebView::CrashReportSubmission::Failure, String const& reason);
+ 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.