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 #include "task_body.h"
17 #include "task_body_member_mask.h"
18 #include "update_log.h"
19 
20 namespace OHOS::UpdateEngine {
GetJsonBuilder(VersionComponent & versionComponent)21 JsonBuilder GetJsonBuilder(VersionComponent &versionComponent)
22 {
23     return versionComponent.GetJsonBuilder();
24 }
25 
GetJsonBuilder(ErrorMessage & errorMessage)26 JsonBuilder GetJsonBuilder(ErrorMessage &errorMessage)
27 {
28     return errorMessage.GetJsonBuilder();
29 }
30 
31 template <typename T>
GetArrayJsonBuilderList(const std::vector<T> & valueList)32 std::vector<JsonBuilder> GetArrayJsonBuilderList(const std::vector<T> &valueList)
33 {
34     std::vector<JsonBuilder> jsonBuilderList;
35     for (T value : valueList) {
36         jsonBuilderList.push_back(GetJsonBuilder(value));
37     }
38     return jsonBuilderList;
39 }
40 
GetJsonBuilder(EventId eventId)41 JsonBuilder TaskBody::GetJsonBuilder(EventId eventId)
42 {
43     JsonBuilder jsonBuilder = JsonBuilder();
44     auto iter = g_taskBodyTemplateMap.find(eventId);
45     if (iter == g_taskBodyTemplateMap.end()) {
46         ENGINE_LOGE("GetJsonBuilder eventId not find");
47         return jsonBuilder;
48     }
49     jsonBuilder.Append("{");
50     uint32_t taskBodyTemplate = iter->second;
51     if (taskBodyTemplate & VERSION_DIGEST_INFO) {
52         jsonBuilder.Append("versionDigestInfo", versionDigestInfo.GetJsonBuilder());
53     }
54     if (taskBodyTemplate & UPGRADE_STATUS) {
55         jsonBuilder.Append("status", CAST_INT(status));
56     }
57     if (taskBodyTemplate & SUB_STATUS) {
58         jsonBuilder.Append("subStatus", subStatus);
59     }
60     if (taskBodyTemplate & PROGRESS) {
61         jsonBuilder.Append("progress", progress);
62     }
63     if (taskBodyTemplate & INSTALL_MODE) {
64         jsonBuilder.Append("installMode", installMode);
65     }
66     if (taskBodyTemplate & ERROR_MESSAGE) {
67         jsonBuilder.Append("errorMessages", GetArrayJsonBuilderList(errorMessages));
68     }
69     if (taskBodyTemplate & VERSION_COMPONENT) {
70         jsonBuilder.Append("versionComponents", GetArrayJsonBuilderList(versionComponents));
71     }
72     return jsonBuilder.Append("}");
73 }
74 } // namespace OHOS::UpdateEngine