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#include "InspectorBackendDispatcher.h"
35#include "bindings/v8/ScopedPersistent.h"
36#include "core/inspector/ScriptBreakpoint.h"
37#include "core/inspector/ScriptDebugListener.h"
38#include <v8-debug.h>
39#include "wtf/HashMap.h"
40#include "wtf/Noncopyable.h"
41#include "wtf/PassOwnPtr.h"
42#include "wtf/Vector.h"
43#include "wtf/text/StringHash.h"
44#include "wtf/text/WTFString.h"
45
46namespace WebCore {
47
48class ScriptController;
49class ScriptDebugListener;
50class ScriptObject;
51class ScriptSourceCode;
52class ScriptState;
53class ScriptValue;
54class JavaScriptCallFrame;
55
56class ScriptDebugServer {
57    WTF_MAKE_NONCOPYABLE(ScriptDebugServer);
58public:
59    String setBreakpoint(const String& sourceID, const ScriptBreakpoint&, int* actualLineNumber, int* actualColumnNumber, bool interstatementLocation);
60    void removeBreakpoint(const String& breakpointId);
61    void clearBreakpoints();
62    void setBreakpointsActivated(bool activated);
63
64    enum PauseOnExceptionsState {
65        DontPauseOnExceptions,
66        PauseOnAllExceptions,
67        PauseOnUncaughtExceptions
68    };
69    PauseOnExceptionsState pauseOnExceptionsState();
70    void setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExceptionsState);
71
72    void setPauseOnNextStatement(bool pause);
73    bool canBreakProgram();
74    void breakProgram();
75    void continueProgram();
76    void stepIntoStatement();
77    void stepOverStatement(const ScriptValue& frame);
78    void stepOutOfFunction(const ScriptValue& frame);
79
80    bool setScriptSource(const String& sourceID, const String& newContent, bool preview, String* error, RefPtr<TypeBuilder::Debugger::SetScriptSourceError>&, ScriptValue* newCallFrames, ScriptObject* result);
81    ScriptValue currentCallFrames();
82
83    class Task {
84    public:
85        virtual ~Task() { }
86        virtual void run() = 0;
87    };
88    static void interruptAndRun(PassOwnPtr<Task>, v8::Isolate*);
89    void runPendingTasks();
90
91    bool isPaused();
92    bool runningNestedMessageLoop() { return m_runningNestedMessageLoop; }
93
94    v8::Local<v8::Value> functionScopes(v8::Handle<v8::Function>);
95    v8::Local<v8::Value> getInternalProperties(v8::Handle<v8::Object>&);
96    v8::Handle<v8::Value> setFunctionVariableValue(v8::Handle<v8::Value> functionValue, int scopeNumber, const String& variableName, v8::Handle<v8::Value> newValue);
97    v8::Local<v8::Value> callDebuggerMethod(const char* functionName, int argc, v8::Handle<v8::Value> argv[]);
98
99    virtual void compileScript(ScriptState*, const String& expression, const String& sourceURL, String* scriptId, String* exceptionMessage);
100    virtual void clearCompiledScripts();
101    virtual void runScript(ScriptState*, const String& scriptId, ScriptValue* result, bool* wasThrown, String* exceptionMessage);
102    virtual void setPreprocessorSource(const String&) { }
103    virtual void preprocessBeforeCompile(const v8::Debug::EventDetails&) { }
104    virtual PassOwnPtr<ScriptSourceCode> preprocess(Frame*, const ScriptSourceCode&);
105    virtual String preprocessEventListener(Frame*, const String& source, const String& url, const String& functionName);
106
107protected:
108    explicit ScriptDebugServer(v8::Isolate*);
109    virtual ~ScriptDebugServer();
110
111    virtual ScriptDebugListener* getDebugListenerForContext(v8::Handle<v8::Context>) = 0;
112    virtual void runMessageLoopOnPause(v8::Handle<v8::Context>) = 0;
113    virtual void quitMessageLoopOnPause() = 0;
114
115    static void breakProgramCallback(const v8::FunctionCallbackInfo<v8::Value>&);
116    void handleProgramBreak(v8::Handle<v8::Object> executionState, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpoints);
117    void handleProgramBreak(const v8::Debug::EventDetails&, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpointNumbers);
118
119    static void v8DebugEventCallback(const v8::Debug::EventDetails& eventDetails);
120    void handleV8DebugEvent(const v8::Debug::EventDetails& eventDetails);
121
122    void dispatchDidParseSource(ScriptDebugListener* listener, v8::Handle<v8::Object> sourceObject);
123
124    void ensureDebuggerScriptCompiled();
125
126    PauseOnExceptionsState m_pauseOnExceptionsState;
127    ScopedPersistent<v8::Object> m_debuggerScript;
128    ScopedPersistent<v8::Object> m_executionState;
129    v8::Handle<v8::Context> m_pausedContext;
130    bool m_breakpointsActivated;
131    ScopedPersistent<v8::FunctionTemplate> m_breakProgramCallbackTemplate;
132    HashMap<String, OwnPtr<ScopedPersistent<v8::Script> > > m_compiledScripts;
133    v8::Isolate* m_isolate;
134
135private:
136    void stepCommandWithFrame(const char* functionName, const ScriptValue& frame);
137    PassRefPtr<JavaScriptCallFrame> wrapCallFrames(v8::Handle<v8::Object> executionState, int maximumLimit);
138    bool executeSkipPauseRequest(ScriptDebugListener::SkipPauseRequest, v8::Handle<v8::Object> executionState);
139
140    bool m_runningNestedMessageLoop;
141};
142
143} // namespace WebCore
144
145
146#endif // ScriptDebugServer_h
147