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 WEBVIEW_CONTROLLER_IMPL_FFI_H
17 #define WEBVIEW_CONTROLLER_IMPL_FFI_H
18 
19 #include <cstdint>
20 #include <map>
21 #include "ffi_remote_data.h"
22 #include "web_errors.h"
23 #include "webview_javascript_result_callback.h"
24 #include "nweb.h"
25 #include "nweb_helper.h"
26 
27 namespace OHOS::Webview {
28     enum class WebHitTestType : int {
29         EDIT = 0,
30         EMAIL,
31         HTTP,
32         HTTP_IMG,
33         IMG,
34         MAP,
35         PHONE,
36         UNKNOWN
37     };
38 
39     enum class SecurityLevel : int {
40         NONE = 0,
41         SECURE,
42         WARNING,
43         DANGEROUS
44     };
45 
46     enum class CoreSecurityLevel : int {
47         NONE = 0,
48         SECURE = 3,
49         WARNING = 6,
50         DANGEROUS = 5
51     };
52 
53     class __attribute__((visibility("default"))) WebviewControllerImpl : public OHOS::FFI::FFIData {
54         DECL_TYPE(WebviewControllerImpl, OHOS::FFI::FFIData)
55     public:
56         explicit WebviewControllerImpl() = default;
57 
58         explicit WebviewControllerImpl(int32_t nwebId);
59 
WebviewControllerImpl(const std::string & webTag)60         explicit WebviewControllerImpl(const std::string& webTag) : webTag_(webTag)
61         {
62             NWeb::NWebHelper::Instance().SetWebTag(-1, webTag_.c_str());
63         };
64 
65         bool IsInit();
66 
67         void SetWebId(int32_t nwebId);
68 
69         void InnerSetHapPath(const std::string &hapPath);
70 
71         int32_t GetWebId() const;
72 
73         int32_t LoadUrl(std::string url);
74 
75         int32_t LoadUrl(std::string url, std::map<std::string, std::string> headers);
76 
77         ErrCode LoadData(std::string data, std::string mimeType, std::string encoding, std::string baseUrl,
78             std::string historyUrl);
79 
80         void Refresh();
81 
82         std::string GetUserAgent();
83 
84         bool AccessForward();
85 
86         bool AccessBackward();
87 
88         int32_t SetCustomUserAgent(const std::string& userAgent);
89 
90         std::string GetCustomUserAgent() const;
91 
92         void RunJavaScript(std::string script, const std::function<void(RetDataCString)>& callbackRef);
93 
94         std::string GetUrl();
95 
96         std::string GetOriginalUrl();
97 
98         void ScrollPageUp(bool top);
99 
100         void ScrollPageDown(bool bottom);
101 
102         void ScrollTo(float x, float y);
103 
104         void ScrollBy(float deltaX, float deltaY);
105 
106         void Forward();
107 
108         void Backward();
109 
110         int32_t BackOrForward(int32_t step);
111 
112         int32_t GetPageHeight();
113 
114         std::string GetTitle();
115 
116         int32_t Zoom(float factor);
117 
118         int32_t ZoomIn();
119 
120         int32_t ZoomOut();
121 
122         void ClearHistory();
123 
124         bool AccessStep(int32_t step);
125 
126         void OnActive();
127 
128         void OnInactive();
129 
130         int32_t GetHitTest();
131 
132         std::shared_ptr<NWeb::HitTestResult> GetHitTestValue();
133 
134         void StoreWebArchiveCallback(std::string baseName, bool autoName,
135             const std::function<void(RetDataCString)>& callbackRef);
136 
137         void EnableSafeBrowsing(bool enable);
138 
139         bool IsSafeBrowsingEnabled();
140 
141         int32_t GetSecurityLevel();
142 
143         bool IsIncognitoMode();
144 
145         void RemoveCache(bool includeDiskFiles);
146 
147         std::shared_ptr<OHOS::NWeb::NWebHistoryList> GetHistoryList();
148 
149         void SetNWebJavaScriptResultCallBack();
150 
151         void RegisterJavaScriptProxy(const std::vector<std::function<char*(const char*)>>& cjFuncs,
152             const std::string& objName, const std::vector<std::string>& methodList);
153 
154         void Stop();
155 
156         void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive);
157 
158     public:
159         static std::string customeSchemeCmdLine_;
160         static bool existNweb_;
161         static bool webDebuggingAccess_;
162 
163     private:
164         int ConverToWebHitTestType(int hitType);
165 
166     private:
167         std::mutex webMtx_;
168         int32_t nwebId_ = -1;
169         std::shared_ptr<WebviewJavaScriptResultCallBackImpl> javaScriptResultCb_ = nullptr;
170         std::string hapPath_ = "";
171         std::string webTag_ = "";
172     };
173 
174     class __attribute__((visibility("default"))) WebHistoryListImpl : public OHOS::FFI::FFIData {
DECL_TYPE(WebHistoryListImpl,OHOS::FFI::FFIData)175         DECL_TYPE(WebHistoryListImpl, OHOS::FFI::FFIData)
176     public:
177         explicit WebHistoryListImpl(std::shared_ptr<NWeb::NWebHistoryList> sptrHistoryList)
178             :sptrHistoryList_(sptrHistoryList) {};
179 
180         int32_t GetCurrentIndex();
181 
182         std::shared_ptr<OHOS::NWeb::NWebHistoryItem> GetItem(int32_t index);
183 
184         int32_t GetListSize();
185     private:
186         std::shared_ptr<OHOS::NWeb::NWebHistoryList> sptrHistoryList_ = nullptr;
187     };
188 }
189 #endif // WEBVIEW_CONTROLLER_IMPL_FFI_H