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 using AddInstanceCallBack = void(*)(int32_t); 27 using SendInstanceMessageCallBack = void(*)(int32_t); 28 #ifdef APP_USE_ARM 29 constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so"; 30 #elif defined(APP_USE_X86_64) 31 constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so"; 32 #else 33 constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so"; 34 #endif 35 namespace OHOS::AbilityRuntime { 36 class ConnectServerManager final { 37 public: 38 static ConnectServerManager& Get(); 39 40 void StartConnectServer(const std::string& bundleName, int socketFd, bool isLocalAbstract); 41 void StopConnectServer(bool isCloseSo = true); 42 bool AddInstance(int32_t tid, int32_t instanceId, const std::string& instanceName = "PandaDebugger"); 43 void RemoveInstance(int32_t instanceId); 44 void SendInspector(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr); 45 void SendMessage(const std::string &message); 46 bool StoreInstanceMessage( 47 int32_t tid, int32_t instanceId, const std::string& instanceName = "PandaDebugger"); 48 void StoreDebuggerInfo(int32_t tid, void* vm, const panda::JSNApi::DebugOption& debugOption, 49 const DebuggerPostTask& debuggerPostTask, bool isDebugApp); 50 void SetSwitchCallback(const std::function<void(int32_t)> &createLayoutInfo, int32_t instanceId); 51 void SetProfilerCallBack(const std::function<void(bool)> &setStateProfilerStatus); 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 bool SetRecordCallback(const std::function<void(void)> &startRecordFunc, 57 const std::function<void(void)> &stopRecordFunc); 58 void SetRecordResults(const std::string &jsonArrayStr); 59 void RegisterConnectServerCallback(const ServerConnectCallback &connectServerCallback); 60 void RegisterSendInstanceMessageCallback(const SendInstanceMessageCallBack &sendInstanceMessageCallback); 61 void RegisterAddInstanceCallback(const AddInstanceCallBack &addInstanceCallback); 62 63 private: 64 ConnectServerManager() = default; 65 ~ConnectServerManager(); 66 67 void* handlerConnectServerSo_ = nullptr; 68 std::string bundleName_; 69 70 std::mutex mutex_; 71 static std::mutex instanceMutex_; 72 static std::mutex connectServerCallbackMutex_; 73 static std::mutex addInstanceCallbackMutex_; 74 static std::mutex sendInstanceMessageCallbackMutex_; 75 std::atomic<bool> isConnected_ = false; 76 std::unordered_map<int32_t, std::pair<std::string, int32_t>> instanceMap_; 77 std::vector<ServerConnectCallback> connectServerCallbacks_; 78 79 std::vector<AddInstanceCallBack> addInstanceCallbacks_; 80 std::vector<SendInstanceMessageCallBack> sendInstanceMessageCallbacks_; 81 ConnectServerManager(const ConnectServerManager&) = delete; 82 ConnectServerManager(ConnectServerManager&&) = delete; 83 ConnectServerManager& operator=(const ConnectServerManager&) = delete; 84 ConnectServerManager& operator=(ConnectServerManager&&) = delete; 85 void SendInstanceMessageCallback(const int32_t instanceId); 86 void AddInstanceCallback(const int32_t instanceId); 87 }; 88 } // namespace OHOS::AbilityRuntime 89 90 #endif // OHOS_ABILITY_RUNTIME_CONNECT_SERVER_MANAGER_H 91