Line data Source code
1 : /*
2 : * Copyright (C) 2012 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_INJECTED_SCRIPT_H_
32 : #define V8_INSPECTOR_INJECTED_SCRIPT_H_
33 :
34 : #include <unordered_map>
35 : #include <unordered_set>
36 :
37 : #include "src/base/macros.h"
38 : #include "src/inspector/inspected-context.h"
39 : #include "src/inspector/protocol/Forward.h"
40 : #include "src/inspector/protocol/Runtime.h"
41 : #include "src/inspector/v8-console.h"
42 : #include "src/inspector/v8-debugger.h"
43 :
44 : #include "include/v8.h"
45 :
46 : namespace v8_inspector {
47 :
48 : class RemoteObjectId;
49 : class V8InspectorImpl;
50 : class V8InspectorSessionImpl;
51 : class ValueMirror;
52 : enum class WrapMode;
53 :
54 : using protocol::Maybe;
55 : using protocol::Response;
56 :
57 650 : class EvaluateCallback {
58 : public:
59 : virtual void sendSuccess(
60 : std::unique_ptr<protocol::Runtime::RemoteObject> result,
61 : protocol::Maybe<protocol::Runtime::ExceptionDetails>
62 : exceptionDetails) = 0;
63 : virtual void sendFailure(const protocol::DispatchResponse& response) = 0;
64 650 : virtual ~EvaluateCallback() = default;
65 : };
66 :
67 : class InjectedScript final {
68 : public:
69 : InjectedScript(InspectedContext*, int sessionId);
70 : ~InjectedScript();
71 :
72 : InspectedContext* context() const { return m_context; }
73 :
74 : Response getProperties(
75 : v8::Local<v8::Object>, const String16& groupName, bool ownProperties,
76 : bool accessorPropertiesOnly, WrapMode wrapMode,
77 : std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
78 : result,
79 : Maybe<protocol::Runtime::ExceptionDetails>*);
80 :
81 : Response getInternalProperties(
82 : v8::Local<v8::Value>, const String16& groupName,
83 : std::unique_ptr<
84 : protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
85 : result);
86 :
87 : void releaseObject(const String16& objectId);
88 :
89 : Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
90 : WrapMode wrapMode,
91 : std::unique_ptr<protocol::Runtime::RemoteObject>* result);
92 : Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
93 : WrapMode wrapMode,
94 : v8::MaybeLocal<v8::Value> customPreviewConfig,
95 : int maxCustomPreviewDepth,
96 : std::unique_ptr<protocol::Runtime::RemoteObject>* result);
97 : Response wrapObjectMirror(
98 : const ValueMirror& mirror, const String16& groupName, WrapMode wrapMode,
99 : v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
100 : std::unique_ptr<protocol::Runtime::RemoteObject>* result);
101 : std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
102 : v8::Local<v8::Object> table, v8::MaybeLocal<v8::Array> columns);
103 :
104 : void addPromiseCallback(V8InspectorSessionImpl* session,
105 : v8::MaybeLocal<v8::Value> value,
106 : const String16& objectGroup, WrapMode wrapMode,
107 : std::unique_ptr<EvaluateCallback> callback);
108 :
109 : Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
110 : String16 objectGroupName(const RemoteObjectId&) const;
111 : void releaseObjectGroup(const String16&);
112 : void setCustomObjectFormatterEnabled(bool);
113 : Response resolveCallArgument(protocol::Runtime::CallArgument*,
114 : v8::Local<v8::Value>* result);
115 :
116 : Response createExceptionDetails(
117 : const v8::TryCatch&, const String16& groupName, WrapMode wrapMode,
118 : Maybe<protocol::Runtime::ExceptionDetails>* result);
119 : Response wrapEvaluateResult(
120 : v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
121 : const String16& objectGroup, WrapMode wrapMode,
122 : std::unique_ptr<protocol::Runtime::RemoteObject>* result,
123 : Maybe<protocol::Runtime::ExceptionDetails>*);
124 : v8::Local<v8::Value> lastEvaluationResult() const;
125 : void setLastEvaluationResult(v8::Local<v8::Value> result);
126 :
127 : class Scope {
128 : public:
129 : Response initialize();
130 : void installCommandLineAPI();
131 : void ignoreExceptionsAndMuteConsole();
132 : void pretendUserGesture();
133 : void allowCodeGenerationFromStrings();
134 : v8::Local<v8::Context> context() const { return m_context; }
135 : InjectedScript* injectedScript() const { return m_injectedScript; }
136 : const v8::TryCatch& tryCatch() const { return m_tryCatch; }
137 : V8InspectorImpl* inspector() const { return m_inspector; }
138 :
139 : protected:
140 : explicit Scope(V8InspectorSessionImpl*);
141 : virtual ~Scope();
142 : virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
143 :
144 : V8InspectorImpl* m_inspector;
145 : InjectedScript* m_injectedScript;
146 :
147 : private:
148 : void cleanup();
149 : v8::debug::ExceptionBreakState setPauseOnExceptionsState(
150 : v8::debug::ExceptionBreakState);
151 :
152 : v8::HandleScope m_handleScope;
153 : v8::TryCatch m_tryCatch;
154 : v8::Local<v8::Context> m_context;
155 : std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope;
156 : bool m_ignoreExceptionsAndMuteConsole;
157 : v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
158 : bool m_userGesture;
159 : bool m_allowEval;
160 : int m_contextGroupId;
161 : int m_sessionId;
162 : };
163 :
164 9587 : class ContextScope : public Scope {
165 : public:
166 : ContextScope(V8InspectorSessionImpl*, int executionContextId);
167 : ~ContextScope() override;
168 :
169 : private:
170 : Response findInjectedScript(V8InspectorSessionImpl*) override;
171 : int m_executionContextId;
172 :
173 : DISALLOW_COPY_AND_ASSIGN(ContextScope);
174 : };
175 :
176 138188 : class ObjectScope : public Scope {
177 : public:
178 : ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
179 : ~ObjectScope() override;
180 : const String16& objectGroupName() const { return m_objectGroupName; }
181 : v8::Local<v8::Value> object() const { return m_object; }
182 :
183 : private:
184 : Response findInjectedScript(V8InspectorSessionImpl*) override;
185 : String16 m_remoteObjectId;
186 : String16 m_objectGroupName;
187 : v8::Local<v8::Value> m_object;
188 :
189 : DISALLOW_COPY_AND_ASSIGN(ObjectScope);
190 : };
191 :
192 22902 : class CallFrameScope : public Scope {
193 : public:
194 : CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
195 : ~CallFrameScope() override;
196 : size_t frameOrdinal() const { return m_frameOrdinal; }
197 :
198 : private:
199 : Response findInjectedScript(V8InspectorSessionImpl*) override;
200 : String16 m_remoteCallFrameId;
201 : size_t m_frameOrdinal;
202 :
203 : DISALLOW_COPY_AND_ASSIGN(CallFrameScope);
204 : };
205 : String16 bindObject(v8::Local<v8::Value>, const String16& groupName);
206 :
207 : private:
208 : v8::Local<v8::Object> commandLineAPI();
209 : void unbindObject(int id);
210 :
211 : static Response bindRemoteObjectIfNeeded(
212 : int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value>,
213 : const String16& groupName, protocol::Runtime::RemoteObject* remoteObject);
214 :
215 : class ProtocolPromiseHandler;
216 : void discardEvaluateCallbacks();
217 : std::unique_ptr<EvaluateCallback> takeEvaluateCallback(
218 : EvaluateCallback* callback);
219 :
220 : InspectedContext* m_context;
221 : int m_sessionId;
222 : v8::Global<v8::Value> m_lastEvaluationResult;
223 : v8::Global<v8::Object> m_commandLineAPI;
224 : int m_lastBoundObjectId = 1;
225 : std::unordered_map<int, v8::Global<v8::Value>> m_idToWrappedObject;
226 : std::unordered_map<int, String16> m_idToObjectGroupName;
227 : std::unordered_map<String16, std::vector<int>> m_nameToObjectGroup;
228 : std::unordered_set<EvaluateCallback*> m_evaluateCallbacks;
229 : bool m_customPreviewEnabled = false;
230 :
231 : DISALLOW_COPY_AND_ASSIGN(InjectedScript);
232 : };
233 :
234 : } // namespace v8_inspector
235 :
236 : #endif // V8_INSPECTOR_INJECTED_SCRIPT_H_
|