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