• 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 CheckResultEx &checkResultEx);
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     template <typename T>
81     ClientStatus ParseUpgOptions(napi_env env, napi_callback_info info, VersionDigestInfo &versionDigestInfo,
82         T &options);
83 #ifndef UPDATER_API_TEST
84     std::mutex sessionMutex_;
85 #endif
86     bool isInit_ = false;
87     std::string upgradeFile_;
88     std::string certsFile_;
89     UpgradeInfo upgradeInfo_ {};
90     UpgradePolicy upgradePolicy_ {};
91     Progress progress_ {};
92     Progress verifyProgress_ {};
93     VersionInfo versionInfo_ {};
94     CheckResultEx checkResultEx_ {};
95     NewVersionInfo newVersionInfo_ {};
96     VersionDescriptionInfo newVersionDescriptionInfo_ {};
97     CurrentVersionInfo currentVersionInfo_ {};
98     VersionDescriptionInfo currentVersionDescriptionInfo_ {};
99     TaskInfo taskInfo_ {};
100     VersionDigestInfo versionDigestInfo_ {};
101     DescriptionOptions descriptionOptions_ {};
102     UpgradeOptions upgradeOptions_;
103     ClearOptions clearOptions_ {};
104     DownloadOptions downloadOptions_ {};
105     ResumeDownloadOptions resumeDownloadOptions_ {};
106     PauseDownloadOptions pauseDownloadOptions_ {};
107 };
108 
109 #ifdef UPDATER_UT
110 napi_value UpdateClientInit(napi_env env, napi_value exports);
111 #endif
112 } // namespace UpdateEngine
113 } // namespace OHOS
114 #endif // UPDATE_CLIENT_H