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