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 <cstdint>
19 #include <regex>
20 #include <securec.h>
21 #include <unistd.h>
22 #include <uv.h>
23
24 #include "application_context.h"
25 #include "business_error.h"
26 #include "napi_parse_utils.h"
27 #include "nweb.h"
28 #include "nweb_helper.h"
29 #include "nweb_log.h"
30 #include "pixel_map.h"
31 #include "pixel_map_napi.h"
32 #include "web_errors.h"
33 #include "webview_javascript_execute_callback.h"
34
35 namespace OHOS {
36 namespace NWeb {
37 using namespace NWebError;
38 using NWebError::NO_ERROR;
39
40 namespace {
41 constexpr uint32_t URL_MAXIMUM = 2048;
42 constexpr uint32_t SOCKET_MAXIMUM = 6;
43 constexpr char URL_REGEXPR[] = "^http(s)?:\\/\\/.+";
44
ParsePrepareUrl(napi_env env,napi_value urlObj,std::string & url)45 bool ParsePrepareUrl(napi_env env, napi_value urlObj, std::string& url)
46 {
47 napi_valuetype valueType = napi_null;
48 napi_typeof(env, urlObj, &valueType);
49
50 if (valueType == napi_string) {
51 NapiParseUtils::ParseString(env, urlObj, url);
52 if (url.size() > URL_MAXIMUM) {
53 WVLOG_E("The URL exceeds the maximum length of %{public}d", URL_MAXIMUM);
54 return false;
55 }
56
57 if (!regex_match(url, std::regex(URL_REGEXPR, std::regex_constants::icase))) {
58 WVLOG_E("ParsePrepareUrl error");
59 return false;
60 }
61
62 return true;
63 }
64
65 WVLOG_E("Unable to parse type from url object.");
66 return false;
67 }
68
GetArrayValueType(napi_env env,napi_value * argv,bool & isDouble)69 napi_valuetype GetArrayValueType(napi_env env, napi_value* argv, bool& isDouble)
70 {
71 uint32_t arrayLength = 0;
72 napi_get_array_length(env, argv[INTEGER_ZERO], &arrayLength);
73 napi_valuetype valueTypeFirst = napi_undefined;
74 napi_valuetype valueTypeCur = napi_undefined;
75 for (uint32_t i = 0; i < arrayLength; ++i) {
76 napi_value obj = nullptr;
77 napi_get_element(env, argv[INTEGER_ZERO], i, &obj);
78 napi_typeof(env, obj, &valueTypeCur);
79 if (i == 0) {
80 valueTypeFirst = valueTypeCur;
81 }
82 if (valueTypeCur != napi_string && valueTypeCur != napi_number && valueTypeCur != napi_boolean) {
83 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
84 return napi_undefined;
85 }
86 if (valueTypeCur != valueTypeFirst) {
87 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
88 return napi_undefined;
89 }
90 if (valueTypeFirst == napi_number) {
91 int32_t elementInt32 = 0;
92 double elementDouble = 0.0;
93 bool isReadValue32 = napi_get_value_int32(env, obj, &elementInt32) == napi_ok;
94 bool isReadDouble = napi_get_value_double(env, obj, &elementDouble) == napi_ok;
95 constexpr double MINIMAL_ERROR = 0.000001;
96 if (isReadValue32 && isReadDouble) {
97 isDouble = abs(elementDouble - elementInt32 * 1.0) > MINIMAL_ERROR;
98 } else if (isReadDouble) {
99 isDouble = true;
100 }
101 }
102 }
103 return valueTypeFirst;
104 }
105
SetArrayHandlerBoolean(napi_env env,napi_value * argv,WebMessageExt * webMessageExt)106 void SetArrayHandlerBoolean(napi_env env, napi_value* argv, WebMessageExt* webMessageExt)
107 {
108 std::vector<bool> outValue;
109 if (!NapiParseUtils::ParseBooleanArray(env, argv[INTEGER_ZERO], outValue)) {
110 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
111 return;
112 }
113 webMessageExt->SetBooleanArray(outValue);
114 }
115
SetArrayHandlerString(napi_env env,napi_value * argv,WebMessageExt * webMessageExt)116 void SetArrayHandlerString(napi_env env, napi_value* argv, WebMessageExt* webMessageExt)
117 {
118 std::vector<std::string> outValue;
119 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_ZERO], outValue)) {
120 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
121 return;
122 }
123 webMessageExt->SetStringArray(outValue);
124 }
125
SetArrayHandlerInteger(napi_env env,napi_value * argv,WebMessageExt * webMessageExt)126 void SetArrayHandlerInteger(napi_env env, napi_value* argv, WebMessageExt* webMessageExt)
127 {
128 std::vector<int64_t> outValue;
129 if (!NapiParseUtils::ParseInt64Array(env, argv[INTEGER_ZERO], outValue)) {
130 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
131 return;
132 }
133 webMessageExt->SetInt64Array(outValue);
134 }
135
SetArrayHandlerDouble(napi_env env,napi_value * argv,WebMessageExt * webMessageExt)136 void SetArrayHandlerDouble(napi_env env, napi_value* argv, WebMessageExt* webMessageExt)
137 {
138 std::vector<double> outValue;
139 if (!NapiParseUtils::ParseDoubleArray(env, argv[INTEGER_ZERO], outValue)) {
140 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
141 return;
142 }
143 webMessageExt->SetDoubleArray(outValue);
144 }
145 } // namespace
146
147 thread_local napi_ref g_classWebMsgPort;
148 thread_local napi_ref g_historyListRef;
149 thread_local napi_ref g_webMsgExtClassRef;
Init(napi_env env,napi_value exports)150 napi_value NapiWebviewController::Init(napi_env env, napi_value exports)
151 {
152 napi_property_descriptor properties[] = {
153 DECLARE_NAPI_STATIC_FUNCTION("initializeWebEngine", NapiWebviewController::InitializeWebEngine),
154 DECLARE_NAPI_STATIC_FUNCTION("setHttpDns", NapiWebviewController::SetHttpDns),
155 DECLARE_NAPI_STATIC_FUNCTION("setWebDebuggingAccess", NapiWebviewController::SetWebDebuggingAccess),
156 DECLARE_NAPI_FUNCTION("getWebDebuggingAccess", NapiWebviewController::InnerGetWebDebuggingAccess),
157 DECLARE_NAPI_FUNCTION("setWebId", NapiWebviewController::SetWebId),
158 DECLARE_NAPI_FUNCTION("jsProxy", NapiWebviewController::InnerJsProxy),
159 DECLARE_NAPI_FUNCTION("getCustomeSchemeCmdLine", NapiWebviewController::InnerGetCustomeSchemeCmdLine),
160 DECLARE_NAPI_FUNCTION("accessForward", NapiWebviewController::AccessForward),
161 DECLARE_NAPI_FUNCTION("accessBackward", NapiWebviewController::AccessBackward),
162 DECLARE_NAPI_FUNCTION("accessStep", NapiWebviewController::AccessStep),
163 DECLARE_NAPI_FUNCTION("clearHistory", NapiWebviewController::ClearHistory),
164 DECLARE_NAPI_FUNCTION("forward", NapiWebviewController::Forward),
165 DECLARE_NAPI_FUNCTION("backward", NapiWebviewController::Backward),
166 DECLARE_NAPI_FUNCTION("onActive", NapiWebviewController::OnActive),
167 DECLARE_NAPI_FUNCTION("onInactive", NapiWebviewController::OnInactive),
168 DECLARE_NAPI_FUNCTION("refresh", NapiWebviewController::Refresh),
169 DECLARE_NAPI_FUNCTION("zoomIn", NapiWebviewController::ZoomIn),
170 DECLARE_NAPI_FUNCTION("zoomOut", NapiWebviewController::ZoomOut),
171 DECLARE_NAPI_FUNCTION("getWebId", NapiWebviewController::GetWebId),
172 DECLARE_NAPI_FUNCTION("getUserAgent", NapiWebviewController::GetUserAgent),
173 DECLARE_NAPI_FUNCTION("getCustomUserAgent", NapiWebviewController::GetCustomUserAgent),
174 DECLARE_NAPI_FUNCTION("setCustomUserAgent", NapiWebviewController::SetCustomUserAgent),
175 DECLARE_NAPI_FUNCTION("getTitle", NapiWebviewController::GetTitle),
176 DECLARE_NAPI_FUNCTION("getPageHeight", NapiWebviewController::GetPageHeight),
177 DECLARE_NAPI_FUNCTION("backOrForward", NapiWebviewController::BackOrForward),
178 DECLARE_NAPI_FUNCTION("storeWebArchive", NapiWebviewController::StoreWebArchive),
179 DECLARE_NAPI_FUNCTION("createWebMessagePorts", NapiWebviewController::CreateWebMessagePorts),
180 DECLARE_NAPI_FUNCTION("postMessage", NapiWebviewController::PostMessage),
181 DECLARE_NAPI_FUNCTION("getHitTestValue", NapiWebviewController::GetHitTestValue),
182 DECLARE_NAPI_FUNCTION("requestFocus", NapiWebviewController::RequestFocus),
183 DECLARE_NAPI_FUNCTION("loadUrl", NapiWebviewController::LoadUrl),
184 DECLARE_NAPI_FUNCTION("loadData", NapiWebviewController::LoadData),
185 DECLARE_NAPI_FUNCTION("getHitTest", NapiWebviewController::GetHitTest),
186 DECLARE_NAPI_FUNCTION("clearMatches", NapiWebviewController::ClearMatches),
187 DECLARE_NAPI_FUNCTION("searchNext", NapiWebviewController::SearchNext),
188 DECLARE_NAPI_FUNCTION("searchAllAsync", NapiWebviewController::SearchAllAsync),
189 DECLARE_NAPI_FUNCTION("clearSslCache", NapiWebviewController::ClearSslCache),
190 DECLARE_NAPI_FUNCTION("clearClientAuthenticationCache", NapiWebviewController::ClearClientAuthenticationCache),
191 DECLARE_NAPI_FUNCTION("stop", NapiWebviewController::Stop),
192 DECLARE_NAPI_FUNCTION("zoom", NapiWebviewController::Zoom),
193 DECLARE_NAPI_FUNCTION("registerJavaScriptProxy", NapiWebviewController::RegisterJavaScriptProxy),
194 DECLARE_NAPI_FUNCTION("deleteJavaScriptRegister", NapiWebviewController::DeleteJavaScriptRegister),
195 DECLARE_NAPI_FUNCTION("runJavaScript", NapiWebviewController::RunJavaScript),
196 DECLARE_NAPI_FUNCTION("runJavaScriptExt", NapiWebviewController::RunJavaScriptExt),
197 DECLARE_NAPI_FUNCTION("getUrl", NapiWebviewController::GetUrl),
198 DECLARE_NAPI_FUNCTION("getOriginalUrl", NapiWebviewController::GetOriginalUrl),
199 DECLARE_NAPI_FUNCTION("setNetworkAvailable", NapiWebviewController::SetNetworkAvailable),
200 DECLARE_NAPI_FUNCTION("innerGetWebId", NapiWebviewController::InnerGetWebId),
201 DECLARE_NAPI_FUNCTION("hasImage", NapiWebviewController::HasImage),
202 DECLARE_NAPI_FUNCTION("removeCache", NapiWebviewController::RemoveCache),
203 DECLARE_NAPI_FUNCTION("getFavicon", NapiWebviewController::GetFavicon),
204 DECLARE_NAPI_FUNCTION("getBackForwardEntries", NapiWebviewController::getBackForwardEntries),
205 DECLARE_NAPI_FUNCTION("serializeWebState", NapiWebviewController::SerializeWebState),
206 DECLARE_NAPI_FUNCTION("restoreWebState", NapiWebviewController::RestoreWebState),
207 DECLARE_NAPI_FUNCTION("pageDown", NapiWebviewController::ScrollPageDown),
208 DECLARE_NAPI_FUNCTION("pageUp", NapiWebviewController::ScrollPageUp),
209 DECLARE_NAPI_FUNCTION("scrollTo", NapiWebviewController::ScrollTo),
210 DECLARE_NAPI_FUNCTION("scrollBy", NapiWebviewController::ScrollBy),
211 DECLARE_NAPI_FUNCTION("slideScroll", NapiWebviewController::SlideScroll),
212 DECLARE_NAPI_STATIC_FUNCTION("customizeSchemes", NapiWebviewController::CustomizeSchemes),
213 DECLARE_NAPI_FUNCTION("innerSetHapPath", NapiWebviewController::InnerSetHapPath),
214 DECLARE_NAPI_FUNCTION("innerGetCertificate", NapiWebviewController::InnerGetCertificate),
215 DECLARE_NAPI_FUNCTION("setAudioMuted", NapiWebviewController::SetAudioMuted),
216 DECLARE_NAPI_FUNCTION("innerGetThisVar", NapiWebviewController::InnerGetThisVar),
217 DECLARE_NAPI_FUNCTION("prefetchPage", NapiWebviewController::PrefetchPage),
218 DECLARE_NAPI_STATIC_FUNCTION("prepareForPageLoad", NapiWebviewController::PrepareForPageLoad),
219 };
220 napi_value constructor = nullptr;
221 napi_define_class(env, WEBVIEW_CONTROLLER_CLASS_NAME.c_str(), WEBVIEW_CONTROLLER_CLASS_NAME.length(),
222 NapiWebviewController::JsConstructor, nullptr, sizeof(properties) / sizeof(properties[0]),
223 properties, &constructor);
224 NAPI_ASSERT(env, constructor != nullptr, "define js class WebviewController failed");
225 napi_status status = napi_set_named_property(env, exports, "WebviewController", constructor);
226 NAPI_ASSERT(env, status == napi_ok, "set property WebviewController failed");
227
228 napi_value webMsgTypeEnum = nullptr;
229 napi_property_descriptor webMsgTypeProperties[] = {
230 DECLARE_NAPI_STATIC_PROPERTY("NOT_SUPPORT", NapiParseUtils::ToInt32Value(env,
231 static_cast<int32_t>(WebMessageType::NOTSUPPORT))),
232 DECLARE_NAPI_STATIC_PROPERTY("STRING", NapiParseUtils::ToInt32Value(env,
233 static_cast<int32_t>(WebMessageType::STRING))),
234 DECLARE_NAPI_STATIC_PROPERTY("NUMBER", NapiParseUtils::ToInt32Value(env,
235 static_cast<int32_t>(WebMessageType::NUMBER))),
236 DECLARE_NAPI_STATIC_PROPERTY("BOOLEAN", NapiParseUtils::ToInt32Value(env,
237 static_cast<int32_t>(WebMessageType::BOOLEAN))),
238 DECLARE_NAPI_STATIC_PROPERTY("ARRAY_BUFFER", NapiParseUtils::ToInt32Value(env,
239 static_cast<int32_t>(WebMessageType::ARRAYBUFFER))),
240 DECLARE_NAPI_STATIC_PROPERTY("ARRAY", NapiParseUtils::ToInt32Value(env,
241 static_cast<int32_t>(WebMessageType::ARRAY))),
242 DECLARE_NAPI_STATIC_PROPERTY("ERROR", NapiParseUtils::ToInt32Value(env,
243 static_cast<int32_t>(WebMessageType::ERROR)))
244 };
245 napi_define_class(env, WEB_PORT_MSG_ENUM_NAME.c_str(), WEB_PORT_MSG_ENUM_NAME.length(),
246 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(webMsgTypeProperties) /
247 sizeof(webMsgTypeProperties[0]), webMsgTypeProperties, &webMsgTypeEnum);
248 napi_set_named_property(env, exports, WEB_PORT_MSG_ENUM_NAME.c_str(), webMsgTypeEnum);
249
250 napi_value webMsgExtClass = nullptr;
251 napi_property_descriptor webMsgExtClsProperties[] = {
252 DECLARE_NAPI_FUNCTION("getType", NapiWebMessageExt::GetType),
253 DECLARE_NAPI_FUNCTION("getString", NapiWebMessageExt::GetString),
254 DECLARE_NAPI_FUNCTION("getNumber", NapiWebMessageExt::GetNumber),
255 DECLARE_NAPI_FUNCTION("getBoolean", NapiWebMessageExt::GetBoolean),
256 DECLARE_NAPI_FUNCTION("getArrayBuffer", NapiWebMessageExt::GetArrayBuffer),
257 DECLARE_NAPI_FUNCTION("getArray", NapiWebMessageExt::GetArray),
258 DECLARE_NAPI_FUNCTION("getError", NapiWebMessageExt::GetError),
259 DECLARE_NAPI_FUNCTION("setType", NapiWebMessageExt::SetType),
260 DECLARE_NAPI_FUNCTION("setString", NapiWebMessageExt::SetString),
261 DECLARE_NAPI_FUNCTION("setNumber", NapiWebMessageExt::SetNumber),
262 DECLARE_NAPI_FUNCTION("setBoolean", NapiWebMessageExt::SetBoolean),
263 DECLARE_NAPI_FUNCTION("setArrayBuffer", NapiWebMessageExt::SetArrayBuffer),
264 DECLARE_NAPI_FUNCTION("setArray", NapiWebMessageExt::SetArray),
265 DECLARE_NAPI_FUNCTION("setError", NapiWebMessageExt::SetError)
266 };
267 napi_define_class(env, WEB_EXT_MSG_CLASS_NAME.c_str(), WEB_EXT_MSG_CLASS_NAME.length(),
268 NapiWebMessageExt::JsConstructor, nullptr, sizeof(webMsgExtClsProperties) / sizeof(webMsgExtClsProperties[0]),
269 webMsgExtClsProperties, &webMsgExtClass);
270 napi_create_reference(env, webMsgExtClass, 1, &g_webMsgExtClassRef);
271 napi_set_named_property(env, exports, WEB_EXT_MSG_CLASS_NAME.c_str(), webMsgExtClass);
272
273 napi_value msgPortCons = nullptr;
274 napi_property_descriptor msgPortProperties[] = {
275 DECLARE_NAPI_FUNCTION("close", NapiWebMessagePort::Close),
276 DECLARE_NAPI_FUNCTION("postMessageEvent", NapiWebMessagePort::PostMessageEvent),
277 DECLARE_NAPI_FUNCTION("onMessageEvent", NapiWebMessagePort::OnMessageEvent),
278 DECLARE_NAPI_FUNCTION("postMessageEventExt", NapiWebMessagePort::PostMessageEventExt),
279 DECLARE_NAPI_FUNCTION("onMessageEventExt", NapiWebMessagePort::OnMessageEventExt)
280 };
281 NAPI_CALL(env, napi_define_class(env, WEB_MESSAGE_PORT_CLASS_NAME.c_str(), WEB_MESSAGE_PORT_CLASS_NAME.length(),
282 NapiWebMessagePort::JsConstructor, nullptr, sizeof(msgPortProperties) / sizeof(msgPortProperties[0]),
283 msgPortProperties, &msgPortCons));
284 NAPI_CALL(env, napi_create_reference(env, msgPortCons, 1, &g_classWebMsgPort));
285 NAPI_CALL(env, napi_set_named_property(env, exports, WEB_MESSAGE_PORT_CLASS_NAME.c_str(), msgPortCons));
286
287 napi_value hitTestTypeEnum = nullptr;
288 napi_property_descriptor hitTestTypeProperties[] = {
289 DECLARE_NAPI_STATIC_PROPERTY("EditText", NapiParseUtils::ToInt32Value(env,
290 static_cast<int32_t>(WebHitTestType::EDIT))),
291 DECLARE_NAPI_STATIC_PROPERTY("Email", NapiParseUtils::ToInt32Value(env,
292 static_cast<int32_t>(WebHitTestType::EMAIL))),
293 DECLARE_NAPI_STATIC_PROPERTY("HttpAnchor", NapiParseUtils::ToInt32Value(env,
294 static_cast<int32_t>(WebHitTestType::HTTP))),
295 DECLARE_NAPI_STATIC_PROPERTY("HttpAnchorImg", NapiParseUtils::ToInt32Value(env,
296 static_cast<int32_t>(WebHitTestType::HTTP_IMG))),
297 DECLARE_NAPI_STATIC_PROPERTY("Img", NapiParseUtils::ToInt32Value(env,
298 static_cast<int32_t>(WebHitTestType::IMG))),
299 DECLARE_NAPI_STATIC_PROPERTY("Map", NapiParseUtils::ToInt32Value(env,
300 static_cast<int32_t>(WebHitTestType::MAP))),
301 DECLARE_NAPI_STATIC_PROPERTY("Phone", NapiParseUtils::ToInt32Value(env,
302 static_cast<int32_t>(WebHitTestType::PHONE))),
303 DECLARE_NAPI_STATIC_PROPERTY("Unknown", NapiParseUtils::ToInt32Value(env,
304 static_cast<int32_t>(WebHitTestType::UNKNOWN))),
305 };
306 napi_define_class(env, WEB_HITTESTTYPE_V9_ENUM_NAME.c_str(), WEB_HITTESTTYPE_V9_ENUM_NAME.length(),
307 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(hitTestTypeProperties) /
308 sizeof(hitTestTypeProperties[0]), hitTestTypeProperties, &hitTestTypeEnum);
309 napi_set_named_property(env, exports, WEB_HITTESTTYPE_V9_ENUM_NAME.c_str(), hitTestTypeEnum);
310
311 napi_define_class(env, WEB_HITTESTTYPE_ENUM_NAME.c_str(), WEB_HITTESTTYPE_ENUM_NAME.length(),
312 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(hitTestTypeProperties) /
313 sizeof(hitTestTypeProperties[0]), hitTestTypeProperties, &hitTestTypeEnum);
314 napi_set_named_property(env, exports, WEB_HITTESTTYPE_ENUM_NAME.c_str(), hitTestTypeEnum);
315
316 napi_value secureDnsModeEnum = nullptr;
317 napi_property_descriptor secureDnsModeProperties[] = {
318 DECLARE_NAPI_STATIC_PROPERTY("Off", NapiParseUtils::ToInt32Value(env,
319 static_cast<int32_t>(SecureDnsModeType::OFF))),
320 DECLARE_NAPI_STATIC_PROPERTY("Auto", NapiParseUtils::ToInt32Value(env,
321 static_cast<int32_t>(SecureDnsModeType::AUTO))),
322 DECLARE_NAPI_STATIC_PROPERTY("SecureOnly", NapiParseUtils::ToInt32Value(env,
323 static_cast<int32_t>(SecureDnsModeType::SECURE_ONLY))),
324 DECLARE_NAPI_STATIC_PROPERTY("OFF", NapiParseUtils::ToInt32Value(env,
325 static_cast<int32_t>(SecureDnsModeType::OFF))),
326 DECLARE_NAPI_STATIC_PROPERTY("AUTO", NapiParseUtils::ToInt32Value(env,
327 static_cast<int32_t>(SecureDnsModeType::AUTO))),
328 DECLARE_NAPI_STATIC_PROPERTY("SECURE_ONLY", NapiParseUtils::ToInt32Value(env,
329 static_cast<int32_t>(SecureDnsModeType::SECURE_ONLY))),
330 };
331 napi_define_class(env, WEB_SECURE_DNS_MODE_ENUM_NAME.c_str(), WEB_SECURE_DNS_MODE_ENUM_NAME.length(),
332 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(secureDnsModeProperties) /
333 sizeof(secureDnsModeProperties[0]), secureDnsModeProperties, &secureDnsModeEnum);
334 napi_set_named_property(env, exports, WEB_SECURE_DNS_MODE_ENUM_NAME.c_str(), secureDnsModeEnum);
335
336 napi_value historyList = nullptr;
337 napi_property_descriptor historyListProperties[] = {
338 DECLARE_NAPI_FUNCTION("getItemAtIndex", NapiWebHistoryList::GetItem)
339 };
340 napi_define_class(env, WEB_HISTORY_LIST_CLASS_NAME.c_str(), WEB_HISTORY_LIST_CLASS_NAME.length(),
341 NapiWebHistoryList::JsConstructor, nullptr, sizeof(historyListProperties) / sizeof(historyListProperties[0]),
342 historyListProperties, &historyList);
343 napi_create_reference(env, historyList, 1, &g_historyListRef);
344 napi_set_named_property(env, exports, WEB_HISTORY_LIST_CLASS_NAME.c_str(), historyList);
345
346 WebviewJavaScriptExecuteCallback::InitJSExcute(env, exports);
347 return exports;
348 }
349
JsConstructor(napi_env env,napi_callback_info info)350 napi_value NapiWebviewController::JsConstructor(napi_env env, napi_callback_info info)
351 {
352 napi_value thisVar = nullptr;
353 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
354
355 WebviewController *webviewController = new (std::nothrow) WebviewController();
356 if (webviewController == nullptr) {
357 WVLOG_E("new webview controller failed");
358 return nullptr;
359 }
360 napi_status status = napi_wrap(
361 env, thisVar, webviewController,
362 [](napi_env env, void *data, void *hint) {
363 WebviewController *webviewController = static_cast<WebviewController *>(data);
364 delete webviewController;
365 },
366 nullptr, nullptr);
367 if (status != napi_ok) {
368 WVLOG_E("Wrap native webviewController failed.");
369 return nullptr;
370 }
371 return thisVar;
372 }
373
InitializeWebEngine(napi_env env,napi_callback_info info)374 napi_value NapiWebviewController::InitializeWebEngine(napi_env env, napi_callback_info info)
375 {
376 WVLOG_D("InitializeWebEngine invoked.");
377
378 // obtain bundle path
379 std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
380 AbilityRuntime::ApplicationContext::GetApplicationContext();
381 if (!ctx) {
382 WVLOG_E("Failed to init web engine due to nil application context.");
383 return nullptr;
384 }
385
386 // load so
387 const std::string& bundle_path = ctx->GetBundleCodeDir();
388 NWebHelper::Instance().SetBundlePath(bundle_path);
389 if (!NWebHelper::Instance().InitAndRun(true)) {
390 WVLOG_E("Failed to init web engine due to NWebHelper failure.");
391 return nullptr;
392 }
393
394 napi_value result = nullptr;
395 NAPI_CALL(env, napi_get_undefined(env, &result));
396 WVLOG_I("NWebHelper initialized, init web engine done, bundle_path: %{public}s", bundle_path.c_str());
397 return result;
398 }
399
SetHttpDns(napi_env env,napi_callback_info info)400 napi_value NapiWebviewController::SetHttpDns(napi_env env, napi_callback_info info)
401 {
402 napi_value thisVar = nullptr;
403 napi_value result = nullptr;
404 size_t argc = INTEGER_TWO;
405 napi_value argv[INTEGER_TWO] = { 0 };
406 int dohMode;
407 std::string dohConfig;
408
409 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
410 if (argc != INTEGER_TWO) {
411 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
412 return result;
413 }
414
415 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], dohMode)) {
416 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
417 return result;
418 }
419
420 if (dohMode < static_cast<int>(SecureDnsModeType::OFF) ||
421 dohMode > static_cast<int>(SecureDnsModeType::SECURE_ONLY)) {
422 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
423 return result;
424 }
425
426 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], dohConfig)) {
427 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
428 return result;
429 }
430
431 if (dohConfig.rfind("https", 0) != 0 && dohConfig.rfind("HTTPS", 0) != 0) {
432 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
433 return result;
434 }
435
436 NWebDOHConfig config;
437 config.dohMode = dohMode;
438 config.dohConfig = dohConfig;
439 WVLOG_I("set http dns mode:%{public}d doh_config:%{public}s", dohMode, dohConfig.c_str());
440
441 NWebHelper::Instance().SetHttpDns(config);
442
443 NAPI_CALL(env, napi_get_undefined(env, &result));
444 return result;
445 }
446
SetWebDebuggingAccess(napi_env env,napi_callback_info info)447 napi_value NapiWebviewController::SetWebDebuggingAccess(napi_env env, napi_callback_info info)
448 {
449 WVLOG_D("SetWebDebuggingAccess start");
450
451 napi_value thisVar = nullptr;
452 napi_value result = nullptr;
453 size_t argc = INTEGER_ONE;
454 napi_value argv[INTEGER_ONE] = {0};
455
456 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
457 if (argc != INTEGER_ONE) {
458 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
459 return result;
460 }
461
462 bool webDebuggingAccess = false;
463 if (!NapiParseUtils::ParseBoolean(env, argv[0], webDebuggingAccess)) {
464 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
465 return result;
466 }
467 WebviewController::webDebuggingAccess_ = webDebuggingAccess;
468
469 NAPI_CALL(env, napi_get_undefined(env, &result));
470 return result;
471 }
472
InnerGetWebDebuggingAccess(napi_env env,napi_callback_info info)473 napi_value NapiWebviewController::InnerGetWebDebuggingAccess(napi_env env, napi_callback_info info)
474 {
475 WVLOG_D("InnerGetWebDebuggingAccess start");
476 bool webDebuggingAccess = WebviewController::webDebuggingAccess_;
477 napi_value result = nullptr;
478 napi_get_boolean(env, webDebuggingAccess, &result);
479 return result;
480 }
481
InnerGetThisVar(napi_env env,napi_callback_info info)482 napi_value NapiWebviewController::InnerGetThisVar(napi_env env, napi_callback_info info)
483 {
484 WVLOG_D("InnerGetThisVar start");
485 napi_value thisVar = nullptr;
486 napi_value result = nullptr;
487 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
488 WebviewController *webviewController = nullptr;
489 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
490 if ((!webviewController) || (status != napi_ok)) {
491 WVLOG_E("webviewController is nullptr.");
492 napi_create_int64(env, 0, &result);
493 } else {
494 napi_create_int64(env, reinterpret_cast<int64_t>(webviewController), &result);
495 }
496 return result;
497 }
498
SetWebId(napi_env env,napi_callback_info info)499 napi_value NapiWebviewController::SetWebId(napi_env env, napi_callback_info info)
500 {
501 napi_value thisVar = nullptr;
502 size_t argc = INTEGER_ONE;
503 napi_value argv[INTEGER_ONE];
504 void* data = nullptr;
505 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
506
507 int32_t webId = -1;
508 if (!NapiParseUtils::ParseInt32(env, argv[0], webId)) {
509 WVLOG_E("Parse web id failed.");
510 return nullptr;
511 }
512 WebviewController *webviewController = nullptr;
513 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
514 if ((!webviewController) || (status != napi_ok)) {
515 WVLOG_E("webviewController is nullptr.");
516 return nullptr;
517 }
518 webviewController->SetWebId(webId);
519 return thisVar;
520 }
521
InnerSetHapPath(napi_env env,napi_callback_info info)522 napi_value NapiWebviewController::InnerSetHapPath(napi_env env, napi_callback_info info)
523 {
524 napi_value result = nullptr;
525 NAPI_CALL(env, napi_get_undefined(env, &result));
526 napi_value thisVar = nullptr;
527 size_t argc = INTEGER_ONE;
528 napi_value argv[INTEGER_ONE];
529 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
530 if (argc != INTEGER_ONE) {
531 WVLOG_E("Failed to run InnerSetHapPath beacuse of wrong Param number.");
532 return result;
533 }
534 std::string hapPath;
535 if (!NapiParseUtils::ParseString(env, argv[0], hapPath)) {
536 WVLOG_E("Parse hap path failed.");
537 return result;
538 }
539 WebviewController *webviewController = nullptr;
540 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
541 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
542 WVLOG_E("Wrap webviewController failed. WebviewController must be associated with a Web component.");
543 return result;
544 }
545 webviewController->InnerSetHapPath(hapPath);
546 return result;
547 }
548
InnerJsProxy(napi_env env,napi_callback_info info)549 napi_value NapiWebviewController::InnerJsProxy(napi_env env, napi_callback_info info)
550 {
551 WVLOG_D("NapiWebviewController::InnerJsProxy");
552 napi_value thisVar = nullptr;
553 napi_value result = nullptr;
554 size_t argc = INTEGER_THREE;
555 napi_value argv[INTEGER_THREE] = { 0 };
556
557 napi_get_undefined(env, &result);
558 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
559 if (argc != INTEGER_THREE) {
560 WVLOG_E("Failed to run InnerJsProxy beacuse of wrong Param number.");
561 return result;
562 }
563
564 napi_valuetype valueType = napi_undefined;
565 napi_typeof(env, argv[INTEGER_ZERO], &valueType);
566 if (valueType != napi_object) {
567 WVLOG_E("Failed to run InnerJsProxy beacuse of wrong Param type.");
568 return result;
569 }
570
571 std::string objName;
572 std::vector<std::string> methodList;
573 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], objName) ||
574 !NapiParseUtils::ParseStringArray(env, argv[INTEGER_TWO], methodList)) {
575 WVLOG_E("Failed to run InnerJsProxy beacuse of wrong Param type.");
576 return result;
577 }
578
579 WebviewController *controller = nullptr;
580 napi_unwrap(env, thisVar, (void **)&controller);
581 if (!controller || !controller->IsInit()) {
582 WVLOG_E("Failed to run InnerJsProxy. The WebviewController must be associted with a Web component.");
583 return result;
584 }
585 controller->SetNWebJavaScriptResultCallBack();
586 controller->RegisterJavaScriptProxy(env, argv[INTEGER_ZERO], objName, methodList);
587 return result;
588 }
589
InnerGetCustomeSchemeCmdLine(napi_env env,napi_callback_info info)590 napi_value NapiWebviewController::InnerGetCustomeSchemeCmdLine(napi_env env, napi_callback_info info)
591 {
592 WebviewController::existNweb_ = true;
593 napi_value result = nullptr;
594 std::string cmdLine = WebviewController::customeSchemeCmdLine_;
595 napi_create_string_utf8(env, cmdLine.c_str(), cmdLine.length(), &result);
596 return result;
597 }
598
AccessForward(napi_env env,napi_callback_info info)599 napi_value NapiWebviewController::AccessForward(napi_env env, napi_callback_info info)
600 {
601 napi_value thisVar = nullptr;
602 napi_value result = nullptr;
603 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
604
605 WebviewController *webviewController = nullptr;
606 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
607 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
608 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
609 return nullptr;
610 }
611
612 bool access = webviewController->AccessForward();
613 NAPI_CALL(env, napi_get_boolean(env, access, &result));
614 return result;
615 }
616
AccessBackward(napi_env env,napi_callback_info info)617 napi_value NapiWebviewController::AccessBackward(napi_env env, napi_callback_info info)
618 {
619 napi_value thisVar = nullptr;
620 napi_value result = nullptr;
621 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
622
623 WebviewController *webviewController = nullptr;
624 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
625 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
626 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
627 return nullptr;
628 }
629
630 bool access = webviewController->AccessBackward();
631 NAPI_CALL(env, napi_get_boolean(env, access, &result));
632 return result;
633 }
634
Forward(napi_env env,napi_callback_info info)635 napi_value NapiWebviewController::Forward(napi_env env, napi_callback_info info)
636 {
637 napi_value thisVar = nullptr;
638 napi_value result = nullptr;
639 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
640
641 WebviewController *webviewController = nullptr;
642 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
643 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
644 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
645 return nullptr;
646 }
647
648 webviewController->Forward();
649 NAPI_CALL(env, napi_get_undefined(env, &result));
650 return result;
651 }
652
Backward(napi_env env,napi_callback_info info)653 napi_value NapiWebviewController::Backward(napi_env env, napi_callback_info info)
654 {
655 napi_value thisVar = nullptr;
656 napi_value result = nullptr;
657 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
658
659 WebviewController *webviewController = nullptr;
660 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
661 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
662 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
663 return nullptr;
664 }
665
666 webviewController->Backward();
667 NAPI_CALL(env, napi_get_undefined(env, &result));
668 return result;
669 }
670
AccessStep(napi_env env,napi_callback_info info)671 napi_value NapiWebviewController::AccessStep(napi_env env, napi_callback_info info)
672 {
673 napi_value thisVar = nullptr;
674 napi_value result = nullptr;
675 size_t argc = INTEGER_ONE;
676 napi_value argv[INTEGER_ONE];
677 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
678 if (argc != INTEGER_ONE) {
679 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
680 return nullptr;
681 }
682
683 int32_t step = INTEGER_ZERO;
684 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], step)) {
685 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
686 return nullptr;
687 }
688
689 WebviewController *webviewController = nullptr;
690 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
691 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
692 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
693 return nullptr;
694 }
695
696 bool access = webviewController->AccessStep(step);
697 NAPI_CALL(env, napi_get_boolean(env, access, &result));
698 return result;
699 }
700
ClearHistory(napi_env env,napi_callback_info info)701 napi_value NapiWebviewController::ClearHistory(napi_env env, napi_callback_info info)
702 {
703 napi_value thisVar = nullptr;
704 napi_value result = nullptr;
705 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
706
707 WebviewController *webviewController = nullptr;
708 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
709 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
710 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
711 return nullptr;
712 }
713
714 webviewController->ClearHistory();
715 NAPI_CALL(env, napi_get_undefined(env, &result));
716 return result;
717 }
718
OnActive(napi_env env,napi_callback_info info)719 napi_value NapiWebviewController::OnActive(napi_env env, napi_callback_info info)
720 {
721 napi_value thisVar = nullptr;
722 napi_value result = nullptr;
723 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
724
725 WebviewController *webviewController = nullptr;
726 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
727 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
728 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
729 return nullptr;
730 }
731
732 webviewController->OnActive();
733 WVLOG_D("The web component has been successfully activated");
734 NAPI_CALL(env, napi_get_undefined(env, &result));
735 return result;
736 }
737
OnInactive(napi_env env,napi_callback_info info)738 napi_value NapiWebviewController::OnInactive(napi_env env, napi_callback_info info)
739 {
740 napi_value thisVar = nullptr;
741 napi_value result = nullptr;
742 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
743
744 WebviewController *webviewController = nullptr;
745 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
746 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
747 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
748 return nullptr;
749 }
750
751 webviewController->OnInactive();
752 WVLOG_D("The web component has been successfully inactivated");
753 NAPI_CALL(env, napi_get_undefined(env, &result));
754 return result;
755 }
756
Refresh(napi_env env,napi_callback_info info)757 napi_value NapiWebviewController::Refresh(napi_env env, napi_callback_info info)
758 {
759 napi_value thisVar = nullptr;
760 napi_value result = nullptr;
761 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
762
763 WebviewController *webviewController = nullptr;
764 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
765 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
766 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
767 return nullptr;
768 }
769
770 webviewController->Refresh();
771 NAPI_CALL(env, napi_get_undefined(env, &result));
772 return result;
773 }
774
775
JsConstructor(napi_env env,napi_callback_info info)776 napi_value NapiWebMessageExt::JsConstructor(napi_env env, napi_callback_info info)
777 {
778 WVLOG_D("NapiWebMessageExt::JsConstructor");
779 napi_value thisVar = nullptr;
780 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
781
782 auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE);
783 WebMessageExt *webMessageExt = new (std::nothrow) WebMessageExt(webMsg);
784 if (webMessageExt == nullptr) {
785 WVLOG_E("new msg port failed");
786 return nullptr;
787 }
788 NAPI_CALL(env, napi_wrap(env, thisVar, webMessageExt,
789 [](napi_env env, void *data, void *hint) {
790 WebMessageExt *webMessageExt = static_cast<WebMessageExt *>(data);
791 delete webMessageExt;
792 },
793 nullptr, nullptr));
794 return thisVar;
795 }
796
GetType(napi_env env,napi_callback_info info)797 napi_value NapiWebMessageExt::GetType(napi_env env, napi_callback_info info)
798 {
799 WVLOG_D("NapiWebMessageExt::GetType start");
800 napi_value thisVar = nullptr;
801 napi_value result = nullptr;
802 napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
803 if (status != napi_status::napi_ok) {
804 WVLOG_E("napi_get_cb_info status not ok");
805 return result;
806 }
807
808 if (thisVar == nullptr) {
809 WVLOG_E("napi_get_cb_info thisVar is nullptr");
810 return result;
811 }
812
813 WebMessageExt *webMessageExt = nullptr;
814 status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
815 if ((!webMessageExt) || (status != napi_ok)) {
816 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
817 return nullptr;
818 }
819
820 int32_t type = webMessageExt->GetType();
821 status = napi_create_int32(env, type, &result);
822 if (status != napi_status::napi_ok) {
823 WVLOG_E("napi_create_int32 failed.");
824 return result;
825 }
826 return result;
827 }
828
GetString(napi_env env,napi_callback_info info)829 napi_value NapiWebMessageExt::GetString(napi_env env, napi_callback_info info)
830 {
831 WVLOG_D(" GetString webJsMessageExt start");
832 napi_value thisVar = nullptr;
833 napi_value result = nullptr;
834 size_t argc = INTEGER_ONE;
835 napi_value argv[INTEGER_ONE] = { 0 };
836
837 WebMessageExt *webJsMessageExt = nullptr;
838 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
839 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt));
840 if (webJsMessageExt == nullptr) {
841 WVLOG_E("unwrap webJsMessageExt failed.");
842 return result;
843 }
844
845 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::STRING)) {
846 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE);
847 return nullptr;
848 }
849
850 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result);
851 return result;
852 }
853
GetNumber(napi_env env,napi_callback_info info)854 napi_value NapiWebMessageExt::GetNumber(napi_env env, napi_callback_info info)
855 {
856 WVLOG_D("GetNumber webJsMessageExt start");
857 napi_value thisVar = nullptr;
858 napi_value result = nullptr;
859 size_t argc = INTEGER_ONE;
860 napi_value argv[INTEGER_ONE] = { 0 };
861
862 WebMessageExt *webJsMessageExt = nullptr;
863 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
864 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt));
865 if (webJsMessageExt == nullptr) {
866 WVLOG_E("unwrap webJsMessageExt failed.");
867 return result;
868 }
869
870 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::NUMBER)) {
871 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE);
872 WVLOG_E("GetNumber webJsMessageExt failed,not match");
873 return nullptr;
874 }
875
876 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result);
877 return result;
878 }
879
GetBoolean(napi_env env,napi_callback_info info)880 napi_value NapiWebMessageExt::GetBoolean(napi_env env, napi_callback_info info)
881 {
882 napi_value thisVar = nullptr;
883 napi_value result = nullptr;
884 size_t argc = INTEGER_ONE;
885 napi_value argv[INTEGER_ONE] = { 0 };
886
887 WebMessageExt *webJsMessageExt = nullptr;
888 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
889 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt));
890 if (webJsMessageExt == nullptr) {
891 WVLOG_E("unwrap webJsMessageExt failed.");
892 return result;
893 }
894
895 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::BOOLEAN)) {
896 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE);
897 return nullptr;
898 }
899
900 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result);
901 return result;
902 }
903
GetArrayBuffer(napi_env env,napi_callback_info info)904 napi_value NapiWebMessageExt::GetArrayBuffer(napi_env env, napi_callback_info info)
905 {
906 napi_value thisVar = nullptr;
907 napi_value result = nullptr;
908 size_t argc = INTEGER_ONE;
909 napi_value argv[INTEGER_ONE] = { 0 };
910
911 WebMessageExt *webJsMessageExt = nullptr;
912 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
913 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt));
914 if (webJsMessageExt == nullptr) {
915 WVLOG_E("unwrap webJsMessageExt failed.");
916 return result;
917 }
918
919 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAYBUFFER)) {
920 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE);
921 return nullptr;
922 }
923 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result);
924 return result;
925 }
926
GetArray(napi_env env,napi_callback_info info)927 napi_value NapiWebMessageExt::GetArray(napi_env env, napi_callback_info info)
928 {
929 napi_value thisVar = nullptr;
930 napi_value result = nullptr;
931 size_t argc = INTEGER_ONE;
932 napi_value argv[INTEGER_ONE] = { 0 };
933
934 WebMessageExt *webJsMessageExt = nullptr;
935 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
936 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt));
937 if (webJsMessageExt == nullptr) {
938 WVLOG_E("unwrap webJsMessageExt failed.");
939 return result;
940 }
941
942 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) {
943 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE);
944 return nullptr;
945 }
946
947 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result);
948 return result;
949 }
950
GetError(napi_env env,napi_callback_info info)951 napi_value NapiWebMessageExt::GetError(napi_env env, napi_callback_info info)
952 {
953 napi_value thisVar = nullptr;
954 napi_value result = nullptr;
955 size_t argc = INTEGER_ONE;
956 napi_value argv[INTEGER_ONE] = { 0 };
957
958 WebMessageExt *webJsMessageExt = nullptr;
959 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
960 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webJsMessageExt));
961 if (webJsMessageExt == nullptr) {
962 WVLOG_E("unwrap webJsMessageExt failed.");
963 return result;
964 }
965
966 if (webJsMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ERROR)) {
967 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE);
968 return nullptr;
969 }
970
971 NapiParseUtils::ConvertNWebToNapiValue(env, webJsMessageExt->GetData(), result);
972 return result;
973 }
974
SetType(napi_env env,napi_callback_info info)975 napi_value NapiWebMessageExt::SetType(napi_env env, napi_callback_info info)
976 {
977 WVLOG_D("NapiWebMessageExt::SetType");
978 napi_value thisVar = nullptr;
979 napi_value result = nullptr;
980 size_t argc = INTEGER_ONE;
981 napi_value argv[INTEGER_ONE] = { 0 };
982 int type = -1;
983
984 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
985 if (status != napi_ok) {
986 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
987 WVLOG_E("NapiWebMessageExt::SetType napi_get_cb_info failed");
988 return result;
989 }
990
991 if (thisVar == nullptr) {
992 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
993 WVLOG_E("NapiWebMessageExt::SetType thisVar is null");
994 return result;
995 }
996
997 if (argc != INTEGER_ONE) {
998 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
999 return result;
1000 }
1001
1002 if (!NapiParseUtils::ParseInt32(env, argv[0], type)) {
1003 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1004 return result;
1005 }
1006 WebMessageExt *webMessageExt = nullptr;
1007 status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1008 if (status != napi_ok) {
1009 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1010 WVLOG_E("NapiWebMessageExt::SetType napi_unwrap failed");
1011 return result;
1012 }
1013 if (!webMessageExt) {
1014 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1015 WVLOG_E("NapiWebMessageExt::SetType webMessageExt is null");
1016 return result;
1017 }
1018
1019 webMessageExt->SetType(type);
1020 return result;
1021 }
1022
SetString(napi_env env,napi_callback_info info)1023 napi_value NapiWebMessageExt::SetString(napi_env env, napi_callback_info info)
1024 {
1025 WVLOG_D("NapiWebMessageExt::SetString start");
1026 napi_value thisVar = nullptr;
1027 napi_value result = nullptr;
1028 size_t argc = INTEGER_ONE;
1029 napi_value argv[INTEGER_ONE] = { 0 };
1030 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1031 if (argc != INTEGER_ONE) {
1032 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1033 return result;
1034 }
1035
1036 std::string value;
1037 if (!NapiParseUtils::ParseString(env, argv[0], value)) {
1038 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1039 return result;
1040 }
1041 WebMessageExt *webMessageExt = nullptr;
1042 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1043 if ((!webMessageExt) || (status != napi_ok)) {
1044 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1045 return result;
1046 }
1047 webMessageExt->SetString(value);
1048 return result;
1049 }
1050
SetNumber(napi_env env,napi_callback_info info)1051 napi_value NapiWebMessageExt::SetNumber(napi_env env, napi_callback_info info)
1052 {
1053 WVLOG_D("NapiWebMessageExt::SetNumber start");
1054 napi_value thisVar = nullptr;
1055 napi_value result = nullptr;
1056 size_t argc = INTEGER_ONE;
1057 napi_value argv[INTEGER_ONE] = { 0 };
1058 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1059 if (argc != INTEGER_ONE) {
1060 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1061 return result;
1062 }
1063
1064 double value = 0;
1065 if (!NapiParseUtils::ParseDouble(env, argv[0], value)) {
1066 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1067 return result;
1068 }
1069
1070 WebMessageExt *webMessageExt = nullptr;
1071 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1072 if ((!webMessageExt) || (status != napi_ok)) {
1073 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1074 return result;
1075 }
1076 webMessageExt->SetNumber(value);
1077 return result;
1078 }
1079
SetBoolean(napi_env env,napi_callback_info info)1080 napi_value NapiWebMessageExt::SetBoolean(napi_env env, napi_callback_info info)
1081 {
1082 WVLOG_D("NapiWebMessageExt::SetBoolean start");
1083 napi_value thisVar = nullptr;
1084 napi_value result = nullptr;
1085 size_t argc = INTEGER_ONE;
1086 napi_value argv[INTEGER_ONE] = { 0 };
1087 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1088 if (argc != INTEGER_ONE) {
1089 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1090 return result;
1091 }
1092
1093 bool value = 0;
1094 if (!NapiParseUtils::ParseBoolean(env, argv[0], value)) {
1095 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1096 return result;
1097 }
1098
1099 WebMessageExt *webMessageExt = nullptr;
1100 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1101 if ((!webMessageExt) || (status != napi_ok)) {
1102 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1103 return result;
1104 }
1105 webMessageExt->SetBoolean(value);
1106 return result;
1107 }
1108
SetArrayBuffer(napi_env env,napi_callback_info info)1109 napi_value NapiWebMessageExt::SetArrayBuffer(napi_env env, napi_callback_info info)
1110 {
1111 WVLOG_D("NapiWebMessageExt::SetArrayBuffer start");
1112 napi_value thisVar = nullptr;
1113 napi_value result = nullptr;
1114 size_t argc = INTEGER_ONE;
1115 napi_value argv[INTEGER_ONE] = { 0 };
1116 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1117 if (argc != INTEGER_ONE) {
1118 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1119 return result;
1120 }
1121
1122 bool isArrayBuffer = false;
1123 NAPI_CALL(env, napi_is_arraybuffer(env, argv[INTEGER_ZERO], &isArrayBuffer));
1124 if (!isArrayBuffer) {
1125 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1126 return result;
1127 }
1128
1129 uint8_t *arrBuf = nullptr;
1130 size_t byteLength = 0;
1131 napi_get_arraybuffer_info(env, argv[INTEGER_ZERO], (void**)&arrBuf, &byteLength);
1132 std::vector<uint8_t> vecData(arrBuf, arrBuf + byteLength);
1133 WebMessageExt *webMessageExt = nullptr;
1134 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1135 if ((!webMessageExt) || (status != napi_ok)) {
1136 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1137 return result;
1138 }
1139 webMessageExt->SetArrayBuffer(vecData);
1140 return result;
1141 }
1142
SetArray(napi_env env,napi_callback_info info)1143 napi_value NapiWebMessageExt::SetArray(napi_env env, napi_callback_info info)
1144 {
1145 WVLOG_D("NapiWebMessageExt::SetArray start");
1146 napi_value thisVar = nullptr;
1147 napi_value result = nullptr;
1148 size_t argc = INTEGER_ONE;
1149 napi_value argv[INTEGER_ONE] = { 0 };
1150 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1151 if (argc != INTEGER_ONE) {
1152 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1153 return result;
1154 }
1155
1156 bool isArray = false;
1157 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ZERO], &isArray));
1158 if (!isArray) {
1159 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1160 return result;
1161 }
1162
1163 WebMessageExt *webMessageExt = nullptr;
1164 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1165 if ((!webMessageExt) || (status != napi_ok)) {
1166 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1167 return result;
1168 }
1169 bool isDouble = false;
1170 napi_valuetype valueType = GetArrayValueType(env, argv, isDouble);
1171 if (valueType == napi_undefined) {
1172 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1173 return result;
1174 }
1175
1176 using SetArrayHandler = std::function<void(napi_env, napi_value*, WebMessageExt*)>;
1177 static const std::unordered_map<napi_valuetype, SetArrayHandler> functionMap = {
1178 { napi_boolean, SetArrayHandlerBoolean },
1179 { napi_string, SetArrayHandlerString },
1180 { napi_number, [isDouble](napi_env env, napi_value* argv, WebMessageExt* msgExt) {
1181 isDouble ? SetArrayHandlerDouble(env, argv, msgExt)
1182 : SetArrayHandlerInteger(env, argv, msgExt);
1183 } }
1184 };
1185 auto it = functionMap.find(valueType);
1186 if (it == functionMap.end()) {
1187 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1188 return result;
1189 }
1190 it->second(env, argv, webMessageExt);
1191 return result;
1192 }
1193
SetError(napi_env env,napi_callback_info info)1194 napi_value NapiWebMessageExt::SetError(napi_env env, napi_callback_info info)
1195 {
1196 WVLOG_D("NapiWebMessageExt::SetError start");
1197 napi_value thisVar = nullptr;
1198 napi_value result = nullptr;
1199 size_t argc = INTEGER_ONE;
1200 napi_value argv[INTEGER_ONE] = { 0 };
1201 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1202 if (argc != INTEGER_ONE) {
1203 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1204 return result;
1205 }
1206
1207 bool isError = false;
1208 NAPI_CALL(env, napi_is_error(env, argv[INTEGER_ZERO], &isError));
1209 if (!isError) {
1210 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1211 return result;
1212 }
1213
1214 napi_value nameObj = 0;
1215 napi_get_named_property(env, argv[INTEGER_ZERO], "name", &nameObj);
1216 std::string nameVal;
1217 if (!NapiParseUtils::ParseString(env, nameObj, nameVal)) {
1218 return result;
1219 }
1220
1221 napi_value msgObj = 0;
1222 napi_get_named_property(env, argv[INTEGER_ZERO], "message", &msgObj);
1223 std::string msgVal;
1224 if (!NapiParseUtils::ParseString(env, msgObj, msgVal)) {
1225 return result;
1226 }
1227
1228 WebMessageExt *webMessageExt = nullptr;
1229 napi_status status = napi_unwrap(env, thisVar, (void **)&webMessageExt);
1230 if ((!webMessageExt) || (status != napi_ok)) {
1231 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1232 return result;
1233 }
1234 webMessageExt->SetError(nameVal, msgVal);
1235 return result;
1236 }
1237
CreateWebMessagePorts(napi_env env,napi_callback_info info)1238 napi_value NapiWebviewController::CreateWebMessagePorts(napi_env env, napi_callback_info info)
1239 {
1240 WVLOG_D("create web message port");
1241 napi_value thisVar = nullptr;
1242 napi_value result = nullptr;
1243 size_t argc = INTEGER_ONE;
1244 napi_value argv[INTEGER_ONE] = { 0 };
1245 bool isExtentionType = false;
1246 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1247 if (argc != INTEGER_ZERO && argc != INTEGER_ONE) {
1248 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1249 return result;
1250 }
1251
1252 if (argc == INTEGER_ONE) {
1253 if (!NapiParseUtils::ParseBoolean(env, argv[0], isExtentionType)) {
1254 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1255 return result;
1256 }
1257 }
1258
1259 WebviewController *webviewController = nullptr;
1260 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController));
1261 if (webviewController == nullptr || !webviewController->IsInit()) {
1262 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1263 WVLOG_E("create message port failed, napi unwrap webviewController failed");
1264 return nullptr;
1265 }
1266 int32_t nwebId = webviewController->GetWebId();
1267 std::vector<std::string> ports;
1268 webviewController->CreateWebMessagePorts(ports);
1269 if (ports.size() != INTEGER_TWO) {
1270 WVLOG_E("create web message port failed");
1271 return result;
1272 }
1273 napi_value msgPortcons = nullptr;
1274 NAPI_CALL(env, napi_get_reference_value(env, g_classWebMsgPort, &msgPortcons));
1275 napi_create_array(env, &result);
1276 napi_value consParam[INTEGER_TWO][INTEGER_THREE] = {{0}};
1277 for (uint32_t i = 0; i < INTEGER_TWO; i++) {
1278 napi_value msgPortObj = nullptr;
1279 NAPI_CALL(env, napi_create_int32(env, nwebId, &consParam[i][INTEGER_ZERO]));
1280 NAPI_CALL(env, napi_create_string_utf8(env, ports[i].c_str(), ports[i].length(), &consParam[i][INTEGER_ONE]));
1281 NAPI_CALL(env, napi_get_boolean(env, isExtentionType, &consParam[i][INTEGER_TWO]));
1282 NAPI_CALL(env, napi_new_instance(env, msgPortcons, INTEGER_THREE, consParam[i], &msgPortObj));
1283 napi_value jsExtention;
1284 napi_get_boolean(env, isExtentionType, &jsExtention);
1285 napi_set_named_property(env, msgPortObj, "isExtentionType", jsExtention);
1286
1287 napi_set_element(env, result, i, msgPortObj);
1288 }
1289
1290 return result;
1291 }
1292
PostMessage(napi_env env,napi_callback_info info)1293 napi_value NapiWebviewController::PostMessage(napi_env env, napi_callback_info info)
1294 {
1295 WVLOG_D("post message port");
1296 napi_value thisVar = nullptr;
1297 napi_value result = nullptr;
1298 size_t argc = INTEGER_THREE;
1299 napi_value argv[INTEGER_THREE];
1300 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1301 if (argc != INTEGER_THREE) {
1302 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1303 return result;
1304 }
1305
1306 std::string portName;
1307 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], portName)) {
1308 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1309 return result;
1310 }
1311
1312 bool isArray = false;
1313 NAPI_CALL(env, napi_is_array(env, argv[INTEGER_ONE], &isArray));
1314 if (!isArray) {
1315 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1316 return result;
1317 }
1318 uint32_t arrayLen = 0;
1319 NAPI_CALL(env, napi_get_array_length(env, argv[INTEGER_ONE], &arrayLen));
1320 if (arrayLen == 0) {
1321 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1322 return result;
1323 }
1324 std::vector<std::string> sendPorts;
1325 napi_valuetype valueType = napi_undefined;
1326 for (uint32_t i = 0; i < arrayLen; i++) {
1327 napi_value portItem = nullptr;
1328 napi_get_element(env, argv[INTEGER_ONE], i, &portItem);
1329 NAPI_CALL(env, napi_typeof(env, portItem, &valueType));
1330 if (valueType != napi_object) {
1331 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1332 return result;
1333 }
1334 WebMessagePort *msgPort = nullptr;
1335 NAPI_CALL(env, napi_unwrap(env, portItem, (void **)&msgPort));
1336 if (!msgPort) {
1337 WVLOG_E("post port to html failed, napi unwrap msg port fail");
1338 return nullptr;
1339 }
1340 std::string portHandle = msgPort->GetPortHandle();
1341 sendPorts.emplace_back(portHandle);
1342 }
1343
1344 std::string urlStr;
1345 if (!NapiParseUtils::ParseString(env, argv[INTEGER_TWO], urlStr)) {
1346 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1347 return result;
1348 }
1349
1350 WebviewController *webviewController = nullptr;
1351 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController));
1352 if (webviewController == nullptr || !webviewController->IsInit()) {
1353 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1354 WVLOG_E("post port to html failed, napi unwrap webviewController failed");
1355 return nullptr;
1356 }
1357
1358 webviewController->PostWebMessage(portName, sendPorts, urlStr);
1359 NAPI_CALL(env, napi_get_undefined(env, &result));
1360
1361 return result;
1362 }
1363
JsConstructor(napi_env env,napi_callback_info info)1364 napi_value NapiWebMessagePort::JsConstructor(napi_env env, napi_callback_info info)
1365 {
1366 WVLOG_D("web message port construct");
1367 napi_value thisVar = nullptr;
1368 size_t argc = INTEGER_THREE;
1369 napi_value argv[INTEGER_THREE] = {0};
1370 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1371
1372 int32_t webId = -1;
1373 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], webId)) {
1374 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1375 return nullptr;
1376 }
1377
1378 std::string portHandle;
1379 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], portHandle)) {
1380 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1381 return nullptr;
1382 }
1383
1384 bool isExtentionType = false;
1385 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_TWO], isExtentionType)) {
1386 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1387 return nullptr;
1388 }
1389
1390 WebMessagePort *msgPort = new (std::nothrow) WebMessagePort(webId, portHandle, isExtentionType);
1391 if (msgPort == nullptr) {
1392 WVLOG_E("new msg port failed");
1393 return nullptr;
1394 }
1395 NAPI_CALL(env, napi_wrap(env, thisVar, msgPort,
1396 [](napi_env env, void *data, void *hint) {
1397 WebMessagePort *msgPort = static_cast<WebMessagePort *>(data);
1398 delete msgPort;
1399 },
1400 nullptr, nullptr));
1401 return thisVar;
1402 }
1403
Close(napi_env env,napi_callback_info info)1404 napi_value NapiWebMessagePort::Close(napi_env env, napi_callback_info info)
1405 {
1406 WVLOG_D("close message port");
1407 napi_value thisVar = nullptr;
1408 napi_value result = nullptr;
1409 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
1410
1411 WebMessagePort *msgPort = nullptr;
1412 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort));
1413 if (msgPort == nullptr) {
1414 WVLOG_E("close message port failed, napi unwrap msg port failed");
1415 return nullptr;
1416 }
1417 ErrCode ret = msgPort->ClosePort();
1418 if (ret != NO_ERROR) {
1419 BusinessError::ThrowErrorByErrcode(env, ret);
1420 return result;
1421 }
1422 NAPI_CALL(env, napi_get_undefined(env, &result));
1423
1424 return result;
1425 }
1426
PostMessageEvent(napi_env env,napi_callback_info info)1427 napi_value NapiWebMessagePort::PostMessageEvent(napi_env env, napi_callback_info info)
1428 {
1429 WVLOG_D("message port post message");
1430 napi_value thisVar = nullptr;
1431 napi_value result = nullptr;
1432 size_t argc = INTEGER_ONE;
1433 napi_value argv[INTEGER_ONE];
1434 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1435 if (argc != INTEGER_ONE) {
1436 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1437 return result;
1438 }
1439 napi_valuetype valueType = napi_undefined;
1440 napi_typeof(env, argv[INTEGER_ZERO], &valueType);
1441
1442 bool isArrayBuffer = false;
1443 NAPI_CALL(env, napi_is_arraybuffer(env, argv[INTEGER_ZERO], &isArrayBuffer));
1444 if (valueType != napi_string && !isArrayBuffer) {
1445 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1446 return result;
1447 }
1448
1449 auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE);
1450 if (valueType == napi_string) {
1451 size_t bufferSize = 0;
1452 napi_get_value_string_utf8(env, argv[INTEGER_ZERO], nullptr, 0, &bufferSize);
1453 if (bufferSize > UINT_MAX) {
1454 WVLOG_E("String length is too long");
1455 return result;
1456 }
1457 char* stringValue = new (std::nothrow) char[bufferSize + 1];
1458 if (stringValue == nullptr) {
1459 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1460 return result;
1461 }
1462 size_t jsStringLength = 0;
1463 napi_get_value_string_utf8(env, argv[INTEGER_ZERO], stringValue, bufferSize + 1, &jsStringLength);
1464 std::string message(stringValue);
1465 delete [] stringValue;
1466 stringValue = nullptr;
1467
1468 webMsg->SetType(NWebValue::Type::STRING);
1469 webMsg->SetString(message);
1470 } else if (isArrayBuffer) {
1471 uint8_t *arrBuf = nullptr;
1472 size_t byteLength = 0;
1473 napi_get_arraybuffer_info(env, argv[INTEGER_ZERO], (void**)&arrBuf, &byteLength);
1474 std::vector<uint8_t> vecData(arrBuf, arrBuf + byteLength);
1475 webMsg->SetType(NWebValue::Type::BINARY);
1476 webMsg->SetBinary(vecData);
1477 }
1478
1479 WebMessagePort *msgPort = nullptr;
1480 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort));
1481 if (msgPort == nullptr) {
1482 WVLOG_E("post message failed, napi unwrap msg port failed");
1483 return nullptr;
1484 }
1485 ErrCode ret = msgPort->PostPortMessage(webMsg);
1486 if (ret != NO_ERROR) {
1487 BusinessError::ThrowErrorByErrcode(env, ret);
1488 return result;
1489 }
1490 NAPI_CALL(env, napi_get_undefined(env, &result));
1491
1492 return result;
1493 }
1494
PostMessageEventExt(napi_env env,napi_callback_info info)1495 napi_value NapiWebMessagePort::PostMessageEventExt(napi_env env, napi_callback_info info)
1496 {
1497 WVLOG_D("message PostMessageEventExt start");
1498 napi_value thisVar = nullptr;
1499 napi_value result = nullptr;
1500 size_t argc = INTEGER_ONE;
1501 napi_value argv[INTEGER_ONE];
1502 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1503 if (argc != INTEGER_ONE) {
1504 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1505 return result;
1506 }
1507 napi_valuetype valueType = napi_undefined;
1508 napi_typeof(env, argv[INTEGER_ZERO], &valueType);
1509 if (valueType != napi_object) {
1510 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1511 return result;
1512 }
1513
1514 WebMessageExt *webMessageExt = nullptr;
1515 NAPI_CALL(env, napi_unwrap(env, argv[INTEGER_ZERO], (void **)&webMessageExt));
1516 if (webMessageExt == nullptr) {
1517 WVLOG_E("post message failed, napi unwrap msg port failed");
1518 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1519 return nullptr;
1520 }
1521
1522 WebMessagePort *msgPort = nullptr;
1523 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort));
1524 if (msgPort == nullptr) {
1525 WVLOG_E("post message failed, napi unwrap msg port failed");
1526 return nullptr;
1527 }
1528
1529 if (!msgPort->IsExtentionType()) {
1530 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1531 return result;
1532 }
1533
1534 ErrCode ret = msgPort->PostPortMessage(webMessageExt->GetData());
1535 if (ret != NO_ERROR) {
1536 BusinessError::ThrowErrorByErrcode(env, ret);
1537 return result;
1538 }
1539 NAPI_CALL(env, napi_get_undefined(env, &result));
1540
1541 return result;
1542 }
1543
1544
OnMessageEventExt(napi_env env,napi_callback_info info)1545 napi_value NapiWebMessagePort::OnMessageEventExt(napi_env env, napi_callback_info info)
1546 {
1547 WVLOG_D("message port set OnMessageEventExt callback");
1548 napi_value thisVar = nullptr;
1549 napi_value result = nullptr;
1550 size_t argc = INTEGER_ONE;
1551 napi_value argv[INTEGER_ONE];
1552 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1553 if (argc != INTEGER_ONE) {
1554 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1555 return result;
1556 }
1557 napi_valuetype valueType = napi_undefined;
1558 napi_typeof(env, argv[INTEGER_ZERO], &valueType);
1559 if (valueType != napi_function) {
1560 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1561 return result;
1562 }
1563
1564 napi_ref onMsgEventFunc = nullptr;
1565 NAPI_CALL(env, napi_create_reference(env, argv[INTEGER_ZERO], INTEGER_ONE, &onMsgEventFunc));
1566
1567 auto callbackImpl = std::make_shared<NWebValueCallbackImpl>(env, onMsgEventFunc, true);
1568
1569 WebMessagePort *msgPort = nullptr;
1570 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort));
1571 if (msgPort == nullptr) {
1572 WVLOG_E("set message event callback failed, napi unwrap msg port failed");
1573 return nullptr;
1574 }
1575 ErrCode ret = msgPort->SetPortMessageCallback(callbackImpl);
1576 if (ret != NO_ERROR) {
1577 BusinessError::ThrowErrorByErrcode(env, ret);
1578 }
1579 NAPI_CALL(env, napi_get_undefined(env, &result));
1580 return result;
1581 }
1582
UvWebMessageOnReceiveValueCallback(uv_work_t * work,int status)1583 void NWebValueCallbackImpl::UvWebMessageOnReceiveValueCallback(uv_work_t *work, int status)
1584 {
1585 if (work == nullptr) {
1586 WVLOG_E("uv work is null");
1587 return;
1588 }
1589 NapiWebMessagePort::WebMsgPortParam *data = reinterpret_cast<NapiWebMessagePort::WebMsgPortParam*>(work->data);
1590 if (data == nullptr) {
1591 WVLOG_E("WebMsgPortParam is null");
1592 delete work;
1593 work = nullptr;
1594 return;
1595 }
1596 napi_handle_scope scope = nullptr;
1597 napi_open_handle_scope(data->env_, &scope);
1598 if (scope == nullptr) {
1599 delete work;
1600 work = nullptr;
1601 return;
1602 }
1603 napi_value result[INTEGER_ONE] = {0};
1604 if (data->extention_) {
1605 napi_value webMsgExt = nullptr;
1606 napi_status status = napi_get_reference_value(data->env_, g_webMsgExtClassRef, &webMsgExt);
1607 if (status != napi_status::napi_ok) {
1608 WVLOG_E("napi_get_reference_value failed.");
1609 delete work;
1610 work = nullptr;
1611 napi_close_handle_scope(data->env_, scope);
1612 return;
1613 }
1614 status = napi_new_instance(data->env_, webMsgExt, 0, NULL, &result[INTEGER_ZERO]);
1615
1616 WebMessageExt *webMessageExt = new (std::nothrow) WebMessageExt(data->msg_);
1617 if (webMessageExt == nullptr) {
1618 WVLOG_E("new WebMessageExt failed.");
1619 delete work;
1620 work = nullptr;
1621 napi_close_handle_scope(data->env_, scope);
1622 return;
1623 }
1624
1625 status = napi_wrap(data->env_, result[INTEGER_ZERO], webMessageExt,
1626 [](napi_env env, void *data, void *hint) {
1627 WebMessageExt *webMessageExt = static_cast<WebMessageExt *>(data);
1628 delete webMessageExt;
1629 },
1630 nullptr, nullptr);
1631 if (status != napi_status::napi_ok) {
1632 WVLOG_E("napi_wrap failed.");
1633 delete work;
1634 work = nullptr;
1635 napi_close_handle_scope(data->env_, scope);
1636 return;
1637 }
1638 } else {
1639 NapiParseUtils::ConvertNWebToNapiValue(data->env_, data->msg_, result[INTEGER_ZERO]);
1640 }
1641
1642 napi_value onMsgEventFunc = nullptr;
1643 napi_get_reference_value(data->env_, data->callback_, &onMsgEventFunc);
1644 napi_value placeHodler = nullptr;
1645 napi_call_function(data->env_, nullptr, onMsgEventFunc, INTEGER_ONE, &result[INTEGER_ZERO], &placeHodler);
1646
1647 std::unique_lock<std::mutex> lock(data->mutex_);
1648 data->ready_ = true;
1649 data->condition_.notify_all();
1650 napi_close_handle_scope(data->env_, scope);
1651 }
1652
OnReceiveValue(std::shared_ptr<NWebMessage> result)1653 void NWebValueCallbackImpl::OnReceiveValue(std::shared_ptr<NWebMessage> result)
1654 {
1655 WVLOG_D("message port received msg");
1656 uv_loop_s *loop = nullptr;
1657 uv_work_t *work = nullptr;
1658 napi_get_uv_event_loop(env_, &loop);
1659 if (loop == nullptr) {
1660 WVLOG_E("get uv event loop failed");
1661 return;
1662 }
1663 work = new (std::nothrow) uv_work_t;
1664 if (work == nullptr) {
1665 WVLOG_E("new uv work failed");
1666 return;
1667 }
1668 NapiWebMessagePort::WebMsgPortParam *param = new (std::nothrow) NapiWebMessagePort::WebMsgPortParam();
1669 if (param == nullptr) {
1670 WVLOG_E("new WebMsgPortParam failed");
1671 delete work;
1672 return;
1673 }
1674 param->env_ = env_;
1675 param->callback_ = callback_;
1676 param->msg_ = result;
1677 param->extention_ = extention_;
1678 work->data = reinterpret_cast<void*>(param);
1679 uv_queue_work(loop, work, [](uv_work_t *work) {}, UvWebMessageOnReceiveValueCallback);
1680
1681 {
1682 std::unique_lock<std::mutex> lock(param->mutex_);
1683 param->condition_.wait(lock, [¶m] { return param->ready_; });
1684 }
1685 if (param != nullptr) {
1686 delete param;
1687 param = nullptr;
1688 }
1689 if (work != nullptr) {
1690 delete work;
1691 work = nullptr;
1692 }
1693 }
1694
~NWebValueCallbackImpl()1695 NWebValueCallbackImpl::~NWebValueCallbackImpl()
1696 {
1697 WVLOG_D("~NWebValueCallbackImpl");
1698 uv_loop_s *loop = nullptr;
1699 uv_work_t *work = nullptr;
1700 napi_get_uv_event_loop(env_, &loop);
1701 if (loop == nullptr) {
1702 WVLOG_E("get uv event loop failed");
1703 return;
1704 }
1705 work = new (std::nothrow) uv_work_t;
1706 if (work == nullptr) {
1707 WVLOG_E("new uv work failed");
1708 return;
1709 }
1710 NapiWebMessagePort::WebMsgPortParam *param = new (std::nothrow) NapiWebMessagePort::WebMsgPortParam();
1711 if (param == nullptr) {
1712 WVLOG_E("new WebMsgPortParam failed");
1713 delete work;
1714 return;
1715 }
1716 param->env_ = env_;
1717 param->callback_ = callback_;
1718 work->data = reinterpret_cast<void*>(param);
1719 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) {
1720 if (work == nullptr) {
1721 WVLOG_E("uv work is null");
1722 return;
1723 }
1724 NapiWebMessagePort::WebMsgPortParam *data = reinterpret_cast<NapiWebMessagePort::WebMsgPortParam*>(work->data);
1725 if (data == nullptr) {
1726 WVLOG_E("WebMsgPortParam is null");
1727 delete work;
1728 work = nullptr;
1729 return;
1730 }
1731
1732 napi_delete_reference(data->env_, data->callback_);
1733 delete data;
1734 data = nullptr;
1735 delete work;
1736 work = nullptr;
1737 });
1738 if (ret != 0) {
1739 if (param != nullptr) {
1740 delete param;
1741 param = nullptr;
1742 }
1743 if (work != nullptr) {
1744 delete work;
1745 work = nullptr;
1746 }
1747 }
1748 }
1749
OnMessageEvent(napi_env env,napi_callback_info info)1750 napi_value NapiWebMessagePort::OnMessageEvent(napi_env env, napi_callback_info info)
1751 {
1752 WVLOG_D("message port set OnMessageEvent callback");
1753 napi_value thisVar = nullptr;
1754 napi_value result = nullptr;
1755 size_t argc = INTEGER_ONE;
1756 napi_value argv[INTEGER_ONE];
1757 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
1758 if (argc != INTEGER_ONE) {
1759 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1760 return result;
1761 }
1762 napi_valuetype valueType = napi_undefined;
1763 napi_typeof(env, argv[INTEGER_ZERO], &valueType);
1764 if (valueType != napi_function) {
1765 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1766 return result;
1767 }
1768
1769 napi_ref onMsgEventFunc = nullptr;
1770 NAPI_CALL(env, napi_create_reference(env, argv[INTEGER_ZERO], INTEGER_ONE, &onMsgEventFunc));
1771
1772 auto callbackImpl = std::make_shared<NWebValueCallbackImpl>(env, onMsgEventFunc, false);
1773
1774 WebMessagePort *msgPort = nullptr;
1775 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&msgPort));
1776 if (msgPort == nullptr) {
1777 WVLOG_E("set message event callback failed, napi unwrap msg port failed");
1778 return nullptr;
1779 }
1780 ErrCode ret = msgPort->SetPortMessageCallback(callbackImpl);
1781 if (ret != NO_ERROR) {
1782 BusinessError::ThrowErrorByErrcode(env, ret);
1783 }
1784 NAPI_CALL(env, napi_get_undefined(env, &result));
1785 return result;
1786 }
1787
ZoomIn(napi_env env,napi_callback_info info)1788 napi_value NapiWebviewController::ZoomIn(napi_env env, napi_callback_info info)
1789 {
1790 napi_value thisVar = nullptr;
1791 napi_value result = nullptr;
1792 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1793
1794 WebviewController *webviewController = nullptr;
1795 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1796 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1797 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1798 return nullptr;
1799 }
1800
1801 ErrCode ret = webviewController->ZoomIn();
1802 if (ret != NO_ERROR) {
1803 if (ret == NWEB_ERROR) {
1804 WVLOG_E("ZoomIn failed.");
1805 return nullptr;
1806 }
1807 BusinessError::ThrowErrorByErrcode(env, ret);
1808 }
1809
1810 NAPI_CALL(env, napi_get_undefined(env, &result));
1811 return result;
1812 }
1813
ZoomOut(napi_env env,napi_callback_info info)1814 napi_value NapiWebviewController::ZoomOut(napi_env env, napi_callback_info info)
1815 {
1816 napi_value thisVar = nullptr;
1817 napi_value result = nullptr;
1818 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1819
1820 WebviewController *webviewController = nullptr;
1821 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1822 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1823 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1824 return nullptr;
1825 }
1826
1827 ErrCode ret = webviewController->ZoomOut();
1828 if (ret != NO_ERROR) {
1829 if (ret == NWEB_ERROR) {
1830 WVLOG_E("ZoomOut failed.");
1831 return nullptr;
1832 }
1833 BusinessError::ThrowErrorByErrcode(env, ret);
1834 }
1835
1836 NAPI_CALL(env, napi_get_undefined(env, &result));
1837 return result;
1838 }
1839
GetWebId(napi_env env,napi_callback_info info)1840 napi_value NapiWebviewController::GetWebId(napi_env env, napi_callback_info info)
1841 {
1842 napi_value thisVar = nullptr;
1843 napi_value result = nullptr;
1844 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1845
1846 WebviewController *webviewController = nullptr;
1847 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1848 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1849 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1850 return nullptr;
1851 }
1852
1853 int32_t webId = webviewController->GetWebId();
1854 napi_create_int32(env, webId, &result);
1855
1856 return result;
1857 }
1858
GetUserAgent(napi_env env,napi_callback_info info)1859 napi_value NapiWebviewController::GetUserAgent(napi_env env, napi_callback_info info)
1860 {
1861 napi_value thisVar = nullptr;
1862 napi_value result = nullptr;
1863 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1864
1865 WebviewController *webviewController = nullptr;
1866 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1867 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1868 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1869 return nullptr;
1870 }
1871
1872 std::string userAgent = "";
1873 userAgent = webviewController->GetUserAgent();
1874 napi_create_string_utf8(env, userAgent.c_str(), userAgent.length(), &result);
1875
1876 return result;
1877 }
1878
GetCustomUserAgent(napi_env env,napi_callback_info info)1879 napi_value NapiWebviewController::GetCustomUserAgent(napi_env env, napi_callback_info info)
1880 {
1881 napi_value thisVar = nullptr;
1882 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1883
1884 WebviewController *webviewController = nullptr;
1885 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1886 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1887 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1888 return nullptr;
1889 }
1890
1891 napi_value result = nullptr;
1892 std::string userAgent = webviewController->GetCustomUserAgent();
1893 napi_create_string_utf8(env, userAgent.c_str(), userAgent.length(), &result);
1894 return result;
1895 }
1896
SetCustomUserAgent(napi_env env,napi_callback_info info)1897 napi_value NapiWebviewController::SetCustomUserAgent(napi_env env, napi_callback_info info)
1898 {
1899 napi_value thisVar = nullptr;
1900 napi_value result = nullptr;
1901 size_t argc = INTEGER_ONE;
1902 napi_value argv[INTEGER_ONE];
1903 NAPI_CALL(env, napi_get_undefined(env, &result));
1904
1905 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1906 if (argc != INTEGER_ONE) {
1907 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1908 return result;
1909 }
1910
1911 std::string userAgent;
1912 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], userAgent)) {
1913 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1914 return result;
1915 }
1916
1917 WebviewController *webviewController = nullptr;
1918 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1919 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1920 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1921 return result;
1922 }
1923 ErrCode ret = webviewController->SetCustomUserAgent(userAgent);
1924 if (ret != NO_ERROR) {
1925 BusinessError::ThrowErrorByErrcode(env, ret);
1926 }
1927 return result;
1928 }
1929
GetTitle(napi_env env,napi_callback_info info)1930 napi_value NapiWebviewController::GetTitle(napi_env env, napi_callback_info info)
1931 {
1932 napi_value thisVar = nullptr;
1933 napi_value result = nullptr;
1934 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1935
1936 WebviewController *webviewController = nullptr;
1937 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1938 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1939 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1940 return nullptr;
1941 }
1942
1943 std::string title = "";
1944 title = webviewController->GetTitle();
1945 napi_create_string_utf8(env, title.c_str(), title.length(), &result);
1946
1947 return result;
1948 }
1949
GetPageHeight(napi_env env,napi_callback_info info)1950 napi_value NapiWebviewController::GetPageHeight(napi_env env, napi_callback_info info)
1951 {
1952 napi_value thisVar = nullptr;
1953 napi_value result = nullptr;
1954 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1955
1956 WebviewController *webviewController = nullptr;
1957 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1958 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1959 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1960 return nullptr;
1961 }
1962
1963 int32_t pageHeight = webviewController->GetPageHeight();
1964 napi_create_int32(env, pageHeight, &result);
1965
1966 return result;
1967 }
1968
BackOrForward(napi_env env,napi_callback_info info)1969 napi_value NapiWebviewController::BackOrForward(napi_env env, napi_callback_info info)
1970 {
1971 napi_value thisVar = nullptr;
1972 napi_value result = nullptr;
1973 size_t argc = INTEGER_ONE;
1974 napi_value argv[INTEGER_ONE] = {0};
1975 void* data = nullptr;
1976 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1977
1978 if (argc != INTEGER_ONE) {
1979 WVLOG_E("Requires 1 parameters.");
1980 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1981 return nullptr;
1982 }
1983
1984 int32_t step = -1;
1985 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], step)) {
1986 WVLOG_E("Parameter is not integer number type.");
1987 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1988 return nullptr;
1989 }
1990
1991 WebviewController *webviewController = nullptr;
1992 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
1993 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
1994 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
1995 return nullptr;
1996 }
1997
1998 ErrCode ret = webviewController->BackOrForward(step);
1999 if (ret != NO_ERROR) {
2000 BusinessError::ThrowErrorByErrcode(env, ret);
2001 }
2002
2003 NAPI_CALL(env, napi_get_undefined(env, &result));
2004 return result;
2005 }
2006
StoreWebArchive(napi_env env,napi_callback_info info)2007 napi_value NapiWebviewController::StoreWebArchive(napi_env env, napi_callback_info info)
2008 {
2009 napi_value thisVar = nullptr;
2010 napi_value result = nullptr;
2011 size_t argc = INTEGER_ONE;
2012 size_t argcPromise = INTEGER_TWO;
2013 size_t argcCallback = INTEGER_THREE;
2014 napi_value argv[INTEGER_THREE] = { 0 };
2015
2016 napi_get_undefined(env, &result);
2017 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2018
2019 if (argc != argcPromise && argc != argcCallback) {
2020 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2021 return result;
2022 }
2023 std::string baseName;
2024 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], baseName)) {
2025 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2026 return result;
2027 }
2028
2029 if (baseName.empty()) {
2030 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2031 return result;
2032 }
2033
2034 bool autoName = false;
2035 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2036 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ONE], autoName)) {
2037 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2038 return result;
2039 }
2040
2041 if (argc == argcCallback) {
2042 napi_valuetype valueType = napi_null;
2043 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2044 napi_typeof(env, argv[argcCallback - 1], &valueType);
2045 if (valueType != napi_function) {
2046 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2047 return result;
2048 }
2049 }
2050 return StoreWebArchiveInternal(env, info, baseName, autoName);
2051 }
2052
StoreWebArchiveInternal(napi_env env,napi_callback_info info,const std::string & baseName,bool autoName)2053 napi_value NapiWebviewController::StoreWebArchiveInternal(napi_env env, napi_callback_info info,
2054 const std::string &baseName, bool autoName)
2055 {
2056 napi_value thisVar = nullptr;
2057 size_t argc = INTEGER_ONE;
2058 size_t argcPromise = INTEGER_TWO;
2059 size_t argcCallback = INTEGER_THREE;
2060 napi_value argv[INTEGER_THREE] = {0};
2061
2062 napi_value result = nullptr;
2063 napi_get_undefined(env, &result);
2064 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2065
2066 WebviewController *webviewController = nullptr;
2067 napi_unwrap(env, thisVar, (void **)&webviewController);
2068
2069 if (!webviewController || !webviewController->IsInit()) {
2070 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2071 return result;
2072 }
2073
2074 if (argc == argcCallback) {
2075 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2076 napi_ref jsCallback = nullptr;
2077 napi_create_reference(env, argv[argcCallback - 1], 1, &jsCallback);
2078
2079 if (jsCallback) {
2080 webviewController->StoreWebArchiveCallback(baseName, autoName, env, std::move(jsCallback));
2081 }
2082 return result;
2083 } else if (argc == argcPromise) {
2084 napi_deferred deferred = nullptr;
2085 napi_value promise = nullptr;
2086 napi_create_promise(env, &deferred, &promise);
2087 if (promise && deferred) {
2088 webviewController->StoreWebArchivePromise(baseName, autoName, env, deferred);
2089 }
2090 return promise;
2091 }
2092 return result;
2093 }
2094
GetHitTestValue(napi_env env,napi_callback_info info)2095 napi_value NapiWebviewController::GetHitTestValue(napi_env env, napi_callback_info info)
2096 {
2097 napi_value thisVar = nullptr;
2098 napi_value result = nullptr;
2099 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2100
2101 WebviewController *webviewController = nullptr;
2102 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2103 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2104 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2105 return nullptr;
2106 }
2107
2108 HitTestResult nwebResult = webviewController->GetHitTestValue();
2109
2110 napi_create_object(env, &result);
2111
2112 napi_value type;
2113 napi_create_uint32(env, nwebResult.GetType(), &type);
2114 napi_set_named_property(env, result, "type", type);
2115
2116 napi_value extra;
2117 napi_create_string_utf8(env, nwebResult.GetExtra().c_str(), NAPI_AUTO_LENGTH, &extra);
2118 napi_set_named_property(env, result, "extra", extra);
2119
2120 return result;
2121 }
2122
RequestFocus(napi_env env,napi_callback_info info)2123 napi_value NapiWebviewController::RequestFocus(napi_env env, napi_callback_info info)
2124 {
2125 napi_value thisVar = nullptr;
2126 napi_value result = nullptr;
2127 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2128
2129 WebviewController *webviewController = nullptr;
2130 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2131 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2132 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2133 return nullptr;
2134 }
2135
2136 webviewController->RequestFocus();
2137 NAPI_CALL(env, napi_get_undefined(env, &result));
2138 return result;
2139 }
2140
LoadUrl(napi_env env,napi_callback_info info)2141 napi_value NapiWebviewController::LoadUrl(napi_env env, napi_callback_info info)
2142 {
2143 napi_value thisVar = nullptr;
2144 napi_value result = nullptr;
2145 size_t argc = INTEGER_TWO;
2146 napi_value argv[INTEGER_TWO];
2147 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2148 if ((argc != INTEGER_ONE) && (argc != INTEGER_TWO)) {
2149 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2150 return nullptr;
2151 }
2152 WebviewController *webviewController = nullptr;
2153 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2154 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2155 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2156 return nullptr;
2157 }
2158 napi_valuetype webSrcType;
2159 napi_typeof(env, argv[INTEGER_ZERO], &webSrcType);
2160 if (webSrcType != napi_string && webSrcType != napi_object) {
2161 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2162 return nullptr;
2163 }
2164 std::string webSrc;
2165 if (!webviewController->ParseUrl(env, argv[INTEGER_ZERO], webSrc)) {
2166 BusinessError::ThrowErrorByErrcode(env, INVALID_URL);
2167 }
2168 std::string fileProtocolName = "file";
2169 if (webSrc.substr(INTEGER_ZERO, fileProtocolName.size()) == "file") {
2170 std::string filePath = webSrc;
2171 std::string fileProtocol = "file:///";
2172 filePath.erase(INTEGER_ZERO, fileProtocol.size());
2173 int isFileExist = access(filePath.c_str(), F_OK);
2174 if (isFileExist == -1) {
2175 BusinessError::ThrowErrorByErrcode(env, INVALID_RESOURCE);
2176 }
2177 }
2178 if (argc == INTEGER_ONE) {
2179 ErrCode ret = webviewController->LoadUrl(webSrc);
2180 if (ret != NO_ERROR) {
2181 if (ret == NWEB_ERROR) {
2182 return nullptr;
2183 }
2184 BusinessError::ThrowErrorByErrcode(env, ret);
2185 return nullptr;
2186 }
2187 NAPI_CALL(env, napi_get_undefined(env, &result));
2188 return result;
2189 }
2190 return LoadUrlWithHttpHeaders(env, info, webSrc, argv, webviewController);
2191 }
2192
LoadUrlWithHttpHeaders(napi_env env,napi_callback_info info,const std::string & url,const napi_value * argv,WebviewController * webviewController)2193 napi_value NapiWebviewController::LoadUrlWithHttpHeaders(napi_env env, napi_callback_info info, const std::string& url,
2194 const napi_value* argv, WebviewController* webviewController)
2195 {
2196 napi_value result = nullptr;
2197 std::map<std::string, std::string> httpHeaders;
2198 napi_value array = argv[INTEGER_ONE];
2199 bool isArray = false;
2200 napi_is_array(env, array, &isArray);
2201 if (isArray) {
2202 uint32_t arrayLength = INTEGER_ZERO;
2203 napi_get_array_length(env, array, &arrayLength);
2204 for (uint32_t i = 0; i < arrayLength; ++i) {
2205 std::string key;
2206 std::string value;
2207 napi_value obj = nullptr;
2208 napi_value keyObj = nullptr;
2209 napi_value valueObj = nullptr;
2210 napi_get_element(env, array, i, &obj);
2211 if (napi_get_named_property(env, obj, "headerKey", &keyObj) != napi_ok) {
2212 continue;
2213 }
2214 if (napi_get_named_property(env, obj, "headerValue", &valueObj) != napi_ok) {
2215 continue;
2216 }
2217 NapiParseUtils::ParseString(env, keyObj, key);
2218 NapiParseUtils::ParseString(env, valueObj, value);
2219 httpHeaders[key] = value;
2220 }
2221 } else {
2222 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2223 return nullptr;
2224 }
2225
2226 ErrCode ret = webviewController->LoadUrl(url, httpHeaders);
2227 if (ret != NO_ERROR) {
2228 if (ret == NWEB_ERROR) {
2229 WVLOG_E("LoadUrl failed.");
2230 return nullptr;
2231 }
2232 BusinessError::ThrowErrorByErrcode(env, ret);
2233 return nullptr;
2234 }
2235 NAPI_CALL(env, napi_get_undefined(env, &result));
2236 return result;
2237 }
2238
LoadData(napi_env env,napi_callback_info info)2239 napi_value NapiWebviewController::LoadData(napi_env env, napi_callback_info info)
2240 {
2241 napi_value thisVar = nullptr;
2242 napi_value result = nullptr;
2243 size_t argc = INTEGER_FIVE;
2244 napi_value argv[INTEGER_FIVE];
2245 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2246 if ((argc != INTEGER_THREE) && (argc != INTEGER_FOUR) &&
2247 (argc != INTEGER_FIVE)) {
2248 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2249 return nullptr;
2250 }
2251 WebviewController *webviewController = nullptr;
2252 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2253 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2254 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2255 return nullptr;
2256 }
2257 std::string data;
2258 std::string mimeType;
2259 std::string encoding;
2260 std::string baseUrl;
2261 std::string historyUrl;
2262 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], data) ||
2263 !NapiParseUtils::ParseString(env, argv[INTEGER_ONE], mimeType) ||
2264 !NapiParseUtils::ParseString(env, argv[INTEGER_TWO], encoding)) {
2265 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2266 return nullptr;
2267 }
2268 if ((argc >= INTEGER_FOUR) && !NapiParseUtils::ParseString(env, argv[INTEGER_THREE], baseUrl)) {
2269 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2270 return nullptr;
2271 }
2272 if ((argc == INTEGER_FIVE) && !NapiParseUtils::ParseString(env, argv[INTEGER_FOUR], historyUrl)) {
2273 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2274 return nullptr;
2275 }
2276 ErrCode ret = webviewController->LoadData(data, mimeType, encoding, baseUrl, historyUrl);
2277 if (ret != NO_ERROR) {
2278 if (ret == NWEB_ERROR) {
2279 WVLOG_E("LoadData failed.");
2280 return nullptr;
2281 }
2282 BusinessError::ThrowErrorByErrcode(env, ret);
2283 return nullptr;
2284 }
2285
2286 NAPI_CALL(env, napi_get_undefined(env, &result));
2287 return result;
2288 }
2289
GetHitTest(napi_env env,napi_callback_info info)2290 napi_value NapiWebviewController::GetHitTest(napi_env env, napi_callback_info info)
2291 {
2292 napi_value thisVar = nullptr;
2293 napi_value result = nullptr;
2294 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2295
2296 WebviewController *webviewController = nullptr;
2297 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2298 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2299 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2300 return nullptr;
2301 }
2302
2303 int32_t type = webviewController->GetHitTest();
2304 napi_create_int32(env, type, &result);
2305 return result;
2306 }
2307
ClearMatches(napi_env env,napi_callback_info info)2308 napi_value NapiWebviewController::ClearMatches(napi_env env, napi_callback_info info)
2309 {
2310 napi_value thisVar = nullptr;
2311 napi_value result = nullptr;
2312
2313 NAPI_CALL(env, napi_get_undefined(env, &result));
2314 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2315
2316 WebviewController *controller = nullptr;
2317 napi_unwrap(env, thisVar, (void **)&controller);
2318 if (!controller || !controller->IsInit()) {
2319 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2320 return result;
2321 }
2322 controller->ClearMatches();
2323 return result;
2324 }
2325
SearchNext(napi_env env,napi_callback_info info)2326 napi_value NapiWebviewController::SearchNext(napi_env env, napi_callback_info info)
2327 {
2328 napi_value thisVar = nullptr;
2329 napi_value result = nullptr;
2330 size_t argc = INTEGER_ONE;
2331 napi_value argv[INTEGER_ONE] = { 0 };
2332
2333 NAPI_CALL(env, napi_get_undefined(env, &result));
2334 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2335 if (argc != INTEGER_ONE) {
2336 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2337 return result;
2338 }
2339 bool forward;
2340 if (!NapiParseUtils::ParseBoolean(env, argv[0], forward)) {
2341 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2342 return result;
2343 }
2344
2345 WebviewController *controller = nullptr;
2346 napi_unwrap(env, thisVar, (void **)&controller);
2347 if (!controller || !controller->IsInit()) {
2348 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2349 return result;
2350 }
2351 controller->SearchNext(forward);
2352 return result;
2353 }
2354
SearchAllAsync(napi_env env,napi_callback_info info)2355 napi_value NapiWebviewController::SearchAllAsync(napi_env env, napi_callback_info info)
2356 {
2357 napi_value thisVar = nullptr;
2358 napi_value result = nullptr;
2359 size_t argc = INTEGER_ONE;
2360 napi_value argv[INTEGER_ONE] = { 0 };
2361
2362 NAPI_CALL(env, napi_get_undefined(env, &result));
2363 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2364 if (argc != INTEGER_ONE) {
2365 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2366 return result;
2367 }
2368 std::string searchString;
2369 if (!NapiParseUtils::ParseString(env, argv[0], searchString)) {
2370 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2371 return result;
2372 }
2373
2374 WebviewController *controller = nullptr;
2375 napi_unwrap(env, thisVar, (void **)&controller);
2376 if (!controller || !controller->IsInit()) {
2377 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2378 return result;
2379 }
2380 controller->SearchAllAsync(searchString);
2381 return result;
2382 }
2383
ClearSslCache(napi_env env,napi_callback_info info)2384 napi_value NapiWebviewController::ClearSslCache(napi_env env, napi_callback_info info)
2385 {
2386 napi_value thisVar = nullptr;
2387 napi_value result = nullptr;
2388
2389 NAPI_CALL(env, napi_get_undefined(env, &result));
2390 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2391
2392 WebviewController *controller = nullptr;
2393 napi_unwrap(env, thisVar, (void **)&controller);
2394 if (!controller || !controller->IsInit()) {
2395 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2396 return result;
2397 }
2398 controller->ClearSslCache();
2399 return result;
2400 }
2401
ClearClientAuthenticationCache(napi_env env,napi_callback_info info)2402 napi_value NapiWebviewController::ClearClientAuthenticationCache(napi_env env, napi_callback_info info)
2403 {
2404 napi_value thisVar = nullptr;
2405 napi_value result = nullptr;
2406
2407 NAPI_CALL(env, napi_get_undefined(env, &result));
2408 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2409
2410 WebviewController *controller = nullptr;
2411 napi_unwrap(env, thisVar, (void **)&controller);
2412 if (!controller || !controller->IsInit()) {
2413 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2414 return result;
2415 }
2416 controller->ClearClientAuthenticationCache();
2417
2418 return result;
2419 }
2420
Stop(napi_env env,napi_callback_info info)2421 napi_value NapiWebviewController::Stop(napi_env env, napi_callback_info info)
2422 {
2423 napi_value thisVar = nullptr;
2424 napi_value result = nullptr;
2425 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2426
2427 WebviewController *controller = nullptr;
2428 napi_unwrap(env, thisVar, (void **)&controller);
2429 if (!controller || !controller->IsInit()) {
2430 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2431 return result;
2432 }
2433 controller->Stop();
2434
2435 NAPI_CALL(env, napi_get_undefined(env, &result));
2436 return result;
2437 }
2438
Zoom(napi_env env,napi_callback_info info)2439 napi_value NapiWebviewController::Zoom(napi_env env, napi_callback_info info)
2440 {
2441 napi_value thisVar = nullptr;
2442 napi_value result = nullptr;
2443 size_t argc = INTEGER_ONE;
2444 napi_value argv[INTEGER_ONE] = { 0 };
2445
2446 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2447 if (argc != INTEGER_ONE) {
2448 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2449 return result;
2450 }
2451 float factor = 0.0;
2452 if (!NapiParseUtils::ParseFloat(env, argv[0], factor)) {
2453 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2454 return result;
2455 }
2456
2457 WebviewController *controller = nullptr;
2458 napi_unwrap(env, thisVar, (void **)&controller);
2459 if (!controller || !controller->IsInit()) {
2460 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2461 return result;
2462 }
2463
2464 ErrCode ret = controller->Zoom(factor);
2465 if (ret != NO_ERROR) {
2466 if (ret == NWEB_ERROR) {
2467 WVLOG_E("Zoom failed.");
2468 return result;
2469 }
2470 BusinessError::ThrowErrorByErrcode(env, ret);
2471 }
2472
2473 NAPI_CALL(env, napi_get_undefined(env, &result));
2474 return result;
2475 }
2476
RegisterJavaScriptProxy(napi_env env,napi_callback_info info)2477 napi_value NapiWebviewController::RegisterJavaScriptProxy(napi_env env, napi_callback_info info)
2478 {
2479 WVLOG_D("NapiWebviewController::RegisterJavaScriptProxy");
2480 napi_value thisVar = nullptr;
2481 napi_value result = nullptr;
2482 size_t argc = INTEGER_THREE;
2483 napi_value argv[INTEGER_THREE] = { 0 };
2484
2485 napi_get_undefined(env, &result);
2486 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2487 if (argc != INTEGER_THREE) {
2488 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2489 return result;
2490 }
2491
2492 napi_valuetype valueType = napi_undefined;
2493 napi_typeof(env, argv[INTEGER_ZERO], &valueType);
2494 if (valueType != napi_object) {
2495 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2496 return result;
2497 }
2498
2499 std::string objName;
2500 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], objName)) {
2501 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2502 return result;
2503 }
2504
2505 std::vector<std::string> methodList;
2506 if (!NapiParseUtils::ParseStringArray(env, argv[INTEGER_TWO], methodList)) {
2507 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2508 return result;
2509 }
2510
2511 WebviewController *controller = nullptr;
2512 napi_unwrap(env, thisVar, (void **)&controller);
2513 if (!controller || !controller->IsInit()) {
2514 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2515 return result;
2516 }
2517 controller->SetNWebJavaScriptResultCallBack();
2518 controller->RegisterJavaScriptProxy(env, argv[INTEGER_ZERO], objName, methodList);
2519 return result;
2520 }
2521
DeleteJavaScriptRegister(napi_env env,napi_callback_info info)2522 napi_value NapiWebviewController::DeleteJavaScriptRegister(napi_env env, napi_callback_info info)
2523 {
2524 napi_value thisVar = nullptr;
2525 napi_value result = nullptr;
2526 size_t argc = INTEGER_ONE;
2527 napi_value argv[INTEGER_ONE] = { 0 };
2528
2529 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2530 if (argc != INTEGER_ONE) {
2531 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2532 return result;
2533 }
2534
2535 std::string objName;
2536 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], objName)) {
2537 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2538 return result;
2539 }
2540
2541 WebviewController *controller = nullptr;
2542 napi_unwrap(env, thisVar, (void **)&controller);
2543 if (!controller || !controller->IsInit()) {
2544 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2545 return result;
2546 }
2547 ErrCode ret = controller->DeleteJavaScriptRegister(objName, {});
2548 if (ret != NO_ERROR) {
2549 BusinessError::ThrowErrorByErrcode(env, ret);
2550 return result;
2551 }
2552
2553 NAPI_CALL(env, napi_get_undefined(env, &result));
2554 return result;
2555 }
2556
RunJavaScript(napi_env env,napi_callback_info info)2557 napi_value NapiWebviewController::RunJavaScript(napi_env env, napi_callback_info info)
2558 {
2559 return RunJS(env, info, false);
2560 }
2561
RunJavaScriptExt(napi_env env,napi_callback_info info)2562 napi_value NapiWebviewController::RunJavaScriptExt(napi_env env, napi_callback_info info)
2563 {
2564 return RunJS(env, info, true);
2565 }
2566
RunJS(napi_env env,napi_callback_info info,bool extention)2567 napi_value NapiWebviewController::RunJS(napi_env env, napi_callback_info info, bool extention)
2568 {
2569 napi_value thisVar = nullptr;
2570 napi_value result = nullptr;
2571 size_t argc = INTEGER_ONE;
2572 size_t argcPromise = INTEGER_ONE;
2573 size_t argcCallback = INTEGER_TWO;
2574 napi_value argv[INTEGER_TWO] = { 0 };
2575
2576 napi_get_undefined(env, &result);
2577 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2578
2579 if (argc != argcPromise && argc != argcCallback) {
2580 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2581 return result;
2582 }
2583 std::string script;
2584 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], script)) {
2585 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2586 return result;
2587 }
2588
2589 if (argc == argcCallback) {
2590 napi_valuetype valueType = napi_null;
2591 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2592 napi_typeof(env, argv[argcCallback - 1], &valueType);
2593 if (valueType != napi_function) {
2594 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2595 return result;
2596 }
2597 }
2598 return RunJavaScriptInternal(env, info, script, extention);
2599 }
2600
RunJavaScriptInternal(napi_env env,napi_callback_info info,const std::string & script,bool extention)2601 napi_value NapiWebviewController::RunJavaScriptInternal(napi_env env, napi_callback_info info,
2602 const std::string &script, bool extention)
2603 {
2604 napi_value thisVar = nullptr;
2605 size_t argc = INTEGER_ONE;
2606 size_t argcPromise = INTEGER_ONE;
2607 size_t argcCallback = INTEGER_TWO;
2608 napi_value argv[INTEGER_TWO] = {0};
2609
2610 napi_value result = nullptr;
2611 napi_get_undefined(env, &result);
2612 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2613
2614 WebviewController *webviewController = nullptr;
2615 napi_unwrap(env, thisVar, (void **)&webviewController);
2616
2617 if (!webviewController || !webviewController->IsInit()) {
2618 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2619 return result;
2620 }
2621
2622 if (argc == argcCallback) {
2623 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2624 napi_ref jsCallback = nullptr;
2625 napi_create_reference(env, argv[argcCallback - 1], 1, &jsCallback);
2626
2627 if (jsCallback) {
2628 webviewController->RunJavaScriptCallback(script, env, std::move(jsCallback), extention);
2629 }
2630 return result;
2631 } else if (argc == argcPromise) {
2632 napi_deferred deferred = nullptr;
2633 napi_value promise = nullptr;
2634 napi_create_promise(env, &deferred, &promise);
2635 if (promise && deferred) {
2636 webviewController->RunJavaScriptPromise(script, env, deferred, extention);
2637 }
2638 return promise;
2639 }
2640 return result;
2641 }
2642
GetUrl(napi_env env,napi_callback_info info)2643 napi_value NapiWebviewController::GetUrl(napi_env env, napi_callback_info info)
2644 {
2645 napi_value thisVar = nullptr;
2646 napi_value result = nullptr;
2647 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2648
2649 WebviewController *webviewController = nullptr;
2650 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2651 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2652 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2653 return nullptr;
2654 }
2655
2656 std::string url = "";
2657 url = webviewController->GetUrl();
2658 napi_create_string_utf8(env, url.c_str(), url.length(), &result);
2659
2660 return result;
2661 }
2662
GetOriginalUrl(napi_env env,napi_callback_info info)2663 napi_value NapiWebviewController::GetOriginalUrl(napi_env env, napi_callback_info info)
2664 {
2665 napi_value thisVar = nullptr;
2666 napi_value result = nullptr;
2667 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2668
2669 WebviewController *webviewController = nullptr;
2670 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2671 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2672 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2673 return nullptr;
2674 }
2675
2676 std::string url = "";
2677 url = webviewController->GetOriginalUrl();
2678 napi_create_string_utf8(env, url.c_str(), url.length(), &result);
2679 return result;
2680 }
2681
SetNetworkAvailable(napi_env env,napi_callback_info info)2682 napi_value NapiWebviewController::SetNetworkAvailable(napi_env env, napi_callback_info info)
2683 {
2684 napi_value thisVar = nullptr;
2685 napi_value result = nullptr;
2686 size_t argc = INTEGER_ONE;
2687 napi_value argv[INTEGER_ONE] = { 0 };
2688 bool enable;
2689
2690 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2691 if (argc != INTEGER_ONE) {
2692 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2693 return result;
2694 }
2695
2696 if (!NapiParseUtils::ParseBoolean(env, argv[0], enable)) {
2697 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2698 return result;
2699 }
2700
2701 WebviewController *webviewController = nullptr;
2702 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2703 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2704 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2705 return nullptr;
2706 }
2707 webviewController->PutNetworkAvailable(enable);
2708 return result;
2709 }
2710
InnerGetWebId(napi_env env,napi_callback_info info)2711 napi_value NapiWebviewController::InnerGetWebId(napi_env env, napi_callback_info info)
2712 {
2713 napi_value thisVar = nullptr;
2714 napi_value result = nullptr;
2715 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2716
2717 WebviewController *webviewController = nullptr;
2718 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2719 int32_t webId = -1;
2720 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2721 WVLOG_E("Init error. The WebviewController must be associated with a Web component.");
2722 napi_create_int32(env, webId, &result);
2723 return result;
2724 }
2725
2726 webId = webviewController->GetWebId();
2727 napi_create_int32(env, webId, &result);
2728
2729 return result;
2730 }
2731
HasImage(napi_env env,napi_callback_info info)2732 napi_value NapiWebviewController::HasImage(napi_env env, napi_callback_info info)
2733 {
2734 napi_value thisVar = nullptr;
2735 napi_value result = nullptr;
2736 size_t argc = INTEGER_ONE;
2737 size_t argcPromiseParaNum = INTEGER_ZERO;
2738 size_t argcCallbackParaNum = INTEGER_ONE;
2739 napi_value argv[INTEGER_ONE] = { 0 };
2740
2741 napi_get_undefined(env, &result);
2742 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2743
2744 if (argc != argcPromiseParaNum && argc != argcCallbackParaNum) {
2745 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2746 return result;
2747 }
2748
2749 if (argc == argcCallbackParaNum) {
2750 napi_valuetype valueType = napi_null;
2751 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2752 napi_typeof(env, argv[argcCallbackParaNum - 1], &valueType);
2753 if (valueType != napi_function) {
2754 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
2755 return result;
2756 }
2757 }
2758 return HasImageInternal(env, info);
2759 }
2760
HasImageInternal(napi_env env,napi_callback_info info)2761 napi_value NapiWebviewController::HasImageInternal(napi_env env, napi_callback_info info)
2762 {
2763 napi_value thisVar = nullptr;
2764 size_t argc = INTEGER_ONE;
2765 size_t argcPromiseParaNum = INTEGER_ZERO;
2766 size_t argcCallbackParaNum = INTEGER_ONE;
2767 napi_value argv[INTEGER_ONE] = { 0 };
2768
2769 napi_value result = nullptr;
2770 napi_get_undefined(env, &result);
2771 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2772
2773 WebviewController *webviewController = nullptr;
2774 napi_unwrap(env, thisVar, (void **)&webviewController);
2775
2776 if (!webviewController || !webviewController->IsInit()) {
2777 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2778 return result;
2779 }
2780
2781 if (argc == argcCallbackParaNum) {
2782 napi_ref jsCallback = nullptr;
2783 napi_create_reference(env, argv[argcCallbackParaNum - 1], 1, &jsCallback);
2784
2785 if (jsCallback) {
2786 ErrCode ret = webviewController->HasImagesCallback(env, std::move(jsCallback));
2787 if (ret != NO_ERROR) {
2788 if (ret == NWEB_ERROR) {
2789 return nullptr;
2790 }
2791 BusinessError::ThrowErrorByErrcode(env, ret);
2792 return nullptr;
2793 }
2794 }
2795 return result;
2796 } else if (argc == argcPromiseParaNum) {
2797 napi_deferred deferred = nullptr;
2798 napi_value promise = nullptr;
2799 napi_create_promise(env, &deferred, &promise);
2800 if (promise && deferred) {
2801 ErrCode ret = webviewController->HasImagesPromise(env, deferred);
2802 if (ret != NO_ERROR) {
2803 if (ret == NWEB_ERROR) {
2804 return nullptr;
2805 }
2806 BusinessError::ThrowErrorByErrcode(env, ret);
2807 return nullptr;
2808 }
2809 }
2810 return promise;
2811 }
2812 return result;
2813 }
2814
RemoveCache(napi_env env,napi_callback_info info)2815 napi_value NapiWebviewController::RemoveCache(napi_env env, napi_callback_info info)
2816 {
2817 napi_value thisVar = nullptr;
2818 napi_value result = nullptr;
2819 size_t argc = INTEGER_ONE;
2820 napi_value argv[INTEGER_ONE] = { 0 };
2821 bool include_disk_files;
2822
2823 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
2824 if (argc != INTEGER_ONE) {
2825 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2826 return result;
2827 }
2828
2829 if (!NapiParseUtils::ParseBoolean(env, argv[0], include_disk_files)) {
2830 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2831 return result;
2832 }
2833
2834 WebviewController *webviewController = nullptr;
2835 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
2836 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
2837 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2838 return nullptr;
2839 }
2840 webviewController->RemoveCache(include_disk_files);
2841 return result;
2842 }
2843
JsConstructor(napi_env env,napi_callback_info info)2844 napi_value NapiWebHistoryList::JsConstructor(napi_env env, napi_callback_info info)
2845 {
2846 napi_value thisVar = nullptr;
2847 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2848 return thisVar;
2849 }
2850
getColorType(ImageColorType colorType)2851 Media::PixelFormat getColorType(ImageColorType colorType)
2852 {
2853 Media::PixelFormat pixelFormat_;
2854 switch (colorType) {
2855 case ImageColorType::COLOR_TYPE_UNKNOWN:
2856 pixelFormat_ = Media::PixelFormat::UNKNOWN;
2857 break;
2858 case ImageColorType::COLOR_TYPE_RGBA_8888:
2859 pixelFormat_ = Media::PixelFormat::RGBA_8888;
2860 break;
2861 case ImageColorType::COLOR_TYPE_BGRA_8888:
2862 pixelFormat_ = Media::PixelFormat::BGRA_8888;
2863 break;
2864 default:
2865 pixelFormat_ = Media::PixelFormat::UNKNOWN;
2866 break;
2867 }
2868 return pixelFormat_;
2869 }
2870
getAlphaType(ImageAlphaType alphaType)2871 Media::AlphaType getAlphaType(ImageAlphaType alphaType)
2872 {
2873 Media::AlphaType alphaType_;
2874 switch (alphaType) {
2875 case ImageAlphaType::ALPHA_TYPE_UNKNOWN:
2876 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
2877 break;
2878 case ImageAlphaType::ALPHA_TYPE_OPAQUE:
2879 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
2880 break;
2881 case ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED:
2882 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
2883 break;
2884 case ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED:
2885 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
2886 break;
2887 default:
2888 alphaType_ = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
2889 break;
2890 }
2891 return alphaType_;
2892 }
2893
GetFavicon(napi_env env,std::shared_ptr<NWebHistoryItem> item)2894 napi_value NapiWebHistoryList::GetFavicon(napi_env env, std::shared_ptr<NWebHistoryItem> item)
2895 {
2896 napi_value result = nullptr;
2897 void *data = nullptr;
2898 int32_t width = 0;
2899 int32_t height = 0;
2900 ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
2901 ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
2902 bool isGetFavicon = item->GetFavicon(&data, width, height, colorType, alphaType);
2903 napi_get_null(env, &result);
2904
2905 if (!isGetFavicon) {
2906 return result;
2907 }
2908
2909 Media::InitializationOptions opt;
2910 opt.size.width = width;
2911 opt.size.height = height;
2912 opt.pixelFormat = getColorType(colorType);
2913 opt.alphaType = getAlphaType(alphaType);
2914 opt.editable = true;
2915 auto pixelMap = Media::PixelMap::Create(opt);
2916 if (pixelMap == nullptr) {
2917 return result;
2918 }
2919 uint64_t stride = static_cast<uint64_t>(width) << 2;
2920 uint64_t bufferSize = stride * static_cast<uint64_t>(height);
2921 pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
2922 std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release());
2923 napi_value jsPixelMap = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs);
2924 return jsPixelMap;
2925 }
2926
GetItem(napi_env env,napi_callback_info info)2927 napi_value NapiWebHistoryList::GetItem(napi_env env, napi_callback_info info)
2928 {
2929 napi_value thisVar = nullptr;
2930 napi_value result = nullptr;
2931 size_t argc = INTEGER_ONE;
2932 napi_value argv[INTEGER_ONE] = { 0 };
2933 int32_t index;
2934 WebHistoryList *historyList = nullptr;
2935
2936 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
2937 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&historyList));
2938 if (historyList == nullptr) {
2939 WVLOG_E("unwrap historyList failed.");
2940 return result;
2941 }
2942 if (argc != INTEGER_ONE) {
2943 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2944 return result;
2945 }
2946 if (!NapiParseUtils::ParseInt32(env, argv[0], index)) {
2947 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2948 return result;
2949 }
2950 if (index >= historyList->GetListSize() || index < 0) {
2951 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
2952 return result;
2953 }
2954
2955 std::shared_ptr<NWebHistoryItem> item = historyList->GetItem(index);
2956 if (!item) {
2957 return result;
2958 }
2959
2960 napi_create_object(env, &result);
2961 std::string historyUrl = item->GetHistoryUrl();
2962 std::string historyRawUrl = item->GetHistoryRawUrl();
2963 std::string title = item->GetHistoryTitle();
2964
2965 napi_value js_historyUrl;
2966 napi_create_string_utf8(env, historyUrl.c_str(), historyUrl.length(), &js_historyUrl);
2967 napi_set_named_property(env, result, "historyUrl", js_historyUrl);
2968
2969 napi_value js_historyRawUrl;
2970 napi_create_string_utf8(env, historyRawUrl.c_str(), historyRawUrl.length(), &js_historyRawUrl);
2971 napi_set_named_property(env, result, "historyRawUrl", js_historyRawUrl);
2972
2973 napi_value js_title;
2974 napi_create_string_utf8(env, title.c_str(), title.length(), &js_title);
2975 napi_set_named_property(env, result, "title", js_title);
2976
2977 napi_value js_icon = GetFavicon(env, item);
2978 napi_set_named_property(env, result, "icon", js_icon);
2979 return result;
2980 }
2981
getBackForwardEntries(napi_env env,napi_callback_info info)2982 napi_value NapiWebviewController::getBackForwardEntries(napi_env env, napi_callback_info info)
2983 {
2984 napi_value thisVar = nullptr;
2985 napi_value result = nullptr;
2986 WebviewController *webviewController = nullptr;
2987
2988 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
2989 NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&webviewController));
2990 if (webviewController == nullptr || !webviewController->IsInit()) {
2991 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
2992 return nullptr;
2993 }
2994
2995 std::shared_ptr<NWebHistoryList> list = webviewController->GetHistoryList();
2996 if (!list) {
2997 return result;
2998 }
2999
3000 WebHistoryList *webHistoryList = new (std::nothrow) WebHistoryList(list);
3001 if (webHistoryList == nullptr) {
3002 return result;
3003 }
3004
3005 int32_t currentIndex = list->GetCurrentIndex();
3006 int32_t size = list->GetListSize();
3007
3008 napi_value historyList = nullptr;
3009 NAPI_CALL(env, napi_get_reference_value(env, g_historyListRef, &historyList));
3010 NAPI_CALL(env, napi_new_instance(env, historyList, 0, NULL, &result));
3011
3012 napi_value js_currentIndex;
3013 napi_create_int32(env, currentIndex, &js_currentIndex);
3014 napi_set_named_property(env, result, "currentIndex", js_currentIndex);
3015
3016 napi_value js_size;
3017 napi_create_int32(env, size, &js_size);
3018 napi_set_named_property(env, result, "size", js_size);
3019 NAPI_CALL(env, napi_wrap(env, result, webHistoryList,
3020 [](napi_env env, void *data, void *hint) {
3021 WebHistoryList *webHistoryList = static_cast<WebHistoryList *>(data);
3022 delete webHistoryList;
3023 },
3024 nullptr, nullptr));
3025
3026 return result;
3027 }
3028
GetFavicon(napi_env env,napi_callback_info info)3029 napi_value NapiWebviewController::GetFavicon(napi_env env, napi_callback_info info)
3030 {
3031 napi_value thisVar = nullptr;
3032 napi_value result = nullptr;
3033 napi_get_null(env, &result);
3034 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
3035
3036 WebviewController *webviewController = nullptr;
3037 napi_unwrap(env, thisVar, (void **)&webviewController);
3038
3039 if (!webviewController || !webviewController->IsInit()) {
3040 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3041 return result;
3042 }
3043
3044 const void *data = nullptr;
3045 size_t width = 0;
3046 size_t height = 0;
3047 ImageColorType colorType = ImageColorType::COLOR_TYPE_UNKNOWN;
3048 ImageAlphaType alphaType = ImageAlphaType::ALPHA_TYPE_UNKNOWN;
3049 bool isGetFavicon = webviewController->GetFavicon(&data, width, height, colorType, alphaType);
3050 if (!isGetFavicon) {
3051 return result;
3052 }
3053
3054 Media::InitializationOptions opt;
3055 opt.size.width = static_cast<int32_t>(width);
3056 opt.size.height = static_cast<int32_t>(height);
3057 opt.pixelFormat = getColorType(colorType);
3058 opt.alphaType = getAlphaType(alphaType);
3059 opt.editable = true;
3060 auto pixelMap = Media::PixelMap::Create(opt);
3061 if (pixelMap == nullptr) {
3062 return result;
3063 }
3064 uint64_t stride = static_cast<uint64_t>(width) << 2;
3065 uint64_t bufferSize = stride * static_cast<uint64_t>(height);
3066 pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
3067 std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release());
3068 napi_value jsPixelMap = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs);
3069 return jsPixelMap;
3070 }
3071
SerializeWebState(napi_env env,napi_callback_info info)3072 napi_value NapiWebviewController::SerializeWebState(napi_env env, napi_callback_info info)
3073 {
3074 napi_value thisVar = nullptr;
3075 napi_value result = nullptr;
3076 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
3077 napi_get_null(env, &result);
3078
3079 WebviewController *webviewController = nullptr;
3080 napi_unwrap(env, thisVar, (void **)&webviewController);
3081 if (!webviewController || !webviewController->IsInit()) {
3082 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3083 return result;
3084 }
3085
3086 void *data = nullptr;
3087 napi_value buffer = nullptr;
3088 auto webState = webviewController->SerializeWebState();
3089 if (!webState) {
3090 return result;
3091 }
3092
3093 NAPI_CALL(env, napi_create_arraybuffer(env, webState->size(), &data, &buffer));
3094 int retCode = memcpy_s(data, webState->size(), webState->data(), webState->size());
3095 if (retCode != 0) {
3096 return result;
3097 }
3098 NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, webState->size(), buffer, 0, &result));
3099 return result;
3100 }
3101
RestoreWebState(napi_env env,napi_callback_info info)3102 napi_value NapiWebviewController::RestoreWebState(napi_env env, napi_callback_info info)
3103 {
3104 napi_value thisVar = nullptr;
3105 napi_value result = nullptr;
3106 size_t argc = INTEGER_ONE;
3107 napi_value argv[INTEGER_ONE] = { 0 };
3108 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
3109 napi_get_null(env, &result);
3110
3111 WebviewController *webviewController = nullptr;
3112 napi_unwrap(env, thisVar, (void **)&webviewController);
3113 if (!webviewController || !webviewController->IsInit()) {
3114 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3115 return result;
3116 }
3117
3118 bool isTypedArray = false;
3119 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3120 if (argc != INTEGER_ONE) {
3121 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3122 return result;
3123 }
3124 NAPI_CALL(env, napi_is_typedarray(env, argv[0], &isTypedArray));
3125 if (!isTypedArray) {
3126 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3127 return result;
3128 }
3129
3130 napi_typedarray_type type;
3131 size_t length = 0;
3132 napi_value buffer = nullptr;
3133 size_t offset = 0;
3134 NAPI_CALL(env, napi_get_typedarray_info(env, argv[0], &type, &length, nullptr, &buffer, &offset));
3135 if (type != napi_uint8_array) {
3136 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3137 return result;
3138 }
3139 uint8_t *data = nullptr;
3140 size_t total = 0;
3141 NAPI_CALL(env, napi_get_arraybuffer_info(env, buffer, reinterpret_cast<void **>(&data), &total));
3142 length = std::min<size_t>(length, total - offset);
3143 std::vector<uint8_t> state(length);
3144 int retCode = memcpy_s(state.data(), state.size(), &data[offset], length);
3145 if (retCode != 0) {
3146 return result;
3147 }
3148 webviewController->RestoreWebState(std::make_shared<std::vector<uint8_t>>(state));
3149 return result;
3150 }
3151
ScrollPageDown(napi_env env,napi_callback_info info)3152 napi_value NapiWebviewController::ScrollPageDown(napi_env env, napi_callback_info info)
3153 {
3154 napi_value thisVar = nullptr;
3155 napi_value result = nullptr;
3156 size_t argc = INTEGER_ONE;
3157 napi_value argv[INTEGER_ONE] = { 0 };
3158 bool bottom;
3159
3160 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3161 if (argc != INTEGER_ONE) {
3162 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3163 return result;
3164 }
3165
3166 if (!NapiParseUtils::ParseBoolean(env, argv[0], bottom)) {
3167 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3168 return result;
3169 }
3170
3171 WebviewController *webviewController = nullptr;
3172 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
3173 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3174 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3175 return nullptr;
3176 }
3177 webviewController->ScrollPageDown(bottom);
3178 return result;
3179 }
3180
ScrollPageUp(napi_env env,napi_callback_info info)3181 napi_value NapiWebviewController::ScrollPageUp(napi_env env, napi_callback_info info)
3182 {
3183 napi_value thisVar = nullptr;
3184 napi_value result = nullptr;
3185 size_t argc = INTEGER_ONE;
3186 napi_value argv[INTEGER_ONE] = { 0 };
3187 bool top;
3188
3189 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3190 if (argc != INTEGER_ONE) {
3191 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3192 return result;
3193 }
3194
3195 if (!NapiParseUtils::ParseBoolean(env, argv[0], top)) {
3196 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3197 return result;
3198 }
3199
3200 WebviewController *webviewController = nullptr;
3201 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
3202 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3203 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3204 return nullptr;
3205 }
3206 webviewController->ScrollPageUp(top);
3207 return result;
3208 }
3209
CheckSchemeName(const std::string & schemeName)3210 bool CheckSchemeName(const std::string& schemeName)
3211 {
3212 if (schemeName.empty() || schemeName.size() > MAX_CUSTOM_SCHEME_NAME_LENGTH) {
3213 WVLOG_E("Invalid scheme name length");
3214 return false;
3215 }
3216 for (auto it = schemeName.begin(); it != schemeName.end(); it++) {
3217 char chr = *it;
3218 if (!((chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') ||
3219 (chr == '.') || (chr == '+') || (chr == '-'))) {
3220 WVLOG_E("invalid character %{public}c", chr);
3221 return false;
3222 }
3223 }
3224 return true;
3225 }
3226
CustomizeSchemes(napi_env env,napi_callback_info info)3227 napi_value NapiWebviewController::CustomizeSchemes(napi_env env, napi_callback_info info)
3228 {
3229 if (WebviewController::existNweb_) {
3230 WVLOG_E("There exist web component which has been already created.");
3231 }
3232
3233 napi_value result = nullptr;
3234 napi_value thisVar = nullptr;
3235 size_t argc = INTEGER_ONE;
3236 napi_value argv[INTEGER_ONE];
3237 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3238 if (argc != INTEGER_ONE) {
3239 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3240 return nullptr;
3241 }
3242 napi_value array = argv[INTEGER_ZERO];
3243 bool isArray = false;
3244 napi_is_array(env, array, &isArray);
3245 if (isArray) {
3246 std::string cmdLine;
3247 uint32_t arrayLength = INTEGER_ZERO;
3248 napi_get_array_length(env, array, &arrayLength);
3249 if (arrayLength > MAX_CUSTOM_SCHEME_SIZE) {
3250 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3251 return nullptr;
3252 }
3253 for (uint32_t i = 0; i < arrayLength; ++i) {
3254 std::string schemeName;
3255 bool isSupportCORS;
3256 bool isSupportFetch;
3257 napi_value schemeNameObj = nullptr;
3258 napi_value isSupportCORSObj = nullptr;
3259 napi_value isSupportFetchObj = nullptr;
3260 napi_value obj = nullptr;
3261 napi_get_element(env, array, i, &obj);
3262 if ((napi_get_named_property(env, obj, "schemeName", &schemeNameObj) != napi_ok) ||
3263 (napi_get_named_property(env, obj, "isSupportCORS", &isSupportCORSObj) != napi_ok) ||
3264 (napi_get_named_property(env, obj, "isSupportFetch", &isSupportFetchObj) != napi_ok)) {
3265 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3266 return nullptr;
3267 }
3268 NapiParseUtils::ParseString(env, schemeNameObj, schemeName);
3269 if (!CheckSchemeName(schemeName)) {
3270 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3271 return nullptr;
3272 }
3273 NapiParseUtils::ParseBoolean(env, isSupportCORSObj, isSupportCORS);
3274 NapiParseUtils::ParseBoolean(env, isSupportFetchObj, isSupportFetch);
3275 std::string corsCmdLine = isSupportCORS ? "1," : "0,";
3276 std::string fetchCmdLine = isSupportFetch ? "1;" : "0;";
3277 cmdLine.append(schemeName + "," + corsCmdLine + fetchCmdLine);
3278 }
3279 cmdLine.pop_back();
3280 WVLOG_I("Reg scheme cmdline %{public}s", cmdLine.c_str());
3281 WebviewController::customeSchemeCmdLine_ = cmdLine;
3282 } else {
3283 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3284 return nullptr;
3285 }
3286 NAPI_CALL(env, napi_get_undefined(env, &result));
3287 return result;
3288 }
3289
ScrollTo(napi_env env,napi_callback_info info)3290 napi_value NapiWebviewController::ScrollTo(napi_env env, napi_callback_info info)
3291 {
3292 napi_value thisVar = nullptr;
3293 napi_value result = nullptr;
3294 size_t argc = INTEGER_TWO;
3295 napi_value argv[INTEGER_TWO] = { 0 };
3296 float x;
3297 float y;
3298
3299 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3300 if (argc != INTEGER_TWO) {
3301 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3302 return result;
3303 }
3304
3305 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], x)) {
3306 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3307 return result;
3308 }
3309
3310 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], y)) {
3311 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3312 return result;
3313 }
3314
3315 WebviewController *webviewController = nullptr;
3316 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
3317 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3318 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3319 return nullptr;
3320 }
3321 webviewController->ScrollTo(x, y);
3322 return result;
3323 }
3324
ScrollBy(napi_env env,napi_callback_info info)3325 napi_value NapiWebviewController::ScrollBy(napi_env env, napi_callback_info info)
3326 {
3327 napi_value thisVar = nullptr;
3328 napi_value result = nullptr;
3329 size_t argc = INTEGER_TWO;
3330 napi_value argv[INTEGER_TWO] = { 0 };
3331 float deltaX;
3332 float deltaY;
3333
3334 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3335 if (argc != INTEGER_TWO) {
3336 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3337 return result;
3338 }
3339
3340 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], deltaX)) {
3341 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3342 return result;
3343 }
3344
3345 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], deltaY)) {
3346 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3347 return result;
3348 }
3349
3350 WebviewController *webviewController = nullptr;
3351 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
3352 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3353 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3354 return nullptr;
3355 }
3356 webviewController->ScrollBy(deltaX, deltaY);
3357 return result;
3358 }
3359
SlideScroll(napi_env env,napi_callback_info info)3360 napi_value NapiWebviewController::SlideScroll(napi_env env, napi_callback_info info)
3361 {
3362 napi_value thisVar = nullptr;
3363 napi_value result = nullptr;
3364 size_t argc = INTEGER_TWO;
3365 napi_value argv[INTEGER_TWO] = { 0 };
3366 float vx;
3367 float vy;
3368
3369 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3370 if (argc != INTEGER_TWO) {
3371 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3372 return result;
3373 }
3374
3375 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ZERO], vx)) {
3376 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3377 return result;
3378 }
3379
3380 if (!NapiParseUtils::ParseFloat(env, argv[INTEGER_ONE], vy)) {
3381 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3382 return result;
3383 }
3384
3385 WebviewController *webviewController = nullptr;
3386 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
3387 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3388 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3389 return nullptr;
3390 }
3391 webviewController->SlideScroll(vx, vy);
3392 return result;
3393 }
3394
InnerGetCertificate(napi_env env,napi_callback_info info)3395 napi_value NapiWebviewController::InnerGetCertificate(napi_env env, napi_callback_info info)
3396 {
3397 napi_value thisVar = nullptr;
3398 napi_value result = nullptr;
3399 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
3400 napi_create_array(env, &result);
3401
3402 WebviewController *webviewController = nullptr;
3403 napi_unwrap(env, thisVar, (void **)&webviewController);
3404 if (!webviewController || !webviewController->IsInit()) {
3405 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3406 return result;
3407 }
3408
3409 std::vector<std::string> certChainDerData;
3410 bool ans = webviewController->GetCertChainDerData(certChainDerData);
3411 if (!ans) {
3412 WVLOG_E("get cert chain data failed");
3413 return result;
3414 }
3415
3416 for (uint8_t i = 0; i < certChainDerData.size(); i++) {
3417 if (i == UINT8_MAX) {
3418 WVLOG_E("error, cert chain data array reach max");
3419 break;
3420 }
3421 void *data = nullptr;
3422 napi_value buffer = nullptr;
3423 napi_value item = nullptr;
3424 NAPI_CALL(env, napi_create_arraybuffer(env, certChainDerData[i].size(), &data, &buffer));
3425 int retCode = memcpy_s(data, certChainDerData[i].size(),
3426 certChainDerData[i].data(), certChainDerData[i].size());
3427 if (retCode != 0) {
3428 WVLOG_E("memcpy_s cert data failed, index = %{public}u,", i);
3429 continue;
3430 }
3431 NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, certChainDerData[i].size(), buffer, 0, &item));
3432 NAPI_CALL(env, napi_set_element(env, result, i, item));
3433 }
3434 return result;
3435 }
3436
SetAudioMuted(napi_env env,napi_callback_info info)3437 napi_value NapiWebviewController::SetAudioMuted(napi_env env, napi_callback_info info)
3438 {
3439 WVLOG_D("SetAudioMuted invoked");
3440
3441 napi_value result = nullptr;
3442 NAPI_CALL(env, napi_get_undefined(env, &result));
3443
3444 napi_value thisVar = nullptr;
3445 size_t argc = INTEGER_ONE;
3446 napi_value argv[INTEGER_ONE] = { 0 };
3447 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3448 if (argc != INTEGER_ONE) {
3449 WVLOG_E("SetAudioMuted failed due to wrong param quantity: %{public}zu", argc);
3450 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3451 return result;
3452 }
3453
3454 bool muted = false;
3455 if (!NapiParseUtils::ParseBoolean(env, argv[0], muted)) {
3456 WVLOG_E("SetAudioMuted failed due to invalid argument value");
3457 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3458 return result;
3459 }
3460
3461 WebviewController* webviewController = nullptr;
3462 napi_status status = napi_unwrap(env, thisVar, (void**)&webviewController);
3463 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3464 WVLOG_E("SetAudioMuted failed due to no associated Web component");
3465 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3466 return result;
3467 }
3468
3469 ErrCode ret = webviewController->SetAudioMuted(muted);
3470 if (ret != NO_ERROR) {
3471 WVLOG_E("SetAudioMuted failed, error code: %{public}d", ret);
3472 BusinessError::ThrowErrorByErrcode(env, ret);
3473 return result;
3474 }
3475
3476 WVLOG_I("SetAudioMuted: %{public}s", (muted ? "true" : "false"));
3477 return result;
3478 }
3479
PrefetchPage(napi_env env,napi_callback_info info)3480 napi_value NapiWebviewController::PrefetchPage(napi_env env, napi_callback_info info)
3481 {
3482 napi_value thisVar = nullptr;
3483 napi_value result = nullptr;
3484 size_t argc = INTEGER_TWO;
3485 napi_value argv[INTEGER_TWO];
3486 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3487 WebviewController *webviewController = nullptr;
3488 napi_status status = napi_unwrap(env, thisVar, (void **)&webviewController);
3489 if ((argc != INTEGER_ONE) && (argc != INTEGER_TWO)) {
3490 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3491 return nullptr;
3492 }
3493 if ((!webviewController) || (status != napi_ok) || !webviewController->IsInit()) {
3494 BusinessError::ThrowErrorByErrcode(env, INIT_ERROR);
3495 return nullptr;
3496 }
3497 std::string url;
3498 if (!ParsePrepareUrl(env, argv[INTEGER_ZERO], url)) {
3499 BusinessError::ThrowErrorByErrcode(env, INVALID_URL);
3500 return nullptr;
3501 }
3502 std::map<std::string, std::string> additionalHttpHeaders;
3503 if (argc == INTEGER_ONE) {
3504 ErrCode ret = webviewController->PrefetchPage(url, additionalHttpHeaders);
3505 if (ret != NO_ERROR) {
3506 WVLOG_E("PrefetchPage failed, error code: %{public}d", ret);
3507 BusinessError::ThrowErrorByErrcode(env, ret);
3508 return nullptr;
3509 }
3510 NAPI_CALL(env, napi_get_undefined(env, &result));
3511 return result;
3512 }
3513 return PrefetchPageWithHttpHeaders(env, info, url, argv, webviewController);
3514 }
3515
PrefetchPageWithHttpHeaders(napi_env env,napi_callback_info info,std::string & url,const napi_value * argv,WebviewController * webviewController)3516 napi_value NapiWebviewController::PrefetchPageWithHttpHeaders(napi_env env, napi_callback_info info, std::string& url,
3517 const napi_value* argv, WebviewController* webviewController)
3518 {
3519 napi_value result = nullptr;
3520 std::map<std::string, std::string> additionalHttpHeaders;
3521 napi_value array = argv[INTEGER_ONE];
3522 bool isArray = false;
3523 napi_is_array(env, array, &isArray);
3524 if (isArray) {
3525 uint32_t arrayLength = INTEGER_ZERO;
3526 napi_get_array_length(env, array, &arrayLength);
3527 for (uint32_t i = 0; i < arrayLength; ++i) {
3528 std::string key;
3529 std::string value;
3530 napi_value obj = nullptr;
3531 napi_value keyObj = nullptr;
3532 napi_value valueObj = nullptr;
3533 napi_get_element(env, array, i, &obj);
3534 if (napi_get_named_property(env, obj, "headerKey", &keyObj) != napi_ok) {
3535 continue;
3536 }
3537 if (napi_get_named_property(env, obj, "headerValue", &valueObj) != napi_ok) {
3538 continue;
3539 }
3540 NapiParseUtils::ParseString(env, keyObj, key);
3541 NapiParseUtils::ParseString(env, valueObj, value);
3542 additionalHttpHeaders[key] = value;
3543 }
3544 } else {
3545 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3546 return nullptr;
3547 }
3548
3549 ErrCode ret = webviewController->PrefetchPage(url, additionalHttpHeaders);
3550 if (ret != NO_ERROR) {
3551 WVLOG_E("PrefetchPage failed, error code: %{public}d", ret);
3552 BusinessError::ThrowErrorByErrcode(env, ret);
3553 return nullptr;
3554 }
3555 NAPI_CALL(env, napi_get_undefined(env, &result));
3556 return result;
3557 }
3558
PrepareForPageLoad(napi_env env,napi_callback_info info)3559 napi_value NapiWebviewController::PrepareForPageLoad(napi_env env, napi_callback_info info)
3560 {
3561 napi_value thisVar = nullptr;
3562 napi_value result = nullptr;
3563 size_t argc = INTEGER_THREE;
3564 napi_value argv[INTEGER_THREE] = { 0 };
3565 napi_get_undefined(env, &result);
3566 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
3567 if (argc != INTEGER_THREE) {
3568 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3569 return nullptr;
3570 }
3571
3572 std::string url;
3573 if (!ParsePrepareUrl(env, argv[INTEGER_ZERO], url)) {
3574 BusinessError::ThrowErrorByErrcode(env, INVALID_URL);
3575 return nullptr;
3576 }
3577
3578 bool preconnectable = false;
3579 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_ONE], preconnectable)) {
3580 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3581 return nullptr;
3582 }
3583
3584 int32_t numSockets = 0;
3585 if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_TWO], numSockets)) {
3586 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
3587 return nullptr;
3588 }
3589 if (numSockets <= 0 || static_cast<uint32_t>(numSockets) > SOCKET_MAXIMUM) {
3590 BusinessError::ThrowErrorByErrcode(env, INVALID_SOCKET_NUMBER);
3591 return nullptr;
3592 }
3593
3594 NWebHelper::Instance().PrepareForPageLoad(url, preconnectable, numSockets);
3595 NAPI_CALL(env, napi_get_undefined(env, &result));
3596 return result;
3597 }
3598 } // namespace NWeb
3599 } // namespace OHOS
3600