2016-10-02 16:45:33 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "test/inspector/inspector-impl.h"
|
|
|
|
|
|
|
|
#include "include/v8.h"
|
2016-10-07 21:16:41 +00:00
|
|
|
|
|
|
|
#include "src/vector.h"
|
2016-10-02 16:45:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kInspectorClientIndex = v8::Context::kDebugIdIndex + 1;
|
|
|
|
|
|
|
|
class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
|
|
|
|
public:
|
|
|
|
explicit ChannelImpl(InspectorClientImpl::FrontendChannel* frontend_channel)
|
|
|
|
: frontend_channel_(frontend_channel) {}
|
|
|
|
virtual ~ChannelImpl() = default;
|
|
|
|
|
|
|
|
private:
|
2016-11-29 19:31:05 +00:00
|
|
|
void sendResponse(
|
|
|
|
int callId,
|
|
|
|
std::unique_ptr<v8_inspector::StringBuffer> message) override {
|
|
|
|
frontend_channel_->SendMessageToFrontend(message->string());
|
2016-10-02 16:45:33 +00:00
|
|
|
}
|
2016-11-29 19:31:05 +00:00
|
|
|
void sendNotification(
|
|
|
|
std::unique_ptr<v8_inspector::StringBuffer> message) override {
|
|
|
|
frontend_channel_->SendMessageToFrontend(message->string());
|
2016-10-02 16:45:33 +00:00
|
|
|
}
|
|
|
|
void flushProtocolNotifications() override {}
|
|
|
|
|
|
|
|
InspectorClientImpl::FrontendChannel* frontend_channel_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ChannelImpl);
|
|
|
|
};
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
InspectorClientImpl* InspectorClientFromContext(
|
|
|
|
v8::Local<v8::Context> context) {
|
|
|
|
InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>(
|
|
|
|
context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex));
|
|
|
|
CHECK(inspector_client);
|
|
|
|
return inspector_client;
|
|
|
|
}
|
|
|
|
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) {
|
|
|
|
v8::internal::Vector<uint16_t> buffer =
|
|
|
|
v8::internal::Vector<uint16_t>::New(str->Length());
|
|
|
|
str->Write(buffer.start(), 0, str->Length());
|
|
|
|
return buffer;
|
2016-10-05 15:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler(v8::Local<v8::Message> message,
|
|
|
|
v8::Local<v8::Value> exception) {
|
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::Local<v8::Context> context = isolate->GetEnteredContext();
|
|
|
|
if (context.IsEmpty()) return;
|
|
|
|
v8_inspector::V8Inspector* inspector =
|
|
|
|
InspectorClientImpl::InspectorFromContext(context);
|
|
|
|
|
|
|
|
v8::Local<v8::StackTrace> stack = message->GetStackTrace();
|
2016-10-05 22:56:41 +00:00
|
|
|
int script_id =
|
|
|
|
static_cast<int>(message->GetScriptOrigin().ScriptID()->Value());
|
2016-10-05 15:07:57 +00:00
|
|
|
if (!stack.IsEmpty() && stack->GetFrameCount() > 0) {
|
|
|
|
int top_script_id = stack->GetFrame(0)->GetScriptId();
|
|
|
|
if (top_script_id == script_id) script_id = 0;
|
|
|
|
}
|
|
|
|
int line_number = message->GetLineNumber(context).FromMaybe(0);
|
|
|
|
int column_number = 0;
|
|
|
|
if (message->GetStartColumn(context).IsJust())
|
|
|
|
column_number = message->GetStartColumn(context).FromJust() + 1;
|
|
|
|
|
|
|
|
v8_inspector::StringView detailed_message;
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::internal::Vector<uint16_t> message_text_string = ToVector(message->Get());
|
|
|
|
v8_inspector::StringView message_text(message_text_string.start(),
|
2016-10-05 15:07:57 +00:00
|
|
|
message_text_string.length());
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::internal::Vector<uint16_t> url_string;
|
2016-10-05 15:07:57 +00:00
|
|
|
if (message->GetScriptOrigin().ResourceName()->IsString()) {
|
|
|
|
url_string =
|
2016-10-07 21:16:41 +00:00
|
|
|
ToVector(message->GetScriptOrigin().ResourceName().As<v8::String>());
|
2016-10-05 15:07:57 +00:00
|
|
|
}
|
2016-10-07 21:16:41 +00:00
|
|
|
v8_inspector::StringView url(url_string.start(), url_string.length());
|
2016-10-05 15:07:57 +00:00
|
|
|
|
|
|
|
inspector->exceptionThrown(context, message_text, exception, detailed_message,
|
|
|
|
url, line_number, column_number,
|
|
|
|
inspector->createStackTrace(stack), script_id);
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:45:33 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class ConnectTask : public TaskRunner::Task {
|
|
|
|
public:
|
|
|
|
ConnectTask(InspectorClientImpl* client, v8::base::Semaphore* ready_semaphore)
|
|
|
|
: client_(client), ready_semaphore_(ready_semaphore) {}
|
|
|
|
virtual ~ConnectTask() = default;
|
|
|
|
|
|
|
|
bool is_inspector_task() final { return true; }
|
|
|
|
|
|
|
|
void Run(v8::Isolate* isolate,
|
|
|
|
const v8::Global<v8::Context>& global_context) {
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = global_context.Get(isolate);
|
|
|
|
client_->connect(context);
|
|
|
|
if (ready_semaphore_) ready_semaphore_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
InspectorClientImpl* client_;
|
|
|
|
v8::base::Semaphore* ready_semaphore_;
|
|
|
|
};
|
|
|
|
|
|
|
|
InspectorClientImpl::InspectorClientImpl(TaskRunner* task_runner,
|
|
|
|
FrontendChannel* frontend_channel,
|
|
|
|
v8::base::Semaphore* ready_semaphore)
|
|
|
|
: isolate_(nullptr),
|
|
|
|
task_runner_(task_runner),
|
|
|
|
frontend_channel_(frontend_channel) {
|
|
|
|
task_runner_->Append(new ConnectTask(this, ready_semaphore));
|
|
|
|
}
|
|
|
|
|
|
|
|
InspectorClientImpl::~InspectorClientImpl() {}
|
|
|
|
|
|
|
|
void InspectorClientImpl::connect(v8::Local<v8::Context> context) {
|
|
|
|
isolate_ = context->GetIsolate();
|
2016-10-05 15:07:57 +00:00
|
|
|
isolate_->AddMessageListener(MessageHandler);
|
2016-10-02 16:45:33 +00:00
|
|
|
channel_.reset(new ChannelImpl(frontend_channel_));
|
|
|
|
|
|
|
|
inspector_ = v8_inspector::V8Inspector::create(isolate_, this);
|
|
|
|
session_ = inspector_->connect(1, channel_.get(), v8_inspector::StringView());
|
|
|
|
|
|
|
|
context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this);
|
|
|
|
inspector_->contextCreated(
|
|
|
|
v8_inspector::V8ContextInfo(context, 1, v8_inspector::StringView()));
|
|
|
|
context_.Reset(isolate_, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Context> InspectorClientImpl::ensureDefaultContextInGroup(int) {
|
|
|
|
CHECK(isolate_);
|
|
|
|
return context_.Get(isolate_);
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:21:38 +00:00
|
|
|
void InspectorClientImpl::setCurrentTimeMSForTest(double time) {
|
|
|
|
current_time_ = time;
|
|
|
|
current_time_set_for_test_ = true;
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:45:33 +00:00
|
|
|
double InspectorClientImpl::currentTimeMS() {
|
2017-01-17 20:21:38 +00:00
|
|
|
if (current_time_set_for_test_) return current_time_;
|
2016-10-02 16:45:33 +00:00
|
|
|
return v8::base::OS::TimeCurrentMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectorClientImpl::runMessageLoopOnPause(int) {
|
|
|
|
task_runner_->RunMessageLoop(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectorClientImpl::quitMessageLoopOnPause() {
|
|
|
|
task_runner_->QuitMessageLoop();
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
v8_inspector::V8Inspector* InspectorClientImpl::InspectorFromContext(
|
|
|
|
v8::Local<v8::Context> context) {
|
|
|
|
return InspectorClientFromContext(context)->inspector_.get();
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:45:33 +00:00
|
|
|
v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext(
|
|
|
|
v8::Local<v8::Context> context) {
|
2016-10-02 18:10:27 +00:00
|
|
|
return InspectorClientFromContext(context)->session_.get();
|
2016-10-02 16:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SendMessageToBackendTask : public TaskRunner::Task {
|
|
|
|
public:
|
2016-10-07 21:16:41 +00:00
|
|
|
explicit SendMessageToBackendTask(
|
|
|
|
const v8::internal::Vector<uint16_t>& message)
|
2016-10-02 16:45:33 +00:00
|
|
|
: message_(message) {}
|
|
|
|
|
|
|
|
bool is_inspector_task() final { return true; }
|
|
|
|
|
|
|
|
void Run(v8::Isolate* isolate,
|
|
|
|
const v8::Global<v8::Context>& global_context) override {
|
|
|
|
v8_inspector::V8InspectorSession* session = nullptr;
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = global_context.Get(isolate);
|
|
|
|
session = InspectorClientImpl::SessionFromContext(context);
|
|
|
|
CHECK(session);
|
|
|
|
}
|
2016-10-07 21:16:41 +00:00
|
|
|
v8_inspector::StringView message_view(message_.start(), message_.length());
|
2016-10-02 16:45:33 +00:00
|
|
|
session->dispatchProtocolMessage(message_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::internal::Vector<uint16_t> message_;
|
2016-10-02 16:45:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TaskRunner* SendMessageToBackendExtension::backend_task_runner_ = nullptr;
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate>
|
|
|
|
SendMessageToBackendExtension::GetNativeFunctionTemplate(
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::String> name) {
|
|
|
|
return v8::FunctionTemplate::New(
|
|
|
|
isolate, SendMessageToBackendExtension::SendMessageToBackend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendMessageToBackendExtension::SendMessageToBackend(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
CHECK(backend_task_runner_);
|
|
|
|
CHECK(args.Length() == 1 && args[0]->IsString());
|
|
|
|
v8::Local<v8::String> message = args[0].As<v8::String>();
|
2016-10-07 21:16:41 +00:00
|
|
|
backend_task_runner_->Append(new SendMessageToBackendTask(ToVector(message)));
|
2016-10-02 16:45:33 +00:00
|
|
|
}
|