1/*
2 * Copyright (C) 2008, 2013, 2014 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 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#pragma once
31
32#include "Debugger.h"
33#include "ScriptBreakpoint.h"
34#include "ScriptDebugListener.h"
35#include <wtf/HashMap.h>
36#include <wtf/HashSet.h>
37#include <wtf/text/WTFString.h>
38
39namespace JSC {
40class ExecState;
41class JSGlobalObject;
42class VM;
43}
44
45namespace Inspector {
46
47class JS_EXPORT_PRIVATE ScriptDebugServer : public JSC::Debugger {
48 WTF_MAKE_NONCOPYABLE(ScriptDebugServer);
49 WTF_MAKE_FAST_ALLOCATED;
50public:
51
52 // FIXME: Move BreakpointAction handling into JSC::Debugger or InspectorDebuggerAgent.
53 void setBreakpointActions(JSC::BreakpointID, const ScriptBreakpoint&);
54 void removeBreakpointActions(JSC::BreakpointID);
55 void clearBreakpointActions();
56
57 const BreakpointActions& getActionsForBreakpoint(JSC::BreakpointID);
58
59 void addListener(ScriptDebugListener*);
60 void removeListener(ScriptDebugListener*, bool isBeingDestroyed);
61
62protected:
63 typedef HashSet<ScriptDebugListener*> ListenerSet;
64 typedef void (ScriptDebugServer::*JavaScriptExecutionCallback)(ScriptDebugListener*);
65
66 ScriptDebugServer(JSC::VM&);
67 ~ScriptDebugServer();
68
69 virtual void attachDebugger() = 0;
70 virtual void detachDebugger(bool isBeingDestroyed) = 0;
71
72 virtual void didPause(JSC::JSGlobalObject*) = 0;
73 virtual void didContinue(JSC::JSGlobalObject*) = 0;
74 virtual void runEventLoopWhilePaused() = 0;
75 virtual bool isContentScript(JSC::ExecState*) const = 0;
76 virtual void reportException(JSC::ExecState*, JSC::Exception*) const = 0;
77
78 bool evaluateBreakpointAction(const ScriptBreakpointAction&);
79
80 void dispatchFunctionToListeners(JavaScriptExecutionCallback);
81 void dispatchFunctionToListeners(const ListenerSet& listeners, JavaScriptExecutionCallback);
82 void dispatchDidPause(ScriptDebugListener*);
83 void dispatchDidContinue(ScriptDebugListener*);
84 void dispatchDidParseSource(const ListenerSet& listeners, JSC::SourceProvider*, bool isContentScript);
85 void dispatchFailedToParseSource(const ListenerSet& listeners, JSC::SourceProvider*, int errorLine, const String& errorMessage);
86 void dispatchBreakpointActionLog(JSC::ExecState*, const String&);
87 void dispatchBreakpointActionSound(JSC::ExecState*, int breakpointActionIdentifier);
88 void dispatchBreakpointActionProbe(JSC::ExecState*, const ScriptBreakpointAction&, JSC::JSValue sample);
89
90 bool m_doneProcessingDebuggerEvents { true };
91
92private:
93 typedef HashMap<JSC::BreakpointID, BreakpointActions> BreakpointIDToActionsMap;
94
95 void sourceParsed(JSC::ExecState*, JSC::SourceProvider*, int errorLine, const String& errorMsg) final;
96 void handleBreakpointHit(JSC::JSGlobalObject*, const JSC::Breakpoint&) final;
97 void handleExceptionInBreakpointCondition(JSC::ExecState*, JSC::Exception*) const final;
98 void handlePause(JSC::JSGlobalObject*, JSC::Debugger::ReasonForPause) final;
99 void notifyDoneProcessingDebuggerEvents() final;
100
101 JSC::JSValue exceptionOrCaughtValue(JSC::ExecState*);
102
103 BreakpointIDToActionsMap m_breakpointIDToActions;
104 ListenerSet m_listeners;
105 bool m_callingListeners { false };
106 unsigned m_nextProbeSampleId { 1 };
107 unsigned m_currentProbeBatchId { 0 };
108};
109
110} // namespace Inspector
111