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 "ohos_nweb/bridge/ark_web_nweb_webview_bridge_helper.h"
17 
18 #include "base/bridge/ark_web_bridge_macros.h"
19 
20 namespace OHOS::ArkWeb {
21 
22 const std::string NWEB_LIB_FILE_NAME = "libarkweb_engine.so";
23 
ArkWebNWebWebviewBridgeHelper()24 ArkWebNWebWebviewBridgeHelper::ArkWebNWebWebviewBridgeHelper()
25 {
26     InitFuncMemberMaps(ARK_WEB_NWEB_INTERFACE_INIT, ARK_WEB_NWEB_INTERFACE_BUTT);
27 }
28 
Init(bool runMode,const std::string & bundlePath)29 bool ArkWebNWebWebviewBridgeHelper::Init(bool runMode, const std::string& bundlePath)
30 {
31     std::string libDirPath;
32     if (runMode) {
33         libDirPath = bundlePath + "/" + WEBVIEW_RELATIVE_PATH_FOR_BUNDLE;
34     } else {
35         libDirPath = bundlePath + "/" + WEBVIEW_RELATIVE_PATH_FOR_MOCK;
36     }
37 
38 #ifdef __MUSL__
39     if (!LoadLibFile(RTLD_NOW | RTLD_GLOBAL, "nweb_ns", libDirPath, NWEB_LIB_FILE_NAME)) {
40 #else
41     if (!LoadLibFile(RTLD_NOW, libDirPath + "/" + NWEB_LIB_FILE_NAME)) {
42 #endif
43         return false;
44     }
45 
46     memberCheckFunc_ =
47         reinterpret_cast<ArkWebMemberCheckFunc>(LoadFuncSymbol("ark_web_nweb_webcore_check_func_static"));
48     if (!memberCheckFunc_) {
49         ARK_WEB_BRIDGE_INFO_LOG("failed to load func ark_web_nweb_webcore_check_func_static");
50     }
51     return true;
52 }
53 
54 ArkWebNWebWebviewBridgeHelper& ArkWebNWebWebviewBridgeHelper::GetInstance()
55 {
56     static ArkWebNWebWebviewBridgeHelper helper;
57     return helper;
58 }
59 
60 void ArkWebNWebWebviewBridgeHelper::PreloadLibFile(bool runMode, const std::string& bundlePath)
61 {
62     std::string libFilePath;
63     if (runMode) {
64         libFilePath = bundlePath + "/" + WEBVIEW_RELATIVE_PATH_FOR_BUNDLE + "/" + NWEB_LIB_FILE_NAME;
65     } else {
66         libFilePath = bundlePath + "/" + WEBVIEW_RELATIVE_PATH_FOR_MOCK + "/" + NWEB_LIB_FILE_NAME;
67     }
68 
69     ArkWebBridgeHelper::PrereadLibFile(libFilePath);
70 }
71 
72 } // namespace OHOS::ArkWeb
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif // __cplusplus
77 
78 ARK_WEB_EXPORT
ark_web_nweb_webview_check_func_static(ArkWebBridgeType bridgeType,const ArkWebString * funcName)79 void* ark_web_nweb_webview_check_func_static(ArkWebBridgeType bridgeType, const ArkWebString* funcName)
80 {
81     if (!funcName) {
82         ARK_WEB_BRIDGE_INFO_LOG("func name is nullptr");
83         return nullptr;
84     }
85 
86     return OHOS::ArkWeb::ArkWebNWebWebviewBridgeHelper::GetInstance().CheckFuncMemberForCalled(
87         bridgeType, ArkWebStringStructToClass(*funcName));
88 }
89 
90 #ifdef __cplusplus
91 }
92 #endif // __cplusplus
93