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_delegate.h"
17
18 #include <cstring>
19
20 #include "nweb_c_api.h"
21 #include "nweb_log.h"
22 #include "napi_web_download_item.h"
23 #include "web_download_manager.h"
24 #include "nweb_napi_scope.h"
25
26 namespace OHOS {
27 namespace NWeb {
WebDownloadDelegate(napi_env env)28 WebDownloadDelegate::WebDownloadDelegate(napi_env env)
29 : delegate_(nullptr),
30 download_before_start_callback_(nullptr),
31 download_did_update_callback_(nullptr),
32 download_did_finish_callback_(nullptr),
33 download_did_fail_callback_(nullptr),
34 env_(env)
35 {
36 WVLOG_D("WebDownloadDelegate::WebDownloadDelegate");
37 }
38
~WebDownloadDelegate()39 WebDownloadDelegate::~WebDownloadDelegate()
40 {
41 WVLOG_D("[DOWNLOAD] WebDownloadDelegate::~WebDownloadDelegate");
42 if (download_before_start_callback_) {
43 napi_delete_reference(env_, download_before_start_callback_);
44 }
45 if (download_did_update_callback_) {
46 napi_delete_reference(env_, download_did_update_callback_);
47 }
48 if (download_did_finish_callback_) {
49 napi_delete_reference(env_, download_did_finish_callback_);
50 }
51 if (download_did_fail_callback_) {
52 napi_delete_reference(env_, download_did_fail_callback_);
53 }
54 WebDownloadManager::RemoveDownloadDelegate(this);
55 }
56
RemoveSelfRef()57 void WebDownloadDelegate::RemoveSelfRef()
58 {
59 if (delegate_) {
60 napi_delete_reference(env_, delegate_);
61 delegate_ = nullptr;
62 }
63 }
64
DownloadBeforeStart(WebDownloadItem *webDownloadItem)65 void WebDownloadDelegate::DownloadBeforeStart(WebDownloadItem *webDownloadItem)
66 {
67 WVLOG_D("[DOWNLOAD] WebDownloadDelegate::DownloadBeforeStart");
68 if (!env_) {
69 WVLOG_E("[DOWNLOAD] WebDownloadDelegate::DownloadBeforeStart nil env");
70 return;
71 }
72 size_t paramCount = 1;
73
74 OHOS::NApiScope scope(env_);
75
76 napi_value callbackFunc = nullptr;
77 napi_status status;
78
79 if (!download_before_start_callback_) {
80 WVLOG_E("[DOWNLOAD] downloadBeforeStart nil env");
81 return;
82 }
83 status = napi_get_reference_value(env_, download_before_start_callback_, &callbackFunc);
84 if (status != napi_ok || callbackFunc == nullptr) {
85 WVLOG_E("[DOWNLOAD] get downloadBeforeStart func failed.");
86 return;
87 }
88
89 napi_value webDownloadItemValue = nullptr;
90 napi_create_object(env_, &webDownloadItemValue);
91 napi_wrap(
92 env_, webDownloadItemValue, webDownloadItem,
93 [](napi_env /* env */, void *data, void * /* hint */) {
94 if (data) {
95 WebDownloadItem *downloadItem = static_cast<WebDownloadItem *>(data);
96 delete downloadItem;
97 }
98 },
99 nullptr, nullptr);
100 NapiWebDownloadItem::DefineProperties(env_, &webDownloadItemValue);
101
102 napi_value result = nullptr;
103 status = napi_call_function(env_, nullptr, callbackFunc, paramCount, &webDownloadItemValue, &result);
104 if (status != napi_status::napi_ok) {
105 WVLOG_E("[DOWNLOAD] call downloadBeforeStart failed.");
106 }
107 }
DownloadDidUpdate(WebDownloadItem *webDownloadItem)108 void WebDownloadDelegate::DownloadDidUpdate(WebDownloadItem *webDownloadItem)
109 {
110 WVLOG_D("[DOWNLOAD] WebDownloadDelegate::DownloadDidUpdate");
111 if (!env_) {
112 WVLOG_E("[DOWNLOAD] WebDownloadDelegate::DownloadDidUpdate nil env.");
113 return;
114 }
115 size_t paramCount = 1;
116
117 OHOS::NApiScope scope(env_);
118
119 napi_value callbackFunc = nullptr;
120 napi_status status;
121
122 if (!download_did_update_callback_) {
123 WVLOG_E("[DOWNLOAD] downloadDidUpdate not exists.");
124 return;
125 }
126 status = napi_get_reference_value(env_, download_did_update_callback_, &callbackFunc);
127 if (status != napi_ok || callbackFunc == nullptr) {
128 WVLOG_E("[DOWNLOAD] get downloadDidUpdate func failed.");
129 return;
130 }
131
132 napi_value webDownloadItemValue = nullptr;
133 napi_create_object(env_, &webDownloadItemValue);
134 napi_wrap(
135 env_, webDownloadItemValue, webDownloadItem,
136 [](napi_env /* env */, void *data, void * /* hint */) {
137 if (data) {
138 WebDownloadItem *downloadItem = static_cast<WebDownloadItem *>(data);
139 delete downloadItem;
140 }
141 },
142 nullptr, nullptr);
143 NapiWebDownloadItem::DefineProperties(env_, &webDownloadItemValue);
144
145 napi_value result = nullptr;
146 status = napi_call_function(env_, nullptr, callbackFunc, paramCount, &webDownloadItemValue, &result);
147 if (status != napi_status::napi_ok) {
148 WVLOG_E("[DOWNLOAD] call downloadDidUpdate failed.");
149 }
150 }
151
DownloadDidFail(WebDownloadItem *webDownloadItem)152 void WebDownloadDelegate::DownloadDidFail(WebDownloadItem *webDownloadItem)
153 {
154 WVLOG_D("WebDownloadDelegate::DownloadDidFail");
155 if (!env_) {
156 WVLOG_E("[DOWNLOAD] WebDownloadDelegate::DownloadDidFail nil env.");
157 return;
158 }
159 size_t paramCount = 1;
160
161 OHOS::NApiScope scope(env_);
162
163 napi_value callbackFunc = nullptr;
164 napi_status status;
165
166 if (!download_did_fail_callback_) {
167 WVLOG_E("[DOWNLOAD] DownloadDidFail not exists.");
168 return;
169 }
170 status = napi_get_reference_value(env_, download_did_fail_callback_, &callbackFunc);
171 if (status != napi_ok || callbackFunc == nullptr) {
172 WVLOG_E("[DOWNLOAD] get downloadDidFail func failed.");
173 return;
174 }
175
176 napi_value webDownloadItemValue = nullptr;
177 napi_create_object(env_, &webDownloadItemValue);
178 napi_wrap(
179 env_, webDownloadItemValue, webDownloadItem,
180 [](napi_env /* env */, void *data, void * /* hint */) {
181 if (data) {
182 WebDownloadItem *downloadItem = static_cast<WebDownloadItem *>(data);
183 delete downloadItem;
184 }
185 },
186 nullptr, nullptr);
187 NapiWebDownloadItem::DefineProperties(env_, &webDownloadItemValue);
188
189 napi_value result = nullptr;
190 status = napi_call_function(env_, nullptr, callbackFunc, paramCount, &webDownloadItemValue, &result);
191 if (status != napi_status::napi_ok) {
192 WVLOG_E("[DOWNLOAD] call downloadDidFail failed.");
193 }
194 }
195
DownloadDidFinish(WebDownloadItem *webDownloadItem)196 void WebDownloadDelegate::DownloadDidFinish(WebDownloadItem *webDownloadItem)
197 {
198 WVLOG_D("WebDownloadDelegate::DownloadDidFinish");
199 if (!env_) {
200 WVLOG_E("[DOWNLOAD] WebDownloadDelegate::DownloadDidFinish nil env.");
201 return;
202 }
203 size_t paramCount = 1;
204
205 OHOS::NApiScope scope(env_);
206
207 napi_value callbackFunc = nullptr;
208 napi_status status;
209
210 if (!download_did_finish_callback_) {
211 WVLOG_E("[DOWNLOAD] downloadDidFinish not exists.");
212 return;
213 }
214 status = napi_get_reference_value(env_, download_did_finish_callback_, &callbackFunc);
215 if (status != napi_ok || callbackFunc == nullptr) {
216 WVLOG_E("[DOWNLOAD] get downloadDidFinish func failed.");
217 return;
218 }
219
220 napi_value webDownloadItemValue = nullptr;
221 napi_create_object(env_, &webDownloadItemValue);
222 napi_wrap(
223 env_, webDownloadItemValue, webDownloadItem,
224 [](napi_env /* env */, void *data, void * /* hint */) {
225 if (data) {
226 WebDownloadItem *downloadItem = static_cast<WebDownloadItem *>(data);
227 delete downloadItem;
228 }
229 },
230 nullptr, nullptr);
231 NapiWebDownloadItem::DefineProperties(env_, &webDownloadItemValue);
232
233 napi_value result = nullptr;
234 status = napi_call_function(env_, nullptr, callbackFunc, paramCount, &webDownloadItemValue, &result);
235 if (status != napi_status::napi_ok) {
236 WVLOG_E("[DOWNLOAD] call downloadDidFinish failed.");
237 }
238 }
239
PutDownloadBeforeStart(napi_env, napi_value callback)240 void WebDownloadDelegate::PutDownloadBeforeStart(napi_env, napi_value callback)
241 {
242 WVLOG_D("WebDownloadDelegate::PutDownloadBeforeStart");
243 napi_status status = napi_create_reference(env_, callback, 1, &download_before_start_callback_);
244 if (status != napi_status::napi_ok) {
245 WVLOG_E("[DOWNLOAD] PutDownloadBeforeStart create reference failed.");
246 }
247 }
248
PutDownloadDidUpdate(napi_env, napi_value callback)249 void WebDownloadDelegate::PutDownloadDidUpdate(napi_env, napi_value callback)
250 {
251 WVLOG_D("[DOWNLOAD] WebDownloadDelegate::PutDownloadDidUpdate");
252 napi_status status = napi_create_reference(env_, callback, 1, &download_did_update_callback_);
253 if (status != napi_status::napi_ok) {
254 WVLOG_E("[DOWNLOAD] PutDownloadDidUpdate create reference failed.");
255 }
256 }
257
PutDownloadDidFinish(napi_env, napi_value callback)258 void WebDownloadDelegate::PutDownloadDidFinish(napi_env, napi_value callback)
259 {
260 WVLOG_D("WebDownloadDelegate::PutDownloadDidFinish");
261 napi_status status = napi_create_reference(env_, callback, 1, &download_did_finish_callback_);
262 if (status != napi_status::napi_ok) {
263 WVLOG_E("[DOWNLOAD] PutDownloadDidFinish create reference failed.");
264 }
265 }
266
PutDownloadDidFail(napi_env, napi_value callback)267 void WebDownloadDelegate::PutDownloadDidFail(napi_env, napi_value callback)
268 {
269 WVLOG_D("WebDownloadDelegate::PutDownloadDidFail");
270 napi_status status = napi_create_reference(env_, callback, 1, &download_did_fail_callback_);
271 if (status != napi_status::napi_ok) {
272 WVLOG_E("[DOWNLOAD] PutDownloadDidFail create reference failed.");
273 }
274 }
275
GetNWebId() const276 int32_t WebDownloadDelegate::GetNWebId() const
277 {
278 return nwebId_;
279 }
280
SetNWebId(int32_t nwebId)281 void WebDownloadDelegate::SetNWebId(int32_t nwebId)
282 {
283 nwebId_ = nwebId;
284 }
285
GetEnv()286 napi_env WebDownloadDelegate::GetEnv()
287 {
288 return env_;
289 }
290 } // namespace NWeb
291 } // namespace OHOS
292