• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 }
44 
ParseFromString(const std::string & jsonString)45 bool PackInfo::ParseFromString(const std::string& jsonString)
46 {
47     Release();
48     if (jsonString.length() == 0) {
49         LOGE("Json length is zero!");
50         return false;
51     }
52     root_ = PtJson::Parse(jsonString);
53     return IsValid();
54 }
55 
ParseFromFile(const std::string & jsonFile)56 bool PackInfo::ParseFromFile(const std::string& jsonFile)
57 {
58     Release();
59     std::string realJsonFile;
60     if (!Utils::GetRealPath(jsonFile, realJsonFile)) {
61         LOGE("get real json file failed! jsonFile=%s", jsonFile.c_str());
62         return false;
63     }
64     std::ifstream inFile(realJsonFile, std::ios::in);
65     if (!inFile.is_open()) {
66         LOGE("Open json file failed! jsonFile=%s, realJsonFile=%s", jsonFile.c_str(), realJsonFile.c_str());
67         return false;
68     }
69     std::string fileContent((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
70     inFile.close();
71     root_ = PtJson::Parse(fileContent);
72     return IsValid();
73 }
74 
ToString()75 std::string PackInfo::ToString()
76 {
77     return root_->Stringify();
78 }
79 
Release()80 void PackInfo::Release()
81 {
82     if (root_) {
83         root_->ReleaseRoot();
84         root_ = nullptr;
85     }
86 }
87 
IsValid()88 bool PackInfo::IsValid()
89 {
90     return (root_.get() != nullptr);
91 }
92 
GetSummaryObject(std::unique_ptr<PtJson> & summaryObj)93 bool PackInfo::GetSummaryObject(std::unique_ptr<PtJson>& summaryObj)
94 {
95     if (root_.get() == nullptr) {
96         LOGE("Json root is null!");
97         return false;
98     }
99     if (!root_->Contains(SUMMARY.c_str())) {
100         LOGE("Json root has no %s node!", SUMMARY.c_str());
101         return false;
102     }
103     if (root_->GetObject(SUMMARY.c_str(), &summaryObj) != Result::SUCCESS) {
104         LOGE("Json root get %s node failed!", SUMMARY.c_str());
105         return false;
106     }
107     return true;
108 }
109 
GetPackagesObject(std::unique_ptr<PtJson> & packagesObj)110 bool PackInfo::GetPackagesObject(std::unique_ptr<PtJson>& packagesObj)
111 {
112     if (root_.get() == nullptr) {
113         LOGE("Json root is null!");
114         return false;
115     }
116     if (!root_->Contains(PACKAGES.c_str())) {
117         LOGE("Json root has no %s node!", PACKAGES.c_str());
118         return false;
119     }
120     if (root_->GetArray(PACKAGES.c_str(), &packagesObj) != Result::SUCCESS) {
121         LOGE("Json root get %s array node failed!", PACKAGES.c_str());
122         return false;
123     }
124     return true;
125 }
126 
GetAppObject(std::unique_ptr<PtJson> & appObj)127 bool PackInfo::GetAppObject(std::unique_ptr<PtJson>& appObj)
128 {
129     std::unique_ptr<PtJson> summaryObj;
130     if (!GetSummaryObject(summaryObj)) {
131         LOGE("GetSummaryObject failed!");
132         return false;
133     }
134     if (!summaryObj->Contains(APP.c_str())) {
135         LOGE("Summary node has no %s node!", APP.c_str());
136         return false;
137     }
138     if (summaryObj->GetObject(APP.c_str(), &appObj) != Result::SUCCESS) {
139         LOGE("Summary node get %s node failed!", APP.c_str());
140         return false;
141     }
142     return true;
143 }
144 
GetModulesObject(std::unique_ptr<PtJson> & modulesObj)145 bool PackInfo::GetModulesObject(std::unique_ptr<PtJson>& modulesObj)
146 {
147     std::unique_ptr<PtJson> summaryObj;
148     if (!GetSummaryObject(summaryObj)) {
149         LOGE("GetSummaryObject failed!");
150         return false;
151     }
152     if (!summaryObj->Contains(MODULES.c_str())) {
153         LOGE("Summary node has no %s node!", MODULES.c_str());
154         return false;
155     }
156     if (summaryObj->GetArray(MODULES.c_str(), &modulesObj) != Result::SUCCESS) {
157         LOGE("Summary node get %s node failed!", MODULES.c_str());
158         return false;
159     }
160     return true;
161 }
162 
GetBundleName(std::string & bundleName)163 bool PackInfo::GetBundleName(std::string& bundleName)
164 {
165     std::unique_ptr<PtJson> appObj;
166     if (!GetAppObject(appObj)) {
167         LOGE("GetAppObject failed!");
168         return false;
169     }
170     return GetBundleNameByAppObj(appObj, bundleName);
171 }
172 
GetBundleNameByAppObj(const std::unique_ptr<PtJson> & appObj,std::string & bundleName)173 bool PackInfo::GetBundleNameByAppObj(const std::unique_ptr<PtJson>& appObj, std::string& bundleName)
174 {
175     if (!appObj) {
176         LOGE("App node is null!");
177         return false;
178     }
179     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
180         LOGE("App node has no %s node!", BUNDLE_NAME.c_str());
181         return false;
182     }
183     if (appObj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) {
184         LOGE("App node get %s failed!", BUNDLE_NAME.c_str());
185         return false;
186     }
187     return true;
188 }
189 
SetBundleName(const std::string & bundleName)190 bool PackInfo::SetBundleName(const std::string& bundleName)
191 {
192     std::unique_ptr<PtJson> appObj;
193     if (!GetAppObject(appObj)) {
194         LOGE("GetAppObject failed!");
195         return false;
196     }
197     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
198         LOGE("App node has no %s node!", BUNDLE_NAME.c_str());
199         return false;
200     }
201     if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) {
202         LOGE("App node set %s failed!", BUNDLE_NAME.c_str());
203         return false;
204     }
205     return true;
206 }
207 
GetBundleType(std::string & bundleType,const std::string & defaultBundleType)208 bool PackInfo::GetBundleType(std::string& bundleType, const std::string& defaultBundleType)
209 {
210     std::unique_ptr<PtJson> appObj;
211     if (!GetAppObject(appObj)) {
212         LOGE("GetAppObject failed!");
213         return false;
214     }
215     return GetBundleTypeByAppObj(appObj, bundleType, defaultBundleType);
216 }
217 
GetBundleTypeByAppObj(const std::unique_ptr<PtJson> & appObj,std::string & bundleType,const std::string & defaultBundleType)218 bool PackInfo::GetBundleTypeByAppObj(const std::unique_ptr<PtJson>& appObj, std::string& bundleType,
219     const std::string& defaultBundleType)
220 {
221     if (!appObj) {
222         LOGE("App node is null!");
223         return false;
224     }
225     if (appObj->Contains(BUNDLE_TYPE.c_str())) {
226         if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) != Result::SUCCESS) {
227             LOGE("App node get %s failed!", BUNDLE_TYPE.c_str());
228             return false;
229         }
230     } else {
231         bundleType = defaultBundleType;
232     }
233     return true;
234 }
235 
GetVersionObject(std::unique_ptr<PtJson> & versionObj)236 bool PackInfo::GetVersionObject(std::unique_ptr<PtJson>& versionObj)
237 {
238     std::unique_ptr<PtJson> appObj;
239     if (!GetAppObject(appObj)) {
240         LOGE("GetAppObject failed!");
241         return false;
242     }
243     if (!appObj->Contains(VERSION.c_str())) {
244         LOGE("App node has no %s node!", VERSION.c_str());
245         return false;
246     }
247     if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) {
248         LOGE("App node get %s node failed!", VERSION.c_str());
249         return false;
250     }
251     return true;
252 }
253 
GetDistroObject(int32_t moduleIndex,std::unique_ptr<PtJson> & distroObj)254 bool PackInfo::GetDistroObject(int32_t moduleIndex, std::unique_ptr<PtJson>& distroObj)
255 {
256     std::unique_ptr<PtJson> modulesObj;
257     if (!GetModulesObject(modulesObj)) {
258         LOGE("GetModulesObject failed!");
259         return false;
260     }
261     return GetDistroObjectByModulesObj(modulesObj, moduleIndex, distroObj);
262 }
263 
GetDistroObjectByModulesObj(const std::unique_ptr<PtJson> & modulesObj,int32_t moduleIndex,std::unique_ptr<PtJson> & distroObj)264 bool PackInfo::GetDistroObjectByModulesObj(const std::unique_ptr<PtJson>& modulesObj,
265     int32_t moduleIndex, std::unique_ptr<PtJson>& distroObj)
266 {
267     if (!modulesObj || !modulesObj->IsArray()) {
268         LOGE("Module node is null or is not array!");
269         return false;
270     }
271     if (moduleIndex >= modulesObj->GetSize()) {
272         LOGE("Module node index error![moduleIndex=%d][modulesObjSize=%d]", moduleIndex, modulesObj->GetSize());
273         return false;
274     }
275     std::unique_ptr<PtJson> moduleObj = modulesObj->Get(moduleIndex);
276     return GetDistroObjectByModuleObj(moduleObj, distroObj);
277 }
278 
GetDistroObjectByModuleObj(const std::unique_ptr<PtJson> & moduleObj,std::unique_ptr<PtJson> & distroObj)279 bool PackInfo::GetDistroObjectByModuleObj(const std::unique_ptr<PtJson>& moduleObj,
280     std::unique_ptr<PtJson>& distroObj)
281 {
282     if (!moduleObj) {
283         LOGE("Module node is null!");
284         return false;
285     }
286     if (!moduleObj->Contains(DISTRO.c_str())) {
287         LOGE("Module node has no %s node!", DISTRO.c_str());
288         return false;
289     }
290     if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
291         LOGE("Module node get %s node failed!", DISTRO.c_str());
292         return false;
293     }
294     return true;
295 }
296 
GetExtensionAbilitiesObj(int32_t moduleIndex,std::unique_ptr<PtJson> & extensionAbilitiesObj)297 bool PackInfo::GetExtensionAbilitiesObj(int32_t moduleIndex, std::unique_ptr<PtJson>& extensionAbilitiesObj)
298 {
299     std::unique_ptr<PtJson> modulesObj;
300     if (!GetModulesObject(modulesObj)) {
301         LOGE("GetModulesObject failed!");
302         return false;
303     }
304     return GetDistroObjectByModulesObj(modulesObj, moduleIndex, extensionAbilitiesObj);
305 }
306 
GetExtensionAbilitiesObjByModulesObj(const std::unique_ptr<PtJson> & modulesObj,int32_t moduleIndex,std::unique_ptr<PtJson> & extensionAbilitiesObj)307 bool PackInfo::GetExtensionAbilitiesObjByModulesObj(const std::unique_ptr<PtJson>& modulesObj,
308     int32_t moduleIndex, std::unique_ptr<PtJson>& extensionAbilitiesObj)
309 {
310     if (!modulesObj || !modulesObj->IsArray()) {
311         LOGE("Module node is null or is not array!");
312         return false;
313     }
314     if (moduleIndex >= modulesObj->GetSize()) {
315         LOGE("Module node index error![moduleIndex=%d][modulesObjSize=%d]", moduleIndex, modulesObj->GetSize());
316         return false;
317     }
318     std::unique_ptr<PtJson> moduleObj = modulesObj->Get(moduleIndex);
319     return GetExtensionAbilitiesObjByModuleObj(moduleObj, extensionAbilitiesObj);
320 }
321 
GetExtensionAbilitiesObjByModuleObj(const std::unique_ptr<PtJson> & moduleObj,std::unique_ptr<PtJson> & extensionAbilitiesObj)322 bool PackInfo::GetExtensionAbilitiesObjByModuleObj(const std::unique_ptr<PtJson>& moduleObj,
323     std::unique_ptr<PtJson>& extensionAbilitiesObj)
324 {
325     if (!moduleObj) {
326         LOGE("Module node is null!");
327         return false;
328     }
329     if (!moduleObj->Contains(EXTENSION_ABILITIES.c_str())) {
330         LOGE("Module node has no %s node!", EXTENSION_ABILITIES.c_str());
331         return false;
332     }
333     if (moduleObj->GetArray(EXTENSION_ABILITIES.c_str(), &extensionAbilitiesObj) != Result::SUCCESS) {
334         LOGE("Module node get %s array node failed!", EXTENSION_ABILITIES.c_str());
335         return false;
336     }
337     return true;
338 }
339 
GetFormsObjByExtensionAbilityObj(const std::unique_ptr<PtJson> & extensionAbilityObj,std::unique_ptr<PtJson> & formsObj)340 bool PackInfo::GetFormsObjByExtensionAbilityObj(const std::unique_ptr<PtJson>& extensionAbilityObj,
341     std::unique_ptr<PtJson>& formsObj)
342 {
343     if (!extensionAbilityObj) {
344         LOGE("ExtensionAbility node is null!");
345         return false;
346     }
347     if (!extensionAbilityObj->Contains(FORMS.c_str())) {
348         LOGE("ExtensionAbility node has no %s node!", FORMS.c_str());
349         return false;
350     }
351     if (extensionAbilityObj->GetArray(FORMS.c_str(), &formsObj) != Result::SUCCESS) {
352         LOGE("ExtensionAbility node get %s array node failed!", FORMS.c_str());
353         return false;
354     }
355     return true;
356 }
357 
GetPackageObject(int32_t packageIndex,std::unique_ptr<PtJson> & packageObj)358 bool PackInfo::GetPackageObject(int32_t packageIndex, std::unique_ptr<PtJson>& packageObj)
359 {
360     std::unique_ptr<PtJson> packagesObj;
361     if (!GetPackagesObject(packagesObj)) {
362         LOGE("GetPackagesObject failed!");
363         return false;
364     }
365     if (!packagesObj->IsArray()) {
366         LOGE("Packages node is not array!");
367         return false;
368     }
369     if (packageIndex >= packagesObj->GetSize()) {
370         LOGE("Packages node index error![packageIndex=%d][packagesObjSize=%d]", packageIndex, packagesObj->GetSize());
371         return false;
372     }
373     packageObj = packagesObj->Get(packageIndex);
374     return true;
375 }
376 
GetVersion(PackInfoVersion & version)377 bool PackInfo::GetVersion(PackInfoVersion& version)
378 {
379     std::unique_ptr<PtJson> versionObj;
380     if (!GetVersionObject(versionObj)) {
381         LOGE("GetVersionObject failed!");
382         return false;
383     }
384     return GetVersionByVersionObj(versionObj, version);
385 }
386 
GetVersionByVersionObj(const std::unique_ptr<PtJson> & versionObj,PackInfoVersion & version)387 bool PackInfo::GetVersionByVersionObj(const std::unique_ptr<PtJson>& versionObj, PackInfoVersion& version)
388 {
389     if (!versionObj) {
390         LOGE("Version node is null!");
391         return false;
392     }
393     if (!versionObj->Contains(CODE.c_str()) || !versionObj->Contains(NAME.c_str())) {
394         LOGE("Version node has no %s node or %s node!", CODE.c_str(), NAME.c_str());
395         return false;
396     }
397     if (versionObj->GetInt(CODE.c_str(), &version.code) != Result::SUCCESS) {
398         LOGE("Version node get %s failed!", CODE.c_str());
399         return false;
400     }
401     if (versionObj->GetString(NAME.c_str(), &version.name) != Result::SUCCESS) {
402         LOGE("Version node get %s failed!", NAME.c_str());
403         return false;
404     }
405     return true;
406 }
407 
SetVersionCode(const int32_t & versionCode)408 bool PackInfo::SetVersionCode(const int32_t& versionCode)
409 {
410     std::unique_ptr<PtJson> versionObj;
411     if (!GetVersionObject(versionObj)) {
412         LOGE("GetVersionObject failed!");
413         return false;
414     }
415     if (!versionObj->Contains(CODE.c_str())) {
416         LOGE("Version node has no %s node!", CODE.c_str());
417         return false;
418     }
419     if (versionObj->SetInt(CODE.c_str(), versionCode) != Result::SUCCESS) {
420         LOGE("Version node set %s failed!", CODE.c_str());
421         return false;
422     }
423     return true;
424 }
425 
SetVersionName(const std::string & versionName)426 bool PackInfo::SetVersionName(const std::string& versionName)
427 {
428     std::unique_ptr<PtJson> versionObj;
429     if (!GetVersionObject(versionObj)) {
430         LOGE("GetVersionObject failed!");
431         return false;
432     }
433     if (!versionObj->Contains(NAME.c_str())) {
434         LOGE("Version node has no %s node!", NAME.c_str());
435         return false;
436     }
437     if (versionObj->SetString(NAME.c_str(), versionName) != Result::SUCCESS) {
438         LOGE("Version node set %s failed!", NAME.c_str());
439         return false;
440     }
441     return true;
442 }
443 
GetModuleNameByDistroObj(const std::unique_ptr<PtJson> & distroObj,std::string & moduleName)444 bool PackInfo::GetModuleNameByDistroObj(const std::unique_ptr<PtJson>& distroObj, std::string& moduleName)
445 {
446     if (!distroObj) {
447         LOGE("Distro node is null!");
448         return false;
449     }
450     if (distroObj->Contains(MODULE_NAME.c_str())) {
451         if (distroObj->GetString(MODULE_NAME.c_str(), &moduleName) != Result::SUCCESS) {
452             LOGE("Distro node get %s failed!", MODULE_NAME.c_str());
453             return false;
454         }
455     } else {
456         moduleName = "";
457     }
458     return true;
459 }
460 
GetNameByPackageObj(const std::unique_ptr<PtJson> & packageObj,std::string & name)461 bool PackInfo::GetNameByPackageObj(const std::unique_ptr<PtJson>& packageObj, std::string& name)
462 {
463     if (!packageObj) {
464         LOGE("Package node is null!");
465         return false;
466     }
467     if (packageObj->Contains(NAME.c_str())) {
468         if (packageObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) {
469             LOGE("Package node get %s failed!", NAME.c_str());
470             return false;
471         }
472     } else {
473         name = "";
474     }
475     return true;
476 }
477 
GetNameByFormObj(const std::unique_ptr<PtJson> & formObj,std::string & name)478 bool PackInfo::GetNameByFormObj(const std::unique_ptr<PtJson>& formObj, std::string& name)
479 {
480     if (!formObj) {
481         LOGE("Form node is null!");
482         return false;
483     }
484     if (formObj->Contains(NAME.c_str())) {
485         if (formObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) {
486             LOGE("Form node get %s failed!", NAME.c_str());
487             return false;
488         }
489     } else {
490         name = "";
491     }
492     return true;
493 }
494 
GetDefaultDimensionByFormObj(const std::unique_ptr<PtJson> & formObj,std::string & defaultDimension)495 bool PackInfo::GetDefaultDimensionByFormObj(const std::unique_ptr<PtJson>& formObj, std::string& defaultDimension)
496 {
497     if (!formObj) {
498         LOGE("Form node is null!");
499         return false;
500     }
501     if (!formObj->Contains(DEFAULT_DIMENSION.c_str())) {
502         LOGE("Form node has no %s node!", DEFAULT_DIMENSION.c_str());
503         return false;
504     }
505     if (formObj->GetString(DEFAULT_DIMENSION.c_str(), &defaultDimension) != Result::SUCCESS) {
506         LOGE("Form node get %s failed!", DEFAULT_DIMENSION.c_str());
507         return false;
508     }
509     if (std::count(defaultDimension.begin(), defaultDimension.end(), ASTERISK) != 1) {
510         LOGE("there must be only one '%c'", ASTERISK);
511         return false;
512     }
513     return true;
514 }
515 
GetSupportDimensionsByFormObj(const std::unique_ptr<PtJson> & formObj,std::list<std::string> & supportDimensions)516 bool PackInfo::GetSupportDimensionsByFormObj(const std::unique_ptr<PtJson>& formObj,
517     std::list<std::string>& supportDimensions)
518 {
519     if (!formObj) {
520         LOGE("Form node is null!");
521         return false;
522     }
523     if (!formObj->Contains(SUPPORT_DIMENSIONS.c_str())) {
524         LOGE("Form node has no %s node!", SUPPORT_DIMENSIONS.c_str());
525         return false;
526     }
527     std::unique_ptr<PtJson> supportDimensionsObj;
528     if (formObj->GetArray(SUPPORT_DIMENSIONS.c_str(), &supportDimensionsObj) != Result::SUCCESS) {
529         LOGE("Form node get %s array node failed!", SUPPORT_DIMENSIONS.c_str());
530         return false;
531     }
532     for (int32_t i = 0; i < supportDimensionsObj->GetSize(); i++) {
533         supportDimensions.push_back(supportDimensionsObj->Get(i)->GetString());
534     }
535     return true;
536 }
537 
538 // java : parsePackInfoFormsName
GetFormNames(std::list<std::string> & formNames,std::list<std::string> & formFullNames)539 bool PackInfo::GetFormNames(std::list<std::string>& formNames, std::list<std::string>& formFullNames)
540 {
541     std::unique_ptr<PtJson> modulesObj;
542     if (!GetModulesObject(modulesObj)) {
543         LOGE("GetModulesObject failed!");
544         return false;
545     }
546     if (!modulesObj->IsArray()) {
547         LOGE("Module node is not array!");
548         return false;
549     }
550     for (int32_t i = 0; i < modulesObj->GetSize(); i++) {
551         std::unique_ptr<PtJson> distroObj;
552         if (!GetDistroObjectByModuleObj(modulesObj->Get(i), distroObj)) {
553             LOGE("GetDistroObjectByModuleObj failed!");
554             return false;
555         }
556         std::string moduleName;
557         if (!GetModuleNameByDistroObj(distroObj, moduleName) || moduleName.empty()) {
558             LOGE("GetModuleNameByDistroObj failed or moduleName is empty!");
559             continue;
560         }
561         std::unique_ptr<PtJson> extensionAbilitiesObj;
562         if (!GetExtensionAbilitiesObjByModuleObj(modulesObj->Get(i), extensionAbilitiesObj)) {
563             LOGE("GetExtensionAbilitiesObjByModuleObj failed!");
564             return false;
565         }
566         if (!GetFormNamesByExtensionAbilitiesObj(extensionAbilitiesObj, moduleName, formNames, formFullNames)) {
567             LOGE("GetFormNamesByExtensionAbilitiesObj failed!");
568             return false;
569         }
570     }
571     return true;
572 }
573 
GetFormNamesByExtensionAbilitiesObj(const std::unique_ptr<PtJson> & extensionAbilitiesObj,std::string moduleName,std::list<std::string> & formNames,std::list<std::string> & formFullNames)574 bool PackInfo::GetFormNamesByExtensionAbilitiesObj(const std::unique_ptr<PtJson>& extensionAbilitiesObj,
575     std::string moduleName, std::list<std::string>& formNames, std::list<std::string>& formFullNames)
576 {
577     if (!extensionAbilitiesObj || !extensionAbilitiesObj->IsArray()) {
578         LOGE("ExtensionAbilities node is null or is not array!");
579         return false;
580     }
581     for (int32_t j = 0; j < extensionAbilitiesObj->GetSize(); j++) {
582         std::unique_ptr<PtJson> formsObj;
583         if (!GetFormsObjByExtensionAbilityObj(extensionAbilitiesObj->Get(j), formsObj)) {
584             LOGE("GetFormsObjByExtensionAbilityObj failed!");
585             return false;
586         }
587         if (!GetFormNamesByFormsObj(formsObj, moduleName, formNames, formFullNames)) {
588             LOGE("GetFormNamesByFormsObj failed!");
589             return false;
590         }
591     }
592     return true;
593 }
594 
595 
GetFormNamesByFormsObj(const std::unique_ptr<PtJson> & formsObj,std::string moduleName,std::list<std::string> & formNames,std::list<std::string> & formFullNames)596 bool PackInfo::GetFormNamesByFormsObj(const std::unique_ptr<PtJson>& formsObj,
597     std::string moduleName, std::list<std::string>& formNames, std::list<std::string>& formFullNames)
598 {
599     if (!formsObj || !formsObj->IsArray()) {
600         LOGE("Form node is null or is not array!");
601         return false;
602     }
603     for (int32_t k = 0; k < formsObj->GetSize(); k++) {
604         std::string formName;
605         std::string defaultDimension;
606         std::list<std::string> supportDimensions;
607         std::string formFullName;
608         if (!GetNameByFormObj(formsObj->Get(k), formName) || formName.empty()) {
609             continue;
610         }
611         formNames.push_back(formName);
612         if (!GetDefaultDimensionByFormObj(formsObj->Get(k), defaultDimension)) {
613             LOGE("GetDefaultDimensionByFormObj failed!");
614             return false;
615         }
616         if (!GetSupportDimensionsByFormObj(formsObj->Get(k), supportDimensions)) {
617             LOGE("GetSupportDimensionsByFormObj failed!");
618             return false;
619         }
620         for (std::string supportDimension : supportDimensions) {
621             formFullName = moduleName + "/" + formName + "-" + Utils::ReplaceAll(supportDimension, "*", "x");
622             formFullNames.push_back(formFullName);
623         }
624     }
625     return true;
626 }
627 
GetPackageNames(std::list<std::string> & packageNames)628 bool PackInfo::GetPackageNames(std::list<std::string> &packageNames)
629 {
630     std::unique_ptr<PtJson> packagesObj;
631     if (!GetPackagesObject(packagesObj)) {
632         LOGE("GetPackagesObject failed!");
633         return false;
634     }
635     return GetPackageNamesByPackagesObj(packagesObj, packageNames);
636 }
637 
GetPackageNamesByPackagesObj(const std::unique_ptr<PtJson> & packagesObj,std::list<std::string> & packageNames)638 bool PackInfo::GetPackageNamesByPackagesObj(const std::unique_ptr<PtJson>& packagesObj,
639     std::list<std::string> &packageNames)
640 {
641     if (!packagesObj || !packagesObj->IsArray()) {
642         LOGE("Packages node is null or is not array!");
643         return false;
644     }
645     for (int i = 0; i < packagesObj->GetSize(); i++) {
646         std::unique_ptr<PtJson> packageObj;
647         if (!GetPackageObject(i, packageObj)) {
648             LOGE("GetPackageObject failed!");
649             return false;
650         }
651         std::string name;
652         if (!GetNameByPackageObj(packageObj, name)) {
653             LOGE("GetNameByPackageObj failed!");
654             return false;
655         }
656         packageNames.push_back(name);
657     }
658     return true;
659 }
660 } // namespace AppPackingTool
661 } // namespace OHOS
662