|
| 1 | +#include "target_agent.h" |
| 2 | +#include <string_view> |
| 3 | +#include "crdtp/dispatch.h" |
| 4 | +#include "inspector/worker_inspector.h" |
| 5 | +#include "main_thread_interface.h" |
| 6 | + |
| 7 | +namespace node { |
| 8 | +namespace inspector { |
| 9 | +namespace protocol { |
| 10 | + |
| 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; |
| 15 | +class WorkerTargetDelegate : public WorkerDelegate { |
| 16 | + public: |
| 17 | + explicit WorkerTargetDelegate(std::shared_ptr<TargetAgent> target_agent) |
| 18 | + : target_agent_(target_agent) {} |
| 19 | + |
| 20 | + void WorkerCreated(const std::string& title, |
| 21 | + const std::string& url, |
| 22 | + bool waiting, |
| 23 | + std::shared_ptr<MainThreadHandle> worker) override { |
| 24 | + target_agent_->createAndAttachIfNecessary(worker, title, url); |
| 25 | + } |
| 26 | + |
| 27 | + private: |
| 28 | + const std::shared_ptr<TargetAgent> target_agent_; |
| 29 | +}; |
| 30 | + |
| 31 | +std::unique_ptr<Target::TargetInfo> createTargetInfo( |
| 32 | + const std::string_view target_id, |
| 33 | + const std::string_view type, |
| 34 | + const std::string_view title, |
| 35 | + const std::string_view url) { |
| 36 | + return Target::TargetInfo::create() |
| 37 | + .setTargetId(std::string(target_id)) |
| 38 | + .setType(std::string(type)) |
| 39 | + .setTitle(std::string(title)) |
| 40 | + .setUrl(std::string(url)) |
| 41 | + .setAttached(false) |
| 42 | + .setCanAccessOpener(true) |
| 43 | + .build(); |
| 44 | +} |
| 45 | + |
| 46 | +void TargetAgent::Wire(UberDispatcher* dispatcher) { |
| 47 | + frontend_ = std::make_unique<Target::Frontend>(dispatcher->channel()); |
| 48 | + Target::Dispatcher::wire(dispatcher, this); |
| 49 | +} |
| 50 | + |
| 51 | +void TargetAgent::createAndAttachIfNecessary( |
| 52 | + std::shared_ptr<MainThreadHandle> worker, |
| 53 | + const std::string& title, |
| 54 | + const std::string& url) { |
| 55 | + std::string target_id = std::to_string(getNextTargetId()); |
| 56 | + std::string type = "node_worker"; |
| 57 | + |
| 58 | + targetCreated(target_id, type, title, url); |
| 59 | + bool attached = false; |
| 60 | + if (auto_attach_) { |
| 61 | + attached = true; |
| 62 | + attachedToTarget(worker, target_id, type, title, url); |
| 63 | + } |
| 64 | + targets_.push_back({target_id, type, title, url, worker, attached}); |
| 65 | +} |
| 66 | + |
| 67 | +void TargetAgent::listenWorker(std::weak_ptr<WorkerManager> worker_manager) { |
| 68 | + auto manager = worker_manager.lock(); |
| 69 | + if (!manager) { |
| 70 | + return; |
| 71 | + } |
| 72 | + std::unique_ptr<WorkerDelegate> delegate( |
| 73 | + new WorkerTargetDelegate(shared_from_this())); |
| 74 | + worker_event_handle_ = manager->SetAutoAttach(std::move(delegate)); |
| 75 | +} |
| 76 | + |
| 77 | +void TargetAgent::reset() { |
| 78 | + if (worker_event_handle_) { |
| 79 | + worker_event_handle_.reset(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void TargetAgent::targetCreated(const std::string_view target_id, |
| 84 | + const std::string_view type, |
| 85 | + const std::string_view title, |
| 86 | + const std::string_view url) { |
| 87 | + frontend_->targetCreated(createTargetInfo(target_id, type, title, url)); |
| 88 | +} |
| 89 | + |
| 90 | +int TargetAgent::getNextSessionId() { |
| 91 | + return next_session_id_++; |
| 92 | +} |
| 93 | + |
| 94 | +int TargetAgent::getNextTargetId() { |
| 95 | + return next_target_id_++; |
| 96 | +} |
| 97 | + |
| 98 | +void TargetAgent::attachedToTarget(std::shared_ptr<MainThreadHandle> worker, |
| 99 | + const std::string& target_id, |
| 100 | + const std::string& type, |
| 101 | + const std::string& title, |
| 102 | + const std::string& url) { |
| 103 | + int session_id = getNextSessionId(); |
| 104 | + target_session_id_worker_map_[session_id] = worker; |
| 105 | + worker->SetTargetSessionId(session_id); |
| 106 | + frontend_->attachedToTarget(std::to_string(session_id), |
| 107 | + createTargetInfo(target_id, type, title, url), |
| 108 | + true); |
| 109 | +} |
| 110 | + |
| 111 | +// TODO(islandryu): Currently, setAutoAttach applies the main thread's value to |
| 112 | +// all threads. Modify it to be managed per worker thread. |
| 113 | +crdtp::DispatchResponse TargetAgent::setAutoAttach( |
| 114 | + 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; |
| 117 | + |
| 118 | + if (auto_attach) { |
| 119 | + for (auto& target : targets_) { |
| 120 | + if (!target.attached) { |
| 121 | + target.attached = true; |
| 122 | + attachedToTarget(target.worker, |
| 123 | + target.target_id, |
| 124 | + target.type, |
| 125 | + target.title, |
| 126 | + target.url); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return DispatchResponse::Success(); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace protocol |
| 135 | +} // namespace inspector |
| 136 | +} // namespace node |
0 commit comments