1 /*
2 * Copyright (c) 2022 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 "interfaces/inner_api/ace/declarative_module_preloader.h"
17
18 #include "utils.h"
19 #include "ace_forward_compatibility.h"
20
21 namespace OHOS::Ace {
22
23 using CreateFunc = void (*)(void*);
24 constexpr char PRE_INIT_ACE_MODULE_FUNC[] = "OHOS_ACE_PreloadAceModule";
25
InitAceModule(void * runtime)26 void InitAceModule(void* runtime)
27 {
28 LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
29 if (handle == nullptr) {
30 return;
31 }
32
33 auto entry = reinterpret_cast<CreateFunc>(LOADSYM(handle, PRE_INIT_ACE_MODULE_FUNC));
34 if (entry == nullptr) {
35 FREELIB(handle);
36 return;
37 }
38
39 entry(runtime);
40 }
41
Preload(NativeEngine & runtime)42 void DeclarativeModulePreloader::Preload(NativeEngine& runtime)
43 {
44 InitAceModule(reinterpret_cast<void*>(&runtime));
45 }
46
47 // ArkTsCard start
48 using CreateFuncCard = void (*)(void*, const char*, const void*);
49 constexpr char PRE_INIT_ACE_MODULE_FUNC_CARD[] = "OHOS_ACE_PreloadAceModuleCard";
50 constexpr char RELOAD_ACE_MODULE_FUNC_CARD[] = "OHOS_ACE_ReloadAceModuleCard";
51
InitAceModuleCard(void * runtime,const char * bundleName,const void * hapPathMap)52 void InitAceModuleCard(void* runtime, const char* bundleName, const void* hapPathMap)
53 {
54 LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
55 if (handle == nullptr) {
56 return;
57 }
58
59 auto entry = reinterpret_cast<CreateFuncCard>(LOADSYM(handle, PRE_INIT_ACE_MODULE_FUNC_CARD));
60 if (entry == nullptr) {
61 FREELIB(handle);
62 return;
63 }
64
65 entry(runtime, bundleName, hapPathMap);
66 }
67
PreloadCard(NativeEngine & runtime,const std::string & bundleName,const std::map<std::string,std::string> & hapPathMap)68 void DeclarativeModulePreloader::PreloadCard(NativeEngine& runtime, const std::string& bundleName,
69 const std::map<std::string, std::string>& hapPathMap)
70 {
71 InitAceModuleCard(reinterpret_cast<void*>(&runtime), bundleName.c_str(),
72 reinterpret_cast<const void*>(&hapPathMap));
73 }
74
ReloadAceModuleCard(void * runtime,const char * bundleName,const void * hapPathMap)75 void ReloadAceModuleCard(void* runtime, const char* bundleName, const void* hapPathMap)
76 {
77 LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
78 if (handle == nullptr) {
79 return;
80 }
81
82 auto entry = reinterpret_cast<CreateFuncCard>(LOADSYM(handle, RELOAD_ACE_MODULE_FUNC_CARD));
83 if (entry == nullptr) {
84 FREELIB(handle);
85 return;
86 }
87
88 entry(runtime, bundleName, hapPathMap);
89 }
90
ReloadCard(NativeEngine & runtime,const std::string & bundleName,const std::map<std::string,std::string> & hapPathMap)91 void DeclarativeModulePreloader::ReloadCard(NativeEngine& runtime, const std::string &bundleName,
92 const std::map<std::string, std::string>& hapPathMap)
93 {
94 ReloadAceModuleCard(reinterpret_cast<void*>(&runtime), bundleName.c_str(),
95 reinterpret_cast<const void*>(&hapPathMap));
96 }
97 // ArkTsCard end
98
99 using CreateFuncWorker = void (*)(void*);
100 constexpr char PRE_INIT_ACE_MODULE_FUNC_WORKER[] = "OHOS_ACE_PreloadAceModuleWorker";
101
InitAceModuleWorker(void * runtime)102 void InitAceModuleWorker(void* runtime)
103 {
104 LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
105 if (handle == nullptr) {
106 return;
107 }
108
109 auto entry = reinterpret_cast<CreateFuncWorker>(LOADSYM(handle, PRE_INIT_ACE_MODULE_FUNC_WORKER));
110 if (entry == nullptr) {
111 FREELIB(handle);
112 return;
113 }
114
115 entry(runtime);
116 }
117
PreloadWorker(NativeEngine & runtime)118 void DeclarativeModulePreloader::PreloadWorker(NativeEngine& runtime)
119 {
120 InitAceModuleWorker(reinterpret_cast<void*>(&runtime));
121 }
122 } // namespace OHOS::Ace
123