• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unordered_map>
23 #include <unordered_set>
24 #include <vector>
25 #include <string>
26 #include <pthread.h>
27 
28 #include "module_load_checker.h"
29 #include "utils/macros.h"
30 
31 #ifdef WINDOWS_PLATFORM
32 #include <winsock2.h>
33 #include <windows.h>
34 using LIBHANDLE = HMODULE;
35 #define LIBFREE FreeLibrary
36 #define LIBSYM GetProcAddress
37 #else
38 #include <dlfcn.h>
39 using LIBHANDLE = void*;
40 #define LIBFREE dlclose
41 #define LIBSYM dlsym
42 #endif
43 
44 #define NAPI_PATH_MAX 4096
45 
46 class NativeValue;
47 
48 class NativeEngine;
49 
50 typedef NativeValue* (*RegisterCallback)(NativeEngine*, NativeValue*);
51 typedef void (*GetJSCodeCallback)(const char** buf, int* bufLen);
52 
53 struct NativeModule {
54     const char* name = nullptr;
55     const char* fileName = nullptr;
56     RegisterCallback registerCallback = nullptr;
57     GetJSCodeCallback getABCCode = nullptr;
58     GetJSCodeCallback getJSCode = nullptr;
59     int32_t version = 0;
60     uint32_t refCount = 0;
61     NativeModule* next = nullptr;
62     const char* jsCode = nullptr;
63     int32_t jsCodeLen = 0;
64     bool moduleLoaded = false;
65     bool isAppModule = false;
66 };
67 
68 class NAPI_EXPORT NativeModuleManager {
69 public:
70     static NativeModuleManager* GetInstance();
71     static uint64_t Release();
72 
73     void Register(NativeModule* nativeModule);
74     void SetAppLibPath(const std::string& moduleName, const std::vector<std::string>& appLibPath,
75                        const bool& isSystemApp = false);
76     NativeModule* LoadNativeModule(const char* moduleName, const char* path, bool isAppModule,
77                                    bool internal = false, const char* relativePath = "");
78     void SetNativeEngine(std::string moduleName, NativeEngine* nativeEngine);
79     bool UnloadNativeModule(const std::string &moduleKey);
80     const char* GetModuleFileName(const char* moduleName, bool isAppModule);
81 
82     /**
83      * @brief Set the path for searching napi dynamic libraries, only for the previewer.
84      *
85      * @param previewSearchPath the path for searching napi dynamic libraries
86      */
87     void SetPreviewSearchPath(const std::string& previewSearchPath);
88 
89     /**
90      * @brief Set the Module Load Checker delegate
91      *
92      * @param moduleCheckerDelegate The Module Load Checker delegate
93      */
94     void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate);
95 
96 private:
97     NativeModuleManager();
98     virtual ~NativeModuleManager();
99 
100     bool GetNativeModulePath(const char* moduleName, const char* path, const char* relativePath,
101         bool isAppModule, char nativeModulePath[][NAPI_PATH_MAX], int32_t pathLength);
102     NativeModule* FindNativeModuleByDisk(const char* moduleName, const char* path, const char* relativePath,
103         bool internal, const bool isAppModule);
104     NativeModule* FindNativeModuleByCache(const char* moduleName);
105     LIBHANDLE LoadModuleLibrary(std::string &moduleKey, const char* path, const char* pathKey, const bool isAppModule);
106     bool UnloadModuleLibrary(LIBHANDLE handle);
107     bool CloseModuleLibrary(LIBHANDLE handle);
108     void CreateLdNamespace(const std::string moduleName, const char* lib_ld_path, const bool& isSystemApp);
109     bool IsExistedPath(const char* pathKey) const;
110     void EmplaceModuleLib(std::string moduleKey, LIBHANDLE lib);
111     bool RemoveModuleLib(std::string moduleKey);
112     LIBHANDLE GetNativeModuleHandle(const std::string &moduleKey) const;
113     bool RemoveNativeModuleByCache(const std::string &moduleKey);
114     bool RemoveNativeModule(const std::string &moduleKey);
115 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
116     !defined(LINUX_PLATFORM)
117     void CreateSharedLibsSonames();
118 
119     char* sharedLibsSonames_ = nullptr;
120     std::map<std::string, Dl_namespace> nsMap_;
121 #endif
122 
123     std::mutex nativeModuleListMutex_;
124     NativeModule* firstNativeModule_ = nullptr;
125     NativeModule* lastNativeModule_ = nullptr;
126 
127     static NativeModuleManager *instance_;
128     pthread_mutex_t mutex_;
129     std::string prefix_;
130     bool isAppModule_ = false;
131 
132     std::mutex nativeEngineListMutex_;
133     std::map<std::string, NativeEngine*> nativeEngineList_;
134 
135     mutable std::mutex moduleLibMutex_;
136     std::map<std::string, const LIBHANDLE> moduleLibMap_;
137 
138     std::map<std::string, char*> appLibPathMap_;
139     std::string previewSearchPath_;
140     std::unique_ptr<ModuleLoadChecker> moduleLoadChecker_ = nullptr;
141 };
142 
143 #endif /* FOUNDATION_ACE_NAPI_MODULE_MANAGER_NATIVE_MODULE_MANAGER_H */
144