Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / console / ConsoleInstance.cpp
blobade82be50fb735950df23340568bd755585b8838
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 "Console.h"
9 #include "mozilla/dom/ConsoleBinding.h"
10 #include "mozilla/Preferences.h"
11 #include "ConsoleCommon.h"
12 #include "ConsoleUtils.h"
13 #include "nsContentUtils.h"
15 namespace mozilla::dom {
17 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ConsoleInstance, mConsole)
19 NS_IMPL_CYCLE_COLLECTING_ADDREF(ConsoleInstance)
20 NS_IMPL_CYCLE_COLLECTING_RELEASE(ConsoleInstance)
22 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ConsoleInstance)
23 NS_INTERFACE_MAP_ENTRY(nsISupports)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_END
27 namespace {
29 ConsoleUtils::Level WebIDLevelToConsoleUtilsLevel(ConsoleLevel aLevel) {
30 switch (aLevel) {
31 case ConsoleLevel::Log:
32 return ConsoleUtils::eLog;
33 case ConsoleLevel::Warning:
34 return ConsoleUtils::eWarning;
35 case ConsoleLevel::Error:
36 return ConsoleUtils::eError;
37 default:
38 break;
41 return ConsoleUtils::eLog;
44 } // namespace
46 ConsoleInstance::ConsoleInstance(JSContext* aCx,
47 const ConsoleInstanceOptions& aOptions)
48 : mMaxLogLevel(ConsoleLogLevel::All),
49 mConsole(new Console(aCx, nullptr, 0, 0)) {
50 mConsole->mConsoleID = aOptions.mConsoleID;
51 mConsole->mPassedInnerID = aOptions.mInnerID;
53 if (aOptions.mDump.WasPassed()) {
54 mConsole->mDumpFunction = &aOptions.mDump.Value();
57 mConsole->mPrefix = aOptions.mPrefix;
59 // Let's inform that this is a custom instance.
60 mConsole->mChromeInstance = true;
62 if (aOptions.mMaxLogLevel.WasPassed()) {
63 mMaxLogLevel = aOptions.mMaxLogLevel.Value();
66 if (!aOptions.mMaxLogLevelPref.IsEmpty()) {
67 if (!NS_IsMainThread()) {
68 NS_WARNING("Console.maxLogLevelPref is not supported on workers!");
69 // Set the log level based on what we have.
70 SetLogLevel();
71 return;
74 mMaxLogLevelPref = aOptions.mMaxLogLevelPref;
76 Preferences::RegisterCallback(MaxLogLevelPrefChangedCallback,
77 mMaxLogLevelPref, this);
79 SetLogLevel();
82 ConsoleInstance::~ConsoleInstance() {
83 AssertIsOnMainThread();
84 if (!mMaxLogLevelPref.IsEmpty()) {
85 Preferences::UnregisterCallback(MaxLogLevelPrefChangedCallback,
86 mMaxLogLevelPref, this);
90 ConsoleLogLevel PrefToValue(const nsACString& aPref,
91 const ConsoleLogLevel aLevel) {
92 if (aPref.IsEmpty()) {
93 return aLevel;
96 nsAutoCString value;
97 nsresult rv = Preferences::GetCString(PromiseFlatCString(aPref).get(), value);
98 if (NS_WARN_IF(NS_FAILED(rv))) {
99 nsString message;
100 message.AssignLiteral(
101 "Console.maxLogLevelPref used with a non-existing pref: ");
102 message.Append(NS_ConvertUTF8toUTF16(aPref));
104 nsContentUtils::LogSimpleConsoleError(message, "chrome"_ns, false,
105 true /* from chrome context*/);
106 return aLevel;
109 Maybe<ConsoleLogLevel> level = StringToEnum<ConsoleLogLevel>(value);
110 if (NS_WARN_IF(level.isNothing())) {
111 nsString message;
112 message.AssignLiteral("Invalid Console.maxLogLevelPref value: ");
113 message.Append(NS_ConvertUTF8toUTF16(value));
115 nsContentUtils::LogSimpleConsoleError(message, "chrome"_ns, false,
116 true /* from chrome context*/);
117 return aLevel;
120 return level.value();
123 void ConsoleInstance::SetLogLevel() {
124 mConsole->mCurrentLogLevel = mConsole->WebIDLLogLevelToInteger(
125 PrefToValue(mMaxLogLevelPref, mMaxLogLevel));
128 // static
129 void ConsoleInstance::MaxLogLevelPrefChangedCallback(
130 const char* /* aPrefName */, void* aSelf) {
131 AssertIsOnMainThread();
132 auto* instance = static_cast<ConsoleInstance*>(aSelf);
133 if (MOZ_UNLIKELY(!instance->mConsole)) {
134 // We've been unlinked already but not destroyed yet. Bail.
135 return;
137 RefPtr pin{instance};
138 pin->SetLogLevel();
141 JSObject* ConsoleInstance::WrapObject(JSContext* aCx,
142 JS::Handle<JSObject*> aGivenProto) {
143 return ConsoleInstance_Binding::Wrap(aCx, this, aGivenProto);
146 #define METHOD(name, string) \
147 void ConsoleInstance::name(JSContext* aCx, \
148 const Sequence<JS::Value>& aData) { \
149 RefPtr<Console> console(mConsole); \
150 console->MethodInternal(aCx, Console::Method##name, \
151 nsLiteralString(string), aData); \
154 METHOD(Log, u"log")
155 METHOD(Info, u"info")
156 METHOD(Warn, u"warn")
157 METHOD(Error, u"error")
158 METHOD(Exception, u"exception")
159 METHOD(Debug, u"debug")
160 METHOD(Table, u"table")
161 METHOD(Trace, u"trace")
162 METHOD(Dir, u"dir");
163 METHOD(Dirxml, u"dirxml");
164 METHOD(Group, u"group")
165 METHOD(GroupCollapsed, u"groupCollapsed")
167 #undef METHOD
169 void ConsoleInstance::GroupEnd(JSContext* aCx) {
170 const Sequence<JS::Value> data;
171 RefPtr<Console> console(mConsole);
172 console->MethodInternal(aCx, Console::MethodGroupEnd, u"groupEnd"_ns, data);
175 void ConsoleInstance::Time(JSContext* aCx, const nsAString& aLabel) {
176 RefPtr<Console> console(mConsole);
177 console->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
178 Console::MethodTime, u"time"_ns);
181 void ConsoleInstance::TimeLog(JSContext* aCx, const nsAString& aLabel,
182 const Sequence<JS::Value>& aData) {
183 RefPtr<Console> console(mConsole);
184 console->StringMethodInternal(aCx, aLabel, aData, Console::MethodTimeLog,
185 u"timeLog"_ns);
188 void ConsoleInstance::TimeEnd(JSContext* aCx, const nsAString& aLabel) {
189 RefPtr<Console> console(mConsole);
190 console->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
191 Console::MethodTimeEnd, u"timeEnd"_ns);
194 void ConsoleInstance::TimeStamp(JSContext* aCx,
195 const JS::Handle<JS::Value> aData) {
196 ConsoleCommon::ClearException ce(aCx);
198 Sequence<JS::Value> data;
199 SequenceRooter<JS::Value> rooter(aCx, &data);
201 if (aData.isString() && !data.AppendElement(aData, fallible)) {
202 return;
205 RefPtr<Console> console(mConsole);
206 console->MethodInternal(aCx, Console::MethodTimeStamp, u"timeStamp"_ns, data);
209 void ConsoleInstance::Profile(JSContext* aCx,
210 const Sequence<JS::Value>& aData) {
211 RefPtr<Console> console(mConsole);
212 console->ProfileMethodInternal(aCx, Console::MethodProfile, u"profile"_ns,
213 aData);
216 void ConsoleInstance::ProfileEnd(JSContext* aCx,
217 const Sequence<JS::Value>& aData) {
218 RefPtr<Console> console(mConsole);
219 console->ProfileMethodInternal(aCx, Console::MethodProfileEnd,
220 u"profileEnd"_ns, aData);
223 void ConsoleInstance::Assert(JSContext* aCx, bool aCondition,
224 const Sequence<JS::Value>& aData) {
225 if (!aCondition) {
226 RefPtr<Console> console(mConsole);
227 console->MethodInternal(aCx, Console::MethodAssert, u"assert"_ns, aData);
231 void ConsoleInstance::Count(JSContext* aCx, const nsAString& aLabel) {
232 RefPtr<Console> console(mConsole);
233 console->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
234 Console::MethodCount, u"count"_ns);
237 void ConsoleInstance::CountReset(JSContext* aCx, const nsAString& aLabel) {
238 RefPtr<Console> console(mConsole);
239 console->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
240 Console::MethodCountReset, u"countReset"_ns);
243 void ConsoleInstance::Clear(JSContext* aCx) {
244 const Sequence<JS::Value> data;
245 RefPtr<Console> console(mConsole);
246 console->MethodInternal(aCx, Console::MethodClear, u"clear"_ns, data);
249 bool ConsoleInstance::ShouldLog(ConsoleLogLevel aLevel) {
250 return mConsole->mCurrentLogLevel <=
251 mConsole->WebIDLLogLevelToInteger(aLevel);
254 void ConsoleInstance::ReportForServiceWorkerScope(const nsAString& aScope,
255 const nsAString& aMessage,
256 const nsAString& aFilename,
257 uint32_t aLineNumber,
258 uint32_t aColumnNumber,
259 ConsoleLevel aLevel) {
260 if (!NS_IsMainThread()) {
261 return;
264 ConsoleUtils::ReportForServiceWorkerScope(
265 aScope, aMessage, aFilename, aLineNumber, aColumnNumber,
266 WebIDLevelToConsoleUtilsLevel(aLevel));
269 } // namespace mozilla::dom