8fe613612e3dff9ba02a65834cd9b44d7a21e16a
[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 <bindings/ScriptValue.h>
39 #include <debugger/Debugger.h>
40 #include <inspector/InspectorJSBackendDispatchers.h>
41 #include <inspector/InspectorJSFrontendDispatchers.h>
42 #include <inspector/ScriptBreakpoint.h>
43 #include <inspector/ScriptDebugListener.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 class ScriptDebugServer;
58 }
59
60 namespace WebCore {
61
62 class InstrumentingAgents;
63
64 typedef String ErrorString;
65
66 class InspectorDebuggerAgent : public InspectorAgentBase, public Inspector::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(Inspector::InspectorDisconnectReason) override;
75
76     bool isPaused();
77     void addMessageToConsole(MessageSource, MessageType);
78
79     // Part of the protocol.
80     virtual void enable(ErrorString*) override;
81     virtual void disable(ErrorString*) override;
82     virtual void setBreakpointsActive(ErrorString*, bool active) override;
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) override;
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) override;
86     virtual void removeBreakpoint(ErrorString*, const String& breakpointIdentifier) override;
87     virtual void continueToLocation(ErrorString*, const RefPtr<Inspector::InspectorObject>& location) override;
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>>&) override;
90     virtual void getScriptSource(ErrorString*, const String& scriptID, String* scriptSource) override;
91     virtual void getFunctionDetails(ErrorString*, const String& functionId, RefPtr<Inspector::TypeBuilder::Debugger::FunctionDetails>&) override;
92     virtual void pause(ErrorString*) override;
93     virtual void resume(ErrorString*) override;
94     virtual void stepOver(ErrorString*) override;
95     virtual void stepInto(ErrorString*) override;
96     virtual void stepOut(ErrorString*) override;
97     virtual void setPauseOnExceptions(ErrorString*, const String& pauseState) override;
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) override;
108     virtual void setOverlayMessage(ErrorString*, const String*) override;
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 Inspector::ScriptDebugServer& scriptDebugServer() = 0;
126
127 protected:
128     InspectorDebuggerAgent(InstrumentingAgents*, Inspector::InjectedScriptManager*);
129
130     virtual void startListeningScriptDebugServer() = 0;
131     virtual void stopListeningScriptDebugServer(bool isBeingDestroyed) = 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(bool isBeingDestroyed);
139     virtual void didPause(JSC::ExecState*, const Deprecated::ScriptValue& callFrames, const Deprecated::ScriptValue& exception) override;
140     virtual void didContinue() override;
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     virtual void breakpointActionSound() override final;
150
151     PassRefPtr<Inspector::TypeBuilder::Debugger::Location> resolveBreakpoint(const String& breakpointIdentifier, JSC::SourceID, const Inspector::ScriptBreakpoint&);
152     bool assertPaused(ErrorString*);
153     void clearResolvedBreakpointState();
154     void clearBreakDetails();
155
156     bool breakpointActionsFromProtocol(ErrorString*, RefPtr<Inspector::InspectorArray>& actions, Vector<Inspector::ScriptBreakpointAction>* result);
157
158     String sourceMapURLForScript(const Script&);
159
160     typedef HashMap<JSC::SourceID, Script> ScriptsMap;
161     typedef HashMap<String, Vector<JSC::BreakpointID>> BreakpointIdentifierToDebugServerBreakpointIDsMap;
162     typedef HashMap<String, RefPtr<Inspector::InspectorObject>> BreakpointIdentifierToBreakpointMap;
163
164     Inspector::InjectedScriptManager* m_injectedScriptManager;
165     std::unique_ptr<Inspector::InspectorDebuggerFrontendDispatcher> m_frontendDispatcher;
166     RefPtr<Inspector::InspectorDebuggerBackendDispatcher> m_backendDispatcher;
167     JSC::ExecState* m_pausedScriptState;
168     Deprecated::ScriptValue m_currentCallStack;
169     ScriptsMap m_scripts;
170     BreakpointIdentifierToDebugServerBreakpointIDsMap m_breakpointIdentifierToDebugServerBreakpointIDs;
171     BreakpointIdentifierToBreakpointMap m_javaScriptBreakpoints;
172     JSC::BreakpointID m_continueToLocationBreakpointID;
173     Inspector::InspectorDebuggerFrontendDispatcher::Reason::Enum m_breakReason;
174     RefPtr<Inspector::InspectorObject> m_breakAuxData;
175     bool m_enabled;
176     bool m_javaScriptPauseScheduled;
177     Listener* m_listener;
178     int m_nextProbeSampleId;
179     int m_nextBreakpointActionIdentifier;
180 };
181
182 } // namespace WebCore
183
184 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
185
186 #endif // !defined(InspectorDebuggerAgent_h)