1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ConsoleInstance.h"
8 #include "mozilla/dom/ConsoleBinding.h"
9 #include "mozilla/Preferences.h"
10 #include "ConsoleCommon.h"
11 #include "ConsoleUtils.h"
12 #include "nsContentUtils.h"
14 namespace mozilla::dom
{
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ConsoleInstance
, mConsole
)
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(ConsoleInstance
)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(ConsoleInstance
)
21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ConsoleInstance
)
22 NS_INTERFACE_MAP_ENTRY(nsISupports
)
23 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
28 ConsoleUtils::Level
WebIDLevelToConsoleUtilsLevel(ConsoleLevel aLevel
) {
30 case ConsoleLevel::Log
:
31 return ConsoleUtils::eLog
;
32 case ConsoleLevel::Warning
:
33 return ConsoleUtils::eWarning
;
34 case ConsoleLevel::Error
:
35 return ConsoleUtils::eError
;
40 return ConsoleUtils::eLog
;
45 ConsoleInstance::ConsoleInstance(JSContext
* aCx
,
46 const ConsoleInstanceOptions
& aOptions
)
47 : mConsole(new Console(aCx
, nullptr, 0, 0)) {
48 mConsole
->mConsoleID
= aOptions
.mConsoleID
;
49 mConsole
->mPassedInnerID
= aOptions
.mInnerID
;
51 if (aOptions
.mDump
.WasPassed()) {
52 mConsole
->mDumpFunction
= &aOptions
.mDump
.Value();
55 mConsole
->mPrefix
= aOptions
.mPrefix
;
57 // Let's inform that this is a custom instance.
58 mConsole
->mChromeInstance
= true;
60 if (aOptions
.mMaxLogLevel
.WasPassed()) {
61 mConsole
->mMaxLogLevel
= aOptions
.mMaxLogLevel
.Value();
64 if (!aOptions
.mMaxLogLevelPref
.IsEmpty()) {
65 mConsole
->mMaxLogLevelPref
= aOptions
.mMaxLogLevelPref
;
66 NS_ConvertUTF16toUTF8
pref(aOptions
.mMaxLogLevelPref
);
68 nsresult rv
= Preferences::GetCString(pref
.get(), value
);
69 if (NS_WARN_IF(NS_FAILED(rv
))) {
71 message
.AssignLiteral(
72 "Console.maxLogLevelPref used with a non-existing pref: ");
73 message
.Append(aOptions
.mMaxLogLevelPref
);
75 nsContentUtils::LogSimpleConsoleError(message
, "chrome"_ns
, false,
76 true /* from chrome context*/);
81 ConsoleInstance::~ConsoleInstance() = default;
83 JSObject
* ConsoleInstance::WrapObject(JSContext
* aCx
,
84 JS::Handle
<JSObject
*> aGivenProto
) {
85 return ConsoleInstance_Binding::Wrap(aCx
, this, aGivenProto
);
88 #define METHOD(name, string) \
89 void ConsoleInstance::name(JSContext* aCx, \
90 const Sequence<JS::Value>& aData) { \
91 RefPtr<Console> console(mConsole); \
92 console->MethodInternal(aCx, Console::Method##name, \
93 nsLiteralString(string), aData); \
99 METHOD(Error
, u
"error")
100 METHOD(Exception
, u
"exception")
101 METHOD(Debug
, u
"debug")
102 METHOD(Table
, u
"table")
103 METHOD(Trace
, u
"trace")
105 METHOD(Dirxml
, u
"dirxml");
106 METHOD(Group
, u
"group")
107 METHOD(GroupCollapsed
, u
"groupCollapsed")
111 void ConsoleInstance::GroupEnd(JSContext
* aCx
) {
112 const Sequence
<JS::Value
> data
;
113 RefPtr
<Console
> console(mConsole
);
114 console
->MethodInternal(aCx
, Console::MethodGroupEnd
, u
"groupEnd"_ns
, data
);
117 void ConsoleInstance::Time(JSContext
* aCx
, const nsAString
& aLabel
) {
118 RefPtr
<Console
> console(mConsole
);
119 console
->StringMethodInternal(aCx
, aLabel
, Sequence
<JS::Value
>(),
120 Console::MethodTime
, u
"time"_ns
);
123 void ConsoleInstance::TimeLog(JSContext
* aCx
, const nsAString
& aLabel
,
124 const Sequence
<JS::Value
>& aData
) {
125 RefPtr
<Console
> console(mConsole
);
126 console
->StringMethodInternal(aCx
, aLabel
, aData
, Console::MethodTimeLog
,
130 void ConsoleInstance::TimeEnd(JSContext
* aCx
, const nsAString
& aLabel
) {
131 RefPtr
<Console
> console(mConsole
);
132 console
->StringMethodInternal(aCx
, aLabel
, Sequence
<JS::Value
>(),
133 Console::MethodTimeEnd
, u
"timeEnd"_ns
);
136 void ConsoleInstance::TimeStamp(JSContext
* aCx
,
137 const JS::Handle
<JS::Value
> aData
) {
138 ConsoleCommon::ClearException
ce(aCx
);
140 Sequence
<JS::Value
> data
;
141 SequenceRooter
<JS::Value
> rooter(aCx
, &data
);
143 if (aData
.isString() && !data
.AppendElement(aData
, fallible
)) {
147 RefPtr
<Console
> console(mConsole
);
148 console
->MethodInternal(aCx
, Console::MethodTimeStamp
, u
"timeStamp"_ns
, data
);
151 void ConsoleInstance::Profile(JSContext
* aCx
,
152 const Sequence
<JS::Value
>& aData
) {
153 RefPtr
<Console
> console(mConsole
);
154 console
->ProfileMethodInternal(aCx
, Console::MethodProfile
, u
"profile"_ns
,
158 void ConsoleInstance::ProfileEnd(JSContext
* aCx
,
159 const Sequence
<JS::Value
>& aData
) {
160 RefPtr
<Console
> console(mConsole
);
161 console
->ProfileMethodInternal(aCx
, Console::MethodProfileEnd
,
162 u
"profileEnd"_ns
, aData
);
165 void ConsoleInstance::Assert(JSContext
* aCx
, bool aCondition
,
166 const Sequence
<JS::Value
>& aData
) {
168 RefPtr
<Console
> console(mConsole
);
169 console
->MethodInternal(aCx
, Console::MethodAssert
, u
"assert"_ns
, aData
);
173 void ConsoleInstance::Count(JSContext
* aCx
, const nsAString
& aLabel
) {
174 RefPtr
<Console
> console(mConsole
);
175 console
->StringMethodInternal(aCx
, aLabel
, Sequence
<JS::Value
>(),
176 Console::MethodCount
, u
"count"_ns
);
179 void ConsoleInstance::CountReset(JSContext
* aCx
, const nsAString
& aLabel
) {
180 RefPtr
<Console
> console(mConsole
);
181 console
->StringMethodInternal(aCx
, aLabel
, Sequence
<JS::Value
>(),
182 Console::MethodCountReset
, u
"countReset"_ns
);
185 void ConsoleInstance::Clear(JSContext
* aCx
) {
186 const Sequence
<JS::Value
> data
;
187 RefPtr
<Console
> console(mConsole
);
188 console
->MethodInternal(aCx
, Console::MethodClear
, u
"clear"_ns
, data
);
191 void ConsoleInstance::ReportForServiceWorkerScope(const nsAString
& aScope
,
192 const nsAString
& aMessage
,
193 const nsAString
& aFilename
,
194 uint32_t aLineNumber
,
195 uint32_t aColumnNumber
,
196 ConsoleLevel aLevel
) {
197 if (!NS_IsMainThread()) {
201 ConsoleUtils::ReportForServiceWorkerScope(
202 aScope
, aMessage
, aFilename
, aLineNumber
, aColumnNumber
,
203 WebIDLevelToConsoleUtilsLevel(aLevel
));
206 } // namespace mozilla::dom