• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef JS_NATIVE_API_V8_INSPECTOR_H
17 #define JS_NATIVE_API_V8_INSPECTOR_H
18 
19 #include <cstddef>
20 #include <memory>
21 
22 #include "js_native_api_v8.h"
23 #include "jsvm_host_port.h"
24 #include "jsvm_inspector_agent.h"
25 #include "v8.h"
26 
27 #ifndef ENABLE_INSPECTOR
28 #error("This header can only be used when inspector is enabled")
29 #endif
30 
31 namespace v8_inspector {
32 class StringView;
33 } // namespace v8_inspector
34 
35 namespace jsvm {
36 namespace inspector {
37 class InspectorSessionDelegate;
38 class InspectorSession;
39 
40 class InspectorSession {
41 public:
42     virtual ~InspectorSession() = default;
43     virtual void Dispatch(const v8_inspector::StringView& message) = 0;
44 };
45 
46 class InspectorSessionDelegate {
47 public:
48     virtual ~InspectorSessionDelegate() = default;
49     virtual void SendMessageToFrontend(const v8_inspector::StringView& message) = 0;
50 };
51 } // namespace inspector
52 } // namespace jsvm
53 
54 namespace v8impl {
55 
56 using jsvm::ExclusiveAccess;
57 using jsvm::HostPort;
58 using jsvm::inspector::InspectorSession;
59 using jsvm::inspector::InspectorSessionDelegate;
60 
61 class IsolateData;
62 using Environment = JSVM_Env__;
63 
64 class InspectorClient;
65 class InspectorIo;
66 
67 struct ContextInfo {
ContextInfoContextInfo68     explicit ContextInfo(const std::string& name) : name(name) {}
69     const std::string name;
70     std::string origin;
71     bool isDefault = false;
72 };
73 
74 class Agent : public jsvm::InspectorAgent {
75 public:
76     explicit Agent(Environment* env);
77     ~Agent();
78 
79 public:
80     bool Start(const std::string& pathParam, const std::string& hostName, int port, int pid = -1) override
81     {
82         auto hostPort = std::make_shared<jsvm::ExclusiveAccess<jsvm::HostPort>>(hostName, port, pid);
83         return Start(pathParam, hostPort, true, false);
84     }
85 
86     bool Start(const std::string& pathParam, int pid) override;
87 
88     // Stop and destroy io_
89     void Stop() override;
90 
91     // Returns true if the inspector is actually in use.
92     bool IsActive() override;
93 
94     // Blocks till frontend connects and sends "runIfWaitingForDebugger"
95     void WaitForConnect() override;
96 
97     // Blocks till all the sessions with "WaitForDisconnectOnShutdown" disconnect
98     void WaitForDisconnect() override;
99 
100     void PauseOnNextJavascriptStatement(const std::string& reason) override;
101 
102 public:
103     // Called to create inspector sessions that can be used from the same thread.
104     // The inspector responds by using the delegate to send messages back.
105     std::unique_ptr<InspectorSession> Connect(std::unique_ptr<InspectorSessionDelegate> delegate, bool preventShutdown);
106 
107     // Can only be called from the main thread.
108     bool StartIoThread();
109 
GetHostPort()110     std::shared_ptr<ExclusiveAccess<HostPort>> GetHostPort()
111     {
112         return hostPort;
113     }
114 
env()115     inline Environment* env() const
116     {
117         return parentEnv;
118     }
119 
120 private:
121     // Create client_, may create io if option enabled
122     bool Start(const std::string& pathParam,
123                std::shared_ptr<ExclusiveAccess<HostPort>> hostPortParam,
124                bool isMain,
125                bool waitForConnect);
126 
127     Environment* parentEnv;
128     // Encapsulates majority of the Inspector functionality
129     std::shared_ptr<InspectorClient> client;
130     // Interface for transports, e.g. WebSocket server
131     std::unique_ptr<InspectorIo> io;
132     std::string path;
133 
134     std::shared_ptr<ExclusiveAccess<HostPort>> hostPort;
135 };
136 
137 } // namespace v8impl
138 
139 #endif