Home | History | Annotate | Download | only in inspector
      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 #include "InspectorFrontend.h"
     34 #include "bindings/v8/ScriptState.h"
     35 #include "core/inspector/ConsoleAPITypes.h"
     36 #include "core/inspector/InjectedScript.h"
     37 #include "core/inspector/InspectorBaseAgent.h"
     38 #include "core/inspector/ScriptBreakpoint.h"
     39 #include "core/inspector/ScriptDebugListener.h"
     40 #include "core/page/ConsoleTypes.h"
     41 #include "wtf/Forward.h"
     42 #include "wtf/HashMap.h"
     43 #include "wtf/PassRefPtr.h"
     44 #include "wtf/Vector.h"
     45 #include "wtf/text/StringHash.h"
     46 
     47 namespace WebCore {
     48 
     49 class InjectedScriptManager;
     50 class InspectorFrontend;
     51 class InstrumentingAgents;
     52 class JSONObject;
     53 class ScriptArguments;
     54 class ScriptCallStack;
     55 class ScriptDebugServer;
     56 class ScriptValue;
     57 class RegularExpression;
     58 
     59 typedef String ErrorString;
     60 
     61 class InspectorDebuggerAgent : public InspectorBaseAgent<InspectorDebuggerAgent>, public ScriptDebugListener, public InspectorBackendDispatcher::DebuggerCommandHandler {
     62     WTF_MAKE_NONCOPYABLE(InspectorDebuggerAgent); WTF_MAKE_FAST_ALLOCATED;
     63 public:
     64     enum BreakpointSource {
     65         UserBreakpointSource,
     66         DebugCommandBreakpointSource,
     67         MonitorCommandBreakpointSource
     68     };
     69 
     70     static const char* backtraceObjectGroup;
     71 
     72     virtual ~InspectorDebuggerAgent();
     73 
     74     virtual void canSetScriptSource(ErrorString*, bool* result) { *result = true; }
     75 
     76     virtual void setFrontend(InspectorFrontend*);
     77     virtual void clearFrontend();
     78     virtual void restore();
     79 
     80     bool isPaused();
     81     bool runningNestedMessageLoop();
     82     void addMessageToConsole(MessageSource, MessageType, MessageLevel, const String&, PassRefPtr<ScriptCallStack>, unsigned long);
     83     void addMessageToConsole(MessageSource, MessageType, MessageLevel, const String&, ScriptState*, PassRefPtr<ScriptArguments>, unsigned long);
     84 
     85     // Part of the protocol.
     86     virtual void enable(ErrorString*);
     87     virtual void disable(ErrorString*);
     88     virtual void setBreakpointsActive(ErrorString*, bool active);
     89 
     90     virtual void setBreakpointByUrl(ErrorString*, int lineNumber, const String* optionalURL, const String* optionalURLRegex, const int* optionalColumnNumber, const String* optionalCondition, const bool* isAntiBreakpoint, TypeBuilder::Debugger::BreakpointId*, RefPtr<TypeBuilder::Array<TypeBuilder::Debugger::Location> >& locations);
     91     virtual void setBreakpoint(ErrorString*, const RefPtr<JSONObject>& location, const String* optionalCondition, TypeBuilder::Debugger::BreakpointId*, RefPtr<TypeBuilder::Debugger::Location>& actualLocation);
     92     virtual void removeBreakpoint(ErrorString*, const String& breakpointId);
     93     virtual void continueToLocation(ErrorString*, const RefPtr<JSONObject>& location, const bool* interstateLocationOpt);
     94     virtual void getStepInPositions(ErrorString*, const String& callFrameId, RefPtr<TypeBuilder::Array<TypeBuilder::Debugger::Location> >& positions);
     95     virtual void getBacktrace(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::Debugger::CallFrame> >&);
     96 
     97     virtual void searchInContent(ErrorString*, const String& scriptId, const String& query, const bool* optionalCaseSensitive, const bool* optionalIsRegex, RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> >&);
     98     virtual void setScriptSource(ErrorString*, RefPtr<TypeBuilder::Debugger::SetScriptSourceError>&, const String& scriptId, const String& newContent, const bool* preview, RefPtr<TypeBuilder::Array<TypeBuilder::Debugger::CallFrame> >& newCallFrames, RefPtr<JSONObject>& result);
     99     virtual void restartFrame(ErrorString*, const String& callFrameId, RefPtr<TypeBuilder::Array<TypeBuilder::Debugger::CallFrame> >& newCallFrames, RefPtr<JSONObject>& result);
    100     virtual void getScriptSource(ErrorString*, const String& scriptId, String* scriptSource);
    101     virtual void getFunctionDetails(ErrorString*, const String& functionId, RefPtr<TypeBuilder::Debugger::FunctionDetails>&);
    102     virtual void pause(ErrorString*);
    103     virtual void resume(ErrorString*);
    104     virtual void stepOver(ErrorString*);
    105     virtual void stepInto(ErrorString*);
    106     virtual void stepOut(ErrorString*);
    107     virtual void setPauseOnExceptions(ErrorString*, const String& pauseState);
    108     virtual void evaluateOnCallFrame(ErrorString*,
    109                              const String& callFrameId,
    110                              const String& expression,
    111                              const String* objectGroup,
    112                              const bool* includeCommandLineAPI,
    113                              const bool* doNotPauseOnExceptionsAndMuteConsole,
    114                              const bool* returnByValue,
    115                              const bool* generatePreview,
    116                              RefPtr<TypeBuilder::Runtime::RemoteObject>& result,
    117                              TypeBuilder::OptOutput<bool>* wasThrown);
    118     void compileScript(ErrorString*, const String& expression, const String& sourceURL, TypeBuilder::OptOutput<TypeBuilder::Debugger::ScriptId>*, TypeBuilder::OptOutput<String>* syntaxErrorMessage);
    119     void runScript(ErrorString*, const TypeBuilder::Debugger::ScriptId&, const int* executionContextId, const String* objectGroup, const bool* doNotPauseOnExceptionsAndMuteConsole, RefPtr<TypeBuilder::Runtime::RemoteObject>& result, TypeBuilder::OptOutput<bool>* wasThrown);
    120     virtual void setOverlayMessage(ErrorString*, const String*);
    121     virtual void setVariableValue(ErrorString*, int in_scopeNumber, const String& in_variableName, const RefPtr<JSONObject>& in_newValue, const String* in_callFrame, const String* in_functionObjectId);
    122     virtual void skipStackFrames(ErrorString*, const String* pattern);
    123 
    124     void schedulePauseOnNextStatement(InspectorFrontend::Debugger::Reason::Enum breakReason, PassRefPtr<JSONObject> data);
    125     void didFireTimer();
    126     void didHandleEvent();
    127     bool canBreakProgram();
    128     void breakProgram(InspectorFrontend::Debugger::Reason::Enum breakReason, PassRefPtr<JSONObject> data);
    129     virtual void scriptExecutionBlockedByCSP(const String& directiveText);
    130 
    131     class Listener {
    132     public:
    133         virtual ~Listener() { }
    134         virtual void debuggerWasEnabled() = 0;
    135         virtual void debuggerWasDisabled() = 0;
    136         virtual void stepInto() = 0;
    137         virtual void didPause() = 0;
    138     };
    139     void setListener(Listener* listener) { m_listener = listener; }
    140 
    141     virtual ScriptDebugServer& scriptDebugServer() = 0;
    142 
    143     void setBreakpoint(const String& scriptId, int lineNumber, int columnNumber, BreakpointSource, const String& condition = String());
    144     void removeBreakpoint(const String& scriptId, int lineNumber, int columnNumber, BreakpointSource);
    145 
    146     SkipPauseRequest shouldSkipExceptionPause(RefPtr<JavaScriptCallFrame>& topFrame);
    147     SkipPauseRequest shouldSkipBreakpointPause(RefPtr<JavaScriptCallFrame>& topFrame);
    148     SkipPauseRequest shouldSkipStepPause(RefPtr<JavaScriptCallFrame>& topFrame);
    149 
    150 protected:
    151     InspectorDebuggerAgent(InstrumentingAgents*, InspectorCompositeState*, InjectedScriptManager*);
    152 
    153     virtual void startListeningScriptDebugServer() = 0;
    154     virtual void stopListeningScriptDebugServer() = 0;
    155     virtual void muteConsole() = 0;
    156     virtual void unmuteConsole() = 0;
    157     InjectedScriptManager* injectedScriptManager() { return m_injectedScriptManager; }
    158     virtual InjectedScript injectedScriptForEval(ErrorString*, const int* executionContextId) = 0;
    159     virtual void addConsoleMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL) = 0;
    160 
    161     virtual void enable();
    162     virtual void disable();
    163     virtual void didPause(ScriptState*, const ScriptValue& callFrames, const ScriptValue& exception, const Vector<String>& hitBreakpoints);
    164     virtual void didContinue();
    165     void reset();
    166 
    167 private:
    168     void cancelPauseOnNextStatement();
    169     void addMessageToConsole(MessageSource, MessageType);
    170 
    171     bool enabled();
    172 
    173     PassRefPtr<TypeBuilder::Array<TypeBuilder::Debugger::CallFrame> > currentCallFrames();
    174 
    175     virtual void didParseSource(const String& scriptId, const Script&);
    176     virtual void failedToParseSource(const String& url, const String& data, int firstLine, int errorLine, const String& errorMessage);
    177 
    178     void setPauseOnExceptionsImpl(ErrorString*, int);
    179 
    180     PassRefPtr<TypeBuilder::Debugger::Location> resolveBreakpoint(const String& breakpointId, const String& scriptId, const ScriptBreakpoint&, BreakpointSource);
    181     void removeBreakpoint(const String& breakpointId);
    182     void clear();
    183     bool assertPaused(ErrorString*);
    184     void clearBreakDetails();
    185 
    186     String sourceMapURLForScript(const Script&);
    187 
    188     String scriptURL(JavaScriptCallFrame*);
    189 
    190     typedef HashMap<String, Script> ScriptsMap;
    191     typedef HashMap<String, Vector<String> > BreakpointIdToDebugServerBreakpointIdsMap;
    192     typedef HashMap<String, std::pair<String, BreakpointSource> > DebugServerBreakpointToBreakpointIdAndSourceMap;
    193 
    194     InjectedScriptManager* m_injectedScriptManager;
    195     InspectorFrontend::Debugger* m_frontend;
    196     ScriptState* m_pausedScriptState;
    197     ScriptValue m_currentCallStack;
    198     ScriptsMap m_scripts;
    199     BreakpointIdToDebugServerBreakpointIdsMap m_breakpointIdToDebugServerBreakpointIds;
    200     DebugServerBreakpointToBreakpointIdAndSourceMap m_serverBreakpoints;
    201     String m_continueToLocationBreakpointId;
    202     InspectorFrontend::Debugger::Reason::Enum m_breakReason;
    203     RefPtr<JSONObject> m_breakAuxData;
    204     bool m_javaScriptPauseScheduled;
    205     Listener* m_listener;
    206 
    207     int m_skipStepInCount;
    208     OwnPtr<RegularExpression> m_cachedSkipStackRegExp;
    209 };
    210 
    211 } // namespace WebCore
    212 
    213 
    214 #endif // !defined(InspectorDebuggerAgent_h)
    215