• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 UPDATER_THREAD_H
17 #define UPDATER_THREAD_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <thread>
22 #include "curl/curl.h"
23 #include "update_helper.h"
24 
25 namespace OHOS {
26 namespace UpdateEngine {
27 #define ENGINE_CHECK_NO_LOG(retCode, exper) \
28     if (!(retCode)) {                     \
29         exper;                            \
30     }
31 
32 constexpr uint32_t DOWNLOAD_FINISH_PERCENT = 100;
33 constexpr uint32_t DOWNLOAD_PERIOD_PERCENT = 1;
34 constexpr int32_t TIMEOUT_FOR_DOWNLOAD = 600;
35 #ifndef UPDATER_UT
36 constexpr int32_t TIMEOUT_FOR_CONNECT = 10;
37 #else
38 constexpr int32_t TIMEOUT_FOR_CONNECT = 1;
39 #endif
40 
41 class ProgressThread {
42 public:
43     ProgressThread() = default;
44     virtual ~ProgressThread();
45 protected:
46     int32_t StartProgress();
47     void StopProgress();
48     void ExitThread();
49     void ExecuteThreadFunc();
50 
51     virtual bool ProcessThreadExecute() = 0;
52     virtual void ProcessThreadExit() = 0;
53 
54 private:
55     std::thread *pDealThread_ { nullptr };
56     std::mutex mutex_;
57     std::condition_variable condition_;
58     bool isWake_ = false;
59     bool isExitThread_ = false;
60 };
61 
62 class DownloadThread : public ProgressThread {
63 public:
64     using ProgressCallback = std::function<int (const std::string &fileName, const Progress &progress)>;
DownloadThread(ProgressCallback callback)65     explicit DownloadThread(ProgressCallback callback) : ProgressThread(), callback_(callback) {}
~DownloadThread()66     ~DownloadThread() override
67     {
68         ProgressThread::ExitThread();
69     }
70 
71     int32_t StartDownload(const std::string &fileName, const std::string &url);
72     void StopDownload();
73 
74     static size_t GetLocalFileLength(const std::string &fileName);
75     static size_t WriteFunc(void *ptr, size_t size, size_t nmemb, const void *stream);
76     static int32_t DownloadProgress(const void *localData,
77         double dlTotal, double dlNow, double ulTotal, double ulNow);
78 
GetPackageSize()79     double GetPackageSize()
80     {
81         return static_cast<double>(packageSize_);
82     };
83 
84 protected:
85     bool ProcessThreadExecute() override;
86     void ProcessThreadExit() override;
87     int32_t DownloadCallback(uint32_t percent, UpgradeStatus status, const std::string &error);
88 
89 private:
90     Progress downloadProgress_ {};
91     ProgressCallback callback_;
92     CURL *downloadHandle_ { nullptr };
93     FILE *downloadFile_ { nullptr };
94     std::string serverUrl_;
95     std::atomic<bool> exitDownload_ { false };
96     size_t packageSize_ { 1 };
97     std::string downloadFileName_;
98 };
99 } // namespace UpdateEngine
100 } // namespace OHOS
101 #endif // UPDATER_THREAD_H
102