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*);
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)52 void InitAceModuleCard(void* runtime, const char* bundleName)
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);
66 }
67
PreloadCard(NativeEngine & runtime,const std::string & bundleName)68 void DeclarativeModulePreloader::PreloadCard(NativeEngine& runtime, const std::string &bundleName)
69 {
70 InitAceModuleCard(reinterpret_cast<void*>(&runtime), bundleName.c_str());
71 }
72
ReloadAceModuleCard(void * runtime,const char * bundleName)73 void ReloadAceModuleCard(void* runtime, const char* bundleName)
74 {
75 LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
76 if (handle == nullptr) {
77 return;
78 }
79
80 auto entry = reinterpret_cast<CreateFuncCard>(LOADSYM(handle, RELOAD_ACE_MODULE_FUNC_CARD));
81 if (entry == nullptr) {
82 FREELIB(handle);
83 return;
84 }
85
86 entry(runtime, bundleName);
87 }
88
ReloadCard(NativeEngine & runtime,const std::string & bundleName)89 void DeclarativeModulePreloader::ReloadCard(NativeEngine& runtime, const std::string &bundleName)
90 {
91 ReloadAceModuleCard(reinterpret_cast<void*>(&runtime), bundleName.c_str());
92 }
93 // ArkTsCard end
94
95 using CreateFuncWorker = void (*)(void*);
96 constexpr char PRE_INIT_ACE_MODULE_FUNC_WORKER[] = "OHOS_ACE_PreloadAceModuleWorker";
97
InitAceModuleWorker(void * runtime)98 void InitAceModuleWorker(void* runtime)
99 {
100 LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
101 if (handle == nullptr) {
102 return;
103 }
104
105 auto entry = reinterpret_cast<CreateFuncWorker>(LOADSYM(handle, PRE_INIT_ACE_MODULE_FUNC_WORKER));
106 if (entry == nullptr) {
107 FREELIB(handle);
108 return;
109 }
110
111 entry(runtime);
112 }
113
PreloadWorker(NativeEngine & runtime)114 void DeclarativeModulePreloader::PreloadWorker(NativeEngine& runtime)
115 {
116 InitAceModuleWorker(reinterpret_cast<void*>(&runtime));
117 }
118 } // namespace OHOS::Ace
119