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