2010-12-22 Sheriff Bot <webkit.review.bot@gmail.com>
[WebKit-https.git] / WebCore / inspector / InspectorDebuggerAgent.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 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  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "InspectorDebuggerAgent.h"
32
33 #if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
34 #include "InjectedScript.h"
35 #include "InjectedScriptHost.h"
36 #include "InspectorFrontend.h"
37 #include "InspectorValues.h"
38 #include "PlatformString.h"
39 #include "ScriptDebugServer.h"
40 #include <wtf/MD5.h>
41 #include <wtf/text/StringConcatenate.h>
42
43 namespace WebCore {
44
45 static String formatBreakpointId(const String& sourceID, unsigned lineNumber)
46 {
47     return makeString(sourceID, ':', String::number(lineNumber));
48 }
49
50 PassOwnPtr<InspectorDebuggerAgent> InspectorDebuggerAgent::create(InspectorController* inspectorController, InspectorFrontend* frontend)
51 {
52     OwnPtr<InspectorDebuggerAgent> agent = adoptPtr(new InspectorDebuggerAgent(inspectorController, frontend));
53     ScriptDebugServer::shared().clearBreakpoints();
54     // FIXME(WK44513): breakpoints activated flag should be synchronized between all front-ends
55     ScriptDebugServer::shared().setBreakpointsActivated(true);
56     ScriptDebugServer::shared().addListener(agent.get(), inspectorController->inspectedPage());
57     return agent.release();
58 }
59
60 InspectorDebuggerAgent::InspectorDebuggerAgent(InspectorController* inspectorController, InspectorFrontend* frontend)
61     : m_inspectorController(inspectorController)
62     , m_frontend(frontend)
63     , m_pausedScriptState(0)
64     , m_breakpointsLoaded(false)
65     , m_javaScriptPauseScheduled(false)
66 {
67 }
68
69 InspectorDebuggerAgent::~InspectorDebuggerAgent()
70 {
71     ScriptDebugServer::shared().removeListener(this, m_inspectorController->inspectedPage());
72     m_pausedScriptState = 0;
73 }
74
75 bool InspectorDebuggerAgent::isDebuggerAlwaysEnabled()
76 {
77     return ScriptDebugServer::shared().isDebuggerAlwaysEnabled();
78 }
79
80 void InspectorDebuggerAgent::activateBreakpoints()
81 {
82     ScriptDebugServer::shared().activateBreakpoints();
83 }
84
85 void InspectorDebuggerAgent::deactivateBreakpoints()
86 {
87     ScriptDebugServer::shared().deactivateBreakpoints();
88 }
89
90 void InspectorDebuggerAgent::setBreakpoint(const String& sourceID, unsigned lineNumber, bool enabled, const String& condition, bool* success, unsigned int* actualLineNumber)
91 {
92     ScriptBreakpoint breakpoint(enabled, condition);
93     *success = ScriptDebugServer::shared().setBreakpoint(sourceID, breakpoint, lineNumber, actualLineNumber);
94     if (!*success)
95         return;
96
97     String url = m_sourceIDToURL.get(sourceID);
98     if (url.isEmpty())
99         return;
100
101     String breakpointId = formatBreakpointId(sourceID, *actualLineNumber);
102     m_breakpointsMapping.set(breakpointId, *actualLineNumber);
103
104     String key = md5Base16(url);
105     HashMap<String, SourceBreakpoints>::iterator it = m_stickyBreakpoints.find(key);
106     if (it == m_stickyBreakpoints.end())
107         it = m_stickyBreakpoints.set(key, SourceBreakpoints()).first;
108     it->second.set(*actualLineNumber, breakpoint);
109     saveBreakpoints();
110 }
111
112 void InspectorDebuggerAgent::removeBreakpoint(const String& sourceID, unsigned lineNumber)
113 {
114     ScriptDebugServer::shared().removeBreakpoint(sourceID, lineNumber);
115
116     String url = m_sourceIDToURL.get(sourceID);
117     if (url.isEmpty())
118         return;
119
120     String breakpointId = formatBreakpointId(sourceID, lineNumber);
121     HashMap<String, unsigned>::iterator mappingIt = m_breakpointsMapping.find(breakpointId);
122     if (mappingIt == m_breakpointsMapping.end())
123         return;
124     unsigned stickyLine = mappingIt->second;
125     m_breakpointsMapping.remove(mappingIt);
126
127     HashMap<String, SourceBreakpoints>::iterator it = m_stickyBreakpoints.find(md5Base16(url));
128     if (it == m_stickyBreakpoints.end())
129         return;
130
131     it->second.remove(stickyLine);
132     saveBreakpoints();
133 }
134
135 void InspectorDebuggerAgent::editScriptSource(const String& sourceID, const String& newContent, bool* success, String* result, RefPtr<InspectorValue>* newCallFrames)
136 {
137     if ((*success = ScriptDebugServer::shared().editScriptSource(sourceID, newContent, *result)))
138         *newCallFrames = currentCallFrames();
139 }
140
141 void InspectorDebuggerAgent::getScriptSource(const String& sourceID, String* scriptSource)
142 {
143     *scriptSource = m_scriptIDToContent.get(sourceID);
144 }
145
146 void InspectorDebuggerAgent::schedulePauseOnNextStatement(DebuggerEventType type, PassRefPtr<InspectorValue> data)
147 {
148     if (m_javaScriptPauseScheduled)
149         return;
150     m_breakProgramDetails = InspectorObject::create();
151     m_breakProgramDetails->setNumber("eventType", type);
152     m_breakProgramDetails->setValue("eventData", data);
153     ScriptDebugServer::shared().setPauseOnNextStatement(true);
154 }
155
156 void InspectorDebuggerAgent::cancelPauseOnNextStatement()
157 {
158     if (m_javaScriptPauseScheduled)
159         return;
160     m_breakProgramDetails = 0;
161     ScriptDebugServer::shared().setPauseOnNextStatement(false);
162 }
163
164 void InspectorDebuggerAgent::pause()
165 {
166     schedulePauseOnNextStatement(JavaScriptPauseEventType, InspectorObject::create());
167     m_javaScriptPauseScheduled = true;
168 }
169
170 void InspectorDebuggerAgent::resume()
171 {
172     ScriptDebugServer::shared().continueProgram();
173 }
174
175 void InspectorDebuggerAgent::stepOverStatement()
176 {
177     ScriptDebugServer::shared().stepOverStatement();
178 }
179
180 void InspectorDebuggerAgent::stepIntoStatement()
181 {
182     ScriptDebugServer::shared().stepIntoStatement();
183 }
184
185 void InspectorDebuggerAgent::stepOutOfFunction()
186 {
187     ScriptDebugServer::shared().stepOutOfFunction();
188 }
189
190 void InspectorDebuggerAgent::setPauseOnExceptionsState(long pauseState, long* newState)
191 {
192     ScriptDebugServer::shared().setPauseOnExceptionsState(static_cast<ScriptDebugServer::PauseOnExceptionsState>(pauseState));
193     *newState = ScriptDebugServer::shared().pauseOnExceptionsState();
194 }
195
196 long InspectorDebuggerAgent::pauseOnExceptionsState()
197 {
198     return ScriptDebugServer::shared().pauseOnExceptionsState();
199 }
200
201 void InspectorDebuggerAgent::clearForPageNavigation()
202 {
203     m_sourceIDToURL.clear();
204     m_scriptIDToContent.clear();
205     m_stickyBreakpoints.clear();
206     m_breakpointsMapping.clear();
207     m_breakpointsLoaded = false;
208 }
209
210 String InspectorDebuggerAgent::md5Base16(const String& string)
211 {
212     static const char digits[] = "0123456789abcdef";
213
214     MD5 md5;
215     md5.addBytes(reinterpret_cast<const uint8_t*>(string.characters()), string.length() * 2);
216     Vector<uint8_t, 16> digest;
217     md5.checksum(digest);
218
219     Vector<char, 32> result;
220     for (int i = 0; i < 16; ++i) {
221         result.append(digits[(digest[i] >> 4) & 0xf]);
222         result.append(digits[digest[i] & 0xf]);
223     }
224     return String(result.data(), result.size());
225 }
226
227 PassRefPtr<InspectorValue> InspectorDebuggerAgent::currentCallFrames()
228 {
229     if (!m_pausedScriptState)
230         return InspectorValue::null();
231     InjectedScript injectedScript = m_inspectorController->injectedScriptHost()->injectedScriptFor(m_pausedScriptState);
232     if (injectedScript.hasNoValue()) {
233         ASSERT_NOT_REACHED();
234         return InspectorValue::null();
235     }
236     return injectedScript.callFrames();
237 }
238
239 void InspectorDebuggerAgent::loadBreakpoints()
240 {
241     if (m_breakpointsLoaded)
242         return;
243     m_breakpointsLoaded = true;
244
245     RefPtr<InspectorValue> parsedSetting = m_inspectorController->loadBreakpoints();
246     if (!parsedSetting)
247         return;
248     RefPtr<InspectorObject> breakpoints = parsedSetting->asObject();
249     if (!breakpoints)
250         return;
251     for (InspectorObject::iterator it = breakpoints->begin(); it != breakpoints->end(); ++it) {
252         RefPtr<InspectorObject> breakpointsForURL = it->second->asObject();
253         if (!breakpointsForURL)
254             continue;
255         HashMap<String, SourceBreakpoints>::iterator sourceBreakpointsIt = m_stickyBreakpoints.set(it->first, SourceBreakpoints()).first;
256         ScriptBreakpoint::sourceBreakpointsFromInspectorObject(breakpointsForURL, &sourceBreakpointsIt->second);
257     }
258 }
259
260 void InspectorDebuggerAgent::saveBreakpoints()
261 {
262     RefPtr<InspectorObject> breakpoints = InspectorObject::create();
263     for (HashMap<String, SourceBreakpoints>::iterator it(m_stickyBreakpoints.begin()); it != m_stickyBreakpoints.end(); ++it) {
264         if (it->second.isEmpty())
265             continue;
266         RefPtr<InspectorObject> breakpointsForURL = ScriptBreakpoint::inspectorObjectFromSourceBreakpoints(it->second);
267         breakpoints->setObject(it->first, breakpointsForURL);
268     }
269     m_inspectorController->saveBreakpoints(breakpoints);
270 }
271
272 // JavaScriptDebugListener functions
273
274 void InspectorDebuggerAgent::didParseSource(const String& sourceID, const String& url, const String& data, int firstLine, ScriptWorldType worldType)
275 {
276     // Don't send script content to the front end until it's really needed.
277     m_frontend->parsedScriptSource(sourceID, url, "", firstLine, worldType);
278
279     m_scriptIDToContent.set(sourceID, data);
280
281     if (url.isEmpty())
282         return;
283
284     loadBreakpoints();
285     HashMap<String, SourceBreakpoints>::iterator it = m_stickyBreakpoints.find(md5Base16(url));
286     if (it != m_stickyBreakpoints.end()) {
287         for (SourceBreakpoints::iterator breakpointIt = it->second.begin(); breakpointIt != it->second.end(); ++breakpointIt) {
288             int lineNumber = breakpointIt->first;
289             if (firstLine > lineNumber)
290                 continue;
291             unsigned actualLineNumber = 0;
292             bool success = ScriptDebugServer::shared().setBreakpoint(sourceID, breakpointIt->second, lineNumber, &actualLineNumber);
293             if (!success)
294                 continue;
295             m_frontend->breakpointRestored(sourceID, url, actualLineNumber, breakpointIt->second.enabled, breakpointIt->second.condition);
296             String breakpointId = formatBreakpointId(sourceID, actualLineNumber);
297             m_breakpointsMapping.set(breakpointId, lineNumber);
298         }
299     }
300     m_sourceIDToURL.set(sourceID, url);
301 }
302
303 void InspectorDebuggerAgent::failedToParseSource(const String& url, const String& data, int firstLine, int errorLine, const String& errorMessage)
304 {
305     m_frontend->failedToParseScriptSource(url, data, firstLine, errorLine, errorMessage);
306 }
307
308 void InspectorDebuggerAgent::didPause(ScriptState* scriptState)
309 {
310     ASSERT(scriptState && !m_pausedScriptState);
311     m_pausedScriptState = scriptState;
312
313     if (!m_breakProgramDetails)
314         m_breakProgramDetails = InspectorObject::create();
315     m_breakProgramDetails->setValue("callFrames", currentCallFrames());
316
317     m_frontend->pausedScript(m_breakProgramDetails);
318     m_javaScriptPauseScheduled = false;
319 }
320
321 void InspectorDebuggerAgent::didContinue()
322 {
323     m_pausedScriptState = 0;
324     m_breakProgramDetails = 0;
325     m_frontend->resumedScript();
326 }
327
328 void InspectorDebuggerAgent::breakProgram(DebuggerEventType type, PassRefPtr<InspectorValue> data)
329 {
330     m_breakProgramDetails = InspectorObject::create();
331     m_breakProgramDetails->setNumber("eventType", type);
332     m_breakProgramDetails->setValue("eventData", data);
333     ScriptDebugServer::shared().breakProgram();
334 }
335
336 } // namespace WebCore
337
338 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)