1 /* 2 * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_MODULE_MANAGER_NATIVE_MODULE_MANAGER_H 17 #define FOUNDATION_ACE_NAPI_MODULE_MANAGER_NATIVE_MODULE_MANAGER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <mutex> 22 #include <vector> 23 #include <string> 24 #include <pthread.h> 25 26 #include "module_load_checker.h" 27 #include "utils/macros.h" 28 29 #ifdef WINDOWS_PLATFORM 30 #include <winsock2.h> 31 #include <windows.h> 32 using LIBHANDLE = HMODULE; 33 #define LIBFREE FreeLibrary 34 #define LIBSYM GetProcAddress 35 #else 36 #include <dlfcn.h> 37 using LIBHANDLE = void*; 38 #define LIBFREE dlclose 39 #define LIBSYM dlsym 40 #endif 41 42 #define NAPI_PATH_MAX 4096 43 44 class NativeValue; 45 46 class NativeEngine; 47 48 typedef NativeValue* (*RegisterCallback)(NativeEngine*, NativeValue*); 49 50 struct NativeModule { 51 const char* name = nullptr; 52 const char* fileName = nullptr; 53 RegisterCallback registerCallback = nullptr; 54 int32_t version = 0; 55 uint32_t refCount = 0; 56 NativeModule* next = nullptr; 57 const char* jsCode = nullptr; 58 int32_t jsCodeLen = 0; 59 bool moduleLoaded = false; 60 bool isAppModule = false; 61 }; 62 63 class NAPI_EXPORT NativeModuleManager { 64 public: 65 static NativeModuleManager* GetInstance(); 66 static uint64_t Release(); 67 68 void Register(NativeModule* nativeModule); 69 void SetAppLibPath(const std::string& moduleName, const std::vector<std::string>& appLibPath); 70 NativeModule* LoadNativeModule(const char* moduleName, const char* path, bool isAppModule, bool internal = false, 71 bool isArk = false); 72 void SetNativeEngine(std::string moduleName, NativeEngine* nativeEngine); 73 const char* GetModuleFileName(const char* moduleName, bool isAppModule); 74 75 /** 76 * @brief Set the Module Blacklist 77 * 78 * @param blocklist the blocklist 79 */ 80 void SetModuleBlocklist(std::unordered_map<int32_t, std::unordered_set<std::string>>&& blocklist); 81 82 /** 83 * @brief Set the Process Extension Type 84 * 85 * @param extensionType extension type 86 */ 87 void SetProcessExtensionType(int32_t extensionType); 88 89 /** 90 * @brief Get the Process Extension Type 91 * 92 * @return Process extension type 93 */ 94 int32_t GetProcessExtensionType(); 95 96 private: 97 NativeModuleManager(); 98 virtual ~NativeModuleManager(); 99 100 bool GetNativeModulePath(const char* moduleName, const char* pathKey, bool isAppModule, 101 char nativeModulePath[][NAPI_PATH_MAX], int32_t pathLength); 102 NativeModule* FindNativeModuleByDisk(const char* moduleName, const char* pathKey, bool internal, 103 const bool isAppModule, bool isArk); 104 NativeModule* FindNativeModuleByCache(const char* moduleName); 105 LIBHANDLE LoadModuleLibrary(const char* path, const char* pathKey, const bool isAppModule); 106 void CreateLdNamespace(const std::string moduleName, const char* lib_ld_path); 107 bool IsExistedPath(const char* pathKey) const; 108 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \ 109 !defined(LINUX_PLATFORM) 110 char* FormatString(); 111 std::map<std::string, Dl_namespace> nsMap_; 112 #endif 113 NativeModule* firstNativeModule_ = nullptr; 114 NativeModule* lastNativeModule_ = nullptr; 115 char* appLibPath_ = nullptr; 116 117 static NativeModuleManager *instance_; 118 pthread_mutex_t mutex_; 119 std::string prefix_; 120 bool isAppModule_ = false; 121 122 std::mutex nativeEngineListMutex_; 123 std::map<std::string, NativeEngine*> nativeEngineList_; 124 std::map<std::string, char*> appLibPathMap_; 125 ModuleLoadChecker* moduleLoadChecker_ = nullptr; 126 }; 127 128 #endif /* FOUNDATION_ACE_NAPI_MODULE_MANAGER_NATIVE_MODULE_MANAGER_H */ 129