1 #ifndef SRC_INSPECTOR_WORKER_INSPECTOR_H_ 2 #define SRC_INSPECTOR_WORKER_INSPECTOR_H_ 3 4 #if !HAVE_INSPECTOR 5 #error("This header can only be used when inspector is enabled") 6 #endif 7 8 #include <memory> 9 #include <string> 10 #include <unordered_map> 11 #include <unordered_set> 12 13 namespace node { 14 namespace inspector { 15 class InspectorSession; 16 class InspectorSessionDelegate; 17 class MainThreadHandle; 18 class WorkerManager; 19 20 class WorkerDelegate { 21 public: 22 virtual void WorkerCreated(const std::string& title, 23 const std::string& url, 24 bool waiting, 25 std::shared_ptr<MainThreadHandle> worker) = 0; 26 virtual ~WorkerDelegate() = default; 27 }; 28 29 class WorkerManagerEventHandle { 30 public: WorkerManagerEventHandle(std::shared_ptr<WorkerManager> manager,int id)31 explicit WorkerManagerEventHandle(std::shared_ptr<WorkerManager> manager, 32 int id) 33 : manager_(manager), id_(id) {} 34 void SetWaitOnStart(bool wait_on_start); 35 ~WorkerManagerEventHandle(); 36 37 private: 38 std::shared_ptr<WorkerManager> manager_; 39 int id_; 40 }; 41 42 struct WorkerInfo { WorkerInfoWorkerInfo43 WorkerInfo(const std::string& target_title, 44 const std::string& target_url, 45 std::shared_ptr<MainThreadHandle> worker_thread) 46 : title(target_title), 47 url(target_url), 48 worker_thread(worker_thread) {} 49 std::string title; 50 std::string url; 51 std::shared_ptr<MainThreadHandle> worker_thread; 52 }; 53 54 class ParentInspectorHandle { 55 public: 56 ParentInspectorHandle(int id, const std::string& url, 57 std::shared_ptr<MainThreadHandle> parent_thread, 58 bool wait_for_connect); 59 ~ParentInspectorHandle(); NewParentInspectorHandle(int thread_id,const std::string & url)60 std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle( 61 int thread_id, const std::string& url) { 62 return std::make_unique<ParentInspectorHandle>(thread_id, 63 url, 64 parent_thread_, 65 wait_); 66 } 67 void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread, 68 bool waiting); WaitForConnect()69 bool WaitForConnect() { 70 return wait_; 71 } url()72 const std::string& url() const { return url_; } 73 std::unique_ptr<inspector::InspectorSession> Connect( 74 std::unique_ptr<inspector::InspectorSessionDelegate> delegate, 75 bool prevent_shutdown); 76 77 private: 78 int id_; 79 std::string url_; 80 std::shared_ptr<MainThreadHandle> parent_thread_; 81 bool wait_; 82 }; 83 84 class WorkerManager : public std::enable_shared_from_this<WorkerManager> { 85 public: WorkerManager(std::shared_ptr<MainThreadHandle> thread)86 explicit WorkerManager(std::shared_ptr<MainThreadHandle> thread) 87 : thread_(thread) {} 88 89 std::unique_ptr<ParentInspectorHandle> NewParentHandle( 90 int thread_id, const std::string& url); 91 void WorkerStarted(int session_id, const WorkerInfo& info, bool waiting); 92 void WorkerFinished(int session_id); 93 std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach( 94 std::unique_ptr<WorkerDelegate> attach_delegate); 95 void SetWaitOnStartForDelegate(int id, bool wait); 96 void RemoveAttachDelegate(int id); MainThread()97 std::shared_ptr<MainThreadHandle> MainThread() { 98 return thread_; 99 } 100 101 private: 102 std::shared_ptr<MainThreadHandle> thread_; 103 std::unordered_map<int, WorkerInfo> children_; 104 std::unordered_map<int, std::unique_ptr<WorkerDelegate>> delegates_; 105 // If any one needs it, workers stop for all 106 std::unordered_set<int> delegates_waiting_on_start_; 107 int next_delegate_id_ = 0; 108 }; 109 } // namespace inspector 110 } // namespace node 111 112 #endif // SRC_INSPECTOR_WORKER_INSPECTOR_H_ 113