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