Source code

Revision control

Other Tools

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/. */
6
7
#include "ScriptErrorHelper.h"
8
9
#include "MainThreadUtils.h"
10
#include "mozilla/SystemGroup.h"
11
#include "nsCOMPtr.h"
12
#include "nsContentUtils.h"
13
#include "nsIConsoleService.h"
14
#include "nsIScriptError.h"
15
#include "nsString.h"
16
#include "nsThreadUtils.h"
17
18
namespace {
19
20
class ScriptErrorRunnable final : public mozilla::Runnable {
21
nsString mMessage;
22
nsCString mMessageName;
23
nsString mFilename;
24
uint32_t mLineNumber;
25
uint32_t mColumnNumber;
26
uint32_t mSeverityFlag;
27
uint64_t mInnerWindowID;
28
bool mIsChrome;
29
30
public:
31
ScriptErrorRunnable(const nsAString& aMessage, const nsAString& aFilename,
32
uint32_t aLineNumber, uint32_t aColumnNumber,
33
uint32_t aSeverityFlag, bool aIsChrome,
34
uint64_t aInnerWindowID)
35
: mozilla::Runnable("ScriptErrorRunnable"),
36
mMessage(aMessage),
37
mFilename(aFilename),
38
mLineNumber(aLineNumber),
39
mColumnNumber(aColumnNumber),
40
mSeverityFlag(aSeverityFlag),
41
mInnerWindowID(aInnerWindowID),
42
mIsChrome(aIsChrome) {
43
MOZ_ASSERT(!NS_IsMainThread());
44
mMessageName.SetIsVoid(true);
45
}
46
47
ScriptErrorRunnable(const nsACString& aMessageName,
48
const nsAString& aFilename, uint32_t aLineNumber,
49
uint32_t aColumnNumber, uint32_t aSeverityFlag,
50
bool aIsChrome, uint64_t aInnerWindowID)
51
: mozilla::Runnable("ScriptErrorRunnable"),
52
mMessageName(aMessageName),
53
mFilename(aFilename),
54
mLineNumber(aLineNumber),
55
mColumnNumber(aColumnNumber),
56
mSeverityFlag(aSeverityFlag),
57
mInnerWindowID(aInnerWindowID),
58
mIsChrome(aIsChrome) {
59
MOZ_ASSERT(!NS_IsMainThread());
60
mMessage.SetIsVoid(true);
61
}
62
63
static void DumpLocalizedMessage(const nsACString& aMessageName,
64
const nsAString& aFilename,
65
uint32_t aLineNumber, uint32_t aColumnNumber,
66
uint32_t aSeverityFlag, bool aIsChrome,
67
uint64_t aInnerWindowID) {
68
MOZ_ASSERT(NS_IsMainThread());
69
MOZ_ASSERT(!aMessageName.IsEmpty());
70
71
nsAutoString localizedMessage;
72
if (NS_WARN_IF(NS_FAILED(nsContentUtils::GetLocalizedString(
73
nsContentUtils::eDOM_PROPERTIES, aMessageName.BeginReading(),
74
localizedMessage)))) {
75
return;
76
}
77
78
Dump(localizedMessage, aFilename, aLineNumber, aColumnNumber, aSeverityFlag,
79
aIsChrome, aInnerWindowID);
80
}
81
82
static void Dump(const nsAString& aMessage, const nsAString& aFilename,
83
uint32_t aLineNumber, uint32_t aColumnNumber,
84
uint32_t aSeverityFlag, bool aIsChrome,
85
uint64_t aInnerWindowID) {
86
MOZ_ASSERT(NS_IsMainThread());
87
88
nsAutoCString category;
89
if (aIsChrome) {
90
category.AssignLiteral("chrome ");
91
} else {
92
category.AssignLiteral("content ");
93
}
94
category.AppendLiteral("javascript");
95
96
nsCOMPtr<nsIConsoleService> consoleService =
97
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
98
99
nsCOMPtr<nsIScriptError> scriptError =
100
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
101
// We may not be able to create the script error object when we're shutting
102
// down.
103
if (!scriptError) {
104
return;
105
}
106
107
if (aInnerWindowID) {
108
MOZ_ALWAYS_SUCCEEDS(scriptError->InitWithWindowID(
109
aMessage, aFilename,
110
/* aSourceLine */ EmptyString(), aLineNumber, aColumnNumber,
111
aSeverityFlag, category, aInnerWindowID));
112
} else {
113
MOZ_ALWAYS_SUCCEEDS(scriptError->Init(
114
aMessage, aFilename,
115
/* aSourceLine */ EmptyString(), aLineNumber, aColumnNumber,
116
aSeverityFlag, category.get(),
117
/* IDB doesn't run on Private browsing mode */ false,
118
/* from chrome context */ aIsChrome));
119
}
120
121
// We may not be able to obtain the console service when we're shutting
122
// down.
123
if (consoleService) {
124
MOZ_ALWAYS_SUCCEEDS(consoleService->LogMessage(scriptError));
125
}
126
}
127
128
NS_IMETHOD
129
Run() override {
130
MOZ_ASSERT(NS_IsMainThread());
131
MOZ_ASSERT(mMessage.IsVoid() != mMessageName.IsVoid());
132
133
if (!mMessage.IsVoid()) {
134
Dump(mMessage, mFilename, mLineNumber, mColumnNumber, mSeverityFlag,
135
mIsChrome, mInnerWindowID);
136
return NS_OK;
137
}
138
139
DumpLocalizedMessage(mMessageName, mFilename, mLineNumber, mColumnNumber,
140
mSeverityFlag, mIsChrome, mInnerWindowID);
141
142
return NS_OK;
143
}
144
145
private:
146
virtual ~ScriptErrorRunnable() = default;
147
};
148
149
} // namespace
150
151
namespace mozilla {
152
namespace dom {
153
namespace indexedDB {
154
155
/*static*/
156
void ScriptErrorHelper::Dump(const nsAString& aMessage,
157
const nsAString& aFilename, uint32_t aLineNumber,
158
uint32_t aColumnNumber, uint32_t aSeverityFlag,
159
bool aIsChrome, uint64_t aInnerWindowID) {
160
if (NS_IsMainThread()) {
161
ScriptErrorRunnable::Dump(aMessage, aFilename, aLineNumber, aColumnNumber,
162
aSeverityFlag, aIsChrome, aInnerWindowID);
163
} else {
164
RefPtr<ScriptErrorRunnable> runnable =
165
new ScriptErrorRunnable(aMessage, aFilename, aLineNumber, aColumnNumber,
166
aSeverityFlag, aIsChrome, aInnerWindowID);
167
MOZ_ALWAYS_SUCCEEDS(
168
SystemGroup::Dispatch(TaskCategory::Other, runnable.forget()));
169
}
170
}
171
172
/*static*/
173
void ScriptErrorHelper::DumpLocalizedMessage(
174
const nsACString& aMessageName, const nsAString& aFilename,
175
uint32_t aLineNumber, uint32_t aColumnNumber, uint32_t aSeverityFlag,
176
bool aIsChrome, uint64_t aInnerWindowID) {
177
if (NS_IsMainThread()) {
178
ScriptErrorRunnable::DumpLocalizedMessage(
179
aMessageName, aFilename, aLineNumber, aColumnNumber, aSeverityFlag,
180
aIsChrome, aInnerWindowID);
181
} else {
182
RefPtr<ScriptErrorRunnable> runnable = new ScriptErrorRunnable(
183
aMessageName, aFilename, aLineNumber, aColumnNumber, aSeverityFlag,
184
aIsChrome, aInnerWindowID);
185
MOZ_ALWAYS_SUCCEEDS(
186
SystemGroup::Dispatch(TaskCategory::Other, runnable.forget()));
187
}
188
}
189
190
} // namespace indexedDB
191
} // namespace dom
192
} // namespace mozilla