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 "web_download_item.h"
17 
18 #include <string>
19 
20 #include "nweb_log.h"
21 
22 namespace OHOS {
23 namespace NWeb {
WebDownloadItem(napi_env env)24 WebDownloadItem::WebDownloadItem(napi_env env)
25     : guid(""),
26       fullPath(""),
27       url(""),
28       etag(""),
29       originalUrl(""),
30       suggestedFileName(""),
31       contentDisposition(""),
32       mimeType(""),
33       lastModified(""),
34       method(""),
35       receivedSlices(""),
36       downloadPath(""),
37       before_download_callback(nullptr),
38       download_item_callback(nullptr)
39 {
40     WVLOG_D("[DOWNLOAD] WebDownloadItem::constructor");
41     this->env_ = env;
42     this->currentSpeed = 0;
43     this->percentComplete = 0;
44     this->totalBytes = 0;
45     this->receivedBytes = 0;
46     this->lastErrorCode = 0;
47     this->webDownloadId = 0;
48     this->nwebId = 0;
49 }
50 
WebDownloadItem(napi_env env,NWebDownloadItem * downloadItem)51 WebDownloadItem::WebDownloadItem(napi_env env, NWebDownloadItem *downloadItem)
52     : guid(""),
53       fullPath(""),
54       url(""),
55       etag(""),
56       originalUrl(""),
57       suggestedFileName(""),
58       contentDisposition(""),
59       mimeType(""),
60       lastModified(""),
61       method(""),
62       receivedSlices(""),
63       downloadPath(""),
64       before_download_callback(nullptr),
65       download_item_callback(nullptr)
66 {
67     WVLOG_D("[DOWNLOAD] WebDownloadItem constructor");
68     this->webDownloadId = WebDownloadItem_GetDownloadItemId(downloadItem);
69     this->state = WebDownloadItem_GetState(downloadItem);
70     this->currentSpeed = WebDownloadItem_CurrentSpeed(downloadItem);
71     this->percentComplete = WebDownloadItem_PercentComplete(downloadItem);
72     this->totalBytes = WebDownloadItem_TotalBytes(downloadItem);
73     this->receivedBytes = WebDownloadItem_ReceivedBytes(downloadItem);
74     this->guid = std::string(WebDownloadItem_Guid(downloadItem));
75     this->fullPath = std::string(WebDownloadItem_FullPath(downloadItem));
76     this->url = std::string(WebDownloadItem_Url(downloadItem));
77     this->originalUrl = std::string(WebDownloadItem_OriginalUrl(downloadItem));
78     this->suggestedFileName = std::string(WebDownloadItem_SuggestedFileName(downloadItem));
79     this->contentDisposition = std::string(WebDownloadItem_ContentDisposition(downloadItem));
80     this->method = std::string(WebDownloadItem_Method(downloadItem));
81     this->lastModified = std::string(WebDownloadItem_LastModified(downloadItem));
82     this->lastErrorCode = WebDownloadItem_LastErrorCode(downloadItem);
83     this->receivedSlices = std::string(WebDownloadItem_ReceivedSlices(downloadItem));
84     this->etag = std::string(WebDownloadItem_ETag(downloadItem));
85     this->mimeType = std::string(WebDownloadItem_MimeType(downloadItem));
86     this->nwebId = WebDownloadItem_NWebId(downloadItem);
87     env_ = env;
88 }
89 
~WebDownloadItem()90 WebDownloadItem::~WebDownloadItem()
91 {
92     WVLOG_D("[DOWNLOAD] WebDownloadItem::~WebDownloadItem()");
93     if (before_download_callback) {
94         DestroyBeforeDownloadCallbackWrapper(before_download_callback);
95         before_download_callback = nullptr;
96     }
97     if (download_item_callback) {
98         DestroyDownloadItemCallbackWrapper(download_item_callback);
99         download_item_callback = nullptr;
100     }
101 }
102 } // namespace NWeb
103 } // namespace OHOS
104