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