• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/bridge/ark_web_bridge_helper.h"
17 
18 #include "base/bridge/ark_web_bridge_macros.h"
19 
20 namespace OHOS::ArkWeb {
21 
~ArkWebBridgeHelper()22 ArkWebBridgeHelper::~ArkWebBridgeHelper()
23 {
24     UnloadLibFile();
25 }
26 
LoadLibFile(int mode,const std::string & libFilePath,bool isPrintLog)27 bool ArkWebBridgeHelper::LoadLibFile(int mode, const std::string& libFilePath, bool isPrintLog)
28 {
29     if (libFileHandler_) {
30         return true;
31     }
32 
33     void* libFileHandler = ::dlopen(libFilePath.c_str(), mode);
34     if (!libFileHandler) {
35         if (isPrintLog) {
36             ARK_WEB_WRAPPER_ERROR_LOG("failed to load lib file,lib file path is %{public}s", libFilePath.c_str());
37         }
38 
39         return false;
40     }
41 
42     libFileHandler_ = libFileHandler;
43     return true;
44 }
45 
46 #if !defined(OHOS_WEBCORE_GLUE)
LoadLibFile(int mode,const std::string & libNsName,const std::string & libDirPath,const std::string & libFileName,bool isPrintLog)47 bool ArkWebBridgeHelper::LoadLibFile(int mode, const std::string& libNsName, const std::string& libDirPath,
48     const std::string& libFileName, bool isPrintLog)
49 {
50     if (libFileHandler_) {
51         return true;
52     }
53 
54     Dl_namespace dlns;
55     dlns_init(&dlns, libNsName.c_str());
56 
57     dlns_create(&dlns, libDirPath.c_str());
58 
59     void* libFileHandler = dlopen_ns(&dlns, libFileName.c_str(), mode);
60     if (!libFileHandler) {
61         if (isPrintLog) {
62             ARK_WEB_WRAPPER_ERROR_LOG("failed to load lib file,lib file name is "
63                                       "%{public}s,lib dir name is %{public}s",
64                 libFileName.c_str(), libDirPath.c_str());
65         }
66 
67         return false;
68     }
69 
70     libFileHandler_ = libFileHandler;
71     return true;
72 }
73 #endif
74 
UnloadLibFile()75 void ArkWebBridgeHelper::UnloadLibFile()
76 {
77     if (libFileHandler_ != nullptr) {
78         ::dlclose(libFileHandler_);
79         libFileHandler_ = nullptr;
80     }
81 }
82 
LoadFuncSymbol(const std::string & funcName,bool isPrintLog)83 void* ArkWebBridgeHelper::LoadFuncSymbol(const std::string& funcName, bool isPrintLog)
84 {
85     if (!libFileHandler_) {
86         if (isPrintLog) {
87             ARK_WEB_WRAPPER_ERROR_LOG("lib file handle is nullptr,func name is %{public}s", funcName.c_str());
88         }
89 
90         return nullptr;
91     }
92 
93     if (isPrintLog) {
94         ARK_WEB_WRAPPER_INFO_LOG("load func %{public}s", funcName.c_str());
95     }
96 
97     return dlsym(libFileHandler_, funcName.c_str());
98 }
99 
100 } // namespace OHOS::ArkWeb
101