2 * Copyright (C) 2007, 2008 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 Computer, 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 "core/inspector/InspectorConsoleMessage.h"
34 #include "bindings/core/v8/ScriptCallStackFactory.h"
35 #include "bindings/core/v8/ScriptValue.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "core/inspector/IdentifiersFactory.h"
38 #include "core/inspector/InjectedScript.h"
39 #include "core/inspector/InjectedScriptManager.h"
40 #include "core/inspector/ScriptArguments.h"
41 #include "core/inspector/ScriptAsyncCallStack.h"
42 #include "core/inspector/ScriptCallFrame.h"
43 #include "core/inspector/ScriptCallStack.h"
44 #include "wtf/CurrentTime.h"
48 InspectorConsoleMessage::InspectorConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message)
57 , m_requestId(IdentifiersFactory::requestId(0))
58 , m_timestamp(WTF::currentTime())
59 , m_workerProxy(nullptr)
61 autogenerateMetadata();
64 InspectorConsoleMessage::InspectorConsoleMessage(bool shouldGenerateCallStack, MessageSource source, MessageType type, MessageLevel level, const String& message, const String& url, unsigned line, unsigned column, ScriptState* scriptState, unsigned long requestIdentifier)
69 , m_scriptState(scriptState)
73 , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
74 , m_timestamp(WTF::currentTime())
75 , m_workerProxy(nullptr)
77 autogenerateMetadata(shouldGenerateCallStack);
80 InspectorConsoleMessage::InspectorConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack, unsigned long requestIdentifier)
86 , m_arguments(nullptr)
87 , m_callStack(callStack)
90 , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
91 , m_timestamp(WTF::currentTime())
92 , m_workerProxy(nullptr)
94 autogenerateMetadata(false);
97 InspectorConsoleMessage::InspectorConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtrWillBeRawPtr<ScriptArguments> arguments, ScriptState* scriptState, unsigned long requestIdentifier)
102 , m_scriptState(scriptState)
103 , m_arguments(arguments)
107 , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
108 , m_timestamp(WTF::currentTime())
109 , m_workerProxy(nullptr)
111 autogenerateMetadata();
114 InspectorConsoleMessage::~InspectorConsoleMessage()
118 void InspectorConsoleMessage::autogenerateMetadata(bool shouldGenerateCallStack)
120 if (m_type == EndGroupMessageType)
123 if (shouldGenerateCallStack)
124 m_callStack = createScriptCallStackForConsole(ScriptCallStack::maxCallStackSizeToCapture, true);
126 if (m_callStack && m_callStack->size()) {
127 const ScriptCallFrame& frame = m_callStack->at(0);
128 m_url = frame.sourceURL();
129 m_line = frame.lineNumber();
130 m_column = frame.columnNumber();
137 static TypeBuilder::Console::ConsoleMessage::Source::Enum messageSourceValue(MessageSource source)
140 case XMLMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Xml;
141 case JSMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Javascript;
142 case NetworkMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Network;
143 case ConsoleAPIMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Console_api;
144 case StorageMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Storage;
145 case AppCacheMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Appcache;
146 case RenderingMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Rendering;
147 case CSSMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Css;
148 case SecurityMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Security;
149 case OtherMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Other;
150 case DeprecationMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Deprecation;
152 return TypeBuilder::Console::ConsoleMessage::Source::Other;
155 static TypeBuilder::Console::ConsoleMessage::Type::Enum messageTypeValue(MessageType type)
158 case LogMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Log;
159 case ClearMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Clear;
160 case DirMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Dir;
161 case DirXMLMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Dirxml;
162 case TableMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Table;
163 case TraceMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Trace;
164 case StartGroupMessageType: return TypeBuilder::Console::ConsoleMessage::Type::StartGroup;
165 case StartGroupCollapsedMessageType: return TypeBuilder::Console::ConsoleMessage::Type::StartGroupCollapsed;
166 case EndGroupMessageType: return TypeBuilder::Console::ConsoleMessage::Type::EndGroup;
167 case AssertMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Assert;
169 return TypeBuilder::Console::ConsoleMessage::Type::Log;
172 static TypeBuilder::Console::ConsoleMessage::Level::Enum messageLevelValue(MessageLevel level)
175 case DebugMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Debug;
176 case LogMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Log;
177 case WarningMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Warning;
178 case ErrorMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Error;
179 case InfoMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Info;
181 return TypeBuilder::Console::ConsoleMessage::Level::Log;
184 void InspectorConsoleMessage::addToFrontend(InspectorFrontend::Console* frontend, InjectedScriptManager* injectedScriptManager, bool generatePreview)
189 RefPtr<TypeBuilder::Console::ConsoleMessage> jsonObj = TypeBuilder::Console::ConsoleMessage::create()
190 .setSource(messageSourceValue(m_source))
191 .setLevel(messageLevelValue(m_level))
193 .setTimestamp(m_timestamp);
194 // FIXME: only send out type for ConsoleAPI source messages.
195 jsonObj->setType(messageTypeValue(m_type));
196 jsonObj->setLine(static_cast<int>(m_line));
197 jsonObj->setColumn(static_cast<int>(m_column));
198 jsonObj->setUrl(m_url);
199 ScriptState* scriptState = m_scriptState.get();
201 jsonObj->setExecutionContextId(injectedScriptManager->injectedScriptIdFor(scriptState));
202 if (m_source == NetworkMessageSource && !m_requestId.isEmpty())
203 jsonObj->setNetworkRequestId(m_requestId);
204 if (m_arguments && m_arguments->argumentCount()) {
205 InjectedScript injectedScript = injectedScriptManager->injectedScriptFor(m_arguments->scriptState());
206 if (!injectedScript.isEmpty()) {
207 RefPtr<TypeBuilder::Array<TypeBuilder::Runtime::RemoteObject> > jsonArgs = TypeBuilder::Array<TypeBuilder::Runtime::RemoteObject>::create();
208 if (m_type == TableMessageType && generatePreview && m_arguments->argumentCount()) {
209 ScriptValue table = m_arguments->argumentAt(0);
210 ScriptValue columns = m_arguments->argumentCount() > 1 ? m_arguments->argumentAt(1) : ScriptValue();
211 RefPtr<TypeBuilder::Runtime::RemoteObject> inspectorValue = injectedScript.wrapTable(table, columns);
212 if (!inspectorValue) {
213 ASSERT_NOT_REACHED();
216 jsonArgs->addItem(inspectorValue);
218 for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) {
219 RefPtr<TypeBuilder::Runtime::RemoteObject> inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console", generatePreview);
220 if (!inspectorValue) {
221 ASSERT_NOT_REACHED();
224 jsonArgs->addItem(inspectorValue);
227 jsonObj->setParameters(jsonArgs);
231 jsonObj->setStackTrace(m_callStack->buildInspectorArray());
232 RefPtrWillBeRawPtr<ScriptAsyncCallStack> asyncCallStack = m_callStack->asyncCallStack();
234 jsonObj->setAsyncStackTrace(asyncCallStack->buildInspectorObject());
236 frontend->messageAdded(jsonObj);
240 void InspectorConsoleMessage::windowCleared(LocalDOMWindow* window)
242 if (m_scriptState.get() && m_scriptState.get()->domWindow() == window)
243 m_scriptState.clear();
247 if (m_arguments->scriptState()->domWindow() != window)
250 m_message = "<message collected>";
254 unsigned InspectorConsoleMessage::argumentCount()
257 return m_arguments->argumentCount();