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