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_buffer.so",
195 "libnative_vsync.so",
196 "libOpenSLES.so",
197 "libpixelmap_ndk.z.so",
198 "librawfile.z.so",
199 "libuv.so",
200 "libhilog.so",
201 "libnative_image.so",
202 "libnative_media_adec.so",
203 "libnative_media_aenc.so",
204 "libnative_media_codecbase.so",
205 "libnative_media_core.so",
206 "libnative_media_vdec.so",
207 "libnative_media_venc.so",
208 // adaptor library
209 "libohosadaptor.so",
210 };
211
212 size_t allowListLength = sizeof(allowList) / sizeof(char*);
213 int32_t sharedLibsSonamesLength = 1;
214 for (int32_t i = 0; i < allowListLength; i++) {
215 sharedLibsSonamesLength += strlen(allowList[i]) + 1;
216 }
217 char* sharedLibsSonames = new char[sharedLibsSonamesLength];
218 int32_t cursor = 0;
219 for (int32_t i = 0; i < allowListLength; i++) {
220 if (sprintf_s(sharedLibsSonames + cursor, sharedLibsSonamesLength - cursor, "%s:", allowList[i]) == -1) {
221 delete[] sharedLibsSonames;
222 return nullptr;
223 }
224 cursor += strlen(allowList[i]) + 1;
225 }
226 sharedLibsSonames[cursor] = '\0';
227 return sharedLibsSonames;
228 }
229 #endif
230
CreateLdNamespace(const std::string moduleName,const char * lib_ld_path)231 void NativeModuleManager::CreateLdNamespace(const std::string moduleName, const char* lib_ld_path)
232 {
233 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
234 !defined(LINUX_PLATFORM)
235 Dl_namespace current_ns;
236 Dl_namespace ns;
237 std::string nsName = "arkUI_" + moduleName;
238 dlns_init(&ns, nsName.c_str());
239 dlns_get(nullptr, ¤t_ns);
240 dlns_create2(&ns, lib_ld_path, 0);
241 dlns_inherit(&ns, ¤t_ns, FormatString());
242 nsMap_[moduleName] = ns;
243 HILOG_INFO("CreateLdNamespace success, path: %{private}s", lib_ld_path);
244 #endif
245 }
246
SetAppLibPath(const std::string & moduleName,const std::vector<std::string> & appLibPath)247 void NativeModuleManager::SetAppLibPath(const std::string& moduleName, const std::vector<std::string>& appLibPath)
248 {
249 char* tmp = new char[NAPI_PATH_MAX];
250 errno_t err = EOK;
251 err = memset_s(tmp, NAPI_PATH_MAX, 0, NAPI_PATH_MAX);
252 if (err != EOK) {
253 delete[] tmp;
254 return;
255 }
256
257 std::string tmpPath = "";
258 for (int i = 0; i < appLibPath.size(); i++) {
259 if (appLibPath[i].empty()) {
260 continue;
261 }
262 tmpPath += appLibPath[i];
263 tmpPath += ":";
264 }
265 if (tmpPath.back() == ':') {
266 tmpPath.pop_back();
267 }
268
269 err = strcpy_s(tmp, NAPI_PATH_MAX, tmpPath.c_str());
270 if (err != EOK) {
271 delete[] tmp;
272 return;
273 }
274
275 if (appLibPathMap_[moduleName] != nullptr) {
276 delete[] appLibPathMap_[moduleName];
277 }
278
279 appLibPathMap_[moduleName] = tmp;
280 CreateLdNamespace(moduleName, tmp);
281 HILOG_INFO("create ld namespace, path: %{private}s", appLibPathMap_[moduleName]);
282 }
283
LoadNativeModule(const char * moduleName,const char * path,bool isAppModule,bool internal,bool isArk)284 NativeModule* NativeModuleManager::LoadNativeModule(
285 const char* moduleName, const char* path, bool isAppModule, bool internal, bool isArk)
286 {
287 if (moduleName == nullptr) {
288 HILOG_ERROR("moduleName value is null");
289 return nullptr;
290 }
291
292 #ifdef ANDROID_PLATFORM
293 std::string strModule(moduleName);
294 std::string strCutName =
295 strModule.find(".") == std::string::npos ?
296 strModule :
297 strModule.substr(0, strModule.find(".")) + "_" + strModule.substr(strModule.find(".") + 1);
298 HILOG_INFO("strCutName value is %{public}s", strCutName.c_str());
299 #endif
300
301 if (pthread_mutex_lock(&mutex_) != 0) {
302 HILOG_ERROR("pthread_mutex_lock is failed");
303 return nullptr;
304 }
305
306 #ifdef ANDROID_PLATFORM
307 NativeModule* nativeModule = FindNativeModuleByCache(strCutName.c_str());
308 #else
309 std::string key(moduleName);
310 isAppModule_ = isAppModule;
311 if (isAppModule) {
312 prefix_ = "default";
313 if (path && IsExistedPath(path)) {
314 prefix_ = path;
315 }
316 key = prefix_ + '/' + moduleName;
317 }
318 NativeModule* nativeModule = FindNativeModuleByCache(key.c_str());
319 #endif
320
321 #ifndef IOS_PLATFORM
322 if (nativeModule == nullptr) {
323 #ifdef ANDROID_PLATFORM
324 HILOG_INFO("not in cache: moduleName: %{public}s", strCutName.c_str());
325 nativeModule = FindNativeModuleByDisk(strCutName.c_str(), "default", internal, isAppModule, isArk);
326 #else
327 HILOG_INFO("not in cache: moduleName: %{public}s", moduleName);
328 nativeModule = FindNativeModuleByDisk(moduleName, prefix_.c_str(), internal, isAppModule, isArk);
329 #endif
330 }
331 #endif
332
333 if (pthread_mutex_unlock(&mutex_) != 0) {
334 HILOG_ERROR("pthread_mutex_unlock is failed");
335 return nullptr;
336 }
337
338 return nativeModule;
339 }
340
GetNativeModulePath(const char * moduleName,const char * path,bool isAppModule,char nativeModulePath[][NAPI_PATH_MAX],int32_t pathLength)341 bool NativeModuleManager::GetNativeModulePath(const char* moduleName, const char* path, bool isAppModule,
342 char nativeModulePath[][NAPI_PATH_MAX], int32_t pathLength)
343 {
344 #ifdef WINDOWS_PLATFORM
345 const char* soPostfix = ".dll";
346 const char* sysPrefix = "./module";
347 const char* zfix = "";
348 #elif defined(MAC_PLATFORM)
349 const char* soPostfix = ".dylib";
350 const char* sysPrefix = "./module";
351 const char* zfix = "";
352 #elif defined(_ARM64_) || defined(SIMULATOR)
353 const char* soPostfix = ".so";
354 const char* sysPrefix = "/system/lib64/module";
355 const char* zfix = ".z";
356 #elif defined(LINUX_PLATFORM)
357 const char* soPostfix = ".so";
358 const char* sysPrefix = "./module";
359 const char* zfix = "";
360 #else
361 const char* soPostfix = ".so";
362 const char* sysPrefix = "/system/lib/module";
363 const char* zfix = ".z";
364 #endif
365
366 #ifdef ANDROID_PLATFORM
367 isAppModule = true;
368 #endif
369 int32_t lengthOfModuleName = strlen(moduleName);
370 char dupModuleName[NAPI_PATH_MAX] = { 0 };
371 if (strcpy_s(dupModuleName, NAPI_PATH_MAX, moduleName) != 0) {
372 HILOG_ERROR("strcpy moduleName failed");
373 return false;
374 }
375
376 const char* prefix = nullptr;
377 if (isAppModule && IsExistedPath(path)) {
378 prefix = appLibPathMap_[path];
379 #ifdef ANDROID_PLATFORM
380 for (int32_t i = 0; i < lengthOfModuleName; i++) {
381 dupModuleName[i] = tolower(dupModuleName[i]);
382 }
383 #endif
384 } else {
385 prefix = sysPrefix;
386 for (int32_t i = 0; i < lengthOfModuleName; i++) {
387 dupModuleName[i] = tolower(dupModuleName[i]);
388 }
389 }
390
391 int32_t lengthOfPostfix = strlen(soPostfix);
392 if ((lengthOfModuleName > lengthOfPostfix) &&
393 (strcmp(dupModuleName + lengthOfModuleName - lengthOfPostfix, soPostfix) == 0)) {
394 if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s", prefix, dupModuleName) == -1) {
395 return false;
396 }
397 return true;
398 }
399
400 char* lastDot = strrchr(dupModuleName, '.');
401 if (lastDot == nullptr) {
402 if (!isAppModule || !IsExistedPath(path)) {
403 if (sprintf_s(nativeModulePath[0], pathLength, "%s/lib%s%s%s",
404 prefix, dupModuleName, zfix, soPostfix) == -1) {
405 return false;
406 }
407 if (sprintf_s(nativeModulePath[1], pathLength, "%s/lib%s_napi%s%s",
408 prefix, dupModuleName, zfix, soPostfix) == -1) {
409 return false;
410 }
411 } else {
412 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
413 !defined(LINUX_PLATFORM)
414 if (sprintf_s(nativeModulePath[0], pathLength, "lib%s%s", dupModuleName, soPostfix) == -1) {
415 return false;
416 }
417 #else
418 if (sprintf_s(nativeModulePath[0], pathLength, "%s/lib%s%s", prefix, dupModuleName, soPostfix) == -1) {
419 return false;
420 }
421 #endif
422 #ifdef ANDROID_PLATFORM
423 if (sprintf_s(nativeModulePath[1], pathLength, "%s/lib%s%s", prefix, moduleName, soPostfix) == -1) {
424 return false;
425 }
426 #endif
427 }
428 } else {
429 char* afterDot = lastDot + 1;
430 if (*afterDot == '\0') {
431 return false;
432 }
433 *lastDot = '\0';
434 lengthOfModuleName = strlen(dupModuleName);
435 for (int32_t i = 0; i < lengthOfModuleName; i++) {
436 if (*(dupModuleName + i) == '.') {
437 *(dupModuleName + i) = '/';
438 }
439 }
440 if (!isAppModule || !IsExistedPath(path)) {
441 if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s/lib%s%s%s",
442 prefix, dupModuleName, afterDot, zfix, soPostfix) == -1) {
443 return false;
444 }
445 if (sprintf_s(nativeModulePath[1], pathLength, "%s/%s/lib%s_napi%s%s",
446 prefix, dupModuleName, afterDot, zfix, soPostfix) == -1) {
447 return false;
448 }
449 } else {
450 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \
451 !defined(LINUX_PLATFORM)
452 if (sprintf_s(nativeModulePath[0], pathLength, "lib%s%s", afterDot, soPostfix) == -1) {
453 return false;
454 }
455 #else
456 if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s/lib%s%s",
457 prefix, dupModuleName, afterDot, soPostfix) == -1) {
458 return false;
459 }
460 #endif
461 #ifdef ANDROID_PLATFORM
462 if (sprintf_s(nativeModulePath[1], pathLength, "%s/%s/lib%s%s",
463 prefix, moduleName, afterDot, soPostfix) == -1) {
464 return false;
465 }
466 #endif
467 }
468 }
469 return true;
470 }
471
LoadModuleLibrary(const char * path,const char * pathKey,const bool isAppModule)472 LIBHANDLE NativeModuleManager::LoadModuleLibrary(const char* path, const char* pathKey, const bool isAppModule)
473 {
474 if (strlen(path) == 0) {
475 HILOG_ERROR("primary module path is empty");
476 return nullptr;
477 }
478 LIBHANDLE lib = nullptr;
479 #ifdef ENABLE_HITRACE
480 StartTrace(HITRACE_TAG_ACE, path);
481 #endif
482 #if defined(WINDOWS_PLATFORM)
483 lib = LoadLibrary(path);
484 if (lib == nullptr) {
485 HILOG_WARN("LoadLibrary failed, error: %{public}d", GetLastError());
486 }
487 #elif defined(MAC_PLATFORM) || defined(__BIONIC__) || defined(LINUX_PLATFORM)
488 lib = dlopen(path, RTLD_LAZY);
489 if (lib == nullptr) {
490 HILOG_WARN("dlopen failed: %{public}s", dlerror());
491 }
492
493 #elif defined(IOS_PLATFORM)
494 lib = nullptr;
495 #else
496 if (isAppModule && IsExistedPath(pathKey)) {
497 Dl_namespace ns = nsMap_[pathKey];
498 lib = dlopen_ns(&ns, path, RTLD_LAZY);
499 } else {
500 lib = dlopen(path, RTLD_LAZY);
501 }
502 if (lib == nullptr) {
503 HILOG_WARN("dlopen failed: %{public}s", dlerror());
504 }
505 #endif
506 #ifdef ENABLE_HITRACE
507 FinishTrace(HITRACE_TAG_ACE);
508 #endif
509 return lib;
510 }
511
512 using NAPIGetJSCode = void (*)(const char** buf, int* bufLen);
FindNativeModuleByDisk(const char * moduleName,const char * path,bool internal,const bool isAppModule,bool isArk)513 NativeModule* NativeModuleManager::FindNativeModuleByDisk(
514 const char* moduleName, const char* path, bool internal, const bool isAppModule, bool isArk)
515 {
516 char nativeModulePath[NATIVE_PATH_NUMBER][NAPI_PATH_MAX];
517 nativeModulePath[0][0] = 0;
518 nativeModulePath[1][0] = 0;
519 if (!GetNativeModulePath(moduleName, path, isAppModule, nativeModulePath, NAPI_PATH_MAX)) {
520 HILOG_WARN("get module failed, moduleName = %{public}s", moduleName);
521 return nullptr;
522 }
523 if (!moduleLoadChecker_) {
524 return nullptr;
525 }
526 if (!moduleLoadChecker_->CheckModuleLoadable(moduleName)) {
527 HILOG_ERROR("module %{public}s is not allow to load", moduleName);
528 return nullptr;
529 }
530
531 // load primary module path first
532 char* loadPath = nativeModulePath[0];
533 HILOG_DEBUG("get primary module path: %{public}s", loadPath);
534 LIBHANDLE lib = LoadModuleLibrary(loadPath, path, isAppModule);
535 if (lib == nullptr) {
536 loadPath = nativeModulePath[1];
537 HILOG_DEBUG("primary module path load failed, try to load secondary module path: %{public}s", loadPath);
538 lib = LoadModuleLibrary(loadPath, path, isAppModule);
539 if (lib == nullptr) {
540 HILOG_ERROR("primary and secondary module path load failed %{public}s", moduleName);
541 return nullptr;
542 }
543 }
544
545 std::string moduleKey(moduleName);
546 if (isAppModule) {
547 moduleKey = path;
548 moduleKey = moduleKey + '/' + moduleName;
549 }
550
551 if (lastNativeModule_ && strcmp(lastNativeModule_->name, moduleKey.c_str())) {
552 HILOG_WARN(
553 "moduleName '%{public}s' does not match plugin's name '%{public}s'", moduleName, lastNativeModule_->name);
554 }
555
556 if (!internal) {
557 char symbol[NAPI_PATH_MAX] = { 0 };
558 if (!isArk) {
559 if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetJSCode", moduleKey.c_str()) == -1) {
560 LIBFREE(lib);
561 return nullptr;
562 }
563 } else {
564 if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetABCCode", moduleKey.c_str()) == -1) {
565 LIBFREE(lib);
566 return nullptr;
567 }
568 }
569
570 // replace '.' with '_'
571 for (char* p = strchr(symbol, '.'); p != nullptr; p = strchr(p + 1, '.')) {
572 *p = '_';
573 }
574
575 auto getJSCode = reinterpret_cast<NAPIGetJSCode>(LIBSYM(lib, symbol));
576 if (getJSCode != nullptr) {
577 const char* buf = nullptr;
578 int bufLen = 0;
579 getJSCode(&buf, &bufLen);
580 if (lastNativeModule_ != nullptr) {
581 HILOG_DEBUG("get js code from module: bufLen: %{public}d", bufLen);
582 lastNativeModule_->jsCode = buf;
583 lastNativeModule_->jsCodeLen = bufLen;
584 }
585 } else {
586 HILOG_DEBUG("ignore: no %{public}s in %{public}s", symbol, loadPath);
587 }
588 }
589 if (lastNativeModule_) {
590 lastNativeModule_->moduleLoaded = true;
591 }
592 return lastNativeModule_;
593 }
594
FindNativeModuleByCache(const char * moduleName)595 NativeModule* NativeModuleManager::FindNativeModuleByCache(const char* moduleName)
596 {
597 NativeModule* result = nullptr;
598 NativeModule* preNativeModule = nullptr;
599 for (NativeModule* temp = firstNativeModule_; temp != nullptr; temp = temp->next) {
600 if (!strcasecmp(temp->name, moduleName)) {
601 if (strcmp(temp->name, moduleName)) {
602 HILOG_WARN("moduleName '%{public}s' does not match plugin's name '%{public}s'", moduleName, temp->name);
603 }
604 result = temp;
605 break;
606 }
607 preNativeModule = temp;
608 }
609
610 if (result && !result->moduleLoaded) {
611 if (result == lastNativeModule_) {
612 return nullptr;
613 }
614 if (preNativeModule) {
615 preNativeModule->next = result->next;
616 } else {
617 firstNativeModule_ = firstNativeModule_->next;
618 }
619 result->next = nullptr;
620 lastNativeModule_->next = result;
621 lastNativeModule_ = result;
622 return nullptr;
623 }
624 return result;
625 }
626
IsExistedPath(const char * pathKey) const627 bool NativeModuleManager::IsExistedPath(const char* pathKey) const
628 {
629 return pathKey && appLibPathMap_.find(pathKey) != appLibPathMap_.end();
630 }
631
SetModuleBlocklist(std::unordered_map<int32_t,std::unordered_set<std::string>> && blocklist)632 void NativeModuleManager::SetModuleBlocklist(
633 std::unordered_map<int32_t, std::unordered_set<std::string>>&& blocklist)
634 {
635 if (!moduleLoadChecker_) {
636 return;
637 }
638 moduleLoadChecker_->SetModuleBlocklist(std::forward<decltype(blocklist)>(blocklist));
639 }
640
SetProcessExtensionType(int32_t extensionType)641 void NativeModuleManager::SetProcessExtensionType(int32_t extensionType)
642 {
643 if (!moduleLoadChecker_) {
644 return;
645 }
646 moduleLoadChecker_->SetProcessExtensionType(extensionType);
647 }
648
GetProcessExtensionType()649 int32_t NativeModuleManager::GetProcessExtensionType()
650 {
651 if (!moduleLoadChecker_) {
652 return EXTENSION_TYPE_UNSPECIFIED;
653 }
654 return moduleLoadChecker_->GetProcessExtensionType();
655 }
656