Bug 1508381 - remove now-unnecessary TASKCLUSTER_* variables r=tomprince
[gecko.git] / gfx / src / gfxCrashReporterUtils.cpp
blobcc9b9a845a860b10db94b1d2e8e46041337bc827
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/. */
7 #include "gfxCrashReporterUtils.h"
8 #include <string.h> // for strcmp
9 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
10 #include "mozilla/Services.h" // for GetObserverService
11 #include "mozilla/StaticMutex.h"
12 #include "mozilla/SystemGroup.h" // for SystemGroup
13 #include "mozilla/mozalloc.h" // for operator new, etc
14 #include "mozilla/RefPtr.h" // for RefPtr
15 #include "MainThreadUtils.h" // for NS_IsMainThread
16 #include "nsCOMPtr.h" // for nsCOMPtr
17 #include "nsError.h" // for NS_OK, NS_FAILED, nsresult
18 #include "nsExceptionHandler.h" // for AppendAppNotesToCrashReport
19 #include "nsID.h"
20 #include "nsIEventTarget.h" // for NS_DISPATCH_NORMAL
21 #include "nsIObserver.h" // for nsIObserver, etc
22 #include "nsIObserverService.h" // for nsIObserverService
23 #include "nsIRunnable.h" // for nsIRunnable
24 #include "nsISupports.h"
25 #include "nsTArray.h" // for nsTArray
26 #include "nscore.h" // for NS_IMETHOD, NS_IMETHODIMP, etc
28 namespace mozilla {
30 static nsTArray<nsCString>* gFeaturesAlreadyReported = nullptr;
31 static StaticMutex gFeaturesAlreadyReportedMutex;
33 class ObserverToDestroyFeaturesAlreadyReported final : public nsIObserver {
34 public:
35 NS_DECL_ISUPPORTS
36 NS_DECL_NSIOBSERVER
38 ObserverToDestroyFeaturesAlreadyReported() {}
40 private:
41 virtual ~ObserverToDestroyFeaturesAlreadyReported() {}
44 NS_IMPL_ISUPPORTS(ObserverToDestroyFeaturesAlreadyReported, nsIObserver)
46 NS_IMETHODIMP
47 ObserverToDestroyFeaturesAlreadyReported::Observe(nsISupports* aSubject,
48 const char* aTopic,
49 const char16_t* aData) {
50 if (!strcmp(aTopic, "xpcom-shutdown")) {
51 StaticMutexAutoLock al(gFeaturesAlreadyReportedMutex);
52 if (gFeaturesAlreadyReported) {
53 delete gFeaturesAlreadyReported;
54 gFeaturesAlreadyReported = nullptr;
57 return NS_OK;
60 class RegisterObserverRunnable : public Runnable {
61 public:
62 RegisterObserverRunnable() : Runnable("RegisterObserverRunnable") {}
63 NS_IMETHOD Run() override {
64 // LeakLog made me do this. Basically, I just wanted
65 // gFeaturesAlreadyReported to be a static nsTArray<nsCString>, and LeakLog
66 // was complaining about leaks like this:
67 // leaked 1 instance of nsTArray_base with size 8 bytes
68 // leaked 7 instances of nsStringBuffer with size 8 bytes each (56 bytes
69 // total)
70 // So this is a work-around using a pointer, and using a nsIObserver to
71 // deallocate on xpcom shutdown. Yay for fighting bloat.
72 nsCOMPtr<nsIObserverService> observerService =
73 mozilla::services::GetObserverService();
74 if (!observerService) return NS_OK;
75 RefPtr<ObserverToDestroyFeaturesAlreadyReported> observer =
76 new ObserverToDestroyFeaturesAlreadyReported;
77 observerService->AddObserver(observer, "xpcom-shutdown", false);
78 return NS_OK;
82 class AppendAppNotesRunnable : public CancelableRunnable {
83 public:
84 explicit AppendAppNotesRunnable(const nsACString& aFeatureStr)
85 : CancelableRunnable("AppendAppNotesRunnable"),
86 mFeatureString(aFeatureStr) {}
88 NS_IMETHOD Run() override {
89 CrashReporter::AppendAppNotesToCrashReport(mFeatureString);
90 return NS_OK;
93 private:
94 nsAutoCString mFeatureString;
97 void ScopedGfxFeatureReporter::WriteAppNote(char statusChar,
98 int32_t statusNumber) {
99 StaticMutexAutoLock al(gFeaturesAlreadyReportedMutex);
101 if (!gFeaturesAlreadyReported) {
102 gFeaturesAlreadyReported = new nsTArray<nsCString>;
103 nsCOMPtr<nsIRunnable> r = new RegisterObserverRunnable();
104 SystemGroup::Dispatch(TaskCategory::Other, r.forget());
107 nsAutoCString featureString;
108 if (statusNumber == 0) {
109 featureString.AppendPrintf("%s%c ", mFeature, statusChar);
110 } else {
111 featureString.AppendPrintf("%s%c%d ", mFeature, statusChar, statusNumber);
114 if (!gFeaturesAlreadyReported->Contains(featureString)) {
115 gFeaturesAlreadyReported->AppendElement(featureString);
116 AppNote(featureString);
120 void ScopedGfxFeatureReporter::AppNote(const nsACString& aMessage) {
121 if (NS_IsMainThread()) {
122 CrashReporter::AppendAppNotesToCrashReport(aMessage);
123 } else {
124 nsCOMPtr<nsIRunnable> r = new AppendAppNotesRunnable(aMessage);
125 SystemGroup::Dispatch(TaskCategory::Other, r.forget());
129 } // end namespace mozilla