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 uint32_t versionCode; 28 std::string versionName; 29 int64_t spaceOccupied; 30 bool allToBackup; 31 std::string extensionName; 32 std::string restoreDeps; 33 std::string supportScene; 34 }; 35 36 public: SetSystemFullName(std::string systemFullName)37 void SetSystemFullName(std::string systemFullName) 38 { 39 obj_["systemFullName"] = systemFullName; 40 } 41 SetDeviceType(std::string deviceType)42 void SetDeviceType(std::string deviceType) 43 { 44 obj_["deviceType"] = deviceType; 45 } 46 SetBundleInfos(std::vector<BundleInfo> bundleInfos)47 void SetBundleInfos(std::vector<BundleInfo> bundleInfos) 48 { 49 if (obj_.isMember("bundleInfos")) { 50 obj_["bundleInfos"].clear(); 51 } 52 for (const auto &item : bundleInfos) { 53 Json::Value arrObj; 54 arrObj["name"] = item.name; 55 arrObj["versionCode"] = item.versionCode; 56 arrObj["versionName"] = item.versionName; 57 arrObj["spaceOccupied"] = item.spaceOccupied; 58 arrObj["allToBackup"] = item.allToBackup; 59 arrObj["extensionName"] = item.extensionName; 60 arrObj["restoreDeps"] = item.restoreDeps; 61 arrObj["supportScene"] = item.supportScene; 62 obj_["bundleInfos"].append(arrObj); 63 } 64 } 65 GetSystemFullName()66 std::string GetSystemFullName() 67 { 68 if (!obj_ || !obj_.isMember("systemFullName") || !obj_["systemFullName"].isString()) { 69 HILOGI("Failed to get field systemFullName"); 70 return ""; 71 } 72 73 return obj_["systemFullName"].asString(); 74 } 75 GetDeviceType()76 std::string GetDeviceType() 77 { 78 if (!obj_ || !obj_.isMember("deviceType") || !obj_["deviceType"].isString()) { 79 HILOGI("Failed to get field deviceType"); 80 return ""; 81 } 82 83 return obj_["deviceType"].asString(); 84 } 85 GetRestoreDeps()86 std::string GetRestoreDeps() 87 { 88 if (!obj_ || !obj_.isMember("restoreDeps") || !obj_["restoreDeps"].isString()) { 89 HILOGI("Failed to get field restoreDeps"); 90 return ""; 91 } 92 93 return obj_["restoreDeps"].asString(); 94 } 95 GetSupportScene()96 std::string GetSupportScene() 97 { 98 if (!obj_ || !obj_.isMember("supportScene") || !obj_["supportScene"].isString()) { 99 HILOGI("Failed to get field supportScene"); 100 return ""; 101 } 102 103 return obj_["supportScene"].asString(); 104 } 105 GetBundleInfos()106 std::vector<BundleInfo> GetBundleInfos() 107 { 108 if (!obj_ || !obj_.isMember("bundleInfos") || !obj_["bundleInfos"].isArray()) { 109 HILOGI("Failed to get field get bundleInfos"); 110 return {}; 111 } 112 std::vector<BundleInfo> bundleInfos; 113 for (const auto &item : obj_["bundleInfos"]) { 114 if (!item || !item["name"].isString() || !item["versionCode"].isUInt() || !item["versionName"].isString() || 115 !item["spaceOccupied"].isInt64() || !item["allToBackup"].isBool() || 116 !item["extensionName"].isString()) { 117 HILOGI("Failed to get field bundleInfos, type error"); 118 return {}; 119 } 120 string restoreDeps(""); 121 if (item.isMember("restoreDeps") && item["restoreDeps"].isString()) { 122 restoreDeps = item["restoreDeps"].asString(); 123 } 124 string supportScene(""); 125 if (item.isMember("supportScene") && item["supportScene"].isString()) { 126 restoreDeps = item["supportScene"].asString(); 127 } 128 bundleInfos.emplace_back(BundleInfo {item["name"].asString(), item["versionCode"].asUInt(), 129 item["versionName"].asString(), item["spaceOccupied"].asInt64(), 130 item["allToBackup"].asBool(), item["extensionName"].asString(), 131 restoreDeps, supportScene}); 132 } 133 return bundleInfos; 134 } 135 136 public: 137 /** 138 * @brief 构造方法,具备T(Json::Value&, std::any)能力的构造函数 139 * 140 * @param obj Json对象引用 141 * @param option 任意类型对象 142 */ BJsonEntity(obj,option)143 explicit BJsonEntityCaps(Json::Value &obj, std::any option = std::any()) : BJsonEntity(obj, option) 144 { 145 SetBundleInfos(GetBundleInfos()); 146 } 147 148 BJsonEntityCaps() = delete; 149 ~BJsonEntityCaps() override = default; 150 }; 151 } // namespace OHOS::FileManagement::Backup 152 153 #endif // OHOS_FILEMGMT_BACKUP_B_JSON_ENTITY_CAPS_H