1/*
2 * Copyright (C) 2011 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "PageDebuggerAgent.h"
33
34#if ENABLE(INSPECTOR)
35
36#include "CachedResource.h"
37#include "InspectorOverlay.h"
38#include "InspectorPageAgent.h"
39#include "InstrumentingAgents.h"
40#include "Page.h"
41#include "PageConsole.h"
42#include "PageScriptDebugServer.h"
43#include "ScriptState.h"
44#include <inspector/InjectedScript.h>
45#include <inspector/InjectedScriptManager.h>
46#include <inspector/ScriptCallStack.h>
47#include <inspector/ScriptCallStackFactory.h>
48
49using namespace Inspector;
50
51namespace WebCore {
52
53PageDebuggerAgent::PageDebuggerAgent(InjectedScriptManager* injectedScriptManager, InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorOverlay* overlay)
54    : WebDebuggerAgent(injectedScriptManager, instrumentingAgents)
55    , m_pageAgent(pageAgent)
56    , m_overlay(overlay)
57    , m_scriptDebugServer(*pageAgent->page())
58{
59}
60
61void PageDebuggerAgent::enable()
62{
63    WebDebuggerAgent::enable();
64    m_instrumentingAgents->setPageDebuggerAgent(this);
65}
66
67void PageDebuggerAgent::disable(bool isBeingDestroyed)
68{
69    WebDebuggerAgent::disable(isBeingDestroyed);
70    m_instrumentingAgents->setPageDebuggerAgent(nullptr);
71}
72
73String PageDebuggerAgent::sourceMapURLForScript(const Script& script)
74{
75    DEPRECATED_DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeader, (ASCIILiteral("SourceMap")));
76    DEPRECATED_DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeaderDeprecated, (ASCIILiteral("X-SourceMap")));
77
78    if (!script.url.isEmpty()) {
79        CachedResource* resource = m_pageAgent->cachedResource(m_pageAgent->mainFrame(), URL(ParsedURLString, script.url));
80        if (resource) {
81            String sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeader);
82            if (!sourceMapHeader.isEmpty())
83                return sourceMapHeader;
84
85            sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeaderDeprecated);
86            if (!sourceMapHeader.isEmpty())
87                return sourceMapHeader;
88        }
89    }
90
91    return InspectorDebuggerAgent::sourceMapURLForScript(script);
92}
93
94void PageDebuggerAgent::startListeningScriptDebugServer()
95{
96    scriptDebugServer().addListener(this);
97}
98
99void PageDebuggerAgent::stopListeningScriptDebugServer(bool isBeingDestroyed)
100{
101    scriptDebugServer().removeListener(this, isBeingDestroyed);
102}
103
104PageScriptDebugServer& PageDebuggerAgent::scriptDebugServer()
105{
106    return m_scriptDebugServer;
107}
108
109void PageDebuggerAgent::muteConsole()
110{
111    PageConsole::mute();
112}
113
114void PageDebuggerAgent::unmuteConsole()
115{
116    PageConsole::unmute();
117}
118
119void PageDebuggerAgent::breakpointActionLog(JSC::ExecState* exec, const String& message)
120{
121    m_pageAgent->page()->console().addMessage(MessageSource::JS, MessageLevel::Log, message, createScriptCallStack(exec, ScriptCallStack::maxCallStackSizeToCapture));
122}
123
124InjectedScript PageDebuggerAgent::injectedScriptForEval(ErrorString* errorString, const int* executionContextId)
125{
126    if (!executionContextId) {
127        JSC::ExecState* scriptState = mainWorldExecState(m_pageAgent->mainFrame());
128        return injectedScriptManager()->injectedScriptFor(scriptState);
129    }
130
131    InjectedScript injectedScript = injectedScriptManager()->injectedScriptForId(*executionContextId);
132    if (injectedScript.hasNoValue())
133        *errorString = ASCIILiteral("Execution context with given id not found.");
134
135    return injectedScript;
136}
137
138void PageDebuggerAgent::setOverlayMessage(ErrorString*, const String* message)
139{
140    m_overlay->setPausedInDebuggerMessage(message);
141}
142
143void PageDebuggerAgent::didClearMainFrameWindowObject()
144{
145    didClearGlobalObject();
146}
147
148} // namespace WebCore
149
150#endif // ENABLE(INSPECTOR)
151