Web Inspector: Breakpoints in sourceURL named scripts do not work
[WebKit-https.git] / Source / WebCore / inspector / InspectorDebuggerAgent.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2010-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  *
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
32 #if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
33 #include "InspectorDebuggerAgent.h"
34
35 #include "CachedResource.h"
36 #include "ContentSearchUtils.h"
37 #include "InjectedScript.h"
38 #include "InjectedScriptManager.h"
39 #include "InspectorFrontend.h"
40 #include "InspectorPageAgent.h"
41 #include "InspectorState.h"
42 #include "InspectorValues.h"
43 #include "InstrumentingAgents.h"
44 #include "RegularExpression.h"
45 #include "ScriptDebugServer.h"
46 #include "ScriptObject.h"
47 #include <wtf/text/WTFString.h>
48
49 using WebCore::TypeBuilder::Array;
50 using WebCore::TypeBuilder::Debugger::FunctionDetails;
51 using WebCore::TypeBuilder::Debugger::ScriptId;
52 using WebCore::TypeBuilder::Runtime::RemoteObject;
53
54 namespace WebCore {
55
56 namespace DebuggerAgentState {
57 static const char debuggerEnabled[] = "debuggerEnabled";
58 static const char javaScriptBreakpoints[] = "javaScriptBreakopints";
59 static const char pauseOnExceptionsState[] = "pauseOnExceptionsState";
60 };
61
62 const char* InspectorDebuggerAgent::backtraceObjectGroup = "backtrace";
63
64 InspectorDebuggerAgent::InspectorDebuggerAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager)
65     : InspectorBaseAgent<InspectorDebuggerAgent>("Debugger", instrumentingAgents, inspectorState)
66     , m_injectedScriptManager(injectedScriptManager)
67     , m_frontend(0)
68     , m_pausedScriptState(0)
69     , m_javaScriptPauseScheduled(false)
70     , m_listener(0)
71 {
72     // FIXME: make breakReason optional so that there was no need to init it with "other".
73     clearBreakDetails();
74     m_state->setLong(DebuggerAgentState::pauseOnExceptionsState, ScriptDebugServer::DontPauseOnExceptions);
75 }
76
77 InspectorDebuggerAgent::~InspectorDebuggerAgent()
78 {
79     ASSERT(!m_instrumentingAgents->inspectorDebuggerAgent());
80 }
81
82 void InspectorDebuggerAgent::enable()
83 {
84     m_instrumentingAgents->setInspectorDebuggerAgent(this);
85
86     // FIXME(WK44513): breakpoints activated flag should be synchronized between all front-ends
87     scriptDebugServer().setBreakpointsActivated(true);
88     startListeningScriptDebugServer();
89
90     if (m_listener)
91         m_listener->debuggerWasEnabled();
92 }
93
94 void InspectorDebuggerAgent::disable()
95 {
96     m_state->setObject(DebuggerAgentState::javaScriptBreakpoints, InspectorObject::create());
97     m_state->setLong(DebuggerAgentState::pauseOnExceptionsState, ScriptDebugServer::DontPauseOnExceptions);
98     m_instrumentingAgents->setInspectorDebuggerAgent(0);
99
100     stopListeningScriptDebugServer();
101     scriptDebugServer().clearBreakpoints();
102     scriptDebugServer().clearCompiledScripts();
103     scriptDebugServer().continueProgram();
104     clear();
105
106     if (m_listener)
107         m_listener->debuggerWasDisabled();
108 }
109
110 bool InspectorDebuggerAgent::enabled()
111 {
112     return m_state->getBoolean(DebuggerAgentState::debuggerEnabled);
113 }
114
115 void InspectorDebuggerAgent::causesRecompilation(ErrorString*, bool* result)
116 {
117     *result = scriptDebugServer().causesRecompilation();
118 }
119
120 void InspectorDebuggerAgent::canSetScriptSource(ErrorString*, bool* result)
121 {
122     *result = scriptDebugServer().canSetScriptSource();
123 }
124
125 void InspectorDebuggerAgent::supportsSeparateScriptCompilationAndExecution(ErrorString*, bool* result)
126 {
127     *result = scriptDebugServer().supportsSeparateScriptCompilationAndExecution();
128 }
129
130 void InspectorDebuggerAgent::enable(ErrorString*)
131 {
132     if (enabled())
133         return;
134
135     enable();
136     m_state->setBoolean(DebuggerAgentState::debuggerEnabled, true);
137
138     ASSERT(m_frontend);
139 }
140
141 void InspectorDebuggerAgent::disable(ErrorString*)
142 {
143     if (!enabled())
144         return;
145
146     disable();
147     m_state->setBoolean(DebuggerAgentState::debuggerEnabled, false);
148 }
149
150 void InspectorDebuggerAgent::restore()
151 {
152     if (enabled()) {
153         m_frontend->globalObjectCleared();
154         enable();
155         long pauseState = m_state->getLong(DebuggerAgentState::pauseOnExceptionsState);
156         String error;
157         setPauseOnExceptionsImpl(&error, pauseState);
158     }
159 }
160
161 void InspectorDebuggerAgent::setFrontend(InspectorFrontend* frontend)
162 {
163     m_frontend = frontend->debugger();
164 }
165
166 void InspectorDebuggerAgent::clearFrontend()
167 {
168     m_frontend = 0;
169
170     if (!enabled())
171         return;
172
173     disable();
174
175     // FIXME: due to m_state->mute() hack in InspectorController, debuggerEnabled is actually set to false only
176     // in InspectorState, but not in cookie. That's why after navigation debuggerEnabled will be true,
177     // but after front-end re-open it will still be false.
178     m_state->setBoolean(DebuggerAgentState::debuggerEnabled, false);
179 }
180
181 void InspectorDebuggerAgent::setBreakpointsActive(ErrorString*, bool active)
182 {
183     if (active)
184         scriptDebugServer().activateBreakpoints();
185     else
186         scriptDebugServer().deactivateBreakpoints();
187 }
188
189 bool InspectorDebuggerAgent::isPaused()
190 {
191     return scriptDebugServer().isPaused();
192 }
193
194 bool InspectorDebuggerAgent::runningNestedMessageLoop()
195 {
196     return scriptDebugServer().runningNestedMessageLoop();
197 }
198
199 void InspectorDebuggerAgent::addMessageToConsole(MessageSource source, MessageType type)
200 {
201     if (scriptDebugServer().pauseOnExceptionsState() != ScriptDebugServer::DontPauseOnExceptions && source == ConsoleAPIMessageSource && type == AssertMessageType)
202         breakProgram(InspectorFrontend::Debugger::Reason::Assert, 0);
203 }
204
205 static PassRefPtr<InspectorObject> buildObjectForBreakpointCookie(const String& url, int lineNumber, int columnNumber, const String& condition, RefPtr<InspectorArray>& actions, bool isRegex, bool autoContinue)
206 {
207     RefPtr<InspectorObject> breakpointObject = InspectorObject::create();
208     breakpointObject->setString("url", url);
209     breakpointObject->setNumber("lineNumber", lineNumber);
210     breakpointObject->setNumber("columnNumber", columnNumber);
211     breakpointObject->setString("condition", condition);
212     breakpointObject->setBoolean("isRegex", isRegex);
213     breakpointObject->setBoolean("autoContinue", autoContinue);
214     if (actions)
215         breakpointObject->setArray("actions", actions);
216
217     return breakpointObject;
218 }
219
220 static bool matches(const String& url, const String& pattern, bool isRegex)
221 {
222     if (isRegex) {
223         RegularExpression regex(pattern, TextCaseSensitive);
224         return regex.match(url) != -1;
225     }
226     return url == pattern;
227 }
228
229 static bool breakpointActionTypeForString(const String& typeString, ScriptBreakpointActionType* output)
230 {
231     if (typeString == TypeBuilder::getEnumConstantValue(TypeBuilder::Debugger::BreakpointAction::Type::Log)) {
232         *output = ScriptBreakpointActionTypeLog;
233         return true;
234     }
235     if (typeString == TypeBuilder::getEnumConstantValue(TypeBuilder::Debugger::BreakpointAction::Type::Evaluate)) {
236         *output = ScriptBreakpointActionTypeEvaluate;
237         return true;
238     }
239     if (typeString == TypeBuilder::getEnumConstantValue(TypeBuilder::Debugger::BreakpointAction::Type::Sound)) {
240         *output = ScriptBreakpointActionTypeSound;
241         return true;
242     }
243
244     return false;
245 }
246
247 static bool breakpointActionsFromProtocol(ErrorString* errorString, RefPtr<InspectorArray>& actions, Vector<ScriptBreakpointAction>* result)
248 {
249     if (!actions)
250         return true;
251
252     unsigned actionsLength = actions->length();
253     if (!actionsLength)
254         return true;
255
256     result->reserveCapacity(actionsLength);
257     for (unsigned i = 0; i < actionsLength; ++i) {
258         RefPtr<InspectorValue> value = actions->get(i);
259         RefPtr<InspectorObject> object;
260         if (!value->asObject(&object)) {
261             *errorString = "BreakpointAction of incorrect type, expected object";
262             return false;
263         }
264
265         String typeString;
266         if (!object->getString("type", &typeString)) {
267             *errorString = "BreakpointAction had type missing";
268             return false;
269         }
270
271         ScriptBreakpointActionType type;
272         if (!breakpointActionTypeForString(typeString, &type)) {
273             *errorString = "BreakpointAction had unknown type";
274             return false;
275         }
276
277         String data;
278         object->getString("data", &data);
279
280         result->append(ScriptBreakpointAction(type, data));
281     }
282
283     return true;
284 }
285
286 void InspectorDebuggerAgent::setBreakpointByUrl(ErrorString* errorString, int lineNumber, const String* const optionalURL, const String* const optionalURLRegex, const int* const optionalColumnNumber, const RefPtr<InspectorObject>* options, TypeBuilder::Debugger::BreakpointId* outBreakpointId, RefPtr<TypeBuilder::Array<TypeBuilder::Debugger::Location>>& locations)
287 {
288     locations = Array<TypeBuilder::Debugger::Location>::create();
289     if (!optionalURL == !optionalURLRegex) {
290         *errorString = "Either url or urlRegex must be specified.";
291         return;
292     }
293
294     String url = optionalURL ? *optionalURL : *optionalURLRegex;
295     int columnNumber = optionalColumnNumber ? *optionalColumnNumber : 0;
296     bool isRegex = optionalURLRegex;
297
298     String breakpointId = (isRegex ? "/" + url + "/" : url) + ':' + String::number(lineNumber) + ':' + String::number(columnNumber);
299     RefPtr<InspectorObject> breakpointsCookie = m_state->getObject(DebuggerAgentState::javaScriptBreakpoints);
300     if (breakpointsCookie->find(breakpointId) != breakpointsCookie->end()) {
301         *errorString = "Breakpoint at specified location already exists.";
302         return;
303     }
304
305     String condition = emptyString();
306     bool autoContinue = false;
307     RefPtr<InspectorArray> actions;
308     if (options) {
309         (*options)->getString("condition", &condition);
310         (*options)->getBoolean("autoContinue", &autoContinue);
311         actions = (*options)->getArray("actions");
312     }
313
314     Vector<ScriptBreakpointAction> breakpointActions;
315     if (!breakpointActionsFromProtocol(errorString, actions, &breakpointActions))
316         return;
317
318     breakpointsCookie->setObject(breakpointId, buildObjectForBreakpointCookie(url, lineNumber, columnNumber, condition, actions, isRegex, autoContinue));
319     m_state->setObject(DebuggerAgentState::javaScriptBreakpoints, breakpointsCookie);
320
321     ScriptBreakpoint breakpoint(lineNumber, columnNumber, condition, breakpointActions, autoContinue);
322     for (ScriptsMap::iterator it = m_scripts.begin(); it != m_scripts.end(); ++it) {
323         String scriptURL = !it->value.sourceURL.isEmpty() ? it->value.sourceURL : it->value.url;
324         if (!matches(scriptURL, url, isRegex))
325             continue;
326
327         RefPtr<TypeBuilder::Debugger::Location> location = resolveBreakpoint(breakpointId, it->key, breakpoint);
328         if (location)
329             locations->addItem(location);
330     }
331     *outBreakpointId = breakpointId;
332 }
333
334 static bool parseLocation(ErrorString* errorString, RefPtr<InspectorObject> location, String* scriptId, int* lineNumber, int* columnNumber)
335 {
336     if (!location->getString("scriptId", scriptId) || !location->getNumber("lineNumber", lineNumber)) {
337         // FIXME: replace with input validation.
338         *errorString = "scriptId and lineNumber are required.";
339         return false;
340     }
341     *columnNumber = 0;
342     location->getNumber("columnNumber", columnNumber);
343     return true;
344 }
345
346 void InspectorDebuggerAgent::setBreakpoint(ErrorString* errorString, const RefPtr<InspectorObject>& location, const RefPtr<InspectorObject>* options, TypeBuilder::Debugger::BreakpointId* outBreakpointId, RefPtr<TypeBuilder::Debugger::Location>& actualLocation)
347 {
348     String scriptId;
349     int lineNumber;
350     int columnNumber;
351
352     if (!parseLocation(errorString, location, &scriptId, &lineNumber, &columnNumber))
353         return;
354
355     String condition = emptyString();
356     bool autoContinue = false;
357     RefPtr<InspectorArray> actions;
358     if (options) {
359         (*options)->getString("condition", &condition);
360         (*options)->getBoolean("autoContinue", &autoContinue);
361         actions = (*options)->getArray("actions");
362     }
363
364     Vector<ScriptBreakpointAction> breakpointActions;
365     if (!breakpointActionsFromProtocol(errorString, actions, &breakpointActions))
366         return;
367
368     String breakpointId = scriptId + ':' + String::number(lineNumber) + ':' + String::number(columnNumber);
369     if (m_breakpointIdToDebugServerBreakpointIds.find(breakpointId) != m_breakpointIdToDebugServerBreakpointIds.end()) {
370         *errorString = "Breakpoint at specified location already exists.";
371         return;
372     }
373     ScriptBreakpoint breakpoint(lineNumber, columnNumber, condition, breakpointActions, autoContinue);
374     actualLocation = resolveBreakpoint(breakpointId, scriptId, breakpoint);
375     if (actualLocation)
376         *outBreakpointId = breakpointId;
377     else
378         *errorString = "Could not resolve breakpoint";
379 }
380
381 void InspectorDebuggerAgent::removeBreakpoint(ErrorString*, const String& breakpointId)
382 {
383     RefPtr<InspectorObject> breakpointsCookie = m_state->getObject(DebuggerAgentState::javaScriptBreakpoints);
384     breakpointsCookie->remove(breakpointId);
385     m_state->setObject(DebuggerAgentState::javaScriptBreakpoints, breakpointsCookie);
386
387     BreakpointIdToDebugServerBreakpointIdsMap::iterator debugServerBreakpointIdsIterator = m_breakpointIdToDebugServerBreakpointIds.find(breakpointId);
388     if (debugServerBreakpointIdsIterator == m_breakpointIdToDebugServerBreakpointIds.end())
389         return;
390     for (size_t i = 0; i < debugServerBreakpointIdsIterator->value.size(); ++i)
391         scriptDebugServer().removeBreakpoint(debugServerBreakpointIdsIterator->value[i]);
392     m_breakpointIdToDebugServerBreakpointIds.remove(debugServerBreakpointIdsIterator);
393 }
394
395 void InspectorDebuggerAgent::continueToLocation(ErrorString* errorString, const RefPtr<InspectorObject>& location)
396 {
397     if (!m_continueToLocationBreakpointId.isEmpty()) {
398         scriptDebugServer().removeBreakpoint(m_continueToLocationBreakpointId);
399         m_continueToLocationBreakpointId = "";
400     }
401
402     String scriptId;
403     int lineNumber;
404     int columnNumber;
405
406     if (!parseLocation(errorString, location, &scriptId, &lineNumber, &columnNumber))
407         return;
408
409     ScriptBreakpoint breakpoint(lineNumber, columnNumber, "", false);
410     m_continueToLocationBreakpointId = scriptDebugServer().setBreakpoint(scriptId, breakpoint, &lineNumber, &columnNumber);
411     resume(errorString);
412 }
413
414 PassRefPtr<TypeBuilder::Debugger::Location> InspectorDebuggerAgent::resolveBreakpoint(const String& breakpointId, const String& scriptId, const ScriptBreakpoint& breakpoint)
415 {
416     ScriptsMap::iterator scriptIterator = m_scripts.find(scriptId);
417     if (scriptIterator == m_scripts.end())
418         return 0;
419     Script& script = scriptIterator->value;
420     if (breakpoint.lineNumber < script.startLine || script.endLine < breakpoint.lineNumber)
421         return 0;
422
423     int actualLineNumber;
424     int actualColumnNumber;
425     String debugServerBreakpointId = scriptDebugServer().setBreakpoint(scriptId, breakpoint, &actualLineNumber, &actualColumnNumber);
426     if (debugServerBreakpointId.isEmpty())
427         return 0;
428
429     BreakpointIdToDebugServerBreakpointIdsMap::iterator debugServerBreakpointIdsIterator = m_breakpointIdToDebugServerBreakpointIds.find(breakpointId);
430     if (debugServerBreakpointIdsIterator == m_breakpointIdToDebugServerBreakpointIds.end())
431         debugServerBreakpointIdsIterator = m_breakpointIdToDebugServerBreakpointIds.set(breakpointId, Vector<String>()).iterator;
432     debugServerBreakpointIdsIterator->value.append(debugServerBreakpointId);
433
434     RefPtr<TypeBuilder::Debugger::Location> location = TypeBuilder::Debugger::Location::create()
435         .setScriptId(scriptId)
436         .setLineNumber(actualLineNumber);
437     location->setColumnNumber(actualColumnNumber);
438     return location;
439 }
440
441 static PassRefPtr<InspectorObject> scriptToInspectorObject(ScriptObject scriptObject)
442 {
443     if (scriptObject.hasNoValue())
444         return 0;
445     RefPtr<InspectorValue> value = scriptObject.toInspectorValue(scriptObject.scriptState());
446     if (!value)
447         return 0;
448     return value->asObject();
449 }
450
451 void InspectorDebuggerAgent::searchInContent(ErrorString* error, const String& scriptId, const String& query, const bool* const optionalCaseSensitive, const bool* const optionalIsRegex, RefPtr<Array<WebCore::TypeBuilder::Page::SearchMatch>>& results)
452 {
453     bool isRegex = optionalIsRegex ? *optionalIsRegex : false;
454     bool caseSensitive = optionalCaseSensitive ? *optionalCaseSensitive : false;
455
456     ScriptsMap::iterator it = m_scripts.find(scriptId);
457     if (it != m_scripts.end())
458         results = ContentSearchUtils::searchInTextByLines(it->value.source, query, caseSensitive, isRegex);
459     else
460         *error = "No script for id: " + scriptId;
461 }
462
463 void InspectorDebuggerAgent::setScriptSource(ErrorString* error, const String& scriptId, const String& newContent, const bool* const preview, RefPtr<Array<TypeBuilder::Debugger::CallFrame>>& newCallFrames, RefPtr<InspectorObject>& result)
464 {
465     bool previewOnly = preview && *preview;
466     ScriptObject resultObject;
467     if (!scriptDebugServer().setScriptSource(scriptId, newContent, previewOnly, error, &m_currentCallStack, &resultObject))
468         return;
469     newCallFrames = currentCallFrames();
470     RefPtr<InspectorObject> object = scriptToInspectorObject(resultObject);
471     if (object)
472         result = object;
473 }
474
475 void InspectorDebuggerAgent::getScriptSource(ErrorString* error, const String& scriptId, String* scriptSource)
476 {
477     ScriptsMap::iterator it = m_scripts.find(scriptId);
478     if (it != m_scripts.end())
479         *scriptSource = it->value.source;
480     else
481         *error = "No script for id: " + scriptId;
482 }
483
484 void InspectorDebuggerAgent::getFunctionDetails(ErrorString* errorString, const String& functionId, RefPtr<TypeBuilder::Debugger::FunctionDetails>& details)
485 {
486     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(functionId);
487     if (injectedScript.hasNoValue()) {
488         *errorString = "Function object id is obsolete";
489         return;
490     }
491     injectedScript.getFunctionDetails(errorString, functionId, &details);
492 }
493
494 void InspectorDebuggerAgent::schedulePauseOnNextStatement(InspectorFrontend::Debugger::Reason::Enum breakReason, PassRefPtr<InspectorObject> data)
495 {
496     if (m_javaScriptPauseScheduled)
497         return;
498     m_breakReason = breakReason;
499     m_breakAuxData = data;
500     scriptDebugServer().setPauseOnNextStatement(true);
501 }
502
503 void InspectorDebuggerAgent::cancelPauseOnNextStatement()
504 {
505     if (m_javaScriptPauseScheduled)
506         return;
507     clearBreakDetails();
508     scriptDebugServer().setPauseOnNextStatement(false);
509 }
510
511 void InspectorDebuggerAgent::pause(ErrorString*)
512 {
513     if (m_javaScriptPauseScheduled)
514         return;
515     clearBreakDetails();
516     scriptDebugServer().setPauseOnNextStatement(true);
517     m_javaScriptPauseScheduled = true;
518 }
519
520 void InspectorDebuggerAgent::resume(ErrorString* errorString)
521 {
522     if (!assertPaused(errorString))
523         return;
524     m_injectedScriptManager->releaseObjectGroup(InspectorDebuggerAgent::backtraceObjectGroup);
525     scriptDebugServer().continueProgram();
526 }
527
528 void InspectorDebuggerAgent::stepOver(ErrorString* errorString)
529 {
530     if (!assertPaused(errorString))
531         return;
532     m_injectedScriptManager->releaseObjectGroup(InspectorDebuggerAgent::backtraceObjectGroup);
533     scriptDebugServer().stepOverStatement();
534 }
535
536 void InspectorDebuggerAgent::stepInto(ErrorString* errorString)
537 {
538     if (!assertPaused(errorString))
539         return;
540     m_injectedScriptManager->releaseObjectGroup(InspectorDebuggerAgent::backtraceObjectGroup);
541     scriptDebugServer().stepIntoStatement();
542     m_listener->stepInto();
543 }
544
545 void InspectorDebuggerAgent::stepOut(ErrorString* errorString)
546 {
547     if (!assertPaused(errorString))
548         return;
549     m_injectedScriptManager->releaseObjectGroup(InspectorDebuggerAgent::backtraceObjectGroup);
550     scriptDebugServer().stepOutOfFunction();
551 }
552
553 void InspectorDebuggerAgent::setPauseOnExceptions(ErrorString* errorString, const String& stringPauseState)
554 {
555     ScriptDebugServer::PauseOnExceptionsState pauseState;
556     if (stringPauseState == "none")
557         pauseState = ScriptDebugServer::DontPauseOnExceptions;
558     else if (stringPauseState == "all")
559         pauseState = ScriptDebugServer::PauseOnAllExceptions;
560     else if (stringPauseState == "uncaught")
561         pauseState = ScriptDebugServer::PauseOnUncaughtExceptions;
562     else {
563         *errorString = "Unknown pause on exceptions mode: " + stringPauseState;
564         return;
565     }
566     setPauseOnExceptionsImpl(errorString, pauseState);
567 }
568
569 void InspectorDebuggerAgent::setPauseOnExceptionsImpl(ErrorString* errorString, int pauseState)
570 {
571     scriptDebugServer().setPauseOnExceptionsState(static_cast<ScriptDebugServer::PauseOnExceptionsState>(pauseState));
572     if (scriptDebugServer().pauseOnExceptionsState() != pauseState)
573         *errorString = "Internal error. Could not change pause on exceptions state";
574     else
575         m_state->setLong(DebuggerAgentState::pauseOnExceptionsState, pauseState);
576 }
577
578 void InspectorDebuggerAgent::evaluateOnCallFrame(ErrorString* errorString, const String& callFrameId, const String& expression, const String* const objectGroup, const bool* const includeCommandLineAPI, const bool* const doNotPauseOnExceptionsAndMuteConsole, const bool* const returnByValue, const bool* generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown)
579 {
580     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(callFrameId);
581     if (injectedScript.hasNoValue()) {
582         *errorString = "Inspected frame has gone";
583         return;
584     }
585
586     ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = scriptDebugServer().pauseOnExceptionsState();
587     if (doNotPauseOnExceptionsAndMuteConsole ? *doNotPauseOnExceptionsAndMuteConsole : false) {
588         if (previousPauseOnExceptionsState != ScriptDebugServer::DontPauseOnExceptions)
589             scriptDebugServer().setPauseOnExceptionsState(ScriptDebugServer::DontPauseOnExceptions);
590         muteConsole();
591     }
592
593     injectedScript.evaluateOnCallFrame(errorString, m_currentCallStack, callFrameId, expression, objectGroup ? *objectGroup : "", includeCommandLineAPI ? *includeCommandLineAPI : false, returnByValue ? *returnByValue : false, generatePreview ? *generatePreview : false, &result, wasThrown);
594
595     if (doNotPauseOnExceptionsAndMuteConsole ? *doNotPauseOnExceptionsAndMuteConsole : false) {
596         unmuteConsole();
597         if (scriptDebugServer().pauseOnExceptionsState() != previousPauseOnExceptionsState)
598             scriptDebugServer().setPauseOnExceptionsState(previousPauseOnExceptionsState);
599     }
600 }
601
602 void InspectorDebuggerAgent::compileScript(ErrorString* errorString, const String& expression, const String& sourceURL, TypeBuilder::OptOutput<ScriptId>* scriptId, TypeBuilder::OptOutput<String>* syntaxErrorMessage)
603 {
604     InjectedScript injectedScript = injectedScriptForEval(errorString, 0);
605     if (injectedScript.hasNoValue()) {
606         *errorString = "Inspected frame has gone";
607         return;
608     }
609
610     String scriptIdValue;
611     String exceptionMessage;
612     scriptDebugServer().compileScript(injectedScript.scriptState(), expression, sourceURL, &scriptIdValue, &exceptionMessage);
613     if (!scriptIdValue && !exceptionMessage) {
614         *errorString = "Script compilation failed";
615         return;
616     }
617     *syntaxErrorMessage = exceptionMessage;
618     *scriptId = scriptIdValue;
619 }
620
621 void InspectorDebuggerAgent::runScript(ErrorString* errorString, const ScriptId& scriptId, const int* executionContextId, const String* const objectGroup, const bool* const doNotPauseOnExceptionsAndMuteConsole, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown)
622 {
623     InjectedScript injectedScript = injectedScriptForEval(errorString, executionContextId);
624     if (injectedScript.hasNoValue()) {
625         *errorString = "Inspected frame has gone";
626         return;
627     }
628
629     ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = scriptDebugServer().pauseOnExceptionsState();
630     if (doNotPauseOnExceptionsAndMuteConsole && *doNotPauseOnExceptionsAndMuteConsole) {
631         if (previousPauseOnExceptionsState != ScriptDebugServer::DontPauseOnExceptions)
632             scriptDebugServer().setPauseOnExceptionsState(ScriptDebugServer::DontPauseOnExceptions);
633         muteConsole();
634     }
635
636     ScriptValue value;
637     bool wasThrownValue;
638     String exceptionMessage;
639     scriptDebugServer().runScript(injectedScript.scriptState(), scriptId, &value, &wasThrownValue, &exceptionMessage);
640     *wasThrown = wasThrownValue;
641     if (value.hasNoValue()) {
642         *errorString = "Script execution failed";
643         return;
644     }
645     result = injectedScript.wrapObject(value, objectGroup ? *objectGroup : "");
646     if (wasThrownValue)
647         result->setDescription(exceptionMessage);
648
649     if (doNotPauseOnExceptionsAndMuteConsole && *doNotPauseOnExceptionsAndMuteConsole) {
650         unmuteConsole();
651         if (scriptDebugServer().pauseOnExceptionsState() != previousPauseOnExceptionsState)
652             scriptDebugServer().setPauseOnExceptionsState(previousPauseOnExceptionsState);
653     }
654 }
655
656 void InspectorDebuggerAgent::setOverlayMessage(ErrorString*, const String*)
657 {
658 }
659
660 void InspectorDebuggerAgent::scriptExecutionBlockedByCSP(const String& directiveText)
661 {
662     if (scriptDebugServer().pauseOnExceptionsState() != ScriptDebugServer::DontPauseOnExceptions) {
663         RefPtr<InspectorObject> directive = InspectorObject::create();
664         directive->setString("directiveText", directiveText);
665         breakProgram(InspectorFrontend::Debugger::Reason::CSPViolation, directive.release());
666     }
667 }
668
669 PassRefPtr<Array<TypeBuilder::Debugger::CallFrame>> InspectorDebuggerAgent::currentCallFrames()
670 {
671     if (!m_pausedScriptState)
672         return Array<TypeBuilder::Debugger::CallFrame>::create();
673     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(m_pausedScriptState);
674     if (injectedScript.hasNoValue()) {
675         ASSERT_NOT_REACHED();
676         return Array<TypeBuilder::Debugger::CallFrame>::create();
677     }
678     return injectedScript.wrapCallFrames(m_currentCallStack);
679 }
680
681 String InspectorDebuggerAgent::sourceMapURLForScript(const Script& script)
682 {
683     DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeader, (ASCIILiteral("SourceMap")));
684     DEFINE_STATIC_LOCAL(String, sourceMapHTTPHeaderDeprecated, (ASCIILiteral("X-SourceMap")));
685
686     if (!script.url.isEmpty()) {
687         if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent()) {
688             CachedResource* resource = pageAgent->cachedResource(pageAgent->mainFrame(), URL(ParsedURLString, script.url));
689             if (resource) {
690                 String sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeader);
691                 if (!sourceMapHeader.isEmpty())
692                     return sourceMapHeader;
693
694                 sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeaderDeprecated);
695                 if (!sourceMapHeader.isEmpty())
696                     return sourceMapHeader;
697             }
698         }
699     }
700
701     return ContentSearchUtils::findScriptSourceMapURL(script.source);
702 }
703
704 // JavaScriptDebugListener functions
705
706 void InspectorDebuggerAgent::didParseSource(const String& scriptId, const Script& inScript)
707 {
708     Script script = inScript;
709     if (!script.startLine && !script.startColumn)
710         script.sourceURL = ContentSearchUtils::findScriptSourceURL(script.source);
711     script.sourceMappingURL = sourceMapURLForScript(script);
712
713     bool hasSourceURL = !script.sourceURL.isEmpty();
714     String scriptURL = hasSourceURL ? script.sourceURL : script.url;
715     bool* hasSourceURLParam = hasSourceURL ? &hasSourceURL : 0;
716     String* sourceMapURLParam = script.sourceMappingURL.isNull() ? 0 : &script.sourceMappingURL;
717     const bool* isContentScript = script.isContentScript ? &script.isContentScript : 0;
718     m_frontend->scriptParsed(scriptId, scriptURL, script.startLine, script.startColumn, script.endLine, script.endColumn, isContentScript, sourceMapURLParam, hasSourceURLParam);
719
720     m_scripts.set(scriptId, script);
721
722     if (scriptURL.isEmpty())
723         return;
724
725     RefPtr<InspectorObject> breakpointsCookie = m_state->getObject(DebuggerAgentState::javaScriptBreakpoints);
726     for (InspectorObject::iterator it = breakpointsCookie->begin(); it != breakpointsCookie->end(); ++it) {
727         RefPtr<InspectorObject> breakpointObject = it->value->asObject();
728         bool isRegex;
729         breakpointObject->getBoolean("isRegex", &isRegex);
730         String url;
731         breakpointObject->getString("url", &url);
732         if (!matches(scriptURL, url, isRegex))
733             continue;
734         ScriptBreakpoint breakpoint;
735         breakpointObject->getNumber("lineNumber", &breakpoint.lineNumber);
736         breakpointObject->getNumber("columnNumber", &breakpoint.columnNumber);
737         breakpointObject->getString("condition", &breakpoint.condition);
738         breakpointObject->getBoolean("autoContinue", &breakpoint.autoContinue);
739         ErrorString errorString;
740         RefPtr<InspectorArray> actions = breakpointObject->getArray("actions");
741         if (!breakpointActionsFromProtocol(&errorString, actions, &breakpoint.actions)) {
742             ASSERT_NOT_REACHED();
743             continue;
744         }
745
746         RefPtr<TypeBuilder::Debugger::Location> location = resolveBreakpoint(it->key, scriptId, breakpoint);
747         if (location)
748             m_frontend->breakpointResolved(it->key, location);
749     }
750 }
751
752 void InspectorDebuggerAgent::failedToParseSource(const String& url, const String& data, int firstLine, int errorLine, const String& errorMessage)
753 {
754     m_frontend->scriptFailedToParse(url, data, firstLine, errorLine, errorMessage);
755 }
756
757 void InspectorDebuggerAgent::didPause(JSC::ExecState* scriptState, const ScriptValue& callFrames, const ScriptValue& exception)
758 {
759     ASSERT(scriptState && !m_pausedScriptState);
760     m_pausedScriptState = scriptState;
761     m_currentCallStack = callFrames;
762
763     if (!exception.hasNoValue()) {
764         InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(scriptState);
765         if (!injectedScript.hasNoValue()) {
766             m_breakReason = InspectorFrontend::Debugger::Reason::Exception;
767             m_breakAuxData = injectedScript.wrapObject(exception, "backtrace")->openAccessors();
768             // m_breakAuxData might be null after this.
769         }
770     }
771
772     m_frontend->paused(currentCallFrames(), m_breakReason, m_breakAuxData);
773     m_javaScriptPauseScheduled = false;
774
775     if (!m_continueToLocationBreakpointId.isEmpty()) {
776         scriptDebugServer().removeBreakpoint(m_continueToLocationBreakpointId);
777         m_continueToLocationBreakpointId = "";
778     }
779     if (m_listener)
780         m_listener->didPause();
781 }
782
783 void InspectorDebuggerAgent::didContinue()
784 {
785     m_pausedScriptState = 0;
786     m_currentCallStack = ScriptValue();
787     clearBreakDetails();
788     m_frontend->resumed();
789 }
790
791 void InspectorDebuggerAgent::breakProgram(InspectorFrontend::Debugger::Reason::Enum breakReason, PassRefPtr<InspectorObject> data)
792 {
793     m_breakReason = breakReason;
794     m_breakAuxData = data;
795     scriptDebugServer().breakProgram();
796 }
797
798 void InspectorDebuggerAgent::clear()
799 {
800     m_pausedScriptState = 0;
801     m_currentCallStack = ScriptValue();
802     m_scripts.clear();
803     m_breakpointIdToDebugServerBreakpointIds.clear();
804     m_continueToLocationBreakpointId = String();
805     clearBreakDetails();
806     m_javaScriptPauseScheduled = false;
807     ErrorString error;
808     setOverlayMessage(&error, 0);
809 }
810
811 bool InspectorDebuggerAgent::assertPaused(ErrorString* errorString)
812 {
813     if (!m_pausedScriptState) {
814         *errorString = "Can only perform operation while paused.";
815         return false;
816     }
817     return true;
818 }
819
820 void InspectorDebuggerAgent::clearBreakDetails()
821 {
822     m_breakReason = InspectorFrontend::Debugger::Reason::Other;
823     m_breakAuxData = 0;
824 }
825
826 void InspectorDebuggerAgent::reset()
827 {
828     scriptDebugServer().clearBreakpoints();
829     m_scripts.clear();
830     m_breakpointIdToDebugServerBreakpointIds.clear();
831     if (m_frontend)
832         m_frontend->globalObjectCleared();
833 }
834
835 } // namespace WebCore
836
837 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)