1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef JavaScriptDebugServer_h
30#define JavaScriptDebugServer_h
31
32#if ENABLE(JAVASCRIPT_DEBUGGER) && USE(JSC)
33
34#include "Timer.h"
35#include <debugger/Debugger.h>
36#include <runtime/UString.h>
37#include <wtf/HashMap.h>
38#include <wtf/HashSet.h>
39#include <wtf/RefPtr.h>
40
41namespace JSC {
42    class DebuggerCallFrame;
43    class JSGlobalObject;
44}
45
46namespace WebCore {
47
48    class Frame;
49    class FrameView;
50    class Page;
51    class PageGroup;
52    class JavaScriptCallFrame;
53    class JavaScriptDebugListener;
54
55    class JavaScriptDebugServer : JSC::Debugger, public Noncopyable {
56    public:
57        static JavaScriptDebugServer& shared();
58
59        void addListener(JavaScriptDebugListener*);
60        void removeListener(JavaScriptDebugListener*);
61
62        void addListener(JavaScriptDebugListener*, Page*);
63        void removeListener(JavaScriptDebugListener*, Page*);
64
65        void addBreakpoint(intptr_t sourceID, unsigned lineNumber, const JSC::UString& condition);
66        void updateBreakpoint(intptr_t sourceID, unsigned lineNumber, const JSC::UString& condition);
67        void removeBreakpoint(intptr_t sourceID, unsigned lineNumber);
68        bool hasBreakpoint(intptr_t sourceID, unsigned lineNumber) const;
69        void clearBreakpoints();
70
71        enum PauseOnExceptionsState {
72            DontPauseOnExceptions,
73            PauseOnAllExceptions,
74            PauseOnUncaughtExceptions
75        };
76        PauseOnExceptionsState pauseOnExceptionsState() const { return m_pauseOnExceptionsState; }
77        void setPauseOnExceptionsState(PauseOnExceptionsState);
78
79        void pauseProgram();
80        void continueProgram();
81        void stepIntoStatement();
82        void stepOverStatement();
83        void stepOutOfFunction();
84
85        void recompileAllJSFunctionsSoon();
86        void recompileAllJSFunctions(Timer<JavaScriptDebugServer>* = 0);
87
88        JavaScriptCallFrame* currentCallFrame();
89
90        void pageCreated(Page*);
91
92        typedef HashSet<JavaScriptDebugListener*> ListenerSet;
93        typedef void (JavaScriptDebugListener::*JavaScriptExecutionCallback)();
94
95    private:
96        class BreakpointInfo {
97        public:
98            BreakpointInfo(const JSC::UString& condition) : m_condition(condition) {}
99            const JSC::UString& condition() const;
100            void setCondition(const JSC::UString& condition);
101        private:
102            JSC::UString m_condition;
103        };
104
105        JavaScriptDebugServer();
106        ~JavaScriptDebugServer();
107
108        bool hasListeners() const { return !m_listeners.isEmpty() || !m_pageListenersMap.isEmpty(); }
109        bool hasGlobalListeners() const { return !m_listeners.isEmpty(); }
110        bool hasListenersInterestedInPage(Page*);
111
112        void setJavaScriptPaused(const PageGroup&, bool paused);
113        void setJavaScriptPaused(Page*, bool paused);
114        void setJavaScriptPaused(Frame*, bool paused);
115        void setJavaScriptPaused(FrameView*, bool paused);
116
117        void dispatchFunctionToListeners(JavaScriptExecutionCallback, Page*);
118        void pauseIfNeeded(Page*);
119        BreakpointInfo* breakpointInfo(intptr_t sourceID, unsigned lineNumber) const;
120        void updateBreakpointInfo(BreakpointInfo* info, const JSC::UString& condition);
121
122        virtual void detach(JSC::JSGlobalObject*);
123
124        virtual void sourceParsed(JSC::ExecState*, const JSC::SourceCode&, int errorLine, const JSC::UString& errorMsg);
125        virtual void callEvent(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber);
126        virtual void atStatement(const JSC::DebuggerCallFrame&, intptr_t sourceID, int firstLine);
127        virtual void returnEvent(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber);
128        virtual void exception(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber, bool hasHandler);
129        virtual void willExecuteProgram(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno);
130        virtual void didExecuteProgram(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno);
131        virtual void didReachBreakpoint(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineno);
132
133        void didAddListener(Page*);
134        void didRemoveListener(Page*);
135        void didRemoveLastListener();
136
137        typedef HashMap<Page*, ListenerSet*> PageListenersMap;
138        typedef HashMap<unsigned, BreakpointInfo*> LineToBreakpointInfoMap;
139        typedef HashMap<intptr_t, LineToBreakpointInfoMap*> BreakpointsMap;
140
141        PageListenersMap m_pageListenersMap;
142        ListenerSet m_listeners;
143        bool m_callingListeners;
144        PauseOnExceptionsState m_pauseOnExceptionsState;
145        bool m_pauseOnNextStatement;
146        bool m_paused;
147        bool m_doneProcessingDebuggerEvents;
148        JavaScriptCallFrame* m_pauseOnCallFrame;
149        RefPtr<JavaScriptCallFrame> m_currentCallFrame;
150        BreakpointsMap m_breakpoints;
151        Timer<JavaScriptDebugServer> m_recompileTimer;
152    };
153
154} // namespace WebCore
155
156#endif // ENABLE(JAVASCRIPT_DEBUGGER)
157
158#endif // JavaScriptDebugServer_h
159