xpcom/base/nsStatusReporterManager.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsStatusReporterManager.h"
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "nsDirectoryServiceDefs.h"
michael@0 10 #include "nsArrayEnumerator.h"
michael@0 11 #include "nsISimpleEnumerator.h"
michael@0 12 #include "nsIFile.h"
michael@0 13 #include "nsDumpUtils.h"
michael@0 14 #include "nsIFileStreams.h"
michael@0 15 #include "nsPrintfCString.h"
michael@0 16
michael@0 17 #ifdef XP_WIN
michael@0 18 #include <process.h>
michael@0 19 #define getpid _getpid
michael@0 20 #else
michael@0 21 #include <unistd.h>
michael@0 22 #endif
michael@0 23
michael@0 24 #ifdef XP_UNIX
michael@0 25 #define DO_STATUS_REPORT 1
michael@0 26 #endif
michael@0 27
michael@0 28 #ifdef DO_STATUS_REPORT // {
michael@0 29 namespace {
michael@0 30
michael@0 31 class DumpStatusInfoToTempDirRunnable : public nsRunnable
michael@0 32 {
michael@0 33 public:
michael@0 34 DumpStatusInfoToTempDirRunnable()
michael@0 35 {}
michael@0 36
michael@0 37 NS_IMETHOD Run()
michael@0 38 {
michael@0 39 nsCOMPtr<nsIStatusReporterManager> mgr =
michael@0 40 do_GetService("@mozilla.org/status-reporter-manager;1");
michael@0 41 mgr->DumpReports();
michael@0 42 return NS_OK;
michael@0 43 }
michael@0 44 };
michael@0 45
michael@0 46 void doStatusReport(const nsCString& inputStr)
michael@0 47 {
michael@0 48 LOG("FifoWatcher(%s) dispatching status report runnable.", inputStr.get());
michael@0 49 nsRefPtr<DumpStatusInfoToTempDirRunnable> runnable =
michael@0 50 new DumpStatusInfoToTempDirRunnable();
michael@0 51 NS_DispatchToMainThread(runnable);
michael@0 52 }
michael@0 53
michael@0 54 } //anonymous namespace
michael@0 55 #endif // DO_STATUS_REPORT }
michael@0 56
michael@0 57 static bool gStatusReportProgress = 0;
michael@0 58 static int gNumReporters = 0;
michael@0 59
michael@0 60 nsresult getStatus(nsACString& desc)
michael@0 61 {
michael@0 62 if(!gStatusReportProgress)
michael@0 63 desc.AssignLiteral("Init");
michael@0 64 else {
michael@0 65 desc.AssignLiteral("Running:\nThere are ");
michael@0 66 desc.AppendInt(gNumReporters);
michael@0 67 desc.AppendLiteral(" reporters");
michael@0 68 }
michael@0 69 return NS_OK;
michael@0 70 }
michael@0 71
michael@0 72 NS_STATUS_REPORTER_IMPLEMENT(StatusReporter, "StatusReporter State", getStatus)
michael@0 73
michael@0 74 #define DUMP(o, s) \
michael@0 75 do { \
michael@0 76 const char* s2 = (s); \
michael@0 77 uint32_t dummy; \
michael@0 78 nsresult rv = (o)->Write((s2), strlen(s2), &dummy); \
michael@0 79 if (NS_WARN_IF(NS_FAILED(rv))) \
michael@0 80 return rv; \
michael@0 81 } while (0)
michael@0 82
michael@0 83 static nsresult
michael@0 84 DumpReport(nsIFileOutputStream* aOStream, const nsCString& aProcess,
michael@0 85 const nsCString& aName, const nsCString& aDescription)
michael@0 86 {
michael@0 87 int pid;
michael@0 88 if (aProcess.IsEmpty()) {
michael@0 89 pid = getpid();
michael@0 90 nsPrintfCString pidStr("PID %u", pid);
michael@0 91 DUMP(aOStream, "\n {\"Process\": \"");
michael@0 92 DUMP(aOStream, pidStr.get());
michael@0 93 } else {
michael@0 94 pid = 0;
michael@0 95 DUMP(aOStream, "\n {\"Unknown Process\": \"");
michael@0 96 }
michael@0 97
michael@0 98 DUMP(aOStream, "\", \"Reporter name\": \"");
michael@0 99 DUMP(aOStream, aName.get());
michael@0 100
michael@0 101 DUMP(aOStream, "\", \"Status Description\": \"");
michael@0 102 DUMP(aOStream, aDescription.get());
michael@0 103 DUMP(aOStream, "\"}");
michael@0 104
michael@0 105 return NS_OK;
michael@0 106 }
michael@0 107
michael@0 108 /**
michael@0 109 ** nsStatusReporterManager implementation
michael@0 110 **/
michael@0 111
michael@0 112 NS_IMPL_ISUPPORTS(nsStatusReporterManager, nsIStatusReporterManager)
michael@0 113
michael@0 114 nsStatusReporterManager::nsStatusReporterManager()
michael@0 115 {
michael@0 116 }
michael@0 117
michael@0 118 nsStatusReporterManager::~nsStatusReporterManager()
michael@0 119 {
michael@0 120 }
michael@0 121
michael@0 122 NS_IMETHODIMP
michael@0 123 nsStatusReporterManager::Init()
michael@0 124 {
michael@0 125 RegisterReporter(new NS_STATUS_REPORTER_NAME(StatusReporter));
michael@0 126 gStatusReportProgress = 1;
michael@0 127
michael@0 128 #ifdef DO_STATUS_REPORT
michael@0 129 if (FifoWatcher::MaybeCreate()) {
michael@0 130 FifoWatcher* fw = FifoWatcher::GetSingleton();
michael@0 131 fw->RegisterCallback(NS_LITERAL_CSTRING("status report"),doStatusReport);
michael@0 132 }
michael@0 133 #endif
michael@0 134
michael@0 135 return NS_OK;
michael@0 136 }
michael@0 137
michael@0 138 NS_IMETHODIMP
michael@0 139 nsStatusReporterManager::DumpReports()
michael@0 140 {
michael@0 141 static unsigned number = 1;
michael@0 142 nsresult rv;
michael@0 143
michael@0 144 nsCString filename("status-reports-");
michael@0 145 filename.AppendInt(getpid());
michael@0 146 filename.AppendLiteral("-");
michael@0 147 filename.AppendInt(number++);
michael@0 148 filename.AppendLiteral(".json");
michael@0 149
michael@0 150 // Open a file in NS_OS_TEMP_DIR for writing.
michael@0 151 // The file is initialized as "incomplete-status-reports-pid-number.json" in the
michael@0 152 // begining, it will be rename as "status-reports-pid-number.json" in the end.
michael@0 153 nsCOMPtr<nsIFile> tmpFile;
michael@0 154 rv = nsDumpUtils::OpenTempFile(NS_LITERAL_CSTRING("incomplete-") +
michael@0 155 filename,
michael@0 156 getter_AddRefs(tmpFile),
michael@0 157 NS_LITERAL_CSTRING("status-reports"));
michael@0 158 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 159 return rv;
michael@0 160
michael@0 161 nsCOMPtr<nsIFileOutputStream> ostream =
michael@0 162 do_CreateInstance("@mozilla.org/network/file-output-stream;1");
michael@0 163 rv = ostream->Init(tmpFile, -1, -1, 0);
michael@0 164 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 165 return rv;
michael@0 166
michael@0 167 //Write the reports to the file
michael@0 168
michael@0 169 DUMP(ostream, " [Sysdump Report Start]: ");
michael@0 170
michael@0 171 nsCOMPtr<nsISimpleEnumerator> e;
michael@0 172 bool more;
michael@0 173 EnumerateReporters(getter_AddRefs(e));
michael@0 174 while (NS_SUCCEEDED(e->HasMoreElements(&more)) && more) {
michael@0 175 nsCOMPtr<nsISupports> supports;
michael@0 176 e->GetNext(getter_AddRefs(supports));
michael@0 177 nsCOMPtr<nsIStatusReporter> r = do_QueryInterface(supports);
michael@0 178
michael@0 179 nsCString process;
michael@0 180 rv = r->GetProcess(process);
michael@0 181 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 182 return rv;
michael@0 183
michael@0 184 nsCString name;
michael@0 185 rv = r->GetName(name);
michael@0 186 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 187 return rv;
michael@0 188
michael@0 189 nsCString description;
michael@0 190 rv = r->GetDescription(description);
michael@0 191 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 192 return rv;
michael@0 193
michael@0 194 rv = DumpReport(ostream, process, name, description);
michael@0 195 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 196 return rv;
michael@0 197 }
michael@0 198
michael@0 199 DUMP(ostream, "\n [Sysdump Report End] ");
michael@0 200
michael@0 201 rv = ostream->Close();
michael@0 202 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 203 return rv;
michael@0 204
michael@0 205 // Rename the status reports file
michael@0 206 nsCOMPtr<nsIFile> srFinalFile;
michael@0 207 rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(srFinalFile));
michael@0 208 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 209 return rv;
michael@0 210
michael@0 211 #ifdef ANDROID
michael@0 212 rv = srFinalFile->AppendNative(NS_LITERAL_CSTRING("status-reports"));
michael@0 213 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 214 return rv;
michael@0 215 #endif
michael@0 216
michael@0 217 rv = srFinalFile->AppendNative(filename);
michael@0 218 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 219 return rv;
michael@0 220
michael@0 221 rv = srFinalFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
michael@0 222 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 223 return rv;
michael@0 224
michael@0 225 nsAutoString srActualFinalFilename;
michael@0 226 rv = srFinalFile->GetLeafName(srActualFinalFilename);
michael@0 227 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 228 return rv;
michael@0 229
michael@0 230 rv = tmpFile->MoveTo(/* directory */ nullptr, srActualFinalFilename);
michael@0 231
michael@0 232 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 233 return rv;
michael@0 234
michael@0 235 return NS_OK;
michael@0 236 }
michael@0 237
michael@0 238 NS_IMETHODIMP
michael@0 239 nsStatusReporterManager::EnumerateReporters(nsISimpleEnumerator** result)
michael@0 240 {
michael@0 241 return NS_NewArrayEnumerator(result, mReporters);
michael@0 242 }
michael@0 243
michael@0 244 NS_IMETHODIMP
michael@0 245 nsStatusReporterManager::RegisterReporter(nsIStatusReporter* reporter)
michael@0 246 {
michael@0 247 if (mReporters.IndexOf(reporter) != -1)
michael@0 248 return NS_ERROR_FAILURE;
michael@0 249
michael@0 250 mReporters.AppendObject(reporter);
michael@0 251 gNumReporters++;
michael@0 252 return NS_OK;
michael@0 253 }
michael@0 254
michael@0 255 NS_IMETHODIMP
michael@0 256 nsStatusReporterManager::UnregisterReporter(nsIStatusReporter* reporter)
michael@0 257 {
michael@0 258 if (!mReporters.RemoveObject(reporter))
michael@0 259 return NS_ERROR_FAILURE;
michael@0 260 gNumReporters--;
michael@0 261 return NS_OK;
michael@0 262 }
michael@0 263
michael@0 264 nsresult
michael@0 265 NS_RegisterStatusReporter (nsIStatusReporter* reporter)
michael@0 266 {
michael@0 267 nsCOMPtr<nsIStatusReporterManager> mgr =
michael@0 268 do_GetService("@mozilla.org/status-reporter-manager;1");
michael@0 269 if (mgr == nullptr)
michael@0 270 return NS_ERROR_FAILURE;
michael@0 271 return mgr->RegisterReporter(reporter);
michael@0 272 }
michael@0 273
michael@0 274 nsresult
michael@0 275 NS_UnregisterStatusReporter (nsIStatusReporter* reporter)
michael@0 276 {
michael@0 277 nsCOMPtr<nsIStatusReporterManager> mgr =
michael@0 278 do_GetService("@mozilla.org/status-reporter-manager;1");
michael@0 279 if (mgr == nullptr)
michael@0 280 return NS_ERROR_FAILURE;
michael@0 281 return mgr->UnregisterReporter(reporter);
michael@0 282 }
michael@0 283
michael@0 284 nsresult
michael@0 285 NS_DumpStatusReporter ()
michael@0 286 {
michael@0 287 nsCOMPtr<nsIStatusReporterManager> mgr =
michael@0 288 do_GetService("@mozilla.org/status-reporter-manager;1");
michael@0 289 if (mgr == nullptr)
michael@0 290 return NS_ERROR_FAILURE;
michael@0 291 return mgr->DumpReports();
michael@0 292 }

mercurial