1 /*
2  * Copyright (c) 2022-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 OHOS_ABILITY_RUNTIME_CONNECT_SERVER_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_CONNECT_SERVER_MANAGER_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 #include "jsnapi.h"
22 using DebuggerPostTask = std::function<void(std::function<void()>&&)>;
23 using DebuggerInfo = std::unordered_map<int, std::pair<void*, const DebuggerPostTask>>;
24 using InstanceMap = std::unordered_map<int32_t, std::string>;
25 using ServerConnectCallback = void(*)(void);
26 #ifdef APP_USE_ARM
27 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib/platformsdk/libark_inspector.z.so";
28 #elif defined(APP_USE_X86_64)
29 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/platformsdk/libark_inspector.z.so";
30 #else
31 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/platformsdk/libark_inspector.z.so";
32 #endif
33 namespace OHOS::AbilityRuntime {
34 class ConnectServerManager final {
35 public:
36     static ConnectServerManager& Get();
37 
38     void StartConnectServer(const std::string& bundleName, int socketFd, bool isLocalAbstract);
39     void StopConnectServer(bool isCloseSo = true);
40     bool AddInstance(int32_t tid, int32_t instanceId, const std::string& instanceName = "PandaDebugger");
41     void RemoveInstance(int32_t instanceId);
42     void SendInspector(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr);
43     void SendArkUIStateProfilerMessage(const std::string &message);
44     void SetLayoutInspectorCallback(
45         const std::function<void(int32_t)> &createLayoutInfo, const std::function<void(bool)> &setStatus);
46     void SetStateProfilerCallback(const std::function<void(bool)> &setArkUIStateProfilerStatus);
47     std::function<void(int32_t)> GetLayoutInspectorCallback();
48     bool StoreInstanceMessage(
49         int32_t tid, int32_t instanceId, const std::string& instanceName = "PandaDebugger");
50     void StoreDebuggerInfo(int32_t tid, void* vm, const panda::JSNApi::DebugOption& debugOption,
51         const DebuggerPostTask& debuggerPostTask, bool isDebugApp);
52     void SetConnectedCallback();
53     bool SendInstanceMessage(int32_t tid, int32_t instanceId, const std::string& instanceName);
54     void SendDebuggerInfo(bool needBreakPoint, bool isDebugApp);
55     void LoadConnectServerDebuggerSo();
56     DebuggerPostTask GetDebuggerPostTask(int32_t tid);
57     void SetSwitchCallback(int32_t instanceId);
58     void SetProfilerCallBack();
59     bool SetRecordCallback(const std::function<void(void)> &startRecordFunc,
60         const std::function<void(void)> &stopRecordFunc);
61     void SetRecordResults(const std::string &jsonArrayStr);
62     void RegistConnectServerCallback(const ServerConnectCallback &connectServerCallback);
63 
64 private:
65     ConnectServerManager() = default;
66     ~ConnectServerManager();
67 
68     void* handlerConnectServerSo_ = nullptr;
69     std::string bundleName_;
70 
71     std::mutex mutex_;
72     static std::mutex instanceMutex_;
73     static std::mutex callbackMutex_;
74     std::atomic<bool> isConnected_ = false;
75     std::unordered_map<int32_t, std::pair<std::string, int32_t>> instanceMap_;
76     std::function<void(int32_t)> createLayoutInfo_;
77     std::function<void(int32_t)> setStatus_;
78     std::function<void(int32_t)> setArkUIStateProfilerStatus_;
79     std::vector<ServerConnectCallback> connectServerCallbacks_;
80     ConnectServerManager(const ConnectServerManager&) = delete;
81     ConnectServerManager(ConnectServerManager&&) = delete;
82     ConnectServerManager& operator=(const ConnectServerManager&) = delete;
83     ConnectServerManager& operator=(ConnectServerManager&&) = delete;
84 };
85 } // namespace OHOS::AbilityRuntime
86 
87 #endif // OHOS_ABILITY_RUNTIME_CONNECT_SERVER_MANAGER_H
88