Bug 1439686 - Console API should print logs on stdout when used by chrome code and if browser.dom.window.dump.enabled is true, r=bgrins

This commit is contained in:
Andrea Marchesini 2018-02-22 08:49:51 +01:00
parent a26b59fe0b
commit 370acd6950
9 changed files with 32 additions and 23 deletions

View File

@ -7474,10 +7474,10 @@ nsGlobalWindowInner::Orientation(CallerType aCallerType) const
#endif
already_AddRefed<Console>
nsGlobalWindowInner::GetConsole(ErrorResult& aRv)
nsGlobalWindowInner::GetConsole(JSContext* aCx, ErrorResult& aRv)
{
if (!mConsole) {
mConsole = Console::Create(this, aRv);
mConsole = Console::Create(aCx, this, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}

View File

@ -679,7 +679,8 @@ public:
int16_t Orientation(mozilla::dom::CallerType aCallerType) const;
#endif
already_AddRefed<mozilla::dom::Console> GetConsole(mozilla::ErrorResult& aRv);
already_AddRefed<mozilla::dom::Console>
GetConsole(JSContext* aCx, mozilla::ErrorResult& aRv);
// https://w3c.github.io/webappsec-secure-contexts/#dom-window-issecurecontext
bool IsSecureContext() const;

View File

@ -10,6 +10,7 @@
#include "ConsoleCommon.h"
#include "mozilla/dom/BlobBinding.h"
#include "mozilla/dom/DOMPrefs.h"
#include "mozilla/dom/Exceptions.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/FunctionBinding.h"
@ -790,11 +791,11 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Console)
NS_INTERFACE_MAP_END
/* static */ already_AddRefed<Console>
Console::Create(nsPIDOMWindowInner* aWindow, ErrorResult& aRv)
Console::Create(JSContext* aCx, nsPIDOMWindowInner* aWindow, ErrorResult& aRv)
{
MOZ_ASSERT_IF(NS_IsMainThread(), aWindow);
RefPtr<Console> console = new Console(aWindow);
RefPtr<Console> console = new Console(aCx, aWindow);
console->Initialize(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
@ -803,7 +804,7 @@ Console::Create(nsPIDOMWindowInner* aWindow, ErrorResult& aRv)
return console.forget();
}
Console::Console(nsPIDOMWindowInner* aWindow)
Console::Console(JSContext* aCx, nsPIDOMWindowInner* aWindow)
: mWindow(aWindow)
, mOuterID(0)
, mInnerID(0)
@ -825,6 +826,11 @@ Console::Console(nsPIDOMWindowInner* aWindow)
}
}
// Let's enable the dumping to stdout by default for chrome.
if (nsContentUtils::ThreadsafeIsSystemCaller(aCx)) {
mDumpToStdout = DOMPrefs::DumpEnabled();
}
mozilla::HoldJSObjects(this);
}
@ -2420,7 +2426,7 @@ Console::GetConsoleInternal(const GlobalObject& aGlobal, ErrorResult& aRv)
nsCOMPtr<WorkletGlobalScope> workletScope =
do_QueryInterface(aGlobal.GetAsSupports());
if (workletScope) {
return workletScope->GetConsole(aRv);
return workletScope->GetConsole(aGlobal.Context(), aRv);
}
}
@ -2431,7 +2437,7 @@ Console::GetConsoleInternal(const GlobalObject& aGlobal, ErrorResult& aRv)
// we are probably running a chrome script.
if (!innerWindow) {
RefPtr<Console> console = new Console(nullptr);
RefPtr<Console> console = new Console(aGlobal.Context(), nullptr);
console->Initialize(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
@ -2441,7 +2447,7 @@ Console::GetConsoleInternal(const GlobalObject& aGlobal, ErrorResult& aRv)
}
nsGlobalWindowInner* window = nsGlobalWindowInner::Cast(innerWindow);
return window->GetConsole(aRv);
return window->GetConsole(aGlobal.Context(), aRv);
}
// Workers
@ -2560,7 +2566,8 @@ Console::MonotonicTimer(JSContext* aCx, MethodName aMethodName,
Console::CreateInstance(const GlobalObject& aGlobal,
const ConsoleInstanceOptions& aOptions)
{
RefPtr<ConsoleInstance> console = new ConsoleInstance(aOptions);
RefPtr<ConsoleInstance> console =
new ConsoleInstance(aGlobal.Context(), aOptions);
return console.forget();
}

View File

@ -42,7 +42,7 @@ public:
NS_DECL_NSIOBSERVER
static already_AddRefed<Console>
Create(nsPIDOMWindowInner* aWindow, ErrorResult& aRv);
Create(JSContext* aCx, nsPIDOMWindowInner* aWindow, ErrorResult& aRv);
// WebIDL methods
nsPIDOMWindowInner* GetParentObject() const
@ -129,7 +129,7 @@ public:
SetConsoleEventHandler(AnyCallback* aHandler);
private:
explicit Console(nsPIDOMWindowInner* aWindow);
Console(JSContext* aCx, nsPIDOMWindowInner* aWindow);
~Console();
void

View File

@ -67,17 +67,15 @@ WebIDLevelToConsoleUtilsLevel(ConsoleLevel aLevel)
} // anonymous
ConsoleInstance::ConsoleInstance(const ConsoleInstanceOptions& aOptions)
: mConsole(new Console(nullptr))
ConsoleInstance::ConsoleInstance(JSContext* aCx,
const ConsoleInstanceOptions& aOptions)
: mConsole(new Console(aCx, nullptr))
{
mConsole->mConsoleID = aOptions.mConsoleID;
mConsole->mPassedInnerID = aOptions.mInnerID;
if (aOptions.mDump.WasPassed()) {
mConsole->mDumpFunction = &aOptions.mDump.Value();
} else {
// For historical reasons, ConsoleInstance prints messages on stdout.
mConsole->mDumpToStdout = true;
}
mConsole->mPrefix = aOptions.mPrefix;

View File

@ -20,7 +20,8 @@ public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ConsoleInstance)
explicit ConsoleInstance(const ConsoleInstanceOptions& aOptions);
explicit ConsoleInstance(JSContext* aCx,
const ConsoleInstanceOptions& aOptions);
// WebIDL methods
JSObject*

View File

@ -144,7 +144,8 @@ WorkerGlobalScope::GetConsole(ErrorResult& aRv)
mWorkerPrivate->AssertIsOnWorkerThread();
if (!mConsole) {
mConsole = Console::Create(nullptr, aRv);
mConsole = Console::Create(mWorkerPrivate->GetJSContext(),
nullptr, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
@ -1077,7 +1078,8 @@ WorkerDebuggerGlobalScope::GetConsole(ErrorResult& aRv)
// Debugger console has its own console object.
if (!mConsole) {
mConsole = Console::Create(nullptr, aRv);
mConsole = Console::Create(mWorkerPrivate->GetJSContext(),
nullptr, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}

View File

@ -55,10 +55,10 @@ WorkletGlobalScope::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto
}
already_AddRefed<Console>
WorkletGlobalScope::GetConsole(ErrorResult& aRv)
WorkletGlobalScope::GetConsole(JSContext* aCx, ErrorResult& aRv)
{
if (!mConsole) {
mConsole = Console::Create(mWindow, aRv);
mConsole = Console::Create(aCx, mWindow, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}

View File

@ -55,7 +55,7 @@ public:
}
already_AddRefed<Console>
GetConsole(ErrorResult& aRv);
GetConsole(JSContext* aCx, ErrorResult& aRv);
void
Dump(const Optional<nsAString>& aString) const;