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