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