• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "multi_download_progress_napi.h"
17 
18 #include "cloud_sync_common.h"
19 #include "dfs_error.h"
20 #include "download_progress_napi.h"
21 #include "utils_log.h"
22 
23 namespace OHOS::FileManagement::CloudSync {
24 using namespace FileManagement::LibN;
25 using namespace std;
Constructor(napi_env env,napi_callback_info info)26 napi_value MultiDlProgressNapi::Constructor(napi_env env, napi_callback_info info)
27 {
28     NFuncArg funcArg(env, info);
29     funcArg.InitArgs(NARG_CNT::ZERO);
30     auto progressEntity = make_unique<MultiDlProgressEntity>();
31     if (!NClass::SetEntityFor<MultiDlProgressEntity>(env, funcArg.GetThisVar(), move(progressEntity))) {
32         LOGE("Failed to set progressEntity.");
33         return nullptr;
34     }
35     return funcArg.GetThisVar();
36 }
37 
GetStatus(napi_env env,napi_callback_info info)38 napi_value MultiDlProgressNapi::GetStatus(napi_env env, napi_callback_info info)
39 {
40     NFuncArg funcArg(env, info);
41     funcArg.InitArgs(NARG_CNT::ZERO);
42     int32_t state = static_cast<int32_t>(DownloadProgressObj::Status::FAILED);
43     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
44     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
45         LOGE("Failed to get MultiDlProgressEntity.");
46     } else {
47         state = progressEntity->downloadProgress->GetState();
48     }
49 
50     return NVal::CreateInt32(env, state).val_;
51 }
52 
GetTaskId(napi_env env,napi_callback_info info)53 napi_value MultiDlProgressNapi::GetTaskId(napi_env env, napi_callback_info info)
54 {
55     int64_t taskId = 0;
56     NFuncArg funcArg(env, info);
57     funcArg.InitArgs(NARG_CNT::ZERO);
58     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
59     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
60         LOGE("Failed to get MultiDlProgressEntity.");
61     } else {
62         taskId = progressEntity->downloadProgress->GetTaskId();
63     }
64 
65     return NVal::CreateInt64(env, taskId).val_;
66 }
67 
GetDownloadedNum(napi_env env,napi_callback_info info)68 napi_value MultiDlProgressNapi::GetDownloadedNum(napi_env env, napi_callback_info info)
69 {
70     int64_t succNum = -1;
71     NFuncArg funcArg(env, info);
72     funcArg.InitArgs(NARG_CNT::ZERO);
73     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
74     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
75         LOGE("Failed to get MultiDlProgressEntity.");
76     } else {
77         succNum = static_cast<int64_t>(progressEntity->downloadProgress->GetSuccNum());
78     }
79 
80     return NVal::CreateInt64(env, succNum).val_;
81 }
82 
GetFailedNum(napi_env env,napi_callback_info info)83 napi_value MultiDlProgressNapi::GetFailedNum(napi_env env, napi_callback_info info)
84 {
85     int64_t failedNum = -1;
86     NFuncArg funcArg(env, info);
87     funcArg.InitArgs(NARG_CNT::ZERO);
88     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
89     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
90         LOGE("Failed to get MultiDlProgressEntity.");
91     } else {
92         failedNum = static_cast<int64_t>(progressEntity->downloadProgress->GetFailedNum());
93     }
94 
95     return NVal::CreateInt64(env, failedNum).val_;
96 }
97 
GetTotalNum(napi_env env,napi_callback_info info)98 napi_value MultiDlProgressNapi::GetTotalNum(napi_env env, napi_callback_info info)
99 {
100     int64_t totalNum = -1;
101     NFuncArg funcArg(env, info);
102     funcArg.InitArgs(NARG_CNT::ZERO);
103     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
104     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
105         LOGE("Failed to get MultiDlProgressEntity.");
106     } else {
107         totalNum = progressEntity->downloadProgress->GetTotalNum();
108     }
109 
110     return NVal::CreateInt64(env, totalNum).val_;
111 }
112 
GetDownloadedSize(napi_env env,napi_callback_info info)113 napi_value MultiDlProgressNapi::GetDownloadedSize(napi_env env, napi_callback_info info)
114 {
115     int64_t downloadSize = INT64_MAX;
116     NFuncArg funcArg(env, info);
117     funcArg.InitArgs(NARG_CNT::ZERO);
118     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
119     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
120         LOGE("Failed to get MultiDlProgressEntity.");
121     } else {
122         downloadSize = progressEntity->downloadProgress->GetDownloadedSize();
123     }
124 
125     return NVal::CreateInt64(env, downloadSize).val_;
126 }
127 
GetTotalSize(napi_env env,napi_callback_info info)128 napi_value MultiDlProgressNapi::GetTotalSize(napi_env env, napi_callback_info info)
129 {
130     int64_t totalSize = INT64_MAX;
131     NFuncArg funcArg(env, info);
132     funcArg.InitArgs(NARG_CNT::ZERO);
133     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
134     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
135         LOGE("Failed to get MultiDlProgressEntity.");
136     } else {
137         totalSize = progressEntity->downloadProgress->GetTotalSize();
138     }
139 
140     return NVal::CreateInt64(env, totalSize).val_;
141 }
142 
GetErrorType(napi_env env,napi_callback_info info)143 napi_value MultiDlProgressNapi::GetErrorType(napi_env env, napi_callback_info info)
144 {
145     NFuncArg funcArg(env, info);
146     funcArg.InitArgs(NARG_CNT::ZERO);
147     int32_t errorType = static_cast<int32_t>(DownloadProgressObj::DownloadErrorType::UNKNOWN_ERROR);
148     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
149     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
150         LOGE("Failed to get MultiDlProgressEntity.");
151     } else {
152         errorType = progressEntity->downloadProgress->GetErrorType();
153     }
154 
155     return NVal::CreateInt32(env, errorType).val_;
156 }
157 
GetFailedFileList(napi_env env,napi_callback_info info)158 napi_value MultiDlProgressNapi::GetFailedFileList(napi_env env, napi_callback_info info)
159 {
160     NFuncArg funcArg(env, info);
161     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
162         LOGE("Failed to get param.");
163         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
164         return nullptr;
165     }
166     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
167     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
168         LOGE("Failed to get MultiDlProgressEntity.");
169         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
170         return nullptr;
171     }
172     napi_value res = nullptr;
173     napi_status status = napi_create_array(env, &res);
174     if (status != napi_ok) {
175         HILOGE("Failed to creat array");
176         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
177         return nullptr;
178     }
179 
180     size_t index = 0;
181     auto failedFiles = progressEntity->downloadProgress->GetFailedFiles();
182     for (const auto &iter : failedFiles) {
183         NVal obj = NVal::CreateObject(env);
184         obj.AddProp("uri", NVal::CreateUTF8String(env, iter.first).val_);
185         obj.AddProp("error", NVal::CreateInt32(env, iter.second).val_);
186         status = napi_set_element(env, res, index, obj.val_);
187         if (status != napi_ok) {
188             HILOGE("Failed to set element on data, %{public}s", GetAnonyString(iter.first).c_str());
189             NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
190             return nullptr;
191         }
192         index++;
193     }
194 
195     return res;
196 }
197 
GetDownloadedFileList(napi_env env,napi_callback_info info)198 napi_value MultiDlProgressNapi::GetDownloadedFileList(napi_env env, napi_callback_info info)
199 {
200     NFuncArg funcArg(env, info);
201     if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
202         LOGE("Failed to get param.");
203         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
204         return nullptr;
205     }
206     auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, funcArg.GetThisVar());
207     if (progressEntity == nullptr || progressEntity->downloadProgress == nullptr) {
208         LOGE("Failed to get MultiDlProgressEntity.");
209         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
210         return nullptr;
211     }
212     napi_value res = nullptr;
213     napi_status status = napi_create_array(env, &res);
214     if (status != napi_ok) {
215         HILOGE("Failed to creat array");
216         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
217         return nullptr;
218     }
219 
220     size_t index = 0;
221     auto downloadedFiles = progressEntity->downloadProgress->GetDownloadedFiles();
222     for (const auto &item : downloadedFiles) {
223         status = napi_set_element(env, res, index, NVal::CreateUTF8String(env, item).val_);
224         if (status != napi_ok) {
225             HILOGE("Failed to set element on data, %{public}s", GetAnonyString(item).c_str());
226             NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(env);
227             return nullptr;
228         }
229         index++;
230     }
231 
232     return res;
233 }
234 
GetClassName()235 std::string MultiDlProgressNapi::GetClassName()
236 {
237     return className_;
238 }
239 
Export()240 bool MultiDlProgressNapi::Export()
241 {
242     vector<napi_property_descriptor> props = {
243         NVal::DeclareNapiGetter("state", GetStatus),
244         NVal::DeclareNapiGetter("taskId", GetTaskId),
245         NVal::DeclareNapiGetter("successfulCount", GetDownloadedNum),
246         NVal::DeclareNapiGetter("failedCount", GetFailedNum),
247         NVal::DeclareNapiGetter("totalCount", GetTotalNum),
248         NVal::DeclareNapiGetter("downloadedSize", GetDownloadedSize),
249         NVal::DeclareNapiGetter("totalSize", GetTotalSize),
250         NVal::DeclareNapiGetter("errType", GetErrorType),
251         NVal::DeclareNapiFunction("getFailedFiles", GetFailedFileList),
252         NVal::DeclareNapiFunction("getSuccessfulFiles", GetDownloadedFileList),
253     };
254 
255     string className = GetClassName();
256     bool succ = false;
257     napi_value classValue = nullptr;
258     tie(succ, classValue) =
259         NClass::DefineClass(exports_.env_, className, MultiDlProgressNapi::Constructor, move(props));
260     if (!succ) {
261         LOGE("Define class exceptions");
262         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(exports_.env_);
263         return false;
264     }
265     succ = NClass::SaveClass(exports_.env_, className, classValue);
266     if (!succ) {
267         LOGE("Save class exceptions");
268         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(exports_.env_);
269         return false;
270     }
271 
272     if (!exports_.AddProp(className, classValue)) {
273         LOGE("Add property exceptions");
274         NError(Convert2JsErrNum(E_SERVICE_INNER_ERROR)).ThrowErr(exports_.env_);
275         return false;
276     }
277 
278     return true;
279 }
280 
281 } // namespace OHOS::FileManagement::CloudSync