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 V8_INSPECTOR_V8INSPECTORIMPL_H_
32#define V8_INSPECTOR_V8INSPECTORIMPL_H_
33
34#include <vector>
35
36#include "src/base/macros.h"
37#include "src/inspector/protocol/Protocol.h"
38
39#include "include/v8-inspector.h"
40
41namespace v8_inspector {
42
43class InspectedContext;
44class V8ConsoleMessageStorage;
45class V8Debugger;
46class V8DebuggerAgentImpl;
47class V8InspectorSessionImpl;
48class V8ProfilerAgentImpl;
49class V8RuntimeAgentImpl;
50class V8StackTraceImpl;
51
52class V8InspectorImpl : public V8Inspector {
53 public:
54  V8InspectorImpl(v8::Isolate*, V8InspectorClient*);
55  ~V8InspectorImpl() override;
56
57  v8::Isolate* isolate() const { return m_isolate; }
58  V8InspectorClient* client() { return m_client; }
59  V8Debugger* debugger() { return m_debugger.get(); }
60  int contextGroupId(v8::Local<v8::Context>);
61  int contextGroupId(int contextId);
62
63  v8::MaybeLocal<v8::Value> runCompiledScript(v8::Local<v8::Context>,
64                                              v8::Local<v8::Script>);
65  v8::MaybeLocal<v8::Value> callFunction(v8::Local<v8::Function>,
66                                         v8::Local<v8::Context>,
67                                         v8::Local<v8::Value> receiver,
68                                         int argc, v8::Local<v8::Value> info[]);
69  v8::MaybeLocal<v8::Value> compileAndRunInternalScript(v8::Local<v8::Context>,
70                                                        v8::Local<v8::String>);
71  v8::MaybeLocal<v8::Value> callInternalFunction(v8::Local<v8::Function>,
72                                                 v8::Local<v8::Context>,
73                                                 v8::Local<v8::Value> receiver,
74                                                 int argc,
75                                                 v8::Local<v8::Value> info[]);
76  v8::MaybeLocal<v8::Script> compileScript(v8::Local<v8::Context>,
77                                           const String16& code,
78                                           const String16& fileName);
79  v8::Local<v8::Context> regexContext();
80
81  // V8Inspector implementation.
82  std::unique_ptr<V8InspectorSession> connect(int contextGroupId,
83                                              V8Inspector::Channel*,
84                                              const StringView& state) override;
85  void contextCreated(const V8ContextInfo&) override;
86  void contextDestroyed(v8::Local<v8::Context>) override;
87  void resetContextGroup(int contextGroupId) override;
88  void willExecuteScript(v8::Local<v8::Context>, int scriptId) override;
89  void didExecuteScript(v8::Local<v8::Context>) override;
90  void idleStarted() override;
91  void idleFinished() override;
92  unsigned exceptionThrown(v8::Local<v8::Context>, const StringView& message,
93                           v8::Local<v8::Value> exception,
94                           const StringView& detailedMessage,
95                           const StringView& url, unsigned lineNumber,
96                           unsigned columnNumber, std::unique_ptr<V8StackTrace>,
97                           int scriptId) override;
98  void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
99                        const StringView& message) override;
100  std::unique_ptr<V8StackTrace> createStackTrace(
101      v8::Local<v8::StackTrace>) override;
102  std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) override;
103  void asyncTaskScheduled(const StringView& taskName, void* task,
104                          bool recurring) override;
105  void asyncTaskCanceled(void* task) override;
106  void asyncTaskStarted(void* task) override;
107  void asyncTaskFinished(void* task) override;
108  void allAsyncTasksCanceled() override;
109
110  unsigned nextExceptionId() { return ++m_lastExceptionId; }
111  void enableStackCapturingIfNeeded();
112  void disableStackCapturingIfNeeded();
113  void muteExceptions(int contextGroupId);
114  void unmuteExceptions(int contextGroupId);
115  V8ConsoleMessageStorage* ensureConsoleMessageStorage(int contextGroupId);
116  bool hasConsoleMessageStorage(int contextGroupId);
117  using ContextByIdMap =
118      protocol::HashMap<int, std::unique_ptr<InspectedContext>>;
119  void discardInspectedContext(int contextGroupId, int contextId);
120  const ContextByIdMap* contextGroup(int contextGroupId);
121  void disconnect(V8InspectorSessionImpl*);
122  V8InspectorSessionImpl* sessionForContextGroup(int contextGroupId);
123  InspectedContext* getContext(int groupId, int contextId) const;
124  V8DebuggerAgentImpl* enabledDebuggerAgentForGroup(int contextGroupId);
125  V8RuntimeAgentImpl* enabledRuntimeAgentForGroup(int contextGroupId);
126  V8ProfilerAgentImpl* enabledProfilerAgentForGroup(int contextGroupId);
127
128 private:
129  v8::MaybeLocal<v8::Value> callFunction(
130      v8::Local<v8::Function>, v8::Local<v8::Context>,
131      v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> info[],
132      v8::MicrotasksScope::Type runMicrotasks);
133
134  v8::Isolate* m_isolate;
135  V8InspectorClient* m_client;
136  std::unique_ptr<V8Debugger> m_debugger;
137  v8::Global<v8::Context> m_regexContext;
138  int m_capturingStackTracesCount;
139  unsigned m_lastExceptionId;
140  int m_lastContextId;
141
142  using MuteExceptionsMap = protocol::HashMap<int, int>;
143  MuteExceptionsMap m_muteExceptionsMap;
144
145  using ContextsByGroupMap =
146      protocol::HashMap<int, std::unique_ptr<ContextByIdMap>>;
147  ContextsByGroupMap m_contexts;
148
149  using SessionMap = protocol::HashMap<int, V8InspectorSessionImpl*>;
150  SessionMap m_sessions;
151
152  using ConsoleStorageMap =
153      protocol::HashMap<int, std::unique_ptr<V8ConsoleMessageStorage>>;
154  ConsoleStorageMap m_consoleStorageMap;
155
156  protocol::HashMap<int, int> m_contextIdToGroupIdMap;
157
158  DISALLOW_COPY_AND_ASSIGN(V8InspectorImpl);
159};
160
161}  // namespace v8_inspector
162
163#endif  // V8_INSPECTOR_V8INSPECTORIMPL_H_
164