2 * Copyright (C) 2007, 2008, 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
32 #include "ConsoleMessage.h"
36 #include "IdentifiersFactory.h"
37 #include "InjectedScript.h"
38 #include "InjectedScriptManager.h"
39 #include "InspectorValues.h"
40 #include "ScriptArguments.h"
41 #include "ScriptCallFrame.h"
42 #include "ScriptCallStack.h"
43 #include "ScriptCallStackFactory.h"
44 #include "ScriptValue.h"
48 ConsoleMessage::ConsoleMessage(MessageSource source
, MessageType type
, MessageLevel level
, const String
& message
, unsigned long requestIdentifier
)
57 , m_requestId(IdentifiersFactory::requestId(requestIdentifier
))
61 ConsoleMessage::ConsoleMessage(MessageSource source
, MessageType type
, MessageLevel level
, const String
& message
, const String
& url
, unsigned line
, unsigned column
, JSC::ExecState
* state
, unsigned long requestIdentifier
)
70 , m_requestId(IdentifiersFactory::requestId(requestIdentifier
))
72 autogenerateMetadata(state
);
75 ConsoleMessage::ConsoleMessage(MessageSource source
, MessageType type
, MessageLevel level
, const String
& message
, PassRefPtr
<ScriptCallStack
> callStack
, unsigned long requestIdentifier
)
84 , m_requestId(IdentifiersFactory::requestId(requestIdentifier
))
86 m_callStack
= callStack
;
88 const ScriptCallFrame
* frame
= m_callStack
? m_callStack
->firstNonNativeCallFrame() : nullptr;
90 m_url
= frame
->sourceURL();
91 m_line
= frame
->lineNumber();
92 m_column
= frame
->columnNumber();
96 ConsoleMessage::ConsoleMessage(MessageSource source
, MessageType type
, MessageLevel level
, const String
& message
, PassRefPtr
<ScriptArguments
> arguments
, JSC::ExecState
* state
, unsigned long requestIdentifier
)
101 , m_arguments(arguments
)
106 , m_requestId(IdentifiersFactory::requestId(requestIdentifier
))
108 autogenerateMetadata(state
);
111 ConsoleMessage::~ConsoleMessage()
115 void ConsoleMessage::autogenerateMetadata(JSC::ExecState
* state
)
120 if (m_type
== MessageType::EndGroup
)
123 // FIXME: Should this really be using "for console" in the generic ConsoleMessage autogeneration? This can skip the first frame.
124 m_callStack
= createScriptCallStackForConsole(state
, ScriptCallStack::maxCallStackSizeToCapture
);
126 if (const ScriptCallFrame
* frame
= m_callStack
->firstNonNativeCallFrame()) {
127 m_url
= frame
->sourceURL();
128 m_line
= frame
->lineNumber();
129 m_column
= frame
->columnNumber();
134 static Inspector::TypeBuilder::Console::ConsoleMessage::Source::Enum
messageSourceValue(MessageSource source
)
137 case MessageSource::XML
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::XML
;
138 case MessageSource::JS
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Javascript
;
139 case MessageSource::Network
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Network
;
140 case MessageSource::ConsoleAPI
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::ConsoleAPI
;
141 case MessageSource::Storage
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Storage
;
142 case MessageSource::AppCache
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Appcache
;
143 case MessageSource::Rendering
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Rendering
;
144 case MessageSource::CSS
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::CSS
;
145 case MessageSource::Security
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Security
;
146 case MessageSource::Other
: return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Other
;
148 return Inspector::TypeBuilder::Console::ConsoleMessage::Source::Other
;
151 static Inspector::TypeBuilder::Console::ConsoleMessage::Type::Enum
messageTypeValue(MessageType type
)
154 case MessageType::Log
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Log
;
155 case MessageType::Clear
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Clear
;
156 case MessageType::Dir
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Dir
;
157 case MessageType::DirXML
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::DirXML
;
158 case MessageType::Table
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Table
;
159 case MessageType::Trace
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Trace
;
160 case MessageType::StartGroup
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::StartGroup
;
161 case MessageType::StartGroupCollapsed
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::StartGroupCollapsed
;
162 case MessageType::EndGroup
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::EndGroup
;
163 case MessageType::Assert
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Assert
;
164 case MessageType::Timing
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Timing
;
165 case MessageType::Profile
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Profile
;
166 case MessageType::ProfileEnd
: return Inspector::TypeBuilder::Console::ConsoleMessage::Type::ProfileEnd
;
168 return Inspector::TypeBuilder::Console::ConsoleMessage::Type::Log
;
171 static Inspector::TypeBuilder::Console::ConsoleMessage::Level::Enum
messageLevelValue(MessageLevel level
)
174 case MessageLevel::Log
: return Inspector::TypeBuilder::Console::ConsoleMessage::Level::Log
;
175 case MessageLevel::Warning
: return Inspector::TypeBuilder::Console::ConsoleMessage::Level::Warning
;
176 case MessageLevel::Error
: return Inspector::TypeBuilder::Console::ConsoleMessage::Level::Error
;
177 case MessageLevel::Debug
: return Inspector::TypeBuilder::Console::ConsoleMessage::Level::Debug
;
179 return Inspector::TypeBuilder::Console::ConsoleMessage::Level::Log
;
182 void ConsoleMessage::addToFrontend(InspectorConsoleFrontendDispatcher
* consoleFrontendDispatcher
, Inspector::InjectedScriptManager
* injectedScriptManager
, bool generatePreview
)
184 RefPtr
<Inspector::TypeBuilder::Console::ConsoleMessage
> jsonObj
= Inspector::TypeBuilder::Console::ConsoleMessage::create()
185 .setSource(messageSourceValue(m_source
))
186 .setLevel(messageLevelValue(m_level
))
189 // FIXME: only send out type for ConsoleAPI source messages.
190 jsonObj
->setType(messageTypeValue(m_type
));
191 jsonObj
->setLine(static_cast<int>(m_line
));
192 jsonObj
->setColumn(static_cast<int>(m_column
));
193 jsonObj
->setUrl(m_url
);
194 jsonObj
->setRepeatCount(static_cast<int>(m_repeatCount
));
196 if (m_source
== MessageSource::Network
&& !m_requestId
.isEmpty())
197 jsonObj
->setNetworkRequestId(m_requestId
);
199 if (m_arguments
&& m_arguments
->argumentCount()) {
200 InjectedScript injectedScript
= injectedScriptManager
->injectedScriptFor(m_arguments
->globalState());
201 if (!injectedScript
.hasNoValue()) {
202 RefPtr
<Inspector::TypeBuilder::Array
<Inspector::TypeBuilder::Runtime::RemoteObject
>> jsonArgs
= Inspector::TypeBuilder::Array
<Inspector::TypeBuilder::Runtime::RemoteObject
>::create();
203 if (m_type
== MessageType::Table
&& generatePreview
&& m_arguments
->argumentCount()) {
204 Deprecated::ScriptValue table
= m_arguments
->argumentAt(0);
205 Deprecated::ScriptValue columns
= m_arguments
->argumentCount() > 1 ? m_arguments
->argumentAt(1) : Deprecated::ScriptValue();
206 RefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
> inspectorValue
= injectedScript
.wrapTable(table
, columns
);
207 if (!inspectorValue
) {
208 ASSERT_NOT_REACHED();
211 jsonArgs
->addItem(inspectorValue
);
213 for (unsigned i
= 0; i
< m_arguments
->argumentCount(); ++i
) {
214 RefPtr
<Inspector::TypeBuilder::Runtime::RemoteObject
> inspectorValue
= injectedScript
.wrapObject(m_arguments
->argumentAt(i
), "console", generatePreview
);
215 if (!inspectorValue
) {
216 ASSERT_NOT_REACHED();
219 jsonArgs
->addItem(inspectorValue
);
222 jsonObj
->setParameters(jsonArgs
);
227 jsonObj
->setStackTrace(m_callStack
->buildInspectorArray());
229 consoleFrontendDispatcher
->messageAdded(jsonObj
);
232 void ConsoleMessage::updateRepeatCountInConsole(InspectorConsoleFrontendDispatcher
* consoleFrontendDispatcher
)
234 consoleFrontendDispatcher
->messageRepeatCountUpdated(m_repeatCount
);
237 bool ConsoleMessage::isEqual(ConsoleMessage
* msg
) const
240 if (!m_arguments
->isEqual(msg
->m_arguments
.get()))
243 // Never treat objects as equal - their properties might change over time.
244 for (size_t i
= 0; i
< m_arguments
->argumentCount(); ++i
) {
245 if (m_arguments
->argumentAt(i
).isObject())
248 } else if (msg
->m_arguments
)
252 if (!m_callStack
->isEqual(msg
->m_callStack
.get()))
254 } else if (msg
->m_callStack
)
257 return msg
->m_source
== m_source
258 && msg
->m_type
== m_type
259 && msg
->m_level
== m_level
260 && msg
->m_message
== m_message
261 && msg
->m_line
== m_line
262 && msg
->m_column
== m_column
263 && msg
->m_url
== m_url
264 && msg
->m_requestId
== m_requestId
;
267 void ConsoleMessage::clear()
270 m_message
= ASCIILiteral("<message collected>");
276 JSC::ExecState
* ConsoleMessage::scriptState() const
279 return m_arguments
->globalState();
284 unsigned ConsoleMessage::argumentCount() const
287 return m_arguments
->argumentCount();
292 } // namespace Inspector
294 #endif // ENABLE(INSPECTOR)