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