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