1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "core/frame/Console.h"
31
32 #include "bindings/v8/ScriptCallStackFactory.h"
33 #include "core/dom/Document.h"
34 #include "core/inspector/InspectorConsoleInstrumentation.h"
35 #include "core/inspector/ScriptArguments.h"
36 #include "platform/TraceEvent.h"
37 #include "wtf/text/CString.h"
38 #include "wtf/text/WTFString.h"
39
40 namespace WebCore {
41
~ConsoleBase()42 ConsoleBase::~ConsoleBase()
43 {
44 }
45
debug(ScriptState * state,PassRefPtr<ScriptArguments> arguments)46 void ConsoleBase::debug(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
47 {
48 internalAddMessage(LogMessageType, DebugMessageLevel, state, arguments);
49 }
50
error(ScriptState * state,PassRefPtr<ScriptArguments> arguments)51 void ConsoleBase::error(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
52 {
53 internalAddMessage(LogMessageType, ErrorMessageLevel, state, arguments);
54 }
55
info(ScriptState * state,PassRefPtr<ScriptArguments> arguments)56 void ConsoleBase::info(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
57 {
58 internalAddMessage(LogMessageType, InfoMessageLevel, state, arguments);
59 }
60
log(ScriptState * state,PassRefPtr<ScriptArguments> arguments)61 void ConsoleBase::log(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
62 {
63 internalAddMessage(LogMessageType, LogMessageLevel, state, arguments);
64 }
65
warn(ScriptState * state,PassRefPtr<ScriptArguments> arguments)66 void ConsoleBase::warn(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
67 {
68 internalAddMessage(LogMessageType, WarningMessageLevel, state, arguments);
69 }
70
dir(ScriptState * state,PassRefPtr<ScriptArguments> arguments)71 void ConsoleBase::dir(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
72 {
73 internalAddMessage(DirMessageType, LogMessageLevel, state, arguments);
74 }
75
dirxml(ScriptState * state,PassRefPtr<ScriptArguments> arguments)76 void ConsoleBase::dirxml(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
77 {
78 internalAddMessage(DirXMLMessageType, LogMessageLevel, state, arguments);
79 }
80
table(ScriptState * state,PassRefPtr<ScriptArguments> arguments)81 void ConsoleBase::table(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
82 {
83 internalAddMessage(TableMessageType, LogMessageLevel, state, arguments);
84 }
85
clear(ScriptState * state,PassRefPtr<ScriptArguments> arguments)86 void ConsoleBase::clear(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
87 {
88 InspectorInstrumentation::addMessageToConsole(context(), ConsoleAPIMessageSource, ClearMessageType, LogMessageLevel, String(), state, arguments);
89 }
90
trace(ScriptState * state,PassRefPtr<ScriptArguments> arguments)91 void ConsoleBase::trace(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
92 {
93 internalAddMessage(TraceMessageType, LogMessageLevel, state, arguments, true, true);
94 }
95
assertCondition(ScriptState * state,PassRefPtr<ScriptArguments> arguments,bool condition)96 void ConsoleBase::assertCondition(ScriptState* state, PassRefPtr<ScriptArguments> arguments, bool condition)
97 {
98 if (condition)
99 return;
100
101 internalAddMessage(AssertMessageType, ErrorMessageLevel, state, arguments, true);
102 }
103
count(ScriptState * state,PassRefPtr<ScriptArguments> arguments)104 void ConsoleBase::count(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
105 {
106 InspectorInstrumentation::consoleCount(context(), state, arguments);
107 }
108
markTimeline(const String & title)109 void ConsoleBase::markTimeline(const String& title)
110 {
111 InspectorInstrumentation::consoleTimeStamp(context(), title);
112 }
113
profile(ScriptState * state,const String & title)114 void ConsoleBase::profile(ScriptState* state, const String& title)
115 {
116 InspectorInstrumentation::consoleProfile(context(), title, state);
117 }
118
profileEnd(ScriptState * state,const String & title)119 void ConsoleBase::profileEnd(ScriptState* state, const String& title)
120 {
121 InspectorInstrumentation::consoleProfileEnd(context(), title);
122 }
123
time(const String & title)124 void ConsoleBase::time(const String& title)
125 {
126 InspectorInstrumentation::consoleTime(context(), title);
127 TRACE_EVENT_COPY_ASYNC_BEGIN0("webkit.console", title.utf8().data(), this);
128 }
129
timeEnd(ScriptState * state,const String & title)130 void ConsoleBase::timeEnd(ScriptState* state, const String& title)
131 {
132 TRACE_EVENT_COPY_ASYNC_END0("webkit.console", title.utf8().data(), this);
133 InspectorInstrumentation::consoleTimeEnd(context(), title, state);
134 }
135
timeStamp(const String & title)136 void ConsoleBase::timeStamp(const String& title)
137 {
138 InspectorInstrumentation::consoleTimeStamp(context(), title);
139 }
140
timeline(ScriptState * state,const String & title)141 void ConsoleBase::timeline(ScriptState* state, const String& title)
142 {
143 InspectorInstrumentation::consoleTimeline(context(), title, state);
144 }
145
timelineEnd(ScriptState * state,const String & title)146 void ConsoleBase::timelineEnd(ScriptState* state, const String& title)
147 {
148 InspectorInstrumentation::consoleTimelineEnd(context(), title, state);
149 }
150
group(ScriptState * state,PassRefPtr<ScriptArguments> arguments)151 void ConsoleBase::group(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
152 {
153 InspectorInstrumentation::addMessageToConsole(context(), ConsoleAPIMessageSource, StartGroupMessageType, LogMessageLevel, String(), state, arguments);
154 }
155
groupCollapsed(ScriptState * state,PassRefPtr<ScriptArguments> arguments)156 void ConsoleBase::groupCollapsed(ScriptState* state, PassRefPtr<ScriptArguments> arguments)
157 {
158 InspectorInstrumentation::addMessageToConsole(context(), ConsoleAPIMessageSource, StartGroupCollapsedMessageType, LogMessageLevel, String(), state, arguments);
159 }
160
groupEnd()161 void ConsoleBase::groupEnd()
162 {
163 InspectorInstrumentation::addMessageToConsole(context(), ConsoleAPIMessageSource, EndGroupMessageType, LogMessageLevel, String(), 0);
164 }
165
internalAddMessage(MessageType type,MessageLevel level,ScriptState * state,PassRefPtr<ScriptArguments> scriptArguments,bool acceptNoArguments,bool printTrace)166 void ConsoleBase::internalAddMessage(MessageType type, MessageLevel level, ScriptState* state, PassRefPtr<ScriptArguments> scriptArguments, bool acceptNoArguments, bool printTrace)
167 {
168 if (!context())
169 return;
170
171 RefPtr<ScriptArguments> arguments = scriptArguments;
172 if (!acceptNoArguments && !arguments->argumentCount())
173 return;
174
175 size_t stackSize = printTrace ? ScriptCallStack::maxCallStackSizeToCapture : 1;
176 RefPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(stackSize));
177
178 String message;
179 bool gotStringMessage = arguments->getFirstArgumentAsString(message);
180 InspectorInstrumentation::addMessageToConsole(context(), ConsoleAPIMessageSource, type, level, message, state, arguments);
181 if (gotStringMessage)
182 reportMessageToClient(level, message, callStack);
183 }
184
185 } // namespace WebCore
186