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#ifndef InspectorDebuggerAgent_h
31#define InspectorDebuggerAgent_h
32
33#if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
34#include "InjectedScript.h"
35#include "InspectorFrontend.h"
36#include "ScriptBreakpoint.h"
37#include "ScriptDebugListener.h"
38#include "ScriptState.h"
39#include <wtf/Forward.h>
40#include <wtf/HashMap.h>
41#include <wtf/PassOwnPtr.h>
42#include <wtf/PassRefPtr.h>
43#include <wtf/Vector.h>
44#include <wtf/text/StringHash.h>
45
46namespace WebCore {
47
48class InjectedScriptManager;
49class InspectorFrontend;
50class InspectorArray;
51class InspectorObject;
52class InspectorState;
53class InspectorValue;
54class InstrumentingAgents;
55class ScriptDebugServer;
56
57typedef String ErrorString;
58
59enum DebuggerEventType {
60    JavaScriptPauseEventType,
61    JavaScriptBreakpointEventType,
62    NativeBreakpointDebuggerEventType
63};
64
65class InspectorDebuggerAgent : public ScriptDebugListener {
66    WTF_MAKE_NONCOPYABLE(InspectorDebuggerAgent); WTF_MAKE_FAST_ALLOCATED;
67public:
68    virtual ~InspectorDebuggerAgent();
69
70    void enable(ErrorString*) { enable(false); }
71    void disable(ErrorString*) { disable(); }
72    void disable();
73    bool enabled();
74    void restore();
75    void setFrontend(InspectorFrontend*);
76    void clearFrontend();
77
78    void inspectedURLChanged(const String& url);
79
80    // Part of the protocol.
81    void setBreakpointsActive(ErrorString*, bool active);
82
83    void setBreakpointByUrl(ErrorString*, const String& url, int lineNumber, const int* const optionalColumnNumber, const String* const optionalCondition, String* breakpointId, RefPtr<InspectorArray>* locations);
84    void setBreakpoint(ErrorString*, PassRefPtr<InspectorObject> location, const String* const optionalCondition, String* breakpointId, RefPtr<InspectorObject>* actualLocation);
85    void removeBreakpoint(ErrorString*, const String& breakpointId);
86    void continueToLocation(ErrorString*, PassRefPtr<InspectorObject> location);
87
88    void editScriptSource(ErrorString*, const String& sourceID, const String& newContent, RefPtr<InspectorArray>* newCallFrames);
89    void getScriptSource(ErrorString*, const String& sourceID, String* scriptSource);
90    void schedulePauseOnNextStatement(DebuggerEventType type, PassRefPtr<InspectorValue> data);
91    void cancelPauseOnNextStatement();
92    void breakProgram(DebuggerEventType type, PassRefPtr<InspectorValue> data);
93    void pause(ErrorString*);
94    void resume(ErrorString*);
95    void stepOver(ErrorString*);
96    void stepInto(ErrorString*);
97    void stepOut(ErrorString*);
98    void setPauseOnExceptions(ErrorString*, const String& pauseState);
99    void evaluateOnCallFrame(ErrorString*, const String& callFrameId, const String& expression, const String* const objectGroup, const bool* const includeCommandLineAPI, RefPtr<InspectorObject>* result);
100
101    class Listener {
102    public:
103        virtual ~Listener() { }
104        virtual void debuggerWasEnabled() = 0;
105        virtual void debuggerWasDisabled() = 0;
106    };
107    void setListener(Listener* listener) { m_listener = listener; }
108
109    virtual ScriptDebugServer& scriptDebugServer() = 0;
110
111protected:
112    InspectorDebuggerAgent(InstrumentingAgents*, InspectorState*, InjectedScriptManager*);
113
114    virtual void startListeningScriptDebugServer() = 0;
115    virtual void stopListeningScriptDebugServer() = 0;
116
117private:
118    void enable(bool restoringFromState);
119
120    PassRefPtr<InspectorArray> currentCallFrames();
121
122    virtual void didParseSource(const String& sourceID, const String& url, const String& data, int lineOffset, int columnOffset, bool isContentScript);
123    virtual void failedToParseSource(const String& url, const String& data, int firstLine, int errorLine, const String& errorMessage);
124    virtual void didPause(ScriptState*);
125    virtual void didContinue();
126
127    PassRefPtr<InspectorObject> resolveBreakpoint(const String& breakpointId, const String& sourceId, const ScriptBreakpoint&);
128    void clear();
129
130    class Script {
131    public:
132        Script()
133            : lineOffset(0)
134            , columnOffset(0)
135            , linesCount(0)
136        {
137        }
138
139        Script(const String& url, const String& data, int lineOffset, int columnOffset)
140            : url(url)
141            , data(data)
142            , lineOffset(lineOffset)
143            , columnOffset(columnOffset)
144            , linesCount(0)
145        {
146        }
147
148        String url;
149        String data;
150        int lineOffset;
151        int columnOffset;
152        int linesCount;
153    };
154
155    typedef HashMap<String, Script> ScriptsMap;
156    typedef HashMap<String, Vector<String> > BreakpointIdToDebugServerBreakpointIdsMap;
157
158    InstrumentingAgents* m_instrumentingAgents;
159    InspectorState* m_inspectorState;
160    InjectedScriptManager* m_injectedScriptManager;
161    InspectorFrontend::Debugger* m_frontend;
162    ScriptState* m_pausedScriptState;
163    ScriptsMap m_scripts;
164    BreakpointIdToDebugServerBreakpointIdsMap m_breakpointIdToDebugServerBreakpointIds;
165    String m_continueToLocationBreakpointId;
166    RefPtr<InspectorObject> m_breakProgramDetails;
167    bool m_javaScriptPauseScheduled;
168    Listener* m_listener;
169};
170
171} // namespace WebCore
172
173#endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
174
175#endif // !defined(InspectorDebuggerAgent_h)
176