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