• 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_TASK_H
17 #define DOWNLOAD_TASK_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <iosfwd>
25 
26 #include "constant.h"
27 #include "curl/curl.h"
28 #include "download_config.h"
29 #include "download_info.h"
30 
31 namespace OHOS::Request::Download {
32     using DownloadTaskCallback = void(*)(const std::string& type, uint32_t taskId,
33                                          int64_t argv1, int64_t argv2, bool isNotify);
34 
35 class DownloadServiceTask {
36 public:
37     DownloadServiceTask(uint32_t taskId, const DownloadConfig &config);
38     ~DownloadServiceTask(void);
39 
40     uint32_t GetId() const;
41     bool Run();
42     bool Pause();
43     bool Resume();
44     bool Remove();
45     bool Query(DownloadInfo &info);
46     bool QueryMimeType(std::string &mimeType);
47 
48     void InstallCallback(DownloadTaskCallback cb);
49     void GetRunResult(DownloadStatus &status, ErrorCode &code, PausedReason &reason);
50 
51     void SetRetryTime(uint32_t retryTime);
52     void SetNetworkStatus(bool isOnline);
53     bool IsSatisfiedConfiguration();
54     void SetNotifyApp(bool isNotifyApp);
55     std::string GetTaskBundleName() const;
56     int32_t GetTaskApplicationInfoUid() const;
57 private:
58     void SetStatus(DownloadStatus status, ErrorCode code, PausedReason reason);
59     void SetStatus(DownloadStatus status);
60     void SetError(ErrorCode code);
61     void SetReason(PausedReason reason);
62 
63     void DumpStatus();
64     void DumpErrorCode();
65     void DumpPausedReason();
66 
67     bool ExecHttp();
68     CURLcode CurlPerformFileTransfer(CURL *handle) const;
69     bool SetFileSizeOption(CURL *curl, struct curl_slist *requestHeader);
70     bool SetOption(CURL *curl, struct curl_slist *requestHeader);
71     struct curl_slist *MakeHeaders(const std::vector<std::string> &vec);
72 
73     void SetResumeFromLarge(CURL *curl, long long pos);
74 
75     bool GetFileSize(int64_t &result);
76     std::string GetTmpPath();
77     void HandleResponseCode(CURLcode code, int32_t httpCode);
78     void HandleCleanup(DownloadStatus status);
79     static size_t WriteCallback(void *buffer, size_t size, size_t num, void *param);
80     static int ProgressCallback(void *param, double dltotal, double dlnow, double ultotal, double ulnow);
81 
82     bool CheckResumeCondition();
83     void ForceStopRunning();
84     bool HandleFileError();
85     bool SetCertificationOption(CURL *curl);
86     bool IsHttpsURL();
87     bool SetHttpCertificationOption(CURL *curl);
88     bool SetHttpsCertificationOption(CURL *curl);
89     std::string ReadCertification();
90     void RecordTaskEvent(int32_t httpCode);
91     void PublishNotification(bool background, uint32_t percent);
92     void PublishNotification(bool background, int64_t prevSize, int64_t downloadSize, int64_t totalSize);
93     std::time_t GetCurTimestamp();
94     uint32_t ProgressNotification(int64_t prevSize, int64_t downloadSize, int64_t totalSize);
95 private:
96     uint32_t taskId_;
97     DownloadConfig config_;
98 
99     DownloadStatus status_;
100     ErrorCode code_;
101     PausedReason reason_;
102     std::string mimeType_;
103     int64_t totalSize_;
104     int64_t downloadSize_;
105     bool isPartialMode_;
106 
107     bool forceStop_;
108     bool isRemoved_;
109     uint32_t retryTime_;
110 
111     DownloadTaskCallback eventCb_;
112     std::recursive_mutex mutex_;
113     bool hasFileSize_;
114     bool isOnline_;
115     int64_t prevSize_;
116 
117     std::time_t lastTimestamp_ = 0;
118     bool isNotifyApp_ = true;
119 };
120 } // namespace OHOS::Request::Download
121 #endif // DOWNLOAD_TASK_H
122