OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/inspector/v8-console.h" | 5 #include "src/inspector/v8-console.h" |
6 | 6 |
7 #include "src/base/macros.h" | 7 #include "src/base/macros.h" |
8 #include "src/inspector/injected-script.h" | 8 #include "src/inspector/injected-script.h" |
9 #include "src/inspector/inspected-context.h" | 9 #include "src/inspector/inspected-context.h" |
10 #include "src/inspector/string-util.h" | 10 #include "src/inspector/string-util.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 v8::MaybeLocal<v8::Function> firstArgAsFunction() { | 118 v8::MaybeLocal<v8::Function> firstArgAsFunction() { |
119 if (m_info.Length() < 1 || !m_info[0]->IsFunction()) | 119 if (m_info.Length() < 1 || !m_info[0]->IsFunction()) |
120 return v8::MaybeLocal<v8::Function>(); | 120 return v8::MaybeLocal<v8::Function>(); |
121 v8::Local<v8::Function> func = m_info[0].As<v8::Function>(); | 121 v8::Local<v8::Function> func = m_info[0].As<v8::Function>(); |
122 while (func->GetBoundFunction()->IsFunction()) | 122 while (func->GetBoundFunction()->IsFunction()) |
123 func = func->GetBoundFunction().As<v8::Function>(); | 123 func = func->GetBoundFunction().As<v8::Function>(); |
124 return func; | 124 return func; |
125 } | 125 } |
126 | 126 |
127 V8ProfilerAgentImpl* profilerAgent() { | 127 void forEachSession(std::function<void(V8InspectorSessionImpl*)> callback) { |
128 if (V8InspectorSessionImpl* session = currentSession()) { | 128 m_inspector->forEachSession(m_groupId, callback); |
129 if (session && session->profilerAgent()->enabled()) | |
130 return session->profilerAgent(); | |
131 } | |
132 return nullptr; | |
133 } | |
134 | |
135 V8DebuggerAgentImpl* debuggerAgent() { | |
136 if (V8InspectorSessionImpl* session = currentSession()) { | |
137 if (session && session->debuggerAgent()->enabled()) | |
138 return session->debuggerAgent(); | |
139 } | |
140 return nullptr; | |
141 } | |
142 | |
143 V8InspectorSessionImpl* currentSession() { | |
144 return m_inspector->sessionForContextGroup(m_groupId); | |
145 } | 129 } |
146 | 130 |
147 private: | 131 private: |
148 const v8::debug::ConsoleCallArguments& m_info; | 132 const v8::debug::ConsoleCallArguments& m_info; |
149 v8::Isolate* m_isolate; | 133 v8::Isolate* m_isolate; |
150 v8::Local<v8::Context> m_context; | 134 v8::Local<v8::Context> m_context; |
151 V8InspectorImpl* m_inspector = nullptr; | 135 V8InspectorImpl* m_inspector = nullptr; |
152 int m_contextId; | 136 int m_contextId; |
153 int m_groupId; | 137 int m_groupId; |
154 | 138 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 ConsoleHelper helper(info, m_inspector); | 265 ConsoleHelper helper(info, m_inspector); |
282 DCHECK(!helper.firstArgToBoolean(false)); | 266 DCHECK(!helper.firstArgToBoolean(false)); |
283 | 267 |
284 std::vector<v8::Local<v8::Value>> arguments; | 268 std::vector<v8::Local<v8::Value>> arguments; |
285 for (int i = 1; i < info.Length(); ++i) arguments.push_back(info[i]); | 269 for (int i = 1; i < info.Length(); ++i) arguments.push_back(info[i]); |
286 if (info.Length() < 2) | 270 if (info.Length() < 2) |
287 arguments.push_back( | 271 arguments.push_back( |
288 toV8String(m_inspector->isolate(), String16("console.assert"))); | 272 toV8String(m_inspector->isolate(), String16("console.assert"))); |
289 helper.reportCall(ConsoleAPIType::kAssert, arguments); | 273 helper.reportCall(ConsoleAPIType::kAssert, arguments); |
290 | 274 |
291 if (V8DebuggerAgentImpl* debuggerAgent = helper.debuggerAgent()) | 275 helper.forEachSession([](V8InspectorSessionImpl* session) { |
292 debuggerAgent->breakProgramOnException( | 276 if (session->debuggerAgent()->enabled()) { |
293 protocol::Debugger::Paused::ReasonEnum::Assert, nullptr); | 277 session->debuggerAgent()->breakProgramOnException( |
| 278 protocol::Debugger::Paused::ReasonEnum::Assert, nullptr); |
| 279 } |
| 280 }); |
294 } | 281 } |
295 | 282 |
296 void V8Console::MarkTimeline(const v8::debug::ConsoleCallArguments& info) { | 283 void V8Console::MarkTimeline(const v8::debug::ConsoleCallArguments& info) { |
297 ConsoleHelper(info, m_inspector) | 284 ConsoleHelper(info, m_inspector) |
298 .reportDeprecatedCall("V8Console#markTimelineDeprecated", | 285 .reportDeprecatedCall("V8Console#markTimelineDeprecated", |
299 "'console.markTimeline' is " | 286 "'console.markTimeline' is " |
300 "deprecated. Please use " | 287 "deprecated. Please use " |
301 "'console.timeStamp' instead."); | 288 "'console.timeStamp' instead."); |
302 TimeStamp(info); | 289 TimeStamp(info); |
303 } | 290 } |
304 | 291 |
305 void V8Console::Profile(const v8::debug::ConsoleCallArguments& info) { | 292 void V8Console::Profile(const v8::debug::ConsoleCallArguments& info) { |
306 ConsoleHelper helper(info, m_inspector); | 293 ConsoleHelper helper(info, m_inspector); |
307 if (V8ProfilerAgentImpl* profilerAgent = helper.profilerAgent()) | 294 helper.forEachSession([&helper](V8InspectorSessionImpl* session) { |
308 profilerAgent->consoleProfile(helper.firstArgToString(String16())); | 295 session->profilerAgent()->consoleProfile( |
| 296 helper.firstArgToString(String16())); |
| 297 }); |
309 } | 298 } |
310 | 299 |
311 void V8Console::ProfileEnd(const v8::debug::ConsoleCallArguments& info) { | 300 void V8Console::ProfileEnd(const v8::debug::ConsoleCallArguments& info) { |
312 ConsoleHelper helper(info, m_inspector); | 301 ConsoleHelper helper(info, m_inspector); |
313 if (V8ProfilerAgentImpl* profilerAgent = helper.profilerAgent()) | 302 helper.forEachSession([&helper](V8InspectorSessionImpl* session) { |
314 profilerAgent->consoleProfileEnd(helper.firstArgToString(String16())); | 303 session->profilerAgent()->consoleProfileEnd( |
| 304 helper.firstArgToString(String16())); |
| 305 }); |
315 } | 306 } |
316 | 307 |
317 static void timeFunction(const v8::debug::ConsoleCallArguments& info, | 308 static void timeFunction(const v8::debug::ConsoleCallArguments& info, |
318 bool timelinePrefix, V8InspectorImpl* inspector) { | 309 bool timelinePrefix, V8InspectorImpl* inspector) { |
319 ConsoleHelper helper(info, inspector); | 310 ConsoleHelper helper(info, inspector); |
320 String16 protocolTitle = helper.firstArgToString("default"); | 311 String16 protocolTitle = helper.firstArgToString("default"); |
321 if (timelinePrefix) protocolTitle = "Timeline '" + protocolTitle + "'"; | 312 if (timelinePrefix) protocolTitle = "Timeline '" + protocolTitle + "'"; |
322 inspector->client()->consoleTime(toStringView(protocolTitle)); | 313 inspector->client()->consoleTime(toStringView(protocolTitle)); |
323 helper.consoleMessageStorage()->time(helper.contextId(), protocolTitle); | 314 helper.consoleMessageStorage()->time(helper.contextId(), protocolTitle); |
324 } | 315 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 if (!obj->Get(context, key).ToLocal(&value)) continue; | 410 if (!obj->Get(context, key).ToLocal(&value)) continue; |
420 createDataProperty(context, values, i, value); | 411 createDataProperty(context, values, i, value); |
421 } | 412 } |
422 info.GetReturnValue().Set(values); | 413 info.GetReturnValue().Set(values); |
423 } | 414 } |
424 | 415 |
425 static void setFunctionBreakpoint(ConsoleHelper& helper, | 416 static void setFunctionBreakpoint(ConsoleHelper& helper, |
426 v8::Local<v8::Function> function, | 417 v8::Local<v8::Function> function, |
427 V8DebuggerAgentImpl::BreakpointSource source, | 418 V8DebuggerAgentImpl::BreakpointSource source, |
428 const String16& condition, bool enable) { | 419 const String16& condition, bool enable) { |
429 V8DebuggerAgentImpl* debuggerAgent = helper.debuggerAgent(); | |
430 if (!debuggerAgent) return; | |
431 String16 scriptId = String16::fromInteger(function->ScriptId()); | 420 String16 scriptId = String16::fromInteger(function->ScriptId()); |
432 int lineNumber = function->GetScriptLineNumber(); | 421 int lineNumber = function->GetScriptLineNumber(); |
433 int columnNumber = function->GetScriptColumnNumber(); | 422 int columnNumber = function->GetScriptColumnNumber(); |
434 if (lineNumber == v8::Function::kLineOffsetNotFound || | 423 if (lineNumber == v8::Function::kLineOffsetNotFound || |
435 columnNumber == v8::Function::kLineOffsetNotFound) | 424 columnNumber == v8::Function::kLineOffsetNotFound) |
436 return; | 425 return; |
437 if (enable) | 426 |
438 debuggerAgent->setBreakpointAt(scriptId, lineNumber, columnNumber, source, | 427 helper.forEachSession([&enable, &scriptId, &lineNumber, &columnNumber, |
439 condition); | 428 &source, &condition](V8InspectorSessionImpl* session) { |
440 else | 429 if (!session->debuggerAgent()->enabled()) return; |
441 debuggerAgent->removeBreakpointAt(scriptId, lineNumber, columnNumber, | 430 if (enable) { |
442 source); | 431 session->debuggerAgent()->setBreakpointAt( |
| 432 scriptId, lineNumber, columnNumber, source, condition); |
| 433 } else { |
| 434 session->debuggerAgent()->removeBreakpointAt(scriptId, lineNumber, |
| 435 columnNumber, source); |
| 436 } |
| 437 }); |
443 } | 438 } |
444 | 439 |
445 void V8Console::debugFunctionCallback( | 440 void V8Console::debugFunctionCallback( |
446 const v8::FunctionCallbackInfo<v8::Value>& info) { | 441 const v8::FunctionCallbackInfo<v8::Value>& info) { |
447 v8::debug::ConsoleCallArguments args(info); | 442 v8::debug::ConsoleCallArguments args(info); |
448 ConsoleHelper helper(args, m_inspector); | 443 ConsoleHelper helper(args, m_inspector); |
449 v8::Local<v8::Function> function; | 444 v8::Local<v8::Function> function; |
450 if (!helper.firstArgAsFunction().ToLocal(&function)) return; | 445 if (!helper.firstArgAsFunction().ToLocal(&function)) return; |
451 setFunctionBreakpoint(helper, function, | 446 setFunctionBreakpoint(helper, function, |
452 V8DebuggerAgentImpl::DebugCommandBreakpointSource, | 447 V8DebuggerAgentImpl::DebugCommandBreakpointSource, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 if (!injectedScript) return; | 514 if (!injectedScript) return; |
520 std::unique_ptr<protocol::Runtime::RemoteObject> wrappedObject; | 515 std::unique_ptr<protocol::Runtime::RemoteObject> wrappedObject; |
521 protocol::Response response = | 516 protocol::Response response = |
522 injectedScript->wrapObject(info[0], "", false /** forceValueType */, | 517 injectedScript->wrapObject(info[0], "", false /** forceValueType */, |
523 false /** generatePreview */, &wrappedObject); | 518 false /** generatePreview */, &wrappedObject); |
524 if (!response.isSuccess()) return; | 519 if (!response.isSuccess()) return; |
525 | 520 |
526 std::unique_ptr<protocol::DictionaryValue> hints = | 521 std::unique_ptr<protocol::DictionaryValue> hints = |
527 protocol::DictionaryValue::create(); | 522 protocol::DictionaryValue::create(); |
528 if (copyToClipboard) hints->setBoolean("copyToClipboard", true); | 523 if (copyToClipboard) hints->setBoolean("copyToClipboard", true); |
529 if (V8InspectorSessionImpl* session = helper.currentSession()) { | 524 helper.forEachSession( |
530 session->runtimeAgent()->inspect(std::move(wrappedObject), | 525 [&wrappedObject, &hints](V8InspectorSessionImpl* session) { |
531 std::move(hints)); | 526 session->runtimeAgent()->inspect(std::move(wrappedObject), |
532 } | 527 std::move(hints)); |
| 528 }); |
533 } | 529 } |
534 | 530 |
535 void V8Console::inspectCallback( | 531 void V8Console::inspectCallback( |
536 const v8::FunctionCallbackInfo<v8::Value>& info) { | 532 const v8::FunctionCallbackInfo<v8::Value>& info) { |
537 inspectImpl(info, false, m_inspector); | 533 inspectImpl(info, false, m_inspector); |
538 } | 534 } |
539 | 535 |
540 void V8Console::copyCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | 536 void V8Console::copyCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
541 inspectImpl(info, true, m_inspector); | 537 inspectImpl(info, true, m_inspector); |
542 } | 538 } |
543 | 539 |
544 void V8Console::inspectedObject(const v8::FunctionCallbackInfo<v8::Value>& info, | 540 void V8Console::inspectedObject(const v8::FunctionCallbackInfo<v8::Value>& info, |
545 unsigned num) { | 541 unsigned num) { |
546 DCHECK(num < V8InspectorSessionImpl::kInspectedObjectBufferSize); | 542 DCHECK(num < V8InspectorSessionImpl::kInspectedObjectBufferSize); |
547 v8::debug::ConsoleCallArguments args(info); | 543 v8::debug::ConsoleCallArguments args(info); |
548 ConsoleHelper helper(args, m_inspector); | 544 ConsoleHelper helper(args, m_inspector); |
549 if (V8InspectorSessionImpl* session = helper.currentSession()) { | 545 helper.forEachSession([&info, &num](V8InspectorSessionImpl* session) { |
550 V8InspectorSession::Inspectable* object = session->inspectedObject(num); | 546 V8InspectorSession::Inspectable* object = session->inspectedObject(num); |
551 v8::Isolate* isolate = info.GetIsolate(); | 547 v8::Isolate* isolate = info.GetIsolate(); |
552 if (object) | 548 if (object) |
553 info.GetReturnValue().Set(object->get(isolate->GetCurrentContext())); | 549 info.GetReturnValue().Set(object->get(isolate->GetCurrentContext())); |
554 else | 550 else |
555 info.GetReturnValue().Set(v8::Undefined(isolate)); | 551 info.GetReturnValue().Set(v8::Undefined(isolate)); |
556 } | 552 }); |
557 } | 553 } |
558 | 554 |
559 void V8Console::installMemoryGetter(v8::Local<v8::Context> context, | 555 void V8Console::installMemoryGetter(v8::Local<v8::Context> context, |
560 v8::Local<v8::Object> console) { | 556 v8::Local<v8::Object> console) { |
561 v8::Isolate* isolate = context->GetIsolate(); | 557 v8::Isolate* isolate = context->GetIsolate(); |
562 v8::Local<v8::External> data = v8::External::New(isolate, this); | 558 v8::Local<v8::External> data = v8::External::New(isolate, this); |
563 console->SetAccessorProperty( | 559 console->SetAccessorProperty( |
564 toV8StringInternalized(isolate, "memory"), | 560 toV8StringInternalized(isolate, "memory"), |
565 v8::Function::New(context, | 561 v8::Function::New(context, |
566 &V8Console::call<&V8Console::memoryGetterCallback>, | 562 &V8Console::call<&V8Console::memoryGetterCallback>, |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 ->GetOwnPropertyDescriptor( | 747 ->GetOwnPropertyDescriptor( |
752 m_context, v8::Local<v8::String>::Cast(name)) | 748 m_context, v8::Local<v8::String>::Cast(name)) |
753 .ToLocal(&descriptor); | 749 .ToLocal(&descriptor); |
754 DCHECK(success); | 750 DCHECK(success); |
755 USE(success); | 751 USE(success); |
756 } | 752 } |
757 } | 753 } |
758 } | 754 } |
759 | 755 |
760 } // namespace v8_inspector | 756 } // namespace v8_inspector |
OLD | NEW |