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 #include "node_api.h" 21 #include "update_define.h" 22 #include "update_helper.h" 23 24 namespace OHOS { 25 namespace UpdateEngine { 26 // Update status 27 enum class ClientStatus { 28 CLIENT_SUCCESS = 0, 29 CLIENT_INVALID_PARAM = 1000, 30 CLIENT_INVALID_TYPE, 31 CLIENT_REPEAT_REQ, 32 CLIENT_FAIL, 33 CLIENT_CHECK_NEW_FIRST, 34 }; 35 36 enum class SessionType { 37 SESSION_CHECK_VERSION = 0, 38 SESSION_DOWNLOAD, 39 SESSION_PAUSE_DOWNLOAD, 40 SESSION_RESUME_DOWNLOAD, 41 SESSION_UPGRADE, 42 SESSION_SET_POLICY, 43 SESSION_GET_POLICY, 44 SESSION_CLEAR_ERROR, 45 SESSION_TERMINATE_UPGRADE, 46 SESSION_GET_NEW_VERSION, 47 SESSION_GET_NEW_VERSION_DESCRIPTION, 48 SESSION_SUBSCRIBE, 49 SESSION_UNSUBSCRIBE, 50 SESSION_GET_UPDATER, 51 SESSION_APPLY_NEW_VERSION, 52 SESSION_FACTORY_RESET, 53 SESSION_VERIFY_PACKAGE, 54 SESSION_CANCEL_UPGRADE, 55 SESSION_GET_CUR_VERSION, 56 SESSION_GET_CUR_VERSION_DESCRIPTION, 57 SESSION_GET_TASK_INFO, 58 SESSION_REPLY_PARAM_ERROR, 59 SESSION_MAX 60 }; 61 62 struct SessionParams { 63 SessionType type; 64 size_t callbackStartIndex; 65 bool isNeedBusinessError; 66 bool isAsyncCompleteWork; 67 68 SessionParams(SessionType typeValue = SessionType::SESSION_MAX, size_t callbackPosition = 1, 69 bool isNeedBusinessErrorValue = false, bool isAsyncCompleteWorkValue = false) typeSessionParams70 : type(typeValue), callbackStartIndex(INDEX(callbackPosition)), isNeedBusinessError(isNeedBusinessErrorValue), 71 isAsyncCompleteWork(isAsyncCompleteWorkValue) {} 72 }; 73 74 struct UpdateResult { 75 using BuildJSObject = std::function<int(napi_env env, napi_value &obj, const UpdateResult &result)>; 76 SessionType type; 77 BusinessError businessError; 78 union { 79 UpgradePolicy *upgradePolicy; 80 Progress *progress; 81 NewVersionInfo *newVersionInfo; 82 VersionDescriptionInfo *versionDescriptionInfo; 83 CheckResultEx *checkResultEx; 84 CurrentVersionInfo *currentVersionInfo; 85 TaskInfo *taskInfo; 86 } result; 87 88 BuildJSObject buildJSObject; 89 ReleaseUpdateResult90 void Release() 91 { 92 CLIENT_LOGI("UpdateResult Release"); 93 if (type == SessionType::SESSION_DOWNLOAD || type == SessionType::SESSION_UPGRADE) { 94 delete result.progress; 95 result.progress = nullptr; 96 } else if (type == SessionType::SESSION_CHECK_VERSION) { 97 delete result.checkResultEx; 98 result.checkResultEx = nullptr; 99 } else if (type == SessionType::SESSION_GET_NEW_VERSION) { 100 delete result.newVersionInfo; 101 result.newVersionInfo = nullptr; 102 } else if (type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION || 103 type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION) { 104 delete result.versionDescriptionInfo; 105 result.versionDescriptionInfo = nullptr; 106 } else if (type == SessionType::SESSION_GET_CUR_VERSION) { 107 delete result.currentVersionInfo; 108 result.currentVersionInfo = nullptr; 109 } else if (type == SessionType::SESSION_GET_POLICY) { 110 delete result.upgradePolicy; 111 result.upgradePolicy = nullptr; 112 } else { 113 CLIENT_LOGI("UpdateResult Release, unknow type"); 114 } 115 CLIENT_LOGI("UpdateResult Release finish"); 116 } 117 118 UpdateResult &operator=(const UpdateResult &updateResult) 119 { 120 if (&updateResult == this) { 121 return *this; 122 } 123 124 buildJSObject = updateResult.buildJSObject; 125 type = updateResult.type; 126 businessError = updateResult.businessError; 127 128 CLIENT_LOGI("UpdateResult operator type %{public}d", updateResult.type); 129 if (type == SessionType::SESSION_DOWNLOAD || type == SessionType::SESSION_UPGRADE) { 130 if (result.progress == nullptr) { 131 result.progress = new (std::nothrow) Progress(); 132 } 133 if ((result.progress != nullptr) && (updateResult.result.progress != nullptr)) { 134 *(result.progress) = *(updateResult.result.progress); 135 } 136 } else if (type == SessionType::SESSION_CHECK_VERSION) { 137 if (result.checkResultEx == nullptr) { 138 result.checkResultEx = new (std::nothrow) CheckResultEx(); 139 } 140 if ((result.checkResultEx != nullptr) && (updateResult.result.checkResultEx != nullptr)) { 141 *(result.checkResultEx) = *(updateResult.result.checkResultEx); 142 } 143 } else if (type == SessionType::SESSION_GET_NEW_VERSION) { 144 if (result.newVersionInfo == nullptr) { 145 result.newVersionInfo = new (std::nothrow) NewVersionInfo(); 146 } 147 if ((result.newVersionInfo != nullptr) && (updateResult.result.newVersionInfo != nullptr)) { 148 *(result.newVersionInfo) = *(updateResult.result.newVersionInfo); 149 } 150 } else if (type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION || 151 type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION) { 152 if (result.versionDescriptionInfo == nullptr) { 153 result.versionDescriptionInfo = new (std::nothrow) VersionDescriptionInfo(); 154 } 155 if ((result.versionDescriptionInfo != nullptr) 156 && (updateResult.result.versionDescriptionInfo != nullptr)) { 157 *(result.versionDescriptionInfo) = *(updateResult.result.versionDescriptionInfo); 158 } 159 } else if (type == SessionType::SESSION_GET_CUR_VERSION) { 160 if (result.currentVersionInfo == nullptr) { 161 result.currentVersionInfo = new (std::nothrow) CurrentVersionInfo(); 162 } 163 if ((result.currentVersionInfo != nullptr) && (updateResult.result.currentVersionInfo != nullptr)) { 164 *(result.currentVersionInfo) = *(updateResult.result.currentVersionInfo); 165 } 166 } else if (type == SessionType::SESSION_GET_POLICY) { 167 if (result.upgradePolicy == nullptr) { 168 result.upgradePolicy = new (std::nothrow) UpgradePolicy(); 169 } 170 if ((result.upgradePolicy != nullptr) && (updateResult.result.upgradePolicy != nullptr)) { 171 *(result.upgradePolicy) = *(updateResult.result.upgradePolicy); 172 } 173 } else { 174 CLIENT_LOGI("UpdateResult unknow type"); 175 } 176 return *this; 177 } 178 }; 179 180 class ClientHelper { 181 public: 182 static void TrimString(std::string &str); 183 static bool IsValidUpgradeFile(const std::string &upgradeFile); 184 185 static int32_t BuildCheckResultEx(napi_env env, napi_value &obj, const UpdateResult &result); 186 static int32_t BuildNewVersionInfo(napi_env env, napi_value &obj, const UpdateResult &result); 187 static int32_t BuildVersionDescriptionInfo(napi_env env, napi_value &obj, const UpdateResult &result); 188 static int32_t BuildUpgradePolicy(napi_env env, napi_value &obj, const UpdateResult &result); 189 static int32_t BuildCurrentVersionInfo(napi_env env, napi_value &obj, const UpdateResult &result); 190 static int32_t BuildTaskInfo(napi_env env, napi_value &obj, const UpdateResult &result); 191 static int32_t BuildUndefinedStatus(napi_env env, napi_value &obj, const UpdateResult &result); 192 static napi_value BuildThrowError(napi_env env, const BusinessError &businessError); 193 static void NapiThrowParamError( 194 napi_env env, std::vector<std::string> ¶mNames, std::vector<std::string> ¶mTypes); 195 196 static ClientStatus GetUpgradeInfoFromArg(napi_env env, const napi_value arg, UpgradeInfo &upgradeInfo); 197 static ClientStatus GetUpgradePolicyFromArg(napi_env env, const napi_value arg, UpgradePolicy &upgradePolicy); 198 static ClientStatus GetUpgradeFileFromArg(napi_env env, const napi_value arg, UpgradeFile &upgradeFile); 199 static ClientStatus GetUpgradeFilesFromArg(napi_env env, const napi_value arg, 200 std::vector<UpgradeFile> &upgradeFiles); 201 static ClientStatus GetVersionDigestInfoFromArg(napi_env env, const napi_value arg, 202 VersionDigestInfo &versionDigestInfo); 203 204 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, DescriptionOptions &descriptionOptions); 205 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, UpgradeOptions &upgradeOptions); 206 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, ClearOptions &clearOptions); 207 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, DownloadOptions &downloadOptions); 208 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, 209 PauseDownloadOptions &pauseDownloadOptions); 210 static ClientStatus GetOptionsFromArg(napi_env env, const napi_value arg, 211 ResumeDownloadOptions &resumeDownloadOptions); 212 static ClientStatus GetEventClassifyInfoFromArg(napi_env env, const napi_value arg, 213 EventClassifyInfo &eventClassifyInfo); 214 static int32_t BuildBusinessError(napi_env env, napi_value &obj, const BusinessError &businessError); 215 static ClientStatus BuildEventInfo(napi_env env, napi_value &obj, const EventInfo &eventInfo); 216 217 private: 218 static ClientStatus GetDescriptionFormat(napi_env env, const napi_value arg, DescriptionFormat &format); 219 static ClientStatus GetNetType(napi_env env, const napi_value arg, NetType &netType); 220 static ClientStatus GetOrder(napi_env env, const napi_value arg, Order &order); 221 }; 222 } // namespace UpdateEngine 223 } // namespace OHOS 224 #endif // UPDATE_CLIENT_HELPER_H 225