• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 DOWNLOAD_SERVICE_MANAGER_H
17 #define DOWNLOAD_SERVICE_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 #include <stdint.h>
24 #include <functional>
25 #include <mutex>
26 #include <iosfwd>
27 #include <vector>
28 
29 #include "constant.h"
30 #include "download_config.h"
31 #include "download_info.h"
32 #include "download_service_task.h"
33 #include "download_thread.h"
34 
35 namespace OHOS::Request::Download {
36 class DownloadServiceManager final {
37 public:
38     static DownloadServiceManager *GetInstance();
39 
40     bool Create(uint32_t threadNum);
41     void Destroy();
42 
43     uint32_t AddTask(const DownloadConfig &config);
44     void InstallCallback(uint32_t taskId, DownloadTaskCallback eventCb);
45     bool ProcessTask();
46 
47     bool Pause(uint32_t taskId, uint32_t uid);
48     bool Resume(uint32_t taskId, uint32_t uid);
49     bool Remove(uint32_t taskId, uint32_t uid);
50     bool Query(uint32_t taskId, DownloadInfo &info);
51     bool Query(uint32_t taskId, uint32_t uid, DownloadInfo &info);
52     bool QueryAllTask(std::vector<DownloadInfo> &taskVector) const;
53     bool QueryMimeType(uint32_t taskId, uint32_t uid, std::string &mimeType);
54 
55     uint32_t GetStartId() const;
56 
57     void SetInterval(uint32_t interval);
58     uint32_t GetInterval() const;
59 
60     void ResumeTaskByNetwork();
61 private:
62     explicit DownloadServiceManager();
63     ~DownloadServiceManager();
64     enum class QueueType {
65         NONE_QUEUE,
66         PENDING_QUEUE,
67         PAUSED_QUEUE,
68     };
69 
70     uint32_t GetCurrentTaskId();
71     QueueType DecideQueueType(DownloadStatus status);
72     void MoveTaskToQueue(uint32_t taskId, std::shared_ptr<DownloadServiceTask> task);
73     void PushQueue(std::queue<uint32_t> &queue, uint32_t taskId);
74     void RemoveFromQueue(std::queue<uint32_t> &queue, uint32_t taskId);
75     bool MonitorNetwork();
76     void UpdateNetworkType();
77     void MonitorAppState();
78     void UpdateAppState(const std::string bundleName, int32_t uid, int32_t state);
79     bool IsSameApplication(const std::string sName, int32_t sUid, const std::string dName, int32_t dUid);
80     bool IsBackgroundOrTerminated(int32_t state);
81     bool IsForeground(int32_t state);
82     bool IsSameBundleName(const std::string &sName, const std::string &dName);
83     bool IsSameUid(int32_t sUid, int32_t dUid);
84 private:
85     bool initialized_;
86     std::recursive_mutex mutex_;
87     std::map<uint32_t, std::shared_ptr<DownloadServiceTask>> taskMap_;
88     std::queue<uint32_t> pendingQueue_;
89     std::queue<uint32_t> pausedQueue_;
90     std::vector<std::shared_ptr<DownloadThread>> threadList_;
91 
92     /* configuration for download service manager */
93     uint32_t interval_;
94     uint32_t threadNum_;
95     uint32_t timeoutRetry_;
96 
97     uint32_t taskId_;
98     static std::mutex instanceLock_;
99     static DownloadServiceManager* instance_;
100     std::mutex appStateMutex_;
101 };
102 } // namespace OHOS::Request::Download
103 #endif // DOWNLOAD_SERVICE_MANAGER_H
104