• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "pack_info.h"
17 
18 #include <algorithm>
19 #include <fstream>
20 
21 #include "log.h"
22 #include "utils.h"
23 
24 namespace OHOS {
25 namespace AppPackingTool {
26 namespace {
27 const std::string SUMMARY = "summary";
28 const std::string PACKAGES = "packages";
29 const std::string APP = "app";
30 const std::string MODULES = "modules";
31 const std::string BUNDLE_NAME = "bundleName";
32 const std::string BUNDLE_TYPE = "bundleType";
33 const std::string VERSION = "version";
34 const std::string CODE = "code";
35 const std::string NAME = "name";
36 const std::string DISTRO = "distro";
37 const std::string MODULE_NAME = "moduleName";
38 const std::string EXTENSION_ABILITIES = "extensionAbilities";
39 const std::string FORMS = "forms";
40 const std::string DEFAULT_DIMENSION = "defaultDimension";
41 const std::string SUPPORT_DIMENSIONS = "supportDimensions";
42 const char ASTERISK = '*';
43 const std::string MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode";
44 const std::string MIN_API_VERSION = "minAPIVersion";
45 const std::string TARGET_API_VERSION = "targetAPIVersion";
46 const std::string API_RELEASE_TYPE = "apiReleaseType";
47 const std::string INSTALLATION_FREE = "installationFree";
48 const std::string DELIVERY_WITH_INSTALL = "deliveryWithInstall";
49 const std::string COMPILE_SDK_TYPE = "compileSdkType";
50 const std::string DEVICE_TYPE = "deviceType";
51 const std::string COMPATIBLE = "compatible";
52 const std::string RELEASE_TYPE = "releaseType";
53 const std::string TARGET = "target";
54 const std::string API_VERSION = "apiVersion";
55 }
56 
ParseFromString(const std::string & jsonString)57 bool PackInfo::ParseFromString(const std::string& jsonString)
58 {
59     Release();
60     if (jsonString.length() == 0) {
61         LOGE("Json length is zero!");
62         return false;
63     }
64     root_ = PtJson::Parse(jsonString);
65     return IsValid();
66 }
67 
ParseFromFile(const std::string & jsonFile)68 bool PackInfo::ParseFromFile(const std::string& jsonFile)
69 {
70     Release();
71     std::string realJsonFile;
72     if (!Utils::GetRealPath(jsonFile, realJsonFile)) {
73         LOGE("get real json file failed! jsonFile=%s", jsonFile.c_str());
74         return false;
75     }
76     std::ifstream inFile(realJsonFile, std::ios::in);
77     if (!inFile.is_open()) {
78         LOGE("Open json file failed! jsonFile=%s, realJsonFile=%s", jsonFile.c_str(), realJsonFile.c_str());
79         return false;
80     }
81     std::string fileContent((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
82     inFile.close();
83     root_ = PtJson::Parse(fileContent);
84     return IsValid();
85 }
86 
ToString()87 std::string PackInfo::ToString()
88 {
89     return root_->Stringify();
90 }
91 
Release()92 void PackInfo::Release()
93 {
94     if (root_) {
95         root_->ReleaseRoot();
96         root_ = nullptr;
97     }
98 }
99 
IsValid()100 bool PackInfo::IsValid()
101 {
102     return (root_.get() != nullptr);
103 }
104 
GetSummaryObject(std::unique_ptr<PtJson> & summaryObj)105 bool PackInfo::GetSummaryObject(std::unique_ptr<PtJson>& summaryObj)
106 {
107     if (root_.get() == nullptr) {
108         LOGE("Json root is null!");
109         return false;
110     }
111     if (!root_->Contains(SUMMARY.c_str())) {
112         LOGE("Json root has no %s node!", SUMMARY.c_str());
113         return false;
114     }
115     if (root_->GetObject(SUMMARY.c_str(), &summaryObj) != Result::SUCCESS) {
116         LOGE("Json root get %s node failed!", SUMMARY.c_str());
117         return false;
118     }
119     return true;
120 }
121 
GetPackagesObject(std::unique_ptr<PtJson> & packagesObj)122 bool PackInfo::GetPackagesObject(std::unique_ptr<PtJson>& packagesObj)
123 {
124     if (root_.get() == nullptr) {
125         LOGE("Json root is null!");
126         return false;
127     }
128     if (!root_->Contains(PACKAGES.c_str())) {
129         LOGE("Json root has no %s node!", PACKAGES.c_str());
130         return false;
131     }
132     if (root_->GetArray(PACKAGES.c_str(), &packagesObj) != Result::SUCCESS) {
133         LOGE("Json root get %s array node failed!", PACKAGES.c_str());
134         return false;
135     }
136     return true;
137 }
138 
GetAppObject(std::unique_ptr<PtJson> & appObj)139 bool PackInfo::GetAppObject(std::unique_ptr<PtJson>& appObj)
140 {
141     std::unique_ptr<PtJson> summaryObj;
142     if (!GetSummaryObject(summaryObj)) {
143         LOGE("GetSummaryObject failed!");
144         return false;
145     }
146     if (!summaryObj->Contains(APP.c_str())) {
147         LOGE("Summary node has no %s node!", APP.c_str());
148         return false;
149     }
150     if (summaryObj->GetObject(APP.c_str(), &appObj) != Result::SUCCESS) {
151         LOGE("Summary node get %s node failed!", APP.c_str());
152         return false;
153     }
154     return true;
155 }
156 
GetModulesObject(std::unique_ptr<PtJson> & modulesObj)157 bool PackInfo::GetModulesObject(std::unique_ptr<PtJson>& modulesObj)
158 {
159     std::unique_ptr<PtJson> summaryObj;
160     if (!GetSummaryObject(summaryObj)) {
161         LOGE("GetSummaryObject failed!");
162         return false;
163     }
164     if (!summaryObj->Contains(MODULES.c_str())) {
165         LOGE("Summary node has no %s node!", MODULES.c_str());
166         return false;
167     }
168     if (summaryObj->GetArray(MODULES.c_str(), &modulesObj) != Result::SUCCESS) {
169         LOGE("Summary node get %s node failed!", MODULES.c_str());
170         return false;
171     }
172     return true;
173 }
174 
GetBundleName(std::string & bundleName)175 bool PackInfo::GetBundleName(std::string& bundleName)
176 {
177     std::unique_ptr<PtJson> appObj;
178     if (!GetAppObject(appObj)) {
179         LOGE("GetAppObject failed!");
180         return false;
181     }
182     return GetBundleNameByAppObj(appObj, bundleName);
183 }
184 
GetBundleNameByAppObj(const std::unique_ptr<PtJson> & appObj,std::string & bundleName)185 bool PackInfo::GetBundleNameByAppObj(const std::unique_ptr<PtJson>& appObj, std::string& bundleName)
186 {
187     if (!appObj) {
188         LOGE("App node is null!");
189         return false;
190     }
191     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
192         LOGE("App node has no %s node!", BUNDLE_NAME.c_str());
193         return false;
194     }
195     if (appObj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) {
196         LOGE("App node get %s failed!", BUNDLE_NAME.c_str());
197         return false;
198     }
199     return true;
200 }
201 
SetBundleName(const std::string & bundleName)202 bool PackInfo::SetBundleName(const std::string& bundleName)
203 {
204     std::unique_ptr<PtJson> appObj;
205     if (!GetAppObject(appObj)) {
206         LOGE("GetAppObject failed!");
207         return false;
208     }
209     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
210         LOGE("App node has no %s node!", BUNDLE_NAME.c_str());
211         return false;
212     }
213     if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) {
214         LOGE("App node set %s failed!", BUNDLE_NAME.c_str());
215         return false;
216     }
217     return true;
218 }
219 
GetBundleType(std::string & bundleType,const std::string & defaultBundleType)220 bool PackInfo::GetBundleType(std::string& bundleType, const std::string& defaultBundleType)
221 {
222     std::unique_ptr<PtJson> appObj;
223     if (!GetAppObject(appObj)) {
224         LOGE("GetAppObject failed!");
225         return false;
226     }
227     return GetBundleTypeByAppObj(appObj, bundleType, defaultBundleType);
228 }
229 
GetBundleTypeByAppObj(const std::unique_ptr<PtJson> & appObj,std::string & bundleType,const std::string & defaultBundleType)230 bool PackInfo::GetBundleTypeByAppObj(const std::unique_ptr<PtJson>& appObj, std::string& bundleType,
231     const std::string& defaultBundleType)
232 {
233     if (!appObj) {
234         LOGE("App node is null!");
235         return false;
236     }
237     if (appObj->Contains(BUNDLE_TYPE.c_str())) {
238         if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) != Result::SUCCESS) {
239             LOGE("App node get %s failed!", BUNDLE_TYPE.c_str());
240             return false;
241         }
242     } else {
243         bundleType = defaultBundleType;
244     }
245     return true;
246 }
247 
GetVersionObject(std::unique_ptr<PtJson> & versionObj)248 bool PackInfo::GetVersionObject(std::unique_ptr<PtJson>& versionObj)
249 {
250     std::unique_ptr<PtJson> appObj;
251     if (!GetAppObject(appObj)) {
252         LOGE("GetAppObject failed!");
253         return false;
254     }
255     if (!appObj->Contains(VERSION.c_str())) {
256         LOGE("App node has no %s node!", VERSION.c_str());
257         return false;
258     }
259     if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) {
260         LOGE("App node get %s node failed!", VERSION.c_str());
261         return false;
262     }
263     return true;
264 }
265 
GetDistroObject(int32_t moduleIndex,std::unique_ptr<PtJson> & distroObj)266 bool PackInfo::GetDistroObject(int32_t moduleIndex, std::unique_ptr<PtJson>& distroObj)
267 {
268     std::unique_ptr<PtJson> modulesObj;
269     if (!GetModulesObject(modulesObj)) {
270         LOGE("GetModulesObject failed!");
271         return false;
272     }
273     return GetDistroObjectByModulesObj(modulesObj, moduleIndex, distroObj);
274 }
275 
GetDistroObjectByModulesObj(const std::unique_ptr<PtJson> & modulesObj,int32_t moduleIndex,std::unique_ptr<PtJson> & distroObj)276 bool PackInfo::GetDistroObjectByModulesObj(const std::unique_ptr<PtJson>& modulesObj,
277     int32_t moduleIndex, std::unique_ptr<PtJson>& distroObj)
278 {
279     if (!modulesObj || !modulesObj->IsArray()) {
280         LOGE("Module node is null or is not array!");
281         return false;
282     }
283     if (moduleIndex >= modulesObj->GetSize()) {
284         LOGE("Module node index error![moduleIndex=%d][modulesObjSize=%d]", moduleIndex, modulesObj->GetSize());
285         return false;
286     }
287     std::unique_ptr<PtJson> moduleObj = modulesObj->Get(moduleIndex);
288     return GetDistroObjectByModuleObj(moduleObj, distroObj);
289 }
290 
GetDistroObjectByModuleObj(const std::unique_ptr<PtJson> & moduleObj,std::unique_ptr<PtJson> & distroObj)291 bool PackInfo::GetDistroObjectByModuleObj(const std::unique_ptr<PtJson>& moduleObj,
292     std::unique_ptr<PtJson>& distroObj)
293 {
294     if (!moduleObj) {
295         LOGE("Module node is null!");
296         return false;
297     }
298     if (!moduleObj->Contains(DISTRO.c_str())) {
299         LOGE("Module node has no %s node!", DISTRO.c_str());
300         return false;
301     }
302     if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
303         LOGE("Module node get %s node failed!", DISTRO.c_str());
304         return false;
305     }
306     return true;
307 }
308 
GetApiVersionObjectByModuleObj(const std::unique_ptr<PtJson> & moduleObj,std::unique_ptr<PtJson> & apiVersionObj)309 bool PackInfo::GetApiVersionObjectByModuleObj(const std::unique_ptr<PtJson>& moduleObj,
310     std::unique_ptr<PtJson>& apiVersionObj)
311 {
312     if (!moduleObj) {
313         LOGE("Module node is null!");
314         return false;
315     }
316     if (!moduleObj->Contains(API_VERSION.c_str())) {
317         cJSON *apiVersion = cJSON_CreateObject();
318         apiVersionObj = std::make_unique<PtJson>(apiVersion);
319         if (!moduleObj->Add(API_VERSION.c_str(), apiVersionObj)) {
320             LOGE("Module node add %s node failed!", API_VERSION.c_str());
321             return false;
322         }
323         return true;
324     }
325     if (moduleObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
326         LOGE("Module node get %s node failed!", API_VERSION.c_str());
327         return false;
328     }
329     return true;
330 }
331 
GetExtensionAbilitiesObj(int32_t moduleIndex,std::unique_ptr<PtJson> & extensionAbilitiesObj)332 bool PackInfo::GetExtensionAbilitiesObj(int32_t moduleIndex, std::unique_ptr<PtJson>& extensionAbilitiesObj)
333 {
334     std::unique_ptr<PtJson> modulesObj;
335     if (!GetModulesObject(modulesObj)) {
336         LOGE("GetModulesObject failed!");
337         return false;
338     }
339     return GetDistroObjectByModulesObj(modulesObj, moduleIndex, extensionAbilitiesObj);
340 }
341 
GetExtensionAbilitiesObjByModulesObj(const std::unique_ptr<PtJson> & modulesObj,int32_t moduleIndex,std::unique_ptr<PtJson> & extensionAbilitiesObj)342 bool PackInfo::GetExtensionAbilitiesObjByModulesObj(const std::unique_ptr<PtJson>& modulesObj,
343     int32_t moduleIndex, std::unique_ptr<PtJson>& extensionAbilitiesObj)
344 {
345     if (!modulesObj || !modulesObj->IsArray()) {
346         LOGE("Module node is null or is not array!");
347         return false;
348     }
349     if (moduleIndex >= modulesObj->GetSize()) {
350         LOGE("Module node index error![moduleIndex=%d][modulesObjSize=%d]", moduleIndex, modulesObj->GetSize());
351         return false;
352     }
353     std::unique_ptr<PtJson> moduleObj = modulesObj->Get(moduleIndex);
354     return GetExtensionAbilitiesObjByModuleObj(moduleObj, extensionAbilitiesObj);
355 }
356 
GetExtensionAbilitiesObjByModuleObj(const std::unique_ptr<PtJson> & moduleObj,std::unique_ptr<PtJson> & extensionAbilitiesObj)357 bool PackInfo::GetExtensionAbilitiesObjByModuleObj(const std::unique_ptr<PtJson>& moduleObj,
358     std::unique_ptr<PtJson>& extensionAbilitiesObj)
359 {
360     if (!moduleObj) {
361         LOGE("Module node is null!");
362         return false;
363     }
364     if (!moduleObj->Contains(EXTENSION_ABILITIES.c_str())) {
365         LOGE("Module node has no %s node!", EXTENSION_ABILITIES.c_str());
366         return false;
367     }
368     if (moduleObj->GetArray(EXTENSION_ABILITIES.c_str(), &extensionAbilitiesObj) != Result::SUCCESS) {
369         LOGE("Module node get %s array node failed!", EXTENSION_ABILITIES.c_str());
370         return false;
371     }
372     return true;
373 }
374 
GetFormsObjByExtensionAbilityObj(const std::unique_ptr<PtJson> & extensionAbilityObj,std::unique_ptr<PtJson> & formsObj)375 bool PackInfo::GetFormsObjByExtensionAbilityObj(const std::unique_ptr<PtJson>& extensionAbilityObj,
376     std::unique_ptr<PtJson>& formsObj)
377 {
378     if (!extensionAbilityObj) {
379         LOGE("ExtensionAbility node is null!");
380         return false;
381     }
382     if (!extensionAbilityObj->Contains(FORMS.c_str())) {
383         LOGE("ExtensionAbility node has no %s node!", FORMS.c_str());
384         return false;
385     }
386     if (extensionAbilityObj->GetArray(FORMS.c_str(), &formsObj) != Result::SUCCESS) {
387         LOGE("ExtensionAbility node get %s array node failed!", FORMS.c_str());
388         return false;
389     }
390     return true;
391 }
392 
GetPackageObject(int32_t packageIndex,std::unique_ptr<PtJson> & packageObj)393 bool PackInfo::GetPackageObject(int32_t packageIndex, std::unique_ptr<PtJson>& packageObj)
394 {
395     std::unique_ptr<PtJson> packagesObj;
396     if (!GetPackagesObject(packagesObj)) {
397         LOGE("GetPackagesObject failed!");
398         return false;
399     }
400     if (!packagesObj->IsArray()) {
401         LOGE("Packages node is not array!");
402         return false;
403     }
404     if (packageIndex >= packagesObj->GetSize()) {
405         LOGE("Packages node index error![packageIndex=%d][packagesObjSize=%d]", packageIndex, packagesObj->GetSize());
406         return false;
407     }
408     packageObj = packagesObj->Get(packageIndex);
409     return true;
410 }
411 
GetVersion(PackInfoVersion & version)412 bool PackInfo::GetVersion(PackInfoVersion& version)
413 {
414     std::unique_ptr<PtJson> versionObj;
415     if (!GetVersionObject(versionObj)) {
416         LOGE("GetVersionObject failed!");
417         return false;
418     }
419     return GetVersionByVersionObj(versionObj, version);
420 }
421 
GetVersionByVersionObj(const std::unique_ptr<PtJson> & versionObj,PackInfoVersion & version)422 bool PackInfo::GetVersionByVersionObj(const std::unique_ptr<PtJson>& versionObj, PackInfoVersion& version)
423 {
424     if (!versionObj) {
425         LOGE("Version node is null!");
426         return false;
427     }
428     if (!versionObj->Contains(CODE.c_str()) || !versionObj->Contains(NAME.c_str())) {
429         LOGE("Version node has no %s node or %s node!", CODE.c_str(), NAME.c_str());
430         return false;
431     }
432     if (versionObj->GetInt(CODE.c_str(), &version.code) != Result::SUCCESS) {
433         LOGE("Version node get %s failed!", CODE.c_str());
434         return false;
435     }
436     if (versionObj->GetString(NAME.c_str(), &version.name) != Result::SUCCESS) {
437         LOGE("Version node get %s failed!", NAME.c_str());
438         return false;
439     }
440     return true;
441 }
442 
SetVersionCode(const int32_t & versionCode)443 bool PackInfo::SetVersionCode(const int32_t& versionCode)
444 {
445     std::unique_ptr<PtJson> versionObj;
446     if (!GetVersionObject(versionObj)) {
447         LOGE("GetVersionObject failed!");
448         return false;
449     }
450     if (!versionObj->Contains(CODE.c_str())) {
451         LOGE("Version node has no %s node!", CODE.c_str());
452         return false;
453     }
454     if (versionObj->SetInt(CODE.c_str(), versionCode) != Result::SUCCESS) {
455         LOGE("Version node set %s failed!", CODE.c_str());
456         return false;
457     }
458     return true;
459 }
460 
SetVersionName(const std::string & versionName)461 bool PackInfo::SetVersionName(const std::string& versionName)
462 {
463     std::unique_ptr<PtJson> versionObj;
464     if (!GetVersionObject(versionObj)) {
465         LOGE("GetVersionObject failed!");
466         return false;
467     }
468     if (!versionObj->Contains(NAME.c_str())) {
469         LOGE("Version node has no %s node!", NAME.c_str());
470         return false;
471     }
472     if (versionObj->SetString(NAME.c_str(), versionName) != Result::SUCCESS) {
473         LOGE("Version node set %s failed!", NAME.c_str());
474         return false;
475     }
476     return true;
477 }
478 
GetModuleNameByDistroObj(const std::unique_ptr<PtJson> & distroObj,std::string & moduleName)479 bool PackInfo::GetModuleNameByDistroObj(const std::unique_ptr<PtJson>& distroObj, std::string& moduleName)
480 {
481     if (!distroObj) {
482         LOGE("Distro node is null!");
483         return false;
484     }
485     if (distroObj->Contains(MODULE_NAME.c_str())) {
486         if (distroObj->GetString(MODULE_NAME.c_str(), &moduleName) != Result::SUCCESS) {
487             LOGE("Distro node get %s failed!", MODULE_NAME.c_str());
488             return false;
489         }
490     } else {
491         moduleName = "";
492     }
493     return true;
494 }
495 
GetNameByPackageObj(const std::unique_ptr<PtJson> & packageObj,std::string & name)496 bool PackInfo::GetNameByPackageObj(const std::unique_ptr<PtJson>& packageObj, std::string& name)
497 {
498     if (!packageObj) {
499         LOGE("Package node is null!");
500         return false;
501     }
502     if (packageObj->Contains(NAME.c_str())) {
503         if (packageObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) {
504             LOGE("Package node get %s failed!", NAME.c_str());
505             return false;
506         }
507     } else {
508         name = "";
509     }
510     return true;
511 }
512 
GetNameByFormObj(const std::unique_ptr<PtJson> & formObj,std::string & name)513 bool PackInfo::GetNameByFormObj(const std::unique_ptr<PtJson>& formObj, std::string& name)
514 {
515     if (!formObj) {
516         LOGE("Form node is null!");
517         return false;
518     }
519     if (formObj->Contains(NAME.c_str())) {
520         if (formObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) {
521             LOGE("Form node get %s failed!", NAME.c_str());
522             return false;
523         }
524     } else {
525         name = "";
526     }
527     return true;
528 }
529 
GetDefaultDimensionByFormObj(const std::unique_ptr<PtJson> & formObj,std::string & defaultDimension)530 bool PackInfo::GetDefaultDimensionByFormObj(const std::unique_ptr<PtJson>& formObj, std::string& defaultDimension)
531 {
532     if (!formObj) {
533         LOGE("Form node is null!");
534         return false;
535     }
536     if (!formObj->Contains(DEFAULT_DIMENSION.c_str())) {
537         LOGE("Form node has no %s node!", DEFAULT_DIMENSION.c_str());
538         return false;
539     }
540     if (formObj->GetString(DEFAULT_DIMENSION.c_str(), &defaultDimension) != Result::SUCCESS) {
541         LOGE("Form node get %s failed!", DEFAULT_DIMENSION.c_str());
542         return false;
543     }
544     if (std::count(defaultDimension.begin(), defaultDimension.end(), ASTERISK) != 1) {
545         LOGE("there must be only one '%c'", ASTERISK);
546         return false;
547     }
548     return true;
549 }
550 
GetSupportDimensionsByFormObj(const std::unique_ptr<PtJson> & formObj,std::list<std::string> & supportDimensions)551 bool PackInfo::GetSupportDimensionsByFormObj(const std::unique_ptr<PtJson>& formObj,
552     std::list<std::string>& supportDimensions)
553 {
554     if (!formObj) {
555         LOGE("Form node is null!");
556         return false;
557     }
558     if (!formObj->Contains(SUPPORT_DIMENSIONS.c_str())) {
559         LOGE("Form node has no %s node!", SUPPORT_DIMENSIONS.c_str());
560         return false;
561     }
562     std::unique_ptr<PtJson> supportDimensionsObj;
563     if (formObj->GetArray(SUPPORT_DIMENSIONS.c_str(), &supportDimensionsObj) != Result::SUCCESS) {
564         LOGE("Form node get %s array node failed!", SUPPORT_DIMENSIONS.c_str());
565         return false;
566     }
567     for (int32_t i = 0; i < supportDimensionsObj->GetSize(); i++) {
568         supportDimensions.push_back(supportDimensionsObj->Get(i)->GetString());
569     }
570     return true;
571 }
572 
573 // java : parsePackInfoFormsName
GetFormNames(std::list<std::string> & formNames,std::list<std::string> & formFullNames)574 bool PackInfo::GetFormNames(std::list<std::string>& formNames, std::list<std::string>& formFullNames)
575 {
576     std::unique_ptr<PtJson> modulesObj;
577     if (!GetModulesObject(modulesObj)) {
578         LOGE("GetModulesObject failed!");
579         return false;
580     }
581     if (!modulesObj->IsArray()) {
582         LOGE("Module node is not array!");
583         return false;
584     }
585     for (int32_t i = 0; i < modulesObj->GetSize(); i++) {
586         std::unique_ptr<PtJson> distroObj;
587         if (!GetDistroObjectByModuleObj(modulesObj->Get(i), distroObj)) {
588             LOGE("GetDistroObjectByModuleObj failed!");
589             return false;
590         }
591         std::string moduleName;
592         if (!GetModuleNameByDistroObj(distroObj, moduleName) || moduleName.empty()) {
593             LOGE("GetModuleNameByDistroObj failed or moduleName is empty!");
594             continue;
595         }
596         std::unique_ptr<PtJson> extensionAbilitiesObj;
597         if (!GetExtensionAbilitiesObjByModuleObj(modulesObj->Get(i), extensionAbilitiesObj)) {
598             LOGE("GetExtensionAbilitiesObjByModuleObj failed!");
599             return false;
600         }
601         if (!GetFormNamesByExtensionAbilitiesObj(extensionAbilitiesObj, moduleName, formNames, formFullNames)) {
602             LOGE("GetFormNamesByExtensionAbilitiesObj failed!");
603             return false;
604         }
605     }
606     return true;
607 }
608 
GetFormNamesByExtensionAbilitiesObj(const std::unique_ptr<PtJson> & extensionAbilitiesObj,std::string moduleName,std::list<std::string> & formNames,std::list<std::string> & formFullNames)609 bool PackInfo::GetFormNamesByExtensionAbilitiesObj(const std::unique_ptr<PtJson>& extensionAbilitiesObj,
610     std::string moduleName, std::list<std::string>& formNames, std::list<std::string>& formFullNames)
611 {
612     if (!extensionAbilitiesObj || !extensionAbilitiesObj->IsArray()) {
613         LOGE("ExtensionAbilities node is null or is not array!");
614         return false;
615     }
616     for (int32_t j = 0; j < extensionAbilitiesObj->GetSize(); j++) {
617         std::unique_ptr<PtJson> formsObj;
618         if (!GetFormsObjByExtensionAbilityObj(extensionAbilitiesObj->Get(j), formsObj)) {
619             LOGE("GetFormsObjByExtensionAbilityObj failed!");
620             return false;
621         }
622         if (!GetFormNamesByFormsObj(formsObj, moduleName, formNames, formFullNames)) {
623             LOGE("GetFormNamesByFormsObj failed!");
624             return false;
625         }
626     }
627     return true;
628 }
629 
630 
GetFormNamesByFormsObj(const std::unique_ptr<PtJson> & formsObj,std::string moduleName,std::list<std::string> & formNames,std::list<std::string> & formFullNames)631 bool PackInfo::GetFormNamesByFormsObj(const std::unique_ptr<PtJson>& formsObj,
632     std::string moduleName, std::list<std::string>& formNames, std::list<std::string>& formFullNames)
633 {
634     if (!formsObj || !formsObj->IsArray()) {
635         LOGE("Form node is null or is not array!");
636         return false;
637     }
638     for (int32_t k = 0; k < formsObj->GetSize(); k++) {
639         std::string formName;
640         std::string defaultDimension;
641         std::list<std::string> supportDimensions;
642         std::string formFullName;
643         if (!GetNameByFormObj(formsObj->Get(k), formName) || formName.empty()) {
644             continue;
645         }
646         formNames.push_back(formName);
647         if (!GetDefaultDimensionByFormObj(formsObj->Get(k), defaultDimension)) {
648             LOGE("GetDefaultDimensionByFormObj failed!");
649             return false;
650         }
651         if (!GetSupportDimensionsByFormObj(formsObj->Get(k), supportDimensions)) {
652             LOGE("GetSupportDimensionsByFormObj failed!");
653             return false;
654         }
655         for (std::string supportDimension : supportDimensions) {
656             formFullName = moduleName + "/" + formName + "-" + Utils::ReplaceAll(supportDimension, "*", "x");
657             formFullNames.push_back(formFullName);
658         }
659     }
660     return true;
661 }
662 
GetPackageNames(std::list<std::string> & packageNames)663 bool PackInfo::GetPackageNames(std::list<std::string> &packageNames)
664 {
665     std::unique_ptr<PtJson> packagesObj;
666     if (!GetPackagesObject(packagesObj)) {
667         LOGE("GetPackagesObject failed!");
668         return false;
669     }
670     return GetPackageNamesByPackagesObj(packagesObj, packageNames);
671 }
672 
GetPackageNamesByPackagesObj(const std::unique_ptr<PtJson> & packagesObj,std::list<std::string> & packageNames)673 bool PackInfo::GetPackageNamesByPackagesObj(const std::unique_ptr<PtJson>& packagesObj,
674     std::list<std::string> &packageNames)
675 {
676     if (!packagesObj || !packagesObj->IsArray()) {
677         LOGE("Packages node is null or is not array!");
678         return false;
679     }
680     for (int i = 0; i < packagesObj->GetSize(); i++) {
681         std::unique_ptr<PtJson> packageObj;
682         if (!GetPackageObject(i, packageObj)) {
683             LOGE("GetPackageObject failed!");
684             return false;
685         }
686         std::string name;
687         if (!GetNameByPackageObj(packageObj, name)) {
688             LOGE("GetNameByPackageObj failed!");
689             return false;
690         }
691         packageNames.push_back(name);
692     }
693     return true;
694 }
695 
SetMinCompatibleVersionCode(const int32_t & minCompatibleVersionCode)696 bool PackInfo::SetMinCompatibleVersionCode(const int32_t& minCompatibleVersionCode)
697 {
698     std::unique_ptr<PtJson> versionObj;
699     if (!GetVersionObject(versionObj)) {
700         LOGE("GetVersionObject failed!");
701         return false;
702     }
703     if (!versionObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
704         if (!versionObj->Add(MIN_COMPATIBLE_VERSION_CODE.c_str(), minCompatibleVersionCode)) {
705             LOGE("App node add %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
706             return false;
707         }
708         return true;
709     }
710     if (versionObj->SetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(), minCompatibleVersionCode) != Result::SUCCESS) {
711         LOGE("Version node set %s failed!", CODE.c_str());
712         return false;
713     }
714     return true;
715 }
716 
SetMinAPIVersion(const int32_t & minAPIVersion)717 bool PackInfo::SetMinAPIVersion(const int32_t& minAPIVersion)
718 {
719     std::unique_ptr<PtJson> modulesObj;
720     if (!GetModulesObject(modulesObj)) {
721         LOGE("GetModulesObject failed!");
722         return false;
723     }
724     int32_t modulesSize = modulesObj->GetSize();
725     for (int i = 0; i < modulesSize; i++) {
726         std::unique_ptr<PtJson> moduleObj = modulesObj->Get(i);
727         if (moduleObj == nullptr) {
728             LOGE("GetModuleObject failed!");
729             return false;
730         }
731         std::unique_ptr<PtJson> apiVersionObj;
732         if (!GetApiVersionObjectByModuleObj(moduleObj, apiVersionObj)) {
733             LOGE("GetApiVersionObjectByModuleObj failed!");
734             return false;
735         }
736         if (!apiVersionObj->Contains(COMPATIBLE.c_str())) {
737             if (!apiVersionObj->Add(COMPATIBLE.c_str(), minAPIVersion)) {
738                 LOGE("App node add %s failed!", COMPATIBLE.c_str());
739                 return false;
740             }
741             return true;
742         }
743         if (apiVersionObj->SetInt(COMPATIBLE.c_str(), minAPIVersion) != Result::SUCCESS) {
744             LOGE("Module node set %s failed!", COMPATIBLE.c_str());
745             return false;
746         }
747     }
748     return true;
749 }
750 
SetTargetAPIVersion(const int32_t & targetAPIVersion)751 bool PackInfo::SetTargetAPIVersion(const int32_t& targetAPIVersion)
752 {
753     std::unique_ptr<PtJson> modulesObj;
754     if (!GetModulesObject(modulesObj)) {
755         LOGE("GetModulesObject failed!");
756         return false;
757     }
758     int32_t modulesSize = modulesObj->GetSize();
759     for (int i = 0; i < modulesSize; i++) {
760         std::unique_ptr<PtJson> moduleObj = modulesObj->Get(i);
761         if (moduleObj == nullptr) {
762             LOGE("GetModuleObject failed!");
763             return false;
764         }
765         std::unique_ptr<PtJson> apiVersionObj;
766         if (!GetApiVersionObjectByModuleObj(moduleObj, apiVersionObj)) {
767             LOGE("GetApiVersionObjectByModuleObj failed!");
768             return false;
769         }
770         if (!apiVersionObj->Contains(TARGET.c_str())) {
771             if (!apiVersionObj->Add(TARGET.c_str(), targetAPIVersion)) {
772                 LOGE("App node add %s failed!", TARGET.c_str());
773                 return false;
774             }
775             return true;
776         }
777         if (apiVersionObj->SetInt(TARGET.c_str(), targetAPIVersion) != Result::SUCCESS) {
778             LOGE("Module node set %s failed!", TARGET.c_str());
779             return false;
780         }
781     }
782     return true;
783 }
784 
SetApiReleaseType(const std::string & apiReleaseType)785 bool PackInfo::SetApiReleaseType(const std::string& apiReleaseType)
786 {
787     std::unique_ptr<PtJson> modulesObj;
788     if (!GetModulesObject(modulesObj)) {
789         LOGE("GetModulesObject failed!");
790         return false;
791     }
792     int32_t modulesSize = modulesObj->GetSize();
793     for (int i = 0; i < modulesSize; i++) {
794         std::unique_ptr<PtJson> moduleObj = modulesObj->Get(i);
795         if (moduleObj == nullptr) {
796             LOGE("GetModuleObject failed!");
797             return false;
798         }
799         std::unique_ptr<PtJson> apiVersionObj;
800         if (!GetApiVersionObjectByModuleObj(moduleObj, apiVersionObj)) {
801             LOGE("GetApiVersionObjectByModuleObj failed!");
802             return false;
803         }
804         if (!apiVersionObj->Contains(RELEASE_TYPE.c_str())) {
805             if (!apiVersionObj->Add(RELEASE_TYPE.c_str(), apiReleaseType.c_str())) {
806                 LOGE("App node add %s failed!", RELEASE_TYPE.c_str());
807                 return false;
808             }
809             return true;
810         }
811         if (apiVersionObj->SetString(RELEASE_TYPE.c_str(), apiReleaseType) != Result::SUCCESS) {
812             LOGE("Module node set %s failed!", RELEASE_TYPE.c_str());
813             return false;
814         }
815     }
816     return true;
817 }
818 
SetBundleType(const std::string & bundleType)819 bool PackInfo::SetBundleType(const std::string& bundleType)
820 {
821     std::unique_ptr<PtJson> appObj;
822     if (!GetAppObject(appObj)) {
823         LOGE("GetAppObject failed!");
824         return false;
825     }
826     if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
827         if (!appObj->Add(BUNDLE_TYPE.c_str(), bundleType.c_str())) {
828             LOGE("App node add %s failed!", BUNDLE_TYPE.c_str());
829             return false;
830         }
831         return true;
832     }
833     if (appObj->SetString(BUNDLE_TYPE.c_str(), bundleType) != Result::SUCCESS) {
834         LOGE("App node set %s failed!", BUNDLE_TYPE.c_str());
835         return false;
836     }
837     return true;
838 }
839 
SetInstallationFree(const bool & installationFree)840 bool PackInfo::SetInstallationFree(const bool& installationFree)
841 {
842     std::unique_ptr<PtJson> modulesObj;
843     if (!GetModulesObject(modulesObj)) {
844         LOGE("GetModulesObject failed!");
845         return false;
846     }
847     int32_t modulesSize = modulesObj->GetSize();
848     for (int i = 0; i < modulesSize; i++) {
849         std::unique_ptr<PtJson> moduleObj = modulesObj->Get(i);
850         if (moduleObj == nullptr) {
851             LOGE("GetModuleObject failed!");
852             return false;
853         }
854         std::unique_ptr<PtJson> distroObj;
855         if (!GetDistroObject(i, distroObj)) {
856             LOGE("GetDistroObject failed!");
857             return false;
858         }
859         if (!distroObj->Contains(INSTALLATION_FREE.c_str())) {
860             if (!distroObj->Add(INSTALLATION_FREE.c_str(), installationFree)) {
861                 LOGE("App node add %s failed!", INSTALLATION_FREE.c_str());
862                 return false;
863             }
864             return true;
865         }
866         if (distroObj->SetBool(INSTALLATION_FREE.c_str(), installationFree) != Result::SUCCESS) {
867             LOGE("Module node set %s failed!", INSTALLATION_FREE.c_str());
868             return false;
869         }
870     }
871     return true;
872 }
873 
SetDeliveryWithInstall(const bool & deliveryWithInstall)874 bool PackInfo::SetDeliveryWithInstall(const bool& deliveryWithInstall)
875 {
876     std::unique_ptr<PtJson> modulesObj;
877     if (!GetModulesObject(modulesObj)) {
878         LOGE("GetModulesObject failed!");
879         return false;
880     }
881     int32_t modulesSize = modulesObj->GetSize();
882     for (int i = 0; i < modulesSize; i++) {
883         std::unique_ptr<PtJson> moduleObj = modulesObj->Get(i);
884         if (moduleObj == nullptr) {
885             LOGE("GetModuleObject failed!");
886             return false;
887         }
888         std::unique_ptr<PtJson> distroObj;
889         if (!GetDistroObject(i, distroObj)) {
890             LOGE("GetDistroObject failed!");
891             return false;
892         }
893         if (!distroObj->Contains(DELIVERY_WITH_INSTALL.c_str())) {
894             if (!distroObj->Add(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall)) {
895                 LOGE("App node add %s failed!", DELIVERY_WITH_INSTALL.c_str());
896                 return false;
897             }
898             return true;
899         }
900         if (distroObj->SetBool(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall) != Result::SUCCESS) {
901             LOGE("Module node set %s failed!", DELIVERY_WITH_INSTALL.c_str());
902             return false;
903         }
904     }
905     std::unique_ptr<PtJson> packagesObj;
906     if (!GetPackagesObject(packagesObj)) {
907         LOGE("GetModuleObject failed!");
908         return false;
909     }
910     int32_t packagesSize = packagesObj->GetSize();
911     for (int i = 0; i < packagesSize; i++) {
912         std::unique_ptr<PtJson> packageObj = packagesObj->Get(i);
913         if (packageObj == nullptr) {
914             LOGE("GetModuleObject failed!");
915             return false;
916         }
917         if (!packageObj->Contains(DELIVERY_WITH_INSTALL.c_str())) {
918             if (!packageObj->Add(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall)) {
919                 LOGE("App node add %s failed!", DELIVERY_WITH_INSTALL.c_str());
920                 return false;
921             }
922             return true;
923         }
924         if (packageObj->SetBool(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall) != Result::SUCCESS) {
925             LOGE("Module node set %s failed!", DELIVERY_WITH_INSTALL.c_str());
926             return false;
927         }
928     }
929     return true;
930 }
931 
SetDeviceTypes(const std::list<std::string> & deviceTypes)932 bool PackInfo::SetDeviceTypes(const std::list<std::string>& deviceTypes)
933 {
934     std::unique_ptr<PtJson> modulesObj;
935     if (!GetModulesObject(modulesObj)) {
936         LOGE("GetModuleObject failed!");
937         return false;
938     }
939     int32_t modulesSize = modulesObj->GetSize();
940     for (int i = 0; i < modulesSize; i++) {
941         std::unique_ptr<PtJson> moduleObj = modulesObj->Get(i);
942         if (moduleObj == nullptr) {
943             LOGE("GetModuleObject failed!");
944             return false;
945         }
946         if (!moduleObj->Contains(DEVICE_TYPE.c_str())) {
947             if (!moduleObj->Add(DEVICE_TYPE.c_str(), deviceTypes)) {
948                 LOGE("App node add %s failed!", DEVICE_TYPE.c_str());
949                 return false;
950             }
951             return true;
952         }
953         if (moduleObj->SetArray(DEVICE_TYPE.c_str(), deviceTypes) != Result::SUCCESS) {
954             LOGE("Module node set %s failed!", DEVICE_TYPE.c_str());
955             return false;
956         }
957     }
958     std::unique_ptr<PtJson> packagesObj;
959     if (!GetPackagesObject(packagesObj)) {
960         LOGE("GetModuleObject failed!");
961         return false;
962     }
963     int32_t packagesSize = packagesObj->GetSize();
964     for (int i = 0; i < packagesSize; i++) {
965         std::unique_ptr<PtJson> packageObj = packagesObj->Get(i);
966         if (packageObj == nullptr) {
967             LOGE("GetModuleObject failed!");
968             return false;
969         }
970         if (!packageObj->Contains(DEVICE_TYPE.c_str())) {
971             if (!packageObj->Add(DEVICE_TYPE.c_str(), deviceTypes)) {
972                 LOGE("App node add %s failed!", DEVICE_TYPE.c_str());
973                 return false;
974             }
975             return true;
976         }
977         if (packageObj->SetArray(DEVICE_TYPE.c_str(), deviceTypes) != Result::SUCCESS) {
978             LOGE("Module node set %s failed!", DEVICE_TYPE.c_str());
979             return false;
980         }
981     }
982     return true;
983 }
984 } // namespace AppPackingTool
985 } // namespace OHOS
986