@@ -8,10 +8,6 @@ namespace node {
88namespace inspector {
99namespace protocol {
1010
11- std::unordered_map<int , std::shared_ptr<MainThreadHandle>>
12- TargetAgent::target_session_id_worker_map_ =
13- std::unordered_map<int , std::shared_ptr<MainThreadHandle>>();
14- int TargetAgent::next_session_id_ = 1 ;
1511class WorkerTargetDelegate : public WorkerDelegate {
1612 public:
1713 explicit WorkerTargetDelegate (std::shared_ptr<TargetAgent> target_agent)
@@ -32,13 +28,14 @@ std::unique_ptr<Target::TargetInfo> createTargetInfo(
3228 const std::string_view target_id,
3329 const std::string_view type,
3430 const std::string_view title,
35- const std::string_view url) {
31+ const std::string_view url,
32+ bool attached = false ) {
3633 return Target::TargetInfo::create ()
3734 .setTargetId (std::string (target_id))
3835 .setType (std::string (type))
3936 .setTitle (std::string (title))
4037 .setUrl (std::string (url))
41- .setAttached (false )
38+ .setAttached (attached )
4239 .setCanAccessOpener (true )
4340 .build ();
4441}
@@ -57,11 +54,11 @@ void TargetAgent::createAndAttachIfNecessary(
5754
5855 targetCreated (target_id, type, title, url);
5956 bool attached = false ;
60- if (auto_attach_ ) {
57+ if (target_manager_-> auto_attach () ) {
6158 attached = true ;
6259 attachedToTarget (worker, target_id, type, title, url);
6360 }
64- targets_. push_back ({ target_id, type, title, url, worker, attached} );
61+ target_manager_-> AddTarget (worker, target_id, type, title, url, attached);
6562}
6663
6764void TargetAgent::listenWorker (std::weak_ptr<WorkerManager> worker_manager) {
@@ -87,12 +84,26 @@ void TargetAgent::targetCreated(const std::string_view target_id,
8784 frontend_->targetCreated (createTargetInfo (target_id, type, title, url));
8885}
8986
87+ crdtp::DispatchResponse TargetAgent::getTargets (
88+ std::unique_ptr<protocol::Array<Target::TargetInfo>>* out_targetInfos) {
89+ auto target_infos = std::make_unique<protocol::Array<Target::TargetInfo>>();
90+ for (const auto & target : target_manager_->GetTargetsSnapshot ()) {
91+ target_infos->push_back (createTargetInfo (target.target_id ,
92+ target.type ,
93+ target.title ,
94+ target.url ,
95+ target.attached ));
96+ }
97+ *out_targetInfos = std::move (target_infos);
98+ return DispatchResponse::Success ();
99+ }
100+
90101int TargetAgent::getNextSessionId () {
91- return next_session_id_++ ;
102+ return target_manager_-> NextSessionId () ;
92103}
93104
94105int TargetAgent::getNextTargetId () {
95- return next_target_id_++ ;
106+ return target_manager_-> NextTargetId () ;
96107}
97108
98109void TargetAgent::attachedToTarget (std::shared_ptr<MainThreadHandle> worker,
@@ -101,7 +112,7 @@ void TargetAgent::attachedToTarget(std::shared_ptr<MainThreadHandle> worker,
101112 const std::string& title,
102113 const std::string& url) {
103114 int session_id = getNextSessionId ();
104- target_session_id_worker_map_[ session_id] = worker;
115+ TargetManager::RegisterSessionWorker ( session_id, worker) ;
105116 worker->SetTargetSessionId (session_id);
106117 frontend_->attachedToTarget (std::to_string (session_id),
107118 createTargetInfo (target_id, type, title, url),
@@ -112,11 +123,10 @@ void TargetAgent::attachedToTarget(std::shared_ptr<MainThreadHandle> worker,
112123// all threads. Modify it to be managed per worker thread.
113124crdtp::DispatchResponse TargetAgent::setAutoAttach (
114125 bool auto_attach, bool wait_for_debugger_on_start) {
115- auto_attach_ = auto_attach;
116- wait_for_debugger_on_start_ = wait_for_debugger_on_start;
126+ target_manager_->SetAutoAttach (auto_attach, wait_for_debugger_on_start);
117127
118128 if (auto_attach) {
119- for (auto & target : targets_ ) {
129+ for (auto & target : target_manager_-> targets () ) {
120130 if (!target.attached ) {
121131 target.attached = true ;
122132 attachedToTarget (target.worker ,
0 commit comments