• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 namespace OHOS::Ace {
21 
22 #if defined(WINDOWS_PLATFORM)
23 constexpr char ACE_LIB_NAME[] = "libace.dll";
24 #elif defined(MAC_PLATFORM)
25 constexpr char ACE_LIB_NAME[] = "libace.dylib";
26 #elif defined(LINUX_PLATFORM)
27 constexpr char ACE_LIB_NAME[] = "libace.so";
28 #else
29 constexpr char ACE_LIB_NAME[] = "libace.z.so";
30 #endif
31 
32 using CreateFunc = void (*)(void*);
33 constexpr char PRE_INIT_ACE_MODULE_FUNC[] = "OHOS_ACE_PreloadAceModule";
34 
InitAceModule(void * runtime)35 void InitAceModule(void* runtime)
36 {
37     LIBHANDLE handle = LOADLIB(ACE_LIB_NAME);
38     if (handle == nullptr) {
39         return;
40     }
41 
42     auto entry = reinterpret_cast<CreateFunc>(LOADSYM(handle, PRE_INIT_ACE_MODULE_FUNC));
43     if (entry == nullptr) {
44         FREELIB(handle);
45         return;
46     }
47 
48     entry(runtime);
49 }
50 
Preload(NativeEngine & runtime)51 void DeclarativeModulePreloader::Preload(NativeEngine& runtime)
52 {
53     InitAceModule(reinterpret_cast<void*>(&runtime));
54 }
55 
56 // ArkTsCard start
57 using CreateFuncCard = void (*)(void*, const char*);
58 constexpr char PRE_INIT_ACE_MODULE_FUNC_CARD[] = "OHOS_ACE_PreloadAceModuleCard";
59 constexpr char RELOAD_ACE_MODULE_FUNC_CARD[] = "OHOS_ACE_ReloadAceModuleCard";
60 
InitAceModuleCard(void * runtime,const char * bundleName)61 void InitAceModuleCard(void* runtime, const char* bundleName)
62 {
63     LIBHANDLE handle = LOADLIB(ACE_LIB_NAME);
64     if (handle == nullptr) {
65         return;
66     }
67 
68     auto entry = reinterpret_cast<CreateFuncCard>(LOADSYM(handle, PRE_INIT_ACE_MODULE_FUNC_CARD));
69     if (entry == nullptr) {
70         FREELIB(handle);
71         return;
72     }
73 
74     entry(runtime, bundleName);
75 }
76 
PreloadCard(NativeEngine & runtime,const std::string & bundleName)77 void DeclarativeModulePreloader::PreloadCard(NativeEngine& runtime, const std::string &bundleName)
78 {
79     InitAceModuleCard(reinterpret_cast<void*>(&runtime), bundleName.c_str());
80 }
81 
ReloadAceModuleCard(void * runtime,const char * bundleName)82 void ReloadAceModuleCard(void* runtime, const char* bundleName)
83 {
84     LIBHANDLE handle = LOADLIB(ACE_LIB_NAME);
85     if (handle == nullptr) {
86         return;
87     }
88 
89     auto entry = reinterpret_cast<CreateFuncCard>(LOADSYM(handle, RELOAD_ACE_MODULE_FUNC_CARD));
90     if (entry == nullptr) {
91         FREELIB(handle);
92         return;
93     }
94 
95     entry(runtime, bundleName);
96 }
97 
ReloadCard(NativeEngine & runtime,const std::string & bundleName)98 void DeclarativeModulePreloader::ReloadCard(NativeEngine& runtime, const std::string &bundleName)
99 {
100     ReloadAceModuleCard(reinterpret_cast<void*>(&runtime), bundleName.c_str());
101 }
102 // ArkTsCard end
103 } // namespace OHOS::Ace
104