• 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 UPDATE_CLIENT_H
17 #define UPDATE_CLIENT_H
18 
19 #include "iupdater.h"
20 
21 namespace OHOS {
22 namespace UpdateEngine {
23 class UpdateClient : public IUpdater {
24 public:
25     class Napi {
26     public:
27         static constexpr const char *FUNCTION_ON = "on";
28         static constexpr const char *FUNCTION_OFF = "off";
29 
30         static napi_value NapiOn(napi_env env, napi_callback_info info);
31         static napi_value NapiOff(napi_env env, napi_callback_info info);
32     };
33 
34     UpdateClient(napi_env env, napi_value thisVar);
35     ~UpdateClient() override;
36 
37     // Obtain the online updater engine and return it through the sync API.
38     napi_value GetOnlineUpdater(napi_env env, napi_callback_info info);
39 
40     // Asynchronous API
41     napi_value CheckNewVersion(napi_env env, napi_callback_info info);
42     napi_value SetUpgradePolicy(napi_env env, napi_callback_info info);
43     napi_value GetUpgradePolicy(napi_env env, napi_callback_info info);
44     napi_value GetNewVersionInfo(napi_env env, napi_callback_info info);
45     napi_value GetNewVersionDescription(napi_env env, napi_callback_info info);
46     napi_value GetCurrentVersionInfo(napi_env env, napi_callback_info info);
47     napi_value GetCurrentVersionDescription(napi_env env, napi_callback_info info);
48     napi_value GetTaskInfo(napi_env env, napi_callback_info info);
49 
50     // Event mode, which is used to send the result to the JS.
51     napi_value CancelUpgrade(napi_env env, napi_callback_info info);
52     napi_value Download(napi_env env, napi_callback_info info);
53     napi_value ResumeDownload(napi_env env, napi_callback_info info);
54     napi_value PauseDownload(napi_env env, napi_callback_info info);
55     napi_value Upgrade(napi_env env, napi_callback_info info);
56     napi_value ClearError(napi_env env, napi_callback_info info);
57     napi_value TerminateUpgrade(napi_env env, napi_callback_info info);
58 
59     void GetUpdateResult(SessionType type, UpdateResult &result) override;
60 
61     // Notify the session.
62     void NotifyCheckVersionDone(const BusinessError &businessError, const CheckResult &checkResult) override;
63     void NotifyDownloadProgress(const BusinessError &businessError, const Progress &progress);
64     void NotifyUpgradeProgresss(const BusinessError &businessError, const Progress &progress);
65 
66     #ifdef UPDATER_UT
GetUpdateSession(uint32_t sessId)67     UpdateSession *GetUpdateSession(uint32_t sessId)
68     {
69     #ifndef UPDATER_API_TEST
70         std::lock_guard<std::mutex> guard(sessionMutex_);
71     #endif
72         auto iter = sessions_.find(sessId);
73         if (iter != sessions_.end()) {
74             return iter->second.get();
75         }
76         return nullptr;
77     }
78     #endif
79 private:
80     void InitCallback();
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 #ifndef UPDATER_API_TEST
87     std::mutex sessionMutex_;
88 #endif
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 UpdateEngine
115 } // namespace OHOS
116 #endif // UPDATE_CLIENT_H