1 /* 2 * Copyright (c) 2021-2024 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 FOUNDATION_ACE_FRAMEWORKS_BASE_NETWORK_DOWNLOAD_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_NETWORK_DOWNLOAD_MANAGER_H 18 19 #include <condition_variable> 20 #include <cstdint> 21 #include <functional> 22 #include <memory> 23 #include <mutex> 24 #include <optional> 25 #include <string> 26 27 namespace OHOS::Ace { 28 struct DownloadCallback { 29 std::function<void(const std::string&&, bool, int32_t)> successCallback; 30 std::function<void(std::string, bool, int32_t)> failCallback; 31 std::function<void(std::string, bool, int32_t)> cancelCallback; 32 std::function<void(uint32_t, uint32_t, bool, int32_t)> onProgressCallback; 33 }; 34 35 struct DownloadCondition { 36 std::condition_variable cv; 37 std::string dataOut; 38 std::mutex downloadMutex; 39 std::string errorMsg; 40 std::optional<bool> downloadSuccess; 41 }; 42 43 struct DownloadResult { 44 std::string dataOut; 45 std::string errorMsg; 46 std::optional<bool> downloadSuccess; 47 }; 48 49 class DownloadManager { 50 public: 51 static DownloadManager* GetInstance(); 52 53 virtual ~DownloadManager() = default; 54 55 virtual bool Download(const std::string& url, std::vector<uint8_t>& dataOut); 56 virtual bool Download(const std::string& url, const std::shared_ptr<DownloadResult>& result); 57 virtual bool DownloadAsync( 58 DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId, int32_t nodeId); 59 virtual bool DownloadSync( 60 DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId, int32_t nodeId); 61 virtual bool RemoveDownloadTask(const std::string& url, int32_t nodeId, bool isCancel = true); 62 63 private: 64 static std::unique_ptr<DownloadManager> instance_; 65 static std::mutex mutex_; 66 }; 67 68 } // namespace OHOS::Ace 69 70 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_NETWORK_DOWNLOAD_MANAGER_H 71