11cb0ef41Sopenharmony_ci#ifndef SRC_INSPECTOR_WORKER_INSPECTOR_H_ 21cb0ef41Sopenharmony_ci#define SRC_INSPECTOR_WORKER_INSPECTOR_H_ 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci#if !HAVE_INSPECTOR 51cb0ef41Sopenharmony_ci#error("This header can only be used when inspector is enabled") 61cb0ef41Sopenharmony_ci#endif 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <memory> 91cb0ef41Sopenharmony_ci#include <string> 101cb0ef41Sopenharmony_ci#include <unordered_map> 111cb0ef41Sopenharmony_ci#include <unordered_set> 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cinamespace node { 141cb0ef41Sopenharmony_cinamespace inspector { 151cb0ef41Sopenharmony_ciclass InspectorSession; 161cb0ef41Sopenharmony_ciclass InspectorSessionDelegate; 171cb0ef41Sopenharmony_ciclass MainThreadHandle; 181cb0ef41Sopenharmony_ciclass WorkerManager; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciclass WorkerDelegate { 211cb0ef41Sopenharmony_ci public: 221cb0ef41Sopenharmony_ci virtual void WorkerCreated(const std::string& title, 231cb0ef41Sopenharmony_ci const std::string& url, 241cb0ef41Sopenharmony_ci bool waiting, 251cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> worker) = 0; 261cb0ef41Sopenharmony_ci virtual ~WorkerDelegate() = default; 271cb0ef41Sopenharmony_ci}; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciclass WorkerManagerEventHandle { 301cb0ef41Sopenharmony_ci public: 311cb0ef41Sopenharmony_ci explicit WorkerManagerEventHandle(std::shared_ptr<WorkerManager> manager, 321cb0ef41Sopenharmony_ci int id) 331cb0ef41Sopenharmony_ci : manager_(manager), id_(id) {} 341cb0ef41Sopenharmony_ci void SetWaitOnStart(bool wait_on_start); 351cb0ef41Sopenharmony_ci ~WorkerManagerEventHandle(); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci private: 381cb0ef41Sopenharmony_ci std::shared_ptr<WorkerManager> manager_; 391cb0ef41Sopenharmony_ci int id_; 401cb0ef41Sopenharmony_ci}; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cistruct WorkerInfo { 431cb0ef41Sopenharmony_ci WorkerInfo(const std::string& target_title, 441cb0ef41Sopenharmony_ci const std::string& target_url, 451cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> worker_thread) 461cb0ef41Sopenharmony_ci : title(target_title), 471cb0ef41Sopenharmony_ci url(target_url), 481cb0ef41Sopenharmony_ci worker_thread(worker_thread) {} 491cb0ef41Sopenharmony_ci std::string title; 501cb0ef41Sopenharmony_ci std::string url; 511cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> worker_thread; 521cb0ef41Sopenharmony_ci}; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciclass ParentInspectorHandle { 551cb0ef41Sopenharmony_ci public: 561cb0ef41Sopenharmony_ci ParentInspectorHandle(uint64_t id, 571cb0ef41Sopenharmony_ci const std::string& url, 581cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> parent_thread, 591cb0ef41Sopenharmony_ci bool wait_for_connect, 601cb0ef41Sopenharmony_ci const std::string& name); 611cb0ef41Sopenharmony_ci ~ParentInspectorHandle(); 621cb0ef41Sopenharmony_ci std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle( 631cb0ef41Sopenharmony_ci uint64_t thread_id, const std::string& url, const std::string& name) { 641cb0ef41Sopenharmony_ci return std::make_unique<ParentInspectorHandle>( 651cb0ef41Sopenharmony_ci thread_id, url, parent_thread_, wait_, name); 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread, 681cb0ef41Sopenharmony_ci bool waiting); 691cb0ef41Sopenharmony_ci bool WaitForConnect() { 701cb0ef41Sopenharmony_ci return wait_; 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci const std::string& url() const { return url_; } 731cb0ef41Sopenharmony_ci std::unique_ptr<inspector::InspectorSession> Connect( 741cb0ef41Sopenharmony_ci std::unique_ptr<inspector::InspectorSessionDelegate> delegate, 751cb0ef41Sopenharmony_ci bool prevent_shutdown); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci private: 781cb0ef41Sopenharmony_ci uint64_t id_; 791cb0ef41Sopenharmony_ci std::string url_; 801cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> parent_thread_; 811cb0ef41Sopenharmony_ci bool wait_; 821cb0ef41Sopenharmony_ci std::string name_; 831cb0ef41Sopenharmony_ci}; 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ciclass WorkerManager : public std::enable_shared_from_this<WorkerManager> { 861cb0ef41Sopenharmony_ci public: 871cb0ef41Sopenharmony_ci explicit WorkerManager(std::shared_ptr<MainThreadHandle> thread) 881cb0ef41Sopenharmony_ci : thread_(thread) {} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci std::unique_ptr<ParentInspectorHandle> NewParentHandle( 911cb0ef41Sopenharmony_ci uint64_t thread_id, const std::string& url, const std::string& name); 921cb0ef41Sopenharmony_ci void WorkerStarted(uint64_t session_id, const WorkerInfo& info, bool waiting); 931cb0ef41Sopenharmony_ci void WorkerFinished(uint64_t session_id); 941cb0ef41Sopenharmony_ci std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach( 951cb0ef41Sopenharmony_ci std::unique_ptr<WorkerDelegate> attach_delegate); 961cb0ef41Sopenharmony_ci void SetWaitOnStartForDelegate(int id, bool wait); 971cb0ef41Sopenharmony_ci void RemoveAttachDelegate(int id); 981cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> MainThread() { 991cb0ef41Sopenharmony_ci return thread_; 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci private: 1031cb0ef41Sopenharmony_ci std::shared_ptr<MainThreadHandle> thread_; 1041cb0ef41Sopenharmony_ci std::unordered_map<uint64_t, WorkerInfo> children_; 1051cb0ef41Sopenharmony_ci std::unordered_map<int, std::unique_ptr<WorkerDelegate>> delegates_; 1061cb0ef41Sopenharmony_ci // If any one needs it, workers stop for all 1071cb0ef41Sopenharmony_ci std::unordered_set<int> delegates_waiting_on_start_; 1081cb0ef41Sopenharmony_ci int next_delegate_id_ = 0; 1091cb0ef41Sopenharmony_ci}; 1101cb0ef41Sopenharmony_ci} // namespace inspector 1111cb0ef41Sopenharmony_ci} // namespace node 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci#endif // SRC_INSPECTOR_WORKER_INSPECTOR_H_ 114