1 /*
2 * Copyright (c) 2022 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 #include "distributed_bundle_info.h"
17
18 #include "nlohmann/json.hpp"
19 #include "parcel_macro.h"
20 #include "string_ex.h"
21
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24 #include "json_util.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const std::string JSON_KEY_VERSION = "version";
30 const std::string JSON_KEY_VERSION_CODE = "versionCode";
31 const std::string JSON_KEY_COMPATIBLE_VERSION_CODE = "compatibleVersionCode";
32 const std::string JSON_KEY_VERSION_NAME = "versionName";
33 const std::string JSON_KEY_MIN_COMPATIBLE_VERSION = "minCompatibleVersion";
34 const std::string JSON_KEY_TARGET_VERSION_CODE = "targetVersionCode";
35 const std::string JSON_KEY_APP_ID = "appId";
36 const std::string JSON_KEY_MODULE_INFOS = "moduleInfos";
37 const std::string JSON_KEY_ENABLED = "enabled";
38 }
ReadFromParcel(Parcel & parcel)39 bool DistributedBundleInfo::ReadFromParcel(Parcel &parcel)
40 {
41 version = parcel.ReadUint32();
42 versionCode = parcel.ReadUint32();
43 compatibleVersionCode = parcel.ReadUint32();
44 minCompatibleVersion = parcel.ReadUint32();
45 targetVersionCode = parcel.ReadUint32();
46 bundleName = Str16ToStr8(parcel.ReadString16());
47 versionName = Str16ToStr8(parcel.ReadString16());
48 appId = Str16ToStr8(parcel.ReadString16());
49
50 uint32_t moduleInfosSize;
51 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, moduleInfosSize);
52 CONTAINER_SECURITY_VERIFY(parcel, moduleInfosSize, &moduleInfos);
53 for (uint32_t i = 0; i < moduleInfosSize; i++) {
54 std::unique_ptr<DistributedModuleInfo> distributedModuleInfo(parcel.ReadParcelable<DistributedModuleInfo>());
55 if (!distributedModuleInfo) {
56 APP_LOGE("ReadParcelable<DistributedModuleInfo> failed");
57 return false;
58 }
59 moduleInfos.emplace_back(*distributedModuleInfo);
60 }
61 enabled = parcel.ReadBool();
62 return true;
63 }
64
Marshalling(Parcel & parcel) const65 bool DistributedBundleInfo::Marshalling(Parcel &parcel) const
66 {
67 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, version);
68 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
69 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, compatibleVersionCode);
70 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, minCompatibleVersion);
71 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, targetVersionCode);
72 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
73 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
74 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appId));
75
76 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, moduleInfos.size());
77 for (auto &moduleInfo : moduleInfos) {
78 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &moduleInfo);
79 }
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
81 return true;
82 }
83
Unmarshalling(Parcel & parcel)84 DistributedBundleInfo *DistributedBundleInfo::Unmarshalling(Parcel &parcel)
85 {
86 DistributedBundleInfo *info = new (std::nothrow) DistributedBundleInfo();
87 if (info && !info->ReadFromParcel(parcel)) {
88 APP_LOGW("read from parcel failed");
89 delete info;
90 info = nullptr;
91 }
92 return info;
93 }
94
ToString() const95 std::string DistributedBundleInfo::ToString() const
96 {
97 nlohmann::json jsonObject;
98 jsonObject[JSON_KEY_VERSION] = version;
99 jsonObject[Constants::BUNDLE_NAME] = bundleName;
100 jsonObject[JSON_KEY_VERSION_CODE] = versionCode;
101 jsonObject[JSON_KEY_VERSION_NAME] = versionName;
102 jsonObject[JSON_KEY_COMPATIBLE_VERSION_CODE] = compatibleVersionCode;
103 jsonObject[JSON_KEY_MIN_COMPATIBLE_VERSION] = minCompatibleVersion;
104 jsonObject[JSON_KEY_TARGET_VERSION_CODE] = targetVersionCode;
105 jsonObject[JSON_KEY_APP_ID] = appId;
106 jsonObject[JSON_KEY_MODULE_INFOS] = moduleInfos;
107 jsonObject[JSON_KEY_ENABLED] = enabled;
108 return jsonObject.dump();
109 }
110
FromJsonString(const std::string & jsonString)111 bool DistributedBundleInfo::FromJsonString(const std::string &jsonString)
112 {
113 nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
114 if (jsonObject.is_discarded()) {
115 APP_LOGE("failed to parse DistributedBundleInfo: %{public}s.", jsonString.c_str());
116 return false;
117 }
118
119 const auto &jsonObjectEnd = jsonObject.end();
120 int32_t parseResult = ERR_OK;
121 GetValueIfFindKey<uint32_t>(jsonObject,
122 jsonObjectEnd,
123 JSON_KEY_VERSION,
124 version,
125 JsonType::NUMBER,
126 false,
127 parseResult,
128 ArrayType::NOT_ARRAY);
129 GetValueIfFindKey<std::string>(jsonObject,
130 jsonObjectEnd,
131 Constants::BUNDLE_NAME,
132 bundleName,
133 JsonType::STRING,
134 false,
135 parseResult,
136 ArrayType::NOT_ARRAY);
137 GetValueIfFindKey<uint32_t>(jsonObject,
138 jsonObjectEnd,
139 JSON_KEY_VERSION_CODE,
140 versionCode,
141 JsonType::NUMBER,
142 false,
143 parseResult,
144 ArrayType::NOT_ARRAY);
145 GetValueIfFindKey<uint32_t>(jsonObject,
146 jsonObjectEnd,
147 JSON_KEY_COMPATIBLE_VERSION_CODE,
148 compatibleVersionCode,
149 JsonType::NUMBER,
150 false,
151 parseResult,
152 ArrayType::NOT_ARRAY);
153 GetValueIfFindKey<std::string>(jsonObject,
154 jsonObjectEnd,
155 JSON_KEY_VERSION_NAME,
156 versionName,
157 JsonType::STRING,
158 false,
159 parseResult,
160 ArrayType::NOT_ARRAY);
161 GetValueIfFindKey<uint32_t>(jsonObject,
162 jsonObjectEnd,
163 JSON_KEY_MIN_COMPATIBLE_VERSION,
164 minCompatibleVersion,
165 JsonType::NUMBER,
166 false,
167 parseResult,
168 ArrayType::NOT_ARRAY);
169 GetValueIfFindKey<uint32_t>(jsonObject,
170 jsonObjectEnd,
171 JSON_KEY_TARGET_VERSION_CODE,
172 targetVersionCode,
173 JsonType::NUMBER,
174 false,
175 parseResult,
176 ArrayType::NOT_ARRAY);
177 GetValueIfFindKey<std::string>(jsonObject,
178 jsonObjectEnd,
179 JSON_KEY_APP_ID,
180 appId,
181 JsonType::STRING,
182 false,
183 parseResult,
184 ArrayType::NOT_ARRAY);
185 GetValueIfFindKey<std::vector<DistributedModuleInfo>>(jsonObject,
186 jsonObjectEnd,
187 JSON_KEY_MODULE_INFOS,
188 moduleInfos,
189 JsonType::ARRAY,
190 false,
191 parseResult,
192 ArrayType::OBJECT);
193 GetValueIfFindKey<bool>(jsonObject,
194 jsonObjectEnd,
195 JSON_KEY_ENABLED,
196 enabled,
197 JsonType::BOOLEAN,
198 false,
199 parseResult,
200 ArrayType::NOT_ARRAY);
201 return parseResult == ERR_OK;
202 }
203 } // namespace AppExecFwk
204 } // namespace OHOS
205