• 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 <pthread.h>
20 #include <stdint.h>
21 #include "utils/macros.h"
22 
23 #ifdef WINDOWS_PLATFORM
24 #include <winsock2.h>
25 #include <windows.h>
26 using LIBHANDLE = HMODULE;
27 #define LIBFREE FreeLibrary
28 #define LIBSYM GetProcAddress
29 #else
30 #include <dlfcn.h>
31 using LIBHANDLE = void*;
32 #define LIBFREE dlclose
33 #define LIBSYM dlsym
34 #endif
35 
36 #define NAPI_PATH_MAX 4096
37 
38 class NativeValue;
39 
40 class NativeEngine;
41 
42 typedef NativeValue* (*RegisterCallback)(NativeEngine*, NativeValue*);
43 
44 struct NativeModule {
45     const char* name = nullptr;
46     const char* fileName = nullptr;
47     RegisterCallback registerCallback = nullptr;
48     int32_t version = 0;
49     unsigned int refCount = 0;
50     NativeModule* next = nullptr;
51     const char* jsCode = nullptr;
52     int32_t jsCodeLen = 0;
53     bool moduleLoaded = false;
54 };
55 
56 class NAPI_EXPORT NativeModuleManager {
57 public:
58     static NativeModuleManager* GetInstance();
59     static unsigned long Release();
60 
61     void Register(NativeModule* nativeModule);
62     void SetAppLibPath(const char* appLibPath);
63     NativeModule* LoadNativeModule(const char* moduleName, const char* path, bool isAppModule, bool internal = false,
64                                    bool isArk = false);
65 
66 private:
67     NativeModuleManager();
68     virtual ~NativeModuleManager();
69 
70     bool GetNativeModulePath(const char* moduleName, const bool isAppModule, char nativeModulePath[][NAPI_PATH_MAX],
71         int32_t pathLength) const;
72     NativeModule* FindNativeModuleByDisk(const char* moduleName, bool internal, const bool isAppModule, bool isArk);
73     NativeModule* FindNativeModuleByCache(const char* moduleName);
74     LIBHANDLE LoadModuleLibrary(const char* path, const bool isAppModule);
75     void CreateLdNamespace(const char* lib_ld_path);
76 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__)
77     Dl_namespace ns_;
78 #endif
79     NativeModule* firstNativeModule_;
80     NativeModule* lastNativeModule_;
81     char* appLibPath_;
82 
83     static NativeModuleManager instance_;
84     pthread_mutex_t mutex_;
85 };
86 
87 #endif /* FOUNDATION_ACE_NAPI_MODULE_MANAGER_NATIVE_MODULE_MANAGER_H */
88