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->downloadPath = std::string(stringValue);
661     WVLOG_D("NapiWebDownloadItem::JS_Start, download_path: %s", webDownloadItem->downloadPath.c_str());
662     WebDownload_Continue(webDownloadItem->before_download_callback, webDownloadItem->downloadPath.c_str());
663     return nullptr;
664 }
665 
SetWebDownloadPb(browser_service::WebDownload &webDownloadPb, const WebDownloadItem *webDownloadItem)666 void SetWebDownloadPb(browser_service::WebDownload &webDownloadPb, const WebDownloadItem *webDownloadItem)
667 {
668     webDownloadPb.set_web_download_id(webDownloadItem->webDownloadId);
669     webDownloadPb.set_current_speed(webDownloadItem->currentSpeed);
670     webDownloadPb.set_percent_complete(webDownloadItem->percentComplete);
671     webDownloadPb.set_total_bytes(webDownloadItem->totalBytes);
672     webDownloadPb.set_received_bytes(webDownloadItem->receivedBytes);
673     webDownloadPb.set_guid(webDownloadItem->guid);
674     webDownloadPb.set_full_path(webDownloadItem->fullPath);
675     webDownloadPb.set_url(webDownloadItem->url);
676     webDownloadPb.set_etag(webDownloadItem->etag);
677     webDownloadPb.set_original_url(webDownloadItem->originalUrl);
678     webDownloadPb.set_suggested_file_name(webDownloadItem->suggestedFileName);
679     webDownloadPb.set_content_disposition(webDownloadItem->contentDisposition);
680     webDownloadPb.set_mime_type(webDownloadItem->mimeType);
681     webDownloadPb.set_last_modified(webDownloadItem->lastModified);
682     webDownloadPb.set_state(static_cast<browser_service::WebDownload::WebDownloadState>(webDownloadItem->state));
683     webDownloadPb.set_method(webDownloadItem->method);
684     webDownloadPb.set_last_error_code(webDownloadItem->lastErrorCode);
685     webDownloadPb.set_received_slices(webDownloadItem->receivedSlices);
686     webDownloadPb.set_download_path(webDownloadItem->downloadPath);
687 }
688 
JS_Serialize(napi_env env, napi_callback_info cbinfo)689 napi_value NapiWebDownloadItem::JS_Serialize(napi_env env, napi_callback_info cbinfo)
690 {
691     napi_value thisVar = nullptr;
692     void *data = nullptr;
693     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
694 
695     WebDownloadItem *webDownloadItem = nullptr;
696     napi_unwrap(env, thisVar, (void **)&webDownloadItem);
697     if(!webDownloadItem) {
698         WVLOG_E("[DOWNLOAD] NapiWebDownloadItem::JS_Serialize webDownloadItem is null");
699         return nullptr;
700     }
701 
702     browser_service::WebDownload webDownloadPb;
703     SetWebDownloadPb(webDownloadPb, webDownloadItem);
704 
705     std::string webDownloadValue;
706     webDownloadPb.SerializeToString(&webDownloadValue);
707     napi_value arraybuffer;
708     void *bufferData = nullptr;
709 
710     napi_status status = napi_create_arraybuffer(env, webDownloadValue.length(), (void **)&bufferData, &arraybuffer);
711     if (status != napi_ok) {
712         WVLOG_E("[DOWNLOAD] create array buffer failed, status: %{public}d", status);
713         return nullptr;
714     }
715     if (memcpy_s(bufferData, webDownloadValue.length(), webDownloadValue.c_str(), webDownloadValue.length()) != 0) {
716         WVLOG_E("[DOWNLOAD] memcpy failed");
717         return nullptr;
718     }
719     napi_ref arraybufferRef;
720     napi_create_reference(env, arraybuffer, 1, &arraybufferRef);
721     napi_value typedArray;
722     status = napi_create_typedarray(env, napi_typedarray_type::napi_uint8_array, webDownloadValue.length(), arraybuffer,
723         0, &typedArray);
724     if (status != napi_ok) {
725         WVLOG_E("[DOWNLOAD] create typed array failed, status: %{public}d", status);
726         napi_delete_reference(env, arraybufferRef);
727         return nullptr;
728     }
729     return typedArray;
730 }
731 
JS_Deserialize(napi_env env, napi_callback_info cbinfo)732 napi_value NapiWebDownloadItem::JS_Deserialize(napi_env env, napi_callback_info cbinfo)
733 {
734     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::Deserialize");
735     size_t argc = 1;
736     napi_value argv[1] = {0};
737     napi_value thisVar = nullptr;
738     void *data = nullptr;
739     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
740     WVLOG_D("[DOWNLOAD] UnSerialize argc: %{public}d", int(argc));
741     napi_value arraybuffer;
742     size_t bufLen;
743 
744     void *buf;
745     napi_status status = napi_get_typedarray_info(env, argv[0], nullptr, &bufLen, &buf, &arraybuffer, nullptr);
746     if (status != napi_ok) {
747         BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR,
748                 "BusinessError: 401. Parameter error. The type of 'serializedData' must be array.");
749         return nullptr;
750     }
751 
752     char *buffer = (char *)buf;
753     browser_service::WebDownload webDownloadPb;
754     bool result = webDownloadPb.ParseFromArray(buffer, bufLen);
755     if (!result) {
756         WVLOG_E("[DOWNLOAD] Unserialize webDownloadItem failed");
757         return nullptr;
758     }
759 
760     WebDownloadItem *webDownloadItem = new WebDownloadItem(env);
761     webDownloadItem->webDownloadId = webDownloadPb.web_download_id();
762     webDownloadItem->currentSpeed = webDownloadPb.current_speed();
763     webDownloadItem->percentComplete = webDownloadPb.percent_complete();
764     webDownloadItem->totalBytes = webDownloadPb.total_bytes();
765     webDownloadItem->receivedBytes = webDownloadPb.received_bytes();
766     webDownloadItem->guid = webDownloadPb.guid();
767     webDownloadItem->fullPath = webDownloadPb.full_path();
768     webDownloadItem->url = webDownloadPb.url();
769     webDownloadItem->etag = webDownloadPb.etag();
770     webDownloadItem->originalUrl = webDownloadPb.original_url();
771     webDownloadItem->suggestedFileName = webDownloadPb.suggested_file_name();
772     webDownloadItem->contentDisposition = webDownloadPb.content_disposition();
773     webDownloadItem->mimeType = webDownloadPb.mime_type();
774     webDownloadItem->lastModified = webDownloadPb.last_modified();
775     webDownloadItem->state = static_cast<NWebDownloadItemState>(webDownloadPb.state());
776     webDownloadItem->method = webDownloadPb.method();
777     webDownloadItem->lastErrorCode = webDownloadPb.last_error_code();
778     webDownloadItem->receivedSlices = webDownloadPb.received_slices();
779     webDownloadItem->downloadPath = webDownloadPb.download_path();
780 
781     napi_value webDownloadUnserialized;
782     napi_create_object(env, &webDownloadUnserialized);
783     napi_wrap(
784         env, webDownloadUnserialized, webDownloadItem,
785         [](napi_env /* env */, void *data, void * /* hint */) {
786             WebDownloadItem *download = (WebDownloadItem *)data;
787             delete download;
788         },
789         nullptr, nullptr);
790     DefineProperties(env, &webDownloadUnserialized);
791     return webDownloadUnserialized;
792 }
793 
Init(napi_env env, napi_value exports)794 napi_value NapiWebDownloadItem::Init(napi_env env, napi_value exports)
795 {
796     WVLOG_D("[DOWNLOAD] NapiWebDownloadItem::Init");
797     /* export WebDownloadItem class */
798 
799     ExportWebDownloadItemClass(env, &exports);
800 
801     /* export WebDownloadState enum */
802 
803     ExportWebDownloadStateEnum(env, &exports);
804 
805     /* export WebDownloadErrorCode enum */
806 
807     ExportWebDownloadErrorCodeEnum(env, &exports);
808 
809     return exports;
810 }
811 
ExportWebDownloadItemClass(napi_env env, napi_value* exportsPointer)812 void NapiWebDownloadItem::ExportWebDownloadItemClass(napi_env env, napi_value* exportsPointer)
813 {
814     napi_property_descriptor properties[] = {
815         DECLARE_NAPI_FUNCTION("getCurrentSpeed", JS_GetCurrentSpeed),
816         DECLARE_NAPI_FUNCTION("getPercentComplete", JS_GetPercentComplete),
817         DECLARE_NAPI_FUNCTION("getTotalBytes", JS_GetTotalBytes),
818         DECLARE_NAPI_FUNCTION("getState", JS_GetState),
819         DECLARE_NAPI_FUNCTION("getLastErrorCode", JS_GetLastErrorCode),
820         DECLARE_NAPI_FUNCTION("getMethod", JS_GetMethod),
821         DECLARE_NAPI_FUNCTION("getMimeType", JS_GetMimeType),
822         DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
823         DECLARE_NAPI_FUNCTION("getSuggestedFileName", JS_GetSuggestedFileName),
824         DECLARE_NAPI_FUNCTION("start", JS_Start),
825         DECLARE_NAPI_FUNCTION("continue", JS_Continue),
826         DECLARE_NAPI_FUNCTION("pause", JS_Pause),
827         DECLARE_NAPI_FUNCTION("cancel", JS_Cancel),
828         DECLARE_NAPI_FUNCTION("resume", JS_Resume),
829         DECLARE_NAPI_FUNCTION("getReceivedBytes", JS_GetReceivedBytes),
830         DECLARE_NAPI_FUNCTION("getFullPath", JS_GetFullPath),
831         DECLARE_NAPI_FUNCTION("getGuid", JS_GetGuid),
832         DECLARE_NAPI_FUNCTION("serialize", JS_Serialize),
833         {"deserialize", nullptr, JS_Deserialize, nullptr, nullptr, nullptr,
834          napi_static, nullptr},
835 
836     };
837     napi_value webDownloadClass = nullptr;
838     napi_define_class(env, WEB_DOWNLOAD_ITEMT.c_str(), WEB_DOWNLOAD_ITEMT.length(), JS_Constructor, nullptr,
839         sizeof(properties) / sizeof(properties[0]), properties, &webDownloadClass);
840     napi_set_named_property(env, *exportsPointer, WEB_DOWNLOAD_ITEMT.c_str(), webDownloadClass);
841 }
842 
ExportWebDownloadStateEnum(napi_env env, napi_value* exportsPointer)843 void NapiWebDownloadItem::ExportWebDownloadStateEnum(napi_env env, napi_value* exportsPointer)
844 {
845     napi_value webDownloadStateTypeEnum = nullptr;
846     napi_property_descriptor webDownloadStateProperties[] = {
847         DECLARE_NAPI_STATIC_PROPERTY(
848             "IN_PROGRESS",
849             ToInt32Value(
850                 env, static_cast<int32_t>(NWebDownloadItemState::IN_PROGRESS))),
851         DECLARE_NAPI_STATIC_PROPERTY(
852             "COMPLETED",
853             ToInt32Value(
854                 env, static_cast<int32_t>(NWebDownloadItemState::COMPLETE))),
855         DECLARE_NAPI_STATIC_PROPERTY(
856             "CANCELED",
857             ToInt32Value(
858                 env, static_cast<int32_t>(NWebDownloadItemState::CANCELED))),
859         DECLARE_NAPI_STATIC_PROPERTY(
860             "INTERRUPTED",
861             ToInt32Value(
862                 env, static_cast<int32_t>(NWebDownloadItemState::INTERRUPTED))),
863         DECLARE_NAPI_STATIC_PROPERTY(
864             "PENDING",
865             ToInt32Value(
866                 env, static_cast<int32_t>(NWebDownloadItemState::PENDING))),
867         DECLARE_NAPI_STATIC_PROPERTY(
868             "PAUSED",
869             ToInt32Value(
870                 env, static_cast<int32_t>(NWebDownloadItemState::PAUSED))),
871         DECLARE_NAPI_STATIC_PROPERTY(
872             "UNKNOWN", ToInt32Value(
873                 env, static_cast<int32_t>(
874                 NWebDownloadItemState::MAX_DOWNLOAD_STATE))),
875     };
876     napi_define_class(env, WEB_DOWNLOAD_STATE_ENUM_NAME.c_str(), WEB_DOWNLOAD_STATE_ENUM_NAME.length(),
877         CreateEnumConstructor, nullptr, sizeof(webDownloadStateProperties) / sizeof(webDownloadStateProperties[0]),
878         webDownloadStateProperties, &webDownloadStateTypeEnum);
879     napi_set_named_property(env, *exportsPointer, WEB_DOWNLOAD_STATE_ENUM_NAME.c_str(), webDownloadStateTypeEnum);
880 }
881 
ExportWebDownloadErrorCodeEnum(napi_env env, napi_value* exportsPointer)882 void NapiWebDownloadItem::ExportWebDownloadErrorCodeEnum(napi_env env, napi_value* exportsPointer)
883 {
884     napi_value webDownloadErrorCodeEnum = nullptr;
885     napi_property_descriptor webDownloadErrorCodeEnumProperties[] = {
886         DECLARE_NAPI_STATIC_PROPERTY(
887             "ERROR_UNKNOWN",
888             ToInt32Value(
889                 env,
890                 static_cast<int32_t>(download::DOWNLOAD_INTERRUPT_REASON_NONE))),
891         DECLARE_NAPI_STATIC_PROPERTY(
892             "FILE_FAILED",
893             ToInt32Value(
894                 env, static_cast<int32_t>(
895                         download::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED))),
896         DECLARE_NAPI_STATIC_PROPERTY(
897             "FILE_ACCESS_DENIED",
898             ToInt32Value(
899                 env,
900                 static_cast<int32_t>(
901                     download::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED))),
902         DECLARE_NAPI_STATIC_PROPERTY(
903             "FILE_NO_SPACE",
904             ToInt32Value(
905                 env, static_cast<int32_t>(
906                         download::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE))),
907         DECLARE_NAPI_STATIC_PROPERTY(
908             "FILE_NAME_TOO_LONG",
909             ToInt32Value(
910                 env,
911                 static_cast<int32_t>(
912                     download::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG))),
913         DECLARE_NAPI_STATIC_PROPERTY(
914             "FILE_TOO_LARGE",
915             ToInt32Value(
916                 env, static_cast<int32_t>(
917                         download::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE))),
918         DECLARE_NAPI_STATIC_PROPERTY(
919             "FILE_VIRUS_INFECTED",
920             ToInt32Value(
921                 env,
922                 static_cast<int32_t>(
923                     download::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED))),
924         DECLARE_NAPI_STATIC_PROPERTY(
925             "FILE_TRANSIENT_ERROR",
926             ToInt32Value(
927                 env,
928                 static_cast<int32_t>(
929                     download::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR))),
930         DECLARE_NAPI_STATIC_PROPERTY(
931             "FILE_BLOCKED",
932             ToInt32Value(
933                 env, static_cast<int32_t>(
934                         download::DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED))),
935         DECLARE_NAPI_STATIC_PROPERTY(
936             "FILE_SECURITY_CHECK_FAILED",
937             ToInt32Value(
938                 env,
939                 static_cast<int32_t>(
940                     download::
941                         DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED))),
942         DECLARE_NAPI_STATIC_PROPERTY(
943             "FILE_TOO_SHORT",
944             ToInt32Value(
945                 env, static_cast<int32_t>(
946                         download::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT))),
947         DECLARE_NAPI_STATIC_PROPERTY(
948             "FILE_HASH_MISMATCH",
949             ToInt32Value(
950                 env,
951                 static_cast<int32_t>(
952                     download::DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH))),
953         DECLARE_NAPI_STATIC_PROPERTY(
954             "FILE_SAME_AS_SOURCE",
955             ToInt32Value(
956                 env,
957                 static_cast<int32_t>(
958                     download::DOWNLOAD_INTERRUPT_REASON_FILE_SAME_AS_SOURCE))),
959 
960         DECLARE_NAPI_STATIC_PROPERTY(
961             "NETWORK_FAILED",
962             ToInt32Value(
963                 env, static_cast<int32_t>(
964                         download::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED))),
965         DECLARE_NAPI_STATIC_PROPERTY(
966             "NETWORK_TIMEOUT",
967             ToInt32Value(
968                 env, static_cast<int32_t>(
969                         download::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT))),
970         DECLARE_NAPI_STATIC_PROPERTY(
971             "NETWORK_DISCONNECTED",
972             ToInt32Value(
973                 env,
974                 static_cast<int32_t>(
975                     download::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED))),
976         DECLARE_NAPI_STATIC_PROPERTY(
977             "NETWORK_SERVER_DOWN",
978             ToInt32Value(
979                 env,
980                 static_cast<int32_t>(
981                     download::DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN))),
982         DECLARE_NAPI_STATIC_PROPERTY(
983             "NETWORK_INVALID_REQUEST",
984             ToInt32Value(
985                 env, static_cast<int32_t>(
986                         download::
987                             DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST))),
988         DECLARE_NAPI_STATIC_PROPERTY(
989             "SERVER_FAILED",
990             ToInt32Value(
991                 env, static_cast<int32_t>(
992                         download::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED))),
993         DECLARE_NAPI_STATIC_PROPERTY(
994             "SERVER_NO_RANGE",
995             ToInt32Value(
996                 env, static_cast<int32_t>(
997                         download::DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE))),
998         DECLARE_NAPI_STATIC_PROPERTY(
999             "SERVER_BAD_CONTENT",
1000             ToInt32Value(
1001                 env,
1002                 static_cast<int32_t>(
1003                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT))),
1004         DECLARE_NAPI_STATIC_PROPERTY(
1005             "SERVER_UNAUTHORIZED",
1006             ToInt32Value(
1007                 env,
1008                 static_cast<int32_t>(
1009                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED))),
1010         DECLARE_NAPI_STATIC_PROPERTY(
1011             "SERVER_CERT_PROBLEM",
1012             ToInt32Value(
1013                 env,
1014                 static_cast<int32_t>(
1015                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM))),
1016         DECLARE_NAPI_STATIC_PROPERTY(
1017             "SERVER_FORBIDDEN",
1018             ToInt32Value(
1019                 env, static_cast<int32_t>(
1020                         download::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN))),
1021         DECLARE_NAPI_STATIC_PROPERTY(
1022             "SERVER_UNREACHABLE",
1023             ToInt32Value(
1024                 env,
1025                 static_cast<int32_t>(
1026                     download::DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE))),
1027         DECLARE_NAPI_STATIC_PROPERTY(
1028             "SERVER_CONTENT_LENGTH_MISMATCH",
1029             ToInt32Value(
1030                 env,
1031                 static_cast<int32_t>(
1032                     download::
1033                         DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH))),
1034         DECLARE_NAPI_STATIC_PROPERTY(
1035             "SERVER_CROSS_ORIGIN_REDIRECT",
1036             ToInt32Value(
1037                 env,
1038                 static_cast<int32_t>(
1039                     download::
1040                         DOWNLOAD_INTERRUPT_REASON_SERVER_CROSS_ORIGIN_REDIRECT))),
1041 
1042         DECLARE_NAPI_STATIC_PROPERTY(
1043             "USER_CANCELED",
1044             ToInt32Value(
1045                 env, static_cast<int32_t>(
1046                         download::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED))),
1047         DECLARE_NAPI_STATIC_PROPERTY(
1048             "USER_SHUTDOWN",
1049             ToInt32Value(
1050                 env, static_cast<int32_t>(
1051                         download::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN))),
1052         DECLARE_NAPI_STATIC_PROPERTY(
1053             "CRASH", ToInt32Value(
1054                 env, static_cast<int32_t>(
1055                         download::DOWNLOAD_INTERRUPT_REASON_CRASH))),
1056     };
1057 
1058     napi_define_class(env, WEB_DOWNLOAD_ERROR_CODE_ENUM_NAME.c_str(), WEB_DOWNLOAD_ERROR_CODE_ENUM_NAME.length(),
1059         CreateEnumConstructor, nullptr,
1060         sizeof(webDownloadErrorCodeEnumProperties) / sizeof(webDownloadErrorCodeEnumProperties[0]),
1061         webDownloadErrorCodeEnumProperties, &webDownloadErrorCodeEnum);
1062     napi_set_named_property(env, *exportsPointer, WEB_DOWNLOAD_ERROR_CODE_ENUM_NAME.c_str(), webDownloadErrorCodeEnum);
1063 }
1064 
DefineProperties(napi_env env, napi_value *object)1065 napi_status NapiWebDownloadItem::DefineProperties(napi_env env, napi_value *object)
1066 {
1067     napi_property_descriptor properties[] = {
1068         DECLARE_NAPI_FUNCTION("getCurrentSpeed", JS_GetCurrentSpeed),
1069         DECLARE_NAPI_FUNCTION("getPercentComplete", JS_GetPercentComplete),
1070         DECLARE_NAPI_FUNCTION("getTotalBytes", JS_GetTotalBytes),
1071         DECLARE_NAPI_FUNCTION("getState", JS_GetState),
1072         DECLARE_NAPI_FUNCTION("getLastErrorCode", JS_GetLastErrorCode),
1073         DECLARE_NAPI_FUNCTION("getMethod", JS_GetMethod),
1074         DECLARE_NAPI_FUNCTION("getMimeType", JS_GetMimeType),
1075         DECLARE_NAPI_FUNCTION("getUrl", JS_GetUrl),
1076         DECLARE_NAPI_FUNCTION("getSuggestedFileName", JS_GetSuggestedFileName),
1077         DECLARE_NAPI_FUNCTION("continue", JS_Continue),
1078         DECLARE_NAPI_FUNCTION("start", JS_Start),
1079         DECLARE_NAPI_FUNCTION("pause", JS_Pause),
1080         DECLARE_NAPI_FUNCTION("cancel", JS_Cancel),
1081         DECLARE_NAPI_FUNCTION("resume", JS_Resume),
1082         DECLARE_NAPI_FUNCTION("getReceivedBytes", JS_GetReceivedBytes),
1083         DECLARE_NAPI_FUNCTION("getFullPath", JS_GetFullPath),
1084         DECLARE_NAPI_FUNCTION("getGuid", JS_GetGuid),
1085         DECLARE_NAPI_FUNCTION("serialize", JS_Serialize),
1086         {"deserialize", nullptr, JS_Deserialize, nullptr, nullptr, nullptr,
1087          napi_static, nullptr},
1088     };
1089     return napi_define_properties(env, *object, sizeof(properties) / sizeof(properties[0]), properties);
1090 }
1091 } // namespace NWeb
1092 } // namespace OHOS
1093