From 66382abe29d8408db4ff5466c1dd54bbcbaa49d5 Mon Sep 17 00:00:00 2001 From: Eugene Ostroukhov Date: Wed, 3 Jul 2019 11:32:31 -0700 Subject: [PATCH] inspector: reduce InspectorIo API surface This is a cleanup, allowing for a better separation of concerns. PR-URL: https://github.com/nodejs/node/pull/28526 Reviewed-By: Anna Henningsen Reviewed-By: Aleksei Koziatinskii Reviewed-By: Rich Trott --- src/inspector_agent.cc | 8 ++++++-- src/inspector_agent.h | 4 +--- src/inspector_io.cc | 13 +++++-------- src/inspector_io.h | 22 +--------------------- src/inspector_js_api.cc | 14 ++++---------- src/inspector_socket_server.h | 4 ++++ 6 files changed, 21 insertions(+), 44 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index ceba6a59a83e4d..6a62f79e15843b 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -977,6 +977,12 @@ std::shared_ptr Agent::GetWorkerManager() { return client_->getWorkerManager(); } +std::string Agent::GetWsUrl() const { + if (io_ == nullptr) + return ""; + return io_->GetWsUrl(); +} + SameThreadInspectorSession::~SameThreadInspectorSession() { auto client = client_.lock(); if (client) @@ -990,7 +996,5 @@ void SameThreadInspectorSession::Dispatch( client->dispatchMessageFromFrontend(session_id_, message); } - - } // namespace inspector } // namespace node diff --git a/src/inspector_agent.h b/src/inspector_agent.h index 5447a68485c228..4fb544f85bd145 100644 --- a/src/inspector_agent.h +++ b/src/inspector_agent.h @@ -94,9 +94,7 @@ class Agent { void PauseOnNextJavascriptStatement(const std::string& reason); - InspectorIo* io() { - return io_.get(); - } + std::string GetWsUrl() const; // Can only be called from the main thread. bool StartIoThread(); diff --git a/src/inspector_io.cc b/src/inspector_io.cc index 91384ca949c2f1..76e481c9530d95 100644 --- a/src/inspector_io.cc +++ b/src/inspector_io.cc @@ -23,6 +23,9 @@ namespace { using v8_inspector::StringBuffer; using v8_inspector::StringView; +// kKill closes connections and stops the server, kStop only stops the server +enum class TransportAction { kKill, kSendMessage, kStop }; + std::string ScriptPath(uv_loop_t* loop, const std::string& script_name) { std::string script_path; @@ -177,12 +180,6 @@ class RequestQueue { data_->Post(session_id, action, std::move(message)); } - void SetServer(InspectorSocketServer* server) { - Mutex::ScopedLock scoped_lock(lock_); - if (data_ != nullptr) - data_->SetServer(server); - } - bool Expired() { Mutex::ScopedLock scoped_lock(lock_); return data_ == nullptr; @@ -315,8 +312,8 @@ void InspectorIo::ThreadMain() { CheckedUvLoopClose(&loop); } -std::vector InspectorIo::GetTargetIds() const { - return { id_ }; +std::string InspectorIo::GetWsUrl() const { + return FormatWsAddress(host_port_->host(), host_port_->port(), id_, true); } InspectorIoDelegate::InspectorIoDelegate( diff --git a/src/inspector_io.h b/src/inspector_io.h index 57facb2294c181..e9f94056733994 100644 --- a/src/inspector_io.h +++ b/src/inspector_io.h @@ -14,32 +14,14 @@ #include #include - -namespace v8_inspector { -class StringBuffer; -class StringView; -} // namespace v8_inspector - namespace node { // Forward declaration to break recursive dependency chain with src/env.h. class Environment; namespace inspector { -std::string FormatWsAddress(const std::string& host, int port, - const std::string& target_id, - bool include_protocol); - -class InspectorIoDelegate; class MainThreadHandle; class RequestQueue; -// kKill closes connections and stops the server, kStop only stops the server -enum class TransportAction { - kKill, - kSendMessage, - kStop -}; - class InspectorIo { public: // Start the inspector agent thread, waiting for it to initialize @@ -55,9 +37,7 @@ class InspectorIo { ~InspectorIo(); void StopAcceptingNewConnections(); - const std::string& host() const { return host_port_->host(); } - int port() const { return host_port_->port(); } - std::vector GetTargetIds() const; + std::string GetWsUrl() const; private: InspectorIo(std::shared_ptr handle, diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index 9d649385706131..3131c6ad373d11 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -259,16 +259,10 @@ void Open(const FunctionCallbackInfo& args) { void Url(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - Agent* agent = env->inspector_agent(); - InspectorIo* io = agent->io(); - - if (!io) return; - - std::vector ids = io->GetTargetIds(); - - if (ids.empty()) return; - - std::string url = FormatWsAddress(io->host(), io->port(), ids[0], true); + std::string url = env->inspector_agent()->GetWsUrl(); + if (url.length() == 0) { + return; + } args.GetReturnValue().Set(OneByteString(env->isolate(), url.c_str())); } diff --git a/src/inspector_socket_server.h b/src/inspector_socket_server.h index 42ab90ac90bcd3..98d4e7d0150169 100644 --- a/src/inspector_socket_server.h +++ b/src/inspector_socket_server.h @@ -18,6 +18,10 @@ namespace node { namespace inspector { +std::string FormatWsAddress(const std::string& host, int port, + const std::string& target_id, + bool include_protocol); + class InspectorSocketServer; class SocketSession; class ServerSocket;