netwerk/protocol/about/nsAboutBloat.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "nsTraceRefcnt.h"
michael@0 7
michael@0 8 // if NS_BUILD_REFCNT_LOGGING isn't defined, don't try to build
michael@0 9 #ifdef NS_BUILD_REFCNT_LOGGING
michael@0 10
michael@0 11 #include "nsAboutBloat.h"
michael@0 12 #include "nsStringStream.h"
michael@0 13 #include "nsIURI.h"
michael@0 14 #include "nsCOMPtr.h"
michael@0 15 #include "nsNetUtil.h"
michael@0 16 #include "nsDirectoryServiceDefs.h"
michael@0 17 #include "nsIFile.h"
michael@0 18
michael@0 19 static void GC_gcollect() {}
michael@0 20
michael@0 21 NS_IMPL_ISUPPORTS(nsAboutBloat, nsIAboutModule)
michael@0 22
michael@0 23 NS_IMETHODIMP
michael@0 24 nsAboutBloat::NewChannel(nsIURI *aURI, nsIChannel **result)
michael@0 25 {
michael@0 26 NS_ENSURE_ARG_POINTER(aURI);
michael@0 27 nsresult rv;
michael@0 28 nsAutoCString path;
michael@0 29 rv = aURI->GetPath(path);
michael@0 30 if (NS_FAILED(rv)) return rv;
michael@0 31
michael@0 32 nsTraceRefcnt::StatisticsType statType = nsTraceRefcnt::ALL_STATS;
michael@0 33 bool clear = false;
michael@0 34 bool leaks = false;
michael@0 35
michael@0 36 int32_t pos = path.Find("?");
michael@0 37 if (pos > 0) {
michael@0 38 nsAutoCString param;
michael@0 39 (void)path.Right(param, path.Length() - (pos+1));
michael@0 40 if (param.EqualsLiteral("new"))
michael@0 41 statType = nsTraceRefcnt::NEW_STATS;
michael@0 42 else if (param.EqualsLiteral("clear"))
michael@0 43 clear = true;
michael@0 44 else if (param.EqualsLiteral("leaks"))
michael@0 45 leaks = true;
michael@0 46 }
michael@0 47
michael@0 48 nsCOMPtr<nsIInputStream> inStr;
michael@0 49 if (clear) {
michael@0 50 nsTraceRefcnt::ResetStatistics();
michael@0 51
michael@0 52 rv = NS_NewCStringInputStream(getter_AddRefs(inStr),
michael@0 53 NS_LITERAL_CSTRING("Bloat statistics cleared."));
michael@0 54 if (NS_FAILED(rv)) return rv;
michael@0 55 }
michael@0 56 else if (leaks) {
michael@0 57 // dump the current set of leaks.
michael@0 58 GC_gcollect();
michael@0 59
michael@0 60 rv = NS_NewCStringInputStream(getter_AddRefs(inStr),
michael@0 61 NS_LITERAL_CSTRING("Memory leaks dumped."));
michael@0 62 if (NS_FAILED(rv)) return rv;
michael@0 63 }
michael@0 64 else {
michael@0 65 nsCOMPtr<nsIFile> file;
michael@0 66 rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR,
michael@0 67 getter_AddRefs(file));
michael@0 68 if (NS_FAILED(rv)) return rv;
michael@0 69
michael@0 70 rv = file->AppendNative(NS_LITERAL_CSTRING("bloatlogs"));
michael@0 71 if (NS_FAILED(rv)) return rv;
michael@0 72
michael@0 73 bool exists;
michael@0 74 rv = file->Exists(&exists);
michael@0 75 if (NS_FAILED(rv)) return rv;
michael@0 76
michael@0 77 if (!exists) {
michael@0 78 // On all the platforms that I know use permissions,
michael@0 79 // directories need to have the executable flag set
michael@0 80 // if you want to do anything inside the directory.
michael@0 81 rv = file->Create(nsIFile::DIRECTORY_TYPE, 0755);
michael@0 82 if (NS_FAILED(rv)) return rv;
michael@0 83 }
michael@0 84
michael@0 85 nsAutoCString dumpFileName;
michael@0 86 if (statType == nsTraceRefcnt::ALL_STATS)
michael@0 87 dumpFileName.AssignLiteral("all-");
michael@0 88 else
michael@0 89 dumpFileName.AssignLiteral("new-");
michael@0 90 PRExplodedTime expTime;
michael@0 91 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &expTime);
michael@0 92 char time[128];
michael@0 93 PR_FormatTimeUSEnglish(time, 128, "%Y-%m-%d-%H%M%S.txt", &expTime);
michael@0 94 dumpFileName += time;
michael@0 95 rv = file->AppendNative(dumpFileName);
michael@0 96 if (NS_FAILED(rv)) return rv;
michael@0 97
michael@0 98 FILE* out;
michael@0 99 rv = file->OpenANSIFileDesc("w", &out);
michael@0 100 if (NS_FAILED(rv)) return rv;
michael@0 101
michael@0 102 rv = nsTraceRefcnt::DumpStatistics(statType, out);
michael@0 103 ::fclose(out);
michael@0 104 if (NS_FAILED(rv)) return rv;
michael@0 105
michael@0 106 rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), file);
michael@0 107 if (NS_FAILED(rv)) return rv;
michael@0 108 }
michael@0 109
michael@0 110 nsIChannel* channel = nullptr;
michael@0 111 rv = NS_NewInputStreamChannel(&channel, aURI, inStr,
michael@0 112 NS_LITERAL_CSTRING("text/plain"),
michael@0 113 NS_LITERAL_CSTRING("utf-8"));
michael@0 114 if (NS_FAILED(rv)) return rv;
michael@0 115
michael@0 116 *result = channel;
michael@0 117 return rv;
michael@0 118 }
michael@0 119
michael@0 120 NS_IMETHODIMP
michael@0 121 nsAboutBloat::GetURIFlags(nsIURI *aURI, uint32_t *result)
michael@0 122 {
michael@0 123 *result = 0;
michael@0 124 return NS_OK;
michael@0 125 }
michael@0 126
michael@0 127 nsresult
michael@0 128 nsAboutBloat::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
michael@0 129 {
michael@0 130 nsAboutBloat* about = new nsAboutBloat();
michael@0 131 if (about == nullptr)
michael@0 132 return NS_ERROR_OUT_OF_MEMORY;
michael@0 133 NS_ADDREF(about);
michael@0 134 nsresult rv = about->QueryInterface(aIID, aResult);
michael@0 135 NS_RELEASE(about);
michael@0 136 return rv;
michael@0 137 }
michael@0 138
michael@0 139 ////////////////////////////////////////////////////////////////////////////////
michael@0 140 #endif /* NS_BUILD_REFCNT_LOGGING */

mercurial