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/platformsdk/libark_inspector.z.so"; 27 #elif defined(APP_USE_X86_64) 28 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/platformsdk/libark_inspector.z.so"; 29 #else 30 constexpr char ARK_DEBUGGER_LIB_PATH[] = "/system/lib64/platformsdk/libark_inspector.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 tid, int32_t instanceId, const std::string& instanceName = "PandaDebugger"); 40 void RemoveInstance(int32_t instanceId); 41 void SendInspector(const std::string& jsonTreeStr, const std::string& jsonSnapshotStr); 42 void SendArkUIStateProfilerMessage(const std::string &message); 43 void SetLayoutInspectorCallback( 44 const std::function<void(int32_t)> &createLayoutInfo, const std::function<void(bool)> &setStatus); 45 void SetStateProfilerCallback(const std::function<void(bool)> &setArkUIStateProfilerStatus); 46 std::function<void(int32_t)> GetLayoutInspectorCallback(); 47 bool StoreInstanceMessage( 48 int32_t tid, int32_t instanceId, const std::string& instanceName = "PandaDebugger"); 49 void StoreDebuggerInfo(int32_t tid, void* vm, const panda::JSNApi::DebugOption& debugOption, 50 const DebuggerPostTask& debuggerPostTask, bool isDebugApp); 51 void SetConnectedCallback(); 52 bool SendInstanceMessage(int32_t tid, int32_t instanceId, const std::string& instanceName); 53 void SendDebuggerInfo(bool needBreakPoint, bool isDebugApp); 54 void LoadConnectServerDebuggerSo(); 55 DebuggerPostTask GetDebuggerPostTask(int32_t tid); 56 void SetSwitchCallback(int32_t instanceId); 57 void SetProfilerCallBack(); 58 59 private: 60 ConnectServerManager() = default; 61 ~ConnectServerManager(); 62 63 void* handlerConnectServerSo_ = nullptr; 64 std::string bundleName_; 65 66 std::mutex mutex_; 67 static std::mutex instanceMutex_; 68 std::atomic<bool> isConnected_ = false; 69 std::unordered_map<int32_t, std::pair<std::string, int32_t>> instanceMap_; 70 std::function<void(int32_t)> createLayoutInfo_; 71 std::function<void(int32_t)> setStatus_; 72 std::function<void(int32_t)> setArkUIStateProfilerStatus_; 73 ConnectServerManager(const ConnectServerManager&) = delete; 74 ConnectServerManager(ConnectServerManager&&) = delete; 75 ConnectServerManager& operator=(const ConnectServerManager&) = delete; 76 ConnectServerManager& operator=(ConnectServerManager&&) = delete; 77 }; 78 } // namespace OHOS::AbilityRuntime 79 80 #endif // OHOS_ABILITY_RUNTIME_CONNECT_SERVER_MANAGER_H 81