• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_B_JSON_ENTITY_CAPS_H
17 #define OHOS_FILEMGMT_BACKUP_B_JSON_ENTITY_CAPS_H
18 
19 #include "b_json/b_json_cached_entity.h"
20 #include "filemgmt_libhilog.h"
21 
22 namespace OHOS::FileManagement::Backup {
23 class BJsonEntityCaps : public BJsonEntity {
24 public:
25     struct BundleInfo {
26         std::string name;
27         int appIndex;
28         int64_t versionCode;
29         std::string versionName;
30         int64_t spaceOccupied;
31         int64_t increSpaceOccupied;
32         bool allToBackup;
33         bool fullBackupOnly;
34         bool requireCompatibility;
35         std::string extensionName;
36         std::string restoreDeps;
37         std::string supportScene;
38         Json::Value extraInfo;
39     };
40     struct BundleBackupConfigPara {
41         bool allToBackup;
42         bool fullBackupOnly;
43         bool requireCompatibility;
44         std::string extensionName;
45         std::string restoreDeps;
46         std::string supportScene;
47         std::vector<std::string> includes;
48         std::vector<std::string> excludes;
49         Json::Value extraInfo;
50     };
51 public:
SetSystemFullName(const char * systemFullName)52     void SetSystemFullName(const char *systemFullName)
53     {
54         if (systemFullName == nullptr) {
55             HILOGE("systemFullName is nullptr, use default.");
56             obj_["systemFullName"] = "default";
57             return;
58         }
59         obj_["systemFullName"] = systemFullName;
60     }
61 
SetDeviceType(const char * deviceType)62     void SetDeviceType(const char * deviceType)
63     {
64         if (deviceType == nullptr) {
65             HILOGE("deviceType is nullptr, use default.");
66             obj_["deviceType"] = "default";
67             return;
68         }
69         obj_["deviceType"] = deviceType;
70     }
71 
72     void SetBundleInfos(std::vector<BundleInfo> bundleInfos, bool incre = false)
73     {
74         if (obj_.isMember("bundleInfos")) {
75             obj_["bundleInfos"].clear();
76         }
77         for (const auto &item : bundleInfos) {
78             Json::Value arrObj;
79             arrObj["name"] = item.name;
80             arrObj["appIndex"] = item.appIndex;
81             arrObj["versionCode"] = item.versionCode;
82             arrObj["versionName"] = item.versionName;
83             arrObj["spaceOccupied"] = item.spaceOccupied;
84             if (incre) {
85                 arrObj["increSpaceOccupied"] = item.increSpaceOccupied;
86             }
87             arrObj["allToBackup"] = item.allToBackup;
88             arrObj["fullBackupOnly"] = item.fullBackupOnly;
89             arrObj["requireCompatibility"] = item.requireCompatibility;
90             arrObj["extensionName"] = item.extensionName;
91             arrObj["restoreDeps"] = item.restoreDeps;
92             arrObj["supportScene"] = item.supportScene;
93             Json::Value extraInfo;
94             if (item.extraInfo.empty()) {
95                 Json::Value senceArray(Json::arrayValue);
96                 extraInfo["supportScene"] = senceArray;
97             } else {
98                 extraInfo = item.extraInfo;
99             }
100             arrObj["extraInfo"] = extraInfo;
101             obj_["bundleInfos"].append(arrObj);
102         }
103     }
104 
SetBackupVersion(const std::string & backupVersion)105     void SetBackupVersion(const std::string &backupVersion)
106     {
107         if (obj_.isMember("backupVersion")) {
108             obj_["backupVersion"].clear();
109         }
110         obj_["backupVersion"] = backupVersion;
111     }
112 
GetBackupVersion()113     std::string GetBackupVersion()
114     {
115         if (!obj_) {
116             HILOGI("Failed to get field backupVersion");
117             return "";
118         }
119         if (!obj_.isMember("backupVersion")) {
120             HILOGI("Failed to get field backupVersion from early Version, returning the default value");
121             return "";
122         }
123         if (!obj_["backupVersion"].isString()) {
124             HILOGI("Failed to get field backupVersion");
125             return "";
126         }
127         return obj_["backupVersion"].asString();
128     }
129 
GetSystemFullName()130     std::string GetSystemFullName()
131     {
132         if (!obj_ || !obj_.isMember("systemFullName") || !obj_["systemFullName"].isString()) {
133             HILOGI("Failed to get field systemFullName");
134             return "";
135         }
136 
137         return obj_["systemFullName"].asString();
138     }
139 
GetDeviceType()140     std::string GetDeviceType()
141     {
142         if (!obj_ || !obj_.isMember("deviceType") || !obj_["deviceType"].isString()) {
143             HILOGI("Failed to get field deviceType");
144             return "";
145         }
146 
147         return obj_["deviceType"].asString();
148     }
149 
GetRestoreDeps()150     std::string GetRestoreDeps()
151     {
152         if (!obj_ || !obj_.isMember("restoreDeps") || !obj_["restoreDeps"].isString()) {
153             HILOGI("Failed to get field restoreDeps");
154             return "";
155         }
156 
157         return obj_["restoreDeps"].asString();
158     }
159 
GetSupportScene()160     std::string GetSupportScene()
161     {
162         if (!obj_ || !obj_.isMember("supportScene") || !obj_["supportScene"].isString()) {
163             HILOGI("Failed to get field supportScene");
164             return "";
165         }
166 
167         return obj_["supportScene"].asString();
168     }
169 
GetExtraInfo()170     Json::Value GetExtraInfo()
171     {
172         if (!obj_ || !obj_.isMember("extraInfo") || !obj_["extraInfo"].isObject()) {
173             HILOGE("Failed to get field extraInfo");
174             return Json::Value();
175         }
176         return obj_["extraInfo"];
177     }
178 
CheckBundlePropertiesValid(const Json::Value & bundleInfo)179     bool CheckBundlePropertiesValid(const Json::Value &bundleInfo)
180     {
181         if (!bundleInfo) {
182             HILOGE("Failed Check bundleInfo");
183             return false;
184         }
185         if (!bundleInfo.isMember("name") || !bundleInfo["name"].isString()) {
186             HILOGE("Failed Check bundleInfo name property");
187             return false;
188         }
189         if (!bundleInfo.isMember("versionCode") || !bundleInfo["versionCode"].isInt64()) {
190             HILOGE("Failed Check bundleInfo versionCode property");
191             return false;
192         }
193         if (!bundleInfo.isMember("versionName") || !bundleInfo["versionName"].isString()) {
194             HILOGE("Failed Check bundleInfo versionName property");
195             return false;
196         }
197         if (!bundleInfo.isMember("spaceOccupied") || !bundleInfo["spaceOccupied"].isInt64()) {
198             HILOGE("Failed Check bundleInfo spaceOccupied property");
199             return false;
200         }
201         if (!bundleInfo.isMember("allToBackup") || !bundleInfo["allToBackup"].isBool()) {
202             HILOGE("Failed Check bundleInfo allToBackup property");
203             return false;
204         }
205         if (!bundleInfo.isMember("extensionName") || !bundleInfo["extensionName"].isString()) {
206             HILOGE("Failed Check bundleInfo extensionName property");
207             return false;
208         }
209         return true;
210     }
211 
GetBundleInfos()212     std::vector<BundleInfo> GetBundleInfos()
213     {
214         if (!obj_ || !obj_.isMember("bundleInfos") || !obj_["bundleInfos"].isArray()) {
215             HILOGI("Failed to get field get bundleInfos");
216             return {};
217         }
218         std::vector<BundleInfo> bundleInfos;
219         for (const auto &item : obj_["bundleInfos"]) {
220             if (!CheckBundlePropertiesValid(item)) {
221                 return {};
222             }
223             string restoreDeps("");
224             if (item.isMember("restoreDeps") && item["restoreDeps"].isString()) {
225                 restoreDeps = item["restoreDeps"].asString();
226             }
227             string supportScene("");
228             if (item.isMember("supportScene") && item["supportScene"].isString()) {
229                 restoreDeps = item["supportScene"].asString();
230             }
231             Json::Value extraInfo;
232             if (item.isMember("extraInfo") && item["extraInfo"].isObject()) {
233                 extraInfo = item["extraInfo"];
234             }
235             bool fullBackupOnly = false;
236             if (item.isMember("fullBackupOnly") && item["fullBackupOnly"].isBool()) {
237                 fullBackupOnly = item["fullBackupOnly"].asBool();
238             }
239             bool requireCompatibility = false;
240             if (item.isMember("requireCompatibility") && item["requireCompatibility"].isBool()) {
241                 requireCompatibility = item["requireCompatibility"].asBool();
242             }
243             int appIndex = 0;
244             if (item.isMember("appIndex") && item["appIndex"].isInt()) {
245                 appIndex = item["appIndex"].asInt();
246             }
247             int64_t increSpaceOccupied = 0;
248             if (item.isMember("increSpaceOccupied") && item["increSpaceOccupied"].isInt64()) {
249                 increSpaceOccupied = item["increSpaceOccupied"].asInt64();
250             }
251             bundleInfos.emplace_back(BundleInfo {item["name"].asString(), appIndex, item["versionCode"].asInt64(),
252                                                  item["versionName"].asString(), item["spaceOccupied"].asInt64(),
253                                                  increSpaceOccupied, item["allToBackup"].asBool(), fullBackupOnly,
254                                                  requireCompatibility, item["extensionName"].asString(),
255                                                  restoreDeps, supportScene, extraInfo});
256         }
257         return bundleInfos;
258     }
259 
260 public:
261     /**
262      * @brief 构造方法,具备T(Json::Value&, std::any)能力的构造函数
263      *
264      * @param obj Json对象引用
265      * @param option 任意类型对象
266      */
BJsonEntity(obj,option)267     explicit BJsonEntityCaps(Json::Value &obj, std::any option = std::any()) : BJsonEntity(obj, option)
268     {
269         SetBundleInfos(GetBundleInfos());
270     }
271 
272     BJsonEntityCaps() = delete;
273     ~BJsonEntityCaps() override = default;
274 };
275 } // namespace OHOS::FileManagement::Backup
276 
277 #endif // OHOS_FILEMGMT_BACKUP_B_JSON_ENTITY_CAPS_H