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