// 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" #include "src/vector.h" namespace { const int kInspectorClientIndex = 0; class ChannelImpl final : public v8_inspector::V8Inspector::Channel { public: explicit ChannelImpl(InspectorClientImpl::FrontendChannel* frontend_channel) : frontend_channel_(frontend_channel) {} virtual ~ChannelImpl() = default; private: void sendResponse( int callId, std::unique_ptr message) override { frontend_channel_->SendMessageToFrontend(message->string()); } void sendNotification( std::unique_ptr message) override { frontend_channel_->SendMessageToFrontend(message->string()); } void flushProtocolNotifications() override {} InspectorClientImpl::FrontendChannel* frontend_channel_; DISALLOW_COPY_AND_ASSIGN(ChannelImpl); }; InspectorClientImpl* InspectorClientFromContext( v8::Local context) { InspectorClientImpl* inspector_client = static_cast( context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex)); CHECK(inspector_client); return inspector_client; } v8::internal::Vector ToVector(v8::Local str) { v8::internal::Vector buffer = v8::internal::Vector::New(str->Length()); str->Write(buffer.start(), 0, str->Length()); return buffer; } void MessageHandler(v8::Local message, v8::Local exception) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Local context = isolate->GetEnteredContext(); if (context.IsEmpty()) return; v8_inspector::V8Inspector* inspector = InspectorClientImpl::InspectorFromContext(context); v8::Local stack = message->GetStackTrace(); int script_id = static_cast(message->GetScriptOrigin().ScriptID()->Value()); 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; v8::internal::Vector message_text_string = ToVector(message->Get()); v8_inspector::StringView message_text(message_text_string.start(), message_text_string.length()); v8::internal::Vector url_string; if (message->GetScriptOrigin().ResourceName()->IsString()) { url_string = ToVector(message->GetScriptOrigin().ResourceName().As()); } v8_inspector::StringView url(url_string.start(), url_string.length()); inspector->exceptionThrown(context, message_text, exception, detailed_message, url, line_number, column_number, inspector->createStackTrace(stack), script_id); } v8::Local ToString(v8::Isolate* isolate, const v8_inspector::StringView& string) { if (string.is8Bit()) return v8::String::NewFromOneByte(isolate, string.characters8(), v8::NewStringType::kNormal, static_cast(string.length())) .ToLocalChecked(); else return v8::String::NewFromTwoByte(isolate, string.characters16(), v8::NewStringType::kNormal, static_cast(string.length())) .ToLocalChecked(); } void Print(v8::Isolate* isolate, const v8_inspector::StringView& string) { v8::Local v8_string = ToString(isolate, string); v8::String::Utf8Value utf8_string(v8_string); fwrite(*utf8_string, sizeof(**utf8_string), utf8_string.length(), stdout); } } // 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; } private: void Run() override { v8::HandleScope handle_scope(isolate()); client_->connect(); if (ready_semaphore_) ready_semaphore_->Signal(); } InspectorClientImpl* client_; v8::base::Semaphore* ready_semaphore_; }; class DisconnectTask : public TaskRunner::Task { public: explicit DisconnectTask(InspectorClientImpl* client, bool reset_inspector, v8::base::Semaphore* ready_semaphore) : client_(client), reset_inspector_(reset_inspector), ready_semaphore_(ready_semaphore) {} virtual ~DisconnectTask() = default; bool is_inspector_task() final { return true; } private: void Run() override { client_->disconnect(reset_inspector_); if (ready_semaphore_) ready_semaphore_->Signal(); } InspectorClientImpl* client_; bool reset_inspector_; v8::base::Semaphore* ready_semaphore_; }; class CreateContextGroupTask : public TaskRunner::Task { public: CreateContextGroupTask(InspectorClientImpl* client, IsolateData::SetupGlobalTasks setup_global_tasks, v8::base::Semaphore* ready_semaphore, int* context_group_id) : client_(client), setup_global_tasks_(std::move(setup_global_tasks)), ready_semaphore_(ready_semaphore), context_group_id_(context_group_id) {} virtual ~CreateContextGroupTask() = default; bool is_inspector_task() final { return true; } private: void Run() override { *context_group_id_ = client_->createContextGroup(setup_global_tasks_); if (ready_semaphore_) ready_semaphore_->Signal(); } InspectorClientImpl* client_; IsolateData::SetupGlobalTasks setup_global_tasks_; v8::base::Semaphore* ready_semaphore_; int* context_group_id_; }; 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() { isolate_ = task_runner_->data()->isolate(); isolate_->AddMessageListener(MessageHandler); channel_.reset(new ChannelImpl(frontend_channel_)); inspector_ = v8_inspector::V8Inspector::create(isolate_, this); if (states_.empty()) { ConnectToContextGroup(task_runner_->default_context_group_id(), v8_inspector::StringView()); } else { for (const auto& it : states_) ConnectToContextGroup(it.first, it.second->string()); } states_.clear(); } void InspectorClientImpl::ConnectToContextGroup( int context_group_id, v8_inspector::StringView state) { v8::Local context = task_runner_->data()->GetContext(context_group_id); sessions_[context_group_id] = inspector_->connect(context_group_id, channel_.get(), state); context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this); v8_inspector::V8ContextInfo info(context, context_group_id, v8_inspector::StringView()); info.hasMemoryOnConsole = true; inspector_->contextCreated(info); } void InspectorClientImpl::scheduleReconnect( v8::base::Semaphore* ready_semaphore) { task_runner_->Append( new DisconnectTask(this, /* reset_inspector */ true, nullptr)); task_runner_->Append(new ConnectTask(this, ready_semaphore)); } void InspectorClientImpl::scheduleDisconnect( v8::base::Semaphore* ready_semaphore) { task_runner_->Append( new DisconnectTask(this, /* reset_inspector */ false, ready_semaphore)); } void InspectorClientImpl::disconnect(bool reset_inspector) { for (const auto& it : sessions_) { states_[it.first] = it.second->stateJSON(); } sessions_.clear(); if (reset_inspector) inspector_.reset(); } void InspectorClientImpl::scheduleCreateContextGroup( IsolateData::SetupGlobalTasks setup_global_tasks, v8::base::Semaphore* ready_semaphore, int* context_group_id) { task_runner_->Append(new CreateContextGroupTask( this, std::move(setup_global_tasks), ready_semaphore, context_group_id)); } int InspectorClientImpl::createContextGroup( const IsolateData::SetupGlobalTasks& setup_global_tasks) { v8::HandleScope handle_scope(isolate_); int context_group_id = task_runner_->data()->CreateContextGroup(); v8::Local context = task_runner_->data()->GetContext(context_group_id); context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this); v8_inspector::StringView state; sessions_[context_group_id] = inspector_->connect(context_group_id, channel_.get(), state); inspector_->contextCreated(v8_inspector::V8ContextInfo( context, context_group_id, v8_inspector::StringView())); return context_group_id; } bool InspectorClientImpl::formatAccessorsAsProperties( v8::Local object) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Local context = isolate->GetCurrentContext(); v8::Local shouldFormatAccessorsPrivate = v8::Private::ForApi( isolate, v8::String::NewFromUtf8(isolate, "allowAccessorFormatting", v8::NewStringType::kNormal) .ToLocalChecked()); CHECK(object->IsObject()); return object.As() ->HasPrivate(context, shouldFormatAccessorsPrivate) .FromMaybe(false); } v8::Local InspectorClientImpl::ensureDefaultContextInGroup( int context_group_id) { CHECK(isolate_); return task_runner_->data()->GetContext(context_group_id); } void InspectorClientImpl::setCurrentTimeMSForTest(double time) { current_time_ = time; current_time_set_for_test_ = true; } double InspectorClientImpl::currentTimeMS() { if (current_time_set_for_test_) return current_time_; return v8::base::OS::TimeCurrentMillis(); } void InspectorClientImpl::setMemoryInfoForTest( v8::Local memory_info) { memory_info_.Reset(isolate_, memory_info); } void InspectorClientImpl::setLogConsoleApiMessageCalls(bool log) { log_console_api_message_calls_ = log; } v8::MaybeLocal InspectorClientImpl::memoryInfo( v8::Isolate* isolate, v8::Local) { if (memory_info_.IsEmpty()) return v8::MaybeLocal(); return memory_info_.Get(isolate); } void InspectorClientImpl::runMessageLoopOnPause(int) { task_runner_->RunMessageLoop(true); } void InspectorClientImpl::quitMessageLoopOnPause() { task_runner_->QuitMessageLoop(); } void InspectorClientImpl::consoleAPIMessage( int contextGroupId, v8::Isolate::MessageErrorLevel level, const v8_inspector::StringView& message, const v8_inspector::StringView& url, unsigned lineNumber, unsigned columnNumber, v8_inspector::V8StackTrace* stack) { if (!log_console_api_message_calls_) return; Print(isolate_, message); fprintf(stdout, " ("); Print(isolate_, url); fprintf(stdout, ":%d:%d)", lineNumber, columnNumber); Print(isolate_, stack->toString()->string()); fprintf(stdout, "\n"); } v8_inspector::V8Inspector* InspectorClientImpl::InspectorFromContext( v8::Local context) { return InspectorClientFromContext(context)->inspector_.get(); } v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext( v8::Local context) { InspectorClientImpl* client = InspectorClientFromContext(context); for (auto& it : client->sessions_) { if (client->task_runner_->data()->GetContext(it.first) == context) return it.second.get(); } return nullptr; } v8_inspector::V8InspectorSession* InspectorClientImpl::session( int context_group_id) { if (context_group_id) { return sessions_[context_group_id].get(); } else { return sessions_.begin()->second.get(); } } class SendMessageToBackendTask : public TaskRunner::Task { public: explicit SendMessageToBackendTask( const v8::internal::Vector& message, int context_group_id) : message_(message), context_group_id_(context_group_id) {} bool is_inspector_task() final { return true; } private: void Run() override { v8_inspector::V8InspectorSession* session = nullptr; { v8::HandleScope handle_scope(isolate()); if (!context_group_id_) { session = InspectorClientImpl::SessionFromContext(default_context()); } else { session = InspectorClientFromContext(default_context()) ->sessions_[context_group_id_] .get(); } if (!session) return; } v8_inspector::StringView message_view(message_.start(), message_.length()); session->dispatchProtocolMessage(message_view); } v8::internal::Vector message_; int context_group_id_; }; TaskRunner* SendMessageToBackendExtension::backend_task_runner_ = nullptr; void SendMessageToBackendExtension::Run(v8::Isolate* isolate, v8::Local global) { global->Set( v8::String::NewFromUtf8(isolate, "sendMessageToBackend", v8::NewStringType::kNormal) .ToLocalChecked(), v8::FunctionTemplate::New( isolate, &SendMessageToBackendExtension::SendMessageToBackend)); } void SendMessageToBackendExtension::SendMessageToBackend( const v8::FunctionCallbackInfo& args) { CHECK(backend_task_runner_); CHECK(args.Length() == 2 && args[0]->IsString() && args[1]->IsInt32()); v8::Local message = args[0].As(); backend_task_runner_->Append(new SendMessageToBackendTask( ToVector(message), args[1].As()->Value())); }