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_adapter/bridge/ark_web_adapter_webview_bridge_helper.h"
17 
18 #include "base/bridge/ark_web_bridge_macros.h"
19 
20 namespace OHOS::ArkWeb {
21 
22 const std::string ADAPTER_LIB_FILE_NAME = "libarkweb_engine.so";
23 
ArkWebAdapterWebviewBridgeHelper()24 ArkWebAdapterWebviewBridgeHelper::ArkWebAdapterWebviewBridgeHelper()
25 {
26     Init();
27 
28     InitFuncMemberMaps(ARK_WEB_ADAPTER_INTERFACE_INIT, ARK_WEB_ADAPTER_INTERFACE_BUTT);
29 }
30 
Init()31 void ArkWebAdapterWebviewBridgeHelper::Init()
32 {
33     std::string libDirPath = WEBVIEW_RELATIVE_PATH_FOR_BUNDLE;
34 
35 #ifdef __MUSL__
36     if (!LoadLibFile(RTLD_NOW | RTLD_GLOBAL, "nweb_ns", libDirPath, ADAPTER_LIB_FILE_NAME)) {
37 #else
38     if (!LoadLibFile(RTLD_NOW, libDirPath + "/" + ADAPTER_LIB_FILE_NAME)) {
39 #endif
40         return;
41     }
42 
43     memberCheckFunc_ =
44         reinterpret_cast<ArkWebMemberCheckFunc>(LoadFuncSymbol("ark_web_adapter_webcore_check_func_static"));
45     if (!memberCheckFunc_) {
46         ARK_WEB_BRIDGE_INFO_LOG("failed to load func ark_web_adapter_webcore_check_func_static");
47     }
48     return;
49 }
50 
51 ArkWebAdapterWebviewBridgeHelper& ArkWebAdapterWebviewBridgeHelper::GetInstance()
52 {
53     static ArkWebAdapterWebviewBridgeHelper helper;
54     return helper;
55 }
56 
57 } // namespace OHOS::ArkWeb
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif // __cplusplus
62 
63 ARK_WEB_EXPORT
ark_web_adapter_webview_check_func_static(ArkWebBridgeType bridgeType,const ArkWebString * funcName)64 void* ark_web_adapter_webview_check_func_static(ArkWebBridgeType bridgeType, const ArkWebString* funcName)
65 {
66     if (!funcName) {
67         ARK_WEB_BRIDGE_INFO_LOG("func name is nullptr");
68         return nullptr;
69     }
70 
71     return OHOS::ArkWeb::ArkWebAdapterWebviewBridgeHelper::GetInstance().CheckFuncMemberForCalled(
72         bridgeType, ArkWebStringStructToClass(*funcName));
73 }
74 
75 #ifdef __cplusplus
76 }
77 #endif // __cplusplus
78