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