-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inspector: support for worker inspection in chrome devtools #56759
Open
islandryu
wants to merge
7
commits into
nodejs:main
Choose a base branch
from
islandryu:feat/cdpTarget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+422
−23
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
525a2d2
inspector: support for worker inspection in chrome devtools
islandryu cab3f96
fix/test
islandryu fafadd0
format
islandryu 234a139
format
islandryu 01011f6
fix test
islandryu cde7b09
support setAutoAttach
islandryu b669db4
fix stoi error
islandryu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "target_agent.h" | ||
#include "crdtp/dispatch.h" | ||
#include "inspector/worker_inspector.h" | ||
#include "main_thread_interface.h" | ||
|
||
namespace node { | ||
namespace inspector { | ||
namespace protocol { | ||
|
||
std::unordered_map<int, std::shared_ptr<MainThreadHandle>> | ||
TargetAgent::target_session_id_worker_map_ = | ||
std::unordered_map<int, std::shared_ptr<MainThreadHandle>>(); | ||
int TargetAgent::next_session_id_ = 1; | ||
class WorkerTargetDelegate : public WorkerDelegate { | ||
public: | ||
explicit WorkerTargetDelegate(std::shared_ptr<TargetAgent> target_agent) | ||
: target_agent_(target_agent) {} | ||
|
||
void WorkerCreated(const std::string& title, | ||
const std::string& url, | ||
bool waiting, | ||
std::shared_ptr<MainThreadHandle> worker) override { | ||
target_agent_->createAndAttachIfNecessary(worker, title, url); | ||
} | ||
|
||
private: | ||
const std::shared_ptr<TargetAgent> target_agent_; | ||
}; | ||
|
||
std::unique_ptr<Target::TargetInfo> createTargetInfo(const String& target_id, | ||
const String& type, | ||
const String& title, | ||
const String& url) { | ||
return Target::TargetInfo::create() | ||
.setTargetId(target_id) | ||
.setType(type) | ||
.setTitle(title) | ||
.setUrl(url) | ||
.setAttached(false) | ||
.setCanAccessOpener(true) | ||
.build(); | ||
} | ||
|
||
void TargetAgent::Wire(UberDispatcher* dispatcher) { | ||
frontend_ = std::make_unique<Target::Frontend>(dispatcher->channel()); | ||
Target::Dispatcher::wire(dispatcher, this); | ||
} | ||
|
||
void TargetAgent::createAndAttachIfNecessary( | ||
std::shared_ptr<MainThreadHandle> worker, | ||
const std::string& title, | ||
const std::string& url) { | ||
std::string target_id = std::to_string(getNextTargetId()); | ||
std::string type = "worker"; | ||
|
||
targetCreated(target_id, type, title, url); | ||
bool attached = false; | ||
if (auto_attach_) { | ||
attached = true; | ||
attachedToTarget(worker, target_id, type, title, url); | ||
} | ||
targets_.push_back({target_id, type, title, url, worker, attached}); | ||
} | ||
|
||
void TargetAgent::listenWorker(std::weak_ptr<WorkerManager> worker_manager) { | ||
auto manager = worker_manager.lock(); | ||
if (!manager) { | ||
return; | ||
} | ||
std::unique_ptr<WorkerDelegate> delegate( | ||
new WorkerTargetDelegate(shared_from_this())); | ||
worker_event_handle_ = manager->SetAutoAttach(std::move(delegate)); | ||
} | ||
|
||
void TargetAgent::reset() { | ||
if (worker_event_handle_) { | ||
worker_event_handle_.reset(); | ||
} | ||
} | ||
|
||
void TargetAgent::targetCreated(const std::string& target_id, | ||
const std::string& type, | ||
const std::string& title, | ||
const std::string& url) { | ||
frontend_->targetCreated(createTargetInfo(target_id, type, title, url)); | ||
} | ||
|
||
int TargetAgent::getNextSessionId() { | ||
return next_session_id_++; | ||
} | ||
|
||
int TargetAgent::getNextTargetId() { | ||
return next_target_id_++; | ||
} | ||
|
||
void TargetAgent::attachedToTarget(std::shared_ptr<MainThreadHandle> worker, | ||
const std::string& target_id, | ||
const std::string& type, | ||
const std::string& title, | ||
const std::string& url) { | ||
int session_id = getNextSessionId(); | ||
target_session_id_worker_map_[session_id] = worker; | ||
worker->SetTargetSessionId(session_id); | ||
frontend_->attachedToTarget(std::to_string(session_id), | ||
createTargetInfo(target_id, type, title, url), | ||
true); | ||
} | ||
|
||
// TODO(islandryu): Currently, setAutoAttach applies the main thread's value to | ||
// all threads. Modify it to be managed per worker thread. | ||
crdtp::DispatchResponse TargetAgent::setAutoAttach( | ||
bool auto_attach, bool wait_for_debugger_on_start) { | ||
auto_attach_ = auto_attach; | ||
wait_for_debugger_on_start_ = wait_for_debugger_on_start; | ||
|
||
if (auto_attach) { | ||
for (auto& target : targets_) { | ||
if (!target.attached) { | ||
target.attached = true; | ||
attachedToTarget(target.worker, | ||
target.target_id, | ||
target.type, | ||
target.title, | ||
target.url); | ||
} | ||
} | ||
} | ||
|
||
return DispatchResponse::Success(); | ||
} | ||
|
||
} // namespace protocol | ||
} // namespace inspector | ||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifndef SRC_INSPECTOR_TARGET_AGENT_H_ | ||
#define SRC_INSPECTOR_TARGET_AGENT_H_ | ||
|
||
#include <unordered_map> | ||
#include <vector> | ||
#include "inspector/worker_inspector.h" | ||
#include "node/inspector/protocol/Target.h" | ||
|
||
namespace node { | ||
|
||
namespace inspector { | ||
class TargetInspector; | ||
|
||
namespace protocol { | ||
|
||
struct TargetInfo { | ||
std::string target_id; | ||
std::string type; | ||
std::string title; | ||
std::string url; | ||
std::shared_ptr<MainThreadHandle> worker; | ||
bool attached; | ||
}; | ||
|
||
class TargetAgent : public Target::Backend, | ||
public std::enable_shared_from_this<TargetAgent> { | ||
public: | ||
void Wire(UberDispatcher* dispatcher); | ||
|
||
void createAndAttachIfNecessary(std::shared_ptr<MainThreadHandle> worker, | ||
const std::string& title, | ||
const std::string& url); | ||
|
||
DispatchResponse setAutoAttach(bool auto_attach, | ||
bool wait_for_debugger_on_start) override; | ||
|
||
void listenWorker(std::weak_ptr<WorkerManager> worker_manager); | ||
void reset(); | ||
static std::unordered_map<int, std::shared_ptr<MainThreadHandle>> | ||
target_session_id_worker_map_; | ||
|
||
bool isThisThread(MainThreadHandle* worker) { return worker == main_thread_; } | ||
|
||
private: | ||
int getNextTargetId(); | ||
int getNextSessionId(); | ||
void targetCreated(const std::string& target_id, | ||
const std::string& type, | ||
const std::string& title, | ||
const std::string& url); | ||
void attachedToTarget(std::shared_ptr<MainThreadHandle> worker, | ||
const std::string& target_id, | ||
const std::string& type, | ||
const std::string& title, | ||
const std::string& url); | ||
std::shared_ptr<Target::Frontend> frontend_; | ||
std::weak_ptr<WorkerManager> worker_manager_; | ||
static int next_session_id_; | ||
int next_target_id_ = 1; | ||
std::unique_ptr<WorkerManagerEventHandle> worker_event_handle_ = nullptr; | ||
bool auto_attach_ = false; | ||
// TODO(islandryu): If false, implement it so that each thread does not wait | ||
// for the worker to execute. | ||
bool wait_for_debugger_on_start_ = true; | ||
std::vector<TargetInfo> targets_; | ||
MainThreadHandle* main_thread_; | ||
}; | ||
|
||
} // namespace protocol | ||
} // namespace inspector | ||
} // namespace node | ||
|
||
#endif // SRC_INSPECTOR_TARGET_AGENT_H_ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setAutoAttach is implemented.
However, I am not able to determine autoAttach for each related Target and waitForDebuggerOnStart.
I have tried several way, but it was difficult to make the changes without affecting the existing nodeWorker, so I would like to make a TODO.