OLD | NEW |
| (Empty) |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/inspector/V8InspectorSessionImpl.h" | |
6 | |
7 #include "src/inspector/InjectedScript.h" | |
8 #include "src/inspector/InspectedContext.h" | |
9 #include "src/inspector/RemoteObjectId.h" | |
10 #include "src/inspector/SearchUtil.h" | |
11 #include "src/inspector/StringUtil.h" | |
12 #include "src/inspector/V8ConsoleAgentImpl.h" | |
13 #include "src/inspector/V8Debugger.h" | |
14 #include "src/inspector/V8DebuggerAgentImpl.h" | |
15 #include "src/inspector/V8HeapProfilerAgentImpl.h" | |
16 #include "src/inspector/V8InspectorImpl.h" | |
17 #include "src/inspector/V8ProfilerAgentImpl.h" | |
18 #include "src/inspector/V8RuntimeAgentImpl.h" | |
19 #include "src/inspector/V8SchemaAgentImpl.h" | |
20 #include "src/inspector/protocol/Protocol.h" | |
21 #include "src/inspector/public/V8ContextInfo.h" | |
22 #include "src/inspector/public/V8InspectorClient.h" | |
23 | |
24 namespace v8_inspector { | |
25 | |
26 // static | |
27 bool V8InspectorSession::canDispatchMethod(const StringView& method) | |
28 { | |
29 return stringViewStartsWith(method, protocol::Runtime::Metainfo::commandPref
ix) | |
30 || stringViewStartsWith(method, protocol::Debugger::Metainfo::commandPre
fix) | |
31 || stringViewStartsWith(method, protocol::Profiler::Metainfo::commandPre
fix) | |
32 || stringViewStartsWith(method, protocol::HeapProfiler::Metainfo::comman
dPrefix) | |
33 || stringViewStartsWith(method, protocol::Console::Metainfo::commandPref
ix) | |
34 || stringViewStartsWith(method, protocol::Schema::Metainfo::commandPrefi
x); | |
35 } | |
36 | |
37 std::unique_ptr<V8InspectorSessionImpl> V8InspectorSessionImpl::create(V8Inspect
orImpl* inspector, int contextGroupId, V8Inspector::Channel* channel, const Stri
ngView& state) | |
38 { | |
39 return wrapUnique(new V8InspectorSessionImpl(inspector, contextGroupId, chan
nel, state)); | |
40 } | |
41 | |
42 V8InspectorSessionImpl::V8InspectorSessionImpl(V8InspectorImpl* inspector, int c
ontextGroupId, V8Inspector::Channel* channel, const StringView& savedState) | |
43 : m_contextGroupId(contextGroupId) | |
44 , m_inspector(inspector) | |
45 , m_channel(channel) | |
46 , m_customObjectFormatterEnabled(false) | |
47 , m_dispatcher(this) | |
48 , m_state(nullptr) | |
49 , m_runtimeAgent(nullptr) | |
50 , m_debuggerAgent(nullptr) | |
51 , m_heapProfilerAgent(nullptr) | |
52 , m_profilerAgent(nullptr) | |
53 , m_consoleAgent(nullptr) | |
54 , m_schemaAgent(nullptr) | |
55 { | |
56 if (savedState.length()) { | |
57 std::unique_ptr<protocol::Value> state = protocol::parseJSON(toString16(
savedState)); | |
58 if (state) | |
59 m_state = protocol::DictionaryValue::cast(std::move(state)); | |
60 if (!m_state) | |
61 m_state = protocol::DictionaryValue::create(); | |
62 } else { | |
63 m_state = protocol::DictionaryValue::create(); | |
64 } | |
65 | |
66 m_runtimeAgent = wrapUnique(new V8RuntimeAgentImpl(this, this, agentState(pr
otocol::Runtime::Metainfo::domainName))); | |
67 protocol::Runtime::Dispatcher::wire(&m_dispatcher, m_runtimeAgent.get()); | |
68 | |
69 m_debuggerAgent = wrapUnique(new V8DebuggerAgentImpl(this, this, agentState(
protocol::Debugger::Metainfo::domainName))); | |
70 protocol::Debugger::Dispatcher::wire(&m_dispatcher, m_debuggerAgent.get()); | |
71 | |
72 m_profilerAgent = wrapUnique(new V8ProfilerAgentImpl(this, this, agentState(
protocol::Profiler::Metainfo::domainName))); | |
73 protocol::Profiler::Dispatcher::wire(&m_dispatcher, m_profilerAgent.get()); | |
74 | |
75 m_heapProfilerAgent = wrapUnique(new V8HeapProfilerAgentImpl(this, this, age
ntState(protocol::HeapProfiler::Metainfo::domainName))); | |
76 protocol::HeapProfiler::Dispatcher::wire(&m_dispatcher, m_heapProfilerAgent.
get()); | |
77 | |
78 m_consoleAgent = wrapUnique(new V8ConsoleAgentImpl(this, this, agentState(pr
otocol::Console::Metainfo::domainName))); | |
79 protocol::Console::Dispatcher::wire(&m_dispatcher, m_consoleAgent.get()); | |
80 | |
81 m_schemaAgent = wrapUnique(new V8SchemaAgentImpl(this, this, agentState(prot
ocol::Schema::Metainfo::domainName))); | |
82 protocol::Schema::Dispatcher::wire(&m_dispatcher, m_schemaAgent.get()); | |
83 | |
84 if (savedState.length()) { | |
85 m_runtimeAgent->restore(); | |
86 m_debuggerAgent->restore(); | |
87 m_heapProfilerAgent->restore(); | |
88 m_profilerAgent->restore(); | |
89 m_consoleAgent->restore(); | |
90 } | |
91 } | |
92 | |
93 V8InspectorSessionImpl::~V8InspectorSessionImpl() | |
94 { | |
95 ErrorString errorString; | |
96 m_consoleAgent->disable(&errorString); | |
97 m_profilerAgent->disable(&errorString); | |
98 m_heapProfilerAgent->disable(&errorString); | |
99 m_debuggerAgent->disable(&errorString); | |
100 m_runtimeAgent->disable(&errorString); | |
101 | |
102 discardInjectedScripts(); | |
103 m_inspector->disconnect(this); | |
104 } | |
105 | |
106 protocol::DictionaryValue* V8InspectorSessionImpl::agentState(const String16& na
me) | |
107 { | |
108 protocol::DictionaryValue* state = m_state->getObject(name); | |
109 if (!state) { | |
110 std::unique_ptr<protocol::DictionaryValue> newState = protocol::Dictiona
ryValue::create(); | |
111 state = newState.get(); | |
112 m_state->setObject(name, std::move(newState)); | |
113 } | |
114 return state; | |
115 } | |
116 | |
117 void V8InspectorSessionImpl::sendProtocolResponse(int callId, const String16& me
ssage) | |
118 { | |
119 m_channel->sendProtocolResponse(callId, toStringView(message)); | |
120 } | |
121 | |
122 void V8InspectorSessionImpl::sendProtocolNotification(const String16& message) | |
123 { | |
124 m_channel->sendProtocolNotification(toStringView(message)); | |
125 } | |
126 | |
127 void V8InspectorSessionImpl::flushProtocolNotifications() | |
128 { | |
129 m_channel->flushProtocolNotifications(); | |
130 } | |
131 | |
132 void V8InspectorSessionImpl::reset() | |
133 { | |
134 m_debuggerAgent->reset(); | |
135 m_runtimeAgent->reset(); | |
136 discardInjectedScripts(); | |
137 } | |
138 | |
139 void V8InspectorSessionImpl::discardInjectedScripts() | |
140 { | |
141 m_inspectedObjects.clear(); | |
142 const V8InspectorImpl::ContextByIdMap* contexts = m_inspector->contextGroup(
m_contextGroupId); | |
143 if (!contexts) | |
144 return; | |
145 | |
146 std::vector<int> keys; | |
147 keys.reserve(contexts->size()); | |
148 for (auto& idContext : *contexts) | |
149 keys.push_back(idContext.first); | |
150 for (auto& key : keys) { | |
151 contexts = m_inspector->contextGroup(m_contextGroupId); | |
152 if (!contexts) | |
153 continue; | |
154 auto contextIt = contexts->find(key); | |
155 if (contextIt != contexts->end()) | |
156 contextIt->second->discardInjectedScript(); // This may destroy some
contexts. | |
157 } | |
158 } | |
159 | |
160 InjectedScript* V8InspectorSessionImpl::findInjectedScript(ErrorString* errorStr
ing, int contextId) | |
161 { | |
162 if (!contextId) { | |
163 *errorString = "Cannot find context with specified id"; | |
164 return nullptr; | |
165 } | |
166 | |
167 const V8InspectorImpl::ContextByIdMap* contexts = m_inspector->contextGroup(
m_contextGroupId); | |
168 if (!contexts) { | |
169 *errorString = "Cannot find context with specified id"; | |
170 return nullptr; | |
171 } | |
172 | |
173 auto contextsIt = contexts->find(contextId); | |
174 if (contextsIt == contexts->end()) { | |
175 *errorString = "Cannot find context with specified id"; | |
176 return nullptr; | |
177 } | |
178 | |
179 const std::unique_ptr<InspectedContext>& context = contextsIt->second; | |
180 if (!context->getInjectedScript()) { | |
181 context->createInjectedScript(); | |
182 if (!context->getInjectedScript()) { | |
183 *errorString = "Cannot access specified execution context"; | |
184 return nullptr; | |
185 } | |
186 if (m_customObjectFormatterEnabled) | |
187 context->getInjectedScript()->setCustomObjectFormatterEnabled(true); | |
188 } | |
189 return context->getInjectedScript(); | |
190 } | |
191 | |
192 InjectedScript* V8InspectorSessionImpl::findInjectedScript(ErrorString* errorStr
ing, RemoteObjectIdBase* objectId) | |
193 { | |
194 return objectId ? findInjectedScript(errorString, objectId->contextId()) : n
ullptr; | |
195 } | |
196 | |
197 void V8InspectorSessionImpl::releaseObjectGroup(const StringView& objectGroup) | |
198 { | |
199 releaseObjectGroup(toString16(objectGroup)); | |
200 } | |
201 | |
202 void V8InspectorSessionImpl::releaseObjectGroup(const String16& objectGroup) | |
203 { | |
204 const V8InspectorImpl::ContextByIdMap* contexts = m_inspector->contextGroup(
m_contextGroupId); | |
205 if (!contexts) | |
206 return; | |
207 | |
208 std::vector<int> keys; | |
209 for (auto& idContext : *contexts) | |
210 keys.push_back(idContext.first); | |
211 for (auto& key : keys) { | |
212 contexts = m_inspector->contextGroup(m_contextGroupId); | |
213 if (!contexts) | |
214 continue; | |
215 auto contextsIt = contexts->find(key); | |
216 if (contextsIt == contexts->end()) | |
217 continue; | |
218 InjectedScript* injectedScript = contextsIt->second->getInjectedScript()
; | |
219 if (injectedScript) | |
220 injectedScript->releaseObjectGroup(objectGroup); // This may destroy
some contexts. | |
221 } | |
222 } | |
223 | |
224 bool V8InspectorSessionImpl::unwrapObject(std::unique_ptr<StringBuffer>* error,
const StringView& objectId, v8::Local<v8::Value>* object, v8::Local<v8::Context>
* context, std::unique_ptr<StringBuffer>* objectGroup) | |
225 { | |
226 ErrorString errorString; | |
227 String16 objectGroupString; | |
228 bool result = unwrapObject(&errorString, toString16(objectId), object, conte
xt, objectGroup ? &objectGroupString : nullptr); | |
229 if (error) | |
230 *error = StringBufferImpl::adopt(errorString); | |
231 if (objectGroup) | |
232 *objectGroup = StringBufferImpl::adopt(objectGroupString); | |
233 return result; | |
234 } | |
235 | |
236 bool V8InspectorSessionImpl::unwrapObject(ErrorString* errorString, const String
16& objectId, v8::Local<v8::Value>* object, v8::Local<v8::Context>* context, Str
ing16* objectGroup) | |
237 { | |
238 std::unique_ptr<RemoteObjectId> remoteId = RemoteObjectId::parse(errorString
, objectId); | |
239 if (!remoteId) | |
240 return false; | |
241 InjectedScript* injectedScript = findInjectedScript(errorString, remoteId.ge
t()); | |
242 if (!injectedScript) | |
243 return false; | |
244 if (!injectedScript->findObject(errorString, *remoteId, object)) | |
245 return false; | |
246 *context = injectedScript->context()->context(); | |
247 if (objectGroup) | |
248 *objectGroup = injectedScript->objectGroupName(*remoteId); | |
249 return true; | |
250 } | |
251 | |
252 std::unique_ptr<protocol::Runtime::API::RemoteObject> V8InspectorSessionImpl::wr
apObject(v8::Local<v8::Context> context, v8::Local<v8::Value> value, const Strin
gView& groupName) | |
253 { | |
254 return wrapObject(context, value, toString16(groupName), false); | |
255 } | |
256 | |
257 std::unique_ptr<protocol::Runtime::RemoteObject> V8InspectorSessionImpl::wrapObj
ect(v8::Local<v8::Context> context, v8::Local<v8::Value> value, const String16&
groupName, bool generatePreview) | |
258 { | |
259 ErrorString errorString; | |
260 InjectedScript* injectedScript = findInjectedScript(&errorString, V8Debugger
::contextId(context)); | |
261 if (!injectedScript) | |
262 return nullptr; | |
263 return injectedScript->wrapObject(&errorString, value, groupName, false, gen
eratePreview); | |
264 } | |
265 | |
266 std::unique_ptr<protocol::Runtime::RemoteObject> V8InspectorSessionImpl::wrapTab
le(v8::Local<v8::Context> context, v8::Local<v8::Value> table, v8::Local<v8::Val
ue> columns) | |
267 { | |
268 ErrorString errorString; | |
269 InjectedScript* injectedScript = findInjectedScript(&errorString, V8Debugger
::contextId(context)); | |
270 if (!injectedScript) | |
271 return nullptr; | |
272 return injectedScript->wrapTable(table, columns); | |
273 } | |
274 | |
275 void V8InspectorSessionImpl::setCustomObjectFormatterEnabled(bool enabled) | |
276 { | |
277 m_customObjectFormatterEnabled = enabled; | |
278 const V8InspectorImpl::ContextByIdMap* contexts = m_inspector->contextGroup(
m_contextGroupId); | |
279 if (!contexts) | |
280 return; | |
281 for (auto& idContext : *contexts) { | |
282 InjectedScript* injectedScript = idContext.second->getInjectedScript(); | |
283 if (injectedScript) | |
284 injectedScript->setCustomObjectFormatterEnabled(enabled); | |
285 } | |
286 } | |
287 | |
288 void V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) | |
289 { | |
290 const V8InspectorImpl::ContextByIdMap* contexts = m_inspector->contextGroup(
m_contextGroupId); | |
291 if (!contexts) | |
292 return; | |
293 for (auto& idContext : *contexts) | |
294 agent->reportExecutionContextCreated(idContext.second.get()); | |
295 } | |
296 | |
297 void V8InspectorSessionImpl::dispatchProtocolMessage(const StringView& message) | |
298 { | |
299 m_dispatcher.dispatch(protocol::parseJSON(message)); | |
300 } | |
301 | |
302 std::unique_ptr<StringBuffer> V8InspectorSessionImpl::stateJSON() | |
303 { | |
304 String16 json = m_state->toJSONString(); | |
305 return StringBufferImpl::adopt(json); | |
306 } | |
307 | |
308 std::vector<std::unique_ptr<protocol::Schema::API::Domain>> V8InspectorSessionIm
pl::supportedDomains() | |
309 { | |
310 std::vector<std::unique_ptr<protocol::Schema::Domain>> domains = supportedDo
mainsImpl(); | |
311 std::vector<std::unique_ptr<protocol::Schema::API::Domain>> result; | |
312 for (size_t i = 0; i < domains.size(); ++i) | |
313 result.push_back(std::move(domains[i])); | |
314 return result; | |
315 } | |
316 | |
317 std::vector<std::unique_ptr<protocol::Schema::Domain>> V8InspectorSessionImpl::s
upportedDomainsImpl() | |
318 { | |
319 std::vector<std::unique_ptr<protocol::Schema::Domain>> result; | |
320 result.push_back(protocol::Schema::Domain::create().setName(protocol::Runtim
e::Metainfo::domainName).setVersion(protocol::Runtime::Metainfo::version).build(
)); | |
321 result.push_back(protocol::Schema::Domain::create().setName(protocol::Debugg
er::Metainfo::domainName).setVersion(protocol::Debugger::Metainfo::version).buil
d()); | |
322 result.push_back(protocol::Schema::Domain::create().setName(protocol::Profil
er::Metainfo::domainName).setVersion(protocol::Profiler::Metainfo::version).buil
d()); | |
323 result.push_back(protocol::Schema::Domain::create().setName(protocol::HeapPr
ofiler::Metainfo::domainName).setVersion(protocol::HeapProfiler::Metainfo::versi
on).build()); | |
324 result.push_back(protocol::Schema::Domain::create().setName(protocol::Schema
::Metainfo::domainName).setVersion(protocol::Schema::Metainfo::version).build())
; | |
325 return result; | |
326 } | |
327 | |
328 void V8InspectorSessionImpl::addInspectedObject(std::unique_ptr<V8InspectorSessi
on::Inspectable> inspectable) | |
329 { | |
330 m_inspectedObjects.insert(m_inspectedObjects.begin(), std::move(inspectable)
); | |
331 if (m_inspectedObjects.size() > kInspectedObjectBufferSize) | |
332 m_inspectedObjects.resize(kInspectedObjectBufferSize); | |
333 } | |
334 | |
335 V8InspectorSession::Inspectable* V8InspectorSessionImpl::inspectedObject(unsigne
d num) | |
336 { | |
337 if (num >= m_inspectedObjects.size()) | |
338 return nullptr; | |
339 return m_inspectedObjects[num].get(); | |
340 } | |
341 | |
342 void V8InspectorSessionImpl::schedulePauseOnNextStatement(const StringView& brea
kReason, const StringView& breakDetails) | |
343 { | |
344 m_debuggerAgent->schedulePauseOnNextStatement(toString16(breakReason), proto
col::DictionaryValue::cast(protocol::parseJSON(breakDetails))); | |
345 } | |
346 | |
347 void V8InspectorSessionImpl::cancelPauseOnNextStatement() | |
348 { | |
349 m_debuggerAgent->cancelPauseOnNextStatement(); | |
350 } | |
351 | |
352 void V8InspectorSessionImpl::breakProgram(const StringView& breakReason, const S
tringView& breakDetails) | |
353 { | |
354 m_debuggerAgent->breakProgram(toString16(breakReason), protocol::DictionaryV
alue::cast(protocol::parseJSON(breakDetails))); | |
355 } | |
356 | |
357 void V8InspectorSessionImpl::setSkipAllPauses(bool skip) | |
358 { | |
359 ErrorString errorString; | |
360 m_debuggerAgent->setSkipAllPauses(&errorString, skip); | |
361 } | |
362 | |
363 void V8InspectorSessionImpl::resume() | |
364 { | |
365 ErrorString errorString; | |
366 m_debuggerAgent->resume(&errorString); | |
367 } | |
368 | |
369 void V8InspectorSessionImpl::stepOver() | |
370 { | |
371 ErrorString errorString; | |
372 m_debuggerAgent->stepOver(&errorString); | |
373 } | |
374 | |
375 std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>> V8InspectorSe
ssionImpl::searchInTextByLines(const StringView& text, const StringView& query,
bool caseSensitive, bool isRegex) | |
376 { | |
377 // TODO(dgozman): search may operate on StringView and avoid copying |text|. | |
378 std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> matches = sear
chInTextByLinesImpl(this, toString16(text), toString16(query), caseSensitive, is
Regex); | |
379 std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>> result; | |
380 for (size_t i = 0; i < matches.size(); ++i) | |
381 result.push_back(std::move(matches[i])); | |
382 return result; | |
383 } | |
384 | |
385 } // namespace v8_inspector | |
OLD | NEW |