• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_V8_INSPECTOR_IMPL_H_
32 #define V8_INSPECTOR_V8_INSPECTOR_IMPL_H_
33 
34 #include <functional>
35 #include <map>
36 #include <unordered_map>
37 
38 #include "src/base/macros.h"
39 #include "src/base/platform/mutex.h"
40 #include "src/inspector/protocol/Protocol.h"
41 
42 #include "include/v8-inspector.h"
43 
44 namespace v8_inspector {
45 
46 class InspectedContext;
47 class V8Console;
48 class V8ConsoleMessageStorage;
49 class V8Debugger;
50 class V8DebuggerAgentImpl;
51 class V8InspectorSessionImpl;
52 class V8ProfilerAgentImpl;
53 class V8RuntimeAgentImpl;
54 class V8StackTraceImpl;
55 
56 class V8InspectorImpl : public V8Inspector {
57  public:
58   V8InspectorImpl(v8::Isolate*, V8InspectorClient*);
59   ~V8InspectorImpl() override;
60 
isolate()61   v8::Isolate* isolate() const { return m_isolate; }
client()62   V8InspectorClient* client() { return m_client; }
debugger()63   V8Debugger* debugger() { return m_debugger.get(); }
64   int contextGroupId(v8::Local<v8::Context>) const;
65   int contextGroupId(int contextId) const;
isolateId()66   uint64_t isolateId() const { return m_isolateId; }
67 
68   v8::MaybeLocal<v8::Value> compileAndRunInternalScript(v8::Local<v8::Context>,
69                                                         v8::Local<v8::String>);
70   v8::MaybeLocal<v8::Script> compileScript(v8::Local<v8::Context>,
71                                            const String16& code,
72                                            const String16& fileName);
73   v8::Local<v8::Context> regexContext();
74 
75   // V8Inspector implementation.
76   std::unique_ptr<V8InspectorSession> connect(int contextGroupId,
77                                               V8Inspector::Channel*,
78                                               const StringView& state) override;
79   void contextCreated(const V8ContextInfo&) override;
80   void contextDestroyed(v8::Local<v8::Context>) override;
81   v8::MaybeLocal<v8::Context> contextById(int groupId,
82                                           v8::Maybe<int> contextId) override;
83   void contextCollected(int contextGroupId, int contextId);
84   void resetContextGroup(int contextGroupId) override;
85   void idleStarted() override;
86   void idleFinished() override;
87   unsigned exceptionThrown(v8::Local<v8::Context>, const StringView& message,
88                            v8::Local<v8::Value> exception,
89                            const StringView& detailedMessage,
90                            const StringView& url, unsigned lineNumber,
91                            unsigned columnNumber, std::unique_ptr<V8StackTrace>,
92                            int scriptId) override;
93   void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
94                         const StringView& message) override;
95   std::unique_ptr<V8StackTrace> createStackTrace(
96       v8::Local<v8::StackTrace>) override;
97   std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) override;
98   void asyncTaskScheduled(const StringView& taskName, void* task,
99                           bool recurring) override;
100   void asyncTaskCanceled(void* task) override;
101   void asyncTaskStarted(void* task) override;
102   void asyncTaskFinished(void* task) override;
103   void allAsyncTasksCanceled() override;
104 
105   V8StackTraceId storeCurrentStackTrace(const StringView& description) override;
106   void externalAsyncTaskStarted(const V8StackTraceId& parent) override;
107   void externalAsyncTaskFinished(const V8StackTraceId& parent) override;
108 
nextExceptionId()109   unsigned nextExceptionId() { return ++m_lastExceptionId; }
110   void enableStackCapturingIfNeeded();
111   void disableStackCapturingIfNeeded();
112   void muteExceptions(int contextGroupId);
113   void unmuteExceptions(int contextGroupId);
114   V8ConsoleMessageStorage* ensureConsoleMessageStorage(int contextGroupId);
115   bool hasConsoleMessageStorage(int contextGroupId);
116   void discardInspectedContext(int contextGroupId, int contextId);
117   void disconnect(V8InspectorSessionImpl*);
118   V8InspectorSessionImpl* sessionById(int contextGroupId, int sessionId);
119   InspectedContext* getContext(int groupId, int contextId) const;
120   InspectedContext* getContext(int contextId) const;
121   V8Console* console();
122   void forEachContext(int contextGroupId,
123                       std::function<void(InspectedContext*)> callback);
124   void forEachSession(int contextGroupId,
125                       std::function<void(V8InspectorSessionImpl*)> callback);
126 
127   class EvaluateScope {
128    public:
129     explicit EvaluateScope(v8::Isolate* isolate);
130     ~EvaluateScope();
131 
132     protocol::Response setTimeout(double timeout);
133 
134    private:
135     v8::Isolate* m_isolate;
136     class TerminateTask;
137     struct CancelToken;
138     std::shared_ptr<CancelToken> m_cancelToken;
139     v8::Isolate::SafeForTerminationScope m_safeForTerminationScope;
140   };
141 
142  private:
143   v8::Isolate* m_isolate;
144   V8InspectorClient* m_client;
145   std::unique_ptr<V8Debugger> m_debugger;
146   v8::Global<v8::Context> m_regexContext;
147   int m_capturingStackTracesCount;
148   unsigned m_lastExceptionId;
149   int m_lastContextId;
150   int m_lastSessionId = 0;
151   uint64_t m_isolateId;
152 
153   using MuteExceptionsMap = std::unordered_map<int, int>;
154   MuteExceptionsMap m_muteExceptionsMap;
155 
156   using ContextByIdMap =
157       std::unordered_map<int, std::unique_ptr<InspectedContext>>;
158   using ContextsByGroupMap =
159       std::unordered_map<int, std::unique_ptr<ContextByIdMap>>;
160   ContextsByGroupMap m_contexts;
161 
162   // contextGroupId -> sessionId -> session
163   std::unordered_map<int, std::map<int, V8InspectorSessionImpl*>> m_sessions;
164 
165   using ConsoleStorageMap =
166       std::unordered_map<int, std::unique_ptr<V8ConsoleMessageStorage>>;
167   ConsoleStorageMap m_consoleStorageMap;
168 
169   std::unordered_map<int, int> m_contextIdToGroupIdMap;
170 
171   std::unique_ptr<V8Console> m_console;
172 
173   DISALLOW_COPY_AND_ASSIGN(V8InspectorImpl);
174 };
175 
176 }  // namespace v8_inspector
177 
178 #endif  // V8_INSPECTOR_V8_INSPECTOR_IMPL_H_
179