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