861e5aacf33a3a3b9828b3b2de7a75202343b176
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / frame / FrameConsole.cpp
1 /*
2  * Copyright (C) 2013 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/FrameConsole.h"
31
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"
46
47 namespace blink {
48
49 static const HashSet<int>& allClientReportingMessageTypes()
50 {
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);
60     }
61     return types;
62 }
63
64 namespace {
65
66 int muteCount = 0;
67
68 }
69
70 FrameConsole::FrameConsole(LocalFrame& frame)
71     : m_frame(&frame)
72 {
73 }
74
75 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(FrameConsole);
76
77 void FrameConsole::addMessage(PassRefPtrWillBeRawPtr<ConsoleMessage> prpConsoleMessage)
78 {
79     RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = prpConsoleMessage;
80     if (muteCount && consoleMessage->source() != ConsoleAPIMessageSource)
81         return;
82
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();
86     if (!context)
87         return;
88     if (!messageStorage())
89         return;
90
91     String messageURL;
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();
96     } else {
97         lineNumber = consoleMessage->lineNumber();
98         messageURL = consoleMessage->url();
99     }
100
101     messageStorage()->reportMessage(consoleMessage);
102
103     if (consoleMessage->source() == CSSMessageSource || consoleMessage->source() == NetworkMessageSource)
104         return;
105
106     RefPtrWillBeRawPtr<ScriptCallStack> reportedCallStack = nullptr;
107     if (consoleMessage->source() != ConsoleAPIMessageSource) {
108         if (consoleMessage->callStack() && frame().chromeClient().shouldReportDetailedMessageForSource(messageURL))
109             reportedCallStack = consoleMessage->callStack();
110     } else {
111         if (!frame().host() || (consoleMessage->scriptArguments() && !consoleMessage->scriptArguments()->argumentCount()))
112             return;
113
114         if (!allClientReportingMessageTypes().contains(consoleMessage->type()))
115             return;
116
117         if (frame().chromeClient().shouldReportDetailedMessageForSource(messageURL))
118             reportedCallStack = createScriptCallStack(ScriptCallStack::maxCallStackSizeToCapture);
119     }
120
121     String stackTrace;
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);
125 }
126
127 void FrameConsole::reportResourceResponseReceived(DocumentLoader* loader, unsigned long requestIdentifier, const ResourceResponse& response)
128 {
129     if (!loader)
130         return;
131     if (response.httpStatusCode() < 400)
132         return;
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());
137 }
138
139 String FrameConsole::formatStackTraceString(const String& originalMessage, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)
140 {
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(')');
152     }
153
154     return stackTrace.toString();
155 }
156
157 void FrameConsole::mute()
158 {
159     muteCount++;
160 }
161
162 void FrameConsole::unmute()
163 {
164     ASSERT(muteCount > 0);
165     muteCount--;
166 }
167
168 ConsoleMessageStorage* FrameConsole::messageStorage()
169 {
170     if (!m_frame->host())
171         return nullptr;
172     return &m_frame->host()->consoleMessageStorage();
173 }
174
175 void FrameConsole::clearMessages()
176 {
177     ConsoleMessageStorage* storage = messageStorage();
178     if (storage)
179         storage->clear();
180 }
181
182 void FrameConsole::adoptWorkerMessagesAfterTermination(WorkerGlobalScopeProxy* proxy)
183 {
184     ConsoleMessageStorage* storage = messageStorage();
185     if (storage)
186         storage->adoptWorkerMessagesAfterTermination(proxy);
187 }
188
189 void FrameConsole::didFailLoading(unsigned long requestIdentifier, const ResourceError& error)
190 {
191     if (error.isCancellation()) // Report failures only.
192         return;
193     ConsoleMessageStorage* storage = messageStorage();
194     if (!storage)
195         return;
196     StringBuilder message;
197     message.appendLiteral("Failed to load resource");
198     if (!error.localizedDescription().isEmpty()) {
199         message.appendLiteral(": ");
200         message.append(error.localizedDescription());
201     }
202     RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(NetworkMessageSource, ErrorMessageLevel, message.toString(), error.failingURL());
203     consoleMessage->setRequestIdentifier(requestIdentifier);
204     storage->reportMessage(consoleMessage.release());
205 }
206
207 void FrameConsole::trace(Visitor* visitor)
208 {
209     visitor->trace(m_frame);
210 }
211
212 } // namespace blink