1/*
2 * Copyright (C) 2014 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#if ENABLE(INSPECTOR)
31
32#include "CommandLineAPIHost.h"
33#include "DOMWindow.h"
34#include "ResourceError.h"
35#include "ResourceResponse.h"
36#include "ScriptState.h"
37#include "WebInjectedScriptManager.h"
38#include <inspector/ConsoleMessage.h>
39#include <runtime/JSCInlines.h>
40#include <wtf/text/StringBuilder.h>
41
42using namespace Inspector;
43
44namespace WebCore {
45
46WebConsoleAgent::WebConsoleAgent(WebInjectedScriptManager* injectedScriptManager)
47    : InspectorConsoleAgent(injectedScriptManager)
48    , m_monitoringXHREnabled(false)
49{
50}
51
52void WebConsoleAgent::setMonitoringXHREnabled(ErrorString*, bool enabled)
53{
54    m_monitoringXHREnabled = enabled;
55}
56
57void WebConsoleAgent::frameWindowDiscarded(DOMWindow* window)
58{
59    size_t messageCount = m_consoleMessages.size();
60    for (size_t i = 0; i < messageCount; ++i) {
61        JSC::ExecState* exec = m_consoleMessages[i]->scriptState();
62        if (!exec)
63            continue;
64        if (domWindowFromExecState(exec) != window)
65            continue;
66        m_consoleMessages[i]->clear();
67    }
68
69    static_cast<WebInjectedScriptManager*>(m_injectedScriptManager)->discardInjectedScriptsFor(window);
70}
71
72void WebConsoleAgent::didFinishXHRLoading(unsigned long requestIdentifier, const String& url, const String& sendURL, unsigned sendLineNumber, unsigned sendColumnNumber)
73{
74    if (!m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled())
75        return;
76
77    if (m_frontendDispatcher && m_monitoringXHREnabled) {
78        String message = "XHR finished loading: \"" + url + "\".";
79        addMessageToConsole(MessageSource::Network, MessageType::Log, MessageLevel::Debug, message, sendURL, sendLineNumber, sendColumnNumber, nullptr, requestIdentifier);
80    }
81}
82
83void WebConsoleAgent::didReceiveResponse(unsigned long requestIdentifier, const ResourceResponse& response)
84{
85    if (!m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled())
86        return;
87
88    if (response.httpStatusCode() >= 400) {
89        String message = "Failed to load resource: the server responded with a status of " + String::number(response.httpStatusCode()) + " (" + response.httpStatusText() + ')';
90        addMessageToConsole(MessageSource::Network, MessageType::Log, MessageLevel::Error, message, response.url().string(), 0, 0, nullptr, requestIdentifier);
91    }
92}
93
94void WebConsoleAgent::didFailLoading(unsigned long requestIdentifier, const ResourceError& error)
95{
96    if (!m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled())
97        return;
98
99    // Report failures only.
100    if (error.isCancellation())
101        return;
102
103    StringBuilder message;
104    message.appendLiteral("Failed to load resource");
105    if (!error.localizedDescription().isEmpty()) {
106        message.appendLiteral(": ");
107        message.append(error.localizedDescription());
108    }
109
110    addMessageToConsole(MessageSource::Network, MessageType::Log, MessageLevel::Error, message.toString(), error.failingURL(), 0, 0, nullptr, requestIdentifier);
111}
112
113} // namespace WebCore
114
115#endif // ENABLE(INSPECTOR)
116