gfx/src/gfxCrashReporterUtils.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "gfxCrashReporterUtils.h"
michael@0 7
michael@0 8 #if defined(MOZ_CRASHREPORTER)
michael@0 9 #define MOZ_GFXFEATUREREPORTER 1
michael@0 10 #endif
michael@0 11
michael@0 12 #ifdef MOZ_GFXFEATUREREPORTER
michael@0 13 #include "gfxCrashReporterUtils.h"
michael@0 14 #include <string.h> // for strcmp
michael@0 15 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
michael@0 16 #include "mozilla/Services.h" // for GetObserverService
michael@0 17 #include "mozilla/mozalloc.h" // for operator new, etc
michael@0 18 #include "nsAutoPtr.h" // for nsRefPtr
michael@0 19 #include "nsCOMPtr.h" // for nsCOMPtr
michael@0 20 #include "nsError.h" // for NS_OK, NS_FAILED, nsresult
michael@0 21 #include "nsExceptionHandler.h" // for AppendAppNotesToCrashReport
michael@0 22 #include "nsID.h"
michael@0 23 #include "nsIEventTarget.h" // for NS_DISPATCH_NORMAL
michael@0 24 #include "nsIObserver.h" // for nsIObserver, etc
michael@0 25 #include "nsIObserverService.h" // for nsIObserverService
michael@0 26 #include "nsIRunnable.h" // for nsIRunnable
michael@0 27 #include "nsISupports.h"
michael@0 28 #include "nsString.h" // for nsAutoCString, nsCString, etc
michael@0 29 #include "nsTArray.h" // for nsTArray
michael@0 30 #include "nsThreadUtils.h" // for NS_DispatchToMainThread, etc
michael@0 31 #include "nscore.h" // for NS_IMETHOD, NS_IMETHODIMP, etc
michael@0 32
michael@0 33 namespace mozilla {
michael@0 34
michael@0 35 static nsTArray<nsCString> *gFeaturesAlreadyReported = nullptr;
michael@0 36
michael@0 37 class ObserverToDestroyFeaturesAlreadyReported : public nsIObserver
michael@0 38 {
michael@0 39
michael@0 40 public:
michael@0 41 NS_DECL_ISUPPORTS
michael@0 42 NS_DECL_NSIOBSERVER
michael@0 43
michael@0 44 ObserverToDestroyFeaturesAlreadyReported() {}
michael@0 45 virtual ~ObserverToDestroyFeaturesAlreadyReported() {}
michael@0 46 };
michael@0 47
michael@0 48 NS_IMPL_ISUPPORTS(ObserverToDestroyFeaturesAlreadyReported,
michael@0 49 nsIObserver)
michael@0 50
michael@0 51 NS_IMETHODIMP
michael@0 52 ObserverToDestroyFeaturesAlreadyReported::Observe(nsISupports* aSubject,
michael@0 53 const char* aTopic,
michael@0 54 const char16_t* aData)
michael@0 55 {
michael@0 56 if (!strcmp(aTopic, "xpcom-shutdown")) {
michael@0 57 if (gFeaturesAlreadyReported) {
michael@0 58 delete gFeaturesAlreadyReported;
michael@0 59 gFeaturesAlreadyReported = nullptr;
michael@0 60 }
michael@0 61 }
michael@0 62 return NS_OK;
michael@0 63 }
michael@0 64
michael@0 65 class ScopedGfxFeatureReporter::AppNoteWritingRunnable : public nsRunnable {
michael@0 66 public:
michael@0 67 AppNoteWritingRunnable(char aStatusChar, const char *aFeature) :
michael@0 68 mStatusChar(aStatusChar), mFeature(aFeature) {}
michael@0 69 NS_IMETHOD Run() {
michael@0 70 // LeakLog made me do this. Basically, I just wanted gFeaturesAlreadyReported to be a static nsTArray<nsCString>,
michael@0 71 // and LeakLog was complaining about leaks like this:
michael@0 72 // leaked 1 instance of nsTArray_base with size 8 bytes
michael@0 73 // leaked 7 instances of nsStringBuffer with size 8 bytes each (56 bytes total)
michael@0 74 // So this is a work-around using a pointer, and using a nsIObserver to deallocate on xpcom shutdown.
michael@0 75 // Yay for fighting bloat.
michael@0 76 if (!gFeaturesAlreadyReported) {
michael@0 77 nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
michael@0 78 if (!observerService)
michael@0 79 return NS_OK;
michael@0 80 nsRefPtr<ObserverToDestroyFeaturesAlreadyReported> observer = new ObserverToDestroyFeaturesAlreadyReported;
michael@0 81 nsresult rv = observerService->AddObserver(observer, "xpcom-shutdown", false);
michael@0 82 if (NS_FAILED(rv)) {
michael@0 83 observer = nullptr;
michael@0 84 return NS_OK;
michael@0 85 }
michael@0 86 gFeaturesAlreadyReported = new nsTArray<nsCString>;
michael@0 87 }
michael@0 88
michael@0 89 nsAutoCString featureString;
michael@0 90 featureString.AppendPrintf("%s%c ",
michael@0 91 mFeature,
michael@0 92 mStatusChar);
michael@0 93
michael@0 94 if (!gFeaturesAlreadyReported->Contains(featureString)) {
michael@0 95 gFeaturesAlreadyReported->AppendElement(featureString);
michael@0 96 CrashReporter::AppendAppNotesToCrashReport(featureString);
michael@0 97 }
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100 private:
michael@0 101 char mStatusChar;
michael@0 102 const char *mFeature;
michael@0 103 };
michael@0 104
michael@0 105 void
michael@0 106 ScopedGfxFeatureReporter::WriteAppNote(char statusChar)
michael@0 107 {
michael@0 108 nsCOMPtr<nsIRunnable> r = new AppNoteWritingRunnable(statusChar, mFeature);
michael@0 109 NS_DispatchToMainThread(r.get(), NS_DISPATCH_NORMAL);
michael@0 110 }
michael@0 111
michael@0 112 } // end namespace mozilla
michael@0 113
michael@0 114 #else
michael@0 115
michael@0 116 namespace mozilla {
michael@0 117 void ScopedGfxFeatureReporter::WriteAppNote(char) {}
michael@0 118 }
michael@0 119
michael@0 120 #endif

mercurial