Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/inspector_agent.h
1 #pragma once 2 3 #include "inspector/network_resource_manager.h" 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #if !HAVE_INSPECTOR 7 #error("This header can only be used when inspector is enabled") 8 #endif 9 10 #include "node_options.h" 11 #include "v8.h" 12 13 #include <cstddef> 14 #include <memory> 15 16 namespace v8_inspector { 17 class StringView; 18 } // namespace v8_inspector 19 20 namespace node { 21 // Forward declaration to break recursive dependency chain with src/env.h. 22 class Environment; 23 struct ContextInfo; 24 25 namespace inspector { 26 class InspectorIo; 27 class ParentInspectorHandle; 28 class NodeInspectorClient; 29 class WorkerManager; 30 31 class InspectorSession { 32 public: 33 virtual ~InspectorSession() = default; 34 virtual void Dispatch(const v8_inspector::StringView& message) = 0; 35 }; 36 37 class InspectorSessionDelegate { 38 public: 39 virtual ~InspectorSessionDelegate() = default; 40 virtual void SendMessageToFrontend(const v8_inspector::StringView& message) 41 = 0; 42 }; 43 44 class Agent { 45 public: 46 explicit Agent(node::Environment* env); 47 ~Agent(); 48 49 // Create client_, may create io_ if option enabled 50 bool Start(const std::string& path, 51 const DebugOptions& options, 52 std::shared_ptr<ExclusiveAccess<HostPort>> host_port, 53 bool is_main); 54 // Stop and destroy io_ 55 void Stop(); 56 57 bool IsListening() { return io_ != nullptr; } 58 // Returns true if the Node inspector is actually in use. It will be true 59 // if either the user explicitly opted into inspector (e.g. with the 60 // --inspect command line flag) or if inspector JS API had been used. 61 bool IsActive(); 62 63 // Blocks till frontend connects and sends "runIfWaitingForDebugger" 64 void WaitForConnect(); 65 bool WaitForConnectByOptions(); 66 void StopIfWaitingForConnect(); 67 68 // Blocks till all the sessions with "WaitForDisconnectOnShutdown" disconnect 69 void WaitForDisconnect(); 70 void ReportUncaughtException(v8::Local<v8::Value> error, 71 v8::Local<v8::Message> message); 72 73 void EmitProtocolEvent(v8::Local<v8::Context> context, 74 const v8_inspector::StringView& event, 75 v8::Local<v8::Object> params); 76 77 void SetupNetworkTracking(v8::Local<v8::Function> enable_function, 78 v8::Local<v8::Function> disable_function); 79 void EnableNetworkTracking(); 80 void DisableNetworkTracking(); 81 82 // Async stack traces instrumentation. 83 void AsyncTaskScheduled(const v8_inspector::StringView& taskName, void* task, 84 bool recurring); 85 void AsyncTaskCanceled(void* task); 86 void AsyncTaskStarted(void* task); 87 void AsyncTaskFinished(void* task); 88 void AllAsyncTasksCanceled(); 89 90 void RegisterAsyncHook(v8::Isolate* isolate, 91 v8::Local<v8::Function> enable_function, 92 v8::Local<v8::Function> disable_function); 93 void EnableAsyncHook(); 94 void DisableAsyncHook(); 95 96 void SetParentHandle(std::unique_ptr<ParentInspectorHandle> parent_handle); 97 std::unique_ptr<ParentInspectorHandle> GetParentHandle(uint64_t thread_id, 98 std::string_view url, 99 std::string_view name); 100 101 // Called to create inspector sessions that can be used from the same thread. 102 // The inspector responds by using the delegate to send messages back. 103 std::unique_ptr<InspectorSession> Connect( 104 std::unique_ptr<InspectorSessionDelegate> delegate, 105 bool prevent_shutdown); 106 107 // Called from the worker to create inspector sessions that is connected 108 // to the main thread. 109 // The inspector responds by using the delegate to send messages back. 110 std::unique_ptr<InspectorSession> ConnectToMainThread( 111 std::unique_ptr<InspectorSessionDelegate> delegate, 112 bool prevent_shutdown); 113 114 void PauseOnNextJavascriptStatement(const std::string& reason); 115 116 std::string GetWsUrl() const; 117 118 // Can only be called from the main thread. 119 bool StartIoThread(); 120 121 // Calls StartIoThread() from off the main thread. 122 void RequestIoThreadStart(); 123 124 const DebugOptions& options() { return debug_options_; } 125 std::shared_ptr<ExclusiveAccess<HostPort>> host_port() { return host_port_; } 126 void ContextCreated(v8::Local<v8::Context> context, const ContextInfo& info); 127 128 // Interface for interacting with inspectors in worker threads 129 std::shared_ptr<WorkerManager> GetWorkerManager(); 130 131 inline Environment* env() const { return parent_env_; } 132 std::shared_ptr<NetworkResourceManager> GetNetworkResourceManager(); 133 134 private: 135 void ToggleAsyncHook(v8::Isolate* isolate, v8::Local<v8::Function> fn); 136 void ToggleNetworkTracking(v8::Isolate* isolate, v8::Local<v8::Function> fn); 137 138 node::Environment* parent_env_; 139 // Encapsulates majority of the Inspector functionality 140 std::shared_ptr<NodeInspectorClient> client_; 141 // Interface for transports, e.g. WebSocket server 142 std::unique_ptr<InspectorIo> io_; 143 std::unique_ptr<ParentInspectorHandle> parent_handle_; 144 std::string path_; 145 146 // This is a copy of the debug options parsed from CLI in the Environment. 147 // Do not use the host_port in that, instead manipulate the shared host_port_ 148 // pointer which is meant to store the actual host and port of the inspector 149 // server. 150 DebugOptions debug_options_; 151 std::shared_ptr<ExclusiveAccess<HostPort>> host_port_; 152 153 bool pending_enable_async_hook_ = false; 154 bool pending_disable_async_hook_ = false; 155 156 bool network_tracking_enabled_ = false; 157 bool pending_enable_network_tracking = false; 158 bool pending_disable_network_tracking = false; 159 std::shared_ptr<NetworkResourceManager> network_resource_manager_; 160 }; 161 162 } // namespace inspector 163 } // namespace node 164 165 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS