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