Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_ipc_CrashReporterClient_h
8 : #define mozilla_ipc_CrashReporterClient_h
9 :
10 : #include "mozilla/StaticMutex.h"
11 : #include "mozilla/StaticPtr.h"
12 : #include "mozilla/Unused.h"
13 : #include "mozilla/ipc/Shmem.h"
14 :
15 : #ifdef MOZ_CRASHREPORTER
16 : namespace mozilla {
17 : namespace ipc {
18 :
19 : class CrashReporterMetadataShmem;
20 :
21 : class CrashReporterClient
22 : {
23 : public:
24 2 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CrashReporterClient);
25 :
26 : // |aTopLevelProtocol| must be a top-level protocol instance, as sub-actors
27 : // do not have AllocUnsafeShmem. It must also have a child-to-parent message:
28 : //
29 : // async SetCrashReporterClient(Shmem shmem);
30 : //
31 : // The parent-side receive function of this message should save the shmem
32 : // somewhere, and when the top-level actor's ActorDestroy runs (or when the
33 : // crash reporter needs metadata), the shmem should be parsed.
34 : template <typename T>
35 2 : static bool InitSingleton(T* aToplevelProtocol) {
36 4 : Shmem shmem;
37 2 : if (!AllocShmem(aToplevelProtocol, &shmem)) {
38 0 : return false;
39 : }
40 :
41 2 : InitSingletonWithShmem(shmem);
42 2 : Unused << aToplevelProtocol->SendInitCrashReporter(
43 : shmem,
44 4 : CrashReporter::CurrentThreadId());
45 2 : return true;
46 : }
47 :
48 : template <typename T>
49 2 : static bool AllocShmem(T* aToplevelProtocol, Shmem* aOutShmem) {
50 : // 16KB should be enough for most metadata - see bug 1278717 comment #11.
51 : static const size_t kShmemSize = 16 * 1024;
52 :
53 : return aToplevelProtocol->AllocUnsafeShmem(
54 : kShmemSize,
55 : SharedMemory::TYPE_BASIC,
56 2 : aOutShmem);
57 : }
58 :
59 : static void InitSingletonWithShmem(const Shmem& aShmem);
60 :
61 : static void DestroySingleton();
62 : static RefPtr<CrashReporterClient> GetSingleton();
63 :
64 : void AnnotateCrashReport(const nsCString& aKey, const nsCString& aData);
65 : void AppendAppNotes(const nsCString& aData);
66 :
67 : private:
68 : explicit CrashReporterClient(const Shmem& aShmem);
69 : ~CrashReporterClient();
70 :
71 : private:
72 : static StaticMutex sLock;
73 : static StaticRefPtr<CrashReporterClient> sClientSingleton;
74 :
75 : private:
76 : UniquePtr<CrashReporterMetadataShmem> mMetadata;
77 : };
78 :
79 : } // namespace ipc
80 : } // namespace mozilla
81 : #endif // MOZ_CRASHREPORTER
82 :
83 : #endif // mozilla_ipc_CrashReporterClient_h
84 :
|