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