• 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 <mutex>
23 #include <optional>
24 #include <string>
25 
26 namespace OHOS::Ace {
27 struct DownloadCallback {
28     std::function<void(const std::string&&, bool, int32_t)> successCallback;
29     std::function<void(std::string, bool, int32_t)> failCallback;
30     std::function<void(std::string, bool, int32_t)> cancelCallback;
31 };
32 
33 struct DownloadCondition {
34     std::condition_variable cv;
35     std::string dataOut;
36     std::mutex downloadMutex;
37     std::string errorMsg;
38     std::optional<bool> downloadSuccess;
39 };
40 
41 class DownloadManager {
42 public:
43     static DownloadManager* GetInstance();
44 
45     virtual ~DownloadManager() = default;
46 
47     virtual bool Download(const std::string& url, std::vector<uint8_t>& dataOut);
48     virtual bool DownloadAsync(DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId);
49     virtual bool DownloadSync(DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId);
50 
51 private:
52     static std::unique_ptr<DownloadManager> instance_;
53     static std::mutex mutex_;
54 };
55 
56 } // namespace OHOS::Ace
57 
58 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_NETWORK_DOWNLOAD_MANAGER_H
59