• 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 #include "native_module_manager.h"
17 
18 #include <dirent.h>
19 #include <mutex>
20 
21 #ifdef ENABLE_HITRACE
22 #include "hitrace_meter.h"
23 #endif
24 #include "native_engine/native_engine.h"
25 #include "securec.h"
26 #include "utils/log.h"
27 
28 namespace {
29 constexpr static int32_t NATIVE_PATH_NUMBER = 2;
30 } // namespace
31 
32 NativeModuleManager* NativeModuleManager::instance_ = NULL;
33 std::mutex g_instanceMutex;
34 
NativeModuleManager()35 NativeModuleManager::NativeModuleManager()
36 {
37     pthread_mutex_init(&mutex_, nullptr);
38     moduleLoadChecker_ = new ModuleLoadChecker();
39 }
40 
~NativeModuleManager()41 NativeModuleManager::~NativeModuleManager()
42 {
43     NativeModule* nativeModule = firstNativeModule_;
44     while (nativeModule != nullptr) {
45         nativeModule = nativeModule->next;
46         if (firstNativeModule_->isAppModule) {
47             delete[] firstNativeModule_->name;
48         }
49         delete firstNativeModule_;
50         firstNativeModule_ = nativeModule;
51     }
52     firstNativeModule_ = nullptr;
53     lastNativeModule_ = nullptr;
54     if (appLibPath_) {
55         delete[] appLibPath_;
56     }
57 
58     for (const auto& item : appLibPathMap_) {
59         delete[] item.second;
60     }
61     std::map<std::string, char*>().swap(appLibPathMap_);
62 
63     while (nativeEngineList_.size() > 0) {
64         NativeEngine* wraper = nativeEngineList_.begin()->second;
65         if (wraper != nullptr) {
66             delete wraper;
67             wraper = nullptr;
68         }
69         nativeEngineList_.erase(nativeEngineList_.begin());
70     }
71     if (moduleLoadChecker_) {
72         delete moduleLoadChecker_;
73     }
74     pthread_mutex_destroy(&mutex_);
75 }
76 
GetInstance()77 NativeModuleManager* NativeModuleManager::GetInstance()
78 {
79     if (instance_ == NULL) {
80         std::lock_guard<std::mutex> lock(g_instanceMutex);
81         if (instance_ == NULL) {
82             instance_ = new NativeModuleManager();
83         }
84     }
85     return instance_;
86 }
87 
SetNativeEngine(std::string moduleName,NativeEngine * nativeEngine)88 void NativeModuleManager::SetNativeEngine(std::string moduleName, NativeEngine* nativeEngine)
89 {
90     HILOG_DEBUG("%{public}s", __func__);
91     std::lock_guard<std::mutex> lock(nativeEngineListMutex_);
92     nativeEngineList_.emplace(moduleName, nativeEngine);
93 }
94 
GetModuleFileName(const char * moduleName,bool isAppModule)95 const char* NativeModuleManager::GetModuleFileName(const char* moduleName, bool isAppModule)
96 {
97     HILOG_INFO("%{public}s, start. moduleName:%{public}s", __func__, moduleName);
98     NativeModule* module = FindNativeModuleByCache(moduleName);
99     if (module != nullptr) {
100         char nativeModulePath[NATIVE_PATH_NUMBER][NAPI_PATH_MAX];
101         if (!GetNativeModulePath(moduleName, "default", isAppModule, nativeModulePath, NAPI_PATH_MAX)) {
102             HILOG_ERROR("%{public}s, get module filed", __func__);
103             return nullptr;
104         }
105         const char* loadPath = nativeModulePath[0];
106         return loadPath;
107     }
108     HILOG_ERROR("%{public}s, module is nullptr", __func__);
109     return nullptr;
110 }
111 
Register(NativeModule * nativeModule)112 void NativeModuleManager::Register(NativeModule* nativeModule)
113 {
114     if (nativeModule == nullptr) {
115         HILOG_ERROR("nativeModule value is null");
116         return;
117     }
118 
119     if (firstNativeModule_ == lastNativeModule_ && lastNativeModule_ == nullptr) {
120         firstNativeModule_ = new NativeModule();
121         if (firstNativeModule_ == nullptr) {
122             HILOG_ERROR("first NativeModule create failed");
123             return;
124         }
125         lastNativeModule_ = firstNativeModule_;
126     } else {
127         auto next = new NativeModule();
128         if (next == nullptr) {
129             HILOG_ERROR("next NativeModule create failed");
130             return;
131         }
132         if (lastNativeModule_) {
133             lastNativeModule_->next = next;
134             lastNativeModule_ = lastNativeModule_->next;
135         }
136     }
137 
138     char* moduleName;
139     if (isAppModule_) {
140         auto tmp = prefix_ + "/" + nativeModule->name;
141         moduleName = new char[NAPI_PATH_MAX];
142         errno_t err = EOK;
143         err = memset_s(moduleName, NAPI_PATH_MAX, 0, NAPI_PATH_MAX);
144         if (err != EOK) {
145             delete[] moduleName;
146             return;
147         }
148         err = strcpy_s(moduleName, NAPI_PATH_MAX, tmp.c_str());
149         if (err != EOK) {
150             delete[] moduleName;
151             return;
152         }
153     }
154 
155     if (lastNativeModule_) {
156         lastNativeModule_->version = nativeModule->version;
157         lastNativeModule_->fileName = nativeModule->fileName;
158         lastNativeModule_->isAppModule = isAppModule_;
159         lastNativeModule_->name = isAppModule_ ? moduleName : nativeModule->name;
160         lastNativeModule_->refCount = nativeModule->refCount;
161         lastNativeModule_->registerCallback = nativeModule->registerCallback;
162         lastNativeModule_->next = nullptr;
163 #ifdef IOS_PLATFORM
164         // For iOS, force make module loaded, should support `GetJSCode` later
165         lastNativeModule_->moduleLoaded = true;
166 #endif
167     }
168 }
169 
170 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
171     !defined(LINUX_PLATFORM)
FormatString()172 char* NativeModuleManager::FormatString()
173 {
174     const char* allowList[] = {
175         // bionic library
176         "libc.so",
177         "libdl.so",
178         "libm.so",
179         "libz.so",
180 	"libclang_rt.asan.so",
181         // z library
182         "libace_napi.z.so",
183         "libace_ndk.z.so",
184         "libbundle_ndk.z.so",
185         "libdeviceinfo_ndk.z.so",
186         "libEGL.so",
187         "libGLESv3.so",
188         "libhiappevent_ndk.z.so",
189         "libhilog_ndk.z.so",
190         "libhuks_ndk.z.so",
191         "libhukssdk.z.so",
192         "libnative_drawing.so",
193         "libnative_window.so",
194         "libnative_vsync.so",
195         "libOpenSLES.so",
196         "libpixelmap_ndk.z.so",
197         "librawfile.z.so",
198         "libuv.so",
199         "libhilog.so",
200         "libnative_image.so",
201         "libnative_media_adec.so",
202         "libnative_media_aenc.so",
203         "libnative_media_codecbase.so",
204         "libnative_media_core.so",
205         "libnative_media_vdec.so",
206         "libnative_media_venc.so",
207         // adaptor library
208         "libohosadaptor.so",
209     };
210 
211     size_t allowListLength = sizeof(allowList) / sizeof(char*);
212     int32_t sharedLibsSonamesLength = 1;
213     for (int32_t i = 0; i < allowListLength; i++) {
214         sharedLibsSonamesLength += strlen(allowList[i]) + 1;
215     }
216     char* sharedLibsSonames = new char[sharedLibsSonamesLength];
217     int32_t cursor = 0;
218     for (int32_t i = 0; i < allowListLength; i++) {
219         if (sprintf_s(sharedLibsSonames + cursor, sharedLibsSonamesLength - cursor, "%s:", allowList[i]) == -1) {
220             delete[] sharedLibsSonames;
221             return nullptr;
222         }
223         cursor += strlen(allowList[i]) + 1;
224     }
225     sharedLibsSonames[cursor] = '\0';
226     return sharedLibsSonames;
227 }
228 #endif
229 
CreateLdNamespace(const std::string moduleName,const char * lib_ld_path)230 void NativeModuleManager::CreateLdNamespace(const std::string moduleName, const char* lib_ld_path)
231 {
232 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
233     !defined(LINUX_PLATFORM)
234     Dl_namespace current_ns;
235     Dl_namespace ns;
236     std::string nsName = "arkUI_" + moduleName;
237     dlns_init(&ns, nsName.c_str());
238     dlns_get(nullptr, &current_ns);
239     dlns_create2(&ns, lib_ld_path, 0);
240     dlns_inherit(&ns, &current_ns, FormatString());
241     nsMap_[moduleName] = ns;
242     HILOG_INFO("CreateLdNamespace success, path: %{private}s", lib_ld_path);
243 #endif
244 }
245 
SetAppLibPath(const std::string & moduleName,const std::vector<std::string> & appLibPath)246 void NativeModuleManager::SetAppLibPath(const std::string& moduleName, const std::vector<std::string>& appLibPath)
247 {
248     char* tmp = new char[NAPI_PATH_MAX];
249     errno_t err = EOK;
250     err = memset_s(tmp, NAPI_PATH_MAX, 0, NAPI_PATH_MAX);
251     if (err != EOK) {
252         delete[] tmp;
253         return;
254     }
255 
256     std::string tmpPath = "";
257     for (int i = 0; i < appLibPath.size(); i++) {
258         if (appLibPath[i].empty()) {
259             continue;
260         }
261         tmpPath += appLibPath[i];
262         tmpPath += ":";
263     }
264     if (tmpPath.back() == ':') {
265         tmpPath.pop_back();
266     }
267 
268     err = strcpy_s(tmp, NAPI_PATH_MAX, tmpPath.c_str());
269     if (err != EOK) {
270         delete[] tmp;
271         return;
272     }
273 
274     if (appLibPathMap_[moduleName] != nullptr) {
275         delete[] appLibPathMap_[moduleName];
276     }
277 
278     appLibPathMap_[moduleName] = tmp;
279     CreateLdNamespace(moduleName, tmp);
280     HILOG_INFO("create ld namespace, path: %{private}s", appLibPathMap_[moduleName]);
281 }
282 
LoadNativeModule(const char * moduleName,const char * path,bool isAppModule,bool internal,bool isArk)283 NativeModule* NativeModuleManager::LoadNativeModule(
284     const char* moduleName, const char* path, bool isAppModule, bool internal, bool isArk)
285 {
286     if (moduleName == nullptr) {
287         HILOG_ERROR("moduleName value is null");
288         return nullptr;
289     }
290 
291 #ifdef ANDROID_PLATFORM
292     std::string strModule(moduleName);
293     std::string strCutName =
294         strModule.find(".") == std::string::npos ?
295             strModule :
296             strModule.substr(0, strModule.find(".")) + "_" + strModule.substr(strModule.find(".") + 1);
297     HILOG_INFO("strCutName value is %{public}s", strCutName.c_str());
298 #endif
299 
300     if (pthread_mutex_lock(&mutex_) != 0) {
301         HILOG_ERROR("pthread_mutex_lock is failed");
302         return nullptr;
303     }
304 
305 #ifdef ANDROID_PLATFORM
306     NativeModule* nativeModule = FindNativeModuleByCache(strCutName.c_str());
307 #else
308     std::string key(moduleName);
309     isAppModule_ = isAppModule;
310     if (isAppModule) {
311         prefix_ = "default";
312         if (path && IsExistedPath(path)) {
313             prefix_ = path;
314         }
315         key = prefix_ + '/' + moduleName;
316     }
317     NativeModule* nativeModule = FindNativeModuleByCache(key.c_str());
318 #endif
319 
320 #ifndef IOS_PLATFORM
321     if (nativeModule == nullptr) {
322 #ifdef ANDROID_PLATFORM
323         HILOG_INFO("not in cache: moduleName: %{public}s", strCutName.c_str());
324         nativeModule = FindNativeModuleByDisk(strCutName.c_str(), "default", internal, isAppModule, isArk);
325 #else
326         HILOG_INFO("not in cache: moduleName: %{public}s", moduleName);
327         nativeModule = FindNativeModuleByDisk(moduleName, prefix_.c_str(), internal, isAppModule, isArk);
328 #endif
329     }
330 #endif
331 
332     if (pthread_mutex_unlock(&mutex_) != 0) {
333         HILOG_ERROR("pthread_mutex_unlock is failed");
334         return nullptr;
335     }
336 
337     return nativeModule;
338 }
339 
GetNativeModulePath(const char * moduleName,const char * path,bool isAppModule,char nativeModulePath[][NAPI_PATH_MAX],int32_t pathLength)340 bool NativeModuleManager::GetNativeModulePath(const char* moduleName, const char* path, bool isAppModule,
341     char nativeModulePath[][NAPI_PATH_MAX], int32_t pathLength)
342 {
343 #ifdef WINDOWS_PLATFORM
344     const char* soPostfix = ".dll";
345     const char* sysPrefix = "./module";
346     const char* zfix = "";
347 #elif defined(MAC_PLATFORM)
348     const char* soPostfix = ".dylib";
349     const char* sysPrefix = "./module";
350     const char* zfix = "";
351 #elif defined(_ARM64_) || defined(SIMULATOR)
352     const char* soPostfix = ".so";
353     const char* sysPrefix = "/system/lib64/module";
354     const char* zfix = ".z";
355 #elif defined(LINUX_PLATFORM)
356     const char* soPostfix = ".so";
357     const char* sysPrefix = "./module";
358     const char* zfix = "";
359 #else
360     const char* soPostfix = ".so";
361     const char* sysPrefix = "/system/lib/module";
362     const char* zfix = ".z";
363 #endif
364 
365 #ifdef ANDROID_PLATFORM
366     isAppModule = true;
367 #endif
368     int32_t lengthOfModuleName = strlen(moduleName);
369     char dupModuleName[NAPI_PATH_MAX] = { 0 };
370     if (strcpy_s(dupModuleName, NAPI_PATH_MAX, moduleName) != 0) {
371         HILOG_ERROR("strcpy moduleName failed");
372         return false;
373     }
374 
375     const char* prefix = nullptr;
376     if (isAppModule && IsExistedPath(path)) {
377         prefix = appLibPathMap_[path];
378 #ifdef ANDROID_PLATFORM
379         for (int32_t i = 0; i < lengthOfModuleName; i++) {
380             dupModuleName[i] = tolower(dupModuleName[i]);
381         }
382 #endif
383     } else {
384         prefix = sysPrefix;
385         for (int32_t i = 0; i < lengthOfModuleName; i++) {
386             dupModuleName[i] = tolower(dupModuleName[i]);
387         }
388     }
389 
390     int32_t lengthOfPostfix = strlen(soPostfix);
391     if ((lengthOfModuleName > lengthOfPostfix) &&
392         (strcmp(dupModuleName + lengthOfModuleName - lengthOfPostfix, soPostfix) == 0)) {
393         if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s", prefix, dupModuleName) == -1) {
394             return false;
395         }
396         return true;
397     }
398 
399     char* lastDot = strrchr(dupModuleName, '.');
400     if (lastDot == nullptr) {
401         if (!isAppModule || !IsExistedPath(path)) {
402             if (sprintf_s(nativeModulePath[0], pathLength, "%s/lib%s%s%s",
403                 prefix, dupModuleName, zfix, soPostfix) == -1) {
404                 return false;
405             }
406             if (sprintf_s(nativeModulePath[1], pathLength, "%s/lib%s_napi%s%s",
407                 prefix, dupModuleName, zfix, soPostfix) == -1) {
408                 return false;
409             }
410         } else {
411 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
412     !defined(LINUX_PLATFORM)
413             if (sprintf_s(nativeModulePath[0], pathLength, "lib%s%s", dupModuleName, soPostfix) == -1) {
414                 return false;
415             }
416 #else
417             if (sprintf_s(nativeModulePath[0], pathLength, "%s/lib%s%s", prefix, dupModuleName, soPostfix) == -1) {
418                 return false;
419             }
420 #endif
421 #ifdef ANDROID_PLATFORM
422             if (sprintf_s(nativeModulePath[1], pathLength, "%s/lib%s%s", prefix, moduleName, soPostfix) == -1) {
423                 return false;
424             }
425 #endif
426         }
427     } else {
428         char* afterDot = lastDot + 1;
429         if (*afterDot == '\0') {
430             return false;
431         }
432         *lastDot = '\0';
433         lengthOfModuleName = strlen(dupModuleName);
434         for (int32_t i = 0; i < lengthOfModuleName; i++) {
435             if (*(dupModuleName + i) == '.') {
436                 *(dupModuleName + i) = '/';
437             }
438         }
439         if (!isAppModule || !IsExistedPath(path)) {
440             if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s/lib%s%s%s",
441                 prefix, dupModuleName, afterDot, zfix, soPostfix) == -1) {
442                 return false;
443             }
444             if (sprintf_s(nativeModulePath[1], pathLength, "%s/%s/lib%s_napi%s%s",
445                 prefix, dupModuleName, afterDot, zfix, soPostfix) == -1) {
446                 return false;
447             }
448         } else {
449 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
450     !defined(LINUX_PLATFORM)
451             if (sprintf_s(nativeModulePath[0], pathLength, "lib%s%s", afterDot, soPostfix) == -1) {
452                 return false;
453             }
454 #else
455             if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s/lib%s%s",
456                 prefix, dupModuleName, afterDot, soPostfix) == -1) {
457                 return false;
458             }
459 #endif
460 #ifdef ANDROID_PLATFORM
461             if (sprintf_s(nativeModulePath[1], pathLength, "%s/%s/lib%s%s",
462                 prefix, moduleName, afterDot, soPostfix) == -1) {
463                 return false;
464             }
465 #endif
466         }
467     }
468     return true;
469 }
470 
LoadModuleLibrary(const char * path,const char * pathKey,const bool isAppModule)471 LIBHANDLE NativeModuleManager::LoadModuleLibrary(const char* path, const char* pathKey, const bool isAppModule)
472 {
473     if (strlen(path) == 0) {
474         HILOG_ERROR("primary module path is empty");
475         return nullptr;
476     }
477     LIBHANDLE lib = nullptr;
478 #ifdef ENABLE_HITRACE
479     StartTrace(HITRACE_TAG_ACE, path);
480 #endif
481 #if defined(WINDOWS_PLATFORM)
482     lib = LoadLibrary(path);
483     if (lib == nullptr) {
484         HILOG_WARN("LoadLibrary failed, error: %{public}d", GetLastError());
485     }
486 #elif defined(MAC_PLATFORM) || defined(__BIONIC__) || defined(LINUX_PLATFORM)
487     lib = dlopen(path, RTLD_LAZY);
488     if (lib == nullptr) {
489         HILOG_WARN("dlopen failed: %{public}s", dlerror());
490     }
491 
492 #elif defined(IOS_PLATFORM)
493     lib = nullptr;
494 #else
495     if (isAppModule && IsExistedPath(pathKey)) {
496         Dl_namespace ns = nsMap_[pathKey];
497         lib = dlopen_ns(&ns, path, RTLD_LAZY);
498     } else {
499         lib = dlopen(path, RTLD_LAZY);
500     }
501     if (lib == nullptr) {
502         HILOG_WARN("dlopen failed: %{public}s", dlerror());
503     }
504 #endif
505 #ifdef ENABLE_HITRACE
506     FinishTrace(HITRACE_TAG_ACE);
507 #endif
508     return lib;
509 }
510 
511 using NAPIGetJSCode = void (*)(const char** buf, int* bufLen);
FindNativeModuleByDisk(const char * moduleName,const char * path,bool internal,const bool isAppModule,bool isArk)512 NativeModule* NativeModuleManager::FindNativeModuleByDisk(
513     const char* moduleName, const char* path, bool internal, const bool isAppModule, bool isArk)
514 {
515     char nativeModulePath[NATIVE_PATH_NUMBER][NAPI_PATH_MAX];
516     nativeModulePath[0][0] = 0;
517     nativeModulePath[1][0] = 0;
518     if (!GetNativeModulePath(moduleName, path, isAppModule, nativeModulePath, NAPI_PATH_MAX)) {
519         HILOG_WARN("get module failed, moduleName = %{public}s", moduleName);
520         return nullptr;
521     }
522     if (!moduleLoadChecker_) {
523         return nullptr;
524     }
525     if (!moduleLoadChecker_->CheckModuleLoadable(moduleName)) {
526         HILOG_ERROR("module %{public}s is not allow to load", moduleName);
527         return nullptr;
528     }
529 
530     // load primary module path first
531     char* loadPath = nativeModulePath[0];
532     HILOG_DEBUG("get primary module path: %{public}s", loadPath);
533     LIBHANDLE lib = LoadModuleLibrary(loadPath, path, isAppModule);
534     if (lib == nullptr) {
535         loadPath = nativeModulePath[1];
536         HILOG_DEBUG("primary module path load failed, try to load secondary module path: %{public}s", loadPath);
537         lib = LoadModuleLibrary(loadPath, path, isAppModule);
538         if (lib == nullptr) {
539             HILOG_ERROR("primary and secondary module path load failed %{public}s", moduleName);
540             return nullptr;
541         }
542     }
543 
544     std::string moduleKey(moduleName);
545     if (isAppModule) {
546         moduleKey = path;
547         moduleKey = moduleKey + '/' + moduleName;
548     }
549 
550     if (lastNativeModule_ && strcmp(lastNativeModule_->name, moduleKey.c_str())) {
551         HILOG_WARN(
552             "moduleName '%{public}s' does not match plugin's name '%{public}s'", moduleName, lastNativeModule_->name);
553     }
554 
555     if (!internal) {
556         char symbol[NAPI_PATH_MAX] = { 0 };
557         if (!isArk) {
558             if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetJSCode", moduleKey.c_str()) == -1) {
559                 LIBFREE(lib);
560                 return nullptr;
561             }
562         } else {
563             if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetABCCode", moduleKey.c_str()) == -1) {
564                 LIBFREE(lib);
565                 return nullptr;
566             }
567         }
568 
569         // replace '.' with '_'
570         for (char* p = strchr(symbol, '.'); p != nullptr; p = strchr(p + 1, '.')) {
571             *p = '_';
572         }
573 
574         auto getJSCode = reinterpret_cast<NAPIGetJSCode>(LIBSYM(lib, symbol));
575         if (getJSCode != nullptr) {
576             const char* buf = nullptr;
577             int bufLen = 0;
578             getJSCode(&buf, &bufLen);
579             if (lastNativeModule_ != nullptr) {
580                 HILOG_DEBUG("get js code from module: bufLen: %{public}d", bufLen);
581                 lastNativeModule_->jsCode = buf;
582                 lastNativeModule_->jsCodeLen = bufLen;
583             }
584         } else {
585             HILOG_DEBUG("ignore: no %{public}s in %{public}s", symbol, loadPath);
586         }
587     }
588     if (lastNativeModule_) {
589         lastNativeModule_->moduleLoaded = true;
590     }
591     return lastNativeModule_;
592 }
593 
FindNativeModuleByCache(const char * moduleName)594 NativeModule* NativeModuleManager::FindNativeModuleByCache(const char* moduleName)
595 {
596     NativeModule* result = nullptr;
597     NativeModule* preNativeModule = nullptr;
598     for (NativeModule* temp = firstNativeModule_; temp != nullptr; temp = temp->next) {
599         if (!strcasecmp(temp->name, moduleName)) {
600             if (strcmp(temp->name, moduleName)) {
601                 HILOG_WARN("moduleName '%{public}s' does not match plugin's name '%{public}s'", moduleName, temp->name);
602             }
603             result = temp;
604             break;
605         }
606         preNativeModule = temp;
607     }
608 
609     if (result && !result->moduleLoaded) {
610         if (result == lastNativeModule_) {
611             return nullptr;
612         }
613         if (preNativeModule) {
614             preNativeModule->next = result->next;
615         } else {
616             firstNativeModule_ = firstNativeModule_->next;
617         }
618         result->next = nullptr;
619         lastNativeModule_->next = result;
620         lastNativeModule_ = result;
621         return nullptr;
622     }
623     return result;
624 }
625 
IsExistedPath(const char * pathKey) const626 bool NativeModuleManager::IsExistedPath(const char* pathKey) const
627 {
628     return pathKey && appLibPathMap_.find(pathKey) != appLibPathMap_.end();
629 }
630 
SetModuleBlocklist(std::unordered_map<int32_t,std::unordered_set<std::string>> && blocklist)631 void NativeModuleManager::SetModuleBlocklist(
632     std::unordered_map<int32_t, std::unordered_set<std::string>>&& blocklist)
633 {
634     if (!moduleLoadChecker_) {
635         return;
636     }
637     moduleLoadChecker_->SetModuleBlocklist(std::forward<decltype(blocklist)>(blocklist));
638 }
639 
SetProcessExtensionType(int32_t extensionType)640 void NativeModuleManager::SetProcessExtensionType(int32_t extensionType)
641 {
642     if (!moduleLoadChecker_) {
643         return;
644     }
645     moduleLoadChecker_->SetProcessExtensionType(extensionType);
646 }
647 
GetProcessExtensionType()648 int32_t NativeModuleManager::GetProcessExtensionType()
649 {
650     if (!moduleLoadChecker_) {
651         return EXTENSION_TYPE_UNSPECIFIED;
652     }
653     return moduleLoadChecker_->GetProcessExtensionType();
654 }
655