1/*
2 * Copyright (c) 2010, Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef ScriptDebugServer_h
32#define ScriptDebugServer_h
33
34#if ENABLE(JAVASCRIPT_DEBUGGER)
35
36#include "JavaScriptCallFrame.h"
37#include "PlatformString.h"
38#include "ScriptBreakpoint.h"
39#include "Timer.h"
40#include <v8-debug.h>
41#include <wtf/HashMap.h>
42#include <wtf/Noncopyable.h>
43#include <wtf/PassOwnPtr.h>
44#include <wtf/text/StringHash.h>
45
46namespace WebCore {
47
48class ScriptDebugListener;
49
50class ScriptDebugServer {
51    WTF_MAKE_NONCOPYABLE(ScriptDebugServer);
52public:
53    String setBreakpoint(const String& sourceID, const ScriptBreakpoint&, int* actualLineNumber, int* actualColumnNumber);
54    void removeBreakpoint(const String& breakpointId);
55    void clearBreakpoints();
56    void setBreakpointsActivated(bool activated);
57    void activateBreakpoints() { setBreakpointsActivated(true); }
58    void deactivateBreakpoints() { setBreakpointsActivated(false); }
59
60    enum PauseOnExceptionsState {
61        DontPauseOnExceptions,
62        PauseOnAllExceptions,
63        PauseOnUncaughtExceptions
64    };
65    PauseOnExceptionsState pauseOnExceptionsState();
66    void setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExceptionsState);
67
68    void setPauseOnNextStatement(bool pause);
69    void breakProgram();
70    void continueProgram();
71    void stepIntoStatement();
72    void stepOverStatement();
73    void stepOutOfFunction();
74
75    bool editScriptSource(const String& sourceID, const String& newContent, String* error);
76
77    void recompileAllJSFunctionsSoon() { }
78    void recompileAllJSFunctions(Timer<ScriptDebugServer>* = 0) { }
79
80    PassRefPtr<JavaScriptCallFrame> currentCallFrame();
81
82    class Task {
83    public:
84        virtual ~Task() { }
85        virtual void run() = 0;
86    };
87    static void interruptAndRun(PassOwnPtr<Task>);
88    void runPendingTasks();
89
90protected:
91    ScriptDebugServer();
92    ~ScriptDebugServer() { }
93
94    virtual ScriptDebugListener* getDebugListenerForContext(v8::Handle<v8::Context>) = 0;
95    virtual void runMessageLoopOnPause(v8::Handle<v8::Context>) = 0;
96    virtual void quitMessageLoopOnPause() = 0;
97
98    static v8::Handle<v8::Value> breakProgramCallback(const v8::Arguments& args);
99    void breakProgram(v8::Handle<v8::Object> executionState);
100
101    static void v8DebugEventCallback(const v8::Debug::EventDetails& eventDetails);
102    void handleV8DebugEvent(const v8::Debug::EventDetails& eventDetails);
103
104    void dispatchDidParseSource(ScriptDebugListener* listener, v8::Handle<v8::Object> sourceObject);
105
106    void ensureDebuggerScriptCompiled();
107
108    bool isPaused();
109
110    PauseOnExceptionsState m_pauseOnExceptionsState;
111    OwnHandle<v8::Object> m_debuggerScript;
112    RefPtr<JavaScriptCallFrame> m_currentCallFrame;
113    OwnHandle<v8::Object> m_executionState;
114    v8::Local<v8::Context> m_pausedPageContext;
115
116    bool m_breakpointsActivated;
117    OwnHandle<v8::FunctionTemplate> m_breakProgramCallbackTemplate;
118};
119
120} // namespace WebCore
121
122#endif // ENABLE(JAVASCRIPT_DEBUGGER)
123
124#endif // ScriptDebugServer_h
125