• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "webview_ffi.h"
17 
18 #include <arpa/inet.h>
19 #include <regex>
20 
21 #include "application_context.h"
22 #include "cj_common_ffi.h"
23 #include "cj_lambda.h"
24 #include "geolocation_permission.h"
25 #include "nweb_cache_options_impl.h"
26 #include "nweb_helper.h"
27 #include "nweb_init_params.h"
28 #include "parameters.h"
29 #include "pixel_map.h"
30 #include "pixel_map_impl.h"
31 #include "web_cookie_manager.h"
32 #include "web_data_base.h"
33 #include "web_download.pb.h"
34 #include "web_download_delegate_impl.h"
35 #include "web_download_item_impl.h"
36 #include "web_download_manager_impl.h"
37 #include "web_errors.h"
38 #include "web_scheme_handler_request.h"
39 #include "web_scheme_handler_response_impl.h"
40 #include "web_storage.h"
41 #include "webview_controller_impl.h"
42 #include "webview_function.h"
43 #include "webview_javascript_execute_callback.h"
44 #include "webview_log.h"
45 #include "webview_utils.h"
46 
47 
48 using namespace OHOS::FFI;
49 using namespace OHOS::NWeb;
50 
51 namespace OHOS {
52 namespace Webview {
53 
54 constexpr uint32_t SOCKET_MAXIMUM = 6;
55 constexpr uint32_t URL_MAXIMUM = 2048;
56 constexpr int INTEGER_TWO = 2;
57 constexpr int INTEGER_THREE = 3;
58 constexpr char URL_REGEXPR[] = "^http(s)?:\\/\\/.+";
59 constexpr size_t MAX_URL_TRUST_LIST_STR_LEN = 10 * 1024 * 1024;
60 std::atomic<bool> g_inWebPageSnapshot {false};
61 
CheckUrl(std::string url)62 bool CheckUrl(std::string url)
63 {
64     if (url.size() > URL_MAXIMUM) {
65         WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d.", URL_MAXIMUM);
66         return false;
67     }
68 
69     if (!regex_match(url, std::regex("^http(s)?:\\/\\/.+", std::regex_constants::icase))) {
70         WEBVIEWLOGE("The Parse URL error. URL: %{private}s", url.c_str());
71         return false;
72     }
73 
74     return true;
75 }
76 
CreateWebPageSnapshotResultCallback(bool check,int32_t inputWidth,int32_t inputHeight,int32_t inputSizeType,const std::function<void (RetDataCSnapshotResult)> & callbackRef)77 NWeb::WebSnapshotCallback CreateWebPageSnapshotResultCallback(bool check, int32_t inputWidth, int32_t inputHeight,
78     int32_t inputSizeType, const std::function<void(RetDataCSnapshotResult)>& callbackRef)
79 {
80     return [check, inputWidth, inputHeight, inputSizeType, callbackRef](
81             const char* returnId, bool returnStatus, float radio, void* returnData,
82             int returnWidth, int returnHeight) {
83             RetDataCSnapshotResult ret = { .code = NWebError::INIT_ERROR, .data = { .id = nullptr, .imageId = 0,
84                 .status = true, .width = 0, .height = 0, .widthType = 1, .heightType = 1 } };
85             OHOS::Media::InitializationOptions opt;
86             opt.size.width = static_cast<int32_t>(returnWidth);
87             opt.size.height = static_cast<int32_t>(returnHeight);
88             opt.pixelFormat = OHOS::Media::PixelFormat::RGBA_8888;
89             opt.alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
90             opt.editable = true;
91             std::unique_ptr<Media::PixelMap> pixelMap = Media::PixelMapImpl::CreatePixelMap(opt);
92             if (pixelMap != nullptr) {
93                 uint64_t stride = static_cast<uint64_t>(returnWidth) << 2;
94                 uint64_t bufferSize = stride * static_cast<uint64_t>(returnHeight);
95                 pixelMap->WritePixels(static_cast<const uint8_t*>(returnData), bufferSize);
96             } else {
97                 WEBVIEWLOGE("WebPageSnapshot create pixel map error");
98             }
99             int returnJsWidth = 0;
100             int returnJsHeight = 0;
101             if (radio > 0) {
102                 returnJsWidth = returnWidth / radio;
103                 returnJsHeight = returnHeight / radio;
104             }
105             if (check) {
106                 if (std::abs(returnJsWidth - inputWidth) < INTEGER_THREE) {
107                     returnJsWidth = inputWidth;
108                 }
109                 if (std::abs(returnJsHeight - inputHeight) < INTEGER_THREE) {
110                     returnJsHeight = inputHeight;
111                 }
112             }
113             ret.code = NWebError::NO_ERROR;
114             ret.data.status = check;
115             ret.data.width = returnJsWidth;
116             ret.data.height = returnJsHeight;
117             ret.data.widthType = inputSizeType;
118             ret.data.heightType = inputSizeType;
119             ret.data.id = MallocCString(returnId);
120             auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap));
121             if (nativeImage != nullptr) {
122                 ret.data.imageId = nativeImage->GetID();
123             }
124             callbackRef(ret);
125             g_inWebPageSnapshot = false;
126         };
127 }
128 
129 extern "C" {
FfiOHOSWebviewOnce(char * type,void (* callbackRef)(void))130     int32_t FfiOHOSWebviewOnce(char* type, void (*callbackRef)(void))
131     {
132         return FfiOnce(type, callbackRef);
133     }
134 
FfiOHOSWebviewCtlConstructor()135     int64_t FfiOHOSWebviewCtlConstructor()
136     {
137         auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>();
138         if (nativeWebviewCtl == nullptr) {
139             WEBVIEWLOGE("new webview controller failed");
140             return -1;
141         }
142         WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false);
143         return nativeWebviewCtl->GetID();
144     }
145 
FfiOHOSWebviewCtlConstructorWithWebTag(char * cWebTag)146     int64_t FfiOHOSWebviewCtlConstructorWithWebTag(char *cWebTag)
147     {
148         std::string webTag = cWebTag;
149         auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>(webTag);
150         if (nativeWebviewCtl == nullptr) {
151             WEBVIEWLOGE("new webview controller failed");
152             return -1;
153         }
154         WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false);
155         return nativeWebviewCtl->GetID();
156     }
157 
FfiOHOSWebviewCtlInitializeWebEngine()158     void FfiOHOSWebviewCtlInitializeWebEngine()
159     {
160         std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
161             AbilityRuntime::ApplicationContext::GetApplicationContext();
162         if (ctx == nullptr) {
163             WEBVIEWLOGE("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to ctx is null.");
164             return;
165         }
166         const std::string& bundle_path = ctx->GetBundleCodeDir();
167         NWebHelper::Instance().SetBundlePath(bundle_path);
168         if (!NWebHelper::Instance().InitAndRun(true)) {
169             WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to NWebHelper failure.");
170         }
171         WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine NWebHelper initialized, \
172             init web engine done, bundle_path: %{public}s", bundle_path.c_str());
173         return;
174     }
175 
FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode,char * secureDnsConfig)176     void FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode, char* secureDnsConfig)
177     {
178         std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
179         config->SetMode(secureDnsMode);
180         config->SetConfig(secureDnsConfig);
181         WEBVIEWLOGI("set http dns mode:%{public}d doh_config:%{public}s", secureDnsMode, secureDnsConfig);
182         NWebHelper::Instance().SetHttpDns(config);
183     }
184 
FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess)185     void FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess)
186     {
187         if (OHOS::system::GetBoolParameter("web.debug.devtools", false)) {
188             return;
189         }
190 
191         WebviewControllerImpl::webDebuggingAccess_ = webDebuggingAccess;
192         return;
193     }
194 
FfiOHOSWebviewCtlLoadUrl(int64_t id,char * url)195     int32_t FfiOHOSWebviewCtlLoadUrl(int64_t id, char *url)
196     {
197         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
198         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
199             return NWebError::INIT_ERROR;
200         }
201         std::string webSrc = url;
202 
203         return nativeWebviewCtl->LoadUrl(webSrc);
204     }
205 
FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id,char * url,ArrWebHeader headers)206     int32_t FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id, char *url, ArrWebHeader headers)
207     {
208         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
209         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
210             return NWebError::INIT_ERROR;
211         }
212 
213         std::map<std::string, std::string> httpHeaders;
214         uint32_t arrayLength = static_cast<uint32_t>(headers.size);
215         for (uint32_t i = 0; i < arrayLength; ++i) {
216             std::string key = headers.head[i].headerKey;
217             std::string value = headers.head[i].headerValue;
218             httpHeaders[key] = value;
219         }
220 
221         return nativeWebviewCtl->LoadUrl(url, httpHeaders);
222     }
223 
FfiOHOSWebviewCtlLoadData(int64_t id,LoadDatas loadDatas)224     int32_t FfiOHOSWebviewCtlLoadData(int64_t id, LoadDatas loadDatas)
225     {
226         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
227         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
228             return NWebError::INIT_ERROR;
229         }
230         std::string data = loadDatas.cData;
231         std::string mimeType = loadDatas.cMimeType;
232         std::string encoding = loadDatas.cEncoding;
233         std::string baseUrl = loadDatas.cBaseUrl;
234         std::string historyUrl = loadDatas.cHistoryUrl;
235         return nativeWebviewCtl->LoadData(data, mimeType, encoding, baseUrl, historyUrl);
236     }
237 
FfiOHOSWebviewCtlPreFetchPage(int64_t id,char * url)238     int32_t FfiOHOSWebviewCtlPreFetchPage(int64_t id, char *url)
239     {
240         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
241         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
242             return NWebError::INIT_ERROR;
243         }
244         std::string webSrc = url;
245         if (webSrc.size() > URL_MAXIMUM) {
246             WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
247             return NWebError::PARAM_CHECK_ERROR;
248         }
249 
250         if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
251             WEBVIEWLOGE("ParsePrepareUrl error");
252             return NWebError::PARAM_CHECK_ERROR;
253         }
254         int32_t ret = nativeWebviewCtl->PreFetchPage(webSrc);
255         if (ret != NWebError::NO_ERROR) {
256             if (ret == NWebError::NWEB_ERROR) {
257                 return ret;
258             }
259         }
260         return ret;
261     }
262 
FfiOHOSWebviewCtlPreFetchPageWithHeaders(int64_t id,char * url,ArrWebHeader headers)263     int32_t FfiOHOSWebviewCtlPreFetchPageWithHeaders(int64_t id, char *url, ArrWebHeader headers)
264     {
265         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
266         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
267             return NWebError::INIT_ERROR;
268         }
269         std::string webSrc = url;
270         if (webSrc.size() > URL_MAXIMUM) {
271             WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
272             return NWebError::PARAM_CHECK_ERROR;
273         }
274 
275         if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
276             WEBVIEWLOGE("ParsePrepareUrl error");
277             return NWebError::PARAM_CHECK_ERROR;
278         }
279         std::map<std::string, std::string> httpHeaders;
280         uint32_t arrayLength = static_cast<uint32_t>(headers.size);
281         for (uint32_t i = 0; i < arrayLength; ++i) {
282             std::string key = headers.head[i].headerKey;
283             std::string value = headers.head[i].headerValue;
284             httpHeaders[key] = value;
285         }
286 
287         int32_t ret = nativeWebviewCtl->PreFetchPage(webSrc, httpHeaders);
288         if (ret != NWebError::NO_ERROR) {
289             if (ret == NWebError::NWEB_ERROR) {
290                 WEBVIEWLOGE("preFetchPage failed.");
291                 return ret;
292             }
293         }
294         return ret;
295     }
296 
FfiOHOSWebviewCtlSetAudioMuted(int64_t id,bool mute)297     int32_t FfiOHOSWebviewCtlSetAudioMuted(int64_t id, bool mute)
298     {
299         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
300         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
301             return NWebError::INIT_ERROR;
302         }
303         int32_t ret = nativeWebviewCtl->SetAudioMuted(mute);
304         if (ret != NWebError::NO_ERROR) {
305             if (ret == NWebError::NWEB_ERROR) {
306                 WEBVIEWLOGE("SetAudioMuted failed, error code: %{public}d", ret);
307                 return ret;
308             }
309         }
310         WEBVIEWLOGI("SetAudioMuted: %{public}s", (mute ? "true" : "false"));
311         return ret;
312     }
313 
FfiOHOSWebviewCtlRefresh(int64_t id)314     int32_t FfiOHOSWebviewCtlRefresh(int64_t id)
315     {
316         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
317         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
318             return NWebError::INIT_ERROR;
319         }
320         nativeWebviewCtl->Refresh();
321         return NWebError::NO_ERROR;
322     }
323 
FfiOHOSWebviewCtlGetUserAgent(int64_t id,int32_t * errCode)324     char *FfiOHOSWebviewCtlGetUserAgent(int64_t id, int32_t *errCode)
325     {
326         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
327         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
328             *errCode = NWebError::INIT_ERROR;
329             return nullptr;
330         }
331         std::string userAgent = nativeWebviewCtl->GetUserAgent();
332         *errCode = NWebError::NO_ERROR;
333         return MallocCString(userAgent);
334     }
335 
FfiOHOSWebviewCtlGetWebId(int64_t id,int32_t * errCode)336     int32_t FfiOHOSWebviewCtlGetWebId(int64_t id, int32_t *errCode)
337     {
338         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
339         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
340             *errCode = NWebError::INIT_ERROR;
341             return -1;
342         }
343         int32_t webId = -1;
344         webId = nativeWebviewCtl->GetWebId();
345         *errCode = NWebError::NO_ERROR;
346         return webId;
347     }
348 
FfiOHOSWebviewCtlAccessForward(int64_t id,int32_t * errCode)349     bool FfiOHOSWebviewCtlAccessForward(int64_t id, int32_t *errCode)
350     {
351         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
352         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
353             *errCode = NWebError::INIT_ERROR;
354             return false;
355         }
356         bool access = nativeWebviewCtl->AccessForward();
357         *errCode = NWebError::NO_ERROR;
358         return access;
359     }
360 
FfiOHOSWebviewCtlAccessBackward(int64_t id,int32_t * errCode)361     bool FfiOHOSWebviewCtlAccessBackward(int64_t id, int32_t *errCode)
362     {
363         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
364         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
365             *errCode = NWebError::INIT_ERROR;
366             return false;
367         }
368         bool access = nativeWebviewCtl->AccessBackward();
369         *errCode = NWebError::NO_ERROR;
370         return access;
371     }
372 
FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id,char * cUserAgent)373     int32_t FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id, char *cUserAgent)
374     {
375         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
376         std::string userAgent = cUserAgent;
377         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
378             return NWebError::INIT_ERROR;
379         }
380         return nativeWebviewCtl->SetCustomUserAgent(userAgent);
381     }
382 
FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id)383     RetDataCString FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id)
384     {
385         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
386         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
387         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
388             return ret;
389         }
390         std::string userAgent = "";
391         userAgent = nativeWebviewCtl->GetCustomUserAgent();
392         ret.code = NWebError::NO_ERROR;
393         ret.data = MallocCString(userAgent);
394         return ret;
395     }
396 
FfiOHOSWebviewCtlRunJavaScript(int64_t id,char * cScript,void (* callbackRef)(RetDataCString infoRef))397     int32_t FfiOHOSWebviewCtlRunJavaScript(int64_t id, char* cScript,
398         void (*callbackRef)(RetDataCString infoRef))
399     {
400         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
401         std::string script = std::string(cScript);
402         auto onChange = [lambda = CJLambda::Create(callbackRef)]
403             (RetDataCString infoRef) -> void { lambda(infoRef); };
404         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
405             return NWebError::INIT_ERROR;
406         }
407         nativeWebviewCtl->RunJavaScript(script, onChange);
408         return NWebError::NO_ERROR;
409     }
410 
FfiOHOSWebviewCtlRunJavaScriptExt(int64_t id,char * cScript,void (* callbackRef)(RetDataI64))411     int32_t FfiOHOSWebviewCtlRunJavaScriptExt(int64_t id, char* cScript,
412         void (*callbackRef)(RetDataI64))
413     {
414         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
415         std::string script = std::string(cScript);
416         auto onChange = CJLambda::Create(callbackRef);
417         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
418             return NWebError::INIT_ERROR;
419         }
420         nativeWebviewCtl->RunJavaScriptExt(script, onChange);
421         return NWebError::NO_ERROR;
422     }
423 
FfiOHOSWebviewCtlRunJavaScriptExtArr(int64_t id,CArrUI8 cScript,void (* callbackRef)(RetDataI64))424     int32_t FfiOHOSWebviewCtlRunJavaScriptExtArr(int64_t id, CArrUI8 cScript,
425         void (*callbackRef)(RetDataI64))
426     {
427         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
428         std::string script = std::string(reinterpret_cast<char*>(cScript.head), cScript.size);
429         auto onChange = CJLambda::Create(callbackRef);
430         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
431             return NWebError::INIT_ERROR;
432         }
433         nativeWebviewCtl->RunJavaScriptExt(script, onChange);
434         return NWebError::NO_ERROR;
435     }
436 
FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id,CArrI64 cFuncIds,const char * cName,CArrString cMethodList)437     int32_t FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id,
438         CArrI64 cFuncIds,  const char* cName, CArrString cMethodList)
439     {
440         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
441         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
442             return NWebError::INIT_ERROR;
443         }
444         std::string objName = std::string(cName);
445         std::vector<std::string> methodList;
446         for (int64_t i = 0; i < cMethodList.size; i++) {
447             methodList.push_back(std::string(cMethodList.head[i]));
448         }
449         std::vector<std::function<char*(const char*)>> cjFuncs;
450         for (int64_t i = 0; i < cFuncIds.size; i++) {
451             auto cFunc = reinterpret_cast<char*(*)(const char*)>(cFuncIds.head[i]);
452             auto onChange = [lambda = CJLambda::Create(cFunc)]
453                 (const char* infoRef) -> char* { return lambda(infoRef); };
454             cjFuncs.push_back(onChange);
455         }
456         nativeWebviewCtl->SetNWebJavaScriptResultCallBack();
457         nativeWebviewCtl->RegisterJavaScriptProxy(cjFuncs, objName, methodList);
458         return NWebError::NO_ERROR;
459     }
460 
FfiOHOSWebviewCtlRegisterJavaScriptProxyEx(int64_t id,CArrI64 cFuncIds,const char * cName,CArrString cMethodList,char * permission)461     int32_t FfiOHOSWebviewCtlRegisterJavaScriptProxyEx(int64_t id, CArrI64 cFuncIds,
462         const char* cName, CArrString cMethodList, char* permission)
463     {
464         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
465         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
466             return NWebError::INIT_ERROR;
467         }
468         std::string objName = std::string(cName);
469         std::vector<std::string> methodList;
470         for (int64_t i = 0; i < cMethodList.size; i++) {
471             methodList.push_back(std::string(cMethodList.head[i]));
472         }
473         std::vector<std::function<char*(const char*)>> cjFuncs;
474         for (int64_t i = 0; i < cFuncIds.size; i++) {
475             auto cFunc = reinterpret_cast<char*(*)(const char*)>(cFuncIds.head[i]);
476             auto onChange = [lambda = CJLambda::Create(cFunc)]
477                 (const char* infoRef) -> char* { return lambda(infoRef); };
478             cjFuncs.push_back(onChange);
479         }
480 
481         nativeWebviewCtl->SetNWebJavaScriptResultCallBack();
482         nativeWebviewCtl->RegisterJavaScriptProxyEx(cjFuncs, objName, methodList, permission);
483         return NWebError::NO_ERROR;
484     }
485 
FfiOHOSWebviewCtlGetUrl(int64_t id)486     RetDataCString FfiOHOSWebviewCtlGetUrl(int64_t id)
487     {
488         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
489         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
490         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
491             return ret;
492         }
493         std::string url = "";
494         url = nativeWebviewCtl->GetUrl();
495         ret.code = NWebError::NO_ERROR;
496         ret.data = MallocCString(url);
497         return ret;
498     }
499 
FfiOHOSWebviewCtlGetOriginalUrl(int64_t id)500     RetDataCString FfiOHOSWebviewCtlGetOriginalUrl(int64_t id)
501     {
502         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
503         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
504         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
505             return ret;
506         }
507         std::string url = "";
508         url = nativeWebviewCtl->GetOriginalUrl();
509         ret.code = NWebError::NO_ERROR;
510         ret.data = MallocCString(url);
511         return ret;
512     }
513 
FfiOHOSWebviewCtlPageUp(int64_t id,bool top)514     int32_t FfiOHOSWebviewCtlPageUp(int64_t id, bool top)
515     {
516         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
517         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
518             return NWebError::INIT_ERROR;
519         }
520         nativeWebviewCtl->ScrollPageUp(top);
521         return NWebError::NO_ERROR;
522     }
523 
FfiOHOSWebviewCtlPageDown(int64_t id,bool bottom)524     int32_t FfiOHOSWebviewCtlPageDown(int64_t id, bool bottom)
525     {
526         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
527         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
528             return NWebError::INIT_ERROR;
529         }
530         nativeWebviewCtl->ScrollPageDown(bottom);
531         return NWebError::NO_ERROR;
532     }
533 
FfiOHOSWebviewCtlScrollTo(int64_t id,float x,float y)534     int32_t FfiOHOSWebviewCtlScrollTo(int64_t id, float x, float y)
535     {
536         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
537         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
538             return NWebError::INIT_ERROR;
539         }
540         nativeWebviewCtl->ScrollTo(x, y);
541         return NWebError::NO_ERROR;
542     }
543 
FfiOHOSWebviewCtlScrollBy(int64_t id,float deltaX,float deltaY)544     int32_t FfiOHOSWebviewCtlScrollBy(int64_t id, float deltaX, float deltaY)
545     {
546         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
547         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
548             return NWebError::INIT_ERROR;
549         }
550         nativeWebviewCtl->ScrollBy(deltaX, deltaY);
551         return NWebError::NO_ERROR;
552     }
553 
FfiOHOSWebviewCtlScrollToWithAnime(int64_t id,float x,float y,int32_t duration)554     int32_t FfiOHOSWebviewCtlScrollToWithAnime(int64_t id, float x, float y, int32_t duration)
555     {
556         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
557         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
558             return NWebError::INIT_ERROR;
559         }
560         if (duration > 0) {
561             nativeWebviewCtl->ScrollToWithAnime(x, y, duration);
562         } else {
563             nativeWebviewCtl->ScrollTo(x, y);
564         }
565         return NWebError::NO_ERROR;
566     }
567 
FfiOHOSWebviewCtlScrollByWithAnime(int64_t id,float deltaX,float deltaY,int32_t duration)568     int32_t FfiOHOSWebviewCtlScrollByWithAnime(int64_t id, float deltaX, float deltaY, int32_t duration)
569     {
570         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
571         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
572             return NWebError::INIT_ERROR;
573         }
574         if (duration > 0) {
575             nativeWebviewCtl->ScrollByWithAnime(deltaX, deltaY, duration);
576         } else {
577             nativeWebviewCtl->ScrollBy(deltaX, deltaY);
578         }
579         return NWebError::NO_ERROR;
580     }
581 
FfiOHOSWebviewCtlForward(int64_t id)582     int32_t FfiOHOSWebviewCtlForward(int64_t id)
583     {
584         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
585         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
586             return NWebError::INIT_ERROR;
587         }
588         nativeWebviewCtl->Forward();
589         return NWebError::NO_ERROR;
590     }
591 
FfiOHOSWebviewCtlBackward(int64_t id)592     int32_t FfiOHOSWebviewCtlBackward(int64_t id)
593     {
594         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
595         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
596             return NWebError::INIT_ERROR;
597         }
598         nativeWebviewCtl->Backward();
599         return NWebError::NO_ERROR;
600     }
601 
FfiOHOSWebviewCtlBackOrForward(int64_t id,int32_t step)602     int32_t FfiOHOSWebviewCtlBackOrForward(int64_t id, int32_t step)
603     {
604         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
605         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
606             return NWebError::INIT_ERROR;
607         }
608         return nativeWebviewCtl->BackOrForward(step);
609     }
610 
FfiOHOSWebviewCtlGetProgress(int64_t id,int32_t * errCode)611     int32_t FfiOHOSWebviewCtlGetProgress(int64_t id, int32_t *errCode)
612     {
613         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
614         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
615             *errCode = NWebError::INIT_ERROR;
616             return -1;
617         }
618         int32_t progress = nativeWebviewCtl->GetProgress();
619         *errCode = NWebError::NO_ERROR;
620         return progress;
621     }
622 
FfiOHOSWebviewCtlGetPageHeight(int64_t id,int32_t * errCode)623     int32_t FfiOHOSWebviewCtlGetPageHeight(int64_t id, int32_t *errCode)
624     {
625         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
626         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
627             *errCode = NWebError::INIT_ERROR;
628             return -1;
629         }
630         int32_t pageHeight = nativeWebviewCtl->GetPageHeight();
631         *errCode = NWebError::NO_ERROR;
632         return pageHeight;
633     }
634 
FfiOHOSWebviewCtlGetTitle(int64_t id)635     RetDataCString FfiOHOSWebviewCtlGetTitle(int64_t id)
636     {
637         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
638         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
639         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
640             return ret;
641         }
642         std::string title = "";
643         title = nativeWebviewCtl->GetTitle();
644         ret.code = NWebError::NO_ERROR;
645         ret.data = MallocCString(title);
646         return ret;
647     }
648 
FfiOHOSWebviewCtlZoom(int64_t id,float factor)649     int32_t FfiOHOSWebviewCtlZoom(int64_t id, float factor)
650     {
651         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
652         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
653             return NWebError::INIT_ERROR;
654         }
655         int32_t ret = nativeWebviewCtl->Zoom(factor);
656         return ret;
657     }
658 
FfiOHOSWebviewCtlZoomIn(int64_t id)659     int32_t FfiOHOSWebviewCtlZoomIn(int64_t id)
660     {
661         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
662         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
663             return NWebError::INIT_ERROR;
664         }
665         int32_t ret = nativeWebviewCtl->ZoomIn();
666         return ret;
667     }
668 
FfiOHOSWebviewCtlZoomOut(int64_t id)669     int32_t FfiOHOSWebviewCtlZoomOut(int64_t id)
670     {
671         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
672         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
673             return NWebError::INIT_ERROR;
674         }
675         int32_t ret = nativeWebviewCtl->ZoomOut();
676         return ret;
677     }
678 
FfiOHOSWebviewCtlRequestFocus(int64_t id)679     int32_t FfiOHOSWebviewCtlRequestFocus(int64_t id)
680     {
681         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
682         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
683             return NWebError::INIT_ERROR;
684         }
685         int32_t ret = nativeWebviewCtl->RequestFocus();
686         return ret;
687     }
688 
FfiOHOSWebviewCtlClearHistory(int64_t id)689     int32_t FfiOHOSWebviewCtlClearHistory(int64_t id)
690     {
691         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
692         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
693             return NWebError::INIT_ERROR;
694         }
695         nativeWebviewCtl->ClearHistory();
696         return NWebError::NO_ERROR;
697     }
698 
FfiOHOSWebviewCtlAccessStep(int64_t id,int32_t * errCode,int32_t step)699     bool FfiOHOSWebviewCtlAccessStep(int64_t id, int32_t *errCode, int32_t step)
700     {
701         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
702         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
703             *errCode = NWebError::INIT_ERROR;
704             return false;
705         }
706         bool access = nativeWebviewCtl->AccessStep(step);
707         *errCode = NWebError::NO_ERROR;
708         return access;
709     }
710 
FfiOHOSWebviewCtlOnActive(int64_t id)711     int32_t FfiOHOSWebviewCtlOnActive(int64_t id)
712     {
713         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
714         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
715             return NWebError::INIT_ERROR;
716         }
717         nativeWebviewCtl->OnActive();
718         return NWebError::NO_ERROR;
719     }
720 
FfiOHOSWebviewCtlOnInactive(int64_t id)721     int32_t FfiOHOSWebviewCtlOnInactive(int64_t id)
722     {
723         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
724         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
725             return NWebError::INIT_ERROR;
726         }
727         nativeWebviewCtl->OnInactive();
728         return NWebError::NO_ERROR;
729     }
730 
FfiOHOSWebviewCtlGetHitTest(int64_t id,int32_t * errCode)731     int32_t FfiOHOSWebviewCtlGetHitTest(int64_t id, int32_t *errCode)
732     {
733         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
734         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
735             *errCode = NWebError::INIT_ERROR;
736             return -1;
737         }
738         int32_t type = nativeWebviewCtl->GetHitTest();
739         *errCode = NWebError::NO_ERROR;
740         return type;
741     }
742 
FfiOHOSWebviewCtlGetHitTestValue(int64_t id,int32_t * errCode)743     RetDataCString FfiOHOSWebviewCtlGetHitTestValue(int64_t id, int32_t *errCode)
744     {
745         RetDataCString ret = { .code = NWeb::HitTestResult::UNKNOWN_TYPE, .data = nullptr };
746         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
747         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
748             *errCode = NWebError::INIT_ERROR;
749             return ret;
750         }
751         std::shared_ptr<NWeb::HitTestResult> nwebResult = nativeWebviewCtl->GetHitTestValue();
752         if (nwebResult == nullptr) {
753             *errCode = NWebError::INIT_ERROR;
754             return ret;
755         }
756         *errCode = NWebError::NO_ERROR;
757         ret.code = nwebResult->GetType();
758         ret.data = MallocCString(nwebResult->GetExtra());
759         return ret;
760     }
761 
FfiOHOSWebviewCtlStoreWebArchive(int64_t id,const char * cBaseName,bool autoName,void (* callbackRef)(RetDataCString infoRef))762     int32_t FfiOHOSWebviewCtlStoreWebArchive(int64_t id, const char* cBaseName,
763         bool autoName, void (*callbackRef)(RetDataCString infoRef))
764     {
765         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
766         std::string baseName = std::string(cBaseName);
767         auto onChange = [lambda = CJLambda::Create(callbackRef)]
768             (RetDataCString infoRef) -> void { lambda(infoRef); };
769         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
770             return NWebError::INIT_ERROR;
771         }
772         nativeWebviewCtl->StoreWebArchiveCallback(baseName, autoName, onChange);
773         return NWebError::NO_ERROR;
774     }
775 
FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id,bool enable)776     int32_t FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id, bool enable)
777     {
778         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
779         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
780             return NWebError::INIT_ERROR;
781         }
782         nativeWebviewCtl->EnableSafeBrowsing(enable);
783         return NWebError::NO_ERROR;
784     }
785 
FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id,int32_t * errCode)786     bool FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id, int32_t *errCode)
787     {
788         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
789         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
790             *errCode = NWebError::INIT_ERROR;
791             return false;
792         }
793         bool isSafeBrowsingEnabled = nativeWebviewCtl->IsSafeBrowsingEnabled();
794         *errCode = NWebError::NO_ERROR;
795         return isSafeBrowsingEnabled;
796     }
797 
FfiOHOSWebviewCtlGetSecurityLevel(int64_t id,int32_t * errCode)798     int32_t FfiOHOSWebviewCtlGetSecurityLevel(int64_t id, int32_t *errCode)
799     {
800         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
801         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
802             *errCode = NWebError::INIT_ERROR;
803             return -1;
804         }
805         int32_t securityLevel = nativeWebviewCtl->GetSecurityLevel();
806         *errCode = NWebError::NO_ERROR;
807         return securityLevel;
808     }
809 
FfiOHOSWebviewCtlIsIncognitoMode(int64_t id,int32_t * errCode)810     bool FfiOHOSWebviewCtlIsIncognitoMode(int64_t id, int32_t *errCode)
811     {
812         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
813         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
814             *errCode = NWebError::INIT_ERROR;
815             return false;
816         }
817         bool incognitoMode = nativeWebviewCtl->IsIncognitoMode();
818         *errCode = NWebError::NO_ERROR;
819         return incognitoMode;
820     }
821 
FfiOHOSWebviewCtlRemoveCache(int64_t id,bool clearRom)822     int32_t FfiOHOSWebviewCtlRemoveCache(int64_t id, bool clearRom)
823     {
824         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
825         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
826             return NWebError::INIT_ERROR;
827         }
828         nativeWebviewCtl->RemoveCache(clearRom);
829         return NWebError::NO_ERROR;
830     }
831 
FfiOHOSWebviewCtlGetBackForwardEntries(int64_t id,int32_t * errCode)832     int64_t FfiOHOSWebviewCtlGetBackForwardEntries(int64_t id, int32_t *errCode)
833     {
834         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
835         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
836             *errCode = NWebError::INIT_ERROR;
837             return -1;
838         }
839 
840         std::shared_ptr<NWebHistoryList> list = nativeWebviewCtl->GetHistoryList();
841         if (!list) {
842             *errCode = NWebError::INIT_ERROR;
843             return -1;
844         }
845 
846         auto nativeWebHistoryList = FFIData::Create<WebHistoryListImpl>(list);
847         if (nativeWebHistoryList == nullptr) {
848             *errCode = NWebError::INIT_ERROR;
849             WEBVIEWLOGE("new WebHistoryList failed");
850             return -1;
851         }
852         *errCode = NWebError::NO_ERROR;
853         return nativeWebHistoryList->GetID();
854     }
855 
FfiOHOSWebviewCtlStop(int64_t id)856     int32_t FfiOHOSWebviewCtlStop(int64_t id)
857     {
858         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
859         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
860             return NWebError::INIT_ERROR;
861         }
862         nativeWebviewCtl->Stop();
863         return NWebError::NO_ERROR;
864     }
865 
FfiOHOSWebviewCtlPostUrl(int64_t id,char * url,CArrUI8 buffer)866     int32_t FfiOHOSWebviewCtlPostUrl(int64_t id, char *url, CArrUI8 buffer)
867     {
868         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
869         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
870             return NWebError::INIT_ERROR;
871         }
872         std::string sUrl = url;
873         std::vector<char> postData(buffer.head, buffer.head + buffer.size);
874         return nativeWebviewCtl->PostUrl(sUrl, postData);
875     }
876 
FfiOHOSWebviewCtlSetDownloadDelegate(int64_t id,int64_t delegateId)877     int32_t FfiOHOSWebviewCtlSetDownloadDelegate(int64_t id, int64_t delegateId)
878     {
879         NWebHelper::Instance().LoadNWebSDK();
880         auto delegate = FFIData::GetData<WebDownloadDelegateImpl>(delegateId);
881         if (!delegate) {
882             WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null");
883             return NWebError::INIT_ERROR;
884         }
885         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
886         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
887             return NWebError::INIT_ERROR;
888         }
889         int32_t nwebId = nativeWebviewCtl->GetWebId();
890         WebDownloadManagerImpl::AddDownloadDelegateForWeb(nwebId, delegate);
891         return NWebError::NO_ERROR;
892     }
893 
ParsePrepareUrl(std::string & url)894     bool ParsePrepareUrl(std::string& url)
895     {
896         if (url.size() > URL_MAXIMUM) {
897             WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
898             return false;
899         }
900         if (!regex_match(url, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
901             WEBVIEWLOGE("ParsePrepareUrl error");
902             return false;
903         }
904         return true;
905     }
906 
FfiOHOSWebviewCtlStartDownload(int64_t id,char * url)907     int32_t FfiOHOSWebviewCtlStartDownload(int64_t id, char *url)
908     {
909         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
910         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
911             return NWebError::INIT_ERROR;
912         }
913         std::string webSrc = url;
914         if (!ParsePrepareUrl(webSrc)) {
915             return NWebError::INVALID_URL;
916         }
917         int32_t nwebId = nativeWebviewCtl->GetWebId();
918         NWebHelper::Instance().LoadNWebSDK();
919         WebDownloader_StartDownload(nwebId, webSrc.c_str());
920         return NWebError::NO_ERROR;
921     }
922 
FfiOHOSWebviewCtlCreateWebMessagePorts(int64_t id,bool isExtentionType,int32_t * errCode)923     CArrI64 FfiOHOSWebviewCtlCreateWebMessagePorts(int64_t id, bool isExtentionType, int32_t *errCode)
924     {
925         CArrI64 messagePorts = {.head = nullptr, .size = 0};
926         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
927         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
928             *errCode = NWebError::INIT_ERROR;
929             return messagePorts;
930         }
931 
932         int32_t nwebId = nativeWebviewCtl->GetWebId();
933         std::vector<std::string> ports = nativeWebviewCtl->CreateWebMessagePorts();
934         if (ports.size() != INTEGER_TWO) {
935             WEBVIEWLOGE("create web message port failed");
936             *errCode = NWebError::CAN_NOT_POST_MESSAGE;
937             return messagePorts;
938         }
939         auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * INTEGER_TWO));
940         if (!arr) {
941             WEBVIEWLOGE("FfiOHOSWebviewCtlCreateWebMessagePorts failed to malloc arr.");
942             *errCode = NWebError::NEW_OOM;
943             return messagePorts;
944         }
945         for (uint32_t i = 0; i < INTEGER_TWO; i++) {
946             auto nativeWebMessagePort = FFIData::Create<WebMessagePortImpl>(nwebId, ports[i], isExtentionType);
947             if (nativeWebMessagePort == nullptr) {
948                 *errCode = NWebError::CAN_NOT_POST_MESSAGE;
949                 WEBVIEWLOGE("new nativeWebMessagePort failed");
950                 free(arr);
951                 return messagePorts;
952             }
953             arr[i] = nativeWebMessagePort->GetID();
954         }
955         *errCode = NWebError::NO_ERROR;
956         messagePorts.head = arr;
957         messagePorts.size = INTEGER_TWO;
958         return messagePorts;
959     }
960 
GetSendPorts(CArrI64 ports,std::vector<std::string> & sendPorts)961     int32_t GetSendPorts(CArrI64 ports, std::vector<std::string>& sendPorts)
962     {
963         int64_t arrayLen = ports.size;
964         if (arrayLen == 0) {
965             return NWebError::PARAM_CHECK_ERROR;
966         }
967         int64_t* portsId = ports.head;
968         if (!portsId) {
969             return NWebError::PARAM_CHECK_ERROR;
970         }
971         for (int64_t i = 0; i < arrayLen; i++) {
972             WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(portsId[i]);
973             if ((!msgPort)) {
974                 return NWebError::PARAM_CHECK_ERROR;
975             }
976             std::string portHandle = msgPort->GetPortHandle();
977             sendPorts.emplace_back(portHandle);
978         }
979         return NWebError::NO_ERROR;
980     }
981 
FfiOHOSWebviewCtlPostMessage(int64_t id,char * name,CArrI64 ports,char * uri)982     int32_t FfiOHOSWebviewCtlPostMessage(int64_t id, char* name, CArrI64 ports, char* uri)
983     {
984         WEBVIEWLOGD("post message port");
985         std::string portName = std::string(name);
986         std::vector<std::string> sendPorts;
987         int32_t ret = GetSendPorts(ports, sendPorts);
988         if (ret != NWebError::NO_ERROR) {
989             WEBVIEWLOGE("post port to html failed, getSendPorts fail");
990             return ret;
991         }
992         std::string urlStr = std::string(uri);
993         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
994         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
995             return NWebError::INIT_ERROR;
996         }
997         nativeWebviewCtl->PostWebMessage(portName, sendPorts, urlStr);
998         return NWebError::NO_ERROR;
999     }
1000 
FfiOHOSWebviewCtlSerializeWebState(int64_t id,int32_t * errCode)1001     CArrUI8 FfiOHOSWebviewCtlSerializeWebState(int64_t id, int32_t *errCode)
1002     {
1003         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1004         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1005             *errCode = NWebError::INIT_ERROR;
1006             return CArrUI8{nullptr, 0};
1007         }
1008         std::vector<uint8_t> webState = nativeWebviewCtl->SerializeWebState();
1009         uint8_t* result = VectorToCArrUI8(webState);
1010         if (result == nullptr) {
1011             WEBVIEWLOGE("FfiOHOSWebviewCtlSerializeWebStatee malloc failed");
1012             *errCode = NWebError::NEW_OOM;
1013             return CArrUI8{nullptr, 0};
1014         }
1015         *errCode = NWebError::NO_ERROR;
1016         return CArrUI8{result, webState.size()};
1017     }
1018 
FfiOHOSWebviewCtlRestoreWebState(int64_t id,CArrUI8 cState)1019     int32_t FfiOHOSWebviewCtlRestoreWebState(int64_t id, CArrUI8 cState)
1020     {
1021         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1022         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1023             return NWebError::INIT_ERROR;
1024         }
1025         std::vector<uint8_t> state(cState.head, cState.head + cState.size);
1026         nativeWebviewCtl->RestoreWebState(state);
1027         return NWebError::NO_ERROR;
1028     }
1029 
FfiOHOSWebviewCtlGetCertificate(int64_t id,int32_t * errCode)1030     CArrString FfiOHOSWebviewCtlGetCertificate(int64_t id, int32_t *errCode)
1031     {
1032         CArrString arrCertificate = {.head = nullptr, .size = 0};
1033         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1034         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1035             *errCode = NWebError::INIT_ERROR;
1036             return arrCertificate;
1037         }
1038         std::vector<std::string> certChainDerData;
1039         bool ans = nativeWebviewCtl->GetCertChainDerData(certChainDerData);
1040         if (!ans) {
1041             WEBVIEWLOGE("get cert chain data failed");
1042             return arrCertificate;
1043         }
1044         if (certChainDerData.size() > UINT8_MAX) {
1045             WEBVIEWLOGE("error, cert chain data array reach max");
1046             return arrCertificate;
1047         }
1048         *errCode = NWebError::NO_ERROR;
1049         arrCertificate.size = static_cast<int64_t>(certChainDerData.size());
1050         arrCertificate.head = OHOS::Webview::VectorToCArrString(certChainDerData);
1051         return arrCertificate;
1052     }
1053 
FfiOHOSWebviewCtlHasImage(int64_t id,void (* callbackRef)(RetDataBool))1054     int32_t FfiOHOSWebviewCtlHasImage(int64_t id, void (*callbackRef)(RetDataBool))
1055     {
1056         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1057         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1058             return NWebError::INIT_ERROR;
1059         }
1060         return nativeWebviewCtl->HasImagesCallback(CJLambda::Create(callbackRef));
1061     }
1062 
FfiWebviewCtlCustomizeSchemes(OHOS::Webview::CArrScheme schemes)1063     int32_t FfiWebviewCtlCustomizeSchemes(OHOS::Webview::CArrScheme schemes)
1064     {
1065         return WebviewControllerImpl::CustomizeSchemesArrayDataHandler(schemes);
1066     }
1067 
FfiOHOSWebviewCtlTerminateRenderProcess(int64_t id,int32_t * errCode)1068     bool FfiOHOSWebviewCtlTerminateRenderProcess(int64_t id, int32_t *errCode)
1069     {
1070         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1071         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1072             *errCode = NWebError::INIT_ERROR;
1073             return false;
1074         }
1075         bool terminate = nativeWebviewCtl->TerminateRenderProcess();
1076         *errCode = NWebError::NO_ERROR;
1077         return terminate;
1078     }
1079 
FfiOHOSWebviewCtlCloseAllMediaPresentations(int64_t id)1080     int32_t FfiOHOSWebviewCtlCloseAllMediaPresentations(int64_t id)
1081     {
1082         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1083         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1084             return NWebError::INIT_ERROR;
1085         }
1086         nativeWebviewCtl->CloseAllMediaPresentations();
1087         return NWebError::NO_ERROR;
1088     }
1089 
FfiOHOSWebviewCtlPauseAllMedia(int64_t id)1090     int32_t FfiOHOSWebviewCtlPauseAllMedia(int64_t id)
1091     {
1092         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1093         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1094             return NWebError::INIT_ERROR;
1095         }
1096         nativeWebviewCtl->PauseAllMedia();
1097         return NWebError::NO_ERROR;
1098     }
1099 
FfiOHOSWebviewCtlResumeAllMedia(int64_t id)1100     int32_t FfiOHOSWebviewCtlResumeAllMedia(int64_t id)
1101     {
1102         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1103         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1104             return NWebError::INIT_ERROR;
1105         }
1106         nativeWebviewCtl->ResumeAllMedia();
1107         return NWebError::NO_ERROR;
1108     }
1109 
FfiOHOSWebviewCtlStopAllMedia(int64_t id)1110     int32_t FfiOHOSWebviewCtlStopAllMedia(int64_t id)
1111     {
1112         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1113         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1114             return NWebError::INIT_ERROR;
1115         }
1116         nativeWebviewCtl->StopAllMedia();
1117         return NWebError::NO_ERROR;
1118     }
1119 
FfiOHOSWebviewCtlResumeAllTimers()1120     void FfiOHOSWebviewCtlResumeAllTimers()
1121     {
1122         NWeb::NWebHelper::Instance().ResumeAllTimers();
1123     }
1124 
FfiOHOSWebviewCtlPauseAllTimers()1125     void FfiOHOSWebviewCtlPauseAllTimers()
1126     {
1127         NWeb::NWebHelper::Instance().PauseAllTimers();
1128     }
1129 
FfiOHOSWebviewCtlSetPrintBackground(int64_t id,bool enable)1130     int32_t FfiOHOSWebviewCtlSetPrintBackground(int64_t id, bool enable)
1131     {
1132         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1133         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1134             return NWebError::INIT_ERROR;
1135         }
1136         nativeWebviewCtl->SetPrintBackground(enable);
1137         return NWebError::NO_ERROR;
1138     }
1139 
FfiOHOSWebviewCtlGetPrintBackground(int64_t id,int32_t * errCode)1140     bool FfiOHOSWebviewCtlGetPrintBackground(int64_t id, int32_t *errCode)
1141     {
1142         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1143         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1144             *errCode = NWebError::INIT_ERROR;
1145             return false;
1146         }
1147         bool printBackground = nativeWebviewCtl->GetPrintBackground();
1148         *errCode = NWebError::NO_ERROR;
1149         return printBackground;
1150     }
1151 
FfiOHOSWebviewCtlSetScrollable(int64_t id,bool enable)1152     int32_t FfiOHOSWebviewCtlSetScrollable(int64_t id, bool enable)
1153     {
1154         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1155         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1156             return NWebError::INIT_ERROR;
1157         }
1158         nativeWebviewCtl->SetScrollable(enable);
1159         return NWebError::NO_ERROR;
1160     }
1161 
FfiOHOSWebviewCtlGetScrollable(int64_t id,int32_t * errCode)1162     bool FfiOHOSWebviewCtlGetScrollable(int64_t id, int32_t *errCode)
1163     {
1164         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1165         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1166             *errCode = NWebError::INIT_ERROR;
1167             return false;
1168         }
1169         bool scrollable = nativeWebviewCtl->GetScrollable();
1170         *errCode = NWebError::NO_ERROR;
1171         return scrollable;
1172     }
1173 
FfiOHOSWebviewCtlEnableAdsBlock(int64_t id,bool enable)1174     void FfiOHOSWebviewCtlEnableAdsBlock(int64_t id, bool enable)
1175     {
1176         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1177         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1178             return;
1179         }
1180         nativeWebviewCtl->EnableAdsBlock(enable);
1181     }
1182 
FfiOHOSWebviewCtlIsAdsBlockEnabled(int64_t id)1183     bool FfiOHOSWebviewCtlIsAdsBlockEnabled(int64_t id)
1184     {
1185         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1186         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1187             return false;
1188         }
1189         return nativeWebviewCtl->IsAdsBlockEnabled();
1190     }
1191 
FfiOHOSWebviewCtlIsAdsBlockEnabledForCurPage(int64_t id)1192     bool FfiOHOSWebviewCtlIsAdsBlockEnabledForCurPage(int64_t id)
1193     {
1194         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1195         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1196             return false;
1197         }
1198         return nativeWebviewCtl->IsAdsBlockEnabledForCurPage();
1199     }
1200 
FfiOHOSWebviewCtlIsIntelligentTrackingPreventionEnabled(int64_t id,int32_t * errCode)1201     bool FfiOHOSWebviewCtlIsIntelligentTrackingPreventionEnabled(int64_t id, int32_t *errCode)
1202     {
1203         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1204         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1205             *errCode = NWebError::INIT_ERROR;
1206             return false;
1207         }
1208         bool isIntelligentTrackingPreventionEnabled = nativeWebviewCtl->IsIntelligentTrackingPreventionEnabled();
1209         *errCode = NWebError::NO_ERROR;
1210         return isIntelligentTrackingPreventionEnabled;
1211     }
1212 
FfiOHOSWebviewCtlEnableIntelligentTrackingPrevention(int64_t id,bool enable)1213     int32_t FfiOHOSWebviewCtlEnableIntelligentTrackingPrevention(int64_t id, bool enable)
1214     {
1215         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1216         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1217             return NWebError::INIT_ERROR;
1218         }
1219         nativeWebviewCtl->EnableIntelligentTrackingPrevention(enable);
1220         return NWebError::NO_ERROR;
1221     }
1222 
FfiOHOSWebviewCtlGetMediaPlaybackState(int64_t id,int32_t * errorCode)1223     int32_t FfiOHOSWebviewCtlGetMediaPlaybackState(int64_t id, int32_t *errorCode)
1224     {
1225         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1226         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1227             *errorCode = NWebError::INIT_ERROR;
1228             return -1;
1229         }
1230         *errorCode = NWebError::NO_ERROR;
1231         return nativeWebviewCtl->GetMediaPlaybackState();
1232     }
1233 
FfiOHOSWebviewCtlGetRenderProcessMode()1234     int32_t FfiOHOSWebviewCtlGetRenderProcessMode()
1235     {
1236         WEBVIEWLOGI("get render process mode.");
1237         int32_t mode = static_cast<int32_t>(NWebHelper::Instance().GetRenderProcessMode());
1238         return mode;
1239     }
1240 
FfiOHOSWebviewCtlSetRenderProcessMode(int32_t mode)1241     void FfiOHOSWebviewCtlSetRenderProcessMode(int32_t mode)
1242     {
1243         WEBVIEWLOGI("set render process mode.");
1244         NWebHelper::Instance().SetRenderProcessMode(
1245             static_cast<NWeb::RenderProcessMode>(mode)
1246         );
1247     }
1248 
FfiOHOSWebviewCtlWarmupServiceWorker(char * url)1249     int32_t FfiOHOSWebviewCtlWarmupServiceWorker(char* url)
1250     {
1251         size_t size = strlen(url);
1252         if (size > URL_MAXIMUM || !regex_match(url, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
1253             return NWebError::INVALID_URL;
1254         }
1255         NWeb::NWebHelper::Instance().WarmupServiceWorker(url);
1256         return NWebError::NO_ERROR;
1257     }
1258 
FfiOHOSWebviewCtlSetHostIP(char * hostname,char * address,int32_t aliveTime)1259     int32_t FfiOHOSWebviewCtlSetHostIP(char* hostname, char* address, int32_t aliveTime)
1260     {
1261         if (strlen(hostname) + 1 > UINT_MAX || strlen(hostname) + 1 > UINT_MAX || aliveTime <= 0) {
1262             return NWebError::PARAM_CHECK_ERROR;
1263         }
1264         unsigned char buf[sizeof(in6_addr)];
1265         if (!((inet_pton(AF_INET, address, buf) == 1) || (inet_pton(AF_INET6, address, buf) == 1))) {
1266             WEBVIEWLOGE("IP Error.");
1267             return NWebError::PARAM_CHECK_ERROR;
1268         }
1269         NWeb::NWebHelper::Instance().SetHostIP(hostname, address, aliveTime);
1270         return NWebError::NO_ERROR;
1271     }
1272 
FfiOHOSWebviewCtlClearHostIP(char * hostname)1273     int32_t FfiOHOSWebviewCtlClearHostIP(char* hostname)
1274     {
1275         if (strlen(hostname) + 1 > UINT_MAX) {
1276             return NWebError::PARAM_CHECK_ERROR;
1277         }
1278         NWeb::NWebHelper::Instance().ClearHostIP(hostname);
1279         return NWebError::NO_ERROR;
1280     }
1281 
FfiOHOSWebviewCtlGetLastJavascriptProxyCallingFrameUrl(int64_t id)1282     RetDataCString FfiOHOSWebviewCtlGetLastJavascriptProxyCallingFrameUrl(int64_t id)
1283     {
1284         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
1285         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1286         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1287             return ret;
1288         }
1289         std::string lastCallingFrameUrl = nativeWebviewCtl->GetLastJavascriptProxyCallingFrameUrl();
1290         if (lastCallingFrameUrl == "") {
1291             ret.code = NWebError::INIT_ERROR;
1292         } else {
1293             ret.code = NWebError::NO_ERROR;
1294         }
1295         ret.data = MallocCString(lastCallingFrameUrl);
1296         return ret;
1297     }
1298 
FfiOHOSWebviewCtlEnableWholeWebPageDrawing()1299     int32_t FfiOHOSWebviewCtlEnableWholeWebPageDrawing()
1300     {
1301         NWebHelper::Instance().EnableWholeWebPageDrawing();
1302         return NWebError::NO_ERROR;
1303     }
1304 
FfiOHOSWebviewCtlClearPrefetchedResource(CArrString cacheKeyList)1305     int32_t FfiOHOSWebviewCtlClearPrefetchedResource(CArrString cacheKeyList)
1306     {
1307         std::vector<std::string> CcacheKeyList;
1308         for (int64_t i = 0; i < cacheKeyList.size; i++) {
1309             CcacheKeyList.push_back(std::string(cacheKeyList.head[i]));
1310         }
1311         NWebHelper::Instance().ClearPrefetchedResource(CcacheKeyList);
1312         return NWebError::NO_ERROR;
1313     }
1314 
FfiOHOSWebviewCtlStartCamera(int64_t id)1315     int32_t FfiOHOSWebviewCtlStartCamera(int64_t id)
1316     {
1317         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1318         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1319             return NWebError::INIT_ERROR;
1320         }
1321         nativeWebviewCtl->StartCamera();
1322         return NWebError::NO_ERROR;
1323     }
1324 
FfiOHOSWebviewCtlStopCamera(int64_t id)1325     int32_t FfiOHOSWebviewCtlStopCamera(int64_t id)
1326     {
1327         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1328         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1329             return NWebError::INIT_ERROR;
1330         }
1331         nativeWebviewCtl->StopCamera();
1332         return NWebError::NO_ERROR;
1333     }
1334 
FfiOHOSWebviewCtlCloseCamera(int64_t id)1335     int32_t FfiOHOSWebviewCtlCloseCamera(int64_t id)
1336     {
1337         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1338         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1339             return NWebError::INIT_ERROR;
1340         }
1341         nativeWebviewCtl->CloseCamera();
1342         return NWebError::NO_ERROR;
1343     }
1344 
FfiOHOSWebviewCtlAddIntelligentTrackingPreventionBypassingList(CArrString hostList)1345     int32_t FfiOHOSWebviewCtlAddIntelligentTrackingPreventionBypassingList(CArrString hostList)
1346     {
1347         std::vector<std::string> hosts;
1348         for (int64_t i = 0; i < hostList.size; i++) {
1349             hosts.push_back(std::string(hostList.head[i]));
1350         }
1351         NWebHelper::Instance().AddIntelligentTrackingPreventionBypassingList(hosts);
1352         return NWebError::NO_ERROR;
1353     }
1354 
FfiOHOSWebviewCtlClearIntelligentTrackingPreventionBypassingList()1355     void FfiOHOSWebviewCtlClearIntelligentTrackingPreventionBypassingList()
1356     {
1357         WEBVIEWLOGD("Clear intelligent tracking prevention bypassing list.")
1358         NWebHelper::Instance().ClearIntelligentTrackingPreventionBypassingList();
1359         return;
1360     }
1361 
FfiOHOSWebviewCtlRemoveIntelligentTrackingPreventionBypassingList(CArrString hostList)1362     void FfiOHOSWebviewCtlRemoveIntelligentTrackingPreventionBypassingList(CArrString hostList)
1363     {
1364         WEBVIEWLOGD("Remove intelligent tracking prevention bypassing list.")
1365         std::vector<std::string> hosts;
1366         for (int64_t i = 0; i < hostList.size; i++) {
1367             hosts.push_back(std::string(hostList.head[i]));
1368         }
1369         NWebHelper::Instance().RemoveIntelligentTrackingPreventionBypassingList(hosts);
1370         return;
1371     }
1372 
FfiOHOSWebviewCtlEnableBackForwardCache(bool nativeEmbed,bool mediaTakeOver)1373     void FfiOHOSWebviewCtlEnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver)
1374     {
1375         NWebHelper::Instance().EnableBackForwardCache(nativeEmbed, mediaTakeOver);
1376         return;
1377     }
1378 
FfiOHOSWebviewCtlGetSurfaceId(int64_t id)1379     RetDataCString FfiOHOSWebviewCtlGetSurfaceId(int64_t id)
1380     {
1381         RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr };
1382         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1383         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1384             return ret;
1385         }
1386         std::string surfaceId = nativeWebviewCtl->GetSurfaceId();
1387         ret.code = NWebError::NO_ERROR;
1388         ret.data = MallocCString(surfaceId);
1389         return ret;
1390     }
1391 
FfiOHOSWebviewCtlInjectOfflineResources(int64_t id,CArrOfflineResourceMap maps)1392     int32_t FfiOHOSWebviewCtlInjectOfflineResources(int64_t id, CArrOfflineResourceMap maps)
1393     {
1394         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1395         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1396             return NWebError::INIT_ERROR;
1397         }
1398         int64_t count = maps.size;
1399         for (int64_t i = 0; i < count; i++) {
1400             COfflineResourceMap map = maps.head[i];
1401             std::vector<std::string> urlList;
1402             for (int i = 0; i < map.urlList.size; i++) {
1403                 urlList.push_back(std::string(map.urlList.head[i]));
1404             }
1405             for (auto url : urlList) {
1406                 if (!CheckUrl(url)) {
1407                     return NWebError::INVALID_URL;
1408                 }
1409             }
1410             std::vector<uint8_t> resource;
1411             for (int i = 0; i < map.resourceCSize; i++) {
1412                 resource.push_back(map.resource[i]);
1413             }
1414             std::map<std::string, std::string> responseHeaders;
1415             for (int i = 0; i < map.responseHeaders.size; i++) {
1416                 std::string key = map.responseHeaders.head[i].headerKey;
1417                 std::string value = map.responseHeaders.head[i].headerValue;
1418                 responseHeaders[key] = value;
1419             }
1420             uint32_t type = map.type;
1421             nativeWebviewCtl->InjectOfflineResources(urlList, resource, responseHeaders, type);
1422         }
1423         return NWebError::NO_ERROR;
1424     }
1425 
FfiOHOSWebviewCtlSetBackForwardCacheOptions(int64_t id,int32_t size,int32_t timeToLive)1426     int32_t FfiOHOSWebviewCtlSetBackForwardCacheOptions(int64_t id, int32_t size, int32_t timeToLive)
1427     {
1428         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1429         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1430             return NWebError::INIT_ERROR;
1431         }
1432         nativeWebviewCtl->SetBackForwardCacheOptions(size, timeToLive);
1433         return NWebError::NO_ERROR;
1434     }
1435 
FfiOHOSWebviewCtlSetPathAllowingUniversalAccess(int64_t id,CArrString pathList)1436     int32_t FfiOHOSWebviewCtlSetPathAllowingUniversalAccess(int64_t id, CArrString pathList)
1437     {
1438         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1439         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1440             return NWebError::INIT_ERROR;
1441         }
1442         std::vector<std::string> paths;
1443         for (int i = 0; i < pathList.size; i++) {
1444             paths.push_back(pathList.head[i]);
1445         }
1446         std::string errorPath;
1447         nativeWebviewCtl->SetPathAllowingUniversalAccess(paths, errorPath);
1448         if (!errorPath.empty()) {
1449             WEBVIEWLOGE("%{public}s is invalid.", errorPath.c_str());
1450             return NWebError::PARAM_CHECK_ERROR;
1451         }
1452         return NWebError::NO_ERROR;
1453     }
1454 
FfiOHOSWebviewCtlSetUrlTrustList(int64_t id,char * cUrl)1455     int32_t FfiOHOSWebviewCtlSetUrlTrustList(int64_t id, char* cUrl)
1456     {
1457         WEBVIEWLOGD("SetUrlTrustList invoked");
1458         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1459         if (nativeWebviewCtl == nullptr) {
1460             return NWebError::INIT_ERROR;
1461         }
1462         std::string urlTrustList = cUrl;
1463         if (urlTrustList.size() > MAX_URL_TRUST_LIST_STR_LEN) {
1464             WEBVIEWLOGE("url trust list len is too large.");
1465             return NWebError::PARAM_CHECK_ERROR;
1466         }
1467         std::string detailMsg;
1468         int32_t ret = nativeWebviewCtl->SetUrlTrustList(urlTrustList, detailMsg);
1469         if (ret != NWebError::NO_ERROR) {
1470             WEBVIEWLOGE("SetUrlTrustList failed, error code : %{public}d", ret);
1471         }
1472         return ret;
1473     }
1474 
FfiOHOSWebviewCtlPrefetchResource(CRequestInfo request,ArrWebHeader additionalHttpHeaders,char * cKey,int32_t cacheValidTime)1475     int32_t FfiOHOSWebviewCtlPrefetchResource(
1476         CRequestInfo request, ArrWebHeader additionalHttpHeaders, char* cKey, int32_t cacheValidTime)
1477     {
1478         std::string url = request.url;
1479         std::string method = request.method;
1480         std::string formData = request.formData;
1481         if (method != "POST") {
1482             WEBVIEWLOGE(
1483                 "The method %{public}s is not supported. The Type of 'method' must be string 'POST'.", method.c_str());
1484             return NWebError::PARAM_CHECK_ERROR;
1485         }
1486         std::shared_ptr<NWebEnginePrefetchArgs> prefetchArgs =
1487             std::make_shared<NWebEnginePrefetchArgsImpl>(url, method, formData);
1488         std::map<std::string, std::string> headers;
1489         for (int i = 0; i < additionalHttpHeaders.size; i++) {
1490             std::string key = additionalHttpHeaders.head[i].headerKey;
1491             std::string value = additionalHttpHeaders.head[i].headerValue;
1492             headers[key] = value;
1493         }
1494 
1495         std::string cacheKey = cKey;
1496         if (cacheKey == "") {
1497             cacheKey = url;
1498         }
1499         for (char c : cacheKey) {
1500             if (!isalnum(c)) {
1501                 WEBVIEWLOGE("The character of 'cacheKey' must be numbers or letters.");
1502                 return NWebError::PARAM_CHECK_ERROR;
1503             }
1504         }
1505         NWebHelper::Instance().PrefetchResource(prefetchArgs, headers, cacheKey, cacheValidTime);
1506         return NWebError::NO_ERROR;
1507     }
1508 
1509     // BackForwardList
FfiOHOSBackForwardListCurrentIndex(int64_t id,int32_t * errCode)1510     int32_t FfiOHOSBackForwardListCurrentIndex(int64_t id, int32_t *errCode)
1511     {
1512         auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
1513         if (nativeWebHistoryListImpl == nullptr) {
1514             *errCode = NWebError::INIT_ERROR;
1515             return -1;
1516         }
1517         *errCode = NWebError::NO_ERROR;
1518         return nativeWebHistoryListImpl->GetCurrentIndex();
1519     }
1520 
FfiOHOSBackForwardListSize(int64_t id,int32_t * errCode)1521     int32_t FfiOHOSBackForwardListSize(int64_t id, int32_t *errCode)
1522     {
1523         auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
1524         if (nativeWebHistoryListImpl == nullptr) {
1525             *errCode = NWebError::INIT_ERROR;
1526             return -1;
1527         }
1528         *errCode = NWebError::NO_ERROR;
1529         return nativeWebHistoryListImpl->GetListSize();
1530     }
1531 
GetColorType(ImageColorType colorType)1532     Media::PixelFormat GetColorType(ImageColorType colorType)
1533     {
1534         Media::PixelFormat pixelFormat;
1535         switch (colorType) {
1536             case ImageColorType::COLOR_TYPE_UNKNOWN:
1537                 pixelFormat = Media::PixelFormat::UNKNOWN;
1538                 break;
1539             case ImageColorType::COLOR_TYPE_RGBA_8888:
1540                 pixelFormat = Media::PixelFormat::RGBA_8888;
1541                 break;
1542             case ImageColorType::COLOR_TYPE_BGRA_8888:
1543                 pixelFormat = Media::PixelFormat::BGRA_8888;
1544                 break;
1545             default:
1546                 pixelFormat = Media::PixelFormat::UNKNOWN;
1547                 break;
1548         }
1549         return pixelFormat;
1550     }
1551 
GetAlphaType(ImageAlphaType imageAlphaType)1552     Media::AlphaType GetAlphaType(ImageAlphaType imageAlphaType)
1553     {
1554         Media::AlphaType alphaType;
1555         switch (imageAlphaType) {
1556             case ImageAlphaType::ALPHA_TYPE_UNKNOWN:
1557                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
1558                 break;
1559             case ImageAlphaType::ALPHA_TYPE_OPAQUE:
1560                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
1561                 break;
1562             case ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED:
1563                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
1564                 break;
1565             case ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED:
1566                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
1567                 break;
1568             default:
1569                 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
1570                 break;
1571         }
1572         return alphaType;
1573     }
1574 
GetFavicon(std::shared_ptr<NWebHistoryItem> item)1575     int64_t GetFavicon(std::shared_ptr<NWebHistoryItem> item)
1576     {
1577         void *data = nullptr;
1578         int32_t width = 0;
1579         int32_t height = 0;
1580         ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
1581         ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
1582         bool isGetFavicon = item->GetFavicon(&data, width, height, colorType, alphaType);
1583         if (!isGetFavicon) {
1584             return -1;
1585         }
1586         OHOS::Media::InitializationOptions opt;
1587         opt.size.width = width;
1588         opt.size.height = height;
1589         opt.pixelFormat = GetColorType(colorType);
1590         opt.alphaType = GetAlphaType(alphaType);
1591         opt.editable = true;
1592         std::unique_ptr<Media::PixelMap> pixelMap = Media::PixelMapImpl::CreatePixelMap(opt);
1593         if (pixelMap == nullptr) {
1594             return -1;
1595         }
1596         uint64_t stride = static_cast<uint64_t>(width) << 2;
1597         uint64_t bufferSize = stride * static_cast<uint64_t>(height);
1598         pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
1599         auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap));
1600         if (nativeImage == nullptr) {
1601             return -1;
1602         }
1603         WEBVIEWLOGI("[PixelMap] create PixelMap success");
1604         return nativeImage->GetID();
1605     }
1606 
FfiOHOSWebviewCtlGetFavicon(int64_t id,int32_t * errCode)1607     int64_t FfiOHOSWebviewCtlGetFavicon(int64_t id, int32_t *errCode)
1608     {
1609         int64_t ret = -1;
1610         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1611         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1612             *errCode = NWebError::INIT_ERROR;
1613             return ret;
1614         }
1615         const void *data = nullptr;
1616         size_t width = 0;
1617         size_t height = 0;
1618         ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
1619         ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
1620         bool isGetFavicon = nativeWebviewCtl->GetFavicon(&data, width, height, colorType, alphaType);
1621         if (!isGetFavicon) {
1622             return ret;
1623         }
1624         OHOS::Media::InitializationOptions opt;
1625         opt.size.width = static_cast<int32_t>(width);
1626         opt.size.height = static_cast<int32_t>(height);
1627         opt.pixelFormat = GetColorType(colorType);
1628         opt.alphaType = GetAlphaType(alphaType);
1629         opt.editable = true;
1630         auto pixelMap = Media::PixelMap::Create(opt);
1631         if (pixelMap == nullptr) {
1632             return ret;
1633         }
1634         uint64_t stride = static_cast<uint64_t>(width) << 2;
1635         uint64_t bufferSize = stride * static_cast<uint64_t>(height);
1636         pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
1637         auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap));
1638         if (nativeImage == nullptr) {
1639             return ret;
1640         }
1641         return nativeImage->GetID();
1642     }
1643 
FfiOHOSGetItemAtIndex(int64_t id,int32_t index,int32_t * errCode)1644     CHistoryItem FfiOHOSGetItemAtIndex(int64_t id, int32_t index, int32_t *errCode)
1645     {
1646         CHistoryItem ret = {.icon = -1, .historyUrl = nullptr, .historyRawUrl = nullptr, .title = nullptr};
1647         auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id);
1648         if (nativeWebHistoryListImpl == nullptr) {
1649             *errCode = NWebError::INIT_ERROR;
1650             return ret;
1651         }
1652         if (index >= nativeWebHistoryListImpl->GetListSize() || index < 0) {
1653             *errCode = NWebError::PARAM_CHECK_ERROR;
1654             return ret;
1655         }
1656         std::shared_ptr<NWebHistoryItem> item = nativeWebHistoryListImpl->GetItem(index);
1657         if (!item) {
1658             *errCode = NWebError::NWEB_ERROR;
1659             return ret;
1660         }
1661         ret.historyUrl = MallocCString(item->GetHistoryUrl());
1662         ret.historyRawUrl = MallocCString(item->GetHistoryRawUrl());
1663         ret.title = MallocCString(item->GetHistoryTitle());
1664         ret.icon = GetFavicon(item);
1665         *errCode = NWebError::NO_ERROR;
1666         return ret;
1667     }
1668 
FfiOHOSWebviewCtlPrepareForPageLoad(char * url,bool preconnectable,int32_t numSockets)1669     int32_t FfiOHOSWebviewCtlPrepareForPageLoad(char *url, bool preconnectable, int32_t numSockets)
1670     {
1671         int32_t ret = -1;
1672         std::string webSrc = url;
1673         if (webSrc.size() > URL_MAXIMUM) {
1674             WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
1675             return NWebError::PARAM_CHECK_ERROR;
1676         }
1677 
1678         if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
1679             WEBVIEWLOGE("ParsePrepareUrl error");
1680             return NWebError::PARAM_CHECK_ERROR;
1681         }
1682 
1683         if (numSockets <= 0 || static_cast<uint32_t>(numSockets) > SOCKET_MAXIMUM) {
1684             return NWebError::PARAM_CHECK_ERROR;
1685         }
1686         NWeb::NWebHelper::Instance().PrepareForPageLoad(webSrc, preconnectable, numSockets);
1687         ret = NWebError::NO_ERROR;
1688         return ret;
1689     }
1690 
FfiOHOSWebviewCtlSetConnectionTimeout(int32_t timeout)1691     int32_t FfiOHOSWebviewCtlSetConnectionTimeout(int32_t timeout)
1692     {
1693         int32_t ret = -1;
1694         if (timeout <= 0) {
1695             return NWebError::PARAM_CHECK_ERROR;
1696         }
1697         NWeb::NWebHelper::Instance().SetConnectionTimeout(timeout);
1698         ret = NWebError::NO_ERROR;
1699         return ret;
1700     }
1701 
FfiOHOSWebviewCtlSlideScroll(int64_t id,float vx,float vy)1702     int32_t FfiOHOSWebviewCtlSlideScroll(int64_t id, float vx, float vy)
1703     {
1704         int32_t ret = -1;
1705         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1706         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1707             ret = NWebError::INIT_ERROR;
1708             return ret;
1709         }
1710         nativeWebviewCtl->SlideScroll(vx, vy);
1711         ret = NWebError::NO_ERROR;
1712         return ret;
1713     }
1714 
FfiOHOSWebviewCtlSetNetworkAvailable(int64_t id,bool enable)1715     int32_t FfiOHOSWebviewCtlSetNetworkAvailable(int64_t id, bool enable)
1716     {
1717         int32_t ret = -1;
1718         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1719         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1720             ret = NWebError::INIT_ERROR;
1721             return ret;
1722         }
1723         nativeWebviewCtl->PutNetworkAvailable(enable);
1724         ret = NWebError::NO_ERROR;
1725         return ret;
1726     }
1727 
FfiOHOSWebviewCtlClearClientAuthenticationCache(int64_t id)1728     int32_t FfiOHOSWebviewCtlClearClientAuthenticationCache(int64_t id)
1729     {
1730         int32_t ret = -1;
1731         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1732         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1733             ret = NWebError::INIT_ERROR;
1734             return ret;
1735         }
1736         nativeWebviewCtl->ClearClientAuthenticationCache();
1737         ret = NWebError::NO_ERROR;
1738         return ret;
1739     }
1740 
FfiOHOSWebviewCtlClearSslCache(int64_t id)1741     int32_t FfiOHOSWebviewCtlClearSslCache(int64_t id)
1742     {
1743         int32_t ret = -1;
1744         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1745         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1746             ret = NWebError::INIT_ERROR;
1747             return ret;
1748         }
1749         nativeWebviewCtl->ClearSslCache();
1750         ret = NWebError::NO_ERROR;
1751         return ret;
1752     }
1753 
FfiOHOSWebviewCtlSearchNext(int64_t id,bool forward)1754     int32_t FfiOHOSWebviewCtlSearchNext(int64_t id, bool forward)
1755     {
1756         int32_t ret = -1;
1757         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1758         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1759             ret = NWebError::INIT_ERROR;
1760             return ret;
1761         }
1762         nativeWebviewCtl->SearchNext(forward);
1763         ret = NWebError::NO_ERROR;
1764         return ret;
1765     }
1766 
FfiOHOSWebviewCtlClearMatches(int64_t id)1767     int32_t FfiOHOSWebviewCtlClearMatches(int64_t id)
1768     {
1769         int32_t ret = -1;
1770         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1771         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1772             ret = NWebError::INIT_ERROR;
1773             return ret;
1774         }
1775         nativeWebviewCtl->ClearMatches();
1776         ret = NWebError::NO_ERROR;
1777         return ret;
1778     }
1779 
FfiOHOSWebviewCtlSearchAllAsync(int64_t id,char * searchString)1780     int32_t FfiOHOSWebviewCtlSearchAllAsync(int64_t id, char * searchString)
1781     {
1782         int32_t ret = -1;
1783         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1784         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1785             ret = NWebError::INIT_ERROR;
1786             return ret;
1787         }
1788         std::string str = searchString;
1789         nativeWebviewCtl->SearchAllAsync(str);
1790         ret = NWebError::NO_ERROR;
1791         return ret;
1792     }
1793 
FfiOHOSWebviewCtlDeleteJavaScriptRegister(int64_t id,char * name)1794     int32_t FfiOHOSWebviewCtlDeleteJavaScriptRegister(int64_t id, char *name)
1795     {
1796         int32_t ret = -1;
1797         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1798         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1799             ret = NWebError::INIT_ERROR;
1800             return ret;
1801         }
1802         std::string str = name;
1803         ret = nativeWebviewCtl->DeleteJavaScriptRegister(str, {});
1804         return ret;
1805     }
1806 
FfiOHOSWebviewCtlSetWebSchemeHandler(int64_t id,char * cScheme,int64_t classId,int32_t * errCode)1807     void FfiOHOSWebviewCtlSetWebSchemeHandler(int64_t id, char* cScheme, int64_t classId, int32_t *errCode)
1808     {
1809         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1810         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1811             *errCode = NWebError::INIT_ERROR;
1812             return;
1813         }
1814         WebSchemeHandlerImpl* nativeWebSchemeHandler = FFIData::GetData<WebSchemeHandlerImpl>(classId);
1815         if (!nativeWebSchemeHandler) {
1816             WEBVIEWLOGE("set WebSchemeHandler is null");
1817             *errCode = NWebError::INIT_ERROR;
1818             return;
1819         }
1820         bool ret = nativeWebviewCtl->SetWebSchemeHandler(cScheme, nativeWebSchemeHandler);
1821         if (!ret) {
1822             WEBVIEWLOGE("FfiOHOSWebviewCtlSetWebSchemeHandler failed");
1823         }
1824         *errCode = NWebError::NO_ERROR;
1825         return;
1826     }
1827 
FfiOHOSWebviewCtlClearWebSchemeHandler(int64_t id)1828     int32_t FfiOHOSWebviewCtlClearWebSchemeHandler(int64_t id)
1829     {
1830         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1831         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1832             return NWebError::INIT_ERROR;
1833         }
1834         int32_t ret = nativeWebviewCtl->ClearWebSchemeHandler();
1835         if (ret != NWebError::NO_ERROR) {
1836             WEBVIEWLOGE("FfiOHOSWebviewCtlClearWebSchemeHandler failed");
1837         }
1838         return ret;
1839     }
1840 
FfiOHOSWebviewCtlSetServiceWorkerWebSchemeHandler(char * cScheme,int64_t classId)1841     int32_t FfiOHOSWebviewCtlSetServiceWorkerWebSchemeHandler(char* cScheme, int64_t classId)
1842     {
1843         auto nativeWebSchemeHandler = FFIData::GetData<WebSchemeHandlerImpl>(classId);
1844         if (!nativeWebSchemeHandler) {
1845             return NWebError::INIT_ERROR;
1846         }
1847         std::string scheme = cScheme;
1848         bool ret = WebviewControllerImpl::SetWebServiceWorkerSchemeHandler(scheme.c_str(), nativeWebSchemeHandler);
1849         if (!ret) {
1850             WEBVIEWLOGE("SetServiceWorkerWebSchemeHandler failed")
1851         }
1852         return ret;
1853     }
1854 
FfiOHOSWebviewCtlClearServiceWorkerWebSchemeHandler()1855     int32_t FfiOHOSWebviewCtlClearServiceWorkerWebSchemeHandler()
1856     {
1857         int32_t ret = WebviewControllerImpl::ClearWebServiceWorkerSchemeHandler();
1858         if (ret != 0) {
1859             WEBVIEWLOGE("FfiOHOSWebviewCtlClearServiceWorkerWebSchemeHandler ret=%{public}d", ret)
1860         }
1861         return ret;
1862     }
1863 
FfiOHOSWebviewCtlOnCreateNativeMediaPlayer(int64_t id,int64_t (* callback)(int64_t,OHOS::Webview::CMediaInfo))1864     void FfiOHOSWebviewCtlOnCreateNativeMediaPlayer(int64_t id,
1865         int64_t (* callback)(int64_t, OHOS::Webview::CMediaInfo))
1866     {
1867         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1868         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1869             WEBVIEWLOGE("Webview controller is null or not init ");
1870             return;
1871         }
1872         nativeWebviewCtl->OnCreateNativeMediaPlayer(CJLambda::Create(callback));
1873     }
1874 
FfiOHOSWebviewCtlPrecompileJavaScript(int64_t id,char * url,char * script,OHOS::Webview::CacheOptions cacheOptions)1875     int32_t FfiOHOSWebviewCtlPrecompileJavaScript(int64_t id,
1876         char* url, char* script, OHOS::Webview::CacheOptions cacheOptions)
1877     {
1878         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1879         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1880             return NWebError::INIT_ERROR;
1881         }
1882         ArrWebHeader headers = cacheOptions.arrHead;
1883         uint32_t arrayLength = static_cast<uint32_t>(headers.size);
1884         std::map<std::string, std::string> responseHeaders;
1885         for (uint32_t i = 0; i < arrayLength; ++i) {
1886             std::string key = headers.head[i].headerKey;
1887             std::string value = headers.head[i].headerValue;
1888             responseHeaders[key] = value;
1889         }
1890         auto cache = std::make_shared<NWebCacheOptionsImpl>(responseHeaders);
1891         std::string str = std::string(script);
1892         return nativeWebviewCtl->PrecompileJavaScript(url, str, cache);
1893     }
1894 
FfiOHOSWebviewCtlWebPageSnapshot(int64_t id,CSnapshotInfo snapshot,void (* callbackRef)(RetDataCSnapshotResult infoRef))1895     int32_t FfiOHOSWebviewCtlWebPageSnapshot(
1896         int64_t id, CSnapshotInfo snapshot, void (*callbackRef)(RetDataCSnapshotResult infoRef))
1897     {
1898         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1899         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1900             return NWebError::INIT_ERROR;
1901         }
1902         if (g_inWebPageSnapshot) {
1903             return NWebError::FUNCTION_NOT_ENABLE;
1904         }
1905         g_inWebPageSnapshot = true;
1906         std::string nativeSnapshotId = snapshot.id;
1907         int32_t nativeSnapshotSizeWidth = snapshot.width;
1908         int32_t nativeSnapshotSizeHeight = snapshot.height;
1909         int32_t nativeSnapshotSizeHeightType = snapshot.widthType;
1910         int32_t nativeSnapshotSizeWidthType = snapshot.heightType;
1911         NWeb::PixelUnit nativeSnapshotSizeType = NWeb::PixelUnit::NONE;
1912 
1913         if (nativeSnapshotSizeHeightType != nativeSnapshotSizeWidthType) {
1914             WEBVIEWLOGE("WebPageSnapshot input different pixel unit");
1915             g_inWebPageSnapshot = false;
1916             return NWebError::PARAM_CHECK_ERROR;
1917         }
1918         if (NWeb::PixelUnit(nativeSnapshotSizeHeightType) != NWeb::PixelUnit::NONE) {
1919             nativeSnapshotSizeType = NWeb::PixelUnit(nativeSnapshotSizeHeightType);
1920         }
1921         if (nativeSnapshotSizeWidth < 0 || nativeSnapshotSizeHeight < 0) {
1922             WEBVIEWLOGE("WebPageSnapshot input pixel length less than 0");
1923             g_inWebPageSnapshot = false;
1924             return NWebError::PARAM_CHECK_ERROR;
1925         }
1926         bool pixelCheck = false;
1927         if (nativeSnapshotSizeType != NWeb::PixelUnit::VP) {
1928             pixelCheck = true;
1929         }
1930         auto onChange = CJLambda::Create(callbackRef);
1931         auto resultCallback = CreateWebPageSnapshotResultCallback(
1932             pixelCheck, nativeSnapshotSizeWidth, nativeSnapshotSizeHeight,
1933             static_cast<int32_t>(nativeSnapshotSizeType), onChange);
1934         int32_t ret = nativeWebviewCtl->WebPageSnapshot(nativeSnapshotId.c_str(),
1935             nativeSnapshotSizeType, nativeSnapshotSizeWidth, nativeSnapshotSizeHeight, std::move(resultCallback));
1936         if (ret != NO_ERROR) {
1937             g_inWebPageSnapshot = false;
1938         }
1939         return ret;
1940     }
1941 
FfiOHOSWebviewCtlGetLastHitTest(int64_t id,int32_t * errCode)1942     RetDataCString FfiOHOSWebviewCtlGetLastHitTest(int64_t id, int32_t *errCode)
1943     {
1944         RetDataCString ret = { .code = NWeb::HitTestResult::UNKNOWN_TYPE, .data = nullptr };
1945         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1946         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1947             *errCode = NWebError::INIT_ERROR;
1948             return ret;
1949         }
1950         std::shared_ptr<NWeb::HitTestResult> nwebResult = nativeWebviewCtl->GetLastHitTest();
1951         *errCode = NWebError::NO_ERROR;
1952         if (nwebResult) {
1953             ret.code = nwebResult->GetType();
1954             ret.data = MallocCString(nwebResult->GetExtra());
1955         }
1956         return ret;
1957     }
1958 
FfiOHOSWebviewCtlGetCertificateByte(int64_t id,int32_t * errCode)1959     CCertByteData FfiOHOSWebviewCtlGetCertificateByte(int64_t id, int32_t *errCode)
1960     {
1961         CCertByteData arrCertificate = {.head = nullptr, .size = 0};
1962         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
1963         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
1964             *errCode = NWebError::INIT_ERROR;
1965             return arrCertificate;
1966         }
1967         std::vector<std::string> certChainDerData;
1968         bool ans = nativeWebviewCtl->GetCertChainDerData(certChainDerData);
1969         if (!ans) {
1970             WEBVIEWLOGE("get cert chain data failed");
1971             return arrCertificate;
1972         }
1973         if (certChainDerData.size() > UINT8_MAX) {
1974             WEBVIEWLOGE("error, cert chain data array reach max");
1975             return arrCertificate;
1976         }
1977         arrCertificate.size = static_cast<int64_t>(certChainDerData.size());
1978         if (certChainDerData.empty()) {
1979             *errCode = NWebError::NO_ERROR;
1980             arrCertificate.head = nullptr;
1981             return arrCertificate;
1982         }
1983         CArrUI8* arr = static_cast<CArrUI8*>(malloc(sizeof(CArrUI8) * certChainDerData.size()));
1984         if (!arr) {
1985             arrCertificate.head = nullptr;
1986             arrCertificate.size = 0;
1987             return arrCertificate;
1988         }
1989 
1990         for (size_t i = 0; i < certChainDerData.size(); ++i) {
1991             const std::string& str = certChainDerData[i];
1992             uint8_t* data = MallocUInt8(str);
1993             if (!data && !str.empty()) {
1994                 // 如果非空字符串分配失败,回收并返回空结果
1995                 for (size_t j = 0; j < i; ++j) {
1996                     free(arr[j].head);
1997                 }
1998                 free(arr);
1999                 arrCertificate.head = nullptr;
2000                 arrCertificate.size = 0;
2001                 return arrCertificate;
2002             }
2003             arr[i].head = data;
2004             arr[i].size = static_cast<int64_t>(str.size());
2005         }
2006         *errCode = NWebError::NO_ERROR;
2007         arrCertificate.head = arr;
2008         return arrCertificate;
2009     }
2010 
FfiOHOSWebviewCtlSetScrollableEx(int64_t id,bool enable,int32_t scrollType)2011      int32_t FfiOHOSWebviewCtlSetScrollableEx(int64_t id, bool enable, int32_t scrollType)
2012     {
2013         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2014         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2015             return NWebError::INIT_ERROR;
2016         }
2017         nativeWebviewCtl->SetScrollable(enable, scrollType);
2018         return NWebError::NO_ERROR;
2019     }
2020 
FfiOHOSWebviewCtlGetScrollOffset(int64_t id,int32_t * errorCode)2021     OHOS::Webview::CScrollOffset FfiOHOSWebviewCtlGetScrollOffset(int64_t id, int32_t* errorCode)
2022     {
2023         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2024         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2025             *errorCode = NWebError::INIT_ERROR;
2026             return {.x=0, .y=0};
2027         }
2028         float offset_x = 0;
2029         float offset_y = 0;
2030         nativeWebviewCtl->GetScrollOffset(&offset_x, &offset_y);
2031         *errorCode = NWebError::NO_ERROR;
2032         return {.x=offset_x, .y=offset_y};
2033     }
2034 
FfiOHOSWebviewCtlScrollByWithResult(int64_t id,float deltaX,float deltaY,int32_t * errorCode)2035     bool FfiOHOSWebviewCtlScrollByWithResult(int64_t id, float deltaX, float deltaY, int32_t* errorCode)
2036     {
2037         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2038         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2039             *errorCode = NWebError::INIT_ERROR;
2040             return false;
2041         }
2042         nativeWebviewCtl->ScrollByWithResult(deltaX, deltaY);
2043         *errorCode = NWebError::NO_ERROR;
2044         return true;
2045     }
2046 
FfiOHOSWebviewCtlAvoidVisibleViewportBottom(int64_t id,int32_t avoidHeight)2047     int32_t FfiOHOSWebviewCtlAvoidVisibleViewportBottom(int64_t id, int32_t avoidHeight)
2048     {
2049         auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id);
2050         if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) {
2051             return NWebError::INIT_ERROR;
2052         }
2053         return nativeWebviewCtl->AvoidVisibleViewportBottom(avoidHeight);
2054     }
2055 }
2056 }
2057 }