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 <regex> 19 20 #include "webview_controller_impl.h" 21 #include "webview_function.h" 22 #include "web_download_item_impl.h" 23 #include "web_download_delegate_impl.h" 24 #include "web_download_manager_impl.h" 25 #include "webview_utils.h" 26 #include "webview_javascript_execute_callback.h" 27 #include "nweb_helper.h" 28 #include "nweb_init_params.h" 29 #include "web_errors.h" 30 #include "web_download.pb.h" 31 #include "web_storage.h" 32 #include "application_context.h" 33 #include "webview_log.h" 34 #include "parameters.h" 35 #include "web_cookie_manager.h" 36 #include "web_data_base.h" 37 #include "pixel_map.h" 38 #include "cj_lambda.h" 39 #include "pixel_map_impl.h" 40 #include "geolocation_permission.h" 41 #include <regex> 42 43 using namespace OHOS::FFI; 44 using namespace OHOS::NWeb; 45 46 namespace OHOS { 47 namespace Webview { 48 49 constexpr uint32_t SOCKET_MAXIMUM = 6; 50 constexpr uint32_t URL_MAXIMUM = 2048; 51 constexpr int INTEGER_TWO = 2; 52 constexpr char URL_REGEXPR[] = "^http(s)?:\\/\\/.+"; 53 54 extern "C" { FfiOHOSWebviewOnce(char* type, void (*callbackRef)(void))55 int32_t FfiOHOSWebviewOnce(char* type, void (*callbackRef)(void)) 56 { 57 return FfiOnce(type, callbackRef); 58 } 59 FfiOHOSWebviewCtlConstructor()60 int64_t FfiOHOSWebviewCtlConstructor() 61 { 62 auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>(); 63 if (nativeWebviewCtl == nullptr) { 64 WEBVIEWLOGE("new webview controller failed"); 65 return -1; 66 } 67 WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false); 68 return nativeWebviewCtl->GetID(); 69 } 70 FfiOHOSWebviewCtlConstructorWithWebTag(char *cWebTag)71 int64_t FfiOHOSWebviewCtlConstructorWithWebTag(char *cWebTag) 72 { 73 std::string webTag = cWebTag; 74 auto nativeWebviewCtl = FFIData::Create<WebviewControllerImpl>(webTag); 75 if (nativeWebviewCtl == nullptr) { 76 WEBVIEWLOGE("new webview controller failed"); 77 return -1; 78 } 79 WebviewControllerImpl::webDebuggingAccess_ = OHOS::system::GetBoolParameter("web.debug.devtools", false); 80 return nativeWebviewCtl->GetID(); 81 } 82 FfiOHOSWebviewCtlInitializeWebEngine()83 void FfiOHOSWebviewCtlInitializeWebEngine() 84 { 85 std::shared_ptr<AbilityRuntime::ApplicationContext> ctx = 86 AbilityRuntime::ApplicationContext::GetApplicationContext(); 87 if (ctx == nullptr) { 88 WEBVIEWLOGE("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to ctx is null."); 89 return; 90 } 91 const std::string& bundle_path = ctx->GetBundleCodeDir(); 92 NWebHelper::Instance().SetBundlePath(bundle_path); 93 if (!NWebHelper::Instance().InitAndRun(true)) { 94 WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine Failed to init web engine due to NWebHelper failure."); 95 } 96 WEBVIEWLOGI("FfiOHOSWebviewCtlInitializeWebEngine NWebHelper initialized, \ 97 init web engine done, bundle_path: %{public}s", bundle_path.c_str()); 98 return; 99 } 100 FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode, char* secureDnsConfig)101 void FfiOHOSWebviewCtlSetHttpDns(int32_t secureDnsMode, char* secureDnsConfig) 102 { 103 std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>(); 104 config->SetMode(secureDnsMode); 105 config->SetConfig(secureDnsConfig); 106 WEBVIEWLOGI("set http dns mode:%{public}d doh_config:%{public}s", secureDnsMode, secureDnsConfig); 107 NWebHelper::Instance().SetHttpDns(config); 108 } 109 FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess)110 void FfiOHOSWebviewCtlSetWebDebuggingAccess(bool webDebuggingAccess) 111 { 112 if (OHOS::system::GetBoolParameter("web.debug.devtools", false)) { 113 return; 114 } 115 116 WebviewControllerImpl::webDebuggingAccess_ = webDebuggingAccess; 117 return; 118 } 119 FfiOHOSWebviewCtlLoadUrl(int64_t id, char *url)120 int32_t FfiOHOSWebviewCtlLoadUrl(int64_t id, char *url) 121 { 122 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 123 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 124 return NWebError::INIT_ERROR; 125 } 126 std::string webSrc = url; 127 128 return nativeWebviewCtl->LoadUrl(webSrc); 129 } 130 FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id, char *url, ArrWebHeader headers)131 int32_t FfiOHOSWebviewCtlLoadUrlWithHeaders(int64_t id, char *url, ArrWebHeader headers) 132 { 133 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 134 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 135 return NWebError::INIT_ERROR; 136 } 137 138 std::map<std::string, std::string> httpHeaders; 139 uint32_t arrayLength = static_cast<uint32_t>(headers.size); 140 for (uint32_t i = 0; i < arrayLength; ++i) { 141 std::string key = headers.head[i].headerKey; 142 std::string value = headers.head[i].headerValue; 143 httpHeaders[key] = value; 144 } 145 146 return nativeWebviewCtl->LoadUrl(url, httpHeaders); 147 } 148 FfiOHOSWebviewCtlLoadData(int64_t id, LoadDatas loadDatas)149 int32_t FfiOHOSWebviewCtlLoadData(int64_t id, LoadDatas loadDatas) 150 { 151 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 152 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 153 return NWebError::INIT_ERROR; 154 } 155 std::string data = loadDatas.cData; 156 std::string mimeType = loadDatas.cMimeType; 157 std::string encoding = loadDatas.cEncoding; 158 std::string baseUrl = loadDatas.cBaseUrl; 159 std::string historyUrl = loadDatas.cHistoryUrl; 160 return nativeWebviewCtl->LoadData(data, mimeType, encoding, baseUrl, historyUrl); 161 } 162 FfiOHOSWebviewCtlPreFetchPage(int64_t id, char *url)163 int32_t FfiOHOSWebviewCtlPreFetchPage(int64_t id, char *url) 164 { 165 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 166 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 167 return NWebError::INIT_ERROR; 168 } 169 std::string webSrc = url; 170 if (webSrc.size() > URL_MAXIMUM) { 171 WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM); 172 return NWebError::PARAM_CHECK_ERROR; 173 } 174 175 if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) { 176 WEBVIEWLOGE("ParsePrepareUrl error"); 177 return NWebError::PARAM_CHECK_ERROR; 178 } 179 int32_t ret = nativeWebviewCtl->PreFetchPage(webSrc); 180 if (ret != NWebError::NO_ERROR) { 181 if (ret == NWebError::NWEB_ERROR) { 182 return ret; 183 } 184 } 185 return ret; 186 } 187 FfiOHOSWebviewCtlPreFetchPageWithHeaders(int64_t id, char *url, ArrWebHeader headers)188 int32_t FfiOHOSWebviewCtlPreFetchPageWithHeaders(int64_t id, char *url, ArrWebHeader headers) 189 { 190 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 191 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 192 return NWebError::INIT_ERROR; 193 } 194 std::string webSrc = url; 195 if (webSrc.size() > URL_MAXIMUM) { 196 WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM); 197 return NWebError::PARAM_CHECK_ERROR; 198 } 199 200 if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) { 201 WEBVIEWLOGE("ParsePrepareUrl error"); 202 return NWebError::PARAM_CHECK_ERROR; 203 } 204 std::map<std::string, std::string> httpHeaders; 205 uint32_t arrayLength = static_cast<uint32_t>(headers.size); 206 for (uint32_t i = 0; i < arrayLength; ++i) { 207 std::string key = headers.head[i].headerKey; 208 std::string value = headers.head[i].headerValue; 209 httpHeaders[key] = value; 210 } 211 212 int32_t ret = nativeWebviewCtl->PreFetchPage(webSrc, httpHeaders); 213 if (ret != NWebError::NO_ERROR) { 214 if (ret == NWebError::NWEB_ERROR) { 215 WEBVIEWLOGE("preFetchPage failed."); 216 return ret; 217 } 218 } 219 return ret; 220 } 221 FfiOHOSWebviewCtlSetAudioMuted(int64_t id, bool mute)222 int32_t FfiOHOSWebviewCtlSetAudioMuted(int64_t id, bool mute) 223 { 224 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 225 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 226 return NWebError::INIT_ERROR; 227 } 228 int32_t ret = nativeWebviewCtl->SetAudioMuted(mute); 229 if (ret != NWebError::NO_ERROR) { 230 if (ret == NWebError::NWEB_ERROR) { 231 WEBVIEWLOGE("SetAudioMuted failed, error code: %{public}d", ret); 232 return ret; 233 } 234 } 235 WEBVIEWLOGI("SetAudioMuted: %{public}s", (mute ? "true" : "false")); 236 return ret; 237 } 238 FfiOHOSWebviewCtlRefresh(int64_t id)239 int32_t FfiOHOSWebviewCtlRefresh(int64_t id) 240 { 241 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 242 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 243 return NWebError::INIT_ERROR; 244 } 245 nativeWebviewCtl->Refresh(); 246 return NWebError::NO_ERROR; 247 } 248 FfiOHOSWebviewCtlGetUserAgent(int64_t id, int32_t *errCode)249 char *FfiOHOSWebviewCtlGetUserAgent(int64_t id, int32_t *errCode) 250 { 251 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 252 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 253 *errCode = NWebError::INIT_ERROR; 254 return nullptr; 255 } 256 std::string userAgent = nativeWebviewCtl->GetUserAgent(); 257 *errCode = NWebError::NO_ERROR; 258 return MallocCString(userAgent); 259 } 260 FfiOHOSWebviewCtlGetWebId(int64_t id, int32_t *errCode)261 int32_t FfiOHOSWebviewCtlGetWebId(int64_t id, int32_t *errCode) 262 { 263 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 264 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 265 *errCode = NWebError::INIT_ERROR; 266 return -1; 267 } 268 int32_t webId = -1; 269 webId = nativeWebviewCtl->GetWebId(); 270 *errCode = NWebError::NO_ERROR; 271 return webId; 272 } 273 FfiOHOSWebviewCtlAccessForward(int64_t id, int32_t *errCode)274 bool FfiOHOSWebviewCtlAccessForward(int64_t id, int32_t *errCode) 275 { 276 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 277 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 278 *errCode = NWebError::INIT_ERROR; 279 return false; 280 } 281 bool access = nativeWebviewCtl->AccessForward(); 282 *errCode = NWebError::NO_ERROR; 283 return access; 284 } 285 FfiOHOSWebviewCtlAccessBackward(int64_t id, int32_t *errCode)286 bool FfiOHOSWebviewCtlAccessBackward(int64_t id, int32_t *errCode) 287 { 288 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 289 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 290 *errCode = NWebError::INIT_ERROR; 291 return false; 292 } 293 bool access = nativeWebviewCtl->AccessBackward(); 294 *errCode = NWebError::NO_ERROR; 295 return access; 296 } 297 FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id, char *cUserAgent)298 int32_t FfiOHOSWebviewCtlSetCustomUserAgent(int64_t id, char *cUserAgent) 299 { 300 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 301 std::string userAgent = cUserAgent; 302 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 303 return NWebError::INIT_ERROR; 304 } 305 return nativeWebviewCtl->SetCustomUserAgent(userAgent); 306 } 307 FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id)308 RetDataCString FfiOHOSWebviewCtlGetCustomUserAgent(int64_t id) 309 { 310 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 311 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 312 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 313 return ret; 314 } 315 std::string userAgent = ""; 316 userAgent = nativeWebviewCtl->GetCustomUserAgent(); 317 ret.code = NWebError::NO_ERROR; 318 ret.data = MallocCString(userAgent); 319 return ret; 320 } 321 FfiOHOSWebviewCtlRunJavaScript(int64_t id, char* cScript, void (*callbackRef)(RetDataCString infoRef))322 int32_t FfiOHOSWebviewCtlRunJavaScript(int64_t id, char* cScript, 323 void (*callbackRef)(RetDataCString infoRef)) 324 { 325 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 326 std::string script = std::string(cScript); 327 auto onChange = [lambda = CJLambda::Create(callbackRef)] 328 (RetDataCString infoRef) -> void { lambda(infoRef); }; 329 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 330 return NWebError::INIT_ERROR; 331 } 332 nativeWebviewCtl->RunJavaScript(script, onChange); 333 return NWebError::NO_ERROR; 334 } 335 FfiOHOSWebviewCtlRunJavaScriptExt(int64_t id, char* cScript, void (*callbackRef)(RetDataI64))336 int32_t FfiOHOSWebviewCtlRunJavaScriptExt(int64_t id, char* cScript, 337 void (*callbackRef)(RetDataI64)) 338 { 339 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 340 std::string script = std::string(cScript); 341 auto onChange = CJLambda::Create(callbackRef); 342 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 343 return NWebError::INIT_ERROR; 344 } 345 nativeWebviewCtl->RunJavaScriptExt(script, onChange); 346 return NWebError::NO_ERROR; 347 } 348 FfiOHOSWebviewCtlRunJavaScriptExtArr(int64_t id, CArrUI8 cScript, void (*callbackRef)(RetDataI64))349 int32_t FfiOHOSWebviewCtlRunJavaScriptExtArr(int64_t id, CArrUI8 cScript, 350 void (*callbackRef)(RetDataI64)) 351 { 352 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 353 std::string script = std::string(reinterpret_cast<char*>(cScript.head), cScript.size); 354 auto onChange = CJLambda::Create(callbackRef); 355 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 356 return NWebError::INIT_ERROR; 357 } 358 nativeWebviewCtl->RunJavaScriptExt(script, onChange); 359 return NWebError::NO_ERROR; 360 } 361 FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id, CArrI64 cFuncIds, const char* cName, CArrString cMethodList)362 int32_t FfiOHOSWebviewCtlRegisterJavaScriptProxy(int64_t id, 363 CArrI64 cFuncIds, const char* cName, CArrString cMethodList) 364 { 365 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 366 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 367 return NWebError::INIT_ERROR; 368 } 369 std::string objName = std::string(cName); 370 std::vector<std::string> methodList; 371 for (int64_t i = 0; i < cMethodList.size; i++) { 372 methodList.push_back(std::string(cMethodList.head[i])); 373 } 374 std::vector<std::function<char*(const char*)>> cjFuncs; 375 for (int64_t i = 0; i < cFuncIds.size; i++) { 376 auto cFunc = reinterpret_cast<char*(*)(const char*)>(cFuncIds.head[i]); 377 auto onChange = [lambda = CJLambda::Create(cFunc)] 378 (const char* infoRef) -> char* { return lambda(infoRef); }; 379 cjFuncs.push_back(onChange); 380 } 381 nativeWebviewCtl->SetNWebJavaScriptResultCallBack(); 382 nativeWebviewCtl->RegisterJavaScriptProxy(cjFuncs, objName, methodList); 383 return NWebError::NO_ERROR; 384 } 385 FfiOHOSWebviewCtlGetUrl(int64_t id)386 RetDataCString FfiOHOSWebviewCtlGetUrl(int64_t id) 387 { 388 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 389 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 390 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 391 return ret; 392 } 393 std::string url = ""; 394 url = nativeWebviewCtl->GetUrl(); 395 ret.code = NWebError::NO_ERROR; 396 ret.data = MallocCString(url); 397 return ret; 398 } 399 FfiOHOSWebviewCtlGetOriginalUrl(int64_t id)400 RetDataCString FfiOHOSWebviewCtlGetOriginalUrl(int64_t id) 401 { 402 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 403 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 404 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 405 return ret; 406 } 407 std::string url = ""; 408 url = nativeWebviewCtl->GetOriginalUrl(); 409 ret.code = NWebError::NO_ERROR; 410 ret.data = MallocCString(url); 411 return ret; 412 } 413 FfiOHOSWebviewCtlPageUp(int64_t id, bool top)414 int32_t FfiOHOSWebviewCtlPageUp(int64_t id, bool top) 415 { 416 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 417 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 418 return NWebError::INIT_ERROR; 419 } 420 nativeWebviewCtl->ScrollPageUp(top); 421 return NWebError::NO_ERROR; 422 } 423 FfiOHOSWebviewCtlPageDown(int64_t id, bool bottom)424 int32_t FfiOHOSWebviewCtlPageDown(int64_t id, bool bottom) 425 { 426 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 427 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 428 return NWebError::INIT_ERROR; 429 } 430 nativeWebviewCtl->ScrollPageDown(bottom); 431 return NWebError::NO_ERROR; 432 } 433 434 // cookie_manager FfiOHOSCookieMgrFetchCookieSync(const char *url, bool incognitoMode, int32_t* errCode)435 const char* FfiOHOSCookieMgrFetchCookieSync(const char *url, bool incognitoMode, int32_t* errCode) 436 { 437 std::string curl = url; 438 std::string value = OHOS::NWeb::WebCookieManager::CjGetCookie(curl, incognitoMode, *errCode); 439 const char* res = MallocCString(value); 440 return res; 441 } 442 FfiOHOSCookieMgrConfigCookieSync(const char* url, const char* value, bool incognitoMode)443 int32_t FfiOHOSCookieMgrConfigCookieSync(const char* url, const char* value, bool incognitoMode) 444 { 445 std::string curl = url; 446 std::string cvalue = value; 447 return OHOS::NWeb::WebCookieManager::CjSetCookie(curl, cvalue, incognitoMode); 448 } 449 FfiOHOSCookieMgrPutAcceptCookieEnabled(bool accept)450 void FfiOHOSCookieMgrPutAcceptCookieEnabled(bool accept) 451 { 452 return OHOS::NWeb::WebCookieManager::CjPutAcceptCookieEnabled(accept); 453 } 454 FfiOHOSCookieMgrIsCookieAllowed()455 bool FfiOHOSCookieMgrIsCookieAllowed() 456 { 457 return OHOS::NWeb::WebCookieManager::CjIsCookieAllowed(); 458 } 459 FfiOHOSCookieMgrPutAcceptThirdPartyCookieEnabled(bool accept)460 void FfiOHOSCookieMgrPutAcceptThirdPartyCookieEnabled(bool accept) 461 { 462 return OHOS::NWeb::WebCookieManager::CjPutAcceptThirdPartyCookieEnabled(accept); 463 } 464 FfiOHOSCookieMgrIsThirdPartyCookieAllowed()465 bool FfiOHOSCookieMgrIsThirdPartyCookieAllowed() 466 { 467 return OHOS::NWeb::WebCookieManager::CjIsThirdPartyCookieAllowed(); 468 } 469 FfiOHOSCookieMgrExistCookie(bool incognitoMode)470 bool FfiOHOSCookieMgrExistCookie(bool incognitoMode) 471 { 472 return OHOS::NWeb::WebCookieManager::CjExistCookie(incognitoMode); 473 } 474 FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode)475 void FfiOHOSCookieMgrClearAllCookiesSync(bool incognitoMode) 476 { 477 return OHOS::NWeb::WebCookieManager::CjDeleteEntireCookie(incognitoMode); 478 } 479 FfiOHOSCookieMgrClearSessionCookieSync()480 void FfiOHOSCookieMgrClearSessionCookieSync() 481 { 482 return OHOS::NWeb::WebCookieManager::CjDeleteSessionCookie(); 483 } 484 FfiOHOSCookieMgrSaveCookieAsync(void (*callbackRef)(void))485 void FfiOHOSCookieMgrSaveCookieAsync(void (*callbackRef)(void)) 486 { 487 return OHOS::NWeb::WebCookieManager::CjSaveCookie(callbackRef); 488 } 489 FfiOHOSWebviewCtlScrollTo(int64_t id, float x, float y)490 int32_t FfiOHOSWebviewCtlScrollTo(int64_t id, float x, float y) 491 { 492 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 493 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 494 return NWebError::INIT_ERROR; 495 } 496 nativeWebviewCtl->ScrollTo(x, y); 497 return NWebError::NO_ERROR; 498 } 499 FfiOHOSWebviewCtlScrollBy(int64_t id, float deltaX, float deltaY)500 int32_t FfiOHOSWebviewCtlScrollBy(int64_t id, float deltaX, float deltaY) 501 { 502 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 503 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 504 return NWebError::INIT_ERROR; 505 } 506 nativeWebviewCtl->ScrollBy(deltaX, deltaY); 507 return NWebError::NO_ERROR; 508 } 509 FfiOHOSWebviewCtlScrollToWithAnime(int64_t id, float x, float y, int32_t duration)510 int32_t FfiOHOSWebviewCtlScrollToWithAnime(int64_t id, float x, float y, int32_t duration) 511 { 512 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 513 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 514 return NWebError::INIT_ERROR; 515 } 516 if (duration > 0) { 517 nativeWebviewCtl->ScrollToWithAnime(x, y, duration); 518 } else { 519 nativeWebviewCtl->ScrollTo(x, y); 520 } 521 return NWebError::NO_ERROR; 522 } 523 FfiOHOSWebviewCtlScrollByWithAnime(int64_t id, float deltaX, float deltaY, int32_t duration)524 int32_t FfiOHOSWebviewCtlScrollByWithAnime(int64_t id, float deltaX, float deltaY, int32_t duration) 525 { 526 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 527 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 528 return NWebError::INIT_ERROR; 529 } 530 if (duration > 0) { 531 nativeWebviewCtl->ScrollByWithAnime(deltaX, deltaY, duration); 532 } else { 533 nativeWebviewCtl->ScrollBy(deltaX, deltaY); 534 } 535 return NWebError::NO_ERROR; 536 } 537 FfiOHOSWebviewCtlForward(int64_t id)538 int32_t FfiOHOSWebviewCtlForward(int64_t id) 539 { 540 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 541 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 542 return NWebError::INIT_ERROR; 543 } 544 nativeWebviewCtl->Forward(); 545 return NWebError::NO_ERROR; 546 } 547 FfiOHOSWebviewCtlBackward(int64_t id)548 int32_t FfiOHOSWebviewCtlBackward(int64_t id) 549 { 550 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 551 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 552 return NWebError::INIT_ERROR; 553 } 554 nativeWebviewCtl->Backward(); 555 return NWebError::NO_ERROR; 556 } 557 FfiOHOSWebviewCtlBackOrForward(int64_t id, int32_t step)558 int32_t FfiOHOSWebviewCtlBackOrForward(int64_t id, int32_t step) 559 { 560 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 561 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 562 return NWebError::INIT_ERROR; 563 } 564 return nativeWebviewCtl->BackOrForward(step); 565 } 566 FfiOHOSWebviewCtlGetPageHeight(int64_t id, int32_t *errCode)567 int32_t FfiOHOSWebviewCtlGetPageHeight(int64_t id, int32_t *errCode) 568 { 569 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 570 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 571 *errCode = NWebError::INIT_ERROR; 572 return -1; 573 } 574 int32_t pageHeight = nativeWebviewCtl->GetPageHeight(); 575 *errCode = NWebError::NO_ERROR; 576 return pageHeight; 577 } 578 FfiOHOSWebviewCtlGetTitle(int64_t id)579 RetDataCString FfiOHOSWebviewCtlGetTitle(int64_t id) 580 { 581 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 582 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 583 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 584 return ret; 585 } 586 std::string title = ""; 587 title = nativeWebviewCtl->GetTitle(); 588 ret.code = NWebError::NO_ERROR; 589 ret.data = MallocCString(title); 590 return ret; 591 } 592 FfiOHOSWebviewCtlZoom(int64_t id, float factor)593 int32_t FfiOHOSWebviewCtlZoom(int64_t id, float factor) 594 { 595 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 596 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 597 return NWebError::INIT_ERROR; 598 } 599 int32_t ret = nativeWebviewCtl->Zoom(factor); 600 return ret; 601 } 602 FfiOHOSWebviewCtlZoomIn(int64_t id)603 int32_t FfiOHOSWebviewCtlZoomIn(int64_t id) 604 { 605 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 606 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 607 return NWebError::INIT_ERROR; 608 } 609 int32_t ret = nativeWebviewCtl->ZoomIn(); 610 return ret; 611 } 612 FfiOHOSWebviewCtlZoomOut(int64_t id)613 int32_t FfiOHOSWebviewCtlZoomOut(int64_t id) 614 { 615 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 616 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 617 return NWebError::INIT_ERROR; 618 } 619 int32_t ret = nativeWebviewCtl->ZoomOut(); 620 return ret; 621 } 622 FfiOHOSWebviewCtlRequestFocus(int64_t id)623 int32_t FfiOHOSWebviewCtlRequestFocus(int64_t id) 624 { 625 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 626 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 627 return NWebError::INIT_ERROR; 628 } 629 int32_t ret = nativeWebviewCtl->RequestFocus(); 630 return ret; 631 } 632 FfiOHOSWebviewCtlClearHistory(int64_t id)633 int32_t FfiOHOSWebviewCtlClearHistory(int64_t id) 634 { 635 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 636 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 637 return NWebError::INIT_ERROR; 638 } 639 nativeWebviewCtl->ClearHistory(); 640 return NWebError::NO_ERROR; 641 } 642 FfiOHOSWebviewCtlAccessStep(int64_t id, int32_t *errCode, int32_t step)643 bool FfiOHOSWebviewCtlAccessStep(int64_t id, int32_t *errCode, int32_t step) 644 { 645 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 646 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 647 *errCode = NWebError::INIT_ERROR; 648 return false; 649 } 650 bool access = nativeWebviewCtl->AccessStep(step); 651 *errCode = NWebError::NO_ERROR; 652 return access; 653 } 654 FfiOHOSWebviewCtlOnActive(int64_t id)655 int32_t FfiOHOSWebviewCtlOnActive(int64_t id) 656 { 657 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 658 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 659 return NWebError::INIT_ERROR; 660 } 661 nativeWebviewCtl->OnActive(); 662 return NWebError::NO_ERROR; 663 } 664 FfiOHOSWebviewCtlOnInactive(int64_t id)665 int32_t FfiOHOSWebviewCtlOnInactive(int64_t id) 666 { 667 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 668 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 669 return NWebError::INIT_ERROR; 670 } 671 nativeWebviewCtl->OnInactive(); 672 return NWebError::NO_ERROR; 673 } 674 FfiOHOSWebviewCtlGetHitTest(int64_t id, int32_t *errCode)675 int32_t FfiOHOSWebviewCtlGetHitTest(int64_t id, int32_t *errCode) 676 { 677 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 678 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 679 *errCode = NWebError::INIT_ERROR; 680 return -1; 681 } 682 int32_t type = nativeWebviewCtl->GetHitTest(); 683 *errCode = NWebError::NO_ERROR; 684 return type; 685 } 686 FfiOHOSWebviewCtlGetHitTestValue(int64_t id, int32_t *errCode)687 RetDataCString FfiOHOSWebviewCtlGetHitTestValue(int64_t id, int32_t *errCode) 688 { 689 RetDataCString ret = { .code = NWeb::HitTestResult::UNKNOWN_TYPE, .data = nullptr }; 690 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 691 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 692 *errCode = NWebError::INIT_ERROR; 693 return ret; 694 } 695 std::shared_ptr<NWeb::HitTestResult> nwebResult = nativeWebviewCtl->GetHitTestValue(); 696 *errCode = NWebError::NO_ERROR; 697 ret.code = nwebResult->GetType(); 698 ret.data = MallocCString(nwebResult->GetExtra()); 699 return ret; 700 } 701 FfiOHOSWebviewCtlStoreWebArchive(int64_t id, const char* cBaseName, bool autoName, void (*callbackRef)(RetDataCString infoRef))702 int32_t FfiOHOSWebviewCtlStoreWebArchive(int64_t id, const char* cBaseName, 703 bool autoName, void (*callbackRef)(RetDataCString infoRef)) 704 { 705 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 706 std::string baseName = std::string(cBaseName); 707 auto onChange = [lambda = CJLambda::Create(callbackRef)] 708 (RetDataCString infoRef) -> void { lambda(infoRef); }; 709 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 710 return NWebError::INIT_ERROR; 711 } 712 nativeWebviewCtl->StoreWebArchiveCallback(baseName, autoName, onChange); 713 return NWebError::NO_ERROR; 714 } 715 FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id, bool enable)716 int32_t FfiOHOSWebviewCtlEnableSafeBrowsing(int64_t id, bool enable) 717 { 718 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 719 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 720 return NWebError::INIT_ERROR; 721 } 722 nativeWebviewCtl->EnableSafeBrowsing(enable); 723 return NWebError::NO_ERROR; 724 } 725 FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id, int32_t *errCode)726 bool FfiOHOSWebviewCtlIsSafeBrowsingEnabled(int64_t id, int32_t *errCode) 727 { 728 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 729 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 730 *errCode = NWebError::INIT_ERROR; 731 return false; 732 } 733 bool isSafeBrowsingEnabled = nativeWebviewCtl->IsSafeBrowsingEnabled(); 734 *errCode = NWebError::NO_ERROR; 735 return isSafeBrowsingEnabled; 736 } 737 FfiOHOSWebviewCtlGetSecurityLevel(int64_t id, int32_t *errCode)738 int32_t FfiOHOSWebviewCtlGetSecurityLevel(int64_t id, int32_t *errCode) 739 { 740 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 741 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 742 *errCode = NWebError::INIT_ERROR; 743 return -1; 744 } 745 int32_t securityLevel = nativeWebviewCtl->GetSecurityLevel(); 746 *errCode = NWebError::NO_ERROR; 747 return securityLevel; 748 } 749 FfiOHOSWebviewCtlIsIncognitoMode(int64_t id, int32_t *errCode)750 bool FfiOHOSWebviewCtlIsIncognitoMode(int64_t id, int32_t *errCode) 751 { 752 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 753 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 754 *errCode = NWebError::INIT_ERROR; 755 return false; 756 } 757 bool incognitoMode = nativeWebviewCtl->IsIncognitoMode(); 758 *errCode = NWebError::NO_ERROR; 759 return incognitoMode; 760 } 761 FfiOHOSWebviewCtlRemoveCache(int64_t id, bool clearRom)762 int32_t FfiOHOSWebviewCtlRemoveCache(int64_t id, bool clearRom) 763 { 764 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 765 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 766 return NWebError::INIT_ERROR; 767 } 768 nativeWebviewCtl->RemoveCache(clearRom); 769 return NWebError::NO_ERROR; 770 } 771 FfiOHOSWebviewCtlGetBackForwardEntries(int64_t id, int32_t *errCode)772 int64_t FfiOHOSWebviewCtlGetBackForwardEntries(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 -1; 778 } 779 780 std::shared_ptr<NWebHistoryList> list = nativeWebviewCtl->GetHistoryList(); 781 if (!list) { 782 *errCode = NWebError::INIT_ERROR; 783 return -1; 784 } 785 786 auto nativeWebHistoryList = FFIData::Create<WebHistoryListImpl>(list); 787 if (nativeWebHistoryList == nullptr) { 788 *errCode = NWebError::INIT_ERROR; 789 WEBVIEWLOGE("new WebHistoryList failed"); 790 return -1; 791 } 792 *errCode = NWebError::NO_ERROR; 793 return nativeWebHistoryList->GetID(); 794 } 795 FfiOHOSWebviewCtlStop(int64_t id)796 int32_t FfiOHOSWebviewCtlStop(int64_t id) 797 { 798 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 799 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 800 return NWebError::INIT_ERROR; 801 } 802 nativeWebviewCtl->Stop(); 803 return NWebError::NO_ERROR; 804 } 805 FfiOHOSWebviewCtlPostUrl(int64_t id, char *url, CArrUI8 buffer)806 int32_t FfiOHOSWebviewCtlPostUrl(int64_t id, char *url, CArrUI8 buffer) 807 { 808 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 809 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 810 return NWebError::INIT_ERROR; 811 } 812 std::string sUrl = url; 813 std::vector<char> postData(buffer.head, buffer.head + buffer.size); 814 return nativeWebviewCtl->PostUrl(sUrl, postData); 815 } 816 FfiOHOSWebviewCtlSetDownloadDelegate(int64_t id, int64_t delegateId)817 int32_t FfiOHOSWebviewCtlSetDownloadDelegate(int64_t id, int64_t delegateId) 818 { 819 NWebHelper::Instance().LoadNWebSDK(); 820 auto delegate = FFIData::GetData<WebDownloadDelegateImpl>(delegateId); 821 if (!delegate) { 822 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 823 return NWebError::INIT_ERROR; 824 } 825 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 826 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 827 return NWebError::INIT_ERROR; 828 } 829 int32_t nwebId = nativeWebviewCtl->GetWebId(); 830 WebDownloadManagerImpl::AddDownloadDelegateForWeb(nwebId, delegate); 831 return NWebError::NO_ERROR; 832 } 833 ParsePrepareUrl(std::string& url)834 bool ParsePrepareUrl(std::string& url) 835 { 836 if (url.size() > URL_MAXIMUM) { 837 WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM); 838 return false; 839 } 840 if (!regex_match(url, std::regex(URL_REGEXPR, std::regex_constants::icase))) { 841 WEBVIEWLOGE("ParsePrepareUrl error"); 842 return false; 843 } 844 return true; 845 } 846 FfiOHOSWebviewCtlStartDownload(int64_t id, char *url)847 int32_t FfiOHOSWebviewCtlStartDownload(int64_t id, char *url) 848 { 849 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 850 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 851 return NWebError::INIT_ERROR; 852 } 853 std::string webSrc = url; 854 if (!ParsePrepareUrl(webSrc)) { 855 return NWebError::INVALID_URL; 856 } 857 int32_t nwebId = nativeWebviewCtl->GetWebId(); 858 NWebHelper::Instance().LoadNWebSDK(); 859 WebDownloader_StartDownload(nwebId, webSrc.c_str()); 860 return NWebError::NO_ERROR; 861 } 862 FfiOHOSWebviewCtlCreateWebMessagePorts(int64_t id, bool isExtentionType, int32_t *errCode)863 CArrI64 FfiOHOSWebviewCtlCreateWebMessagePorts(int64_t id, bool isExtentionType, int32_t *errCode) 864 { 865 CArrI64 messagePorts = {.head = nullptr, .size = 0}; 866 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 867 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 868 *errCode = NWebError::INIT_ERROR; 869 return messagePorts; 870 } 871 872 int32_t nwebId = nativeWebviewCtl->GetWebId(); 873 std::vector<std::string> ports = nativeWebviewCtl->CreateWebMessagePorts(); 874 if (ports.size() != INTEGER_TWO) { 875 WEBVIEWLOGE("create web message port failed"); 876 *errCode = NWebError::CAN_NOT_POST_MESSAGE; 877 return messagePorts; 878 } 879 auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * INTEGER_TWO)); 880 if (!arr) { 881 WEBVIEWLOGE("FfiOHOSWebviewCtlCreateWebMessagePorts failed to malloc arr."); 882 *errCode = NWebError::NEW_OOM; 883 return messagePorts; 884 } 885 for (uint32_t i = 0; i < INTEGER_TWO; i++) { 886 auto nativeWebMessagePort = FFIData::Create<WebMessagePortImpl>(nwebId, ports[i], isExtentionType); 887 if (nativeWebMessagePort == nullptr) { 888 *errCode = NWebError::CAN_NOT_POST_MESSAGE; 889 WEBVIEWLOGE("new nativeWebMessagePort failed"); 890 free(arr); 891 return messagePorts; 892 } 893 arr[i] = nativeWebMessagePort->GetID(); 894 } 895 *errCode = NWebError::NO_ERROR; 896 messagePorts.head = arr; 897 messagePorts.size = INTEGER_TWO; 898 return messagePorts; 899 } 900 GetSendPorts(CArrI64 ports, std::vector<std::string>& sendPorts)901 int32_t GetSendPorts(CArrI64 ports, std::vector<std::string>& sendPorts) 902 { 903 int64_t arrayLen = ports.size; 904 if (arrayLen == 0) { 905 return NWebError::PARAM_CHECK_ERROR; 906 } 907 int64_t* portsId = ports.head; 908 if (!portsId) { 909 return NWebError::PARAM_CHECK_ERROR; 910 } 911 for (int64_t i = 0; i < arrayLen; i++) { 912 WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(portsId[i]); 913 if ((!msgPort)) { 914 return NWebError::PARAM_CHECK_ERROR; 915 } 916 std::string portHandle = msgPort->GetPortHandle(); 917 sendPorts.emplace_back(portHandle); 918 } 919 return NWebError::NO_ERROR; 920 } 921 FfiOHOSWebviewCtlPostMessage(int64_t id, char* name, CArrI64 ports, char* uri)922 int32_t FfiOHOSWebviewCtlPostMessage(int64_t id, char* name, CArrI64 ports, char* uri) 923 { 924 WEBVIEWLOGD("post message port"); 925 std::string portName = std::string(name); 926 std::vector<std::string> sendPorts; 927 int32_t ret = GetSendPorts(ports, sendPorts); 928 if (ret != NWebError::NO_ERROR) { 929 WEBVIEWLOGE("post port to html failed, getSendPorts fail"); 930 return ret; 931 } 932 std::string urlStr = std::string(uri); 933 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 934 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 935 return NWebError::INIT_ERROR; 936 } 937 nativeWebviewCtl->PostWebMessage(portName, sendPorts, urlStr); 938 return NWebError::NO_ERROR; 939 } 940 FfiOHOSWebviewCtlSerializeWebState(int64_t id, int32_t *errCode)941 CArrUI8 FfiOHOSWebviewCtlSerializeWebState(int64_t id, int32_t *errCode) 942 { 943 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 944 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 945 *errCode = NWebError::INIT_ERROR; 946 return CArrUI8{nullptr, 0}; 947 } 948 std::vector<uint8_t> webState = nativeWebviewCtl->SerializeWebState(); 949 uint8_t* result = VectorToCArrUI8(webState); 950 if (result == nullptr) { 951 WEBVIEWLOGE("FfiOHOSWebviewCtlSerializeWebStatee malloc failed"); 952 *errCode = NWebError::NEW_OOM; 953 return CArrUI8{nullptr, 0}; 954 } 955 *errCode = NWebError::NO_ERROR; 956 return CArrUI8{result, webState.size()}; 957 } 958 FfiOHOSWebviewCtlRestoreWebState(int64_t id, CArrUI8 cState)959 int32_t FfiOHOSWebviewCtlRestoreWebState(int64_t id, CArrUI8 cState) 960 { 961 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 962 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 963 return NWebError::INIT_ERROR; 964 } 965 std::vector<uint8_t> state(cState.head, cState.head + cState.size); 966 nativeWebviewCtl->RestoreWebState(state); 967 return NWebError::NO_ERROR; 968 } 969 FfiOHOSWebviewCtlGetCertificate(int64_t id, int32_t *errCode)970 CArrString FfiOHOSWebviewCtlGetCertificate(int64_t id, int32_t *errCode) 971 { 972 CArrString arrCertificate = {.head = nullptr, .size = 0}; 973 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 974 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 975 *errCode = NWebError::INIT_ERROR; 976 return arrCertificate; 977 } 978 std::vector<std::string> certChainDerData; 979 bool ans = nativeWebviewCtl->GetCertChainDerData(certChainDerData); 980 if (!ans) { 981 WEBVIEWLOGE("get cert chain data failed"); 982 return arrCertificate; 983 } 984 if (certChainDerData.size() > UINT8_MAX) { 985 WEBVIEWLOGE("error, cert chain data array reach max"); 986 return arrCertificate; 987 } 988 *errCode = NWebError::NO_ERROR; 989 arrCertificate.size = static_cast<int64_t>(certChainDerData.size()); 990 arrCertificate.head = OHOS::Webview::VectorToCArrString(certChainDerData); 991 return arrCertificate; 992 } 993 FfiOHOSWebviewCtlHasImage(int64_t id, void (*callbackRef)(RetDataBool))994 int32_t FfiOHOSWebviewCtlHasImage(int64_t id, void (*callbackRef)(RetDataBool)) 995 { 996 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 997 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 998 return NWebError::INIT_ERROR; 999 } 1000 return nativeWebviewCtl->HasImagesCallback(CJLambda::Create(callbackRef)); 1001 } 1002 FfiWebviewCtlCustomizeSchemes(OHOS::Webview::CArrScheme schemes)1003 int32_t FfiWebviewCtlCustomizeSchemes(OHOS::Webview::CArrScheme schemes) 1004 { 1005 return WebviewControllerImpl::CustomizeSchemesArrayDataHandler(schemes); 1006 } 1007 1008 // BackForwardList FfiOHOSBackForwardListCurrentIndex(int64_t id, int32_t *errCode)1009 int32_t FfiOHOSBackForwardListCurrentIndex(int64_t id, int32_t *errCode) 1010 { 1011 auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id); 1012 if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) { 1013 *errCode = NWebError::INIT_ERROR; 1014 return -1; 1015 } 1016 *errCode = NWebError::NO_ERROR; 1017 return nativeWebHistoryListImpl->GetCurrentIndex(); 1018 } 1019 FfiOHOSBackForwardListSize(int64_t id, int32_t *errCode)1020 int32_t FfiOHOSBackForwardListSize(int64_t id, int32_t *errCode) 1021 { 1022 auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id); 1023 if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) { 1024 *errCode = NWebError::INIT_ERROR; 1025 return -1; 1026 } 1027 *errCode = NWebError::NO_ERROR; 1028 return nativeWebHistoryListImpl->GetListSize(); 1029 } 1030 GetColorType(ImageColorType colorType)1031 Media::PixelFormat GetColorType(ImageColorType colorType) 1032 { 1033 Media::PixelFormat pixelFormat; 1034 switch (colorType) { 1035 case ImageColorType::COLOR_TYPE_UNKNOWN: 1036 pixelFormat = Media::PixelFormat::UNKNOWN; 1037 break; 1038 case ImageColorType::COLOR_TYPE_RGBA_8888: 1039 pixelFormat = Media::PixelFormat::RGBA_8888; 1040 break; 1041 case ImageColorType::COLOR_TYPE_BGRA_8888: 1042 pixelFormat = Media::PixelFormat::BGRA_8888; 1043 break; 1044 default: 1045 pixelFormat = Media::PixelFormat::UNKNOWN; 1046 break; 1047 } 1048 return pixelFormat; 1049 } 1050 GetAlphaType(ImageAlphaType imageAlphaType)1051 Media::AlphaType GetAlphaType(ImageAlphaType imageAlphaType) 1052 { 1053 Media::AlphaType alphaType; 1054 switch (imageAlphaType) { 1055 case ImageAlphaType::ALPHA_TYPE_UNKNOWN: 1056 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 1057 break; 1058 case ImageAlphaType::ALPHA_TYPE_OPAQUE: 1059 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; 1060 break; 1061 case ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED: 1062 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL; 1063 break; 1064 case ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED: 1065 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL; 1066 break; 1067 default: 1068 alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 1069 break; 1070 } 1071 return alphaType; 1072 } 1073 GetFavicon(std::shared_ptr<NWebHistoryItem> item)1074 int64_t GetFavicon(std::shared_ptr<NWebHistoryItem> item) 1075 { 1076 void *data = nullptr; 1077 int32_t width = 0; 1078 int32_t height = 0; 1079 ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN; 1080 ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN; 1081 bool isGetFavicon = item->GetFavicon(&data, width, height, colorType, alphaType); 1082 if (!isGetFavicon) { 1083 return -1; 1084 } 1085 OHOS::Media::InitializationOptions opt; 1086 opt.size.width = width; 1087 opt.size.height = height; 1088 opt.pixelFormat = GetColorType(colorType); 1089 opt.alphaType = GetAlphaType(alphaType); 1090 opt.editable = true; 1091 std::unique_ptr<Media::PixelMap> pixelMap = Media::PixelMapImpl::CreatePixelMap(opt); 1092 if (pixelMap == nullptr) { 1093 return -1; 1094 } 1095 uint64_t stride = static_cast<uint64_t>(width) << 2; 1096 uint64_t bufferSize = stride * static_cast<uint64_t>(height); 1097 pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize); 1098 auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap)); 1099 if (nativeImage == nullptr) { 1100 return -1; 1101 } 1102 WEBVIEWLOGI("[PixelMap] create PixelMap success"); 1103 return nativeImage->GetID(); 1104 } 1105 FfiOHOSWebviewCtlGetFavicon(int64_t id, int32_t *errCode)1106 int64_t FfiOHOSWebviewCtlGetFavicon(int64_t id, int32_t *errCode) 1107 { 1108 int64_t ret = -1; 1109 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1110 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1111 *errCode = NWebError::INIT_ERROR; 1112 return ret; 1113 } 1114 const void *data = nullptr; 1115 size_t width = 0; 1116 size_t height = 0; 1117 ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN; 1118 ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN; 1119 bool isGetFavicon = nativeWebviewCtl->GetFavicon(&data, width, height, colorType, alphaType); 1120 if (!isGetFavicon) { 1121 return ret; 1122 } 1123 OHOS::Media::InitializationOptions opt; 1124 opt.size.width = static_cast<int32_t>(width); 1125 opt.size.height = static_cast<int32_t>(height); 1126 opt.pixelFormat = GetColorType(colorType); 1127 opt.alphaType = GetAlphaType(alphaType); 1128 opt.editable = true; 1129 auto pixelMap = Media::PixelMap::Create(opt); 1130 if (pixelMap == nullptr) { 1131 return ret; 1132 } 1133 uint64_t stride = static_cast<uint64_t>(width) << 2; 1134 uint64_t bufferSize = stride * static_cast<uint64_t>(height); 1135 pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize); 1136 auto nativeImage = FFIData::Create<Media::PixelMapImpl>(move(pixelMap)); 1137 if (nativeImage == nullptr) { 1138 return ret; 1139 } 1140 return nativeImage->GetID(); 1141 } 1142 FfiOHOSGetItemAtIndex(int64_t id, int32_t index, int32_t *errCode)1143 CHistoryItem FfiOHOSGetItemAtIndex(int64_t id, int32_t index, int32_t *errCode) 1144 { 1145 CHistoryItem ret = {.icon = -1, .historyUrl = nullptr, .historyRawUrl = nullptr, .title = nullptr}; 1146 auto nativeWebHistoryListImpl = FFIData::GetData<WebHistoryListImpl>(id); 1147 if (nativeWebHistoryListImpl == nullptr || !nativeWebHistoryListImpl) { 1148 *errCode = NWebError::INIT_ERROR; 1149 return ret; 1150 } 1151 if (index >= nativeWebHistoryListImpl->GetListSize() || index < 0) { 1152 *errCode = NWebError::PARAM_CHECK_ERROR; 1153 return ret; 1154 } 1155 std::shared_ptr<NWebHistoryItem> item = nativeWebHistoryListImpl->GetItem(index); 1156 if (!item) { 1157 *errCode = NWebError::NWEB_ERROR; 1158 return ret; 1159 } 1160 ret.historyUrl = MallocCString(item->GetHistoryUrl()); 1161 ret.historyRawUrl = MallocCString(item->GetHistoryRawUrl()); 1162 ret.title = MallocCString(item->GetHistoryTitle()); 1163 ret.icon = GetFavicon(item); 1164 *errCode = NWebError::NO_ERROR; 1165 return ret; 1166 } 1167 FfiOHOSWebviewCtlPrepareForPageLoad(char *url, bool preconnectable, int32_t numSockets)1168 int32_t FfiOHOSWebviewCtlPrepareForPageLoad(char *url, bool preconnectable, int32_t numSockets) 1169 { 1170 int32_t ret = -1; 1171 std::string webSrc = url; 1172 if (webSrc.size() > URL_MAXIMUM) { 1173 WEBVIEWLOGE("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM); 1174 return NWebError::PARAM_CHECK_ERROR; 1175 } 1176 1177 if (!regex_match(webSrc, std::regex(URL_REGEXPR, std::regex_constants::icase))) { 1178 WEBVIEWLOGE("ParsePrepareUrl error"); 1179 return NWebError::PARAM_CHECK_ERROR; 1180 } 1181 1182 if (numSockets <= 0 || static_cast<uint32_t>(numSockets) > SOCKET_MAXIMUM) { 1183 return NWebError::PARAM_CHECK_ERROR; 1184 } 1185 NWeb::NWebHelper::Instance().PrepareForPageLoad(webSrc, preconnectable, numSockets); 1186 ret = NWebError::NO_ERROR; 1187 return ret; 1188 } 1189 FfiOHOSWebviewCtlSetConnectionTimeout(int32_t timeout)1190 int32_t FfiOHOSWebviewCtlSetConnectionTimeout(int32_t timeout) 1191 { 1192 int32_t ret = -1; 1193 if (timeout <= 0) { 1194 return NWebError::PARAM_CHECK_ERROR; 1195 } 1196 NWeb::NWebHelper::Instance().SetConnectionTimeout(timeout); 1197 ret = NWebError::NO_ERROR; 1198 return ret; 1199 } 1200 FfiOHOSWebviewCtlSlideScroll(int64_t id, float vx, float vy)1201 int32_t FfiOHOSWebviewCtlSlideScroll(int64_t id, float vx, float vy) 1202 { 1203 int32_t ret = -1; 1204 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1205 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1206 ret = NWebError::INIT_ERROR; 1207 return ret; 1208 } 1209 nativeWebviewCtl->SlideScroll(vx, vy); 1210 ret = NWebError::NO_ERROR; 1211 return ret; 1212 } 1213 FfiOHOSWebviewCtlSetNetworkAvailable(int64_t id, bool enable)1214 int32_t FfiOHOSWebviewCtlSetNetworkAvailable(int64_t id, bool enable) 1215 { 1216 int32_t ret = -1; 1217 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1218 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1219 ret = NWebError::INIT_ERROR; 1220 return ret; 1221 } 1222 nativeWebviewCtl->PutNetworkAvailable(enable); 1223 ret = NWebError::NO_ERROR; 1224 return ret; 1225 } 1226 FfiOHOSWebviewCtlClearClientAuthenticationCache(int64_t id)1227 int32_t FfiOHOSWebviewCtlClearClientAuthenticationCache(int64_t id) 1228 { 1229 int32_t ret = -1; 1230 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1231 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1232 ret = NWebError::INIT_ERROR; 1233 return ret; 1234 } 1235 nativeWebviewCtl->ClearClientAuthenticationCache(); 1236 ret = NWebError::NO_ERROR; 1237 return ret; 1238 } 1239 FfiOHOSWebviewCtlClearSslCache(int64_t id)1240 int32_t FfiOHOSWebviewCtlClearSslCache(int64_t id) 1241 { 1242 int32_t ret = -1; 1243 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1244 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1245 ret = NWebError::INIT_ERROR; 1246 return ret; 1247 } 1248 nativeWebviewCtl->ClearSslCache(); 1249 ret = NWebError::NO_ERROR; 1250 return ret; 1251 } 1252 FfiOHOSWebviewCtlSearchNext(int64_t id, bool forward)1253 int32_t FfiOHOSWebviewCtlSearchNext(int64_t id, bool forward) 1254 { 1255 int32_t ret = -1; 1256 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1257 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1258 ret = NWebError::INIT_ERROR; 1259 return ret; 1260 } 1261 nativeWebviewCtl->SearchNext(forward); 1262 ret = NWebError::NO_ERROR; 1263 return ret; 1264 } 1265 FfiOHOSWebviewCtlClearMatches(int64_t id)1266 int32_t FfiOHOSWebviewCtlClearMatches(int64_t id) 1267 { 1268 int32_t ret = -1; 1269 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1270 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1271 ret = NWebError::INIT_ERROR; 1272 return ret; 1273 } 1274 nativeWebviewCtl->ClearMatches(); 1275 ret = NWebError::NO_ERROR; 1276 return ret; 1277 } 1278 FfiOHOSWebviewCtlSearchAllAsync(int64_t id, char * searchString)1279 int32_t FfiOHOSWebviewCtlSearchAllAsync(int64_t id, char * searchString) 1280 { 1281 int32_t ret = -1; 1282 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1283 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1284 ret = NWebError::INIT_ERROR; 1285 return ret; 1286 } 1287 std::string str = searchString; 1288 nativeWebviewCtl->SearchAllAsync(str); 1289 ret = NWebError::NO_ERROR; 1290 return ret; 1291 } 1292 FfiOHOSWebviewCtlDeleteJavaScriptRegister(int64_t id, char *name)1293 int32_t FfiOHOSWebviewCtlDeleteJavaScriptRegister(int64_t id, char *name) 1294 { 1295 int32_t ret = -1; 1296 auto nativeWebviewCtl = FFIData::GetData<WebviewControllerImpl>(id); 1297 if (nativeWebviewCtl == nullptr || !nativeWebviewCtl->IsInit()) { 1298 ret = NWebError::INIT_ERROR; 1299 return ret; 1300 } 1301 std::string str = name; 1302 ret = nativeWebviewCtl->DeleteJavaScriptRegister(str, {}); 1303 return ret; 1304 } 1305 1306 // web data base; FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm)1307 RetDataCArrString FfiOHOSDBGetHttpAuthCredentials(const char *host, const char *realm) 1308 { 1309 std::string host_s = std::string(host); 1310 std::string realm_s = std::string(realm); 1311 1312 CArrString result = OHOS::NWeb::WebDataBase::CJGetHttpAuthCredentials(host_s, realm_s); 1313 RetDataCArrString ret; 1314 1315 if (result.size == -1) { 1316 ret.code = NWebError::HTTP_AUTH_MALLOC_FAILED; 1317 } else { 1318 ret.code = NWebError::NO_ERROR; 1319 } 1320 1321 ret.data = result; 1322 return ret; 1323 } 1324 FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, const char *username, const char *password)1325 void FfiOHOSDBSaveHttpAuthCredentials(const char *host, const char *realm, 1326 const char *username, const char *password) 1327 { 1328 std::string host_s = std::string(host); 1329 std::string realm_s = std::string(realm); 1330 std::string username_s = std::string(username); 1331 std::string password_s = std::string(password); 1332 1333 OHOS::NWeb::WebDataBase::CJSaveHttpAuthCredentials(host_s, realm_s, username_s, password_s); 1334 } 1335 FfiOHOSDBExistHttpAuthCredentials()1336 bool FfiOHOSDBExistHttpAuthCredentials() 1337 { 1338 return OHOS::NWeb::WebDataBase::CJExistHttpAuthCredentials(); 1339 } 1340 FfiOHOSDBDeleteHttpAuthCredentials()1341 void FfiOHOSDBDeleteHttpAuthCredentials() 1342 { 1343 OHOS::NWeb::WebDataBase::CJDeleteHttpAuthCredentials(); 1344 } 1345 1346 // WebDownloadItemImpl FfiOHOSWebDownloadItemImplConstructor()1347 int64_t FfiOHOSWebDownloadItemImplConstructor() 1348 { 1349 auto nativeWebDownloadItemImpl = FFIData::Create<WebDownloadItemImpl>(); 1350 if (nativeWebDownloadItemImpl == nullptr) { 1351 WEBVIEWLOGE("new web download item failed"); 1352 return -1; 1353 } 1354 return nativeWebDownloadItemImpl->GetID(); 1355 } 1356 FfiOHOSWebDownloadItemImplGetGuid(int64_t id)1357 RetDataCString FfiOHOSWebDownloadItemImplGetGuid(int64_t id) 1358 { 1359 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 1360 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1361 if (!nativeWebDownloadItemImpl) { 1362 return ret; 1363 } 1364 std::string guid = nativeWebDownloadItemImpl->guid; 1365 ret.code = NWebError::NO_ERROR; 1366 ret.data = MallocCString(guid); 1367 return ret; 1368 } 1369 FfiOHOSWebDownloadItemImplGetCurrentSpeed(int64_t id, int32_t *errCode)1370 int64_t FfiOHOSWebDownloadItemImplGetCurrentSpeed(int64_t id, int32_t *errCode) 1371 { 1372 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1373 if (!nativeWebDownloadItemImpl) { 1374 *errCode = NWebError::INIT_ERROR; 1375 return -1; 1376 } 1377 *errCode = NWebError::NO_ERROR; 1378 return static_cast<int64_t>(nativeWebDownloadItemImpl->currentSpeed); 1379 } 1380 FfiOHOSWebDownloadItemImplGetPercentComplete(int64_t id, int32_t *errCode)1381 int64_t FfiOHOSWebDownloadItemImplGetPercentComplete(int64_t id, int32_t *errCode) 1382 { 1383 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1384 if (!nativeWebDownloadItemImpl) { 1385 *errCode = NWebError::INIT_ERROR; 1386 return -1; 1387 } 1388 *errCode = NWebError::NO_ERROR; 1389 return static_cast<int64_t>(nativeWebDownloadItemImpl->percentComplete); 1390 } 1391 FfiOHOSWebDownloadItemImplGetTotalBytes(int64_t id, int32_t *errCode)1392 int64_t FfiOHOSWebDownloadItemImplGetTotalBytes(int64_t id, int32_t *errCode) 1393 { 1394 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1395 if (!nativeWebDownloadItemImpl) { 1396 *errCode = NWebError::INIT_ERROR; 1397 return -1; 1398 } 1399 *errCode = NWebError::NO_ERROR; 1400 return static_cast<int64_t>(nativeWebDownloadItemImpl->totalBytes); 1401 } 1402 FfiOHOSWebDownloadItemImplGetReceivedBytes(int64_t id, int32_t *errCode)1403 int64_t FfiOHOSWebDownloadItemImplGetReceivedBytes(int64_t id, int32_t *errCode) 1404 { 1405 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1406 if (!nativeWebDownloadItemImpl) { 1407 *errCode = NWebError::INIT_ERROR; 1408 return -1; 1409 } 1410 *errCode = NWebError::NO_ERROR; 1411 return static_cast<int64_t>(nativeWebDownloadItemImpl->receivedBytes); 1412 } 1413 FfiOHOSWebDownloadItemImplGetState(int64_t id, int32_t *errCode)1414 int32_t FfiOHOSWebDownloadItemImplGetState(int64_t id, int32_t *errCode) 1415 { 1416 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1417 if (!nativeWebDownloadItemImpl) { 1418 *errCode = NWebError::INIT_ERROR; 1419 return -1; 1420 } 1421 *errCode = NWebError::NO_ERROR; 1422 return static_cast<int32_t>(nativeWebDownloadItemImpl->state); 1423 } 1424 FfiOHOSWebDownloadItemImplGetLastErrorCode(int64_t id, int32_t *errCode)1425 int32_t FfiOHOSWebDownloadItemImplGetLastErrorCode(int64_t id, int32_t *errCode) 1426 { 1427 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1428 if (!nativeWebDownloadItemImpl) { 1429 *errCode = NWebError::INIT_ERROR; 1430 return -1; 1431 } 1432 *errCode = NWebError::NO_ERROR; 1433 return static_cast<int32_t>(nativeWebDownloadItemImpl->lastErrorCode); 1434 } 1435 FfiOHOSWebDownloadItemImplGetMethod(int64_t id)1436 RetDataCString FfiOHOSWebDownloadItemImplGetMethod(int64_t id) 1437 { 1438 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 1439 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1440 if (!nativeWebDownloadItemImpl) { 1441 return ret; 1442 } 1443 std::string methodValue = nativeWebDownloadItemImpl->method; 1444 ret.code = NWebError::NO_ERROR; 1445 ret.data = MallocCString(methodValue); 1446 return ret; 1447 } 1448 FfiOHOSWebDownloadItemImplGetMimeType(int64_t id)1449 RetDataCString FfiOHOSWebDownloadItemImplGetMimeType(int64_t id) 1450 { 1451 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 1452 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1453 if (!nativeWebDownloadItemImpl) { 1454 return ret; 1455 } 1456 std::string mimeTypeValue = nativeWebDownloadItemImpl->mimeType; 1457 ret.code = NWebError::NO_ERROR; 1458 ret.data = MallocCString(mimeTypeValue); 1459 return ret; 1460 } 1461 FfiOHOSWebDownloadItemImplGetUrl(int64_t id)1462 RetDataCString FfiOHOSWebDownloadItemImplGetUrl(int64_t id) 1463 { 1464 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 1465 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1466 if (!nativeWebDownloadItemImpl) { 1467 return ret; 1468 } 1469 std::string urlValue = nativeWebDownloadItemImpl->url; 1470 ret.code = NWebError::NO_ERROR; 1471 ret.data = MallocCString(urlValue); 1472 return ret; 1473 } 1474 FfiOHOSWebDownloadItemImplGetSuggestedFileName(int64_t id)1475 RetDataCString FfiOHOSWebDownloadItemImplGetSuggestedFileName(int64_t id) 1476 { 1477 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 1478 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1479 if (!nativeWebDownloadItemImpl) { 1480 return ret; 1481 } 1482 std::string fileNameValue = nativeWebDownloadItemImpl->suggestedFileName; 1483 ret.code = NWebError::NO_ERROR; 1484 ret.data = MallocCString(fileNameValue); 1485 return ret; 1486 } 1487 FfiOHOSWebDownloadItemImplGetFullPath(int64_t id)1488 RetDataCString FfiOHOSWebDownloadItemImplGetFullPath(int64_t id) 1489 { 1490 RetDataCString ret = { .code = NWebError::INIT_ERROR, .data = nullptr }; 1491 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1492 if (!nativeWebDownloadItemImpl) { 1493 return ret; 1494 } 1495 std::string fullPath = nativeWebDownloadItemImpl->fullPath; 1496 ret.code = NWebError::NO_ERROR; 1497 ret.data = MallocCString(fullPath); 1498 return ret; 1499 } 1500 FfiOHOSWebDownloadItemImplStart(int64_t id, char *downloadPath)1501 int32_t FfiOHOSWebDownloadItemImplStart(int64_t id, char *downloadPath) 1502 { 1503 std::string sDownloadPath = downloadPath; 1504 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1505 if (!nativeWebDownloadItemImpl) { 1506 return NWebError::INIT_ERROR; 1507 } 1508 nativeWebDownloadItemImpl->downloadPath = sDownloadPath; 1509 WebDownload_Continue(nativeWebDownloadItemImpl->before_download_callback, 1510 nativeWebDownloadItemImpl->downloadPath.c_str()); 1511 return NWebError::NO_ERROR; 1512 } 1513 FfiOHOSWebDownloadItemImplCancel(int64_t id)1514 int32_t FfiOHOSWebDownloadItemImplCancel(int64_t id) 1515 { 1516 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1517 if (!nativeWebDownloadItemImpl) { 1518 return NWebError::INIT_ERROR; 1519 } 1520 if (nativeWebDownloadItemImpl->download_item_callback) { 1521 WebDownload_Cancel(nativeWebDownloadItemImpl->download_item_callback); 1522 } else if (nativeWebDownloadItemImpl->before_download_callback) { 1523 WebDownload_CancelBeforeDownload(nativeWebDownloadItemImpl->before_download_callback); 1524 } else { 1525 WEBVIEWLOGE("[DOWNLOAD] WebDownloadItem::Cancel failed for callback nullptr"); 1526 } 1527 return NWebError::NO_ERROR; 1528 } 1529 FfiOHOSWebDownloadItemImplPause(int64_t id)1530 int32_t FfiOHOSWebDownloadItemImplPause(int64_t id) 1531 { 1532 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1533 if (!nativeWebDownloadItemImpl) { 1534 return NWebError::INIT_ERROR; 1535 } 1536 NWebDownloadItemState state = WebDownload_GetItemState( 1537 nativeWebDownloadItemImpl->nwebId, nativeWebDownloadItemImpl->webDownloadId); 1538 if (state != NWebDownloadItemState::IN_PROGRESS && 1539 state != NWebDownloadItemState::PENDING) { 1540 return NWebError::DOWNLOAD_NOT_START; 1541 } 1542 if (nativeWebDownloadItemImpl->download_item_callback) { 1543 WebDownload_Pause(nativeWebDownloadItemImpl->download_item_callback); 1544 } else if (nativeWebDownloadItemImpl->before_download_callback) { 1545 WebDownload_PauseBeforeDownload(nativeWebDownloadItemImpl->before_download_callback); 1546 } else { 1547 WEBVIEWLOGE("[DOWNLOAD] WebDownloadItem::Pause failed for callback nullptr"); 1548 } 1549 return NWebError::NO_ERROR; 1550 } 1551 FfiOHOSWebDownloadItemImplResume(int64_t id)1552 int32_t FfiOHOSWebDownloadItemImplResume(int64_t id) 1553 { 1554 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1555 if (!nativeWebDownloadItemImpl) { 1556 return NWebError::INIT_ERROR; 1557 } 1558 NWebDownloadItemState state = WebDownload_GetItemState( 1559 nativeWebDownloadItemImpl->nwebId, nativeWebDownloadItemImpl->webDownloadId); 1560 if (state != NWebDownloadItemState::PAUSED) { 1561 return NWebError::DOWNLOAD_NOT_PAUSED; 1562 } 1563 1564 if (nativeWebDownloadItemImpl->download_item_callback) { 1565 WebDownload_Resume(nativeWebDownloadItemImpl->download_item_callback); 1566 } else if (nativeWebDownloadItemImpl->before_download_callback) { 1567 WebDownload_ResumeBeforeDownload(nativeWebDownloadItemImpl->before_download_callback); 1568 } else { 1569 WEBVIEWLOGE("[DOWNLOAD] WebDownloadItem::Resume failed for callback nullptr"); 1570 } 1571 return NWebError::NO_ERROR; 1572 } 1573 FfiOHOSWebDownloadItemImplSerialize(int64_t id, int32_t *errCode)1574 CArrUI8 FfiOHOSWebDownloadItemImplSerialize(int64_t id, int32_t *errCode) 1575 { 1576 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(id); 1577 if (!nativeWebDownloadItemImpl) { 1578 *errCode = NWebError::INIT_ERROR; 1579 return CArrUI8{nullptr, 0}; 1580 } 1581 1582 browser_service::WebDownload webDownloadPb; 1583 webDownloadPb.set_web_download_id(nativeWebDownloadItemImpl->webDownloadId); 1584 webDownloadPb.set_current_speed(nativeWebDownloadItemImpl->currentSpeed); 1585 webDownloadPb.set_percent_complete(nativeWebDownloadItemImpl->percentComplete); 1586 webDownloadPb.set_total_bytes(nativeWebDownloadItemImpl->totalBytes); 1587 webDownloadPb.set_received_bytes(nativeWebDownloadItemImpl->receivedBytes); 1588 webDownloadPb.set_guid(nativeWebDownloadItemImpl->guid); 1589 webDownloadPb.set_full_path(nativeWebDownloadItemImpl->fullPath); 1590 webDownloadPb.set_url(nativeWebDownloadItemImpl->url); 1591 webDownloadPb.set_etag(nativeWebDownloadItemImpl->etag); 1592 webDownloadPb.set_original_url(nativeWebDownloadItemImpl->originalUrl); 1593 webDownloadPb.set_suggested_file_name(nativeWebDownloadItemImpl->suggestedFileName); 1594 webDownloadPb.set_content_disposition(nativeWebDownloadItemImpl->contentDisposition); 1595 webDownloadPb.set_mime_type(nativeWebDownloadItemImpl->mimeType); 1596 webDownloadPb.set_last_modified(nativeWebDownloadItemImpl->lastModified); 1597 webDownloadPb.set_state( 1598 static_cast<browser_service::WebDownload::WebDownloadState>(nativeWebDownloadItemImpl->state)); 1599 webDownloadPb.set_method(nativeWebDownloadItemImpl->method); 1600 webDownloadPb.set_last_error_code(nativeWebDownloadItemImpl->lastErrorCode); 1601 webDownloadPb.set_received_slices(nativeWebDownloadItemImpl->receivedSlices); 1602 webDownloadPb.set_download_path(nativeWebDownloadItemImpl->downloadPath); 1603 1604 std::string webDownloadValue; 1605 webDownloadPb.SerializeToString(&webDownloadValue); 1606 uint8_t* result = MallocUInt8(webDownloadValue); 1607 if (result == nullptr) { 1608 WEBVIEWLOGE("[DOWNLOAD] malloc failed"); 1609 *errCode = NWebError::NEW_OOM; 1610 return CArrUI8{nullptr, 0}; 1611 } 1612 *errCode = NWebError::NO_ERROR; 1613 return CArrUI8{result, webDownloadValue.length()}; 1614 } 1615 FfiOHOSWebDownloadItemImplDeserialize(CArrUI8 serializedData, int32_t *errCode)1616 int64_t FfiOHOSWebDownloadItemImplDeserialize(CArrUI8 serializedData, int32_t *errCode) 1617 { 1618 char *buffer = reinterpret_cast<char*>(serializedData.head); 1619 browser_service::WebDownload webDownloadPb; 1620 bool result = webDownloadPb.ParseFromArray(buffer, serializedData.size); 1621 if (!result) { 1622 *errCode = NWebError::INIT_ERROR; 1623 return -1; 1624 } 1625 WebDownloadItemImpl *webDownloadItem = FFIData::Create<WebDownloadItemImpl>(); 1626 if (webDownloadItem == nullptr) { 1627 WEBVIEWLOGE("new web download item failed"); 1628 *errCode = NWebError::INIT_ERROR; 1629 return -1; 1630 } 1631 webDownloadItem->webDownloadId = webDownloadPb.web_download_id(); 1632 webDownloadItem->currentSpeed = webDownloadPb.current_speed(); 1633 webDownloadItem->percentComplete = webDownloadPb.percent_complete(); 1634 webDownloadItem->totalBytes = webDownloadPb.total_bytes(); 1635 webDownloadItem->receivedBytes = webDownloadPb.received_bytes(); 1636 webDownloadItem->guid = webDownloadPb.guid(); 1637 webDownloadItem->fullPath = webDownloadPb.full_path(); 1638 webDownloadItem->url = webDownloadPb.url(); 1639 webDownloadItem->etag = webDownloadPb.etag(); 1640 webDownloadItem->originalUrl = webDownloadPb.original_url(); 1641 webDownloadItem->suggestedFileName = webDownloadPb.suggested_file_name(); 1642 webDownloadItem->contentDisposition = webDownloadPb.content_disposition(); 1643 webDownloadItem->mimeType = webDownloadPb.mime_type(); 1644 webDownloadItem->lastModified = webDownloadPb.last_modified(); 1645 webDownloadItem->state = static_cast<NWebDownloadItemState>(webDownloadPb.state()); 1646 webDownloadItem->method = webDownloadPb.method(); 1647 webDownloadItem->lastErrorCode = webDownloadPb.last_error_code(); 1648 webDownloadItem->receivedSlices = webDownloadPb.received_slices(); 1649 webDownloadItem->downloadPath = webDownloadPb.download_path(); 1650 *errCode = NWebError::NO_ERROR; 1651 return webDownloadItem->GetID(); 1652 } 1653 1654 // WebDownloadDelegateImpl FfiOHOSWebDownloadDelegateImplConstructor()1655 int64_t FfiOHOSWebDownloadDelegateImplConstructor() 1656 { 1657 auto nativeWebDownloadDelegateImpl = FFIData::Create<WebDownloadDelegateImpl>(); 1658 if (nativeWebDownloadDelegateImpl == nullptr) { 1659 WEBVIEWLOGE("new web download delegate failed"); 1660 return -1; 1661 } 1662 return nativeWebDownloadDelegateImpl->GetID(); 1663 } 1664 FfiOHOSWebDownloadDelegateImplOnBeforeDownload(int64_t id, void (*callback)(int64_t))1665 void FfiOHOSWebDownloadDelegateImplOnBeforeDownload(int64_t id, void (*callback)(int64_t)) 1666 { 1667 auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id); 1668 if (!nativeWebDownloadDelegateImpl) { 1669 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 1670 return; 1671 } 1672 nativeWebDownloadDelegateImpl->PutDownloadBeforeStart(CJLambda::Create(callback)); 1673 } 1674 FfiOHOSWebDownloadDelegateImplOnDownloadUpdated(int64_t id, void (*callback)(int64_t))1675 void FfiOHOSWebDownloadDelegateImplOnDownloadUpdated(int64_t id, void (*callback)(int64_t)) 1676 { 1677 auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id); 1678 if (!nativeWebDownloadDelegateImpl) { 1679 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 1680 return; 1681 } 1682 nativeWebDownloadDelegateImpl->PutDownloadDidUpdate(CJLambda::Create(callback)); 1683 } 1684 FfiOHOSWebDownloadDelegateImplOnDownloadFinish(int64_t id, void (*callback)(int64_t))1685 void FfiOHOSWebDownloadDelegateImplOnDownloadFinish(int64_t id, void (*callback)(int64_t)) 1686 { 1687 auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id); 1688 if (!nativeWebDownloadDelegateImpl) { 1689 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 1690 return; 1691 } 1692 nativeWebDownloadDelegateImpl->PutDownloadDidFinish(CJLambda::Create(callback)); 1693 } 1694 FfiOHOSWebDownloadDelegateImplOnDownloadFailed(int64_t id, void (*callback)(int64_t))1695 void FfiOHOSWebDownloadDelegateImplOnDownloadFailed(int64_t id, void (*callback)(int64_t)) 1696 { 1697 auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(id); 1698 if (!nativeWebDownloadDelegateImpl) { 1699 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 1700 return; 1701 } 1702 nativeWebDownloadDelegateImpl->PutDownloadDidFail(CJLambda::Create(callback)); 1703 } 1704 1705 // WebDownloadManagerImpl FfiOHOSWebDownloadManagerImplSetDownloadDelegate(int64_t delegateId)1706 void FfiOHOSWebDownloadManagerImplSetDownloadDelegate(int64_t delegateId) 1707 { 1708 auto nativeWebDownloadDelegateImpl = FFIData::GetData<WebDownloadDelegateImpl>(delegateId); 1709 if (!nativeWebDownloadDelegateImpl) { 1710 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 1711 return; 1712 } 1713 WebDownloadManagerImpl::SetDownloadDelegate(nativeWebDownloadDelegateImpl); 1714 } 1715 FfiOHOSWebDownloadManagerImplResumeDownload(int64_t itemId)1716 int32_t FfiOHOSWebDownloadManagerImplResumeDownload(int64_t itemId) 1717 { 1718 if (!WebDownloadManagerImpl::HasValidDelegate()) { 1719 return NWebError::NO_DOWNLOAD_DELEGATE_SET; 1720 } 1721 auto nativeWebDownloadItemImpl = FFIData::GetData<WebDownloadItemImpl>(itemId); 1722 if (!nativeWebDownloadItemImpl) { 1723 WEBVIEWLOGE("[DOWNLOAD] webDownloadDelegate is null"); 1724 return NWebError::INIT_ERROR; 1725 } 1726 WebDownloadManagerImpl::ResumeDownload(nativeWebDownloadItemImpl); 1727 return NWebError::NO_ERROR; 1728 } 1729 1730 // GeolocationPermissions FfiOHOSGeolocationAllowGeolocation(char* origin, bool incognito, int32_t *errCode)1731 void FfiOHOSGeolocationAllowGeolocation(char* origin, bool incognito, int32_t *errCode) 1732 { 1733 std::string originStr = std::string(origin); 1734 GeolocationPermission::CjAllowGeolocation(originStr, incognito, errCode); 1735 } 1736 FfiOHOSGeolocationDeleteGeolocation(char* origin, bool incognito, int32_t *errCode)1737 void FfiOHOSGeolocationDeleteGeolocation(char* origin, bool incognito, int32_t *errCode) 1738 { 1739 std::string originStr = std::string(origin); 1740 GeolocationPermission::CjDeleteGeolocation(originStr, incognito, errCode); 1741 } 1742 FfiOHOSGeolocationGetAccessibleGeolocation(char* origin, bool incognito, int32_t *errCode)1743 bool FfiOHOSGeolocationGetAccessibleGeolocation(char* origin, bool incognito, int32_t *errCode) 1744 { 1745 std::string originStr = std::string(origin); 1746 return GeolocationPermission::CjGetAccessibleGeolocation(originStr, incognito, errCode); 1747 } 1748 FfiOHOSGeolocationGetStoredGeolocation(bool incognito, int32_t *errCode)1749 CArrString FfiOHOSGeolocationGetStoredGeolocation(bool incognito, int32_t *errCode) 1750 { 1751 std::vector<std::string> origins = GeolocationPermission::CjGetStoredGeolocation(incognito, errCode); 1752 CArrString arrOrigins = {.head = nullptr, .size = 0}; 1753 if (errCode && *errCode != 0) { 1754 return arrOrigins; 1755 } 1756 arrOrigins.size = static_cast<int64_t>(origins.size()); 1757 arrOrigins.head = OHOS::Webview::VectorToCArrString(origins); 1758 return arrOrigins; 1759 } 1760 FfiOHOSGeolocationDeleteAllGeolocation(bool incognito, int32_t *errCode)1761 void FfiOHOSGeolocationDeleteAllGeolocation(bool incognito, int32_t *errCode) 1762 { 1763 return GeolocationPermission::CjDeleteAllGeolocation(incognito, errCode); 1764 } 1765 1766 // web_storage FfiWebStorageDeleteOrigin(char *corigin)1767 int32_t FfiWebStorageDeleteOrigin(char *corigin) 1768 { 1769 std::string origin(corigin); 1770 return OHOS::NWeb::WebStorage::CJdeleteOrigin(origin); 1771 } 1772 FfiWebStorageDeleteAllData(bool incognito)1773 void FfiWebStorageDeleteAllData(bool incognito) 1774 { 1775 OHOS::NWeb::WebStorage::CJdeleteAllData(incognito); 1776 } 1777 FfiWebStorageGetOriginQuota(char* corigin, int32_t *errCode)1778 int64_t FfiWebStorageGetOriginQuota(char* corigin, int32_t *errCode) 1779 { 1780 std::string origin(corigin); 1781 return OHOS::NWeb::WebStorage::CjGetOriginUsageOrQuota(origin, errCode, true); 1782 } 1783 FfiWebStorageGetOriginUsage(char* corigin, int32_t *errCode)1784 int64_t FfiWebStorageGetOriginUsage(char* corigin, int32_t *errCode) 1785 { 1786 std::string origin(corigin); 1787 return OHOS::NWeb::WebStorage::CjGetOriginUsageOrQuota(origin, errCode, false); 1788 } 1789 FfiWebStorageGetOrigins(int32_t *errCode)1790 CArrWebStorageOrigin FfiWebStorageGetOrigins(int32_t *errCode) 1791 { 1792 return OHOS::NWeb::WebStorage::CjGetOrigins(errCode); 1793 } 1794 } 1795 } 1796 }