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 #include "base/image/image_defines.h" 28 29 namespace OHOS::Ace { 30 struct DownloadCallback { 31 std::function<void(const std::string&&, bool, int32_t)> successCallback; 32 std::function<void(std::string, ImageErrorInfo, bool, int32_t)> failCallback; 33 std::function<void(std::string, ImageErrorInfo, bool, int32_t)> cancelCallback; 34 std::function<void(uint32_t, uint32_t, bool, int32_t)> onProgressCallback; 35 }; 36 37 struct DownloadCondition { 38 std::condition_variable cv; 39 std::string dataOut; 40 std::mutex downloadMutex; 41 std::string errorMsg; 42 ImageErrorInfo errorInfo; 43 std::optional<bool> downloadSuccess; 44 }; 45 46 struct DownloadResult { 47 std::string dataOut; 48 std::string errorMsg; 49 std::optional<bool> downloadSuccess; 50 }; 51 52 class DownloadManager { 53 public: 54 static DownloadManager* GetInstance(); 55 56 virtual ~DownloadManager() = default; 57 58 virtual bool Download(const std::string& url, std::vector<uint8_t>& dataOut); 59 virtual bool Download(const std::string& url, const std::shared_ptr<DownloadResult>& result); 60 virtual bool DownloadAsync( 61 DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId, int32_t nodeId); 62 virtual bool DownloadSync( 63 DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId, int32_t nodeId); 64 virtual bool RemoveDownloadTask(const std::string& url, int32_t nodeId, bool isCancel = true); 65 virtual bool RemoveDownloadTaskWithPreload(const std::string& url, bool isCancel = true); 66 67 // use preload module to download the url 68 virtual bool DownloadAsyncWithPreload( 69 DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId); 70 virtual bool DownloadSyncWithPreload( 71 DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId); 72 virtual bool IsContains(const std::string& url); 73 // Synchronously queries the cache in the preDownLoad module and assigns the result if found. 74 virtual bool fetchCachedResult(const std::string& url, std::string& result); 75 76 private: 77 static std::unique_ptr<DownloadManager> instance_; 78 static std::mutex mutex_; 79 }; 80 81 } // namespace OHOS::Ace 82 83 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_NETWORK_DOWNLOAD_MANAGER_H 84