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 #ifndef OHOS_FILEMGMT_DOWNLOAD_PROGRESS_ANI_H 17 #define OHOS_FILEMGMT_DOWNLOAD_PROGRESS_ANI_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <unordered_set> 22 23 #include "ani_utils.h" 24 #include "cloud_sync_common.h" 25 26 namespace OHOS::FileManagement::CloudSync { 27 class DlProgressAni { 28 public: DlProgressAni(int64_t downloadId)29 explicit DlProgressAni(int64_t downloadId) : taskId_(downloadId) {} 30 virtual ~DlProgressAni() = default; 31 virtual void Update(const DownloadProgressObj &progress) = 0; 32 virtual ani_object ConvertToObject(ani_env *env) = 0; 33 virtual std::shared_ptr<DlProgressAni> CreateNewObject() = 0; 34 GetTaskId()35 int64_t GetTaskId() const 36 { 37 return taskId_; 38 } GetTotalSize()39 int64_t GetTotalSize() const 40 { 41 return totalSize_; 42 } GetDownloadedSize()43 int64_t GetDownloadedSize() const 44 { 45 return downloadedSize_; 46 } GetState()47 int32_t GetState() const 48 { 49 return state_; 50 } GetErrorType()51 int32_t GetErrorType() const 52 { 53 return errorType_; 54 } IsNeedClean()55 bool IsNeedClean() const 56 { 57 return needClean_; 58 } GetUri()59 std::string GetUri() const 60 { 61 return uri_; 62 } 63 64 protected: 65 std::string uri_; 66 // taskId_ is used to identify the download task, it is unique for each download 67 int64_t taskId_{0}; 68 int64_t totalSize_{0}; 69 int64_t downloadedSize_{0}; 70 int32_t state_{0}; 71 int32_t errorType_{0}; 72 bool needClean_{false}; 73 std::mutex mtx_; 74 }; 75 76 class SingleProgressAni : public DlProgressAni { 77 public: SingleProgressAni(int64_t downloadId)78 explicit SingleProgressAni(int64_t downloadId) : DlProgressAni(downloadId) {} 79 void Update(const DownloadProgressObj &progress) override; 80 ani_object ConvertToObject(ani_env *env) override; 81 std::shared_ptr<DlProgressAni> CreateNewObject() override; 82 }; 83 84 class BatchProgressAni : public DlProgressAni, 85 public std::enable_shared_from_this<BatchProgressAni> { 86 public: BatchProgressAni(int64_t downloadId)87 explicit BatchProgressAni(int64_t downloadId) : DlProgressAni(downloadId) {} 88 void Update(const DownloadProgressObj &progress) override; 89 ani_object ConvertToObject(ani_env *env) override; 90 std::shared_ptr<DlProgressAni> CreateNewObject() override; 91 92 // Getters for batch download progress GetTotalNum()93 int64_t GetTotalNum() const 94 { 95 return totalNum_; 96 } GetSuccNum()97 std::size_t GetSuccNum() const 98 { 99 return downloadedFiles_.size(); 100 } GetFailedNum()101 std::size_t GetFailedNum() const 102 { 103 return failedFiles_.size(); 104 } GetDownloadedFiles()105 const std::unordered_set<std::string>& GetDownloadedFiles() const 106 { 107 return downloadedFiles_; 108 } GetFailedFiles()109 const std::unordered_map<std::string, int32_t>& GetFailedFiles() const 110 { 111 return failedFiles_; 112 } 113 114 // The maximum size of files list is 400, which may cause performance problems. 115 void SetDownloadedFiles(const std::unordered_set<std::string> &fileList); 116 void SetFailedFiles(const std::unordered_map<std::string, int32_t> &fileList); 117 118 private: 119 int64_t totalNum_{0}; 120 std::unordered_set<std::string> downloadedFiles_; 121 std::unordered_map<std::string, int32_t> failedFiles_; 122 }; 123 } // namespace OHOS::FileManagement::CloudSync 124 #endif // OHOS_FILEMGMT_DOWNLOAD_PROGRESS_ANI_H 125