11#include " inspector_agent.h"
2+ #include < string>
23
34#include " env-inl.h"
45#include " inspector/main_thread_interface.h"
56#include " inspector/network_inspector.h"
67#include " inspector/node_string.h"
78#include " inspector/runtime_agent.h"
9+ #include " inspector/target_agent.h"
810#include " inspector/tracing_agent.h"
911#include " inspector/worker_agent.h"
1012#include " inspector/worker_inspector.h"
1820#include " permission/permission.h"
1921#include " timer_wrap-inl.h"
2022#include " util-inl.h"
23+ #include " util.h"
2124#include " v8-inspector.h"
2225#include " v8-platform.h"
2326
@@ -216,8 +219,9 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
216219 std::unique_ptr<InspectorSessionDelegate> delegate,
217220 std::shared_ptr<MainThreadHandle> main_thread_,
218221 bool prevent_shutdown)
219- : delegate_(std::move(delegate)), prevent_shutdown_(prevent_shutdown),
220- retaining_context_(false ) {
222+ : delegate_(std::move(delegate)), main_thread_(main_thread_),
223+ prevent_shutdown_(prevent_shutdown), retaining_context_(false )
224+ {
221225 session_ = inspector->connect (CONTEXT_GROUP_ID,
222226 this ,
223227 StringView (),
@@ -234,6 +238,11 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
234238 runtime_agent_->Wire (node_dispatcher_.get ());
235239 network_inspector_ = std::make_unique<NetworkInspector>(env);
236240 network_inspector_->Wire (node_dispatcher_.get ());
241+ if (env->options ()->experimental_worker_inspection ) {
242+ target_agent_ = std::make_shared<protocol::TargetAgent>();
243+ target_agent_->Wire (node_dispatcher_.get ());
244+ target_agent_->listenWorker (worker_manager);
245+ }
237246 }
238247
239248 ~ChannelImpl () override {
@@ -247,6 +256,9 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
247256 runtime_agent_.reset (); // Dispose before the dispatchers
248257 network_inspector_->Disable ();
249258 network_inspector_.reset (); // Dispose before the dispatchers
259+ if (target_agent_) {
260+ target_agent_->reset ();
261+ }
250262 }
251263
252264 void emitNotificationFromBackend (const StringView& event,
@@ -329,6 +341,17 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
329341 " [inspector send] %s\n " ,
330342 raw_message);
331343 }
344+ std::optional<int > target_session_id = main_thread_->GetTargetSessionId ();
345+ if (target_session_id.has_value ()) {
346+ std::string raw_message = protocol::StringUtil::StringViewToUtf8 (message);
347+ std::unique_ptr<protocol::DictionaryValue> value =
348+ protocol::DictionaryValue::cast (
349+ protocol::StringUtil::parseJSON (raw_message));
350+ std::string target_session_id_str = std::to_string (*target_session_id);
351+ value->setString (" sessionId" , target_session_id_str);
352+ delegate_->SendMessageToFrontend (Utf8ToStringView (value->serializeToJSON ())->string ());
353+ }
354+
332355 delegate_->SendMessageToFrontend (message);
333356 }
334357
@@ -356,10 +379,12 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
356379 std::unique_ptr<protocol::RuntimeAgent> runtime_agent_;
357380 std::unique_ptr<protocol::TracingAgent> tracing_agent_;
358381 std::unique_ptr<protocol::WorkerAgent> worker_agent_;
382+ std::shared_ptr<protocol::TargetAgent> target_agent_;
359383 std::unique_ptr<NetworkInspector> network_inspector_;
360384 std::unique_ptr<InspectorSessionDelegate> delegate_;
361385 std::unique_ptr<v8_inspector::V8InspectorSession> session_;
362386 std::unique_ptr<protocol::UberDispatcher> node_dispatcher_;
387+ std::shared_ptr<MainThreadHandle> main_thread_;
363388 bool prevent_shutdown_;
364389 bool retaining_context_;
365390};
0 commit comments