1 | /* |
2 | * Copyright (C) 2007, 2008, 2014, 2015 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2008 Matt Lilek <[email protected]> |
4 | * Copyright (C) 2009, 2010 Google Inc. All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
16 | * its contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | #include "ConsoleMessage.h" |
33 | |
34 | #include "IdentifiersFactory.h" |
35 | #include "InjectedScript.h" |
36 | #include "InjectedScriptManager.h" |
37 | #include "InspectorFrontendDispatchers.h" |
38 | #include "ScriptArguments.h" |
39 | #include "ScriptCallFrame.h" |
40 | #include "ScriptCallStack.h" |
41 | #include "ScriptCallStackFactory.h" |
42 | |
43 | namespace Inspector { |
44 | |
45 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned long requestIdentifier) |
46 | : m_source(source) |
47 | , m_type(type) |
48 | , m_level(level) |
49 | , m_message(message) |
50 | , m_url() |
51 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
52 | { |
53 | } |
54 | |
55 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, const String& url, unsigned line, unsigned column, JSC::JSGlobalObject* globalObject, unsigned long requestIdentifier) |
56 | : m_source(source) |
57 | , m_type(type) |
58 | , m_level(level) |
59 | , m_message(message) |
60 | , m_url(url) |
61 | , m_line(line) |
62 | , m_column(column) |
63 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
64 | { |
65 | autogenerateMetadata(globalObject); |
66 | } |
67 | |
68 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, Ref<ScriptCallStack>&& callStack, unsigned long requestIdentifier) |
69 | : m_source(source) |
70 | , m_type(type) |
71 | , m_level(level) |
72 | , m_message(message) |
73 | , m_callStack(WTFMove(callStack)) |
74 | , m_url() |
75 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
76 | { |
77 | const ScriptCallFrame* frame = m_callStack ? m_callStack->firstNonNativeCallFrame() : nullptr; |
78 | if (frame) { |
79 | m_url = frame->sourceURL(); |
80 | m_line = frame->lineNumber(); |
81 | m_column = frame->columnNumber(); |
82 | } |
83 | } |
84 | |
85 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, Ref<ScriptArguments>&& arguments, Ref<ScriptCallStack>&& callStack, unsigned long requestIdentifier) |
86 | : m_source(source) |
87 | , m_type(type) |
88 | , m_level(level) |
89 | , m_message(message) |
90 | , m_arguments(WTFMove(arguments)) |
91 | , m_callStack(WTFMove(callStack)) |
92 | , m_url() |
93 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
94 | { |
95 | const ScriptCallFrame* frame = m_callStack ? m_callStack->firstNonNativeCallFrame() : nullptr; |
96 | if (frame) { |
97 | m_url = frame->sourceURL(); |
98 | m_line = frame->lineNumber(); |
99 | m_column = frame->columnNumber(); |
100 | } |
101 | } |
102 | |
103 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, Ref<ScriptArguments>&& arguments, JSC::JSGlobalObject* globalObject, unsigned long requestIdentifier) |
104 | : m_source(source) |
105 | , m_type(type) |
106 | , m_level(level) |
107 | , m_message(message) |
108 | , m_arguments(WTFMove(arguments)) |
109 | , m_url() |
110 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
111 | { |
112 | autogenerateMetadata(globalObject); |
113 | } |
114 | |
115 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, Vector<JSONLogValue>&& messages, JSC::JSGlobalObject* globalObject, unsigned long requestIdentifier) |
116 | : m_source(source) |
117 | , m_type(type) |
118 | , m_level(level) |
119 | , m_url() |
120 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
121 | { |
122 | if (globalObject) |
123 | m_globalObject = { globalObject->vm(), globalObject }; |
124 | |
125 | if (!messages.size()) |
126 | return; |
127 | |
128 | m_jsonLogValues.reserveInitialCapacity(messages.size()); |
129 | |
130 | StringBuilder builder; |
131 | for (auto& message : messages) { |
132 | switch (message.type) { |
133 | case JSONLogValue::Type::String: |
134 | builder.append(message.value); |
135 | break; |
136 | case JSONLogValue::Type::JSON: |
137 | if (builder.length()) { |
138 | m_jsonLogValues.append({ JSONLogValue::Type::String, JSON::Value::create(builder.toString())->toJSONString() }); |
139 | builder.resize(0); |
140 | } |
141 | |
142 | m_jsonLogValues.append(message); |
143 | break; |
144 | } |
145 | } |
146 | |
147 | if (builder.length()) |
148 | m_jsonLogValues.append({ JSONLogValue::Type::String, JSON::Value::create(builder.toString())->toJSONString() }); |
149 | |
150 | if (m_jsonLogValues.size()) |
151 | m_message = m_jsonLogValues[0].value; |
152 | } |
153 | |
154 | ConsoleMessage::~ConsoleMessage() |
155 | { |
156 | } |
157 | |
158 | void ConsoleMessage::autogenerateMetadata(JSC::JSGlobalObject* globalObject) |
159 | { |
160 | if (!globalObject) |
161 | return; |
162 | |
163 | if (m_type == MessageType::EndGroup) |
164 | return; |
165 | |
166 | // FIXME: Should this really be using "for console" in the generic ConsoleMessage autogeneration? This can skip the first frame. |
167 | m_callStack = createScriptCallStackForConsole(globalObject); |
168 | |
169 | if (const ScriptCallFrame* frame = m_callStack->firstNonNativeCallFrame()) { |
170 | m_url = frame->sourceURL(); |
171 | m_line = frame->lineNumber(); |
172 | m_column = frame->columnNumber(); |
173 | return; |
174 | } |
175 | } |
176 | |
177 | static Protocol::Console::ChannelSource messageSourceValue(MessageSource source) |
178 | { |
179 | switch (source) { |
180 | case MessageSource::XML: return Protocol::Console::ChannelSource::XML; |
181 | case MessageSource::JS: return Protocol::Console::ChannelSource::JavaScript; |
182 | case MessageSource::Network: return Protocol::Console::ChannelSource::Network; |
183 | case MessageSource::ConsoleAPI: return Protocol::Console::ChannelSource::ConsoleAPI; |
184 | case MessageSource::Storage: return Protocol::Console::ChannelSource::Storage; |
185 | case MessageSource::AppCache: return Protocol::Console::ChannelSource::Appcache; |
186 | case MessageSource::Rendering: return Protocol::Console::ChannelSource::Rendering; |
187 | case MessageSource::CSS: return Protocol::Console::ChannelSource::CSS; |
188 | case MessageSource::Security: return Protocol::Console::ChannelSource::Security; |
189 | case MessageSource::ContentBlocker: return Protocol::Console::ChannelSource::ContentBlocker; |
190 | case MessageSource::Other: return Protocol::Console::ChannelSource::Other; |
191 | case MessageSource::Media: return Protocol::Console::ChannelSource::Media; |
192 | case MessageSource::WebRTC: return Protocol::Console::ChannelSource::WebRTC; |
193 | case MessageSource::MediaSource: return Protocol::Console::ChannelSource::MediaSource; |
194 | } |
195 | return Protocol::Console::ChannelSource::Other; |
196 | } |
197 | |
198 | static Protocol::Console::ConsoleMessage::Type messageTypeValue(MessageType type) |
199 | { |
200 | switch (type) { |
201 | case MessageType::Log: return Protocol::Console::ConsoleMessage::Type::Log; |
202 | case MessageType::Clear: return Protocol::Console::ConsoleMessage::Type::Clear; |
203 | case MessageType::Dir: return Protocol::Console::ConsoleMessage::Type::Dir; |
204 | case MessageType::DirXML: return Protocol::Console::ConsoleMessage::Type::DirXML; |
205 | case MessageType::Table: return Protocol::Console::ConsoleMessage::Type::Table; |
206 | case MessageType::Trace: return Protocol::Console::ConsoleMessage::Type::Trace; |
207 | case MessageType::StartGroup: return Protocol::Console::ConsoleMessage::Type::StartGroup; |
208 | case MessageType::StartGroupCollapsed: return Protocol::Console::ConsoleMessage::Type::StartGroupCollapsed; |
209 | case MessageType::EndGroup: return Protocol::Console::ConsoleMessage::Type::EndGroup; |
210 | case MessageType::Assert: return Protocol::Console::ConsoleMessage::Type::Assert; |
211 | case MessageType::Timing: return Protocol::Console::ConsoleMessage::Type::Timing; |
212 | case MessageType::Profile: return Protocol::Console::ConsoleMessage::Type::Profile; |
213 | case MessageType::ProfileEnd: return Protocol::Console::ConsoleMessage::Type::ProfileEnd; |
214 | case MessageType::Image: return Protocol::Console::ConsoleMessage::Type::Image; |
215 | } |
216 | return Protocol::Console::ConsoleMessage::Type::Log; |
217 | } |
218 | |
219 | static Protocol::Console::ConsoleMessage::Level messageLevelValue(MessageLevel level) |
220 | { |
221 | switch (level) { |
222 | case MessageLevel::Log: return Protocol::Console::ConsoleMessage::Level::Log; |
223 | case MessageLevel::Info: return Protocol::Console::ConsoleMessage::Level::Info; |
224 | case MessageLevel::Warning: return Protocol::Console::ConsoleMessage::Level::Warning; |
225 | case MessageLevel::Error: return Protocol::Console::ConsoleMessage::Level::Error; |
226 | case MessageLevel::Debug: return Protocol::Console::ConsoleMessage::Level::Debug; |
227 | } |
228 | return Protocol::Console::ConsoleMessage::Level::Log; |
229 | } |
230 | |
231 | void ConsoleMessage::addToFrontend(ConsoleFrontendDispatcher& consoleFrontendDispatcher, InjectedScriptManager& injectedScriptManager, bool generatePreview) |
232 | { |
233 | auto messageObject = Protocol::Console::ConsoleMessage::create() |
234 | .setSource(messageSourceValue(m_source)) |
235 | .setLevel(messageLevelValue(m_level)) |
236 | .setText(m_message) |
237 | .release(); |
238 | |
239 | // FIXME: only send out type for ConsoleAPI source messages. |
240 | messageObject->setType(messageTypeValue(m_type)); |
241 | messageObject->setLine(static_cast<int>(m_line)); |
242 | messageObject->setColumn(static_cast<int>(m_column)); |
243 | messageObject->setUrl(m_url); |
244 | messageObject->setRepeatCount(static_cast<int>(m_repeatCount)); |
245 | |
246 | if (m_source == MessageSource::Network && !m_requestId.isEmpty()) |
247 | messageObject->setNetworkRequestId(m_requestId); |
248 | |
249 | if ((m_arguments && m_arguments->argumentCount()) || m_jsonLogValues.size()) { |
250 | InjectedScript injectedScript = injectedScriptManager.injectedScriptFor(globalObject()); |
251 | if (!injectedScript.hasNoValue()) { |
252 | auto argumentsObject = JSON::ArrayOf<Protocol::Runtime::RemoteObject>::create(); |
253 | if (m_arguments && m_arguments->argumentCount()) { |
254 | if (m_type == MessageType::Table && generatePreview && m_arguments->argumentCount()) { |
255 | auto table = m_arguments->argumentAt(0); |
256 | auto columns = m_arguments->argumentCount() > 1 ? m_arguments->argumentAt(1) : JSC::JSValue(); |
257 | auto inspectorValue = injectedScript.wrapTable(table, columns); |
258 | if (!inspectorValue) { |
259 | ASSERT_NOT_REACHED(); |
260 | return; |
261 | } |
262 | argumentsObject->addItem(WTFMove(inspectorValue)); |
263 | if (m_arguments->argumentCount() > 1) |
264 | argumentsObject->addItem(injectedScript.wrapObject(columns, "console"_s , true)); |
265 | } else { |
266 | for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) { |
267 | auto inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console"_s , generatePreview); |
268 | if (!inspectorValue) { |
269 | ASSERT_NOT_REACHED(); |
270 | return; |
271 | } |
272 | argumentsObject->addItem(WTFMove(inspectorValue)); |
273 | } |
274 | } |
275 | } |
276 | |
277 | if (m_jsonLogValues.size()) { |
278 | for (auto& message : m_jsonLogValues) { |
279 | if (message.value.isEmpty()) |
280 | continue; |
281 | auto inspectorValue = injectedScript.wrapJSONString(message.value, "console"_s , generatePreview); |
282 | if (!inspectorValue) |
283 | continue; |
284 | |
285 | argumentsObject->addItem(WTFMove(inspectorValue)); |
286 | } |
287 | } |
288 | |
289 | if (argumentsObject->length()) |
290 | messageObject->setParameters(WTFMove(argumentsObject)); |
291 | } |
292 | } |
293 | |
294 | if (m_callStack) |
295 | messageObject->setStackTrace(m_callStack->buildInspectorArray()); |
296 | |
297 | consoleFrontendDispatcher.messageAdded(WTFMove(messageObject)); |
298 | } |
299 | |
300 | void ConsoleMessage::updateRepeatCountInConsole(ConsoleFrontendDispatcher& consoleFrontendDispatcher) |
301 | { |
302 | consoleFrontendDispatcher.messageRepeatCountUpdated(m_repeatCount); |
303 | } |
304 | |
305 | bool ConsoleMessage::isEqual(ConsoleMessage* msg) const |
306 | { |
307 | if (m_arguments) { |
308 | if (!msg->m_arguments || !m_arguments->isEqual(*msg->m_arguments)) |
309 | return false; |
310 | |
311 | // Never treat objects as equal - their properties might change over time. |
312 | for (size_t i = 0; i < m_arguments->argumentCount(); ++i) { |
313 | if (m_arguments->argumentAt(i).isObject()) |
314 | return false; |
315 | } |
316 | } else if (msg->m_arguments) |
317 | return false; |
318 | |
319 | if (m_callStack) { |
320 | if (!m_callStack->isEqual(msg->m_callStack.get())) |
321 | return false; |
322 | } else if (msg->m_callStack) |
323 | return false; |
324 | |
325 | if (m_jsonLogValues.size() || msg->m_jsonLogValues.size()) |
326 | return false; |
327 | |
328 | return msg->m_source == m_source |
329 | && msg->m_type == m_type |
330 | && msg->m_level == m_level |
331 | && msg->m_message == m_message |
332 | && msg->m_line == m_line |
333 | && msg->m_column == m_column |
334 | && msg->m_url == m_url |
335 | && msg->m_requestId == m_requestId; |
336 | } |
337 | |
338 | void ConsoleMessage::clear() |
339 | { |
340 | if (!m_message) |
341 | m_message = "<message collected>"_s ; |
342 | |
343 | if (m_arguments) |
344 | m_arguments = nullptr; |
345 | |
346 | if (m_globalObject) |
347 | m_globalObject.clear(); |
348 | } |
349 | |
350 | JSC::JSGlobalObject* ConsoleMessage::globalObject() const |
351 | { |
352 | if (m_arguments) |
353 | return m_arguments->globalObject(); |
354 | |
355 | if (m_globalObject) |
356 | return m_globalObject.get(); |
357 | |
358 | return nullptr; |
359 | } |
360 | |
361 | unsigned ConsoleMessage::argumentCount() const |
362 | { |
363 | if (m_arguments) |
364 | return m_arguments->argumentCount(); |
365 | |
366 | return 0; |
367 | } |
368 | |
369 | } // namespace Inspector |
370 | |