1/*
2 * Copyright (C) 2008 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 ScriptDebugServer_h
31#define ScriptDebugServer_h
32
33#if ENABLE(JAVASCRIPT_DEBUGGER)
34
35#include "ScriptDebugListener.h"
36#include "PlatformString.h"
37#include "ScriptBreakpoint.h"
38#include "Timer.h"
39
40#include <debugger/Debugger.h>
41#include <runtime/UString.h>
42#include <wtf/HashMap.h>
43#include <wtf/HashSet.h>
44#include <wtf/RefPtr.h>
45#include <wtf/text/TextPosition.h>
46
47namespace JSC {
48class DebuggerCallFrame;
49class JSGlobalObject;
50}
51namespace WebCore {
52
53class ScriptDebugListener;
54class JavaScriptCallFrame;
55
56class ScriptDebugServer : protected JSC::Debugger {
57    WTF_MAKE_NONCOPYABLE(ScriptDebugServer); WTF_MAKE_FAST_ALLOCATED;
58public:
59    String setBreakpoint(const String& sourceID, const ScriptBreakpoint&, int* actualLineNumber, int* actualColumnNumber);
60    void removeBreakpoint(const String& breakpointId);
61    void clearBreakpoints();
62    void setBreakpointsActivated(bool activated);
63    void activateBreakpoints() { setBreakpointsActivated(true); }
64    void deactivateBreakpoints() { setBreakpointsActivated(false); }
65
66    enum PauseOnExceptionsState {
67        DontPauseOnExceptions,
68        PauseOnAllExceptions,
69        PauseOnUncaughtExceptions
70    };
71    PauseOnExceptionsState pauseOnExceptionsState() const { return m_pauseOnExceptionsState; }
72    void setPauseOnExceptionsState(PauseOnExceptionsState);
73
74    void setPauseOnNextStatement(bool pause);
75    void breakProgram();
76    void continueProgram();
77    void stepIntoStatement();
78    void stepOverStatement();
79    void stepOutOfFunction();
80
81    bool editScriptSource(const String& sourceID, const String& newContent, String* error);
82
83    void recompileAllJSFunctionsSoon();
84    virtual void recompileAllJSFunctions(Timer<ScriptDebugServer>* = 0) = 0;
85
86    JavaScriptCallFrame* currentCallFrame();
87
88protected:
89    typedef HashSet<ScriptDebugListener*> ListenerSet;
90    typedef void (ScriptDebugServer::*JavaScriptExecutionCallback)(ScriptDebugListener*);
91
92    ScriptDebugServer();
93    ~ScriptDebugServer();
94
95    virtual ListenerSet* getListenersForGlobalObject(JSC::JSGlobalObject*) = 0;
96    virtual void didPause(JSC::JSGlobalObject*) = 0;
97    virtual void didContinue(JSC::JSGlobalObject*) = 0;
98
99    bool hasBreakpoint(intptr_t sourceID, const TextPosition0&) const;
100
101    void dispatchFunctionToListeners(JavaScriptExecutionCallback, JSC::JSGlobalObject*);
102    void dispatchFunctionToListeners(const ListenerSet& listeners, JavaScriptExecutionCallback callback);
103    void dispatchDidPause(ScriptDebugListener*);
104    void dispatchDidContinue(ScriptDebugListener*);
105    void dispatchDidParseSource(const ListenerSet& listeners, JSC::SourceProvider*, bool isContentScript);
106    void dispatchFailedToParseSource(const ListenerSet& listeners, JSC::SourceProvider*, int errorLine, const String& errorMessage);
107
108    void createCallFrameAndPauseIfNeeded(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber);
109    void updateCallFrameAndPauseIfNeeded(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber);
110    void pauseIfNeeded(JSC::JSGlobalObject* dynamicGlobalObject);
111
112    virtual void detach(JSC::JSGlobalObject*);
113
114    virtual void sourceParsed(JSC::ExecState*, JSC::SourceProvider*, int errorLine, const JSC::UString& errorMsg);
115    virtual void callEvent(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber);
116    virtual void atStatement(const JSC::DebuggerCallFrame&, intptr_t sourceID, int firstLine);
117    virtual void returnEvent(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber);
118    virtual void exception(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber, bool hasHandler);
119    virtual void willExecuteProgram(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno);
120    virtual void didExecuteProgram(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno);
121    virtual void didReachBreakpoint(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno);
122
123    typedef HashMap<Page*, ListenerSet*> PageListenersMap;
124    typedef HashMap<long, ScriptBreakpoint> LineToBreakpointMap;
125    typedef HashMap<intptr_t, LineToBreakpointMap> SourceIdToBreakpointsMap;
126
127    PageListenersMap m_pageListenersMap;
128    bool m_callingListeners;
129    PauseOnExceptionsState m_pauseOnExceptionsState;
130    bool m_pauseOnNextStatement;
131    bool m_paused;
132    bool m_doneProcessingDebuggerEvents;
133    bool m_breakpointsActivated;
134    JavaScriptCallFrame* m_pauseOnCallFrame;
135    RefPtr<JavaScriptCallFrame> m_currentCallFrame;
136    SourceIdToBreakpointsMap m_sourceIdToBreakpoints;
137    Timer<ScriptDebugServer> m_recompileTimer;
138};
139
140} // namespace WebCore
141
142#endif // ENABLE(JAVASCRIPT_DEBUGGER)
143
144#endif // ScriptDebugServer_h
145