1 /* 2 * Copyright (c) 2023 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 LEGACY_DOWNLOAD_TASK_H 17 #define LEGACY_DOWNLOAD_TASK_H 18 19 #include <cstdio> 20 #include <functional> 21 #include <thread> 22 #include <vector> 23 #include "curl/curl.h" 24 25 namespace OHOS::Request::Legacy { 26 class DownloadTask { 27 public: 28 struct DownloadOption { 29 std::string url_; 30 std::string filename_; 31 std::string fileDir_; 32 std::vector<std::string> header_; 33 }; 34 35 using DoneFunc = std::function<void(const std::string&, bool, const std::string&)>; 36 DownloadTask(const std::string &token, const DownloadOption &option, const DoneFunc &callback); 37 38 ~DownloadTask(); 39 40 void Start(); 41 42 void Run(); 43 44 bool DoDownload(); 45 46 void SetResumeFromLarge(CURL *curl, uint64_t pos); 47 private: 48 FILE *OpenDownloadFile() const; 49 50 uint32_t GetLocalFileSize(); 51 52 bool SetOption(CURL *handle, curl_slist *&headers); 53 54 void NotifyDone(bool successful, const std::string& errMsg = ""); 55 56 bool GetFileSize(uint32_t &result); 57 58 std::string taskId_; 59 DownloadOption option_; 60 DoneFunc callback_; 61 std::thread *thread_ {}; 62 FILE *filp_ {}; 63 char *errorBuffer_ {}; 64 static bool isCurlGlobalInited_; 65 66 uint32_t totalSize_; 67 bool hasFileSize_; 68 }; 69 } 70 #endif // LEGACY_DOWNLOAD_TASK_H