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