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