2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "core/frame/FrameConsole.h"
32 #include "bindings/core/v8/ScriptCallStackFactory.h"
33 #include "core/frame/FrameHost.h"
34 #include "core/inspector/ConsoleAPITypes.h"
35 #include "core/inspector/ConsoleMessage.h"
36 #include "core/inspector/ConsoleMessageStorage.h"
37 #include "core/inspector/InspectorConsoleInstrumentation.h"
38 #include "core/inspector/ScriptCallStack.h"
39 #include "core/page/Chrome.h"
40 #include "core/page/ChromeClient.h"
41 #include "core/page/Page.h"
42 #include "core/workers/WorkerGlobalScopeProxy.h"
43 #include "platform/network/ResourceError.h"
44 #include "platform/network/ResourceResponse.h"
45 #include "wtf/text/StringBuilder.h"
49 static const HashSet<int>& allClientReportingMessageTypes()
51 DEFINE_STATIC_LOCAL(HashSet<int>, types, ());
52 if (types.isEmpty()) {
53 types.add(LogMessageType);
54 types.add(DirMessageType);
55 types.add(DirXMLMessageType);
56 types.add(TableMessageType);
57 types.add(TraceMessageType);
58 types.add(ClearMessageType);
59 types.add(AssertMessageType);
70 FrameConsole::FrameConsole(LocalFrame& frame)
75 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(FrameConsole);
77 void FrameConsole::addMessage(PassRefPtrWillBeRawPtr<ConsoleMessage> prpConsoleMessage)
79 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = prpConsoleMessage;
80 if (muteCount && consoleMessage->source() != ConsoleAPIMessageSource)
83 // FIXME: This should not need to reach for the main-frame.
84 // Inspector code should just take the current frame and know how to walk itself.
85 ExecutionContext* context = frame().document();
88 if (!messageStorage())
92 unsigned lineNumber = 0;
93 if (consoleMessage->callStack() && consoleMessage->callStack()->size()) {
94 lineNumber = consoleMessage->callStack()->at(0).lineNumber();
95 messageURL = consoleMessage->callStack()->at(0).sourceURL();
97 lineNumber = consoleMessage->lineNumber();
98 messageURL = consoleMessage->url();
101 messageStorage()->reportMessage(consoleMessage);
103 if (consoleMessage->source() == CSSMessageSource || consoleMessage->source() == NetworkMessageSource)
106 RefPtrWillBeRawPtr<ScriptCallStack> reportedCallStack = nullptr;
107 if (consoleMessage->source() != ConsoleAPIMessageSource) {
108 if (consoleMessage->callStack() && frame().chromeClient().shouldReportDetailedMessageForSource(messageURL))
109 reportedCallStack = consoleMessage->callStack();
111 if (!frame().host() || (consoleMessage->scriptArguments() && !consoleMessage->scriptArguments()->argumentCount()))
114 if (!allClientReportingMessageTypes().contains(consoleMessage->type()))
117 if (frame().chromeClient().shouldReportDetailedMessageForSource(messageURL))
118 reportedCallStack = createScriptCallStack(ScriptCallStack::maxCallStackSizeToCapture);
122 if (reportedCallStack)
123 stackTrace = FrameConsole::formatStackTraceString(consoleMessage->message(), reportedCallStack);
124 frame().chromeClient().addMessageToConsole(m_frame, consoleMessage->source(), consoleMessage->level(), consoleMessage->message(), lineNumber, messageURL, stackTrace);
127 void FrameConsole::reportResourceResponseReceived(DocumentLoader* loader, unsigned long requestIdentifier, const ResourceResponse& response)
131 if (response.httpStatusCode() < 400)
133 String message = "Failed to load resource: the server responded with a status of " + String::number(response.httpStatusCode()) + " (" + response.httpStatusText() + ')';
134 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(NetworkMessageSource, ErrorMessageLevel, message, response.url().string());
135 consoleMessage->setRequestIdentifier(requestIdentifier);
136 addMessage(consoleMessage.release());
139 String FrameConsole::formatStackTraceString(const String& originalMessage, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)
141 StringBuilder stackTrace;
142 for (size_t i = 0; i < callStack->size(); ++i) {
143 const ScriptCallFrame& frame = callStack->at(i);
144 stackTrace.append("\n at " + (frame.functionName().length() ? frame.functionName() : "(anonymous function)"));
145 stackTrace.appendLiteral(" (");
146 stackTrace.append(frame.sourceURL());
147 stackTrace.append(':');
148 stackTrace.appendNumber(frame.lineNumber());
149 stackTrace.append(':');
150 stackTrace.appendNumber(frame.columnNumber());
151 stackTrace.append(')');
154 return stackTrace.toString();
157 void FrameConsole::mute()
162 void FrameConsole::unmute()
164 ASSERT(muteCount > 0);
168 ConsoleMessageStorage* FrameConsole::messageStorage()
170 if (!m_frame->host())
172 return &m_frame->host()->consoleMessageStorage();
175 void FrameConsole::clearMessages()
177 ConsoleMessageStorage* storage = messageStorage();
182 void FrameConsole::adoptWorkerMessagesAfterTermination(WorkerGlobalScopeProxy* proxy)
184 ConsoleMessageStorage* storage = messageStorage();
186 storage->adoptWorkerMessagesAfterTermination(proxy);
189 void FrameConsole::didFailLoading(unsigned long requestIdentifier, const ResourceError& error)
191 if (error.isCancellation()) // Report failures only.
193 ConsoleMessageStorage* storage = messageStorage();
196 StringBuilder message;
197 message.appendLiteral("Failed to load resource");
198 if (!error.localizedDescription().isEmpty()) {
199 message.appendLiteral(": ");
200 message.append(error.localizedDescription());
202 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(NetworkMessageSource, ErrorMessageLevel, message.toString(), error.failingURL());
203 consoleMessage->setRequestIdentifier(requestIdentifier);
204 storage->reportMessage(consoleMessage.release());
207 void FrameConsole::trace(Visitor* visitor)
209 visitor->trace(m_frame);