• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     typedef struct BundleDataSize {
34         std::string bundleName;
35         int64_t dataSize = -1;
36         int64_t incDataSize = -1;
37     }BundleDataSize;
38 
39     /**
40      * @brief 带有拼接字符的bundleName按照拼接字符进行分割
41      *
42      * @param bundleNameStr bundleName拼接index的字符串
43      *
44      * @return 分割好的结果赋值给结构体
45      */
46     static BundleDetailInfo ParseBundleNameIndexStr (const std::string &bundleNameStr);
47 
48     /**
49      * @brief 将传进来的bundleNames的集合进行按照拼接字符分割处理
50      *
51      * @param bundleNames bundleName拼接index的字符串集合
52      * @param details infos的集合
53      * @param patternInfo 拼接的字符
54      * @param realBundleNames 分割后真正的bundleNames
55      * @param userId userId
56      * @param isClearDataFlags 框架是否清理标志集合
57      *
58      * @return 包名和解析结果的对应关系集合
59      *
60      */
61     static std::map<std::string, std::vector<BundleDetailInfo>> BuildBundleInfos(
62         const std::vector<std::string> &bundleNames, const std::vector<std::string> &details,
63         std::vector<std::string> &realBundleNames, int32_t userId,
64         std::map<std::string, bool> &isClearDataFlags);
65 
66     /**
67      * @brief 解析单个bundle对应的json串
68      *
69      * @param bundleInfo json串
70      * @param bundleDetails 结构体对象
71      * @param bundleDetailInfo bundle信息
72      * @param isClearData 框架是否清理标志
73      * @param userId userId
74      *
75      */
76     static void ParseBundleInfoJson(const std::string &bundleInfo, std::vector<BundleDetailInfo> &bundleDetails,
77         BJsonUtil::BundleDetailInfo bundleDetailInfo, bool &isClearData, int32_t userId);
78 
79     /**
80      * @brief 根据业务类型和bundleName确定唯一的bundleInfo
81      *
82      * @param bundleNameDetailsMap 包名和当前包扩展信息解析结果的集合
83      * @param bundleName 包名
84      * @param jobType 业务类型broadcast或者unicast
85      * @param bundleDetail 确定下来的bundleInfo
86      *
87      * @return 是否获取到
88      *
89      */
90     static bool FindBundleInfoByName(std::map<std::string, std::vector<BundleDetailInfo>> &bundleNameDetailsMap,
91         std::string &bundleName, const std::string &jobType, BundleDetailInfo &bundleDetail);
92 
93     /**
94      * @brief 组建恢复错误信息的json
95      *
96      * @param jsonStr 组建结果
97      * @param errCode 错误码
98      * @param errMsg 错误信息
99      *
100      * @return 是否组建成功
101      *
102      */
103     static bool BuildExtensionErrInfo(std::string &jsonStr, int errCode, std::string errMsg);
104 
105     /**
106      * @brief 拼接包名和分身对应的索引
107      *
108      * @param bundleName 包名
109      * @param bundleIndex 索引
110      *
111      * @return 拼接结果
112      */
113     static std::string BuildBundleNameIndexInfo(const std::string &bundleName, int bundleIndex);
114 
115     /**
116      * @brief 组建恢复文件错误信息的json
117      *
118      * @param jsonStr 组建结果
119      * @param errCode 错误码
120      *
121      * @return 是否组建成功
122      *
123      */
124     static bool BuildExtensionErrInfo(std::string &jsonStr, std::map<std::string, std::vector<int>> errFileInfo);
125 
126     /**
127      * @brief 组建App进度返回的信息
128      *
129      * @param jsonStr 组建结果
130      * @param onProcessRet onProcess接口返回值
131      *
132      * @return 是否组建成功
133      *
134      */
135     static bool BuildOnProcessRetInfo(std::string &jsonStr, std::string onProcessRet);
136 
137     /**
138      * @brief 组建App进度返回的信息
139      *
140      * @param reportInfo 组建结果
141      * @param path 报错文件
142      * @param err 错误码
143      *
144      * @return 是否组建成功
145      *
146      */
147     static bool BuildOnProcessErrInfo(std::string &reportInfo, std::string path, int err);
148 
149     /**
150      * @brief 构建包含userId的detailInfo
151      *
152      * @param userId userId
153      * @param detailInfo 包含userId的detailInfo
154      *
155      * @return 是否组建成功
156      *
157      */
158     static bool BuildBundleInfoJson(int32_t userId, std::string &detailInfo);
159 
160     /**
161      * @brief 判断传入的bundleinfo中是否包含unicast字段
162      *
163      * @param bundleinfo json串
164      *
165      * @return 是否包含unicast字段
166      *
167      */
168     static bool HasUnicastInfo(std::string &bundleInfo);
169 
170     /**
171      * @brief 解析备份框架版本号字段
172      *
173      * @return 备份恢复框架版本号
174      *
175      */
176     static std::string ParseBackupVersion();
177 
178     /**
179      * @brief 拼接session冲突时报错信息
180      *
181      * @param userId 用户id
182      * @param callerName session创建方
183      * @param activeTime session创建时间
184      *
185      * @return 拼接结果
186      */
187     static std::string BuildInitSessionErrInfo(int32_t userId, std::string callerName, std::string activeTime);
188 
189     /**
190      * @brief 将已经扫描的结果转换成json串
191      *
192      * @param bundleDataList 存储扫描结果的结构体列表
193      * @param listSize 当前需要返回的数量
194      * @param scanning 当前正在扫描的包名
195      * @param jsonStr 需要返回的结果
196      *
197      * @return 是否成功
198      */
199     static bool WriteToStr(std::vector<BundleDataSize> &bundleDataList,
200                            size_t listSize,
201                            std::string scanning,
202                            std::string &jsonStr);
203 };
204 } // namespace OHOS::FileManagement::Backup
205 
206 #endif // OHOS_FILEMGMT_BACKUP_JSON_UTIL_H