Skip to content

Commit 46cbe91

Browse files
committed
Add Crashpad upload thread to process pending native reports
- Start a CrashReportUploadThread during Crashpad initialization. - Enable watch_pending_reports so pending native crashes from previous runs are retried automatically. - Store Backtrace URL and pass it to the upload thread. - Update DisableCrashpad()/ReEnableCrashpad() to stop/start the thread.
1 parent 3cf4822 commit 46cbe91

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

backtrace-library/src/main/cpp/backends/crashpad-backend.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "crashpad-backend.h"
22
#include "handler/handler_main.h"
3+
#include "handler/crash_report_upload_thread.h"
34
#include "backtrace-native.h"
45
#include <jni.h>
56
#include <libgen.h>
@@ -12,6 +13,51 @@ extern std::atomic_bool disabled;
1213
static crashpad::CrashpadClient *client;
1314
static std::unique_ptr<crashpad::CrashReportDatabase> database;
1415

16+
// offline native uploads
17+
static std::unique_ptr<crashpad::CrashReportUploadThread> upload_thread;
18+
static std::string server_url;
19+
20+
namespace {
21+
22+
void StartCrashpadUploadThreadIfNeeded() {
23+
// Return if No DB or if the SDK is disabled
24+
if (!database || disabled) {
25+
return;
26+
}
27+
28+
if (server_url.empty()) {
29+
__android_log_print(
30+
ANDROID_LOG_WARN,
31+
"Backtrace-Android",
32+
"Crashpad upload thread not started: server URL is empty");
33+
return;
34+
}
35+
36+
if (upload_thread && upload_thread->is_running()) {
37+
return;
38+
}
39+
40+
crashpad::CrashReportUploadThread::Options options{};
41+
// Scan the DB for pending reports
42+
options.watch_pending_reports = true;
43+
44+
crashpad::CrashReportUploadThread::ProcessPendingReportsObservationCallback callback;
45+
upload_thread = std::make_unique<crashpad::CrashReportUploadThread>(
46+
database.get(),
47+
server_url,
48+
options,
49+
callback);
50+
51+
upload_thread->Start();
52+
53+
__android_log_print(
54+
ANDROID_LOG_INFO,
55+
"Backtrace-Android",
56+
"Started Crashpad upload thread for offline native reports");
57+
}
58+
59+
} // namespace
60+
1561
std::vector<std::string>
1662
generateInitializationArguments(JNIEnv *env, jobjectArray attachmentPaths) {
1763
std::vector<std::string> arguments;
@@ -141,6 +187,10 @@ bool InitializeCrashpad(jstring url,
141187
// Enable automated uploads.
142188
database->GetSettings()->SetUploadsEnabled(true);
143189

190+
// Process pending reports
191+
server_url.assign(backtraceUrl);
192+
StartCrashpadUploadThreadIfNeeded();
193+
144194
// Start crash handler
145195
client = new crashpad::CrashpadClient();
146196

@@ -210,6 +260,9 @@ bool InitializeCrashpadJavaCrashHandler(jstring url,
210260
// Enable automated uploads.
211261
database->GetSettings()->SetUploadsEnabled(true);
212262

263+
server_url.assign(backtraceUrl);
264+
StartCrashpadUploadThreadIfNeeded();
265+
213266
// Start crash handler
214267
client = new crashpad::CrashpadClient();
215268

@@ -346,6 +399,12 @@ void DisableCrashpad() {
346399
// Disable automated uploads.
347400
database->GetSettings()->SetUploadsEnabled(false);
348401
disabled = true;
402+
403+
// Stop background upload thread
404+
if (upload_thread && upload_thread->is_running()) {
405+
upload_thread->Stop();
406+
upload_thread.reset();
407+
}
349408
}
350409

351410
void ReEnableCrashpad() {
@@ -358,5 +417,10 @@ void ReEnableCrashpad() {
358417
}
359418
database->GetSettings()->SetUploadsEnabled(true);
360419
disabled = false;
420+
421+
// Restart upload thread
422+
if (!server_url.empty()) {
423+
StartCrashpadUploadThreadIfNeeded();
424+
}
361425
}
362426
}

0 commit comments

Comments
 (0)