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