blob: e21b862e97c1fbd6b831e40820c101fa831f3338 [file] [log] [blame]
weinig5e5675e2007-10-28 02:11:55 +00001/*
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
aroben@apple.coma7c639a2009-12-18 17:38:45 +000032#include "Chrome.h"
levin@chromium.org4e1469c2009-07-23 16:52:50 +000033#include "ChromeClient.h"
mkwst@chromium.org236e8982012-12-11 16:10:06 +000034#include "ConsoleAPITypes.h"
35#include "ConsoleTypes.h"
mkwst@chromium.orgd2e0adc2012-12-05 10:02:25 +000036#include "Document.h"
weinig5e5675e2007-10-28 02:11:55 +000037#include "Frame.h"
38#include "FrameLoader.h"
kmccullough@apple.comd9e61e02008-04-24 17:20:01 +000039#include "FrameTree.h"
yurys@chromium.org4fcd1c82011-01-24 10:20:57 +000040#include "InspectorConsoleInstrumentation.h"
commit-queue@webkit.org4c49a662011-01-02 22:27:10 +000041#include "InspectorController.h"
weinig5e5675e2007-10-28 02:11:55 +000042#include "Page.h"
commit-queue@webkit.org7195ba52013-03-19 15:54:14 +000043#include "PageConsole.h"
kmccullough@apple.comd9e61e02008-04-24 17:20:01 +000044#include "PageGroup.h"
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000045#include "ScriptArguments.h"
darin@chromium.orgc593adc2008-12-09 20:25:33 +000046#include "ScriptCallStack.h"
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +000047#include "ScriptCallStackFactory.h"
eric@webkit.orgcd8a5d52010-02-03 10:54:28 +000048#include "ScriptProfile.h"
49#include "ScriptProfiler.h"
mkwst@chromium.orgd2e0adc2012-12-05 10:02:25 +000050#include "ScriptableDocumentParser.h"
mkwst@chromium.orgf7c61792012-12-11 09:17:59 +000051#include "Settings.h"
joepeck@webkit.orga3198442013-12-11 22:40:23 +000052#include <bindings/ScriptValue.h>
darin@chromium.orgc593adc2008-12-09 20:25:33 +000053#include <stdio.h>
weinig@apple.com771c7a22011-05-01 23:56:41 +000054#include <wtf/text/CString.h>
weinig@apple.com3f5ab022012-09-06 17:36:48 +000055#include <wtf/text/WTFString.h>
weinig5e5675e2007-10-28 02:11:55 +000056
57namespace WebCore {
58
59Console::Console(Frame* frame)
abarth@webkit.org84fe60e2012-01-07 08:46:38 +000060 : DOMWindowProperty(frame)
weinig5e5675e2007-10-28 02:11:55 +000061{
62}
63
weinig@apple.comf25e6ff2011-05-11 19:03:20 +000064Console::~Console()
65{
66}
67
weinig@apple.com4da3e8d2013-09-14 01:30:06 +000068static void internalAddMessage(Page* page, MessageType type, MessageLevel level, JSC::ExecState* state, PassRefPtr<ScriptArguments> prpArguments, bool acceptNoArguments = false, bool printTrace = false)
ddkilzer@apple.com9c4dacc2009-07-11 05:36:29 +000069{
yurys@chromium.org4fcd1c82011-01-24 10:20:57 +000070 RefPtr<ScriptArguments> arguments = prpArguments;
yurys@chromium.org4fcd1c82011-01-24 10:20:57 +000071
darin@chromium.orgc593adc2008-12-09 20:25:33 +000072 if (!page)
73 return;
74
yurys@chromium.org02ff0d62010-11-08 14:05:32 +000075 if (!acceptNoArguments && !arguments->argumentCount())
darin@chromium.orgc593adc2008-12-09 20:25:33 +000076 return;
77
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +000078 size_t stackSize = printTrace ? ScriptCallStack::maxCallStackSizeToCapture : 1;
79 RefPtr<ScriptCallStack> callStack(createScriptCallStack(state, stackSize));
80 const ScriptCallFrame& lastCaller = callStack->at(0);
81
mkwst@chromium.orgf7c61792012-12-11 09:17:59 +000082 String message;
83 bool gotMessage = arguments->getFirstArgumentAsString(message);
timothy@apple.come133ede2013-01-16 22:52:45 +000084 InspectorInstrumentation::addMessageToConsole(page, ConsoleAPIMessageSource, type, level, message, state, arguments);
mkwst@chromium.orgf7c61792012-12-11 09:17:59 +000085
akling@apple.comff5990f2013-08-11 20:30:48 +000086 if (page->settings().privateBrowsingEnabled())
mkwst@chromium.orgf7c61792012-12-11 09:17:59 +000087 return;
88
89 if (gotMessage)
akling@apple.comfedaa632013-08-19 10:56:27 +000090 page->chrome().client().addMessageToConsole(ConsoleAPIMessageSource, type, level, message, lastCaller.lineNumber(), lastCaller.columnNumber(), lastCaller.sourceURL());
mkwst@chromium.orgf7c61792012-12-11 09:17:59 +000091
akling@apple.comff5990f2013-08-11 20:30:48 +000092 if (!page->settings().logsPageMessagesToSystemConsoleEnabled() && !PageConsole::shouldPrintExceptions())
timothy@apple.comd070f9c2013-01-17 22:32:22 +000093 return;
yurys@chromium.org97b7c4a2010-11-13 14:50:48 +000094
commit-queue@webkit.orgec979f82013-12-10 18:53:51 +000095 PageConsole::printSourceURLAndPosition(lastCaller.sourceURL(), lastCaller.lineNumber());
timothy@apple.com3fc29ba2014-01-10 22:16:17 +000096
97 printf(": ");
98
99 PageConsole::printMessageSourceAndLevelPrefix(ConsoleAPIMessageSource, level, printTrace);
timothy@apple.come133ede2013-01-16 22:52:45 +0000100
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000101 for (size_t i = 0; i < arguments->argumentCount(); ++i) {
102 String argAsString = arguments->argumentAt(i).toString(arguments->globalState());
103 printf(" %s", argAsString.utf8().data());
yurys@chromium.org97b7c4a2010-11-13 14:50:48 +0000104 }
105
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000106 printf("\n");
107
timothy@apple.com3fc29ba2014-01-10 22:16:17 +0000108 if (!printTrace)
109 return;
110
111 for (size_t i = 0; i < callStack->size(); ++i) {
112 const ScriptCallFrame& callFrame = callStack->at(i);
113
114 String functionName = String(callFrame.functionName());
115 if (functionName.isEmpty())
116 functionName = ASCIILiteral("(unknown)");
117
joepeck@webkit.orgcb25f562014-01-11 00:55:15 +0000118 printf("%lu: %s (", static_cast<unsigned long>(i), functionName.utf8().data());
timothy@apple.com3fc29ba2014-01-10 22:16:17 +0000119
120 PageConsole::printSourceURLAndPosition(callFrame.sourceURL(), callFrame.lineNumber());
121
122 printf(")\n");
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000123 }
darin@chromium.orgc593adc2008-12-09 20:25:33 +0000124}
125
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000126void Console::debug(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
aroben@apple.com4bd0bc4e2008-05-22 05:58:51 +0000127{
mkwst@chromium.org819c0242013-01-24 20:40:23 +0000128 internalAddMessage(page(), LogMessageType, DebugMessageLevel, state, arguments);
aroben@apple.com4bd0bc4e2008-05-22 05:58:51 +0000129}
130
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000131void Console::error(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
weinig5e5675e2007-10-28 02:11:55 +0000132{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000133 internalAddMessage(page(), LogMessageType, ErrorMessageLevel, state, arguments);
weinig5e5675e2007-10-28 02:11:55 +0000134}
135
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000136void Console::info(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
weinig5e5675e2007-10-28 02:11:55 +0000137{
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000138 log(state, arguments);
weinig5e5675e2007-10-28 02:11:55 +0000139}
140
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000141void Console::log(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
weinig5e5675e2007-10-28 02:11:55 +0000142{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000143 internalAddMessage(page(), LogMessageType, LogMessageLevel, state, arguments);
weinig5e5675e2007-10-28 02:11:55 +0000144}
145
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000146void Console::warn(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
vsevik@chromium.org9cd1b012012-04-19 15:34:31 +0000147{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000148 internalAddMessage(page(), LogMessageType, WarningMessageLevel, state, arguments);
vsevik@chromium.org9cd1b012012-04-19 15:34:31 +0000149}
150
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000151void Console::dir(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
timothy@apple.com7310b6a2008-08-15 17:35:22 +0000152{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000153 internalAddMessage(page(), DirMessageType, LogMessageLevel, state, arguments);
timothy@apple.com7310b6a2008-08-15 17:35:22 +0000154}
155
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000156void Console::dirxml(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
timothy@apple.com968ebc92008-09-07 16:48:41 +0000157{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000158 internalAddMessage(page(), DirXMLMessageType, LogMessageLevel, state, arguments);
timothy@apple.com968ebc92008-09-07 16:48:41 +0000159}
160
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000161void Console::table(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
pfeldman@chromium.orgc2268702013-02-18 10:51:37 +0000162{
163 internalAddMessage(page(), TableMessageType, LogMessageLevel, state, arguments);
164}
165
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000166void Console::clear(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
commit-queue@webkit.org8e628312012-11-06 16:14:27 +0000167{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000168 internalAddMessage(page(), ClearMessageType, LogMessageLevel, state, arguments, true);
commit-queue@webkit.org8e628312012-11-06 16:14:27 +0000169}
170
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000171void Console::trace(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
timothy@apple.com345f1af2008-09-27 22:18:35 +0000172{
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000173 internalAddMessage(page(), TraceMessageType, LogMessageLevel, state, arguments, true, true);
timothy@apple.com345f1af2008-09-27 22:18:35 +0000174}
175
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000176void Console::assertCondition(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments, bool condition)
aroben@apple.comd1b1b1f2008-05-20 23:05:51 +0000177{
178 if (condition)
179 return;
180
timothy@apple.comd070f9c2013-01-17 22:32:22 +0000181 internalAddMessage(page(), AssertMessageType, ErrorMessageLevel, state, arguments, true);
aroben@apple.comd1b1b1f2008-05-20 23:05:51 +0000182}
183
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000184void Console::count(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
timothy@apple.com7d08c322008-08-19 18:33:27 +0000185{
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000186 InspectorInstrumentation::consoleCount(page(), state, arguments);
timothy@apple.com7d08c322008-08-19 18:33:27 +0000187}
188
weinig@apple.com1dc3b822013-09-14 18:29:29 +0000189void Console::profile(JSC::ExecState* state, const String& title)
kmccullough@apple.coma014dc72008-04-18 21:50:35 +0000190{
timothy@apple.com7dec1b92008-10-28 20:42:34 +0000191 Page* page = this->page();
192 if (!page)
193 return;
194
levin@chromium.org4e1469c2009-07-23 16:52:50 +0000195 // FIXME: log a console message when profiling is disabled.
loislo@chromium.org043f7d32011-01-21 12:30:56 +0000196 if (!InspectorInstrumentation::profilerEnabled(page))
timothy@apple.com7dec1b92008-10-28 20:42:34 +0000197 return;
198
eric@webkit.orgcd8a5d52010-02-03 10:54:28 +0000199 String resolvedTitle = title;
200 if (title.isNull()) // no title so give it the next user initiated profile title.
loislo@chromium.org043f7d32011-01-21 12:30:56 +0000201 resolvedTitle = InspectorInstrumentation::getCurrentUserInitiatedProfileName(page, true);
kmccullough@apple.comd65049f2009-03-03 22:47:49 +0000202
yurys@chromium.org02ff0d62010-11-08 14:05:32 +0000203 ScriptProfiler::start(state, resolvedTitle);
kmccullough@apple.comcd1d51b2009-06-22 18:51:22 +0000204
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000205 RefPtr<ScriptCallStack> callStack(createScriptCallStack(state, 1));
kmccullough@apple.comcd1d51b2009-06-22 18:51:22 +0000206 const ScriptCallFrame& lastCaller = callStack->at(0);
joepeck@webkit.org379c8a12013-04-25 18:31:05 +0000207 InspectorInstrumentation::addStartProfilingMessageToConsole(page, resolvedTitle, lastCaller.lineNumber(), lastCaller.columnNumber(), lastCaller.sourceURL());
kmccullough@apple.coma014dc72008-04-18 21:50:35 +0000208}
209
weinig@apple.com1dc3b822013-09-14 18:29:29 +0000210void Console::profileEnd(JSC::ExecState* state, const String& title)
kmccullough@apple.coma014dc72008-04-18 21:50:35 +0000211{
timothy@apple.com7dec1b92008-10-28 20:42:34 +0000212 Page* page = this->page();
213 if (!page)
214 return;
215
loislo@chromium.org043f7d32011-01-21 12:30:56 +0000216 if (!InspectorInstrumentation::profilerEnabled(page))
timothy@apple.com7dec1b92008-10-28 20:42:34 +0000217 return;
218
yurys@chromium.org02ff0d62010-11-08 14:05:32 +0000219 RefPtr<ScriptProfile> profile = ScriptProfiler::stop(state, title);
kmccullough@apple.com88219342008-09-30 22:33:30 +0000220 if (!profile)
221 return;
kmccullough@apple.comb0f4c622008-09-03 21:53:17 +0000222
kmccullough@apple.com0435f782008-10-22 23:04:00 +0000223 m_profiles.append(profile);
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000224 RefPtr<ScriptCallStack> callStack(createScriptCallStack(state, 1));
yurys@chromium.org4fcd1c82011-01-24 10:20:57 +0000225 InspectorInstrumentation::addProfile(page, profile, callStack);
kmccullough@apple.comc9005ae2008-06-20 19:36:10 +0000226}
227
darin@chromium.orgc593adc2008-12-09 20:25:33 +0000228void Console::time(const String& title)
229{
caseq@chromium.org221060d2012-08-10 13:42:47 +0000230 InspectorInstrumentation::startConsoleTiming(m_frame, title);
mrowe@apple.com8ef61772008-07-27 05:44:30 +0000231}
232
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000233void Console::timeEnd(JSC::ExecState* state, const String& title)
mrowe@apple.com8ef61772008-07-27 05:44:30 +0000234{
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000235 RefPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(state));
236 InspectorInstrumentation::stopConsoleTiming(m_frame, title, callStack.release());
mrowe@apple.com8ef61772008-07-27 05:44:30 +0000237}
238
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000239void Console::timeStamp(PassRefPtr<ScriptArguments> arguments)
commit-queue@webkit.org058fb452011-07-15 10:06:31 +0000240{
caseq@chromium.org221060d2012-08-10 13:42:47 +0000241 InspectorInstrumentation::consoleTimeStamp(m_frame, arguments);
commit-queue@webkit.org058fb452011-07-15 10:06:31 +0000242}
243
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000244void Console::group(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
timothy@apple.com7be8b232008-07-29 18:04:29 +0000245{
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000246 InspectorInstrumentation::addMessageToConsole(page(), ConsoleAPIMessageSource, StartGroupMessageType, LogMessageLevel, String(), state, arguments);
timothy@apple.com7be8b232008-07-29 18:04:29 +0000247}
248
weinig@apple.com4da3e8d2013-09-14 01:30:06 +0000249void Console::groupCollapsed(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
jberlin@webkit.org73086142010-05-26 13:44:21 +0000250{
mkwst@chromium.orgd643a8e2012-11-16 12:32:53 +0000251 InspectorInstrumentation::addMessageToConsole(page(), ConsoleAPIMessageSource, StartGroupCollapsedMessageType, LogMessageLevel, String(), state, arguments);
jberlin@webkit.org73086142010-05-26 13:44:21 +0000252}
253
timothy@apple.com7be8b232008-07-29 18:04:29 +0000254void Console::groupEnd()
255{
joepeck@webkit.org379c8a12013-04-25 18:31:05 +0000256 InspectorInstrumentation::addMessageToConsole(page(), ConsoleAPIMessageSource, EndGroupMessageType, LogMessageLevel, String(), String(), 0, 0);
timothy@apple.com7be8b232008-07-29 18:04:29 +0000257}
258
kmccullough@apple.come8ac2f32008-08-19 16:38:06 +0000259Page* Console::page() const
260{
261 if (!m_frame)
262 return 0;
kmccullough@apple.come8ac2f32008-08-19 16:38:06 +0000263 return m_frame->page();
264}
265
weinig5e5675e2007-10-28 02:11:55 +0000266} // namespace WebCore