• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "client_helper.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "node_api.h"
22 #include "unistd.h"
23 
24 #include "napi_common_define.h"
25 #include "update_define.h"
26 #include "update_helper.h"
27 
28 namespace OHOS::UpdateEngine {
TrimString(std::string & str)29 void ClientHelper::TrimString(std::string &str)
30 {
31     str.erase(0, str.find_first_not_of(" "));
32     str.erase(str.find_last_not_of(" ") + 1);
33 }
34 
IsValidUpgradeFile(const std::string & upgradeFile)35 bool ClientHelper::IsValidUpgradeFile(const std::string &upgradeFile)
36 {
37     if (upgradeFile.empty()) {
38         return false;
39     }
40 
41     std::string::size_type pos = upgradeFile.find_first_of('/');
42     if (pos != 0) {
43         return false;
44     }
45 
46     pos = upgradeFile.find_last_of('.');
47     if (pos == std::string::npos) {
48         return false;
49     }
50 
51     std::string postfix = upgradeFile.substr(pos + 1);
52     std::transform(postfix.begin(), postfix.end(), postfix.begin(), ::tolower);
53     if (postfix.compare("bin") == 0) {
54         return true;
55     } else if (postfix.compare("zip") == 0) {
56         return true;
57     } else if (postfix.compare("lz4") == 0) {
58         return true;
59     } else if (postfix.compare("gz") == 0) {
60         return true;
61     }
62     return false;
63 }
64 
BuildDescInfo(napi_env env,napi_value & obj,const ComponentDescription & componentDescription)65 void BuildDescInfo(napi_env env, napi_value &obj, const ComponentDescription &componentDescription)
66 {
67     napi_value napiDescriptInfo;
68     napi_create_object(env, &napiDescriptInfo);
69     NapiCommonUtils::SetInt32(env, napiDescriptInfo, "descriptionType",
70         static_cast<int32_t>(componentDescription.descriptionInfo.descriptionType));
71     NapiCommonUtils::SetString(env, napiDescriptInfo, "content", componentDescription.descriptionInfo.content.c_str());
72     napi_set_named_property(env, obj, "descriptionInfo", napiDescriptInfo);
73 
74     napi_value napiNotifyDescriptInfo;
75     napi_create_object(env, &napiNotifyDescriptInfo);
76     NapiCommonUtils::SetInt32(env, napiNotifyDescriptInfo, "descriptionType",
77         static_cast<int32_t>(componentDescription.notifyDescriptionInfo.descriptionType));
78     NapiCommonUtils::SetString(env, napiNotifyDescriptInfo, "content",
79         componentDescription.notifyDescriptionInfo.content.c_str());
80     napi_set_named_property(env, obj, "notifyDescriptionInfo", napiNotifyDescriptInfo);
81 }
82 
IsValidData(const ErrorMessage & errorMessage)83 bool IsValidData(const ErrorMessage &errorMessage)
84 {
85     return errorMessage.errorCode != 0;
86 }
87 
IsValidData(const ComponentDescription & componentDescription)88 bool IsValidData(const ComponentDescription &componentDescription)
89 {
90     return componentDescription.componentId != "";
91 }
92 
IsValidData(const VersionComponent & versionComponent)93 bool IsValidData(const VersionComponent &versionComponent)
94 {
95     return versionComponent.componentType != CAST_INT(ComponentType::INVALID);
96 }
97 
98 template <typename T>
GetValidDataCount(const std::vector<T> & list)99 size_t GetValidDataCount(const std::vector<T> &list)
100 {
101     size_t validDataCount = 0;
102     for (size_t i = 0; i < list.size(); i++) {
103         if (IsValidData(list[i])) {
104             validDataCount++;
105         }
106     }
107     return validDataCount;
108 }
109 
BuildComponentDescriptions(napi_env env,napi_value & obj,const std::vector<ComponentDescription> & componentDescriptions)110 void BuildComponentDescriptions(napi_env env, napi_value &obj, const std::vector<ComponentDescription>
111     &componentDescriptions)
112 {
113     size_t validComponentCount = GetValidDataCount(componentDescriptions);
114     if (validComponentCount == 0) {
115         return;
116     }
117     napi_create_array_with_length(env, validComponentCount, &obj);
118     napi_status status;
119     size_t index = 0;
120     for (size_t i = 0; (i < componentDescriptions.size()) && (index < validComponentCount); i++) {
121         if (IsValidData(componentDescriptions[i])) {
122             napi_value napiComponentDescription;
123             status = napi_create_object(env, &napiComponentDescription);
124             PARAM_CHECK(status == napi_ok, continue, "Failed to napi_create_object %d", static_cast<int32_t>(status));
125             NapiCommonUtils::SetString(env, napiComponentDescription, "componentId",
126                 componentDescriptions[i].componentId.c_str());
127             BuildDescInfo(env, napiComponentDescription, componentDescriptions[i]);
128             napi_set_element(env, obj, index, napiComponentDescription);
129             index++;
130         }
131     }
132 }
133 
BuildVersionComponents(napi_env env,napi_value & obj,const std::vector<VersionComponent> & versionComponents)134 void BuildVersionComponents(napi_env env, napi_value &obj, const std::vector<VersionComponent> &versionComponents)
135 {
136     size_t validComponentCount = GetValidDataCount(versionComponents);
137     if (validComponentCount == 0) {
138         return;
139     }
140     napi_value napiVersionComponents;
141     napi_create_array_with_length(env, validComponentCount, &napiVersionComponents);
142     napi_status status;
143     size_t index = 0;
144     for (size_t i = 0; (i < versionComponents.size()) && (index < validComponentCount); i++) {
145         if (IsValidData(versionComponents[i])) {
146             napi_value napiVersionComponent;
147             status = napi_create_object(env, &napiVersionComponent);
148             PARAM_CHECK(status == napi_ok, continue, "Failed to napi_create_object %d", static_cast<int32_t>(status));
149             NapiCommonUtils::SetString(env, napiVersionComponent, "componentId",
150                 versionComponents[i].componentId.c_str());
151             NapiCommonUtils::SetInt32(env, napiVersionComponent, "componentType", versionComponents[i].componentType);
152             NapiCommonUtils::SetString(env, napiVersionComponent, "upgradeAction",
153                 versionComponents[i].upgradeAction.c_str());
154             NapiCommonUtils::SetString(env, napiVersionComponent, "displayVersion",
155                 versionComponents[i].displayVersion.c_str());
156             NapiCommonUtils::SetString(env, napiVersionComponent, "innerVersion",
157                 versionComponents[i].innerVersion.c_str());
158             NapiCommonUtils::SetInt64(env, napiVersionComponent, "size", versionComponents[i].size);
159             NapiCommonUtils::SetInt32(env, napiVersionComponent, "effectiveMode", versionComponents[i].effectiveMode);
160             ComponentDescription componentDescription = { .descriptionInfo = versionComponents[i].descriptionInfo };
161             BuildDescInfo(env, napiVersionComponent, componentDescription);
162             NapiCommonUtils::SetString(env, napiVersionComponent, "componentExtra",
163                 versionComponents[i].componentExtra.c_str());
164             napi_set_element(env, napiVersionComponents, index, napiVersionComponent);
165             index++;
166         }
167     }
168     napi_set_named_property(env, obj, "versionComponents", napiVersionComponents);
169 }
170 
BuildCurrentVersionInfo(napi_env env,napi_value & obj,const UpdateResult & result)171 int32_t ClientHelper::BuildCurrentVersionInfo(napi_env env, napi_value &obj, const UpdateResult &result)
172 {
173     ENGINE_LOGI("BuildCurrentVersionInfo");
174     PARAM_CHECK(result.result.currentVersionInfo != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS),
175         "ClientHelper::BuildCurrentVersionInfo null");
176     PARAM_CHECK(result.type == SessionType::SESSION_GET_CUR_VERSION,
177         return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE), "invalid type %{public}d", result.type);
178 
179     napi_status status = napi_create_object(env, &obj);
180     PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE),
181         "Failed to create napi_create_object %d", static_cast<int32_t>(status));
182 
183     CurrentVersionInfo *info = result.result.currentVersionInfo;
184     PARAM_CHECK(info != nullptr, return CAST_INT(ClientStatus::CLIENT_FAIL), "info is null");
185 
186     NapiCommonUtils::SetString(env, obj, "osVersion", info->osVersion);
187     NapiCommonUtils::SetString(env, obj, "deviceName", info->deviceName);
188     BuildVersionComponents(env, obj, info->versionComponents);
189     return CAST_INT(ClientStatus::CLIENT_SUCCESS);
190 }
191 
BuildVersionDigestInfo(napi_env env,napi_value & obj,const VersionDigestInfo & versionDigestInfo)192 void BuildVersionDigestInfo(napi_env env, napi_value &obj, const VersionDigestInfo &versionDigestInfo)
193 {
194     napi_value napiVersionDigestInfo;
195     napi_create_object(env, &napiVersionDigestInfo);
196     NapiCommonUtils::SetString(env, napiVersionDigestInfo, "versionDigest", versionDigestInfo.versionDigest);
197     napi_set_named_property(env, obj, "versionDigestInfo", napiVersionDigestInfo);
198 }
199 
BuildErrorMessages(napi_env env,napi_value & obj,const std::string & name,const std::vector<ErrorMessage> & errorMessages)200 void BuildErrorMessages(
201     napi_env env, napi_value &obj, const std::string &name, const std::vector<ErrorMessage> &errorMessages)
202 {
203     size_t validErrorMsgCount = GetValidDataCount(errorMessages);
204     if (validErrorMsgCount == 0) {
205         return;
206     }
207 
208     napi_value napiErrorMessages;
209     napi_create_array_with_length(env, validErrorMsgCount, &napiErrorMessages);
210     size_t index = 0;
211     for (size_t i = 0; (i < errorMessages.size()) && (index < validErrorMsgCount); i++) {
212         if (IsValidData(errorMessages[i])) {
213             napi_value napiErrorMessage;
214             napi_create_object(env, &napiErrorMessage);
215             NapiCommonUtils::SetInt32(env, napiErrorMessage, "errorCode", errorMessages[i].errorCode);
216             NapiCommonUtils::SetString(env, napiErrorMessage, "errorMessage", errorMessages[i].errorMessage);
217             napi_set_element(env, napiErrorMessages, index, napiErrorMessage);
218             index++;
219         }
220     }
221     napi_set_named_property(env, obj, name.c_str(), napiErrorMessages);
222 }
223 
BuildTaskBody(napi_env env,napi_value & obj,const TaskBody & taskBody)224 void BuildTaskBody(napi_env env, napi_value &obj, const TaskBody &taskBody)
225 {
226     napi_value napiTaskBody;
227     napi_create_object(env, &napiTaskBody);
228     BuildVersionDigestInfo(env, napiTaskBody, taskBody.versionDigestInfo);
229     NapiCommonUtils::SetInt32(env, napiTaskBody, "status", CAST_INT(taskBody.status));
230     NapiCommonUtils::SetInt32(env, napiTaskBody, "subStatus", taskBody.subStatus);
231     NapiCommonUtils::SetInt32(env, napiTaskBody, "progress", taskBody.progress);
232     NapiCommonUtils::SetInt32(env, napiTaskBody, "installMode", taskBody.installMode);
233     BuildErrorMessages(env, napiTaskBody, "errorMessages", taskBody.errorMessages);
234     BuildVersionComponents(env, napiTaskBody, taskBody.versionComponents);
235     napi_set_named_property(env, obj, "taskBody", napiTaskBody);
236 }
237 
BuildTaskInfo(napi_env env,napi_value & obj,const UpdateResult & result)238 int32_t ClientHelper::BuildTaskInfo(napi_env env, napi_value &obj, const UpdateResult &result)
239 {
240     ENGINE_LOGI("BuildTaskInfo");
241     PARAM_CHECK(result.result.taskInfo != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS),
242         "ClientHelper::BuildTaskInfo null");
243 
244     napi_status status = napi_create_object(env, &obj);
245     PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE),
246         "Failed to create napi_create_object %d", static_cast<int32_t>(status));
247 
248     NapiCommonUtils::SetBool(env, obj, "existTask", result.result.taskInfo->existTask);
249     if (result.result.taskInfo->existTask) {
250         BuildTaskBody(env, obj, result.result.taskInfo->taskBody);
251     }
252     return CAST_INT(ClientStatus::CLIENT_SUCCESS);
253 }
254 
BuildNewVersionInfo(napi_env env,napi_value & obj,const UpdateResult & result)255 int32_t ClientHelper::BuildNewVersionInfo(napi_env env, napi_value &obj, const UpdateResult &result)
256 {
257     ENGINE_LOGI("BuildNewVersionInfo");
258     PARAM_CHECK(result.result.newVersionInfo != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS),
259         "ClientHelper::BuildNewVersionInfo null");
260     PARAM_CHECK(result.type == SessionType::SESSION_GET_NEW_VERSION,
261         return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE),
262         "invalid type %d",
263         result.type);
264 
265     napi_status status = napi_create_object(env, &obj);
266     PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE),
267         "Failed to create napi_create_object %d", static_cast<int32_t>(status));
268 
269     BuildVersionDigestInfo(env, obj, result.result.newVersionInfo->versionDigestInfo);
270     BuildVersionComponents(env, obj, result.result.newVersionInfo->versionComponents);
271     return CAST_INT(ClientStatus::CLIENT_SUCCESS);
272 }
273 
BuildVersionDescriptionInfo(napi_env env,napi_value & obj,const UpdateResult & result)274 int32_t ClientHelper::BuildVersionDescriptionInfo(napi_env env, napi_value &obj, const UpdateResult &result)
275 {
276     ENGINE_LOGI("BuildVersionDescriptionInfo");
277     PARAM_CHECK(result.result.versionDescriptionInfo != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS),
278         "ClientHelper::BuildVersionDescriptionInfo null");
279 
280     PARAM_CHECK(result.type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION ||
281         result.type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION,
282         return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE), "invalid type %{public}d", result.type);
283 
284     VersionDescriptionInfo *info = result.result.versionDescriptionInfo;
285     PARAM_CHECK(info != nullptr, return CAST_INT(ClientStatus::CLIENT_FAIL), "info is null");
286 
287     BuildComponentDescriptions(env, obj, info->componentDescriptions);
288     PARAM_CHECK(obj != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS), "BuildComponentDescriptions null");
289     return CAST_INT(ClientStatus::CLIENT_SUCCESS);
290 }
291 
BuildCheckResult(napi_env env,napi_value & obj,const UpdateResult & result)292 int32_t ClientHelper::BuildCheckResult(napi_env env, napi_value &obj, const UpdateResult &result)
293 {
294     ENGINE_LOGI("BuildCheckResult");
295     PARAM_CHECK(result.result.checkResult != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS),
296         "ClientHelper::BuildCheckResult null");
297     PARAM_CHECK(result.type == SessionType::SESSION_CHECK_VERSION,
298         return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE), "invalid type %d", result.type);
299 
300     napi_status status = napi_create_object(env, &obj);
301     PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE),
302         "Failed to create napi_create_object %d", static_cast<int32_t>(status));
303     CheckResult *checkResult = result.result.checkResult;
304     NapiCommonUtils::SetBool(env, obj, "isExistNewVersion", checkResult->isExistNewVersion);
305 
306     if (checkResult->isExistNewVersion) {
307         napi_value newVersionInfo;
308         napi_create_object(env, &newVersionInfo);
309         BuildVersionDigestInfo(env, newVersionInfo, checkResult->newVersionInfo.versionDigestInfo);
310         BuildVersionComponents(env, newVersionInfo, checkResult->newVersionInfo.versionComponents);
311         napi_set_named_property(env, obj, "newVersionInfo", newVersionInfo);
312     }
313     return CAST_INT(ClientStatus::CLIENT_SUCCESS);
314 }
315 
BuildUpgradePolicy(napi_env env,napi_value & obj,const UpdateResult & result)316 int32_t ClientHelper::BuildUpgradePolicy(napi_env env, napi_value &obj, const UpdateResult &result)
317 {
318     PARAM_CHECK(result.result.upgradePolicy != nullptr, return CAST_INT(ClientStatus::CLIENT_SUCCESS),
319         "ClientHelper::BuildUpgradePolicy null");
320     PARAM_CHECK(result.type == SessionType::SESSION_GET_POLICY,
321         return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE), "invalid type %d", result.type);
322     napi_status status = napi_create_object(env, &obj);
323     PARAM_CHECK(status == napi_ok, return status, "Failed to create napi_create_object %d", status);
324     UpgradePolicy &upgradePolicy = *result.result.upgradePolicy;
325 
326     // Add the result.
327     NapiCommonUtils::SetBool(env, obj, "downloadStrategy", upgradePolicy.downloadStrategy);
328     NapiCommonUtils::SetBool(env, obj, "autoUpgradeStrategy", upgradePolicy.autoUpgradeStrategy);
329 
330     napi_value autoUpgradePeriods;
331     size_t count = COUNT_OF(upgradePolicy.autoUpgradePeriods);
332     status = napi_create_array_with_length(env, count, &autoUpgradePeriods);
333     PARAM_CHECK(status == napi_ok, return status, "Failed to create array for interval %d", status);
334     for (size_t i = 0; i < count; i++) {
335         napi_value result;
336         status = napi_create_object(env, &result);
337         PARAM_CHECK(status == napi_ok, continue, "Failed to napi_create_object %d", static_cast<int32_t>(status));
338         NapiCommonUtils::SetInt32(env, result, "start", upgradePolicy.autoUpgradePeriods[i].start);
339         NapiCommonUtils::SetInt32(env, result, "end", upgradePolicy.autoUpgradePeriods[i].end);
340         napi_set_element(env, autoUpgradePeriods, i, result);
341     }
342     status = napi_set_named_property(env, obj, "autoUpgradePeriods", autoUpgradePeriods);
343     PARAM_CHECK(status == napi_ok, return CAST_INT(ClientStatus::CLIENT_INVALID_TYPE),
344         "Failed to napi_set_named_property %d", static_cast<int32_t>(status));
345     return napi_ok;
346 }
347 
BuildUndefinedStatus(napi_env env,napi_value & obj,const UpdateResult & result)348 int32_t ClientHelper::BuildUndefinedStatus(napi_env env, napi_value &obj, const UpdateResult &result)
349 {
350     return napi_get_undefined(env, &obj);
351 }
352 
CheckNapiObjectType(napi_env env,const napi_value arg)353 ClientStatus CheckNapiObjectType(napi_env env, const napi_value arg)
354 {
355     napi_valuetype type = napi_undefined;
356     napi_status status = napi_typeof(env, arg, &type);
357     PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_INVALID_TYPE,
358         "Invalid argc %d", static_cast<int32_t>(status));
359     PARAM_CHECK(type == napi_object, return ClientStatus::CLIENT_INVALID_TYPE,
360         "Invalid argc %d", static_cast<int32_t>(type))
361     return ClientStatus::CLIENT_SUCCESS;
362 }
363 
ParseBusinessType(napi_env env,const napi_value arg,UpgradeInfo & upgradeInfo)364 void ParseBusinessType(napi_env env, const napi_value arg, UpgradeInfo &upgradeInfo)
365 {
366     bool result = false;
367     napi_status status = napi_has_named_property(env, arg, "businessType", &result);
368     if (result && (status == napi_ok)) {
369         napi_value businessTypeValue;
370         status = napi_get_named_property(env, arg, "businessType", &businessTypeValue);
371         PARAM_CHECK(status == napi_ok, return, "Failed to napi_set_named_property %d", static_cast<int32_t>(status));
372         NapiCommonUtils::GetString(env, businessTypeValue, "vendor", upgradeInfo.businessType.vendor);
373 
374         int32_t subType;
375         NapiCommonUtils::GetInt32(env, businessTypeValue, "subType", subType);
376         upgradeInfo.businessType.subType = static_cast<BusinessSubType>(subType);
377     }
378     if (upgradeInfo.businessType.subType == BusinessSubType::ROLLBACK) {
379         ENGINE_LOGI("ParseBusinessType: subType is rollback");
380         return;
381     }
382     if (upgradeInfo.businessType.subType == BusinessSubType::ACCESSORY) {
383         ENGINE_LOGI("ParseBusinessType: subType is accessory");
384         return;
385     }
386     if (upgradeInfo.businessType.subType != BusinessSubType::PARAM) {
387         upgradeInfo.businessType.subType = BusinessSubType::FIRMWARE;
388     }
389 }
390 
GetUpgradeInfoFromArg(napi_env env,const napi_value arg,UpgradeInfo & upgradeInfo)391 ClientStatus ClientHelper::GetUpgradeInfoFromArg(napi_env env, const napi_value arg, UpgradeInfo &upgradeInfo)
392 {
393     PARAM_CHECK(CheckNapiObjectType(env, arg) == ClientStatus::CLIENT_SUCCESS,
394         return ClientStatus::CLIENT_INVALID_TYPE, "GetUpgradeInfoFromArg type invalid");
395     NapiCommonUtils::GetString(env, arg, "upgradeApp", upgradeInfo.upgradeApp);
396     ParseBusinessType(env, arg, upgradeInfo);
397     NapiCommonUtils::GetString(env, arg, "upgradeDevId", upgradeInfo.upgradeDevId);
398     NapiCommonUtils::GetString(env, arg, "controlDevId", upgradeInfo.controlDevId);
399     int32_t type;
400     NapiCommonUtils::GetInt32(env, arg, "deviceType", type);
401     upgradeInfo.deviceType = static_cast<DeviceType>(type);
402     // 进程ID
403     upgradeInfo.processId = getpid();
404     return ClientStatus::CLIENT_SUCCESS;
405 }
406 
GetUpgradePolicyFromArg(napi_env env,const napi_value arg,UpgradePolicy & upgradePolicy)407 ClientStatus ClientHelper::GetUpgradePolicyFromArg(napi_env env, const napi_value arg, UpgradePolicy &upgradePolicy)
408 {
409     PARAM_CHECK(CheckNapiObjectType(env, arg) == ClientStatus::CLIENT_SUCCESS,
410         return ClientStatus::CLIENT_INVALID_TYPE, "GetUpgradePolicyFromArg type invalid");
411 
412     // upgradePolicy
413     NapiCommonUtils::GetBool(env, arg, "downloadStrategy", upgradePolicy.downloadStrategy);
414     NapiCommonUtils::GetBool(env, arg, "autoUpgradeStrategy", upgradePolicy.autoUpgradeStrategy);
415 
416     // Get the array.
417     bool result = false;
418     napi_status status = napi_has_named_property(env, arg, "autoUpgradePeriods", &result);
419     if (result && (status == napi_ok)) {
420         napi_value value;
421         status = napi_get_named_property(env, arg, "autoUpgradePeriods", &value);
422         PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_FAIL, "Failed to get attr autoUpgradePeriods");
423         status = napi_is_array(env, value, &result);
424         PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_FAIL, "napi_is_array failed");
425         uint32_t count = 0;
426         status = napi_get_array_length(env, value, &count);
427         PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_FAIL,
428             "napi_get_array_length failed");
429         uint32_t i = 0;
430         do {
431             if (i >= COUNT_OF(upgradePolicy.autoUpgradePeriods)) {
432                 break;
433             }
434             napi_value element;
435             napi_get_element(env, value, i, &element);
436             napi_get_value_uint32(env, element, &upgradePolicy.autoUpgradePeriods[i].start);
437             napi_get_value_uint32(env, element, &upgradePolicy.autoUpgradePeriods[i].end);
438             ENGINE_LOGI("upgradePolicy autoUpgradeInterval");
439             i++;
440         } while (i < count);
441     }
442     ENGINE_LOGI("upgradePolicy autoDownload:%d autoDownloadNet:%d",
443         static_cast<int32_t>(upgradePolicy.downloadStrategy),
444         static_cast<int32_t>(upgradePolicy.autoUpgradeStrategy));
445     return ClientStatus::CLIENT_SUCCESS;
446 }
447 
GetDescriptionFormat(napi_env env,const napi_value arg,DescriptionFormat & format)448 ClientStatus ClientHelper::GetDescriptionFormat(napi_env env, const napi_value arg, DescriptionFormat &format)
449 {
450     int tmpFormat = 0;
451     NapiCommonUtils::GetInt32(env, arg, "format", tmpFormat);
452     static const std::list<DescriptionFormat> formatList = {DescriptionFormat::STANDARD, DescriptionFormat::SIMPLIFIED};
453     PARAM_CHECK(IsValidEnum(formatList, tmpFormat), return ClientStatus::CLIENT_INVALID_TYPE,
454         "GetDescriptionFormat error, invalid format:%{public}d", tmpFormat);
455     format = static_cast<DescriptionFormat>(tmpFormat);
456     return ClientStatus::CLIENT_SUCCESS;
457 }
458 
GetNetType(napi_env env,const napi_value arg,NetType & netType)459 ClientStatus ClientHelper::GetNetType(napi_env env, const napi_value arg, NetType &netType)
460 {
461     int allowNetwork = 0;
462     NapiCommonUtils::GetInt32(env, arg, "allowNetwork", allowNetwork);
463     static const std::list<NetType> netTypeList = {NetType::CELLULAR, NetType::METERED_WIFI, NetType::NOT_METERED_WIFI,
464         NetType::WIFI, NetType::CELLULAR_AND_WIFI};
465     PARAM_CHECK(IsValidEnum(netTypeList, allowNetwork), return ClientStatus::CLIENT_INVALID_TYPE,
466         "GetNetType error, invalid NetType:%{public}d", allowNetwork);
467     netType = static_cast<NetType>(allowNetwork);
468     return ClientStatus::CLIENT_SUCCESS;
469 }
470 
GetOrder(napi_env env,const napi_value arg,Order & order)471 ClientStatus ClientHelper::GetOrder(napi_env env, const napi_value arg, Order &order)
472 {
473     int tmpOrder = 0;
474     NapiCommonUtils::GetInt32(env, arg, "order", tmpOrder);
475     static const std::list<Order> orderList = {Order::DOWNLOAD, Order::INSTALL,
476         Order::APPLY, Order::DOWNLOAD_AND_INSTALL, Order::INSTALL_AND_APPLY};
477     PARAM_CHECK(IsValidEnum(orderList, tmpOrder), return ClientStatus::CLIENT_INVALID_TYPE,
478         "GetOrder error, invalid order:%{public}d", tmpOrder);
479     order = static_cast<Order>(tmpOrder);
480     return ClientStatus::CLIENT_SUCCESS;
481 }
482 
GetOptionsFromArg(napi_env env,const napi_value arg,DescriptionOptions & descriptionOptions)483 ClientStatus ClientHelper::GetOptionsFromArg(napi_env env, const napi_value arg, DescriptionOptions &descriptionOptions)
484 {
485     ClientStatus ret = GetDescriptionFormat(env, arg, descriptionOptions.format);
486     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS, return ClientStatus::CLIENT_INVALID_TYPE,
487         "GetDescriptionOptionsFromArg GetDescriptionFormat error");
488     NapiCommonUtils::GetString(env, arg, "language", descriptionOptions.language);
489     return ClientStatus::CLIENT_SUCCESS;
490 }
491 
GetOptionsFromArg(napi_env env,const napi_value arg,DownloadOptions & downloadOptions)492 ClientStatus ClientHelper::GetOptionsFromArg(napi_env env, const napi_value arg, DownloadOptions &downloadOptions)
493 {
494     ClientStatus ret = GetNetType(env, arg, downloadOptions.allowNetwork);
495     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS, return ClientStatus::CLIENT_INVALID_TYPE,
496         "GetDownloadOptionsFromArg GetNetType error");
497 
498     ret = GetOrder(env, arg, downloadOptions.order);
499     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS, return ClientStatus::CLIENT_INVALID_TYPE,
500         "GetDownloadOptionsFromArg GetOrder error");
501     return ClientStatus::CLIENT_SUCCESS;
502 }
503 
GetOptionsFromArg(napi_env env,const napi_value arg,PauseDownloadOptions & pauseDownloadOptions)504 ClientStatus ClientHelper::GetOptionsFromArg(napi_env env, const napi_value arg,
505     PauseDownloadOptions &pauseDownloadOptions)
506 {
507     NapiCommonUtils::GetBool(env, arg, "isAllowAutoResume", pauseDownloadOptions.isAllowAutoResume);
508     return ClientStatus::CLIENT_SUCCESS;
509 }
510 
GetOptionsFromArg(napi_env env,const napi_value arg,ResumeDownloadOptions & resumeDownloadOptions)511 ClientStatus ClientHelper::GetOptionsFromArg(napi_env env, const napi_value arg,
512     ResumeDownloadOptions &resumeDownloadOptions)
513 {
514     return GetNetType(env, arg, resumeDownloadOptions.allowNetwork);
515 }
516 
GetVersionDigestInfoFromArg(napi_env env,const napi_value arg,VersionDigestInfo & versionDigestInfo)517 ClientStatus ClientHelper::GetVersionDigestInfoFromArg(napi_env env, const napi_value arg,
518     VersionDigestInfo &versionDigestInfo)
519 {
520     NapiCommonUtils::GetString(env, arg, "versionDigest", versionDigestInfo.versionDigest);
521     ENGINE_LOGI("GetVersionDigestInfoFromArg versionDigest:%{public}s", versionDigestInfo.versionDigest.c_str());
522     return ClientStatus::CLIENT_SUCCESS;
523 }
524 
GetOptionsFromArg(napi_env env,const napi_value arg,UpgradeOptions & upgradeOptions)525 ClientStatus ClientHelper::GetOptionsFromArg(napi_env env, const napi_value arg, UpgradeOptions &upgradeOptions)
526 {
527     return GetOrder(env, arg, upgradeOptions.order);
528 }
529 
GetOptionsFromArg(napi_env env,const napi_value arg,ClearOptions & clearOptions)530 ClientStatus ClientHelper::GetOptionsFromArg(napi_env env, const napi_value arg, ClearOptions &clearOptions)
531 {
532     int32_t status = 0;
533     NapiCommonUtils::GetInt32(env, arg, "status", status);
534     static const std::list<UpgradeStatus> statusList = {
535         UpgradeStatus::INIT,                  UpgradeStatus::CHECKING_VERSION,
536         UpgradeStatus::CHECK_VERSION_FAIL,    UpgradeStatus::CHECK_VERSION_SUCCESS,
537         UpgradeStatus::DOWNLOADING,           UpgradeStatus::DOWNLOAD_PAUSE,
538         UpgradeStatus::DOWNLOAD_CANCEL,       UpgradeStatus::DOWNLOAD_FAIL,
539         UpgradeStatus::DOWNLOAD_SUCCESS,      UpgradeStatus::VERIFYING,
540         UpgradeStatus::VERIFY_FAIL,           UpgradeStatus::VERIFY_SUCCESS,
541         UpgradeStatus::PACKAGE_TRANSING,      UpgradeStatus::PACKAGE_TRANS_FAIL,
542         UpgradeStatus::PACKAGE_TRANS_SUCCESS, UpgradeStatus::INSTALLING,
543         UpgradeStatus::INSTALL_FAIL,          UpgradeStatus::INSTALL_SUCCESS,
544         UpgradeStatus::UPDATING,              UpgradeStatus::UPDATE_FAIL,
545         UpgradeStatus::UPDATE_SUCCESS };
546     PARAM_CHECK(IsValidEnum(statusList, status), return ClientStatus::CLIENT_INVALID_TYPE,
547         "GetClearOptionsFromArg error, invalid status:%{public}d", status);
548     clearOptions.status = static_cast<UpgradeStatus>(status);
549     ENGINE_LOGI("GetClearOptionsFromArg status:%{public}d", clearOptions.status);
550     return ClientStatus::CLIENT_SUCCESS;
551 }
552 
ParseUpgradeFile(napi_env env,const napi_value arg,UpgradeFile & upgradeFile)553 ClientStatus ParseUpgradeFile(napi_env env, const napi_value arg, UpgradeFile &upgradeFile)
554 {
555     napi_valuetype type = napi_undefined;
556     napi_status status = napi_typeof(env, arg, &type);
557     PARAM_CHECK(status == napi_ok && type == napi_object, return ClientStatus::CLIENT_INVALID_TYPE,
558         "ParseUpgradeFile error, error type");
559 
560     int32_t fileType = 0;
561     NapiCommonUtils::GetInt32(env, arg, "fileType", fileType);
562     static const std::list<ComponentType> enumList = { ComponentType::OTA, ComponentType::PATCH, ComponentType::COTA,
563         ComponentType::PARAM };
564     PARAM_CHECK(IsValidEnum(enumList, fileType), return ClientStatus::CLIENT_INVALID_PARAM,
565         "ParseUpgradeFile error, invalid fileType:%{public}d", fileType);
566     upgradeFile.fileType = static_cast<ComponentType>(fileType);
567 
568     NapiCommonUtils::GetString(env, arg, "filePath", upgradeFile.filePath);
569     ClientHelper::TrimString(upgradeFile.filePath);
570     if (!ClientHelper::IsValidUpgradeFile(upgradeFile.filePath)) {
571         ENGINE_LOGE("ParseUpgradeFile, invalid filePath:%s", upgradeFile.filePath.c_str());
572         return ClientStatus::CLIENT_INVALID_PARAM;
573     }
574     ENGINE_LOGI("ParseUpgradeFile fileType:%{public}d, filePath:%s", fileType, upgradeFile.filePath.c_str());
575     return ClientStatus::CLIENT_SUCCESS;
576 }
577 
GetUpgradeFileFromArg(napi_env env,const napi_value arg,UpgradeFile & upgradeFile)578 ClientStatus ClientHelper::GetUpgradeFileFromArg(napi_env env, const napi_value arg, UpgradeFile &upgradeFile)
579 {
580     return ParseUpgradeFile(env, arg, upgradeFile);
581 }
582 
GetUpgradeFilesFromArg(napi_env env,const napi_value arg,std::vector<UpgradeFile> & upgradeFiles)583 ClientStatus ClientHelper::GetUpgradeFilesFromArg(napi_env env, const napi_value arg,
584     std::vector<UpgradeFile> &upgradeFiles)
585 {
586     bool result = false;
587     napi_status status = napi_is_array(env, arg, &result);
588     PARAM_CHECK(status == napi_ok && result, return ClientStatus::CLIENT_FAIL,
589         "GetUpgradeFilesFromArg error, napi_is_array failed");
590 
591     uint32_t count = 0;
592     status = napi_get_array_length(env, arg, &count);
593     PARAM_CHECK((status == napi_ok) && (count > 0), return ClientStatus::CLIENT_FAIL,
594         "GetUpgradeFilesFromArg error, napi_get_array_length failed");
595     for (uint32_t idx = 0; idx < count; idx++) {
596         napi_value element;
597         napi_get_element(env, arg, idx, &element);
598         UpgradeFile upgradeFile;
599         ClientStatus ret = ParseUpgradeFile(env, element, upgradeFile);
600         if (ret != ClientStatus::CLIENT_SUCCESS) {
601             return ret;
602         }
603         upgradeFiles.emplace_back(upgradeFile);
604     }
605     ENGINE_LOGI("GetUpgradeFilesFromArg success, size:%{public}zu", upgradeFiles.size());
606     return ClientStatus::CLIENT_SUCCESS;
607 }
608 
GetEventClassifyInfoFromArg(napi_env env,const napi_value arg,EventClassifyInfo & eventClassifyInfo)609 ClientStatus ClientHelper::GetEventClassifyInfoFromArg(napi_env env, const napi_value arg,
610     EventClassifyInfo &eventClassifyInfo)
611 {
612     napi_valuetype type = napi_undefined;
613     napi_status status = napi_typeof(env, arg, &type);
614     PARAM_CHECK(status == napi_ok && type == napi_object, return ClientStatus::CLIENT_INVALID_TYPE,
615         "GetEventClassifyInfoFromArg error, error type");
616 
617     int32_t eventClassify = 0;
618     NapiCommonUtils::GetInt32(env, arg, "eventClassify", eventClassify);
619     PARAM_CHECK(IsValidEnum(g_eventClassifyList, eventClassify), return ClientStatus::CLIENT_INVALID_TYPE,
620         "GetEventClassifyInfoFromArg error, invalid eventClassify:0x%{public}x", eventClassify);
621     eventClassifyInfo.eventClassify = static_cast<EventClassify>(eventClassify);
622 
623     NapiCommonUtils::GetString(env, arg, "extraInfo", eventClassifyInfo.extraInfo);
624     ENGINE_LOGI("GetEventClassifyInfoFromArg eventClassify:0x%{public}x, extraInfo:%s",
625         eventClassify, eventClassifyInfo.extraInfo.c_str());
626     return ClientStatus::CLIENT_SUCCESS;
627 }
628 
629 
BuildTaskBody(napi_env env,napi_value & obj,EventId eventId,const TaskBody & taskBody)630 ClientStatus BuildTaskBody(napi_env env, napi_value &obj, EventId eventId, const TaskBody &taskBody)
631 {
632     auto iter = g_taskBodyTemplateMap.find(eventId);
633     PARAM_CHECK(iter != g_taskBodyTemplateMap.end(), return ClientStatus::CLIENT_INVALID_PARAM,
634         "BuildTaskBody error, eventId %{public}d", CAST_INT(eventId));
635     uint32_t taskBodyTemplate = iter->second;
636     napi_value napiTaskBody = nullptr;
637     napi_create_object(env, &napiTaskBody);
638     if (taskBodyTemplate & VERSION_DIGEST_INFO) {
639         BuildVersionDigestInfo(env, napiTaskBody, taskBody.versionDigestInfo);
640     }
641     if (taskBodyTemplate & UPGRADE_STATUS) {
642         NapiCommonUtils::SetInt32(env, napiTaskBody, "status",  CAST_INT(taskBody.status));
643     }
644     if (taskBodyTemplate & SUB_STATUS) {
645         NapiCommonUtils::SetInt32(env, napiTaskBody, "subStatus", taskBody.subStatus);
646     }
647     if (taskBodyTemplate & PROGRESS) {
648         NapiCommonUtils::SetInt32(env, napiTaskBody, "progress", taskBody.progress);
649     }
650     if (taskBodyTemplate & INSTALL_MODE) {
651         NapiCommonUtils::SetInt32(env, napiTaskBody, "installMode", taskBody.installMode);
652     }
653     if (taskBodyTemplate & ERROR_MESSAGE) {
654         BuildErrorMessages(env, napiTaskBody, "errorMessages", taskBody.errorMessages);
655     }
656     if (taskBodyTemplate & VERSION_COMPONENT) {
657         BuildVersionComponents(env, napiTaskBody, taskBody.versionComponents);
658     }
659     napi_set_named_property(env, obj, "taskBody", napiTaskBody);
660     return ClientStatus::CLIENT_SUCCESS;
661 }
662 
BuildEventInfo(napi_env env,napi_value & obj,const EventInfo & eventInfo)663 ClientStatus ClientHelper::BuildEventInfo(napi_env env, napi_value &obj, const EventInfo &eventInfo)
664 {
665     napi_status status = napi_create_object(env, &obj);
666     PARAM_CHECK(status == napi_ok, return ClientStatus::CLIENT_FAIL,
667         "BuildEventInfo error, failed to create napi_create_object %{public}d", CAST_INT(status));
668 
669     NapiCommonUtils::SetInt32(env, obj, "eventId", CAST_INT(eventInfo.eventId));
670     ClientStatus ret = BuildTaskBody(env, obj, eventInfo.eventId, eventInfo.taskBody);
671     PARAM_CHECK(ret == ClientStatus::CLIENT_SUCCESS, return ClientStatus::CLIENT_FAIL,
672         "BuildEventInfo error, build task info fail");
673     return ClientStatus::CLIENT_SUCCESS;
674 }
675 } // namespace OHOS::UpdateEngine