1/*
2 * Copyright (C) 2014, 2015 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#include "config.h"
28#include "WebConsoleAgent.h"
29
30#include "CommandLineAPIHost.h"
31#include "DOMWindow.h"
32#include "Logging.h"
33#include "ResourceError.h"
34#include "ResourceResponse.h"
35#include "ScriptState.h"
36#include "WebInjectedScriptManager.h"
37#include <JavaScriptCore/ConsoleMessage.h>
38#include <JavaScriptCore/JSCInlines.h>
39#include <wtf/text/StringBuilder.h>
40
41
42namespace WebCore {
43
44using namespace Inspector;
45
46WebConsoleAgent::WebConsoleAgent(AgentContext& context)
47 : InspectorConsoleAgent(context)
48{
49}
50
51void WebConsoleAgent::frameWindowDiscarded(DOMWindow* window)
52{
53 if (!m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled())
54 return;
55
56 for (auto& message : m_consoleMessages) {
57 JSC::ExecState* exec = message->scriptState();
58 if (!exec)
59 continue;
60 if (domWindowFromExecState(exec) != window)
61 continue;
62 message->clear();
63 }
64
65 static_cast<WebInjectedScriptManager&>(m_injectedScriptManager).discardInjectedScriptsFor(window);
66}
67
68void WebConsoleAgent::didReceiveResponse(unsigned long requestIdentifier, const ResourceResponse& response)
69{
70 if (!m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled())
71 return;
72
73 if (response.httpStatusCode() >= 400) {
74 String message = makeString("Failed to load resource: the server responded with a status of ", response.httpStatusCode(), " (", response.httpStatusText(), ')');
75 addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::Network, MessageType::Log, MessageLevel::Error, message, response.url().string(), 0, 0, nullptr, requestIdentifier));
76 }
77}
78
79void WebConsoleAgent::didFailLoading(unsigned long requestIdentifier, const ResourceError& error)
80{
81 if (!m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled())
82 return;
83
84 // Report failures only.
85 if (error.isCancellation())
86 return;
87
88 StringBuilder message;
89 message.appendLiteral("Failed to load resource");
90 if (!error.localizedDescription().isEmpty()) {
91 message.appendLiteral(": ");
92 message.append(error.localizedDescription());
93 }
94
95 addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::Network, MessageType::Log, MessageLevel::Error, message.toString(), error.failingURL(), 0, 0, nullptr, requestIdentifier));
96}
97
98} // namespace WebCore
99