1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "web_download_item_impl.h"
17 
18 #include <string>
19 
20 #include "webview_log.h"
21 
22 namespace OHOS::Webview {
WebDownloadItemImpl()23     WebDownloadItemImpl::WebDownloadItemImpl()
24         : guid(""),
25         fullPath(""),
26         url(""),
27         etag(""),
28         originalUrl(""),
29         suggestedFileName(""),
30         contentDisposition(""),
31         mimeType(""),
32         lastModified(""),
33         method(""),
34         receivedSlices(""),
35         downloadPath(""),
36         before_download_callback(nullptr),
37         download_item_callback(nullptr)
38     {
39         WEBVIEWLOGD("[DOWNLOAD] WebDownloadItemImpl::constructor");
40         this->currentSpeed = 0;
41         this->percentComplete = 0;
42         this->totalBytes = 0;
43         this->receivedBytes = 0;
44         this->lastErrorCode = 0;
45         this->webDownloadId = 0;
46         this->nwebId = 0;
47     }
48 
WebDownloadItemImpl(NWebDownloadItem *downloadItem)49     WebDownloadItemImpl::WebDownloadItemImpl(NWebDownloadItem *downloadItem)
50         : guid(""),
51         fullPath(""),
52         url(""),
53         etag(""),
54         originalUrl(""),
55         suggestedFileName(""),
56         contentDisposition(""),
57         mimeType(""),
58         lastModified(""),
59         method(""),
60         receivedSlices(""),
61         downloadPath(""),
62         before_download_callback(nullptr),
63         download_item_callback(nullptr)
64     {
65         WEBVIEWLOGD("[DOWNLOAD] WebDownloadItem constructor");
66         this->webDownloadId = WebDownloadItem_GetDownloadItemId(downloadItem);
67         this->state = WebDownloadItem_GetState(downloadItem);
68         this->currentSpeed = WebDownloadItem_CurrentSpeed(downloadItem);
69         this->percentComplete = WebDownloadItem_PercentComplete(downloadItem);
70         this->totalBytes = WebDownloadItem_TotalBytes(downloadItem);
71         this->receivedBytes = WebDownloadItem_ReceivedBytes(downloadItem);
72         this->guid = std::string(WebDownloadItem_Guid(downloadItem));
73         this->fullPath = std::string(WebDownloadItem_FullPath(downloadItem));
74         this->url = std::string(WebDownloadItem_Url(downloadItem));
75         this->originalUrl = std::string(WebDownloadItem_OriginalUrl(downloadItem));
76         this->suggestedFileName = std::string(WebDownloadItem_SuggestedFileName(downloadItem));
77         this->contentDisposition = std::string(WebDownloadItem_ContentDisposition(downloadItem));
78         this->method = std::string(WebDownloadItem_Method(downloadItem));
79         this->lastModified = std::string(WebDownloadItem_LastModified(downloadItem));
80         this->lastErrorCode = WebDownloadItem_LastErrorCode(downloadItem);
81         this->receivedSlices = std::string(WebDownloadItem_ReceivedSlices(downloadItem));
82         this->etag = std::string(WebDownloadItem_ETag(downloadItem));
83         this->mimeType = std::string(WebDownloadItem_MimeType(downloadItem));
84         this->nwebId = WebDownloadItem_NWebId(downloadItem);
85     }
86 
~WebDownloadItemImpl()87     WebDownloadItemImpl::~WebDownloadItemImpl()
88     {
89         WEBVIEWLOGD("[DOWNLOAD] WebDownloadItemImpl::~WebDownloadItemImpl()");
90         if (before_download_callback) {
91             DestroyBeforeDownloadCallbackWrapper(before_download_callback);
92             before_download_callback = nullptr;
93         }
94         if (download_item_callback) {
95             DestroyDownloadItemCallbackWrapper(download_item_callback);
96             download_item_callback = nullptr;
97         }
98     }
99 } // namespace OHOS::Webview {