2014-03-20 02:52:11 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsStatusReporterManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsArrayEnumerator.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsDumpUtils.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#define getpid _getpid
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 03:24:00 +00:00
|
|
|
#ifdef XP_UNIX
|
2014-03-20 02:52:11 +00:00
|
|
|
#define DO_STATUS_REPORT 1
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 03:24:00 +00:00
|
|
|
#ifdef DO_STATUS_REPORT // {
|
2014-03-20 02:52:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DumpStatusInfoToTempDirRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DumpStatusInfoToTempDirRunnable()
|
2014-05-13 17:41:38 +00:00
|
|
|
{
|
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStatusReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/status-reporter-manager;1");
|
|
|
|
mgr->DumpReports();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
void
|
|
|
|
doStatusReport(const nsCString& aInputStr)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
LOG("FifoWatcher(%s) dispatching status report runnable.", aInputStr.get());
|
2014-03-20 02:52:11 +00:00
|
|
|
nsRefPtr<DumpStatusInfoToTempDirRunnable> runnable =
|
|
|
|
new DumpStatusInfoToTempDirRunnable();
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
} //anonymous namespace
|
|
|
|
#endif // DO_STATUS_REPORT }
|
|
|
|
|
|
|
|
static bool gStatusReportProgress = 0;
|
|
|
|
static int gNumReporters = 0;
|
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
nsresult
|
|
|
|
getStatus(nsACString& aDesc)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
if (!gStatusReportProgress) {
|
|
|
|
aDesc.AssignLiteral("Init");
|
|
|
|
} else {
|
|
|
|
aDesc.AssignLiteral("Running: There are ");
|
|
|
|
aDesc.AppendInt(gNumReporters);
|
|
|
|
aDesc.AppendLiteral(" reporters");
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_STATUS_REPORTER_IMPLEMENT(StatusReporter, "StatusReporter State", getStatus)
|
|
|
|
|
|
|
|
#define DUMP(o, s) \
|
2014-05-05 17:30:39 +00:00
|
|
|
do { \
|
|
|
|
const char* s2 = (s); \
|
|
|
|
uint32_t dummy; \
|
|
|
|
nsresult rv = (o)->Write((s2), strlen(s2), &dummy); \
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) \
|
|
|
|
return rv; \
|
|
|
|
} while (0)
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
static nsresult
|
|
|
|
DumpReport(nsIFileOutputStream* aOStream, const nsCString& aProcess,
|
|
|
|
const nsCString& aName, const nsCString& aDescription)
|
|
|
|
{
|
|
|
|
int pid;
|
|
|
|
if (aProcess.IsEmpty()) {
|
|
|
|
pid = getpid();
|
|
|
|
nsPrintfCString pidStr("PID %u", pid);
|
2014-04-28 06:20:41 +00:00
|
|
|
DUMP(aOStream, "\n {\n \"Process\": \"");
|
2014-03-20 02:52:11 +00:00
|
|
|
DUMP(aOStream, pidStr.get());
|
|
|
|
} else {
|
|
|
|
pid = 0;
|
2014-04-28 06:20:41 +00:00
|
|
|
DUMP(aOStream, "\n { \"Unknown Process\": \"");
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 06:20:41 +00:00
|
|
|
DUMP(aOStream, "\",\n \"Reporter name\": \"");
|
2014-03-20 02:52:11 +00:00
|
|
|
DUMP(aOStream, aName.get());
|
|
|
|
|
2014-04-28 06:20:41 +00:00
|
|
|
DUMP(aOStream, "\",\n \"Status Description\": [\"");
|
|
|
|
nsCString desc = aDescription;
|
|
|
|
desc.ReplaceSubstring("|", "\",\"");
|
|
|
|
DUMP(aOStream, desc.get());
|
|
|
|
|
|
|
|
DUMP(aOStream, "\"]\n }");
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** nsStatusReporterManager implementation
|
|
|
|
**/
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsStatusReporterManager, nsIStatusReporterManager)
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
nsStatusReporterManager::nsStatusReporterManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStatusReporterManager::~nsStatusReporterManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStatusReporterManager::Init()
|
|
|
|
{
|
|
|
|
RegisterReporter(new NS_STATUS_REPORTER_NAME(StatusReporter));
|
|
|
|
gStatusReportProgress = 1;
|
|
|
|
|
2014-05-29 03:24:00 +00:00
|
|
|
#ifdef DO_STATUS_REPORT
|
2014-03-20 02:52:11 +00:00
|
|
|
if (FifoWatcher::MaybeCreate()) {
|
|
|
|
FifoWatcher* fw = FifoWatcher::GetSingleton();
|
2014-05-13 17:41:38 +00:00
|
|
|
fw->RegisterCallback(NS_LITERAL_CSTRING("status report"), doStatusReport);
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStatusReporterManager::DumpReports()
|
|
|
|
{
|
|
|
|
static unsigned number = 1;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCString filename("status-reports-");
|
|
|
|
filename.AppendInt(getpid());
|
2014-05-22 03:48:51 +00:00
|
|
|
filename.Append('-');
|
2014-03-20 02:52:11 +00:00
|
|
|
filename.AppendInt(number++);
|
|
|
|
filename.AppendLiteral(".json");
|
|
|
|
|
|
|
|
// Open a file in NS_OS_TEMP_DIR for writing.
|
|
|
|
// The file is initialized as "incomplete-status-reports-pid-number.json" in the
|
|
|
|
// begining, it will be rename as "status-reports-pid-number.json" in the end.
|
|
|
|
nsCOMPtr<nsIFile> tmpFile;
|
|
|
|
rv = nsDumpUtils::OpenTempFile(NS_LITERAL_CSTRING("incomplete-") +
|
2014-05-05 17:30:39 +00:00
|
|
|
filename,
|
|
|
|
getter_AddRefs(tmpFile),
|
|
|
|
NS_LITERAL_CSTRING("status-reports"));
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFileOutputStream> ostream =
|
|
|
|
do_CreateInstance("@mozilla.org/network/file-output-stream;1");
|
|
|
|
rv = ostream->Init(tmpFile, -1, -1, 0);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
//Write the reports to the file
|
|
|
|
|
2014-04-28 06:20:41 +00:00
|
|
|
DUMP(ostream, "{\n\"subject\":\"about:service reports\",\n");
|
|
|
|
DUMP(ostream, "\"reporters\": [ ");
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
2014-04-28 06:20:41 +00:00
|
|
|
bool more, first = true;
|
2014-03-20 02:52:11 +00:00
|
|
|
EnumerateReporters(getter_AddRefs(e));
|
|
|
|
while (NS_SUCCEEDED(e->HasMoreElements(&more)) && more) {
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
e->GetNext(getter_AddRefs(supports));
|
|
|
|
nsCOMPtr<nsIStatusReporter> r = do_QueryInterface(supports);
|
|
|
|
|
|
|
|
nsCString process;
|
|
|
|
rv = r->GetProcess(process);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
nsCString name;
|
|
|
|
rv = r->GetName(name);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
nsCString description;
|
|
|
|
rv = r->GetDescription(description);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
2014-04-28 06:20:41 +00:00
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
DUMP(ostream, ",");
|
|
|
|
}
|
|
|
|
|
2014-03-20 02:52:11 +00:00
|
|
|
rv = DumpReport(ostream, process, name, description);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
2014-04-28 06:20:41 +00:00
|
|
|
DUMP(ostream, "\n]\n}\n");
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
rv = ostream->Close();
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
// Rename the status reports file
|
|
|
|
nsCOMPtr<nsIFile> srFinalFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(srFinalFile));
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
rv = srFinalFile->AppendNative(NS_LITERAL_CSTRING("status-reports"));
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = srFinalFile->AppendNative(filename);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
rv = srFinalFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
nsAutoString srActualFinalFilename;
|
|
|
|
rv = srFinalFile->GetLeafName(srActualFinalFilename);
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
rv = tmpFile->MoveTo(/* directory */ nullptr, srActualFinalFilename);
|
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return rv;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 17:41:38 +00:00
|
|
|
nsStatusReporterManager::EnumerateReporters(nsISimpleEnumerator** aResult)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
return NS_NewArrayEnumerator(aResult, mReporters);
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 17:41:38 +00:00
|
|
|
nsStatusReporterManager::RegisterReporter(nsIStatusReporter* aReporter)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
if (mReporters.IndexOf(aReporter) != -1) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
mReporters.AppendObject(aReporter);
|
2014-03-20 02:52:11 +00:00
|
|
|
gNumReporters++;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 17:41:38 +00:00
|
|
|
nsStatusReporterManager::UnregisterReporter(nsIStatusReporter* aReporter)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
if (!mReporters.RemoveObject(aReporter)) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
gNumReporters--;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-13 17:41:38 +00:00
|
|
|
NS_RegisterStatusReporter(nsIStatusReporter* aReporter)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStatusReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/status-reporter-manager;1");
|
2014-05-13 17:41:38 +00:00
|
|
|
if (!mgr) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
|
|
|
return mgr->RegisterReporter(aReporter);
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-13 17:41:38 +00:00
|
|
|
NS_UnregisterStatusReporter(nsIStatusReporter* aReporter)
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStatusReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/status-reporter-manager;1");
|
2014-05-13 17:41:38 +00:00
|
|
|
if (!mgr) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
|
|
|
return mgr->UnregisterReporter(aReporter);
|
2014-03-20 02:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-13 17:41:38 +00:00
|
|
|
NS_DumpStatusReporter()
|
2014-03-20 02:52:11 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStatusReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/status-reporter-manager;1");
|
2014-05-13 17:41:38 +00:00
|
|
|
if (!mgr) {
|
2014-03-20 02:52:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 17:41:38 +00:00
|
|
|
}
|
2014-03-20 02:52:11 +00:00
|
|
|
return mgr->DumpReports();
|
|
|
|
}
|