1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UPDATE_CLIENT_HELPER_H 17 #define UPDATE_CLIENT_HELPER_H 18 19 #include <string> 20 21 #include "node_api.h" 22 23 #include "client_define.h" 24 #include "update_helper.h" 25 26 constexpr int32_t COMPONENT_ERR = 11500000; 27 namespace OHOS { 28 namespace UpdateEngine { 29 // Update status 30 enum class ClientStatus { 31 CLIENT_SUCCESS = 0, 32 CLIENT_INVALID_PARAM = 1000, 33 CLIENT_INVALID_TYPE, 34 CLIENT_REPEAT_REQ, 35 CLIENT_FAIL, 36 CLIENT_CHECK_NEW_FIRST, 37 }; 38 39 enum class SessionType { 40 SESSION_CHECK_VERSION = 0, 41 SESSION_DOWNLOAD, 42 SESSION_PAUSE_DOWNLOAD, 43 SESSION_RESUME_DOWNLOAD, 44 SESSION_UPGRADE, 45 SESSION_SET_POLICY, 46 SESSION_GET_POLICY, 47 SESSION_CLEAR_ERROR, 48 SESSION_TERMINATE_UPGRADE, 49 SESSION_GET_NEW_VERSION, 50 SESSION_GET_NEW_VERSION_DESCRIPTION, 51 SESSION_SUBSCRIBE, 52 SESSION_UNSUBSCRIBE, 53 SESSION_GET_UPDATER, 54 SESSION_APPLY_NEW_VERSION, 55 SESSION_FACTORY_RESET, 56 SESSION_VERIFY_PACKAGE, 57 SESSION_CANCEL_UPGRADE, 58 SESSION_GET_CUR_VERSION, 59 SESSION_GET_CUR_VERSION_DESCRIPTION, 60 SESSION_GET_TASK_INFO, 61 SESSION_REPLY_PARAM_ERROR, 62 SESSION_MAX 63 }; 64 65 struct SessionParams { 66 SessionType type; 67 size_t callbackStartIndex; 68 bool isNeedBusinessError; 69 bool isAsyncCompleteWork; 70 71 SessionParams(SessionType typeValue = SessionType::SESSION_MAX, size_t callbackPosition = 1, 72 bool isNeedBusinessErrorValue = false, bool isAsyncCompleteWorkValue = false) typeSessionParams73 : type(typeValue), callbackStartIndex(INDEX(callbackPosition)), isNeedBusinessError(isNeedBusinessErrorValue), 74 isAsyncCompleteWork(isAsyncCompleteWorkValue) {} 75 }; 76 77 struct UpdateResult { 78 using BuildJSObject = std::function<int(napi_env env, napi_value &obj, const UpdateResult &result)>; 79 SessionType type; 80 BusinessError businessError; 81 union { 82 UpgradePolicy *upgradePolicy; 83 Progress *progress; 84 NewVersionInfo *newVersionInfo; 85 VersionDescriptionInfo *versionDescriptionInfo; 86 CheckResult *checkResult; 87 CurrentVersionInfo *currentVersionInfo; 88 TaskInfo *taskInfo; 89 } result; 90 91 BuildJSObject buildJSObject; 92 ReleaseUpdateResult93 void Release() 94 { 95 CLIENT_LOGI("UpdateResult Release"); 96 if (type == SessionType::SESSION_DOWNLOAD || type == SessionType::SESSION_UPGRADE) { 97 delete result.progress; 98 result.progress = nullptr; 99 } else if (type == SessionType::SESSION_CHECK_VERSION) { 100 delete result.checkResult; 101 result.checkResult = nullptr; 102 } else if (type == SessionType::SESSION_GET_NEW_VERSION) { 103 delete result.newVersionInfo; 104 result.newVersionInfo = nullptr; 105 } else if (type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION || 106 type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION) { 107 delete result.versionDescriptionInfo; 108 result.versionDescriptionInfo = nullptr; 109 } else if (type == SessionType::SESSION_GET_CUR_VERSION) { 110 delete result.currentVersionInfo; 111 result.currentVersionInfo = nullptr; 112 } else if (type == SessionType::SESSION_GET_POLICY) { 113 delete result.upgradePolicy; 114 result.upgradePolicy = nullptr; 115 } else { 116 CLIENT_LOGI("UpdateResult Release, unknow type"); 117 } 118 CLIENT_LOGI("UpdateResult Release finish"); 119 } 120 121 UpdateResult &operator=(const UpdateResult &updateResult) 122 { 123 if (&updateResult == this) { 124 return *this; 125 } 126 127 buildJSObject = updateResult.buildJSObject; 128 type = updateResult.type; 129 businessError = updateResult.businessError; 130 131 CLIENT_LOGI("UpdateResult operator type %{public}d", updateResult.type); 132 if (type == SessionType::SESSION_DOWNLOAD || type == SessionType::SESSION_UPGRADE) { 133 if (result.progress == nullptr) { 134 result.progress = new (std::nothrow) Progress(); 135 } 136 if ((result.progress != nullptr) && (updateResult.result.progress != nullptr)) { 137 *(result.progress) = *(updateResult.result.progress); 138 } 139 } else if (type == SessionType::SESSION_CHECK_VERSION) { 140 if (result.checkResult == nullptr) { 141 result.checkResult = new (std::nothrow) CheckResult(); 142 } 143 if ((result.checkResult != nullptr) && (updateResult.result.checkResult != nullptr)) { 144 *(result.checkResult) = *(updateResult.result.checkResult); 145 } 146 } else if (type == SessionType::SESSION_GET_NEW_VERSION) { 147 if (result.newVersionInfo == nullptr) { 148 result.newVersionInfo = new (std::nothrow) NewVersionInfo(); 149 } 150 if ((result.newVersionInfo != nullptr) && (updateResult.result.newVersionInfo != nullptr)) { 151 *(result.newVersionInfo) = *(updateResult.result.newVersionInfo); 152 } 153 } else if (type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION || 154 type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION) { 155 if (result.versionDescriptionInfo == nullptr) { 156 result.versionDescriptionInfo = new (std::nothrow) VersionDescriptionInfo(); 157 } 158 if ((result.versionDescriptionInfo != nullptr) 159 && (updateResult.result.versionDescriptionInfo != nullptr)) { 160 *(result.versionDescriptionInfo) = *(updateResult.result.versionDescriptionInfo); 161 } 162 } else if (type == SessionType::SESSION_GET_CUR_VERSION) { 163 if (result.currentVersionInfo == nullptr) { 164 result.currentVersionInfo = new (std::nothrow) CurrentVersionInfo(); 165 } 166 if ((result.currentVersionInfo != nullptr) && (updateResult.result.currentVersionInfo != nullptr)) { 167 *(result.currentVersionInfo) = *(updateResult.result.currentVersionInfo); 168 } 169 } else if (type == SessionType::SESSION_GET_POLICY) { 170 if (result.upgradePolicy == nullptr) { 171 result.upgradePolicy = new (std::nothrow) UpgradePolicy(); 172 } 173 if ((result.upgradePolicy != nullptr) && (updateResult.result.upgradePolicy != nullptr)) { 174 *(result.upgradePolicy) = *(updateResult.result.upgradePolicy); 175 } 176 } else { 177 CLIENT_LOGI("UpdateResult unknow type"); 178 } 179 return *this; 180 } 181 }; 182 183 class ClientHelper { 184 public: 185 static void TrimString(std::string &str); 186 static bool IsValidUpgradeFile(const std::string &upgradeFile); 187 188 static int32_t BuildCheckResult(napi_env env, napi_value &obj, const UpdateResult &result); 189 static int32_t BuildNewVersionInfo(napi_env env, napi_value &obj, const UpdateResult &result); 190 static int32_t BuildVersionDescriptionInfo(napi_env env, napi_value &obj, const UpdateResult &result); 191 static int32_t BuildUpgradePolicy(napi_env env, napi_value &obj, const UpdateResult &result); 192 static int32_t BuildCurrentVersionInfo(napi_env env, napi_value &obj, const UpdateResult &result); 193 static int32_t BuildTaskInfo(napi_env env, napi_value &obj, const UpdateResult &result); 194 static int32_t BuildUndefinedStatus(napi_env env, napi_value &obj, const UpdateResult &result); 195 static napi_value BuildThrowError(napi_env env, const BusinessError &businessError); 196 static void NapiThrowParamError( 197 napi_env env, std::vector<std::pair<std::string, std::string>> ¶mInfos); 198 199 static ClientStatus GetUpgradeInfoFromArg(napi_env env, const napi_value arg, UpgradeInfo &upgradeInfo); 200 static ClientStatus GetUpgradePolicyFromArg(napi_env env, const napi_value arg, UpgradePolicy &upgradePolicy); 201 static ClientStatus GetUpgradeFileFromArg(napi_env env, const napi_value arg, UpgradeFile &upgradeFile); 202 static ClientStatus GetUpgradeFilesFromArg(napi_env env, const napi_value arg, 203 std::vector<UpgradeFile> &upgradeFiles); 204 static ClientStatus GetVersionDigestInfoFromArg(napi_env env, const napi_value arg, 205 VersionDigestInfo &versionDigestInfo); 206 207 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, DescriptionOptions &descriptionOptions); 208 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, UpgradeOptions &upgradeOptions); 209 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, ClearOptions &clearOptions); 210 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, DownloadOptions &downloadOptions); 211 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, 212 PauseDownloadOptions &pauseDownloadOptions); 213 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, 214 ResumeDownloadOptions &resumeDownloadOptions); 215 static ClientStatus GetEventClassifyInfoFromArg(napi_env env, const napi_value arg, 216 EventClassifyInfo &eventClassifyInfo); 217 static int32_t BuildBusinessError(napi_env env, napi_value &obj, const BusinessError &businessError); 218 static ClientStatus BuildEventInfo(napi_env env, napi_value &obj, const EventInfo &eventInfo); 219 static int32_t ConvertToErrorCode(CallResult callResult); 220 221 static bool IsErrorExist(const BusinessError &businessError); 222 223 private: 224 static ClientStatus GetDescriptionFormat(napi_env env, const napi_value arg, DescriptionFormat &format); 225 static ClientStatus GetNetType(napi_env env, const napi_value arg, NetType &netType); 226 static ClientStatus GetOrder(napi_env env, const napi_value arg, Order &order); 227 static std::string ConvertVectorToStr(std::vector<std::pair<std::string, std::string>> &strVector, bool isFirst); 228 static bool IsCommonError(CallResult callResult); 229 static std::string GetParamNames(std::vector<std::pair<std::string, std::string>> &strVector); 230 static std::string GetParamTypes(std::vector<std::pair<std::string, std::string>> &strVector); 231 }; 232 } // namespace UpdateEngine 233 } // namespace OHOS 234 #endif // UPDATE_CLIENT_HELPER_H