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_delegate_impl.h" 17 18 #include <cstring> 19 20 #include "nweb_c_api.h" 21 #include "webview_log.h" 22 #include "web_download_manager_impl.h" 23 24 namespace OHOS::Webview { WebDownloadDelegateImpl()25 WebDownloadDelegateImpl::WebDownloadDelegateImpl() 26 : download_before_start_callback_(nullptr), 27 download_did_update_callback_(nullptr), 28 download_did_finish_callback_(nullptr), 29 download_did_fail_callback_(nullptr) 30 { 31 WEBVIEWLOGD("WebDownloadDelegate::WebDownloadDelegate"); 32 } 33 ~WebDownloadDelegateImpl()34 WebDownloadDelegateImpl::~WebDownloadDelegateImpl() 35 { 36 WEBVIEWLOGI("[DOWNLOAD] WebDownloadDelegate::~WebDownloadDelegate"); 37 WebDownloadManagerImpl::RemoveDownloadDelegate(this); 38 } 39 DownloadBeforeStart(WebDownloadItemImpl *webDownloadItemImpl)40 void WebDownloadDelegateImpl::DownloadBeforeStart(WebDownloadItemImpl *webDownloadItemImpl) 41 { 42 WEBVIEWLOGI("[DOWNLOAD] WebDownloadDelegate::DownloadBeforeStart"); 43 if (!download_before_start_callback_) { 44 WEBVIEWLOGE("[DOWNLOAD] downloadBeforeStart not exists."); 45 return; 46 } 47 download_before_start_callback_(webDownloadItemImpl->GetID()); 48 } 49 DownloadDidUpdate(WebDownloadItemImpl *webDownloadItemImpl)50 void WebDownloadDelegateImpl::DownloadDidUpdate(WebDownloadItemImpl *webDownloadItemImpl) 51 { 52 WEBVIEWLOGI("[DOWNLOAD] WebDownloadDelegate::DownloadDidUpdate"); 53 if (!download_did_update_callback_) { 54 WEBVIEWLOGE("[DOWNLOAD] downloadBeforeStart not exists."); 55 return; 56 } 57 download_did_update_callback_(webDownloadItemImpl->GetID()); 58 } 59 DownloadDidFail(WebDownloadItemImpl *webDownloadItemImpl)60 void WebDownloadDelegateImpl::DownloadDidFail(WebDownloadItemImpl *webDownloadItemImpl) 61 { 62 WEBVIEWLOGI("WebDownloadDelegate::DownloadDidFail"); 63 if (!download_did_fail_callback_) { 64 WEBVIEWLOGE("[DOWNLOAD] downloadDidFail not exists."); 65 return; 66 } 67 download_did_fail_callback_(webDownloadItemImpl->GetID()); 68 } 69 DownloadDidFinish(WebDownloadItemImpl *webDownloadItemImpl)70 void WebDownloadDelegateImpl::DownloadDidFinish(WebDownloadItemImpl *webDownloadItemImpl) 71 { 72 WEBVIEWLOGI("WebDownloadDelegate::DownloadDidFinish"); 73 if (!download_did_finish_callback_) { 74 WEBVIEWLOGE("[DOWNLOAD] downloadBeforeStart not exists."); 75 return; 76 } 77 download_did_finish_callback_(webDownloadItemImpl->GetID()); 78 } 79 PutDownloadBeforeStart(std::function<void(int64_t)> callback)80 void WebDownloadDelegateImpl::PutDownloadBeforeStart(std::function<void(int64_t)> callback) 81 { 82 WEBVIEWLOGD("WebDownloadDelegate::PutDownloadBeforeStart"); 83 download_before_start_callback_ = callback; 84 } 85 PutDownloadDidUpdate(std::function<void(int64_t)> callback)86 void WebDownloadDelegateImpl::PutDownloadDidUpdate(std::function<void(int64_t)> callback) 87 { 88 WEBVIEWLOGI("[DOWNLOAD] WebDownloadDelegate::PutDownloadDidUpdate"); 89 download_did_update_callback_ = callback; 90 } 91 PutDownloadDidFinish(std::function<void(int64_t)> callback)92 void WebDownloadDelegateImpl::PutDownloadDidFinish(std::function<void(int64_t)> callback) 93 { 94 WEBVIEWLOGD("WebDownloadDelegate::PutDownloadDidFinish"); 95 download_did_finish_callback_ = callback; 96 } 97 PutDownloadDidFail(std::function<void(int64_t)> callback)98 void WebDownloadDelegateImpl::PutDownloadDidFail(std::function<void(int64_t)> callback) 99 { 100 WEBVIEWLOGD("WebDownloadDelegate::PutDownloadDidFail"); 101 download_did_fail_callback_ = callback; 102 } 103 GetNWebId()104 int32_t WebDownloadDelegateImpl::GetNWebId() 105 { 106 return nwebId_; 107 } 108 SetNWebId(int32_t nwebId)109 void WebDownloadDelegateImpl::SetNWebId(int32_t nwebId) 110 { 111 nwebId_ = nwebId; 112 } 113 114 } // namespace OHOS:WebView 115