1 /* 2 * Copyright (c) 2024 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_RESULT_H 17 #define UPDATE_RESULT_H 18 19 #include "napi_structs_base.h" 20 #include "session_type.h" 21 22 namespace OHOS::UpdateEngine { 23 struct UpdateResult : NapiResult { 24 public: 25 using BuildJSObject = std::function<int(napi_env env, napi_value &obj, const UpdateResult &napiResult)>; 26 BuildJSObject buildJSObject; 27 28 union ResultUnion { 29 UpgradePolicy *upgradePolicy; 30 Progress *progress; 31 NewVersionInfo *newVersionInfo; 32 VersionDescriptionInfo *versionDescriptionInfo; 33 CheckResult *checkResult; 34 CurrentVersionInfo *currentVersionInfo; 35 TaskInfo *taskInfo; 36 }; 37 38 ResultUnion result; 39 ReleaseUpdateResult40 void Release() 41 { 42 ENGINE_LOGI("UpdateResult Release"); 43 if (type == SessionType::SESSION_DOWNLOAD || type == SessionType::SESSION_UPGRADE) { 44 ReleaseValue<Progress>(result.progress); 45 } else if (type == SessionType::SESSION_CHECK_VERSION) { 46 ReleaseValue<CheckResult>(result.checkResult); 47 } else if (type == SessionType::SESSION_GET_NEW_VERSION) { 48 ReleaseValue<NewVersionInfo>(result.newVersionInfo); 49 } else if (type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION || 50 type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION) { 51 ReleaseValue<VersionDescriptionInfo>(result.versionDescriptionInfo); 52 } else if (type == SessionType::SESSION_GET_CUR_VERSION) { 53 ReleaseValue<CurrentVersionInfo>(result.currentVersionInfo); 54 } else if (type == SessionType::SESSION_GET_POLICY) { 55 ReleaseValue<UpgradePolicy>(result.upgradePolicy); 56 } else { 57 ENGINE_LOGI("UpdateResult Release, unknow type"); 58 } 59 ENGINE_LOGI("UpdateResult Release finish"); 60 } 61 62 UpdateResult &operator = (const UpdateResult &updateResult) 63 { 64 if (&updateResult == this) { 65 return *this; 66 } 67 buildJSObject = updateResult.buildJSObject; 68 static_cast<NapiResult &>(*this) = updateResult; 69 this->type = updateResult.type; 70 this->businessError = updateResult.businessError; 71 72 ENGINE_LOGI("UpdateResult operator type %{public}d", updateResult.type); 73 if (type == SessionType::SESSION_DOWNLOAD || type == SessionType::SESSION_UPGRADE) { 74 AssignValue<Progress>(updateResult.result.progress, result.progress); 75 } else if (type == SessionType::SESSION_CHECK_VERSION) { 76 AssignValue<CheckResult>(updateResult.result.checkResult, result.checkResult); 77 } else if (type == SessionType::SESSION_GET_NEW_VERSION) { 78 AssignValue<NewVersionInfo>(updateResult.result.newVersionInfo, result.newVersionInfo); 79 } else if (type == SessionType::SESSION_GET_NEW_VERSION_DESCRIPTION || 80 type == SessionType::SESSION_GET_CUR_VERSION_DESCRIPTION) { 81 AssignValue<VersionDescriptionInfo>( 82 updateResult.result.versionDescriptionInfo, result.versionDescriptionInfo); 83 } else if (type == SessionType::SESSION_GET_CUR_VERSION) { 84 AssignValue<CurrentVersionInfo>( 85 updateResult.result.currentVersionInfo, result.currentVersionInfo); 86 } else if (type == SessionType::SESSION_GET_POLICY) { 87 AssignValue<UpgradePolicy>(updateResult.result.upgradePolicy, result.upgradePolicy); 88 } else { 89 ENGINE_LOGI("UpdateResult unknow type"); 90 } 91 return *this; 92 } 93 }; 94 } // namespace OHOS::UpdateEngine 95 #endif // UPDATE_RESULT_H