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 #ifndef ARK_WEB_BRIDGE_HELPER_H_
17 #define ARK_WEB_BRIDGE_HELPER_H_
18 #pragma once
19 
20 #include <dlfcn.h>
21 
22 #include "base/include/ark_web_bridge_types.h"
23 #include "base/include/ark_web_types.h"
24 
25 namespace OHOS::ArkWeb {
26 
27 extern int g_ark_web_init_addr;
28 #define ARK_WEB_INIT_ADDR &g_ark_web_init_addr
29 
30 #if defined(OHOS_WEBVIEW_GLUE)
31 #if defined(webview_arm64)
32 const std::string WEBVIEW_RELATIVE_PATH_FOR_MOCK = "libs/arm64";
33 const std::string WEBVIEW_RELATIVE_PATH_FOR_BUNDLE = "arkwebcore/libs/arm64";
34 #elif defined(webview_x86_64)
35 const std::string WEBVIEW_RELATIVE_PATH_FOR_MOCK = "libs/x86_64";
36 const std::string WEBVIEW_RELATIVE_PATH_FOR_BUNDLE = "arkwebcore/libs/x86_64";
37 #else
38 const std::string WEBVIEW_RELATIVE_PATH_FOR_MOCK = "libs/arm";
39 const std::string WEBVIEW_RELATIVE_PATH_FOR_BUNDLE = "arkwebcore/libs/arm";
40 #endif
41 #endif
42 
43 using ArkWebMemberCheckFunc = void* (*)(ArkWebBridgeType, const ArkWebString*);
44 
45 class ArkWebBridgeHelper {
46 public:
47     virtual ~ArkWebBridgeHelper();
48 
49     void* LoadFuncSymbol(const char* funcName, bool isPrintLog = true);
50 
51     void RegisterFuncMember(ArkWebBridgeType bridgeType, const std::map<std::string, void*>& funcMemberMap);
52 
53     void* CheckFuncMemberForCalled(ArkWebBridgeType bridgeType, const std::string& funcName);
54 
55     void* CheckFuncMemberForCaller(ArkWebBridgeType bridgeType, const std::string& funcName);
56 
57 protected:
58     ArkWebBridgeHelper() = default;
59 
60     bool LoadLibFile(int openMode, const std::string& libFilePath, bool isPrintLog = true);
61 
62 #if defined(OHOS_WEBVIEW_GLUE)
63     bool LoadLibFile(int openMode, const std::string& libNsName, const std::string& libDirPath,
64         const std::string& libFileName, bool isPrintLog = true);
65 #endif
66 
67     static void PrereadLibFile(const std::string& libFilePath, bool isPrintLog = true);
68 
69     void InitFuncMemberMaps(ArkWebBridgeType init, ArkWebBridgeType butt, bool isPrintLog = true);
70 
71 private:
72     void UnloadLibFile();
73 
74 protected:
75     ArkWebMemberCheckFunc memberCheckFunc_ = nullptr;
76     std::map<int, std::map<std::string, void*>> funcMemberMaps_;
77 
78 private:
79     void* libFileHandler_ = nullptr;
80 };
81 
82 } // namespace OHOS::ArkWeb
83 
84 #endif // ARK_WEB_BRIDGE_HELPER_H_
85