10826e83eSopenharmony_ci/*
20826e83eSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
30826e83eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40826e83eSopenharmony_ci * you may not use this file except in compliance with the License.
50826e83eSopenharmony_ci * You may obtain a copy of the License at
60826e83eSopenharmony_ci *
70826e83eSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
80826e83eSopenharmony_ci *
90826e83eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100826e83eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110826e83eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120826e83eSopenharmony_ci * See the License for the specific language governing permissions and
130826e83eSopenharmony_ci * limitations under the License.
140826e83eSopenharmony_ci */
150826e83eSopenharmony_ci
160826e83eSopenharmony_ci#include "webview_ffi.h"
170826e83eSopenharmony_ci
180826e83eSopenharmony_ci#include <regex>
190826e83eSopenharmony_ci
200826e83eSopenharmony_ci#include "webview_controller_impl.h"
210826e83eSopenharmony_ci#include "webview_function.h"
220826e83eSopenharmony_ci#include "web_download_item_impl.h"
230826e83eSopenharmony_ci#include "web_download_delegate_impl.h"
240826e83eSopenharmony_ci#include "web_download_manager_impl.h"
250826e83eSopenharmony_ci#include "webview_utils.h"
260826e83eSopenharmony_ci#include "webview_javascript_execute_callback.h"
270826e83eSopenharmony_ci#include "nweb_helper.h"
280826e83eSopenharmony_ci#include "nweb_init_params.h"
290826e83eSopenharmony_ci#include "web_errors.h"
300826e83eSopenharmony_ci#include "web_download.pb.h"
310826e83eSopenharmony_ci#include "web_storage.h"
320826e83eSopenharmony_ci#include "application_context.h"
330826e83eSopenharmony_ci#include "webview_log.h"
340826e83eSopenharmony_ci#include "parameters.h"
350826e83eSopenharmony_ci#include "web_cookie_manager.h"
360826e83eSopenharmony_ci#include "web_data_base.h"
370826e83eSopenharmony_ci#include "pixel_map.h"
380826e83eSopenharmony_ci#include "cj_lambda.h"
390826e83eSopenharmony_ci#include "pixel_map_impl.h"
400826e83eSopenharmony_ci#include "geolocation_permission.h"
410826e83eSopenharmony_ci#include <regex>
420826e83eSopenharmony_ci
430826e83eSopenharmony_ciusing namespace OHOS::FFI;
440826e83eSopenharmony_ciusing namespace OHOS::NWeb;
450826e83eSopenharmony_ci
460826e83eSopenharmony_cinamespace OHOS {
470826e83eSopenharmony_cinamespace Webview {
480826e83eSopenharmony_ci
490826e83eSopenharmony_ciconstexpr uint32_t SOCKET_MAXIMUM = 6;
500826e83eSopenharmony_ciconstexpr uint32_t URL_MAXIMUM = 2048;
510826e83eSopenharmony_ciconstexpr int INTEGER_TWO = 2;
520826e83eSopenharmony_ciconstexpr char URL_REGEXPR[] = "^http(s)?:\\/\\/.+";
530826e83eSopenharmony_ci
540826e83eSopenharmony_ciextern "C" {
550826e83eSopenharmony_ci    int32_t FfiOHOSWebviewOnce(char* type, void (*callbackRef)(void))
560826e83eSopenharmony_ci    {
570826e83eSopenharmony_ci        return FfiOnce(type, callbackRef);
580826e83eSopenharmony_ci    }
590826e83eSopenharmony_ci
600826e83eSopenharmony_ci    int64_t FfiOHOSWebviewCtlConstructor()
610826e83eSopenharmony_ci    {
620826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>();
630826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr) {
640826e83eSopenharmony_ci            WEBVIEWLOGE("new webview controller failed");
650826e83eSopenharmony_ci            return -1;
660826e83eSopenharmony_ci        }
670826e83eSopenharmony_ci        WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false);
680826e83eSopenharmony_ci        return nativeWebviewCtl->GetID();
690826e83eSopenharmony_ci    }
700826e83eSopenharmony_ci
710826e83eSopenharmony_ci    int64_t FfiOHOSWebviewCtlConstructorWithWebTag(char *cWebTag)
720826e83eSopenharmony_ci    {
730826e83eSopenharmony_ci        std::string webTag = cWebTag;
740826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>(webTag);
750826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr) {
760826e83eSopenharmony_ci            WEBVIEWLOGE("new webview controller failed");
770826e83eSopenharmony_ci            return -1;
780826e83eSopenharmony_ci        }
790826e83eSopenharmony_ci        WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false);
800826e83eSopenharmony_ci        return nativeWebviewCtl->GetID();
810826e83eSopenharmony_ci    }
820826e83eSopenharmony_ci
830826e83eSopenharmony_ci    void FfiOHOSWebviewCtlInitializeWebEngine()
840826e83eSopenharmony_ci    {
850826e83eSopenharmony_ci        std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
860826e83eSopenharmony_ci            AbilityRuntime::ApplicationContext::GetApplicationContext();
870826e83eSopenharmony_ci        if (ctx == nullptr) {
880826e83eSopenharmony_ci            WEBVIEWLOGE("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to ctx is null.");
890826e83eSopenharmony_ci            return;
900826e83eSopenharmony_ci        }
910826e83eSopenharmony_ci        const std::string& bundle_path = ctx->GetBundleCodeDir();
920826e83eSopenharmony_ci        NWebHelper::Instance().SetBundlePath(bundle_path);
930826e83eSopenharmony_ci        if (!NWebHelper::Instance().InitAndRun(true)) {
940826e83eSopenharmony_ci            WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to NWebHelper failure.");
950826e83eSopenharmony_ci        }
960826e83eSopenharmony_ci        WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine NWebHelper initialized, \
970826e83eSopenharmony_ci            init web engine done, bundle_path: %{public}s", bundle_path.c_str());
980826e83eSopenharmony_ci        return;
990826e83eSopenharmony_ci    }
1000826e83eSopenharmony_ci
1010826e83eSopenharmony_ci    void FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode, char* secureDnsConfig)
1020826e83eSopenharmony_ci    {
1030826e83eSopenharmony_ci        std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
1040826e83eSopenharmony_ci        config->SetMode(secureDnsMode);
1050826e83eSopenharmony_ci        config->SetConfig(secureDnsConfig);
1060826e83eSopenharmony_ci        WEBVIEWLOGI("set http dns mode:%{public}d doh_config:%{public}s", secureDnsMode, secureDnsConfig);
1070826e83eSopenharmony_ci        NWebHelper::Instance().SetHttpDns(config);
1080826e83eSopenharmony_ci    }
1090826e83eSopenharmony_ci
1100826e83eSopenharmony_ci    void FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess)
1110826e83eSopenharmony_ci    {
1120826e83eSopenharmony_ci        if (OHOS::system::GetBoolParameter("web.debug.devtools", false)) {
1130826e83eSopenharmony_ci            return;
1140826e83eSopenharmony_ci        }
1150826e83eSopenharmony_ci
1160826e83eSopenharmony_ci        WebviewControllerImpl::webDebuggingAccess_ = webDebuggingAccess;
1170826e83eSopenharmony_ci        return;
1180826e83eSopenharmony_ci    }
1190826e83eSopenharmony_ci
1200826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlLoadUrl(int64_t id, char *url)
1210826e83eSopenharmony_ci    {
1220826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1230826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1240826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
1250826e83eSopenharmony_ci        }
1260826e83eSopenharmony_ci        std::string webSrc = url;
1270826e83eSopenharmony_ci
1280826e83eSopenharmony_ci        return nativeWebviewCtl->LoadUrl(webSrc);
1290826e83eSopenharmony_ci    }
1300826e83eSopenharmony_ci
1310826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id, char *url, ArrWebHeader headers)
1320826e83eSopenharmony_ci    {
1330826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1340826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1350826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
1360826e83eSopenharmony_ci        }
1370826e83eSopenharmony_ci
1380826e83eSopenharmony_ci        std::map<std::string, std::string> httpHeaders;
1390826e83eSopenharmony_ci        uint32_t arrayLength = static_cast<uint32_t>(headers.size);
1400826e83eSopenharmony_ci        for (uint32_t i = 0; i < arrayLength; ++i) {
1410826e83eSopenharmony_ci            std::string key = headers.head[i].headerKey;
1420826e83eSopenharmony_ci            std::string value = headers.head[i].headerValue;
1430826e83eSopenharmony_ci            httpHeaders[key] = value;
1440826e83eSopenharmony_ci        }
1450826e83eSopenharmony_ci
1460826e83eSopenharmony_ci        return nativeWebviewCtl->LoadUrl(url, httpHeaders);
1470826e83eSopenharmony_ci    }
1480826e83eSopenharmony_ci
1490826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlLoadData(int64_t id, LoadDatas loadDatas)
1500826e83eSopenharmony_ci    {
1510826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1520826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1530826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
1540826e83eSopenharmony_ci        }
1550826e83eSopenharmony_ci        std::string data = loadDatas.cData;
1560826e83eSopenharmony_ci        std::string mimeType = loadDatas.cMimeType;
1570826e83eSopenharmony_ci        std::string encoding = loadDatas.cEncoding;
1580826e83eSopenharmony_ci        std::string baseUrl = loadDatas.cBaseUrl;
1590826e83eSopenharmony_ci        std::string historyUrl = loadDatas.cHistoryUrl;
1600826e83eSopenharmony_ci        return nativeWebviewCtl->LoadData(data, mimeType, encoding, baseUrl, historyUrl);
1610826e83eSopenharmony_ci    }
1620826e83eSopenharmony_ci
1630826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPreFetchPage(int64_t id, char *url)
1640826e83eSopenharmony_ci    {
1650826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1660826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1670826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
1680826e83eSopenharmony_ci        }
1690826e83eSopenharmony_ci        std::string webSrc = url;
1700826e83eSopenharmony_ci        if (webSrc.size() > URL_MAXIMUM) {
1710826e83eSopenharmony_ci            WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
1720826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
1730826e83eSopenharmony_ci        }
1740826e83eSopenharmony_ci
1750826e83eSopenharmony_ci        if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
1760826e83eSopenharmony_ci            WEBVIEWLOGE("ParsePrepareUrl error");
1770826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
1780826e83eSopenharmony_ci        }
1790826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->PreFetchPage(webSrc);
1800826e83eSopenharmony_ci        if (ret != NWebError::NO_ERROR) {
1810826e83eSopenharmony_ci            if (ret == NWebError::NWEB_ERROR) {
1820826e83eSopenharmony_ci                return ret;
1830826e83eSopenharmony_ci            }
1840826e83eSopenharmony_ci        }
1850826e83eSopenharmony_ci        return ret;
1860826e83eSopenharmony_ci    }
1870826e83eSopenharmony_ci
1880826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPreFetchPageWithHeaders(int64_t id, char *url, ArrWebHeader headers)
1890826e83eSopenharmony_ci    {
1900826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1910826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1920826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
1930826e83eSopenharmony_ci        }
1940826e83eSopenharmony_ci        std::string webSrc = url;
1950826e83eSopenharmony_ci        if (webSrc.size() > URL_MAXIMUM) {
1960826e83eSopenharmony_ci            WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
1970826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
1980826e83eSopenharmony_ci        }
1990826e83eSopenharmony_ci
2000826e83eSopenharmony_ci        if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
2010826e83eSopenharmony_ci            WEBVIEWLOGE("ParsePrepareUrl error");
2020826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
2030826e83eSopenharmony_ci        }
2040826e83eSopenharmony_ci        std::map<std::string, std::string> httpHeaders;
2050826e83eSopenharmony_ci        uint32_t arrayLength = static_cast<uint32_t>(headers.size);
2060826e83eSopenharmony_ci        for (uint32_t i = 0; i < arrayLength; ++i) {
2070826e83eSopenharmony_ci            std::string key = headers.head[i].headerKey;
2080826e83eSopenharmony_ci            std::string value = headers.head[i].headerValue;
2090826e83eSopenharmony_ci            httpHeaders[key] = value;
2100826e83eSopenharmony_ci        }
2110826e83eSopenharmony_ci
2120826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->PreFetchPage(webSrc, httpHeaders);
2130826e83eSopenharmony_ci        if (ret != NWebError::NO_ERROR) {
2140826e83eSopenharmony_ci            if (ret == NWebError::NWEB_ERROR) {
2150826e83eSopenharmony_ci                WEBVIEWLOGE("preFetchPage failed.");
2160826e83eSopenharmony_ci                return ret;
2170826e83eSopenharmony_ci            }
2180826e83eSopenharmony_ci        }
2190826e83eSopenharmony_ci        return ret;
2200826e83eSopenharmony_ci    }
2210826e83eSopenharmony_ci
2220826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSetAudioMuted(int64_t id, bool mute)
2230826e83eSopenharmony_ci    {
2240826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2250826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2260826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
2270826e83eSopenharmony_ci        }
2280826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->SetAudioMuted(mute);
2290826e83eSopenharmony_ci        if (ret != NWebError::NO_ERROR) {
2300826e83eSopenharmony_ci            if (ret == NWebError::NWEB_ERROR) {
2310826e83eSopenharmony_ci                WEBVIEWLOGE("SetAudioMuted failed, error code: %{public}d", ret);
2320826e83eSopenharmony_ci                return ret;
2330826e83eSopenharmony_ci            }
2340826e83eSopenharmony_ci        }
2350826e83eSopenharmony_ci        WEBVIEWLOGI("SetAudioMuted: %{public}s", (mute ? "true" : "false"));
2360826e83eSopenharmony_ci        return ret;
2370826e83eSopenharmony_ci    }
2380826e83eSopenharmony_ci
2390826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRefresh(int64_t id)
2400826e83eSopenharmony_ci    {
2410826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2420826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2430826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
2440826e83eSopenharmony_ci        }
2450826e83eSopenharmony_ci        nativeWebviewCtl->Refresh();
2460826e83eSopenharmony_ci        return NWebError::NO_ERROR;
2470826e83eSopenharmony_ci    }
2480826e83eSopenharmony_ci
2490826e83eSopenharmony_ci    char *FfiOHOSWebviewCtlGetUserAgent(int64_t id, int32_t *errCode)
2500826e83eSopenharmony_ci    {
2510826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2520826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2530826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
2540826e83eSopenharmony_ci            return nullptr;
2550826e83eSopenharmony_ci        }
2560826e83eSopenharmony_ci        std::string userAgent = nativeWebviewCtl->GetUserAgent();
2570826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
2580826e83eSopenharmony_ci        return MallocCString(userAgent);
2590826e83eSopenharmony_ci    }
2600826e83eSopenharmony_ci
2610826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlGetWebId(int64_t id, int32_t *errCode)
2620826e83eSopenharmony_ci    {
2630826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2640826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2650826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
2660826e83eSopenharmony_ci            return -1;
2670826e83eSopenharmony_ci        }
2680826e83eSopenharmony_ci        int32_t webId = -1;
2690826e83eSopenharmony_ci        webId = nativeWebviewCtl->GetWebId();
2700826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
2710826e83eSopenharmony_ci        return webId;
2720826e83eSopenharmony_ci    }
2730826e83eSopenharmony_ci
2740826e83eSopenharmony_ci    bool FfiOHOSWebviewCtlAccessForward(int64_t id, int32_t *errCode)
2750826e83eSopenharmony_ci    {
2760826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2770826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2780826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
2790826e83eSopenharmony_ci            return false;
2800826e83eSopenharmony_ci        }
2810826e83eSopenharmony_ci        bool access = nativeWebviewCtl->AccessForward();
2820826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
2830826e83eSopenharmony_ci        return access;
2840826e83eSopenharmony_ci    }
2850826e83eSopenharmony_ci
2860826e83eSopenharmony_ci    bool FfiOHOSWebviewCtlAccessBackward(int64_t id, int32_t *errCode)
2870826e83eSopenharmony_ci    {
2880826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2890826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2900826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
2910826e83eSopenharmony_ci            return false;
2920826e83eSopenharmony_ci        }
2930826e83eSopenharmony_ci        bool access = nativeWebviewCtl->AccessBackward();
2940826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
2950826e83eSopenharmony_ci        return access;
2960826e83eSopenharmony_ci    }
2970826e83eSopenharmony_ci
2980826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id, char *cUserAgent)
2990826e83eSopenharmony_ci    {
3000826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3010826e83eSopenharmony_ci        std::string userAgent = cUserAgent;
3020826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3030826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
3040826e83eSopenharmony_ci        }
3050826e83eSopenharmony_ci        return nativeWebviewCtl->SetCustomUserAgent(userAgent);
3060826e83eSopenharmony_ci    }
3070826e83eSopenharmony_ci
3080826e83eSopenharmony_ci    RetDataCString FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id)
3090826e83eSopenharmony_ci    {
3100826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
3110826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3120826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3130826e83eSopenharmony_ci            return ret;
3140826e83eSopenharmony_ci        }
3150826e83eSopenharmony_ci        std::string userAgent = "";
3160826e83eSopenharmony_ci        userAgent = nativeWebviewCtl->GetCustomUserAgent();
3170826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
3180826e83eSopenharmony_ci        ret.data = MallocCString(userAgent);
3190826e83eSopenharmony_ci        return ret;
3200826e83eSopenharmony_ci    }
3210826e83eSopenharmony_ci
3220826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRunJavaScript(int64_t id, char* cScript,
3230826e83eSopenharmony_ci        void (*callbackRef)(RetDataCString infoRef))
3240826e83eSopenharmony_ci    {
3250826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3260826e83eSopenharmony_ci        std::string script = std::string(cScript);
3270826e83eSopenharmony_ci        auto onChange = [lambda = CJLambda::Create(callbackRef)]
3280826e83eSopenharmony_ci            (RetDataCString infoRef) -> void { lambda(infoRef); };
3290826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3300826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
3310826e83eSopenharmony_ci        }
3320826e83eSopenharmony_ci        nativeWebviewCtl->RunJavaScript(script, onChange);
3330826e83eSopenharmony_ci        return NWebError::NO_ERROR;
3340826e83eSopenharmony_ci    }
3350826e83eSopenharmony_ci
3360826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRunJavaScriptExt(int64_t id, char* cScript,
3370826e83eSopenharmony_ci        void (*callbackRef)(RetDataI64))
3380826e83eSopenharmony_ci    {
3390826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3400826e83eSopenharmony_ci        std::string script = std::string(cScript);
3410826e83eSopenharmony_ci        auto onChange = CJLambda::Create(callbackRef);
3420826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3430826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
3440826e83eSopenharmony_ci        }
3450826e83eSopenharmony_ci        nativeWebviewCtl->RunJavaScriptExt(script, onChange);
3460826e83eSopenharmony_ci        return NWebError::NO_ERROR;
3470826e83eSopenharmony_ci    }
3480826e83eSopenharmony_ci
3490826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRunJavaScriptExtArr(int64_t id, CArrUI8 cScript,
3500826e83eSopenharmony_ci        void (*callbackRef)(RetDataI64))
3510826e83eSopenharmony_ci    {
3520826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3530826e83eSopenharmony_ci        std::string script = std::string(reinterpret_cast<char*>(cScript.head), cScript.size);
3540826e83eSopenharmony_ci        auto onChange = CJLambda::Create(callbackRef);
3550826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3560826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
3570826e83eSopenharmony_ci        }
3580826e83eSopenharmony_ci        nativeWebviewCtl->RunJavaScriptExt(script, onChange);
3590826e83eSopenharmony_ci        return NWebError::NO_ERROR;
3600826e83eSopenharmony_ci    }
3610826e83eSopenharmony_ci
3620826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id,
3630826e83eSopenharmony_ci        CArrI64 cFuncIds,  const char* cName, CArrString cMethodList)
3640826e83eSopenharmony_ci    {
3650826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3660826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3670826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
3680826e83eSopenharmony_ci        }
3690826e83eSopenharmony_ci        std::string objName = std::string(cName);
3700826e83eSopenharmony_ci        std::vector<std::string> methodList;
3710826e83eSopenharmony_ci        for (int64_t i = 0; i < cMethodList.size; i++) {
3720826e83eSopenharmony_ci            methodList.push_back(std::string(cMethodList.head[i]));
3730826e83eSopenharmony_ci        }
3740826e83eSopenharmony_ci        std::vector<std::function<char*(const char*)>> cjFuncs;
3750826e83eSopenharmony_ci        for (int64_t i = 0; i < cFuncIds.size; i++) {
3760826e83eSopenharmony_ci            auto cFunc = reinterpret_cast<char*(*)(const char*)>(cFuncIds.head[i]);
3770826e83eSopenharmony_ci            auto onChange = [lambda = CJLambda::Create(cFunc)]
3780826e83eSopenharmony_ci                (const char* infoRef) -> char* { return lambda(infoRef); };
3790826e83eSopenharmony_ci            cjFuncs.push_back(onChange);
3800826e83eSopenharmony_ci        }
3810826e83eSopenharmony_ci        nativeWebviewCtl->SetNWebJavaScriptResultCallBack();
3820826e83eSopenharmony_ci        nativeWebviewCtl->RegisterJavaScriptProxy(cjFuncs, objName, methodList);
3830826e83eSopenharmony_ci        return NWebError::NO_ERROR;
3840826e83eSopenharmony_ci    }
3850826e83eSopenharmony_ci
3860826e83eSopenharmony_ci    RetDataCString FfiOHOSWebviewCtlGetUrl(int64_t id)
3870826e83eSopenharmony_ci    {
3880826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
3890826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
3900826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
3910826e83eSopenharmony_ci            return ret;
3920826e83eSopenharmony_ci        }
3930826e83eSopenharmony_ci        std::string url = "";
3940826e83eSopenharmony_ci        url = nativeWebviewCtl->GetUrl();
3950826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
3960826e83eSopenharmony_ci        ret.data = MallocCString(url);
3970826e83eSopenharmony_ci        return ret;
3980826e83eSopenharmony_ci    }
3990826e83eSopenharmony_ci
4000826e83eSopenharmony_ci    RetDataCString FfiOHOSWebviewCtlGetOriginalUrl(int64_t id)
4010826e83eSopenharmony_ci    {
4020826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
4030826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
4040826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
4050826e83eSopenharmony_ci            return ret;
4060826e83eSopenharmony_ci        }
4070826e83eSopenharmony_ci        std::string url = "";
4080826e83eSopenharmony_ci        url = nativeWebviewCtl->GetOriginalUrl();
4090826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
4100826e83eSopenharmony_ci        ret.data = MallocCString(url);
4110826e83eSopenharmony_ci        return ret;
4120826e83eSopenharmony_ci    }
4130826e83eSopenharmony_ci
4140826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPageUp(int64_t id, bool top)
4150826e83eSopenharmony_ci    {
4160826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
4170826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
4180826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
4190826e83eSopenharmony_ci        }
4200826e83eSopenharmony_ci        nativeWebviewCtl->ScrollPageUp(top);
4210826e83eSopenharmony_ci        return NWebError::NO_ERROR;
4220826e83eSopenharmony_ci    }
4230826e83eSopenharmony_ci
4240826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPageDown(int64_t id, bool bottom)
4250826e83eSopenharmony_ci    {
4260826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
4270826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
4280826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
4290826e83eSopenharmony_ci        }
4300826e83eSopenharmony_ci        nativeWebviewCtl->ScrollPageDown(bottom);
4310826e83eSopenharmony_ci        return NWebError::NO_ERROR;
4320826e83eSopenharmony_ci    }
4330826e83eSopenharmony_ci
4340826e83eSopenharmony_ci    // cookie_manager
4350826e83eSopenharmony_ci    const char* FfiOHOSCookieMgrFetchCookieSync(const char *url, bool incognitoMode, int32_t* errCode)
4360826e83eSopenharmony_ci    {
4370826e83eSopenharmony_ci        std::string curl = url;
4380826e83eSopenharmony_ci        std::string value = OHOS::NWeb::WebCookieManager::CjGetCookie(curl, incognitoMode, *errCode);
4390826e83eSopenharmony_ci        const char* res = MallocCString(value);
4400826e83eSopenharmony_ci        return res;
4410826e83eSopenharmony_ci    }
4420826e83eSopenharmony_ci
4430826e83eSopenharmony_ci    int32_t FfiOHOSCookieMgrConfigCookieSync(const char* url, const char* value, bool incognitoMode)
4440826e83eSopenharmony_ci    {
4450826e83eSopenharmony_ci        std::string curl = url;
4460826e83eSopenharmony_ci        std::string cvalue = value;
4470826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjSetCookie(curl, cvalue, incognitoMode);
4480826e83eSopenharmony_ci    }
4490826e83eSopenharmony_ci
4500826e83eSopenharmony_ci    void FfiOHOSCookieMgrPutAcceptCookieEnabled(bool accept)
4510826e83eSopenharmony_ci    {
4520826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjPutAcceptCookieEnabled(accept);
4530826e83eSopenharmony_ci    }
4540826e83eSopenharmony_ci
4550826e83eSopenharmony_ci    bool FfiOHOSCookieMgrIsCookieAllowed()
4560826e83eSopenharmony_ci    {
4570826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjIsCookieAllowed();
4580826e83eSopenharmony_ci    }
4590826e83eSopenharmony_ci
4600826e83eSopenharmony_ci    void FfiOHOSCookieMgrPutAcceptThirdPartyCookieEnabled(bool accept)
4610826e83eSopenharmony_ci    {
4620826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjPutAcceptThirdPartyCookieEnabled(accept);
4630826e83eSopenharmony_ci    }
4640826e83eSopenharmony_ci
4650826e83eSopenharmony_ci    bool FfiOHOSCookieMgrIsThirdPartyCookieAllowed()
4660826e83eSopenharmony_ci    {
4670826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjIsThirdPartyCookieAllowed();
4680826e83eSopenharmony_ci    }
4690826e83eSopenharmony_ci
4700826e83eSopenharmony_ci    bool FfiOHOSCookieMgrExistCookie(bool incognitoMode)
4710826e83eSopenharmony_ci    {
4720826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjExistCookie(incognitoMode);
4730826e83eSopenharmony_ci    }
4740826e83eSopenharmony_ci
4750826e83eSopenharmony_ci    void FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode)
4760826e83eSopenharmony_ci    {
4770826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjDeleteEntireCookie(incognitoMode);
4780826e83eSopenharmony_ci    }
4790826e83eSopenharmony_ci
4800826e83eSopenharmony_ci    void FfiOHOSCookieMgrClearSessionCookieSync()
4810826e83eSopenharmony_ci    {
4820826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjDeleteSessionCookie();
4830826e83eSopenharmony_ci    }
4840826e83eSopenharmony_ci
4850826e83eSopenharmony_ci    void FfiOHOSCookieMgrSaveCookieAsync(void (*callbackRef)(void))
4860826e83eSopenharmony_ci    {
4870826e83eSopenharmony_ci        return OHOS::NWeb::WebCookieManager::CjSaveCookie(callbackRef);
4880826e83eSopenharmony_ci    }
4890826e83eSopenharmony_ci
4900826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlScrollTo(int64_t id, float x, float y)
4910826e83eSopenharmony_ci    {
4920826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
4930826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
4940826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
4950826e83eSopenharmony_ci        }
4960826e83eSopenharmony_ci        nativeWebviewCtl->ScrollTo(x, y);
4970826e83eSopenharmony_ci        return NWebError::NO_ERROR;
4980826e83eSopenharmony_ci    }
4990826e83eSopenharmony_ci
5000826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlScrollBy(int64_t id, float deltaX, float deltaY)
5010826e83eSopenharmony_ci    {
5020826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5030826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5040826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5050826e83eSopenharmony_ci        }
5060826e83eSopenharmony_ci        nativeWebviewCtl->ScrollBy(deltaX, deltaY);
5070826e83eSopenharmony_ci        return NWebError::NO_ERROR;
5080826e83eSopenharmony_ci    }
5090826e83eSopenharmony_ci
5100826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlScrollToWithAnime(int64_t id, float x, float y, int32_t duration)
5110826e83eSopenharmony_ci    {
5120826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5130826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5140826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5150826e83eSopenharmony_ci        }
5160826e83eSopenharmony_ci        if (duration > 0) {
5170826e83eSopenharmony_ci            nativeWebviewCtl->ScrollToWithAnime(x, y, duration);
5180826e83eSopenharmony_ci        } else {
5190826e83eSopenharmony_ci            nativeWebviewCtl->ScrollTo(x, y);
5200826e83eSopenharmony_ci        }
5210826e83eSopenharmony_ci        return NWebError::NO_ERROR;
5220826e83eSopenharmony_ci    }
5230826e83eSopenharmony_ci
5240826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlScrollByWithAnime(int64_t id, float deltaX, float deltaY, int32_t duration)
5250826e83eSopenharmony_ci    {
5260826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5270826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5280826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5290826e83eSopenharmony_ci        }
5300826e83eSopenharmony_ci        if (duration > 0) {
5310826e83eSopenharmony_ci            nativeWebviewCtl->ScrollByWithAnime(deltaX, deltaY, duration);
5320826e83eSopenharmony_ci        } else {
5330826e83eSopenharmony_ci            nativeWebviewCtl->ScrollBy(deltaX, deltaY);
5340826e83eSopenharmony_ci        }
5350826e83eSopenharmony_ci        return NWebError::NO_ERROR;
5360826e83eSopenharmony_ci    }
5370826e83eSopenharmony_ci
5380826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlForward(int64_t id)
5390826e83eSopenharmony_ci    {
5400826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5410826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5420826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5430826e83eSopenharmony_ci        }
5440826e83eSopenharmony_ci        nativeWebviewCtl->Forward();
5450826e83eSopenharmony_ci        return NWebError::NO_ERROR;
5460826e83eSopenharmony_ci    }
5470826e83eSopenharmony_ci
5480826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlBackward(int64_t id)
5490826e83eSopenharmony_ci    {
5500826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5510826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5520826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5530826e83eSopenharmony_ci        }
5540826e83eSopenharmony_ci        nativeWebviewCtl->Backward();
5550826e83eSopenharmony_ci        return NWebError::NO_ERROR;
5560826e83eSopenharmony_ci    }
5570826e83eSopenharmony_ci
5580826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlBackOrForward(int64_t id, int32_t step)
5590826e83eSopenharmony_ci    {
5600826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5610826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5620826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5630826e83eSopenharmony_ci        }
5640826e83eSopenharmony_ci        return nativeWebviewCtl->BackOrForward(step);
5650826e83eSopenharmony_ci    }
5660826e83eSopenharmony_ci
5670826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlGetPageHeight(int64_t id, int32_t *errCode)
5680826e83eSopenharmony_ci    {
5690826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5700826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5710826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
5720826e83eSopenharmony_ci            return -1;
5730826e83eSopenharmony_ci        }
5740826e83eSopenharmony_ci        int32_t pageHeight = nativeWebviewCtl->GetPageHeight();
5750826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
5760826e83eSopenharmony_ci        return pageHeight;
5770826e83eSopenharmony_ci    }
5780826e83eSopenharmony_ci
5790826e83eSopenharmony_ci    RetDataCString FfiOHOSWebviewCtlGetTitle(int64_t id)
5800826e83eSopenharmony_ci    {
5810826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
5820826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5830826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5840826e83eSopenharmony_ci            return ret;
5850826e83eSopenharmony_ci        }
5860826e83eSopenharmony_ci        std::string title = "";
5870826e83eSopenharmony_ci        title = nativeWebviewCtl->GetTitle();
5880826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
5890826e83eSopenharmony_ci        ret.data = MallocCString(title);
5900826e83eSopenharmony_ci        return ret;
5910826e83eSopenharmony_ci    }
5920826e83eSopenharmony_ci
5930826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlZoom(int64_t id, float factor)
5940826e83eSopenharmony_ci    {
5950826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
5960826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
5970826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
5980826e83eSopenharmony_ci        }
5990826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->Zoom(factor);
6000826e83eSopenharmony_ci        return ret;
6010826e83eSopenharmony_ci    }
6020826e83eSopenharmony_ci
6030826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlZoomIn(int64_t id)
6040826e83eSopenharmony_ci    {
6050826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6060826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6070826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
6080826e83eSopenharmony_ci        }
6090826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->ZoomIn();
6100826e83eSopenharmony_ci        return ret;
6110826e83eSopenharmony_ci    }
6120826e83eSopenharmony_ci
6130826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlZoomOut(int64_t id)
6140826e83eSopenharmony_ci    {
6150826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6160826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6170826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
6180826e83eSopenharmony_ci        }
6190826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->ZoomOut();
6200826e83eSopenharmony_ci        return ret;
6210826e83eSopenharmony_ci    }
6220826e83eSopenharmony_ci
6230826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRequestFocus(int64_t id)
6240826e83eSopenharmony_ci    {
6250826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6260826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6270826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
6280826e83eSopenharmony_ci        }
6290826e83eSopenharmony_ci        int32_t ret = nativeWebviewCtl->RequestFocus();
6300826e83eSopenharmony_ci        return ret;
6310826e83eSopenharmony_ci    }
6320826e83eSopenharmony_ci
6330826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlClearHistory(int64_t id)
6340826e83eSopenharmony_ci    {
6350826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6360826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6370826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
6380826e83eSopenharmony_ci        }
6390826e83eSopenharmony_ci        nativeWebviewCtl->ClearHistory();
6400826e83eSopenharmony_ci        return NWebError::NO_ERROR;
6410826e83eSopenharmony_ci    }
6420826e83eSopenharmony_ci
6430826e83eSopenharmony_ci    bool FfiOHOSWebviewCtlAccessStep(int64_t id, int32_t *errCode, int32_t step)
6440826e83eSopenharmony_ci    {
6450826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6460826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6470826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
6480826e83eSopenharmony_ci            return false;
6490826e83eSopenharmony_ci        }
6500826e83eSopenharmony_ci        bool access = nativeWebviewCtl->AccessStep(step);
6510826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
6520826e83eSopenharmony_ci        return access;
6530826e83eSopenharmony_ci    }
6540826e83eSopenharmony_ci
6550826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlOnActive(int64_t id)
6560826e83eSopenharmony_ci    {
6570826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6580826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6590826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
6600826e83eSopenharmony_ci        }
6610826e83eSopenharmony_ci        nativeWebviewCtl->OnActive();
6620826e83eSopenharmony_ci        return NWebError::NO_ERROR;
6630826e83eSopenharmony_ci    }
6640826e83eSopenharmony_ci
6650826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlOnInactive(int64_t id)
6660826e83eSopenharmony_ci    {
6670826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6680826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6690826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
6700826e83eSopenharmony_ci        }
6710826e83eSopenharmony_ci        nativeWebviewCtl->OnInactive();
6720826e83eSopenharmony_ci        return NWebError::NO_ERROR;
6730826e83eSopenharmony_ci    }
6740826e83eSopenharmony_ci
6750826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlGetHitTest(int64_t id, int32_t *errCode)
6760826e83eSopenharmony_ci    {
6770826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6780826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6790826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
6800826e83eSopenharmony_ci            return -1;
6810826e83eSopenharmony_ci        }
6820826e83eSopenharmony_ci        int32_t type = nativeWebviewCtl->GetHitTest();
6830826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
6840826e83eSopenharmony_ci        return type;
6850826e83eSopenharmony_ci    }
6860826e83eSopenharmony_ci
6870826e83eSopenharmony_ci    RetDataCString FfiOHOSWebviewCtlGetHitTestValue(int64_t id, int32_t *errCode)
6880826e83eSopenharmony_ci    {
6890826e83eSopenharmony_ci        RetDataCString ret = { .code = NWeb::HitTestResult::UNKNOWN_TYPE, .data = nullptr };
6900826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
6910826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
6920826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
6930826e83eSopenharmony_ci            return ret;
6940826e83eSopenharmony_ci        }
6950826e83eSopenharmony_ci        std::shared_ptr<NWeb::HitTestResult> nwebResult = nativeWebviewCtl->GetHitTestValue();
6960826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
6970826e83eSopenharmony_ci        ret.code = nwebResult->GetType();
6980826e83eSopenharmony_ci        ret.data = MallocCString(nwebResult->GetExtra());
6990826e83eSopenharmony_ci        return ret;
7000826e83eSopenharmony_ci    }
7010826e83eSopenharmony_ci
7020826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlStoreWebArchive(int64_t id, const char* cBaseName,
7030826e83eSopenharmony_ci        bool autoName, void (*callbackRef)(RetDataCString infoRef))
7040826e83eSopenharmony_ci    {
7050826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7060826e83eSopenharmony_ci        std::string baseName = std::string(cBaseName);
7070826e83eSopenharmony_ci        auto onChange = [lambda = CJLambda::Create(callbackRef)]
7080826e83eSopenharmony_ci            (RetDataCString infoRef) -> void { lambda(infoRef); };
7090826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7100826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
7110826e83eSopenharmony_ci        }
7120826e83eSopenharmony_ci        nativeWebviewCtl->StoreWebArchiveCallback(baseName, autoName, onChange);
7130826e83eSopenharmony_ci        return NWebError::NO_ERROR;
7140826e83eSopenharmony_ci    }
7150826e83eSopenharmony_ci
7160826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id, bool enable)
7170826e83eSopenharmony_ci    {
7180826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7190826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7200826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
7210826e83eSopenharmony_ci        }
7220826e83eSopenharmony_ci        nativeWebviewCtl->EnableSafeBrowsing(enable);
7230826e83eSopenharmony_ci        return NWebError::NO_ERROR;
7240826e83eSopenharmony_ci    }
7250826e83eSopenharmony_ci
7260826e83eSopenharmony_ci    bool FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id, int32_t *errCode)
7270826e83eSopenharmony_ci    {
7280826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7290826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7300826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
7310826e83eSopenharmony_ci            return false;
7320826e83eSopenharmony_ci        }
7330826e83eSopenharmony_ci        bool isSafeBrowsingEnabled = nativeWebviewCtl->IsSafeBrowsingEnabled();
7340826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
7350826e83eSopenharmony_ci        return isSafeBrowsingEnabled;
7360826e83eSopenharmony_ci    }
7370826e83eSopenharmony_ci
7380826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlGetSecurityLevel(int64_t id, int32_t *errCode)
7390826e83eSopenharmony_ci    {
7400826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7410826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7420826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
7430826e83eSopenharmony_ci            return -1;
7440826e83eSopenharmony_ci        }
7450826e83eSopenharmony_ci        int32_t securityLevel = nativeWebviewCtl->GetSecurityLevel();
7460826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
7470826e83eSopenharmony_ci        return securityLevel;
7480826e83eSopenharmony_ci    }
7490826e83eSopenharmony_ci
7500826e83eSopenharmony_ci    bool FfiOHOSWebviewCtlIsIncognitoMode(int64_t id, int32_t *errCode)
7510826e83eSopenharmony_ci    {
7520826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7530826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7540826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
7550826e83eSopenharmony_ci            return false;
7560826e83eSopenharmony_ci        }
7570826e83eSopenharmony_ci        bool incognitoMode = nativeWebviewCtl->IsIncognitoMode();
7580826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
7590826e83eSopenharmony_ci        return incognitoMode;
7600826e83eSopenharmony_ci    }
7610826e83eSopenharmony_ci
7620826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRemoveCache(int64_t id, bool clearRom)
7630826e83eSopenharmony_ci    {
7640826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7650826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7660826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
7670826e83eSopenharmony_ci        }
7680826e83eSopenharmony_ci        nativeWebviewCtl->RemoveCache(clearRom);
7690826e83eSopenharmony_ci        return NWebError::NO_ERROR;
7700826e83eSopenharmony_ci    }
7710826e83eSopenharmony_ci
7720826e83eSopenharmony_ci    int64_t FfiOHOSWebviewCtlGetBackForwardEntries(int64_t id, int32_t *errCode)
7730826e83eSopenharmony_ci    {
7740826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7750826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
7760826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
7770826e83eSopenharmony_ci            return -1;
7780826e83eSopenharmony_ci        }
7790826e83eSopenharmony_ci
7800826e83eSopenharmony_ci        std::shared_ptr<NWebHistoryList> list = nativeWebviewCtl->GetHistoryList();
7810826e83eSopenharmony_ci        if (!list) {
7820826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
7830826e83eSopenharmony_ci            return -1;
7840826e83eSopenharmony_ci        }
7850826e83eSopenharmony_ci
7860826e83eSopenharmony_ci        auto nativeWebHistoryList = FFIData::Create<WebHistoryListImpl>(list);
7870826e83eSopenharmony_ci        if (nativeWebHistoryList == nullptr) {
7880826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
7890826e83eSopenharmony_ci            WEBVIEWLOGE("new WebHistoryList failed");
7900826e83eSopenharmony_ci            return -1;
7910826e83eSopenharmony_ci        }
7920826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
7930826e83eSopenharmony_ci        return nativeWebHistoryList->GetID();
7940826e83eSopenharmony_ci    }
7950826e83eSopenharmony_ci
7960826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlStop(int64_t id)
7970826e83eSopenharmony_ci    {
7980826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
7990826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
8000826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
8010826e83eSopenharmony_ci        }
8020826e83eSopenharmony_ci        nativeWebviewCtl->Stop();
8030826e83eSopenharmony_ci        return NWebError::NO_ERROR;
8040826e83eSopenharmony_ci    }
8050826e83eSopenharmony_ci
8060826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPostUrl(int64_t id, char *url, CArrUI8 buffer)
8070826e83eSopenharmony_ci    {
8080826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
8090826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
8100826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
8110826e83eSopenharmony_ci        }
8120826e83eSopenharmony_ci        std::string sUrl = url;
8130826e83eSopenharmony_ci        std::vector<char> postData(buffer.head, buffer.head + buffer.size);
8140826e83eSopenharmony_ci        return nativeWebviewCtl->PostUrl(sUrl, postData);
8150826e83eSopenharmony_ci    }
8160826e83eSopenharmony_ci
8170826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSetDownloadDelegate(int64_t id, int64_t delegateId)
8180826e83eSopenharmony_ci    {
8190826e83eSopenharmony_ci        NWebHelper::Instance().LoadNWebSDK();
8200826e83eSopenharmony_ci        auto delegate = FFIData::GetData<WebDownloadDelegateImpl>(delegateId);
8210826e83eSopenharmony_ci        if (!delegate) {
8220826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
8230826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
8240826e83eSopenharmony_ci        }
8250826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
8260826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
8270826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
8280826e83eSopenharmony_ci        }
8290826e83eSopenharmony_ci        int32_t nwebId = nativeWebviewCtl->GetWebId();
8300826e83eSopenharmony_ci        WebDownloadManagerImpl::AddDownloadDelegateForWeb(nwebId, delegate);
8310826e83eSopenharmony_ci        return NWebError::NO_ERROR;
8320826e83eSopenharmony_ci    }
8330826e83eSopenharmony_ci
8340826e83eSopenharmony_ci    bool ParsePrepareUrl(std::string& url)
8350826e83eSopenharmony_ci    {
8360826e83eSopenharmony_ci        if (url.size() > URL_MAXIMUM) {
8370826e83eSopenharmony_ci            WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
8380826e83eSopenharmony_ci            return false;
8390826e83eSopenharmony_ci        }
8400826e83eSopenharmony_ci        if (!regex_match(url, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
8410826e83eSopenharmony_ci            WEBVIEWLOGE("ParsePrepareUrl error");
8420826e83eSopenharmony_ci            return false;
8430826e83eSopenharmony_ci        }
8440826e83eSopenharmony_ci        return true;
8450826e83eSopenharmony_ci    }
8460826e83eSopenharmony_ci
8470826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlStartDownload(int64_t id, char *url)
8480826e83eSopenharmony_ci    {
8490826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
8500826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
8510826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
8520826e83eSopenharmony_ci        }
8530826e83eSopenharmony_ci        std::string webSrc = url;
8540826e83eSopenharmony_ci        if (!ParsePrepareUrl(webSrc)) {
8550826e83eSopenharmony_ci            return NWebError::INVALID_URL;
8560826e83eSopenharmony_ci        }
8570826e83eSopenharmony_ci        int32_t nwebId = nativeWebviewCtl->GetWebId();
8580826e83eSopenharmony_ci        NWebHelper::Instance().LoadNWebSDK();
8590826e83eSopenharmony_ci        WebDownloader_StartDownload(nwebId, webSrc.c_str());
8600826e83eSopenharmony_ci        return NWebError::NO_ERROR;
8610826e83eSopenharmony_ci    }
8620826e83eSopenharmony_ci
8630826e83eSopenharmony_ci    CArrI64 FfiOHOSWebviewCtlCreateWebMessagePorts(int64_t id, bool isExtentionType, int32_t *errCode)
8640826e83eSopenharmony_ci    {
8650826e83eSopenharmony_ci        CArrI64 messagePorts = {.head = nullptr, .size = 0};
8660826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
8670826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
8680826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
8690826e83eSopenharmony_ci            return messagePorts;
8700826e83eSopenharmony_ci        }
8710826e83eSopenharmony_ci
8720826e83eSopenharmony_ci        int32_t nwebId = nativeWebviewCtl->GetWebId();
8730826e83eSopenharmony_ci        std::vector<std::string> ports = nativeWebviewCtl->CreateWebMessagePorts();
8740826e83eSopenharmony_ci        if (ports.size() != INTEGER_TWO) {
8750826e83eSopenharmony_ci            WEBVIEWLOGE("create web message port failed");
8760826e83eSopenharmony_ci            *errCode = NWebError::CAN_NOT_POST_MESSAGE;
8770826e83eSopenharmony_ci            return messagePorts;
8780826e83eSopenharmony_ci        }
8790826e83eSopenharmony_ci        auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * INTEGER_TWO));
8800826e83eSopenharmony_ci        if (!arr) {
8810826e83eSopenharmony_ci            WEBVIEWLOGE("FfiOHOSWebviewCtlCreateWebMessagePorts failed to malloc arr.");
8820826e83eSopenharmony_ci            *errCode = NWebError::NEW_OOM;
8830826e83eSopenharmony_ci            return messagePorts;
8840826e83eSopenharmony_ci        }
8850826e83eSopenharmony_ci        for (uint32_t i = 0; i < INTEGER_TWO; i++) {
8860826e83eSopenharmony_ci            auto nativeWebMessagePort = FFIData::Create<WebMessagePortImpl>(nwebId, ports[i], isExtentionType);
8870826e83eSopenharmony_ci            if (nativeWebMessagePort == nullptr) {
8880826e83eSopenharmony_ci                *errCode = NWebError::CAN_NOT_POST_MESSAGE;
8890826e83eSopenharmony_ci                WEBVIEWLOGE("new nativeWebMessagePort failed");
8900826e83eSopenharmony_ci                free(arr);
8910826e83eSopenharmony_ci                return messagePorts;
8920826e83eSopenharmony_ci            }
8930826e83eSopenharmony_ci            arr[i] = nativeWebMessagePort->GetID();
8940826e83eSopenharmony_ci        }
8950826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
8960826e83eSopenharmony_ci        messagePorts.head = arr;
8970826e83eSopenharmony_ci        messagePorts.size = INTEGER_TWO;
8980826e83eSopenharmony_ci        return messagePorts;
8990826e83eSopenharmony_ci    }
9000826e83eSopenharmony_ci
9010826e83eSopenharmony_ci    int32_t GetSendPorts(CArrI64 ports, std::vector<std::string>& sendPorts)
9020826e83eSopenharmony_ci    {
9030826e83eSopenharmony_ci        int64_t arrayLen = ports.size;
9040826e83eSopenharmony_ci        if (arrayLen == 0) {
9050826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
9060826e83eSopenharmony_ci        }
9070826e83eSopenharmony_ci        int64_t* portsId = ports.head;
9080826e83eSopenharmony_ci        if (!portsId) {
9090826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
9100826e83eSopenharmony_ci        }
9110826e83eSopenharmony_ci        for (int64_t i = 0; i < arrayLen; i++) {
9120826e83eSopenharmony_ci            WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(portsId[i]);
9130826e83eSopenharmony_ci            if ((!msgPort)) {
9140826e83eSopenharmony_ci                return NWebError::PARAM_CHECK_ERROR;
9150826e83eSopenharmony_ci            }
9160826e83eSopenharmony_ci            std::string portHandle = msgPort->GetPortHandle();
9170826e83eSopenharmony_ci            sendPorts.emplace_back(portHandle);
9180826e83eSopenharmony_ci        }
9190826e83eSopenharmony_ci        return NWebError::NO_ERROR;
9200826e83eSopenharmony_ci    }
9210826e83eSopenharmony_ci
9220826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPostMessage(int64_t id, char* name, CArrI64 ports, char* uri)
9230826e83eSopenharmony_ci    {
9240826e83eSopenharmony_ci        WEBVIEWLOGD("post message port");
9250826e83eSopenharmony_ci        std::string portName = std::string(name);
9260826e83eSopenharmony_ci        std::vector<std::string> sendPorts;
9270826e83eSopenharmony_ci        int32_t ret = GetSendPorts(ports, sendPorts);
9280826e83eSopenharmony_ci        if (ret != NWebError::NO_ERROR) {
9290826e83eSopenharmony_ci            WEBVIEWLOGE("post port to html failed, getSendPorts fail");
9300826e83eSopenharmony_ci            return ret;
9310826e83eSopenharmony_ci        }
9320826e83eSopenharmony_ci        std::string urlStr = std::string(uri);
9330826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
9340826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
9350826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
9360826e83eSopenharmony_ci        }
9370826e83eSopenharmony_ci        nativeWebviewCtl->PostWebMessage(portName, sendPorts, urlStr);
9380826e83eSopenharmony_ci        return NWebError::NO_ERROR;
9390826e83eSopenharmony_ci    }
9400826e83eSopenharmony_ci
9410826e83eSopenharmony_ci    CArrUI8 FfiOHOSWebviewCtlSerializeWebState(int64_t id, int32_t *errCode)
9420826e83eSopenharmony_ci    {
9430826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
9440826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
9450826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
9460826e83eSopenharmony_ci            return CArrUI8{nullptr, 0};
9470826e83eSopenharmony_ci        }
9480826e83eSopenharmony_ci        std::vector<uint8_t> webState = nativeWebviewCtl->SerializeWebState();
9490826e83eSopenharmony_ci        uint8_t* result = VectorToCArrUI8(webState);
9500826e83eSopenharmony_ci        if (result == nullptr) {
9510826e83eSopenharmony_ci            WEBVIEWLOGE("FfiOHOSWebviewCtlSerializeWebStatee malloc failed");
9520826e83eSopenharmony_ci            *errCode = NWebError::NEW_OOM;
9530826e83eSopenharmony_ci            return CArrUI8{nullptr, 0};
9540826e83eSopenharmony_ci        }
9550826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
9560826e83eSopenharmony_ci        return CArrUI8{result, webState.size()};
9570826e83eSopenharmony_ci    }
9580826e83eSopenharmony_ci
9590826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlRestoreWebState(int64_t id, CArrUI8 cState)
9600826e83eSopenharmony_ci    {
9610826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
9620826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
9630826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
9640826e83eSopenharmony_ci        }
9650826e83eSopenharmony_ci        std::vector<uint8_t> state(cState.head, cState.head + cState.size);
9660826e83eSopenharmony_ci        nativeWebviewCtl->RestoreWebState(state);
9670826e83eSopenharmony_ci        return NWebError::NO_ERROR;
9680826e83eSopenharmony_ci    }
9690826e83eSopenharmony_ci
9700826e83eSopenharmony_ci    CArrString FfiOHOSWebviewCtlGetCertificate(int64_t id, int32_t *errCode)
9710826e83eSopenharmony_ci    {
9720826e83eSopenharmony_ci        CArrString arrCertificate = {.head = nullptr, .size = 0};
9730826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
9740826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
9750826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
9760826e83eSopenharmony_ci            return arrCertificate;
9770826e83eSopenharmony_ci        }
9780826e83eSopenharmony_ci        std::vector<std::string> certChainDerData;
9790826e83eSopenharmony_ci        bool ans = nativeWebviewCtl->GetCertChainDerData(certChainDerData);
9800826e83eSopenharmony_ci        if (!ans) {
9810826e83eSopenharmony_ci            WEBVIEWLOGE("get cert chain data failed");
9820826e83eSopenharmony_ci            return arrCertificate;
9830826e83eSopenharmony_ci        }
9840826e83eSopenharmony_ci        if (certChainDerData.size() > UINT8_MAX) {
9850826e83eSopenharmony_ci            WEBVIEWLOGE("error, cert chain data array reach max");
9860826e83eSopenharmony_ci            return arrCertificate;
9870826e83eSopenharmony_ci        }
9880826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
9890826e83eSopenharmony_ci        arrCertificate.size = static_cast<int64_t>(certChainDerData.size());
9900826e83eSopenharmony_ci        arrCertificate.head = OHOS::Webview::VectorToCArrString(certChainDerData);
9910826e83eSopenharmony_ci        return arrCertificate;
9920826e83eSopenharmony_ci    }
9930826e83eSopenharmony_ci
9940826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlHasImage(int64_t id, void (*callbackRef)(RetDataBool))
9950826e83eSopenharmony_ci    {
9960826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
9970826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
9980826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
9990826e83eSopenharmony_ci        }
10000826e83eSopenharmony_ci        return nativeWebviewCtl->HasImagesCallback(CJLambda::Create(callbackRef));
10010826e83eSopenharmony_ci    }
10020826e83eSopenharmony_ci
10030826e83eSopenharmony_ci    int32_t FfiWebviewCtlCustomizeSchemes(OHOS::Webview::CArrScheme schemes)
10040826e83eSopenharmony_ci    {
10050826e83eSopenharmony_ci        return WebviewControllerImpl::CustomizeSchemesArrayDataHandler(schemes);
10060826e83eSopenharmony_ci    }
10070826e83eSopenharmony_ci
10080826e83eSopenharmony_ci    // BackForwardList
10090826e83eSopenharmony_ci    int32_t FfiOHOSBackForwardListCurrentIndex(int64_t id, int32_t *errCode)
10100826e83eSopenharmony_ci    {
10110826e83eSopenharmony_ci        auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
10120826e83eSopenharmony_ci        if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) {
10130826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
10140826e83eSopenharmony_ci            return -1;
10150826e83eSopenharmony_ci        }
10160826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
10170826e83eSopenharmony_ci        return nativeWebHistoryListImpl->GetCurrentIndex();
10180826e83eSopenharmony_ci    }
10190826e83eSopenharmony_ci
10200826e83eSopenharmony_ci    int32_t FfiOHOSBackForwardListSize(int64_t id, int32_t *errCode)
10210826e83eSopenharmony_ci    {
10220826e83eSopenharmony_ci        auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
10230826e83eSopenharmony_ci        if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) {
10240826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
10250826e83eSopenharmony_ci            return -1;
10260826e83eSopenharmony_ci        }
10270826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
10280826e83eSopenharmony_ci        return nativeWebHistoryListImpl->GetListSize();
10290826e83eSopenharmony_ci    }
10300826e83eSopenharmony_ci
10310826e83eSopenharmony_ci    Media::PixelFormat GetColorType(ImageColorType colorType)
10320826e83eSopenharmony_ci    {
10330826e83eSopenharmony_ci        Media::PixelFormat pixelFormat;
10340826e83eSopenharmony_ci        switch (colorType) {
10350826e83eSopenharmony_ci            case ImageColorType::COLOR_TYPE_UNKNOWN:
10360826e83eSopenharmony_ci                pixelFormat = Media::PixelFormat::UNKNOWN;
10370826e83eSopenharmony_ci                break;
10380826e83eSopenharmony_ci            case ImageColorType::COLOR_TYPE_RGBA_8888:
10390826e83eSopenharmony_ci                pixelFormat = Media::PixelFormat::RGBA_8888;
10400826e83eSopenharmony_ci                break;
10410826e83eSopenharmony_ci            case ImageColorType::COLOR_TYPE_BGRA_8888:
10420826e83eSopenharmony_ci                pixelFormat = Media::PixelFormat::BGRA_8888;
10430826e83eSopenharmony_ci                break;
10440826e83eSopenharmony_ci            default:
10450826e83eSopenharmony_ci                pixelFormat = Media::PixelFormat::UNKNOWN;
10460826e83eSopenharmony_ci                break;
10470826e83eSopenharmony_ci        }
10480826e83eSopenharmony_ci        return pixelFormat;
10490826e83eSopenharmony_ci    }
10500826e83eSopenharmony_ci
10510826e83eSopenharmony_ci    Media::AlphaType GetAlphaType(ImageAlphaType imageAlphaType)
10520826e83eSopenharmony_ci    {
10530826e83eSopenharmony_ci        Media::AlphaType alphaType;
10540826e83eSopenharmony_ci        switch (imageAlphaType) {
10550826e83eSopenharmony_ci            case ImageAlphaType::ALPHA_TYPE_UNKNOWN:
10560826e83eSopenharmony_ci                alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
10570826e83eSopenharmony_ci                break;
10580826e83eSopenharmony_ci            case ImageAlphaType::ALPHA_TYPE_OPAQUE:
10590826e83eSopenharmony_ci                alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
10600826e83eSopenharmony_ci                break;
10610826e83eSopenharmony_ci            case ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED:
10620826e83eSopenharmony_ci                alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
10630826e83eSopenharmony_ci                break;
10640826e83eSopenharmony_ci            case ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED:
10650826e83eSopenharmony_ci                alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
10660826e83eSopenharmony_ci                break;
10670826e83eSopenharmony_ci            default:
10680826e83eSopenharmony_ci                alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
10690826e83eSopenharmony_ci                break;
10700826e83eSopenharmony_ci        }
10710826e83eSopenharmony_ci        return alphaType;
10720826e83eSopenharmony_ci    }
10730826e83eSopenharmony_ci
10740826e83eSopenharmony_ci    int64_t GetFavicon(std::shared_ptr<NWebHistoryItem> item)
10750826e83eSopenharmony_ci    {
10760826e83eSopenharmony_ci        void *data = nullptr;
10770826e83eSopenharmony_ci        int32_t width = 0;
10780826e83eSopenharmony_ci        int32_t height = 0;
10790826e83eSopenharmony_ci        ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
10800826e83eSopenharmony_ci        ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
10810826e83eSopenharmony_ci        bool isGetFavicon = item->GetFavicon(&data, width, height, colorType, alphaType);
10820826e83eSopenharmony_ci        if (!isGetFavicon) {
10830826e83eSopenharmony_ci            return -1;
10840826e83eSopenharmony_ci        }
10850826e83eSopenharmony_ci        OHOS::Media::InitializationOptions opt;
10860826e83eSopenharmony_ci        opt.size.width = width;
10870826e83eSopenharmony_ci        opt.size.height = height;
10880826e83eSopenharmony_ci        opt.pixelFormat = GetColorType(colorType);
10890826e83eSopenharmony_ci        opt.alphaType = GetAlphaType(alphaType);
10900826e83eSopenharmony_ci        opt.editable = true;
10910826e83eSopenharmony_ci        std::unique_ptr<Media::PixelMap> pixelMap = Media::PixelMapImpl::CreatePixelMap(opt);
10920826e83eSopenharmony_ci        if (pixelMap == nullptr) {
10930826e83eSopenharmony_ci            return -1;
10940826e83eSopenharmony_ci        }
10950826e83eSopenharmony_ci        uint64_t stride = static_cast<uint64_t>(width) << 2;
10960826e83eSopenharmony_ci        uint64_t bufferSize = stride * static_cast<uint64_t>(height);
10970826e83eSopenharmony_ci        pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
10980826e83eSopenharmony_ci        auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap));
10990826e83eSopenharmony_ci        if (nativeImage == nullptr) {
11000826e83eSopenharmony_ci            return -1;
11010826e83eSopenharmony_ci        }
11020826e83eSopenharmony_ci        WEBVIEWLOGI("[PixelMap] create PixelMap success");
11030826e83eSopenharmony_ci        return nativeImage->GetID();
11040826e83eSopenharmony_ci    }
11050826e83eSopenharmony_ci
11060826e83eSopenharmony_ci    int64_t FfiOHOSWebviewCtlGetFavicon(int64_t id, int32_t *errCode)
11070826e83eSopenharmony_ci    {
11080826e83eSopenharmony_ci        int64_t ret = -1;
11090826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
11100826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
11110826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
11120826e83eSopenharmony_ci            return ret;
11130826e83eSopenharmony_ci        }
11140826e83eSopenharmony_ci        const void *data = nullptr;
11150826e83eSopenharmony_ci        size_t width = 0;
11160826e83eSopenharmony_ci        size_t height = 0;
11170826e83eSopenharmony_ci        ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
11180826e83eSopenharmony_ci        ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
11190826e83eSopenharmony_ci        bool isGetFavicon = nativeWebviewCtl->GetFavicon(&data, width, height, colorType, alphaType);
11200826e83eSopenharmony_ci        if (!isGetFavicon) {
11210826e83eSopenharmony_ci            return ret;
11220826e83eSopenharmony_ci        }
11230826e83eSopenharmony_ci        OHOS::Media::InitializationOptions opt;
11240826e83eSopenharmony_ci        opt.size.width = static_cast<int32_t>(width);
11250826e83eSopenharmony_ci        opt.size.height = static_cast<int32_t>(height);
11260826e83eSopenharmony_ci        opt.pixelFormat = GetColorType(colorType);
11270826e83eSopenharmony_ci        opt.alphaType = GetAlphaType(alphaType);
11280826e83eSopenharmony_ci        opt.editable = true;
11290826e83eSopenharmony_ci        auto pixelMap = Media::PixelMap::Create(opt);
11300826e83eSopenharmony_ci        if (pixelMap == nullptr) {
11310826e83eSopenharmony_ci            return ret;
11320826e83eSopenharmony_ci        }
11330826e83eSopenharmony_ci        uint64_t stride = static_cast<uint64_t>(width) << 2;
11340826e83eSopenharmony_ci        uint64_t bufferSize = stride * static_cast<uint64_t>(height);
11350826e83eSopenharmony_ci        pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
11360826e83eSopenharmony_ci        auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap));
11370826e83eSopenharmony_ci        if (nativeImage == nullptr) {
11380826e83eSopenharmony_ci            return ret;
11390826e83eSopenharmony_ci        }
11400826e83eSopenharmony_ci        return nativeImage->GetID();
11410826e83eSopenharmony_ci    }
11420826e83eSopenharmony_ci
11430826e83eSopenharmony_ci    CHistoryItem FfiOHOSGetItemAtIndex(int64_t id, int32_t index, int32_t *errCode)
11440826e83eSopenharmony_ci    {
11450826e83eSopenharmony_ci        CHistoryItem ret = {.icon = -1, .historyUrl = nullptr, .historyRawUrl = nullptr, .title = nullptr};
11460826e83eSopenharmony_ci        auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
11470826e83eSopenharmony_ci        if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) {
11480826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
11490826e83eSopenharmony_ci            return ret;
11500826e83eSopenharmony_ci        }
11510826e83eSopenharmony_ci        if (index >= nativeWebHistoryListImpl->GetListSize() || index < 0) {
11520826e83eSopenharmony_ci            *errCode = NWebError::PARAM_CHECK_ERROR;
11530826e83eSopenharmony_ci            return ret;
11540826e83eSopenharmony_ci        }
11550826e83eSopenharmony_ci        std::shared_ptr<NWebHistoryItem> item = nativeWebHistoryListImpl->GetItem(index);
11560826e83eSopenharmony_ci        if (!item) {
11570826e83eSopenharmony_ci            *errCode = NWebError::NWEB_ERROR;
11580826e83eSopenharmony_ci            return ret;
11590826e83eSopenharmony_ci        }
11600826e83eSopenharmony_ci        ret.historyUrl = MallocCString(item->GetHistoryUrl());
11610826e83eSopenharmony_ci        ret.historyRawUrl = MallocCString(item->GetHistoryRawUrl());
11620826e83eSopenharmony_ci        ret.title = MallocCString(item->GetHistoryTitle());
11630826e83eSopenharmony_ci        ret.icon = GetFavicon(item);
11640826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
11650826e83eSopenharmony_ci        return ret;
11660826e83eSopenharmony_ci    }
11670826e83eSopenharmony_ci
11680826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlPrepareForPageLoad(char *url, bool preconnectable, int32_t numSockets)
11690826e83eSopenharmony_ci    {
11700826e83eSopenharmony_ci        int32_t ret = -1;
11710826e83eSopenharmony_ci        std::string webSrc = url;
11720826e83eSopenharmony_ci        if (webSrc.size() > URL_MAXIMUM) {
11730826e83eSopenharmony_ci            WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
11740826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
11750826e83eSopenharmony_ci        }
11760826e83eSopenharmony_ci
11770826e83eSopenharmony_ci        if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
11780826e83eSopenharmony_ci            WEBVIEWLOGE("ParsePrepareUrl error");
11790826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
11800826e83eSopenharmony_ci        }
11810826e83eSopenharmony_ci
11820826e83eSopenharmony_ci        if (numSockets <= 0 || static_cast<uint32_t>(numSockets) > SOCKET_MAXIMUM) {
11830826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
11840826e83eSopenharmony_ci        }
11850826e83eSopenharmony_ci        NWeb::NWebHelper::Instance().PrepareForPageLoad(webSrc, preconnectable, numSockets);
11860826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
11870826e83eSopenharmony_ci        return ret;
11880826e83eSopenharmony_ci    }
11890826e83eSopenharmony_ci
11900826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSetConnectionTimeout(int32_t timeout)
11910826e83eSopenharmony_ci    {
11920826e83eSopenharmony_ci        int32_t ret = -1;
11930826e83eSopenharmony_ci        if (timeout <= 0) {
11940826e83eSopenharmony_ci            return NWebError::PARAM_CHECK_ERROR;
11950826e83eSopenharmony_ci        }
11960826e83eSopenharmony_ci        NWeb::NWebHelper::Instance().SetConnectionTimeout(timeout);
11970826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
11980826e83eSopenharmony_ci        return ret;
11990826e83eSopenharmony_ci    }
12000826e83eSopenharmony_ci
12010826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSlideScroll(int64_t id, float vx, float vy)
12020826e83eSopenharmony_ci    {
12030826e83eSopenharmony_ci        int32_t ret = -1;
12040826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12050826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12060826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12070826e83eSopenharmony_ci            return ret;
12080826e83eSopenharmony_ci        }
12090826e83eSopenharmony_ci        nativeWebviewCtl->SlideScroll(vx, vy);
12100826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12110826e83eSopenharmony_ci        return ret;
12120826e83eSopenharmony_ci    }
12130826e83eSopenharmony_ci
12140826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSetNetworkAvailable(int64_t id, bool enable)
12150826e83eSopenharmony_ci    {
12160826e83eSopenharmony_ci        int32_t ret = -1;
12170826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12180826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12190826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12200826e83eSopenharmony_ci            return ret;
12210826e83eSopenharmony_ci        }
12220826e83eSopenharmony_ci        nativeWebviewCtl->PutNetworkAvailable(enable);
12230826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12240826e83eSopenharmony_ci        return ret;
12250826e83eSopenharmony_ci    }
12260826e83eSopenharmony_ci
12270826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlClearClientAuthenticationCache(int64_t id)
12280826e83eSopenharmony_ci    {
12290826e83eSopenharmony_ci        int32_t ret = -1;
12300826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12310826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12320826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12330826e83eSopenharmony_ci            return ret;
12340826e83eSopenharmony_ci        }
12350826e83eSopenharmony_ci        nativeWebviewCtl->ClearClientAuthenticationCache();
12360826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12370826e83eSopenharmony_ci        return ret;
12380826e83eSopenharmony_ci    }
12390826e83eSopenharmony_ci
12400826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlClearSslCache(int64_t id)
12410826e83eSopenharmony_ci    {
12420826e83eSopenharmony_ci        int32_t ret = -1;
12430826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12440826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12450826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12460826e83eSopenharmony_ci            return ret;
12470826e83eSopenharmony_ci        }
12480826e83eSopenharmony_ci        nativeWebviewCtl->ClearSslCache();
12490826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12500826e83eSopenharmony_ci        return ret;
12510826e83eSopenharmony_ci    }
12520826e83eSopenharmony_ci
12530826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSearchNext(int64_t id, bool forward)
12540826e83eSopenharmony_ci    {
12550826e83eSopenharmony_ci        int32_t ret = -1;
12560826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12570826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12580826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12590826e83eSopenharmony_ci            return ret;
12600826e83eSopenharmony_ci        }
12610826e83eSopenharmony_ci        nativeWebviewCtl->SearchNext(forward);
12620826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12630826e83eSopenharmony_ci        return ret;
12640826e83eSopenharmony_ci    }
12650826e83eSopenharmony_ci
12660826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlClearMatches(int64_t id)
12670826e83eSopenharmony_ci    {
12680826e83eSopenharmony_ci        int32_t ret = -1;
12690826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12700826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12710826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12720826e83eSopenharmony_ci            return ret;
12730826e83eSopenharmony_ci        }
12740826e83eSopenharmony_ci        nativeWebviewCtl->ClearMatches();
12750826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12760826e83eSopenharmony_ci        return ret;
12770826e83eSopenharmony_ci    }
12780826e83eSopenharmony_ci
12790826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlSearchAllAsync(int64_t id, char * searchString)
12800826e83eSopenharmony_ci    {
12810826e83eSopenharmony_ci        int32_t ret = -1;
12820826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12830826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12840826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12850826e83eSopenharmony_ci            return ret;
12860826e83eSopenharmony_ci        }
12870826e83eSopenharmony_ci        std::string str = searchString;
12880826e83eSopenharmony_ci        nativeWebviewCtl->SearchAllAsync(str);
12890826e83eSopenharmony_ci        ret = NWebError::NO_ERROR;
12900826e83eSopenharmony_ci        return ret;
12910826e83eSopenharmony_ci    }
12920826e83eSopenharmony_ci
12930826e83eSopenharmony_ci    int32_t FfiOHOSWebviewCtlDeleteJavaScriptRegister(int64_t id, char *name)
12940826e83eSopenharmony_ci    {
12950826e83eSopenharmony_ci        int32_t ret = -1;
12960826e83eSopenharmony_ci        auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
12970826e83eSopenharmony_ci        if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
12980826e83eSopenharmony_ci            ret = NWebError::INIT_ERROR;
12990826e83eSopenharmony_ci            return ret;
13000826e83eSopenharmony_ci        }
13010826e83eSopenharmony_ci        std::string str = name;
13020826e83eSopenharmony_ci        ret = nativeWebviewCtl->DeleteJavaScriptRegister(str, {});
13030826e83eSopenharmony_ci        return ret;
13040826e83eSopenharmony_ci    }
13050826e83eSopenharmony_ci
13060826e83eSopenharmony_ci    // web data base;
13070826e83eSopenharmony_ci    RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm)
13080826e83eSopenharmony_ci    {
13090826e83eSopenharmony_ci        std::string host_s = std::string(host);
13100826e83eSopenharmony_ci        std::string realm_s = std::string(realm);
13110826e83eSopenharmony_ci
13120826e83eSopenharmony_ci        CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s);
13130826e83eSopenharmony_ci        RetDataCArrString ret;
13140826e83eSopenharmony_ci
13150826e83eSopenharmony_ci        if (result.size == -1) {
13160826e83eSopenharmony_ci            ret.code = NWebError::HTTP_AUTH_MALLOC_FAILED;
13170826e83eSopenharmony_ci        } else {
13180826e83eSopenharmony_ci            ret.code = NWebError::NO_ERROR;
13190826e83eSopenharmony_ci        }
13200826e83eSopenharmony_ci
13210826e83eSopenharmony_ci        ret.data = result;
13220826e83eSopenharmony_ci        return ret;
13230826e83eSopenharmony_ci    }
13240826e83eSopenharmony_ci
13250826e83eSopenharmony_ci    void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm,
13260826e83eSopenharmony_ci        const char *username, const char *password)
13270826e83eSopenharmony_ci    {
13280826e83eSopenharmony_ci        std::string host_s = std::string(host);
13290826e83eSopenharmony_ci        std::string realm_s = std::string(realm);
13300826e83eSopenharmony_ci        std::string username_s = std::string(username);
13310826e83eSopenharmony_ci        std::string password_s = std::string(password);
13320826e83eSopenharmony_ci
13330826e83eSopenharmony_ci        OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s);
13340826e83eSopenharmony_ci    }
13350826e83eSopenharmony_ci
13360826e83eSopenharmony_ci    bool FfiOHOSDBExistHttpAuthCredentials()
13370826e83eSopenharmony_ci    {
13380826e83eSopenharmony_ci        return OHOS::NWeb::WebDataBase::CJExistHttpAuthCredentials();
13390826e83eSopenharmony_ci    }
13400826e83eSopenharmony_ci
13410826e83eSopenharmony_ci    void FfiOHOSDBDeleteHttpAuthCredentials()
13420826e83eSopenharmony_ci    {
13430826e83eSopenharmony_ci        OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials();
13440826e83eSopenharmony_ci    }
13450826e83eSopenharmony_ci
13460826e83eSopenharmony_ci    // WebDownloadItemImpl
13470826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadItemImplConstructor()
13480826e83eSopenharmony_ci    {
13490826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::Create<WebDownloadItemImpl>();
13500826e83eSopenharmony_ci        if (nativeWebDownloadItemImpl == nullptr) {
13510826e83eSopenharmony_ci            WEBVIEWLOGE("new web download item failed");
13520826e83eSopenharmony_ci            return -1;
13530826e83eSopenharmony_ci        }
13540826e83eSopenharmony_ci        return nativeWebDownloadItemImpl->GetID();
13550826e83eSopenharmony_ci    }
13560826e83eSopenharmony_ci
13570826e83eSopenharmony_ci    RetDataCString FfiOHOSWebDownloadItemImplGetGuid(int64_t id)
13580826e83eSopenharmony_ci    {
13590826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
13600826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
13610826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
13620826e83eSopenharmony_ci            return ret;
13630826e83eSopenharmony_ci        }
13640826e83eSopenharmony_ci        std::string guid = nativeWebDownloadItemImpl->guid;
13650826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
13660826e83eSopenharmony_ci        ret.data = MallocCString(guid);
13670826e83eSopenharmony_ci        return ret;
13680826e83eSopenharmony_ci    }
13690826e83eSopenharmony_ci
13700826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadItemImplGetCurrentSpeed(int64_t id, int32_t *errCode)
13710826e83eSopenharmony_ci    {
13720826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
13730826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
13740826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
13750826e83eSopenharmony_ci            return -1;
13760826e83eSopenharmony_ci        }
13770826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
13780826e83eSopenharmony_ci        return static_cast<int64_t>(nativeWebDownloadItemImpl->currentSpeed);
13790826e83eSopenharmony_ci    }
13800826e83eSopenharmony_ci
13810826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadItemImplGetPercentComplete(int64_t id, int32_t *errCode)
13820826e83eSopenharmony_ci    {
13830826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
13840826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
13850826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
13860826e83eSopenharmony_ci            return -1;
13870826e83eSopenharmony_ci        }
13880826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
13890826e83eSopenharmony_ci        return static_cast<int64_t>(nativeWebDownloadItemImpl->percentComplete);
13900826e83eSopenharmony_ci    }
13910826e83eSopenharmony_ci
13920826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadItemImplGetTotalBytes(int64_t id, int32_t *errCode)
13930826e83eSopenharmony_ci    {
13940826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
13950826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
13960826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
13970826e83eSopenharmony_ci            return -1;
13980826e83eSopenharmony_ci        }
13990826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
14000826e83eSopenharmony_ci        return static_cast<int64_t>(nativeWebDownloadItemImpl->totalBytes);
14010826e83eSopenharmony_ci    }
14020826e83eSopenharmony_ci
14030826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadItemImplGetReceivedBytes(int64_t id, int32_t *errCode)
14040826e83eSopenharmony_ci    {
14050826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14060826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14070826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
14080826e83eSopenharmony_ci            return -1;
14090826e83eSopenharmony_ci        }
14100826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
14110826e83eSopenharmony_ci        return static_cast<int64_t>(nativeWebDownloadItemImpl->receivedBytes);
14120826e83eSopenharmony_ci    }
14130826e83eSopenharmony_ci
14140826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadItemImplGetState(int64_t id, int32_t *errCode)
14150826e83eSopenharmony_ci    {
14160826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14170826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14180826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
14190826e83eSopenharmony_ci            return -1;
14200826e83eSopenharmony_ci        }
14210826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
14220826e83eSopenharmony_ci        return static_cast<int32_t>(nativeWebDownloadItemImpl->state);
14230826e83eSopenharmony_ci    }
14240826e83eSopenharmony_ci
14250826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadItemImplGetLastErrorCode(int64_t id, int32_t *errCode)
14260826e83eSopenharmony_ci    {
14270826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14280826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14290826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
14300826e83eSopenharmony_ci            return -1;
14310826e83eSopenharmony_ci        }
14320826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
14330826e83eSopenharmony_ci        return static_cast<int32_t>(nativeWebDownloadItemImpl->lastErrorCode);
14340826e83eSopenharmony_ci    }
14350826e83eSopenharmony_ci
14360826e83eSopenharmony_ci    RetDataCString FfiOHOSWebDownloadItemImplGetMethod(int64_t id)
14370826e83eSopenharmony_ci    {
14380826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
14390826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14400826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14410826e83eSopenharmony_ci            return ret;
14420826e83eSopenharmony_ci        }
14430826e83eSopenharmony_ci        std::string methodValue = nativeWebDownloadItemImpl->method;
14440826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
14450826e83eSopenharmony_ci        ret.data = MallocCString(methodValue);
14460826e83eSopenharmony_ci        return ret;
14470826e83eSopenharmony_ci    }
14480826e83eSopenharmony_ci
14490826e83eSopenharmony_ci    RetDataCString FfiOHOSWebDownloadItemImplGetMimeType(int64_t id)
14500826e83eSopenharmony_ci    {
14510826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
14520826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14530826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14540826e83eSopenharmony_ci            return ret;
14550826e83eSopenharmony_ci        }
14560826e83eSopenharmony_ci        std::string mimeTypeValue = nativeWebDownloadItemImpl->mimeType;
14570826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
14580826e83eSopenharmony_ci        ret.data = MallocCString(mimeTypeValue);
14590826e83eSopenharmony_ci        return ret;
14600826e83eSopenharmony_ci    }
14610826e83eSopenharmony_ci
14620826e83eSopenharmony_ci    RetDataCString FfiOHOSWebDownloadItemImplGetUrl(int64_t id)
14630826e83eSopenharmony_ci    {
14640826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
14650826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14660826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14670826e83eSopenharmony_ci            return ret;
14680826e83eSopenharmony_ci        }
14690826e83eSopenharmony_ci        std::string urlValue = nativeWebDownloadItemImpl->url;
14700826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
14710826e83eSopenharmony_ci        ret.data = MallocCString(urlValue);
14720826e83eSopenharmony_ci        return ret;
14730826e83eSopenharmony_ci    }
14740826e83eSopenharmony_ci
14750826e83eSopenharmony_ci    RetDataCString FfiOHOSWebDownloadItemImplGetSuggestedFileName(int64_t id)
14760826e83eSopenharmony_ci    {
14770826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
14780826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14790826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14800826e83eSopenharmony_ci            return ret;
14810826e83eSopenharmony_ci        }
14820826e83eSopenharmony_ci        std::string fileNameValue = nativeWebDownloadItemImpl->suggestedFileName;
14830826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
14840826e83eSopenharmony_ci        ret.data = MallocCString(fileNameValue);
14850826e83eSopenharmony_ci        return ret;
14860826e83eSopenharmony_ci    }
14870826e83eSopenharmony_ci
14880826e83eSopenharmony_ci    RetDataCString FfiOHOSWebDownloadItemImplGetFullPath(int64_t id)
14890826e83eSopenharmony_ci    {
14900826e83eSopenharmony_ci        RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
14910826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
14920826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
14930826e83eSopenharmony_ci            return ret;
14940826e83eSopenharmony_ci        }
14950826e83eSopenharmony_ci        std::string fullPath = nativeWebDownloadItemImpl->fullPath;
14960826e83eSopenharmony_ci        ret.code = NWebError::NO_ERROR;
14970826e83eSopenharmony_ci        ret.data = MallocCString(fullPath);
14980826e83eSopenharmony_ci        return ret;
14990826e83eSopenharmony_ci    }
15000826e83eSopenharmony_ci
15010826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadItemImplStart(int64_t id, char *downloadPath)
15020826e83eSopenharmony_ci    {
15030826e83eSopenharmony_ci        std::string sDownloadPath = downloadPath;
15040826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
15050826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
15060826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
15070826e83eSopenharmony_ci        }
15080826e83eSopenharmony_ci        nativeWebDownloadItemImpl->downloadPath = sDownloadPath;
15090826e83eSopenharmony_ci        WebDownload_Continue(nativeWebDownloadItemImpl->before_download_callback,
15100826e83eSopenharmony_ci            nativeWebDownloadItemImpl->downloadPath.c_str());
15110826e83eSopenharmony_ci        return NWebError::NO_ERROR;
15120826e83eSopenharmony_ci    }
15130826e83eSopenharmony_ci
15140826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadItemImplCancel(int64_t id)
15150826e83eSopenharmony_ci    {
15160826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
15170826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
15180826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
15190826e83eSopenharmony_ci        }
15200826e83eSopenharmony_ci        if (nativeWebDownloadItemImpl->download_item_callback) {
15210826e83eSopenharmony_ci            WebDownload_Cancel(nativeWebDownloadItemImpl->download_item_callback);
15220826e83eSopenharmony_ci        } else if (nativeWebDownloadItemImpl->before_download_callback) {
15230826e83eSopenharmony_ci            WebDownload_CancelBeforeDownload(nativeWebDownloadItemImpl->before_download_callback);
15240826e83eSopenharmony_ci        } else {
15250826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] WebDownloadItem::Cancel failed for callback nullptr");
15260826e83eSopenharmony_ci        }
15270826e83eSopenharmony_ci        return NWebError::NO_ERROR;
15280826e83eSopenharmony_ci    }
15290826e83eSopenharmony_ci
15300826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadItemImplPause(int64_t id)
15310826e83eSopenharmony_ci    {
15320826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
15330826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
15340826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
15350826e83eSopenharmony_ci        }
15360826e83eSopenharmony_ci        NWebDownloadItemState state = WebDownload_GetItemState(
15370826e83eSopenharmony_ci            nativeWebDownloadItemImpl->nwebId, nativeWebDownloadItemImpl->webDownloadId);
15380826e83eSopenharmony_ci        if (state != NWebDownloadItemState::IN_PROGRESS &&
15390826e83eSopenharmony_ci                state != NWebDownloadItemState::PENDING) {
15400826e83eSopenharmony_ci            return NWebError::DOWNLOAD_NOT_START;
15410826e83eSopenharmony_ci        }
15420826e83eSopenharmony_ci        if (nativeWebDownloadItemImpl->download_item_callback) {
15430826e83eSopenharmony_ci            WebDownload_Pause(nativeWebDownloadItemImpl->download_item_callback);
15440826e83eSopenharmony_ci        } else if (nativeWebDownloadItemImpl->before_download_callback) {
15450826e83eSopenharmony_ci            WebDownload_PauseBeforeDownload(nativeWebDownloadItemImpl->before_download_callback);
15460826e83eSopenharmony_ci        } else {
15470826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] WebDownloadItem::Pause failed for callback nullptr");
15480826e83eSopenharmony_ci        }
15490826e83eSopenharmony_ci        return NWebError::NO_ERROR;
15500826e83eSopenharmony_ci    }
15510826e83eSopenharmony_ci
15520826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadItemImplResume(int64_t id)
15530826e83eSopenharmony_ci    {
15540826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
15550826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
15560826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
15570826e83eSopenharmony_ci        }
15580826e83eSopenharmony_ci        NWebDownloadItemState state = WebDownload_GetItemState(
15590826e83eSopenharmony_ci            nativeWebDownloadItemImpl->nwebId, nativeWebDownloadItemImpl->webDownloadId);
15600826e83eSopenharmony_ci        if (state != NWebDownloadItemState::PAUSED) {
15610826e83eSopenharmony_ci            return NWebError::DOWNLOAD_NOT_PAUSED;
15620826e83eSopenharmony_ci        }
15630826e83eSopenharmony_ci
15640826e83eSopenharmony_ci        if (nativeWebDownloadItemImpl->download_item_callback) {
15650826e83eSopenharmony_ci            WebDownload_Resume(nativeWebDownloadItemImpl->download_item_callback);
15660826e83eSopenharmony_ci        } else if (nativeWebDownloadItemImpl->before_download_callback) {
15670826e83eSopenharmony_ci            WebDownload_ResumeBeforeDownload(nativeWebDownloadItemImpl->before_download_callback);
15680826e83eSopenharmony_ci        } else {
15690826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] WebDownloadItem::Resume failed for callback nullptr");
15700826e83eSopenharmony_ci        }
15710826e83eSopenharmony_ci        return NWebError::NO_ERROR;
15720826e83eSopenharmony_ci    }
15730826e83eSopenharmony_ci
15740826e83eSopenharmony_ci    CArrUI8 FfiOHOSWebDownloadItemImplSerialize(int64_t id, int32_t *errCode)
15750826e83eSopenharmony_ci    {
15760826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id);
15770826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
15780826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
15790826e83eSopenharmony_ci            return CArrUI8{nullptr, 0};
15800826e83eSopenharmony_ci        }
15810826e83eSopenharmony_ci
15820826e83eSopenharmony_ci        browser_service::WebDownload webDownloadPb;
15830826e83eSopenharmony_ci        webDownloadPb.set_web_download_id(nativeWebDownloadItemImpl->webDownloadId);
15840826e83eSopenharmony_ci        webDownloadPb.set_current_speed(nativeWebDownloadItemImpl->currentSpeed);
15850826e83eSopenharmony_ci        webDownloadPb.set_percent_complete(nativeWebDownloadItemImpl->percentComplete);
15860826e83eSopenharmony_ci        webDownloadPb.set_total_bytes(nativeWebDownloadItemImpl->totalBytes);
15870826e83eSopenharmony_ci        webDownloadPb.set_received_bytes(nativeWebDownloadItemImpl->receivedBytes);
15880826e83eSopenharmony_ci        webDownloadPb.set_guid(nativeWebDownloadItemImpl->guid);
15890826e83eSopenharmony_ci        webDownloadPb.set_full_path(nativeWebDownloadItemImpl->fullPath);
15900826e83eSopenharmony_ci        webDownloadPb.set_url(nativeWebDownloadItemImpl->url);
15910826e83eSopenharmony_ci        webDownloadPb.set_etag(nativeWebDownloadItemImpl->etag);
15920826e83eSopenharmony_ci        webDownloadPb.set_original_url(nativeWebDownloadItemImpl->originalUrl);
15930826e83eSopenharmony_ci        webDownloadPb.set_suggested_file_name(nativeWebDownloadItemImpl->suggestedFileName);
15940826e83eSopenharmony_ci        webDownloadPb.set_content_disposition(nativeWebDownloadItemImpl->contentDisposition);
15950826e83eSopenharmony_ci        webDownloadPb.set_mime_type(nativeWebDownloadItemImpl->mimeType);
15960826e83eSopenharmony_ci        webDownloadPb.set_last_modified(nativeWebDownloadItemImpl->lastModified);
15970826e83eSopenharmony_ci        webDownloadPb.set_state(
15980826e83eSopenharmony_ci            static_cast<browser_service::WebDownload::WebDownloadState>(nativeWebDownloadItemImpl->state));
15990826e83eSopenharmony_ci        webDownloadPb.set_method(nativeWebDownloadItemImpl->method);
16000826e83eSopenharmony_ci        webDownloadPb.set_last_error_code(nativeWebDownloadItemImpl->lastErrorCode);
16010826e83eSopenharmony_ci        webDownloadPb.set_received_slices(nativeWebDownloadItemImpl->receivedSlices);
16020826e83eSopenharmony_ci        webDownloadPb.set_download_path(nativeWebDownloadItemImpl->downloadPath);
16030826e83eSopenharmony_ci
16040826e83eSopenharmony_ci        std::string webDownloadValue;
16050826e83eSopenharmony_ci        webDownloadPb.SerializeToString(&webDownloadValue);
16060826e83eSopenharmony_ci        uint8_t* result = MallocUInt8(webDownloadValue);
16070826e83eSopenharmony_ci        if (result == nullptr) {
16080826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] malloc failed");
16090826e83eSopenharmony_ci            *errCode = NWebError::NEW_OOM;
16100826e83eSopenharmony_ci            return CArrUI8{nullptr, 0};
16110826e83eSopenharmony_ci        }
16120826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
16130826e83eSopenharmony_ci        return CArrUI8{result, webDownloadValue.length()};
16140826e83eSopenharmony_ci    }
16150826e83eSopenharmony_ci
16160826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadItemImplDeserialize(CArrUI8 serializedData, int32_t *errCode)
16170826e83eSopenharmony_ci    {
16180826e83eSopenharmony_ci        char *buffer = reinterpret_cast<char*>(serializedData.head);
16190826e83eSopenharmony_ci        browser_service::WebDownload webDownloadPb;
16200826e83eSopenharmony_ci        bool result = webDownloadPb.ParseFromArray(buffer, serializedData.size);
16210826e83eSopenharmony_ci        if (!result) {
16220826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
16230826e83eSopenharmony_ci            return -1;
16240826e83eSopenharmony_ci        }
16250826e83eSopenharmony_ci        WebDownloadItemImpl *webDownloadItem = FFIData::Create<WebDownloadItemImpl>();
16260826e83eSopenharmony_ci        if (webDownloadItem == nullptr) {
16270826e83eSopenharmony_ci            WEBVIEWLOGE("new web download item failed");
16280826e83eSopenharmony_ci            *errCode = NWebError::INIT_ERROR;
16290826e83eSopenharmony_ci            return -1;
16300826e83eSopenharmony_ci        }
16310826e83eSopenharmony_ci        webDownloadItem->webDownloadId = webDownloadPb.web_download_id();
16320826e83eSopenharmony_ci        webDownloadItem->currentSpeed = webDownloadPb.current_speed();
16330826e83eSopenharmony_ci        webDownloadItem->percentComplete = webDownloadPb.percent_complete();
16340826e83eSopenharmony_ci        webDownloadItem->totalBytes = webDownloadPb.total_bytes();
16350826e83eSopenharmony_ci        webDownloadItem->receivedBytes = webDownloadPb.received_bytes();
16360826e83eSopenharmony_ci        webDownloadItem->guid = webDownloadPb.guid();
16370826e83eSopenharmony_ci        webDownloadItem->fullPath = webDownloadPb.full_path();
16380826e83eSopenharmony_ci        webDownloadItem->url = webDownloadPb.url();
16390826e83eSopenharmony_ci        webDownloadItem->etag = webDownloadPb.etag();
16400826e83eSopenharmony_ci        webDownloadItem->originalUrl = webDownloadPb.original_url();
16410826e83eSopenharmony_ci        webDownloadItem->suggestedFileName = webDownloadPb.suggested_file_name();
16420826e83eSopenharmony_ci        webDownloadItem->contentDisposition = webDownloadPb.content_disposition();
16430826e83eSopenharmony_ci        webDownloadItem->mimeType = webDownloadPb.mime_type();
16440826e83eSopenharmony_ci        webDownloadItem->lastModified = webDownloadPb.last_modified();
16450826e83eSopenharmony_ci        webDownloadItem->state = static_cast<NWebDownloadItemState>(webDownloadPb.state());
16460826e83eSopenharmony_ci        webDownloadItem->method = webDownloadPb.method();
16470826e83eSopenharmony_ci        webDownloadItem->lastErrorCode = webDownloadPb.last_error_code();
16480826e83eSopenharmony_ci        webDownloadItem->receivedSlices = webDownloadPb.received_slices();
16490826e83eSopenharmony_ci        webDownloadItem->downloadPath = webDownloadPb.download_path();
16500826e83eSopenharmony_ci        *errCode = NWebError::NO_ERROR;
16510826e83eSopenharmony_ci        return webDownloadItem->GetID();
16520826e83eSopenharmony_ci    }
16530826e83eSopenharmony_ci
16540826e83eSopenharmony_ci    // WebDownloadDelegateImpl
16550826e83eSopenharmony_ci    int64_t FfiOHOSWebDownloadDelegateImplConstructor()
16560826e83eSopenharmony_ci    {
16570826e83eSopenharmony_ci        auto nativeWebDownloadDelegateImpl = FFIData::Create<WebDownloadDelegateImpl>();
16580826e83eSopenharmony_ci        if (nativeWebDownloadDelegateImpl == nullptr) {
16590826e83eSopenharmony_ci            WEBVIEWLOGE("new web download delegate failed");
16600826e83eSopenharmony_ci            return -1;
16610826e83eSopenharmony_ci        }
16620826e83eSopenharmony_ci        return nativeWebDownloadDelegateImpl->GetID();
16630826e83eSopenharmony_ci    }
16640826e83eSopenharmony_ci
16650826e83eSopenharmony_ci    void FfiOHOSWebDownloadDelegateImplOnBeforeDownload(int64_t id, void (*callback)(int64_t))
16660826e83eSopenharmony_ci    {
16670826e83eSopenharmony_ci        auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id);
16680826e83eSopenharmony_ci        if (!nativeWebDownloadDelegateImpl) {
16690826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
16700826e83eSopenharmony_ci            return;
16710826e83eSopenharmony_ci        }
16720826e83eSopenharmony_ci        nativeWebDownloadDelegateImpl->PutDownloadBeforeStart(CJLambda::Create(callback));
16730826e83eSopenharmony_ci    }
16740826e83eSopenharmony_ci
16750826e83eSopenharmony_ci    void FfiOHOSWebDownloadDelegateImplOnDownloadUpdated(int64_t id, void (*callback)(int64_t))
16760826e83eSopenharmony_ci    {
16770826e83eSopenharmony_ci        auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id);
16780826e83eSopenharmony_ci        if (!nativeWebDownloadDelegateImpl) {
16790826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
16800826e83eSopenharmony_ci            return;
16810826e83eSopenharmony_ci        }
16820826e83eSopenharmony_ci        nativeWebDownloadDelegateImpl->PutDownloadDidUpdate(CJLambda::Create(callback));
16830826e83eSopenharmony_ci    }
16840826e83eSopenharmony_ci
16850826e83eSopenharmony_ci    void FfiOHOSWebDownloadDelegateImplOnDownloadFinish(int64_t id, void (*callback)(int64_t))
16860826e83eSopenharmony_ci    {
16870826e83eSopenharmony_ci        auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id);
16880826e83eSopenharmony_ci        if (!nativeWebDownloadDelegateImpl) {
16890826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
16900826e83eSopenharmony_ci            return;
16910826e83eSopenharmony_ci        }
16920826e83eSopenharmony_ci        nativeWebDownloadDelegateImpl->PutDownloadDidFinish(CJLambda::Create(callback));
16930826e83eSopenharmony_ci    }
16940826e83eSopenharmony_ci
16950826e83eSopenharmony_ci    void FfiOHOSWebDownloadDelegateImplOnDownloadFailed(int64_t id, void (*callback)(int64_t))
16960826e83eSopenharmony_ci    {
16970826e83eSopenharmony_ci        auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id);
16980826e83eSopenharmony_ci        if (!nativeWebDownloadDelegateImpl) {
16990826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
17000826e83eSopenharmony_ci            return;
17010826e83eSopenharmony_ci        }
17020826e83eSopenharmony_ci        nativeWebDownloadDelegateImpl->PutDownloadDidFail(CJLambda::Create(callback));
17030826e83eSopenharmony_ci    }
17040826e83eSopenharmony_ci
17050826e83eSopenharmony_ci    // WebDownloadManagerImpl
17060826e83eSopenharmony_ci    void FfiOHOSWebDownloadManagerImplSetDownloadDelegate(int64_t delegateId)
17070826e83eSopenharmony_ci    {
17080826e83eSopenharmony_ci        auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(delegateId);
17090826e83eSopenharmony_ci        if (!nativeWebDownloadDelegateImpl) {
17100826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
17110826e83eSopenharmony_ci            return;
17120826e83eSopenharmony_ci        }
17130826e83eSopenharmony_ci        WebDownloadManagerImpl::SetDownloadDelegate(nativeWebDownloadDelegateImpl);
17140826e83eSopenharmony_ci    }
17150826e83eSopenharmony_ci
17160826e83eSopenharmony_ci    int32_t FfiOHOSWebDownloadManagerImplResumeDownload(int64_t itemId)
17170826e83eSopenharmony_ci    {
17180826e83eSopenharmony_ci        if (!WebDownloadManagerImpl::HasValidDelegate()) {
17190826e83eSopenharmony_ci            return NWebError::NO_DOWNLOAD_DELEGATE_SET;
17200826e83eSopenharmony_ci        }
17210826e83eSopenharmony_ci        auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(itemId);
17220826e83eSopenharmony_ci        if (!nativeWebDownloadItemImpl) {
17230826e83eSopenharmony_ci            WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
17240826e83eSopenharmony_ci            return NWebError::INIT_ERROR;
17250826e83eSopenharmony_ci        }
17260826e83eSopenharmony_ci        WebDownloadManagerImpl::ResumeDownload(nativeWebDownloadItemImpl);
17270826e83eSopenharmony_ci        return NWebError::NO_ERROR;
17280826e83eSopenharmony_ci    }
17290826e83eSopenharmony_ci
17300826e83eSopenharmony_ci    // GeolocationPermissions
17310826e83eSopenharmony_ci    void FfiOHOSGeolocationAllowGeolocation(char* origin, bool incognito, int32_t *errCode)
17320826e83eSopenharmony_ci    {
17330826e83eSopenharmony_ci        std::string originStr = std::string(origin);
17340826e83eSopenharmony_ci        GeolocationPermission::CjAllowGeolocation(originStr, incognito, errCode);
17350826e83eSopenharmony_ci    }
17360826e83eSopenharmony_ci
17370826e83eSopenharmony_ci    void FfiOHOSGeolocationDeleteGeolocation(char* origin, bool incognito, int32_t *errCode)
17380826e83eSopenharmony_ci    {
17390826e83eSopenharmony_ci        std::string originStr = std::string(origin);
17400826e83eSopenharmony_ci        GeolocationPermission::CjDeleteGeolocation(originStr, incognito, errCode);
17410826e83eSopenharmony_ci    }
17420826e83eSopenharmony_ci
17430826e83eSopenharmony_ci    bool FfiOHOSGeolocationGetAccessibleGeolocation(char* origin, bool incognito, int32_t *errCode)
17440826e83eSopenharmony_ci    {
17450826e83eSopenharmony_ci        std::string originStr = std::string(origin);
17460826e83eSopenharmony_ci        return GeolocationPermission::CjGetAccessibleGeolocation(originStr, incognito, errCode);
17470826e83eSopenharmony_ci    }
17480826e83eSopenharmony_ci
17490826e83eSopenharmony_ci    CArrString FfiOHOSGeolocationGetStoredGeolocation(bool incognito, int32_t *errCode)
17500826e83eSopenharmony_ci    {
17510826e83eSopenharmony_ci        std::vector<std::string> origins = GeolocationPermission::CjGetStoredGeolocation(incognito, errCode);
17520826e83eSopenharmony_ci        CArrString arrOrigins = {.head = nullptr, .size = 0};
17530826e83eSopenharmony_ci        if (errCode && *errCode != 0) {
17540826e83eSopenharmony_ci            return arrOrigins;
17550826e83eSopenharmony_ci        }
17560826e83eSopenharmony_ci        arrOrigins.size = static_cast<int64_t>(origins.size());
17570826e83eSopenharmony_ci        arrOrigins.head = OHOS::Webview::VectorToCArrString(origins);
17580826e83eSopenharmony_ci        return arrOrigins;
17590826e83eSopenharmony_ci    }
17600826e83eSopenharmony_ci
17610826e83eSopenharmony_ci    void FfiOHOSGeolocationDeleteAllGeolocation(bool incognito, int32_t *errCode)
17620826e83eSopenharmony_ci    {
17630826e83eSopenharmony_ci        return GeolocationPermission::CjDeleteAllGeolocation(incognito, errCode);
17640826e83eSopenharmony_ci    }
17650826e83eSopenharmony_ci
17660826e83eSopenharmony_ci    // web_storage
17670826e83eSopenharmony_ci    int32_t FfiWebStorageDeleteOrigin(char *corigin)
17680826e83eSopenharmony_ci    {
17690826e83eSopenharmony_ci        std::string origin(corigin);
17700826e83eSopenharmony_ci        return OHOS::NWeb::WebStorage::CJdeleteOrigin(origin);
17710826e83eSopenharmony_ci    }
17720826e83eSopenharmony_ci
17730826e83eSopenharmony_ci    void FfiWebStorageDeleteAllData(bool incognito)
17740826e83eSopenharmony_ci    {
17750826e83eSopenharmony_ci        OHOS::NWeb::WebStorage::CJdeleteAllData(incognito);
17760826e83eSopenharmony_ci    }
17770826e83eSopenharmony_ci
17780826e83eSopenharmony_ci    int64_t FfiWebStorageGetOriginQuota(char* corigin, int32_t *errCode)
17790826e83eSopenharmony_ci    {
17800826e83eSopenharmony_ci        std::string origin(corigin);
17810826e83eSopenharmony_ci        return OHOS::NWeb::WebStorage::CjGetOriginUsageOrQuota(origin, errCode, true);
17820826e83eSopenharmony_ci    }
17830826e83eSopenharmony_ci
17840826e83eSopenharmony_ci    int64_t FfiWebStorageGetOriginUsage(char* corigin, int32_t *errCode)
17850826e83eSopenharmony_ci    {
17860826e83eSopenharmony_ci        std::string origin(corigin);
17870826e83eSopenharmony_ci        return OHOS::NWeb::WebStorage::CjGetOriginUsageOrQuota(origin, errCode, false);
17880826e83eSopenharmony_ci    }
17890826e83eSopenharmony_ci
17900826e83eSopenharmony_ci    CArrWebStorageOrigin FfiWebStorageGetOrigins(int32_t *errCode)
17910826e83eSopenharmony_ci    {
17920826e83eSopenharmony_ci        return OHOS::NWeb::WebStorage::CjGetOrigins(errCode);
17930826e83eSopenharmony_ci    }
17940826e83eSopenharmony_ci}
17950826e83eSopenharmony_ci}
17960826e83eSopenharmony_ci}