1 /*
2  * Copyright (c) 2022-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 OHOS_FILEMGMT_BACKUP_JSON_UTIL_H
17 #define OHOS_FILEMGMT_BACKUP_JSON_UTIL_H
18 
19 #include <string>
20 #include <map>
21 
22 namespace OHOS::FileManagement::Backup {
23 class BJsonUtil {
24 public:
25     typedef struct BundleDetailInfo {
26         std::string bundleName;
27         std::string type;
28         std::string detail;
29         int bundleIndex;
30         int32_t userId;
31     }BundleDetailInfo;
32 
33     /**
34      * @brief 带有拼接字符的bundleName按照拼接字符进行分割
35      *
36      * @param bundleNameStr bundleName拼接index的字符串
37      *
38      * @return 分割好的结果赋值给结构体
39      */
40     static BundleDetailInfo ParseBundleNameIndexStr (const std::string &bundleNameStr);
41 
42     /**
43      * @brief 将传进来的bundleNames的集合进行按照拼接字符分割处理
44      *
45      * @param bundleNames bundleName拼接index的字符串集合
46      * @param details infos的集合
47      * @param patternInfo 拼接的字符
48      * @param realBundleNames 分割后真正的bundleNames
49      * @param userId userId
50      * @param isClearDataFlags 框架是否清理标志集合
51      *
52      * @return 包名和解析结果的对应关系集合
53      *
54      */
55     static std::map<std::string, std::vector<BundleDetailInfo>> BuildBundleInfos(
56         const std::vector<std::string> &bundleNames, const std::vector<std::string> &details,
57         std::vector<std::string> &realBundleNames, int32_t userId,
58         std::map<std::string, bool> &isClearDataFlags);
59 
60     /**
61      * @brief 解析单个bundle对应的json串
62      *
63      * @param bundleInfo json串
64      * @param bundleDetails 结构体对象
65      * @param bundleDetailInfo bundle信息
66      * @param isClearData 框架是否清理标志
67      * @param userId userId
68      *
69      */
70     static void ParseBundleInfoJson(const std::string &bundleInfo, std::vector<BundleDetailInfo> &bundleDetails,
71         BJsonUtil::BundleDetailInfo bundleDetailInfo, bool &isClearData, int32_t userId);
72 
73     /**
74      * @brief 根据业务类型和bundleName确定唯一的bundleInfo
75      *
76      * @param bundleNameDetailsMap 包名和当前包扩展信息解析结果的集合
77      * @param bundleName 包名
78      * @param jobType 业务类型broadcast或者unicast
79      * @param bundleDetail 确定下来的bundleInfo
80      *
81      * @return 是否获取到
82      *
83      */
84     static bool FindBundleInfoByName(std::map<std::string, std::vector<BundleDetailInfo>> &bundleNameDetailsMap,
85         std::string &bundleName, const std::string &jobType, BundleDetailInfo &bundleDetail);
86 
87     /**
88      * @brief 组建恢复错误信息的json
89      *
90      * @param jsonStr 组建结果
91      * @param errCode 错误码
92      * @param errMsg 错误信息
93      *
94      * @return 是否组建成功
95      *
96      */
97     static bool BuildExtensionErrInfo(std::string &jsonStr, int errCode, std::string errMsg);
98 
99     /* *
100      * @brief 组建恢复文件错误信息的json
101      *
102      * @param jsonStr 组建结果
103      * @param errCode 错误码
104      *
105      * @return 是否组建成功
106      *
107      */
108     static bool BuildExtensionErrInfo(std::string &jsonStr, std::map<std::string, std::vector<int>> errFileInfo);
109 
110     /* *
111      * @brief 组建App进度返回的信息
112      * @param jsonStr 组建结果
113      * @param onProcessRet onProcess接口返回值
114      *
115      * @return 是否组建成功
116      *
117      */
118     static bool BuildOnProcessRetInfo(std::string &jsonStr, std::string onProcessRet);
119 
120     /* *
121      * @brief 拼接包名和分身对应的索引
122      *
123      * @param bundleName 包名
124      * @param bundleIndex 索引
125      *
126      * @return 拼接结果
127      */
128     static std::string BuildBundleNameIndexInfo(const std::string &bundleName, int bundleIndex);
129     /**
130      * @brief 组建App进度返回的信息
131      *
132      * @param reportInfo 组建结果
133      * @param path 报错文件
134      * @param err 错误码
135      *
136      * @return 是否组建成功
137      *
138      */
139     static bool BuildOnProcessErrInfo(std::string &reportInfo, std::string path, int err);
140 
141     /**
142      * @brief 构建包含userId的detailInfo
143      *
144      * @param userId userId
145      * @param detailInfo 包含userId的detailInfo
146      *
147      * @return 是否组建成功
148      *
149      */
150     static bool BuildBundleInfoJson(int32_t userId, std::string &detailInfo);
151 
152     /**
153      * @brief 判断传入的bundleinfo中是否包含unicast字段
154      *
155      * @param bundleinfo json串
156      *
157      * @return 是否包含unicast字段
158      *
159      */
160     static bool HasUnicastInfo(std::string &bundleInfo);
161 };
162 } // namespace OHOS::FileManagement::Backup
163 
164 #endif // OHOS_FILEMGMT_BACKUP_JSON_UTIL_H