• 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 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 }
90 
GetFaVersion(Version & version)91 bool ModuleJson::GetFaVersion(Version& version)
92 {
93     std::unique_ptr<PtJson> appObj;
94     if (!GetAppObject(appObj)) {
95         LOGE("GetAppObject failed!");
96         return false;
97     }
98     return GetFaVersionByAppObj(appObj, version);
99 }
100 
GetFaVersionByAppObj(std::unique_ptr<PtJson> & appObj,Version & version)101 bool ModuleJson::GetFaVersionByAppObj(std::unique_ptr<PtJson>& appObj, Version& version)
102 {
103     if (!appObj) {
104         LOGE("App node is null!");
105         return false;
106     }
107     if (!appObj->Contains(VERSION.c_str())) {
108         LOGE("App node has no %s node!", VERSION.c_str());
109         return false;
110     }
111     std::unique_ptr<PtJson> versionObj;
112     if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) {
113         LOGE("App node get %s node failed!", VERSION.c_str());
114         return false;
115     }
116     return GetFaVersionByVersionObj(versionObj, version);
117 }
118 
GetFaVersionByVersionObj(std::unique_ptr<PtJson> & versionObj,Version & version)119 bool ModuleJson::GetFaVersionByVersionObj(std::unique_ptr<PtJson>& versionObj, Version& version)
120 {
121     if (!versionObj) {
122         LOGE("Version node is null!");
123         return false;
124     }
125     if (!versionObj->Contains(CODE.c_str()) || !versionObj->Contains(NAME.c_str())) {
126         LOGE("Version node has no %s node or %s node", CODE.c_str(), NAME.c_str());
127         return false;
128     }
129     if (versionObj->GetInt(CODE.c_str(), &version.versionCode) != Result::SUCCESS) {
130         LOGE("Version node get %s failed!", CODE.c_str());
131         return false;
132     }
133     if (versionObj->GetString(NAME.c_str(), &version.versionName) != Result::SUCCESS) {
134         LOGE("Version node get %s failed!", NAME.c_str());
135         return false;
136     }
137     if (versionObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
138         if (versionObj->GetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(),
139             &version.minCompatibleVersionCode) != Result::SUCCESS) {
140             LOGE("Version node get %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
141             return false;
142         }
143     } else {
144         version.minCompatibleVersionCode = version.versionCode;
145     }
146     return true;
147 }
148 
SetFaVersionCode(const int32_t & versionCode)149 bool ModuleJson::SetFaVersionCode(const int32_t& versionCode)
150 {
151     std::unique_ptr<PtJson> versionObj;
152     if (!GetVersionObject(versionObj)) {
153         LOGE("GetVersionObject failed!");
154         return false;
155     }
156     if (!versionObj->Contains(CODE.c_str())) {
157         LOGE("Version node has no %s node!", CODE.c_str());
158         return false;
159     }
160     if (versionObj->SetInt(CODE.c_str(), versionCode) != Result::SUCCESS) {
161         LOGE("Version node set %s failed!", CODE.c_str());
162         return false;
163     }
164     return true;
165 }
166 
SetFaVersionName(const std::string & versionName)167 bool ModuleJson::SetFaVersionName(const std::string& versionName)
168 {
169     std::unique_ptr<PtJson> versionObj;
170     if (!GetVersionObject(versionObj)) {
171         LOGE("GetVersionObject failed!");
172         return false;
173     }
174     if (!versionObj->Contains(NAME.c_str())) {
175         LOGE("Version node has no %s node!", NAME.c_str());
176         return false;
177     }
178     if (versionObj->SetString(NAME.c_str(), versionName) != Result::SUCCESS) {
179         LOGE("Version node set %s failed!", NAME.c_str());
180         return false;
181     }
182     return true;
183 }
184 
GetFaInstallationFree(bool & installationFree)185 bool ModuleJson::GetFaInstallationFree(bool& installationFree)
186 {
187     std::unique_ptr<PtJson> distroObj;
188     if (!GetDistroObject(distroObj)) {
189         LOGE("GetDistroObject failed!");
190         return false;
191     }
192     return GetFaInstallationFreeByDistroObj(distroObj, installationFree);
193 }
194 
GetFaInstallationFreeByModuleObj(std::unique_ptr<PtJson> & moduleObj,bool & installationFree)195 bool ModuleJson::GetFaInstallationFreeByModuleObj(std::unique_ptr<PtJson>& moduleObj, bool& installationFree)
196 {
197     if (!moduleObj) {
198         LOGE("Module node is null!");
199         return false;
200     }
201     std::unique_ptr<PtJson> distroObj;
202     if (!GetDistroObjectByModuleObj(moduleObj, distroObj)) {
203         LOGE("GetDistroObjectByModuleObj failed!");
204         return false;
205     }
206     return GetFaInstallationFreeByDistroObj(distroObj, installationFree);
207 }
208 
GetFaInstallationFreeByDistroObj(std::unique_ptr<PtJson> & distroObj,bool & installationFree)209 bool ModuleJson::GetFaInstallationFreeByDistroObj(std::unique_ptr<PtJson>& distroObj, bool& installationFree)
210 {
211     if (!distroObj) {
212         LOGE("Distro node is null!");
213         return false;
214     }
215     installationFree = false;
216     if (distroObj->Contains(INSTALLATION_FREE.c_str())) {
217         if (distroObj->GetBool(INSTALLATION_FREE.c_str(), &installationFree) != Result::SUCCESS) {
218             LOGE("Distro node get %s failed!", INSTALLATION_FREE.c_str());
219             return false;
220         }
221     }
222     return true;
223 }
224 
GetFaDeliveryWithInstall(bool & deliveryWithInstall)225 bool ModuleJson::GetFaDeliveryWithInstall(bool& deliveryWithInstall)
226 {
227     std::unique_ptr<PtJson> distroObj;
228     if (!GetDistroObject(distroObj)) {
229         LOGE("GetDistroObject failed!");
230         return false;
231     }
232     if (!distroObj) {
233         LOGE("Distro node is null!");
234         return false;
235     }
236     if (distroObj->Contains(DELIVERY_WITH_INSTALL.c_str())) {
237         if (distroObj->GetBool(DELIVERY_WITH_INSTALL.c_str(), &deliveryWithInstall) != Result::SUCCESS) {
238             LOGE("Distro node get %s failed!", DELIVERY_WITH_INSTALL.c_str());
239             return false;
240         }
241     } else {
242         LOGE("Distro node has no %s node!", DELIVERY_WITH_INSTALL.c_str());
243         return false;
244     }
245     return true;
246 }
247 
GetFaBundleType(std::string & bundleType)248 bool ModuleJson::GetFaBundleType(std::string& bundleType)
249 {
250     bool installationFree = false;
251     if (!GetFaInstallationFree(installationFree)) {
252         return false;
253     }
254     if (installationFree) {
255         bundleType = ATOMIC_SERVICE;
256     } else {
257         bundleType = APP;
258     }
259     return true;
260 }
261 
GetFaModuleApiVersion(ModuleApiVersion & moduleApiVersion)262 bool ModuleJson::GetFaModuleApiVersion(ModuleApiVersion& moduleApiVersion)
263 {
264     std::unique_ptr<PtJson> appObj;
265     if (!GetAppObject(appObj)) {
266         LOGE("GetAppObject failed!");
267         return false;
268     }
269     return GetFaModuleApiVersionByAppObj(appObj, moduleApiVersion);
270 }
271 
GetFaModuleApiVersionByAppObj(std::unique_ptr<PtJson> & appObj,ModuleApiVersion & moduleApiVersion)272 bool ModuleJson::GetFaModuleApiVersionByAppObj(std::unique_ptr<PtJson>& appObj, ModuleApiVersion& moduleApiVersion)
273 {
274     if (!appObj) {
275         LOGE("App node is null!");
276         return false;
277     }
278     if (!appObj->Contains(API_VERSION.c_str())) {
279         LOGE("App node has no %s node!", API_VERSION.c_str());
280         return false;
281     }
282     std::unique_ptr<PtJson> apiVersionObj;
283     if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
284         LOGE("App node get %s node failed!", API_VERSION.c_str());
285         return false;
286     }
287     return GetFaModuleApiVersionByApiVersionObj(apiVersionObj, moduleApiVersion);
288 }
289 
GetFaModuleApiVersionByApiVersionObj(std::unique_ptr<PtJson> & apiVersionObj,ModuleApiVersion & moduleApiVersion)290 bool ModuleJson::GetFaModuleApiVersionByApiVersionObj(
291     std::unique_ptr<PtJson>& apiVersionObj, ModuleApiVersion& moduleApiVersion)
292 {
293     if (!apiVersionObj) {
294         LOGE("ApiVersion node is null!");
295         return false;
296     }
297     if (apiVersionObj->Contains(COMPATIBLE.c_str())) {
298         if (apiVersionObj->GetInt(COMPATIBLE.c_str(), &moduleApiVersion.compatibleApiVersion) != Result::SUCCESS) {
299             LOGE("ApiVersion node get %s node failed!", COMPATIBLE.c_str());
300             return false;
301         }
302     }
303     if (apiVersionObj->Contains(TARGET.c_str())) {
304         if (apiVersionObj->GetInt(TARGET.c_str(), &moduleApiVersion.targetApiVersion) != Result::SUCCESS) {
305             LOGE("ApiVersion node get %s node failed!", TARGET.c_str());
306             return false;
307         }
308     }
309     if (apiVersionObj->Contains(RELEASE_TYPE.c_str())) {
310         if (apiVersionObj->GetString(RELEASE_TYPE.c_str(), &moduleApiVersion.releaseType) != Result::SUCCESS) {
311             LOGE("ApiVersion node get %s node failed!", RELEASE_TYPE.c_str());
312             return false;
313         }
314     }
315     return true;
316 }
317 
GetFaModuleName(std::string & faModuleName)318 bool ModuleJson::GetFaModuleName(std::string& faModuleName)
319 {
320     std::unique_ptr<PtJson> moduleObj;
321     if (!GetModuleObject(moduleObj)) {
322         LOGE("GetModuleObject failed!");
323         return false;
324     }
325     std::unique_ptr<PtJson> distroObj;
326     if (!GetDistroObjectByModuleObj(moduleObj, distroObj)) {
327         LOGE("GetDistroObjectByModuleObj failed!");
328         return false;
329     }
330     if (!distroObj->Contains(MODULE_NAME.c_str())) {
331         LOGE("Distro node has no %s node!", MODULE_NAME.c_str());
332         return false;
333     }
334     if (distroObj->GetString(MODULE_NAME.c_str(), &faModuleName) != Result::SUCCESS) {
335         LOGE("Distro node get %s node failed!", MODULE_NAME.c_str());
336         return false;
337     }
338     return true;
339 }
340 
GetFaModuleNameByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & faModuleName)341 bool ModuleJson::GetFaModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& faModuleName)
342 {
343     if (!moduleObj) {
344         LOGE("Module node is null!");
345         return false;
346     }
347     std::unique_ptr<PtJson> distroObj;
348     if (!GetDistroObjectByModuleObj(moduleObj, distroObj)) {
349         LOGE("GetDistroObjectByModuleObj failed!");
350         return false;
351     }
352     return GetFaModuleNameByDistroObj(distroObj, faModuleName);
353 }
354 
GetFaModuleNameByDistroObj(std::unique_ptr<PtJson> & distroObj,std::string & faModuleName)355 bool ModuleJson::GetFaModuleNameByDistroObj(std::unique_ptr<PtJson>& distroObj, std::string& faModuleName)
356 {
357     if (!distroObj) {
358         LOGE("Distro node is null!");
359         return false;
360     }
361     if (!distroObj->Contains(MODULE_NAME.c_str())) {
362         LOGE("Distro node has no %s node!", MODULE_NAME.c_str());
363         return false;
364     }
365     if (distroObj->GetString(MODULE_NAME.c_str(), &faModuleName) != Result::SUCCESS) {
366         LOGE("Distro node get %s node failed!", MODULE_NAME.c_str());
367         return false;
368     }
369     return true;
370 }
371 
GetFaPackageStr(std::string & packageStr)372 bool ModuleJson::GetFaPackageStr(std::string& packageStr)
373 {
374     std::unique_ptr<PtJson> moduleObj;
375     if (!GetModuleObject(moduleObj)) {
376         LOGE("GetModuleObject failed!");
377         return false;
378     }
379     return GetFaPackageStrByModuleObj(moduleObj, packageStr);
380 }
381 
GetFaPackageStrByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & packageStr)382 bool ModuleJson::GetFaPackageStrByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& packageStr)
383 {
384     if (!moduleObj) {
385         LOGE("Module node is null!");
386         return false;
387     }
388     if (!moduleObj->Contains(PACKAGE.c_str())) {
389         LOGE("Module node has no %s node!", PACKAGE.c_str());
390         return false;
391     }
392     if (moduleObj->GetString(PACKAGE.c_str(), &packageStr) != Result::SUCCESS) {
393         LOGE("Module node get %s failed!", PACKAGE.c_str());
394         return false;
395     }
396     return true;
397 }
398 
GetFaCompileSdkType(std::string & compileSdkType)399 bool ModuleJson::GetFaCompileSdkType(std::string& compileSdkType)
400 {
401     std::unique_ptr<PtJson> appObj;
402     if (!GetAppObject(appObj)) {
403         LOGE("GetAppObject failed!");
404         return false;
405     }
406     return GetFaCompileSdkTypeByAppObj(appObj, compileSdkType);
407 }
408 
GetFaCompileSdkTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkType)409 bool ModuleJson::GetFaCompileSdkTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkType)
410 {
411     if (!appObj) {
412         LOGE("App node is null!");
413         return false;
414     }
415     if (!appObj->Contains(API_VERSION.c_str())) {
416         LOGE("App node has no %s node!", API_VERSION.c_str());
417         return false;
418     }
419     std::unique_ptr<PtJson> apiVersionObj;
420     if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
421         LOGE("App node get %s node failed!", API_VERSION.c_str());
422         return false;
423     }
424     if (apiVersionObj->Contains(COMPILE_SDK_TYPE.c_str())) {
425         if (apiVersionObj->GetString(COMPILE_SDK_TYPE.c_str(), &compileSdkType) != Result::SUCCESS) {
426             LOGE("ApiVersion node get %s failed!", COMPILE_SDK_TYPE.c_str());
427             return false;
428         }
429     } else {
430         compileSdkType = "";
431     }
432     return true;
433 }
434 
GetFaCompileSdkVersion(std::string & compileSdkVersion)435 bool ModuleJson::GetFaCompileSdkVersion(std::string& compileSdkVersion)
436 {
437     std::unique_ptr<PtJson> appObj;
438     if (!GetAppObject(appObj)) {
439         LOGE("GetAppObject failed!");
440         return false;
441     }
442     return GetFaCompileSdkVersionByAppObj(appObj, compileSdkVersion);
443 }
444 
GetFaCompileSdkVersionByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkVersion)445 bool ModuleJson::GetFaCompileSdkVersionByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkVersion)
446 {
447     if (!appObj) {
448         LOGE("App node is null!");
449         return false;
450     }
451     if (!appObj->Contains(API_VERSION.c_str())) {
452         LOGE("App node has no %s node!", API_VERSION.c_str());
453         return false;
454     }
455     std::unique_ptr<PtJson> apiVersionObj;
456     if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
457         LOGE("App node get %s node failed!", API_VERSION.c_str());
458         return false;
459     }
460     if (apiVersionObj->Contains(COMPILE_SDK_VERSION.c_str())) {
461         if (apiVersionObj->GetString(COMPILE_SDK_VERSION.c_str(), &compileSdkVersion) != Result::SUCCESS) {
462             LOGE("ApiVersion node get %s failed!", COMPILE_SDK_VERSION.c_str());
463             return false;
464         }
465     } else {
466         compileSdkVersion = "";
467     }
468     return true;
469 }
470 
GetFaDebug(bool & debug)471 bool ModuleJson::GetFaDebug(bool& debug)
472 {
473     std::unique_ptr<PtJson> deviceConfigObj;
474     if (!GetDeviceConfigObject(deviceConfigObj)) {
475         LOGE("GetAppObject failed!");
476         return false;
477     }
478     return GetFaDebugByDeviceConfigObj(deviceConfigObj, debug);
479 }
480 
GetFaDebugByDeviceConfigObj(std::unique_ptr<PtJson> & deviceConfigObj,bool & debug)481 bool ModuleJson::GetFaDebugByDeviceConfigObj(std::unique_ptr<PtJson>& deviceConfigObj, bool& debug)
482 {
483     if (!deviceConfigObj) {
484         LOGE("DeviceConfig node is null!");
485         return false;
486     }
487     if (!deviceConfigObj->Contains(DEFAULT.c_str())) {
488         LOGE("DeviceConfig node has no %s node!", DEFAULT.c_str());
489         return false;
490     }
491     std::unique_ptr<PtJson> defaultObj;
492     if (deviceConfigObj->GetObject(DEFAULT.c_str(), &defaultObj) != Result::SUCCESS) {
493         LOGE("DeviceConfig node get %s failed!", DEFAULT.c_str());
494         return false;
495     }
496     if (defaultObj->Contains(DEBUG.c_str())) {
497         if (defaultObj->GetBool(DEBUG.c_str(), &debug) != Result::SUCCESS) {
498             LOGE("debug node get %s failed!", DEBUG.c_str());
499             return false;
500         }
501     }
502     return true;
503 }
504 
505 // java: parseFaEntry / getDeviceTypeFromFAModule
506 // parseFaEntry not called anywhere
GetFaEntry(std::list<std::string> & deviceTypes)507 bool ModuleJson::GetFaEntry(std::list<std::string>& deviceTypes)
508 {
509     std::unique_ptr<PtJson> moduleObj;
510     if (!GetModuleObject(moduleObj)) {
511         LOGE("GetModuleObject failed!");
512         return false;
513     }
514     std::unique_ptr<PtJson> distroObj;
515     if (!moduleObj->Contains(DISTRO.c_str())) {
516         LOGE("Module node has no %s node!", DISTRO.c_str());
517         return false;
518     }
519     if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
520         LOGE("Module node get %s node failed!", DISTRO.c_str());
521         return false;
522     }
523     std::string moduleType;
524     if (!distroObj->Contains(MODULE_TYPE.c_str())) {
525         LOGE("Distro node has no %s node!", MODULE_TYPE.c_str());
526         return false;
527     }
528     if (distroObj->GetString(MODULE_TYPE.c_str(), &moduleType) != Result::SUCCESS) {
529         LOGE("Distro node get %s failed!", MODULE_TYPE.c_str());
530         return false;
531     }
532     if (moduleType.compare(ENTRY) == 0) {
533         if (!GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
534             LOGE("GetFaDeviceTypesByModuleObj failed!");
535             return false;
536         }
537     }
538     return true;
539 }
540 
GetFaDeviceTypes(std::list<std::string> & deviceTypes)541 bool ModuleJson::GetFaDeviceTypes(std::list<std::string>& deviceTypes)
542 {
543     std::unique_ptr<PtJson> moduleObj;
544     if (!GetModuleObject(moduleObj)) {
545         LOGE("GetModuleObject failed!");
546         return false;
547     }
548     return GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes);
549 }
550 
GetFaDeviceTypesByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & deviceTypes)551 bool ModuleJson::GetFaDeviceTypesByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& deviceTypes)
552 {
553     if (!moduleObj) {
554         LOGE("Module node is null!");
555         return false;
556     }
557     if (!moduleObj->Contains(DEVICE_TYPE.c_str())) {
558         LOGE("Module node has no %s node!", DEVICE_TYPE.c_str());
559         return false;
560     }
561     std::unique_ptr<PtJson> deviceTypeObj;
562     if (moduleObj->GetArray(DEVICE_TYPE.c_str(), &deviceTypeObj) != Result::SUCCESS) {
563         LOGE("Module node get %s array node failed!", DEVICE_TYPE.c_str());
564         return false;
565     }
566     for (int32_t i = 0; i < deviceTypeObj->GetSize(); i++) {
567         deviceTypes.push_back(deviceTypeObj->Get(i)->GetString());
568     }
569     return true;
570 }
571 
GetFaModuleType(std::string & moduleType)572 bool ModuleJson::GetFaModuleType(std::string& moduleType)
573 {
574     std::unique_ptr<PtJson> moduleObj;
575     if (!GetModuleObject(moduleObj)) {
576         LOGE("GetModuleObject failed!");
577         return false;
578     }
579     return GetFaModuleTypeByModuleObj(moduleObj, moduleType);
580 }
581 
582 // java : parseFAIsEntry
GetFaModuleTypeByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & moduleType)583 bool ModuleJson::GetFaModuleTypeByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleType)
584 {
585     if (!moduleObj) {
586         LOGE("Module node is null!");
587         return false;
588     }
589     moduleType = "";
590     if (moduleObj->Contains(DISTRO.c_str())) {
591         std::unique_ptr<PtJson> distroObj;
592         if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
593             LOGE("Module node get %s node failed!", DISTRO.c_str());
594             return false;
595         }
596         if (distroObj->Contains(MODULE_TYPE.c_str())) {
597             if (distroObj->GetString(MODULE_TYPE.c_str(), &moduleType) != Result::SUCCESS) {
598                 LOGE("Module node get %s failed!", MODULE_TYPE.c_str());
599                 return false;
600             }
601         }
602     }
603     return true;
604 }
605 
GetFaReleaseType(std::string & releaseType)606 bool ModuleJson::GetFaReleaseType(std::string& releaseType)
607 {
608     std::unique_ptr<PtJson> appObj;
609     if (!GetAppObject(appObj)) {
610         LOGE("GetAppObject failed!");
611         return false;
612     }
613     return GetFaReleaseTypeByAppObj(appObj, releaseType);
614 }
615 
GetFaReleaseTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & releaseType)616 bool ModuleJson::GetFaReleaseTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& releaseType)
617 {
618     if (!appObj) {
619         LOGE("App node is null!");
620         return false;
621     }
622     std::unique_ptr<PtJson> apiVersionObj;
623     releaseType = "";
624     if (appObj->Contains(API_VERSION.c_str())) {
625         if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
626             LOGE("App node get %s node failed!", API_VERSION.c_str());
627             return false;
628         }
629         if (apiVersionObj->Contains(RELEASE_TYPE.c_str())) {
630             if (apiVersionObj->GetString(RELEASE_TYPE.c_str(), &releaseType) != Result::SUCCESS) {
631                 LOGE("App node get %s failed!", RELEASE_TYPE.c_str());
632                 return false;
633             }
634         }
635     }
636     return true;
637 }
638 
GetFaAsanEnabled(bool & asanEnabled)639 bool ModuleJson::GetFaAsanEnabled(bool& asanEnabled)
640 {
641     std::unique_ptr<PtJson> appObj;
642     if (!GetAppObject(appObj)) {
643         LOGE("GetAppObject failed!");
644         return false;
645     }
646     return GetFaAsanEnabledByAppObj(appObj, asanEnabled);
647 }
648 
GetFaAsanEnabledByAppObj(std::unique_ptr<PtJson> & appObj,bool & asanEnabled)649 bool ModuleJson::GetFaAsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& asanEnabled)
650 {
651     if (!appObj) {
652         LOGE("App node is null!");
653         return false;
654     }
655     if (appObj->Contains(ASAN_ENABLED.c_str())) {
656         if (appObj->GetBool(ASAN_ENABLED.c_str(), &asanEnabled) != Result::SUCCESS) {
657             LOGE("App node get %s failed!", ASAN_ENABLED.c_str());
658             return false;
659         }
660     } else {
661         asanEnabled = false;
662     }
663     return true;
664 }
665 
GetFaDistroFilter(DistroFilter & distroFilter)666 bool ModuleJson::GetFaDistroFilter(DistroFilter& distroFilter)
667 {
668     std::unique_ptr<PtJson> moduleObj;
669     if (!GetModuleObject(moduleObj)) {
670         LOGE("GetModuleObject failed!");
671         return false;
672     }
673     return GetFaDistroFilterByModuleObj(moduleObj, distroFilter);
674 }
675 
GetFaDistroFilterByModuleObj(std::unique_ptr<PtJson> & moduleObj,DistroFilter & distroFilter)676 bool ModuleJson::GetFaDistroFilterByModuleObj(std::unique_ptr<PtJson>& moduleObj, DistroFilter& distroFilter)
677 {
678     if (!moduleObj) {
679         LOGE("Module node is null!");
680         return false;
681     }
682     if (moduleObj->Contains(DISTRO_FILTER.c_str())) {
683         std::string distroFilterStr;
684         if (moduleObj->GetString(DISTRO_FILTER.c_str(), &distroFilterStr) != Result::SUCCESS) {
685             LOGE("Module node get %s failed!", DISTRO_FILTER.c_str());
686             return false;
687         }
688         std::unique_ptr<PtJson> distroFilterJsonObj = PtJson::Parse(distroFilterStr);
689         if (!distroFilterJsonObj) {
690             LOGE("Parse distro filter string failed!");
691             return false;
692         }
693         std::unique_ptr<PtJson> distroFilterObj;
694         if (distroFilterJsonObj->Contains(DISTRO_FILTER.c_str())) {
695             if (distroFilterJsonObj->GetObject(DISTRO_FILTER.c_str(), &distroFilterObj) != Result::SUCCESS) {
696                 LOGE("DistroFilter node get %s failed!", DISTRO_FILTER.c_str());
697                 return false;
698             }
699         }
700         if (!distroFilter.ParseFromJson(distroFilterObj)) {
701             LOGE("Parse distro filter failed!");
702             return false;
703         }
704     }
705     return true;
706 }
707 
GetFaHapVerifyInfo(HapVerifyInfo & hapVerifyInfo)708 bool ModuleJson::GetFaHapVerifyInfo(HapVerifyInfo& hapVerifyInfo)
709 {
710     std::unique_ptr<PtJson> appObj;
711     std::unique_ptr<PtJson> moduleObj;
712     if (!GetAppObject(appObj)) {
713         LOGE("GetAppObject failed!");
714         return false;
715     }
716     if (!GetModuleObject(moduleObj)) {
717         LOGE("GetModuleObject failed!");
718         return false;
719     }
720     std::string bundleName;
721     std::string bundleType;
722     std::list<DependencyItem> dependencyItems;
723     bool debug = false;
724     if (!GetBundleNameByAppObj(appObj, bundleName)) {
725         LOGE("GetBundleNameByAppObj failed!");
726         return false;
727     }
728     if (!GetFaBundleType(bundleType)) {
729         LOGE("GetFaBundleType failed!");
730         return false;
731     }
732     if (!GetDependencyItemsByModuleObj(moduleObj, dependencyItems, bundleName)) {
733         LOGE("GetDependencyItemsByModuleObj failed!");
734         return false;
735     }
736     GetFaDebug(debug);
737     hapVerifyInfo.SetBundleName(bundleName);
738     hapVerifyInfo.SetBundleType(bundleType);
739     hapVerifyInfo.SetDependencyItemList(dependencyItems);
740     hapVerifyInfo.SetDebug(debug);
741     if (!SetFaHapVerifyInfoByAppObj(appObj, hapVerifyInfo)) {
742         LOGE("SetFaHapVerifyInfoByAppObj failed!");
743         return false;
744     }
745     if (!SetFaHapVerifyInfoByModuleObj(moduleObj, hapVerifyInfo)) {
746         LOGE("SetFaHapVerifyInfoByModuleObj failed!");
747         return false;
748     }
749     return true;
750 }
751 
SetFaHapVerifyInfoByAppObj(std::unique_ptr<PtJson> & appObj,HapVerifyInfo & hapVerifyInfo)752 bool ModuleJson::SetFaHapVerifyInfoByAppObj(std::unique_ptr<PtJson>& appObj, HapVerifyInfo& hapVerifyInfo)
753 {
754     if (!appObj) {
755         LOGE("App node is null!");
756         return false;
757     }
758     std::string vendor;
759     Version version;
760     ModuleApiVersion moduleApiVersion;
761     std::string compileSdkType;
762     std::string compileSdkVersion;
763     if (!GetVendorByAppObj(appObj, vendor)) {
764         LOGE("GetVendorByAppObj failed!");
765         return false;
766     }
767     if (!GetFaVersionByAppObj(appObj, version)) {
768         LOGE("GetFaVersionByAppObj failed!");
769         return false;
770     }
771     if (!GetFaModuleApiVersionByAppObj(appObj, moduleApiVersion)) {
772         LOGE("GetFaModuleApiVersionByAppObj failed!");
773         return false;
774     }
775     if (!GetFaCompileSdkTypeByAppObj(appObj, compileSdkType)) {
776         LOGE("GetFaCompileSdkTypeByAppObj failed!");
777         return false;
778     }
779     if (!GetFaCompileSdkVersionByAppObj(appObj, compileSdkVersion)) {
780         LOGE("GetFaCompileSdkVersionByAppObj failed!");
781         return false;
782     }
783     hapVerifyInfo.SetVendor(vendor);
784     hapVerifyInfo.SetVersion(version);
785     hapVerifyInfo.SetApiVersion(moduleApiVersion);
786     hapVerifyInfo.SetCompileSdkType(compileSdkType);
787     hapVerifyInfo.SetCompileSdkVersion(compileSdkVersion);
788     return true;
789 }
790 
SetFaHapVerifyInfoByModuleObj(std::unique_ptr<PtJson> & moduleObj,HapVerifyInfo & hapVerifyInfo)791 bool ModuleJson::SetFaHapVerifyInfoByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo)
792 {
793     if (!moduleObj) {
794         LOGE("Module node is null!");
795         return false;
796     }
797     std::string moduleName;
798     DistroFilter distroFilter;
799     std::list<std::string> deviceTypes;
800     std::list<std::string> abilityNames;
801     std::string moduleType;
802     std::string packageStr;
803     bool installationFree = false;
804     if (!GetFaModuleNameByModuleObj(moduleObj, moduleName)) {
805         LOGE("GetFaModuleNameByModuleObj failed!");
806         return false;
807     }
808     if (!GetFaDistroFilterByModuleObj(moduleObj, distroFilter)) {
809         LOGE("GetFaDistroFilterByModuleObj failed!");
810         return false;
811     }
812     if (!GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
813         LOGE("GetFaDeviceTypesByModuleObj failed!");
814         return false;
815     }
816     if (!GetAbilityNamesByModuleObj(moduleObj, abilityNames)) {
817         LOGE("GetAbilityNamesByModuleObj failed!");
818         return false;
819     }
820     if (!GetFaModuleTypeByModuleObj(moduleObj, moduleType)) {
821         LOGE("GetFaModuleTypeByModuleObj failed!");
822         return false;
823     }
824     if (!GetFaPackageStrByModuleObj(moduleObj, packageStr)) {
825         LOGE("GetFaPackageStrByModuleObj failed!");
826         return false;
827     }
828     if (!GetFaInstallationFreeByModuleObj(moduleObj, installationFree)) {
829         LOGE("GetFaInstallationFreeByModuleObj failed!");
830         return false;
831     }
832     hapVerifyInfo.SetModuleName(moduleName);
833     hapVerifyInfo.SetDistroFilter(distroFilter);
834     hapVerifyInfo.SetDeviceTypes(deviceTypes);
835     hapVerifyInfo.SetAbilityNames(abilityNames);
836     hapVerifyInfo.SetModuleType(moduleType);
837     hapVerifyInfo.SetPackageName(packageStr);
838     hapVerifyInfo.SetInstallationFree(installationFree);
839     return true;
840 }
841 
SetFaBundleName(const std::string & bundleName)842 bool ModuleJson::SetFaBundleName(const std::string& bundleName)
843 {
844     std::unique_ptr<PtJson> appObj;
845     if (!GetAppObject(appObj)) {
846         LOGE("GetAppObject failed!");
847         return false;
848     }
849     if (!appObj->Contains(BUNDLE_NAME.c_str())) {
850         if (!appObj->Add(BUNDLE_NAME.c_str(), bundleName.c_str())) {
851             LOGE("App node add %s failed!", BUNDLE_NAME.c_str());
852             return false;
853         }
854         return true;
855     }
856     if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) {
857         LOGE("App node set %s failed!", BUNDLE_NAME.c_str());
858         return false;
859     }
860     return true;
861 }
862 
SetFaMinCompatibleVersionCode(const int32_t & minCompatibleVersionCode)863 bool ModuleJson::SetFaMinCompatibleVersionCode(const int32_t& minCompatibleVersionCode)
864 {
865     std::unique_ptr<PtJson> versionObj;
866     if (!GetVersionObject(versionObj)) {
867         LOGE("GetVersionObject failed!");
868         return false;
869     }
870     if (!versionObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
871         if (!versionObj->Add(MIN_COMPATIBLE_VERSION_CODE.c_str(), minCompatibleVersionCode)) {
872             LOGE("Version node add %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
873             return false;
874         }
875         return true;
876     }
877     if (versionObj->SetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(), minCompatibleVersionCode) != Result::SUCCESS) {
878         LOGE("Version node set %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
879         return false;
880     }
881     return true;
882 }
883 
SetFaMinAPIVersion(const int32_t & minAPIVersion)884 bool ModuleJson::SetFaMinAPIVersion(const int32_t& minAPIVersion)
885 {
886     std::unique_ptr<PtJson> apiVersionObj;
887     if (!GetApiVersionObject(apiVersionObj)) {
888         LOGE("GetAppObject failed!");
889         return false;
890     }
891     if (!apiVersionObj->Contains(COMPATIBLE.c_str())) {
892         if (!apiVersionObj->Add(COMPATIBLE.c_str(), minAPIVersion)) {
893             LOGE("ApiVersion node add %s failed!", COMPATIBLE.c_str());
894             return false;
895         }
896         return true;
897     }
898     if (apiVersionObj->SetInt(COMPATIBLE.c_str(), minAPIVersion) != Result::SUCCESS) {
899         LOGE("ApiVersion node set %s failed!", COMPATIBLE.c_str());
900         return false;
901     }
902     return true;
903 }
904 
SetFaTargetAPIVersion(const int32_t & targetAPIVersion)905 bool ModuleJson::SetFaTargetAPIVersion(const int32_t& targetAPIVersion)
906 {
907     std::unique_ptr<PtJson> apiVersionObj;
908     if (!GetApiVersionObject(apiVersionObj)) {
909         LOGE("GetApiVersionObject failed!");
910         return false;
911     }
912     if (!apiVersionObj->Contains(TARGET.c_str())) {
913         if (!apiVersionObj->Add(TARGET.c_str(), targetAPIVersion)) {
914             LOGE("ApiVersion node add %s failed!", TARGET.c_str());
915             return false;
916         }
917         return true;
918     }
919     if (apiVersionObj->SetInt(TARGET.c_str(), targetAPIVersion) != Result::SUCCESS) {
920         LOGE("ApiVersion node set %s failed!", TARGET.c_str());
921         return false;
922     }
923     return true;
924 }
925 
SetFaApiReleaseType(const std::string & apiReleaseType)926 bool ModuleJson::SetFaApiReleaseType(const std::string& apiReleaseType)
927 {
928     std::unique_ptr<PtJson> apiVersionObj;
929     if (!GetApiVersionObject(apiVersionObj)) {
930         LOGE("GetApiVersionObject failed!");
931         return false;
932     }
933     if (!apiVersionObj->Contains(RELEASE_TYPE.c_str())) {
934         if (!apiVersionObj->Add(RELEASE_TYPE.c_str(), apiReleaseType.c_str())) {
935             LOGE("ApiVersion node add %s failed!", RELEASE_TYPE.c_str());
936             return false;
937         }
938         return true;
939     }
940     if (apiVersionObj->SetString(RELEASE_TYPE.c_str(), apiReleaseType) != Result::SUCCESS) {
941         LOGE("ApiVersion node set %s failed!", RELEASE_TYPE.c_str());
942         return false;
943     }
944     return true;
945 }
946 
SetFaBundleType(const std::string & bundleType)947 bool ModuleJson::SetFaBundleType(const std::string& bundleType)
948 {
949     std::unique_ptr<PtJson> appObj;
950     if (!GetAppObject(appObj)) {
951         LOGE("GetAppObject failed!");
952         return false;
953     }
954     if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
955         if (!appObj->Add(BUNDLE_TYPE.c_str(), bundleType.c_str())) {
956             LOGE("App node add %s failed!", BUNDLE_TYPE.c_str());
957             return false;
958         }
959         return true;
960     }
961     if (appObj->SetString(BUNDLE_TYPE.c_str(), bundleType) != Result::SUCCESS) {
962         LOGE("App node set %s failed!", BUNDLE_TYPE.c_str());
963         return false;
964     }
965     return true;
966 }
967 
SetFaInstallationFree(const bool & installationFree)968 bool ModuleJson::SetFaInstallationFree(const bool& installationFree)
969 {
970     std::unique_ptr<PtJson> distroObj;
971     if (!GetDistroObject(distroObj)) {
972         LOGE("GetDistroObject failed!");
973         return false;
974     }
975     if (!distroObj->Contains(INSTALLATION_FREE.c_str())) {
976         if (!distroObj->Add(INSTALLATION_FREE.c_str(), installationFree)) {
977             LOGE("Distro node add %s failed!", INSTALLATION_FREE.c_str());
978             return false;
979         }
980         return true;
981     }
982     if (distroObj->SetBool(INSTALLATION_FREE.c_str(), installationFree) != Result::SUCCESS) {
983         LOGE("Distro node set %s failed!", INSTALLATION_FREE.c_str());
984         return false;
985     }
986     return true;
987 }
988 
SetFaDeliveryWithInstall(const bool & deliveryWithInstall)989 bool ModuleJson::SetFaDeliveryWithInstall(const bool& deliveryWithInstall)
990 {
991     std::unique_ptr<PtJson> distroObj;
992     if (!GetDistroObject(distroObj)) {
993         LOGE("GetDistroObject failed!");
994         return false;
995     }
996     if (!distroObj->Contains(DELIVERY_WITH_INSTALL.c_str())) {
997         if (!distroObj->Add(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall)) {
998             LOGE("Distro node add %s failed!", DELIVERY_WITH_INSTALL.c_str());
999             return false;
1000         }
1001         return true;
1002     }
1003     if (distroObj->SetBool(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall) != Result::SUCCESS) {
1004         LOGE("Distro node set %s failed!", DELIVERY_WITH_INSTALL.c_str());
1005         return false;
1006     }
1007     return true;
1008 }
1009 
SetFaDeviceTypes(const std::list<std::string> & deviceTypes)1010 bool ModuleJson::SetFaDeviceTypes(const std::list<std::string>& deviceTypes)
1011 {
1012     std::unique_ptr<PtJson> moduleObj;
1013     if (!GetModuleObject(moduleObj)) {
1014         LOGE("GetModuleObject failed!");
1015         return false;
1016     }
1017     if (!moduleObj->Contains(DEVICE_TYPE.c_str())) {
1018         if (!moduleObj->Add(DEVICE_TYPE.c_str(), deviceTypes)) {
1019             LOGE("Module node add %s failed!", DEVICE_TYPE.c_str());
1020             return false;
1021         }
1022         return true;
1023     }
1024     if (moduleObj->SetArray(DEVICE_TYPE.c_str(), deviceTypes) != Result::SUCCESS) {
1025         LOGE("Module node set %s failed!", DEVICE_TYPE.c_str());
1026         return false;
1027     }
1028     return true;
1029 }
1030 } // namespace AppPackingTool
1031 } // namespace OHOS
1032