node/src/inspector/worker_inspector.h
theanarkh 9ec68afdc5
Some checks failed
Coverage Windows / coverage-windows (push) Waiting to run
Coverage Linux / coverage-linux (push) Failing after 1m29s
Coverage Linux (without intl) / coverage-linux-without-intl (push) Failing after 1m29s
Linters / lint-cpp (push) Successful in 3m46s
Test and upload documentation to artifacts / build-docs (push) Failing after 5m35s
Linters / lint-py (push) Successful in 2m40s
Linters / lint-sh (push) Failing after 1m25s
Linters / lint-addon-docs (push) Successful in 2m20s
Linters / format-cpp (push) Has been skipped
Linters / lint-yaml (push) Successful in 2m13s
Linters / lint-codeowners (push) Failing after 57s
Linters / lint-pr-url (push) Has been skipped
Linters / lint-readme (push) Successful in 1m26s
Notify on Push / Notify on Force Push on `main` (push) Has been skipped
Notify on Push / Notify on Push on `main` that lacks metadata (push) Has been skipped
Scorecard supply-chain security / Scorecard analysis (push) Failing after 53s
Linters / lint-js-and-md (push) Failing after 19m26s
worker: fix worker name with \0
PR-URL: https://github.com/nodejs/node/pull/59214
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2025-08-13 13:53:24 +00:00

124 lines
3.9 KiB
C++

#ifndef SRC_INSPECTOR_WORKER_INSPECTOR_H_
#define SRC_INSPECTOR_WORKER_INSPECTOR_H_
#include "inspector/network_resource_manager.h"
#if !HAVE_INSPECTOR
#error("This header can only be used when inspector is enabled")
#endif
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace node {
namespace inspector {
class InspectorSession;
class InspectorSessionDelegate;
class MainThreadHandle;
class WorkerManager;
class WorkerDelegate {
public:
virtual void WorkerCreated(const std::string& title,
const std::string& url,
bool waiting,
std::shared_ptr<MainThreadHandle> worker) = 0;
virtual ~WorkerDelegate() = default;
};
class WorkerManagerEventHandle {
public:
explicit WorkerManagerEventHandle(std::shared_ptr<WorkerManager> manager,
int id)
: manager_(manager), id_(id) {}
void SetWaitOnStart(bool wait_on_start);
~WorkerManagerEventHandle();
private:
std::shared_ptr<WorkerManager> manager_;
int id_;
};
struct WorkerInfo {
WorkerInfo(const std::string& target_title,
const std::string& target_url,
std::shared_ptr<MainThreadHandle> worker_thread)
: title(target_title),
url(target_url),
worker_thread(worker_thread) {}
std::string title;
std::string url;
std::shared_ptr<MainThreadHandle> worker_thread;
};
class ParentInspectorHandle {
public:
ParentInspectorHandle(
uint64_t id,
std::string_view url,
std::shared_ptr<MainThreadHandle> parent_thread,
bool wait_for_connect,
std::string_view name,
std::shared_ptr<NetworkResourceManager> network_resource_manager);
~ParentInspectorHandle();
std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle(
uint64_t thread_id, std::string_view url, std::string_view name) {
return std::make_unique<ParentInspectorHandle>(
thread_id, url, parent_thread_, wait_, name, network_resource_manager_);
}
void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread,
bool waiting);
bool WaitForConnect() {
return wait_;
}
const std::string& url() const { return url_; }
std::unique_ptr<inspector::InspectorSession> Connect(
std::unique_ptr<inspector::InspectorSessionDelegate> delegate,
bool prevent_shutdown);
std::shared_ptr<NetworkResourceManager> GetNetworkResourceManager() {
return network_resource_manager_;
}
private:
uint64_t id_;
std::string url_;
std::shared_ptr<MainThreadHandle> parent_thread_;
bool wait_;
std::string name_;
std::shared_ptr<NetworkResourceManager> network_resource_manager_;
};
class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
public:
explicit WorkerManager(std::shared_ptr<MainThreadHandle> thread)
: thread_(thread) {}
std::unique_ptr<ParentInspectorHandle> NewParentHandle(
uint64_t thread_id,
std::string_view url,
std::string_view name,
std::shared_ptr<NetworkResourceManager> network_resource_manager);
void WorkerStarted(uint64_t session_id, const WorkerInfo& info, bool waiting);
void WorkerFinished(uint64_t session_id);
std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach(
std::unique_ptr<WorkerDelegate> attach_delegate);
void SetWaitOnStartForDelegate(int id, bool wait);
void RemoveAttachDelegate(int id);
std::shared_ptr<MainThreadHandle> MainThread() {
return thread_;
}
private:
std::shared_ptr<MainThreadHandle> thread_;
std::unordered_map<uint64_t, WorkerInfo> children_;
std::unordered_map<int, std::unique_ptr<WorkerDelegate>> delegates_;
// If any one needs it, workers stop for all
std::unordered_set<int> delegates_waiting_on_start_;
int next_delegate_id_ = 0;
};
} // namespace inspector
} // namespace node
#endif // SRC_INSPECTOR_WORKER_INSPECTOR_H_