Web Inspector: Move InspectorDebuggerAgent into JavaScriptCore
[WebKit-https.git] / Source / WebCore / inspector / PageDebuggerAgent.cpp
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(JAVASCRIPT_DEBUGGER) && 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
47 using namespace Inspector;
48
49 namespace WebCore {
50
51 PageDebuggerAgent::PageDebuggerAgent(InjectedScriptManager* injectedScriptManager, InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorOverlay* overlay)
52     : WebDebuggerAgent(injectedScriptManager, instrumentingAgents)
53     , m_pageAgent(pageAgent)
54     , m_overlay(overlay)
55 {
56 }
57
58 void PageDebuggerAgent::enable()
59 {
60     WebDebuggerAgent::enable();
61     m_instrumentingAgents->setPageDebuggerAgent(this);
62 }
63
64 void PageDebuggerAgent::disable(bool isBeingDestroyed)
65 {
66     WebDebuggerAgent::disable(isBeingDestroyed);
67     m_instrumentingAgents->setPageDebuggerAgent(nullptr);
68 }
69
70 String PageDebuggerAgent::sourceMapURLForScript(const Script& script)
71 {
72     DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeader, (ASCIILiteral("SourceMap")));
73     DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeaderDeprecated, (ASCIILiteral("X-SourceMap")));
74
75     if (!script.url.isEmpty()) {
76         CachedResource* resource = m_pageAgent->cachedResource(m_pageAgent->mainFrame(), URL(ParsedURLString, script.url));
77         if (resource) {
78             String sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeader);
79             if (!sourceMapHeader.isEmpty())
80                 return sourceMapHeader;
81
82             sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeaderDeprecated);
83             if (!sourceMapHeader.isEmpty())
84                 return sourceMapHeader;
85         }
86     }
87
88     return InspectorDebuggerAgent::sourceMapURLForScript(script);
89 }
90
91 void PageDebuggerAgent::startListeningScriptDebugServer()
92 {
93     scriptDebugServer().addListener(this, m_pageAgent->page());
94 }
95
96 void PageDebuggerAgent::stopListeningScriptDebugServer(bool isBeingDestroyed)
97 {
98     scriptDebugServer().removeListener(this, m_pageAgent->page(), isBeingDestroyed);
99 }
100
101 PageScriptDebugServer& PageDebuggerAgent::scriptDebugServer()
102 {
103     return PageScriptDebugServer::shared();
104 }
105
106 void PageDebuggerAgent::muteConsole()
107 {
108     PageConsole::mute();
109 }
110
111 void PageDebuggerAgent::unmuteConsole()
112 {
113     PageConsole::unmute();
114 }
115
116 void PageDebuggerAgent::breakpointActionLog(JSC::ExecState*, const String& message)
117 {
118     m_pageAgent->page()->console().addMessage(JSMessageSource, LogMessageLevel, message);
119 }
120
121 InjectedScript PageDebuggerAgent::injectedScriptForEval(ErrorString* errorString, const int* executionContextId)
122 {
123     if (!executionContextId) {
124         JSC::ExecState* scriptState = mainWorldExecState(m_pageAgent->mainFrame());
125         return injectedScriptManager()->injectedScriptFor(scriptState);
126     }
127
128     InjectedScript injectedScript = injectedScriptManager()->injectedScriptForId(*executionContextId);
129     if (injectedScript.hasNoValue())
130         *errorString = ASCIILiteral("Execution context with given id not found.");
131
132     return injectedScript;
133 }
134
135 void PageDebuggerAgent::setOverlayMessage(ErrorString*, const String* message)
136 {
137     m_overlay->setPausedInDebuggerMessage(message);
138 }
139
140 void PageDebuggerAgent::didClearMainFrameWindowObject()
141 {
142     didClearGlobalObject();
143 }
144
145 } // namespace WebCore
146
147 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)