• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Console.h"
31 
32 #include "CString.h"
33 #include "ChromeClient.h"
34 #include "ConsoleMessage.h"
35 #include "Frame.h"
36 #include "FrameLoader.h"
37 #include "FrameTree.h"
38 #include "InspectorController.h"
39 #include "Page.h"
40 #include "PageGroup.h"
41 #include "PlatformString.h"
42 
43 #if ENABLE(JAVASCRIPT_DEBUGGER)
44 #include <profiler/Profiler.h>
45 #endif
46 
47 #include "ScriptCallStack.h"
48 #include <stdio.h>
49 
50 namespace WebCore {
51 
Console(Frame * frame)52 Console::Console(Frame* frame)
53     : m_frame(frame)
54 {
55 }
56 
frame() const57 Frame* Console::frame() const
58 {
59     return m_frame;
60 }
61 
disconnectFrame()62 void Console::disconnectFrame()
63 {
64     m_frame = 0;
65 }
66 
printSourceURLAndLine(const String & sourceURL,unsigned lineNumber)67 static void printSourceURLAndLine(const String& sourceURL, unsigned lineNumber)
68 {
69     if (!sourceURL.isEmpty()) {
70         if (lineNumber > 0)
71             printf("%s:%d: ", sourceURL.utf8().data(), lineNumber);
72         else
73             printf("%s: ", sourceURL.utf8().data());
74     }
75 }
76 
getFirstArgumentAsString(ScriptState * scriptState,const ScriptCallFrame & callFrame,String & result,bool checkForNullOrUndefined=false)77 static bool getFirstArgumentAsString(ScriptState* scriptState, const ScriptCallFrame& callFrame, String& result, bool checkForNullOrUndefined = false)
78 {
79     if (!callFrame.argumentCount())
80         return false;
81 
82     const ScriptValue& value = callFrame.argumentAt(0);
83     if (checkForNullOrUndefined && (value.isNull() || value.isUndefined()))
84         return false;
85 
86     result = value.toString(scriptState);
87     return true;
88 }
89 
printMessageSourceAndLevelPrefix(MessageSource source,MessageLevel level)90 static void printMessageSourceAndLevelPrefix(MessageSource source, MessageLevel level)
91 {
92     const char* sourceString;
93     switch (source) {
94     case HTMLMessageSource:
95         sourceString = "HTML";
96         break;
97     case WMLMessageSource:
98         sourceString = "WML";
99         break;
100     case XMLMessageSource:
101         sourceString = "XML";
102         break;
103     case JSMessageSource:
104         sourceString = "JS";
105         break;
106     case CSSMessageSource:
107         sourceString = "CSS";
108         break;
109     case OtherMessageSource:
110         sourceString = "OTHER";
111         break;
112     default:
113         ASSERT_NOT_REACHED();
114         sourceString = "UNKNOWN";
115         break;
116     }
117 
118     const char* levelString;
119     switch (level) {
120     case TipMessageLevel:
121         levelString = "TIP";
122         break;
123     case LogMessageLevel:
124         levelString = "LOG";
125         break;
126     case WarningMessageLevel:
127         levelString = "WARN";
128         break;
129     case ErrorMessageLevel:
130         levelString = "ERROR";
131         break;
132     default:
133         ASSERT_NOT_REACHED();
134         levelString = "UNKNOWN";
135         break;
136     }
137 
138     printf("%s %s:", sourceString, levelString);
139 }
140 
addMessage(MessageSource source,MessageType type,MessageLevel level,const String & message,unsigned lineNumber,const String & sourceURL)141 void Console::addMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL)
142 {
143     Page* page = this->page();
144     if (!page)
145         return;
146 
147     if (source == JSMessageSource)
148         page->chrome()->client()->addMessageToConsole(source, type, level, message, lineNumber, sourceURL);
149 
150     page->inspectorController()->addMessageToConsole(source, type, level, message, lineNumber, sourceURL);
151 
152     if (!Console::shouldPrintExceptions())
153         return;
154 
155     printSourceURLAndLine(sourceURL, lineNumber);
156     printMessageSourceAndLevelPrefix(source, level);
157 
158     printf(" %s\n", message.utf8().data());
159 }
160 
addMessage(MessageType type,MessageLevel level,ScriptCallStack * callStack,bool acceptNoArguments)161 void Console::addMessage(MessageType type, MessageLevel level, ScriptCallStack* callStack, bool acceptNoArguments)
162 {
163     Page* page = this->page();
164     if (!page)
165         return;
166 
167     const ScriptCallFrame& lastCaller = callStack->at(0);
168 
169     if (!acceptNoArguments && !lastCaller.argumentCount())
170         return;
171 
172     String message;
173     if (getFirstArgumentAsString(callStack->state(), lastCaller, message))
174         page->chrome()->client()->addMessageToConsole(JSMessageSource, type, level, message, lastCaller.lineNumber(), lastCaller.sourceURL().prettyURL());
175 
176     page->inspectorController()->addMessageToConsole(JSMessageSource, type, level, callStack);
177 
178     if (!Console::shouldPrintExceptions())
179         return;
180 
181     printSourceURLAndLine(lastCaller.sourceURL().prettyURL(), 0);
182     printMessageSourceAndLevelPrefix(JSMessageSource, level);
183 
184     for (unsigned i = 0; i < lastCaller.argumentCount(); ++i) {
185         String argAsString;
186         if (lastCaller.argumentAt(i).getString(argAsString))
187             printf(" %s", argAsString.utf8().data());
188     }
189     printf("\n");
190 }
191 
debug(ScriptCallStack * callStack)192 void Console::debug(ScriptCallStack* callStack)
193 {
194     // In Firebug, console.debug has the same behavior as console.log. So we'll do the same.
195     log(callStack);
196 }
197 
error(ScriptCallStack * callStack)198 void Console::error(ScriptCallStack* callStack)
199 {
200     addMessage(LogMessageType, ErrorMessageLevel, callStack);
201 }
202 
info(ScriptCallStack * callStack)203 void Console::info(ScriptCallStack* callStack)
204 {
205     log(callStack);
206 }
207 
log(ScriptCallStack * callStack)208 void Console::log(ScriptCallStack* callStack)
209 {
210     addMessage(LogMessageType, LogMessageLevel, callStack);
211 }
212 
dir(ScriptCallStack * callStack)213 void Console::dir(ScriptCallStack* callStack)
214 {
215     addMessage(ObjectMessageType, LogMessageLevel, callStack);
216 }
217 
dirxml(ScriptCallStack * callStack)218 void Console::dirxml(ScriptCallStack* callStack)
219 {
220     // The standard behavior of our console.log will print the DOM tree for nodes.
221     log(callStack);
222 }
223 
trace(ScriptCallStack * callStack)224 void Console::trace(ScriptCallStack* callStack)
225 {
226     addMessage(TraceMessageType, LogMessageLevel, callStack, true);
227 
228     if (!shouldPrintExceptions())
229         return;
230 
231     printf("Stack Trace\n");
232     for (unsigned i = 0; i < callStack->size(); ++i) {
233         String functionName = String(callStack->at(i).functionName());
234         printf("\t%s\n", functionName.utf8().data());
235     }
236 }
237 
assertCondition(bool condition,ScriptCallStack * callStack)238 void Console::assertCondition(bool condition, ScriptCallStack* callStack)
239 {
240     if (condition)
241         return;
242 
243     // FIXME: <https://bugs.webkit.org/show_bug.cgi?id=19135> It would be nice to prefix assertion failures with a message like "Assertion failed: ".
244     addMessage(LogMessageType, ErrorMessageLevel, callStack, true);
245 }
246 
count(ScriptCallStack * callStack)247 void Console::count(ScriptCallStack* callStack)
248 {
249     Page* page = this->page();
250     if (!page)
251         return;
252 
253     const ScriptCallFrame& lastCaller = callStack->at(0);
254     // Follow Firebug's behavior of counting with null and undefined title in
255     // the same bucket as no argument
256     String title;
257     getFirstArgumentAsString(callStack->state(), lastCaller, title);
258 
259     page->inspectorController()->count(title, lastCaller.lineNumber(), lastCaller.sourceURL().string());
260 }
261 
262 #if ENABLE(WML)
lastWMLErrorMessage() const263 String Console::lastWMLErrorMessage() const
264 {
265     Page* page = this->page();
266     if (!page)
267         return String();
268 
269     const Vector<ConsoleMessage*>& consoleMessages = page->inspectorController()->consoleMessages();
270     if (consoleMessages.isEmpty())
271         return String();
272 
273     Vector<ConsoleMessage*>::const_iterator it = consoleMessages.begin();
274     const Vector<ConsoleMessage*>::const_iterator end = consoleMessages.end();
275 
276     for (; it != end; ++it) {
277         ConsoleMessage* message = *it;
278         if (message->source() != WMLMessageSource)
279             continue;
280 
281         return message->message();
282     }
283 
284     return String();
285 }
286 #endif
287 
288 #if ENABLE(JAVASCRIPT_DEBUGGER)
289 
profile(const JSC::UString & title,ScriptCallStack * callStack)290 void Console::profile(const JSC::UString& title, ScriptCallStack* callStack)
291 {
292     Page* page = this->page();
293     if (!page)
294         return;
295 
296     InspectorController* controller = page->inspectorController();
297     // FIXME: log a console message when profiling is disabled.
298     if (!controller->profilerEnabled())
299         return;
300 
301     JSC::UString resolvedTitle = title;
302     if (title.isNull())   // no title so give it the next user initiated profile title.
303         resolvedTitle = controller->getCurrentUserInitiatedProfileName(true);
304 
305     JSC::Profiler::profiler()->startProfiling(callStack->state(), resolvedTitle);
306 
307     const ScriptCallFrame& lastCaller = callStack->at(0);
308     controller->addStartProfilingMessageToConsole(resolvedTitle, lastCaller.lineNumber(), lastCaller.sourceURL());
309 }
310 
profileEnd(const JSC::UString & title,ScriptCallStack * callStack)311 void Console::profileEnd(const JSC::UString& title, ScriptCallStack* callStack)
312 {
313     Page* page = this->page();
314     if (!page)
315         return;
316 
317     if (!this->page())
318         return;
319 
320     InspectorController* controller = page->inspectorController();
321     if (!controller->profilerEnabled())
322         return;
323 
324     RefPtr<JSC::Profile> profile = JSC::Profiler::profiler()->stopProfiling(callStack->state(), title);
325     if (!profile)
326         return;
327 
328     m_profiles.append(profile);
329 
330     const ScriptCallFrame& lastCaller = callStack->at(0);
331     controller->addProfile(profile, lastCaller.lineNumber(), lastCaller.sourceURL());
332 }
333 
334 #endif
335 
time(const String & title)336 void Console::time(const String& title)
337 {
338     Page* page = this->page();
339     if (!page)
340         return;
341 
342     // Follow Firebug's behavior of requiring a title that is not null or
343     // undefined for timing functions
344     if (title.isNull())
345         return;
346 
347     page->inspectorController()->startTiming(title);
348 }
349 
timeEnd(const String & title,ScriptCallStack * callStack)350 void Console::timeEnd(const String& title, ScriptCallStack* callStack)
351 {
352     Page* page = this->page();
353     if (!page)
354         return;
355 
356     // Follow Firebug's behavior of requiring a title that is not null or
357     // undefined for timing functions
358     if (title.isNull())
359         return;
360 
361     double elapsed;
362     if (!page->inspectorController()->stopTiming(title, elapsed))
363         return;
364 
365     String message = title + String::format(": %.0fms", elapsed);
366 
367     const ScriptCallFrame& lastCaller = callStack->at(0);
368     page->inspectorController()->addMessageToConsole(JSMessageSource, LogMessageType, LogMessageLevel, message, lastCaller.lineNumber(), lastCaller.sourceURL().string());
369 }
370 
group(ScriptCallStack * callStack)371 void Console::group(ScriptCallStack* callStack)
372 {
373     Page* page = this->page();
374     if (!page)
375         return;
376 
377     page->inspectorController()->startGroup(JSMessageSource, callStack);
378 }
379 
groupEnd()380 void Console::groupEnd()
381 {
382     Page* page = this->page();
383     if (!page)
384         return;
385 
386     page->inspectorController()->endGroup(JSMessageSource, 0, String());
387 }
388 
warn(ScriptCallStack * callStack)389 void Console::warn(ScriptCallStack* callStack)
390 {
391     addMessage(LogMessageType, WarningMessageLevel, callStack);
392 }
393 
394 static bool printExceptions = false;
395 
shouldPrintExceptions()396 bool Console::shouldPrintExceptions()
397 {
398     return printExceptions;
399 }
400 
setShouldPrintExceptions(bool print)401 void Console::setShouldPrintExceptions(bool print)
402 {
403     printExceptions = print;
404 }
405 
page() const406 Page* Console::page() const
407 {
408     if (!m_frame)
409         return 0;
410     return m_frame->page();
411 }
412 
413 } // namespace WebCore
414