Bug 1758813 [wpt PR 33142] - Implement RP sign out, a=testonly
[gecko.git] / ipc / glue / CrashReporterHelper.h
blobe749ef9a2ca138dce2d9b05310ddb95820ec94c8
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_ipc_CrashReporterHelper_h
6 #define mozilla_ipc_CrashReporterHelper_h
8 #include "CrashReporterHost.h"
9 #include "mozilla/UniquePtr.h"
10 #include "nsExceptionHandler.h"
11 #include "nsICrashService.h"
12 #include "nsPrintfCString.h"
14 namespace mozilla {
15 namespace ipc {
17 /**
18 * This class encapsulates the common elements of crash report handling for
19 * toplevel protocols representing processes. To use this class, you should:
21 * 1. Declare a method to initialize the crash reporter in your IPDL:
22 * `async InitCrashReporter(NativeThreadId threadId)`
24 * 2. Inherit from this class, providing the appropriate `GeckoProcessType`
25 * enum value for the template parameter PT.
27 * 3. When your protocol actor is destroyed with a reason of `AbnormalShutdown`,
28 * you should call `GenerateCrashReport(OtherPid())`. If you need the crash
29 * report ID it will be copied in the second optional parameter upon
30 * successful crash report generation.
32 template <GeckoProcessType PT>
33 class CrashReporterHelper {
34 public:
35 CrashReporterHelper() : mCrashReporter(nullptr) {}
36 IPCResult RecvInitCrashReporter(const CrashReporter::ThreadId& aThreadId) {
37 mCrashReporter = MakeUnique<ipc::CrashReporterHost>(PT, aThreadId);
38 return IPC_OK();
41 protected:
42 void GenerateCrashReport(base::ProcessId aPid,
43 nsString* aMinidumpId = nullptr) {
44 nsAutoString minidumpId;
45 if (!mCrashReporter) {
46 HandleOrphanedMinidump(aPid, minidumpId);
47 } else if (mCrashReporter->GenerateCrashReport(aPid)) {
48 minidumpId = mCrashReporter->MinidumpID();
51 if (aMinidumpId) {
52 *aMinidumpId = minidumpId;
55 mCrashReporter = nullptr;
58 private:
59 void HandleOrphanedMinidump(base::ProcessId aPid, nsString& aMinidumpId) {
60 if (CrashReporter::FinalizeOrphanedMinidump(aPid, PT, &aMinidumpId)) {
61 CrashReporterHost::RecordCrash(PT, nsICrashService::CRASH_TYPE_CRASH,
62 aMinidumpId);
63 } else {
64 NS_WARNING(nsPrintfCString("child process pid = %d crashed without "
65 "leaving a minidump behind",
66 aPid)
67 .get());
71 protected:
72 UniquePtr<ipc::CrashReporterHost> mCrashReporter;
75 } // namespace ipc
76 } // namespace mozilla
78 #endif // mozilla_ipc_CrashReporterHelper_h