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