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