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
43namespace Inspector {
44
45ConsoleMessage::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
55ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, const String& url, unsigned line, unsigned column, JSC::ExecState* state, 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(state);
66}
67
68ConsoleMessage::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
85ConsoleMessage::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
103ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, Ref<ScriptArguments>&& arguments, JSC::ExecState* state, 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(state);
113}
114
115ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, Vector<JSONLogValue>&& messages, JSC::ExecState* state, unsigned long requestIdentifier)
116 : m_source(source)
117 , m_type(type)
118 , m_level(level)
119 , m_url()
120 , m_scriptState(state)
121 , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
122{
123 if (!messages.size())
124 return;
125
126 m_jsonLogValues.reserveInitialCapacity(messages.size());
127
128 StringBuilder builder;
129 for (auto& message : messages) {
130 switch (message.type) {
131 case JSONLogValue::Type::String:
132 builder.append(message.value);
133 break;
134 case JSONLogValue::Type::JSON:
135 if (builder.length()) {
136 m_jsonLogValues.append({ JSONLogValue::Type::String, JSON::Value::create(builder.toString())->toJSONString() });
137 builder.resize(0);
138 }
139
140 m_jsonLogValues.append(message);
141 break;
142 }
143 }
144
145 if (builder.length())
146 m_jsonLogValues.append({ JSONLogValue::Type::String, JSON::Value::create(builder.toString())->toJSONString() });
147
148 if (m_jsonLogValues.size())
149 m_message = m_jsonLogValues[0].value;
150}
151
152ConsoleMessage::~ConsoleMessage()
153{
154}
155
156void ConsoleMessage::autogenerateMetadata(JSC::ExecState* state)
157{
158 if (!state)
159 return;
160
161 if (m_type == MessageType::EndGroup)
162 return;
163
164 // FIXME: Should this really be using "for console" in the generic ConsoleMessage autogeneration? This can skip the first frame.
165 m_callStack = createScriptCallStackForConsole(state);
166
167 if (const ScriptCallFrame* frame = m_callStack->firstNonNativeCallFrame()) {
168 m_url = frame->sourceURL();
169 m_line = frame->lineNumber();
170 m_column = frame->columnNumber();
171 return;
172 }
173}
174
175static Protocol::Console::ChannelSource messageSourceValue(MessageSource source)
176{
177 switch (source) {
178 case MessageSource::XML: return Protocol::Console::ChannelSource::XML;
179 case MessageSource::JS: return Protocol::Console::ChannelSource::JavaScript;
180 case MessageSource::Network: return Protocol::Console::ChannelSource::Network;
181 case MessageSource::ConsoleAPI: return Protocol::Console::ChannelSource::ConsoleAPI;
182 case MessageSource::Storage: return Protocol::Console::ChannelSource::Storage;
183 case MessageSource::AppCache: return Protocol::Console::ChannelSource::Appcache;
184 case MessageSource::Rendering: return Protocol::Console::ChannelSource::Rendering;
185 case MessageSource::CSS: return Protocol::Console::ChannelSource::CSS;
186 case MessageSource::Security: return Protocol::Console::ChannelSource::Security;
187 case MessageSource::ContentBlocker: return Protocol::Console::ChannelSource::ContentBlocker;
188 case MessageSource::Other: return Protocol::Console::ChannelSource::Other;
189 case MessageSource::Media: return Protocol::Console::ChannelSource::Media;
190 case MessageSource::WebRTC: return Protocol::Console::ChannelSource::WebRTC;
191 case MessageSource::MediaSource: return Protocol::Console::ChannelSource::MediaSource;
192 }
193 return Protocol::Console::ChannelSource::Other;
194}
195
196static Protocol::Console::ConsoleMessage::Type messageTypeValue(MessageType type)
197{
198 switch (type) {
199 case MessageType::Log: return Protocol::Console::ConsoleMessage::Type::Log;
200 case MessageType::Clear: return Protocol::Console::ConsoleMessage::Type::Clear;
201 case MessageType::Dir: return Protocol::Console::ConsoleMessage::Type::Dir;
202 case MessageType::DirXML: return Protocol::Console::ConsoleMessage::Type::DirXML;
203 case MessageType::Table: return Protocol::Console::ConsoleMessage::Type::Table;
204 case MessageType::Trace: return Protocol::Console::ConsoleMessage::Type::Trace;
205 case MessageType::StartGroup: return Protocol::Console::ConsoleMessage::Type::StartGroup;
206 case MessageType::StartGroupCollapsed: return Protocol::Console::ConsoleMessage::Type::StartGroupCollapsed;
207 case MessageType::EndGroup: return Protocol::Console::ConsoleMessage::Type::EndGroup;
208 case MessageType::Assert: return Protocol::Console::ConsoleMessage::Type::Assert;
209 case MessageType::Timing: return Protocol::Console::ConsoleMessage::Type::Timing;
210 case MessageType::Profile: return Protocol::Console::ConsoleMessage::Type::Profile;
211 case MessageType::ProfileEnd: return Protocol::Console::ConsoleMessage::Type::ProfileEnd;
212 case MessageType::Image: return Protocol::Console::ConsoleMessage::Type::Image;
213 }
214 return Protocol::Console::ConsoleMessage::Type::Log;
215}
216
217static Protocol::Console::ConsoleMessage::Level messageLevelValue(MessageLevel level)
218{
219 switch (level) {
220 case MessageLevel::Log: return Protocol::Console::ConsoleMessage::Level::Log;
221 case MessageLevel::Info: return Protocol::Console::ConsoleMessage::Level::Info;
222 case MessageLevel::Warning: return Protocol::Console::ConsoleMessage::Level::Warning;
223 case MessageLevel::Error: return Protocol::Console::ConsoleMessage::Level::Error;
224 case MessageLevel::Debug: return Protocol::Console::ConsoleMessage::Level::Debug;
225 }
226 return Protocol::Console::ConsoleMessage::Level::Log;
227}
228
229void ConsoleMessage::addToFrontend(ConsoleFrontendDispatcher& consoleFrontendDispatcher, InjectedScriptManager& injectedScriptManager, bool generatePreview)
230{
231 auto messageObject = Protocol::Console::ConsoleMessage::create()
232 .setSource(messageSourceValue(m_source))
233 .setLevel(messageLevelValue(m_level))
234 .setText(m_message)
235 .release();
236
237 // FIXME: only send out type for ConsoleAPI source messages.
238 messageObject->setType(messageTypeValue(m_type));
239 messageObject->setLine(static_cast<int>(m_line));
240 messageObject->setColumn(static_cast<int>(m_column));
241 messageObject->setUrl(m_url);
242 messageObject->setRepeatCount(static_cast<int>(m_repeatCount));
243
244 if (m_source == MessageSource::Network && !m_requestId.isEmpty())
245 messageObject->setNetworkRequestId(m_requestId);
246
247 if ((m_arguments && m_arguments->argumentCount()) || m_jsonLogValues.size()) {
248 InjectedScript injectedScript = injectedScriptManager.injectedScriptFor(scriptState());
249 if (!injectedScript.hasNoValue()) {
250 auto argumentsObject = JSON::ArrayOf<Protocol::Runtime::RemoteObject>::create();
251 if (m_arguments && m_arguments->argumentCount()) {
252 if (m_type == MessageType::Table && generatePreview && m_arguments->argumentCount()) {
253 auto table = m_arguments->argumentAt(0);
254 auto columns = m_arguments->argumentCount() > 1 ? m_arguments->argumentAt(1) : JSC::JSValue();
255 auto inspectorValue = injectedScript.wrapTable(table, columns);
256 if (!inspectorValue) {
257 ASSERT_NOT_REACHED();
258 return;
259 }
260 argumentsObject->addItem(WTFMove(inspectorValue));
261 if (m_arguments->argumentCount() > 1)
262 argumentsObject->addItem(injectedScript.wrapObject(columns, "console"_s, true));
263 } else {
264 for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) {
265 auto inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console"_s, generatePreview);
266 if (!inspectorValue) {
267 ASSERT_NOT_REACHED();
268 return;
269 }
270 argumentsObject->addItem(WTFMove(inspectorValue));
271 }
272 }
273 }
274
275 if (m_jsonLogValues.size()) {
276 for (auto& message : m_jsonLogValues) {
277 if (message.value.isEmpty())
278 continue;
279 auto inspectorValue = injectedScript.wrapJSONString(message.value, "console"_s, generatePreview);
280 if (!inspectorValue)
281 continue;
282
283 argumentsObject->addItem(WTFMove(inspectorValue));
284 }
285 }
286
287 if (argumentsObject->length())
288 messageObject->setParameters(WTFMove(argumentsObject));
289 }
290 }
291
292 if (m_callStack)
293 messageObject->setStackTrace(m_callStack->buildInspectorArray());
294
295 consoleFrontendDispatcher.messageAdded(WTFMove(messageObject));
296}
297
298void ConsoleMessage::updateRepeatCountInConsole(ConsoleFrontendDispatcher& consoleFrontendDispatcher)
299{
300 consoleFrontendDispatcher.messageRepeatCountUpdated(m_repeatCount);
301}
302
303bool ConsoleMessage::isEqual(ConsoleMessage* msg) const
304{
305 if (m_arguments) {
306 if (!msg->m_arguments || !m_arguments->isEqual(*msg->m_arguments))
307 return false;
308
309 // Never treat objects as equal - their properties might change over time.
310 for (size_t i = 0; i < m_arguments->argumentCount(); ++i) {
311 if (m_arguments->argumentAt(i).isObject())
312 return false;
313 }
314 } else if (msg->m_arguments)
315 return false;
316
317 if (m_callStack) {
318 if (!m_callStack->isEqual(msg->m_callStack.get()))
319 return false;
320 } else if (msg->m_callStack)
321 return false;
322
323 if (m_jsonLogValues.size() || msg->m_jsonLogValues.size())
324 return false;
325
326 return msg->m_source == m_source
327 && msg->m_type == m_type
328 && msg->m_level == m_level
329 && msg->m_message == m_message
330 && msg->m_line == m_line
331 && msg->m_column == m_column
332 && msg->m_url == m_url
333 && msg->m_requestId == m_requestId;
334}
335
336void ConsoleMessage::clear()
337{
338 if (!m_message)
339 m_message = "<message collected>"_s;
340
341 if (m_arguments)
342 m_arguments = nullptr;
343}
344
345JSC::ExecState* ConsoleMessage::scriptState() const
346{
347 if (m_arguments)
348 return m_arguments->globalState();
349
350 if (m_scriptState)
351 return m_scriptState;
352
353 return nullptr;
354}
355
356unsigned ConsoleMessage::argumentCount() const
357{
358 if (m_arguments)
359 return m_arguments->argumentCount();
360
361 return 0;
362}
363
364} // namespace Inspector
365