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 "ScriptErrorHelper.h"
9 #include "MainThreadUtils.h"
10 #include "nsContentUtils.h"
11 #include "nsThreadUtils.h"
13 #include "mozilla/SchedulerGroup.h"
17 class ScriptErrorRunnable final
: public mozilla::Runnable
{
19 nsCString mMessageName
;
20 mozilla::JSCallingLocation mCallingLocation
;
21 uint32_t mSeverityFlag
;
22 uint64_t mInnerWindowID
;
26 ScriptErrorRunnable(const nsAString
& aMessage
,
27 const mozilla::JSCallingLocation
& aCallingLocation
,
28 uint32_t aSeverityFlag
, bool aIsChrome
,
29 uint64_t aInnerWindowID
)
30 : mozilla::Runnable("ScriptErrorRunnable"),
32 mCallingLocation(aCallingLocation
),
33 mSeverityFlag(aSeverityFlag
),
34 mInnerWindowID(aInnerWindowID
),
35 mIsChrome(aIsChrome
) {
36 MOZ_ASSERT(!NS_IsMainThread());
37 mMessageName
.SetIsVoid(true);
40 ScriptErrorRunnable(const nsACString
& aMessageName
,
41 const mozilla::JSCallingLocation
& aCallingLocation
,
42 uint32_t aSeverityFlag
, bool aIsChrome
,
43 uint64_t aInnerWindowID
)
44 : mozilla::Runnable("ScriptErrorRunnable"),
45 mMessageName(aMessageName
),
46 mCallingLocation(aCallingLocation
),
47 mSeverityFlag(aSeverityFlag
),
48 mInnerWindowID(aInnerWindowID
),
49 mIsChrome(aIsChrome
) {
50 MOZ_ASSERT(!NS_IsMainThread());
51 mMessage
.SetIsVoid(true);
54 static void DumpLocalizedMessage(
55 const nsACString
& aMessageName
,
56 const mozilla::JSCallingLocation
& aCallingLocation
,
57 uint32_t aSeverityFlag
, bool aIsChrome
, uint64_t aInnerWindowID
) {
58 MOZ_ASSERT(NS_IsMainThread());
59 MOZ_ASSERT(!aMessageName
.IsEmpty());
61 nsAutoString localizedMessage
;
62 if (NS_WARN_IF(NS_FAILED(nsContentUtils::GetLocalizedString(
63 nsContentUtils::eDOM_PROPERTIES
, aMessageName
.BeginReading(),
64 localizedMessage
)))) {
67 Dump(localizedMessage
, aCallingLocation
, aSeverityFlag
, aIsChrome
,
71 static void Dump(const nsAString
& aMessage
,
72 const mozilla::JSCallingLocation
& aCallingLocation
,
73 uint32_t aSeverityFlag
, bool aIsChrome
,
74 uint64_t aInnerWindowID
) {
75 MOZ_ASSERT(NS_IsMainThread());
77 nsAutoCString category
;
79 category
.AssignLiteral("chrome ");
81 category
.AssignLiteral("content ");
83 category
.AppendLiteral("javascript");
84 nsContentUtils::ReportToConsoleByWindowID(aMessage
, aSeverityFlag
, category
,
85 aInnerWindowID
, aCallingLocation
);
90 MOZ_ASSERT(NS_IsMainThread());
91 MOZ_ASSERT(mMessage
.IsVoid() != mMessageName
.IsVoid());
93 if (!mMessage
.IsVoid()) {
94 Dump(mMessage
, mCallingLocation
, mSeverityFlag
, mIsChrome
,
99 DumpLocalizedMessage(mMessageName
, mCallingLocation
, mSeverityFlag
,
100 mIsChrome
, mInnerWindowID
);
106 virtual ~ScriptErrorRunnable() = default;
111 namespace mozilla::dom::indexedDB
{
114 void ScriptErrorHelper::Dump(const nsAString
& aMessage
,
115 const JSCallingLocation
& aCallingLocation
,
116 uint32_t aSeverityFlag
, bool aIsChrome
,
117 uint64_t aInnerWindowID
) {
118 if (NS_IsMainThread()) {
119 ScriptErrorRunnable::Dump(aMessage
, aCallingLocation
, aSeverityFlag
,
120 aIsChrome
, aInnerWindowID
);
122 RefPtr
<ScriptErrorRunnable
> runnable
= new ScriptErrorRunnable(
123 aMessage
, aCallingLocation
, aSeverityFlag
, aIsChrome
, aInnerWindowID
);
124 MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(runnable
.forget()));
129 void ScriptErrorHelper::DumpLocalizedMessage(
130 const nsACString
& aMessageName
, const JSCallingLocation
& aCallingLocation
,
131 uint32_t aSeverityFlag
, bool aIsChrome
, uint64_t aInnerWindowID
) {
132 if (NS_IsMainThread()) {
133 ScriptErrorRunnable::DumpLocalizedMessage(aMessageName
, aCallingLocation
,
134 aSeverityFlag
, aIsChrome
,
137 RefPtr
<ScriptErrorRunnable
> runnable
=
138 new ScriptErrorRunnable(aMessageName
, aCallingLocation
, aSeverityFlag
,
139 aIsChrome
, aInnerWindowID
);
140 MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(runnable
.forget()));
144 } // namespace mozilla::dom::indexedDB