1 /* 2 * Copyright (c) 2022 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 "napi_webview_controller.h" 17 18 #include <arpa/inet.h> 19 #include <cctype> 20 #include <climits> 21 #include <cstdint> 22 #include <regex> 23 #include <securec.h> 24 #include <unistd.h> 25 #include <uv.h> 26 27 #include "application_context.h" 28 #include "business_error.h" 29 #include "napi_parse_utils.h" 30 #include "native_engine/native_engine.h" 31 #include "nweb.h" 32 #include "nweb_adapter_helper.h" 33 #include "nweb_helper.h" 34 #include "nweb_init_params.h" 35 #include "nweb_log.h" 36 #include "ohos_adapter_helper.h" 37 #include "parameters.h" 38 #include "pixel_map.h" 39 #include "pixel_map_napi.h" 40 #include "web_errors.h" 41 #include "webview_javascript_execute_callback.h" 42 #include "webview_createpdf_execute_callback.h" 43 44 #include "web_download_delegate.h" 45 #include "web_download_manager.h" 46 #include "arkweb_scheme_handler.h" 47 #include "web_scheme_handler_request.h" 48 49 namespace OHOS { 50 namespace NWeb { 51 using namespace NWebError; 52 using NWebError::NO_ERROR; 53 54 namespace { 55 constexpr uint32_t URL_MAXIMUM = 2048; 56 constexpr uint32_t SOCKET_MAXIMUM = 6; 57 constexpr char URL_REGEXPR[] = "^http(s)?:\\/\\/.+"; 58 constexpr size_t MAX_RESOURCES_COUNT = 30; 59 constexpr size_t MAX_RESOURCE_SIZE = 10 * 1024 * 1024; 60 constexpr size_t MAX_URL_TRUST_LIST_STR_LEN = 10 * 1024 * 1024; // 10M 61 constexpr size_t BFCACHE_DEFAULT_SIZE = 1; 62 constexpr size_t BFCACHE_DEFAULT_TIMETOLIVE = 600; 63 constexpr double A4_WIDTH = 8.27; 64 constexpr double A4_HEIGHT = 11.69; 65 constexpr double SCALE_MIN = 0.1; 66 constexpr double SCALE_MAX = 2.0; 67 constexpr double HALF = 2.0; 68 constexpr double TEN_MILLIMETER_TO_INCH = 0.39; 69 using WebPrintWriteResultCallback = std::function<void(std::string, uint32_t)>; 70 ParsePrepareUrl(napi_env env,napi_value urlObj,std::string & url)71 bool ParsePrepareUrl(napi_env env, napi_value urlObj, std::string& url) 72 { 73 napi_valuetype valueType = napi_null; 74 napi_typeof(env, urlObj, &valueType); 75 76 if (valueType == napi_string) { 77 NapiParseUtils::ParseString(env, urlObj, url); 78 if (url.size() > URL_MAXIMUM) { 79 WVLOG_E("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM); 80 return false; 81 } 82 83 if (!regex_match(url, std::regex(URL_REGEXPR, std::regex_constants::icase))) { 84 WVLOG_E("ParsePrepareUrl error"); 85 return false; 86 } 87 88 return true; 89 } 90 91 WVLOG_E("Unable to parse type from url object."); 92 return false; 93 } 94 ParseIP(napi_env env,napi_value urlObj,std::string & ip)95 bool ParseIP(napi_env env, napi_value urlObj, std::string& ip) 96 { 97 napi_valuetype valueType = napi_null; 98 napi_typeof(env, urlObj, &valueType); 99 100 if (valueType == napi_string) { 101 NapiParseUtils::ParseString(env, urlObj, ip); 102 if (ip == "") { 103 WVLOG_E("The IP is null"); 104 return false; 105 } 106 107 unsigned char buf[sizeof(struct in6_addr)]; 108 if ((inet_pton(AF_INET, ip.c_str(), buf) == 1) || (inet_pton(AF_INET6, ip.c_str(), buf) == 1)) { 109 return true; 110 } 111 WVLOG_E("IP error."); 112 return false; 113 } 114 115 WVLOG_E("Unable to parse type from ip object."); 116 return false; 117 } 118 GetArrayValueType(napi_env env,napi_value array,bool & isDouble)119 napi_valuetype GetArrayValueType(napi_env env, napi_value array, bool& isDouble) 120 { 121 uint32_t arrayLength = 0; 122 napi_get_array_length(env, array, &arrayLength); 123 napi_valuetype valueTypeFirst = napi_undefined; 124 napi_valuetype valueTypeCur = napi_undefined; 125 for (uint32_t i = 0; i < arrayLength; ++i) { 126 napi_value obj = nullptr; 127 napi_get_element(env, array, i, &obj); 128 napi_typeof(env, obj, &valueTypeCur); 129 if (i == 0) { 130 valueTypeFirst = valueTypeCur; 131 } 132 if (valueTypeCur != napi_string && valueTypeCur != napi_number && valueTypeCur != napi_boolean) { 133 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 134 return napi_undefined; 135 } 136 if (valueTypeCur != valueTypeFirst) { 137 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 138 return napi_undefined; 139 } 140 if (valueTypeFirst == napi_number) { 141 int32_t elementInt32 = 0; 142 double elementDouble = 0.0; 143 bool isReadValue32 = napi_get_value_int32(env, obj, &elementInt32) == napi_ok; 144 bool isReadDouble = napi_get_value_double(env, obj, &elementDouble) == napi_ok; 145 constexpr double MINIMAL_ERROR = 0.000001; 146 if (isReadValue32 && isReadDouble) { 147 isDouble = abs(elementDouble - elementInt32 * 1.0) > MINIMAL_ERROR; 148 } else if (isReadDouble) { 149 isDouble = true; 150 } 151 } 152 } 153 return valueTypeFirst; 154 } 155 SetArrayHandlerBoolean(napi_env env,napi_value array,WebMessageExt * webMessageExt)156 void SetArrayHandlerBoolean(napi_env env, napi_value array, WebMessageExt* webMessageExt) 157 { 158 std::vector<bool> outValue; 159 if (!NapiParseUtils::ParseBooleanArray(env, array, outValue)) { 160 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 161 return; 162 } 163 webMessageExt->SetBooleanArray(outValue); 164 } 165 SetArrayHandlerString(napi_env env,napi_value array,WebMessageExt * webMessageExt)166 void SetArrayHandlerString(napi_env env, napi_value array, WebMessageExt* webMessageExt) 167 { 168 std::vector<std::string> outValue; 169 if (!NapiParseUtils::ParseStringArray(env, array, outValue)) { 170 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 171 return; 172 } 173 webMessageExt->SetStringArray(outValue); 174 } 175 SetArrayHandlerInteger(napi_env env,napi_value array,WebMessageExt * webMessageExt)176 void SetArrayHandlerInteger(napi_env env, napi_value array, WebMessageExt* webMessageExt) 177 { 178 std::vector<int64_t> outValue; 179 if (!NapiParseUtils::ParseInt64Array(env, array, outValue)) { 180 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 181 return; 182 } 183 webMessageExt->SetInt64Array(outValue); 184 } 185 SetArrayHandlerDouble(napi_env env,napi_value array,WebMessageExt * webMessageExt)186 void SetArrayHandlerDouble(napi_env env, napi_value array, WebMessageExt* webMessageExt) 187 { 188 std::vector<double> outValue; 189 if (!NapiParseUtils::ParseDoubleArray(env, array, outValue)) { 190 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 191 return; 192 } 193 webMessageExt->SetDoubleArray(outValue); 194 } 195 GetWebviewController(napi_env env,napi_callback_info info)196 WebviewController* GetWebviewController(napi_env env, napi_callback_info info) 197 { 198 napi_value thisVar = nullptr; 199 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 200 201 WebviewController *webviewController = nullptr; 202 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 203 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 204 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 205 return nullptr; 206 } 207 return webviewController; 208 } 209 ParsePrepareRequestMethod(napi_env env,napi_value methodObj,std::string & method)210 bool ParsePrepareRequestMethod(napi_env env, napi_value methodObj, std::string& method) 211 { 212 napi_valuetype valueType = napi_null; 213 napi_typeof(env, methodObj, &valueType); 214 215 if (valueType == napi_string) { 216 NapiParseUtils::ParseString(env, methodObj, method); 217 if (method != "POST") { 218 WVLOG_E("The method %{public}s is not supported.", method.c_str()); 219 return false; 220 } 221 return true; 222 } 223 224 WVLOG_E("Unable to parse type from method object."); 225 return false; 226 } 227 ParseHttpHeaders(napi_env env,napi_value headersArray,std::map<std::string,std::string> * headers)228 bool ParseHttpHeaders(napi_env env, napi_value headersArray, std::map<std::string, std::string>* headers) 229 { 230 bool isArray = false; 231 napi_is_array(env, headersArray, &isArray); 232 if (isArray) { 233 uint32_t arrayLength = INTEGER_ZERO; 234 napi_get_array_length(env, headersArray, &arrayLength); 235 for (uint32_t i = 0; i < arrayLength; ++i) { 236 std::string key; 237 std::string value; 238 napi_value obj = nullptr; 239 napi_value keyObj = nullptr; 240 napi_value valueObj = nullptr; 241 napi_get_element(env, headersArray, i, &obj); 242 if (napi_get_named_property(env, obj, "headerKey", &keyObj) != napi_ok) { 243 continue; 244 } 245 if (napi_get_named_property(env, obj, "headerValue", &valueObj) != napi_ok) { 246 continue; 247 } 248 if (!NapiParseUtils::ParseString(env, keyObj, key) || !NapiParseUtils::ParseString(env, valueObj, value)) { 249 WVLOG_E("Unable to parse string from headers array object."); 250 return false; 251 } 252 if (key.empty()) { 253 WVLOG_E("Key from headers is empty."); 254 return false; 255 } 256 (*headers)[key] = value; 257 } 258 } else { 259 WVLOG_E("Unable to parse type from headers array object."); 260 return false; 261 } 262 return true; 263 } 264 CheckCacheKey(napi_env env,const std::string & cacheKey)265 bool CheckCacheKey(napi_env env, const std::string& cacheKey) 266 { 267 for (char c : cacheKey) { 268 if (!isalnum(c)) { 269 WVLOG_E("BusinessError: 401. The character of 'cacheKey' must be number or letters."); 270 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 271 return false; 272 } 273 } 274 return true; 275 } 276 ParseCacheKeyList(napi_env env,napi_value cacheKeyArray,std::vector<std::string> * cacheKeyList)277 bool ParseCacheKeyList(napi_env env, napi_value cacheKeyArray, std::vector<std::string>* cacheKeyList) 278 { 279 bool isArray = false; 280 napi_is_array(env, cacheKeyArray, &isArray); 281 if (!isArray) { 282 WVLOG_E("Unable to parse type from CacheKey array object."); 283 return false; 284 } 285 uint32_t arrayLength = INTEGER_ZERO; 286 napi_get_array_length(env, cacheKeyArray, &arrayLength); 287 if (arrayLength == 0) { 288 WVLOG_E("cacheKey array length is invalid"); 289 return false; 290 } 291 for (uint32_t i = 0; i < arrayLength; ++i) { 292 napi_value cacheKeyItem = nullptr; 293 napi_get_element(env, cacheKeyArray, i, &cacheKeyItem); 294 std::string cacheKeyStr; 295 if (!NapiParseUtils::ParseString(env, cacheKeyItem, cacheKeyStr)) { 296 WVLOG_E("Unable to parse string from cacheKey array object."); 297 return false; 298 } 299 if (cacheKeyStr.empty()) { 300 WVLOG_E("Cache Key is empty."); 301 return false; 302 } 303 for (char c : cacheKeyStr) { 304 if (!isalnum(c)) { 305 WVLOG_E("Cache Key is invalid."); 306 return false; 307 } 308 } 309 cacheKeyList->emplace_back(cacheKeyStr); 310 } 311 return true; 312 } 313 ParsePrefetchArgs(napi_env env,napi_value preArgs)314 std::shared_ptr<NWebEnginePrefetchArgs> ParsePrefetchArgs(napi_env env, napi_value preArgs) 315 { 316 napi_value urlObj = nullptr; 317 std::string url; 318 napi_get_named_property(env, preArgs, "url", &urlObj); 319 if (!ParsePrepareUrl(env, urlObj, url)) { 320 BusinessError::ThrowErrorByErrcode(env, INVALID_URL); 321 return nullptr; 322 } 323 324 napi_value methodObj = nullptr; 325 std::string method; 326 napi_get_named_property(env, preArgs, "method", &methodObj); 327 if (!ParsePrepareRequestMethod(env, methodObj, method)) { 328 WVLOG_E("BusinessError: 401. The type of 'method' must be string 'POST'."); 329 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 330 return nullptr; 331 } 332 333 napi_value formDataObj = nullptr; 334 std::string formData; 335 napi_get_named_property(env, preArgs, "formData", &formDataObj); 336 if (!NapiParseUtils::ParseString(env, formDataObj, formData)) { 337 WVLOG_E("BusinessError: 401. The type of 'formData' must be string."); 338 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 339 return nullptr; 340 } 341 std::shared_ptr<NWebEnginePrefetchArgs> prefetchArgs = std::make_shared<NWebEnginePrefetchArgsImpl>( 342 url, method, formData); 343 return prefetchArgs; 344 } 345 ParsePDFMarginConfigArgs(napi_env env,napi_value preArgs,double width,double height)346 PDFMarginConfig ParsePDFMarginConfigArgs(napi_env env, napi_value preArgs, double width, double height) 347 { 348 napi_value marginTopObj = nullptr; 349 double marginTop = TEN_MILLIMETER_TO_INCH; 350 napi_get_named_property(env, preArgs, "marginTop", &marginTopObj); 351 if (!NapiParseUtils::ParseDouble(env, marginTopObj, marginTop)) { 352 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 353 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "marginTop", "number")); 354 return PDFMarginConfig(); 355 } 356 marginTop = (marginTop >= height / HALF || marginTop <= 0.0) ? 0.0 : marginTop; 357 358 napi_value marginBottomObj = nullptr; 359 double marginBottom = TEN_MILLIMETER_TO_INCH; 360 napi_get_named_property(env, preArgs, "marginBottom", &marginBottomObj); 361 if (!NapiParseUtils::ParseDouble(env, marginBottomObj, marginBottom)) { 362 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 363 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "marginBottom", "number")); 364 return PDFMarginConfig(); 365 } 366 marginBottom = (marginBottom >= height / HALF || marginBottom <= 0.0) ? 0.0 : marginBottom; 367 368 napi_value marginRightObj = nullptr; 369 double marginRight = TEN_MILLIMETER_TO_INCH; 370 napi_get_named_property(env, preArgs, "marginRight", &marginRightObj); 371 if (!NapiParseUtils::ParseDouble(env, marginRightObj, marginRight)) { 372 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 373 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "marginRight", "number")); 374 return PDFMarginConfig(); 375 } 376 marginRight = (marginRight >= width / HALF || marginRight <= 0.0) ? 0.0 : marginRight; 377 378 napi_value marginLeftObj = nullptr; 379 double marginLeft = TEN_MILLIMETER_TO_INCH; 380 napi_get_named_property(env, preArgs, "marginLeft", &marginLeftObj); 381 if (!NapiParseUtils::ParseDouble(env, marginLeftObj, marginLeft)) { 382 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 383 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "marginLeft", "number")); 384 return PDFMarginConfig(); 385 } 386 marginLeft = (marginLeft >= width / HALF || marginLeft <= 0.0) ? 0.0 : marginLeft; 387 388 return { marginTop, marginBottom, marginRight, marginLeft }; 389 } 390 ParsePDFConfigArgs(napi_env env,napi_value preArgs)391 std::shared_ptr<NWebPDFConfigArgs> ParsePDFConfigArgs(napi_env env, napi_value preArgs) 392 { 393 napi_value widthObj = nullptr; 394 double width = A4_WIDTH; 395 napi_get_named_property(env, preArgs, "width", &widthObj); 396 if (!NapiParseUtils::ParseDouble(env, widthObj, width)) { 397 BusinessError::ThrowErrorByErrcode( 398 env, PARAM_CHECK_ERROR, NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "width", "number")); 399 return nullptr; 400 } 401 402 napi_value heightObj = nullptr; 403 double height = A4_HEIGHT; 404 napi_get_named_property(env, preArgs, "height", &heightObj); 405 if (!NapiParseUtils::ParseDouble(env, heightObj, height)) { 406 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 407 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "height", "number")); 408 return nullptr; 409 } 410 411 napi_value scaleObj = nullptr; 412 double scale = 1.0; 413 napi_get_named_property(env, preArgs, "scale", &scaleObj); 414 NapiParseUtils::ParseDouble(env, scaleObj, scale); 415 scale = scale > SCALE_MAX ? SCALE_MAX : scale < SCALE_MIN ? SCALE_MIN : scale; 416 417 auto margin = ParsePDFMarginConfigArgs(env, preArgs, width, height); 418 419 napi_value shouldPrintBackgroundObj = nullptr; 420 bool shouldPrintBackground = false; 421 napi_get_named_property(env, preArgs, "shouldPrintBackground", &shouldPrintBackgroundObj); 422 NapiParseUtils::ParseBoolean(env, shouldPrintBackgroundObj, shouldPrintBackground); 423 424 std::shared_ptr<NWebPDFConfigArgs> pdfConfig = std::make_shared<NWebPDFConfigArgsImpl>( 425 width, height, scale, margin.top, margin.bottom, margin.right, margin.left, shouldPrintBackground); 426 return pdfConfig; 427 } 428 JsErrorCallback(napi_env env,napi_ref jsCallback,int32_t err)429 void JsErrorCallback(napi_env env, napi_ref jsCallback, int32_t err) 430 { 431 napi_value jsError = nullptr; 432 napi_value jsResult = nullptr; 433 434 jsError = BusinessError::CreateError(env, err); 435 napi_get_undefined(env, &jsResult); 436 napi_value args[INTEGER_TWO] = {jsError, jsResult}; 437 438 napi_value callback = nullptr; 439 napi_value callbackResult = nullptr; 440 napi_get_reference_value(env, jsCallback, &callback); 441 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult); 442 napi_delete_reference(env, jsCallback); 443 } 444 ParseRegisterJavaScriptProxyParam(napi_env env,size_t argc,napi_value * argv,RegisterJavaScriptProxyParam * param)445 bool ParseRegisterJavaScriptProxyParam(napi_env env, size_t argc, napi_value* argv, 446 RegisterJavaScriptProxyParam* param) 447 { 448 std::string objName; 449 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], objName)) { 450 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 451 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "name", "string")); 452 return false; 453 } 454 std::vector<std::string> methodList; 455 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_TWO], methodList)) { 456 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 457 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "methodList", "array")); 458 return false; 459 } 460 std::vector<std::string> asyncMethodList; 461 if (argc == INTEGER_FOUR && !NapiParseUtils::ParseStringArray(env, argv[INTEGER_THREE], asyncMethodList)) { 462 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 463 return false; 464 } 465 std::string permission; 466 if (argc == INTEGER_FIVE && !NapiParseUtils::ParseString(env, argv[INTEGER_FOUR], permission)) { 467 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 468 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "permission", "string")); 469 return false; 470 } 471 param->env = env; 472 param->obj = argv[INTEGER_ZERO]; 473 param->objName = objName; 474 param->syncMethodList = methodList; 475 param->asyncMethodList = asyncMethodList; 476 param->permission = permission; 477 return true; 478 } 479 RemoveDownloadDelegateRef(napi_env env,napi_value thisVar)480 napi_value RemoveDownloadDelegateRef(napi_env env, napi_value thisVar) 481 { 482 WebviewController *webviewController = nullptr; 483 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 484 if (webviewController == nullptr || !webviewController->IsInit()) { 485 WVLOG_E("create message port failed, napi unwrap webviewController failed"); 486 return nullptr; 487 } 488 489 WebDownloadManager::RemoveDownloadDelegateRef(webviewController->GetWebId()); 490 return nullptr; 491 } 492 } // namespace 493 494 int32_t NapiWebviewController::maxFdNum_ = -1; 495 std::atomic<int32_t> NapiWebviewController::usedFd_ {0}; 496 std::atomic<bool> g_inWebPageSnapshot {false}; 497 498 thread_local napi_ref g_classWebMsgPort; 499 thread_local napi_ref g_historyListRef; 500 thread_local napi_ref g_webMsgExtClassRef; 501 thread_local napi_ref g_webPrintDocClassRef; Init(napi_env env,napi_value exports)502 napi_value NapiWebviewController::Init(napi_env env, napi_value exports) 503 { 504 napi_property_descriptor properties[] = { 505 DECLARE_NAPI_STATIC_FUNCTION("initializeWebEngine", NapiWebviewController::InitializeWebEngine), 506 DECLARE_NAPI_STATIC_FUNCTION("setHttpDns", NapiWebviewController::SetHttpDns), 507 DECLARE_NAPI_STATIC_FUNCTION("setWebDebuggingAccess", NapiWebviewController::SetWebDebuggingAccess), 508 DECLARE_NAPI_STATIC_FUNCTION("setServiceWorkerWebSchemeHandler", 509 NapiWebviewController::SetServiceWorkerWebSchemeHandler), 510 DECLARE_NAPI_STATIC_FUNCTION("clearServiceWorkerWebSchemeHandler", 511 NapiWebviewController::ClearServiceWorkerWebSchemeHandler), 512 DECLARE_NAPI_FUNCTION("getWebDebuggingAccess", NapiWebviewController::InnerGetWebDebuggingAccess), 513 DECLARE_NAPI_FUNCTION("setWebId", NapiWebviewController::SetWebId), 514 DECLARE_NAPI_FUNCTION("jsProxy", NapiWebviewController::InnerJsProxy), 515 DECLARE_NAPI_FUNCTION("getCustomeSchemeCmdLine", NapiWebviewController::InnerGetCustomeSchemeCmdLine), 516 DECLARE_NAPI_FUNCTION("accessForward", NapiWebviewController::AccessForward), 517 DECLARE_NAPI_FUNCTION("accessBackward", NapiWebviewController::AccessBackward), 518 DECLARE_NAPI_FUNCTION("accessStep", NapiWebviewController::AccessStep), 519 DECLARE_NAPI_FUNCTION("clearHistory", NapiWebviewController::ClearHistory), 520 DECLARE_NAPI_FUNCTION("forward", NapiWebviewController::Forward), 521 DECLARE_NAPI_FUNCTION("backward", NapiWebviewController::Backward), 522 DECLARE_NAPI_FUNCTION("onActive", NapiWebviewController::OnActive), 523 DECLARE_NAPI_FUNCTION("onInactive", NapiWebviewController::OnInactive), 524 DECLARE_NAPI_FUNCTION("refresh", NapiWebviewController::Refresh), 525 DECLARE_NAPI_FUNCTION("zoomIn", NapiWebviewController::ZoomIn), 526 DECLARE_NAPI_FUNCTION("zoomOut", NapiWebviewController::ZoomOut), 527 DECLARE_NAPI_FUNCTION("getWebId", NapiWebviewController::GetWebId), 528 DECLARE_NAPI_FUNCTION("getUserAgent", NapiWebviewController::GetUserAgent), 529 DECLARE_NAPI_FUNCTION("getCustomUserAgent", NapiWebviewController::GetCustomUserAgent), 530 DECLARE_NAPI_FUNCTION("setCustomUserAgent", NapiWebviewController::SetCustomUserAgent), 531 DECLARE_NAPI_FUNCTION("getTitle", NapiWebviewController::GetTitle), 532 DECLARE_NAPI_FUNCTION("getPageHeight", NapiWebviewController::GetPageHeight), 533 DECLARE_NAPI_FUNCTION("backOrForward", NapiWebviewController::BackOrForward), 534 DECLARE_NAPI_FUNCTION("storeWebArchive", NapiWebviewController::StoreWebArchive), 535 DECLARE_NAPI_FUNCTION("createWebMessagePorts", NapiWebviewController::CreateWebMessagePorts), 536 DECLARE_NAPI_FUNCTION("postMessage", NapiWebviewController::PostMessage), 537 DECLARE_NAPI_FUNCTION("getHitTestValue", NapiWebviewController::GetHitTestValue), 538 DECLARE_NAPI_FUNCTION("requestFocus", NapiWebviewController::RequestFocus), 539 DECLARE_NAPI_FUNCTION("loadUrl", NapiWebviewController::LoadUrl), 540 DECLARE_NAPI_FUNCTION("postUrl", NapiWebviewController::PostUrl), 541 DECLARE_NAPI_FUNCTION("loadData", NapiWebviewController::LoadData), 542 DECLARE_NAPI_FUNCTION("getHitTest", NapiWebviewController::GetHitTest), 543 DECLARE_NAPI_FUNCTION("clearMatches", NapiWebviewController::ClearMatches), 544 DECLARE_NAPI_FUNCTION("searchNext", NapiWebviewController::SearchNext), 545 DECLARE_NAPI_FUNCTION("searchAllAsync", NapiWebviewController::SearchAllAsync), 546 DECLARE_NAPI_FUNCTION("clearSslCache", NapiWebviewController::ClearSslCache), 547 DECLARE_NAPI_FUNCTION("clearClientAuthenticationCache", NapiWebviewController::ClearClientAuthenticationCache), 548 DECLARE_NAPI_FUNCTION("stop", NapiWebviewController::Stop), 549 DECLARE_NAPI_FUNCTION("zoom", NapiWebviewController::Zoom), 550 DECLARE_NAPI_FUNCTION("registerJavaScriptProxy", NapiWebviewController::RegisterJavaScriptProxy), 551 DECLARE_NAPI_FUNCTION("innerCompleteWindowNew", NapiWebviewController::InnerCompleteWindowNew), 552 DECLARE_NAPI_FUNCTION("deleteJavaScriptRegister", NapiWebviewController::DeleteJavaScriptRegister), 553 DECLARE_NAPI_FUNCTION("runJavaScript", NapiWebviewController::RunJavaScript), 554 DECLARE_NAPI_FUNCTION("runJavaScriptExt", NapiWebviewController::RunJavaScriptExt), 555 DECLARE_NAPI_FUNCTION("createPdf", NapiWebviewController::RunCreatePDFExt), 556 DECLARE_NAPI_FUNCTION("getUrl", NapiWebviewController::GetUrl), 557 DECLARE_NAPI_FUNCTION("terminateRenderProcess", NapiWebviewController::TerminateRenderProcess), 558 DECLARE_NAPI_FUNCTION("getOriginalUrl", NapiWebviewController::GetOriginalUrl), 559 DECLARE_NAPI_FUNCTION("setNetworkAvailable", NapiWebviewController::SetNetworkAvailable), 560 DECLARE_NAPI_FUNCTION("innerGetWebId", NapiWebviewController::InnerGetWebId), 561 DECLARE_NAPI_FUNCTION("hasImage", NapiWebviewController::HasImage), 562 DECLARE_NAPI_FUNCTION("removeCache", NapiWebviewController::RemoveCache), 563 DECLARE_NAPI_FUNCTION("getFavicon", NapiWebviewController::GetFavicon), 564 DECLARE_NAPI_FUNCTION("getBackForwardEntries", NapiWebviewController::getBackForwardEntries), 565 DECLARE_NAPI_FUNCTION("serializeWebState", NapiWebviewController::SerializeWebState), 566 DECLARE_NAPI_FUNCTION("restoreWebState", NapiWebviewController::RestoreWebState), 567 DECLARE_NAPI_FUNCTION("pageDown", NapiWebviewController::ScrollPageDown), 568 DECLARE_NAPI_FUNCTION("pageUp", NapiWebviewController::ScrollPageUp), 569 DECLARE_NAPI_FUNCTION("scrollTo", NapiWebviewController::ScrollTo), 570 DECLARE_NAPI_FUNCTION("scrollBy", NapiWebviewController::ScrollBy), 571 DECLARE_NAPI_FUNCTION("slideScroll", NapiWebviewController::SlideScroll), 572 DECLARE_NAPI_FUNCTION("setScrollable", NapiWebviewController::SetScrollable), 573 DECLARE_NAPI_FUNCTION("getScrollable", NapiWebviewController::GetScrollable), 574 DECLARE_NAPI_STATIC_FUNCTION("customizeSchemes", NapiWebviewController::CustomizeSchemes), 575 DECLARE_NAPI_FUNCTION("innerSetHapPath", NapiWebviewController::InnerSetHapPath), 576 DECLARE_NAPI_FUNCTION("innerGetCertificate", NapiWebviewController::InnerGetCertificate), 577 DECLARE_NAPI_FUNCTION("setAudioMuted", NapiWebviewController::SetAudioMuted), 578 DECLARE_NAPI_FUNCTION("innerGetThisVar", NapiWebviewController::InnerGetThisVar), 579 DECLARE_NAPI_FUNCTION("prefetchPage", NapiWebviewController::PrefetchPage), 580 DECLARE_NAPI_FUNCTION("setDownloadDelegate", NapiWebviewController::SetDownloadDelegate), 581 DECLARE_NAPI_FUNCTION("startDownload", NapiWebviewController::StartDownload), 582 DECLARE_NAPI_STATIC_FUNCTION("prepareForPageLoad", NapiWebviewController::PrepareForPageLoad), 583 DECLARE_NAPI_FUNCTION("createWebPrintDocumentAdapter", NapiWebviewController::CreateWebPrintDocumentAdapter), 584 DECLARE_NAPI_STATIC_FUNCTION("setConnectionTimeout", NapiWebviewController::SetConnectionTimeout), 585 DECLARE_NAPI_FUNCTION("enableSafeBrowsing", NapiWebviewController::EnableSafeBrowsing), 586 DECLARE_NAPI_FUNCTION("isSafeBrowsingEnabled", NapiWebviewController::IsSafeBrowsingEnabled), 587 DECLARE_NAPI_FUNCTION("getSecurityLevel", NapiWebviewController::GetSecurityLevel), 588 DECLARE_NAPI_FUNCTION("isIncognitoMode", NapiWebviewController::IsIncognitoMode), 589 DECLARE_NAPI_FUNCTION("setPrintBackground", NapiWebviewController::SetPrintBackground), 590 DECLARE_NAPI_FUNCTION("getPrintBackground", NapiWebviewController::GetPrintBackground), 591 DECLARE_NAPI_FUNCTION("setWebSchemeHandler", NapiWebviewController::SetWebSchemeHandler), 592 DECLARE_NAPI_FUNCTION("clearWebSchemeHandler", NapiWebviewController::ClearWebSchemeHandler), 593 DECLARE_NAPI_FUNCTION("enableIntelligentTrackingPrevention", 594 NapiWebviewController::EnableIntelligentTrackingPrevention), 595 DECLARE_NAPI_FUNCTION("isIntelligentTrackingPreventionEnabled", 596 NapiWebviewController::IsIntelligentTrackingPreventionEnabled), 597 DECLARE_NAPI_STATIC_FUNCTION("addIntelligentTrackingPreventionBypassingList", 598 NapiWebviewController::AddIntelligentTrackingPreventionBypassingList), 599 DECLARE_NAPI_STATIC_FUNCTION("removeIntelligentTrackingPreventionBypassingList", 600 NapiWebviewController::RemoveIntelligentTrackingPreventionBypassingList), 601 DECLARE_NAPI_STATIC_FUNCTION("clearIntelligentTrackingPreventionBypassingList", 602 NapiWebviewController::ClearIntelligentTrackingPreventionBypassingList), 603 DECLARE_NAPI_FUNCTION("getLastJavascriptProxyCallingFrameUrl", 604 NapiWebviewController::GetLastJavascriptProxyCallingFrameUrl), 605 DECLARE_NAPI_STATIC_FUNCTION("getDefaultUserAgent", NapiWebviewController::GetDefaultUserAgent), 606 DECLARE_NAPI_STATIC_FUNCTION("pauseAllTimers", NapiWebviewController::PauseAllTimers), 607 DECLARE_NAPI_STATIC_FUNCTION("resumeAllTimers", NapiWebviewController::ResumeAllTimers), 608 DECLARE_NAPI_FUNCTION("startCamera", NapiWebviewController::StartCamera), 609 DECLARE_NAPI_FUNCTION("stopCamera", NapiWebviewController::StopCamera), 610 DECLARE_NAPI_FUNCTION("closeCamera", NapiWebviewController::CloseCamera), 611 DECLARE_NAPI_FUNCTION("closeAllMediaPresentations", NapiWebviewController::CloseAllMediaPresentations), 612 DECLARE_NAPI_FUNCTION("stopAllMedia", NapiWebviewController::StopAllMedia), 613 DECLARE_NAPI_FUNCTION("resumeAllMedia", NapiWebviewController::ResumeAllMedia), 614 DECLARE_NAPI_FUNCTION("pauseAllMedia", NapiWebviewController::PauseAllMedia), 615 DECLARE_NAPI_FUNCTION("getMediaPlaybackState", NapiWebviewController::GetMediaPlaybackState), 616 DECLARE_NAPI_FUNCTION("onCreateNativeMediaPlayer", NapiWebviewController::OnCreateNativeMediaPlayer), 617 DECLARE_NAPI_STATIC_FUNCTION("prefetchResource", NapiWebviewController::PrefetchResource), 618 DECLARE_NAPI_STATIC_FUNCTION("clearPrefetchedResource", NapiWebviewController::ClearPrefetchedResource), 619 DECLARE_NAPI_STATIC_FUNCTION("setRenderProcessMode", NapiWebviewController::SetRenderProcessMode), 620 DECLARE_NAPI_STATIC_FUNCTION("getRenderProcessMode", NapiWebviewController::GetRenderProcessMode), 621 DECLARE_NAPI_FUNCTION("precompileJavaScript", NapiWebviewController::PrecompileJavaScript), 622 DECLARE_NAPI_FUNCTION("injectOfflineResources", NapiWebviewController::InjectOfflineResources), 623 DECLARE_NAPI_STATIC_FUNCTION("setHostIP", NapiWebviewController::SetHostIP), 624 DECLARE_NAPI_STATIC_FUNCTION("clearHostIP", NapiWebviewController::ClearHostIP), 625 DECLARE_NAPI_STATIC_FUNCTION("warmupServiceWorker", NapiWebviewController::WarmupServiceWorker), 626 DECLARE_NAPI_FUNCTION("getSurfaceId", NapiWebviewController::GetSurfaceId), 627 DECLARE_NAPI_STATIC_FUNCTION("enableWholeWebPageDrawing", NapiWebviewController::EnableWholeWebPageDrawing), 628 DECLARE_NAPI_FUNCTION("enableAdsBlock", NapiWebviewController::EnableAdsBlock), 629 DECLARE_NAPI_FUNCTION("isAdsBlockEnabled", NapiWebviewController::IsAdsBlockEnabled), 630 DECLARE_NAPI_FUNCTION("isAdsBlockEnabledForCurPage", NapiWebviewController::IsAdsBlockEnabledForCurPage), 631 DECLARE_NAPI_FUNCTION("webPageSnapshot", NapiWebviewController::WebPageSnapshot), 632 DECLARE_NAPI_FUNCTION("setUrlTrustList", NapiWebviewController::SetUrlTrustList), 633 DECLARE_NAPI_FUNCTION("setPathAllowingUniversalAccess", 634 NapiWebviewController::SetPathAllowingUniversalAccess), 635 DECLARE_NAPI_STATIC_FUNCTION("enableBackForwardCache", NapiWebviewController::EnableBackForwardCache), 636 DECLARE_NAPI_FUNCTION("setBackForwardCacheOptions", NapiWebviewController::SetBackForwardCacheOptions), 637 DECLARE_NAPI_FUNCTION("scrollByWithResult", NapiWebviewController::ScrollByWithResult), 638 DECLARE_NAPI_FUNCTION("updateInstanceId", NapiWebviewController::UpdateInstanceId), 639 DECLARE_NAPI_FUNCTION("getScrollOffset", 640 NapiWebviewController::GetScrollOffset), 641 DECLARE_NAPI_STATIC_FUNCTION("trimMemoryByPressureLevel", 642 NapiWebviewController::TrimMemoryByPressureLevel), 643 }; 644 napi_value constructor = nullptr; 645 napi_define_class(env, WEBVIEW_CONTROLLER_CLASS_NAME.c_str(), WEBVIEW_CONTROLLER_CLASS_NAME.length(), 646 NapiWebviewController::JsConstructor, nullptr, sizeof(properties) / sizeof(properties[0]), 647 properties, &constructor); 648 NAPI_ASSERT(env, constructor != nullptr, "define js class WebviewController failed"); 649 napi_status status = napi_set_named_property(env, exports, "WebviewController", constructor); 650 NAPI_ASSERT(env, status == napi_ok, "set property WebviewController failed"); 651 652 napi_value webMsgTypeEnum = nullptr; 653 napi_property_descriptor webMsgTypeProperties[] = { 654 DECLARE_NAPI_STATIC_PROPERTY("NOT_SUPPORT", NapiParseUtils::ToInt32Value(env, 655 static_cast<int32_t>(WebMessageType::NOTSUPPORT))), 656 DECLARE_NAPI_STATIC_PROPERTY("STRING", NapiParseUtils::ToInt32Value(env, 657 static_cast<int32_t>(WebMessageType::STRING))), 658 DECLARE_NAPI_STATIC_PROPERTY("NUMBER", NapiParseUtils::ToInt32Value(env, 659 static_cast<int32_t>(WebMessageType::NUMBER))), 660 DECLARE_NAPI_STATIC_PROPERTY("BOOLEAN", NapiParseUtils::ToInt32Value(env, 661 static_cast<int32_t>(WebMessageType::BOOLEAN))), 662 DECLARE_NAPI_STATIC_PROPERTY("ARRAY_BUFFER", NapiParseUtils::ToInt32Value(env, 663 static_cast<int32_t>(WebMessageType::ARRAYBUFFER))), 664 DECLARE_NAPI_STATIC_PROPERTY("ARRAY", NapiParseUtils::ToInt32Value(env, 665 static_cast<int32_t>(WebMessageType::ARRAY))), 666 DECLARE_NAPI_STATIC_PROPERTY("ERROR", NapiParseUtils::ToInt32Value(env, 667 static_cast<int32_t>(WebMessageType::ERROR))) 668 }; 669 napi_define_class(env, WEB_PORT_MSG_ENUM_NAME.c_str(), WEB_PORT_MSG_ENUM_NAME.length(), 670 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(webMsgTypeProperties) / 671 sizeof(webMsgTypeProperties[0]), webMsgTypeProperties, &webMsgTypeEnum); 672 napi_set_named_property(env, exports, WEB_PORT_MSG_ENUM_NAME.c_str(), webMsgTypeEnum); 673 674 napi_value webMsgExtClass = nullptr; 675 napi_property_descriptor webMsgExtClsProperties[] = { 676 DECLARE_NAPI_FUNCTION("getType", NapiWebMessageExt::GetType), 677 DECLARE_NAPI_FUNCTION("getString", NapiWebMessageExt::GetString), 678 DECLARE_NAPI_FUNCTION("getNumber", NapiWebMessageExt::GetNumber), 679 DECLARE_NAPI_FUNCTION("getBoolean", NapiWebMessageExt::GetBoolean), 680 DECLARE_NAPI_FUNCTION("getArrayBuffer", NapiWebMessageExt::GetArrayBuffer), 681 DECLARE_NAPI_FUNCTION("getArray", NapiWebMessageExt::GetArray), 682 DECLARE_NAPI_FUNCTION("getError", NapiWebMessageExt::GetError), 683 DECLARE_NAPI_FUNCTION("setType", NapiWebMessageExt::SetType), 684 DECLARE_NAPI_FUNCTION("setString", NapiWebMessageExt::SetString), 685 DECLARE_NAPI_FUNCTION("setNumber", NapiWebMessageExt::SetNumber), 686 DECLARE_NAPI_FUNCTION("setBoolean", NapiWebMessageExt::SetBoolean), 687 DECLARE_NAPI_FUNCTION("setArrayBuffer", NapiWebMessageExt::SetArrayBuffer), 688 DECLARE_NAPI_FUNCTION("setArray", NapiWebMessageExt::SetArray), 689 DECLARE_NAPI_FUNCTION("setError", NapiWebMessageExt::SetError) 690 }; 691 napi_define_class(env, WEB_EXT_MSG_CLASS_NAME.c_str(), WEB_EXT_MSG_CLASS_NAME.length(), 692 NapiWebMessageExt::JsConstructor, nullptr, sizeof(webMsgExtClsProperties) / sizeof(webMsgExtClsProperties[0]), 693 webMsgExtClsProperties, &webMsgExtClass); 694 napi_create_reference(env, webMsgExtClass, 1, &g_webMsgExtClassRef); 695 napi_set_named_property(env, exports, WEB_EXT_MSG_CLASS_NAME.c_str(), webMsgExtClass); 696 697 napi_value securityLevelEnum = nullptr; 698 napi_property_descriptor securityLevelProperties[] = { 699 DECLARE_NAPI_STATIC_PROPERTY("NONE", NapiParseUtils::ToInt32Value(env, 700 static_cast<int32_t>(SecurityLevel::NONE))), 701 DECLARE_NAPI_STATIC_PROPERTY("SECURE", NapiParseUtils::ToInt32Value(env, 702 static_cast<int32_t>(SecurityLevel::SECURE))), 703 DECLARE_NAPI_STATIC_PROPERTY("WARNING", NapiParseUtils::ToInt32Value(env, 704 static_cast<int32_t>(SecurityLevel::WARNING))), 705 DECLARE_NAPI_STATIC_PROPERTY("DANGEROUS", NapiParseUtils::ToInt32Value(env, 706 static_cast<int32_t>(SecurityLevel::DANGEROUS))) 707 }; 708 napi_define_class(env, WEB_SECURITY_LEVEL_ENUM_NAME.c_str(), WEB_SECURITY_LEVEL_ENUM_NAME.length(), 709 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(securityLevelProperties) / 710 sizeof(securityLevelProperties[0]), securityLevelProperties, &securityLevelEnum); 711 napi_set_named_property(env, exports, WEB_SECURITY_LEVEL_ENUM_NAME.c_str(), securityLevelEnum); 712 713 napi_value msgPortCons = nullptr; 714 napi_property_descriptor msgPortProperties[] = { 715 DECLARE_NAPI_FUNCTION("close", NapiWebMessagePort::Close), 716 DECLARE_NAPI_FUNCTION("postMessageEvent", NapiWebMessagePort::PostMessageEvent), 717 DECLARE_NAPI_FUNCTION("onMessageEvent", NapiWebMessagePort::OnMessageEvent), 718 DECLARE_NAPI_FUNCTION("postMessageEventExt", NapiWebMessagePort::PostMessageEventExt), 719 DECLARE_NAPI_FUNCTION("onMessageEventExt", NapiWebMessagePort::OnMessageEventExt) 720 }; 721 NAPI_CALL(env, napi_define_class(env, WEB_MESSAGE_PORT_CLASS_NAME.c_str(), WEB_MESSAGE_PORT_CLASS_NAME.length(), 722 NapiWebMessagePort::JsConstructor, nullptr, sizeof(msgPortProperties) / sizeof(msgPortProperties[0]), 723 msgPortProperties, &msgPortCons)); 724 NAPI_CALL(env, napi_create_reference(env, msgPortCons, 1, &g_classWebMsgPort)); 725 NAPI_CALL(env, napi_set_named_property(env, exports, WEB_MESSAGE_PORT_CLASS_NAME.c_str(), msgPortCons)); 726 727 napi_value hitTestTypeEnum = nullptr; 728 napi_property_descriptor hitTestTypeProperties[] = { 729 DECLARE_NAPI_STATIC_PROPERTY("EditText", NapiParseUtils::ToInt32Value(env, 730 static_cast<int32_t>(WebHitTestType::EDIT))), 731 DECLARE_NAPI_STATIC_PROPERTY("Email", NapiParseUtils::ToInt32Value(env, 732 static_cast<int32_t>(WebHitTestType::EMAIL))), 733 DECLARE_NAPI_STATIC_PROPERTY("HttpAnchor", NapiParseUtils::ToInt32Value(env, 734 static_cast<int32_t>(WebHitTestType::HTTP))), 735 DECLARE_NAPI_STATIC_PROPERTY("HttpAnchorImg", NapiParseUtils::ToInt32Value(env, 736 static_cast<int32_t>(WebHitTestType::HTTP_IMG))), 737 DECLARE_NAPI_STATIC_PROPERTY("Img", NapiParseUtils::ToInt32Value(env, 738 static_cast<int32_t>(WebHitTestType::IMG))), 739 DECLARE_NAPI_STATIC_PROPERTY("Map", NapiParseUtils::ToInt32Value(env, 740 static_cast<int32_t>(WebHitTestType::MAP))), 741 DECLARE_NAPI_STATIC_PROPERTY("Phone", NapiParseUtils::ToInt32Value(env, 742 static_cast<int32_t>(WebHitTestType::PHONE))), 743 DECLARE_NAPI_STATIC_PROPERTY("Unknown", NapiParseUtils::ToInt32Value(env, 744 static_cast<int32_t>(WebHitTestType::UNKNOWN))), 745 }; 746 napi_define_class(env, WEB_HITTESTTYPE_V9_ENUM_NAME.c_str(), WEB_HITTESTTYPE_V9_ENUM_NAME.length(), 747 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(hitTestTypeProperties) / 748 sizeof(hitTestTypeProperties[0]), hitTestTypeProperties, &hitTestTypeEnum); 749 napi_set_named_property(env, exports, WEB_HITTESTTYPE_V9_ENUM_NAME.c_str(), hitTestTypeEnum); 750 751 napi_define_class(env, WEB_HITTESTTYPE_ENUM_NAME.c_str(), WEB_HITTESTTYPE_ENUM_NAME.length(), 752 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(hitTestTypeProperties) / 753 sizeof(hitTestTypeProperties[0]), hitTestTypeProperties, &hitTestTypeEnum); 754 napi_set_named_property(env, exports, WEB_HITTESTTYPE_ENUM_NAME.c_str(), hitTestTypeEnum); 755 756 napi_value secureDnsModeEnum = nullptr; 757 napi_property_descriptor secureDnsModeProperties[] = { 758 DECLARE_NAPI_STATIC_PROPERTY("Off", NapiParseUtils::ToInt32Value(env, 759 static_cast<int32_t>(SecureDnsModeType::OFF))), 760 DECLARE_NAPI_STATIC_PROPERTY("Auto", NapiParseUtils::ToInt32Value(env, 761 static_cast<int32_t>(SecureDnsModeType::AUTO))), 762 DECLARE_NAPI_STATIC_PROPERTY("SecureOnly", NapiParseUtils::ToInt32Value(env, 763 static_cast<int32_t>(SecureDnsModeType::SECURE_ONLY))), 764 DECLARE_NAPI_STATIC_PROPERTY("OFF", NapiParseUtils::ToInt32Value(env, 765 static_cast<int32_t>(SecureDnsModeType::OFF))), 766 DECLARE_NAPI_STATIC_PROPERTY("AUTO", NapiParseUtils::ToInt32Value(env, 767 static_cast<int32_t>(SecureDnsModeType::AUTO))), 768 DECLARE_NAPI_STATIC_PROPERTY("SECURE_ONLY", NapiParseUtils::ToInt32Value(env, 769 static_cast<int32_t>(SecureDnsModeType::SECURE_ONLY))), 770 }; 771 napi_define_class(env, WEB_SECURE_DNS_MODE_ENUM_NAME.c_str(), WEB_SECURE_DNS_MODE_ENUM_NAME.length(), 772 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(secureDnsModeProperties) / 773 sizeof(secureDnsModeProperties[0]), secureDnsModeProperties, &secureDnsModeEnum); 774 napi_set_named_property(env, exports, WEB_SECURE_DNS_MODE_ENUM_NAME.c_str(), secureDnsModeEnum); 775 776 napi_value historyList = nullptr; 777 napi_property_descriptor historyListProperties[] = { 778 DECLARE_NAPI_FUNCTION("getItemAtIndex", NapiWebHistoryList::GetItem) 779 }; 780 napi_define_class(env, WEB_HISTORY_LIST_CLASS_NAME.c_str(), WEB_HISTORY_LIST_CLASS_NAME.length(), 781 NapiWebHistoryList::JsConstructor, nullptr, sizeof(historyListProperties) / sizeof(historyListProperties[0]), 782 historyListProperties, &historyList); 783 napi_create_reference(env, historyList, 1, &g_historyListRef); 784 napi_set_named_property(env, exports, WEB_HISTORY_LIST_CLASS_NAME.c_str(), historyList); 785 786 napi_value webPrintDoc = nullptr; 787 napi_property_descriptor WebPrintDocumentClass[] = { 788 DECLARE_NAPI_FUNCTION("onStartLayoutWrite", NapiWebPrintDocument::OnStartLayoutWrite), 789 DECLARE_NAPI_FUNCTION("onJobStateChanged", NapiWebPrintDocument::OnJobStateChanged), 790 }; 791 napi_define_class(env, WEB_PRINT_DOCUMENT_CLASS_NAME.c_str(), WEB_PRINT_DOCUMENT_CLASS_NAME.length(), 792 NapiWebPrintDocument::JsConstructor, nullptr, 793 sizeof(WebPrintDocumentClass) / sizeof(WebPrintDocumentClass[0]), 794 WebPrintDocumentClass, &webPrintDoc); 795 napi_create_reference(env, webPrintDoc, 1, &g_webPrintDocClassRef); 796 napi_set_named_property(env, exports, WEB_PRINT_DOCUMENT_CLASS_NAME.c_str(), webPrintDoc); 797 798 napi_value renderProcessModeEnum = nullptr; 799 napi_property_descriptor renderProcessModeProperties[] = { 800 DECLARE_NAPI_STATIC_PROPERTY("SINGLE", NapiParseUtils::ToInt32Value(env, 801 static_cast<int32_t>(RenderProcessMode::SINGLE_MODE))), 802 DECLARE_NAPI_STATIC_PROPERTY("MULTIPLE", NapiParseUtils::ToInt32Value(env, 803 static_cast<int32_t>(RenderProcessMode::MULTIPLE_MODE))), 804 }; 805 napi_define_class(env, WEB_RENDER_PROCESS_MODE_ENUM_NAME.c_str(), WEB_RENDER_PROCESS_MODE_ENUM_NAME.length(), 806 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(renderProcessModeProperties) / 807 sizeof(renderProcessModeProperties[0]), renderProcessModeProperties, &renderProcessModeEnum); 808 napi_set_named_property(env, exports, WEB_RENDER_PROCESS_MODE_ENUM_NAME.c_str(), renderProcessModeEnum); 809 810 napi_value offlineResourceTypeEnum = nullptr; 811 napi_property_descriptor offlineResourceTypeProperties[] = { 812 DECLARE_NAPI_STATIC_PROPERTY("IMAGE", NapiParseUtils::ToInt32Value(env, 813 static_cast<int32_t>(OfflineResourceType::IMAGE))), 814 DECLARE_NAPI_STATIC_PROPERTY("CSS", NapiParseUtils::ToInt32Value(env, 815 static_cast<int32_t>(OfflineResourceType::CSS))), 816 DECLARE_NAPI_STATIC_PROPERTY("CLASSIC_JS", NapiParseUtils::ToInt32Value(env, 817 static_cast<int32_t>(OfflineResourceType::CLASSIC_JS))), 818 DECLARE_NAPI_STATIC_PROPERTY("MODULE_JS", NapiParseUtils::ToInt32Value(env, 819 static_cast<int32_t>(OfflineResourceType::MODULE_JS))), 820 }; 821 napi_define_class(env, OFFLINE_RESOURCE_TYPE_ENUM_NAME.c_str(), OFFLINE_RESOURCE_TYPE_ENUM_NAME.length(), 822 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(offlineResourceTypeProperties) / 823 sizeof(offlineResourceTypeProperties[0]), offlineResourceTypeProperties, &offlineResourceTypeEnum); 824 napi_set_named_property(env, exports, OFFLINE_RESOURCE_TYPE_ENUM_NAME.c_str(), offlineResourceTypeEnum); 825 826 napi_value scrollTypeEnum = nullptr; 827 napi_property_descriptor scrollTypeProperties[] = { 828 DECLARE_NAPI_STATIC_PROPERTY("EVENT", NapiParseUtils::ToInt32Value(env, 829 static_cast<int32_t>(ScrollType::EVENT))), 830 }; 831 napi_define_class(env, WEB_SCROLL_TYPE_ENUM_NAME.c_str(), WEB_SCROLL_TYPE_ENUM_NAME.length(), 832 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(scrollTypeProperties) / 833 sizeof(scrollTypeProperties[0]), scrollTypeProperties, &scrollTypeEnum); 834 napi_set_named_property(env, exports, WEB_SCROLL_TYPE_ENUM_NAME.c_str(), scrollTypeEnum); 835 836 napi_value pressureLevelEnum = nullptr; 837 napi_property_descriptor pressureLevelProperties[] = { 838 DECLARE_NAPI_STATIC_PROPERTY("MEMORY_PRESSURE_LEVEL_MODERATE", NapiParseUtils::ToInt32Value(env, 839 static_cast<int32_t>(PressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE))), 840 DECLARE_NAPI_STATIC_PROPERTY("MEMORY_PRESSURE_LEVEL_CRITICAL", NapiParseUtils::ToInt32Value(env, 841 static_cast<int32_t>(PressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL))), 842 }; 843 napi_define_class(env, WEB_PRESSURE_LEVEL_ENUM_NAME.c_str(), WEB_PRESSURE_LEVEL_ENUM_NAME.length(), 844 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(pressureLevelProperties) / 845 sizeof(pressureLevelProperties[0]), pressureLevelProperties, &pressureLevelEnum); 846 napi_set_named_property(env, exports, WEB_PRESSURE_LEVEL_ENUM_NAME.c_str(), pressureLevelEnum); 847 848 WebviewJavaScriptExecuteCallback::InitJSExcute(env, exports); 849 WebviewCreatePDFExecuteCallback::InitJSExcute(env, exports); 850 return exports; 851 } 852 JsConstructor(napi_env env,napi_callback_info info)853 napi_value NapiWebviewController::JsConstructor(napi_env env, napi_callback_info info) 854 { 855 WVLOG_I("NapiWebviewController::JsConstructor start"); 856 napi_value thisVar = nullptr; 857 858 size_t argc = INTEGER_ONE; 859 napi_value argv[INTEGER_ONE] = { 0 }; 860 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 861 862 WebviewController *webviewController; 863 std::string webTag; 864 if (argc == 1) { 865 NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], webTag); 866 if (webTag.empty()) { 867 WVLOG_E("native webTag is empty"); 868 return nullptr; 869 } 870 webviewController = new (std::nothrow) WebviewController(webTag); 871 WVLOG_I("new webview controller webname:%{public}s", webTag.c_str()); 872 } else { 873 webTag = WebviewController::GenerateWebTag(); 874 webviewController = new (std::nothrow) WebviewController(webTag); 875 } 876 WebviewController::webTagSet_.insert(webTag); 877 878 if (webviewController == nullptr) { 879 WVLOG_E("new webview controller failed"); 880 return nullptr; 881 } 882 napi_status status = napi_wrap( 883 env, thisVar, webviewController, 884 [](napi_env env, void *data, void *hint) { 885 WebviewController *webviewController = static_cast<WebviewController *>(data); 886 delete webviewController; 887 }, 888 nullptr, nullptr); 889 if (status != napi_ok) { 890 WVLOG_E("Wrap native webviewController failed."); 891 delete webviewController; 892 webviewController = nullptr; 893 return nullptr; 894 } 895 return thisVar; 896 } 897 InitializeWebEngine(napi_env env,napi_callback_info info)898 napi_value NapiWebviewController::InitializeWebEngine(napi_env env, napi_callback_info info) 899 { 900 WVLOG_D("InitializeWebEngine invoked."); 901 902 // obtain bundle path 903 std::shared_ptr<AbilityRuntime::ApplicationContext> ctx = 904 AbilityRuntime::ApplicationContext::GetApplicationContext(); 905 if (!ctx) { 906 WVLOG_E("Failed to init web engine due to nil application context."); 907 return nullptr; 908 } 909 910 // load so 911 const std::string& bundlePath = ctx->GetBundleCodeDir(); 912 NWebHelper::Instance().SetBundlePath(bundlePath); 913 if (!NWebHelper::Instance().InitAndRun(true)) { 914 WVLOG_E("Failed to init web engine due to NWebHelper failure."); 915 return nullptr; 916 } 917 918 napi_value result = nullptr; 919 NAPI_CALL(env, napi_get_undefined(env, &result)); 920 WVLOG_I("NWebHelper initialized, init web engine done, bundle_path: %{public}s", bundlePath.c_str()); 921 return result; 922 } 923 SetHttpDns(napi_env env,napi_callback_info info)924 napi_value NapiWebviewController::SetHttpDns(napi_env env, napi_callback_info info) 925 { 926 napi_value thisVar = nullptr; 927 napi_value result = nullptr; 928 size_t argc = INTEGER_TWO; 929 napi_value argv[INTEGER_TWO] = { 0 }; 930 int dohMode; 931 std::string dohConfig; 932 933 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 934 if (argc != INTEGER_TWO) { 935 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 936 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "two")); 937 return result; 938 } 939 940 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], dohMode)) { 941 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 942 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "secureDnsMode", "SecureDnsMode")); 943 return result; 944 } 945 946 if (dohMode < static_cast<int>(SecureDnsModeType::OFF) || 947 dohMode > static_cast<int>(SecureDnsModeType::SECURE_ONLY)) { 948 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 949 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "secureDnsMode")); 950 return result; 951 } 952 953 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], dohConfig)) { 954 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 955 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "secureDnsConfig", "string")); 956 return result; 957 } 958 959 if (dohConfig.rfind("https", 0) != 0 && dohConfig.rfind("HTTPS", 0) != 0) { 960 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 961 "BusinessError 401: Parameter error. Parameter secureDnsConfig must start with 'http' or 'https'."); 962 return result; 963 } 964 965 std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>(); 966 config->SetMode(dohMode); 967 config->SetConfig(dohConfig); 968 WVLOG_I("set http dns mode:%{public}d doh_config:%{public}s", dohMode, dohConfig.c_str()); 969 970 NWebHelper::Instance().SetHttpDns(config); 971 972 NAPI_CALL(env, napi_get_undefined(env, &result)); 973 return result; 974 } 975 SetWebDebuggingAccess(napi_env env,napi_callback_info info)976 napi_value NapiWebviewController::SetWebDebuggingAccess(napi_env env, napi_callback_info info) 977 { 978 WVLOG_D("SetWebDebuggingAccess start"); 979 napi_value result = nullptr; 980 if (OHOS::system::GetBoolParameter("web.debug.devtools", false)) { 981 return result; 982 } 983 napi_value thisVar = nullptr; 984 size_t argc = INTEGER_ONE; 985 napi_value argv[INTEGER_ONE] = {0}; 986 987 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 988 if (argc != INTEGER_ONE) { 989 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 990 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 991 return result; 992 } 993 994 bool webDebuggingAccess = false; 995 if (!NapiParseUtils::ParseBoolean(env, argv[0], webDebuggingAccess)) { 996 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 997 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "webDebuggingAccess", "boolean")); 998 return result; 999 } 1000 WebviewController::webDebuggingAccess_ = webDebuggingAccess; 1001 1002 NAPI_CALL(env, napi_get_undefined(env, &result)); 1003 return result; 1004 } 1005 EnableSafeBrowsing(napi_env env,napi_callback_info info)1006 napi_value NapiWebviewController::EnableSafeBrowsing(napi_env env, napi_callback_info info) 1007 { 1008 WVLOG_D("EnableSafeBrowsing start"); 1009 napi_value result = nullptr; 1010 napi_value thisVar = nullptr; 1011 size_t argc = INTEGER_ONE; 1012 napi_value argv[INTEGER_ONE] = {0}; 1013 1014 NAPI_CALL(env, napi_get_undefined(env, &result)); 1015 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1016 if (argc != INTEGER_ONE) { 1017 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1018 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1019 return result; 1020 } 1021 1022 bool safeBrowsingEnable = false; 1023 if (!NapiParseUtils::ParseBoolean(env, argv[0], safeBrowsingEnable)) { 1024 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1025 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "enable", "boolean")); 1026 return result; 1027 } 1028 1029 WebviewController *controller = nullptr; 1030 napi_unwrap(env, thisVar, (void **)&controller); 1031 if (!controller || !controller->IsInit()) { 1032 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 1033 return result; 1034 } 1035 controller->EnableSafeBrowsing(safeBrowsingEnable); 1036 return result; 1037 } 1038 IsSafeBrowsingEnabled(napi_env env,napi_callback_info info)1039 napi_value NapiWebviewController::IsSafeBrowsingEnabled(napi_env env, napi_callback_info info) 1040 { 1041 WVLOG_D("IsSafeBrowsingEnabled start"); 1042 napi_value result = nullptr; 1043 WebviewController *webviewController = GetWebviewController(env, info); 1044 if (!webviewController) { 1045 return nullptr; 1046 } 1047 1048 bool isSafeBrowsingEnabled = webviewController->IsSafeBrowsingEnabled(); 1049 NAPI_CALL(env, napi_get_boolean(env, isSafeBrowsingEnabled, &result)); 1050 return result; 1051 } 1052 InnerGetWebDebuggingAccess(napi_env env,napi_callback_info info)1053 napi_value NapiWebviewController::InnerGetWebDebuggingAccess(napi_env env, napi_callback_info info) 1054 { 1055 WVLOG_D("InnerGetWebDebuggingAccess start"); 1056 bool webDebuggingAccess = WebviewController::webDebuggingAccess_; 1057 napi_value result = nullptr; 1058 napi_get_boolean(env, webDebuggingAccess, &result); 1059 return result; 1060 } 1061 InnerGetThisVar(napi_env env,napi_callback_info info)1062 napi_value NapiWebviewController::InnerGetThisVar(napi_env env, napi_callback_info info) 1063 { 1064 WVLOG_D("InnerGetThisVar start"); 1065 napi_value thisVar = nullptr; 1066 napi_value result = nullptr; 1067 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 1068 WebviewController *webviewController = nullptr; 1069 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 1070 if ((!webviewController) || (status != napi_ok)) { 1071 WVLOG_E("webviewController is nullptr."); 1072 napi_create_int64(env, 0, &result); 1073 } else { 1074 napi_create_int64(env, reinterpret_cast<int64_t>(webviewController), &result); 1075 } 1076 return result; 1077 } 1078 SetWebId(napi_env env,napi_callback_info info)1079 napi_value NapiWebviewController::SetWebId(napi_env env, napi_callback_info info) 1080 { 1081 napi_value thisVar = nullptr; 1082 size_t argc = INTEGER_ONE; 1083 napi_value argv[INTEGER_ONE]; 1084 void* data = nullptr; 1085 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 1086 1087 int32_t webId = -1; 1088 if (!NapiParseUtils::ParseInt32(env, argv[0], webId)) { 1089 WVLOG_E("Parse web id failed."); 1090 return nullptr; 1091 } 1092 WebviewController *webviewController = nullptr; 1093 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 1094 if ((!webviewController) || (status != napi_ok)) { 1095 WVLOG_E("webviewController is nullptr."); 1096 return nullptr; 1097 } 1098 webviewController->SetWebId(webId); 1099 return thisVar; 1100 } 1101 InnerSetHapPath(napi_env env,napi_callback_info info)1102 napi_value NapiWebviewController::InnerSetHapPath(napi_env env, napi_callback_info info) 1103 { 1104 napi_value result = nullptr; 1105 NAPI_CALL(env, napi_get_undefined(env, &result)); 1106 napi_value thisVar = nullptr; 1107 size_t argc = INTEGER_ONE; 1108 napi_value argv[INTEGER_ONE]; 1109 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1110 if (argc != INTEGER_ONE) { 1111 WVLOG_E("Failed to run InnerSetHapPath beacuse of wrong Param number."); 1112 return result; 1113 } 1114 std::string hapPath; 1115 if (!NapiParseUtils::ParseString(env, argv[0], hapPath)) { 1116 WVLOG_E("Parse hap path failed."); 1117 return result; 1118 } 1119 WebviewController *webviewController = nullptr; 1120 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 1121 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 1122 WVLOG_E("Wrap webviewController failed. WebviewController must be associated with a Web component."); 1123 return result; 1124 } 1125 webviewController->InnerSetHapPath(hapPath); 1126 return result; 1127 } 1128 InnerJsProxy(napi_env env,napi_callback_info info)1129 napi_value NapiWebviewController::InnerJsProxy(napi_env env, napi_callback_info info) 1130 { 1131 napi_value thisVar = nullptr; 1132 napi_value result = nullptr; 1133 size_t argc = INTEGER_FIVE; 1134 napi_value argv[INTEGER_FIVE] = { 0 }; 1135 napi_get_undefined(env, &result); 1136 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1137 if (argc != INTEGER_FIVE) { 1138 WVLOG_E("Failed to run InnerJsProxy beacuse of wrong Param number."); 1139 return result; 1140 } 1141 napi_valuetype valueType = napi_undefined; 1142 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 1143 if (valueType != napi_object) { 1144 WVLOG_E("Failed to run InnerJsProxy beacuse of wrong Param type."); 1145 return result; 1146 } 1147 std::string objName; 1148 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], objName)) { 1149 WVLOG_E("Failed to run InnerJsProxy beacuse of wrong object name."); 1150 return result; 1151 } 1152 std::vector<std::string> methodList; 1153 bool hasSyncMethod = NapiParseUtils::ParseStringArray(env, argv[INTEGER_TWO], methodList); 1154 std::vector<std::string> asyncMethodList; 1155 bool hasAsyncMethod = NapiParseUtils::ParseStringArray(env, argv[INTEGER_THREE], asyncMethodList); 1156 if (!hasSyncMethod && !hasAsyncMethod) { 1157 WVLOG_E("Failed to run InnerJsProxy beacuse of empty method lists."); 1158 return result; 1159 } 1160 std::string permission = ""; 1161 NapiParseUtils::ParseString(env, argv[INTEGER_FOUR], permission); 1162 WebviewController* controller = nullptr; 1163 napi_unwrap(env, thisVar, (void **)&controller); 1164 if (!controller || !controller->IsInit()) { 1165 WVLOG_E("Failed to run InnerJsProxy. The WebviewController must be associted with a Web component."); 1166 return result; 1167 } 1168 controller->SetNWebJavaScriptResultCallBack(); 1169 RegisterJavaScriptProxyParam param; 1170 param.env = env; 1171 param.obj = argv[INTEGER_ZERO]; 1172 param.objName = objName; 1173 param.syncMethodList = methodList; 1174 param.asyncMethodList = asyncMethodList; 1175 param.permission = permission; 1176 controller->RegisterJavaScriptProxy(param); 1177 return result; 1178 } 1179 InnerGetCustomeSchemeCmdLine(napi_env env,napi_callback_info info)1180 napi_value NapiWebviewController::InnerGetCustomeSchemeCmdLine(napi_env env, napi_callback_info info) 1181 { 1182 WebviewController::existNweb_ = true; 1183 napi_value result = nullptr; 1184 const std::string& cmdLine = WebviewController::customeSchemeCmdLine_; 1185 napi_create_string_utf8(env, cmdLine.c_str(), cmdLine.length(), &result); 1186 return result; 1187 } 1188 AccessForward(napi_env env,napi_callback_info info)1189 napi_value NapiWebviewController::AccessForward(napi_env env, napi_callback_info info) 1190 { 1191 napi_value result = nullptr; 1192 WebviewController *webviewController = GetWebviewController(env, info); 1193 if (!webviewController) { 1194 return nullptr; 1195 } 1196 1197 bool access = webviewController->AccessForward(); 1198 NAPI_CALL(env, napi_get_boolean(env, access, &result)); 1199 return result; 1200 } 1201 AccessBackward(napi_env env,napi_callback_info info)1202 napi_value NapiWebviewController::AccessBackward(napi_env env, napi_callback_info info) 1203 { 1204 napi_value result = nullptr; 1205 WebviewController *webviewController = GetWebviewController(env, info); 1206 if (!webviewController) { 1207 return nullptr; 1208 } 1209 1210 bool access = webviewController->AccessBackward(); 1211 NAPI_CALL(env, napi_get_boolean(env, access, &result)); 1212 return result; 1213 } 1214 Forward(napi_env env,napi_callback_info info)1215 napi_value NapiWebviewController::Forward(napi_env env, napi_callback_info info) 1216 { 1217 napi_value result = nullptr; 1218 WebviewController *webviewController = GetWebviewController(env, info); 1219 if (!webviewController) { 1220 return nullptr; 1221 } 1222 1223 webviewController->Forward(); 1224 NAPI_CALL(env, napi_get_undefined(env, &result)); 1225 return result; 1226 } 1227 Backward(napi_env env,napi_callback_info info)1228 napi_value NapiWebviewController::Backward(napi_env env, napi_callback_info info) 1229 { 1230 napi_value result = nullptr; 1231 WebviewController *webviewController = GetWebviewController(env, info); 1232 if (!webviewController) { 1233 return nullptr; 1234 } 1235 1236 webviewController->Backward(); 1237 NAPI_CALL(env, napi_get_undefined(env, &result)); 1238 return result; 1239 } 1240 AccessStep(napi_env env,napi_callback_info info)1241 napi_value NapiWebviewController::AccessStep(napi_env env, napi_callback_info info) 1242 { 1243 napi_value thisVar = nullptr; 1244 napi_value result = nullptr; 1245 size_t argc = INTEGER_ONE; 1246 napi_value argv[INTEGER_ONE]; 1247 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1248 if (argc != INTEGER_ONE) { 1249 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1250 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1251 return nullptr; 1252 } 1253 1254 int32_t step = INTEGER_ZERO; 1255 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], step)) { 1256 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1257 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "step", "number")); 1258 return nullptr; 1259 } 1260 1261 WebviewController *webviewController = nullptr; 1262 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 1263 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 1264 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 1265 return nullptr; 1266 } 1267 1268 bool access = webviewController->AccessStep(step); 1269 NAPI_CALL(env, napi_get_boolean(env, access, &result)); 1270 return result; 1271 } 1272 ClearHistory(napi_env env,napi_callback_info info)1273 napi_value NapiWebviewController::ClearHistory(napi_env env, napi_callback_info info) 1274 { 1275 napi_value result = nullptr; 1276 WebviewController *webviewController = GetWebviewController(env, info); 1277 if (!webviewController) { 1278 return nullptr; 1279 } 1280 1281 webviewController->ClearHistory(); 1282 NAPI_CALL(env, napi_get_undefined(env, &result)); 1283 return result; 1284 } 1285 OnActive(napi_env env,napi_callback_info info)1286 napi_value NapiWebviewController::OnActive(napi_env env, napi_callback_info info) 1287 { 1288 napi_value result = nullptr; 1289 WebviewController *webviewController = GetWebviewController(env, info); 1290 if (!webviewController) { 1291 WVLOG_E("NapiWebviewController::OnActive get controller failed"); 1292 return nullptr; 1293 } 1294 1295 webviewController->OnActive(); 1296 WVLOG_I("The web component has been successfully activated"); 1297 NAPI_CALL(env, napi_get_undefined(env, &result)); 1298 return result; 1299 } 1300 OnInactive(napi_env env,napi_callback_info info)1301 napi_value NapiWebviewController::OnInactive(napi_env env, napi_callback_info info) 1302 { 1303 napi_value result = nullptr; 1304 WebviewController *webviewController = GetWebviewController(env, info); 1305 if (!webviewController) { 1306 WVLOG_E("NapiWebviewController::OnInactive get controller failed"); 1307 return nullptr; 1308 } 1309 1310 webviewController->OnInactive(); 1311 WVLOG_I("The web component has been successfully inactivated"); 1312 NAPI_CALL(env, napi_get_undefined(env, &result)); 1313 return result; 1314 } 1315 Refresh(napi_env env,napi_callback_info info)1316 napi_value NapiWebviewController::Refresh(napi_env env, napi_callback_info info) 1317 { 1318 napi_value result = nullptr; 1319 WebviewController *webviewController = GetWebviewController(env, info); 1320 if (!webviewController) { 1321 return nullptr; 1322 } 1323 1324 webviewController->Refresh(); 1325 NAPI_CALL(env, napi_get_undefined(env, &result)); 1326 return result; 1327 } 1328 1329 JsConstructor(napi_env env,napi_callback_info info)1330 napi_value NapiWebMessageExt::JsConstructor(napi_env env, napi_callback_info info) 1331 { 1332 WVLOG_D("NapiWebMessageExt::JsConstructor"); 1333 napi_value thisVar = nullptr; 1334 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 1335 1336 auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE); 1337 WebMessageExt *webMessageExt = new (std::nothrow) WebMessageExt(webMsg); 1338 if (webMessageExt == nullptr) { 1339 WVLOG_E("new msg port failed"); 1340 return nullptr; 1341 } 1342 NAPI_CALL(env, napi_wrap(env, thisVar, webMessageExt, 1343 [](napi_env env, void *data, void *hint) { 1344 WebMessageExt *webMessageExt = static_cast<WebMessageExt *>(data); 1345 if (webMessageExt) { 1346 delete webMessageExt; 1347 } 1348 }, 1349 nullptr, nullptr)); 1350 return thisVar; 1351 } 1352 GetType(napi_env env,napi_callback_info info)1353 napi_value NapiWebMessageExt::GetType(napi_env env, napi_callback_info info) 1354 { 1355 WVLOG_D("NapiWebMessageExt::GetType start"); 1356 napi_value thisVar = nullptr; 1357 napi_value result = nullptr; 1358 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 1359 if (status != napi_status::napi_ok) { 1360 WVLOG_E("napi_get_cb_info status not ok"); 1361 return result; 1362 } 1363 1364 if (thisVar == nullptr) { 1365 WVLOG_E("napi_get_cb_info thisVar is nullptr"); 1366 return result; 1367 } 1368 1369 WebMessageExt *webMessageExt = nullptr; 1370 status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1371 if ((!webMessageExt) || (status != napi_ok)) { 1372 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 1373 return nullptr; 1374 } 1375 1376 int32_t type = webMessageExt->GetType(); 1377 status = napi_create_int32(env, type, &result); 1378 if (status != napi_status::napi_ok) { 1379 WVLOG_E("napi_create_int32 failed."); 1380 return result; 1381 } 1382 return result; 1383 } 1384 GetString(napi_env env,napi_callback_info info)1385 napi_value NapiWebMessageExt::GetString(napi_env env, napi_callback_info info) 1386 { 1387 WVLOG_D(" GetString webJsMessageExt start"); 1388 napi_value thisVar = nullptr; 1389 napi_value result = nullptr; 1390 size_t argc = INTEGER_ONE; 1391 napi_value argv[INTEGER_ONE] = { 0 }; 1392 1393 WebMessageExt *webJsMessageExt = nullptr; 1394 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1395 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt)); 1396 if (webJsMessageExt == nullptr) { 1397 WVLOG_E("unwrap webJsMessageExt failed."); 1398 return result; 1399 } 1400 1401 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::STRING)) { 1402 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1403 return nullptr; 1404 } 1405 1406 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result); 1407 return result; 1408 } 1409 GetNumber(napi_env env,napi_callback_info info)1410 napi_value NapiWebMessageExt::GetNumber(napi_env env, napi_callback_info info) 1411 { 1412 WVLOG_D("GetNumber webJsMessageExt start"); 1413 napi_value thisVar = nullptr; 1414 napi_value result = nullptr; 1415 size_t argc = INTEGER_ONE; 1416 napi_value argv[INTEGER_ONE] = { 0 }; 1417 1418 WebMessageExt *webJsMessageExt = nullptr; 1419 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1420 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt)); 1421 if (webJsMessageExt == nullptr) { 1422 WVLOG_E("unwrap webJsMessageExt failed."); 1423 return result; 1424 } 1425 1426 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::NUMBER)) { 1427 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1428 WVLOG_E("GetNumber webJsMessageExt failed,not match"); 1429 return nullptr; 1430 } 1431 1432 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result); 1433 return result; 1434 } 1435 GetBoolean(napi_env env,napi_callback_info info)1436 napi_value NapiWebMessageExt::GetBoolean(napi_env env, napi_callback_info info) 1437 { 1438 napi_value thisVar = nullptr; 1439 napi_value result = nullptr; 1440 size_t argc = INTEGER_ONE; 1441 napi_value argv[INTEGER_ONE] = { 0 }; 1442 1443 WebMessageExt *webJsMessageExt = nullptr; 1444 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1445 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt)); 1446 if (webJsMessageExt == nullptr) { 1447 WVLOG_E("unwrap webJsMessageExt failed."); 1448 return result; 1449 } 1450 1451 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::BOOLEAN)) { 1452 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1453 return nullptr; 1454 } 1455 1456 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result); 1457 return result; 1458 } 1459 GetArrayBuffer(napi_env env,napi_callback_info info)1460 napi_value NapiWebMessageExt::GetArrayBuffer(napi_env env, napi_callback_info info) 1461 { 1462 napi_value thisVar = nullptr; 1463 napi_value result = nullptr; 1464 size_t argc = INTEGER_ONE; 1465 napi_value argv[INTEGER_ONE] = { 0 }; 1466 1467 WebMessageExt *webJsMessageExt = nullptr; 1468 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1469 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt)); 1470 if (webJsMessageExt == nullptr) { 1471 WVLOG_E("unwrap webJsMessageExt failed."); 1472 return result; 1473 } 1474 1475 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAYBUFFER)) { 1476 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1477 return nullptr; 1478 } 1479 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result); 1480 return result; 1481 } 1482 GetArray(napi_env env,napi_callback_info info)1483 napi_value NapiWebMessageExt::GetArray(napi_env env, napi_callback_info info) 1484 { 1485 napi_value thisVar = nullptr; 1486 napi_value result = nullptr; 1487 size_t argc = INTEGER_ONE; 1488 napi_value argv[INTEGER_ONE] = { 0 }; 1489 1490 WebMessageExt *webJsMessageExt = nullptr; 1491 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1492 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt)); 1493 if (webJsMessageExt == nullptr) { 1494 WVLOG_E("unwrap webJsMessageExt failed."); 1495 return result; 1496 } 1497 1498 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) { 1499 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1500 return nullptr; 1501 } 1502 1503 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result); 1504 return result; 1505 } 1506 GetError(napi_env env,napi_callback_info info)1507 napi_value NapiWebMessageExt::GetError(napi_env env, napi_callback_info info) 1508 { 1509 napi_value thisVar = nullptr; 1510 napi_value result = nullptr; 1511 size_t argc = INTEGER_ONE; 1512 napi_value argv[INTEGER_ONE] = { 0 }; 1513 1514 WebMessageExt *webJsMessageExt = nullptr; 1515 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1516 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt)); 1517 if (webJsMessageExt == nullptr) { 1518 WVLOG_E("unwrap webJsMessageExt failed."); 1519 return result; 1520 } 1521 1522 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ERROR)) { 1523 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1524 return nullptr; 1525 } 1526 1527 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result); 1528 return result; 1529 } 1530 SetType(napi_env env,napi_callback_info info)1531 napi_value NapiWebMessageExt::SetType(napi_env env, napi_callback_info info) 1532 { 1533 WVLOG_D("NapiWebMessageExt::SetType"); 1534 napi_value thisVar = nullptr; 1535 napi_value result = nullptr; 1536 size_t argc = INTEGER_ONE; 1537 napi_value argv[INTEGER_ONE] = { 0 }; 1538 int type = -1; 1539 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1540 if (status != napi_ok) { 1541 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1542 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "type")); 1543 WVLOG_E("NapiWebMessageExt::SetType napi_get_cb_info failed"); 1544 return result; 1545 } 1546 if (thisVar == nullptr) { 1547 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1548 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NOT_NULL, "type")); 1549 WVLOG_E("NapiWebMessageExt::SetType thisVar is null"); 1550 return result; 1551 } 1552 if (argc != INTEGER_ONE) { 1553 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1554 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1555 return result; 1556 } 1557 if (!NapiParseUtils::ParseInt32(env, argv[0], type)) { 1558 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, ParamCheckErrorMsgTemplate::TYPE_ALL_INT); 1559 return result; 1560 } 1561 if (type <= static_cast<int>(WebMessageType::NOTSUPPORT) || type > static_cast<int>(WebMessageType::ERROR)) { 1562 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1563 return result; 1564 } 1565 WebMessageExt *webMessageExt = nullptr; 1566 status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1567 if (status != napi_ok) { 1568 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1569 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "type")); 1570 WVLOG_E("NapiWebMessageExt::SetType napi_unwrap failed"); 1571 return result; 1572 } 1573 if (!webMessageExt) { 1574 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1575 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NOT_NULL, "type")); 1576 WVLOG_E("NapiWebMessageExt::SetType webMessageExt is null"); 1577 return result; 1578 } 1579 webMessageExt->SetType(type); 1580 return result; 1581 } 1582 SetString(napi_env env,napi_callback_info info)1583 napi_value NapiWebMessageExt::SetString(napi_env env, napi_callback_info info) 1584 { 1585 WVLOG_D("NapiWebMessageExt::SetString start"); 1586 napi_value thisVar = nullptr; 1587 napi_value result = nullptr; 1588 size_t argc = INTEGER_ONE; 1589 napi_value argv[INTEGER_ONE] = { 0 }; 1590 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1591 if (argc != INTEGER_ONE) { 1592 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1593 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1594 return result; 1595 } 1596 1597 std::string value; 1598 if (!NapiParseUtils::ParseString(env, argv[0], value)) { 1599 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1600 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "string")); 1601 return result; 1602 } 1603 WebMessageExt *webMessageExt = nullptr; 1604 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1605 if ((!webMessageExt) || (status != napi_ok)) { 1606 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1607 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 1608 return result; 1609 } 1610 1611 int32_t type = webMessageExt->GetType(); 1612 if (type != static_cast<int32_t>(WebMessageType::STRING)) { 1613 WVLOG_E("web message SetString error type:%{public}d", type); 1614 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1615 return result; 1616 } 1617 webMessageExt->SetString(value); 1618 return result; 1619 } 1620 SetNumber(napi_env env,napi_callback_info info)1621 napi_value NapiWebMessageExt::SetNumber(napi_env env, napi_callback_info info) 1622 { 1623 WVLOG_D("NapiWebMessageExt::SetNumber start"); 1624 napi_value thisVar = nullptr; 1625 napi_value result = nullptr; 1626 size_t argc = INTEGER_ONE; 1627 napi_value argv[INTEGER_ONE] = { 0 }; 1628 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1629 if (argc != INTEGER_ONE) { 1630 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1631 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1632 return result; 1633 } 1634 1635 double value = 0; 1636 if (!NapiParseUtils::ParseDouble(env, argv[0], value)) { 1637 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1638 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "number")); 1639 return result; 1640 } 1641 1642 WebMessageExt *webMessageExt = nullptr; 1643 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1644 if ((!webMessageExt) || (status != napi_ok)) { 1645 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1646 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 1647 return result; 1648 } 1649 1650 int32_t type = webMessageExt->GetType(); 1651 if (type != static_cast<int32_t>(WebMessageType::NUMBER)) { 1652 WVLOG_E("web message SetNumber error type:%{public}d", type); 1653 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1654 return result; 1655 } 1656 webMessageExt->SetNumber(value); 1657 return result; 1658 } 1659 SetBoolean(napi_env env,napi_callback_info info)1660 napi_value NapiWebMessageExt::SetBoolean(napi_env env, napi_callback_info info) 1661 { 1662 WVLOG_D("NapiWebMessageExt::SetBoolean start"); 1663 napi_value thisVar = nullptr; 1664 napi_value result = nullptr; 1665 size_t argc = INTEGER_ONE; 1666 napi_value argv[INTEGER_ONE] = { 0 }; 1667 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1668 if (argc != INTEGER_ONE) { 1669 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1670 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1671 return result; 1672 } 1673 1674 bool value = 0; 1675 if (!NapiParseUtils::ParseBoolean(env, argv[0], value)) { 1676 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1677 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "boolean")); 1678 return result; 1679 } 1680 1681 WebMessageExt *webMessageExt = nullptr; 1682 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1683 if ((!webMessageExt) || (status != napi_ok)) { 1684 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1685 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 1686 return result; 1687 } 1688 1689 int32_t type = webMessageExt->GetType(); 1690 if (type != static_cast<int32_t>(WebMessageType::BOOLEAN)) { 1691 WVLOG_E("web message SetBoolean error type:%{public}d", type); 1692 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1693 return result; 1694 } 1695 webMessageExt->SetBoolean(value); 1696 return result; 1697 } 1698 SetArrayBuffer(napi_env env,napi_callback_info info)1699 napi_value NapiWebMessageExt::SetArrayBuffer(napi_env env, napi_callback_info info) 1700 { 1701 WVLOG_D("NapiWebMessageExt::SetArrayBuffer start"); 1702 napi_value thisVar = nullptr; 1703 napi_value result = nullptr; 1704 size_t argc = INTEGER_ONE; 1705 napi_value argv[INTEGER_ONE] = { 0 }; 1706 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1707 if (argc != INTEGER_ONE) { 1708 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1709 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1710 return result; 1711 } 1712 1713 bool isArrayBuffer = false; 1714 NAPI_CALL(env, napi_is_arraybuffer(env, argv[INTEGER_ZERO], &isArrayBuffer)); 1715 if (!isArrayBuffer) { 1716 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1717 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "arrayBuffer")); 1718 return result; 1719 } 1720 1721 uint8_t *arrBuf = nullptr; 1722 size_t byteLength = 0; 1723 napi_get_arraybuffer_info(env, argv[INTEGER_ZERO], (void**)&arrBuf, &byteLength); 1724 std::vector<uint8_t> vecData(arrBuf, arrBuf + byteLength); 1725 WebMessageExt *webMessageExt = nullptr; 1726 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1727 if ((!webMessageExt) || (status != napi_ok)) { 1728 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1729 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 1730 return result; 1731 } 1732 1733 int32_t type = webMessageExt->GetType(); 1734 if (type != static_cast<int32_t>(WebMessageType::ARRAYBUFFER)) { 1735 WVLOG_E("web message SetArrayBuffer error type:%{public}d", type); 1736 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1737 return result; 1738 } 1739 webMessageExt->SetArrayBuffer(vecData); 1740 return result; 1741 } 1742 SetArray(napi_env env,napi_callback_info info)1743 napi_value NapiWebMessageExt::SetArray(napi_env env, napi_callback_info info) 1744 { 1745 WVLOG_D("NapiWebMessageExt::SetArray start"); 1746 napi_value thisVar = nullptr; 1747 napi_value result = nullptr; 1748 size_t argc = INTEGER_ONE; 1749 napi_value argv[INTEGER_ONE] = { 0 }; 1750 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1751 if (argc != INTEGER_ONE) { 1752 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1753 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1754 return result; 1755 } 1756 bool isArray = false; 1757 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ZERO], &isArray)); 1758 if (!isArray) { 1759 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1760 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "array")); 1761 return result; 1762 } 1763 WebMessageExt *webMessageExt = nullptr; 1764 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1765 if ((!webMessageExt) || (status != napi_ok)) { 1766 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1767 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 1768 return result; 1769 } 1770 int32_t type = webMessageExt->GetType(); 1771 if (type != static_cast<int32_t>(WebMessageType::ARRAY)) { 1772 WVLOG_E("web message SetArray error type:%{public}d", type); 1773 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1774 return result; 1775 } 1776 bool isDouble = false; 1777 napi_valuetype valueType = GetArrayValueType(env, argv[INTEGER_ZERO], isDouble); 1778 if (valueType == napi_undefined) { 1779 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1780 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "number")); 1781 return result; 1782 } 1783 using SetArrayHandler = std::function<void(napi_env, napi_value, WebMessageExt*)>; 1784 const std::unordered_map<napi_valuetype, SetArrayHandler> functionMap = { 1785 { napi_boolean, SetArrayHandlerBoolean }, 1786 { napi_string, SetArrayHandlerString }, 1787 { napi_number, [isDouble](napi_env env, napi_value array, WebMessageExt* msgExt) { 1788 isDouble ? SetArrayHandlerDouble(env, array, msgExt) 1789 : SetArrayHandlerInteger(env, array, msgExt); 1790 } } 1791 }; 1792 auto it = functionMap.find(valueType); 1793 if (it != functionMap.end()) { 1794 it->second(env, argv[INTEGER_ZERO], webMessageExt); 1795 } 1796 return result; 1797 } 1798 SetError(napi_env env,napi_callback_info info)1799 napi_value NapiWebMessageExt::SetError(napi_env env, napi_callback_info info) 1800 { 1801 WVLOG_D("NapiWebMessageExt::SetError start"); 1802 napi_value thisVar = nullptr; 1803 napi_value result = nullptr; 1804 size_t argc = INTEGER_ONE; 1805 napi_value argv[INTEGER_ONE] = { 0 }; 1806 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 1807 if (argc != INTEGER_ONE) { 1808 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1809 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 1810 return result; 1811 } 1812 1813 bool isError = false; 1814 NAPI_CALL(env, napi_is_error(env, argv[INTEGER_ZERO], &isError)); 1815 if (!isError) { 1816 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1817 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "message", "error")); 1818 return result; 1819 } 1820 1821 napi_value nameObj = 0; 1822 napi_get_named_property(env, argv[INTEGER_ZERO], "name", &nameObj); 1823 std::string nameVal; 1824 if (!NapiParseUtils::ParseString(env, nameObj, nameVal)) { 1825 return result; 1826 } 1827 1828 napi_value msgObj = 0; 1829 napi_get_named_property(env, argv[INTEGER_ZERO], "message", &msgObj); 1830 std::string msgVal; 1831 if (!NapiParseUtils::ParseString(env, msgObj, msgVal)) { 1832 return result; 1833 } 1834 1835 WebMessageExt *webMessageExt = nullptr; 1836 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt); 1837 if ((!webMessageExt) || (status != napi_ok)) { 1838 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1839 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 1840 return result; 1841 } 1842 1843 int32_t type = webMessageExt->GetType(); 1844 if (type != static_cast<int32_t>(WebMessageType::ERROR)) { 1845 WVLOG_E("web message SetError error type:%{public}d", type); 1846 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 1847 return result; 1848 } 1849 webMessageExt->SetError(nameVal, msgVal); 1850 return result; 1851 } 1852 CreateWebMessagePorts(napi_env env,napi_callback_info info)1853 napi_value NapiWebviewController::CreateWebMessagePorts(napi_env env, napi_callback_info info) 1854 { 1855 WVLOG_D("create web message port"); 1856 napi_value thisVar = nullptr; 1857 napi_value result = nullptr; 1858 size_t argc = INTEGER_ONE; 1859 napi_value argv[INTEGER_ONE] = { 0 }; 1860 bool isExtentionType = false; 1861 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1862 if (argc != INTEGER_ZERO && argc != INTEGER_ONE) { 1863 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1864 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "zero", "one")); 1865 return result; 1866 } 1867 1868 if (argc == INTEGER_ONE) { 1869 if (!NapiParseUtils::ParseBoolean(env, argv[0], isExtentionType)) { 1870 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1871 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "isExtentionType", "boolean")); 1872 return result; 1873 } 1874 } 1875 1876 WebviewController *webviewController = nullptr; 1877 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 1878 if (webviewController == nullptr || !webviewController->IsInit()) { 1879 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 1880 WVLOG_E("create message port failed, napi unwrap webviewController failed"); 1881 return nullptr; 1882 } 1883 int32_t nwebId = webviewController->GetWebId(); 1884 std::vector<std::string> ports = webviewController->CreateWebMessagePorts(); 1885 if (ports.size() != INTEGER_TWO) { 1886 WVLOG_E("create web message port failed"); 1887 return result; 1888 } 1889 napi_value msgPortcons = nullptr; 1890 NAPI_CALL(env, napi_get_reference_value(env, g_classWebMsgPort, &msgPortcons)); 1891 napi_create_array(env, &result); 1892 napi_value consParam[INTEGER_TWO][INTEGER_THREE] = {{0}}; 1893 for (uint32_t i = 0; i < INTEGER_TWO; i++) { 1894 napi_value msgPortObj = nullptr; 1895 NAPI_CALL(env, napi_create_int32(env, nwebId, &consParam[i][INTEGER_ZERO])); 1896 NAPI_CALL(env, napi_create_string_utf8(env, ports[i].c_str(), ports[i].length(), &consParam[i][INTEGER_ONE])); 1897 NAPI_CALL(env, napi_get_boolean(env, isExtentionType, &consParam[i][INTEGER_TWO])); 1898 NAPI_CALL(env, napi_new_instance(env, msgPortcons, INTEGER_THREE, consParam[i], &msgPortObj)); 1899 napi_value jsExtention; 1900 napi_get_boolean(env, isExtentionType, &jsExtention); 1901 napi_set_named_property(env, msgPortObj, "isExtentionType", jsExtention); 1902 1903 napi_set_element(env, result, i, msgPortObj); 1904 } 1905 1906 return result; 1907 } 1908 GetSendPorts(napi_env env,napi_value argv,std::vector<std::string> & sendPorts)1909 bool GetSendPorts(napi_env env, napi_value argv, std::vector<std::string>& sendPorts) 1910 { 1911 uint32_t arrayLen = 0; 1912 napi_get_array_length(env, argv, &arrayLen); 1913 if (arrayLen == 0) { 1914 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 1915 return false; 1916 } 1917 1918 napi_valuetype valueType = napi_undefined; 1919 for (uint32_t i = 0; i < arrayLen; i++) { 1920 napi_value portItem = nullptr; 1921 napi_get_element(env, argv, i, &portItem); 1922 napi_typeof(env, portItem, &valueType); 1923 if (valueType != napi_object) { 1924 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 1925 return false; 1926 } 1927 WebMessagePort *msgPort = nullptr; 1928 napi_status status = napi_unwrap(env, portItem, (void **)&msgPort); 1929 if ((!msgPort) || (status != napi_ok)) { 1930 WVLOG_E("post port to html failed, napi unwrap msg port fail"); 1931 return false; 1932 } 1933 std::string portHandle = msgPort->GetPortHandle(); 1934 sendPorts.emplace_back(portHandle); 1935 } 1936 return true; 1937 } 1938 PostMessage(napi_env env,napi_callback_info info)1939 napi_value NapiWebviewController::PostMessage(napi_env env, napi_callback_info info) 1940 { 1941 WVLOG_D("post message port"); 1942 napi_value thisVar = nullptr; 1943 napi_value result = nullptr; 1944 size_t argc = INTEGER_THREE; 1945 napi_value argv[INTEGER_THREE]; 1946 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 1947 if (argc != INTEGER_THREE) { 1948 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1949 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "three")); 1950 return result; 1951 } 1952 1953 std::string portName; 1954 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], portName)) { 1955 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1956 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "name", "string")); 1957 return result; 1958 } 1959 1960 bool isArray = false; 1961 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ONE], &isArray)); 1962 if (!isArray) { 1963 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1964 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "ports", "array")); 1965 return result; 1966 } 1967 std::vector<std::string> sendPorts; 1968 if (!GetSendPorts(env, argv[INTEGER_ONE], sendPorts)) { 1969 WVLOG_E("post port to html failed, getSendPorts fail"); 1970 return result; 1971 } 1972 1973 std::string urlStr; 1974 if (!NapiParseUtils::ParseString(env, argv[INTEGER_TWO], urlStr)) { 1975 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 1976 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "uri", "string")); 1977 return result; 1978 } 1979 1980 WebviewController *webviewController = nullptr; 1981 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 1982 if (webviewController == nullptr || !webviewController->IsInit()) { 1983 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 1984 WVLOG_E("post port to html failed, napi unwrap webviewController failed"); 1985 return nullptr; 1986 } 1987 1988 webviewController->PostWebMessage(portName, sendPorts, urlStr); 1989 NAPI_CALL(env, napi_get_undefined(env, &result)); 1990 1991 return result; 1992 } 1993 JsConstructor(napi_env env,napi_callback_info info)1994 napi_value NapiWebMessagePort::JsConstructor(napi_env env, napi_callback_info info) 1995 { 1996 WVLOG_D("web message port construct"); 1997 napi_value thisVar = nullptr; 1998 size_t argc = INTEGER_THREE; 1999 napi_value argv[INTEGER_THREE] = {0}; 2000 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2001 2002 int32_t webId = -1; 2003 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], webId)) { 2004 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 2005 return nullptr; 2006 } 2007 2008 std::string portHandle; 2009 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], portHandle)) { 2010 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 2011 return nullptr; 2012 } 2013 2014 bool isExtentionType = false; 2015 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_TWO], isExtentionType)) { 2016 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 2017 return nullptr; 2018 } 2019 2020 WebMessagePort *msgPort = new (std::nothrow) WebMessagePort(webId, portHandle, isExtentionType); 2021 if (msgPort == nullptr) { 2022 WVLOG_E("new msg port failed"); 2023 return nullptr; 2024 } 2025 NAPI_CALL(env, napi_wrap(env, thisVar, msgPort, 2026 [](napi_env env, void *data, void *hint) { 2027 WebMessagePort *msgPort = static_cast<WebMessagePort *>(data); 2028 delete msgPort; 2029 }, 2030 nullptr, nullptr)); 2031 return thisVar; 2032 } 2033 Close(napi_env env,napi_callback_info info)2034 napi_value NapiWebMessagePort::Close(napi_env env, napi_callback_info info) 2035 { 2036 WVLOG_D("close message port"); 2037 napi_value thisVar = nullptr; 2038 napi_value result = nullptr; 2039 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); 2040 2041 WebMessagePort *msgPort = nullptr; 2042 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort)); 2043 if (msgPort == nullptr) { 2044 WVLOG_E("close message port failed, napi unwrap msg port failed"); 2045 return nullptr; 2046 } 2047 ErrCode ret = msgPort->ClosePort(); 2048 if (ret != NO_ERROR) { 2049 BusinessError::ThrowErrorByErrcode(env, ret); 2050 return result; 2051 } 2052 NAPI_CALL(env, napi_get_undefined(env, &result)); 2053 2054 return result; 2055 } 2056 PostMessageEventMsgHandler(napi_env env,napi_value argv,napi_valuetype valueType,bool isArrayBuffer,std::shared_ptr<NWebMessage> webMsg)2057 bool PostMessageEventMsgHandler(napi_env env, napi_value argv, napi_valuetype valueType, bool isArrayBuffer, 2058 std::shared_ptr<NWebMessage> webMsg) 2059 { 2060 if (valueType == napi_string) { 2061 size_t bufferSize = 0; 2062 napi_get_value_string_utf8(env, argv, nullptr, 0, &bufferSize); 2063 if (bufferSize > UINT_MAX) { 2064 WVLOG_E("String length is too long"); 2065 return false; 2066 } 2067 char* stringValue = new (std::nothrow) char[bufferSize + 1]; 2068 if (stringValue == nullptr) { 2069 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 2070 return false; 2071 } 2072 size_t jsStringLength = 0; 2073 napi_get_value_string_utf8(env, argv, stringValue, bufferSize + 1, &jsStringLength); 2074 std::string message(stringValue); 2075 delete [] stringValue; 2076 stringValue = nullptr; 2077 2078 webMsg->SetType(NWebValue::Type::STRING); 2079 webMsg->SetString(message); 2080 } else if (isArrayBuffer) { 2081 uint8_t *arrBuf = nullptr; 2082 size_t byteLength = 0; 2083 napi_get_arraybuffer_info(env, argv, (void**)&arrBuf, &byteLength); 2084 std::vector<uint8_t> vecData(arrBuf, arrBuf + byteLength); 2085 webMsg->SetType(NWebValue::Type::BINARY); 2086 webMsg->SetBinary(vecData); 2087 } 2088 return true; 2089 } 2090 PostMessageEvent(napi_env env,napi_callback_info info)2091 napi_value NapiWebMessagePort::PostMessageEvent(napi_env env, napi_callback_info info) 2092 { 2093 WVLOG_D("message port post message"); 2094 napi_value thisVar = nullptr; 2095 napi_value result = nullptr; 2096 size_t argc = INTEGER_ONE; 2097 napi_value argv[INTEGER_ONE]; 2098 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 2099 if (argc != INTEGER_ONE) { 2100 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2101 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 2102 return result; 2103 } 2104 napi_valuetype valueType = napi_undefined; 2105 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 2106 2107 bool isArrayBuffer = false; 2108 NAPI_CALL(env, napi_is_arraybuffer(env, argv[INTEGER_ZERO], &isArrayBuffer)); 2109 if (valueType != napi_string && !isArrayBuffer) { 2110 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2111 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 2112 return result; 2113 } 2114 2115 auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE); 2116 if (!PostMessageEventMsgHandler(env, argv[INTEGER_ZERO], valueType, isArrayBuffer, webMsg)) { 2117 WVLOG_E("post message failed, PostMessageEventMsgHandler failed"); 2118 return result; 2119 } 2120 2121 WebMessagePort *msgPort = nullptr; 2122 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort)); 2123 if (msgPort == nullptr) { 2124 WVLOG_E("post message failed, napi unwrap msg port failed"); 2125 return nullptr; 2126 } 2127 ErrCode ret = msgPort->PostPortMessage(webMsg); 2128 if (ret != NO_ERROR) { 2129 BusinessError::ThrowErrorByErrcode(env, ret); 2130 return result; 2131 } 2132 NAPI_CALL(env, napi_get_undefined(env, &result)); 2133 2134 return result; 2135 } 2136 PostMessageEventExt(napi_env env,napi_callback_info info)2137 napi_value NapiWebMessagePort::PostMessageEventExt(napi_env env, napi_callback_info info) 2138 { 2139 WVLOG_D("message PostMessageEventExt start"); 2140 napi_value thisVar = nullptr; 2141 napi_value result = nullptr; 2142 size_t argc = INTEGER_ONE; 2143 napi_value argv[INTEGER_ONE]; 2144 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 2145 if (argc != INTEGER_ONE) { 2146 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2147 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 2148 return result; 2149 } 2150 napi_valuetype valueType = napi_undefined; 2151 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 2152 if (valueType != napi_object) { 2153 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2154 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 2155 return result; 2156 } 2157 2158 WebMessageExt *webMessageExt = nullptr; 2159 NAPI_CALL(env, napi_unwrap(env, argv[INTEGER_ZERO], (void **)&webMessageExt)); 2160 if (webMessageExt == nullptr) { 2161 WVLOG_E("post message failed, napi unwrap msg port failed"); 2162 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2163 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NOT_NULL, "message")); 2164 return nullptr; 2165 } 2166 2167 WebMessagePort *msgPort = nullptr; 2168 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort)); 2169 if (msgPort == nullptr) { 2170 WVLOG_E("post message failed, napi unwrap msg port failed"); 2171 return nullptr; 2172 } 2173 2174 if (!msgPort->IsExtentionType()) { 2175 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2176 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "message")); 2177 return result; 2178 } 2179 2180 ErrCode ret = msgPort->PostPortMessage(webMessageExt->GetData()); 2181 if (ret != NO_ERROR) { 2182 BusinessError::ThrowErrorByErrcode(env, ret); 2183 return result; 2184 } 2185 NAPI_CALL(env, napi_get_undefined(env, &result)); 2186 2187 return result; 2188 } 2189 2190 OnMessageEventExt(napi_env env,napi_callback_info info)2191 napi_value NapiWebMessagePort::OnMessageEventExt(napi_env env, napi_callback_info info) 2192 { 2193 WVLOG_D("message port set OnMessageEventExt callback"); 2194 napi_value thisVar = nullptr; 2195 napi_value result = nullptr; 2196 size_t argc = INTEGER_ONE; 2197 napi_value argv[INTEGER_ONE]; 2198 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 2199 if (argc != INTEGER_ONE) { 2200 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2201 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 2202 return result; 2203 } 2204 napi_valuetype valueType = napi_undefined; 2205 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 2206 if (valueType != napi_function) { 2207 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2208 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function")); 2209 return result; 2210 } 2211 2212 napi_ref onMsgEventFunc = nullptr; 2213 NAPI_CALL(env, napi_create_reference(env, argv[INTEGER_ZERO], INTEGER_ONE, &onMsgEventFunc)); 2214 2215 auto callbackImpl = std::make_shared<NWebValueCallbackImpl>(env, onMsgEventFunc, true); 2216 2217 WebMessagePort *msgPort = nullptr; 2218 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort)); 2219 if (msgPort == nullptr) { 2220 WVLOG_E("set message event callback failed, napi unwrap msg port failed"); 2221 napi_delete_reference(env, onMsgEventFunc); 2222 return nullptr; 2223 } 2224 ErrCode ret = msgPort->SetPortMessageCallback(callbackImpl); 2225 if (ret != NO_ERROR) { 2226 BusinessError::ThrowErrorByErrcode(env, ret); 2227 } 2228 NAPI_CALL(env, napi_get_undefined(env, &result)); 2229 return result; 2230 } 2231 UvWebMsgOnReceiveCbDataHandler(NapiWebMessagePort::WebMsgPortParam * data,napi_value & result)2232 bool UvWebMsgOnReceiveCbDataHandler(NapiWebMessagePort::WebMsgPortParam *data, napi_value& result) 2233 { 2234 if (data->extention_) { 2235 napi_value webMsgExt = nullptr; 2236 napi_status status = napi_get_reference_value(data->env_, g_webMsgExtClassRef, &webMsgExt); 2237 if (status != napi_status::napi_ok) { 2238 WVLOG_E("napi_get_reference_value failed."); 2239 return false; 2240 } 2241 status = napi_new_instance(data->env_, webMsgExt, 0, NULL, &result); 2242 if (status != napi_status::napi_ok) { 2243 WVLOG_E("napi_new_instance failed."); 2244 return false; 2245 } 2246 2247 WebMessageExt *webMessageExt = new (std::nothrow) WebMessageExt(data->msg_); 2248 if (webMessageExt == nullptr) { 2249 WVLOG_E("new WebMessageExt failed."); 2250 return false; 2251 } 2252 2253 status = napi_wrap(data->env_, result, webMessageExt, 2254 [](napi_env env, void *data, void *hint) { 2255 WebMessageExt *webMessageExt = static_cast<WebMessageExt *>(data); 2256 delete webMessageExt; 2257 }, 2258 nullptr, nullptr); 2259 if (status != napi_status::napi_ok) { 2260 WVLOG_E("napi_wrap failed."); 2261 return false; 2262 } 2263 } else { 2264 NapiParseUtils::ConvertNWebToNapiValue(data->env_, data->msg_, result); 2265 } 2266 return true; 2267 } 2268 UvWebMessageOnReceiveValueCallback(uv_work_t * work,int status)2269 void NWebValueCallbackImpl::UvWebMessageOnReceiveValueCallback(uv_work_t *work, int status) 2270 { 2271 if (work == nullptr) { 2272 WVLOG_E("uv work is null"); 2273 return; 2274 } 2275 NapiWebMessagePort::WebMsgPortParam *data = reinterpret_cast<NapiWebMessagePort::WebMsgPortParam*>(work->data); 2276 if (data == nullptr) { 2277 WVLOG_E("WebMsgPortParam is null"); 2278 delete work; 2279 work = nullptr; 2280 return; 2281 } 2282 napi_handle_scope scope = nullptr; 2283 napi_open_handle_scope(data->env_, &scope); 2284 if (scope == nullptr) { 2285 delete work; 2286 work = nullptr; 2287 return; 2288 } 2289 napi_value result[INTEGER_ONE] = {0}; 2290 if (!UvWebMsgOnReceiveCbDataHandler(data, result[INTEGER_ZERO])) { 2291 delete work; 2292 work = nullptr; 2293 napi_close_handle_scope(data->env_, scope); 2294 return; 2295 } 2296 2297 napi_value onMsgEventFunc = nullptr; 2298 napi_get_reference_value(data->env_, data->callback_, &onMsgEventFunc); 2299 napi_value placeHodler = nullptr; 2300 napi_call_function(data->env_, nullptr, onMsgEventFunc, INTEGER_ONE, &result[INTEGER_ZERO], &placeHodler); 2301 2302 std::unique_lock<std::mutex> lock(data->mutex_); 2303 data->ready_ = true; 2304 data->condition_.notify_all(); 2305 napi_close_handle_scope(data->env_, scope); 2306 } 2307 InvokeWebMessageCallback(NapiWebMessagePort::WebMsgPortParam * data)2308 static void InvokeWebMessageCallback(NapiWebMessagePort::WebMsgPortParam *data) 2309 { 2310 napi_handle_scope scope = nullptr; 2311 napi_open_handle_scope(data->env_, &scope); 2312 if (scope == nullptr) { 2313 WVLOG_E("scope is null"); 2314 return; 2315 } 2316 napi_value result[INTEGER_ONE] = {0}; 2317 if (!UvWebMsgOnReceiveCbDataHandler(data, result[INTEGER_ZERO])) { 2318 WVLOG_E("get result failed"); 2319 napi_close_handle_scope(data->env_, scope); 2320 return; 2321 } 2322 2323 napi_value onMsgEventFunc = nullptr; 2324 napi_get_reference_value(data->env_, data->callback_, &onMsgEventFunc); 2325 napi_value placeHodler = nullptr; 2326 napi_call_function(data->env_, nullptr, onMsgEventFunc, INTEGER_ONE, &result[INTEGER_ZERO], &placeHodler); 2327 2328 napi_close_handle_scope(data->env_, scope); 2329 } 2330 OnReceiveValue(std::shared_ptr<NWebMessage> result)2331 void NWebValueCallbackImpl::OnReceiveValue(std::shared_ptr<NWebMessage> result) 2332 { 2333 WVLOG_D("message port received msg"); 2334 uv_loop_s *loop = nullptr; 2335 uv_work_t *work = nullptr; 2336 napi_get_uv_event_loop(env_, &loop); 2337 auto engine = reinterpret_cast<NativeEngine*>(env_); 2338 if (loop == nullptr) { 2339 WVLOG_E("get uv event loop failed"); 2340 return; 2341 } 2342 work = new (std::nothrow) uv_work_t; 2343 if (work == nullptr) { 2344 WVLOG_E("new uv work failed"); 2345 return; 2346 } 2347 NapiWebMessagePort::WebMsgPortParam *param = new (std::nothrow) NapiWebMessagePort::WebMsgPortParam(); 2348 if (param == nullptr) { 2349 WVLOG_E("new WebMsgPortParam failed"); 2350 delete work; 2351 return; 2352 } 2353 param->env_ = env_; 2354 param->callback_ = callback_; 2355 param->msg_ = result; 2356 param->extention_ = extention_; 2357 if (pthread_self() == engine->GetTid()) { 2358 InvokeWebMessageCallback(param); 2359 } else { 2360 work->data = reinterpret_cast<void*>(param); 2361 uv_queue_work_with_qos( 2362 loop, work, [](uv_work_t* work) {}, UvWebMessageOnReceiveValueCallback, uv_qos_user_initiated); 2363 2364 { 2365 std::unique_lock<std::mutex> lock(param->mutex_); 2366 param->condition_.wait(lock, [¶m] { return param->ready_; }); 2367 } 2368 } 2369 2370 if (param != nullptr) { 2371 delete param; 2372 param = nullptr; 2373 } 2374 if (work != nullptr) { 2375 delete work; 2376 work = nullptr; 2377 } 2378 } 2379 UvNWebValueCallbackImplThreadWoker(uv_work_t * work,int status)2380 void UvNWebValueCallbackImplThreadWoker(uv_work_t *work, int status) 2381 { 2382 if (work == nullptr) { 2383 WVLOG_E("uv work is null"); 2384 return; 2385 } 2386 NapiWebMessagePort::WebMsgPortParam *data = reinterpret_cast<NapiWebMessagePort::WebMsgPortParam*>(work->data); 2387 if (data == nullptr) { 2388 WVLOG_E("WebMsgPortParam is null"); 2389 delete work; 2390 return; 2391 } 2392 2393 napi_delete_reference(data->env_, data->callback_); 2394 delete data; 2395 data = nullptr; 2396 delete work; 2397 work = nullptr; 2398 } 2399 ~NWebValueCallbackImpl()2400 NWebValueCallbackImpl::~NWebValueCallbackImpl() 2401 { 2402 WVLOG_D("~NWebValueCallbackImpl"); 2403 uv_loop_s *loop = nullptr; 2404 uv_work_t *work = nullptr; 2405 napi_get_uv_event_loop(env_, &loop); 2406 if (loop == nullptr) { 2407 WVLOG_E("get uv event loop failed"); 2408 return; 2409 } 2410 work = new (std::nothrow) uv_work_t; 2411 if (work == nullptr) { 2412 WVLOG_E("new uv work failed"); 2413 return; 2414 } 2415 NapiWebMessagePort::WebMsgPortParam *param = new (std::nothrow) NapiWebMessagePort::WebMsgPortParam(); 2416 if (param == nullptr) { 2417 WVLOG_E("new WebMsgPortParam failed"); 2418 delete work; 2419 return; 2420 } 2421 param->env_ = env_; 2422 param->callback_ = callback_; 2423 work->data = reinterpret_cast<void*>(param); 2424 int ret = uv_queue_work_with_qos( 2425 loop, work, [](uv_work_t *work) {}, UvNWebValueCallbackImplThreadWoker, uv_qos_user_initiated); 2426 if (ret != 0) { 2427 if (param != nullptr) { 2428 delete param; 2429 param = nullptr; 2430 } 2431 if (work != nullptr) { 2432 delete work; 2433 work = nullptr; 2434 } 2435 } 2436 } 2437 OnMessageEvent(napi_env env,napi_callback_info info)2438 napi_value NapiWebMessagePort::OnMessageEvent(napi_env env, napi_callback_info info) 2439 { 2440 WVLOG_D("message port set OnMessageEvent callback"); 2441 napi_value thisVar = nullptr; 2442 napi_value result = nullptr; 2443 size_t argc = INTEGER_ONE; 2444 napi_value argv[INTEGER_ONE]; 2445 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 2446 if (argc != INTEGER_ONE) { 2447 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2448 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 2449 return result; 2450 } 2451 napi_valuetype valueType = napi_undefined; 2452 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 2453 if (valueType != napi_function) { 2454 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2455 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function")); 2456 return result; 2457 } 2458 2459 napi_ref onMsgEventFunc = nullptr; 2460 NAPI_CALL(env, napi_create_reference(env, argv[INTEGER_ZERO], INTEGER_ONE, &onMsgEventFunc)); 2461 2462 auto callbackImpl = std::make_shared<NWebValueCallbackImpl>(env, onMsgEventFunc, false); 2463 2464 WebMessagePort *msgPort = nullptr; 2465 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort)); 2466 if (msgPort == nullptr) { 2467 WVLOG_E("set message event callback failed, napi unwrap msg port failed"); 2468 return nullptr; 2469 } 2470 ErrCode ret = msgPort->SetPortMessageCallback(callbackImpl); 2471 if (ret != NO_ERROR) { 2472 BusinessError::ThrowErrorByErrcode(env, ret); 2473 } 2474 NAPI_CALL(env, napi_get_undefined(env, &result)); 2475 return result; 2476 } 2477 ZoomIn(napi_env env,napi_callback_info info)2478 napi_value NapiWebviewController::ZoomIn(napi_env env, napi_callback_info info) 2479 { 2480 napi_value result = nullptr; 2481 WebviewController *webviewController = GetWebviewController(env, info); 2482 if (!webviewController) { 2483 return nullptr; 2484 } 2485 2486 ErrCode ret = webviewController->ZoomIn(); 2487 if (ret != NO_ERROR) { 2488 if (ret == NWEB_ERROR) { 2489 WVLOG_E("ZoomIn failed."); 2490 return nullptr; 2491 } 2492 BusinessError::ThrowErrorByErrcode(env, ret); 2493 } 2494 2495 NAPI_CALL(env, napi_get_undefined(env, &result)); 2496 return result; 2497 } 2498 ZoomOut(napi_env env,napi_callback_info info)2499 napi_value NapiWebviewController::ZoomOut(napi_env env, napi_callback_info info) 2500 { 2501 napi_value result = nullptr; 2502 WebviewController *webviewController = GetWebviewController(env, info); 2503 if (!webviewController) { 2504 return nullptr; 2505 } 2506 2507 ErrCode ret = webviewController->ZoomOut(); 2508 if (ret != NO_ERROR) { 2509 if (ret == NWEB_ERROR) { 2510 WVLOG_E("ZoomOut failed."); 2511 return nullptr; 2512 } 2513 BusinessError::ThrowErrorByErrcode(env, ret); 2514 } 2515 2516 NAPI_CALL(env, napi_get_undefined(env, &result)); 2517 return result; 2518 } 2519 GetWebId(napi_env env,napi_callback_info info)2520 napi_value NapiWebviewController::GetWebId(napi_env env, napi_callback_info info) 2521 { 2522 napi_value result = nullptr; 2523 WebviewController *webviewController = GetWebviewController(env, info); 2524 if (!webviewController) { 2525 return nullptr; 2526 } 2527 2528 int32_t webId = webviewController->GetWebId(); 2529 napi_create_int32(env, webId, &result); 2530 2531 return result; 2532 } 2533 GetUserAgent(napi_env env,napi_callback_info info)2534 napi_value NapiWebviewController::GetUserAgent(napi_env env, napi_callback_info info) 2535 { 2536 napi_value result = nullptr; 2537 WebviewController *webviewController = GetWebviewController(env, info); 2538 if (!webviewController) { 2539 return nullptr; 2540 } 2541 2542 std::string userAgent = ""; 2543 userAgent = webviewController->GetUserAgent(); 2544 napi_create_string_utf8(env, userAgent.c_str(), userAgent.length(), &result); 2545 2546 return result; 2547 } 2548 GetCustomUserAgent(napi_env env,napi_callback_info info)2549 napi_value NapiWebviewController::GetCustomUserAgent(napi_env env, napi_callback_info info) 2550 { 2551 napi_value thisVar = nullptr; 2552 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 2553 2554 WebviewController *webviewController = nullptr; 2555 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 2556 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 2557 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2558 return nullptr; 2559 } 2560 2561 napi_value result = nullptr; 2562 std::string userAgent = webviewController->GetCustomUserAgent(); 2563 napi_create_string_utf8(env, userAgent.c_str(), userAgent.length(), &result); 2564 return result; 2565 } 2566 SetCustomUserAgent(napi_env env,napi_callback_info info)2567 napi_value NapiWebviewController::SetCustomUserAgent(napi_env env, napi_callback_info info) 2568 { 2569 napi_value thisVar = nullptr; 2570 napi_value result = nullptr; 2571 size_t argc = INTEGER_ONE; 2572 napi_value argv[INTEGER_ONE]; 2573 NAPI_CALL(env, napi_get_undefined(env, &result)); 2574 2575 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2576 if (argc != INTEGER_ONE) { 2577 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2578 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 2579 return result; 2580 } 2581 2582 std::string userAgent; 2583 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], userAgent)) { 2584 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2585 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "userAgent", "string")); 2586 return result; 2587 } 2588 2589 WebviewController *webviewController = nullptr; 2590 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 2591 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 2592 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2593 return result; 2594 } 2595 ErrCode ret = webviewController->SetCustomUserAgent(userAgent); 2596 if (ret != NO_ERROR) { 2597 BusinessError::ThrowErrorByErrcode(env, ret); 2598 } 2599 return result; 2600 } 2601 GetTitle(napi_env env,napi_callback_info info)2602 napi_value NapiWebviewController::GetTitle(napi_env env, napi_callback_info info) 2603 { 2604 napi_value result = nullptr; 2605 WebviewController *webviewController = GetWebviewController(env, info); 2606 if (!webviewController) { 2607 return nullptr; 2608 } 2609 2610 std::string title = ""; 2611 title = webviewController->GetTitle(); 2612 napi_create_string_utf8(env, title.c_str(), title.length(), &result); 2613 2614 return result; 2615 } 2616 GetPageHeight(napi_env env,napi_callback_info info)2617 napi_value NapiWebviewController::GetPageHeight(napi_env env, napi_callback_info info) 2618 { 2619 napi_value result = nullptr; 2620 WebviewController *webviewController = GetWebviewController(env, info); 2621 if (!webviewController) { 2622 return nullptr; 2623 } 2624 2625 int32_t pageHeight = webviewController->GetPageHeight(); 2626 napi_create_int32(env, pageHeight, &result); 2627 2628 return result; 2629 } 2630 BackOrForward(napi_env env,napi_callback_info info)2631 napi_value NapiWebviewController::BackOrForward(napi_env env, napi_callback_info info) 2632 { 2633 napi_value thisVar = nullptr; 2634 napi_value result = nullptr; 2635 size_t argc = INTEGER_ONE; 2636 napi_value argv[INTEGER_ONE] = {0}; 2637 void* data = nullptr; 2638 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 2639 2640 if (argc != INTEGER_ONE) { 2641 WVLOG_E("Requires 1 parameters."); 2642 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2643 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 2644 return nullptr; 2645 } 2646 2647 int32_t step = -1; 2648 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], step)) { 2649 WVLOG_E("Parameter is not integer number type."); 2650 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2651 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "step", "number")); 2652 return nullptr; 2653 } 2654 2655 WebviewController *webviewController = nullptr; 2656 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 2657 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 2658 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2659 return nullptr; 2660 } 2661 2662 ErrCode ret = webviewController->BackOrForward(step); 2663 if (ret != NO_ERROR) { 2664 BusinessError::ThrowErrorByErrcode(env, ret); 2665 } 2666 2667 NAPI_CALL(env, napi_get_undefined(env, &result)); 2668 return result; 2669 } 2670 StoreWebArchive(napi_env env,napi_callback_info info)2671 napi_value NapiWebviewController::StoreWebArchive(napi_env env, napi_callback_info info) 2672 { 2673 napi_value thisVar = nullptr; 2674 napi_value result = nullptr; 2675 size_t argc = INTEGER_ONE; 2676 size_t argcPromise = INTEGER_TWO; 2677 size_t argcCallback = INTEGER_THREE; 2678 napi_value argv[INTEGER_THREE] = { 0 }; 2679 2680 napi_get_undefined(env, &result); 2681 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2682 2683 if (argc != argcPromise && argc != argcCallback) { 2684 BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 2685 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "two", "three")); 2686 return result; 2687 } 2688 std::string baseName; 2689 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], baseName)) { 2690 BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 2691 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "baseName", "string")); 2692 return result; 2693 } 2694 2695 if (baseName.empty()) { 2696 BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 2697 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NOT_NULL, "baseName")); 2698 return result; 2699 } 2700 2701 bool autoName = false; 2702 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2703 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ONE], autoName)) { 2704 BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 2705 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "autoName", "boolean")); 2706 return result; 2707 } 2708 2709 if (argc == argcCallback) { 2710 napi_valuetype valueType = napi_null; 2711 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2712 napi_typeof(env, argv[argcCallback - 1], &valueType); 2713 if (valueType != napi_function) { 2714 BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 2715 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function")); 2716 return result; 2717 } 2718 } 2719 return StoreWebArchiveInternal(env, info, baseName, autoName); 2720 } 2721 StoreWebArchiveInternal(napi_env env,napi_callback_info info,const std::string & baseName,bool autoName)2722 napi_value NapiWebviewController::StoreWebArchiveInternal(napi_env env, napi_callback_info info, 2723 const std::string &baseName, bool autoName) 2724 { 2725 napi_value thisVar = nullptr; 2726 size_t argc = INTEGER_ONE; 2727 size_t argcPromise = INTEGER_TWO; 2728 size_t argcCallback = INTEGER_THREE; 2729 napi_value argv[INTEGER_THREE] = {0}; 2730 2731 napi_value result = nullptr; 2732 napi_get_undefined(env, &result); 2733 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2734 2735 WebviewController *webviewController = nullptr; 2736 napi_unwrap(env, thisVar, (void **)&webviewController); 2737 2738 if (!webviewController || !webviewController->IsInit()) { 2739 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2740 return result; 2741 } 2742 2743 if (argc == argcCallback) { 2744 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2745 napi_ref jsCallback = nullptr; 2746 napi_create_reference(env, argv[argcCallback - 1], 1, &jsCallback); 2747 2748 if (jsCallback) { 2749 webviewController->StoreWebArchiveCallback(baseName, autoName, env, std::move(jsCallback)); 2750 } 2751 return result; 2752 } else if (argc == argcPromise) { 2753 napi_deferred deferred = nullptr; 2754 napi_value promise = nullptr; 2755 napi_create_promise(env, &deferred, &promise); 2756 if (promise && deferred) { 2757 webviewController->StoreWebArchivePromise(baseName, autoName, env, deferred); 2758 } 2759 return promise; 2760 } 2761 return result; 2762 } 2763 GetHitTestValue(napi_env env,napi_callback_info info)2764 napi_value NapiWebviewController::GetHitTestValue(napi_env env, napi_callback_info info) 2765 { 2766 napi_value result = nullptr; 2767 WebviewController *webviewController = GetWebviewController(env, info); 2768 if (!webviewController) { 2769 return nullptr; 2770 } 2771 2772 std::shared_ptr<HitTestResult> nwebResult = webviewController->GetHitTestValue(); 2773 2774 napi_create_object(env, &result); 2775 2776 napi_value type; 2777 if (nwebResult) { 2778 napi_create_uint32(env, nwebResult->GetType(), &type); 2779 } else { 2780 napi_create_uint32(env, HitTestResult::UNKNOWN_TYPE, &type); 2781 } 2782 napi_set_named_property(env, result, "type", type); 2783 2784 napi_value extra; 2785 if (nwebResult) { 2786 napi_create_string_utf8(env, nwebResult->GetExtra().c_str(), NAPI_AUTO_LENGTH, &extra); 2787 } else { 2788 napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &extra); 2789 } 2790 napi_set_named_property(env, result, "extra", extra); 2791 2792 return result; 2793 } 2794 RequestFocus(napi_env env,napi_callback_info info)2795 napi_value NapiWebviewController::RequestFocus(napi_env env, napi_callback_info info) 2796 { 2797 napi_value result = nullptr; 2798 WebviewController *webviewController = GetWebviewController(env, info); 2799 if (!webviewController) { 2800 return nullptr; 2801 } 2802 2803 webviewController->RequestFocus(); 2804 NAPI_CALL(env, napi_get_undefined(env, &result)); 2805 return result; 2806 } 2807 PostUrl(napi_env env,napi_callback_info info)2808 napi_value NapiWebviewController::PostUrl(napi_env env, napi_callback_info info) 2809 { 2810 WVLOG_D("NapiWebMessageExt::PostUrl start"); 2811 napi_value thisVar = nullptr; 2812 napi_value result = nullptr; 2813 size_t argc = INTEGER_TWO; 2814 napi_value argv[INTEGER_TWO] = { 0 }; 2815 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2816 if (argc != INTEGER_TWO) { 2817 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2818 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "two")); 2819 return result; 2820 } 2821 2822 WebviewController *webviewController = nullptr; 2823 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 2824 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 2825 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2826 return nullptr; 2827 } 2828 2829 std::string url; 2830 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], url)) { 2831 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2832 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "url", "string")); 2833 return result; 2834 } 2835 2836 bool isArrayBuffer = false; 2837 NAPI_CALL(env, napi_is_arraybuffer(env, argv[INTEGER_ONE], &isArrayBuffer)); 2838 if (!isArrayBuffer) { 2839 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2840 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "postData", "array")); 2841 return result; 2842 } 2843 2844 char *arrBuf = nullptr; 2845 size_t byteLength = 0; 2846 napi_get_arraybuffer_info(env, argv[INTEGER_ONE], (void **)&arrBuf, &byteLength); 2847 2848 std::vector<char> postData(arrBuf, arrBuf + byteLength); 2849 ErrCode ret = webviewController->PostUrl(url, postData); 2850 if (ret != NO_ERROR) { 2851 if (ret == NWEB_ERROR) { 2852 WVLOG_E("PostData failed"); 2853 return result; 2854 } 2855 BusinessError::ThrowErrorByErrcode(env, ret); 2856 return result; 2857 } 2858 NAPI_CALL(env, napi_get_undefined(env, &result)); 2859 return result; 2860 } 2861 LoadUrl(napi_env env,napi_callback_info info)2862 napi_value NapiWebviewController::LoadUrl(napi_env env, napi_callback_info info) 2863 { 2864 napi_value thisVar = nullptr; 2865 napi_value result = nullptr; 2866 size_t argc = INTEGER_TWO; 2867 napi_value argv[INTEGER_TWO]; 2868 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2869 if ((argc != INTEGER_ONE) && (argc != INTEGER_TWO)) { 2870 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2871 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "one", "two")); 2872 return nullptr; 2873 } 2874 WebviewController *webviewController = nullptr; 2875 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 2876 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 2877 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2878 return nullptr; 2879 } 2880 napi_valuetype webSrcType; 2881 napi_typeof(env, argv[INTEGER_ZERO], &webSrcType); 2882 if (webSrcType != napi_string && webSrcType != napi_object) { 2883 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2884 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "url")); 2885 return nullptr; 2886 } 2887 std::string webSrc; 2888 if (!webviewController->ParseUrl(env, argv[INTEGER_ZERO], webSrc)) { 2889 BusinessError::ThrowErrorByErrcode(env, INVALID_URL); 2890 return nullptr; 2891 } 2892 if (argc == INTEGER_ONE) { 2893 ErrCode ret = webviewController->LoadUrl(webSrc); 2894 if (ret != NO_ERROR) { 2895 if (ret == NWEB_ERROR) { 2896 return nullptr; 2897 } 2898 BusinessError::ThrowErrorByErrcode(env, ret); 2899 return nullptr; 2900 } 2901 NAPI_CALL(env, napi_get_undefined(env, &result)); 2902 return result; 2903 } 2904 return LoadUrlWithHttpHeaders(env, info, webSrc, argv, webviewController); 2905 } 2906 LoadUrlWithHttpHeaders(napi_env env,napi_callback_info info,const std::string & url,const napi_value * argv,WebviewController * webviewController)2907 napi_value NapiWebviewController::LoadUrlWithHttpHeaders(napi_env env, napi_callback_info info, const std::string& url, 2908 const napi_value* argv, WebviewController* webviewController) 2909 { 2910 napi_value result = nullptr; 2911 std::map<std::string, std::string> httpHeaders; 2912 napi_value array = argv[INTEGER_ONE]; 2913 bool isArray = false; 2914 napi_is_array(env, array, &isArray); 2915 if (isArray) { 2916 uint32_t arrayLength = INTEGER_ZERO; 2917 napi_get_array_length(env, array, &arrayLength); 2918 for (uint32_t i = 0; i < arrayLength; ++i) { 2919 std::string key; 2920 std::string value; 2921 napi_value obj = nullptr; 2922 napi_value keyObj = nullptr; 2923 napi_value valueObj = nullptr; 2924 napi_get_element(env, array, i, &obj); 2925 if (napi_get_named_property(env, obj, "headerKey", &keyObj) != napi_ok) { 2926 continue; 2927 } 2928 if (napi_get_named_property(env, obj, "headerValue", &valueObj) != napi_ok) { 2929 continue; 2930 } 2931 NapiParseUtils::ParseString(env, keyObj, key); 2932 NapiParseUtils::ParseString(env, valueObj, value); 2933 httpHeaders[key] = value; 2934 } 2935 } else { 2936 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 2937 return nullptr; 2938 } 2939 2940 ErrCode ret = webviewController->LoadUrl(url, httpHeaders); 2941 if (ret != NO_ERROR) { 2942 if (ret == NWEB_ERROR) { 2943 WVLOG_E("LoadUrl failed."); 2944 return nullptr; 2945 } 2946 BusinessError::ThrowErrorByErrcode(env, ret); 2947 return nullptr; 2948 } 2949 NAPI_CALL(env, napi_get_undefined(env, &result)); 2950 return result; 2951 } 2952 LoadData(napi_env env,napi_callback_info info)2953 napi_value NapiWebviewController::LoadData(napi_env env, napi_callback_info info) 2954 { 2955 napi_value thisVar = nullptr; 2956 napi_value result = nullptr; 2957 size_t argc = INTEGER_FIVE; 2958 napi_value argv[INTEGER_FIVE]; 2959 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 2960 if ((argc != INTEGER_THREE) && (argc != INTEGER_FOUR) && 2961 (argc != INTEGER_FIVE)) { 2962 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 2963 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "three", "four")); 2964 return nullptr; 2965 } 2966 WebviewController *webviewController = nullptr; 2967 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 2968 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 2969 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 2970 return nullptr; 2971 } 2972 std::string data; 2973 std::string mimeType; 2974 std::string encoding; 2975 std::string baseUrl; 2976 std::string historyUrl; 2977 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], data) || 2978 !NapiParseUtils::ParseString(env, argv[INTEGER_ONE], mimeType) || 2979 !NapiParseUtils::ParseString(env, argv[INTEGER_TWO], encoding)) { 2980 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, ParamCheckErrorMsgTemplate::TYPE_ALL_STRING); 2981 return nullptr; 2982 } 2983 if ((argc >= INTEGER_FOUR) && !NapiParseUtils::ParseString(env, argv[INTEGER_THREE], baseUrl)) { 2984 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, ParamCheckErrorMsgTemplate::TYPE_ALL_STRING); 2985 return nullptr; 2986 } 2987 if ((argc == INTEGER_FIVE) && !NapiParseUtils::ParseString(env, argv[INTEGER_FOUR], historyUrl)) { 2988 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, ParamCheckErrorMsgTemplate::TYPE_ALL_STRING); 2989 return nullptr; 2990 } 2991 ErrCode ret = webviewController->LoadData(data, mimeType, encoding, baseUrl, historyUrl); 2992 if (ret != NO_ERROR) { 2993 if (ret == NWEB_ERROR) { 2994 WVLOG_E("LoadData failed."); 2995 return nullptr; 2996 } 2997 BusinessError::ThrowErrorByErrcode(env, ret); 2998 return nullptr; 2999 } 3000 NAPI_CALL(env, napi_get_undefined(env, &result)); 3001 return result; 3002 } 3003 GetHitTest(napi_env env,napi_callback_info info)3004 napi_value NapiWebviewController::GetHitTest(napi_env env, napi_callback_info info) 3005 { 3006 napi_value result = nullptr; 3007 WebviewController *webviewController = GetWebviewController(env, info); 3008 if (!webviewController) { 3009 return nullptr; 3010 } 3011 3012 int32_t type = webviewController->GetHitTest(); 3013 napi_create_int32(env, type, &result); 3014 return result; 3015 } 3016 ClearMatches(napi_env env,napi_callback_info info)3017 napi_value NapiWebviewController::ClearMatches(napi_env env, napi_callback_info info) 3018 { 3019 napi_value thisVar = nullptr; 3020 napi_value result = nullptr; 3021 3022 NAPI_CALL(env, napi_get_undefined(env, &result)); 3023 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3024 3025 WebviewController *controller = nullptr; 3026 napi_unwrap(env, thisVar, (void **)&controller); 3027 if (!controller || !controller->IsInit()) { 3028 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3029 return result; 3030 } 3031 controller->ClearMatches(); 3032 return result; 3033 } 3034 SearchNext(napi_env env,napi_callback_info info)3035 napi_value NapiWebviewController::SearchNext(napi_env env, napi_callback_info info) 3036 { 3037 napi_value thisVar = nullptr; 3038 napi_value result = nullptr; 3039 size_t argc = INTEGER_ONE; 3040 napi_value argv[INTEGER_ONE] = { 0 }; 3041 3042 NAPI_CALL(env, napi_get_undefined(env, &result)); 3043 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3044 if (argc != INTEGER_ONE) { 3045 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3046 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3047 return result; 3048 } 3049 bool forward; 3050 if (!NapiParseUtils::ParseBoolean(env, argv[0], forward)) { 3051 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3052 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "forward", "boolean")); 3053 return result; 3054 } 3055 3056 WebviewController *controller = nullptr; 3057 napi_unwrap(env, thisVar, (void **)&controller); 3058 if (!controller || !controller->IsInit()) { 3059 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3060 return result; 3061 } 3062 controller->SearchNext(forward); 3063 return result; 3064 } 3065 SearchAllAsync(napi_env env,napi_callback_info info)3066 napi_value NapiWebviewController::SearchAllAsync(napi_env env, napi_callback_info info) 3067 { 3068 napi_value thisVar = nullptr; 3069 napi_value result = nullptr; 3070 size_t argc = INTEGER_ONE; 3071 napi_value argv[INTEGER_ONE] = { 0 }; 3072 3073 NAPI_CALL(env, napi_get_undefined(env, &result)); 3074 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3075 if (argc != INTEGER_ONE) { 3076 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3077 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3078 return result; 3079 } 3080 std::string searchString; 3081 if (!NapiParseUtils::ParseString(env, argv[0], searchString)) { 3082 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3083 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "searchString", "number")); 3084 return result; 3085 } 3086 3087 WebviewController *controller = nullptr; 3088 napi_unwrap(env, thisVar, (void **)&controller); 3089 if (!controller || !controller->IsInit()) { 3090 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3091 return result; 3092 } 3093 controller->SearchAllAsync(searchString); 3094 return result; 3095 } 3096 ClearSslCache(napi_env env,napi_callback_info info)3097 napi_value NapiWebviewController::ClearSslCache(napi_env env, napi_callback_info info) 3098 { 3099 napi_value thisVar = nullptr; 3100 napi_value result = nullptr; 3101 3102 NAPI_CALL(env, napi_get_undefined(env, &result)); 3103 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3104 3105 WebviewController *controller = nullptr; 3106 napi_unwrap(env, thisVar, (void **)&controller); 3107 if (!controller || !controller->IsInit()) { 3108 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3109 return result; 3110 } 3111 controller->ClearSslCache(); 3112 return result; 3113 } 3114 ClearClientAuthenticationCache(napi_env env,napi_callback_info info)3115 napi_value NapiWebviewController::ClearClientAuthenticationCache(napi_env env, napi_callback_info info) 3116 { 3117 napi_value thisVar = nullptr; 3118 napi_value result = nullptr; 3119 3120 NAPI_CALL(env, napi_get_undefined(env, &result)); 3121 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3122 3123 WebviewController *controller = nullptr; 3124 napi_unwrap(env, thisVar, (void **)&controller); 3125 if (!controller || !controller->IsInit()) { 3126 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3127 return result; 3128 } 3129 controller->ClearClientAuthenticationCache(); 3130 3131 return result; 3132 } 3133 Stop(napi_env env,napi_callback_info info)3134 napi_value NapiWebviewController::Stop(napi_env env, napi_callback_info info) 3135 { 3136 napi_value thisVar = nullptr; 3137 napi_value result = nullptr; 3138 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3139 3140 WebviewController *controller = nullptr; 3141 napi_unwrap(env, thisVar, (void **)&controller); 3142 if (!controller || !controller->IsInit()) { 3143 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3144 return result; 3145 } 3146 controller->Stop(); 3147 3148 NAPI_CALL(env, napi_get_undefined(env, &result)); 3149 return result; 3150 } 3151 Zoom(napi_env env,napi_callback_info info)3152 napi_value NapiWebviewController::Zoom(napi_env env, napi_callback_info info) 3153 { 3154 napi_value thisVar = nullptr; 3155 napi_value result = nullptr; 3156 size_t argc = INTEGER_ONE; 3157 napi_value argv[INTEGER_ONE] = { 0 }; 3158 3159 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3160 if (argc != INTEGER_ONE) { 3161 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3162 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3163 return result; 3164 } 3165 float factor = 0.0; 3166 if (!NapiParseUtils::ParseFloat(env, argv[0], factor)) { 3167 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3168 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "factor", "number")); 3169 return result; 3170 } 3171 3172 WebviewController *controller = nullptr; 3173 napi_unwrap(env, thisVar, (void **)&controller); 3174 if (!controller || !controller->IsInit()) { 3175 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3176 return result; 3177 } 3178 3179 ErrCode ret = controller->Zoom(factor); 3180 if (ret != NO_ERROR) { 3181 if (ret == NWEB_ERROR) { 3182 WVLOG_E("Zoom failed."); 3183 return result; 3184 } 3185 BusinessError::ThrowErrorByErrcode(env, ret); 3186 } 3187 3188 NAPI_CALL(env, napi_get_undefined(env, &result)); 3189 return result; 3190 } 3191 InnerCompleteWindowNew(napi_env env,napi_callback_info info)3192 napi_value NapiWebviewController::InnerCompleteWindowNew(napi_env env, napi_callback_info info) 3193 { 3194 napi_value thisVar = nullptr; 3195 size_t argc = INTEGER_ONE; 3196 napi_value argv[INTEGER_ONE]; 3197 void* data = nullptr; 3198 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 3199 3200 int32_t parentNwebId = -1; 3201 if (!NapiParseUtils::ParseInt32(env, argv[0], parentNwebId) || parentNwebId == -1) { 3202 WVLOG_E("Parse parent nweb id failed."); 3203 return nullptr; 3204 } 3205 WebviewController* webviewController = nullptr; 3206 napi_status status = napi_unwrap(env, thisVar, (void**)&webviewController); 3207 if ((!webviewController) || (status != napi_ok)) { 3208 WVLOG_E("webviewController is nullptr."); 3209 return nullptr; 3210 } 3211 webviewController->InnerCompleteWindowNew(parentNwebId); 3212 return thisVar; 3213 } 3214 RegisterJavaScriptProxy(napi_env env,napi_callback_info info)3215 napi_value NapiWebviewController::RegisterJavaScriptProxy(napi_env env, napi_callback_info info) 3216 { 3217 napi_value thisVar = nullptr; 3218 napi_value result = nullptr; 3219 size_t argc = INTEGER_FIVE; 3220 napi_value argv[INTEGER_FIVE] = { 0 }; 3221 napi_get_undefined(env, &result); 3222 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3223 if (argc != INTEGER_THREE && argc != INTEGER_FOUR && argc != INTEGER_FIVE) { 3224 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3225 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_THREE, "three", "four", "five")); 3226 return result; 3227 } 3228 napi_valuetype valueType = napi_undefined; 3229 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 3230 if (valueType != napi_object) { 3231 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3232 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "object", "object")); 3233 return result; 3234 } 3235 RegisterJavaScriptProxyParam param; 3236 if (!ParseRegisterJavaScriptProxyParam(env, argc, argv, ¶m)) { 3237 return result; 3238 } 3239 WebviewController* controller = nullptr; 3240 napi_unwrap(env, thisVar, (void **)&controller); 3241 if (!controller || !controller->IsInit()) { 3242 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3243 return result; 3244 } 3245 controller->SetNWebJavaScriptResultCallBack(); 3246 controller->RegisterJavaScriptProxy(param); 3247 return result; 3248 } 3249 DeleteJavaScriptRegister(napi_env env,napi_callback_info info)3250 napi_value NapiWebviewController::DeleteJavaScriptRegister(napi_env env, napi_callback_info info) 3251 { 3252 napi_value thisVar = nullptr; 3253 napi_value result = nullptr; 3254 size_t argc = INTEGER_ONE; 3255 napi_value argv[INTEGER_ONE] = { 0 }; 3256 3257 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3258 if (argc != INTEGER_ONE) { 3259 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3260 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3261 return result; 3262 } 3263 3264 std::string objName; 3265 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], objName)) { 3266 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3267 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "name", "string")); 3268 return result; 3269 } 3270 3271 WebviewController *controller = nullptr; 3272 napi_unwrap(env, thisVar, (void **)&controller); 3273 if (!controller || !controller->IsInit()) { 3274 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3275 return result; 3276 } 3277 ErrCode ret = controller->DeleteJavaScriptRegister(objName, {}); 3278 if (ret != NO_ERROR) { 3279 BusinessError::ThrowErrorByErrcode(env, ret); 3280 return result; 3281 } 3282 3283 NAPI_CALL(env, napi_get_undefined(env, &result)); 3284 return result; 3285 } 3286 RunJavaScript(napi_env env,napi_callback_info info)3287 napi_value NapiWebviewController::RunJavaScript(napi_env env, napi_callback_info info) 3288 { 3289 return RunJS(env, info, false); 3290 } 3291 RunJavaScriptExt(napi_env env,napi_callback_info info)3292 napi_value NapiWebviewController::RunJavaScriptExt(napi_env env, napi_callback_info info) 3293 { 3294 return RunJS(env, info, true); 3295 } 3296 RunJS(napi_env env,napi_callback_info info,bool extention)3297 napi_value NapiWebviewController::RunJS(napi_env env, napi_callback_info info, bool extention) 3298 { 3299 napi_value thisVar = nullptr; 3300 napi_value result = nullptr; 3301 size_t argc = INTEGER_ONE; 3302 size_t argcPromise = INTEGER_ONE; 3303 size_t argcCallback = INTEGER_TWO; 3304 napi_value argv[INTEGER_TWO] = { 0 }; 3305 3306 napi_get_undefined(env, &result); 3307 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3308 3309 if (argc != argcPromise && argc != argcCallback) { 3310 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3311 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "two")); 3312 return result; 3313 } 3314 3315 if (argc == argcCallback) { 3316 napi_valuetype valueType = napi_null; 3317 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3318 napi_typeof(env, argv[argcCallback - 1], &valueType); 3319 if (valueType != napi_function) { 3320 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3321 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function")); 3322 return result; 3323 } 3324 } 3325 3326 if (maxFdNum_ == -1) { 3327 maxFdNum_ = 3328 std::atoi(NWebAdapterHelper::Instance().ParsePerfConfig("flowBufferConfig", "maxFdNumber").c_str()); 3329 } 3330 3331 if (usedFd_.load() < maxFdNum_) { 3332 return RunJavaScriptInternalExt(env, info, extention); 3333 } 3334 3335 std::string script; 3336 napi_valuetype valueType = napi_undefined; 3337 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 3338 bool parseResult = (valueType == napi_string) ? NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], script) : 3339 NapiParseUtils::ParseArrayBuffer(env, argv[INTEGER_ZERO], script); 3340 if (!parseResult) { 3341 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3342 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "script", "string")); 3343 return result; 3344 } 3345 return RunJavaScriptInternal(env, info, script, extention); 3346 } 3347 RunCreatePDFExt(napi_env env,napi_callback_info info)3348 napi_value NapiWebviewController::RunCreatePDFExt(napi_env env, napi_callback_info info) 3349 { 3350 napi_value thisVar = nullptr; 3351 napi_value result = nullptr; 3352 size_t argc = INTEGER_ONE; 3353 size_t argcPromise = INTEGER_ONE; 3354 size_t argcCallback = INTEGER_TWO; 3355 napi_value argv[INTEGER_TWO] = { 0 }; 3356 3357 napi_get_undefined(env, &result); 3358 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3359 3360 WebviewController* webviewController = nullptr; 3361 napi_unwrap(env, thisVar, (void**)&webviewController); 3362 3363 if (!webviewController || !webviewController->IsInit()) { 3364 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3365 return result; 3366 } 3367 3368 std::shared_ptr<NWebPDFConfigArgs> pdfConfig = ParsePDFConfigArgs(env, argv[INTEGER_ZERO]); 3369 if (pdfConfig == nullptr) { 3370 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 3371 return nullptr; 3372 } 3373 3374 if (argc == argcCallback) { 3375 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3376 napi_ref jsCallback = nullptr; 3377 napi_create_reference(env, argv[argcCallback - 1], 1, &jsCallback); 3378 3379 if (jsCallback) { 3380 webviewController->CreatePDFCallbackExt(env, pdfConfig, std::move(jsCallback)); 3381 } 3382 return result; 3383 } else if (argc == argcPromise) { 3384 napi_deferred deferred = nullptr; 3385 napi_value promise = nullptr; 3386 napi_create_promise(env, &deferred, &promise); 3387 if (promise && deferred) { 3388 webviewController->CreatePDFPromiseExt(env, pdfConfig, deferred); 3389 } 3390 return promise; 3391 } 3392 return result; 3393 } 3394 RunJavaScriptInternal(napi_env env,napi_callback_info info,const std::string & script,bool extention)3395 napi_value NapiWebviewController::RunJavaScriptInternal(napi_env env, napi_callback_info info, 3396 const std::string &script, bool extention) 3397 { 3398 napi_value thisVar = nullptr; 3399 size_t argc = INTEGER_ONE; 3400 size_t argcPromise = INTEGER_ONE; 3401 size_t argcCallback = INTEGER_TWO; 3402 napi_value argv[INTEGER_TWO] = {0}; 3403 3404 napi_value result = nullptr; 3405 napi_get_undefined(env, &result); 3406 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3407 3408 WebviewController *webviewController = nullptr; 3409 napi_unwrap(env, thisVar, (void **)&webviewController); 3410 3411 if (!webviewController || !webviewController->IsInit()) { 3412 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3413 return result; 3414 } 3415 3416 if (argc == argcCallback) { 3417 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3418 napi_ref jsCallback = nullptr; 3419 napi_create_reference(env, argv[argcCallback - 1], 1, &jsCallback); 3420 3421 if (jsCallback) { 3422 webviewController->RunJavaScriptCallback(script, env, std::move(jsCallback), extention); 3423 } 3424 return result; 3425 } else if (argc == argcPromise) { 3426 napi_deferred deferred = nullptr; 3427 napi_value promise = nullptr; 3428 napi_create_promise(env, &deferred, &promise); 3429 if (promise && deferred) { 3430 webviewController->RunJavaScriptPromise(script, env, deferred, extention); 3431 } 3432 return promise; 3433 } 3434 return result; 3435 } 3436 ConstructFlowbuf(napi_env env,napi_value argv,int & fd,size_t & scriptLength)3437 ErrCode NapiWebviewController::ConstructFlowbuf(napi_env env, napi_value argv, int& fd, size_t& scriptLength) 3438 { 3439 auto flowbufferAdapter = OhosAdapterHelper::GetInstance().CreateFlowbufferAdapter(); 3440 if (!flowbufferAdapter) { 3441 return NWebError::NEW_OOM; 3442 } 3443 flowbufferAdapter->StartPerformanceBoost(); 3444 3445 napi_valuetype valueType = napi_undefined; 3446 napi_typeof(env, argv, &valueType); 3447 3448 ErrCode constructResult = (valueType == napi_string) ? 3449 NapiParseUtils::ConstructStringFlowbuf(env, argv, fd, scriptLength) : 3450 NapiParseUtils::ConstructArrayBufFlowbuf(env, argv, fd, scriptLength); 3451 return constructResult; 3452 } 3453 RunJSBackToOriginal(napi_env env,napi_callback_info info,bool extention,napi_value argv,napi_value result)3454 napi_value NapiWebviewController::RunJSBackToOriginal(napi_env env, napi_callback_info info, 3455 bool extention, napi_value argv, napi_value result) 3456 { 3457 std::string script; 3458 napi_valuetype valueType = napi_undefined; 3459 napi_typeof(env, argv, &valueType); 3460 bool parseResult = (valueType == napi_string) ? NapiParseUtils::ParseString(env, argv, script) : 3461 NapiParseUtils::ParseArrayBuffer(env, argv, script); 3462 if (!parseResult) { 3463 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 3464 return result; 3465 } 3466 return RunJavaScriptInternal(env, info, script, extention); 3467 } 3468 RunJavaScriptInternalExt(napi_env env,napi_callback_info info,bool extention)3469 napi_value NapiWebviewController::RunJavaScriptInternalExt(napi_env env, napi_callback_info info, bool extention) 3470 { 3471 napi_value thisVar = nullptr; 3472 napi_value result = nullptr; 3473 size_t argc = INTEGER_ONE; 3474 size_t argcPromise = INTEGER_ONE; 3475 size_t argcCallback = INTEGER_TWO; 3476 napi_value argv[INTEGER_TWO] = {0}; 3477 3478 napi_get_undefined(env, &result); 3479 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3480 3481 int fd; 3482 size_t scriptLength; 3483 ErrCode constructResult = ConstructFlowbuf(env, argv[INTEGER_ZERO], fd, scriptLength); 3484 if (constructResult != NO_ERROR) { 3485 return RunJSBackToOriginal(env, info, extention, argv[INTEGER_ZERO], result); 3486 } 3487 usedFd_++; 3488 3489 WebviewController *webviewController = nullptr; 3490 napi_unwrap(env, thisVar, (void **)&webviewController); 3491 3492 if (!webviewController || !webviewController->IsInit()) { 3493 close(fd); 3494 usedFd_--; 3495 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3496 return result; 3497 } 3498 3499 if (argc == argcCallback) { 3500 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3501 napi_ref jsCallback = nullptr; 3502 napi_create_reference(env, argv[argcCallback - 1], 1, &jsCallback); 3503 3504 if (jsCallback) { 3505 // RunJavaScriptCallbackExt will close fd after IPC 3506 webviewController->RunJavaScriptCallbackExt(fd, scriptLength, env, std::move(jsCallback), extention); 3507 } 3508 usedFd_--; 3509 return result; 3510 } else if (argc == argcPromise) { 3511 napi_deferred deferred = nullptr; 3512 napi_value promise = nullptr; 3513 napi_create_promise(env, &deferred, &promise); 3514 if (promise && deferred) { 3515 // RunJavaScriptCallbackExt will close fd after IPC 3516 webviewController->RunJavaScriptPromiseExt(fd, scriptLength, env, deferred, extention); 3517 } 3518 usedFd_--; 3519 return promise; 3520 } 3521 close(fd); 3522 usedFd_--; 3523 return result; 3524 } 3525 GetUrl(napi_env env,napi_callback_info info)3526 napi_value NapiWebviewController::GetUrl(napi_env env, napi_callback_info info) 3527 { 3528 napi_value result = nullptr; 3529 WebviewController *webviewController = GetWebviewController(env, info); 3530 if (!webviewController) { 3531 return nullptr; 3532 } 3533 3534 std::string url = ""; 3535 url = webviewController->GetUrl(); 3536 napi_create_string_utf8(env, url.c_str(), url.length(), &result); 3537 3538 return result; 3539 } 3540 GetOriginalUrl(napi_env env,napi_callback_info info)3541 napi_value NapiWebviewController::GetOriginalUrl(napi_env env, napi_callback_info info) 3542 { 3543 napi_value result = nullptr; 3544 WebviewController *webviewController = GetWebviewController(env, info); 3545 if (!webviewController) { 3546 return nullptr; 3547 } 3548 3549 std::string url = ""; 3550 url = webviewController->GetOriginalUrl(); 3551 napi_create_string_utf8(env, url.c_str(), url.length(), &result); 3552 return result; 3553 } 3554 TerminateRenderProcess(napi_env env,napi_callback_info info)3555 napi_value NapiWebviewController::TerminateRenderProcess(napi_env env, napi_callback_info info) 3556 { 3557 napi_value result = nullptr; 3558 WebviewController *webviewController = GetWebviewController(env, info); 3559 if (!webviewController) { 3560 return nullptr; 3561 } 3562 bool ret = false; 3563 ret = webviewController->TerminateRenderProcess(); 3564 NAPI_CALL(env, napi_get_boolean(env, ret, &result)); 3565 return result; 3566 } 3567 SetNetworkAvailable(napi_env env,napi_callback_info info)3568 napi_value NapiWebviewController::SetNetworkAvailable(napi_env env, napi_callback_info info) 3569 { 3570 napi_value thisVar = nullptr; 3571 napi_value result = nullptr; 3572 size_t argc = INTEGER_ONE; 3573 napi_value argv[INTEGER_ONE] = { 0 }; 3574 bool enable; 3575 3576 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3577 if (argc != INTEGER_ONE) { 3578 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3579 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3580 return result; 3581 } 3582 3583 if (!NapiParseUtils::ParseBoolean(env, argv[0], enable)) { 3584 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3585 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "enable", "booleane")); 3586 return result; 3587 } 3588 3589 WebviewController *webviewController = nullptr; 3590 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 3591 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 3592 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3593 return nullptr; 3594 } 3595 webviewController->PutNetworkAvailable(enable); 3596 return result; 3597 } 3598 InnerGetWebId(napi_env env,napi_callback_info info)3599 napi_value NapiWebviewController::InnerGetWebId(napi_env env, napi_callback_info info) 3600 { 3601 napi_value thisVar = nullptr; 3602 napi_value result = nullptr; 3603 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3604 3605 WebviewController *webviewController = nullptr; 3606 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 3607 int32_t webId = -1; 3608 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 3609 WVLOG_E("Init error. The WebviewController must be associated with a Web component."); 3610 napi_create_int32(env, webId, &result); 3611 return result; 3612 } 3613 3614 webId = webviewController->GetWebId(); 3615 napi_create_int32(env, webId, &result); 3616 3617 return result; 3618 } 3619 HasImage(napi_env env,napi_callback_info info)3620 napi_value NapiWebviewController::HasImage(napi_env env, napi_callback_info info) 3621 { 3622 napi_value thisVar = nullptr; 3623 napi_value result = nullptr; 3624 size_t argc = INTEGER_ONE; 3625 size_t argcPromiseParaNum = INTEGER_ZERO; 3626 size_t argcCallbackParaNum = INTEGER_ONE; 3627 napi_value argv[INTEGER_ONE] = { 0 }; 3628 3629 napi_get_undefined(env, &result); 3630 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3631 3632 if (argc != argcPromiseParaNum && argc != argcCallbackParaNum) { 3633 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 3634 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "zero", "one")); 3635 return result; 3636 } 3637 3638 if (argc == argcCallbackParaNum) { 3639 napi_valuetype valueType = napi_null; 3640 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3641 napi_typeof(env, argv[argcCallbackParaNum - 1], &valueType); 3642 if (valueType != napi_function) { 3643 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 3644 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function")); 3645 return result; 3646 } 3647 } 3648 return HasImageInternal(env, info); 3649 } 3650 HasImageInternal(napi_env env,napi_callback_info info)3651 napi_value NapiWebviewController::HasImageInternal(napi_env env, napi_callback_info info) 3652 { 3653 napi_value thisVar = nullptr; 3654 size_t argc = INTEGER_ONE; 3655 size_t argcPromiseParaNum = INTEGER_ZERO; 3656 size_t argcCallbackParaNum = INTEGER_ONE; 3657 napi_value argv[INTEGER_ONE] = { 0 }; 3658 3659 napi_value result = nullptr; 3660 napi_get_undefined(env, &result); 3661 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3662 3663 WebviewController *webviewController = nullptr; 3664 napi_unwrap(env, thisVar, (void **)&webviewController); 3665 3666 if (!webviewController || !webviewController->IsInit()) { 3667 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3668 return result; 3669 } 3670 3671 if (argc == argcCallbackParaNum) { 3672 napi_ref jsCallback = nullptr; 3673 napi_create_reference(env, argv[argcCallbackParaNum - 1], 1, &jsCallback); 3674 3675 if (jsCallback) { 3676 ErrCode ret = webviewController->HasImagesCallback(env, std::move(jsCallback)); 3677 if (ret == NWEB_ERROR) { 3678 return nullptr; 3679 } else if (ret != NO_ERROR) { 3680 BusinessError::ThrowErrorByErrcode(env, ret); 3681 return nullptr; 3682 } 3683 } 3684 return result; 3685 } else if (argc == argcPromiseParaNum) { 3686 napi_deferred deferred = nullptr; 3687 napi_value promise = nullptr; 3688 napi_create_promise(env, &deferred, &promise); 3689 if (promise && deferred) { 3690 ErrCode ret = webviewController->HasImagesPromise(env, deferred); 3691 if (ret == NWEB_ERROR) { 3692 return nullptr; 3693 } else if (ret != NO_ERROR) { 3694 BusinessError::ThrowErrorByErrcode(env, ret); 3695 return nullptr; 3696 } 3697 } 3698 return promise; 3699 } 3700 return result; 3701 } 3702 RemoveCache(napi_env env,napi_callback_info info)3703 napi_value NapiWebviewController::RemoveCache(napi_env env, napi_callback_info info) 3704 { 3705 napi_value thisVar = nullptr; 3706 napi_value result = nullptr; 3707 size_t argc = INTEGER_ONE; 3708 napi_value argv[INTEGER_ONE] = { 0 }; 3709 bool includeDiskFiles; 3710 3711 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 3712 if (argc != INTEGER_ONE) { 3713 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3714 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3715 return result; 3716 } 3717 3718 if (!NapiParseUtils::ParseBoolean(env, argv[0], includeDiskFiles)) { 3719 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3720 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "clearRom", "boolean")); 3721 return result; 3722 } 3723 3724 WebviewController *webviewController = nullptr; 3725 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 3726 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 3727 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3728 return nullptr; 3729 } 3730 webviewController->RemoveCache(includeDiskFiles); 3731 return result; 3732 } 3733 IsIncognitoMode(napi_env env,napi_callback_info info)3734 napi_value NapiWebviewController::IsIncognitoMode(napi_env env, napi_callback_info info) 3735 { 3736 napi_value result = nullptr; 3737 WebviewController *webviewController = GetWebviewController(env, info); 3738 if (!webviewController) { 3739 return nullptr; 3740 } 3741 3742 bool incognitoMode = false; 3743 incognitoMode = webviewController->IsIncognitoMode(); 3744 NAPI_CALL(env, napi_get_boolean(env, incognitoMode, &result)); 3745 return result; 3746 } 3747 JsConstructor(napi_env env,napi_callback_info info)3748 napi_value NapiWebHistoryList::JsConstructor(napi_env env, napi_callback_info info) 3749 { 3750 napi_value thisVar = nullptr; 3751 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3752 return thisVar; 3753 } 3754 getColorType(ImageColorType colorType)3755 Media::PixelFormat getColorType(ImageColorType colorType) 3756 { 3757 Media::PixelFormat pixelFormat_; 3758 switch (colorType) { 3759 case ImageColorType::COLOR_TYPE_UNKNOWN: 3760 pixelFormat_ = Media::PixelFormat::UNKNOWN; 3761 break; 3762 case ImageColorType::COLOR_TYPE_RGBA_8888: 3763 pixelFormat_ = Media::PixelFormat::RGBA_8888; 3764 break; 3765 case ImageColorType::COLOR_TYPE_BGRA_8888: 3766 pixelFormat_ = Media::PixelFormat::BGRA_8888; 3767 break; 3768 default: 3769 pixelFormat_ = Media::PixelFormat::UNKNOWN; 3770 break; 3771 } 3772 return pixelFormat_; 3773 } 3774 getAlphaType(ImageAlphaType alphaType)3775 Media::AlphaType getAlphaType(ImageAlphaType alphaType) 3776 { 3777 Media::AlphaType alphaType_; 3778 switch (alphaType) { 3779 case ImageAlphaType::ALPHA_TYPE_UNKNOWN: 3780 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 3781 break; 3782 case ImageAlphaType::ALPHA_TYPE_OPAQUE: 3783 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; 3784 break; 3785 case ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED: 3786 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL; 3787 break; 3788 case ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED: 3789 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL; 3790 break; 3791 default: 3792 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 3793 break; 3794 } 3795 return alphaType_; 3796 } 3797 GetFavicon(napi_env env,std::shared_ptr<NWebHistoryItem> item)3798 napi_value NapiWebHistoryList::GetFavicon(napi_env env, std::shared_ptr<NWebHistoryItem> item) 3799 { 3800 napi_value result = nullptr; 3801 void *data = nullptr; 3802 int32_t width = 0; 3803 int32_t height = 0; 3804 ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN; 3805 ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN; 3806 bool isGetFavicon = item->GetFavicon(&data, width, height, colorType, alphaType); 3807 napi_get_null(env, &result); 3808 3809 if (!isGetFavicon) { 3810 return result; 3811 } 3812 3813 Media::InitializationOptions opt; 3814 opt.size.width = width; 3815 opt.size.height = height; 3816 opt.pixelFormat = getColorType(colorType); 3817 opt.alphaType = getAlphaType(alphaType); 3818 opt.editable = true; 3819 auto pixelMap = Media::PixelMap::Create(opt); 3820 if (pixelMap == nullptr) { 3821 return result; 3822 } 3823 uint64_t stride = static_cast<uint64_t>(width) << 2; 3824 uint64_t bufferSize = stride * static_cast<uint64_t>(height); 3825 pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize); 3826 std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release()); 3827 napi_value jsPixelMap = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs); 3828 return jsPixelMap; 3829 } 3830 GetItem(napi_env env,napi_callback_info info)3831 napi_value NapiWebHistoryList::GetItem(napi_env env, napi_callback_info info) 3832 { 3833 napi_value thisVar = nullptr; 3834 napi_value result = nullptr; 3835 size_t argc = INTEGER_ONE; 3836 napi_value argv[INTEGER_ONE] = { 0 }; 3837 int32_t index; 3838 WebHistoryList *historyList = nullptr; 3839 3840 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 3841 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&historyList)); 3842 if (historyList == nullptr) { 3843 WVLOG_E("unwrap historyList failed."); 3844 return result; 3845 } 3846 if (argc != INTEGER_ONE) { 3847 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3848 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 3849 return result; 3850 } 3851 if (!NapiParseUtils::ParseInt32(env, argv[0], index)) { 3852 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3853 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NOT_NULL_TWO, "index", "int")); 3854 return result; 3855 } 3856 if (index >= historyList->GetListSize() || index < 0) { 3857 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 3858 "BusinessError 401: Parameter error. The value of index must be greater than or equal to 0"); 3859 return result; 3860 } 3861 3862 std::shared_ptr<NWebHistoryItem> item = historyList->GetItem(index); 3863 if (!item) { 3864 return result; 3865 } 3866 3867 napi_create_object(env, &result); 3868 std::string historyUrl = item->GetHistoryUrl(); 3869 std::string historyRawUrl = item->GetHistoryRawUrl(); 3870 std::string title = item->GetHistoryTitle(); 3871 3872 napi_value js_historyUrl; 3873 napi_create_string_utf8(env, historyUrl.c_str(), historyUrl.length(), &js_historyUrl); 3874 napi_set_named_property(env, result, "historyUrl", js_historyUrl); 3875 3876 napi_value js_historyRawUrl; 3877 napi_create_string_utf8(env, historyRawUrl.c_str(), historyRawUrl.length(), &js_historyRawUrl); 3878 napi_set_named_property(env, result, "historyRawUrl", js_historyRawUrl); 3879 3880 napi_value js_title; 3881 napi_create_string_utf8(env, title.c_str(), title.length(), &js_title); 3882 napi_set_named_property(env, result, "title", js_title); 3883 3884 napi_value js_icon = GetFavicon(env, item); 3885 napi_set_named_property(env, result, "icon", js_icon); 3886 return result; 3887 } 3888 getBackForwardEntries(napi_env env,napi_callback_info info)3889 napi_value NapiWebviewController::getBackForwardEntries(napi_env env, napi_callback_info info) 3890 { 3891 napi_value thisVar = nullptr; 3892 napi_value result = nullptr; 3893 WebviewController *webviewController = nullptr; 3894 3895 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); 3896 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 3897 if (webviewController == nullptr || !webviewController->IsInit()) { 3898 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3899 return nullptr; 3900 } 3901 3902 std::shared_ptr<NWebHistoryList> list = webviewController->GetHistoryList(); 3903 if (!list) { 3904 return result; 3905 } 3906 3907 int32_t currentIndex = list->GetCurrentIndex(); 3908 int32_t size = list->GetListSize(); 3909 3910 napi_value historyList = nullptr; 3911 NAPI_CALL(env, napi_get_reference_value(env, g_historyListRef, &historyList)); 3912 NAPI_CALL(env, napi_new_instance(env, historyList, 0, NULL, &result)); 3913 3914 napi_value js_currentIndex; 3915 napi_create_int32(env, currentIndex, &js_currentIndex); 3916 napi_set_named_property(env, result, "currentIndex", js_currentIndex); 3917 3918 napi_value js_size; 3919 napi_create_int32(env, size, &js_size); 3920 napi_set_named_property(env, result, "size", js_size); 3921 3922 WebHistoryList *webHistoryList = new (std::nothrow) WebHistoryList(list); 3923 if (webHistoryList == nullptr) { 3924 return result; 3925 } 3926 3927 NAPI_CALL(env, napi_wrap(env, result, webHistoryList, 3928 [](napi_env env, void *data, void *hint) { 3929 WebHistoryList *webHistoryList = static_cast<WebHistoryList *>(data); 3930 delete webHistoryList; 3931 }, 3932 nullptr, nullptr)); 3933 3934 return result; 3935 } 3936 GetFavicon(napi_env env,napi_callback_info info)3937 napi_value NapiWebviewController::GetFavicon(napi_env env, napi_callback_info info) 3938 { 3939 napi_value thisVar = nullptr; 3940 napi_value result = nullptr; 3941 napi_get_null(env, &result); 3942 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3943 3944 WebviewController *webviewController = nullptr; 3945 napi_unwrap(env, thisVar, (void **)&webviewController); 3946 3947 if (!webviewController || !webviewController->IsInit()) { 3948 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3949 return result; 3950 } 3951 3952 const void *data = nullptr; 3953 size_t width = 0; 3954 size_t height = 0; 3955 ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN; 3956 ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN; 3957 bool isGetFavicon = webviewController->GetFavicon(&data, width, height, colorType, alphaType); 3958 if (!isGetFavicon) { 3959 return result; 3960 } 3961 3962 Media::InitializationOptions opt; 3963 opt.size.width = static_cast<int32_t>(width); 3964 opt.size.height = static_cast<int32_t>(height); 3965 opt.pixelFormat = getColorType(colorType); 3966 opt.alphaType = getAlphaType(alphaType); 3967 opt.editable = true; 3968 auto pixelMap = Media::PixelMap::Create(opt); 3969 if (pixelMap == nullptr) { 3970 return result; 3971 } 3972 uint64_t stride = static_cast<uint64_t>(width) << 2; 3973 uint64_t bufferSize = stride * static_cast<uint64_t>(height); 3974 pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize); 3975 std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release()); 3976 napi_value jsPixelMap = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs); 3977 return jsPixelMap; 3978 } 3979 SerializeWebState(napi_env env,napi_callback_info info)3980 napi_value NapiWebviewController::SerializeWebState(napi_env env, napi_callback_info info) 3981 { 3982 napi_value thisVar = nullptr; 3983 napi_value result = nullptr; 3984 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 3985 napi_get_null(env, &result); 3986 3987 WebviewController *webviewController = nullptr; 3988 napi_unwrap(env, thisVar, (void **)&webviewController); 3989 if (!webviewController || !webviewController->IsInit()) { 3990 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 3991 return result; 3992 } 3993 3994 void *data = nullptr; 3995 napi_value buffer = nullptr; 3996 auto webState = webviewController->SerializeWebState(); 3997 3998 NAPI_CALL(env, napi_create_arraybuffer(env, webState.size(), &data, &buffer)); 3999 int retCode = memcpy_s(data, webState.size(), webState.data(), webState.size()); 4000 if (retCode != 0) { 4001 return result; 4002 } 4003 NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, webState.size(), buffer, 0, &result)); 4004 return result; 4005 } 4006 RestoreWebState(napi_env env,napi_callback_info info)4007 napi_value NapiWebviewController::RestoreWebState(napi_env env, napi_callback_info info) 4008 { 4009 napi_value thisVar = nullptr; 4010 napi_value result = nullptr; 4011 size_t argc = INTEGER_ONE; 4012 napi_value argv[INTEGER_ONE] = { 0 }; 4013 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 4014 napi_get_null(env, &result); 4015 4016 WebviewController *webviewController = nullptr; 4017 napi_unwrap(env, thisVar, (void **)&webviewController); 4018 if (!webviewController || !webviewController->IsInit()) { 4019 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4020 return result; 4021 } 4022 4023 bool isTypedArray = false; 4024 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4025 if (argc != INTEGER_ONE) { 4026 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4027 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 4028 return result; 4029 } 4030 NAPI_CALL(env, napi_is_typedarray(env, argv[0], &isTypedArray)); 4031 if (!isTypedArray) { 4032 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4033 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "state", "uint8Array")); 4034 return result; 4035 } 4036 4037 napi_typedarray_type type; 4038 size_t length = 0; 4039 napi_value buffer = nullptr; 4040 size_t offset = 0; 4041 NAPI_CALL(env, napi_get_typedarray_info(env, argv[0], &type, &length, nullptr, &buffer, &offset)); 4042 if (type != napi_uint8_array) { 4043 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4044 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "state", "uint8Array")); 4045 return result; 4046 } 4047 uint8_t *data = nullptr; 4048 size_t total = 0; 4049 NAPI_CALL(env, napi_get_arraybuffer_info(env, buffer, reinterpret_cast<void **>(&data), &total)); 4050 length = std::min<size_t>(length, total - offset); 4051 std::vector<uint8_t> state(length); 4052 int retCode = memcpy_s(state.data(), state.size(), &data[offset], length); 4053 if (retCode != 0) { 4054 return result; 4055 } 4056 webviewController->RestoreWebState(state); 4057 return result; 4058 } 4059 ScrollPageDown(napi_env env,napi_callback_info info)4060 napi_value NapiWebviewController::ScrollPageDown(napi_env env, napi_callback_info info) 4061 { 4062 napi_value thisVar = nullptr; 4063 napi_value result = nullptr; 4064 size_t argc = INTEGER_ONE; 4065 napi_value argv[INTEGER_ONE] = { 0 }; 4066 bool bottom; 4067 4068 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4069 if (argc != INTEGER_ONE) { 4070 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4071 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 4072 return result; 4073 } 4074 4075 if (!NapiParseUtils::ParseBoolean(env, argv[0], bottom)) { 4076 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4077 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "bottom", "booleane")); 4078 return result; 4079 } 4080 4081 WebviewController *webviewController = nullptr; 4082 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4083 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4084 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4085 return nullptr; 4086 } 4087 webviewController->ScrollPageDown(bottom); 4088 return result; 4089 } 4090 ScrollPageUp(napi_env env,napi_callback_info info)4091 napi_value NapiWebviewController::ScrollPageUp(napi_env env, napi_callback_info info) 4092 { 4093 napi_value thisVar = nullptr; 4094 napi_value result = nullptr; 4095 size_t argc = INTEGER_ONE; 4096 napi_value argv[INTEGER_ONE] = { 0 }; 4097 bool top; 4098 4099 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4100 if (argc != INTEGER_ONE) { 4101 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4102 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 4103 return result; 4104 } 4105 4106 if (!NapiParseUtils::ParseBoolean(env, argv[0], top)) { 4107 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4108 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "top", "booleane")); 4109 return result; 4110 } 4111 4112 WebviewController *webviewController = nullptr; 4113 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4114 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4115 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4116 return nullptr; 4117 } 4118 webviewController->ScrollPageUp(top); 4119 return result; 4120 } 4121 CheckSchemeName(const std::string & schemeName)4122 bool CheckSchemeName(const std::string& schemeName) 4123 { 4124 if (schemeName.empty() || schemeName.size() > MAX_CUSTOM_SCHEME_NAME_LENGTH) { 4125 WVLOG_E("Invalid scheme name length"); 4126 return false; 4127 } 4128 for (auto it = schemeName.begin(); it != schemeName.end(); it++) { 4129 char chr = *it; 4130 if (!((chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') || 4131 (chr == '.') || (chr == '+') || (chr == '-'))) { 4132 WVLOG_E("invalid character %{public}c", chr); 4133 return false; 4134 } 4135 } 4136 return true; 4137 } 4138 SetCustomizeSchemeOption(Scheme & scheme)4139 void SetCustomizeSchemeOption(Scheme& scheme) 4140 { 4141 std::map<int, std::function<bool(const Scheme&)>> schemeProperties = { 4142 {0, [](const Scheme& scheme) { return scheme.isStandard; }}, 4143 {1, [](const Scheme& scheme) { return scheme.isLocal; }}, 4144 {2, [](const Scheme& scheme) { return scheme.isDisplayIsolated; }}, 4145 {3, [](const Scheme& scheme) { return scheme.isSecure; }}, 4146 {4, [](const Scheme& scheme) { return scheme.isSupportCORS; }}, 4147 {5, [](const Scheme& scheme) { return scheme.isCspBypassing; }}, 4148 {6, [](const Scheme& scheme) { return scheme.isSupportFetch; }}, 4149 {7, [](const Scheme& scheme) { return scheme.isCodeCacheSupported; }} 4150 }; 4151 4152 for (const auto& property : schemeProperties) { 4153 if (property.second(scheme)) { 4154 scheme.option += 1 << property.first; 4155 } 4156 } 4157 } 4158 SetCustomizeScheme(napi_env env,napi_value obj,Scheme & scheme)4159 bool SetCustomizeScheme(napi_env env, napi_value obj, Scheme& scheme) 4160 { 4161 std::map<std::string, std::function<void(Scheme&, bool)>> schemeBooleanProperties = { 4162 {"isSupportCORS", [](Scheme& scheme, bool value) { scheme.isSupportCORS = value; }}, 4163 {"isSupportFetch", [](Scheme& scheme, bool value) { scheme.isSupportFetch = value; }}, 4164 {"isStandard", [](Scheme& scheme, bool value) { scheme.isStandard = value; }}, 4165 {"isLocal", [](Scheme& scheme, bool value) { scheme.isLocal = value; }}, 4166 {"isDisplayIsolated", [](Scheme& scheme, bool value) { scheme.isDisplayIsolated = value; }}, 4167 {"isSecure", [](Scheme& scheme, bool value) { scheme.isSecure = value; }}, 4168 {"isCspBypassing", [](Scheme& scheme, bool value) { scheme.isCspBypassing = value; }}, 4169 {"isCodeCacheSupported", [](Scheme& scheme, bool value) { scheme.isCodeCacheSupported = value; }} 4170 }; 4171 4172 for (const auto& property : schemeBooleanProperties) { 4173 napi_value propertyObj = nullptr; 4174 napi_get_named_property(env, obj, property.first.c_str(), &propertyObj); 4175 bool schemeProperty = false; 4176 if (!NapiParseUtils::ParseBoolean(env, propertyObj, schemeProperty)) { 4177 if (property.first == "isSupportCORS" || property.first == "isSupportFetch") { 4178 return false; 4179 } 4180 } 4181 property.second(scheme, schemeProperty); 4182 } 4183 4184 napi_value schemeNameObj = nullptr; 4185 if (napi_get_named_property(env, obj, "schemeName", &schemeNameObj) != napi_ok) { 4186 return false; 4187 } 4188 if (!NapiParseUtils::ParseString(env, schemeNameObj, scheme.name)) { 4189 return false; 4190 } 4191 4192 if (!CheckSchemeName(scheme.name)) { 4193 return false; 4194 } 4195 4196 SetCustomizeSchemeOption(scheme); 4197 return true; 4198 } 4199 CustomizeSchemesArrayDataHandler(napi_env env,napi_value array)4200 int32_t CustomizeSchemesArrayDataHandler(napi_env env, napi_value array) 4201 { 4202 uint32_t arrayLength = 0; 4203 napi_get_array_length(env, array, &arrayLength); 4204 if (arrayLength > MAX_CUSTOM_SCHEME_SIZE) { 4205 return PARAM_CHECK_ERROR; 4206 } 4207 std::vector<Scheme> schemeVector; 4208 for (uint32_t i = 0; i < arrayLength; ++i) { 4209 napi_value obj = nullptr; 4210 napi_get_element(env, array, i, &obj); 4211 Scheme scheme; 4212 bool result = SetCustomizeScheme(env, obj, scheme); 4213 if (!result) { 4214 return PARAM_CHECK_ERROR; 4215 } 4216 schemeVector.push_back(scheme); 4217 } 4218 int32_t registerResult; 4219 for (auto it = schemeVector.begin(); it != schemeVector.end(); ++it) { 4220 registerResult = OH_ArkWeb_RegisterCustomSchemes(it->name.c_str(), it->option); 4221 if (registerResult != NO_ERROR) { 4222 return registerResult; 4223 } 4224 } 4225 return NO_ERROR; 4226 } 4227 CustomizeSchemes(napi_env env,napi_callback_info info)4228 napi_value NapiWebviewController::CustomizeSchemes(napi_env env, napi_callback_info info) 4229 { 4230 if (WebviewController::existNweb_) { 4231 WVLOG_E("There exist web component which has been already created."); 4232 } 4233 4234 napi_value result = nullptr; 4235 napi_value thisVar = nullptr; 4236 size_t argc = INTEGER_ONE; 4237 napi_value argv[INTEGER_ONE]; 4238 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4239 if (argc != INTEGER_ONE) { 4240 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4241 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 4242 return nullptr; 4243 } 4244 napi_value array = argv[INTEGER_ZERO]; 4245 bool isArray = false; 4246 napi_is_array(env, array, &isArray); 4247 if (!isArray) { 4248 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4249 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "schemes", "array")); 4250 return nullptr; 4251 } 4252 int32_t registerResult = CustomizeSchemesArrayDataHandler(env, array); 4253 if (registerResult == NO_ERROR) { 4254 NAPI_CALL(env, napi_get_undefined(env, &result)); 4255 return result; 4256 } 4257 if (registerResult == PARAM_CHECK_ERROR) { 4258 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4259 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "schemeName", "string")); 4260 return nullptr; 4261 } 4262 BusinessError::ThrowErrorByErrcode(env, REGISTER_CUSTOM_SCHEME_FAILED); 4263 return nullptr; 4264 } 4265 ScrollTo(napi_env env,napi_callback_info info)4266 napi_value NapiWebviewController::ScrollTo(napi_env env, napi_callback_info info) 4267 { 4268 napi_value thisVar = nullptr; 4269 napi_value result = nullptr; 4270 size_t argc = INTEGER_THREE; 4271 napi_value argv[INTEGER_THREE] = { 0 }; 4272 float x; 4273 float y; 4274 int32_t duration; 4275 4276 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4277 if (argc != INTEGER_TWO && argc != INTEGER_THREE) { 4278 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4279 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "two", "three")); 4280 return result; 4281 } 4282 4283 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], x)) { 4284 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4285 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "x", "number")); 4286 return result; 4287 } 4288 4289 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], y)) { 4290 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4291 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "y", "number")); 4292 return result; 4293 } 4294 4295 if (argc == INTEGER_THREE) { 4296 if(!NapiParseUtils::ParseInt32(env, argv[INTEGER_TWO], duration)) { 4297 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4298 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "duration", "number")); 4299 return result; 4300 } 4301 } 4302 4303 WebviewController *webviewController = nullptr; 4304 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4305 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4306 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4307 return nullptr; 4308 } 4309 if(argc == INTEGER_THREE) { 4310 webviewController->ScrollToWithAnime(x, y, duration); 4311 } else { 4312 webviewController->ScrollTo(x, y); 4313 } 4314 return result; 4315 } 4316 ScrollBy(napi_env env,napi_callback_info info)4317 napi_value NapiWebviewController::ScrollBy(napi_env env, napi_callback_info info) 4318 { 4319 napi_value thisVar = nullptr; 4320 napi_value result = nullptr; 4321 size_t argc = INTEGER_THREE; 4322 napi_value argv[INTEGER_THREE] = { 0 }; 4323 float deltaX; 4324 float deltaY; 4325 int32_t duration = 0; 4326 4327 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4328 if (argc != INTEGER_TWO && argc != INTEGER_THREE) { 4329 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4330 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "two", "three")); 4331 return result; 4332 } 4333 4334 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], deltaX)) { 4335 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4336 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "deltaX", "number")); 4337 return result; 4338 } 4339 4340 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], deltaY)) { 4341 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4342 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "deltaY", "number")); 4343 return result; 4344 } 4345 4346 if (argc == INTEGER_THREE) { 4347 if(!NapiParseUtils::ParseInt32(env, argv[INTEGER_TWO], duration)) { 4348 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4349 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "duration", "number")); 4350 return result; 4351 } 4352 } 4353 4354 WebviewController *webviewController = nullptr; 4355 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4356 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4357 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4358 return nullptr; 4359 } 4360 if(argc == INTEGER_THREE) { 4361 webviewController->ScrollByWithAnime(deltaX, deltaY, duration); 4362 } else { 4363 webviewController->ScrollBy(deltaX, deltaY); 4364 } 4365 return result; 4366 } 4367 SlideScroll(napi_env env,napi_callback_info info)4368 napi_value NapiWebviewController::SlideScroll(napi_env env, napi_callback_info info) 4369 { 4370 napi_value thisVar = nullptr; 4371 napi_value result = nullptr; 4372 size_t argc = INTEGER_TWO; 4373 napi_value argv[INTEGER_TWO] = { 0 }; 4374 float vx; 4375 float vy; 4376 4377 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4378 if (argc != INTEGER_TWO) { 4379 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4380 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "two")); 4381 return result; 4382 } 4383 4384 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], vx)) { 4385 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4386 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "vx", "number")); 4387 return result; 4388 } 4389 4390 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], vy)) { 4391 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4392 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "vy", "number")); 4393 return result; 4394 } 4395 4396 WebviewController *webviewController = nullptr; 4397 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4398 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4399 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4400 return nullptr; 4401 } 4402 webviewController->SlideScroll(vx, vy); 4403 return result; 4404 } 4405 SetScrollable(napi_env env,napi_callback_info info)4406 napi_value NapiWebviewController::SetScrollable(napi_env env, napi_callback_info info) 4407 { 4408 napi_value thisVar = nullptr; 4409 napi_value result = nullptr; 4410 size_t argc = INTEGER_TWO; 4411 size_t argcForOld = INTEGER_ONE; 4412 napi_value argv[INTEGER_TWO] = { 0 }; 4413 4414 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4415 if (argc != INTEGER_TWO && argc != argcForOld) { 4416 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR, 4417 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "one", "two")); 4418 return result; 4419 } 4420 bool isEnableScroll; 4421 if (!NapiParseUtils::ParseBoolean(env, argv[0], isEnableScroll)) { 4422 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4423 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "enable", "boolean")); 4424 return result; 4425 } 4426 4427 int32_t scrollType = -1; 4428 if (argc == INTEGER_TWO) { 4429 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ONE], scrollType) || scrollType < 0 || 4430 scrollType >= INTEGER_ONE) { 4431 WVLOG_E("BusinessError: 401. The character of 'scrollType' must be int32."); 4432 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4433 return result; 4434 } 4435 } 4436 4437 WebviewController* webviewController = nullptr; 4438 napi_status status = napi_unwrap(env, thisVar, (void**)&webviewController); 4439 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4440 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4441 return nullptr; 4442 } 4443 webviewController->SetScrollable(isEnableScroll, scrollType); 4444 return result; 4445 } 4446 GetScrollable(napi_env env,napi_callback_info info)4447 napi_value NapiWebviewController::GetScrollable(napi_env env, napi_callback_info info) 4448 { 4449 napi_value result = nullptr; 4450 WebviewController *webviewController = GetWebviewController(env, info); 4451 if (!webviewController) { 4452 return nullptr; 4453 } 4454 4455 bool isScrollable = webviewController->GetScrollable(); 4456 NAPI_CALL(env, napi_get_boolean(env, isScrollable, &result)); 4457 return result; 4458 } 4459 InnerGetCertificate(napi_env env,napi_callback_info info)4460 napi_value NapiWebviewController::InnerGetCertificate(napi_env env, napi_callback_info info) 4461 { 4462 napi_value thisVar = nullptr; 4463 napi_value result = nullptr; 4464 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); 4465 napi_create_array(env, &result); 4466 4467 WebviewController *webviewController = nullptr; 4468 napi_unwrap(env, thisVar, (void **)&webviewController); 4469 if (!webviewController || !webviewController->IsInit()) { 4470 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4471 return result; 4472 } 4473 4474 std::vector<std::string> certChainDerData; 4475 bool ans = webviewController->GetCertChainDerData(certChainDerData); 4476 if (!ans) { 4477 WVLOG_E("get cert chain data failed"); 4478 return result; 4479 } 4480 4481 for (uint8_t i = 0; i < certChainDerData.size(); i++) { 4482 if (i == UINT8_MAX) { 4483 WVLOG_E("error, cert chain data array reach max"); 4484 break; 4485 } 4486 void *data = nullptr; 4487 napi_value buffer = nullptr; 4488 napi_value item = nullptr; 4489 NAPI_CALL(env, napi_create_arraybuffer(env, certChainDerData[i].size(), &data, &buffer)); 4490 int retCode = memcpy_s(data, certChainDerData[i].size(), 4491 certChainDerData[i].data(), certChainDerData[i].size()); 4492 if (retCode != 0) { 4493 WVLOG_E("memcpy_s cert data failed, index = %{public}u,", i); 4494 continue; 4495 } 4496 NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, certChainDerData[i].size(), buffer, 0, &item)); 4497 NAPI_CALL(env, napi_set_element(env, result, i, item)); 4498 } 4499 return result; 4500 } 4501 SetAudioMuted(napi_env env,napi_callback_info info)4502 napi_value NapiWebviewController::SetAudioMuted(napi_env env, napi_callback_info info) 4503 { 4504 WVLOG_D("SetAudioMuted invoked"); 4505 4506 napi_value result = nullptr; 4507 NAPI_CALL(env, napi_get_undefined(env, &result)); 4508 4509 napi_value thisVar = nullptr; 4510 size_t argc = INTEGER_ONE; 4511 napi_value argv[INTEGER_ONE] = { 0 }; 4512 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4513 if (argc != INTEGER_ONE) { 4514 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4515 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 4516 return result; 4517 } 4518 4519 bool muted = false; 4520 if (!NapiParseUtils::ParseBoolean(env, argv[0], muted)) { 4521 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4522 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "mute", "boolean")); 4523 return result; 4524 } 4525 4526 WebviewController* webviewController = nullptr; 4527 napi_status status = napi_unwrap(env, thisVar, (void**)&webviewController); 4528 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4529 WVLOG_E("SetAudioMuted failed due to no associated Web component"); 4530 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4531 return result; 4532 } 4533 4534 ErrCode ret = webviewController->SetAudioMuted(muted); 4535 if (ret != NO_ERROR) { 4536 WVLOG_E("SetAudioMuted failed, error code: %{public}d", ret); 4537 BusinessError::ThrowErrorByErrcode(env, ret); 4538 return result; 4539 } 4540 4541 WVLOG_I("SetAudioMuted: %{public}s", (muted ? "true" : "false")); 4542 return result; 4543 } 4544 PrefetchPage(napi_env env,napi_callback_info info)4545 napi_value NapiWebviewController::PrefetchPage(napi_env env, napi_callback_info info) 4546 { 4547 napi_value thisVar = nullptr; 4548 napi_value result = nullptr; 4549 size_t argc = INTEGER_TWO; 4550 napi_value argv[INTEGER_TWO]; 4551 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4552 WebviewController *webviewController = nullptr; 4553 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4554 if ((argc != INTEGER_ONE) && (argc != INTEGER_TWO)) { 4555 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4556 return nullptr; 4557 } 4558 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4559 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4560 return nullptr; 4561 } 4562 std::string url; 4563 if (!ParsePrepareUrl(env, argv[INTEGER_ZERO], url)) { 4564 BusinessError::ThrowErrorByErrcode(env, INVALID_URL); 4565 return nullptr; 4566 } 4567 std::map<std::string, std::string> additionalHttpHeaders; 4568 if (argc == INTEGER_ONE) { 4569 ErrCode ret = webviewController->PrefetchPage(url, additionalHttpHeaders); 4570 if (ret != NO_ERROR) { 4571 WVLOG_E("PrefetchPage failed, error code: %{public}d", ret); 4572 BusinessError::ThrowErrorByErrcode(env, ret); 4573 return nullptr; 4574 } 4575 NAPI_CALL(env, napi_get_undefined(env, &result)); 4576 return result; 4577 } 4578 return PrefetchPageWithHttpHeaders(env, info, url, argv, webviewController); 4579 } 4580 PrefetchPageWithHttpHeaders(napi_env env,napi_callback_info info,std::string & url,const napi_value * argv,WebviewController * webviewController)4581 napi_value NapiWebviewController::PrefetchPageWithHttpHeaders(napi_env env, napi_callback_info info, std::string& url, 4582 const napi_value* argv, WebviewController* webviewController) 4583 { 4584 napi_value result = nullptr; 4585 std::map<std::string, std::string> additionalHttpHeaders; 4586 napi_value array = argv[INTEGER_ONE]; 4587 bool isArray = false; 4588 napi_is_array(env, array, &isArray); 4589 if (isArray) { 4590 uint32_t arrayLength = INTEGER_ZERO; 4591 napi_get_array_length(env, array, &arrayLength); 4592 for (uint32_t i = 0; i < arrayLength; ++i) { 4593 std::string key; 4594 std::string value; 4595 napi_value obj = nullptr; 4596 napi_value keyObj = nullptr; 4597 napi_value valueObj = nullptr; 4598 napi_get_element(env, array, i, &obj); 4599 if (napi_get_named_property(env, obj, "headerKey", &keyObj) != napi_ok) { 4600 continue; 4601 } 4602 if (napi_get_named_property(env, obj, "headerValue", &valueObj) != napi_ok) { 4603 continue; 4604 } 4605 NapiParseUtils::ParseString(env, keyObj, key); 4606 NapiParseUtils::ParseString(env, valueObj, value); 4607 additionalHttpHeaders[key] = value; 4608 } 4609 } else { 4610 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4611 return nullptr; 4612 } 4613 4614 ErrCode ret = webviewController->PrefetchPage(url, additionalHttpHeaders); 4615 if (ret != NO_ERROR) { 4616 WVLOG_E("PrefetchPage failed, error code: %{public}d", ret); 4617 BusinessError::ThrowErrorByErrcode(env, ret); 4618 return nullptr; 4619 } 4620 NAPI_CALL(env, napi_get_undefined(env, &result)); 4621 return result; 4622 } 4623 GetLastJavascriptProxyCallingFrameUrl(napi_env env,napi_callback_info info)4624 napi_value NapiWebviewController::GetLastJavascriptProxyCallingFrameUrl(napi_env env, napi_callback_info info) 4625 { 4626 napi_value result = nullptr; 4627 WebviewController *webviewController = GetWebviewController(env, info); 4628 if (!webviewController) { 4629 return nullptr; 4630 } 4631 4632 std::string lastCallingFrameUrl = webviewController->GetLastJavascriptProxyCallingFrameUrl(); 4633 napi_create_string_utf8(env, lastCallingFrameUrl.c_str(), lastCallingFrameUrl.length(), &result); 4634 return result; 4635 } 4636 PrepareForPageLoad(napi_env env,napi_callback_info info)4637 napi_value NapiWebviewController::PrepareForPageLoad(napi_env env, napi_callback_info info) 4638 { 4639 napi_value thisVar = nullptr; 4640 napi_value result = nullptr; 4641 size_t argc = INTEGER_THREE; 4642 napi_value argv[INTEGER_THREE] = { 0 }; 4643 napi_get_undefined(env, &result); 4644 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4645 if (argc != INTEGER_THREE) { 4646 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4647 return nullptr; 4648 } 4649 4650 std::string url; 4651 if (!ParsePrepareUrl(env, argv[INTEGER_ZERO], url)) { 4652 BusinessError::ThrowErrorByErrcode(env, INVALID_URL); 4653 return nullptr; 4654 } 4655 4656 bool preconnectable = false; 4657 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ONE], preconnectable)) { 4658 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4659 return nullptr; 4660 } 4661 4662 int32_t numSockets = 0; 4663 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_TWO], numSockets)) { 4664 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4665 return nullptr; 4666 } 4667 if (numSockets <= 0 || static_cast<uint32_t>(numSockets) > SOCKET_MAXIMUM) { 4668 BusinessError::ThrowErrorByErrcode(env, INVALID_SOCKET_NUMBER); 4669 return nullptr; 4670 } 4671 4672 NWebHelper::Instance().PrepareForPageLoad(url, preconnectable, numSockets); 4673 NAPI_CALL(env, napi_get_undefined(env, &result)); 4674 return result; 4675 } 4676 PrefetchResource(napi_env env,napi_callback_info info)4677 napi_value NapiWebviewController::PrefetchResource(napi_env env, napi_callback_info info) 4678 { 4679 napi_value thisVar = nullptr; 4680 napi_value result = nullptr; 4681 size_t argc = INTEGER_FOUR; 4682 napi_value argv[INTEGER_FOUR] = { 0 }; 4683 napi_get_undefined(env, &result); 4684 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4685 if (argc > INTEGER_FOUR || argc < INTEGER_ONE) { 4686 WVLOG_E("BusinessError: 401. Arg count must between 1 and 4."); 4687 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4688 return nullptr; 4689 } 4690 4691 std::shared_ptr<NWebEnginePrefetchArgs> prefetchArgs = ParsePrefetchArgs(env, argv[INTEGER_ZERO]); 4692 if (prefetchArgs == nullptr) { 4693 return nullptr; 4694 } 4695 4696 std::map<std::string, std::string> additionalHttpHeaders; 4697 if (argc >= INTEGER_TWO && !ParseHttpHeaders(env, argv[INTEGER_ONE], &additionalHttpHeaders)) { 4698 WVLOG_E("BusinessError: 401. The type of 'additionalHttpHeaders' must be Array of 'WebHeader'."); 4699 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4700 return nullptr; 4701 } 4702 4703 std::string cacheKey; 4704 if ((argc >= INTEGER_THREE) && !NapiParseUtils::ParseString(env, argv[INTEGER_TWO], cacheKey)) { 4705 WVLOG_E("BusinessError: 401.The type of 'cacheKey' must be string."); 4706 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4707 return nullptr; 4708 } 4709 4710 if (cacheKey.empty()) { 4711 cacheKey = prefetchArgs->GetUrl(); 4712 } else { 4713 if (!CheckCacheKey(env, cacheKey)) { 4714 return nullptr; 4715 } 4716 } 4717 4718 int32_t cacheValidTime = 0; 4719 if (argc >= INTEGER_FOUR) { 4720 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_THREE], cacheValidTime) || cacheValidTime <= 0 || 4721 cacheValidTime > INT_MAX) { 4722 WVLOG_E("BusinessError: 401. The character of 'cacheValidTime' must be int32."); 4723 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4724 return nullptr; 4725 } 4726 } 4727 4728 NAPI_CALL(env, napi_get_undefined(env, &result)); 4729 NWebHelper::Instance().PrefetchResource(prefetchArgs, additionalHttpHeaders, cacheKey, cacheValidTime); 4730 return result; 4731 } 4732 CloseAllMediaPresentations(napi_env env,napi_callback_info info)4733 napi_value NapiWebviewController::CloseAllMediaPresentations(napi_env env, napi_callback_info info) 4734 { 4735 napi_value result = nullptr; 4736 WebviewController* webviewController = GetWebviewController(env, info); 4737 if (!webviewController) { 4738 return result; 4739 } 4740 4741 webviewController->CloseAllMediaPresentations(); 4742 NAPI_CALL(env, napi_get_undefined(env, &result)); 4743 return result; 4744 } 4745 StopAllMedia(napi_env env,napi_callback_info info)4746 napi_value NapiWebviewController::StopAllMedia(napi_env env, napi_callback_info info) 4747 { 4748 napi_value result = nullptr; 4749 WebviewController* webviewController = GetWebviewController(env, info); 4750 if (!webviewController) { 4751 return result; 4752 } 4753 4754 webviewController->StopAllMedia(); 4755 NAPI_CALL(env, napi_get_undefined(env, &result)); 4756 return result; 4757 } 4758 ResumeAllMedia(napi_env env,napi_callback_info info)4759 napi_value NapiWebviewController::ResumeAllMedia(napi_env env, napi_callback_info info) 4760 { 4761 napi_value result = nullptr; 4762 WebviewController* webviewController = GetWebviewController(env, info); 4763 if (!webviewController) { 4764 return result; 4765 } 4766 4767 webviewController->ResumeAllMedia(); 4768 NAPI_CALL(env, napi_get_undefined(env, &result)); 4769 return result; 4770 } 4771 PauseAllMedia(napi_env env,napi_callback_info info)4772 napi_value NapiWebviewController::PauseAllMedia(napi_env env, napi_callback_info info) 4773 { 4774 napi_value result = nullptr; 4775 WebviewController* webviewController = GetWebviewController(env, info); 4776 if (!webviewController) { 4777 return result; 4778 } 4779 4780 webviewController->PauseAllMedia(); 4781 NAPI_CALL(env, napi_get_undefined(env, &result)); 4782 return result; 4783 } 4784 GetMediaPlaybackState(napi_env env,napi_callback_info info)4785 napi_value NapiWebviewController::GetMediaPlaybackState(napi_env env, napi_callback_info info) 4786 { 4787 napi_value result = nullptr; 4788 WebviewController* webviewController = GetWebviewController(env, info); 4789 if (!webviewController) { 4790 return result; 4791 } 4792 4793 int32_t mediaPlaybackState = webviewController->GetMediaPlaybackState(); 4794 napi_create_int32(env, mediaPlaybackState, &result); 4795 return result; 4796 } 4797 ClearPrefetchedResource(napi_env env,napi_callback_info info)4798 napi_value NapiWebviewController::ClearPrefetchedResource(napi_env env, napi_callback_info info) 4799 { 4800 napi_value thisVar = nullptr; 4801 napi_value result = nullptr; 4802 size_t argc = INTEGER_ONE; 4803 napi_value argv[INTEGER_ONE] = { 0 }; 4804 napi_get_undefined(env, &result); 4805 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4806 if (argc != INTEGER_ONE) { 4807 WVLOG_E("BusinessError: 401. Arg count must be 1."); 4808 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4809 return nullptr; 4810 } 4811 4812 std::vector<std::string> cacheKeyList; 4813 if (!ParseCacheKeyList(env, argv[INTEGER_ZERO], &cacheKeyList)) { 4814 WVLOG_E("BusinessError: 401. The type of 'cacheKeyList' must be Array of string."); 4815 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4816 return nullptr; 4817 } 4818 4819 NAPI_CALL(env, napi_get_undefined(env, &result)); 4820 NWebHelper::Instance().ClearPrefetchedResource(cacheKeyList); 4821 return result; 4822 } 4823 CreateWebPrintDocumentAdapter(napi_env env,napi_callback_info info)4824 napi_value NapiWebviewController::CreateWebPrintDocumentAdapter(napi_env env, napi_callback_info info) 4825 { 4826 WVLOG_I("Create web print document adapter."); 4827 napi_value thisVar = nullptr; 4828 napi_value result = nullptr; 4829 size_t argc = INTEGER_ONE; 4830 napi_value argv[INTEGER_ONE]; 4831 NAPI_CALL(env, napi_get_undefined(env, &result)); 4832 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 4833 if (argc != INTEGER_ONE) { 4834 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4835 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 4836 return result; 4837 } 4838 4839 std::string jobName; 4840 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], jobName)) { 4841 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 4842 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "jopName", "string")); 4843 return result; 4844 } 4845 4846 WebviewController *webviewController = nullptr; 4847 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 4848 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 4849 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 4850 return result; 4851 } 4852 void* webPrintDocument = webviewController->CreateWebPrintDocumentAdapter(jobName); 4853 if (!webPrintDocument) { 4854 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4855 return result; 4856 } 4857 4858 napi_value webPrintDoc = nullptr; 4859 NAPI_CALL(env, napi_get_reference_value(env, g_webPrintDocClassRef, &webPrintDoc)); 4860 napi_value consParam[INTEGER_ONE] = {0}; 4861 NAPI_CALL(env, napi_create_bigint_uint64(env, reinterpret_cast<uint64_t>(webPrintDocument), 4862 &consParam[INTEGER_ZERO])); 4863 napi_value proxy = nullptr; 4864 status = napi_new_instance(env, webPrintDoc, INTEGER_ONE, &consParam[INTEGER_ZERO], &proxy); 4865 if (status!= napi_ok) { 4866 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 4867 return result; 4868 } 4869 return proxy; 4870 } 4871 GetSecurityLevel(napi_env env,napi_callback_info info)4872 napi_value NapiWebviewController::GetSecurityLevel(napi_env env, napi_callback_info info) 4873 { 4874 napi_value result = nullptr; 4875 WebviewController *webviewController = GetWebviewController(env, info); 4876 if (!webviewController) { 4877 return result; 4878 } 4879 4880 int32_t securityLevel = webviewController->GetSecurityLevel(); 4881 napi_create_int32(env, securityLevel, &result); 4882 return result; 4883 } 4884 ParsePrintRangeAdapter(napi_env env,napi_value pageRange,PrintAttributesAdapter & printAttr)4885 void ParsePrintRangeAdapter(napi_env env, napi_value pageRange, PrintAttributesAdapter& printAttr) 4886 { 4887 if (!pageRange) { 4888 WVLOG_E("ParsePrintRangeAdapter failed."); 4889 return; 4890 } 4891 napi_value startPage = nullptr; 4892 napi_value endPage = nullptr; 4893 napi_value pages = nullptr; 4894 napi_get_named_property(env, pageRange, "startPage", &startPage); 4895 napi_get_named_property(env, pageRange, "endPage", &endPage); 4896 if (startPage) { 4897 NapiParseUtils::ParseUint32(env, startPage, printAttr.pageRange.startPage); 4898 } 4899 if (endPage) { 4900 NapiParseUtils::ParseUint32(env, endPage, printAttr.pageRange.endPage); 4901 } 4902 napi_get_named_property(env, pageRange, "pages", &pages); 4903 uint32_t pageArrayLength = 0; 4904 napi_get_array_length(env, pages, &pageArrayLength); 4905 for (uint32_t i = 0; i < pageArrayLength; ++i) { 4906 napi_value pagesNumObj = nullptr; 4907 napi_get_element(env, pages, i, &pagesNumObj); 4908 uint32_t pagesNum; 4909 NapiParseUtils::ParseUint32(env, pagesNumObj, pagesNum); 4910 printAttr.pageRange.pages.push_back(pagesNum); 4911 } 4912 } 4913 ParsePrintPageSizeAdapter(napi_env env,napi_value pageSize,PrintAttributesAdapter & printAttr)4914 void ParsePrintPageSizeAdapter(napi_env env, napi_value pageSize, PrintAttributesAdapter& printAttr) 4915 { 4916 if (!pageSize) { 4917 WVLOG_E("ParsePrintPageSizeAdapter failed."); 4918 return; 4919 } 4920 napi_value id = nullptr; 4921 napi_value name = nullptr; 4922 napi_value width = nullptr; 4923 napi_value height = nullptr; 4924 napi_get_named_property(env, pageSize, "id", &id); 4925 napi_get_named_property(env, pageSize, "name", &name); 4926 napi_get_named_property(env, pageSize, "width", &width); 4927 napi_get_named_property(env, pageSize, "height", &height); 4928 if (width) { 4929 NapiParseUtils::ParseUint32(env, width, printAttr.pageSize.width); 4930 } 4931 if (height) { 4932 NapiParseUtils::ParseUint32(env, height, printAttr.pageSize.height); 4933 } 4934 } 4935 ParsePrintMarginAdapter(napi_env env,napi_value margin,PrintAttributesAdapter & printAttr)4936 void ParsePrintMarginAdapter(napi_env env, napi_value margin, PrintAttributesAdapter& printAttr) 4937 { 4938 if (!margin) { 4939 WVLOG_E("ParsePrintMarginAdapter failed."); 4940 return; 4941 } 4942 napi_value top = nullptr; 4943 napi_value bottom = nullptr; 4944 napi_value left = nullptr; 4945 napi_value right = nullptr; 4946 napi_get_named_property(env, margin, "top", &top); 4947 napi_get_named_property(env, margin, "bottom", &bottom); 4948 napi_get_named_property(env, margin, "left", &left); 4949 napi_get_named_property(env, margin, "right", &right); 4950 if (top) { 4951 NapiParseUtils::ParseUint32(env, top, printAttr.margin.top); 4952 } 4953 if (bottom) { 4954 NapiParseUtils::ParseUint32(env, bottom, printAttr.margin.bottom); 4955 } 4956 if (left) { 4957 NapiParseUtils::ParseUint32(env, left, printAttr.margin.left); 4958 } 4959 if (right) { 4960 NapiParseUtils::ParseUint32(env, right, printAttr.margin.right); 4961 } 4962 } 4963 ParseWebPrintWriteResultCallback(napi_env env,napi_value argv)4964 WebPrintWriteResultCallback ParseWebPrintWriteResultCallback(napi_env env, napi_value argv) 4965 { 4966 if (!argv) { 4967 WVLOG_E("ParseWebPrintWriteResultCallback failed."); 4968 return nullptr; 4969 } 4970 napi_ref jsCallback = nullptr; 4971 napi_create_reference(env, argv, 1, &jsCallback); 4972 if (jsCallback) { 4973 WebPrintWriteResultCallback callbackImpl = 4974 [env, jCallback = std::move(jsCallback)](std::string jobId, uint32_t state) { 4975 if (!env) { 4976 return; 4977 } 4978 napi_handle_scope scope = nullptr; 4979 napi_open_handle_scope(env, &scope); 4980 if (scope == nullptr) { 4981 return; 4982 } 4983 napi_value setResult[INTEGER_TWO] = {0}; 4984 napi_create_string_utf8(env, jobId.c_str(), NAPI_AUTO_LENGTH, &setResult[INTEGER_ZERO]); 4985 napi_create_uint32(env, state, &setResult[INTEGER_ONE]); 4986 napi_value args[INTEGER_TWO] = {setResult[INTEGER_ZERO], setResult[INTEGER_ONE]}; 4987 napi_value callback = nullptr; 4988 napi_get_reference_value(env, jCallback, &callback); 4989 napi_value callbackResult = nullptr; 4990 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult); 4991 napi_delete_reference(env, jCallback); 4992 napi_close_handle_scope(env, scope); 4993 }; 4994 return callbackImpl; 4995 } 4996 return nullptr; 4997 } 4998 ParseWebPrintAttrParams(napi_env env,napi_value obj,PrintAttributesAdapter & printAttr)4999 bool ParseWebPrintAttrParams(napi_env env, napi_value obj, PrintAttributesAdapter& printAttr) 5000 { 5001 if (!obj) { 5002 WVLOG_E("ParseWebPrintAttrParams failed."); 5003 return false; 5004 } 5005 napi_value copyNumber = nullptr; 5006 napi_value pageRange = nullptr; 5007 napi_value isSequential = nullptr; 5008 napi_value pageSize = nullptr; 5009 napi_value isLandscape = nullptr; 5010 napi_value colorMode = nullptr; 5011 napi_value duplexMode = nullptr; 5012 napi_value margin = nullptr; 5013 napi_value option = nullptr; 5014 napi_get_named_property(env, obj, "copyNumber", ©Number); 5015 napi_get_named_property(env, obj, "pageRange", &pageRange); 5016 napi_get_named_property(env, obj, "isSequential", &isSequential); 5017 napi_get_named_property(env, obj, "pageSize", &pageSize); 5018 napi_get_named_property(env, obj, "isLandscape", &isLandscape); 5019 napi_get_named_property(env, obj, "colorMode", &colorMode); 5020 napi_get_named_property(env, obj, "duplexMode", &duplexMode); 5021 napi_get_named_property(env, obj, "margin", &margin); 5022 napi_get_named_property(env, obj, "option", &option); 5023 if (copyNumber) { 5024 NapiParseUtils::ParseUint32(env, copyNumber, printAttr.copyNumber); 5025 } 5026 if (isSequential) { 5027 NapiParseUtils::ParseBoolean(env, isSequential, printAttr.isSequential); 5028 } 5029 if (isLandscape) { 5030 NapiParseUtils::ParseBoolean(env, isLandscape, printAttr.isLandscape); 5031 } 5032 if (colorMode) { 5033 NapiParseUtils::ParseUint32(env, colorMode, printAttr.colorMode); 5034 } 5035 if (duplexMode) { 5036 NapiParseUtils::ParseUint32(env, duplexMode, printAttr.duplexMode); 5037 } 5038 if (option) { 5039 NapiParseUtils::ParseString(env, option, printAttr.option); 5040 } 5041 ParsePrintRangeAdapter(env, pageRange, printAttr); 5042 ParsePrintPageSizeAdapter(env, pageSize, printAttr); 5043 ParsePrintMarginAdapter(env, margin, printAttr); 5044 return true; 5045 } 5046 OnStartLayoutWrite(napi_env env,napi_callback_info info)5047 napi_value NapiWebPrintDocument::OnStartLayoutWrite(napi_env env, napi_callback_info info) 5048 { 5049 WVLOG_I("On Start Layout Write."); 5050 napi_value thisVar = nullptr; 5051 napi_value result = nullptr; 5052 size_t argc = INTEGER_FIVE; 5053 napi_value argv[INTEGER_FIVE] = { 0 }; 5054 WebPrintDocument *webPrintDocument = nullptr; 5055 5056 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); 5057 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webPrintDocument)); 5058 if (webPrintDocument == nullptr) { 5059 WVLOG_E("unwrap webPrintDocument failed."); 5060 return result; 5061 } 5062 5063 std::string jobId; 5064 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], jobId)) { 5065 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5066 return result; 5067 } 5068 5069 int32_t fd; 5070 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_THREE], fd)) { 5071 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5072 return result; 5073 } 5074 PrintAttributesAdapter oldPrintAttr; 5075 PrintAttributesAdapter newPrintAttr; 5076 bool ret = false; 5077 ret = ParseWebPrintAttrParams(env, argv[INTEGER_ONE], oldPrintAttr); 5078 if (!ret) { 5079 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5080 return result; 5081 } 5082 ret = ParseWebPrintAttrParams(env, argv[INTEGER_TWO], newPrintAttr); 5083 if (!ret) { 5084 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5085 return result; 5086 } 5087 WebPrintWriteResultCallback writeResultCallback = nullptr; 5088 writeResultCallback = ParseWebPrintWriteResultCallback(env, argv[INTEGER_FOUR]); 5089 webPrintDocument->OnStartLayoutWrite(jobId, oldPrintAttr, newPrintAttr, fd, writeResultCallback); 5090 return result; 5091 } 5092 OnJobStateChanged(napi_env env,napi_callback_info info)5093 napi_value NapiWebPrintDocument::OnJobStateChanged(napi_env env, napi_callback_info info) 5094 { 5095 WVLOG_I("On Job State Changed."); 5096 napi_value thisVar = nullptr; 5097 napi_value result = nullptr; 5098 size_t argc = INTEGER_TWO; 5099 napi_value argv[INTEGER_TWO]; 5100 NAPI_CALL(env, napi_get_undefined(env, &result)); 5101 5102 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5103 WebPrintDocument *webPrintDocument = nullptr; 5104 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webPrintDocument)); 5105 if (webPrintDocument == nullptr) { 5106 WVLOG_E("unwrap webPrintDocument failed."); 5107 return result; 5108 } 5109 if (argc != INTEGER_TWO) { 5110 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5111 return result; 5112 } 5113 5114 std::string jobId; 5115 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], jobId)) { 5116 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5117 return result; 5118 } 5119 5120 int32_t state; 5121 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ONE], state)) { 5122 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5123 return result; 5124 } 5125 webPrintDocument->OnJobStateChanged(jobId, state); 5126 return result; 5127 } 5128 JsConstructor(napi_env env,napi_callback_info info)5129 napi_value NapiWebPrintDocument::JsConstructor(napi_env env, napi_callback_info info) 5130 { 5131 napi_value thisVar = nullptr; 5132 size_t argc = INTEGER_ONE; 5133 napi_value argv[INTEGER_ONE]; 5134 uint64_t addrWebPrintDoc = 0; 5135 bool loseLess = true; 5136 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5137 5138 if (!NapiParseUtils::ParseUint64(env, argv[INTEGER_ZERO], addrWebPrintDoc, &loseLess)) { 5139 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5140 return nullptr; 5141 } 5142 void *webPrintDocPtr = reinterpret_cast<void *>(addrWebPrintDoc); 5143 WebPrintDocument *webPrintDoc = new (std::nothrow) WebPrintDocument(webPrintDocPtr); 5144 if (webPrintDoc == nullptr) { 5145 WVLOG_E("new web print failed"); 5146 return nullptr; 5147 } 5148 NAPI_CALL(env, napi_wrap(env, thisVar, webPrintDoc, 5149 [](napi_env env, void *data, void *hint) { 5150 WebPrintDocument *webPrintDocument = static_cast<WebPrintDocument *>(data); 5151 delete webPrintDocument; 5152 }, 5153 nullptr, nullptr)); 5154 return thisVar; 5155 } 5156 SetDownloadDelegate(napi_env env,napi_callback_info info)5157 napi_value NapiWebviewController::SetDownloadDelegate(napi_env env, napi_callback_info info) 5158 { 5159 WVLOG_D("WebDownloader::JS_SetDownloadDelegate"); 5160 NWebHelper::Instance().LoadNWebSDK(); 5161 5162 size_t argc = 1; 5163 napi_value argv[1] = {0}; 5164 napi_value thisVar = nullptr; 5165 void* data = nullptr; 5166 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 5167 5168 WebDownloadDelegate* delegate = nullptr; 5169 napi_value obj = argv[0]; 5170 napi_unwrap(env, obj, (void**)&delegate); 5171 if (!delegate) { 5172 WVLOG_E("[DOWNLOAD] WebDownloader::JS_SetDownloadDelegate delegate is null"); 5173 (void)RemoveDownloadDelegateRef(env, thisVar); 5174 return nullptr; 5175 } 5176 napi_create_reference(env, obj, 1, &delegate->delegate_); 5177 5178 WebviewController *webviewController = nullptr; 5179 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 5180 if (webviewController == nullptr || !webviewController->IsInit()) { 5181 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 5182 WVLOG_E("create message port failed, napi unwrap webviewController failed"); 5183 return nullptr; 5184 } 5185 int32_t nwebId = webviewController->GetWebId(); 5186 WebDownloadManager::AddDownloadDelegateForWeb(nwebId, delegate); 5187 return nullptr; 5188 } 5189 StartDownload(napi_env env,napi_callback_info info)5190 napi_value NapiWebviewController::StartDownload(napi_env env, napi_callback_info info) 5191 { 5192 WVLOG_D("[DOWNLOAD] NapiWebviewController::StartDownload"); 5193 size_t argc = 1; 5194 napi_value argv[1] = {0}; 5195 napi_value thisVar = nullptr; 5196 void* data = nullptr; 5197 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 5198 5199 WebviewController *webviewController = nullptr; 5200 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 5201 if (webviewController == nullptr || !webviewController->IsInit()) { 5202 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 5203 WVLOG_E("create message port failed, napi unwrap webviewController failed"); 5204 return nullptr; 5205 } 5206 5207 std::string url; 5208 if (!ParsePrepareUrl(env, argv[INTEGER_ZERO], url)) { 5209 BusinessError::ThrowErrorByErrcode(env, INVALID_URL); 5210 return nullptr; 5211 } 5212 int32_t nwebId = webviewController->GetWebId(); 5213 NWebHelper::Instance().LoadNWebSDK(); 5214 WebDownloader_StartDownload(nwebId, url.c_str()); 5215 return nullptr; 5216 } 5217 SetConnectionTimeout(napi_env env,napi_callback_info info)5218 napi_value NapiWebviewController::SetConnectionTimeout(napi_env env, napi_callback_info info) 5219 { 5220 napi_value result = nullptr; 5221 size_t argc = INTEGER_ONE; 5222 napi_value argv[INTEGER_ONE] = { nullptr }; 5223 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 5224 if (argc != INTEGER_ONE) { 5225 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5226 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 5227 return result; 5228 } 5229 5230 int32_t timeout = 0; 5231 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], timeout) || (timeout <= 0)) { 5232 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5233 "BusinessError: 401. Parameter error. The type of 'timeout' must be int and must be positive integer."); 5234 return result; 5235 } 5236 5237 NWebHelper::Instance().SetConnectionTimeout(timeout); 5238 NAPI_CALL(env, napi_get_undefined(env, &result)); 5239 return result; 5240 } 5241 SetPrintBackground(napi_env env,napi_callback_info info)5242 napi_value NapiWebviewController::SetPrintBackground(napi_env env, napi_callback_info info) 5243 { 5244 napi_value result = nullptr; 5245 napi_value thisVar = nullptr; 5246 size_t argc = INTEGER_ONE; 5247 napi_value argv[INTEGER_ONE] = { 0 }; 5248 5249 NAPI_CALL(env, napi_get_undefined(env, &result)); 5250 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5251 if (argc != INTEGER_ONE) { 5252 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5253 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 5254 return result; 5255 } 5256 5257 bool printBackgroundEnabled = false; 5258 if (!NapiParseUtils::ParseBoolean(env, argv[0], printBackgroundEnabled)) { 5259 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5260 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "enable", "boolean")); 5261 return result; 5262 } 5263 5264 WebviewController *webviewController = GetWebviewController(env, info); 5265 if (!webviewController) { 5266 return result; 5267 } 5268 webviewController->SetPrintBackground(printBackgroundEnabled); 5269 return result; 5270 } 5271 GetPrintBackground(napi_env env,napi_callback_info info)5272 napi_value NapiWebviewController::GetPrintBackground(napi_env env, napi_callback_info info) 5273 { 5274 napi_value result = nullptr; 5275 WebviewController *webviewController = GetWebviewController(env, info); 5276 5277 if (!webviewController) { 5278 return result; 5279 } 5280 5281 bool printBackgroundEnabled = webviewController->GetPrintBackground(); 5282 NAPI_CALL(env, napi_get_boolean(env, printBackgroundEnabled, &result)); 5283 return result; 5284 } 5285 SetWebSchemeHandler(napi_env env,napi_callback_info info)5286 napi_value NapiWebviewController::SetWebSchemeHandler(napi_env env, napi_callback_info info) 5287 { 5288 size_t argc = 2; 5289 napi_value argv[2] = {0}; 5290 napi_value thisVar = nullptr; 5291 void* data = nullptr; 5292 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 5293 5294 WebviewController *webviewController = nullptr; 5295 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController)); 5296 if (webviewController == nullptr || !webviewController->IsInit()) { 5297 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR); 5298 WVLOG_E("create message port failed, napi unwrap webviewController failed"); 5299 return nullptr; 5300 } 5301 5302 std::string scheme = ""; 5303 if (!NapiParseUtils::ParseString(env, argv[0], scheme)) { 5304 WVLOG_E("NapiWebviewController::SetWebSchemeHandler parse scheme failed"); 5305 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5306 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "scheme", "string")); 5307 return nullptr; 5308 } 5309 5310 WebSchemeHandler* handler = nullptr; 5311 napi_value obj = argv[1]; 5312 napi_unwrap(env, obj, (void**)&handler); 5313 if (!handler) { 5314 WVLOG_E("NapiWebviewController::SetWebSchemeHandler handler is null"); 5315 return nullptr; 5316 } 5317 napi_create_reference(env, obj, 1, &handler->delegate_); 5318 5319 if (!webviewController->SetWebSchemeHandler(scheme.c_str(), handler)) { 5320 WVLOG_E("NapiWebviewController::SetWebSchemeHandler failed"); 5321 } 5322 return nullptr; 5323 } 5324 ClearWebSchemeHandler(napi_env env,napi_callback_info info)5325 napi_value NapiWebviewController::ClearWebSchemeHandler(napi_env env, napi_callback_info info) 5326 { 5327 napi_value result = nullptr; 5328 WebviewController *webviewController = GetWebviewController(env, info); 5329 if (!webviewController) { 5330 return nullptr; 5331 } 5332 5333 int32_t ret = webviewController->ClearWebSchemeHandler(); 5334 if (ret != 0) { 5335 WVLOG_E("NapiWebviewController::ClearWebSchemeHandler failed"); 5336 } 5337 NAPI_CALL(env, napi_get_undefined(env, &result)); 5338 return result; 5339 } 5340 SetServiceWorkerWebSchemeHandler(napi_env env,napi_callback_info info)5341 napi_value NapiWebviewController::SetServiceWorkerWebSchemeHandler( 5342 napi_env env, napi_callback_info info) 5343 { 5344 size_t argc = 2; 5345 napi_value argv[2] = {0}; 5346 napi_value thisVar = nullptr; 5347 void* data = nullptr; 5348 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 5349 5350 std::string scheme = ""; 5351 if (!NapiParseUtils::ParseString(env, argv[0], scheme)) { 5352 WVLOG_E("NapiWebviewController::SetWebSchemeHandler parse scheme failed"); 5353 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5354 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "scheme", "string")); 5355 return nullptr; 5356 } 5357 5358 WebSchemeHandler* handler = nullptr; 5359 napi_value obj = argv[1]; 5360 napi_unwrap(env, obj, (void**)&handler); 5361 if (!handler) { 5362 WVLOG_E("NapiWebviewController::SetServiceWorkerWebSchemeHandler handler is null"); 5363 return nullptr; 5364 } 5365 napi_create_reference(env, obj, 1, &handler->delegate_); 5366 5367 if (!WebviewController::SetWebServiveWorkerSchemeHandler( 5368 scheme.c_str(), handler)) { 5369 WVLOG_E("NapiWebviewController::SetWebSchemeHandler failed"); 5370 } 5371 return nullptr; 5372 } 5373 ClearServiceWorkerWebSchemeHandler(napi_env env,napi_callback_info info)5374 napi_value NapiWebviewController::ClearServiceWorkerWebSchemeHandler( 5375 napi_env env, napi_callback_info info) 5376 { 5377 int32_t ret = WebviewController::ClearWebServiceWorkerSchemeHandler(); 5378 if (ret != 0) { 5379 WVLOG_E("ClearServiceWorkerWebSchemeHandler ret=%{public}d", ret); 5380 return nullptr; 5381 } 5382 return nullptr; 5383 } 5384 EnableIntelligentTrackingPrevention(napi_env env,napi_callback_info info)5385 napi_value NapiWebviewController::EnableIntelligentTrackingPrevention( 5386 napi_env env, napi_callback_info info) 5387 { 5388 WVLOG_I("enable/disable intelligent tracking prevention."); 5389 napi_value result = nullptr; 5390 napi_value thisVar = nullptr; 5391 size_t argc = INTEGER_ONE; 5392 napi_value argv[INTEGER_ONE] = { 0 }; 5393 5394 NAPI_CALL(env, napi_get_undefined(env, &result)); 5395 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5396 if (argc != INTEGER_ONE) { 5397 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5398 "BusinessError 401: Parameter error. The number of params must be one."); 5399 return result; 5400 } 5401 5402 bool enabled = false; 5403 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ZERO], enabled)) { 5404 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5405 "BusinessError 401: Parameter error. The type of 'enable' must be boolean."); 5406 return result; 5407 } 5408 5409 WebviewController *webviewController = GetWebviewController(env, info); 5410 if (!webviewController) { 5411 WVLOG_E("EnableIntelligentTrackingPrevention failed for webviewController failed"); 5412 return result; 5413 } 5414 webviewController->EnableIntelligentTrackingPrevention(enabled); 5415 return result; 5416 } 5417 IsIntelligentTrackingPreventionEnabled(napi_env env,napi_callback_info info)5418 napi_value NapiWebviewController::IsIntelligentTrackingPreventionEnabled( 5419 napi_env env, napi_callback_info info) 5420 { 5421 WVLOG_I("get intelligent tracking prevention enabled value."); 5422 napi_value result = nullptr; 5423 WebviewController *webviewController = GetWebviewController(env, info); 5424 5425 if (!webviewController) { 5426 WVLOG_E("IsIntelligentTrackingPreventionEnabled failed for webviewController failed"); 5427 return result; 5428 } 5429 5430 bool enabled = webviewController-> 5431 IsIntelligentTrackingPreventionEnabled(); 5432 NAPI_CALL(env, napi_get_boolean(env, enabled, &result)); 5433 return result; 5434 } 5435 GetHostList(napi_env env,napi_value array,std::vector<std::string> & hosts)5436 bool GetHostList(napi_env env, napi_value array, std::vector<std::string>& hosts) 5437 { 5438 uint32_t arrayLen = 0; 5439 napi_get_array_length(env, array, &arrayLen); 5440 if (arrayLen == 0) { 5441 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5442 "BusinessError 401: Parameter error. The array length must be greater than 0."); 5443 return false; 5444 } 5445 5446 for (uint32_t i = 0; i < arrayLen; i++) { 5447 napi_value hostItem = nullptr; 5448 napi_get_element(env, array, i, &hostItem); 5449 5450 size_t hostLen = 0; 5451 napi_get_value_string_utf8(env, hostItem, nullptr, 0, &hostLen); 5452 if (hostLen == 0 || hostLen > UINT_MAX) { 5453 WVLOG_E("hostitem length is invalid"); 5454 return false; 5455 } 5456 5457 char host[hostLen + 1]; 5458 int retCode = memset_s(host, sizeof(host), 0, hostLen + 1); 5459 if (retCode < 0) { 5460 WVLOG_E("memset_s failed, retCode=%{public}d", retCode); 5461 return false; 5462 } 5463 napi_get_value_string_utf8(env, hostItem, host, sizeof(host), &hostLen); 5464 std::string hostStr(host); 5465 hosts.emplace_back(hostStr); 5466 } 5467 return true; 5468 } 5469 AddIntelligentTrackingPreventionBypassingList(napi_env env,napi_callback_info info)5470 napi_value NapiWebviewController::AddIntelligentTrackingPreventionBypassingList( 5471 napi_env env, napi_callback_info info) 5472 { 5473 WVLOG_I("Add intelligent tracking prevention bypassing list."); 5474 napi_value result = nullptr; 5475 napi_value thisVar = nullptr; 5476 size_t argc = INTEGER_ONE; 5477 napi_value argv[INTEGER_ONE] = { 0 }; 5478 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5479 if (argc != INTEGER_ONE) { 5480 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5481 "BusinessError 401: Parameter error. The number of params must be one."); 5482 return result; 5483 } 5484 5485 bool isArray = false; 5486 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ZERO], &isArray)); 5487 if (!isArray) { 5488 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5489 "BusinessError 401: Parameter error. The type of 'hostList' must be string array."); 5490 return result; 5491 } 5492 5493 std::vector<std::string> hosts; 5494 if (!GetHostList(env, argv[INTEGER_ZERO], hosts)) { 5495 WVLOG_E("get host list failed, GetHostList fail"); 5496 return result; 5497 } 5498 5499 NWebHelper::Instance().AddIntelligentTrackingPreventionBypassingList(hosts); 5500 NAPI_CALL(env, napi_get_undefined(env, &result)); 5501 return result; 5502 } 5503 RemoveIntelligentTrackingPreventionBypassingList(napi_env env,napi_callback_info info)5504 napi_value NapiWebviewController::RemoveIntelligentTrackingPreventionBypassingList( 5505 napi_env env, napi_callback_info info) 5506 { 5507 WVLOG_I("Remove intelligent tracking prevention bypassing list."); 5508 napi_value result = nullptr; 5509 napi_value thisVar = nullptr; 5510 size_t argc = INTEGER_ONE; 5511 napi_value argv[INTEGER_ONE] = { 0 }; 5512 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5513 if (argc != INTEGER_ONE) { 5514 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5515 "BusinessError 401: Parameter error. The number of params must be one."); 5516 return result; 5517 } 5518 5519 bool isArray = false; 5520 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ZERO], &isArray)); 5521 if (!isArray) { 5522 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5523 "BusinessError 401: Parameter error. The type of 'hostList' must be string array."); 5524 return result; 5525 } 5526 5527 std::vector<std::string> hosts; 5528 if (!GetHostList(env, argv[INTEGER_ZERO], hosts)) { 5529 WVLOG_E("get host list failed, GetHostList fail"); 5530 return result; 5531 } 5532 5533 NWebHelper::Instance().RemoveIntelligentTrackingPreventionBypassingList(hosts); 5534 NAPI_CALL(env, napi_get_undefined(env, &result)); 5535 return result; 5536 } 5537 ClearIntelligentTrackingPreventionBypassingList(napi_env env,napi_callback_info info)5538 napi_value NapiWebviewController::ClearIntelligentTrackingPreventionBypassingList( 5539 napi_env env, napi_callback_info info) 5540 { 5541 napi_value result = nullptr; 5542 WVLOG_I("Clear intelligent tracking prevention bypassing list."); 5543 NWebHelper::Instance().ClearIntelligentTrackingPreventionBypassingList(); 5544 NAPI_CALL(env, napi_get_undefined(env, &result)); 5545 return result; 5546 } 5547 GetDefaultUserAgent(napi_env env,napi_callback_info info)5548 napi_value NapiWebviewController::GetDefaultUserAgent(napi_env env, napi_callback_info info) 5549 { 5550 WVLOG_I("Get the default user agent."); 5551 napi_value result = nullptr; 5552 5553 std::string userAgent = NWebHelper::Instance().GetDefaultUserAgent(); 5554 NAPI_CALL(env, napi_create_string_utf8(env, userAgent.c_str(), userAgent.length(), &result)); 5555 return result; 5556 } 5557 PauseAllTimers(napi_env env,napi_callback_info info)5558 napi_value NapiWebviewController::PauseAllTimers(napi_env env, napi_callback_info info) 5559 { 5560 napi_value result = nullptr; 5561 NWebHelper::Instance().PauseAllTimers(); 5562 NAPI_CALL(env, napi_get_undefined(env, &result)); 5563 return result; 5564 } 5565 ResumeAllTimers(napi_env env,napi_callback_info info)5566 napi_value NapiWebviewController::ResumeAllTimers(napi_env env, napi_callback_info info) 5567 { 5568 napi_value result = nullptr; 5569 NWebHelper::Instance().ResumeAllTimers(); 5570 NAPI_CALL(env, napi_get_undefined(env, &result)); 5571 return result; 5572 } 5573 StartCamera(napi_env env,napi_callback_info info)5574 napi_value NapiWebviewController::StartCamera(napi_env env, napi_callback_info info) 5575 { 5576 napi_value result = nullptr; 5577 NAPI_CALL(env, napi_get_undefined(env, &result)); 5578 WebviewController* webviewController = GetWebviewController(env, info); 5579 if (!webviewController) { 5580 return result; 5581 } 5582 webviewController->StartCamera(); 5583 5584 return result; 5585 } 5586 StopCamera(napi_env env,napi_callback_info info)5587 napi_value NapiWebviewController::StopCamera(napi_env env, napi_callback_info info) 5588 { 5589 napi_value result = nullptr; 5590 NAPI_CALL(env, napi_get_undefined(env, &result)); 5591 WebviewController* webviewController = GetWebviewController(env, info); 5592 if (!webviewController) { 5593 return result; 5594 } 5595 webviewController->StopCamera(); 5596 5597 return result; 5598 } 5599 CloseCamera(napi_env env,napi_callback_info info)5600 napi_value NapiWebviewController::CloseCamera(napi_env env, napi_callback_info info) 5601 { 5602 napi_value result = nullptr; 5603 NAPI_CALL(env, napi_get_undefined(env, &result)); 5604 WebviewController* webviewController = GetWebviewController(env, info); 5605 if (!webviewController) { 5606 return result; 5607 } 5608 webviewController->CloseCamera(); 5609 5610 return result; 5611 } 5612 OnCreateNativeMediaPlayer(napi_env env,napi_callback_info info)5613 napi_value NapiWebviewController::OnCreateNativeMediaPlayer(napi_env env, napi_callback_info info) 5614 { 5615 WVLOG_D("put on_create_native_media_player callback"); 5616 5617 size_t argc = INTEGER_ONE; 5618 napi_value value = nullptr; 5619 napi_value argv[INTEGER_ONE]; 5620 napi_get_cb_info(env, info, &argc, argv, &value, nullptr); 5621 if (argc != INTEGER_ONE) { 5622 WVLOG_E("arg count %{public}zu is not equal to 1", argc); 5623 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5624 return nullptr; 5625 } 5626 5627 napi_valuetype valueType = napi_undefined; 5628 napi_typeof(env, argv[INTEGER_ZERO], &valueType); 5629 if (valueType != napi_function) { 5630 WVLOG_E("arg type is invalid"); 5631 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5632 return nullptr; 5633 } 5634 5635 napi_ref callback = nullptr; 5636 napi_create_reference(env, argv[INTEGER_ZERO], INTEGER_ONE, &callback); 5637 if (!callback) { 5638 WVLOG_E("failed to create reference for callback"); 5639 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5640 return nullptr; 5641 } 5642 5643 WebviewController* webviewController = GetWebviewController(env, info); 5644 if (!webviewController || !webviewController->IsInit()) { 5645 WVLOG_E("webview controller is null or not init"); 5646 napi_delete_reference(env, callback); 5647 return nullptr; 5648 } 5649 5650 webviewController->OnCreateNativeMediaPlayer(env, std::move(callback)); 5651 return nullptr; 5652 } 5653 SetRenderProcessMode(napi_env env,napi_callback_info info)5654 napi_value NapiWebviewController::SetRenderProcessMode( 5655 napi_env env, napi_callback_info info) 5656 { 5657 WVLOG_I("set render process mode."); 5658 napi_value result = nullptr; 5659 napi_value thisVar = nullptr; 5660 size_t argc = INTEGER_ONE; 5661 napi_value argv[INTEGER_ONE] = { 0 }; 5662 5663 NAPI_CALL(env, napi_get_undefined(env, &result)); 5664 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5665 if (argc != INTEGER_ONE) { 5666 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5667 "BusinessError 401: Parameter error. The number of params must be one."); 5668 return result; 5669 } 5670 5671 int32_t mode = 0; 5672 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], mode)) { 5673 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5674 "BusinessError 401: Parameter error. The type of 'mode' must be int."); 5675 return result; 5676 } 5677 5678 NWebHelper::Instance().SetRenderProcessMode( 5679 static_cast<RenderProcessMode>(mode)); 5680 5681 return result; 5682 } 5683 GetRenderProcessMode(napi_env env,napi_callback_info info)5684 napi_value NapiWebviewController::GetRenderProcessMode( 5685 napi_env env, napi_callback_info info) 5686 { 5687 WVLOG_I("get render mode."); 5688 napi_value result = nullptr; 5689 5690 int32_t mode = static_cast<int32_t>(NWebHelper::Instance().GetRenderProcessMode()); 5691 NAPI_CALL(env, napi_create_int32(env, mode, &result)); 5692 return result; 5693 } 5694 PrecompileJavaScript(napi_env env,napi_callback_info info)5695 napi_value NapiWebviewController::PrecompileJavaScript(napi_env env, napi_callback_info info) 5696 { 5697 napi_value thisVar = nullptr; 5698 napi_value result = nullptr; 5699 size_t argc = INTEGER_THREE; 5700 napi_value argv[INTEGER_THREE] = {0}; 5701 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5702 if (argc != INTEGER_THREE) { 5703 WVLOG_E("BusinessError: 401. Args count of 'PrecompileJavaScript' must be 3."); 5704 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5705 return result; 5706 } 5707 5708 WebviewController* webviewController = GetWebviewController(env, info); 5709 if (!webviewController) { 5710 WVLOG_E("PrecompileJavaScript: init webview controller error."); 5711 return result; 5712 } 5713 5714 std::string url; 5715 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], url) || url.empty()) { 5716 WVLOG_E("BusinessError: 401. The type of 'url' must be string."); 5717 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5718 return result; 5719 } 5720 5721 std::string script; 5722 bool parseResult = webviewController->ParseScriptContent(env, argv[INTEGER_ONE], script); 5723 if (!parseResult) { 5724 WVLOG_E("BusinessError: 401. The type of 'script' must be string or Uint8Array."); 5725 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5726 return result; 5727 } 5728 5729 auto cacheOptions = webviewController->ParseCacheOptions(env, argv[INTEGER_TWO]); 5730 5731 napi_deferred deferred = nullptr; 5732 napi_value promise = nullptr; 5733 napi_create_promise(env, &deferred, &promise); 5734 if (promise && deferred) { 5735 webviewController->PrecompileJavaScriptPromise(env, deferred, url, script, cacheOptions); 5736 return promise; 5737 } 5738 5739 return promise; 5740 } 5741 EnableBackForwardCache(napi_env env,napi_callback_info info)5742 napi_value NapiWebviewController::EnableBackForwardCache(napi_env env, napi_callback_info info) 5743 { 5744 napi_value thisVar = nullptr; 5745 napi_value result = nullptr; 5746 size_t argc = INTEGER_ONE; 5747 napi_value argv[INTEGER_ONE] = { 0 }; 5748 napi_get_undefined(env, &result); 5749 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5750 if (argc != INTEGER_ONE) { 5751 WVLOG_E("EnalbeBackForwardCache: wrong number of params."); 5752 NWebHelper::Instance().EnableBackForwardCache(false, false); 5753 NAPI_CALL(env, napi_get_undefined(env, &result)); 5754 return result; 5755 } 5756 5757 bool nativeEmbed = false; 5758 bool mediaTakeOver = false; 5759 napi_value embedObj = nullptr; 5760 napi_value mediaObj = nullptr; 5761 if (napi_get_named_property(env, argv[INTEGER_ZERO], "nativeEmbed", &embedObj) == napi_ok) { 5762 if (!NapiParseUtils::ParseBoolean(env, embedObj, nativeEmbed)) { 5763 nativeEmbed = false; 5764 } 5765 } 5766 5767 if (napi_get_named_property(env, argv[INTEGER_ZERO], "mediaTakeOver", &mediaObj) == napi_ok) { 5768 if (!NapiParseUtils::ParseBoolean(env, mediaObj, mediaTakeOver)) { 5769 mediaTakeOver = false; 5770 } 5771 } 5772 5773 NWebHelper::Instance().EnableBackForwardCache(nativeEmbed, mediaTakeOver); 5774 NAPI_CALL(env, napi_get_undefined(env, &result)); 5775 return result; 5776 } 5777 SetBackForwardCacheOptions(napi_env env,napi_callback_info info)5778 napi_value NapiWebviewController::SetBackForwardCacheOptions(napi_env env, napi_callback_info info) 5779 { 5780 napi_value thisVar = nullptr; 5781 napi_value result = nullptr; 5782 size_t argc = INTEGER_ONE; 5783 napi_value argv[INTEGER_ONE] = { 0 }; 5784 napi_get_undefined(env, &result); 5785 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5786 WebviewController* webviewController = GetWebviewController(env, info); 5787 if (!webviewController) { 5788 WVLOG_E("SetBackForwardCacheOptions: Init webview controller error."); 5789 return result; 5790 } 5791 5792 if (argc != INTEGER_ONE) { 5793 WVLOG_E("SetBackForwardCacheOptions: wrong number of params."); 5794 webviewController->SetBackForwardCacheOptions( 5795 BFCACHE_DEFAULT_SIZE, BFCACHE_DEFAULT_TIMETOLIVE); 5796 NAPI_CALL(env, napi_get_undefined(env, &result)); 5797 return result; 5798 } 5799 5800 int32_t size = BFCACHE_DEFAULT_SIZE; 5801 int32_t timeToLive = BFCACHE_DEFAULT_TIMETOLIVE; 5802 napi_value sizeObj = nullptr; 5803 napi_value timeToLiveObj = nullptr; 5804 if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &sizeObj) == napi_ok) { 5805 if (!NapiParseUtils::ParseInt32(env, sizeObj, size)) { 5806 size = BFCACHE_DEFAULT_SIZE; 5807 } 5808 } 5809 5810 if (napi_get_named_property(env, argv[INTEGER_ZERO], "timeToLive", &timeToLiveObj) == napi_ok) { 5811 if (!NapiParseUtils::ParseInt32(env, timeToLiveObj, timeToLive)) { 5812 timeToLive = BFCACHE_DEFAULT_TIMETOLIVE; 5813 } 5814 } 5815 5816 webviewController->SetBackForwardCacheOptions(size, timeToLive); 5817 NAPI_CALL(env, napi_get_undefined(env, &result)); 5818 return result; 5819 } 5820 InjectOfflineResources(napi_env env,napi_callback_info info)5821 napi_value NapiWebviewController::InjectOfflineResources(napi_env env, napi_callback_info info) 5822 { 5823 napi_value thisVar = nullptr; 5824 napi_value result = nullptr; 5825 size_t argc = INTEGER_ONE; 5826 napi_value argv[INTEGER_ONE] = {0}; 5827 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5828 if (argc != INTEGER_ONE) { 5829 WVLOG_E("BusinessError: 401. Args count of 'InjectOfflineResource' must be 1."); 5830 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5831 return result; 5832 } 5833 5834 napi_value resourcesList = argv[INTEGER_ZERO]; 5835 bool isArray = false; 5836 napi_is_array(env, resourcesList, &isArray); 5837 if (!isArray) { 5838 WVLOG_E("BusinessError: 401. The type of 'resourceMaps' must be Array"); 5839 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5840 return result; 5841 } 5842 5843 AddResourcesToMemoryCache(env, info, resourcesList); 5844 return result; 5845 } 5846 AddResourcesToMemoryCache(napi_env env,napi_callback_info info,napi_value & resourcesList)5847 void NapiWebviewController::AddResourcesToMemoryCache(napi_env env, 5848 napi_callback_info info, 5849 napi_value& resourcesList) 5850 { 5851 uint32_t resourcesCount = 0; 5852 napi_get_array_length(env, resourcesList, &resourcesCount); 5853 5854 if (resourcesCount > MAX_RESOURCES_COUNT || resourcesCount == 0) { 5855 WVLOG_E("BusinessError: 401. The size of 'resourceMaps' must less than %{public}zu and not 0", 5856 MAX_RESOURCES_COUNT); 5857 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5858 return; 5859 } 5860 5861 for (uint32_t i = 0 ; i < resourcesCount ; i++) { 5862 napi_value urlListObj = nullptr; 5863 napi_value resourceObj = nullptr; 5864 napi_value headersObj = nullptr; 5865 napi_value typeObj = nullptr; 5866 napi_value obj = nullptr; 5867 5868 napi_create_array(env, &headersObj); 5869 napi_create_array(env, &urlListObj); 5870 5871 napi_get_element(env, resourcesList, i, &obj); 5872 if ((napi_get_named_property(env, obj, "urlList", &urlListObj) != napi_ok) || 5873 (napi_get_named_property(env, obj, "resource", &resourceObj) != napi_ok) || 5874 (napi_get_named_property(env, obj, "responseHeaders", &headersObj) != napi_ok) || 5875 (napi_get_named_property(env, obj, "type", &typeObj) != napi_ok)) { 5876 WVLOG_E("InjectOfflineResources: parse params from resource map failed."); 5877 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5878 continue; 5879 } 5880 5881 OfflineResourceValue resourceValue; 5882 resourceValue.urlList = urlListObj; 5883 resourceValue.resource = resourceObj; 5884 resourceValue.responseHeaders = headersObj; 5885 resourceValue.type = typeObj; 5886 AddResourceItemToMemoryCache(env, info, resourceValue); 5887 } 5888 } 5889 AddResourceItemToMemoryCache(napi_env env,napi_callback_info info,OfflineResourceValue resourceValue)5890 void NapiWebviewController::AddResourceItemToMemoryCache(napi_env env, 5891 napi_callback_info info, 5892 OfflineResourceValue resourceValue) 5893 { 5894 WebviewController* webviewController = GetWebviewController(env, info); 5895 if (!webviewController) { 5896 WVLOG_E("InjectOfflineResource: init webview controller error."); 5897 return; 5898 } 5899 5900 std::vector<std::string> urlList; 5901 ParseURLResult result = webviewController->ParseURLList(env, resourceValue.urlList, urlList); 5902 if (result != ParseURLResult::OK) { 5903 auto errCode = result == ParseURLResult::FAILED ? PARAM_CHECK_ERROR : INVALID_URL; 5904 if (errCode == PARAM_CHECK_ERROR) { 5905 WVLOG_E("BusinessError: 401. The type of 'urlList' must be Array of string."); 5906 } 5907 BusinessError::ThrowErrorByErrcode(env, errCode); 5908 return; 5909 } 5910 5911 std::vector<uint8_t> resource = webviewController->ParseUint8Array(env, resourceValue.resource); 5912 if (resource.empty() || resource.size() > MAX_RESOURCE_SIZE) { 5913 WVLOG_E("BusinessError: 401. The type of 'resource' must be Uint8Array. " 5914 "'resource' size must less than %{public}zu and must not be empty.", MAX_RESOURCE_SIZE); 5915 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5916 return; 5917 } 5918 5919 std::map<std::string, std::string> responseHeaders; 5920 if (!webviewController->ParseResponseHeaders(env, resourceValue.responseHeaders, responseHeaders)) { 5921 WVLOG_E("BusinessError: 401. The type of 'responseHeaders' must be Array of 'WebHeader'."); 5922 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5923 return; 5924 } 5925 5926 uint32_t type = 0; 5927 if (!NapiParseUtils::ParseUint32(env, resourceValue.type, type)) { 5928 WVLOG_E("BusinessError: 401. The type of 'type' must be one kind of 'OfflineResourceType'."); 5929 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 5930 return; 5931 } 5932 5933 webviewController->InjectOfflineResource(urlList, resource, responseHeaders, type); 5934 } 5935 SetHostIP(napi_env env,napi_callback_info info)5936 napi_value NapiWebviewController::SetHostIP(napi_env env, napi_callback_info info) 5937 { 5938 napi_value thisVar = nullptr; 5939 napi_value result = nullptr; 5940 size_t argc = INTEGER_THREE; 5941 napi_value argv[INTEGER_THREE] = { 0 }; 5942 std::string hostName; 5943 std::string address; 5944 int32_t aliveTime = INTEGER_ZERO; 5945 5946 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5947 if (argc != INTEGER_THREE) { 5948 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5949 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "three")); 5950 return result; 5951 } 5952 5953 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], hostName) || 5954 !NapiParseUtils::ParseString(env, argv[INTEGER_ONE], address) || 5955 !NapiParseUtils::ParseInt32(env, argv[INTEGER_TWO], aliveTime)) { 5956 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, ParamCheckErrorMsgTemplate::PARAM_TYEPS_ERROR); 5957 return result; 5958 } 5959 5960 if (!ParseIP(env, argv[INTEGER_ONE], address)) { 5961 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5962 "BusinessError 401: IP address error."); 5963 return result; 5964 } 5965 5966 NWebHelper::Instance().SetHostIP(hostName, address, aliveTime); 5967 NAPI_CALL(env, napi_get_undefined(env, &result)); 5968 5969 return result; 5970 } 5971 ClearHostIP(napi_env env,napi_callback_info info)5972 napi_value NapiWebviewController::ClearHostIP(napi_env env, napi_callback_info info) 5973 { 5974 napi_value thisVar = nullptr; 5975 napi_value result = nullptr; 5976 size_t argc = INTEGER_ONE; 5977 napi_value argv[INTEGER_ONE] = { 0 }; 5978 std::string hostName; 5979 5980 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 5981 if (argc != INTEGER_ONE) { 5982 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5983 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 5984 return result; 5985 } 5986 5987 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], hostName)) { 5988 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 5989 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "hostName", "string")); 5990 return result; 5991 } 5992 5993 NWebHelper::Instance().ClearHostIP(hostName); 5994 NAPI_CALL(env, napi_get_undefined(env, &result)); 5995 return result; 5996 } 5997 WarmupServiceWorker(napi_env env,napi_callback_info info)5998 napi_value NapiWebviewController::WarmupServiceWorker(napi_env env, napi_callback_info info) 5999 { 6000 napi_value thisVar = nullptr; 6001 napi_value result = nullptr; 6002 size_t argc = INTEGER_ONE; 6003 napi_value argv[INTEGER_ONE] = { 0 }; 6004 napi_get_undefined(env, &result); 6005 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6006 if (argc != INTEGER_ONE) { 6007 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 6008 return result; 6009 } 6010 6011 std::string url; 6012 if (!ParsePrepareUrl(env, argv[INTEGER_ZERO], url)) { 6013 BusinessError::ThrowErrorByErrcode(env, INVALID_URL); 6014 return result; 6015 } 6016 6017 NWebHelper::Instance().WarmupServiceWorker(url); 6018 return result; 6019 } 6020 GetSurfaceId(napi_env env,napi_callback_info info)6021 napi_value NapiWebviewController::GetSurfaceId(napi_env env, napi_callback_info info) 6022 { 6023 napi_value result = nullptr; 6024 WebviewController *webviewController = GetWebviewController(env, info); 6025 if (!webviewController) { 6026 return nullptr; 6027 } 6028 6029 std::string surfaceId = webviewController->GetSurfaceId(); 6030 napi_create_string_utf8(env, surfaceId.c_str(), surfaceId.length(), &result); 6031 return result; 6032 } 6033 EnableWholeWebPageDrawing(napi_env env,napi_callback_info info)6034 napi_value NapiWebviewController::EnableWholeWebPageDrawing(napi_env env, napi_callback_info info) 6035 { 6036 napi_value result = nullptr; 6037 NWebHelper::Instance().SetWholeWebDrawing(); 6038 NAPI_CALL(env, napi_get_undefined(env, &result)); 6039 return result; 6040 } 6041 EnableAdsBlock(napi_env env,napi_callback_info info)6042 napi_value NapiWebviewController::EnableAdsBlock( 6043 napi_env env, napi_callback_info info) 6044 { 6045 napi_value result = nullptr; 6046 napi_value thisVar = nullptr; 6047 size_t argc = INTEGER_ONE; 6048 napi_value argv[INTEGER_ONE] = { 0 }; 6049 6050 NAPI_CALL(env, napi_get_undefined(env, &result)); 6051 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6052 if (argc != INTEGER_ONE) { 6053 WVLOG_E("EnableAdsBlock: args count is not allowed."); 6054 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6055 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 6056 return result; 6057 } 6058 6059 bool enabled = false; 6060 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ZERO], enabled)) { 6061 WVLOG_E("EnableAdsBlock: the given enabled is not allowed."); 6062 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6063 "BusinessError 401: Parameter error. The type of 'enable' must be boolean."); 6064 return result; 6065 } 6066 6067 WebviewController *webviewController = GetWebviewController(env, info); 6068 if (!webviewController) { 6069 WVLOG_E("EnableAdsBlock: init webview controller error."); 6070 return result; 6071 } 6072 6073 WVLOG_I("EnableAdsBlock: %{public}s", (enabled ? "true" : "false")); 6074 webviewController->EnableAdsBlock(enabled); 6075 return result; 6076 } 6077 IsAdsBlockEnabled(napi_env env,napi_callback_info info)6078 napi_value NapiWebviewController::IsAdsBlockEnabled(napi_env env, napi_callback_info info) 6079 { 6080 napi_value result = nullptr; 6081 WebviewController *webviewController = GetWebviewController(env, info); 6082 if (!webviewController) { 6083 return nullptr; 6084 } 6085 6086 bool isAdsBlockEnabled = webviewController->IsAdsBlockEnabled(); 6087 NAPI_CALL(env, napi_get_boolean(env, isAdsBlockEnabled, &result)); 6088 return result; 6089 } 6090 IsAdsBlockEnabledForCurPage(napi_env env,napi_callback_info info)6091 napi_value NapiWebviewController::IsAdsBlockEnabledForCurPage(napi_env env, napi_callback_info info) 6092 { 6093 napi_value result = nullptr; 6094 WebviewController *webviewController = GetWebviewController(env, info); 6095 if (!webviewController) { 6096 return nullptr; 6097 } 6098 6099 bool isAdsBlockEnabledForCurPage = webviewController->IsAdsBlockEnabledForCurPage(); 6100 NAPI_CALL(env, napi_get_boolean(env, isAdsBlockEnabledForCurPage, &result)); 6101 return result; 6102 } 6103 CreateWebPageSnapshotResultCallback(napi_env env,napi_ref jsCallback,bool check,int32_t inputWidth,int32_t inputHeight)6104 WebSnapshotCallback CreateWebPageSnapshotResultCallback( 6105 napi_env env, napi_ref jsCallback, bool check, int32_t inputWidth, int32_t inputHeight) 6106 { 6107 return 6108 [env, jCallback = std::move(jsCallback), check, inputWidth, inputHeight]( 6109 const char *returnId, bool returnStatus, float radio, void *returnData, 6110 int returnWidth, int returnHeight) { 6111 WVLOG_I("WebPageSnapshot return napi callback"); 6112 napi_value jsResult = nullptr; 6113 napi_create_object(env, &jsResult); 6114 6115 napi_value jsPixelMap = nullptr; 6116 Media::InitializationOptions opt; 6117 opt.size.width = static_cast<int32_t>(returnWidth); 6118 opt.size.height = static_cast<int32_t>(returnHeight); 6119 opt.pixelFormat = Media::PixelFormat::RGBA_8888; 6120 opt.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; 6121 opt.editable = true; 6122 auto pixelMap = Media::PixelMap::Create(opt); 6123 if (pixelMap != nullptr) { 6124 uint64_t stride = static_cast<uint64_t>(returnWidth) << 2; 6125 uint64_t bufferSize = stride * static_cast<uint64_t>(returnHeight); 6126 pixelMap->WritePixels(static_cast<const uint8_t *>(returnData), bufferSize); 6127 std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release()); 6128 jsPixelMap = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs); 6129 } else { 6130 WVLOG_E("WebPageSnapshot create pixel map error"); 6131 } 6132 napi_set_named_property(env, jsResult, "imagePixelMap", jsPixelMap); 6133 6134 int returnJsWidth = 0; 6135 int returnJsHeight = 0; 6136 if (radio > 0) { 6137 returnJsWidth = returnWidth / radio; 6138 returnJsHeight = returnHeight / radio; 6139 } 6140 if (check) { 6141 if (std::abs(returnJsWidth - inputWidth) < INTEGER_THREE) { 6142 returnJsWidth = inputWidth; 6143 } 6144 6145 if (std::abs(returnJsHeight - inputHeight) < INTEGER_THREE) { 6146 returnJsHeight = inputHeight; 6147 } 6148 } 6149 napi_value jsSizeObj = nullptr; 6150 napi_create_object(env, &jsSizeObj); 6151 napi_value jsSize[2] = {0}; 6152 napi_create_int32(env, returnJsWidth, &jsSize[0]); 6153 napi_create_int32(env, returnJsHeight, &jsSize[1]); 6154 napi_set_named_property(env, jsSizeObj, "width", jsSize[0]); 6155 napi_set_named_property(env, jsSizeObj, "height", jsSize[1]); 6156 napi_set_named_property(env, jsResult, "size", jsSizeObj); 6157 6158 napi_value jsId = nullptr; 6159 napi_create_string_utf8(env, returnId, strlen(returnId), &jsId); 6160 napi_set_named_property(env, jsResult, "id", jsId); 6161 6162 napi_value jsStatus = nullptr; 6163 napi_get_boolean(env, returnStatus, &jsStatus); 6164 napi_set_named_property(env, jsResult, "status", jsStatus); 6165 6166 napi_value jsError = nullptr; 6167 napi_get_undefined(env, &jsError); 6168 napi_value args[INTEGER_TWO] = {jsError, jsResult}; 6169 6170 napi_value callback = nullptr; 6171 napi_value callbackResult = nullptr; 6172 napi_get_reference_value(env, jCallback, &callback); 6173 6174 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult); 6175 napi_delete_reference(env, jCallback); 6176 g_inWebPageSnapshot = false; 6177 }; 6178 } 6179 WebPageSnapshot(napi_env env,napi_callback_info info)6180 napi_value NapiWebviewController::WebPageSnapshot(napi_env env, napi_callback_info info) 6181 { 6182 napi_value thisVar = nullptr; 6183 napi_value result = nullptr; 6184 size_t argc = INTEGER_TWO; 6185 napi_value argv[INTEGER_TWO] = {0}; 6186 6187 napi_get_undefined(env, &result); 6188 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6189 6190 if (argc != INTEGER_TWO) { 6191 WVLOG_E("WebPageSnapshot: args count is not allowed."); 6192 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 6193 return result; 6194 } 6195 6196 napi_ref callback = nullptr; 6197 napi_create_reference(env, argv[INTEGER_ONE], INTEGER_ONE, &callback); 6198 if (!callback) { 6199 WVLOG_E("WebPageSnapshot failed to create reference for callback"); 6200 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 6201 return result; 6202 } 6203 6204 WebviewController *webviewController = GetWebviewController(env, info); 6205 if (!webviewController) { 6206 WVLOG_E("WebPageSnapshot init webview controller error."); 6207 napi_delete_reference(env, callback); 6208 return result; 6209 } 6210 6211 if (g_inWebPageSnapshot) { 6212 JsErrorCallback(env, std::move(callback), FUNCTION_NOT_ENABLE); 6213 return result; 6214 } 6215 g_inWebPageSnapshot = true; 6216 6217 napi_value snapshotId = nullptr; 6218 napi_value snapshotSize = nullptr; 6219 napi_value snapshotSizeWidth = nullptr; 6220 napi_value snapshotSizeHeight = nullptr; 6221 6222 std::string nativeSnapshotId = ""; 6223 int32_t nativeSnapshotSizeWidth = 0; 6224 int32_t nativeSnapshotSizeHeight = 0; 6225 PixelUnit nativeSnapshotSizeWidthType = PixelUnit::NONE; 6226 PixelUnit nativeSnapshotSizeHeightType = PixelUnit::NONE; 6227 PixelUnit nativeSnapshotSizeType = PixelUnit::NONE; 6228 6229 if (napi_get_named_property(env, argv[INTEGER_ZERO], "id", &snapshotId) == napi_ok) { 6230 NapiParseUtils::ParseString(env, snapshotId, nativeSnapshotId); 6231 } 6232 6233 if (napi_get_named_property(env, argv[INTEGER_ZERO], "size", &snapshotSize) == napi_ok) { 6234 if (napi_get_named_property(env, snapshotSize, "width", &snapshotSizeWidth) == napi_ok) { 6235 if (!webviewController->ParseJsLengthToInt(env, snapshotSizeWidth, 6236 nativeSnapshotSizeWidthType, 6237 nativeSnapshotSizeWidth)) { 6238 JsErrorCallback(env, std::move(callback), PARAM_CHECK_ERROR); 6239 g_inWebPageSnapshot = false; 6240 return result; 6241 } 6242 } 6243 if (napi_get_named_property(env, snapshotSize, "height", &snapshotSizeHeight) == napi_ok) { 6244 if (!webviewController->ParseJsLengthToInt(env, snapshotSizeHeight, 6245 nativeSnapshotSizeHeightType, 6246 nativeSnapshotSizeHeight)) { 6247 JsErrorCallback(env, std::move(callback), PARAM_CHECK_ERROR); 6248 g_inWebPageSnapshot = false; 6249 return result; 6250 } 6251 } 6252 } 6253 6254 if (nativeSnapshotSizeWidthType != PixelUnit::NONE && nativeSnapshotSizeHeightType != PixelUnit::NONE && 6255 nativeSnapshotSizeWidthType != nativeSnapshotSizeHeightType) { 6256 WVLOG_E("WebPageSnapshot input different pixel unit"); 6257 JsErrorCallback(env, std::move(callback), PARAM_CHECK_ERROR); 6258 g_inWebPageSnapshot = false; 6259 return result; 6260 } 6261 6262 if (nativeSnapshotSizeWidthType != PixelUnit::NONE) { 6263 nativeSnapshotSizeType = nativeSnapshotSizeWidthType; 6264 } 6265 if (nativeSnapshotSizeHeightType != PixelUnit::NONE) { 6266 nativeSnapshotSizeType = nativeSnapshotSizeHeightType; 6267 } 6268 if (nativeSnapshotSizeWidth < 0 || nativeSnapshotSizeHeight < 0) { 6269 WVLOG_E("WebPageSnapshot input pixel length less than 0"); 6270 JsErrorCallback(env, std::move(callback), PARAM_CHECK_ERROR); 6271 g_inWebPageSnapshot = false; 6272 return result; 6273 } 6274 bool pixelCheck = false; 6275 if (nativeSnapshotSizeType == PixelUnit::VP) { 6276 pixelCheck = true; 6277 } 6278 WVLOG_I("WebPageSnapshot pixel type :%{public}d", static_cast<int>(nativeSnapshotSizeType)); 6279 6280 auto resultCallback = CreateWebPageSnapshotResultCallback( 6281 env, std::move(callback), pixelCheck, nativeSnapshotSizeWidth, nativeSnapshotSizeHeight); 6282 6283 ErrCode ret = webviewController->WebPageSnapshot(nativeSnapshotId.c_str(), 6284 nativeSnapshotSizeType, 6285 nativeSnapshotSizeWidth, 6286 nativeSnapshotSizeHeight, 6287 std::move(resultCallback)); 6288 if (ret != NO_ERROR) { 6289 g_inWebPageSnapshot = false; 6290 BusinessError::ThrowErrorByErrcode(env, ret); 6291 } 6292 return result; 6293 } 6294 SetUrlTrustList(napi_env env,napi_callback_info info)6295 napi_value NapiWebviewController::SetUrlTrustList(napi_env env, napi_callback_info info) 6296 { 6297 WVLOG_D("SetUrlTrustList invoked"); 6298 6299 napi_value result = nullptr; 6300 NAPI_CALL(env, napi_get_undefined(env, &result)); 6301 6302 napi_value thisVar = nullptr; 6303 size_t argc = INTEGER_ONE; 6304 napi_value argv[INTEGER_ONE] = { 0 }; 6305 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6306 if (argc != INTEGER_ONE) { 6307 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6308 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 6309 return result; 6310 } 6311 6312 std::string urlTrustList; 6313 if (!NapiParseUtils::ParseString(env, argv[0], urlTrustList)) { 6314 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6315 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "urlTrustList", "string")); 6316 return result; 6317 } 6318 if (urlTrustList.size() > MAX_URL_TRUST_LIST_STR_LEN) { 6319 WVLOG_E("url trust list len is too large."); 6320 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 6321 return result; 6322 } 6323 6324 WebviewController* webviewController = GetWebviewController(env, info); 6325 if (!webviewController) { 6326 WVLOG_E("webview controller is null or not init"); 6327 return result; 6328 } 6329 6330 std::string detailMsg; 6331 ErrCode ret = webviewController->SetUrlTrustList(urlTrustList, detailMsg); 6332 if (ret != NO_ERROR) { 6333 WVLOG_E("SetUrlTrustList failed, error code: %{public}d", ret); 6334 BusinessError::ThrowErrorByErrcode(env, ret, 6335 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_DETAIL_ERROR_MSG, detailMsg.c_str())); 6336 return result; 6337 } 6338 return result; 6339 } 6340 UpdateInstanceId(napi_env env,napi_callback_info info)6341 napi_value NapiWebviewController::UpdateInstanceId(napi_env env, napi_callback_info info) 6342 { 6343 WVLOG_D("Instance changed"); 6344 napi_value result = nullptr; 6345 napi_value thisVar = nullptr; 6346 size_t argc = INTEGER_ONE; 6347 napi_value argv[INTEGER_ONE] = { 0 }; 6348 6349 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6350 if (argc != INTEGER_ONE) { 6351 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 6352 return result; 6353 } 6354 6355 int32_t newId = 0; 6356 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], newId)) { 6357 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 6358 return result; 6359 } 6360 6361 WebviewController *webviewController = nullptr; 6362 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController); 6363 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) { 6364 return result; 6365 } 6366 6367 webviewController->UpdateInstanceId(newId); 6368 6369 NAPI_CALL(env, napi_get_undefined(env, &result)); 6370 return result; 6371 } 6372 SetPathAllowingUniversalAccess(napi_env env,napi_callback_info info)6373 napi_value NapiWebviewController::SetPathAllowingUniversalAccess( 6374 napi_env env, napi_callback_info info) 6375 { 6376 napi_value result = nullptr; 6377 napi_value thisVar = nullptr; 6378 size_t argc = INTEGER_ONE; 6379 napi_value argv[INTEGER_ONE] = { 0 }; 6380 NAPI_CALL(env, napi_get_undefined(env, &result)); 6381 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6382 WebviewController *webviewController = GetWebviewController(env, info); 6383 if (!webviewController) { 6384 WVLOG_E("SetPathAllowingUniversalAccess init webview controller error."); 6385 return result; 6386 } 6387 bool isArray = false; 6388 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ZERO], &isArray)); 6389 if (!isArray) { 6390 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6391 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "pathList", "Array<string>")); 6392 return result; 6393 } 6394 std::vector<std::string> pathList; 6395 uint32_t pathCount = 0; 6396 NAPI_CALL(env, napi_get_array_length(env, argv[INTEGER_ZERO], &pathCount)); 6397 for (uint32_t i = 0 ; i < pathCount ; i++) { 6398 napi_value pathItem = nullptr; 6399 napi_get_element(env, argv[INTEGER_ZERO], i, &pathItem); 6400 std::string path; 6401 if (!NapiParseUtils::ParseString(env, pathItem, path)) { 6402 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6403 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "pathList", "Array<string>")); 6404 return result; 6405 } 6406 if (path.empty()) { 6407 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6408 NWebError::FormatString("BusinessError 401: Parameter error. Path: '%s' is invalid", path.c_str())); 6409 return result; 6410 } 6411 pathList.emplace_back(path); 6412 } 6413 std::string errorPath; 6414 webviewController->SetPathAllowingUniversalAccess(pathList, errorPath); 6415 if (!errorPath.empty()) { 6416 WVLOG_E("%{public}s is invalid.", errorPath.c_str()); 6417 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6418 NWebError::FormatString("BusinessError 401: Parameter error. Path: '%s' is invalid", errorPath.c_str())); 6419 } 6420 return result; 6421 } 6422 ScrollByWithResult(napi_env env,napi_callback_info info)6423 napi_value NapiWebviewController::ScrollByWithResult(napi_env env, napi_callback_info info) 6424 { 6425 napi_value thisVar = nullptr; 6426 napi_value result = nullptr; 6427 size_t argc = INTEGER_TWO; 6428 napi_value argv[INTEGER_TWO] = { 0 }; 6429 float deltaX; 6430 float deltaY; 6431 6432 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6433 if (argc != INTEGER_TWO) { 6434 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6435 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "two")); 6436 return result; 6437 } 6438 6439 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], deltaX)) { 6440 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6441 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "deltaX", "number")); 6442 return result; 6443 } 6444 6445 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], deltaY)) { 6446 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6447 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "deltaY", "number")); 6448 return result; 6449 } 6450 6451 WebviewController *webviewController = GetWebviewController(env, info); 6452 if (!webviewController) { 6453 return nullptr; 6454 } 6455 6456 bool scrollByWithResult = webviewController->ScrollByWithResult(deltaX, deltaY); 6457 NAPI_CALL(env, napi_get_boolean(env, scrollByWithResult, &result)); 6458 return result; 6459 } 6460 GetScrollOffset(napi_env env,napi_callback_info info)6461 napi_value NapiWebviewController::GetScrollOffset(napi_env env, 6462 napi_callback_info info) 6463 { 6464 napi_value result = nullptr; 6465 napi_value horizontal; 6466 napi_value vertical; 6467 float offsetX = 0; 6468 float offsetY = 0; 6469 6470 WebviewController* webviewController = GetWebviewController(env, info); 6471 if (!webviewController) { 6472 return nullptr; 6473 } 6474 6475 webviewController->GetScrollOffset(&offsetX, &offsetY); 6476 6477 napi_create_object(env, &result); 6478 napi_create_double(env, static_cast<double>(offsetX), &horizontal); 6479 napi_create_double(env, static_cast<double>(offsetY), &vertical); 6480 napi_set_named_property(env, result, "x", horizontal); 6481 napi_set_named_property(env, result, "y", vertical); 6482 return result; 6483 } 6484 TrimMemoryByPressureLevel(napi_env env,napi_callback_info info)6485 napi_value NapiWebviewController::TrimMemoryByPressureLevel(napi_env env, 6486 napi_callback_info info) 6487 { 6488 napi_value thisVar = nullptr; 6489 napi_value result = nullptr; 6490 size_t argc = INTEGER_ONE; 6491 napi_value argv[INTEGER_ONE] = { 0 }; 6492 int32_t memoryLevel; 6493 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 6494 if (argc != INTEGER_ONE) { 6495 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6496 NWebError::FormatString( 6497 ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); 6498 return result; 6499 } 6500 6501 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], memoryLevel)) { 6502 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, 6503 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, 6504 "PressureLevel", "number")); 6505 return result; 6506 } 6507 6508 memoryLevel = memoryLevel == 1 ? 0 : memoryLevel; 6509 NWebHelper::Instance().TrimMemoryByPressureLevel(memoryLevel); 6510 NAPI_CALL(env, napi_get_undefined(env, &result)); 6511 return result; 6512 } 6513 } // namespace NWeb 6514 } // namespace OHOS 6515