1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/src/gfxCrashReporterUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "gfxCrashReporterUtils.h" 1.10 + 1.11 +#if defined(MOZ_CRASHREPORTER) 1.12 +#define MOZ_GFXFEATUREREPORTER 1 1.13 +#endif 1.14 + 1.15 +#ifdef MOZ_GFXFEATUREREPORTER 1.16 +#include "gfxCrashReporterUtils.h" 1.17 +#include <string.h> // for strcmp 1.18 +#include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 1.19 +#include "mozilla/Services.h" // for GetObserverService 1.20 +#include "mozilla/mozalloc.h" // for operator new, etc 1.21 +#include "nsAutoPtr.h" // for nsRefPtr 1.22 +#include "nsCOMPtr.h" // for nsCOMPtr 1.23 +#include "nsError.h" // for NS_OK, NS_FAILED, nsresult 1.24 +#include "nsExceptionHandler.h" // for AppendAppNotesToCrashReport 1.25 +#include "nsID.h" 1.26 +#include "nsIEventTarget.h" // for NS_DISPATCH_NORMAL 1.27 +#include "nsIObserver.h" // for nsIObserver, etc 1.28 +#include "nsIObserverService.h" // for nsIObserverService 1.29 +#include "nsIRunnable.h" // for nsIRunnable 1.30 +#include "nsISupports.h" 1.31 +#include "nsString.h" // for nsAutoCString, nsCString, etc 1.32 +#include "nsTArray.h" // for nsTArray 1.33 +#include "nsThreadUtils.h" // for NS_DispatchToMainThread, etc 1.34 +#include "nscore.h" // for NS_IMETHOD, NS_IMETHODIMP, etc 1.35 + 1.36 +namespace mozilla { 1.37 + 1.38 +static nsTArray<nsCString> *gFeaturesAlreadyReported = nullptr; 1.39 + 1.40 +class ObserverToDestroyFeaturesAlreadyReported : public nsIObserver 1.41 +{ 1.42 + 1.43 +public: 1.44 + NS_DECL_ISUPPORTS 1.45 + NS_DECL_NSIOBSERVER 1.46 + 1.47 + ObserverToDestroyFeaturesAlreadyReported() {} 1.48 + virtual ~ObserverToDestroyFeaturesAlreadyReported() {} 1.49 +}; 1.50 + 1.51 +NS_IMPL_ISUPPORTS(ObserverToDestroyFeaturesAlreadyReported, 1.52 + nsIObserver) 1.53 + 1.54 +NS_IMETHODIMP 1.55 +ObserverToDestroyFeaturesAlreadyReported::Observe(nsISupports* aSubject, 1.56 + const char* aTopic, 1.57 + const char16_t* aData) 1.58 +{ 1.59 + if (!strcmp(aTopic, "xpcom-shutdown")) { 1.60 + if (gFeaturesAlreadyReported) { 1.61 + delete gFeaturesAlreadyReported; 1.62 + gFeaturesAlreadyReported = nullptr; 1.63 + } 1.64 + } 1.65 + return NS_OK; 1.66 +} 1.67 + 1.68 +class ScopedGfxFeatureReporter::AppNoteWritingRunnable : public nsRunnable { 1.69 +public: 1.70 + AppNoteWritingRunnable(char aStatusChar, const char *aFeature) : 1.71 + mStatusChar(aStatusChar), mFeature(aFeature) {} 1.72 + NS_IMETHOD Run() { 1.73 + // LeakLog made me do this. Basically, I just wanted gFeaturesAlreadyReported to be a static nsTArray<nsCString>, 1.74 + // and LeakLog was complaining about leaks like this: 1.75 + // leaked 1 instance of nsTArray_base with size 8 bytes 1.76 + // leaked 7 instances of nsStringBuffer with size 8 bytes each (56 bytes total) 1.77 + // So this is a work-around using a pointer, and using a nsIObserver to deallocate on xpcom shutdown. 1.78 + // Yay for fighting bloat. 1.79 + if (!gFeaturesAlreadyReported) { 1.80 + nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService(); 1.81 + if (!observerService) 1.82 + return NS_OK; 1.83 + nsRefPtr<ObserverToDestroyFeaturesAlreadyReported> observer = new ObserverToDestroyFeaturesAlreadyReported; 1.84 + nsresult rv = observerService->AddObserver(observer, "xpcom-shutdown", false); 1.85 + if (NS_FAILED(rv)) { 1.86 + observer = nullptr; 1.87 + return NS_OK; 1.88 + } 1.89 + gFeaturesAlreadyReported = new nsTArray<nsCString>; 1.90 + } 1.91 + 1.92 + nsAutoCString featureString; 1.93 + featureString.AppendPrintf("%s%c ", 1.94 + mFeature, 1.95 + mStatusChar); 1.96 + 1.97 + if (!gFeaturesAlreadyReported->Contains(featureString)) { 1.98 + gFeaturesAlreadyReported->AppendElement(featureString); 1.99 + CrashReporter::AppendAppNotesToCrashReport(featureString); 1.100 + } 1.101 + return NS_OK; 1.102 + } 1.103 +private: 1.104 + char mStatusChar; 1.105 + const char *mFeature; 1.106 +}; 1.107 + 1.108 +void 1.109 +ScopedGfxFeatureReporter::WriteAppNote(char statusChar) 1.110 +{ 1.111 + nsCOMPtr<nsIRunnable> r = new AppNoteWritingRunnable(statusChar, mFeature); 1.112 + NS_DispatchToMainThread(r.get(), NS_DISPATCH_NORMAL); 1.113 +} 1.114 + 1.115 +} // end namespace mozilla 1.116 + 1.117 +#else 1.118 + 1.119 +namespace mozilla { 1.120 +void ScopedGfxFeatureReporter::WriteAppNote(char) {} 1.121 +} 1.122 + 1.123 +#endif