• 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 #include "update_client.h"
17 #include "client_helper.h"
18 #include "iupdate_service.h"
19 #include "napi_util.h"
20 #include "update_helper.h"
21 #include "update_service_kits.h"
22 #include "update_session.h"
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace UpdateEngine {
NapiOn(napi_env env,napi_callback_info info)28 napi_value UpdateClient::Napi::NapiOn(napi_env env, napi_callback_info info)
29 {
30     CLIENT_LOGI("UpdateClient::Napi::NapiOn");
31     UpdateClient *updater = UnwrapJsObject<UpdateClient>(env, info);
32     PARAM_CHECK_NAPI_CALL(env, updater != nullptr, return nullptr, "Error get UpdateClient");
33     return updater->On(env, info);
34 }
35 
NapiOff(napi_env env,napi_callback_info info)36 napi_value UpdateClient::Napi::NapiOff(napi_env env, napi_callback_info info)
37 {
38     CLIENT_LOGI("UpdateClient::Napi::NapiOff");
39     UpdateClient *updater = UnwrapJsObject<UpdateClient>(env, info);
40     PARAM_CHECK_NAPI_CALL(env, updater != nullptr, return nullptr, "Error get UpdateClient");
41     return updater->Off(env, info);
42 }
43 
UpdateClient(napi_env env,napi_value thisVar)44 UpdateClient::UpdateClient(napi_env env, napi_value thisVar)
45 {
46     napi_ref thisReference = nullptr;
47     napi_create_reference(env, thisVar, 1, &thisReference);
48     sessionsMgr_ = std::make_shared<SessionManager>(env, thisReference);
49 }
50 
~UpdateClient()51 UpdateClient::~UpdateClient()
52 {
53     if (sessionsMgr_ != nullptr) {
54         sessionsMgr_->Clear();
55     }
56 }
57 
GetOnlineUpdater(napi_env env,napi_callback_info info)58 napi_value UpdateClient::GetOnlineUpdater(napi_env env, napi_callback_info info)
59 {
60     napi_value result;
61     napi_create_int32(env, 0, &result);
62     size_t argc = MAX_ARGC;
63     napi_value args[MAX_ARGC] = {0};
64     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
65     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
66     std::vector<std::string> paramNames;
67     paramNames.push_back("upgradeInfo");
68     std::vector<std::string> paramTypes;
69     paramTypes.push_back("UpgradeInfo");
70     PARAM_CHECK_NAPI_CALL(env, argc >= 1,
71         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
72         return nullptr, "Invalid param");
73     PARAM_CHECK_NAPI_CALL(env, !isInit_, return result, "Has been init");
74 
75     ClientStatus ret = ClientHelper::GetUpgradeInfoFromArg(env, args[0], upgradeInfo_);
76     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
77         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
78         return nullptr, "Failed to get upgradeInfo param");
79 
80     UpdateCallbackInfo callback {
81         [=](const BusinessError &businessError, const CheckResultEx &checkResultEx) {
82             NotifyCheckVersionDone(businessError, checkResultEx);
83         },
84         [=](const EventInfo &eventInfo) {
85             NotifyEventInfo(eventInfo);
86         }
87     };
88     UpdateServiceKits::GetInstance().RegisterUpdateCallback(upgradeInfo_, callback);
89     isInit_ = true;
90     return result;
91 }
92 
CheckNewVersion(napi_env env,napi_callback_info info)93 napi_value UpdateClient::CheckNewVersion(napi_env env, napi_callback_info info)
94 {
95     versionInfo_.status = SYSTEM_ERROR;
96     SessionParams sessionParams(SessionType::SESSION_CHECK_VERSION, CALLBACK_POSITION_ONE, false, true);
97     napi_value ret = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
98         return UpdateServiceKits::GetInstance().CheckNewVersion(upgradeInfo_);
99     });
100     PARAM_CHECK(ret != nullptr, return nullptr, "Failed to start worker.");
101     return ret;
102 }
103 
CancelUpgrade(napi_env env,napi_callback_info info)104 napi_value UpdateClient::CancelUpgrade(napi_env env, napi_callback_info info)
105 {
106     size_t argc = MAX_ARGC;
107     napi_value args[MAX_ARGC] = {0};
108     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
109     PARAM_CHECK_NAPI_CALL(env, status == napi_ok && argc == 0, return nullptr, "Error get cb info");
110     CLIENT_LOGI("CancelUpgrade");
111     SessionParams sessionParams(SessionType::SESSION_CANCEL_UPGRADE, CALLBACK_POSITION_ONE, true);
112     std::shared_ptr<UpdateSession> sess = nullptr;
113     sess = std::make_shared<UpdateAsyncSessionNoCallback>(this, sessionParams, argc);
114     PARAM_CHECK_NAPI_CALL(env, sess != nullptr, return nullptr, "Failed to create update session");
115     sessionsMgr_->AddSession(sess);
116     napi_value retValue = sess->StartWork(
117         env,
118         args,
119         [=](SessionType type, void *context) -> int {
120             BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
121             return UpdateServiceKits::GetInstance().Cancel(upgradeInfo_, IUpdateService::DOWNLOAD, *businessError);
122         },
123         nullptr);
124     PARAM_CHECK(retValue != nullptr, sessionsMgr_->RemoveSession(sess->GetSessionId());
125         return nullptr, "Failed to start worker.");
126     return retValue;
127 }
128 
129 template <typename T>
ParseUpgOptions(napi_env env,napi_callback_info info,VersionDigestInfo & versionDigestInfo,T & options)130 ClientStatus UpdateClient::ParseUpgOptions(napi_env env, napi_callback_info info, VersionDigestInfo &versionDigestInfo,
131     T &options)
132 {
133     size_t argc = MAX_ARGC;
134     napi_value args[MAX_ARGC] = {0};
135     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
136     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return ClientStatus::CLIENT_INVALID_PARAM, "Error get cb info");
137 
138     ClientStatus ret = ClientHelper::GetVersionDigestInfoFromArg(env, args[0], versionDigestInfo);
139     std::vector<std::string> paramNames;
140     paramNames.push_back("versionDigestInfo");
141     std::vector<std::string> paramTypes;
142     paramTypes.push_back("VersionDigestInfo");
143     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS, ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
144         return ClientStatus::CLIENT_INVALID_PARAM, "Failed to get versionDigestInfo param");
145 
146     ret = ClientHelper::GetOptionsFromArg(env, args[1], options);
147     std::vector<std::string> names;
148     names.push_back("descriptionOptions");
149     std::vector<std::string> types;
150     types.push_back("DescriptionOptions");
151     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS, ClientHelper::NapiThrowParamError(env, names, types);
152         return ClientStatus::CLIENT_INVALID_PARAM, "Failed to get Options param");
153     return ClientStatus::CLIENT_SUCCESS;
154 }
155 
Download(napi_env env,napi_callback_info info)156 napi_value UpdateClient::Download(napi_env env, napi_callback_info info)
157 {
158     size_t argc = MAX_ARGC;
159     napi_value args[MAX_ARGC] = {0};
160     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
161     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
162     CLIENT_LOGI("Download");
163 
164     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, downloadOptions_);
165     std::vector<std::string> paramNames;
166     paramNames.push_back("versionDigestInfo");
167     paramNames.push_back("downloadOptions");
168     std::vector<std::string> paramTypes;
169     paramTypes.push_back("VersionDigestInfo");
170     paramTypes.push_back("DownloadOptions");
171     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
172         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
173         return nullptr, "Failed to get Download param");
174 
175     SessionParams sessionParams(SessionType::SESSION_DOWNLOAD, CALLBACK_POSITION_THREE, true);
176     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
177         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
178         return UpdateServiceKits::GetInstance().Download(
179             upgradeInfo_, versionDigestInfo_, downloadOptions_, *businessError);
180     });
181     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
182     return retValue;
183 }
184 
PauseDownload(napi_env env,napi_callback_info info)185 napi_value UpdateClient::PauseDownload(napi_env env, napi_callback_info info)
186 {
187     size_t argc = MAX_ARGC;
188     napi_value args[MAX_ARGC] = {0};
189     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
190     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
191 
192     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, pauseDownloadOptions_);
193     std::vector<std::string> paramNames;
194     paramNames.push_back("versionDigestInfo");
195     paramNames.push_back("pauseDownloadOptions");
196     std::vector<std::string> paramTypes;
197     paramTypes.push_back("VersionDigestInfo");
198     paramTypes.push_back("PauseDownloadOptions");
199     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
200         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
201         return nullptr, "Failed to get pauseDownloadOptions param");
202 
203     SessionParams sessionParams(SessionType::SESSION_PAUSE_DOWNLOAD, CALLBACK_POSITION_THREE, true);
204     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
205         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
206         return UpdateServiceKits::GetInstance().PauseDownload(
207             upgradeInfo_, versionDigestInfo_, pauseDownloadOptions_, *businessError);
208     });
209     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
210     return retValue;
211 }
212 
ResumeDownload(napi_env env,napi_callback_info info)213 napi_value UpdateClient::ResumeDownload(napi_env env, napi_callback_info info)
214 {
215     size_t argc = MAX_ARGC;
216     napi_value args[MAX_ARGC] = {0};
217     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
218     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
219 
220     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, resumeDownloadOptions_);
221     std::vector<std::string> paramNames;
222     paramNames.push_back("versionDigestInfo");
223     paramNames.push_back("resumeDownloadOptions");
224     std::vector<std::string> paramTypes;
225     paramTypes.push_back("VersionDigestInfo");
226     paramTypes.push_back("ResumeDownloadOptions");
227     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
228         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
229         return nullptr, "Failed to get resumeDownloadOptions param");
230 
231     SessionParams sessionParams(SessionType::SESSION_RESUME_DOWNLOAD, CALLBACK_POSITION_THREE, true);
232     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
233         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
234         return UpdateServiceKits::GetInstance().ResumeDownload(
235             upgradeInfo_, versionDigestInfo_, resumeDownloadOptions_, *businessError);
236     });
237     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
238     return retValue;
239 }
240 
Upgrade(napi_env env,napi_callback_info info)241 napi_value UpdateClient::Upgrade(napi_env env, napi_callback_info info)
242 {
243     size_t argc = MAX_ARGC;
244     napi_value args[MAX_ARGC] = {0};
245     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
246     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
247 
248     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, upgradeOptions_);
249     std::vector<std::string> paramNames;
250     paramNames.push_back("versionDigestInfo");
251     paramNames.push_back("upgradeOptions");
252     std::vector<std::string> paramTypes;
253     paramTypes.push_back("VersionDigestInfo");
254     paramTypes.push_back("UpgradeOptions");
255     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
256         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
257         return nullptr, "Failed to get resumeDownloadOptions param");
258 
259     SessionParams sessionParams(SessionType::SESSION_UPGRADE, CALLBACK_POSITION_THREE, true);
260     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
261 #ifndef UPDATER_API_TEST
262         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
263         return UpdateServiceKits::GetInstance().Upgrade(
264             upgradeInfo_, versionDigestInfo_, upgradeOptions_, *businessError);
265 #else
266         return INT_CALL_SUCCESS;
267 #endif
268     });
269     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
270     return retValue;
271 }
272 
ClearError(napi_env env,napi_callback_info info)273 napi_value UpdateClient::ClearError(napi_env env, napi_callback_info info)
274 {
275     size_t argc = MAX_ARGC;
276     napi_value args[MAX_ARGC] = {0};
277     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
278     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
279 
280     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, clearOptions_);
281     std::vector<std::string> paramNames;
282     paramNames.push_back("versionDigestInfo");
283     paramNames.push_back("clearOptions");
284     std::vector<std::string> paramTypes;
285     paramTypes.push_back("VersionDigestInfo");
286     paramTypes.push_back("ClearOptions");
287     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
288         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
289         return nullptr, "Failed to get clearOptions param");
290 
291     SessionParams sessionParams(SessionType::SESSION_CLEAR_ERROR, CALLBACK_POSITION_THREE, true);
292     napi_value retValue = StartSession(env, info, sessionParams,
293         [=](SessionType type, void *context) -> int {
294             BusinessError *businessError = reinterpret_cast<BusinessError*>(context);
295             return UpdateServiceKits::GetInstance().ClearError(
296                 upgradeInfo_, versionDigestInfo_, clearOptions_, *businessError);
297     });
298     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
299     return retValue;
300 }
301 
TerminateUpgrade(napi_env env,napi_callback_info info)302 napi_value UpdateClient::TerminateUpgrade(napi_env env, napi_callback_info info)
303 {
304     size_t argc = MAX_ARGC;
305     napi_value args[MAX_ARGC] = {0};
306     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
307     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
308 
309     SessionParams sessionParams(SessionType::SESSION_TERMINATE_UPGRADE, CALLBACK_POSITION_ONE, true);
310     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
311         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
312         return UpdateServiceKits::GetInstance().TerminateUpgrade(upgradeInfo_, *businessError);
313     });
314     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
315     return retValue;
316 }
317 
SetUpgradePolicy(napi_env env,napi_callback_info info)318 napi_value UpdateClient::SetUpgradePolicy(napi_env env, napi_callback_info info)
319 {
320     size_t argc = MAX_ARGC;
321     napi_value args[MAX_ARGC] = {0};
322     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
323     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
324 
325     ClientStatus ret = ClientHelper::GetUpgradePolicyFromArg(env, args[0], upgradePolicy_);
326     std::vector<std::string> paramNames;
327     paramNames.push_back("upgradePolicy");
328     std::vector<std::string> paramTypes;
329     paramTypes.push_back("UpgradePolicy");
330     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
331         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
332         return nullptr, "Failed to get upgradePolicy param");
333 
334     SessionParams sessionParams(SessionType::SESSION_SET_POLICY, CALLBACK_POSITION_TWO, true);
335     napi_value retValue = StartSession(env, info, sessionParams,
336         [&](SessionType type, void *context) -> int {
337             BusinessError *businessError = reinterpret_cast<BusinessError*>(context);
338             return UpdateServiceKits::GetInstance().SetUpgradePolicy(upgradeInfo_, upgradePolicy_, *businessError);
339     });
340     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to SetUpgradePolicy.");
341     return retValue;
342 }
343 
GetUpgradePolicy(napi_env env,napi_callback_info info)344 napi_value UpdateClient::GetUpgradePolicy(napi_env env, napi_callback_info info)
345 {
346     SessionParams sessionParams(SessionType::SESSION_GET_POLICY, CALLBACK_POSITION_ONE, true);
347     napi_value retValue = StartSession(env, info, sessionParams,
348         [=](SessionType type, void *context) -> int {
349             BusinessError *businessError = reinterpret_cast<BusinessError*>(context);
350             return UpdateServiceKits::GetInstance().GetUpgradePolicy(upgradeInfo_, upgradePolicy_, *businessError);
351     });
352     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to UpgradeVersion.");
353     return retValue;
354 }
355 
GetNewVersionInfo(napi_env env,napi_callback_info info)356 napi_value UpdateClient::GetNewVersionInfo(napi_env env, napi_callback_info info)
357 {
358     SessionParams sessionParams(SessionType::SESSION_GET_NEW_VERSION, CALLBACK_POSITION_ONE, true);
359     napi_value retValue = StartSession(env, info, sessionParams,
360         [=](SessionType type, void *context) -> int {
361             BusinessError *businessError = reinterpret_cast<BusinessError*>(context);
362             return UpdateServiceKits::GetInstance().GetNewVersionInfo(upgradeInfo_, newVersionInfo_, *businessError);
363     });
364     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo.");
365     return retValue;
366 }
367 
GetNewVersionDescription(napi_env env,napi_callback_info info)368 napi_value UpdateClient::GetNewVersionDescription(napi_env env, napi_callback_info info)
369 {
370     size_t argc = MAX_ARGC;
371     napi_value args[MAX_ARGC] = {0};
372     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
373     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
374     CLIENT_LOGI("GetNewVersionDescription");
375 
376     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, descriptionOptions_);
377     std::vector<std::string> paramNames;
378     paramNames.push_back("versionDigestInfo");
379     paramNames.push_back("descriptionOptions");
380     std::vector<std::string> paramTypes;
381     paramTypes.push_back("VersionDigestInfo");
382     paramTypes.push_back("DescriptionOptions");
383     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
384         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
385         return nullptr, "Failed to get GetNewVersionDescription param");
386 
387     SessionParams sessionParams(SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION, CALLBACK_POSITION_THREE, true);
388     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
389         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
390         return UpdateServiceKits::GetInstance().GetNewVersionDescription(
391             upgradeInfo_, versionDigestInfo_, descriptionOptions_, newVersionDescriptionInfo_, *businessError);
392     });
393     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
394     return retValue;
395 }
396 
GetCurrentVersionInfo(napi_env env,napi_callback_info info)397 napi_value UpdateClient::GetCurrentVersionInfo(napi_env env, napi_callback_info info)
398 {
399     SessionParams sessionParams(SessionType::SESSION_GET_CUR_VERSION, CALLBACK_POSITION_ONE, true);
400     napi_value retValue = StartSession(env, info, sessionParams,
401         [=](SessionType type, void *context) -> int {
402             BusinessError *businessError = reinterpret_cast<BusinessError*>(context);
403             return UpdateServiceKits::GetInstance().GetCurrentVersionInfo(upgradeInfo_, currentVersionInfo_,
404                 *businessError);
405     });
406     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to GetCurrentVersionInfo.");
407     return retValue;
408 }
409 
GetCurrentVersionDescription(napi_env env,napi_callback_info info)410 napi_value UpdateClient::GetCurrentVersionDescription(napi_env env, napi_callback_info info)
411 {
412     size_t argc = MAX_ARGC;
413     napi_value args[MAX_ARGC] = {0};
414     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
415     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info");
416     CLIENT_LOGI("GetCurrentVersionDescription");
417 
418     ClientStatus ret = ParseUpgOptions(env, info, versionDigestInfo_, descriptionOptions_);
419     std::vector<std::string> paramNames;
420     paramNames.push_back("versionDigestInfo");
421     paramNames.push_back("descriptionOptions");
422     std::vector<std::string> paramTypes;
423     paramTypes.push_back("VersionDigestInfo");
424     paramTypes.push_back("DescriptionOptions");
425     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS,
426         ClientHelper::NapiThrowParamError(env, paramNames, paramTypes);
427         return nullptr, "Failed to get GetCurrentVersionDescription param");
428 
429     SessionParams sessionParams(SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION, CALLBACK_POSITION_TWO, true);
430     napi_value retValue = StartSession(env, info, sessionParams, [=](SessionType type, void *context) -> int {
431         BusinessError *businessError = reinterpret_cast<BusinessError *>(context);
432         return UpdateServiceKits::GetInstance().GetCurrentVersionDescription(
433             upgradeInfo_, descriptionOptions_, currentVersionDescriptionInfo_, *businessError);
434     });
435     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to start worker.");
436     return retValue;
437 }
438 
GetTaskInfo(napi_env env,napi_callback_info info)439 napi_value UpdateClient::GetTaskInfo(napi_env env, napi_callback_info info)
440 {
441     SessionParams sessionParams(SessionType::SESSION_GET_TASK_INFO, CALLBACK_POSITION_ONE, true);
442     napi_value retValue = StartSession(env, info, sessionParams,
443         [=](SessionType type, void *context) -> int {
444             BusinessError *businessError = reinterpret_cast<BusinessError*>(context);
445             return UpdateServiceKits::GetInstance().GetTaskInfo(upgradeInfo_, taskInfo_, *businessError);
446     });
447     PARAM_CHECK(retValue != nullptr, return nullptr, "Failed to GetTaskInfo.");
448     return retValue;
449 }
450 
NotifyCheckVersionDone(const BusinessError & businessError,const CheckResultEx & checkResultEx)451 void UpdateClient::NotifyCheckVersionDone(const BusinessError &businessError, const CheckResultEx &checkResultEx)
452 {
453     CLIENT_LOGI("NotifyCheckVersionDone businessError %{public}d", static_cast<int32_t> (businessError.errorNum));
454     CLIENT_LOGI("NotifyCheckVersionDone isExistNewVersion %{public}d", checkResultEx.isExistNewVersion);
455     checkResultEx_ = checkResultEx;
456 }
457 
GetUpdateResult(SessionType type,UpdateResult & result)458 void UpdateClient::GetUpdateResult(SessionType type, UpdateResult &result)
459 {
460     CLIENT_LOGI("GetUpdateResult type %{public}d", type);
461     result.type = type;
462     switch (type) {
463         case SessionType::SESSION_CHECK_VERSION:
464             result.result.checkResultEx = &checkResultEx_;
465             result.buildJSObject = ClientHelper::BuildCheckResultEx;
466             break;
467         case SessionType::SESSION_GET_NEW_VERSION:
468             result.result.newVersionInfo = &newVersionInfo_;
469             result.buildJSObject = ClientHelper::BuildNewVersionInfo;
470             break;
471         case SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION:
472             result.result.versionDescriptionInfo = &newVersionDescriptionInfo_;
473             result.buildJSObject = ClientHelper::BuildVersionDescriptionInfo;
474             break;
475         case SessionType::SESSION_GET_TASK_INFO:
476             result.result.taskInfo = &taskInfo_;
477             result.buildJSObject = ClientHelper::BuildTaskInfo;
478             break;
479         case SessionType::SESSION_GET_CUR_VERSION:
480             result.result.currentVersionInfo = &currentVersionInfo_;
481             result.buildJSObject = ClientHelper::BuildCurrentVersionInfo;
482             break;
483         case SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION:
484             result.result.versionDescriptionInfo = &currentVersionDescriptionInfo_;
485             result.buildJSObject = ClientHelper::BuildVersionDescriptionInfo;
486             break;
487         case SessionType::SESSION_GET_POLICY:
488             result.result.upgradePolicy = &upgradePolicy_;
489             result.buildJSObject = ClientHelper::BuildUpgradePolicy;
490             break;
491         default:
492             result.buildJSObject = ClientHelper::BuildUndefinedStatus;
493             break;
494     }
495 }
496 } // namespace UpdateEngine
497 } // namespace OHOS