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 GetSystemFullName()102 std::string GetSystemFullName() 103 { 104 if (!obj_ || !obj_.isMember("systemFullName") || !obj_["systemFullName"].isString()) { 105 HILOGI("Failed to get field systemFullName"); 106 return ""; 107 } 108 109 return obj_["systemFullName"].asString(); 110 } 111 GetDeviceType()112 std::string GetDeviceType() 113 { 114 if (!obj_ || !obj_.isMember("deviceType") || !obj_["deviceType"].isString()) { 115 HILOGI("Failed to get field deviceType"); 116 return ""; 117 } 118 119 return obj_["deviceType"].asString(); 120 } 121 GetRestoreDeps()122 std::string GetRestoreDeps() 123 { 124 if (!obj_ || !obj_.isMember("restoreDeps") || !obj_["restoreDeps"].isString()) { 125 HILOGI("Failed to get field restoreDeps"); 126 return ""; 127 } 128 129 return obj_["restoreDeps"].asString(); 130 } 131 GetSupportScene()132 std::string GetSupportScene() 133 { 134 if (!obj_ || !obj_.isMember("supportScene") || !obj_["supportScene"].isString()) { 135 HILOGI("Failed to get field supportScene"); 136 return ""; 137 } 138 139 return obj_["supportScene"].asString(); 140 } 141 GetExtraInfo()142 Json::Value GetExtraInfo() 143 { 144 if (!obj_ || !obj_.isMember("extraInfo") || !obj_["extraInfo"].isObject()) { 145 HILOGE("Failed to get field extraInfo"); 146 return Json::Value(); 147 } 148 return obj_["extraInfo"]; 149 } 150 CheckBundlePropertiesValid(const Json::Value & bundleInfo)151 bool CheckBundlePropertiesValid(const Json::Value &bundleInfo) 152 { 153 if (!bundleInfo) { 154 HILOGE("Failed Check bundleInfo"); 155 return false; 156 } 157 if (!bundleInfo.isMember("name") || !bundleInfo["name"].isString()) { 158 HILOGE("Failed Check bundleInfo name property"); 159 return false; 160 } 161 if (!bundleInfo.isMember("versionCode") || !bundleInfo["versionCode"].isInt64()) { 162 HILOGE("Failed Check bundleInfo versionCode property"); 163 return false; 164 } 165 if (!bundleInfo.isMember("versionName") || !bundleInfo["versionName"].isString()) { 166 HILOGE("Failed Check bundleInfo versionName property"); 167 return false; 168 } 169 if (!bundleInfo.isMember("spaceOccupied") || !bundleInfo["spaceOccupied"].isInt64()) { 170 HILOGE("Failed Check bundleInfo spaceOccupied property"); 171 return false; 172 } 173 if (!bundleInfo.isMember("allToBackup") || !bundleInfo["allToBackup"].isBool()) { 174 HILOGE("Failed Check bundleInfo allToBackup property"); 175 return false; 176 } 177 if (!bundleInfo.isMember("extensionName") || !bundleInfo["extensionName"].isString()) { 178 HILOGE("Failed Check bundleInfo extensionName property"); 179 return false; 180 } 181 return true; 182 } 183 GetBundleInfos()184 std::vector<BundleInfo> GetBundleInfos() 185 { 186 if (!obj_ || !obj_.isMember("bundleInfos") || !obj_["bundleInfos"].isArray()) { 187 HILOGI("Failed to get field get bundleInfos"); 188 return {}; 189 } 190 std::vector<BundleInfo> bundleInfos; 191 for (const auto &item : obj_["bundleInfos"]) { 192 if (!CheckBundlePropertiesValid(item)) { 193 return {}; 194 } 195 string restoreDeps(""); 196 if (item.isMember("restoreDeps") && item["restoreDeps"].isString()) { 197 restoreDeps = item["restoreDeps"].asString(); 198 } 199 string supportScene(""); 200 if (item.isMember("supportScene") && item["supportScene"].isString()) { 201 restoreDeps = item["supportScene"].asString(); 202 } 203 Json::Value extraInfo; 204 if (item.isMember("extraInfo") && item["extraInfo"].isObject()) { 205 extraInfo = item["extraInfo"]; 206 } 207 bool fullBackupOnly = false; 208 if (item.isMember("fullBackupOnly") && item["fullBackupOnly"].isBool()) { 209 fullBackupOnly = item["fullBackupOnly"].asBool(); 210 } 211 int appIndex = 0; 212 if (item.isMember("appIndex") && item["appIndex"].isInt()) { 213 appIndex = item["appIndex"].asInt(); 214 } 215 int64_t increSpaceOccupied = 0; 216 if (item.isMember("increSpaceOccupied") && item["increSpaceOccupied"].isInt64()) { 217 increSpaceOccupied = item["increSpaceOccupied"].asInt64(); 218 } 219 bundleInfos.emplace_back(BundleInfo {item["name"].asString(), appIndex, item["versionCode"].asInt64(), 220 item["versionName"].asString(), item["spaceOccupied"].asInt64(), 221 increSpaceOccupied, item["allToBackup"].asBool(), fullBackupOnly, 222 item["extensionName"].asString(), 223 restoreDeps, supportScene, extraInfo}); 224 } 225 return bundleInfos; 226 } 227 228 public: 229 /** 230 * @brief 构造方法,具备T(Json::Value&, std::any)能力的构造函数 231 * 232 * @param obj Json对象引用 233 * @param option 任意类型对象 234 */ 235 explicit BJsonEntityCaps(Json::Value &obj, std::any option = std::any()) : BJsonEntity(obj, option) 236 { 237 SetBundleInfos(GetBundleInfos()); 238 } 239 240 BJsonEntityCaps() = delete; 241 ~BJsonEntityCaps() override = default; 242 }; 243 } // namespace OHOS::FileManagement::Backup 244 245 #endif // OHOS_FILEMGMT_BACKUP_B_JSON_ENTITY_CAPS_H