1 /*
2 * Copyright (c) 2024 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 "core/common/dynamic_module_helper.h"
17
18 #include <dlfcn.h>
19 #include <memory>
20
21 #include "base/utils/utils.h"
22 #include "compatible/components/component_loader.h"
23
24 namespace OHOS::Ace {
25 namespace {
26 const std::string COMPATIABLE_LIB = "libace_compatible_components.z.so";
27 const std::string COMPATIABLE_COMPONENT_LOADER = "OHOS_ACE_Compatible_GetLoader";
28 const std::string COMPATIABLE_CANVAS_RENDERING_CONTEXT = "OHOS_ACE_Compatible_GetCanvasRenderingContext";
29 const std::string COMPATIABLE_CANVAS_BRIDGE = "OHOS_ACE_Compatible_CreateCanvasBridge";
30 } // namespace
GetInstance()31 DynamicModuleHelper& DynamicModuleHelper::GetInstance()
32 {
33 static DynamicModuleHelper instance;
34 return instance;
35 }
36
GetLoaderByName(const char * name)37 std::unique_ptr<ComponentLoader> DynamicModuleHelper::GetLoaderByName(const char* name)
38 {
39 if (!componentLoaderFunc_) {
40 componentLoaderFunc_ = reinterpret_cast<ComponentLoaderFunc>(LoadSymbol(COMPATIABLE_COMPONENT_LOADER.c_str()));
41 }
42
43 CHECK_NULL_RETURN(componentLoaderFunc_, nullptr);
44 return std::unique_ptr<ComponentLoader>(componentLoaderFunc_(name));
45 }
46
CreateCanvasRenderingContextModel(bool isOffscreen)47 void* DynamicModuleHelper::CreateCanvasRenderingContextModel(bool isOffscreen)
48 {
49 if (!canvasRenderingContextLoaderFunc_) {
50 canvasRenderingContextLoaderFunc_ =
51 reinterpret_cast<CanvasLoaderFunc>(LoadSymbol(COMPATIABLE_CANVAS_RENDERING_CONTEXT.c_str()));
52 }
53
54 CHECK_NULL_RETURN(canvasRenderingContextLoaderFunc_, nullptr);
55 return canvasRenderingContextLoaderFunc_(isOffscreen);
56 }
57
CreateCanvasBridge(CanvasBridgeParams & params)58 void* DynamicModuleHelper::CreateCanvasBridge(CanvasBridgeParams& params)
59 {
60 if (!canvasBridgeLoaderFunc_) {
61 canvasBridgeLoaderFunc_ = reinterpret_cast<CanvasBridgeFunc>(LoadSymbol(COMPATIABLE_CANVAS_BRIDGE.c_str()));
62 }
63
64 CHECK_NULL_RETURN(canvasBridgeLoaderFunc_, nullptr);
65 return canvasBridgeLoaderFunc_(params);
66 }
67
DynamicModuleHelper()68 DynamicModuleHelper::DynamicModuleHelper()
69 {
70 LoadLibrary();
71 }
72
~DynamicModuleHelper()73 DynamicModuleHelper::~DynamicModuleHelper()
74 {
75 CloseLibrary();
76 }
77
LoadLibrary()78 bool DynamicModuleHelper::LoadLibrary()
79 {
80 if (!compatibleLibLoaded_) {
81 compatibleLibHandle_ = dlopen(COMPATIABLE_LIB.c_str(), RTLD_LAZY);
82 CHECK_NULL_RETURN(compatibleLibHandle_, false);
83
84 compatibleLibLoaded_ = true;
85 }
86 return true;
87 }
88
CloseLibrary()89 void DynamicModuleHelper::CloseLibrary()
90 {
91 if (dlclose(compatibleLibHandle_) != 0) {
92 return;
93 }
94 compatibleLibHandle_ = nullptr;
95 compatibleLibLoaded_ = false;
96 }
97
LoadSymbol(const char * symName)98 void* DynamicModuleHelper::LoadSymbol(const char* symName)
99 {
100 CHECK_NULL_RETURN(compatibleLibHandle_, nullptr);
101 return dlsym(compatibleLibHandle_, symName);
102 }
103
104 } // namespace OHOS::Ace
105