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