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