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 getInternalAndPrivateProperties(
82 : v8::Local<v8::Value>, const String16& groupName,
83 : std::unique_ptr<
84 : protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
85 : internalProperties,
86 : std::unique_ptr<
87 : protocol::Array<protocol::Runtime::PrivatePropertyDescriptor>>*
88 : privateProperties);
89 :
90 : void releaseObject(const String16& objectId);
91 :
92 : Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
93 : WrapMode wrapMode,
94 : std::unique_ptr<protocol::Runtime::RemoteObject>* result);
95 : Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
96 : WrapMode wrapMode,
97 : v8::MaybeLocal<v8::Value> customPreviewConfig,
98 : int maxCustomPreviewDepth,
99 : std::unique_ptr<protocol::Runtime::RemoteObject>* result);
100 : Response wrapObjectMirror(
101 : const ValueMirror& mirror, const String16& groupName, WrapMode wrapMode,
102 : v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
103 : std::unique_ptr<protocol::Runtime::RemoteObject>* result);
104 : std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
105 : v8::Local<v8::Object> table, v8::MaybeLocal<v8::Array> columns);
106 :
107 : void addPromiseCallback(V8InspectorSessionImpl* session,
108 : v8::MaybeLocal<v8::Value> value,
109 : const String16& objectGroup, WrapMode wrapMode,
110 : std::unique_ptr<EvaluateCallback> callback);
111 :
112 : Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
113 : String16 objectGroupName(const RemoteObjectId&) const;
114 : void releaseObjectGroup(const String16&);
115 : void setCustomObjectFormatterEnabled(bool);
116 : Response resolveCallArgument(protocol::Runtime::CallArgument*,
117 : v8::Local<v8::Value>* result);
118 :
119 : Response createExceptionDetails(
120 : const v8::TryCatch&, const String16& groupName, WrapMode wrapMode,
121 : Maybe<protocol::Runtime::ExceptionDetails>* result);
122 : Response wrapEvaluateResult(
123 : v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
124 : const String16& objectGroup, WrapMode wrapMode,
125 : std::unique_ptr<protocol::Runtime::RemoteObject>* result,
126 : Maybe<protocol::Runtime::ExceptionDetails>*);
127 : v8::Local<v8::Value> lastEvaluationResult() const;
128 : void setLastEvaluationResult(v8::Local<v8::Value> result);
129 :
130 : class Scope {
131 : public:
132 : Response initialize();
133 : void installCommandLineAPI();
134 : void ignoreExceptionsAndMuteConsole();
135 : void pretendUserGesture();
136 : void allowCodeGenerationFromStrings();
137 : v8::Local<v8::Context> context() const { return m_context; }
138 : InjectedScript* injectedScript() const { return m_injectedScript; }
139 21024 : const v8::TryCatch& tryCatch() const { return m_tryCatch; }
140 : V8InspectorImpl* inspector() const { return m_inspector; }
141 :
142 : protected:
143 : explicit Scope(V8InspectorSessionImpl*);
144 : virtual ~Scope();
145 : virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
146 :
147 : V8InspectorImpl* m_inspector;
148 : InjectedScript* m_injectedScript;
149 :
150 : private:
151 : void cleanup();
152 : v8::debug::ExceptionBreakState setPauseOnExceptionsState(
153 : v8::debug::ExceptionBreakState);
154 :
155 : v8::HandleScope m_handleScope;
156 : v8::TryCatch m_tryCatch;
157 : v8::Local<v8::Context> m_context;
158 : std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope;
159 : bool m_ignoreExceptionsAndMuteConsole;
160 : v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
161 : bool m_userGesture;
162 : bool m_allowEval;
163 : int m_contextGroupId;
164 : int m_sessionId;
165 : };
166 :
167 10061 : class ContextScope : public Scope {
168 : public:
169 : ContextScope(V8InspectorSessionImpl*, int executionContextId);
170 : ~ContextScope() override;
171 :
172 : private:
173 : Response findInjectedScript(V8InspectorSessionImpl*) override;
174 : int m_executionContextId;
175 :
176 : DISALLOW_COPY_AND_ASSIGN(ContextScope);
177 : };
178 :
179 138658 : class ObjectScope : public Scope {
180 : public:
181 : ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
182 : ~ObjectScope() override;
183 : const String16& objectGroupName() const { return m_objectGroupName; }
184 : v8::Local<v8::Value> object() const { return m_object; }
185 :
186 : private:
187 : Response findInjectedScript(V8InspectorSessionImpl*) override;
188 : String16 m_remoteObjectId;
189 : String16 m_objectGroupName;
190 : v8::Local<v8::Value> m_object;
191 :
192 : DISALLOW_COPY_AND_ASSIGN(ObjectScope);
193 : };
194 :
195 23430 : class CallFrameScope : public Scope {
196 : public:
197 : CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
198 : ~CallFrameScope() override;
199 : size_t frameOrdinal() const { return m_frameOrdinal; }
200 :
201 : private:
202 : Response findInjectedScript(V8InspectorSessionImpl*) override;
203 : String16 m_remoteCallFrameId;
204 : size_t m_frameOrdinal;
205 :
206 : DISALLOW_COPY_AND_ASSIGN(CallFrameScope);
207 : };
208 : String16 bindObject(v8::Local<v8::Value>, const String16& groupName);
209 :
210 : private:
211 : v8::Local<v8::Object> commandLineAPI();
212 : void unbindObject(int id);
213 :
214 : static Response bindRemoteObjectIfNeeded(
215 : int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value>,
216 : const String16& groupName, protocol::Runtime::RemoteObject* remoteObject);
217 :
218 : class ProtocolPromiseHandler;
219 : void discardEvaluateCallbacks();
220 : std::unique_ptr<EvaluateCallback> takeEvaluateCallback(
221 : EvaluateCallback* callback);
222 :
223 : InspectedContext* m_context;
224 : int m_sessionId;
225 : v8::Global<v8::Value> m_lastEvaluationResult;
226 : v8::Global<v8::Object> m_commandLineAPI;
227 : int m_lastBoundObjectId = 1;
228 : std::unordered_map<int, v8::Global<v8::Value>> m_idToWrappedObject;
229 : std::unordered_map<int, String16> m_idToObjectGroupName;
230 : std::unordered_map<String16, std::vector<int>> m_nameToObjectGroup;
231 : std::unordered_set<EvaluateCallback*> m_evaluateCallbacks;
232 : bool m_customPreviewEnabled = false;
233 :
234 : DISALLOW_COPY_AND_ASSIGN(InjectedScript);
235 : };
236 :
237 : } // namespace v8_inspector
238 :
239 : #endif // V8_INSPECTOR_INJECTED_SCRIPT_H_
|