• 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 "module_json.h"
17 
18 #include <fstream>
19 
20 #include "log.h"
21 #include "utils.h"
22 
23 namespace OHOS {
24 namespace AppPackingTool {
25 namespace {
26 const std::string APP = "app";
27 const std::string BUNDLE_TYPE = "bundleType";
28 const std::string ABILITIES = "abilities";
29 const std::string VERSIONCODE = "versionCode";
30 const std::string VERSIONNAME = "versionName";
31 const std::string MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode";
32 const std::string API_VERSION = "apiVersion";
33 const std::string MIN_API_VERSION = "minAPIVersion";
34 const std::string TARGET_API_VERSION = "targetAPIVersion";
35 const std::string API_RELEASE_TYPE = "apiReleaseType";
36 const std::string DEBUG = "debug";
37 const std::string COMPATIBLE = "compatible";
38 const std::string RELEASE_TYPE = "releaseType";
39 const std::string TARGET = "target";
40 const std::string VERSION = "version";
41 const std::string CODE = "code";
42 const std::string NAME = "name";
43 const std::string MODULE = "module";
44 const std::string MODULE_NAME = "moduleName";
45 const std::string MODULE_TYPE = "moduleType";
46 const std::string DISTRO = "distro";
47 const std::string PACKAGE = "package";
48 const std::string BUNDLE_NAME = "bundleName";
49 const std::string ENTRY = "entry";
50 const std::string DEVICE_TYPE = "deviceType";
51 const std::string DEVICE_TYPES = "deviceTypes";
52 const std::string TYPE = "type";
53 const std::string VENDOR = "vendor";
54 const std::string METADATA = "metadata";
55 const std::string RESOURCE = "resource";
56 const std::string PROFILE = "$profile:";
57 const std::string VALUE = "value";
58 const std::string JSON_PREFIX = ".json";
59 const std::string DISTRO_FILTER = "distroFilter";
60 const std::string DISTRIBUTION_FILTER = "distributionFilter";
61 const std::string DEPENDENCIES = "dependencies";
62 const std::string EXTENSION_ABILITIES = "extensionAbilities";
63 const std::string INSTALLATION_FREE = "installationFree";
64 const std::string COMPRESS_NATIVE_LIBS = "compressNativeLibs";
65 const std::string ASAN_ENABLED = "asanEnabled";
66 const std::string TSAN_ENABLED = "tsanEnabled";
67 const std::string ATOMIC_SERVICE = "atomicService";
68 const std::string PRELOADS = "preloads";
69 const std::string SHARED = "shared";
70 const std::string APP_SERVICE = "appService";
71 const std::string APP_PLUGIN = "appPlugin";
72 const std::string REQUEST_PERMISSIONS = "requestPermissions";
73 const std::string TARGET_MODULE_NAME = "targetModuleName";
74 const std::string TARGET_PRIORITY = "targetPriority";
75 const std::string TARGET_BUNDLE_NAME = "targetBundleName";
76 const std::string DEVICE_CONFIG = "deviceConfig";
77 const std::string DEFAULT = "default";
78 const std::string COMPILE_SDK_VERSION = "compileSdkVersion";
79 const std::string COMPILE_SDK_TYPE = "compileSdkType";
80 const std::string PROXY_DATAS = "proxyDatas";
81 const std::string PROXY_DATA = "proxyData";
82 const std::string PROXY_URI = "uri";
83 const std::string CONTINUE_TYPE = "continueType";
84 const std::string MULTI_APP_MODE = "multiAppMode";
85 const std::string MULTI_APP_MODE_TYPE = "multiAppModeType";
86 const std::string MULTI_APP_MODE_NUMBER = "maxCount";
87 const std::string GENERATE_BUILD_HASH = "generateBuildHash";
88 const std::string BUILD_HASH = "buildHash";
89 const std::string ASSET_ACCESS_GROUPS = "assetAccessGroups";
90 }
91 
ParseFromString(const std::string & jsonString)92 bool ModuleJson::ParseFromString(const std::string& jsonString)
93 {
94     Release();
95     if (jsonString.length() == 0) {
96         LOGE("Json length is zero!");
97         return false;
98     }
99     root_ = PtJson::Parse(jsonString);
100     return IsValid();
101 }
102 
ParseFromFile(const std::string & jsonFile)103 bool ModuleJson::ParseFromFile(const std::string& jsonFile)
104 {
105     Release();
106     std::string realJsonFile;
107     if (!Utils::GetRealPath(jsonFile, realJsonFile)) {
108         LOGE("get real json file failed! jsonFile=%s", jsonFile.c_str());
109         return false;
110     }
111     std::ifstream inFile(realJsonFile, std::ios::in);
112     if (!inFile.is_open()) {
113         LOGE("Open json file failed! jsonFile=%s, realJsonFile=%s", jsonFile.c_str(), realJsonFile.c_str());
114         return false;
115     }
116     std::string fileContent((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
117     inFile.close();
118     root_ = PtJson::Parse(fileContent);
119     return IsValid();
120 }
121 
ToString()122 std::string ModuleJson::ToString()
123 {
124     return root_->Stringify();
125 }
126 
Release()127 void ModuleJson::Release()
128 {
129     if (root_.get() != nullptr) {
130         root_->ReleaseRoot();
131         root_ = nullptr;
132     }
133 }
134 
IsValid()135 bool ModuleJson::IsValid()
136 {
137     return (root_.get() != nullptr);
138 }
139 
GetAppObject(std::unique_ptr<PtJson> & appObj)140 bool ModuleJson::GetAppObject(std::unique_ptr<PtJson>& appObj)
141 {
142     if (root_.get() == nullptr) {
143         LOGE("Json root is null!");
144         return false;
145     }
146     if (!root_->Contains(APP.c_str())) {
147         LOGE("Json root has no %s node!", APP.c_str());
148         return false;
149     }
150     if (root_->GetObject(APP.c_str(), &appObj) != Result::SUCCESS) {
151         LOGE("Json root get %s node failed!", APP.c_str());
152         return false;
153     }
154     return true;
155 }
156 
GetDeviceConfigObject(std::unique_ptr<PtJson> & deviceConfigObj)157 bool ModuleJson::GetDeviceConfigObject(std::unique_ptr<PtJson>& deviceConfigObj)
158 {
159     if (root_.get() == nullptr) {
160         LOGE("Json root is null!");
161         return false;
162     }
163     if (!root_->Contains(DEVICE_CONFIG.c_str())) {
164         LOGE("Json root has no %s node!", DEVICE_CONFIG.c_str());
165         return false;
166     }
167     if (root_->GetObject(DEVICE_CONFIG.c_str(), &deviceConfigObj) != Result::SUCCESS) {
168         LOGE("Json root get %s node failed!", DEVICE_CONFIG.c_str());
169         return false;
170     }
171     return true;
172 }
173 
GetVersionObject(std::unique_ptr<PtJson> & versionObj)174 bool ModuleJson::GetVersionObject(std::unique_ptr<PtJson>& versionObj)
175 {
176     std::unique_ptr<PtJson> appObj;
177     if (!GetAppObject(appObj)) {
178         LOGE("GetAppObject failed!");
179         return false;
180     }
181     if (!appObj->Contains(VERSION.c_str())) {
182         LOGE("App node has no %s node!", VERSION.c_str());
183         return false;
184     }
185     if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) {
186         LOGE("App node get %s node failed!", VERSION.c_str());
187         return false;
188     }
189     return true;
190 }
191 
GetModuleObject(std::unique_ptr<PtJson> & moduleObj)192 bool ModuleJson::GetModuleObject(std::unique_ptr<PtJson>& moduleObj)
193 {
194     if (root_.get() == nullptr) {
195         LOGE("Json root is null!");
196         return false;
197     }
198     if (!root_->Contains(MODULE.c_str())) {
199         LOGE("Json root has no %s node!", MODULE.c_str());
200         return false;
201     }
202     if (root_->GetObject(MODULE.c_str(), &moduleObj) != Result::SUCCESS) {
203         LOGE("Json root get %s node failed!", MODULE.c_str());
204         return false;
205     }
206     return true;
207 }
208 
209 
GetDistroObject(std::unique_ptr<PtJson> & distroObj)210 bool ModuleJson::GetDistroObject(std::unique_ptr<PtJson>& distroObj)
211 {
212     std::unique_ptr<PtJson> moduleObj;
213     if (!GetModuleObject(moduleObj)) {
214         LOGE("GetModuleObject failed!");
215         return false;
216     }
217     return GetDistroObjectByModuleObj(moduleObj, distroObj);
218 }
219 
GetDistroObjectByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::unique_ptr<PtJson> & distroObj)220 bool ModuleJson::GetDistroObjectByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::unique_ptr<PtJson>& distroObj)
221 {
222     if (!moduleObj) {
223         LOGE("Module node is null!");
224         return false;
225     }
226     if (!moduleObj->Contains(DISTRO.c_str())) {
227         LOGE("Module node has no %s node!", DISTRO.c_str());
228         return false;
229     }
230     if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
231         LOGE("Module node get %s node failed!", DISTRO.c_str());
232         return false;
233     }
234     return true;
235 }
236 
GetApiVersionObject(std::unique_ptr<PtJson> & apiVersionObj)237 bool ModuleJson::GetApiVersionObject(std::unique_ptr<PtJson>& apiVersionObj)
238 {
239     std::unique_ptr<PtJson> appObj;
240     if (!GetAppObject(appObj)) {
241         LOGE("GetAppObject failed!");
242         return false;
243     }
244     if (!appObj->Contains(API_VERSION.c_str())) {
245         LOGE("App node has no %s node!", API_VERSION.c_str());
246         return false;
247     }
248     if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
249         LOGE("App node get %s node failed!", API_VERSION.c_str());
250         return false;
251     }
252     return true;
253 }
254 
GetModuleName(std::string & moduleName)255 bool ModuleJson::GetModuleName(std::string& moduleName)
256 {
257     std::unique_ptr<PtJson> moduleObj;
258     if (!GetModuleObject(moduleObj)) {
259         LOGE("GetModuleObject failed!");
260         return false;
261     }
262     return GetModuleNameByModuleObj(moduleObj, moduleName);
263 }
264 
GetModuleNameByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & moduleName)265 bool ModuleJson::GetModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleName)
266 {
267     if (!moduleObj) {
268         LOGE("Module node is null!");
269         return false;
270     }
271     if (!moduleObj->Contains(NAME.c_str())) {
272         LOGE("Module node has no %s node!", NAME.c_str());
273         return false;
274     }
275     if (moduleObj->GetString(NAME.c_str(), &moduleName) != Result::SUCCESS) {
276         LOGE("Module node get %s failed!", NAME.c_str());
277         return false;
278     }
279     return true;
280 }
281 
GetPatchModuleName(std::string & patchModuleName)282 bool ModuleJson::GetPatchModuleName(std::string& patchModuleName)
283 {
284     std::unique_ptr<PtJson> moduleObj;
285     if (!GetModuleObject(moduleObj)) {
286         LOGE("GetModuleObject failed!");
287         return false;
288     }
289     if (!moduleObj->Contains(NAME.c_str())) {
290         LOGE("Module node has no %s node!", NAME.c_str());
291         return false;
292     }
293     if (moduleObj->GetString(NAME.c_str(), &patchModuleName) != Result::SUCCESS) {
294         LOGE("Module node get %s failed!", NAME.c_str());
295         return false;
296     }
297     return true;
298 }
299 
GetBundleName(std::string & bundleName)300 bool ModuleJson::GetBundleName(std::string& bundleName)
301 {
302     std::unique_ptr<PtJson> appObj;
303     if (!GetAppObject(appObj)) {
304         LOGE("GetAppObject failed!");
305         return false;
306     }
307     return GetBundleNameByAppObj(appObj, bundleName);
308 }
309 
GetBundleNameByAppObj(std::unique_ptr<PtJson> & appObj,std::string & bundleName)310 bool ModuleJson::GetBundleNameByAppObj(std::unique_ptr<PtJson>& appObj, std::string& bundleName)
311 {
312     if (!appObj) {
313         LOGE("App node is null!");
314         return false;
315     }
316     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
317         LOGE("App node has no %s node!", BUNDLE_NAME.c_str());
318         return false;
319     }
320     if (appObj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) {
321         LOGE("App node get %s failed!", BUNDLE_NAME.c_str());
322         return false;
323     }
324     return true;
325 }
326 
SetBundleName(const std::string & bundleName)327 bool ModuleJson::SetBundleName(const std::string& bundleName)
328 {
329     std::unique_ptr<PtJson> appObj;
330     if (!GetAppObject(appObj)) {
331         LOGE("GetAppObject failed!");
332         return false;
333     }
334     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
335         LOGE("App node has no %s node!", BUNDLE_NAME.c_str());
336         return false;
337     }
338     if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) {
339         LOGE("App node set %s failed!", BUNDLE_NAME.c_str());
340         return false;
341     }
342     return true;
343 }
344 
GetVendor(std::string & vendor)345 bool ModuleJson::GetVendor(std::string& vendor)
346 {
347     std::unique_ptr<PtJson> appObj;
348     if (!GetAppObject(appObj)) {
349         LOGE("GetAppObject failed!");
350         return false;
351     }
352     return GetVendorByAppObj(appObj, vendor);
353 }
354 
GetVendorByAppObj(std::unique_ptr<PtJson> & appObj,std::string & vendor)355 bool ModuleJson::GetVendorByAppObj(std::unique_ptr<PtJson>& appObj, std::string& vendor)
356 {
357     if (!appObj) {
358         LOGE("App node is null!");
359         return false;
360     }
361     if (appObj->Contains(VENDOR.c_str())) {
362         if (appObj->GetString(VENDOR.c_str(), &vendor) != Result::SUCCESS) {
363             LOGE("App node get %s failed!", VENDOR.c_str());
364             return false;
365         }
366     } else {
367         vendor = "";
368     }
369     return true;
370 }
371 
GetTargetBundleName(std::string & targetBundleName)372 bool ModuleJson::GetTargetBundleName(std::string& targetBundleName)
373 {
374     std::unique_ptr<PtJson> appObj;
375     if (!GetAppObject(appObj)) {
376         LOGE("GetAppObject failed!");
377         return false;
378     }
379     return GetTargetBundleNameByAppObj(appObj, targetBundleName);
380 }
381 
GetTargetBundleNameByAppObj(std::unique_ptr<PtJson> & appObj,std::string & targetBundleName)382 bool ModuleJson::GetTargetBundleNameByAppObj(std::unique_ptr<PtJson>& appObj, std::string& targetBundleName)
383 {
384     if (!appObj) {
385         LOGE("App node is null!");
386         return false;
387     }
388     if (appObj->Contains(TARGET_BUNDLE_NAME.c_str())) {
389         if (appObj->GetString(TARGET_BUNDLE_NAME.c_str(), &targetBundleName) != Result::SUCCESS) {
390             LOGE("App node get %s failed!", TARGET_BUNDLE_NAME.c_str());
391             return false;
392         }
393     } else {
394         targetBundleName = "";
395     }
396     return true;
397 }
398 
GetTargetPriority(int32_t & targetPriority)399 bool ModuleJson::GetTargetPriority(int32_t& targetPriority)
400 {
401     std::unique_ptr<PtJson> appObj;
402     if (!GetAppObject(appObj)) {
403         LOGE("GetAppObject failed!");
404         return false;
405     }
406     return GetTargetPriorityByAppObj(appObj, targetPriority);
407 }
408 
GetTargetPriorityByAppObj(std::unique_ptr<PtJson> & appObj,int32_t & targetPriority)409 bool ModuleJson::GetTargetPriorityByAppObj(std::unique_ptr<PtJson>& appObj, int32_t& targetPriority)
410 {
411     if (!appObj) {
412         LOGE("App node is null!");
413         return false;
414     }
415     if (appObj->Contains(TARGET_PRIORITY.c_str())) {
416         if (appObj->GetInt(TARGET_PRIORITY.c_str(), &targetPriority) != Result::SUCCESS) {
417             LOGE("App node get %s failed!", TARGET_PRIORITY.c_str());
418             return false;
419         }
420     } else {
421         targetPriority = 0;
422     }
423     return true;
424 }
425 
GetTargetModuleName(std::string & targetModuleName)426 bool ModuleJson::GetTargetModuleName(std::string& targetModuleName)
427 {
428     std::unique_ptr<PtJson> moduleObj;
429     if (!GetModuleObject(moduleObj)) {
430         LOGE("GetModuleObject failed!");
431         return false;
432     }
433     return GetTargetModuleNameByModuleObj(moduleObj, targetModuleName);
434 }
435 
GetTargetModuleNameByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & targetModuleName)436 bool ModuleJson::GetTargetModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& targetModuleName)
437 {
438     if (!moduleObj) {
439         LOGE("Module node is null!");
440         return false;
441     }
442     if (moduleObj->Contains(TARGET_MODULE_NAME.c_str())) {
443         if (moduleObj->GetString(TARGET_MODULE_NAME.c_str(), &targetModuleName) != Result::SUCCESS) {
444             LOGE("Module node get %s failed!", TARGET_MODULE_NAME.c_str());
445             return false;
446         }
447     } else {
448         targetModuleName = "";
449     }
450     return true;
451 }
452 
GetTargetModulePriority(int32_t & targetModulePriority)453 bool ModuleJson::GetTargetModulePriority(int32_t& targetModulePriority)
454 {
455     std::unique_ptr<PtJson> moduleObj;
456     if (!GetModuleObject(moduleObj)) {
457         LOGE("GetModuleObject failed!");
458         return false;
459     }
460     return GetTargetModulePriorityByModuleObj(moduleObj, targetModulePriority);
461 }
462 
GetTargetModulePriorityByModuleObj(std::unique_ptr<PtJson> & moduleObj,int32_t & targetModulePriority)463 bool ModuleJson::GetTargetModulePriorityByModuleObj(std::unique_ptr<PtJson>& moduleObj, int32_t& targetModulePriority)
464 {
465     if (!moduleObj) {
466         LOGE("Module node is null!");
467         return false;
468     }
469     if (moduleObj->Contains(TARGET_PRIORITY.c_str())) {
470         if (moduleObj->GetInt(TARGET_PRIORITY.c_str(), &targetModulePriority) != Result::SUCCESS) {
471             LOGE("Module node get %s failed!", TARGET_PRIORITY.c_str());
472             return false;
473         }
474     } else {
475         targetModulePriority = 0;
476     }
477     return true;
478 }
479 
480 // parseModuleMetadata
GetModuleMetadatas(std::list<ModuleMetadataInfo> & moduleMetadataInfos,const std::map<std::string,std::string> & resourceMap)481 bool ModuleJson::GetModuleMetadatas(std::list<ModuleMetadataInfo>& moduleMetadataInfos,
482     const std::map<std::string, std::string>& resourceMap)
483 {
484     std::unique_ptr<PtJson> moduleObj;
485     if (!GetModuleObject(moduleObj)) {
486         LOGE("GetModuleObject failed!");
487         return false;
488     }
489     return GetModuleMetadatasByModuleObj(moduleObj, resourceMap, moduleMetadataInfos);
490 }
491 
GetModuleMetadataInfoByModuleMetadataInfoObj(std::unique_ptr<PtJson> & moduleMetadataInfoObj,const std::map<std::string,std::string> & resourceMap,ModuleMetadataInfo & moduleMetadataInfo)492 bool ModuleJson::GetModuleMetadataInfoByModuleMetadataInfoObj(std::unique_ptr<PtJson>& moduleMetadataInfoObj,
493     const std::map<std::string, std::string>& resourceMap, ModuleMetadataInfo& moduleMetadataInfo)
494 {
495     if (!moduleMetadataInfoObj) {
496         LOGE("ModuleMetadataInfo node is null!");
497         return false;
498     }
499     if (moduleMetadataInfoObj->Contains(NAME.c_str())) {
500         if (moduleMetadataInfoObj->GetString(NAME.c_str(), &moduleMetadataInfo.name) != Result::SUCCESS) {
501             LOGE("ModuleMetadataInfo node get %s failed!", NAME.c_str());
502             return false;
503         }
504     }
505     if (moduleMetadataInfoObj->Contains(VALUE.c_str())) {
506         if (moduleMetadataInfoObj->GetString(VALUE.c_str(), &moduleMetadataInfo.value) != Result::SUCCESS) {
507             LOGE("ModuleMetadataInfo node get %s failed!", VALUE.c_str());
508             return false;
509         }
510     }
511     if (moduleMetadataInfoObj->Contains(RESOURCE.c_str())) {
512         std::string resource;
513         if (moduleMetadataInfoObj->GetString(RESOURCE.c_str(), &resource) != Result::SUCCESS) {
514             LOGE("ModuleMetadataInfo node get %s failed!", RESOURCE.c_str());
515             return false;
516         }
517         std::string fileName = Utils::ReplaceAll(resource, PROFILE, "") + JSON_PREFIX;
518         auto iter = resourceMap.find(fileName);
519         if (iter == resourceMap.end()) {
520             LOGE("find filename in resourceMap failed!");
521             return false;
522         }
523         moduleMetadataInfo.resource = iter->second;
524     }
525     return true;
526 }
527 
GetModuleMetadatasByModuleObj(std::unique_ptr<PtJson> & moduleObj,const std::map<std::string,std::string> & resourceMap,std::list<ModuleMetadataInfo> & moduleMetadataInfos)528 bool ModuleJson::GetModuleMetadatasByModuleObj(std::unique_ptr<PtJson>& moduleObj,
529     const std::map<std::string, std::string>& resourceMap, std::list<ModuleMetadataInfo>& moduleMetadataInfos)
530 {
531     if (!moduleObj) {
532         LOGE("Module node is null!");
533         return false;
534     }
535     std::unique_ptr<PtJson> moduleMetadataInfosObj;
536     if (moduleObj->Contains(METADATA.c_str())) {
537         if (moduleObj->GetArray(METADATA.c_str(), &moduleMetadataInfosObj) != Result::SUCCESS) {
538             LOGE("Module node get %s array node failed!", METADATA.c_str());
539             return false;
540         }
541         for (int32_t i = 0; i < moduleMetadataInfosObj->GetSize(); i++) {
542             ModuleMetadataInfo moduleMetadataInfo;
543             std::unique_ptr<PtJson> moduleMetadataInfoObj = moduleMetadataInfosObj->Get(i);
544             if (!GetModuleMetadataInfoByModuleMetadataInfoObj(moduleMetadataInfoObj, resourceMap,
545                 moduleMetadataInfo)) {
546                 LOGE("GetModuleMetadataInfoByModuleMetadataInfoObj failed!");
547                 return false;
548             }
549             moduleMetadataInfos.push_back(moduleMetadataInfo);
550         }
551     }
552     return true;
553 }
554 
ParseModuleMetadatasToDistroFilter(const std::list<ModuleMetadataInfo> & moduleMetadataInfos,DistroFilter & distroFilter)555 bool ModuleJson::ParseModuleMetadatasToDistroFilter(const std::list<ModuleMetadataInfo>& moduleMetadataInfos,
556     DistroFilter& distroFilter)
557 {
558     for (auto& moduleMetadataInfo : moduleMetadataInfos) {
559         if (moduleMetadataInfo.resource.empty()) {
560             continue;
561         }
562         std::unique_ptr<PtJson> distroFilterJsonObj = PtJson::Parse(moduleMetadataInfo.resource);
563         if (!distroFilterJsonObj) {
564             LOGE("DistroFilter node is null!");
565             return false;
566         }
567         std::unique_ptr<PtJson> distroFilterObj;
568         if (distroFilterJsonObj->Contains(DISTRIBUTION_FILTER.c_str())) {
569             if (distroFilterJsonObj->GetObject(DISTRIBUTION_FILTER.c_str(), &distroFilterObj) != Result::SUCCESS) {
570                 LOGE("DistroFilter node get %s failed!", DISTRO_FILTER.c_str());
571                 return false;
572             }
573         } else if (distroFilterJsonObj->Contains(DISTRO_FILTER.c_str())) {
574             if (distroFilterJsonObj->GetObject(DISTRO_FILTER.c_str(), &distroFilterObj) != Result::SUCCESS) {
575                 LOGE("DistroFilter node get %s failed!", DISTRO_FILTER.c_str());
576                 return false;
577             }
578         }
579         if (!distroFilter.ParseFromJson(distroFilterObj)) {
580             LOGE("Parse distro filter failed!");
581             return false;
582         }
583     }
584     return true;
585 }
586 
GetAbilityNames(std::list<std::string> & abilityNames)587 bool ModuleJson::GetAbilityNames(std::list<std::string>& abilityNames)
588 {
589     std::unique_ptr<PtJson> moduleObj;
590     if (!GetModuleObject(moduleObj)) {
591         LOGE("GetModuleObject failed!");
592         return false;
593     }
594     return GetAbilityNamesByModuleObj(moduleObj, abilityNames);
595 }
596 
GetAbilityNamesByAbilitiesObj(std::unique_ptr<PtJson> & abilitiesObj,std::list<std::string> & abilityNames)597 bool ModuleJson::GetAbilityNamesByAbilitiesObj(std::unique_ptr<PtJson>& abilitiesObj,
598     std::list<std::string>& abilityNames)
599 {
600     if (!abilitiesObj) {
601         LOGE("Abilities node is null!");
602         return false;
603     }
604     for (int32_t i = 0; i < abilitiesObj->GetSize(); i++) {
605         std::unique_ptr<PtJson> abilityObj = abilitiesObj->Get(i);
606         if (abilityObj->Contains(NAME.c_str())) {
607             std::string name;
608             if (abilityObj->GetString(NAME.c_str(), &name) != Result::SUCCESS) {
609                 LOGE("Ability node get %s failed!", NAME.c_str());
610                 return false;
611             }
612             abilityNames.push_back(name);
613         }
614     }
615     return true;
616 }
617 
GetAbilityNamesByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & abilityNames)618 bool ModuleJson::GetAbilityNamesByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& abilityNames)
619 {
620     if (!moduleObj) {
621         LOGE("Module node is null!");
622         return false;
623     }
624     if (moduleObj->Contains(ABILITIES.c_str())) {
625         std::unique_ptr<PtJson> abilitiesObj;
626         if (moduleObj->GetArray(ABILITIES.c_str(), &abilitiesObj) != Result::SUCCESS) {
627             LOGE("Module node get %s array node failed!", ABILITIES.c_str());
628             return false;
629         }
630         if (!GetAbilityNamesByAbilitiesObj(abilitiesObj, abilityNames)) {
631             LOGE("GetAbilityNamesByAbilitiesObj failed!");
632             return false;
633         }
634     }
635     return true;
636 }
637 
GetProxyDataUris(std::list<std::string> & proxyDataUris)638 bool ModuleJson::GetProxyDataUris(std::list<std::string>& proxyDataUris)
639 {
640     std::unique_ptr<PtJson> moduleObj;
641     if (!GetModuleObject(moduleObj)) {
642         LOGE("GetModuleObject failed!");
643         return false;
644     }
645     return GetProxyDataUrisByModuleObj(moduleObj, proxyDataUris);
646 }
647 
GetProxyDataUrisByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & proxyDataUris)648 bool ModuleJson::GetProxyDataUrisByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& proxyDataUris)
649 {
650     if (!moduleObj) {
651         LOGE("Module node is null!");
652         return false;
653     }
654     if (moduleObj->Contains(PROXY_DATAS.c_str())) {
655         std::unique_ptr<PtJson> proxyDatasObj;
656         if (moduleObj->GetArray(PROXY_DATAS.c_str(), &proxyDatasObj) != Result::SUCCESS) {
657             LOGE("Module node get %s array node failed!", PROXY_DATAS.c_str());
658             return false;
659         }
660         if (!GetProxyDataUrisByProxyDatasObj(proxyDatasObj, proxyDataUris)) {
661             LOGE("GetProxyDataUrisByProxyDatasObj failed!");
662             return false;
663         }
664     } else if (moduleObj->Contains(PROXY_DATA.c_str())) {
665         std::unique_ptr<PtJson> proxyDatasObj;
666         if (moduleObj->GetArray(PROXY_DATA.c_str(), &proxyDatasObj) != Result::SUCCESS) {
667             LOGE("Module node get %s array node failed!", PROXY_DATA.c_str());
668             return false;
669         }
670         if (!GetProxyDataUrisByProxyDatasObj(proxyDatasObj, proxyDataUris)) {
671             LOGE("GetProxyDataUrisByProxyDatasObj failed!");
672             return false;
673         }
674     }
675     return true;
676 }
677 
GetProxyDataUrisByProxyDatasObj(std::unique_ptr<PtJson> & proxyDatasObj,std::list<std::string> & proxyDataUris)678 bool ModuleJson::GetProxyDataUrisByProxyDatasObj(std::unique_ptr<PtJson>& proxyDatasObj,
679     std::list<std::string>& proxyDataUris)
680 {
681     if (!proxyDatasObj || !proxyDatasObj->IsArray()) {
682         LOGE("ProxyData node is null or is not array!");
683         return false;
684     }
685     for (int32_t i = 0; i < proxyDatasObj->GetSize(); i++) {
686         std::unique_ptr<PtJson> proxyDataObj = proxyDatasObj->Get(i);
687         if (!proxyDataObj->Contains(PROXY_URI.c_str())) {
688             LOGE("proxyData node has no %s node!", PROXY_URI.c_str());
689             return false;
690         }
691         std::string proxyUri;
692         if (proxyDataObj->GetString(PROXY_URI.c_str(), &proxyUri) != Result::SUCCESS) {
693             LOGE("ProxyData node get %s failed!", PROXY_URI.c_str());
694             return false;
695         }
696         proxyDataUris.push_back(proxyUri);
697     }
698     return true;
699 }
700 
GetAssetAccessGroups(std::list<std::string> & assetAccessGroups)701 bool ModuleJson::GetAssetAccessGroups(std::list<std::string>& assetAccessGroups)
702 {
703     std::unique_ptr<PtJson> moduleObj;
704     if (!GetModuleObject(moduleObj)) {
705         LOGE("GetModuleObject failed!");
706         return false;
707     }
708     return GetAssetAccessGroupsByModuleObj(moduleObj, assetAccessGroups);
709 }
710 
GetAssetAccessGroupsByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & assetAccessGroups)711 bool ModuleJson::GetAssetAccessGroupsByModuleObj(std::unique_ptr<PtJson>& moduleObj,
712     std::list<std::string>& assetAccessGroups)
713 {
714     if (!moduleObj) {
715         LOGE("Module node is null!");
716         return false;
717     }
718     if (moduleObj->Contains(ASSET_ACCESS_GROUPS.c_str())) {
719         std::unique_ptr<PtJson> assetAccessGroupObj;
720         if (moduleObj->GetArray(ASSET_ACCESS_GROUPS.c_str(), &assetAccessGroupObj) != Result::SUCCESS) {
721             LOGE("Module node get %s array node failed!", ASSET_ACCESS_GROUPS.c_str());
722             return false;
723         }
724         for (int32_t i = 0; i < assetAccessGroupObj->GetSize(); i++) {
725             assetAccessGroups.push_back(assetAccessGroupObj->Get(i)->GetString());
726         }
727     }
728     return true;
729 }
730 
GetExtensionAbilityNames(std::list<std::string> & extensionAbilityNames)731 bool ModuleJson::GetExtensionAbilityNames(std::list<std::string>& extensionAbilityNames)
732 {
733     std::unique_ptr<PtJson> moduleObj;
734     if (!GetModuleObject(moduleObj)) {
735         LOGE("GetModuleObject failed!");
736         return false;
737     }
738     return GetAbilityNamesByModuleObj(moduleObj, extensionAbilityNames);
739 }
740 
GetExtensionAbilityNamesByExtensionAbilityObj(std::unique_ptr<PtJson> & extensionAbilitiesObj,std::list<std::string> & extensionAbilityNames)741 bool ModuleJson::GetExtensionAbilityNamesByExtensionAbilityObj(std::unique_ptr<PtJson>& extensionAbilitiesObj,
742     std::list<std::string>& extensionAbilityNames)
743 {
744     if (!extensionAbilitiesObj) {
745         LOGE("ExtensionAbilities node is null!");
746         return false;
747     }
748     for (int32_t i = 0; i < extensionAbilitiesObj->GetSize(); i++) {
749         std::unique_ptr<PtJson> extensionAbilityObj = extensionAbilitiesObj->Get(i);
750         if (extensionAbilityObj->Contains(NAME.c_str())) {
751             std::string extensionAbilityName;
752             if (extensionAbilityObj->GetString(NAME.c_str(), &extensionAbilityName) != Result::SUCCESS) {
753                 LOGE("ExtensionAbility node get %s failed!", NAME.c_str());
754                 return false;
755             }
756             extensionAbilityNames.push_back(extensionAbilityName);
757         }
758     }
759     return true;
760 }
761 
GetExtensionAbilityNamesByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & extensionAbilityNames)762 bool ModuleJson::GetExtensionAbilityNamesByModuleObj(std::unique_ptr<PtJson>& moduleObj,
763     std::list<std::string>& extensionAbilityNames)
764 {
765     if (!moduleObj) {
766         LOGE("Module node is null!");
767         return false;
768     }
769     if (moduleObj->Contains(EXTENSION_ABILITIES.c_str())) {
770         std::unique_ptr<PtJson> extensionAbilitiesObj;
771         if (moduleObj->GetArray(EXTENSION_ABILITIES.c_str(), &extensionAbilitiesObj) != Result::SUCCESS) {
772             LOGE("Module node get %s array node failed!", EXTENSION_ABILITIES.c_str());
773             return false;
774         }
775         if (!GetExtensionAbilityNamesByExtensionAbilityObj(extensionAbilitiesObj, extensionAbilityNames)) {
776             LOGE("GetExtensionAbilityNamesByExtensionAbilityObj failed!");
777             return false;
778         }
779     }
780     return true;
781 }
782 
GetDependencyItems(std::list<DependencyItem> & dependencyItems,const std::string & defaultBundleName)783 bool ModuleJson::GetDependencyItems(std::list<DependencyItem>& dependencyItems, const std::string& defaultBundleName)
784 {
785     std::unique_ptr<PtJson> moduleObj;
786     if (!GetModuleObject(moduleObj)) {
787         LOGE("GetModuleObject failed!");
788         return false;
789     }
790     return GetDependencyItemsByModuleObj(moduleObj, dependencyItems, defaultBundleName);
791 }
792 
GetDependencyBundleNameByDependencyItemObj(std::unique_ptr<PtJson> & dependencyItemObj,std::string & bundleName,const std::string & defaultBundleName)793 bool ModuleJson::GetDependencyBundleNameByDependencyItemObj(std::unique_ptr<PtJson>& dependencyItemObj,
794     std::string& bundleName, const std::string& defaultBundleName)
795 {
796     if (!dependencyItemObj) {
797         LOGE("dependencyItem node is null!");
798         return false;
799     }
800     if (dependencyItemObj->Contains(BUNDLE_NAME.c_str())) {
801         if (dependencyItemObj->GetString(BUNDLE_NAME.c_str(), &bundleName) != Result::SUCCESS) {
802             LOGE("DependencyItem node get %s failed!", BUNDLE_NAME.c_str());
803             return false;
804         }
805     } else {
806         bundleName = defaultBundleName;
807     }
808     return true;
809 }
810 
GetDependencyModuleNameByDependencyItemObj(std::unique_ptr<PtJson> & dependencyItemObj,std::string & moduleName)811 bool ModuleJson::GetDependencyModuleNameByDependencyItemObj(std::unique_ptr<PtJson>& dependencyItemObj,
812     std::string& moduleName)
813 {
814     if (!dependencyItemObj) {
815         LOGE("dependencyItem node is null!");
816         return false;
817     }
818     moduleName = "";
819     if (dependencyItemObj->Contains(MODULE_NAME.c_str())) {
820         if (dependencyItemObj->GetString(MODULE_NAME.c_str(), &moduleName) != Result::SUCCESS) {
821             LOGE("DependencyItem node get %s failed!", MODULE_NAME.c_str());
822             return false;
823         }
824     }
825     return true;
826 }
827 
GetDependencyItemsByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<DependencyItem> & dependencyItems,const std::string & defaultBundleName)828 bool ModuleJson::GetDependencyItemsByModuleObj(std::unique_ptr<PtJson>& moduleObj,
829     std::list<DependencyItem>& dependencyItems, const std::string& defaultBundleName)
830 {
831     if (!moduleObj) {
832         LOGE("Module node is null!");
833         return false;
834     }
835     if (moduleObj->Contains(DEPENDENCIES.c_str())) {
836         std::unique_ptr<PtJson> dependencyItemsObj;
837         if (moduleObj->GetArray(DEPENDENCIES.c_str(), &dependencyItemsObj) != Result::SUCCESS) {
838             LOGE("Module node get %s array node failed!", DEPENDENCIES.c_str());
839             return false;
840         }
841         for (int32_t i = 0; i < dependencyItemsObj->GetSize(); i++) {
842             std::unique_ptr<PtJson> dependencyItemObj = dependencyItemsObj->Get(i);
843             DependencyItem dependencyItem;
844             if (!GetDependencyBundleNameByDependencyItemObj(dependencyItemObj, dependencyItem.bundleName,
845                 defaultBundleName)) {
846                 LOGE("GetDependencyBundleNameByDependencyItemObj failed!");
847                 return false;
848             }
849             if (!GetDependencyModuleNameByDependencyItemObj(dependencyItemObj, dependencyItem.moduleName)) {
850                 LOGE("GetDependencyModuleNameByDependencyItemObj failed!");
851                 return false;
852             }
853             dependencyItems.push_back(dependencyItem);
854         }
855     }
856 
857     return true;
858 }
859 
CheckStageBundleType(const std::string & moduleName,const std::string & moduleType,const std::string & bundleType,const bool & installationFree)860 bool ModuleJson::CheckStageBundleType(const std::string& moduleName, const std::string& moduleType,
861     const std::string& bundleType, const bool& installationFree)
862 {
863     if (bundleType.compare(APP) == 0) {
864         if (installationFree) {
865             LOGE("installationFree must be false in module %s when bundleType is app", moduleName.c_str());
866             return false;
867         }
868         return true;
869     } else if (bundleType.compare(ATOMIC_SERVICE) == 0) {
870         if (!installationFree) {
871             LOGE("installationfree must be true in module %s when bundleType is atomicService",
872                 moduleName.c_str());
873             return false;
874         }
875         return true;
876     } else if (bundleType.compare(SHARED) == 0) {
877         if (moduleType.compare(SHARED) != 0) {
878             LOGE("moduleType must be shared bundleType is shared[bundleType=%s][moduleType=%s]",
879                 bundleType.c_str(), moduleType.c_str());
880             return false;
881         }
882         return true;
883     } else if (bundleType.compare(APP_SERVICE) == 0) {
884         return true;
885     } else if (bundleType.compare(APP_PLUGIN) == 0) {
886         if (moduleType.compare(SHARED) != 0) {
887             LOGE("moduleType must be shared bundleType is appPlugin[bundleType=%s][moduleType=%s]",
888                 bundleType.c_str(), moduleType.c_str());
889             return false;
890         }
891         return true;
892     }
893     return false;
894 }
895 
GetAtomicServicePreloads(std::list<PreloadItem> & preloadItems)896 bool ModuleJson::GetAtomicServicePreloads(std::list<PreloadItem>& preloadItems)
897 {
898     std::unique_ptr<PtJson> moduleObj;
899     if (!GetModuleObject(moduleObj)) {
900         LOGE("GetModuleObject failed!");
901         return false;
902     }
903     return GetAtomicServicePreloadsByModuleObj(moduleObj, preloadItems);
904 }
905 
GetAtomicServicePreloadsByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<PreloadItem> & preloadItems)906 bool ModuleJson::GetAtomicServicePreloadsByModuleObj(std::unique_ptr<PtJson>& moduleObj,
907     std::list<PreloadItem>& preloadItems)
908 {
909     if (!moduleObj) {
910         LOGE("Module node is null!");
911         return false;
912     }
913 
914     if (!moduleObj->Contains(ATOMIC_SERVICE.c_str())) {
915         return true;
916     }
917     std::unique_ptr<PtJson> atomicServiceObj;
918     if (moduleObj->GetObject(ATOMIC_SERVICE.c_str(), &atomicServiceObj) != Result::SUCCESS) {
919         LOGE("Module node get %s node failed!", ATOMIC_SERVICE.c_str());
920         return false;
921     }
922 
923     if (!atomicServiceObj->Contains(PRELOADS.c_str())) {
924         return true;
925     }
926     std::unique_ptr<PtJson> preloadsObj;
927     if (atomicServiceObj->GetArray(PRELOADS.c_str(), &preloadsObj) != Result::SUCCESS) {
928         LOGE("Module node get %s array node failed!", PRELOADS.c_str());
929         return false;
930     }
931     for (int32_t i = 0; i < preloadsObj->GetSize(); i++) {
932         std::unique_ptr<PtJson> preloadObj = preloadsObj->Get(i);
933         PreloadItem preloadItem;
934         std::string moduleName;
935         if (preloadObj->Contains(MODULE_NAME.c_str())) {
936             if (preloadObj->GetString(MODULE_NAME.c_str(), &moduleName) == Result::SUCCESS) {
937                 preloadItem.moduleName = moduleName;
938             }
939         }
940         preloadItems.push_back(preloadItem);
941     }
942 
943     return true;
944 }
945 
GetAbilityContinueTypeMap(std::map<std::string,std::list<std::string>> & abilityContinueTypeMap)946 bool ModuleJson::GetAbilityContinueTypeMap(std::map<std::string, std::list<std::string>>& abilityContinueTypeMap)
947 {
948     std::unique_ptr<PtJson> moduleObj;
949     if (!GetModuleObject(moduleObj)) {
950         LOGE("GetModuleObject failed!");
951         return false;
952     }
953     return GetAbilityContinueTypeMapByModuleObj(moduleObj, abilityContinueTypeMap);
954 }
955 
GetAbilityNameByAbilityObj(std::unique_ptr<PtJson> & abilityObj,std::string & abilityName)956 bool ModuleJson::GetAbilityNameByAbilityObj(std::unique_ptr<PtJson>& abilityObj,
957     std::string& abilityName)
958 {
959     if (!abilityObj) {
960         LOGE("Ability node is null!");
961         return false;
962     }
963     abilityName = "";
964     if (abilityObj->Contains(NAME.c_str())) {
965         if (abilityObj->GetString(NAME.c_str(), &abilityName) != Result::SUCCESS) {
966             LOGE("Ability node get %s failed!", NAME.c_str());
967             return false;
968         }
969     }
970     return true;
971 }
972 
GetContinueTypesByAbilityObj(std::unique_ptr<PtJson> & abilityObj,std::list<std::string> & continueTypes)973 bool ModuleJson::GetContinueTypesByAbilityObj(std::unique_ptr<PtJson>& abilityObj,
974     std::list<std::string>& continueTypes)
975 {
976     if (!abilityObj) {
977         LOGE("Ability node is null!");
978         return false;
979     }
980     if (abilityObj->Contains(CONTINUE_TYPE.c_str())) {
981         std::unique_ptr<PtJson> continueTypeObj;
982         if (abilityObj->GetArray(CONTINUE_TYPE.c_str(), &continueTypeObj) != Result::SUCCESS) {
983             LOGE("Module node get %s array node failed!", CONTINUE_TYPE.c_str());
984             return false;
985         }
986         for (int32_t i = 0; i < continueTypeObj->GetSize(); i++) {
987             continueTypes.push_back(continueTypeObj->Get(i)->GetString());
988         }
989     }
990     return true;
991 }
992 
GetAbilityContinueTypeMapByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::map<std::string,std::list<std::string>> & abilityContinueTypeMap)993 bool ModuleJson::GetAbilityContinueTypeMapByModuleObj(std::unique_ptr<PtJson>& moduleObj,
994     std::map<std::string, std::list<std::string>>& abilityContinueTypeMap)
995 {
996     if (!moduleObj) {
997         LOGE("Module node is null!");
998         return false;
999     }
1000     if (moduleObj->Contains(ABILITIES.c_str())) {
1001         std::unique_ptr<PtJson> abilitysObj;
1002         if (moduleObj->GetArray(ABILITIES.c_str(), &abilitysObj) != Result::SUCCESS) {
1003             LOGE("Module node get %s array node failed!", PRELOADS.c_str());
1004             return false;
1005         }
1006         for (int32_t i = 0; i < abilitysObj->GetSize(); i++) {
1007             std::unique_ptr<PtJson> abilityObj = abilitysObj->Get(i);
1008             std::string abilityName = "";
1009             if (!GetAbilityNameByAbilityObj(abilityObj, abilityName)) {
1010                 LOGE("GetAbilityNameByAbilityObj failed!");
1011                 return false;
1012             }
1013             std::list<std::string> continueTypes;
1014             if (!GetContinueTypesByAbilityObj(abilityObj, continueTypes)) {
1015                 LOGE("GetContinueTypesByAbilityObj failed!");
1016                 return false;
1017             }
1018             abilityContinueTypeMap.emplace(abilityName, continueTypes);
1019         }
1020     }
1021     return true;
1022 }
1023 
GetMultiAppMode(MultiAppMode & multiAppMode)1024 bool ModuleJson::GetMultiAppMode(MultiAppMode& multiAppMode)
1025 {
1026     std::unique_ptr<PtJson> appObj;
1027     if (!GetAppObject(appObj)) {
1028         LOGE("GetAppObject failed!");
1029         return false;
1030     }
1031     return GetMultiAppModeByAppObj(appObj, multiAppMode);
1032 }
GetMultiAppModeByAppObj(std::unique_ptr<PtJson> & appObj,MultiAppMode & multiAppMode)1033 bool ModuleJson::GetMultiAppModeByAppObj(std::unique_ptr<PtJson>& appObj, MultiAppMode& multiAppMode)
1034 {
1035     if (!appObj) {
1036         LOGE("App node is null!");
1037         return false;
1038     }
1039     if (appObj->Contains(MULTI_APP_MODE.c_str())) {
1040         std::unique_ptr<PtJson> multiAppModeObj;
1041         if (appObj->GetObject(MULTI_APP_MODE.c_str(), &multiAppModeObj) != Result::SUCCESS) {
1042             LOGE("App node get %s node failed!", MULTI_APP_MODE.c_str());
1043             return false;
1044         }
1045         multiAppMode.multiAppModeType = "";
1046         multiAppMode.maxCount = 0;
1047         if (multiAppModeObj->Contains(MULTI_APP_MODE_TYPE.c_str())) {
1048             if (multiAppModeObj->GetString(MULTI_APP_MODE_TYPE.c_str(),
1049                 &multiAppMode.multiAppModeType) != Result::SUCCESS) {
1050                 LOGE("App node get %s failed!", MULTI_APP_MODE_TYPE.c_str());
1051                 return false;
1052             }
1053         }
1054         if (multiAppModeObj->Contains(MULTI_APP_MODE_NUMBER.c_str())) {
1055             if (multiAppModeObj->GetInt(MULTI_APP_MODE_NUMBER.c_str(), &multiAppMode.maxCount) != Result::SUCCESS) {
1056                 LOGE("App node get %s failed!", MULTI_APP_MODE_NUMBER.c_str());
1057                 return false;
1058             }
1059         }
1060     }
1061     return true;
1062 }
1063 
IsExistedStageRequestPermissions()1064 bool ModuleJson::IsExistedStageRequestPermissions()
1065 {
1066     std::unique_ptr<PtJson> moduleObj;
1067     if (!GetModuleObject(moduleObj)) {
1068         LOGE("GetModuleObject failed!");
1069         return false;
1070     }
1071     return moduleObj->Contains(REQUEST_PERMISSIONS.c_str());
1072 }
1073 
IsExistedStageModuleTargetPriority()1074 bool ModuleJson::IsExistedStageModuleTargetPriority()
1075 {
1076     std::unique_ptr<PtJson> moduleObj;
1077     if (!GetModuleObject(moduleObj)) {
1078         LOGE("GetModuleObject failed!");
1079         return false;
1080     }
1081     return moduleObj->Contains(TARGET_PRIORITY.c_str());
1082 }
1083 
IsExistedStageAppTargetPriority()1084 bool ModuleJson::IsExistedStageAppTargetPriority()
1085 {
1086     std::unique_ptr<PtJson> appObj;
1087     if (!GetAppObject(appObj)) {
1088         LOGE("GetAppObject failed!");
1089         return false;
1090     }
1091     return appObj->Contains(TARGET_PRIORITY.c_str());
1092 }
1093 
IsModuleAtomicServiceValid()1094 bool ModuleJson::IsModuleAtomicServiceValid()
1095 {
1096     std::unique_ptr<PtJson> moduleObj;
1097     if (!GetModuleObject(moduleObj)) {
1098         LOGE("GetModuleObject failed!");
1099         return false;
1100     }
1101     if (!moduleObj->Contains(ATOMIC_SERVICE.c_str())) {
1102         LOGE("Module node has no %s node!", ATOMIC_SERVICE.c_str());
1103         return true;
1104     }
1105     std::unique_ptr<PtJson> appObj;
1106     if (!GetAppObject(appObj)) {
1107         LOGE("GetAppObject failed!");
1108         return false;
1109     }
1110     if (moduleObj->Contains(ATOMIC_SERVICE.c_str())) {
1111         if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
1112             LOGE("Module cannotconfig atomicService when app node has no bundleType");
1113             return false;
1114         } else {
1115             std::string bundleType;
1116             if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) != Result::SUCCESS) {
1117                 LOGE("App node get %s failed!", BUNDLE_TYPE.c_str());
1118                 return false;
1119             }
1120             if (bundleType.compare(ATOMIC_SERVICE) != 0) {
1121                 LOGE("Module can not config atomicService when bundleType is not atomicService.");
1122                 return false;
1123             }
1124         }
1125     }
1126     return true;
1127 }
1128 
CheckEntryInAtomicService()1129 bool ModuleJson::CheckEntryInAtomicService()
1130 {
1131     std::string bundleType;
1132     if (!GetStageBundleType(bundleType)) {
1133         LOGE("GetStageBundleType failed!");
1134         return false;
1135     }
1136     if (bundleType.compare(ATOMIC_SERVICE) != 0) {
1137         return true;
1138     }
1139     std::string moduleType;
1140     std::list<std::string> abilityNames;
1141     if (!GetStageModuleType(moduleType) || !GetAbilityNames(abilityNames)) {
1142         LOGE("entry module must contain at least one ability.");
1143         return false;
1144     }
1145     if (moduleType.compare(ENTRY) == 0 && abilityNames.empty()) {
1146         LOGE("entry module must contain at least one ability.");
1147         return false;
1148     }
1149     return true;
1150 }
1151 
CheckAtomicServiceInstallationFree()1152 bool ModuleJson::CheckAtomicServiceInstallationFree()
1153 {
1154     std::unique_ptr<PtJson> moduleObj;
1155     std::unique_ptr<PtJson> appObj;
1156     if (!GetModuleObject(moduleObj) || !GetAppObject(appObj)) {
1157         LOGE("GetAppObject or module node failed!");
1158         return false;
1159     }
1160     bool installationFree = false;
1161     GetStageInstallationFreeByModuleObj(moduleObj, installationFree);
1162     if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
1163         if (installationFree) {
1164             LOGE("installationFree must be false when app node has no %s", BUNDLE_TYPE.c_str());
1165             return false;
1166         }
1167         return true;
1168     }
1169     std::string bundleType;
1170     if (!GetStageBundleType(bundleType)) {
1171         LOGE("GetStageBundleType failed!");
1172         return false;
1173     }
1174     std::string moduleName;
1175     if (!GetStageModuleNameByModuleObj(moduleObj, moduleName)) {
1176         LOGE("GetStageModuleNameByModuleObj failed!");
1177         return false;
1178     }
1179     if ((bundleType.compare(APP) == 0) ||
1180         (bundleType.compare(SHARED) == 0) ||
1181         (bundleType.compare(APP_SERVICE) == 0) ||
1182         (bundleType.compare(APP_PLUGIN) == 0)) {
1183         if (installationFree) {
1184             LOGE("installationFree must be false in module %s when bundleType is app/shared/appService/appPlugin",
1185                 moduleName.c_str());
1186             return false;
1187         }
1188     } else if (bundleType.compare(ATOMIC_SERVICE) == 0) {
1189         if (!installationFree) {
1190             LOGE("installationfree must be true in module %s when bundleType is atomicService", moduleName.c_str());
1191             return false;
1192         }
1193     } else {
1194         LOGE("bundleType is not app/shared/appService/atomicService/appPlugin");
1195         return false;
1196     }
1197     return true;
1198 }
1199 
CheckStageAsanTsanEnabledValid()1200 bool ModuleJson::CheckStageAsanTsanEnabledValid()
1201 {
1202     bool asanEnabled = false;
1203     bool tsanEnabled = false;
1204     if (!GetStageAsanEnabled(asanEnabled) || !GetStageTsanEnabled(tsanEnabled)) {
1205         LOGE("GetStageAsanEnabled or GetStageTsanEnabled failed");
1206         return false;
1207     }
1208 
1209     if (asanEnabled && tsanEnabled) {
1210         LOGE("asanEnabled and tsanEnabled cannot be true at the same time.");
1211         return false;
1212     }
1213     return true;
1214 }
1215 
CheckStageAtomicService()1216 bool ModuleJson::CheckStageAtomicService()
1217 {
1218     if (!IsModuleAtomicServiceValid()) {
1219         LOGE("IsModuleAtomicServiceValid failed!");
1220         return false;
1221     }
1222     if (!CheckEntryInAtomicService()) {
1223         LOGE("CheckEntryInAtomicService failed!");
1224         return false;
1225     }
1226     if (!CheckAtomicServiceInstallationFree()) {
1227         LOGE("CheckAtomicServiceInstallationFree failed!");
1228         return false;
1229     }
1230     return true;
1231 }
1232 
CheckStageOverlayCfg()1233 bool ModuleJson::CheckStageOverlayCfg()
1234 {
1235     std::string targetModuleName;
1236     if (!GetTargetModuleName(targetModuleName)) {
1237         LOGE("GetTargetModuleName failed.");
1238         return false;
1239     }
1240     if (!targetModuleName.empty()) {
1241         if (IsExistedStageRequestPermissions()) {
1242             LOGE("targetModuleName cannot be existed with requestPermissions.");
1243             return false;
1244         }
1245         std::string moduleName;
1246         if (!GetStageModuleName(moduleName)) {
1247             LOGE("GetModuleName failed.");
1248             return false;
1249         }
1250         if (targetModuleName == moduleName) {
1251             LOGE("targetModuleName cannot be same with name in the overlay module.");
1252             return false;
1253         }
1254     } else if (IsExistedStageModuleTargetPriority()) {
1255         LOGE("targetPriority cannot be existed without the targetModuleName in module.json.");
1256         return false;
1257     }
1258 
1259     std::string targetBundleName;
1260     if (!GetTargetBundleName(targetBundleName)) {
1261         LOGE("GetTargetBundleName failed.");
1262         return false;
1263     }
1264     if (!targetBundleName.empty()) {
1265         if (targetModuleName.empty()) {
1266             LOGE("targetModuleName is necessary in the overlay bundle.");
1267             return false;
1268         }
1269         std::string bundleName;
1270         if (!GetBundleName(bundleName)) {
1271             LOGE("GetModuleName failed.");
1272             return false;
1273         }
1274         if (targetBundleName == bundleName) {
1275             LOGE("targetBundleName cannot be same with the bundleName.");
1276             return false;
1277         }
1278     } else if (IsExistedStageAppTargetPriority()) {
1279         LOGE("targetPriority cannot be existed without the targetBundleName in app.json.");
1280         return false;
1281     }
1282     return true;
1283 }
1284 
GetGenerateBuildHash(bool & generateBuildHash)1285 bool ModuleJson::GetGenerateBuildHash(bool& generateBuildHash)
1286 {
1287     std::unique_ptr<PtJson> moduleObj;
1288     std::unique_ptr<PtJson> appObj;
1289     if (!GetModuleObject(moduleObj) || !GetAppObject(appObj)) {
1290         LOGE("GetAppObject or module node failed!");
1291         return false;
1292     }
1293     if (appObj->Contains(GENERATE_BUILD_HASH.c_str())) {
1294         if (appObj->GetBool(GENERATE_BUILD_HASH.c_str(), &generateBuildHash) != Result::SUCCESS) {
1295             LOGE("App node get %s failed!", GENERATE_BUILD_HASH.c_str());
1296             return false;
1297         }
1298     } else if (moduleObj->Contains(GENERATE_BUILD_HASH.c_str())) {
1299         if (moduleObj->GetBool(GENERATE_BUILD_HASH.c_str(), &generateBuildHash) != Result::SUCCESS) {
1300             LOGE("Module node get %s failed!", GENERATE_BUILD_HASH.c_str());
1301             return false;
1302         }
1303     } else {
1304         LOGW("App node and module node are both not contain %s node", GENERATE_BUILD_HASH.c_str());
1305         return false;
1306     }
1307     return true;
1308 }
1309 
RemoveGenerateBuildHash()1310 bool ModuleJson::RemoveGenerateBuildHash()
1311 {
1312     if (!RemoveGenerateBuildHashFromAppObj() || !RemoveGenerateBuildHashFromModuleObj()) {
1313         LOGE("RemoveGenerateBuildHashFromAppObj or RemoveGenerateBuildHashFromModuleObj failed!");
1314         return false;
1315     }
1316     return true;
1317 }
1318 
RemoveGenerateBuildHashFromAppObj()1319 bool ModuleJson::RemoveGenerateBuildHashFromAppObj()
1320 {
1321     std::unique_ptr<PtJson> appObj;
1322     if (!GetAppObject(appObj)) {
1323         LOGE("GetAppObject failed!");
1324         return false;
1325     }
1326     if (appObj->Contains(GENERATE_BUILD_HASH.c_str())) {
1327         LOGD("Remove %s nodefrom app node", GENERATE_BUILD_HASH.c_str());
1328         appObj->Remove(GENERATE_BUILD_HASH.c_str());
1329     }
1330     return true;
1331 }
1332 
RemoveGenerateBuildHashFromModuleObj()1333 bool ModuleJson::RemoveGenerateBuildHashFromModuleObj()
1334 {
1335     std::unique_ptr<PtJson> moduleObj;
1336     if (!GetModuleObject(moduleObj)) {
1337         LOGE("GetModuleObject failed!");
1338         return false;
1339     }
1340     if (moduleObj->Contains(GENERATE_BUILD_HASH.c_str())) {
1341         LOGD("Remove %s node from module node", GENERATE_BUILD_HASH.c_str());
1342         moduleObj->Remove(GENERATE_BUILD_HASH.c_str());
1343     }
1344     return true;
1345 }
1346 
GetNormalizeVersion(NormalizeVersion & normalizeVersion,const bool & isStage)1347 bool ModuleJson::GetNormalizeVersion(NormalizeVersion& normalizeVersion, const bool& isStage)
1348 {
1349     Version version;
1350     if (isStage) {
1351         if (!GetStageVersion(version)) {
1352         LOGE("GetStageVersion failed!");
1353             return false;
1354         }
1355     } else {
1356         if (!GetFaVersion(version)) {
1357         LOGE("GetFaVersion failed!");
1358             return false;
1359         }
1360     }
1361     std::string moduleName;
1362     if (!GetModuleName(moduleName)) {
1363         LOGE("GetModuleName failed!");
1364         return false;
1365     }
1366     normalizeVersion.originVersionCode = version.versionCode;
1367     normalizeVersion.originVersionName = version.versionName;
1368     normalizeVersion.moduleName = moduleName;
1369     return true;
1370 }
1371 
SetVersionCodeAndName(const int32_t & versionCode,const std::string & versionName,const bool & isStage)1372 bool ModuleJson::SetVersionCodeAndName(const int32_t& versionCode, const std::string& versionName, const bool& isStage)
1373 {
1374     if (isStage) {
1375         if (!SetStageVersionCode(versionCode) || !SetStageVersionName(versionName)) {
1376             LOGE("SetStageVersionCode or SetStageVersionName failed!");
1377             return false;
1378         }
1379     } else {
1380         if (!SetFaVersionCode(versionCode) || !SetFaVersionName(versionName)) {
1381             LOGE("SetFaVersionCode or SetFaVersionName failed!");
1382             return false;
1383         }
1384     }
1385     return true;
1386 }
1387 
SetBuildHash(const std::string & buildHash)1388 bool ModuleJson::SetBuildHash(const std::string& buildHash)
1389 {
1390     std::unique_ptr<PtJson> moduleObj;
1391     if (!GetModuleObject(moduleObj)) {
1392         LOGE("GetModuleObject failed!");
1393         return false;
1394     }
1395     if (moduleObj->Contains(BUILD_HASH.c_str())) {
1396         if (moduleObj->SetString(BUILD_HASH.c_str(), buildHash) != Result::SUCCESS) {
1397             LOGE("Module node set %s failed!", BUILD_HASH.c_str());
1398             return false;
1399         }
1400     } else {
1401         if (!moduleObj->Add(BUILD_HASH.c_str(), buildHash.c_str())) {
1402             LOGE("Module node add %s failed!", BUILD_HASH.c_str());
1403             return false;
1404         }
1405     }
1406     return true;
1407 }
1408 } // namespace AppPackingTool
1409 } // namespace OHOS
1410