d1c5e300c07d54329480b3b8fd5453119ff6090a
[WebKit-https.git] / Source / WebCore / inspector / InspectorDebuggerAgent.h
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 #ifndef InspectorDebuggerAgent_h
31 #define InspectorDebuggerAgent_h
32
33 #if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
34
35 #include "ConsoleAPITypes.h"
36 #include "ConsoleTypes.h"
37 #include "InspectorWebAgentBase.h"
38 #include "ScriptBreakpoint.h"
39 #include "ScriptDebugListener.h"
40 #include <bindings/ScriptValue.h>
41 #include <debugger/Debugger.h>
42 #include <inspector/InspectorJSBackendDispatchers.h>
43 #include <inspector/InspectorJSFrontendDispatchers.h>
44 #include <wtf/Forward.h>
45 #include <wtf/HashMap.h>
46 #include <wtf/PassOwnPtr.h>
47 #include <wtf/PassRefPtr.h>
48 #include <wtf/Vector.h>
49 #include <wtf/text/StringHash.h>
50
51 namespace Inspector {
52 class InjectedScript;
53 class InjectedScriptManager;
54 class InspectorArray;
55 class InspectorObject;
56 class InspectorValue;
57 }
58
59 namespace WebCore {
60
61 class InstrumentingAgents;
62 class ScriptDebugServer;
63
64 typedef String ErrorString;
65
66 class InspectorDebuggerAgent : public InspectorAgentBase, public ScriptDebugListener, public Inspector::InspectorDebuggerBackendDispatcherHandler {
67     WTF_MAKE_NONCOPYABLE(InspectorDebuggerAgent); WTF_MAKE_FAST_ALLOCATED;
68 public:
69     static const char* backtraceObjectGroup;
70
71     virtual ~InspectorDebuggerAgent();
72
73     virtual void didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel*, Inspector::InspectorBackendDispatcher*) override;
74     virtual void willDestroyFrontendAndBackend() override;
75
76     bool isPaused();
77     void addMessageToConsole(MessageSource, MessageType);
78
79     // Part of the protocol.
80     virtual void enable(ErrorString*);
81     virtual void disable(ErrorString*);
82     virtual void setBreakpointsActive(ErrorString*, bool active);
83
84     virtual void setBreakpointByUrl(ErrorString*, int lineNumber, const String* optionalURL, const String* optionalURLRegex, const int* optionalColumnNumber, const RefPtr<Inspector::InspectorObject>* options, Inspector::TypeBuilder::Debugger::BreakpointId*, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::Debugger::Location>>& locations, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::Debugger::BreakpointActionIdentifier>>& breakpointActionIdentifiers);
85     virtual void setBreakpoint(ErrorString*, const RefPtr<Inspector::InspectorObject>& location, const RefPtr<Inspector::InspectorObject>* options, Inspector::TypeBuilder::Debugger::BreakpointId*, RefPtr<Inspector::TypeBuilder::Debugger::Location>& actualLocation, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::Debugger::BreakpointActionIdentifier>>& breakpointActionIdentifiers);
86     virtual void removeBreakpoint(ErrorString*, const String& breakpointIdentifier);
87     virtual void continueToLocation(ErrorString*, const RefPtr<Inspector::InspectorObject>& location);
88
89     virtual void searchInContent(ErrorString*, const String& scriptID, const String& query, const bool* optionalCaseSensitive, const bool* optionalIsRegex, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::GenericTypes::SearchMatch>>&);
90     virtual void getScriptSource(ErrorString*, const String& scriptID, String* scriptSource);
91     virtual void getFunctionDetails(ErrorString*, const String& functionId, RefPtr<Inspector::TypeBuilder::Debugger::FunctionDetails>&);
92     virtual void pause(ErrorString*);
93     virtual void resume(ErrorString*);
94     virtual void stepOver(ErrorString*);
95     virtual void stepInto(ErrorString*);
96     virtual void stepOut(ErrorString*);
97     virtual void setPauseOnExceptions(ErrorString*, const String& pauseState);
98     virtual void evaluateOnCallFrame(ErrorString*,
99                              const String& callFrameId,
100                              const String& expression,
101                              const String* objectGroup,
102                              const bool* includeCommandLineAPI,
103                              const bool* doNotPauseOnExceptionsAndMuteConsole,
104                              const bool* returnByValue,
105                              const bool* generatePreview,
106                              RefPtr<Inspector::TypeBuilder::Runtime::RemoteObject>& result,
107                              Inspector::TypeBuilder::OptOutput<bool>* wasThrown);
108     virtual void setOverlayMessage(ErrorString*, const String*);
109
110     void schedulePauseOnNextStatement(Inspector::InspectorDebuggerFrontendDispatcher::Reason::Enum breakReason, PassRefPtr<Inspector::InspectorObject> data);
111     void cancelPauseOnNextStatement();
112     void breakProgram(Inspector::InspectorDebuggerFrontendDispatcher::Reason::Enum breakReason, PassRefPtr<Inspector::InspectorObject> data);
113     void scriptExecutionBlockedByCSP(const String& directiveText);
114
115     class Listener {
116     public:
117         virtual ~Listener() { }
118         virtual void debuggerWasEnabled() = 0;
119         virtual void debuggerWasDisabled() = 0;
120         virtual void stepInto() = 0;
121         virtual void didPause() = 0;
122     };
123     void setListener(Listener* listener) { m_listener = listener; }
124
125     virtual ScriptDebugServer& scriptDebugServer() = 0;
126
127 protected:
128     InspectorDebuggerAgent(InstrumentingAgents*, Inspector::InjectedScriptManager*);
129
130     virtual void startListeningScriptDebugServer() = 0;
131     virtual void stopListeningScriptDebugServer() = 0;
132     virtual void muteConsole() = 0;
133     virtual void unmuteConsole() = 0;
134     Inspector::InjectedScriptManager* injectedScriptManager() const { return m_injectedScriptManager; }
135     virtual Inspector::InjectedScript injectedScriptForEval(ErrorString*, const int* executionContextId) = 0;
136
137     virtual void enable();
138     virtual void disable();
139     virtual void didPause(JSC::ExecState*, const Deprecated::ScriptValue& callFrames, const Deprecated::ScriptValue& exception);
140     virtual void didContinue();
141     void didClearGlobalObject();
142
143 private:
144     PassRefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::Debugger::CallFrame>> currentCallFrames();
145
146     virtual void didParseSource(JSC::SourceID, const Script&) override final;
147     virtual void failedToParseSource(const String& url, const String& data, int firstLine, int errorLine, const String& errorMessage) override final;
148     virtual void didSampleProbe(JSC::ExecState*, int probeIdentifier, int hitCount, const Deprecated::ScriptValue& sample) override final;
149
150     PassRefPtr<Inspector::TypeBuilder::Debugger::Location> resolveBreakpoint(const String& breakpointIdentifier, JSC::SourceID, const ScriptBreakpoint&);
151     bool assertPaused(ErrorString*);
152     void clearResolvedBreakpointState();
153     void clearBreakDetails();
154
155     bool breakpointActionsFromProtocol(ErrorString*, RefPtr<Inspector::InspectorArray>& actions, Vector<ScriptBreakpointAction>* result);
156
157     String sourceMapURLForScript(const Script&);
158
159     typedef HashMap<JSC::SourceID, Script> ScriptsMap;
160     typedef HashMap<String, Vector<JSC::BreakpointID>> BreakpointIdentifierToDebugServerBreakpointIDsMap;
161     typedef HashMap<String, RefPtr<Inspector::InspectorObject>> BreakpointIdentifierToBreakpointMap;
162
163     Inspector::InjectedScriptManager* m_injectedScriptManager;
164     std::unique_ptr<Inspector::InspectorDebuggerFrontendDispatcher> m_frontendDispatcher;
165     RefPtr<Inspector::InspectorDebuggerBackendDispatcher> m_backendDispatcher;
166     JSC::ExecState* m_pausedScriptState;
167     Deprecated::ScriptValue m_currentCallStack;
168     ScriptsMap m_scripts;
169     BreakpointIdentifierToDebugServerBreakpointIDsMap m_breakpointIdentifierToDebugServerBreakpointIDs;
170     BreakpointIdentifierToBreakpointMap m_javaScriptBreakpoints;
171     JSC::BreakpointID m_continueToLocationBreakpointID;
172     Inspector::InspectorDebuggerFrontendDispatcher::Reason::Enum m_breakReason;
173     RefPtr<Inspector::InspectorObject> m_breakAuxData;
174     bool m_enabled;
175     bool m_javaScriptPauseScheduled;
176     Listener* m_listener;
177     int m_nextProbeSampleId;
178     int m_nextBreakpointActionIdentifier;
179 };
180
181 } // namespace WebCore
182
183 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
184
185 #endif // !defined(InspectorDebuggerAgent_h)