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 "webview_utils.h"
23#include "web_errors.h"
24#include "webview_javascript_result_callback.h"
25#include "nweb.h"
26#include "nweb_helper.h"
27#include "nweb_web_message.h"
28
29namespace OHOS::Webview {
30    enum class WebHitTestType : int {
31        EDIT = 0,
32        EMAIL,
33        HTTP,
34        HTTP_IMG,
35        IMG,
36        MAP,
37        PHONE,
38        UNKNOWN
39    };
40
41    enum class SecurityLevel : int {
42        NONE = 0,
43        SECURE,
44        WARNING,
45        DANGEROUS
46    };
47
48    enum class CoreSecurityLevel : int {
49        NONE = 0,
50        SECURE = 3,
51        WARNING = 6,
52        DANGEROUS = 5
53    };
54
55    enum class WebMessageType : int {
56        NOTSUPPORT = 0,
57        STRING,
58        NUMBER,
59        BOOLEAN,
60        ARRAYBUFFER,
61        ARRAY,
62        ERROR
63    };
64
65    class __attribute__((visibility("default"))) WebviewControllerImpl : public OHOS::FFI::FFIData {
66        DECL_TYPE(WebviewControllerImpl, OHOS::FFI::FFIData)
67    public:
68        explicit WebviewControllerImpl() = default;
69
70        explicit WebviewControllerImpl(int32_t nwebId);
71
72        explicit WebviewControllerImpl(const std::string& webTag) : webTag_(webTag)
73        {
74            NWeb::NWebHelper::Instance().SetWebTag(-1, webTag_.c_str());
75        };
76
77        bool IsInit();
78
79        void SetWebId(int32_t nwebId);
80
81        void InnerSetHapPath(const std::string &hapPath);
82
83        int32_t GetWebId() const;
84
85        int32_t LoadUrl(std::string url);
86
87        int32_t LoadUrl(std::string url, std::map<std::string, std::string> headers);
88
89        ErrCode LoadData(std::string data, std::string mimeType, std::string encoding, std::string baseUrl,
90            std::string historyUrl);
91
92        int32_t PreFetchPage(std::string url);
93
94        int32_t PreFetchPage(std::string url, std::map<std::string, std::string> headers);
95
96        int32_t SetAudioMuted(bool mute);
97
98        void SlideScroll(float vx, float vy);
99
100        void PutNetworkAvailable(bool enable);
101
102        void ClearClientAuthenticationCache();
103
104        void ClearSslCache();
105
106        void SearchNext(bool forward);
107
108        void ClearMatches();
109
110        void SearchAllAsync(std::string str);
111
112        ErrCode DeleteJavaScriptRegister(const std::string& objName,
113            const std::vector<std::string>& methodList);
114
115        void Refresh();
116
117        std::string GetUserAgent();
118
119        bool AccessForward();
120
121        bool AccessBackward();
122
123        int32_t SetCustomUserAgent(const std::string& userAgent);
124
125        std::string GetCustomUserAgent() const;
126
127        void RunJavaScript(std::string script, const std::function<void(RetDataCString)>& callbackRef);
128
129        void RunJavaScriptExt(std::string script, const std::function<void(RetDataI64)>& callbackRef);
130
131        std::string GetUrl();
132
133        std::string GetOriginalUrl();
134
135        void ScrollPageUp(bool top);
136
137        void ScrollPageDown(bool bottom);
138
139        void ScrollTo(float x, float y);
140
141        void ScrollBy(float deltaX, float deltaY);
142
143        void ScrollToWithAnime(float x, float y, int32_t duration);
144
145        void ScrollByWithAnime(float deltaX, float deltaY, int32_t duration);
146
147        void Forward();
148
149        void Backward();
150
151        int32_t BackOrForward(int32_t step);
152
153        int32_t GetPageHeight();
154
155        std::string GetTitle();
156
157        int32_t Zoom(float factor);
158
159        int32_t ZoomIn();
160
161        int32_t ZoomOut();
162
163        int32_t RequestFocus();
164
165        void ClearHistory();
166
167        bool AccessStep(int32_t step);
168
169        void OnActive();
170
171        void OnInactive();
172
173        int32_t GetHitTest();
174
175        std::shared_ptr<NWeb::HitTestResult> GetHitTestValue();
176
177        void StoreWebArchiveCallback(std::string baseName, bool autoName,
178            const std::function<void(RetDataCString)>& callbackRef);
179
180        void EnableSafeBrowsing(bool enable);
181
182        bool IsSafeBrowsingEnabled();
183
184        int32_t GetSecurityLevel();
185
186        bool IsIncognitoMode();
187
188        void RemoveCache(bool includeDiskFiles);
189
190        std::shared_ptr<OHOS::NWeb::NWebHistoryList> GetHistoryList();
191
192        bool GetFavicon(const void **data, size_t &width, size_t &height,
193            NWeb::ImageColorType &colorType, NWeb::ImageAlphaType &alphaType) const;
194
195        void SetNWebJavaScriptResultCallBack();
196
197        void RegisterJavaScriptProxy(const std::vector<std::function<char*(const char*)>>& cjFuncs,
198            const std::string& objName, const std::vector<std::string>& methodList);
199
200        void Stop();
201
202        void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive);
203
204        int32_t PostUrl(std::string& url, std::vector<char>& postData);
205
206        std::vector<std::string> CreateWebMessagePorts();
207
208        ErrCode PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUrl);
209
210        std::vector<uint8_t> SerializeWebState();
211
212        bool RestoreWebState(const std::vector<uint8_t> &state) const;
213
214        bool GetCertChainDerData(std::vector<std::string> &certChainDerData) const;
215
216        ErrCode HasImagesCallback(const std::function<void(RetDataBool)>& callbackRef);
217
218        static int32_t CustomizeSchemesArrayDataHandler(CArrScheme schemes);
219
220    public:
221        static std::string customeSchemeCmdLine_;
222        static bool existNweb_;
223        static bool webDebuggingAccess_;
224
225    private:
226        int ConverToWebHitTestType(int hitType);
227
228    private:
229        std::mutex webMtx_;
230        int32_t nwebId_ = -1;
231        std::shared_ptr<WebviewJavaScriptResultCallBackImpl> javaScriptResultCb_ = nullptr;
232        std::string hapPath_ = "";
233        std::string webTag_ = "";
234    };
235
236    class __attribute__((visibility("default"))) WebHistoryListImpl : public OHOS::FFI::FFIData {
237        DECL_TYPE(WebHistoryListImpl, OHOS::FFI::FFIData)
238    public:
239        explicit WebHistoryListImpl(std::shared_ptr<NWeb::NWebHistoryList> sptrHistoryList)
240            :sptrHistoryList_(sptrHistoryList) {};
241
242        int32_t GetCurrentIndex();
243
244        std::shared_ptr<OHOS::NWeb::NWebHistoryItem> GetItem(int32_t index);
245
246        int32_t GetListSize();
247    private:
248        std::shared_ptr<OHOS::NWeb::NWebHistoryList> sptrHistoryList_ = nullptr;
249    };
250
251    class WebMessagePortImpl : public OHOS::FFI::FFIData {
252        DECL_TYPE(WebMessagePortImpl, OHOS::FFI::FFIData)
253    public:
254        WebMessagePortImpl(int32_t nwebId, std::string port, bool isExtentionType);
255
256        ~WebMessagePortImpl() = default;
257
258        ErrCode ClosePort();
259
260        ErrCode PostPortMessage(std::shared_ptr<NWeb::NWebMessage> data);
261
262        ErrCode SetPortMessageCallback(std::shared_ptr<NWeb::NWebMessageValueCallback> callback);
263
264        std::string GetPortHandle() const;
265
266        bool IsExtentionType()
267        {
268            return isExtentionType_;
269        }
270
271    private:
272        int32_t nwebId_ = -1;
273        std::string portHandle_;
274        bool isExtentionType_;
275    };
276
277    class WebMessageExtImpl : public OHOS::FFI::FFIData {
278        DECL_TYPE(WebMessageExtImpl, OHOS::FFI::FFIData)
279    public:
280        explicit WebMessageExtImpl(std::shared_ptr<NWeb::NWebMessage> data) : data_(data) {};
281        ~WebMessageExtImpl() = default;
282
283        void SetType(int type)
284        {
285            type_ = type;
286            WebMessageType jsType = static_cast<WebMessageType>(type);
287            NWeb::NWebValue::Type nwebType = NWeb::NWebValue::Type::NONE;
288            switch (jsType) {
289                case WebMessageType::STRING: {
290                    nwebType = NWeb::NWebValue::Type::STRING;
291                    break;
292                }
293                case WebMessageType::NUMBER: {
294                    nwebType = NWeb::NWebValue::Type::DOUBLE;
295                    break;
296                }
297                case WebMessageType::BOOLEAN: {
298                    nwebType = NWeb::NWebValue::Type::BOOLEAN;
299                    break;
300                }
301                case WebMessageType::ARRAYBUFFER: {
302                    nwebType = NWeb::NWebValue::Type::BINARY;
303                    break;
304                }
305                case WebMessageType::ARRAY: {
306                    nwebType = NWeb::NWebValue::Type::STRINGARRAY;
307                    break;
308                }
309                case WebMessageType::ERROR: {
310                    nwebType = NWeb::NWebValue::Type::ERROR;
311                    break;
312                }
313                default: {
314                    nwebType = NWeb::NWebValue::Type::NONE;
315                    break;
316                }
317            }
318            if (data_) {
319                data_->SetType(nwebType);
320            }
321        }
322
323        int ConvertNwebType2JsType(NWeb::NWebValue::Type type)
324        {
325            WebMessageType jsType = WebMessageType::NOTSUPPORT;
326            switch (type) {
327                case NWeb::NWebValue::Type::STRING: {
328                    jsType = WebMessageType::STRING;
329                    break;
330                }
331                case NWeb::NWebValue::Type::DOUBLE:
332                case NWeb::NWebValue::Type::INTEGER: {
333                    jsType = WebMessageType::NUMBER;
334                    break;
335                }
336                case NWeb::NWebValue::Type::BOOLEAN: {
337                    jsType = WebMessageType::BOOLEAN;
338                    break;
339                }
340                case NWeb::NWebValue::Type::STRINGARRAY:
341                case NWeb::NWebValue::Type::DOUBLEARRAY:
342                case NWeb::NWebValue::Type::INT64ARRAY:
343                case NWeb::NWebValue::Type::BOOLEANARRAY: {
344                    jsType = WebMessageType::ARRAY;
345                    break;
346                }
347                case NWeb::NWebValue::Type::BINARY: {
348                    jsType = WebMessageType::ARRAYBUFFER;
349                    break;
350                }
351                case NWeb::NWebValue::Type::ERROR: {
352                    jsType = WebMessageType::ERROR;
353                    break;
354                }
355                default: {
356                    jsType = WebMessageType::NOTSUPPORT;
357                    break;
358                }
359            }
360            return static_cast<int>(jsType);
361        }
362
363        int GetType()
364        {
365            if (data_) {
366                return ConvertNwebType2JsType(data_->GetType());
367            }
368            return static_cast<int>(WebMessageType::NOTSUPPORT);
369        }
370
371        void SetString(std::string value)
372        {
373            if (data_) {
374                data_->SetType(NWeb::NWebValue::Type::STRING);
375                data_->SetString(value);
376            }
377        }
378
379        void SetNumber(double value)
380        {
381            if (data_) {
382                data_->SetType(NWeb::NWebValue::Type::DOUBLE);
383                data_->SetDouble(value);
384            }
385        }
386
387        void SetBoolean(bool value)
388        {
389            if (data_) {
390                data_->SetType(NWeb::NWebValue::Type::BOOLEAN);
391                data_->SetBoolean(value);
392            }
393        }
394
395        void SetArrayBuffer(std::vector<uint8_t>& value)
396        {
397            if (data_) {
398                data_->SetType(NWeb::NWebValue::Type::BINARY);
399                data_->SetBinary(value);
400            }
401        }
402
403        void SetStringArray(std::vector<std::string> value)
404        {
405            if (data_) {
406                data_->SetType(NWeb::NWebValue::Type::STRINGARRAY);
407                data_->SetStringArray(value);
408            }
409        }
410
411        void SetDoubleArray(std::vector<double> value)
412        {
413            if (data_) {
414                data_->SetType(NWeb::NWebValue::Type::DOUBLEARRAY);
415                data_->SetDoubleArray(value);
416            }
417        }
418
419        void SetInt64Array(std::vector<int64_t> value)
420        {
421            if (data_) {
422                data_->SetType(NWeb::NWebValue::Type::INT64ARRAY);
423                data_->SetInt64Array(value);
424            }
425        }
426
427        void SetBooleanArray(std::vector<bool> value)
428        {
429            if (data_) {
430                data_->SetType(NWeb::NWebValue::Type::BOOLEANARRAY);
431                data_->SetBooleanArray(value);
432            }
433        }
434
435        void SetError(std::string name, std::string message)
436        {
437            if (data_) {
438                data_->SetType(NWeb::NWebValue::Type::ERROR);
439                data_->SetErrName(name);
440                data_->SetErrMsg(message);
441            }
442        }
443
444        std::shared_ptr<NWeb::NWebMessage> GetData()
445        {
446            return data_;
447        }
448
449    private:
450        int type_ = 0;
451        std::shared_ptr<NWeb::NWebMessage> data_;
452    };
453
454    class NWebMessageCallbackImpl : public NWeb::NWebMessageValueCallback {
455    public:
456        NWebMessageCallbackImpl(std::function<void(RetWebMessage)> callback)
457            : callback_(callback)
458        {}
459        ~NWebMessageCallbackImpl() = default;
460        void OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result) override;
461
462    private:
463        std::function<void(RetWebMessage)> callback_;
464    };
465
466    class NWebWebMessageExtCallbackImpl : public NWeb::NWebMessageValueCallback {
467    public:
468        NWebWebMessageExtCallbackImpl(std::function<void(int64_t)> callback)
469            : callback_(callback)
470        {}
471        ~NWebWebMessageExtCallbackImpl() = default;
472        void OnReceiveValue(std::shared_ptr<NWeb::NWebMessage> result) override;
473
474    private:
475        std::function<void(int64_t)> callback_;
476    };
477}
478#endif // WEBVIEW_CONTROLLER_IMPL_FFI_H