Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorConsoleMessage.cpp
1 /*
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.
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 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.
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 "core/inspector/InspectorConsoleMessage.h"
33
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"
45
46 namespace blink {
47
48 InspectorConsoleMessage::InspectorConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message)
49     : m_source(source)
50     , m_type(type)
51     , m_level(level)
52     , m_message(message)
53     , m_scriptState(0)
54     , m_url()
55     , m_line(0)
56     , m_column(0)
57     , m_requestId(IdentifiersFactory::requestId(0))
58     , m_timestamp(WTF::currentTime())
59     , m_workerProxy(nullptr)
60 {
61     autogenerateMetadata();
62 }
63
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)
65     : m_source(source)
66     , m_type(type)
67     , m_level(level)
68     , m_message(message)
69     , m_scriptState(scriptState)
70     , m_url(url)
71     , m_line(line)
72     , m_column(column)
73     , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
74     , m_timestamp(WTF::currentTime())
75     , m_workerProxy(nullptr)
76 {
77     autogenerateMetadata(shouldGenerateCallStack);
78 }
79
80 InspectorConsoleMessage::InspectorConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack, unsigned long requestIdentifier)
81     : m_source(source)
82     , m_type(type)
83     , m_level(level)
84     , m_message(message)
85     , m_scriptState(0)
86     , m_arguments(nullptr)
87     , m_callStack(callStack)
88     , m_line(0)
89     , m_column(0)
90     , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
91     , m_timestamp(WTF::currentTime())
92     , m_workerProxy(nullptr)
93 {
94     autogenerateMetadata(false);
95 }
96
97 InspectorConsoleMessage::InspectorConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtrWillBeRawPtr<ScriptArguments> arguments, ScriptState* scriptState, unsigned long requestIdentifier)
98     : m_source(source)
99     , m_type(type)
100     , m_level(level)
101     , m_message(message)
102     , m_scriptState(scriptState)
103     , m_arguments(arguments)
104     , m_url()
105     , m_line(0)
106     , m_column(0)
107     , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
108     , m_timestamp(WTF::currentTime())
109     , m_workerProxy(nullptr)
110 {
111     autogenerateMetadata();
112 }
113
114 InspectorConsoleMessage::~InspectorConsoleMessage()
115 {
116 }
117
118 void InspectorConsoleMessage::autogenerateMetadata(bool shouldGenerateCallStack)
119 {
120     if (m_type == EndGroupMessageType)
121         return;
122
123     if (shouldGenerateCallStack)
124         m_callStack = createScriptCallStackForConsole(ScriptCallStack::maxCallStackSizeToCapture, true);
125
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();
131         return;
132     }
133
134     m_callStack.clear();
135 }
136
137 static TypeBuilder::Console::ConsoleMessage::Source::Enum messageSourceValue(MessageSource source)
138 {
139     switch (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;
151     }
152     return TypeBuilder::Console::ConsoleMessage::Source::Other;
153 }
154
155 static TypeBuilder::Console::ConsoleMessage::Type::Enum messageTypeValue(MessageType type)
156 {
157     switch (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;
168     }
169     return TypeBuilder::Console::ConsoleMessage::Type::Log;
170 }
171
172 static TypeBuilder::Console::ConsoleMessage::Level::Enum messageLevelValue(MessageLevel level)
173 {
174     switch (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;
180     }
181     return TypeBuilder::Console::ConsoleMessage::Level::Log;
182 }
183
184 void InspectorConsoleMessage::addToFrontend(InspectorFrontend::Console* frontend, InjectedScriptManager* injectedScriptManager, bool generatePreview)
185 {
186     if (m_workerProxy)
187         return;
188
189     RefPtr<TypeBuilder::Console::ConsoleMessage> jsonObj = TypeBuilder::Console::ConsoleMessage::create()
190         .setSource(messageSourceValue(m_source))
191         .setLevel(messageLevelValue(m_level))
192         .setText(m_message)
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();
200     if (scriptState)
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();
214                     return;
215                 }
216                 jsonArgs->addItem(inspectorValue);
217             } else {
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();
222                         return;
223                     }
224                     jsonArgs->addItem(inspectorValue);
225                 }
226             }
227             jsonObj->setParameters(jsonArgs);
228         }
229     }
230     if (m_callStack) {
231         jsonObj->setStackTrace(m_callStack->buildInspectorArray());
232         RefPtrWillBeRawPtr<ScriptAsyncCallStack> asyncCallStack = m_callStack->asyncCallStack();
233         if (asyncCallStack)
234             jsonObj->setAsyncStackTrace(asyncCallStack->buildInspectorObject());
235     }
236     frontend->messageAdded(jsonObj);
237     frontend->flush();
238 }
239
240 void InspectorConsoleMessage::windowCleared(LocalDOMWindow* window)
241 {
242     if (m_scriptState.get() && m_scriptState.get()->domWindow() == window)
243         m_scriptState.clear();
244
245     if (!m_arguments)
246         return;
247     if (m_arguments->scriptState()->domWindow() != window)
248         return;
249     if (!m_message)
250         m_message = "<message collected>";
251     m_arguments.clear();
252 }
253
254 unsigned InspectorConsoleMessage::argumentCount()
255 {
256     if (m_arguments)
257         return m_arguments->argumentCount();
258     return 0;
259 }
260
261 } // namespace blink