1 /* 2 * Copyright (c) 2022 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 #ifdef APP_USE_ARM 26 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib/libark_debugger.z.so"; 27 #elif defined(APP_USE_X86_64) 28 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/libark_debugger.z.so"; 29 #else 30 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/libark_debugger.z.so"; 31 #endif 32 namespace OHOS::AbilityRuntime { 33 class ConnectServerManager final { 34 public: 35 static ConnectServerManager& Get(); 36 37 void StartConnectServer(const std::string& bundleName, int socketFd, bool isLocalAbstract); 38 void StopConnectServer(bool isCloseSo = true); 39 bool AddInstance(int32_t instanceId, const std::string& instanceName = "PandaDebugger", bool isworker = true); 40 void RemoveInstance(int32_t instanceId); 41 void SendInspector(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr); 42 void SetLayoutInspectorCallback( 43 const std::function<void(int32_t)> &createLayoutInfo, const std::function<void(bool)> &setStatus); 44 std::function<void(int32_t)> GetLayoutInspectorCallback(); 45 bool StoreInstanceMessage( 46 int32_t instanceId, const std::string& instanceName = "PandaDebugger", bool isworker = false); 47 void StoreDebuggerInfo(int tid, void* vm, const panda::JSNApi::DebugOption& debugOption, 48 const DebuggerPostTask& debuggerPostTask, bool isDebugApp); 49 void SetConnectedCallback(); 50 bool SendInstanceMessage(int32_t instanceId, const std::string& instanceName, bool isworker = false); 51 void SendDebuggerInfo(bool needBreakPoint, bool isDebugApp); 52 void LoadConnectServerDebuggerSo(); 53 54 private: 55 ConnectServerManager() = default; 56 ~ConnectServerManager(); 57 58 void* handlerConnectServerSo_ = nullptr; 59 std::string bundleName_; 60 61 std::mutex mutex_; 62 static std::mutex instanceMutex_; 63 std::atomic<bool> isConnected_ = false; 64 std::unordered_map<int32_t, std::pair<std::string, bool>> instanceMap_; 65 std::function<void(int32_t)> createLayoutInfo_; 66 std::function<void(int32_t)> setStatus_; 67 ConnectServerManager(const ConnectServerManager&) = delete; 68 ConnectServerManager(ConnectServerManager&&) = delete; 69 ConnectServerManager& operator=(const ConnectServerManager&) = delete; 70 ConnectServerManager& operator=(ConnectServerManager&&) = delete; 71 }; 72 } // namespace OHOS::AbilityRuntime 73 74 #endif // OHOS_ABILITY_RUNTIME_CONNECT_SERVER_MANAGER_H