1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_web_download_item.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_c_api.h"
26 #include "nweb_log.h"
27 #include "web_download_item.h"
28 #include "web_download.pb.h"
29 #include "web_errors.h"
30 
31 namespace download {
32 enum DownloadInterruptReason {
33     DOWNLOAD_INTERRUPT_REASON_NONE = 0,
34 
35 #define INTERRUPT_REASON(name, value) DOWNLOAD_INTERRUPT_REASON_##name = value,
36 
37     // Generic file operation failure.
38     // "File Error".
39     INTERRUPT_REASON(FILE_FAILED, 1)
40 
41     // The file cannot be accessed due to security restrictions.
42     // "Access Denied".
43     INTERRUPT_REASON(FILE_ACCESS_DENIED, 2)
44 
45     // There is not enough room on the drive.
46     // "Disk Full".
47     INTERRUPT_REASON(FILE_NO_SPACE, 3)
48 
49     // The directory or file name is too long.
50     // "Path Too Long".
51     INTERRUPT_REASON(FILE_NAME_TOO_LONG, 5)
52 
53     // The file is too large for the file system to handle.
54     // "File Too Large".
55     INTERRUPT_REASON(FILE_TOO_LARGE, 6)
56 
57     // The file contains a virus.
58     // "Virus".
59     INTERRUPT_REASON(FILE_VIRUS_INFECTED, 7)
60 
61     // The file was in use.
62     // Too many files are opened at once.
63     // We have run out of memory.
64     // "Temporary Problem".
65     INTERRUPT_REASON(FILE_TRANSIENT_ERROR, 10)
66 
67     // The file was blocked due to local policy.
68     // "Blocked"
69     INTERRUPT_REASON(FILE_BLOCKED, 11)
70 
71     // An attempt to check the safety of the download failed due to unexpected
72     // reasons. See http://crbug.com/153212.
73     INTERRUPT_REASON(FILE_SECURITY_CHECK_FAILED, 12)
74 
75     // An attempt was made to seek past the end of a file in opening
76     // a file (as part of resuming a previously interrupted download).
77     INTERRUPT_REASON(FILE_TOO_SHORT, 13)
78 
79     // The partial file didn't match the expected hash.
80     INTERRUPT_REASON(FILE_HASH_MISMATCH, 14)
81 
82     // The source and the target of the download were the same.
83     INTERRUPT_REASON(FILE_SAME_AS_SOURCE, 15)
84 
85     // Network errors.
86 
87     // Generic network failure.
88     // "Network Error".
89     INTERRUPT_REASON(NETWORK_FAILED, 20)
90 
91     // The network operation timed out.
92     // "Operation Timed Out".
93     INTERRUPT_REASON(NETWORK_TIMEOUT, 21)
94 
95     // The network connection has been lost.
96     // "Connection Lost".
97     INTERRUPT_REASON(NETWORK_DISCONNECTED, 22)
98 
99     // The server has gone down.
100     // "Server Down".
101     INTERRUPT_REASON(NETWORK_SERVER_DOWN, 23)
102 
103     // The network request was invalid. This may be due to the original URL or a
104     // redirected URL:
105     // - Having an unsupported scheme.
106     // - Being an invalid URL.
107     // - Being disallowed by policy.
108     INTERRUPT_REASON(NETWORK_INVALID_REQUEST, 24)
109 
110     // Server responses.
111 
112     // The server indicates that the operation has failed (generic).
113     // "Server Error".
114     INTERRUPT_REASON(SERVER_FAILED, 30)
115 
116     // The server does not support range requests.
117     // Internal use only:  must restart from the beginning.
118     INTERRUPT_REASON(SERVER_NO_RANGE, 31)
119 
120     // Precondition failed. This type of interruption could legitimately occur if a
121     // partial download resumption was attempted using a If-Match header. However,
122     // the downloads logic no longer uses If-Match headers and instead uses If-Range
123     // headers where a precondition failure is not expected.
124     //
125     // Obsolete: INTERRUPT_REASON(SERVER_PRECONDITION, 32)
126 
127     // The server does not have the requested data.
128     // "Unable to get file".
129     INTERRUPT_REASON(SERVER_BAD_CONTENT, 33)
130 
131     // Server didn't authorize access to resource.
132     INTERRUPT_REASON(SERVER_UNAUTHORIZED, 34)
133 
134     // Server certificate problem.
135     INTERRUPT_REASON(SERVER_CERT_PROBLEM, 35)
136 
137     // Server access forbidden.
138     INTERRUPT_REASON(SERVER_FORBIDDEN, 36)
139 
140     // Unexpected server response. This might indicate that the responding server
141     // may not be the intended server.
142     INTERRUPT_REASON(SERVER_UNREACHABLE, 37)
143 
144     // The server sent fewer bytes than the content-length header. It may indicate
145     // that the connection was closed prematurely, or the Content-Length header was
146     // invalid. The download is only interrupted if strong validators are present.
147     // Otherwise, it is treated as finished.
148     INTERRUPT_REASON(SERVER_CONTENT_LENGTH_MISMATCH, 38)
149 
150     // An unexpected cross-origin redirect happened.
151     INTERRUPT_REASON(SERVER_CROSS_ORIGIN_REDIRECT, 39)
152 
153     // User input.
154 
155     // The user canceled the download.
156     // "Canceled".
157     INTERRUPT_REASON(USER_CANCELED, 40)
158 
159     // The user shut down the browser.
160     // Internal use only:  resume pending downloads if possible.
161     INTERRUPT_REASON(USER_SHUTDOWN, 41)
162 
163     // Crash.
164 
165     // The browser crashed.
166     // Internal use only:  resume pending downloads if possible.
167     INTERRUPT_REASON(CRASH, 50)
168 
169 #undef INTERRUPT_REASON
170 };
171 } // namespace download
172 
173 namespace OHOS {
174 namespace NWeb {
175 
176 using namespace NWebError;
177 
178 namespace {
ToInt32Value(napi_env env,int32_t number)179 napi_value ToInt32Value(napi_env env, int32_t number)
180 {
181     napi_value result = nullptr;
182     napi_status status = napi_create_int32(env, number, &result);
183     if (status != napi_ok) {
184         WVLOG_E("napi_create_int32 failed.");
185         return nullptr;
186     }
187     return result;
188 }
189 
CreateEnumConstructor(napi_env env,napi_callback_info info)190 napi_value CreateEnumConstructor(napi_env env, napi_callback_info info)
191 {
192     napi_value arg = nullptr;
193     napi_get_cb_info(env, info, nullptr, nullptr, &arg, nullptr);
194     return arg;
195 }
196 } // namespace
197 
JS_GetMethod(napi_env env,napi_callback_info cbinfo)198 napi_value NapiWebDownloadItem::JS_GetMethod(napi_env env, napi_callback_info cbinfo)
199 {
200     WVLOG_I("[DOWNLOAD] NapiWebDownloadItem::JS_GetMethod");
201     size_t argc = 1;
202     napi_value argv[1] = {0};
203     napi_value thisVar = nullptr;
204     void *data = nullptr;
205     WebDownloadItem *webDownloadItem = nullptr;
206     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
207 
208     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
209     if (!webDownloadItem) {
210         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetMethod webDownloadItem is null");
211         return nullptr;
212     }
213 
214     napi_value methodValue;
215     napi_status status = napi_create_string_utf8(env, webDownloadItem->method.c_str(), NAPI_AUTO_LENGTH, &methodValue);
216     if (status != napi_ok) {
217         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetMethod failed");
218         return nullptr;
219     }
220     return methodValue;
221 }
222 
JS_GetMimeType(napi_env env,napi_callback_info cbinfo)223 napi_value NapiWebDownloadItem::JS_GetMimeType(napi_env env, napi_callback_info cbinfo)
224 {
225     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetMimeType");
226     size_t argc = 1;
227     napi_value argv[1] = {0};
228     napi_value thisVar = nullptr;
229     void *data = nullptr;
230     WebDownloadItem *webDownloadItem = nullptr;
231     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
232 
233     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
234     if (!webDownloadItem) {
235         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetMimeType webDownloadItem is null");
236         return nullptr;
237     }
238 
239     napi_value mimeTypeValue;
240     napi_status status =
241         napi_create_string_utf8(env, webDownloadItem->mimeType.c_str(), NAPI_AUTO_LENGTH, &mimeTypeValue);
242     if (status != napi_ok) {
243         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetMimeType failed");
244         return nullptr;
245     }
246     return mimeTypeValue;
247 }
248 
JS_GetUrl(napi_env env,napi_callback_info cbinfo)249 napi_value NapiWebDownloadItem::JS_GetUrl(napi_env env, napi_callback_info cbinfo)
250 {
251     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetUrl");
252     size_t argc = 1;
253     napi_value argv[1] = {0};
254     napi_value thisVar = nullptr;
255     void *data = nullptr;
256     WebDownloadItem *webDownloadItem = nullptr;
257     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
258 
259     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
260     if (!webDownloadItem) {
261         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetUrl webDownloadItem is null");
262         return nullptr;
263     }
264 
265     napi_value urlValue;
266     napi_status status = napi_create_string_utf8(env, webDownloadItem->url.c_str(), NAPI_AUTO_LENGTH, &urlValue);
267     if (status != napi_ok) {
268         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetUrl failed");
269         return nullptr;
270     }
271     return urlValue;
272 }
273 
JS_GetSuggestedFileName(napi_env env,napi_callback_info cbinfo)274 napi_value NapiWebDownloadItem::JS_GetSuggestedFileName(napi_env env, napi_callback_info cbinfo)
275 {
276     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetSuggestedFileName");
277     size_t argc = 1;
278     napi_value argv[1] = {0};
279     napi_value thisVar = nullptr;
280     void *data = nullptr;
281     WebDownloadItem *webDownloadItem = nullptr;
282     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
283 
284     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
285     if (!webDownloadItem) {
286         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetSuggestedFileName webDownloadItem is null");
287         return nullptr;
288     }
289 
290     napi_value fileNameValue;
291     napi_status status =
292         napi_create_string_utf8(env, webDownloadItem->suggestedFileName.c_str(), NAPI_AUTO_LENGTH, &fileNameValue);
293     if (status != napi_ok) {
294         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetSuggestedFileName failed");
295         return nullptr;
296     }
297     return fileNameValue;
298 }
299 
JS_GetCurrentSpeed(napi_env env,napi_callback_info cbinfo)300 napi_value NapiWebDownloadItem::JS_GetCurrentSpeed(napi_env env, napi_callback_info cbinfo)
301 {
302     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetCurrentSpeed");
303     size_t argc = 1;
304     napi_value argv[1] = {0};
305     napi_value thisVar = nullptr;
306     void *data = nullptr;
307     WebDownloadItem *webDownloadItem = nullptr;
308     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
309 
310     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
311     if (!webDownloadItem) {
312         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetCurrentSpeed webDownloadItem is null");
313         return nullptr;
314     }
315 
316     napi_value currentSpeed;
317     napi_status status = napi_create_int64(env, webDownloadItem->currentSpeed, &currentSpeed);
318     if (status != napi_ok) {
319         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetCurrentSpeed failed");
320         return nullptr;
321     }
322     return currentSpeed;
323 }
324 
JS_GetPercentComplete(napi_env env,napi_callback_info cbinfo)325 napi_value NapiWebDownloadItem::JS_GetPercentComplete(napi_env env, napi_callback_info cbinfo)
326 {
327     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetPercentComplete");
328     size_t argc = 1;
329     napi_value argv[1] = {0};
330     napi_value thisVar = nullptr;
331     void *data = nullptr;
332     WebDownloadItem *webDownloadItem = nullptr;
333     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
334 
335     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
336     if (!webDownloadItem) {
337         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetPercentComplete webDownloadItem is null");
338         return nullptr;
339     }
340 
341     napi_value percentComplete;
342     napi_status status = napi_create_int64(env, webDownloadItem->percentComplete, &percentComplete);
343     if (status != napi_ok) {
344         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetPercentComplete failed");
345         return nullptr;
346     }
347     return percentComplete;
348 }
349 
JS_GetTotalBytes(napi_env env,napi_callback_info cbinfo)350 napi_value NapiWebDownloadItem::JS_GetTotalBytes(napi_env env, napi_callback_info cbinfo)
351 {
352     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetTotalBytes");
353     size_t argc = 1;
354     napi_value argv[1] = {0};
355     napi_value thisVar = nullptr;
356     void *data = nullptr;
357     WebDownloadItem *webDownloadItem = nullptr;
358     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
359 
360     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
361     if (!webDownloadItem) {
362         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetTotalBytes webDownloadItem is null");
363         return nullptr;
364     }
365 
366     napi_value totalBytes;
367     napi_status status = napi_create_int64(env, webDownloadItem->totalBytes, &totalBytes);
368     if (status != napi_ok) {
369         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetTotalBytes failed");
370         return nullptr;
371     }
372     return totalBytes;
373 }
374 
JS_GetReceivedBytes(napi_env env,napi_callback_info cbinfo)375 napi_value NapiWebDownloadItem::JS_GetReceivedBytes(napi_env env, napi_callback_info cbinfo)
376 {
377     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetReceivedBytes");
378     size_t argc = 1;
379     napi_value argv[1] = {0};
380     napi_value thisVar = nullptr;
381     void *data = nullptr;
382     WebDownloadItem *webDownloadItem = nullptr;
383     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
384 
385     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
386     if (!webDownloadItem) {
387         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetReceivedBytes webDownloadItem is null");
388         return nullptr;
389     }
390 
391     napi_value receivedBytes;
392     napi_status status = napi_create_int64(env, webDownloadItem->receivedBytes, &receivedBytes);
393     if (status != napi_ok) {
394         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetReceivedBytes failed");
395         return nullptr;
396     }
397     return receivedBytes;
398 }
399 
JS_GetState(napi_env env,napi_callback_info cbinfo)400 napi_value NapiWebDownloadItem::JS_GetState(napi_env env, napi_callback_info cbinfo)
401 {
402     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetState");
403     size_t argc = 1;
404     napi_value argv[1] = {0};
405     napi_value thisVar = nullptr;
406     void *data = nullptr;
407     WebDownloadItem *webDownloadItem = nullptr;
408     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
409     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
410 
411     if (!webDownloadItem) {
412         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
413         return nullptr;
414     }
415 
416     napi_value state;
417     napi_status status = napi_create_int32(env, static_cast<int32_t>(webDownloadItem->state), &state);
418     if (status != napi_ok) {
419         WVLOG_E("napi_create_int32 failed.");
420         return nullptr;
421     }
422     return state;
423 }
424 
JS_GetLastErrorCode(napi_env env,napi_callback_info cbinfo)425 napi_value NapiWebDownloadItem::JS_GetLastErrorCode(napi_env env, napi_callback_info cbinfo)
426 {
427     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetLastErrorCode");
428     size_t argc = 1;
429     napi_value argv[1] = {0};
430     napi_value thisVar = nullptr;
431     void *data = nullptr;
432     WebDownloadItem *webDownloadItem = nullptr;
433     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
434 
435     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
436 
437     if (!webDownloadItem) {
438         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
439         return nullptr;
440     }
441 
442     napi_value errorCode;
443     napi_status status = napi_create_int32(env, static_cast<int32_t>(webDownloadItem->lastErrorCode), &errorCode);
444     if (status != napi_ok) {
445         WVLOG_E("napi_create_int32 failed.");
446         return nullptr;
447     }
448     return errorCode;
449 }
450 
JS_GetGuid(napi_env env,napi_callback_info cbinfo)451 napi_value NapiWebDownloadItem::JS_GetGuid(napi_env env, napi_callback_info cbinfo)
452 {
453     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetGuid");
454     size_t argc = 1;
455     napi_value argv[1] = {0};
456     napi_value thisVar = nullptr;
457     void *data = nullptr;
458     WebDownloadItem *webDownloadItem = nullptr;
459     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
460 
461     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
462 
463     if (!webDownloadItem) {
464         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
465         return nullptr;
466     }
467 
468     napi_value guid;
469     napi_status status = napi_create_string_utf8(env, webDownloadItem->guid.c_str(), NAPI_AUTO_LENGTH, &guid);
470     if (status != napi_ok) {
471         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetGuid failed");
472         return nullptr;
473     }
474     return guid;
475 }
476 
JS_GetFullPath(napi_env env,napi_callback_info cbinfo)477 napi_value NapiWebDownloadItem::JS_GetFullPath(napi_env env, napi_callback_info cbinfo)
478 {
479     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_GetFullPath");
480     size_t argc = 1;
481     napi_value argv[1] = {0};
482     napi_value thisVar = nullptr;
483     void *data = nullptr;
484     WebDownloadItem *webDownloadItem = nullptr;
485     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
486 
487     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
488 
489     if (!webDownloadItem) {
490         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
491         return nullptr;
492     }
493 
494     napi_value fullPath;
495     napi_status status = napi_create_string_utf8(env, webDownloadItem->fullPath.c_str(), NAPI_AUTO_LENGTH, &fullPath);
496     if (status != napi_ok) {
497         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_GetFullPath failed");
498         return nullptr;
499     }
500     return fullPath;
501 }
502 
JS_Continue(napi_env env,napi_callback_info cbinfo)503 napi_value NapiWebDownloadItem::JS_Continue(napi_env env, napi_callback_info cbinfo)
504 {
505     WVLOG_I("NapiWebDownloadItem::JS_Continue");
506     napi_value thisVar = nullptr;
507     void *data = nullptr;
508     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
509 
510     WebDownloadItem *webDownloadItem = nullptr;
511     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
512 
513     if (!webDownloadItem) {
514         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
515         return nullptr;
516     }
517 
518     WebDownload_Continue(webDownloadItem->before_download_callback, webDownloadItem->downloadPath.c_str());
519     return nullptr;
520 }
521 
JS_Cancel(napi_env env,napi_callback_info cbinfo)522 napi_value NapiWebDownloadItem::JS_Cancel(napi_env env, napi_callback_info cbinfo)
523 {
524     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_Cancel is called");
525     napi_value thisVar = nullptr;
526     void *data = nullptr;
527     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
528 
529     WebDownloadItem *webDownloadItem = nullptr;
530     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
531 
532     if (!webDownloadItem) {
533         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
534         return nullptr;
535     }
536     if (webDownloadItem->download_item_callback) {
537         WebDownload_Cancel(webDownloadItem->download_item_callback);
538     } else if (webDownloadItem->before_download_callback) {
539         WebDownload_CancelBeforeDownload(webDownloadItem->before_download_callback);
540     } else {
541         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_Cancel failed for callback nullptr");
542     }
543     return nullptr;
544 }
545 
JS_Pause(napi_env env,napi_callback_info cbinfo)546 napi_value NapiWebDownloadItem::JS_Pause(napi_env env, napi_callback_info cbinfo)
547 {
548     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_Pause is called");
549     napi_value thisVar = nullptr;
550     void *data = nullptr;
551     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
552 
553     WebDownloadItem *webDownloadItem = nullptr;
554     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
555 
556     if (!webDownloadItem) {
557         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
558         return nullptr;
559     }
560     NWebDownloadItemState state = WebDownload_GetItemStateByGuid(webDownloadItem->guid);
561     WVLOG_D("[DOWNLOAD] pause state %{public}d", static_cast<int>(state));
562     if (state != NWebDownloadItemState::IN_PROGRESS &&
563             state != NWebDownloadItemState::PENDING) {
564         BusinessError::ThrowErrorByErrcode(env, DOWNLOAD_NOT_START);
565         return nullptr;
566     }
567     if (webDownloadItem->download_item_callback) {
568         WebDownload_Pause(webDownloadItem->download_item_callback);
569     } else if (webDownloadItem->before_download_callback) {
570         WebDownload_PauseBeforeDownload(webDownloadItem->before_download_callback);
571     } else {
572         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_Pause failed for callback nullptr");
573     }
574     return nullptr;
575 }
576 
JS_Resume(napi_env env,napi_callback_info cbinfo)577 napi_value NapiWebDownloadItem::JS_Resume(napi_env env, napi_callback_info cbinfo)
578 {
579     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_Resume is called");
580     napi_value thisVar = nullptr;
581     void *data = nullptr;
582     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
583 
584     WebDownloadItem *webDownloadItem = nullptr;
585     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
586 
587     if (!webDownloadItem) {
588         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
589         return nullptr;
590     }
591 
592     NWebDownloadItemState state = WebDownload_GetItemStateByGuid(webDownloadItem->guid);
593     WVLOG_D("[DOWNLOAD] resume state %{public}d", static_cast<int>(state));
594     if (state != NWebDownloadItemState::PAUSED) {
595         BusinessError::ThrowErrorByErrcode(env, DOWNLOAD_NOT_PAUSED);
596         return nullptr;
597     }
598 
599     if (webDownloadItem->download_item_callback) {
600         WebDownload_Resume(webDownloadItem->download_item_callback);
601     } else if (webDownloadItem->before_download_callback) {
602         WebDownload_ResumeBeforeDownload(webDownloadItem->before_download_callback);
603     } else {
604         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_Resume failed for callback nullptr");
605     }
606     return nullptr;
607 }
608 
609 // static
JS_Constructor(napi_env env,napi_callback_info cbinfo)610 napi_value NapiWebDownloadItem::JS_Constructor(napi_env env, napi_callback_info cbinfo)
611 {
612     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::JS_Constructor is called");
613     napi_value thisVar = nullptr;
614     void *data = nullptr;
615     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
616 
617     WebDownloadItem *webDownloadItem = new WebDownloadItem(env);
618 
619     napi_wrap(
620         env, thisVar, webDownloadItem,
621         [](napi_env /* env */, void *data, void * /* hint */) {
622             WebDownloadItem *webDownloadItem = (WebDownloadItem *)data;
623             delete webDownloadItem;
624         },
625         nullptr, nullptr);
626 
627     return thisVar;
628 }
629 
JS_Start(napi_env env,napi_callback_info cbinfo)630 napi_value NapiWebDownloadItem::JS_Start(napi_env env, napi_callback_info cbinfo)
631 {
632     WVLOG_I("NapiWebDownloadItem::JS_Start");
633     size_t argc = 1;
634     napi_value argv[1] = {0};
635     napi_value thisVar = nullptr;
636     void *data = nullptr;
637     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
638 
639     napi_valuetype value_type = napi_undefined;
640     napi_typeof(env, argv[0], &value_type);
641 
642     size_t pathLen = 0;
643     napi_get_value_string_utf8(env, argv[0], nullptr, 0, &pathLen);
644     WebDownloadItem *webDownloadItem = nullptr;
645     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
646 
647     if (!webDownloadItem) {
648         WVLOG_E("[DOWNLOAD] unwrap webDownloadItem failed");
649         return nullptr;
650     }
651 
652     char stringValue[pathLen + 1];
653     size_t jsStringLength = 0;
654     napi_get_value_string_utf8(env, argv[0], stringValue, pathLen + 1, &jsStringLength);
655     if (jsStringLength != pathLen) {
656         BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
657             "BusinessError: 401. Parameter error. The type of 'downloadPath' must be a valid path string.");
658         return nullptr;
659     }
660     webDownloadItem->hasStarted = true;
661     webDownloadItem->downloadPath = std::string(stringValue);
662     WVLOG_D("NapiWebDownloadItem::JS_Start, download_path: %s", webDownloadItem->downloadPath.c_str());
663     WebDownload_Continue(webDownloadItem->before_download_callback, webDownloadItem->downloadPath.c_str());
664     return nullptr;
665 }
666 
SetWebDownloadPb(browser_service::WebDownload & webDownloadPb,const WebDownloadItem * webDownloadItem)667 void SetWebDownloadPb(browser_service::WebDownload &webDownloadPb, const WebDownloadItem *webDownloadItem)
668 {
669     webDownloadPb.set_web_download_id(webDownloadItem->webDownloadId);
670     webDownloadPb.set_current_speed(webDownloadItem->currentSpeed);
671     webDownloadPb.set_percent_complete(webDownloadItem->percentComplete);
672     webDownloadPb.set_total_bytes(webDownloadItem->totalBytes);
673     webDownloadPb.set_received_bytes(webDownloadItem->receivedBytes);
674     webDownloadPb.set_guid(webDownloadItem->guid);
675     webDownloadPb.set_full_path(webDownloadItem->fullPath);
676     webDownloadPb.set_url(webDownloadItem->url);
677     webDownloadPb.set_etag(webDownloadItem->etag);
678     webDownloadPb.set_original_url(webDownloadItem->originalUrl);
679     webDownloadPb.set_suggested_file_name(webDownloadItem->suggestedFileName);
680     webDownloadPb.set_content_disposition(webDownloadItem->contentDisposition);
681     webDownloadPb.set_mime_type(webDownloadItem->mimeType);
682     webDownloadPb.set_last_modified(webDownloadItem->lastModified);
683     webDownloadPb.set_state(static_cast<browser_service::WebDownload::WebDownloadState>(webDownloadItem->state));
684     webDownloadPb.set_method(webDownloadItem->method);
685     webDownloadPb.set_last_error_code(webDownloadItem->lastErrorCode);
686     webDownloadPb.set_received_slices(webDownloadItem->receivedSlices);
687     webDownloadPb.set_download_path(webDownloadItem->downloadPath);
688 }
689 
JS_Serialize(napi_env env,napi_callback_info cbinfo)690 napi_value NapiWebDownloadItem::JS_Serialize(napi_env env, napi_callback_info cbinfo)
691 {
692     napi_value thisVar = nullptr;
693     void *data = nullptr;
694     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
695 
696     WebDownloadItem *webDownloadItem = nullptr;
697     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
698     if (!webDownloadItem) {
699         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_Serialize webDownloadItem is null");
700         return nullptr;
701     }
702 
703     browser_service::WebDownload webDownloadPb;
704     SetWebDownloadPb(webDownloadPb, webDownloadItem);
705 
706     std::string webDownloadValue;
707     webDownloadPb.SerializeToString(&webDownloadValue);
708     napi_value arraybuffer;
709     void *bufferData = nullptr;
710 
711     napi_status status = napi_create_arraybuffer(env, webDownloadValue.length(), (void **)&bufferData, &arraybuffer);
712     if (status != napi_ok) {
713         WVLOG_E("[DOWNLOAD] create array buffer failed, status: %{public}d", status);
714         return nullptr;
715     }
716     if (memcpy_s(bufferData, webDownloadValue.length(), webDownloadValue.c_str(), webDownloadValue.length()) != 0) {
717         WVLOG_E("[DOWNLOAD] memcpy failed");
718         return nullptr;
719     }
720     napi_ref arraybufferRef;
721     napi_create_reference(env, arraybuffer, 1, &arraybufferRef);
722     napi_value typedArray;
723     status = napi_create_typedarray(env, napi_typedarray_type::napi_uint8_array, webDownloadValue.length(), arraybuffer,
724         0, &typedArray);
725     if (status != napi_ok) {
726         WVLOG_E("[DOWNLOAD] create typed array failed, status: %{public}d", status);
727         napi_delete_reference(env, arraybufferRef);
728         return nullptr;
729     }
730     return typedArray;
731 }
732 
JS_Deserialize(napi_env env,napi_callback_info cbinfo)733 napi_value NapiWebDownloadItem::JS_Deserialize(napi_env env, napi_callback_info cbinfo)
734 {
735     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::Deserialize");
736     size_t argc = 1;
737     napi_value argv[1] = {0};
738     napi_value thisVar = nullptr;
739     void *data = nullptr;
740     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
741     WVLOG_D("[DOWNLOAD] UnSerialize argc: %{public}d", int(argc));
742     napi_value arraybuffer;
743     size_t bufLen;
744 
745     void *buf;
746     napi_status status = napi_get_typedarray_info(env, argv[0], nullptr, &bufLen, &buf, &arraybuffer, nullptr);
747     if (status != napi_ok) {
748         BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
749             "BusinessError: 401. Parameter error. The type of 'serializedData' must be array.");
750         return nullptr;
751     }
752 
753     char *buffer = (char *)buf;
754     browser_service::WebDownload webDownloadPb;
755     bool result = webDownloadPb.ParseFromArray(buffer, bufLen);
756     if (!result) {
757         WVLOG_E("[DOWNLOAD] Unserialize webDownloadItem failed");
758         return nullptr;
759     }
760 
761     WebDownloadItem *webDownloadItem = new WebDownloadItem(env);
762     webDownloadItem->webDownloadId = webDownloadPb.web_download_id();
763     webDownloadItem->currentSpeed = webDownloadPb.current_speed();
764     webDownloadItem->percentComplete = webDownloadPb.percent_complete();
765     webDownloadItem->totalBytes = webDownloadPb.total_bytes();
766     webDownloadItem->receivedBytes = webDownloadPb.received_bytes();
767     webDownloadItem->guid = webDownloadPb.guid();
768     webDownloadItem->fullPath = webDownloadPb.full_path();
769     webDownloadItem->url = webDownloadPb.url();
770     webDownloadItem->etag = webDownloadPb.etag();
771     webDownloadItem->originalUrl = webDownloadPb.original_url();
772     webDownloadItem->suggestedFileName = webDownloadPb.suggested_file_name();
773     webDownloadItem->contentDisposition = webDownloadPb.content_disposition();
774     webDownloadItem->mimeType = webDownloadPb.mime_type();
775     webDownloadItem->lastModified = webDownloadPb.last_modified();
776     webDownloadItem->state = static_cast<NWebDownloadItemState>(webDownloadPb.state());
777     webDownloadItem->method = webDownloadPb.method();
778     webDownloadItem->lastErrorCode = webDownloadPb.last_error_code();
779     webDownloadItem->receivedSlices = webDownloadPb.received_slices();
780     webDownloadItem->downloadPath = webDownloadPb.download_path();
781 
782     napi_value webDownloadUnserialized;
783     napi_create_object(env, &webDownloadUnserialized);
784     napi_wrap(
785         env, webDownloadUnserialized, webDownloadItem,
786         [](napi_env /* env */, void *data, void * /* hint */) {
787             WebDownloadItem *download = (WebDownloadItem *)data;
788             delete download;
789         },
790         nullptr, nullptr);
791     DefineProperties(env, &webDownloadUnserialized);
792     return webDownloadUnserialized;
793 }
794 
Init(napi_env env,napi_value exports)795 napi_value NapiWebDownloadItem::Init(napi_env env, napi_value exports)
796 {
797     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::Init");
798     /* export WebDownloadItem class */
799 
800     ExportWebDownloadItemClass(env, &exports);
801 
802     /* export WebDownloadState enum */
803 
804     ExportWebDownloadStateEnum(env, &exports);
805 
806     /* export WebDownloadErrorCode enum */
807 
808     ExportWebDownloadErrorCodeEnum(env, &exports);
809 
810     return exports;
811 }
812 
ExportWebDownloadItemClass(napi_env env,napi_value * exportsPointer)813 void NapiWebDownloadItem::ExportWebDownloadItemClass(napi_env env, napi_value* exportsPointer)
814 {
815     napi_property_descriptor properties[] = {
816         DECLARE_NAPI_FUNCTION("getCurrentSpeed", JS_GetCurrentSpeed),
817         DECLARE_NAPI_FUNCTION("getPercentComplete", JS_GetPercentComplete),
818         DECLARE_NAPI_FUNCTION("getTotalBytes", JS_GetTotalBytes),
819         DECLARE_NAPI_FUNCTION("getState", JS_GetState),
820         DECLARE_NAPI_FUNCTION("getLastErrorCode", JS_GetLastErrorCode),
821         DECLARE_NAPI_FUNCTION("getMethod", JS_GetMethod),
822         DECLARE_NAPI_FUNCTION("getMimeType", JS_GetMimeType),
823         DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
824         DECLARE_NAPI_FUNCTION("getSuggestedFileName", JS_GetSuggestedFileName),
825         DECLARE_NAPI_FUNCTION("start", JS_Start),
826         DECLARE_NAPI_FUNCTION("continue", JS_Continue),
827         DECLARE_NAPI_FUNCTION("pause", JS_Pause),
828         DECLARE_NAPI_FUNCTION("cancel", JS_Cancel),
829         DECLARE_NAPI_FUNCTION("resume", JS_Resume),
830         DECLARE_NAPI_FUNCTION("getReceivedBytes", JS_GetReceivedBytes),
831         DECLARE_NAPI_FUNCTION("getFullPath", JS_GetFullPath),
832         DECLARE_NAPI_FUNCTION("getGuid", JS_GetGuid),
833         DECLARE_NAPI_FUNCTION("serialize", JS_Serialize),
834         {"deserialize", nullptr, JS_Deserialize, nullptr, nullptr, nullptr,
835          napi_static, nullptr},
836 
837     };
838     napi_value webDownloadClass = nullptr;
839     napi_define_class(env, WEB_DOWNLOAD_ITEMT.c_str(), WEB_DOWNLOAD_ITEMT.length(), JS_Constructor, nullptr,
840         sizeof(properties) / sizeof(properties[0]), properties, &webDownloadClass);
841     napi_set_named_property(env, *exportsPointer, WEB_DOWNLOAD_ITEMT.c_str(), webDownloadClass);
842 }
843 
ExportWebDownloadStateEnum(napi_env env,napi_value * exportsPointer)844 void NapiWebDownloadItem::ExportWebDownloadStateEnum(napi_env env, napi_value* exportsPointer)
845 {
846     napi_value webDownloadStateTypeEnum = nullptr;
847     napi_property_descriptor webDownloadStateProperties[] = {
848         DECLARE_NAPI_STATIC_PROPERTY(
849             "IN_PROGRESS",
850             ToInt32Value(
851                 env, static_cast<int32_t>(NWebDownloadItemState::IN_PROGRESS))),
852         DECLARE_NAPI_STATIC_PROPERTY(
853             "COMPLETED",
854             ToInt32Value(
855                 env, static_cast<int32_t>(NWebDownloadItemState::COMPLETE))),
856         DECLARE_NAPI_STATIC_PROPERTY(
857             "CANCELED",
858             ToInt32Value(
859                 env, static_cast<int32_t>(NWebDownloadItemState::CANCELED))),
860         DECLARE_NAPI_STATIC_PROPERTY(
861             "INTERRUPTED",
862             ToInt32Value(
863                 env, static_cast<int32_t>(NWebDownloadItemState::INTERRUPTED))),
864         DECLARE_NAPI_STATIC_PROPERTY(
865             "PENDING",
866             ToInt32Value(
867                 env, static_cast<int32_t>(NWebDownloadItemState::PENDING))),
868         DECLARE_NAPI_STATIC_PROPERTY(
869             "PAUSED",
870             ToInt32Value(
871                 env, static_cast<int32_t>(NWebDownloadItemState::PAUSED))),
872         DECLARE_NAPI_STATIC_PROPERTY(
873             "UNKNOWN", ToInt32Value(
874                 env, static_cast<int32_t>(
875                 NWebDownloadItemState::MAX_DOWNLOAD_STATE))),
876     };
877     napi_define_class(env, WEB_DOWNLOAD_STATE_ENUM_NAME.c_str(), WEB_DOWNLOAD_STATE_ENUM_NAME.length(),
878         CreateEnumConstructor, nullptr, sizeof(webDownloadStateProperties) / sizeof(webDownloadStateProperties[0]),
879         webDownloadStateProperties, &webDownloadStateTypeEnum);
880     napi_set_named_property(env, *exportsPointer, WEB_DOWNLOAD_STATE_ENUM_NAME.c_str(), webDownloadStateTypeEnum);
881 }
882 
ExportWebDownloadErrorCodeEnum(napi_env env,napi_value * exportsPointer)883 void NapiWebDownloadItem::ExportWebDownloadErrorCodeEnum(napi_env env, napi_value* exportsPointer)
884 {
885     napi_value webDownloadErrorCodeEnum = nullptr;
886     napi_property_descriptor webDownloadErrorCodeEnumProperties[] = {
887         DECLARE_NAPI_STATIC_PROPERTY(
888             "ERROR_UNKNOWN",
889             ToInt32Value(
890                 env,
891                 static_cast<int32_t>(download::DOWNLOAD_INTERRUPT_REASON_NONE))),
892         DECLARE_NAPI_STATIC_PROPERTY(
893             "FILE_FAILED",
894             ToInt32Value(
895                 env, static_cast<int32_t>(
896                         download::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED))),
897         DECLARE_NAPI_STATIC_PROPERTY(
898             "FILE_ACCESS_DENIED",
899             ToInt32Value(
900                 env,
901                 static_cast<int32_t>(
902                     download::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED))),
903         DECLARE_NAPI_STATIC_PROPERTY(
904             "FILE_NO_SPACE",
905             ToInt32Value(
906                 env, static_cast<int32_t>(
907                         download::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE))),
908         DECLARE_NAPI_STATIC_PROPERTY(
909             "FILE_NAME_TOO_LONG",
910             ToInt32Value(
911                 env,
912                 static_cast<int32_t>(
913                     download::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG))),
914         DECLARE_NAPI_STATIC_PROPERTY(
915             "FILE_TOO_LARGE",
916             ToInt32Value(
917                 env, static_cast<int32_t>(
918                         download::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE))),
919         DECLARE_NAPI_STATIC_PROPERTY(
920             "FILE_VIRUS_INFECTED",
921             ToInt32Value(
922                 env,
923                 static_cast<int32_t>(
924                     download::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED))),
925         DECLARE_NAPI_STATIC_PROPERTY(
926             "FILE_TRANSIENT_ERROR",
927             ToInt32Value(
928                 env,
929                 static_cast<int32_t>(
930                     download::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR))),
931         DECLARE_NAPI_STATIC_PROPERTY(
932             "FILE_BLOCKED",
933             ToInt32Value(
934                 env, static_cast<int32_t>(
935                         download::DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED))),
936         DECLARE_NAPI_STATIC_PROPERTY(
937             "FILE_SECURITY_CHECK_FAILED",
938             ToInt32Value(
939                 env,
940                 static_cast<int32_t>(
941                     download::
942                         DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED))),
943         DECLARE_NAPI_STATIC_PROPERTY(
944             "FILE_TOO_SHORT",
945             ToInt32Value(
946                 env, static_cast<int32_t>(
947                         download::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT))),
948         DECLARE_NAPI_STATIC_PROPERTY(
949             "FILE_HASH_MISMATCH",
950             ToInt32Value(
951                 env,
952                 static_cast<int32_t>(
953                     download::DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH))),
954         DECLARE_NAPI_STATIC_PROPERTY(
955             "FILE_SAME_AS_SOURCE",
956             ToInt32Value(
957                 env,
958                 static_cast<int32_t>(
959                     download::DOWNLOAD_INTERRUPT_REASON_FILE_SAME_AS_SOURCE))),
960 
961         DECLARE_NAPI_STATIC_PROPERTY(
962             "NETWORK_FAILED",
963             ToInt32Value(
964                 env, static_cast<int32_t>(
965                         download::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED))),
966         DECLARE_NAPI_STATIC_PROPERTY(
967             "NETWORK_TIMEOUT",
968             ToInt32Value(
969                 env, static_cast<int32_t>(
970                         download::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT))),
971         DECLARE_NAPI_STATIC_PROPERTY(
972             "NETWORK_DISCONNECTED",
973             ToInt32Value(
974                 env,
975                 static_cast<int32_t>(
976                     download::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))),
977         DECLARE_NAPI_STATIC_PROPERTY(
978             "NETWORK_SERVER_DOWN",
979             ToInt32Value(
980                 env,
981                 static_cast<int32_t>(
982                     download::DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN))),
983         DECLARE_NAPI_STATIC_PROPERTY(
984             "NETWORK_INVALID_REQUEST",
985             ToInt32Value(
986                 env, static_cast<int32_t>(
987                         download::
988                             DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST))),
989         DECLARE_NAPI_STATIC_PROPERTY(
990             "SERVER_FAILED",
991             ToInt32Value(
992                 env, static_cast<int32_t>(
993                         download::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED))),
994         DECLARE_NAPI_STATIC_PROPERTY(
995             "SERVER_NO_RANGE",
996             ToInt32Value(
997                 env, static_cast<int32_t>(
998                         download::DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE))),
999         DECLARE_NAPI_STATIC_PROPERTY(
1000             "SERVER_BAD_CONTENT",
1001             ToInt32Value(
1002                 env,
1003                 static_cast<int32_t>(
1004                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT))),
1005         DECLARE_NAPI_STATIC_PROPERTY(
1006             "SERVER_UNAUTHORIZED",
1007             ToInt32Value(
1008                 env,
1009                 static_cast<int32_t>(
1010                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED))),
1011         DECLARE_NAPI_STATIC_PROPERTY(
1012             "SERVER_CERT_PROBLEM",
1013             ToInt32Value(
1014                 env,
1015                 static_cast<int32_t>(
1016                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM))),
1017         DECLARE_NAPI_STATIC_PROPERTY(
1018             "SERVER_FORBIDDEN",
1019             ToInt32Value(
1020                 env, static_cast<int32_t>(
1021                         download::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN))),
1022         DECLARE_NAPI_STATIC_PROPERTY(
1023             "SERVER_UNREACHABLE",
1024             ToInt32Value(
1025                 env,
1026                 static_cast<int32_t>(
1027                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE))),
1028         DECLARE_NAPI_STATIC_PROPERTY(
1029             "SERVER_CONTENT_LENGTH_MISMATCH",
1030             ToInt32Value(
1031                 env,
1032                 static_cast<int32_t>(
1033                     download::
1034                         DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH))),
1035         DECLARE_NAPI_STATIC_PROPERTY(
1036             "SERVER_CROSS_ORIGIN_REDIRECT",
1037             ToInt32Value(
1038                 env,
1039                 static_cast<int32_t>(
1040                     download::
1041                         DOWNLOAD_INTERRUPT_REASON_SERVER_CROSS_ORIGIN_REDIRECT))),
1042 
1043         DECLARE_NAPI_STATIC_PROPERTY(
1044             "USER_CANCELED",
1045             ToInt32Value(
1046                 env, static_cast<int32_t>(
1047                         download::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED))),
1048         DECLARE_NAPI_STATIC_PROPERTY(
1049             "USER_SHUTDOWN",
1050             ToInt32Value(
1051                 env, static_cast<int32_t>(
1052                         download::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN))),
1053         DECLARE_NAPI_STATIC_PROPERTY(
1054             "CRASH", ToInt32Value(
1055                 env, static_cast<int32_t>(
1056                         download::DOWNLOAD_INTERRUPT_REASON_CRASH))),
1057     };
1058 
1059     napi_define_class(env, WEB_DOWNLOAD_ERROR_CODE_ENUM_NAME.c_str(), WEB_DOWNLOAD_ERROR_CODE_ENUM_NAME.length(),
1060         CreateEnumConstructor, nullptr,
1061         sizeof(webDownloadErrorCodeEnumProperties) / sizeof(webDownloadErrorCodeEnumProperties[0]),
1062         webDownloadErrorCodeEnumProperties, &webDownloadErrorCodeEnum);
1063     napi_set_named_property(env, *exportsPointer, WEB_DOWNLOAD_ERROR_CODE_ENUM_NAME.c_str(), webDownloadErrorCodeEnum);
1064 }
1065 
DefineProperties(napi_env env,napi_value * object)1066 napi_status NapiWebDownloadItem::DefineProperties(napi_env env, napi_value *object)
1067 {
1068     napi_property_descriptor properties[] = {
1069         DECLARE_NAPI_FUNCTION("getCurrentSpeed", JS_GetCurrentSpeed),
1070         DECLARE_NAPI_FUNCTION("getPercentComplete", JS_GetPercentComplete),
1071         DECLARE_NAPI_FUNCTION("getTotalBytes", JS_GetTotalBytes),
1072         DECLARE_NAPI_FUNCTION("getState", JS_GetState),
1073         DECLARE_NAPI_FUNCTION("getLastErrorCode", JS_GetLastErrorCode),
1074         DECLARE_NAPI_FUNCTION("getMethod", JS_GetMethod),
1075         DECLARE_NAPI_FUNCTION("getMimeType", JS_GetMimeType),
1076         DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
1077         DECLARE_NAPI_FUNCTION("getSuggestedFileName", JS_GetSuggestedFileName),
1078         DECLARE_NAPI_FUNCTION("continue", JS_Continue),
1079         DECLARE_NAPI_FUNCTION("start", JS_Start),
1080         DECLARE_NAPI_FUNCTION("pause", JS_Pause),
1081         DECLARE_NAPI_FUNCTION("cancel", JS_Cancel),
1082         DECLARE_NAPI_FUNCTION("resume", JS_Resume),
1083         DECLARE_NAPI_FUNCTION("getReceivedBytes", JS_GetReceivedBytes),
1084         DECLARE_NAPI_FUNCTION("getFullPath", JS_GetFullPath),
1085         DECLARE_NAPI_FUNCTION("getGuid", JS_GetGuid),
1086         DECLARE_NAPI_FUNCTION("serialize", JS_Serialize),
1087         {"deserialize", nullptr, JS_Deserialize, nullptr, nullptr, nullptr,
1088          napi_static, nullptr},
1089     };
1090     return napi_define_properties(env, *object, sizeof(properties) / sizeof(properties[0]), properties);
1091 }
1092 } // namespace NWeb
1093 } // namespace OHOS
1094