• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     virtual bool RemoveDownloadTaskWithPreload(const std::string& url, int32_t nodeId, bool isCancel = true);
63 
64     // use preload module to download the url
65     virtual bool DownloadAsyncWithPreload(
66         DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId, int32_t nodeId);
67     virtual bool DownloadSyncWithPreload(
68         DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId, int32_t nodeId);
69     virtual bool IsContains(const std::string& url);
70     // Synchronously queries the cache in the preDownLoad module and assigns the result if found.
71     virtual bool fetchCachedResult(const std::string& url, std::string& result);
72 
73 private:
74     static std::unique_ptr<DownloadManager> instance_;
75     static std::mutex mutex_;
76 };
77 
78 } // namespace OHOS::Ace
79 
80 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_NETWORK_DOWNLOAD_MANAGER_H
81