1 /*
2 * Copyright (c) 2024 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 "mission/distributed_bundle_info.h"
17
18 #include "mission/json_util.h"
19 #include "nlohmann/json.hpp"
20 #include "parcel_helper.h"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace DistributedSchedule {
26 namespace {
27 const std::string TAG = "dmsBundleInfo";
28 const std::string JSON_KEY_VERSION = "version";
29 const std::string JSON_KEY_VERSION_CODE = "versionCode";
30 const std::string JSON_KEY_COMPATIBLE_VERSION_CODE = "compatibleVersionCode";
31 const std::string JSON_KEY_VERSION_NAME = "versionName";
32 const std::string JSON_KEY_MIN_COMPATIBLE_VERSION = "minCompatibleVersion";
33 const std::string JSON_KEY_TARGET_VERSION_CODE = "targetVersionCode";
34 const std::string JSON_KEY_APP_ID = "appId";
35 const std::string JSON_KEY_ENABLED = "enabled";
36 const std::string JSON_KEY_BUNDLE_NAME_ID = "bundleNameId";
37 const std::string JSON_KEY_UPDATE_TIME = "updateTime";
38 const std::string JSON_KEY_DEVELOPER_ID = "developerId";
39 const std::string JSON_KEY_DMS_ABILITY_INFOS = "dmsAbilityInfos";
40 const std::string JSON_KEY_DMS_ABILITY_NAME = "abilityName";
41 const std::string JSON_KEY_DMS_CONTINUETYPE = "continueType";
42 const std::string JSON_KEY_DMS_CONTINUETYPEID = "continueTypeId";
43 const std::string JSON_KEY_DMS_USERID = "userIdArr";
44 const std::string JSON_KEY_DMS_MODULENAME = "moduleName";
45 const std::string JSON_KEY_DMS_CONTINUEBUNDLENAME = "continueBundleName";
46 const std::string JSON_KEY_DMS_MAX_BUNDLENAME_ID = "maxBundleNameId";
47 }
48
ReadFromParcel(Parcel & parcel)49 bool PublicRecordsInfo::ReadFromParcel(Parcel &parcel)
50 {
51 maxBundleNameId = parcel.ReadUint16();
52 return true;
53 }
54
Marshalling(Parcel & parcel) const55 bool PublicRecordsInfo::Marshalling(Parcel &parcel) const
56 {
57 parcel.WriteUint16(maxBundleNameId);
58 return true;
59 }
60
Unmarshalling(Parcel & parcel)61 PublicRecordsInfo *PublicRecordsInfo::Unmarshalling(Parcel &parcel)
62 {
63 PublicRecordsInfo *info = new (std::nothrow) PublicRecordsInfo();
64 if (info != nullptr && !info->ReadFromParcel(parcel)) {
65 APP_LOGW("read from parcel failed");
66 delete info;
67 info = nullptr;
68 }
69 return info;
70 }
71
ToString() const72 std::string PublicRecordsInfo::ToString() const
73 {
74 nlohmann::json jsonObject;
75 jsonObject[JSON_KEY_DMS_MAX_BUNDLENAME_ID] = maxBundleNameId;
76 return jsonObject.dump();
77 }
78
to_json(nlohmann::json & jsonObject,const PublicRecordsInfo & publicRecordsInfo)79 void to_json(nlohmann::json &jsonObject, const PublicRecordsInfo &publicRecordsInfo)
80 {
81 APP_LOGI("call");
82 jsonObject = nlohmann::json {
83 {JSON_KEY_DMS_MAX_BUNDLENAME_ID, publicRecordsInfo.maxBundleNameId},
84 };
85 APP_LOGI("end");
86 }
87
from_json(const nlohmann::json & jsonObject,PublicRecordsInfo & publicRecordsInfo)88 void from_json(const nlohmann::json &jsonObject, PublicRecordsInfo &publicRecordsInfo)
89 {
90 APP_LOGI("call");
91 const auto &jsonObjectEnd = jsonObject.end();
92 int32_t parseResult = ERR_OK;
93 GetValueIfFindKey<uint16_t>(jsonObject,
94 jsonObjectEnd,
95 JSON_KEY_DMS_CONTINUETYPEID,
96 publicRecordsInfo.maxBundleNameId,
97 JsonType::NUMBER,
98 false,
99 parseResult,
100 ArrayType::NOT_ARRAY);
101 if (parseResult != ERR_OK) {
102 APP_LOGE("read PublicRecordsInfo from jsonObject error, error code : %{public}d", parseResult);
103 }
104 APP_LOGI("end");
105 }
106
FromJsonString(const std::string & jsonString)107 bool PublicRecordsInfo::FromJsonString(const std::string &jsonString)
108 {
109 nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
110 if (jsonObject.is_discarded()) {
111 return false;
112 }
113 const auto &jsonObjectEnd = jsonObject.end();
114 int32_t parseResult = ERR_OK;
115 GetValueIfFindKey<uint16_t>(jsonObject, jsonObjectEnd, JSON_KEY_DMS_MAX_BUNDLENAME_ID, maxBundleNameId,
116 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
117 return parseResult == ERR_OK;
118 }
119
ReadFromParcel(Parcel & parcel)120 bool DmsAbilityInfo::ReadFromParcel(Parcel &parcel)
121 {
122 abilityName = Str16ToStr8(parcel.ReadString16());
123
124 int32_t continueTypeSize;
125 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, continueTypeSize);
126 CONTAINER_SECURITY_VERIFY(parcel, continueTypeSize, &continueType);
127 for (auto i = 0; i < continueTypeSize; i++) {
128 continueType.emplace_back(Str16ToStr8(parcel.ReadString16()));
129 }
130
131 int32_t continueTypeIdSize;
132 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, continueTypeIdSize);
133 CONTAINER_SECURITY_VERIFY(parcel, continueTypeIdSize, &continueTypeId);
134 for (auto i = 0; i < continueTypeIdSize; i++) {
135 continueTypeId.emplace_back(parcel.ReadUint8());
136 }
137 moduleName = Str16ToStr8(parcel.ReadString16());
138 return true;
139 }
140
Marshalling(Parcel & parcel) const141 bool DmsAbilityInfo::Marshalling(Parcel &parcel) const
142 {
143 parcel.WriteString16(Str8ToStr16(abilityName));
144 for (auto &ele : continueType) {
145 parcel.WriteString16(Str8ToStr16(ele));
146 }
147 for (auto &ele : continueTypeId) {
148 parcel.WriteUint8(ele);
149 }
150 parcel.WriteString16(Str8ToStr16(moduleName));
151 return true;
152 }
153
Unmarshalling(Parcel & parcel)154 DmsAbilityInfo *DmsAbilityInfo::Unmarshalling(Parcel &parcel)
155 {
156 DmsAbilityInfo *info = new (std::nothrow) DmsAbilityInfo();
157 if (info != nullptr && !info->ReadFromParcel(parcel)) {
158 APP_LOGW("read from parcel failed");
159 delete info;
160 info = nullptr;
161 }
162 return info;
163 }
164
to_json(nlohmann::json & jsonObject,const DmsAbilityInfo & dmsAbilityInfo)165 void to_json(nlohmann::json &jsonObject, const DmsAbilityInfo &dmsAbilityInfo)
166 {
167 jsonObject = nlohmann::json {
168 {JSON_KEY_DMS_ABILITY_NAME, dmsAbilityInfo.abilityName},
169 {JSON_KEY_DMS_CONTINUETYPE, dmsAbilityInfo.continueType},
170 {JSON_KEY_DMS_CONTINUETYPEID, dmsAbilityInfo.continueTypeId},
171 {JSON_KEY_DMS_MODULENAME, dmsAbilityInfo.moduleName},
172 {JSON_KEY_DMS_CONTINUEBUNDLENAME, dmsAbilityInfo.continueBundleName}
173 };
174 }
175
from_json(const nlohmann::json & jsonObject,DmsAbilityInfo & dmsAbilityInfo)176 void from_json(const nlohmann::json &jsonObject, DmsAbilityInfo &dmsAbilityInfo)
177 {
178 const auto &jsonObjectEnd = jsonObject.end();
179 int32_t parseResult = ERR_OK;
180 GetValueIfFindKey<std::string>(jsonObject,
181 jsonObjectEnd,
182 JSON_KEY_DMS_ABILITY_NAME,
183 dmsAbilityInfo.abilityName,
184 JsonType::STRING,
185 false,
186 parseResult,
187 ArrayType::NOT_ARRAY);
188 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
189 jsonObjectEnd,
190 JSON_KEY_DMS_CONTINUETYPE,
191 dmsAbilityInfo.continueType,
192 JsonType::ARRAY,
193 false,
194 parseResult,
195 ArrayType::STRING);
196 GetValueIfFindKey<std::vector<uint8_t>>(jsonObject,
197 jsonObjectEnd,
198 JSON_KEY_DMS_CONTINUETYPEID,
199 dmsAbilityInfo.continueTypeId,
200 JsonType::ARRAY,
201 false,
202 parseResult,
203 ArrayType::NUMBER);
204 GetValueIfFindKey<std::string>(jsonObject,
205 jsonObjectEnd,
206 JSON_KEY_DMS_MODULENAME,
207 dmsAbilityInfo.moduleName,
208 JsonType::STRING,
209 false,
210 parseResult,
211 ArrayType::NOT_ARRAY);
212 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
213 jsonObjectEnd,
214 JSON_KEY_DMS_CONTINUEBUNDLENAME,
215 dmsAbilityInfo.continueBundleName,
216 JsonType::ARRAY,
217 false,
218 parseResult,
219 ArrayType::STRING);
220 if (parseResult != ERR_OK) {
221 APP_LOGE("read module moduleInfo from jsonObject error, error code : %{public}d", parseResult);
222 }
223 }
224
ReadFromParcel(Parcel & parcel)225 bool DmsBundleInfo::ReadFromParcel(Parcel &parcel)
226 {
227 version = parcel.ReadUint32();
228 versionCode = parcel.ReadUint32();
229 compatibleVersionCode = parcel.ReadUint32();
230 minCompatibleVersion = parcel.ReadUint32();
231 targetVersionCode = parcel.ReadUint32();
232 bundleName = Str16ToStr8(parcel.ReadString16());
233 versionName = Str16ToStr8(parcel.ReadString16());
234 appId = Str16ToStr8(parcel.ReadString16());
235 enabled = parcel.ReadBool();
236 bundleNameId = parcel.ReadUint16();
237 updateTime = parcel.ReadInt64();
238 developerId = Str16ToStr8(parcel.ReadString16());
239 uint32_t abilityInfosSize;
240 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, abilityInfosSize);
241 CONTAINER_SECURITY_VERIFY(parcel, abilityInfosSize, &dmsAbilityInfos);
242 for (uint32_t i = 0; i < abilityInfosSize; i++) {
243 std::unique_ptr<DmsAbilityInfo> dmsAbilityInfo(parcel.ReadParcelable<DmsAbilityInfo>());
244 if (!dmsAbilityInfo) {
245 APP_LOGE("ReadParcelable<DmsAbilityInfo> failed");
246 return false;
247 }
248 dmsAbilityInfos.emplace_back(*dmsAbilityInfo);
249 }
250 uint32_t userIdArrSize;
251 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, userIdArrSize);
252 CONTAINER_SECURITY_VERIFY(parcel, userIdArrSize, &userIdArr);
253 for (uint32_t i = 0; i < userIdArrSize; i++) {
254 uint8_t userId = parcel.ReadUint8();
255 userIdArr.emplace_back(userId);
256 }
257 return true;
258 }
259
Marshalling(Parcel & parcel) const260 bool DmsBundleInfo::Marshalling(Parcel &parcel) const
261 {
262 parcel.WriteUint32(version);
263 parcel.WriteUint32(versionCode);
264 parcel.WriteUint32(compatibleVersionCode);
265 parcel.WriteUint32(minCompatibleVersion);
266 parcel.WriteUint32(targetVersionCode);
267 parcel.WriteString16(Str8ToStr16(bundleName));
268 parcel.WriteString16(Str8ToStr16(versionName));
269 parcel.WriteString16(Str8ToStr16(appId));
270 parcel.WriteBool(enabled);
271 parcel.WriteUint16(bundleNameId);
272 parcel.WriteInt64(updateTime);
273 for (auto &dmsAbilityInfo : dmsAbilityInfos) {
274 (parcel).WriteParcelable(&dmsAbilityInfo);
275 }
276 for (auto userId : userIdArr) {
277 (parcel).WriteUint8(userId);
278 }
279 return true;
280 }
281
Unmarshalling(Parcel & parcel)282 DmsBundleInfo *DmsBundleInfo::Unmarshalling(Parcel &parcel)
283 {
284 DmsBundleInfo *info = new (std::nothrow) DmsBundleInfo();
285 if (info != nullptr && !info->ReadFromParcel(parcel)) {
286 delete info;
287 info = nullptr;
288 }
289 return info;
290 }
291
ToString() const292 std::string DmsBundleInfo::ToString() const
293 {
294 nlohmann::json jsonObject;
295 jsonObject[JSON_KEY_VERSION] = version;
296 jsonObject[AppExecFwk::Constants::BUNDLE_NAME] = bundleName;
297 jsonObject[JSON_KEY_VERSION_CODE] = versionCode;
298 jsonObject[JSON_KEY_VERSION_NAME] = versionName;
299 jsonObject[JSON_KEY_COMPATIBLE_VERSION_CODE] = compatibleVersionCode;
300 jsonObject[JSON_KEY_MIN_COMPATIBLE_VERSION] = minCompatibleVersion;
301 jsonObject[JSON_KEY_TARGET_VERSION_CODE] = targetVersionCode;
302 jsonObject[JSON_KEY_APP_ID] = appId;
303 jsonObject[JSON_KEY_ENABLED] = enabled;
304 jsonObject[JSON_KEY_BUNDLE_NAME_ID] = bundleNameId;
305 jsonObject[JSON_KEY_UPDATE_TIME] = updateTime;
306 jsonObject[JSON_KEY_DEVELOPER_ID] = developerId;
307 jsonObject[JSON_KEY_DMS_ABILITY_INFOS] = dmsAbilityInfos;
308 jsonObject[JSON_KEY_DMS_USERID] = userIdArr;
309 return jsonObject.dump();
310 }
311
FromJsonString(const std::string & jsonString)312 bool DmsBundleInfo::FromJsonString(const std::string &jsonString)
313 {
314 nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
315 if (jsonObject.is_discarded()) {
316 return false;
317 }
318
319 const auto &jsonObjectEnd = jsonObject.end();
320 int32_t parseResult = ERR_OK;
321 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, JSON_KEY_VERSION, version,
322 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
323 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, AppExecFwk::Constants::BUNDLE_NAME, bundleName,
324 JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
325 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, JSON_KEY_VERSION_CODE, versionCode,
326 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
327 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, JSON_KEY_COMPATIBLE_VERSION_CODE, compatibleVersionCode,
328 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
329 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, JSON_KEY_VERSION_NAME, versionName,
330 JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
331 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, JSON_KEY_MIN_COMPATIBLE_VERSION, minCompatibleVersion,
332 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
333 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, JSON_KEY_TARGET_VERSION_CODE, targetVersionCode,
334 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
335 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, JSON_KEY_APP_ID, appId,
336 JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
337 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, JSON_KEY_ENABLED, enabled,
338 JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
339 GetValueIfFindKey<uint16_t>(jsonObject, jsonObjectEnd, JSON_KEY_BUNDLE_NAME_ID, bundleNameId,
340 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
341 GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, JSON_KEY_UPDATE_TIME, updateTime,
342 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
343 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, JSON_KEY_DEVELOPER_ID, developerId,
344 JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
345 GetValueIfFindKey<std::vector<DmsAbilityInfo>>(jsonObject, jsonObjectEnd, JSON_KEY_DMS_ABILITY_INFOS,
346 dmsAbilityInfos, JsonType::ARRAY, false, parseResult, ArrayType::OBJECT);
347 GetValueIfFindKey<std::vector<uint8_t>>(jsonObject, jsonObjectEnd, JSON_KEY_DMS_USERID, userIdArr,
348 JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
349 return parseResult == ERR_OK;
350 }
351 } // namespace DistributedSchedule
352 } // namespace OHOS