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