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 "download_progress_napi.h"
17
18 #include <memory>
19
20 #include "dfs_error.h"
21 #include "multi_download_progress_napi.h"
22 #include "utils_log.h"
23
24 namespace OHOS::FileManagement::CloudSync {
25 using namespace FileManagement::LibN;
Update(const DownloadProgressObj & progress)26 void SingleProgressNapi::Update(const DownloadProgressObj &progress)
27 {
28 if (taskId_ != progress.downloadId) {
29 return;
30 }
31 std::lock_guard<std::mutex> lock(mtx_);
32 uri_ = progress.path;
33 totalSize_ = progress.totalSize;
34 downloadedSize_ = progress.downloadedSize;
35 state_ = progress.state;
36 errorType_ = progress.downloadErrorType;
37 needClean_ = (progress.state != DownloadProgressObj::RUNNING);
38 }
39
ConvertToValue(napi_env env)40 napi_value SingleProgressNapi::ConvertToValue(napi_env env)
41 {
42 NVal obj = NVal::CreateObject(env);
43 obj.AddProp("state", NVal::CreateInt32(env, state_).val_);
44 obj.AddProp("processed", NVal::CreateInt64(env, downloadedSize_).val_);
45 obj.AddProp("size", NVal::CreateInt64(env, totalSize_).val_);
46 obj.AddProp("uri", NVal::CreateUTF8String(env, uri_).val_);
47 obj.AddProp("error", NVal::CreateInt32(env, errorType_).val_);
48 return obj.val_;
49 }
50
CreateNewObject()51 std::shared_ptr<DlProgressNapi> SingleProgressNapi::CreateNewObject()
52 {
53 return std::make_shared<SingleProgressNapi>(taskId_);
54 }
55
Update(const DownloadProgressObj & progress)56 void BatchProgressNapi::Update(const DownloadProgressObj &progress)
57 {
58 if (taskId_ != progress.downloadId) {
59 return;
60 }
61 std::lock_guard<std::mutex> lock(mtx_);
62 state_ = static_cast<int32_t>(progress.batchState);
63 downloadedSize_ = progress.batchDownloadSize;
64 totalSize_ = progress.batchTotalSize;
65 totalNum_ = progress.batchTotalNum;
66 errorType_ = static_cast<int32_t>(progress.downloadErrorType);
67 if (progress.state == DownloadProgressObj::COMPLETED) {
68 downloadedFiles_.insert(progress.path);
69 } else if (progress.state != DownloadProgressObj::RUNNING) {
70 failedFiles_.insert(std::make_pair(progress.path, static_cast<int32_t>(progress.downloadErrorType)));
71 }
72 needClean_ = ((progress.batchTotalNum == progress.batchFailNum + progress.batchSuccNum) &&
73 progress.batchState != DownloadProgressObj::RUNNING);
74 }
75
76 // no need to lock here, because it is called only once when a new object is copied out.
SetDownloadedFiles(const std::unordered_set<std::string> & fileList)77 void BatchProgressNapi::SetDownloadedFiles(const std::unordered_set<std::string> &fileList)
78 {
79 downloadedFiles_ = fileList;
80 }
81
SetFailedFiles(const std::unordered_map<std::string,int32_t> & fileList)82 void BatchProgressNapi::SetFailedFiles(const std::unordered_map<std::string, int32_t> &fileList)
83 {
84 failedFiles_ = fileList;
85 }
86
CreateNewObject()87 std::shared_ptr<DlProgressNapi> BatchProgressNapi::CreateNewObject()
88 {
89 auto resProgress = std::make_shared<BatchProgressNapi>(taskId_);
90 std::lock_guard<std::mutex> lock(mtx_);
91 resProgress->SetDownloadedFiles(downloadedFiles_);
92 resProgress->SetFailedFiles(failedFiles_);
93 return resProgress;
94 }
95
ConvertToValue(napi_env env)96 napi_value BatchProgressNapi::ConvertToValue(napi_env env)
97 {
98 napi_value progressVal = NClass::InstantiateClass(env, MultiDlProgressNapi::className_, {});
99 if (progressVal == nullptr) {
100 LOGE("Failed to instantiate class");
101 return nullptr;
102 }
103 auto progressEntity = NClass::GetEntityOf<MultiDlProgressEntity>(env, progressVal);
104 if (progressEntity == nullptr) {
105 LOGE("Failed to get progressEntity.");
106 return nullptr;
107 }
108 progressEntity->downloadProgress = shared_from_this();
109 return progressVal;
110 }
111 } // namespace OHOS::FileManagement::CloudSync
112