1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_web_scheme_handler_request.h"
17
18 #include <js_native_api.h>
19 #include <js_native_api_types.h>
20 #include <napi/native_api.h>
21 #include <securec.h>
22 #include <cstring>
23
24 #include "business_error.h"
25 #include "web_scheme_handler_request.h"
26 #include "nweb_log.h"
27 #include "napi_parse_utils.h"
28 #include "web_errors.h"
29
30 using namespace OHOS::NWebError;
31
32 namespace OHOS {
33 namespace NWeb {
Init(napi_env env,napi_value exports)34 napi_value NapiWebSchemeHandlerRequest::Init(napi_env env, napi_value exports)
35 {
36 WVLOG_D("NapiWebSchemeHandlerRequest::Init");
37 ExportWebSchemeHandlerRequestClass(env, &exports);
38 ExportEnumWebResourceType(env, &exports);
39 return exports;
40 }
41
ExportWebSchemeHandlerRequestClass(napi_env env,napi_value * exportsPointer)42 void NapiWebSchemeHandlerRequest::ExportWebSchemeHandlerRequestClass(
43 napi_env env, napi_value* exportsPointer)
44 {
45 napi_property_descriptor properties[] = {
46 DECLARE_NAPI_FUNCTION("getHeader", JS_GetHeader),
47 DECLARE_NAPI_FUNCTION("getRequestUrl", JS_GetRequestUrl),
48 DECLARE_NAPI_FUNCTION("getRequestMethod", JS_GetRequestMethod),
49 DECLARE_NAPI_FUNCTION("getReferrer", JS_GetReferrer),
50 DECLARE_NAPI_FUNCTION("isRedirect", JS_IsRedirect),
51 DECLARE_NAPI_FUNCTION("isMainFrame", JS_IsMainFrame),
52 DECLARE_NAPI_FUNCTION("hasGesture", JS_HasGesture),
53 DECLARE_NAPI_FUNCTION("getHttpBodyStream", JS_HttpBodyStream),
54 DECLARE_NAPI_FUNCTION("getRequestResourceType", JS_GetRequestResourceType),
55 DECLARE_NAPI_FUNCTION("getFrameUrl", JS_GetFrameUrl),
56 };
57 napi_value webSchemeHandlerRequest = nullptr;
58 napi_define_class(env, WEB_SCHEME_HANDLER_REQUEST.c_str(), WEB_SCHEME_HANDLER_REQUEST.length(),
59 JS_Constructor, nullptr,
60 sizeof(properties) / sizeof(properties[0]), properties, &webSchemeHandlerRequest);
61 napi_set_named_property(env, *exportsPointer, WEB_SCHEME_HANDLER_REQUEST.c_str(),
62 webSchemeHandlerRequest);
63 }
64
DefineProperties(napi_env env,napi_value * object)65 napi_status NapiWebSchemeHandlerRequest::DefineProperties(
66 napi_env env, napi_value* object)
67 {
68 napi_property_descriptor properties[] = {
69 DECLARE_NAPI_FUNCTION("getHeader", JS_GetHeader),
70 DECLARE_NAPI_FUNCTION("getRequestUrl", JS_GetRequestUrl),
71 DECLARE_NAPI_FUNCTION("getRequestMethod", JS_GetRequestMethod),
72 DECLARE_NAPI_FUNCTION("getReferrer", JS_GetReferrer),
73 DECLARE_NAPI_FUNCTION("isRedirect", JS_IsRedirect),
74 DECLARE_NAPI_FUNCTION("isMainFrame", JS_IsMainFrame),
75 DECLARE_NAPI_FUNCTION("hasGesture", JS_HasGesture),
76 DECLARE_NAPI_FUNCTION("getHttpBodyStream", JS_HttpBodyStream),
77 DECLARE_NAPI_FUNCTION("getRequestResourceType", JS_GetRequestResourceType),
78 DECLARE_NAPI_FUNCTION("getFrameUrl", JS_GetFrameUrl),
79 };
80 return napi_define_properties(env, *object, sizeof(properties) / sizeof(properties[0]), properties);
81 }
82
JS_Constructor(napi_env env,napi_callback_info cbinfo)83 napi_value NapiWebSchemeHandlerRequest::JS_Constructor(napi_env env, napi_callback_info cbinfo)
84 {
85 WVLOG_D("NapiWebSchemeHandlerRequest::JS_Constructor is called");
86 napi_value thisVar = nullptr;
87 void *data = nullptr;
88 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
89
90 WebSchemeHandlerRequest *request = new (std::nothrow) WebSchemeHandlerRequest(env);
91 if (request == nullptr) {
92 return nullptr;
93 }
94
95 napi_wrap(
96 env, thisVar, request,
97 [](napi_env /* env */, void *data, void * /* hint */) {
98 WebSchemeHandlerRequest *request = reinterpret_cast<WebSchemeHandlerRequest *>(data);
99 delete request;
100 },
101 nullptr, nullptr);
102
103 return thisVar;
104 }
105
JS_GetHeader(napi_env env,napi_callback_info cbinfo)106 napi_value NapiWebSchemeHandlerRequest::JS_GetHeader(napi_env env, napi_callback_info cbinfo)
107 {
108 napi_value thisVar = nullptr;
109 void *data = nullptr;
110 WebSchemeHandlerRequest *request = nullptr;
111 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
112
113 napi_unwrap(env, thisVar, (void **)&request);
114 if (!request) {
115 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetHeader request is nullptr");
116 return nullptr;
117 }
118
119 WebHeaderList list = request->GetHeader();
120 napi_value result = nullptr;
121 napi_create_array(env, &result);
122 size_t headerSize = list.size();
123 for (size_t index = 0; index < headerSize; index++) {
124 napi_handle_scope scope;
125 napi_status status = napi_open_handle_scope(env, &scope);
126 if (status != napi_ok) {
127 break;
128 }
129 napi_value webHeaderObj = nullptr;
130 napi_value headerKey = nullptr;
131 napi_value headerValue = nullptr;
132 NAPI_CALL(env, napi_create_object(env, &webHeaderObj));
133 napi_create_string_utf8(env, list[index].first.c_str(), NAPI_AUTO_LENGTH, &headerKey);
134 napi_create_string_utf8(env, list[index].second.c_str(), NAPI_AUTO_LENGTH, &headerValue);
135 napi_set_named_property(env, webHeaderObj, "headerKey", headerKey);
136 napi_set_named_property(env, webHeaderObj, "headerValue", headerValue);
137 napi_set_element(env, result, index, webHeaderObj);
138 status = napi_close_handle_scope(env, scope);
139 if (status != napi_ok) {
140 break;
141 }
142 }
143 return result;
144 }
145
JS_GetRequestUrl(napi_env env,napi_callback_info cbinfo)146 napi_value NapiWebSchemeHandlerRequest::JS_GetRequestUrl(napi_env env, napi_callback_info cbinfo)
147 {
148 napi_value thisVar = nullptr;
149 void *data = nullptr;
150 WebSchemeHandlerRequest *request = nullptr;
151 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
152
153 napi_unwrap(env, thisVar, (void **)&request);
154 if (!request) {
155 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetRequestUrl request is nullptr");
156 return nullptr;
157 }
158
159 napi_value value;
160 char *result = request->GetRequestUrl();
161 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &value);
162 if (status != napi_ok) {
163 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetRequestUrl response get url failed");
164 return nullptr;
165 }
166 return value;
167 }
168
JS_GetRequestMethod(napi_env env,napi_callback_info cbinfo)169 napi_value NapiWebSchemeHandlerRequest::JS_GetRequestMethod(napi_env env, napi_callback_info cbinfo)
170 {
171 napi_value thisVar = nullptr;
172 void *data = nullptr;
173 WebSchemeHandlerRequest *request = nullptr;
174 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
175
176 napi_unwrap(env, thisVar, (void **)&request);
177 if (!request) {
178 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetRequestMethod request is nullptr");
179 return nullptr;
180 }
181
182 napi_value value;
183 char *result = request->GetMethod();
184 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &value);
185 if (status != napi_ok) {
186 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetRequestMethod response get url failed");
187 return nullptr;
188 }
189 return value;
190 }
191
JS_GetReferrer(napi_env env,napi_callback_info cbinfo)192 napi_value NapiWebSchemeHandlerRequest::JS_GetReferrer(napi_env env, napi_callback_info cbinfo)
193 {
194 napi_value thisVar = nullptr;
195 void *data = nullptr;
196 WebSchemeHandlerRequest *request = nullptr;
197 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
198
199 napi_unwrap(env, thisVar, (void **)&request);
200 if (!request) {
201 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetReferrer request is nullptr");
202 return nullptr;
203 }
204
205 napi_value value;
206 char *result = request->GetReferrer();
207 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &value);
208 if (status != napi_ok) {
209 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetReferrer response get url failed");
210 return nullptr;
211 }
212 return value;
213 }
214
JS_IsRedirect(napi_env env,napi_callback_info cbinfo)215 napi_value NapiWebSchemeHandlerRequest::JS_IsRedirect(napi_env env, napi_callback_info cbinfo)
216 {
217 napi_value thisVar = nullptr;
218 void *data = nullptr;
219 WebSchemeHandlerRequest *request = nullptr;
220 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
221
222 napi_unwrap(env, thisVar, (void **)&request);
223 if (!request) {
224 WVLOG_E("NapiWebSchemeHandlerRequest::JS_IsRedirect request is nullptr");
225 return nullptr;
226 }
227
228 napi_value value;
229 bool result = request->IsRedirect();
230 NAPI_CALL(env, napi_get_boolean(env, result, &value));
231 return value;
232 }
233
JS_IsMainFrame(napi_env env,napi_callback_info cbinfo)234 napi_value NapiWebSchemeHandlerRequest::JS_IsMainFrame(napi_env env, napi_callback_info cbinfo)
235 {
236 napi_value thisVar = nullptr;
237 void *data = nullptr;
238 WebSchemeHandlerRequest *request = nullptr;
239 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
240
241 napi_unwrap(env, thisVar, (void **)&request);
242 if (!request) {
243 WVLOG_E("NapiWebSchemeHandlerRequest::JS_IsMainFrame request is nullptr");
244 return nullptr;
245 }
246
247 napi_value value;
248 bool result = request->IsMainFrame();
249 NAPI_CALL(env, napi_get_boolean(env, result, &value));
250 return value;
251 }
252
JS_HasGesture(napi_env env,napi_callback_info cbinfo)253 napi_value NapiWebSchemeHandlerRequest::JS_HasGesture(napi_env env, napi_callback_info cbinfo)
254 {
255 napi_value thisVar = nullptr;
256 void *data = nullptr;
257 WebSchemeHandlerRequest *request = nullptr;
258 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
259
260 napi_unwrap(env, thisVar, (void **)&request);
261 if (!request) {
262 WVLOG_E("NapiWebSchemeHandlerRequest::JS_HasGesture request is nullptr");
263 return nullptr;
264 }
265
266 napi_value value;
267 bool result = request->HasGesture();
268 NAPI_CALL(env, napi_get_boolean(env, result, &value));
269 return value;
270 }
271
JS_HttpBodyStream(napi_env env,napi_callback_info cbinfo)272 napi_value NapiWebSchemeHandlerRequest::JS_HttpBodyStream(napi_env env, napi_callback_info cbinfo)
273 {
274 napi_value thisVar = nullptr;
275 void *data = nullptr;
276 WebSchemeHandlerRequest *request = nullptr;
277 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
278
279 napi_unwrap(env, thisVar, (void **)&request);
280 if (!request) {
281 WVLOG_E("NapiWebSchemeHandlerRequest::JS_HttpBodyStream request is nullptr");
282 return nullptr;
283 }
284
285 ArkWeb_HttpBodyStream* arkWebPostStream = request->GetHttpBodyStream();
286 if (!arkWebPostStream) {
287 WVLOG_D("NapiWebSchemeHandlerRequest::JS_HttpBodyStream stream is nullptr");
288 return nullptr;
289 }
290 napi_value httpBodyStreamObject;
291 WebHttpBodyStream* stream = new (std::nothrow) WebHttpBodyStream(env, arkWebPostStream);
292 if (stream == nullptr) {
293 return nullptr;
294 }
295 NAPI_CALL(env, napi_create_object(env, &httpBodyStreamObject));
296 napi_wrap(
297 env, httpBodyStreamObject, stream,
298 [](napi_env /* env */, void *data, void * /* hint */) {
299 WebHttpBodyStream *stream = reinterpret_cast<WebHttpBodyStream *>(data);
300 delete stream;
301 },
302 nullptr, nullptr);
303 NapiWebHttpBodyStream::DefineProperties(env, &httpBodyStreamObject);
304 return httpBodyStreamObject;
305 }
306
JS_GetRequestResourceType(napi_env env,napi_callback_info cbinfo)307 napi_value NapiWebSchemeHandlerRequest::JS_GetRequestResourceType(napi_env env, napi_callback_info cbinfo)
308 {
309 napi_value thisVar = nullptr;
310 void *data = nullptr;
311 WebSchemeHandlerRequest *request = nullptr;
312 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
313
314 napi_unwrap(env, thisVar, (void **)&request);
315 if (!request) {
316 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetRequestResourceType request is nullptr");
317 return nullptr;
318 }
319
320 napi_value value;
321 napi_create_int32(env, request->GetRequestResourceType(), &value);
322 return value;
323 }
324
JS_GetFrameUrl(napi_env env,napi_callback_info cbinfo)325 napi_value NapiWebSchemeHandlerRequest::JS_GetFrameUrl(napi_env env, napi_callback_info cbinfo)
326 {
327 napi_value thisVar = nullptr;
328 void *data = nullptr;
329 WebSchemeHandlerRequest *request = nullptr;
330 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
331
332 napi_unwrap(env, thisVar, (void **)&request);
333 if (!request) {
334 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetFrameUrl request is nullptr");
335 return nullptr;
336 }
337
338 napi_value value;
339 char *result = request->GetFrameUrl();
340 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &value);
341 if (status != napi_ok) {
342 WVLOG_E("NapiWebSchemeHandlerRequest::JS_GetFrameUrl response get frame url failed");
343 return nullptr;
344 }
345 return value;
346 }
347
ExportEnumWebResourceType(napi_env env,napi_value * value)348 napi_status NapiWebSchemeHandlerRequest::ExportEnumWebResourceType(napi_env env, napi_value* value)
349 {
350 WVLOG_D("begin to export enum web resource type");
351
352 const std::string NPI_WEB_RESOURCE_TYPE_ENUM_NAME = "WebResourceType";
353 napi_property_descriptor properties[] = {
354 DECLARE_NAPI_STATIC_PROPERTY(
355 "MAIN_FRAME", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::MAIN_FRAME))),
356 DECLARE_NAPI_STATIC_PROPERTY(
357 "SUB_FRAME", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::SUB_FRAME))),
358 DECLARE_NAPI_STATIC_PROPERTY(
359 "STYLE_SHEET", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::STYLE_SHEET))),
360 DECLARE_NAPI_STATIC_PROPERTY(
361 "SCRIPT", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::SCRIPT))),
362 DECLARE_NAPI_STATIC_PROPERTY(
363 "IMAGE", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::IMAGE))),
364 DECLARE_NAPI_STATIC_PROPERTY(
365 "FONT_RESOURCE", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::FONT_RESOURCE))),
366 DECLARE_NAPI_STATIC_PROPERTY(
367 "SUB_RESOURCE", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::SUB_RESOURCE))),
368 DECLARE_NAPI_STATIC_PROPERTY(
369 "OBJECT", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::OBJECT))),
370 DECLARE_NAPI_STATIC_PROPERTY(
371 "MEDIA", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::MEDIA))),
372 DECLARE_NAPI_STATIC_PROPERTY(
373 "WORKER", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::WORKER))),
374 DECLARE_NAPI_STATIC_PROPERTY(
375 "SHARED_WORKER", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::SHARED_WORKER))),
376 DECLARE_NAPI_STATIC_PROPERTY(
377 "PREFETCH", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::PREFETCH))),
378 DECLARE_NAPI_STATIC_PROPERTY(
379 "FAVICON", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::FAVICON))),
380 DECLARE_NAPI_STATIC_PROPERTY(
381 "XHR", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::XHR))),
382 DECLARE_NAPI_STATIC_PROPERTY(
383 "PING", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::PING))),
384 DECLARE_NAPI_STATIC_PROPERTY(
385 "SERVICE_WORKER", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::SERVICE_WORKER))),
386 DECLARE_NAPI_STATIC_PROPERTY(
387 "CSP_REPORT", NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::CSP_REPORT))),
388 DECLARE_NAPI_STATIC_PROPERTY("PLUGIN_RESOURCE",
389 NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::PLUGIN_RESOURCE))),
390 DECLARE_NAPI_STATIC_PROPERTY("NAVIGATION_PRELOAD_MAIN_FRAME",
391 NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::NAVIGATION_PRELOAD_MAIN_FRAME))),
392 DECLARE_NAPI_STATIC_PROPERTY("NAVIGATION_PRELOAD_SUB_FRAME",
393 NapiParseUtils::ToInt32Value(env, static_cast<int32_t>(WebResourceType::NAVIGATION_PRELOAD_SUB_FRAME))),
394 };
395
396 napi_value enumValue = nullptr;
397 napi_define_class(env, NPI_WEB_RESOURCE_TYPE_ENUM_NAME.c_str(), NPI_WEB_RESOURCE_TYPE_ENUM_NAME.length(),
398 NapiParseUtils::CreateEnumConstructor, nullptr, sizeof(properties) / sizeof(properties[0]), properties,
399 &enumValue);
400 return napi_set_named_property(env, *value, NPI_WEB_RESOURCE_TYPE_ENUM_NAME.c_str(), enumValue);
401 }
402
Init(napi_env env,napi_value exports)403 napi_value NapiWebSchemeHandlerResponse::Init(napi_env env, napi_value exports)
404 {
405 ExportWebSchemeHandlerResponseClass(env, &exports);
406 return exports;
407 }
408
ExportWebSchemeHandlerResponseClass(napi_env env,napi_value * exportsPointer)409 void NapiWebSchemeHandlerResponse::ExportWebSchemeHandlerResponseClass(
410 napi_env env, napi_value* exportsPointer)
411 {
412 napi_property_descriptor properties[] = {
413 DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
414 DECLARE_NAPI_FUNCTION("setUrl", JS_SetUrl),
415 DECLARE_NAPI_FUNCTION("getStatus", JS_GetStatus),
416 DECLARE_NAPI_FUNCTION("setStatus", JS_SetStatus),
417 DECLARE_NAPI_FUNCTION("getStatusText", JS_GetStatusText),
418 DECLARE_NAPI_FUNCTION("setStatusText", JS_SetStatusText),
419 DECLARE_NAPI_FUNCTION("getMimeType", JS_GetMimeType),
420 DECLARE_NAPI_FUNCTION("setMimeType", JS_SetMimeType),
421 DECLARE_NAPI_FUNCTION("getEncoding", JS_GetEncoding),
422 DECLARE_NAPI_FUNCTION("setEncoding", JS_SetEncoding),
423 DECLARE_NAPI_FUNCTION("getHeaderByName", JS_GetHeaderByName),
424 DECLARE_NAPI_FUNCTION("setHeaderByName", JS_SetHeaderByName),
425 DECLARE_NAPI_FUNCTION("getNetErrorCode", JS_GetNetErrorCode),
426 DECLARE_NAPI_FUNCTION("setNetErrorCode", JS_SetNetErrorCode),
427 };
428 napi_value webSchemeHandlerResponse = nullptr;
429 napi_define_class(env, WEB_SCHEME_HANDLER_RESPONSE.c_str(), WEB_SCHEME_HANDLER_RESPONSE.length(),
430 JS_Constructor, nullptr,
431 sizeof(properties) / sizeof(properties[0]), properties, &webSchemeHandlerResponse);
432 napi_set_named_property(env, *exportsPointer, WEB_SCHEME_HANDLER_RESPONSE.c_str(),
433 webSchemeHandlerResponse);
434 }
435
JS_Constructor(napi_env env,napi_callback_info cbinfo)436 napi_value NapiWebSchemeHandlerResponse::JS_Constructor(napi_env env, napi_callback_info cbinfo)
437 {
438 WVLOG_D("NapiWebSchemeHandlerResponse::JS_Constructor is called");
439 napi_value thisVar = nullptr;
440 void *data = nullptr;
441 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
442
443 WebSchemeHandlerResponse *response = new WebSchemeHandlerResponse(env);
444
445 napi_wrap(
446 env, thisVar, response,
447 [](napi_env /* env */, void *data, void * /* hint */) {
448 WebSchemeHandlerResponse *response = (WebSchemeHandlerResponse *)data;
449 delete response;
450 },
451 nullptr, nullptr);
452
453 return thisVar;
454 }
455
JS_GetUrl(napi_env env,napi_callback_info cbinfo)456 napi_value NapiWebSchemeHandlerResponse::JS_GetUrl(napi_env env, napi_callback_info cbinfo)
457 {
458 size_t argc = 1;
459 napi_value argv[1] = {0};
460 napi_value thisVar = nullptr;
461 void *data = nullptr;
462 WebSchemeHandlerResponse *response = nullptr;
463 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
464
465 napi_unwrap(env, thisVar, (void **)&response);
466 if (!response) {
467 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetUrl response is nullptr");
468 return nullptr;
469 }
470
471 napi_value urlValue;
472 char *result = response->GetUrl();
473 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &urlValue);
474 OH_ArkWeb_ReleaseString(result);
475 if (status != napi_ok) {
476 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetUrl response get url failed");
477 return nullptr;
478 }
479 return urlValue;
480 }
481
JS_SetUrl(napi_env env,napi_callback_info cbinfo)482 napi_value NapiWebSchemeHandlerResponse::JS_SetUrl(napi_env env, napi_callback_info cbinfo)
483 {
484 size_t argc = 1;
485 napi_value argv[1] = {0};
486 napi_value thisVar = nullptr;
487 void *data = nullptr;
488 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
489
490 WebSchemeHandlerResponse *response = nullptr;
491 napi_unwrap(env, thisVar, (void **)&response);
492
493 if (!response) {
494 WVLOG_E("unwrap WebSchemeHandlerResponse failed");
495 return nullptr;
496 }
497 std::string url = "";
498 if (!NapiParseUtils::ParseString(env, argv[0], url)) {
499 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
500 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "url", "string"));
501 return nullptr;
502 }
503 response->SetUrl(url.c_str());
504 return nullptr;
505 }
506
JS_GetStatus(napi_env env,napi_callback_info cbinfo)507 napi_value NapiWebSchemeHandlerResponse::JS_GetStatus(napi_env env, napi_callback_info cbinfo)
508 {
509 size_t argc = 1;
510 napi_value argv[1] = {0};
511 napi_value thisVar = nullptr;
512 void *data = nullptr;
513 WebSchemeHandlerResponse *response = nullptr;
514 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
515
516 napi_unwrap(env, thisVar, (void **)&response);
517 if (!response) {
518 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetStatus response is nullptr");
519 return nullptr;
520 }
521
522 napi_value status;
523 napi_create_int32(env, response->GetStatus(), &status);
524 return status;
525 }
526
JS_SetStatus(napi_env env,napi_callback_info cbinfo)527 napi_value NapiWebSchemeHandlerResponse::JS_SetStatus(napi_env env, napi_callback_info cbinfo)
528 {
529 size_t argc = 1;
530 napi_value argv[1] = {0};
531 napi_value thisVar = nullptr;
532 void *data = nullptr;
533 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
534
535 WebSchemeHandlerResponse *response = nullptr;
536 napi_unwrap(env, thisVar, (void **)&response);
537
538 if (!response) {
539 WVLOG_E("unwrap WebSchemeHandlerResponse failed");
540 return nullptr;
541 }
542 int32_t status = 0;
543 if (!NapiParseUtils::ParseInt32(env, argv[0], status)) {
544 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
545 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "code", "int"));
546 WVLOG_E("NapiWebSchemeHandlerResponse::JS_SetStatus parse failed");
547 return nullptr;
548 }
549 response->SetStatus(status);
550 return nullptr;
551 }
552
JS_GetStatusText(napi_env env,napi_callback_info cbinfo)553 napi_value NapiWebSchemeHandlerResponse::JS_GetStatusText(napi_env env, napi_callback_info cbinfo)
554 {
555 size_t argc = 1;
556 napi_value argv[1] = {0};
557 napi_value thisVar = nullptr;
558 void *data = nullptr;
559 WebSchemeHandlerResponse *response = nullptr;
560 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
561
562 napi_unwrap(env, thisVar, (void **)&response);
563 if (!response) {
564 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetStatusText response is nullptr");
565 return nullptr;
566 }
567
568 napi_value statusText;
569 char* result = response->GetStatusText();
570 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &statusText);
571 OH_ArkWeb_ReleaseString(result);
572 if (status != napi_ok) {
573 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetStatusText response get failed");
574 return nullptr;
575 }
576 return statusText;
577 }
578
JS_SetStatusText(napi_env env,napi_callback_info cbinfo)579 napi_value NapiWebSchemeHandlerResponse::JS_SetStatusText(napi_env env, napi_callback_info cbinfo)
580 {
581 size_t argc = 1;
582 napi_value argv[1] = {0};
583 napi_value thisVar = nullptr;
584 void *data = nullptr;
585 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
586
587 WebSchemeHandlerResponse *response = nullptr;
588 napi_unwrap(env, thisVar, (void **)&response);
589
590 if (!response) {
591 WVLOG_E("unwrap WebSchemeHandlerResponse failed");
592 return nullptr;
593 }
594 std::string statusText = "";
595 if (!NapiParseUtils::ParseString(env, argv[0], statusText)) {
596 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
597 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "text", "string"));
598 return nullptr;
599 }
600 response->SetStatusText(statusText.c_str());
601 return nullptr;
602 }
603
JS_GetMimeType(napi_env env,napi_callback_info cbinfo)604 napi_value NapiWebSchemeHandlerResponse::JS_GetMimeType(napi_env env, napi_callback_info cbinfo)
605 {
606 size_t argc = 1;
607 napi_value argv[1] = {0};
608 napi_value thisVar = nullptr;
609 void *data = nullptr;
610 WebSchemeHandlerResponse *response = nullptr;
611 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
612
613 napi_unwrap(env, thisVar, (void **)&response);
614 if (!response) {
615 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetMimeType response is nullptr");
616 return nullptr;
617 }
618
619 napi_value mimeType;
620 char* result = response->GetMimeType();
621 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &mimeType);
622 OH_ArkWeb_ReleaseString(result);
623 if (status != napi_ok) {
624 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetMimeType response get failed");
625 return nullptr;
626 }
627 return mimeType;
628 }
629
JS_SetMimeType(napi_env env,napi_callback_info cbinfo)630 napi_value NapiWebSchemeHandlerResponse::JS_SetMimeType(napi_env env, napi_callback_info cbinfo)
631 {
632 size_t argc = 1;
633 napi_value argv[1] = {0};
634 napi_value thisVar = nullptr;
635 void *data = nullptr;
636 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
637 WebSchemeHandlerResponse *response = nullptr;
638 napi_unwrap(env, thisVar, (void **)&response);
639
640 if (!response) {
641 WVLOG_E("unwrap WebSchemeHandlerResponse failed");
642 return nullptr;
643 }
644 std::string mimeType = "";
645 if (!NapiParseUtils::ParseString(env, argv[0], mimeType)) {
646 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
647 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "type", "string"));
648 return nullptr;
649 }
650 response->SetMimeType(mimeType.c_str());
651 return nullptr;
652 }
653
JS_GetEncoding(napi_env env,napi_callback_info cbinfo)654 napi_value NapiWebSchemeHandlerResponse::JS_GetEncoding(napi_env env, napi_callback_info cbinfo)
655 {
656 size_t argc = 1;
657 napi_value argv[1] = {0};
658 napi_value thisVar = nullptr;
659 void *data = nullptr;
660 WebSchemeHandlerResponse *response = nullptr;
661 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
662
663 napi_unwrap(env, thisVar, (void **)&response);
664 if (!response) {
665 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetEncoding response is nullptr");
666 return nullptr;
667 }
668
669 napi_value encoding;
670 char* result = response->GetEncoding();
671 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &encoding);
672 OH_ArkWeb_ReleaseString(result);
673 if (status != napi_ok) {
674 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetEncoding response get failed");
675 return nullptr;
676 }
677 return encoding;
678 }
679
JS_SetEncoding(napi_env env,napi_callback_info cbinfo)680 napi_value NapiWebSchemeHandlerResponse::JS_SetEncoding(napi_env env, napi_callback_info cbinfo)
681 {
682 size_t argc = 1;
683 napi_value argv[1] = {0};
684 napi_value thisVar = nullptr;
685 void *data = nullptr;
686 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
687
688 WebSchemeHandlerResponse *response = nullptr;
689 napi_unwrap(env, thisVar, (void **)&response);
690
691 if (!response) {
692 WVLOG_E("unwrap WebSchemeHandlerResponse failed");
693 return nullptr;
694 }
695 std::string encoding = "";
696 if (!NapiParseUtils::ParseString(env, argv[0], encoding)) {
697 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
698 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "encoding", "string"));
699 return nullptr;
700 }
701 response->SetEncoding(encoding.c_str());
702 return nullptr;
703 }
704
JS_GetHeaderByName(napi_env env,napi_callback_info cbinfo)705 napi_value NapiWebSchemeHandlerResponse::JS_GetHeaderByName(napi_env env, napi_callback_info cbinfo)
706 {
707 size_t argc = 1;
708 napi_value argv[1] = {0};
709 napi_value thisVar = nullptr;
710 void *data = nullptr;
711 WebSchemeHandlerResponse *response = nullptr;
712 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
713
714 napi_unwrap(env, thisVar, (void **)&response);
715 if (!response) {
716 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetEncoding response is nullptr");
717 return nullptr;
718 }
719
720 std::string name;
721 if (!NapiParseUtils::ParseString(env, argv[0], name)) {
722 return nullptr;
723 }
724
725 napi_value headerValue;
726 char* result = response->GetHeaderByName(name.c_str());
727 napi_status status = napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &headerValue);
728 OH_ArkWeb_ReleaseString(result);
729 if (status != napi_ok) {
730 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetEncoding response get failed");
731 return nullptr;
732 }
733 return headerValue;
734 }
735
JS_SetHeaderByName(napi_env env,napi_callback_info cbinfo)736 napi_value NapiWebSchemeHandlerResponse::JS_SetHeaderByName(napi_env env, napi_callback_info cbinfo)
737 {
738 size_t argc = INTEGER_THREE;
739 napi_value argv[INTEGER_THREE] = {0};
740 napi_value thisVar = nullptr;
741 void *data = nullptr;
742 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
743
744 WebSchemeHandlerResponse *response = nullptr;
745 napi_unwrap(env, thisVar, (void **)&response);
746
747 if (!response) {
748 WVLOG_E("unwrap WebSchemeHandlerResponse failed");
749 return nullptr;
750 }
751 std::string name;
752 std::string value;
753 bool overwrite = false;
754 if (argc != INTEGER_THREE) {
755 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
756 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "three"));
757 WVLOG_E("NapiWebSchemeHandlerResponse::JS_SetHeaderByName parse failed");
758 return nullptr;
759 }
760 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ZERO], name)) {
761 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
762 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "name", "string"));
763 WVLOG_E("NapiWebSchemeHandlerResponse::JS_SetHeaderByName parse failed");
764 return nullptr;
765 }
766 if (!NapiParseUtils::ParseString(env, argv[INTEGER_ONE], value)) {
767 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
768 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "value", "string"));
769 WVLOG_E("NapiWebSchemeHandlerResponse::JS_SetHeaderByName parse failed");
770 return nullptr;
771 }
772 if (!NapiParseUtils::ParseBoolean(env, argv[INTEGER_TWO], overwrite)) {
773 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
774 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "overwrite", "booleane"));
775 WVLOG_E("NapiWebSchemeHandlerResponse::JS_SetHeaderByName parse failed");
776 return nullptr;
777 }
778 response->SetHeaderByName(name.c_str(), value.c_str(), overwrite);
779 return nullptr;
780 }
781
JS_GetNetErrorCode(napi_env env,napi_callback_info cbinfo)782 napi_value NapiWebSchemeHandlerResponse::JS_GetNetErrorCode(napi_env env, napi_callback_info cbinfo)
783 {
784 size_t argc = 1;
785 napi_value argv[1] = {0};
786 napi_value thisVar = nullptr;
787 void *data = nullptr;
788 WebSchemeHandlerResponse *response = nullptr;
789 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
790
791 napi_unwrap(env, thisVar, (void **)&response);
792 if (!response) {
793 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetEncoding response is nullptr");
794 return nullptr;
795 }
796
797 napi_value code;
798 int32_t result = response->GetErrorCode();
799 NAPI_CALL(env, napi_create_int32(env, result, &code));
800 return code;
801 }
802
JS_SetNetErrorCode(napi_env env,napi_callback_info cbinfo)803 napi_value NapiWebSchemeHandlerResponse::JS_SetNetErrorCode(napi_env env, napi_callback_info cbinfo)
804 {
805 size_t argc = INTEGER_ONE;
806 napi_value argv[INTEGER_ONE] = {0};
807 napi_value thisVar = nullptr;
808 void *data = nullptr;
809 WebSchemeHandlerResponse *response = nullptr;
810 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
811
812 napi_unwrap(env, thisVar, (void **)&response);
813 if (!response) {
814 WVLOG_E("NapiWebSchemeHandlerResponse::JS_GetEncoding response is nullptr");
815 return nullptr;
816 }
817 int32_t code = 0;
818 if (argc != INTEGER_ONE) {
819 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
820 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
821 return nullptr;
822 }
823 if (!NapiParseUtils::ParseInt32(env, argv[0], code)) {
824 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
825 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "code", "int"));
826 return nullptr;
827 }
828 response->SetErrorCode(code);
829 return nullptr;
830 }
831
Init(napi_env env,napi_value exports)832 napi_value NapiWebSchemeHandler::Init(napi_env env, napi_value exports)
833 {
834 WVLOG_D("NapiWebSchemeHandler::Init");
835 napi_property_descriptor properties[] = {
836 DECLARE_NAPI_FUNCTION("onRequestStart", JS_RequestStart),
837 DECLARE_NAPI_FUNCTION("onRequestStop", JS_RequestStop),
838 };
839 napi_value webSchemeHandler = nullptr;
840 napi_define_class(env, WEB_SCHEME_HANDLER.c_str(), WEB_SCHEME_HANDLER.length(),
841 JS_Constructor, nullptr,
842 sizeof(properties) / sizeof(properties[0]), properties, &webSchemeHandler);
843 napi_set_named_property(env, exports, WEB_SCHEME_HANDLER.c_str(),
844 webSchemeHandler);
845 return exports;
846 }
847
JS_Constructor(napi_env env,napi_callback_info info)848 napi_value NapiWebSchemeHandler::JS_Constructor(napi_env env, napi_callback_info info)
849 {
850 WVLOG_D("NapiWebSchemeHandler::JS_Constructor is called");
851 napi_value thisVar = nullptr;
852 void *data = nullptr;
853 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
854
855 WebSchemeHandler *handler = new WebSchemeHandler(env);
856
857 napi_wrap(
858 env, thisVar, handler,
859 [](napi_env /* env */, void *data, void * /* hint */) {
860 WebSchemeHandler *handler = (WebSchemeHandler *)data;
861 delete handler;
862 },
863 nullptr, nullptr);
864
865 return thisVar;
866 }
867
JS_RequestStart(napi_env env,napi_callback_info info)868 napi_value NapiWebSchemeHandler::JS_RequestStart(napi_env env, napi_callback_info info)
869 {
870 WVLOG_D("NapiWebSchemeHandler::JS_RequestStart");
871 size_t argc = 1;
872 napi_value argv[1] = {0};
873 napi_value thisVar = nullptr;
874 void *data = nullptr;
875 WebSchemeHandler *handler = nullptr;
876 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
877
878 napi_unwrap(env, thisVar, (void **)&handler);
879 if (!handler) {
880 WVLOG_E("webSchemeHandler is null");
881 return thisVar;
882 }
883
884 napi_valuetype valueType = napi_undefined;
885 napi_typeof(env, argv[0], &valueType);
886
887 if (argc != INTEGER_ONE) {
888 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
889 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
890 return nullptr;
891 }
892 if (valueType != napi_function) {
893 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
894 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function"));
895 return nullptr;
896 }
897
898 handler->PutRequestStart(env, argv[0]);
899 return thisVar;
900 }
901
JS_RequestStop(napi_env env,napi_callback_info info)902 napi_value NapiWebSchemeHandler::JS_RequestStop(napi_env env, napi_callback_info info)
903 {
904 WVLOG_D("NapiWebSchemeHandler::JS_RequestStop");
905 size_t argc = 1;
906 napi_value argv[1] = {0};
907 napi_value thisVar = nullptr;
908 void *data = nullptr;
909 WebSchemeHandler *handler = nullptr;
910 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
911
912 napi_unwrap(env, thisVar, (void **)&handler);
913 if (!handler) {
914 WVLOG_E("webSchemeHandler is null");
915 return thisVar;
916 }
917
918 napi_valuetype valueType = napi_undefined;
919 napi_typeof(env, argv[0], &valueType);
920
921 if (argc != INTEGER_ONE || valueType != napi_function) {
922 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
923 return nullptr;
924 }
925
926 handler->PutRequestStop(env, argv[0]);
927 return thisVar;
928 }
929
Init(napi_env env,napi_value exports)930 napi_value NapiWebResourceHandler::Init(napi_env env, napi_value exports)
931 {
932 ExportWebResourceHandlerClass(env, &exports);
933 return exports;
934 }
935
ExportWebResourceHandlerClass(napi_env env,napi_value * exportsPointer)936 void NapiWebResourceHandler::ExportWebResourceHandlerClass(
937 napi_env env, napi_value* exportsPointer)
938 {
939 napi_property_descriptor properties[] = {
940 DECLARE_NAPI_FUNCTION("didReceiveResponse", JS_DidReceiveResponse),
941 DECLARE_NAPI_FUNCTION("didReceiveResponseBody", JS_DidReceiveResponseBody),
942 DECLARE_NAPI_FUNCTION("didFinish", JS_DidFinish),
943 DECLARE_NAPI_FUNCTION("didFail", JS_DidFailWithError),
944 };
945 napi_value webResourceHandler = nullptr;
946 napi_define_class(env, WEB_RESOURCE_HANDLER.c_str(), WEB_RESOURCE_HANDLER.length(),
947 JS_Constructor, nullptr,
948 sizeof(properties) / sizeof(properties[0]), properties, &webResourceHandler);
949 napi_set_named_property(env, *exportsPointer, WEB_RESOURCE_HANDLER.c_str(),
950 webResourceHandler);
951 }
952
DefineProperties(napi_env env,napi_value * object)953 napi_status NapiWebResourceHandler::DefineProperties(
954 napi_env env, napi_value* object)
955 {
956 napi_property_descriptor properties[] = {
957 DECLARE_NAPI_FUNCTION("didReceiveResponse", JS_DidReceiveResponse),
958 DECLARE_NAPI_FUNCTION("didReceiveResponseBody", JS_DidReceiveResponseBody),
959 DECLARE_NAPI_FUNCTION("didFinish", JS_DidFinish),
960 DECLARE_NAPI_FUNCTION("didFail", JS_DidFailWithError),
961 };
962 return napi_define_properties(env, *object, sizeof(properties) / sizeof(properties[0]), properties);
963 }
964
JS_Constructor(napi_env env,napi_callback_info info)965 napi_value NapiWebResourceHandler::JS_Constructor(napi_env env, napi_callback_info info)
966 {
967 WVLOG_D("NapiWebResourceHandler::JS_Constructor is called");
968 napi_value thisVar = nullptr;
969 void *data = nullptr;
970 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
971
972 WebResourceHandler *handler = new WebResourceHandler(env);
973
974 napi_wrap(
975 env, thisVar, handler,
976 [](napi_env /* env */, void *data, void * /* hint */) {
977 WebResourceHandler *handler = (WebResourceHandler *)data;
978 delete handler;
979 },
980 nullptr, nullptr);
981
982 return thisVar;
983 }
984
JS_DidReceiveResponse(napi_env env,napi_callback_info info)985 napi_value NapiWebResourceHandler::JS_DidReceiveResponse(napi_env env, napi_callback_info info)
986 {
987 WVLOG_D("NapiWebResourceHandler::JS_DidReceiveResponse is called");
988 size_t argc = 1;
989 napi_value argv[1] = {0};
990 napi_value thisVar = nullptr;
991 void *data = nullptr;
992 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
993
994 if (argc != 1) {
995 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
996 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
997 return nullptr;
998 }
999
1000 WebResourceHandler *resourceHandler = nullptr;
1001 napi_unwrap(env, thisVar, (void **)&resourceHandler);
1002
1003 if (!resourceHandler) {
1004 WVLOG_E("JS_DidReceiveResponse unwrap resource handler failed");
1005 return nullptr;
1006 }
1007
1008 WebSchemeHandlerResponse* response = nullptr;
1009 napi_value obj = argv[0];
1010 napi_unwrap(env, obj, (void**)&response);
1011 if (!response) {
1012 WVLOG_E("JS_DidReceiveResponse unwrap response failed");
1013 return nullptr;
1014 }
1015 int32_t ret =
1016 resourceHandler->DidReceiveResponse(response->GetArkWebResponse());
1017 if (ret != 0) {
1018 BusinessError::ThrowErrorByErrcode(env, RESOURCE_HANDLER_INVALID);
1019 }
1020 return nullptr;
1021 }
1022
JS_DidReceiveResponseBody(napi_env env,napi_callback_info info)1023 napi_value NapiWebResourceHandler::JS_DidReceiveResponseBody(napi_env env, napi_callback_info info)
1024 {
1025 WVLOG_D("NapiWebResourceHandler::JS_DidReceiveResponseBody is called");
1026 size_t argc = 1;
1027 napi_value argv[1] = {0};
1028 napi_value thisVar = nullptr;
1029 void *data = nullptr;
1030 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1031
1032 if (argc != 1) {
1033 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
1034 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one"));
1035 return nullptr;
1036 }
1037
1038 WebResourceHandler *resourceHandler = nullptr;
1039 napi_unwrap(env, thisVar, (void **)&resourceHandler);
1040
1041 if (!resourceHandler) {
1042 WVLOG_E("unwrap resource handler failed");
1043 return nullptr;
1044 }
1045 bool isArrayBuffer = false;
1046 NAPI_CALL(env, napi_is_arraybuffer(env, argv[0], &isArrayBuffer));
1047 if (!isArrayBuffer) {
1048 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR);
1049 return nullptr;
1050 }
1051
1052 uint8_t *arrBuf = nullptr;
1053 size_t byteLength = 0;
1054 napi_get_arraybuffer_info(env, argv[0], (void **)&arrBuf, &byteLength);
1055 int32_t ret = resourceHandler->DidReceiveResponseBody(
1056 arrBuf, static_cast<int64_t>(byteLength));
1057 if (ret != 0) {
1058 WVLOG_E("JS_DidReceiveResponseBody ret=%{public}d", ret);
1059 BusinessError::ThrowErrorByErrcode(env, RESOURCE_HANDLER_INVALID);
1060 }
1061 return nullptr;
1062 }
1063
JS_DidFinish(napi_env env,napi_callback_info info)1064 napi_value NapiWebResourceHandler::JS_DidFinish(napi_env env, napi_callback_info info)
1065 {
1066 WVLOG_D("NapiWebResourceHandler::JS_DidFinish is called");
1067 napi_value thisVar = nullptr;
1068 void *data = nullptr;
1069 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1070
1071 WebResourceHandler *resourceHandler = nullptr;
1072 napi_unwrap(env, thisVar, (void **)&resourceHandler);
1073
1074 if (!resourceHandler) {
1075 WVLOG_E("unwrap resource handler failed");
1076 return nullptr;
1077 }
1078
1079 int32_t ret = resourceHandler->DidFinish();
1080 if (ret != 0) {
1081 BusinessError::ThrowErrorByErrcode(env, RESOURCE_HANDLER_INVALID);
1082 WVLOG_E("JS_DidFinish ret=%{public}d", ret);
1083 }
1084 return nullptr;
1085 }
1086
JS_DidFailWithError(napi_env env,napi_callback_info info)1087 napi_value NapiWebResourceHandler::JS_DidFailWithError(napi_env env, napi_callback_info info)
1088 {
1089 size_t argc = 1;
1090 napi_value argv[1] = {0};
1091 napi_value thisVar = nullptr;
1092 void *data = nullptr;
1093 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1094
1095 WebResourceHandler *resourceHandler = nullptr;
1096 napi_unwrap(env, thisVar, (void **)&resourceHandler);
1097
1098 if (!resourceHandler) {
1099 WVLOG_E("unwrap resource handler failed");
1100 return nullptr;
1101 }
1102
1103 int32_t errorCode;
1104 if (!NapiParseUtils::ParseInt32(env, argv[0], errorCode)) {
1105 WVLOG_E("JS_DidFailWithError unwrap error code failed");
1106 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
1107 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "code", "int"));
1108 return nullptr;
1109 }
1110
1111 int32_t ret = resourceHandler->DidFailWithError(
1112 static_cast<ArkWeb_NetError>(errorCode));
1113 if (ret != 0) {
1114 BusinessError::ThrowErrorByErrcode(env, RESOURCE_HANDLER_INVALID);
1115 WVLOG_E("JS_DidFailWithError ret=%{public}d", ret);
1116 }
1117 return nullptr;
1118 }
1119
Init(napi_env env,napi_value exports)1120 napi_value NapiWebHttpBodyStream::Init(napi_env env, napi_value exports)
1121 {
1122 ExportWebHttpBodyStreamClass(env, &exports);
1123 return exports;
1124 }
1125
ExportWebHttpBodyStreamClass(napi_env env,napi_value * exportsPointer)1126 void NapiWebHttpBodyStream::ExportWebHttpBodyStreamClass(
1127 napi_env env, napi_value* exportsPointer)
1128 {
1129 napi_property_descriptor properties[] = {
1130 DECLARE_NAPI_FUNCTION("initialize", JS_Initialize),
1131 DECLARE_NAPI_FUNCTION("read", JS_Read),
1132 DECLARE_NAPI_FUNCTION("getSize", JS_GetSize),
1133 DECLARE_NAPI_FUNCTION("getPosition", JS_GetPostion),
1134 DECLARE_NAPI_FUNCTION("isChunked", JS_IsChunked),
1135 DECLARE_NAPI_FUNCTION("isEof", JS_IsEof),
1136 DECLARE_NAPI_FUNCTION("isInMemory", JS_IsInMemory),
1137 };
1138 napi_value webHttpBodyStream = nullptr;
1139 napi_define_class(env, WEB_HTTP_BODY_STREAM.c_str(), WEB_HTTP_BODY_STREAM.length(),
1140 JS_Constructor, nullptr,
1141 sizeof(properties) / sizeof(properties[0]), properties, &webHttpBodyStream);
1142 napi_set_named_property(env, *exportsPointer, WEB_HTTP_BODY_STREAM.c_str(),
1143 webHttpBodyStream);
1144 }
1145
DefineProperties(napi_env env,napi_value * object)1146 napi_status NapiWebHttpBodyStream::DefineProperties(napi_env env, napi_value* object)
1147 {
1148 napi_property_descriptor properties[] = {
1149 DECLARE_NAPI_FUNCTION("initialize", JS_Initialize),
1150 DECLARE_NAPI_FUNCTION("read", JS_Read),
1151 DECLARE_NAPI_FUNCTION("getSize", JS_GetSize),
1152 DECLARE_NAPI_FUNCTION("getPosition", JS_GetPostion),
1153 DECLARE_NAPI_FUNCTION("isChunked", JS_IsChunked),
1154 DECLARE_NAPI_FUNCTION("isEof", JS_IsEof),
1155 DECLARE_NAPI_FUNCTION("isInMemory", JS_IsInMemory),
1156 };
1157 return napi_define_properties(env, *object,
1158 sizeof(properties) / sizeof(properties[0]), properties);
1159 }
1160
JS_Constructor(napi_env env,napi_callback_info info)1161 napi_value NapiWebHttpBodyStream::JS_Constructor(napi_env env, napi_callback_info info)
1162 {
1163 WVLOG_D("NapiWebHttpBodyStream::JS_Constructor is called");
1164 napi_value thisVar = nullptr;
1165 void *data = nullptr;
1166 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1167
1168 WebHttpBodyStream *stream = new WebHttpBodyStream(env);
1169 napi_wrap(
1170 env, thisVar, stream,
1171 [](napi_env /* env */, void *data, void * /* hint */) {
1172 WebHttpBodyStream *stream = (WebHttpBodyStream *)data;
1173 delete stream;
1174 },
1175 nullptr, nullptr);
1176 return thisVar;
1177 }
1178
JS_Initialize(napi_env env,napi_callback_info info)1179 napi_value NapiWebHttpBodyStream::JS_Initialize(napi_env env, napi_callback_info info)
1180 {
1181 napi_value thisVar = nullptr;
1182 size_t argc = INTEGER_ONE;
1183 size_t argcPromise = INTEGER_ZERO;
1184 size_t argcCallback = INTEGER_ONE;
1185 napi_value argv[INTEGER_ONE] = {0};
1186 WebHttpBodyStream *stream = nullptr;
1187
1188 napi_value result = nullptr;
1189 napi_get_undefined(env, &result);
1190 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1191 napi_unwrap(env, thisVar, (void **)&stream);
1192 if (!stream) {
1193 WVLOG_E("NapiWebHttpBodyStream::JS_Initialize stream is nullptr");
1194 return nullptr;
1195 }
1196 if (argc != argcPromise && argc != argcCallback) {
1197 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
1198 return nullptr;
1199 }
1200 if (argc == argcCallback) {
1201 napi_valuetype valueType = napi_null;
1202 napi_typeof(env, argv[argcCallback - 1], &valueType);
1203 if (valueType != napi_function) {
1204 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
1205 return nullptr;
1206 }
1207 napi_ref jsCallback = nullptr;
1208 napi_create_reference(env, argv[argcCallback - 1], INTEGER_ONE, &jsCallback);
1209
1210 if (jsCallback) {
1211 stream->Init(std::move(jsCallback), nullptr);
1212 }
1213 return result;
1214 }
1215
1216 napi_deferred deferred = nullptr;
1217 napi_value promise = nullptr;
1218 napi_create_promise(env, &deferred, &promise);
1219 if (promise && deferred) {
1220 stream->Init(nullptr, std::move(deferred));
1221 return promise;
1222 }
1223 return result;
1224 }
1225
CheckReadParamsNumber(napi_env env,const size_t argc)1226 bool CheckReadParamsNumber(napi_env env, const size_t argc)
1227 {
1228 size_t argcPromise = INTEGER_ONE;
1229 size_t argcCallback = INTEGER_TWO;
1230 if (argc != argcPromise && argc != argcCallback) {
1231 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
1232 NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_TWO, "one", "two"));
1233 WVLOG_E("NapiWebHttpBodyStream::JS_Read parse failed");
1234 return false;
1235 }
1236 return true;
1237 }
1238
checkReadBufLen(napi_env env,const int32_t bufLen)1239 bool checkReadBufLen(napi_env env, const int32_t bufLen)
1240 {
1241 if (bufLen <= 0) {
1242 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
1243 "BusinessError 401: Parameter error. The value of size must be a number greater than 0.");
1244 WVLOG_E("NapiWebHttpBodyStream::JS_Read parse failed");
1245 return false;
1246 }
1247 return true;
1248 }
1249
JS_Read(napi_env env,napi_callback_info info)1250 napi_value NapiWebHttpBodyStream::JS_Read(napi_env env, napi_callback_info info)
1251 {
1252 napi_value thisVar = nullptr;
1253 void* data = nullptr;
1254 size_t argc = INTEGER_TWO;
1255 size_t argcCallback = INTEGER_TWO;
1256 napi_value argv[INTEGER_TWO] = {0};
1257 WebHttpBodyStream *stream = nullptr;
1258
1259 napi_value result = nullptr;
1260 napi_get_undefined(env, &result);
1261 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1262 napi_unwrap(env, thisVar, (void **)&stream);
1263 if (!stream) {
1264 WVLOG_E("NapiWebHttpBodyStream::JS_Initialize stream is nullptr");
1265 return nullptr;
1266 }
1267 if (!CheckReadParamsNumber(env, argc)) {
1268 return nullptr;
1269 }
1270
1271 int32_t bufLen = 0;
1272 NapiParseUtils::ParseInt32(env, argv[0], bufLen);
1273 if (!checkReadBufLen(env, bufLen)) {
1274 return nullptr;
1275 }
1276
1277 if (argc == argcCallback) {
1278 napi_valuetype valueType = napi_null;
1279 napi_typeof(env, argv[argcCallback - 1], &valueType);
1280 if (valueType != napi_function) {
1281 NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR,
1282 NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "callback", "function"));
1283 return nullptr;
1284 }
1285 napi_ref jsCallback = nullptr;
1286 napi_create_reference(env, argv[argcCallback - 1], INTEGER_ONE, &jsCallback);
1287
1288 if (jsCallback) {
1289 stream->Read(bufLen, std::move(jsCallback), nullptr);
1290 }
1291 return result;
1292 }
1293
1294 napi_deferred deferred = nullptr;
1295 napi_value promise = nullptr;
1296 napi_create_promise(env, &deferred, &promise);
1297 if (promise && deferred) {
1298 stream->Read(bufLen, nullptr, std::move(deferred));
1299 return promise;
1300 }
1301 return result;
1302 }
1303
JS_GetSize(napi_env env,napi_callback_info info)1304 napi_value NapiWebHttpBodyStream::JS_GetSize(napi_env env, napi_callback_info info)
1305 {
1306 napi_value thisVar = nullptr;
1307 void *data = nullptr;
1308 WebHttpBodyStream *stream = nullptr;
1309 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1310
1311 napi_unwrap(env, thisVar, (void **)&stream);
1312 if (!stream) {
1313 WVLOG_E("NapiWebHttpBodyStream::JS_GetSize stream is nullptr");
1314 return nullptr;
1315 }
1316
1317 napi_value value;
1318 int64_t result = static_cast<int64_t>(stream->GetSize());
1319 napi_create_int64(env, result, &value);
1320 return value;
1321 }
1322
JS_GetPostion(napi_env env,napi_callback_info info)1323 napi_value NapiWebHttpBodyStream::JS_GetPostion(napi_env env, napi_callback_info info)
1324 {
1325 napi_value thisVar = nullptr;
1326 void *data = nullptr;
1327 WebHttpBodyStream *stream = nullptr;
1328 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1329
1330 napi_unwrap(env, thisVar, (void **)&stream);
1331 if (!stream) {
1332 WVLOG_E("NapiWebHttpBodyStream::JS_GetPostion stream is nullptr");
1333 return nullptr;
1334 }
1335
1336 napi_value value;
1337 int64_t result = static_cast<int64_t>(stream->GetPostion());
1338 napi_create_int64(env, result, &value);
1339 return value;
1340 }
1341
JS_IsChunked(napi_env env,napi_callback_info info)1342 napi_value NapiWebHttpBodyStream::JS_IsChunked(napi_env env, napi_callback_info info)
1343 {
1344 napi_value thisVar = nullptr;
1345 void *data = nullptr;
1346 WebHttpBodyStream *stream = nullptr;
1347 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1348
1349 napi_unwrap(env, thisVar, (void **)&stream);
1350 if (!stream) {
1351 WVLOG_E("NapiWebHttpBodyStream::JS_IsChunked stream is nullptr");
1352 return nullptr;
1353 }
1354
1355 napi_value value;
1356 bool result = stream->IsChunked();
1357 NAPI_CALL(env, napi_get_boolean(env, result, &value));
1358 return value;
1359 }
1360
JS_IsEof(napi_env env,napi_callback_info info)1361 napi_value NapiWebHttpBodyStream::JS_IsEof(napi_env env, napi_callback_info info)
1362 {
1363 napi_value thisVar = nullptr;
1364 void *data = nullptr;
1365 WebHttpBodyStream *stream = nullptr;
1366 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1367
1368 napi_unwrap(env, thisVar, (void **)&stream);
1369 if (!stream) {
1370 WVLOG_E("NapiWebHttpBodyStream::JS_IsEof stream is nullptr");
1371 return nullptr;
1372 }
1373
1374 napi_value value;
1375 bool result = stream->IsEof();
1376 NAPI_CALL(env, napi_get_boolean(env, result, &value));
1377 return value;
1378 }
1379
JS_IsInMemory(napi_env env,napi_callback_info info)1380 napi_value NapiWebHttpBodyStream::JS_IsInMemory(napi_env env, napi_callback_info info)
1381 {
1382 napi_value thisVar = nullptr;
1383 void *data = nullptr;
1384 WebHttpBodyStream *stream = nullptr;
1385 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
1386
1387 napi_unwrap(env, thisVar, (void **)&stream);
1388 if (!stream) {
1389 WVLOG_E("NapiWebHttpBodyStream::JS_IsInMemory stream is nullptr");
1390 return nullptr;
1391 }
1392
1393 napi_value value;
1394 bool result = stream->IsInMemory();
1395 NAPI_CALL(env, napi_get_boolean(env, result, &value));
1396 return value;
1397 }
1398 }
1399 }
1400