• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 UPDATE_CLIENT_H
17 #define UPDATE_CLIENT_H
18 
19 #include "iupdater.h"
20 
21 namespace OHOS::UpdateEngine {
22 class UpdateClient : public IUpdater {
23 public:
24     class Napi {
25     public:
26         static constexpr const char *FUNCTION_ON = "on";
27         static constexpr const char *FUNCTION_OFF = "off";
28 
29         static napi_value NapiOn(napi_env env, napi_callback_info info);
30         static napi_value NapiOff(napi_env env, napi_callback_info info);
31     };
32 
33     UpdateClient(napi_env env, napi_value thisVar);
34     ~UpdateClient() override;
35 
36     // Obtain the online updater engine and return it through the sync API.
37     napi_value GetOnlineUpdater(napi_env env, napi_callback_info info);
38 
39     // Asynchronous API
40     napi_value CheckNewVersion(napi_env env, napi_callback_info info);
41     napi_value SetUpgradePolicy(napi_env env, napi_callback_info info);
42     napi_value GetUpgradePolicy(napi_env env, napi_callback_info info);
43     napi_value GetNewVersionInfo(napi_env env, napi_callback_info info);
44     napi_value GetNewVersionDescription(napi_env env, napi_callback_info info);
45     napi_value GetCurrentVersionInfo(napi_env env, napi_callback_info info);
46     napi_value GetCurrentVersionDescription(napi_env env, napi_callback_info info);
47     napi_value GetTaskInfo(napi_env env, napi_callback_info info);
48 
49     // Event mode, which is used to send the result to the JS.
50     napi_value CancelUpgrade(napi_env env, napi_callback_info info);
51     napi_value Download(napi_env env, napi_callback_info info);
52     napi_value ResumeDownload(napi_env env, napi_callback_info info);
53     napi_value PauseDownload(napi_env env, napi_callback_info info);
54     napi_value Upgrade(napi_env env, napi_callback_info info);
55     napi_value ClearError(napi_env env, napi_callback_info info);
56     napi_value TerminateUpgrade(napi_env env, napi_callback_info info);
57 
58     void GetUpdateResult(uint32_t type, UpdateResult &result) override;
59 
60     // Notify the session.
61     void NotifyDownloadProgress(const BusinessError &businessError, const Progress &progress);
62     void NotifyUpgradeProgresss(const BusinessError &businessError, const Progress &progress);
63 
64     #ifdef UPDATER_UT
GetUpdateSession(uint32_t sessId)65     UpdateSession *GetUpdateSession(uint32_t sessId)
66     {
67         std::lock_guard<std::mutex> guard(sessionMutex_);
68         auto iter = sessions_.find(sessId);
69         if (iter != sessions_.end()) {
70             return iter->second.get();
71         }
72         return nullptr;
73     }
74     #endif
75 
76 protected:
77     void RegisterCallback() override;
78     void UnRegisterCallback() override;
79 
80 private:
81     template <typename T>
82     ClientStatus ParseUpgOptions(napi_env env, napi_callback_info info, VersionDigestInfo &versionDigestInfo,
83         T &options);
84     template <typename T>
85     ClientStatus ParseUpgOptions(napi_env env, napi_callback_info info, T &options);
86 
87 private:
88     std::mutex sessionMutex_;
89     bool isInit_ = false;
90     std::string upgradeFile_;
91     std::string certsFile_;
92     UpgradeInfo upgradeInfo_ {};
93     UpgradePolicy upgradePolicy_ {};
94     Progress progress_ {};
95     Progress verifyProgress_ {};
96     CheckResult checkResult_ {};
97     NewVersionInfo newVersionInfo_ {};
98     VersionDescriptionInfo newVersionDescriptionInfo_ {};
99     CurrentVersionInfo currentVersionInfo_ {};
100     VersionDescriptionInfo currentVersionDescriptionInfo_ {};
101     TaskInfo taskInfo_ {};
102     VersionDigestInfo versionDigestInfo_ {};
103     DescriptionOptions descriptionOptions_ {};
104     UpgradeOptions upgradeOptions_;
105     ClearOptions clearOptions_ {};
106     DownloadOptions downloadOptions_ {};
107     ResumeDownloadOptions resumeDownloadOptions_ {};
108     PauseDownloadOptions pauseDownloadOptions_ {};
109 };
110 
111 #ifdef UPDATER_UT
112 napi_value UpdateClientInit(napi_env env, napi_value exports);
113 #endif
114 } // namespace OHOS::UpdateEngine
115 #endif // UPDATE_CLIENT_H