v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
injected-script.h
Go to the documentation of this file.
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 <memory>
35#include <unordered_map>
36#include <unordered_set>
37
41#include "src/base/macros.h"
43#include "src/inspector/protocol/Forward.h"
44#include "src/inspector/protocol/Runtime.h"
47
48namespace v8_inspector {
49
50class RemoteObjectId;
51class V8InspectorImpl;
52class V8InspectorSessionImpl;
53class ValueMirror;
54
55using protocol::Response;
56
58 public:
59 static void sendSuccess(
60 std::weak_ptr<EvaluateCallback> callback, InjectedScript* injectedScript,
61 std::unique_ptr<protocol::Runtime::RemoteObject> result,
62 std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails);
63 static void sendFailure(std::weak_ptr<EvaluateCallback> callback,
64 InjectedScript* injectedScript,
65 const protocol::DispatchResponse& response);
66
67 virtual ~EvaluateCallback() = default;
68
69 private:
70 virtual void sendSuccess(
71 std::unique_ptr<protocol::Runtime::RemoteObject> result,
72 std::unique_ptr<protocol::Runtime::ExceptionDetails>
73 exceptionDetails) = 0;
74 virtual void sendFailure(const protocol::DispatchResponse& response) = 0;
75};
76
77class InjectedScript final {
78 public:
79 InjectedScript(InspectedContext*, int sessionId);
83
84 InspectedContext* context() const { return m_context; }
85
86 Response getProperties(
87 v8::Local<v8::Object>, const String16& groupName, bool ownProperties,
88 bool accessorPropertiesOnly, bool nonIndexedPropertiesOnly,
89 const WrapOptions& wrapOptions,
90 std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
91 result,
92 std::unique_ptr<protocol::Runtime::ExceptionDetails>*);
93
95 v8::Local<v8::Value>, const String16& groupName,
96 bool accessorPropertiesOnly,
97 std::unique_ptr<
98 protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
99 internalProperties,
100 std::unique_ptr<
101 protocol::Array<protocol::Runtime::PrivatePropertyDescriptor>>*
102 privateProperties);
103
104 void releaseObject(const String16& objectId);
105
106 Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
107 const WrapOptions& wrapOptions,
108 std::unique_ptr<protocol::Runtime::RemoteObject>* result);
109 Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
110 const WrapOptions& wrapOptions,
111 v8::MaybeLocal<v8::Value> customPreviewConfig,
112 int maxCustomPreviewDepth,
113 std::unique_ptr<protocol::Runtime::RemoteObject>* result);
114 Response wrapObjectMirror(
115 const ValueMirror& mirror, const String16& groupName,
116 const WrapOptions& wrapOptions,
117 v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
118 std::unique_ptr<protocol::Runtime::RemoteObject>* result);
119 std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
121
124 const String16& objectGroup,
125 std::unique_ptr<WrapOptions> wrapOptions,
126 bool replMode, bool throwOnSideEffect,
127 std::shared_ptr<EvaluateCallback> callback);
128
129 Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
131 void releaseObjectGroup(const String16&);
133 Response resolveCallArgument(protocol::Runtime::CallArgument*,
135
136 Response createExceptionDetails(
137 const v8::TryCatch&, const String16& groupName,
138 std::unique_ptr<protocol::Runtime::ExceptionDetails>* result);
139 Response createExceptionDetails(
141 const String16& groupName,
142 std::unique_ptr<protocol::Runtime::ExceptionDetails>* result);
143
144 Response wrapEvaluateResult(
145 v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
146 const String16& objectGroup, const WrapOptions& wrapOptions,
147 bool throwOnSideEffect,
148 std::unique_ptr<protocol::Runtime::RemoteObject>* result,
149 std::unique_ptr<protocol::Runtime::ExceptionDetails>*);
152
190
191 class ContextScope : public Scope {
192 public:
193 ContextScope(V8InspectorSessionImpl*, int executionContextId);
194 ~ContextScope() override;
195 ContextScope(const ContextScope&) = delete;
197
198 private:
199 Response findInjectedScript(V8InspectorSessionImpl*) override;
201 };
202
203 class ObjectScope : public Scope {
204 public:
205 ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
206 ~ObjectScope() override;
207 ObjectScope(const ObjectScope&) = delete;
209 const String16& objectGroupName() const { return m_objectGroupName; }
211
212 private:
213 Response findInjectedScript(V8InspectorSessionImpl*) override;
217 };
218
219 class CallFrameScope : public Scope {
220 public:
221 CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
222 ~CallFrameScope() override;
225 size_t frameOrdinal() const { return m_frameOrdinal; }
226
227 private:
228 Response findInjectedScript(V8InspectorSessionImpl*) override;
231 };
233
234 private:
235 friend class EvaluateCallback;
237
239 void unbindObject(int id);
240
241 static Response bindRemoteObjectIfNeeded(
242 int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value>,
243 const String16& groupName, protocol::Runtime::RemoteObject* remoteObject);
244
247 void deleteEvaluateCallback(std::shared_ptr<EvaluateCallback> callback);
248 Response addExceptionToDetails(
249 v8::Local<v8::Value> exception,
250 protocol::Runtime::ExceptionDetails* exceptionDetails,
251 const String16& objectGroup);
252
258 std::unordered_map<int, v8::Global<v8::Value>> m_idToWrappedObject;
259 std::unordered_map<int, String16> m_idToObjectGroupName;
260 std::unordered_map<String16, std::vector<int>> m_nameToObjectGroup;
261 std::unordered_set<std::shared_ptr<EvaluateCallback>> m_evaluateCallbacks;
263};
264
265// Owns and tracks the life-time of {ProtocolPromiseHandler} instances.
266// Each Runtime#evaluate, Runtime#awaitPromise or Runtime#callFunctionOn
267// can create a {ProtocolPromiseHandler} to send the CDP response once it's
268// ready.
269//
270// A {ProtocolPromiseHandler} can be destroyed by various events:
271//
272// 1) The evaluation promise fulfills (and we send the CDP response).
273// 2) The evaluation promise gets GC'ed
274// 3) The {PromiseHandlerTracker} owning the {ProtocolPromiseHandler} dies.
275//
276// We keep the logic of {PromiseHandlerTracker} separate so it's
277// easier to move it. E.g. we could keep it on the inspector, session or
278// inspected context level.
280 public:
283 void operator=(const PromiseHandlerTracker&) = delete;
285
286 // Any reason other then kFulfilled will send a CDP error response as to
287 // not keep the request pending forever. Depending on when the
288 // {PromiseHandlerTracker} is destructed, the {EvaluateCallback} might
289 // already be dead and we can't send the error response (but that's fine).
290 enum class DiscardReason {
293 kTearDown,
294 };
295 using Id = int64_t;
296
297 template <typename... Args>
298 Id create(Args&&... args);
299 void discard(Id id, DiscardReason reason);
301
302 private:
304 const protocol::DispatchResponse& response) const;
305 void discardAll();
306
307 std::map<Id, std::unique_ptr<InjectedScript::ProtocolPromiseHandler>>
310};
311
312} // namespace v8_inspector
313
314#endif // V8_INSPECTOR_INJECTED_SCRIPT_H_
virtual void sendFailure(const protocol::DispatchResponse &response)=0
virtual ~EvaluateCallback()=default
static void sendFailure(std::weak_ptr< EvaluateCallback > callback, InjectedScript *injectedScript, const protocol::DispatchResponse &response)
virtual void sendSuccess(std::unique_ptr< protocol::Runtime::RemoteObject > result, std::unique_ptr< protocol::Runtime::ExceptionDetails > exceptionDetails)=0
static void sendSuccess(std::weak_ptr< EvaluateCallback > callback, InjectedScript *injectedScript, std::unique_ptr< protocol::Runtime::RemoteObject > result, std::unique_ptr< protocol::Runtime::ExceptionDetails > exceptionDetails)
CallFrameScope(const CallFrameScope &)=delete
CallFrameScope & operator=(const CallFrameScope &)=delete
Response findInjectedScript(V8InspectorSessionImpl *) override
CallFrameScope(V8InspectorSessionImpl *, const String16 &remoteCallFrameId)
ContextScope(const ContextScope &)=delete
Response findInjectedScript(V8InspectorSessionImpl *) override
ContextScope(V8InspectorSessionImpl *, int executionContextId)
ContextScope & operator=(const ContextScope &)=delete
v8::Local< v8::Value > object() const
ObjectScope(V8InspectorSessionImpl *, const String16 &remoteObjectId)
ObjectScope & operator=(const ObjectScope &)=delete
ObjectScope(const ObjectScope &)=delete
Response findInjectedScript(V8InspectorSessionImpl *) override
v8::Local< v8::Context > m_context
v8::Local< v8::Context > context() const
InjectedScript * injectedScript() const
const v8::TryCatch & tryCatch() const
std::unique_ptr< V8Console::CommandLineAPIScope > m_commandLineAPIScope
V8InspectorImpl * inspector() const
v8::debug::ExceptionBreakState setPauseOnExceptionsState(v8::debug::ExceptionBreakState)
v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState
virtual Response findInjectedScript(V8InspectorSessionImpl *)=0
v8::Local< v8::Value > lastEvaluationResult() const
std::unordered_map< String16, std::vector< int > > m_nameToObjectGroup
Response wrapObject(v8::Local< v8::Value >, const String16 &groupName, const WrapOptions &wrapOptions, std::unique_ptr< protocol::Runtime::RemoteObject > *result)
Response getProperties(v8::Local< v8::Object >, const String16 &groupName, bool ownProperties, bool accessorPropertiesOnly, bool nonIndexedPropertiesOnly, const WrapOptions &wrapOptions, std::unique_ptr< protocol::Array< protocol::Runtime::PropertyDescriptor > > *result, std::unique_ptr< protocol::Runtime::ExceptionDetails > *)
static Response bindRemoteObjectIfNeeded(int sessionId, v8::Local< v8::Context > context, v8::Local< v8::Value >, const String16 &groupName, protocol::Runtime::RemoteObject *remoteObject)
void setLastEvaluationResult(v8::Local< v8::Value > result)
v8::Global< v8::Object > m_commandLineAPI
void releaseObjectGroup(const String16 &)
Response resolveCallArgument(protocol::Runtime::CallArgument *, v8::Local< v8::Value > *result)
std::unordered_map< int, v8::Global< v8::Value > > m_idToWrappedObject
InjectedScript(InspectedContext *, int sessionId)
String16 bindObject(v8::Local< v8::Value >, const String16 &groupName)
InjectedScript(const InjectedScript &)=delete
InspectedContext * context() const
v8::Local< v8::Object > commandLineAPI()
void addPromiseCallback(V8InspectorSessionImpl *session, v8::MaybeLocal< v8::Value > value, const String16 &objectGroup, std::unique_ptr< WrapOptions > wrapOptions, bool replMode, bool throwOnSideEffect, std::shared_ptr< EvaluateCallback > callback)
Response wrapEvaluateResult(v8::MaybeLocal< v8::Value > maybeResultValue, const v8::TryCatch &, const String16 &objectGroup, const WrapOptions &wrapOptions, bool throwOnSideEffect, std::unique_ptr< protocol::Runtime::RemoteObject > *result, std::unique_ptr< protocol::Runtime::ExceptionDetails > *)
Response addExceptionToDetails(v8::Local< v8::Value > exception, protocol::Runtime::ExceptionDetails *exceptionDetails, const String16 &objectGroup)
Response getInternalAndPrivateProperties(v8::Local< v8::Value >, const String16 &groupName, bool accessorPropertiesOnly, std::unique_ptr< protocol::Array< protocol::Runtime::InternalPropertyDescriptor > > *internalProperties, std::unique_ptr< protocol::Array< protocol::Runtime::PrivatePropertyDescriptor > > *privateProperties)
void releaseObject(const String16 &objectId)
Response wrapObjectMirror(const ValueMirror &mirror, const String16 &groupName, const WrapOptions &wrapOptions, v8::MaybeLocal< v8::Value > customPreviewConfig, int maxCustomPreviewDepth, std::unique_ptr< protocol::Runtime::RemoteObject > *result)
Response findObject(const RemoteObjectId &, v8::Local< v8::Value > *) const
v8::Global< v8::Value > m_lastEvaluationResult
Response createExceptionDetails(const v8::TryCatch &, const String16 &groupName, std::unique_ptr< protocol::Runtime::ExceptionDetails > *result)
InjectedScript & operator=(const InjectedScript &)=delete
std::unique_ptr< protocol::Runtime::RemoteObject > wrapTable(v8::Local< v8::Object > table, v8::MaybeLocal< v8::Array > columns)
String16 objectGroupName(const RemoteObjectId &) const
std::unordered_set< std::shared_ptr< EvaluateCallback > > m_evaluateCallbacks
std::unordered_map< int, String16 > m_idToObjectGroupName
void deleteEvaluateCallback(std::shared_ptr< EvaluateCallback > callback)
InjectedScript::ProtocolPromiseHandler * get(Id id) const
void discard(Id id, DiscardReason reason)
PromiseHandlerTracker(const PromiseHandlerTracker &)=delete
std::map< Id, std::unique_ptr< InjectedScript::ProtocolPromiseHandler > > m_promiseHandlers
void sendFailure(InjectedScript::ProtocolPromiseHandler *handler, const protocol::DispatchResponse &response) const
void operator=(const PromiseHandlerTracker &)=delete
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
TNode< Object > callback
ZoneVector< RpoNumber > & result