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