1 /*
2 * Copyright (c) 2021-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 "bundle_pack_info.h"
17
18 #include "bundle_constants.h"
19 #include "json_util.h"
20 #include "parcel_macro.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 // version
27 const char* PACK_SUMMARY_APP_VERSION_CODE = "code";
28 const char* PACK_SUMMARY_APP_VERSION_NAME = "name";
29 const char* PACK_SUMMARY_APP_VERSION_MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode";
30
31 // app
32 const char* PACK_SUMMARY_APP_BUNDLE_VERSION = "version";
33
34 // module ablities
35 const char* PACK_SUMMARY_MODULE_ABILITY_NAME = "name";
36 const char* PACK_SUMMARY_MODULE_ABILITY_LABEL = "label";
37 const char* PACK_SUMMARY_MODULE_ABILITY_VISIBLE = "visible";
38 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS = "forms";
39
40 // module extensionAbilities
41 const char* PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_NAME = "name";
42 const char* PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_FORMS = "forms";
43
44 // module ablities forms
45 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_NAME = "name";
46 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_TYPE = "type";
47 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_ENABLED = "updateEnabled";
48 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_SCHEDULE_DUPDATETIME = "scheduledUpdateTime";
49 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_DURATION = "updateDuration";
50 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_SUPPORT_DIMENSIONS = "supportDimensions";
51 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_DEFAULT_DIMENSION = "defaultDimension";
52
53 // module distro
54 const char* PACK_SUMMARY_MODULE_DISTRO_MODULE_TYPE = "moduleType";
55 const char* PACK_SUMMARY_MODULE_DISTRO_INSTALLATION_FREE = "installationFree";
56 const char* PACK_SUMMARY_MODULE_DISTRO_DELIVERY_WITH_INSTALL = "deliveryWithInstall";
57
58 // module apiversion
59 const char* PACK_SUMMARY_MODULE_API_VERSION_COMPATIBLE = "compatible";
60 const char* PACK_SUMMARY_MODULE_API_VERSION_RELEASE_TYPE = "releaseType";
61 const char* PACK_SUMMARY_MODULE_API_VERSION_TARGET = "target";
62
63 // package module
64 const char* PACK_SUMMARY_MODULE_MAIN_ABILITY = "mainAbility";
65 const char* PACK_SUMMARY_MODULE_DEVICE_TYPE = "deviceType";
66 const char* PACK_SUMMARY_MODULE_ABILITIES = "abilities";
67 const char* PACK_SUMMARY_MODULE_DISTRO = "distro";
68 const char* PACK_SUMMARY_MODULE_API_VERSION = "apiVersion";
69 const char* PACK_SUMMARY_MODULE_EXTENSION_ABILITIES = "extensionAbilities";
70
71 // summary
72 const char* PACK_SUMMARY_APP = "app";
73 const char* PACK_SUMMARY_MODULE = "modules";
74
75 // packages
76 const char* PACK_PACKAGES_DEVICE_TYPE = "deviceType";
77 const char* PACK_PACKAGES_MODULE_TYPE = "moduleType";
78 const char* PACK_PACKAGES_DELIVERY_WITH_INSTALL = "deliveryWithInstall";
79 const char* PACK_PACKAGES_NAME = "name";
80
81 // bundle pack info
82 const char* BUNDLE_PACK_INFO_SUMMARY = "summary";
83 const char* BUNDLE_PACK_INFO_PACKAGES = "packages";
84
85
86 } // namespace
87
to_json(nlohmann::json & jsonObject,const Version & version)88 void to_json(nlohmann::json &jsonObject, const Version &version)
89 {
90 jsonObject = nlohmann::json {
91 {PACK_SUMMARY_APP_VERSION_CODE, version.code},
92 {PACK_SUMMARY_APP_VERSION_NAME, version.name},
93 {PACK_SUMMARY_APP_VERSION_MIN_COMPATIBLE_VERSION_CODE, version.minCompatibleVersionCode}
94 };
95 }
96
from_json(const nlohmann::json & jsonObject,Version & version)97 void from_json(const nlohmann::json &jsonObject, Version &version)
98 {
99 const auto &jsonObjectEnd = jsonObject.end();
100 int32_t parseResult = ERR_OK;
101 GetValueIfFindKey<uint32_t>(jsonObject,
102 jsonObjectEnd,
103 PACK_SUMMARY_APP_VERSION_CODE,
104 version.code,
105 JsonType::NUMBER,
106 false,
107 parseResult,
108 ArrayType::NOT_ARRAY);
109 GetValueIfFindKey<std::string>(jsonObject,
110 jsonObjectEnd,
111 PACK_SUMMARY_APP_VERSION_NAME,
112 version.name,
113 JsonType::STRING,
114 false,
115 parseResult,
116 ArrayType::NOT_ARRAY);
117 GetValueIfFindKey<uint32_t>(jsonObject,
118 jsonObjectEnd,
119 PACK_SUMMARY_APP_VERSION_MIN_COMPATIBLE_VERSION_CODE,
120 version.minCompatibleVersionCode,
121 JsonType::NUMBER,
122 false,
123 parseResult,
124 ArrayType::NOT_ARRAY);
125 if (parseResult != ERR_OK) {
126 APP_LOGE("read version error %{public}d", parseResult);
127 }
128 }
129
to_json(nlohmann::json & jsonObject,const PackageApp & app)130 void to_json(nlohmann::json &jsonObject, const PackageApp &app)
131 {
132 jsonObject = nlohmann::json {
133 {Constants::BUNDLE_NAME, app.bundleName},
134 {PACK_SUMMARY_APP_BUNDLE_VERSION, app.version}
135 };
136 }
137
from_json(const nlohmann::json & jsonObject,PackageApp & app)138 void from_json(const nlohmann::json &jsonObject, PackageApp &app)
139 {
140 const auto &jsonObjectEnd = jsonObject.end();
141 int32_t parseResult = ERR_OK;
142 GetValueIfFindKey<std::string>(jsonObject,
143 jsonObjectEnd,
144 Constants::BUNDLE_NAME,
145 app.bundleName,
146 JsonType::STRING,
147 false,
148 parseResult,
149 ArrayType::NOT_ARRAY);
150 GetValueIfFindKey<Version>(jsonObject,
151 jsonObjectEnd,
152 PACK_SUMMARY_APP_BUNDLE_VERSION,
153 app.version,
154 JsonType::OBJECT,
155 false,
156 parseResult,
157 ArrayType::NOT_ARRAY);
158 if (parseResult != ERR_OK) {
159 APP_LOGE("read package app error %{public}d", parseResult);
160 }
161 }
162
to_json(nlohmann::json & jsonObject,const ExtensionAbilities & extensionAbilities)163 void to_json(nlohmann::json &jsonObject, const ExtensionAbilities &extensionAbilities)
164 {
165 jsonObject = nlohmann::json {
166 {PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_NAME, extensionAbilities.name},
167 {PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_FORMS, extensionAbilities.forms}
168 };
169 }
170
from_json(const nlohmann::json & jsonObject,ExtensionAbilities & extensionAbilities)171 void from_json(const nlohmann::json &jsonObject, ExtensionAbilities &extensionAbilities)
172 {
173 const auto &jsonObjectEnd = jsonObject.end();
174 int32_t parseResult = ERR_OK;
175 GetValueIfFindKey<std::string>(jsonObject,
176 jsonObjectEnd,
177 PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_NAME,
178 extensionAbilities.name,
179 JsonType::STRING,
180 false,
181 parseResult,
182 ArrayType::NOT_ARRAY);
183 GetValueIfFindKey<std::vector<AbilityFormInfo>>(jsonObject,
184 jsonObjectEnd,
185 PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_FORMS,
186 extensionAbilities.forms,
187 JsonType::ARRAY,
188 false,
189 parseResult,
190 ArrayType::OBJECT);
191 if (parseResult != ERR_OK) {
192 APP_LOGE("read abilityinfo error %{public}d", parseResult);
193 }
194 }
195
to_json(nlohmann::json & jsonObject,const ModuleAbilityInfo & abilityinfo)196 void to_json(nlohmann::json &jsonObject, const ModuleAbilityInfo &abilityinfo)
197 {
198 jsonObject = nlohmann::json {
199 {PACK_SUMMARY_MODULE_ABILITY_NAME, abilityinfo.name},
200 {PACK_SUMMARY_MODULE_ABILITY_LABEL, abilityinfo.label},
201 {PACK_SUMMARY_MODULE_ABILITY_VISIBLE, abilityinfo.visible},
202 {PACK_SUMMARY_MODULE_ABILITY_FORMS, abilityinfo.forms}
203 };
204 }
205
from_json(const nlohmann::json & jsonObject,ModuleAbilityInfo & abilityinfo)206 void from_json(const nlohmann::json &jsonObject, ModuleAbilityInfo &abilityinfo)
207 {
208 const auto &jsonObjectEnd = jsonObject.end();
209 int32_t parseResult = ERR_OK;
210 GetValueIfFindKey<std::string>(jsonObject,
211 jsonObjectEnd,
212 PACK_SUMMARY_MODULE_ABILITY_NAME,
213 abilityinfo.name,
214 JsonType::STRING,
215 false,
216 parseResult,
217 ArrayType::NOT_ARRAY);
218 GetValueIfFindKey<std::string>(jsonObject,
219 jsonObjectEnd,
220 PACK_SUMMARY_MODULE_ABILITY_LABEL,
221 abilityinfo.label,
222 JsonType::STRING,
223 false,
224 parseResult,
225 ArrayType::NOT_ARRAY);
226 GetValueIfFindKey<bool>(jsonObject,
227 jsonObjectEnd,
228 PACK_SUMMARY_MODULE_ABILITY_VISIBLE,
229 abilityinfo.visible,
230 JsonType::BOOLEAN,
231 false,
232 parseResult,
233 ArrayType::NOT_ARRAY);
234 GetValueIfFindKey<std::vector<AbilityFormInfo>>(jsonObject,
235 jsonObjectEnd,
236 PACK_SUMMARY_MODULE_ABILITY_FORMS,
237 abilityinfo.forms,
238 JsonType::ARRAY,
239 false,
240 parseResult,
241 ArrayType::OBJECT);
242 if (parseResult != ERR_OK) {
243 APP_LOGE("read abilityinfo error %{public}d", parseResult);
244 }
245 }
246
to_json(nlohmann::json & jsonObject,const AbilityFormInfo & abilityFormInfo)247 void to_json(nlohmann::json &jsonObject, const AbilityFormInfo &abilityFormInfo)
248 {
249 jsonObject = nlohmann::json {
250 {PACK_SUMMARY_MODULE_ABILITY_FORMS_NAME, abilityFormInfo.name},
251 {PACK_SUMMARY_MODULE_ABILITY_FORMS_TYPE, abilityFormInfo.type},
252 {PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_ENABLED, abilityFormInfo.updateEnabled},
253 {PACK_SUMMARY_MODULE_ABILITY_FORMS_SCHEDULE_DUPDATETIME, abilityFormInfo.scheduledUpdateTime},
254 {PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_DURATION, abilityFormInfo.updateDuration},
255 {PACK_SUMMARY_MODULE_ABILITY_FORMS_SUPPORT_DIMENSIONS, abilityFormInfo.supportDimensions},
256 {PACK_SUMMARY_MODULE_ABILITY_FORMS_DEFAULT_DIMENSION, abilityFormInfo.defaultDimension}};
257 }
258
from_json(const nlohmann::json & jsonObject,AbilityFormInfo & abilityFormInfo)259 void from_json(const nlohmann::json &jsonObject, AbilityFormInfo &abilityFormInfo)
260 {
261 const auto &jsonObjectEnd = jsonObject.end();
262 int32_t parseResult = ERR_OK;
263 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_NAME,
264 abilityFormInfo.name, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
265 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_TYPE,
266 abilityFormInfo.type, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
267 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_ENABLED,
268 abilityFormInfo.updateEnabled, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
269 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_SCHEDULE_DUPDATETIME,
270 abilityFormInfo.scheduledUpdateTime, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
271 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_DURATION,
272 abilityFormInfo.updateDuration, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
273 GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd,
274 PACK_SUMMARY_MODULE_ABILITY_FORMS_SUPPORT_DIMENSIONS, abilityFormInfo.supportDimensions, JsonType::ARRAY, false,
275 parseResult, ArrayType::STRING);
276 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_DEFAULT_DIMENSION,
277 abilityFormInfo.defaultDimension, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
278 if (parseResult != ERR_OK) {
279 APP_LOGE("read BundleConfigInfo error %{public}d", parseResult);
280 }
281 }
282
to_json(nlohmann::json & jsonObject,const ModuleDistro & distro)283 void to_json(nlohmann::json &jsonObject, const ModuleDistro &distro)
284 {
285 jsonObject = nlohmann::json {
286 {PACK_SUMMARY_MODULE_DISTRO_MODULE_TYPE, distro.moduleType},
287 {Constants::MODULE_NAME, distro.moduleName},
288 {PACK_SUMMARY_MODULE_DISTRO_INSTALLATION_FREE, distro.installationFree},
289 {PACK_SUMMARY_MODULE_DISTRO_DELIVERY_WITH_INSTALL, distro.deliveryWithInstall}
290 };
291 }
292
from_json(const nlohmann::json & jsonObject,ModuleDistro & distro)293 void from_json(const nlohmann::json &jsonObject, ModuleDistro &distro)
294 {
295 const auto &jsonObjectEnd = jsonObject.end();
296 int32_t parseResult = ERR_OK;
297 GetValueIfFindKey<std::string>(jsonObject,
298 jsonObjectEnd,
299 PACK_SUMMARY_MODULE_DISTRO_MODULE_TYPE,
300 distro.moduleType,
301 JsonType::STRING,
302 false,
303 parseResult,
304 ArrayType::NOT_ARRAY);
305 GetValueIfFindKey<std::string>(jsonObject,
306 jsonObjectEnd,
307 Constants::MODULE_NAME,
308 distro.moduleName,
309 JsonType::STRING,
310 false,
311 parseResult,
312 ArrayType::NOT_ARRAY);
313 GetValueIfFindKey<bool>(jsonObject,
314 jsonObjectEnd,
315 PACK_SUMMARY_MODULE_DISTRO_INSTALLATION_FREE,
316 distro.installationFree,
317 JsonType::BOOLEAN,
318 false,
319 parseResult,
320 ArrayType::NOT_ARRAY);
321 GetValueIfFindKey<bool>(jsonObject,
322 jsonObjectEnd,
323 PACK_SUMMARY_MODULE_DISTRO_DELIVERY_WITH_INSTALL,
324 distro.deliveryWithInstall,
325 JsonType::BOOLEAN,
326 false,
327 parseResult,
328 ArrayType::NOT_ARRAY);
329 if (parseResult != ERR_OK) {
330 APP_LOGE("read abilityinfo error %{public}d", parseResult);
331 }
332 }
333
to_json(nlohmann::json & jsonObject,const ApiVersion & apiVersion)334 void to_json(nlohmann::json &jsonObject, const ApiVersion &apiVersion)
335 {
336 jsonObject = nlohmann::json {
337 {PACK_SUMMARY_MODULE_API_VERSION_COMPATIBLE, apiVersion.compatible},
338 {PACK_SUMMARY_MODULE_API_VERSION_RELEASE_TYPE, apiVersion.releaseType},
339 {PACK_SUMMARY_MODULE_API_VERSION_TARGET, apiVersion.target}
340 };
341 }
342
from_json(const nlohmann::json & jsonObject,ApiVersion & apiVersion)343 void from_json(const nlohmann::json &jsonObject, ApiVersion &apiVersion)
344 {
345 const auto &jsonObjectEnd = jsonObject.end();
346 int32_t parseResult = ERR_OK;
347 GetValueIfFindKey<uint32_t>(jsonObject,
348 jsonObjectEnd,
349 PACK_SUMMARY_MODULE_API_VERSION_COMPATIBLE,
350 apiVersion.compatible,
351 JsonType::NUMBER,
352 false,
353 parseResult,
354 ArrayType::NOT_ARRAY);
355 GetValueIfFindKey<std::string>(jsonObject,
356 jsonObjectEnd,
357 PACK_SUMMARY_MODULE_API_VERSION_RELEASE_TYPE,
358 apiVersion.releaseType,
359 JsonType::STRING,
360 false,
361 parseResult,
362 ArrayType::NOT_ARRAY);
363 GetValueIfFindKey<uint32_t>(jsonObject,
364 jsonObjectEnd,
365 PACK_SUMMARY_MODULE_API_VERSION_TARGET,
366 apiVersion.target,
367 JsonType::NUMBER,
368 false,
369 parseResult,
370 ArrayType::NOT_ARRAY);
371 if (parseResult != ERR_OK) {
372 APP_LOGE("read abilityinfo error %{public}d", parseResult);
373 }
374 }
375
to_json(nlohmann::json & jsonObject,const PackageModule & packageModule)376 void to_json(nlohmann::json &jsonObject, const PackageModule &packageModule)
377 {
378 jsonObject = nlohmann::json {
379 {PACK_SUMMARY_MODULE_MAIN_ABILITY, packageModule.mainAbility},
380 {PACK_SUMMARY_MODULE_DEVICE_TYPE, packageModule.deviceType},
381 {PACK_SUMMARY_MODULE_ABILITIES, packageModule.abilities},
382 {PACK_SUMMARY_MODULE_EXTENSION_ABILITIES, packageModule.extensionAbilities},
383 {PACK_SUMMARY_MODULE_DISTRO, packageModule.distro},
384 {PACK_SUMMARY_MODULE_API_VERSION, packageModule.apiVersion}
385 };
386 }
387
from_json(const nlohmann::json & jsonObject,PackageModule & packageModule)388 void from_json(const nlohmann::json &jsonObject, PackageModule &packageModule)
389 {
390 const auto &jsonObjectEnd = jsonObject.end();
391 int32_t parseResult = ERR_OK;
392 GetValueIfFindKey<std::string>(jsonObject,
393 jsonObjectEnd,
394 PACK_SUMMARY_MODULE_MAIN_ABILITY,
395 packageModule.mainAbility,
396 JsonType::STRING,
397 false,
398 parseResult,
399 ArrayType::NOT_ARRAY);
400 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
401 jsonObjectEnd,
402 PACK_SUMMARY_MODULE_DEVICE_TYPE,
403 packageModule.deviceType,
404 JsonType::ARRAY,
405 false,
406 parseResult,
407 ArrayType::STRING);
408 GetValueIfFindKey<std::vector<ModuleAbilityInfo>>(jsonObject,
409 jsonObjectEnd,
410 PACK_SUMMARY_MODULE_ABILITIES,
411 packageModule.abilities,
412 JsonType::ARRAY,
413 false,
414 parseResult,
415 ArrayType::OBJECT);
416 GetValueIfFindKey<std::vector<ExtensionAbilities>>(jsonObject,
417 jsonObjectEnd,
418 PACK_SUMMARY_MODULE_EXTENSION_ABILITIES,
419 packageModule.extensionAbilities,
420 JsonType::ARRAY,
421 false,
422 parseResult,
423 ArrayType::OBJECT);
424 GetValueIfFindKey<ModuleDistro>(jsonObject,
425 jsonObjectEnd,
426 PACK_SUMMARY_MODULE_DISTRO,
427 packageModule.distro,
428 JsonType::OBJECT,
429 false,
430 parseResult,
431 ArrayType::NOT_ARRAY);
432 GetValueIfFindKey<ApiVersion>(jsonObject,
433 jsonObjectEnd,
434 PACK_SUMMARY_MODULE_API_VERSION,
435 packageModule.apiVersion,
436 JsonType::OBJECT,
437 false,
438 parseResult,
439 ArrayType::NOT_ARRAY);
440 if (parseResult != ERR_OK) {
441 APP_LOGE("read abilityinfo error %{public}d", parseResult);
442 }
443 }
444
to_json(nlohmann::json & jsonObject,const Summary & summary)445 void to_json(nlohmann::json &jsonObject, const Summary &summary)
446 {
447 jsonObject = nlohmann::json {
448 {PACK_SUMMARY_APP, summary.app},
449 {PACK_SUMMARY_MODULE, summary.modules}
450 };
451 }
452
from_json(const nlohmann::json & jsonObject,Summary & summary)453 void from_json(const nlohmann::json &jsonObject, Summary &summary)
454 {
455 const auto &jsonObjectEnd = jsonObject.end();
456 int32_t parseResult = ERR_OK;
457 GetValueIfFindKey<PackageApp>(jsonObject,
458 jsonObjectEnd,
459 PACK_SUMMARY_APP,
460 summary.app,
461 JsonType::OBJECT,
462 false,
463 parseResult,
464 ArrayType::NOT_ARRAY);
465 GetValueIfFindKey<std::vector<PackageModule>>(jsonObject,
466 jsonObjectEnd,
467 PACK_SUMMARY_MODULE,
468 summary.modules,
469 JsonType::ARRAY,
470 false,
471 parseResult,
472 ArrayType::OBJECT);
473 if (parseResult != ERR_OK) {
474 APP_LOGE("read abilityinfo error %{public}d", parseResult);
475 }
476 }
477
to_json(nlohmann::json & jsonObject,const Packages & packages)478 void to_json(nlohmann::json &jsonObject, const Packages &packages)
479 {
480 jsonObject = nlohmann::json {
481 {PACK_PACKAGES_DEVICE_TYPE, packages.deviceType},
482 {PACK_PACKAGES_MODULE_TYPE, packages.moduleType},
483 {PACK_PACKAGES_DELIVERY_WITH_INSTALL, packages.deliveryWithInstall},
484 {PACK_PACKAGES_NAME, packages.name}
485 };
486 }
487
from_json(const nlohmann::json & jsonObject,Packages & packages)488 void from_json(const nlohmann::json &jsonObject, Packages &packages)
489 {
490 const auto &jsonObjectEnd = jsonObject.end();
491 int32_t parseResult = ERR_OK;
492 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
493 jsonObjectEnd,
494 PACK_PACKAGES_DEVICE_TYPE,
495 packages.deviceType,
496 JsonType::ARRAY,
497 false,
498 parseResult,
499 ArrayType::STRING);
500 GetValueIfFindKey<std::string>(jsonObject,
501 jsonObjectEnd,
502 PACK_PACKAGES_MODULE_TYPE,
503 packages.moduleType,
504 JsonType::STRING,
505 false,
506 parseResult,
507 ArrayType::NOT_ARRAY);
508 GetValueIfFindKey<bool>(jsonObject,
509 jsonObjectEnd,
510 PACK_PACKAGES_DELIVERY_WITH_INSTALL,
511 packages.deliveryWithInstall,
512 JsonType::BOOLEAN,
513 false,
514 parseResult,
515 ArrayType::NOT_ARRAY);
516 GetValueIfFindKey<std::string>(jsonObject,
517 jsonObjectEnd,
518 PACK_PACKAGES_NAME,
519 packages.name,
520 JsonType::STRING,
521 false,
522 parseResult,
523 ArrayType::NOT_ARRAY);
524 if (parseResult != ERR_OK) {
525 APP_LOGE("read abilityinfo error %{public}d", parseResult);
526 }
527 }
528
to_json(nlohmann::json & jsonObject,const BundlePackInfo & bundlePackInfo)529 void to_json(nlohmann::json &jsonObject, const BundlePackInfo &bundlePackInfo)
530 {
531 jsonObject = nlohmann::json {
532 {BUNDLE_PACK_INFO_SUMMARY, bundlePackInfo.summary},
533 {BUNDLE_PACK_INFO_PACKAGES, bundlePackInfo.packages}
534 };
535 }
536
from_json(const nlohmann::json & jsonObject,BundlePackInfo & bundlePackInfo)537 void from_json(const nlohmann::json &jsonObject, BundlePackInfo &bundlePackInfo)
538 {
539 const auto &jsonObjectEnd = jsonObject.end();
540 int32_t parseResult = ERR_OK;
541 GetValueIfFindKey<Summary>(jsonObject,
542 jsonObjectEnd,
543 BUNDLE_PACK_INFO_SUMMARY,
544 bundlePackInfo.summary,
545 JsonType::OBJECT,
546 false,
547 parseResult,
548 ArrayType::NOT_ARRAY);
549 GetValueIfFindKey<std::vector<Packages>>(jsonObject,
550 jsonObjectEnd,
551 BUNDLE_PACK_INFO_PACKAGES,
552 bundlePackInfo.packages,
553 JsonType::ARRAY,
554 false,
555 parseResult,
556 ArrayType::OBJECT);
557 if (parseResult != ERR_OK) {
558 APP_LOGE("read abilityinfo error %{public}d", parseResult);
559 }
560 }
561
ReadFromParcel(Parcel & parcel)562 bool BundlePackInfo::ReadFromParcel(Parcel &parcel)
563 {
564 MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
565 if (!messageParcel) {
566 APP_LOGE("Type conversion failed");
567 return false;
568 }
569 uint32_t length = messageParcel->ReadUint32();
570 if (length == 0) {
571 APP_LOGE("Invalid data length");
572 return false;
573 }
574 const char *data = reinterpret_cast<const char *>(messageParcel->ReadRawData(length));
575 if (!data) {
576 APP_LOGE("Fail read raw length = %{public}d", length);
577 return false;
578 }
579 nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false);
580 if (jsonObject.is_discarded()) {
581 APP_LOGE("failed to parse BundleInfo");
582 return false;
583 }
584 *this = jsonObject.get<BundlePackInfo>();
585 return true;
586 }
587
Marshalling(Parcel & parcel) const588 bool BundlePackInfo::Marshalling(Parcel &parcel) const
589 {
590 MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
591 if (!messageParcel) {
592 APP_LOGE("Type conversion failed");
593 return false;
594 }
595 nlohmann::json jsonObject = *this;
596 std::string str = jsonObject.dump();
597 if (!messageParcel->WriteUint32(str.size() + 1)) {
598 APP_LOGE("Failed to write;data size");
599 return false;
600 }
601 if (!messageParcel->WriteRawData(str.c_str(), str.size() + 1)) {
602 APP_LOGE("Failed to write data");
603 return false;
604 }
605 return true;
606 }
607
Unmarshalling(Parcel & parcel)608 BundlePackInfo *BundlePackInfo::Unmarshalling(Parcel &parcel)
609 {
610 BundlePackInfo *info = new (std::nothrow) BundlePackInfo();
611 if (info && !info->ReadFromParcel(parcel)) {
612 APP_LOGW("read from parcel failed");
613 delete info;
614 info = nullptr;
615 }
616 return info;
617 }
618 } // AppExecFwk
619 } // OHOS
620