2014-05-05 21:30:39 +04: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: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2000-04-18 06:34:54 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Maintains a circular buffer of recent messages, and notifies
|
|
|
|
* listeners when new messages are logged.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Threadsafe. */
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
2009-04-14 12:02:58 +04:00
|
|
|
#include "nsCOMArray.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2000-04-18 06:34:54 +04:00
|
|
|
|
|
|
|
#include "nsConsoleService.h"
|
|
|
|
#include "nsConsoleMessage.h"
|
2006-03-15 07:59:42 +03:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsIConsoleListener.h"
|
2017-01-23 17:24:01 +03:00
|
|
|
#include "nsIObserverService.h"
|
2013-10-04 21:16:53 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2015-07-21 17:50:09 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2015-07-09 13:49:00 +03:00
|
|
|
#include "nsIScriptError.h"
|
2015-07-31 16:49:27 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2012-11-07 10:34:33 +04:00
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
2017-01-23 17:24:01 +03:00
|
|
|
#include "mozilla/Services.h"
|
2017-01-20 02:44:43 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2014-02-12 01:01:29 +04:00
|
|
|
#if defined(ANDROID)
|
|
|
|
# include <android/log.h>
|
2014-09-10 03:52:08 +04:00
|
|
|
# include "mozilla/dom/ContentChild.h"
|
2014-02-12 01:01:29 +04:00
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2014-11-20 05:57:27 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
# include "GeckoTaskTracer.h"
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_IMPL_ADDREF(nsConsoleService)
|
|
|
|
NS_IMPL_RELEASE(nsConsoleService)
|
2014-08-25 23:17:15 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsConsoleService, nullptr,
|
|
|
|
nsIClassInfo::THREADSAFE | nsIClassInfo::SINGLETON,
|
|
|
|
NS_CONSOLESERVICE_CID)
|
2015-07-31 16:49:27 +03:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsConsoleService, nsIConsoleService, nsIObserver)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsConsoleService, nsIConsoleService, nsIObserver)
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2017-11-07 23:54:13 +03:00
|
|
|
static const bool gLoggingEnabled = true;
|
|
|
|
static const bool gLoggingBuffered = true;
|
2018-01-16 13:27:00 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
static bool gLoggingToDebugger = true;
|
|
|
|
#endif // XP_WIN
|
2015-05-29 16:58:04 +03:00
|
|
|
#if defined(ANDROID)
|
2017-11-07 23:54:13 +03:00
|
|
|
static bool gLoggingLogcat = false;
|
2015-05-29 16:58:04 +03:00
|
|
|
#endif // defined(ANDROID)
|
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
nsConsoleService::MessageElement::~MessageElement() {}
|
2012-11-07 10:34:33 +04:00
|
|
|
|
2000-04-18 06:34:54 +04:00
|
|
|
nsConsoleService::nsConsoleService()
|
2015-08-18 04:05:48 +03:00
|
|
|
: mCurrentSize(0),
|
2014-05-05 21:30:39 +04:00
|
|
|
mDeliveringMessage(false),
|
|
|
|
mLock("nsConsoleService.mLock") {
|
|
|
|
// XXX grab this from a pref!
|
|
|
|
// hm, but worry about circularity, bc we want to be able to report
|
|
|
|
// prefs errs...
|
2015-08-18 04:05:48 +03:00
|
|
|
mMaximumSize = 250;
|
2018-01-16 13:27:00 +03:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// This environment variable controls whether the console service
|
|
|
|
// should be prevented from putting output to the attached debugger.
|
|
|
|
// It only affects the Windows platform.
|
|
|
|
//
|
|
|
|
// To disable OutputDebugString, set:
|
|
|
|
// MOZ_CONSOLESERVICE_DISABLE_DEBUGGER_OUTPUT=1
|
|
|
|
//
|
|
|
|
const char* disableDebugLoggingVar =
|
|
|
|
getenv("MOZ_CONSOLESERVICE_DISABLE_DEBUGGER_OUTPUT");
|
|
|
|
gLoggingToDebugger =
|
|
|
|
!disableDebugLoggingVar || (disableDebugLoggingVar[0] == '0');
|
|
|
|
#endif // XP_WIN
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
|
|
|
|
2015-07-09 13:49:00 +03:00
|
|
|
void nsConsoleService::ClearMessagesForWindowID(const uint64_t innerID) {
|
2015-07-21 17:50:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
2016-03-21 22:15:42 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2015-07-21 17:50:09 +03:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
for (MessageElement* e = mMessages.getFirst(); e != nullptr;) {
|
|
|
|
// Only messages implementing nsIScriptError interface expose the
|
|
|
|
// inner window ID.
|
|
|
|
nsCOMPtr<nsIScriptError> scriptError = do_QueryInterface(e->Get());
|
2015-07-09 13:49:00 +03:00
|
|
|
if (!scriptError) {
|
2015-08-18 04:05:48 +03:00
|
|
|
e = e->getNext();
|
2015-07-09 13:49:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint64_t innerWindowID;
|
|
|
|
nsresult rv = scriptError->GetInnerWindowID(&innerWindowID);
|
|
|
|
if (NS_FAILED(rv) || innerWindowID != innerID) {
|
2015-08-18 04:05:48 +03:00
|
|
|
e = e->getNext();
|
2015-07-09 13:49:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
MessageElement* next = e->getNext();
|
|
|
|
e->remove();
|
|
|
|
delete e;
|
|
|
|
mCurrentSize--;
|
|
|
|
MOZ_ASSERT(mCurrentSize < mMaximumSize);
|
2015-07-09 13:49:00 +03:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
e = next;
|
|
|
|
}
|
|
|
|
}
|
2015-07-09 13:49:00 +03:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
void nsConsoleService::ClearMessages() {
|
2016-03-21 22:15:42 +03:00
|
|
|
// NB: A lock is not required here as it's only called from |Reset| which
|
|
|
|
// locks for us and from the dtor.
|
2015-08-18 04:05:48 +03:00
|
|
|
while (!mMessages.isEmpty()) {
|
|
|
|
MessageElement* e = mMessages.popFirst();
|
|
|
|
delete e;
|
2015-07-09 13:49:00 +03:00
|
|
|
}
|
2015-08-18 04:05:48 +03:00
|
|
|
mCurrentSize = 0;
|
2015-07-09 13:49:00 +03:00
|
|
|
}
|
|
|
|
|
2000-04-18 06:34:54 +04:00
|
|
|
nsConsoleService::~nsConsoleService() {
|
2015-07-21 17:50:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
ClearMessages();
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class AddConsolePrefWatchers : public Runnable {
|
2012-11-07 10:34:33 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit AddConsolePrefWatchers(nsConsoleService* aConsole)
|
|
|
|
: mozilla::Runnable("AddConsolePrefWatchers"), mConsole(aConsole) {}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-05-29 16:58:04 +03:00
|
|
|
#if defined(ANDROID)
|
2017-11-07 23:54:13 +03:00
|
|
|
Preferences::AddBoolVarCache(&gLoggingLogcat, "consoleservice.logcat",
|
|
|
|
# ifdef RELEASE_OR_BETA
|
|
|
|
false
|
|
|
|
# else
|
|
|
|
true
|
|
|
|
# endif
|
|
|
|
);
|
2015-05-29 16:58:04 +03:00
|
|
|
#endif // defined(ANDROID)
|
2015-07-31 16:49:27 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
MOZ_ASSERT(obs);
|
|
|
|
obs->AddObserver(mConsole, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
obs->AddObserver(mConsole, "inner-window-destroyed", false);
|
|
|
|
|
2017-11-07 23:54:13 +03:00
|
|
|
if (!gLoggingBuffered) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mConsole->Reset();
|
2012-11-07 10:34:33 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-14 09:55:25 +04:00
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsConsoleService> mConsole;
|
2012-11-07 10:34:33 +04:00
|
|
|
};
|
|
|
|
|
2005-03-02 19:37:53 +03:00
|
|
|
nsresult nsConsoleService::Init() {
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_DispatchToMainThread(new AddConsolePrefWatchers(this));
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2005-03-02 19:37:53 +03:00
|
|
|
}
|
|
|
|
|
2012-01-11 20:28:21 +04:00
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class LogMessageRunnable : public Runnable {
|
2012-01-11 20:28:21 +04:00
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
LogMessageRunnable(nsIConsoleMessage* aMessage, nsConsoleService* aService)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("LogMessageRunnable"),
|
|
|
|
mMessage(aMessage),
|
2014-05-13 21:41:38 +04:00
|
|
|
mService(aService) {}
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2012-01-11 20:28:21 +04:00
|
|
|
|
|
|
|
private:
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCOMPtr<nsIConsoleMessage> mMessage;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsConsoleService> mService;
|
2012-01-11 20:28:21 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LogMessageRunnable::Run() {
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
// Snapshot of listeners so that we don't reenter this hash during
|
|
|
|
// enumeration.
|
|
|
|
nsCOMArray<nsIConsoleListener> listeners;
|
2015-07-10 02:54:59 +03:00
|
|
|
mService->CollectCurrentListeners(listeners);
|
2013-01-29 20:02:56 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mService->SetIsDelivering();
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
for (int32_t i = 0; i < listeners.Count(); ++i) {
|
2014-05-05 21:30:39 +04:00
|
|
|
listeners[i]->Observe(mMessage);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mService->SetDoneDelivering();
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2012-01-11 20:28:21 +04:00
|
|
|
}
|
2001-10-26 08:51:08 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2000-04-18 06:34:54 +04:00
|
|
|
// nsIConsoleService methods
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsConsoleService::LogMessage(nsIConsoleMessage* aMessage) {
|
|
|
|
return LogMessageWithMode(aMessage, OutputToLog);
|
2012-11-09 21:52:09 +04:00
|
|
|
}
|
|
|
|
|
2015-07-21 17:50:09 +03:00
|
|
|
// This can be called off the main thread.
|
2014-05-13 21:41:38 +04:00
|
|
|
nsresult nsConsoleService::LogMessageWithMode(
|
|
|
|
nsIConsoleMessage* aMessage, nsConsoleService::OutputMode aOutputMode) {
|
|
|
|
if (!aMessage) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2017-11-07 23:54:13 +03:00
|
|
|
if (!gLoggingEnabled) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread() && mDeliveringMessage) {
|
|
|
|
nsCString msg;
|
2014-05-13 21:41:38 +04:00
|
|
|
aMessage->ToString(msg);
|
2018-11-29 00:06:53 +03:00
|
|
|
NS_WARNING(
|
|
|
|
nsPrintfCString(
|
|
|
|
"Reentrancy error: some client attempted to display a message to "
|
|
|
|
"the console while in a console listener. The following message "
|
|
|
|
"was discarded: \"%s\"",
|
|
|
|
msg.get())
|
|
|
|
.get());
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<LogMessageRunnable> r;
|
2015-08-18 04:05:48 +03:00
|
|
|
nsCOMPtr<nsIConsoleMessage> retiredMessage;
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock while updating buffer, and while taking snapshot of
|
|
|
|
* listeners array.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2012-11-07 10:34:33 +04:00
|
|
|
|
2015-05-29 16:58:04 +03:00
|
|
|
#if defined(ANDROID)
|
2017-11-07 23:54:13 +03:00
|
|
|
if (gLoggingLogcat && aOutputMode == OutputToLog) {
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCString msg;
|
2014-05-13 21:41:38 +04:00
|
|
|
aMessage->ToString(msg);
|
2014-09-10 03:52:08 +04:00
|
|
|
|
|
|
|
/** Attempt to use the process name as the log tag. */
|
|
|
|
mozilla::dom::ContentChild* child =
|
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
nsCString appName;
|
|
|
|
if (child) {
|
|
|
|
child->GetProcessName(appName);
|
|
|
|
} else {
|
|
|
|
appName = "GeckoConsole";
|
|
|
|
}
|
|
|
|
|
2014-09-23 02:33:36 +04:00
|
|
|
uint32_t logLevel = 0;
|
|
|
|
aMessage->GetLogLevel(&logLevel);
|
|
|
|
|
|
|
|
android_LogPriority logPriority = ANDROID_LOG_INFO;
|
|
|
|
switch (logLevel) {
|
|
|
|
case nsIConsoleMessage::debug:
|
|
|
|
logPriority = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case nsIConsoleMessage::info:
|
|
|
|
logPriority = ANDROID_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case nsIConsoleMessage::warn:
|
|
|
|
logPriority = ANDROID_LOG_WARN;
|
|
|
|
break;
|
|
|
|
case nsIConsoleMessage::error:
|
|
|
|
logPriority = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
__android_log_print(logPriority, appName.get(), "%s", msg.get());
|
2012-01-11 20:28:21 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
2018-01-16 13:27:00 +03:00
|
|
|
if (gLoggingToDebugger && IsDebuggerPresent()) {
|
2014-05-05 21:30:39 +04:00
|
|
|
nsString msg;
|
2018-09-05 06:31:42 +03:00
|
|
|
aMessage->GetMessageMoz(msg);
|
2014-05-22 07:48:51 +04:00
|
|
|
msg.Append('\n');
|
2014-05-05 21:30:39 +04:00
|
|
|
OutputDebugStringW(msg.get());
|
2013-03-14 09:55:25 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
#endif
|
2014-11-20 05:57:27 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
2017-03-22 23:49:00 +03:00
|
|
|
if (IsStartLogging()) {
|
2014-11-20 05:57:27 +03:00
|
|
|
nsCString msg;
|
|
|
|
aMessage->ToString(msg);
|
|
|
|
int prefixPos = msg.Find(GetJSLabelPrefix());
|
|
|
|
if (prefixPos >= 0) {
|
|
|
|
nsDependentCSubstring submsg(msg, prefixPos);
|
|
|
|
AddLabel("%s", submsg.BeginReading());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2017-11-07 23:54:13 +03:00
|
|
|
if (gLoggingBuffered) {
|
2015-08-18 04:05:48 +03:00
|
|
|
MessageElement* e = new MessageElement(aMessage);
|
|
|
|
mMessages.insertBack(e);
|
|
|
|
if (mCurrentSize != mMaximumSize) {
|
|
|
|
mCurrentSize++;
|
|
|
|
} else {
|
|
|
|
MessageElement* p = mMessages.popFirst();
|
|
|
|
MOZ_ASSERT(p);
|
2015-10-03 00:47:13 +03:00
|
|
|
p->swapMessage(retiredMessage);
|
2015-08-18 04:05:48 +03:00
|
|
|
delete p;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 07:12:20 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (mListeners.Count() > 0) {
|
2014-05-13 21:41:38 +04:00
|
|
|
r = new LogMessageRunnable(aMessage, this);
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-09-28 20:54:36 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (retiredMessage) {
|
2015-07-21 17:50:09 +03:00
|
|
|
// Release |retiredMessage| on the main thread in case it is an instance of
|
|
|
|
// a mainthread-only class like nsScriptErrorWithStack and we're off the
|
|
|
|
// main thread.
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ReleaseOnMainThreadSystemGroup("nsConsoleService::retiredMessage",
|
|
|
|
retiredMessage.forget());
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (r) {
|
2015-07-10 06:21:46 +03:00
|
|
|
// avoid failing in XPCShell tests
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
if (mainThread) {
|
2017-07-26 11:13:35 +03:00
|
|
|
SystemGroup::Dispatch(TaskCategory::Other, r.forget());
|
2015-07-10 06:21:46 +03:00
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
|
|
|
|
2015-07-10 02:54:59 +03:00
|
|
|
void nsConsoleService::CollectCurrentListeners(
|
|
|
|
nsCOMArray<nsIConsoleListener>& aListeners) {
|
2014-05-05 21:30:39 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = mListeners.Iter(); !iter.Done(); iter.Next()) {
|
2015-07-20 15:21:28 +03:00
|
|
|
nsIConsoleListener* value = iter.UserData();
|
2015-07-10 02:54:59 +03:00
|
|
|
aListeners.AppendObject(value);
|
|
|
|
}
|
2013-01-29 20:02:56 +04:00
|
|
|
}
|
|
|
|
|
2000-04-18 06:34:54 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsConsoleService::LogStringMessage(const char16_t* aMessage) {
|
2017-11-07 23:54:13 +03:00
|
|
|
if (!gLoggingEnabled) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-07 10:34:33 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsConsoleMessage> msg(new nsConsoleMessage(aMessage));
|
2014-05-05 21:30:39 +04:00
|
|
|
return this->LogMessage(msg);
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-25 23:17:15 +04:00
|
|
|
nsConsoleService::GetMessageArray(uint32_t* aCount,
|
|
|
|
nsIConsoleMessage*** aMessages) {
|
2015-07-21 17:50:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
if (mMessages.isEmpty()) {
|
2000-04-18 06:34:54 +04:00
|
|
|
/*
|
2014-05-05 21:30:39 +04:00
|
|
|
* Make a 1-length output array so that nobody gets confused,
|
|
|
|
* and return a count of 0. This should result in a 0-length
|
|
|
|
* array object when called from script.
|
2000-04-18 06:34:54 +04:00
|
|
|
*/
|
2015-08-18 04:05:48 +03:00
|
|
|
nsIConsoleMessage** messageArray =
|
2015-03-27 03:01:12 +03:00
|
|
|
(nsIConsoleMessage**)moz_xmalloc(sizeof(nsIConsoleMessage*));
|
2014-05-05 21:30:39 +04:00
|
|
|
*messageArray = nullptr;
|
2014-05-13 21:41:38 +04:00
|
|
|
*aMessages = messageArray;
|
|
|
|
*aCount = 0;
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
MOZ_ASSERT(mCurrentSize <= mMaximumSize);
|
|
|
|
nsIConsoleMessage** messageArray = static_cast<nsIConsoleMessage**>(
|
|
|
|
moz_xmalloc(sizeof(nsIConsoleMessage*) * mCurrentSize));
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
uint32_t i = 0;
|
|
|
|
for (MessageElement* e = mMessages.getFirst(); e != nullptr;
|
|
|
|
e = e->getNext()) {
|
|
|
|
nsCOMPtr<nsIConsoleMessage> m = e->Get();
|
|
|
|
m.forget(&messageArray[i]);
|
|
|
|
i++;
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
MOZ_ASSERT(i == mCurrentSize);
|
|
|
|
|
|
|
|
*aCount = i;
|
2014-05-13 21:41:38 +04:00
|
|
|
*aMessages = messageArray;
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsConsoleService::RegisterListener(nsIConsoleListener* aListener) {
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsConsoleService::RegisterListener is main thread only.");
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aListener);
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mListeners.GetWeak(canonical)) {
|
|
|
|
// Reregistering a listener isn't good
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
mListeners.Put(canonical, aListener);
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsConsoleService::UnregisterListener(nsIConsoleListener* aListener) {
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsConsoleService::UnregisterListener is main thread only.");
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCOMPtr<nsISupports> canonical = do_QueryInterface(aListener);
|
2012-01-11 20:28:21 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2000-04-18 06:34:54 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
if (!mListeners.GetWeak(canonical)) {
|
|
|
|
// Unregistering a listener that was never registered?
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mListeners.Remove(canonical);
|
|
|
|
return NS_OK;
|
2000-04-18 06:34:54 +04:00
|
|
|
}
|
2000-10-10 03:45:59 +04:00
|
|
|
|
2006-07-19 16:49:33 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConsoleService::Reset() {
|
2015-07-21 17:50:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
/*
|
|
|
|
* Make sure nobody trips into the buffer while it's being reset
|
|
|
|
*/
|
|
|
|
MutexAutoLock lock(mLock);
|
2006-07-19 16:49:33 +04:00
|
|
|
|
2015-08-18 04:05:48 +03:00
|
|
|
ClearMessages();
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2006-07-19 16:49:33 +04:00
|
|
|
}
|
2015-07-31 16:49:27 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConsoleService::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
// Dump all our messages, in case any are cycle collected.
|
|
|
|
Reset();
|
|
|
|
// We could remove ourselves from the observer service, but it is about to
|
|
|
|
// drop all observers anyways, so why bother.
|
|
|
|
} else if (!strcmp(aTopic, "inner-window-destroyed")) {
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> supportsInt = do_QueryInterface(aSubject);
|
|
|
|
MOZ_ASSERT(supportsInt);
|
|
|
|
|
|
|
|
uint64_t windowId;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(supportsInt->GetData(&windowId));
|
2015-07-31 16:49:27 +03:00
|
|
|
|
|
|
|
ClearMessagesForWindowID(windowId);
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|