1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "module_json.h"
17
18 #include <fstream>
19
20 #include "log.h"
21 #include "utils.h"
22
23 namespace OHOS {
24 namespace AppPackingTool {
25 namespace {
26 const std::string APP = "app";
27 const std::string BUNDLE_TYPE = "bundleType";
28 const std::string ABILITIES = "abilities";
29 const std::string VERSIONCODE = "versionCode";
30 const std::string VERSIONNAME = "versionName";
31 const std::string MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode";
32 const std::string API_VERSION = "apiVersion";
33 const std::string MIN_API_VERSION = "minAPIVersion";
34 const std::string TARGET_API_VERSION = "targetAPIVersion";
35 const std::string API_RELEASE_TYPE = "apiReleaseType";
36 const std::string DEBUG = "debug";
37 const std::string COMPATIBLE = "compatible";
38 const std::string RELEASE_TYPE = "releaseType";
39 const std::string TARGET = "target";
40 const std::string VERSION = "version";
41 const std::string CODE = "code";
42 const std::string NAME = "name";
43 const std::string MODULE = "module";
44 const std::string MODULE_NAME = "moduleName";
45 const std::string MODULE_TYPE = "moduleType";
46 const std::string DISTRO = "distro";
47 const std::string PACKAGE = "package";
48 const std::string BUNDLE_NAME = "bundleName";
49 const std::string ENTRY = "entry";
50 const std::string DEVICE_TYPE = "deviceType";
51 const std::string DEVICE_TYPES = "deviceTypes";
52 const std::string TYPE = "type";
53 const std::string VENDOR = "vendor";
54 const std::string METADATA = "metadata";
55 const std::string RESOURCE = "resource";
56 const std::string PROFILE = "$profile:";
57 const std::string VALUE = "value";
58 const std::string JSON_PREFIX = ".json";
59 const std::string DISTRO_FILTER = "distroFilter";
60 const std::string DISTRIBUTION_FILTER = "distributionFilter";
61 const std::string DEPENDENCIES = "dependencies";
62 const std::string EXTENSION_ABILITIES = "extensionAbilities";
63 const std::string INSTALLATION_FREE = "installationFree";
64 const std::string COMPRESS_NATIVE_LIBS = "compressNativeLibs";
65 const std::string ASAN_ENABLED = "asanEnabled";
66 const std::string TSAN_ENABLED = "tsanEnabled";
67 const std::string ATOMIC_SERVICE = "atomicService";
68 const std::string PRELOADS = "preloads";
69 const std::string SHARED = "shared";
70 const std::string APP_SERVICE = "appService";
71 const std::string REQUEST_PERMISSIONS = "requestPermissions";
72 const std::string TARGET_MODULE_NAME = "targetModuleName";
73 const std::string TARGET_PRIORITY = "targetPriority";
74 const std::string TARGET_BUNDLE_NAME = "targetBundleName";
75 const std::string DEVICE_CONFIG = "deviceConfig";
76 const std::string DEFAULT = "default";
77 const std::string COMPILE_SDK_VERSION = "compileSdkVersion";
78 const std::string COMPILE_SDK_TYPE = "compileSdkType";
79 const std::string PROXY_DATAS = "proxyDatas";
80 const std::string PROXY_DATA = "proxyData";
81 const std::string PROXY_URI = "uri";
82 const std::string CONTINUE_TYPE = "continueType";
83 const std::string MULTI_APP_MODE = "multiAppMode";
84 const std::string MULTI_APP_MODE_TYPE = "multiAppModeType";
85 const std::string MULTI_APP_MODE_NUMBER = "maxCount";
86 const std::string GENERATE_BUILD_HASH = "generateBuildHash";
87 const std::string BUILD_HASH = "buildHash";
88 }
89
SetStageVersionCode(const int32_t & versionCode)90 bool ModuleJson::SetStageVersionCode(const int32_t& versionCode)
91 {
92 std::unique_ptr<PtJson> appObj;
93 if (!GetAppObject(appObj)) {
94 LOGE("GetAppObject failed!");
95 return false;
96 }
97 if (!appObj->Contains(VERSIONCODE.c_str())) {
98 LOGE("App node has no %s node!", VERSIONCODE.c_str());
99 return false;
100 }
101 if (appObj->SetInt(VERSIONCODE.c_str(), versionCode) != Result::SUCCESS) {
102 LOGE("App node set %s failed!", VERSIONCODE.c_str());
103 return false;
104 }
105 return true;
106 }
107
SetStageVersionName(const std::string & versionName)108 bool ModuleJson::SetStageVersionName(const std::string& versionName)
109 {
110 std::unique_ptr<PtJson> appObj;
111 if (!GetAppObject(appObj)) {
112 LOGE("GetAppObject failed!");
113 return false;
114 }
115 if (!appObj->Contains(VERSIONNAME.c_str())) {
116 LOGE("App node has no %s node!", VERSIONNAME.c_str());
117 return false;
118 }
119 if (appObj->SetString(VERSIONNAME.c_str(), versionName) != Result::SUCCESS) {
120 LOGE("App node set %s failed!", VERSIONNAME.c_str());
121 return false;
122 }
123 return true;
124 }
125
GetStageVersion(Version & version)126 bool ModuleJson::GetStageVersion(Version& version)
127 {
128 std::unique_ptr<PtJson> appObj;
129 if (!GetAppObject(appObj)) {
130 LOGE("GetAppObject failed!");
131 return false;
132 }
133 return GetStageVersionByAppObj(appObj, version);
134 }
135
GetStageVersionByAppObj(std::unique_ptr<PtJson> & appObj,Version & version)136 bool ModuleJson::GetStageVersionByAppObj(std::unique_ptr<PtJson>& appObj, Version& version)
137 {
138 if (!appObj) {
139 LOGE("App node is null!");
140 return false;
141 }
142 if (!appObj->Contains(VERSIONCODE.c_str()) || !appObj->Contains(VERSIONNAME.c_str())) {
143 LOGE("App node has no %s node or %s node", VERSIONCODE.c_str(), VERSIONNAME.c_str());
144 return false;
145 }
146 if (appObj->GetInt(VERSIONCODE.c_str(), &version.versionCode) != Result::SUCCESS) {
147 LOGE("App node get %s failed!", VERSIONCODE.c_str());
148 return false;
149 }
150 if (appObj->GetString(VERSIONNAME.c_str(), &version.versionName) != Result::SUCCESS) {
151 LOGE("App node get %s failed!", VERSIONNAME.c_str());
152 return false;
153 }
154 if (appObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
155 if (appObj->GetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(),
156 &version.minCompatibleVersionCode) != Result::SUCCESS) {
157 LOGE("App node get %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
158 return false;
159 }
160 } else {
161 version.minCompatibleVersionCode = version.versionCode;
162 }
163 return true;
164 }
165
GetStageInstallationFree(bool & installationFree)166 bool ModuleJson::GetStageInstallationFree(bool& installationFree)
167 {
168 std::unique_ptr<PtJson> moduleObj;
169 if (!GetModuleObject(moduleObj)) {
170 LOGE("GetModuleObject failed!");
171 return false;
172 }
173 return GetStageInstallationFreeByModuleObj(moduleObj, installationFree);
174 }
175
GetStageInstallationFreeByModuleObj(std::unique_ptr<PtJson> & moduleObj,bool & installationFree)176 bool ModuleJson::GetStageInstallationFreeByModuleObj(std::unique_ptr<PtJson>& moduleObj, bool& installationFree)
177 {
178 if (!moduleObj) {
179 LOGE("Module node is null!");
180 return false;
181 }
182 installationFree = false;
183 if (moduleObj->Contains(INSTALLATION_FREE.c_str())) {
184 if (moduleObj->GetBool(INSTALLATION_FREE.c_str(), &installationFree) != Result::SUCCESS) {
185 LOGE("Module node get %s failed!", INSTALLATION_FREE.c_str());
186 return false;
187 }
188 }
189 return true;
190 }
191
GetStageModuleApiVersion(ModuleApiVersion & moduleApiVersion)192 bool ModuleJson::GetStageModuleApiVersion(ModuleApiVersion& moduleApiVersion)
193 {
194 std::unique_ptr<PtJson> appObj;
195 if (!GetAppObject(appObj)) {
196 LOGE("GetAppObject failed!");
197 return false;
198 }
199 return GetStageModuleApiVersionByAppObj(appObj, moduleApiVersion);
200 }
201
GetStageModuleApiVersionByAppObj(std::unique_ptr<PtJson> & appObj,ModuleApiVersion & moduleApiVersion)202 bool ModuleJson::GetStageModuleApiVersionByAppObj(std::unique_ptr<PtJson>& appObj, ModuleApiVersion& moduleApiVersion)
203 {
204 if (!appObj) {
205 LOGE("App node is null!");
206 return false;
207 }
208 if (appObj->Contains(MIN_API_VERSION.c_str())) {
209 if (appObj->GetInt(MIN_API_VERSION.c_str(), &moduleApiVersion.compatibleApiVersion) != Result::SUCCESS) {
210 LOGE("App node get %s failed!", MIN_API_VERSION.c_str());
211 return false;
212 }
213 }
214 if (appObj->Contains(TARGET_API_VERSION.c_str())) {
215 if (appObj->GetInt(TARGET_API_VERSION.c_str(), &moduleApiVersion.targetApiVersion) != Result::SUCCESS) {
216 LOGE("App node get %s failed!", TARGET_API_VERSION.c_str());
217 return false;
218 }
219 }
220 if (appObj->Contains(API_RELEASE_TYPE.c_str())) {
221 if (appObj->GetString(API_RELEASE_TYPE.c_str(), &moduleApiVersion.releaseType) != Result::SUCCESS) {
222 LOGE("App node get %s failed!", API_RELEASE_TYPE.c_str());
223 return false;
224 }
225 }
226 return true;
227 }
228
GetStageModuleName(std::string & stageModuleName)229 bool ModuleJson::GetStageModuleName(std::string& stageModuleName)
230 {
231 std::unique_ptr<PtJson> moduleObj;
232 if (!GetModuleObject(moduleObj)) {
233 LOGE("GetModuleObject failed!");
234 return false;
235 }
236 return GetStageModuleNameByModuleObj(moduleObj, stageModuleName);
237 }
238
GetStageModuleNameByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & stageModuleName)239 bool ModuleJson::GetStageModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& stageModuleName)
240 {
241 if (!moduleObj) {
242 LOGE("Module node is null!");
243 return false;
244 }
245 if (!moduleObj->Contains(NAME.c_str())) {
246 LOGE("Module node has no %s node!", NAME.c_str());
247 return false;
248 }
249 if (moduleObj->GetString(NAME.c_str(), &stageModuleName) != Result::SUCCESS) {
250 LOGE("Module node get %s failed!", NAME.c_str());
251 return false;
252 }
253 return true;
254 }
255
GetStageCompileSdkType(std::string & compileSdkType)256 bool ModuleJson::GetStageCompileSdkType(std::string& compileSdkType)
257 {
258 std::unique_ptr<PtJson> appObj;
259 if (!GetAppObject(appObj)) {
260 LOGE("GetAppObject failed!");
261 return false;
262 }
263 return GetStageCompileSdkTypeByAppObj(appObj, compileSdkType);
264 }
265
GetStageCompileSdkTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkType)266 bool ModuleJson::GetStageCompileSdkTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkType)
267 {
268 if (!appObj) {
269 LOGE("App node is null!");
270 return false;
271 }
272 if (appObj->Contains(COMPILE_SDK_TYPE.c_str())) {
273 if (appObj->GetString(COMPILE_SDK_TYPE.c_str(), &compileSdkType) != Result::SUCCESS) {
274 LOGE("App node get %s failed!", COMPILE_SDK_TYPE.c_str());
275 return false;
276 }
277 } else {
278 compileSdkType = "";
279 }
280 return true;
281 }
282
GetStageCompileSdkVersion(std::string & compileSdkVersion)283 bool ModuleJson::GetStageCompileSdkVersion(std::string& compileSdkVersion)
284 {
285 std::unique_ptr<PtJson> appObj;
286 if (!GetAppObject(appObj)) {
287 LOGE("GetAppObject failed!");
288 return false;
289 }
290 return GetStageCompileSdkVersionByAppObj(appObj, compileSdkVersion);
291 }
292
GetStageCompileSdkVersionByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkVersion)293 bool ModuleJson::GetStageCompileSdkVersionByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkVersion)
294 {
295 if (!appObj) {
296 LOGE("App node is null!");
297 return false;
298 }
299 if (appObj->Contains(COMPILE_SDK_VERSION.c_str())) {
300 if (appObj->GetString(COMPILE_SDK_VERSION.c_str(), &compileSdkVersion) != Result::SUCCESS) {
301 LOGE("App node get %s failed!", COMPILE_SDK_VERSION.c_str());
302 return false;
303 }
304 } else {
305 compileSdkVersion = "";
306 }
307 return true;
308 }
309
GetStageDebug(bool & debug)310 bool ModuleJson::GetStageDebug(bool& debug)
311 {
312 std::unique_ptr<PtJson> appObj;
313 if (!GetAppObject(appObj)) {
314 LOGE("GetAppObject failed!");
315 return false;
316 }
317 return GetStageDebugByAppObj(appObj, debug);
318 }
319
GetStageDebugByAppObj(std::unique_ptr<PtJson> & appObj,bool & debug)320 bool ModuleJson::GetStageDebugByAppObj(std::unique_ptr<PtJson>& appObj, bool& debug)
321 {
322 if (!appObj) {
323 LOGE("App node is null!");
324 return false;
325 }
326 if (appObj->Contains(DEBUG.c_str())) {
327 if (appObj->GetBool(DEBUG.c_str(), &debug) != Result::SUCCESS) {
328 LOGE("App node get %s failed!", DEBUG.c_str());
329 return false;
330 }
331 } else {
332 debug = false;
333 }
334 return true;
335 }
336
337 // java: parseStageEntry / getDeviceTypesFromStageModule
GetStageEntry(std::list<std::string> & deviceTypes)338 bool ModuleJson::GetStageEntry(std::list<std::string>& deviceTypes)
339 {
340 std::unique_ptr<PtJson> moduleObj;
341 if (!GetModuleObject(moduleObj)) {
342 LOGE("GetModuleObject failed!");
343 return false;
344 }
345 std::string moduleType;
346 if (!moduleObj->Contains(TYPE.c_str())) {
347 LOGE("Module node has no %s node!", TYPE.c_str());
348 return false;
349 }
350 if (moduleObj->GetString(TYPE.c_str(), &moduleType) != Result::SUCCESS) {
351 LOGE("Module node get %s failed!", TYPE.c_str());
352 return false;
353 }
354 if (moduleType.compare(ENTRY) == 0) {
355 if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
356 LOGE("GetStageDeviceTypesByModuleObj failed!");
357 return false;
358 }
359 }
360 return true;
361 }
362
GetStageDeviceTypes(std::list<std::string> & deviceTypes)363 bool ModuleJson::GetStageDeviceTypes(std::list<std::string>& deviceTypes)
364 {
365 std::unique_ptr<PtJson> moduleObj;
366 if (!GetModuleObject(moduleObj)) {
367 LOGE("GetModuleObject failed!");
368 return false;
369 }
370 return GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes);
371 }
372
GetStageDeviceTypesByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & deviceTypes)373 bool ModuleJson::GetStageDeviceTypesByModuleObj(std::unique_ptr<PtJson>& moduleObj,
374 std::list<std::string>& deviceTypes)
375 {
376 if (!moduleObj) {
377 LOGE("Module node is null!");
378 return false;
379 }
380 if (!moduleObj->Contains(DEVICE_TYPES.c_str())) {
381 LOGE("Module node has no %s node!", DEVICE_TYPES.c_str());
382 return false;
383 }
384 std::unique_ptr<PtJson> deviceTypeObj;
385 if (moduleObj->GetArray(DEVICE_TYPES.c_str(), &deviceTypeObj) != Result::SUCCESS) {
386 LOGE("Module node get %s array node failed!", DEVICE_TYPES.c_str());
387 return false;
388 }
389 for (int32_t i = 0; i < deviceTypeObj->GetSize(); i++) {
390 deviceTypes.push_back(deviceTypeObj->Get(i)->GetString());
391 }
392 return true;
393 }
394
GetStageDistroFilter(DistroFilter & distroFilter,const std::map<std::string,std::string> & resourceMap)395 bool ModuleJson::GetStageDistroFilter(DistroFilter& distroFilter,
396 const std::map<std::string, std::string>& resourceMap)
397 {
398 std::list<ModuleMetadataInfo> moduleMetadataInfos;
399 if (!GetModuleMetadatas(moduleMetadataInfos, resourceMap)) {
400 LOGE("GetModuleMetadatas failed!");
401 return false;
402 }
403 return ParseModuleMetadatasToDistroFilter(moduleMetadataInfos, distroFilter);
404 }
405
GetStageDistroFilterByModuleObj(std::unique_ptr<PtJson> & moduleObj,const std::map<std::string,std::string> & resourceMap,DistroFilter & distroFilter)406 bool ModuleJson::GetStageDistroFilterByModuleObj(std::unique_ptr<PtJson>& moduleObj,
407 const std::map<std::string, std::string>& resourceMap, DistroFilter& distroFilter)
408 {
409 std::list<ModuleMetadataInfo> moduleMetadataInfos;
410 if (!GetModuleMetadatasByModuleObj(moduleObj, resourceMap, moduleMetadataInfos)) {
411 LOGE("GetModuleMetadatasByModuleObj failed!");
412 return false;
413 }
414 return ParseModuleMetadatasToDistroFilter(moduleMetadataInfos, distroFilter);
415 }
416
GetStageModuleType(std::string & moduleType)417 bool ModuleJson::GetStageModuleType(std::string& moduleType)
418 {
419 std::unique_ptr<PtJson> moduleObj;
420 if (!GetModuleObject(moduleObj)) {
421 LOGE("GetModuleObject failed!");
422 return false;
423 }
424 return GetStageModuleTypeByModuleObj(moduleObj, moduleType);
425 }
426
GetStageModuleTypeByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & moduleType)427 bool ModuleJson::GetStageModuleTypeByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleType)
428 {
429 if (!moduleObj) {
430 LOGE("Module node is null!");
431 return false;
432 }
433 if (moduleObj->Contains(TYPE.c_str())) {
434 if (moduleObj->GetString(TYPE.c_str(), &moduleType) != Result::SUCCESS) {
435 LOGE("Module node get %s failed!", TYPE.c_str());
436 return false;
437 }
438 } else {
439 moduleType = "";
440 }
441 return true;
442 }
443
GetStageBundleType(std::string & bundleType)444 bool ModuleJson::GetStageBundleType(std::string& bundleType)
445 {
446 std::unique_ptr<PtJson> appObj;
447 std::unique_ptr<PtJson> moduleObj;
448 if (!GetAppObject(appObj) || !GetModuleObject(moduleObj)) {
449 LOGE("GetAppObject or module node failed!");
450 return false;
451 }
452 if (!moduleObj->Contains(TYPE.c_str())) {
453 LOGE("Module node has no %s node!", TYPE.c_str());
454 return false;
455 }
456 std::string moduleName;
457 std::string moduleType;
458 if (!GetStageModuleNameByModuleObj(moduleObj, moduleName) ||
459 !GetStageModuleTypeByModuleObj(moduleObj, moduleType)) {
460 LOGE("GetStageModuleNameByModuleObj or GetStageModuleTypeByModuleObj failed!");
461 return false;
462 }
463 bool installationFree = false;
464 GetStageInstallationFreeByModuleObj(moduleObj, installationFree);
465 if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
466 if (installationFree) {
467 LOGE("The app.json5 file configuration does not match the installationFree: "
468 "true settings. Add the bundleType field to the app.json5 file and set it atomicService.");
469 return false;
470 }
471 bundleType = APP;
472 return true;
473 } else if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) == Result::SUCCESS) {
474 return CheckStageBundleType(moduleName, moduleType, bundleType, installationFree);
475 }
476 return false;
477 }
478
GetStageAsanEnabled(bool & asanEnabled)479 bool ModuleJson::GetStageAsanEnabled(bool& asanEnabled)
480 {
481 std::unique_ptr<PtJson> appObj;
482 if (!GetAppObject(appObj)) {
483 LOGE("GetAppObject failed!");
484 return false;
485 }
486 return GetStageAsanEnabledByAppObj(appObj, asanEnabled);
487 }
488
GetStageAsanEnabledByAppObj(std::unique_ptr<PtJson> & appObj,bool & asanEnabled)489 bool ModuleJson::GetStageAsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& asanEnabled)
490 {
491 if (!appObj) {
492 LOGE("App node is null!");
493 return false;
494 }
495 if (appObj->Contains(ASAN_ENABLED.c_str())) {
496 if (appObj->GetBool(ASAN_ENABLED.c_str(), &asanEnabled) != Result::SUCCESS) {
497 LOGE("App node get %s failed!", ASAN_ENABLED.c_str());
498 return false;
499 }
500 } else {
501 asanEnabled = false;
502 }
503 return true;
504 }
505
GetStageTsanEnabled(bool & tsanEnabled)506 bool ModuleJson::GetStageTsanEnabled(bool& tsanEnabled)
507 {
508 std::unique_ptr<PtJson> appObj;
509 if (!GetAppObject(appObj)) {
510 LOGE("GetAppObject failed!");
511 return false;
512 }
513 return GetStageTsanEnabledByAppObj(appObj, tsanEnabled);
514 }
515
GetStageTsanEnabledByAppObj(std::unique_ptr<PtJson> & appObj,bool & tsanEnabled)516 bool ModuleJson::GetStageTsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& tsanEnabled)
517 {
518 if (!appObj) {
519 LOGE("App node is null!");
520 return false;
521 }
522 if (appObj->Contains(TSAN_ENABLED.c_str())) {
523 if (appObj->GetBool(TSAN_ENABLED.c_str(), &tsanEnabled) != Result::SUCCESS) {
524 LOGE("App node get %s failed!", TSAN_ENABLED.c_str());
525 return false;
526 }
527 } else {
528 tsanEnabled = false;
529 }
530 return true;
531 }
532
GetStageCompressNativeLibs(bool & compressNativeLibs)533 bool ModuleJson::GetStageCompressNativeLibs(bool& compressNativeLibs)
534 {
535 std::unique_ptr<PtJson> appObj;
536 if (!GetAppObject(appObj)) {
537 LOGE("GetAppObject failed!");
538 return false;
539 }
540 return GetStageCompressNativeLibsByAppObj(appObj, compressNativeLibs);
541 }
542
GetStageCompressNativeLibsByAppObj(std::unique_ptr<PtJson> & appObj,bool & compressNativeLibs)543 bool ModuleJson::GetStageCompressNativeLibsByAppObj(std::unique_ptr<PtJson>& appObj, bool& compressNativeLibs)
544 {
545 if (!appObj) {
546 LOGE("App node is null!");
547 return false;
548 }
549 if (appObj->Contains(COMPRESS_NATIVE_LIBS.c_str())) {
550 if (appObj->GetBool(COMPRESS_NATIVE_LIBS.c_str(), &compressNativeLibs) != Result::SUCCESS) {
551 LOGE("App node get %s failed!", COMPRESS_NATIVE_LIBS.c_str());
552 return false;
553 }
554 } else {
555 compressNativeLibs = false;
556 }
557 return true;
558 }
559
560 // apiReleaseType was included in ModuleApiVersion.
GetStageApiReleaseType(std::string & apiReleaseType)561 bool ModuleJson::GetStageApiReleaseType(std::string& apiReleaseType)
562 {
563 std::unique_ptr<PtJson> appObj;
564 if (!GetAppObject(appObj)) {
565 LOGE("GetAppObject failed!");
566 return false;
567 }
568 return GetStageApiReleaseTypeByAppObj(appObj, apiReleaseType);
569 }
570
GetStageApiReleaseTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & apiReleaseType)571 bool ModuleJson::GetStageApiReleaseTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& apiReleaseType)
572 {
573 if (!appObj) {
574 LOGE("App node is null!");
575 return false;
576 }
577 if (appObj->Contains(API_RELEASE_TYPE.c_str())) {
578 if (appObj->GetString(API_RELEASE_TYPE.c_str(), &apiReleaseType) != Result::SUCCESS) {
579 LOGE("App node get %s failed!", API_RELEASE_TYPE.c_str());
580 return false;
581 }
582 } else {
583 apiReleaseType = "";
584 }
585 return true;
586 }
587
588 // java Compressor.java : parseStageHapVerifyInfo / ModuleJsonUtil.java : parseStageHapVerifyInfo
589 // The following parameters require additional settings
590 // SetStageModule: stage is true, fa is false
591 // SetFileLength
592 // SetResourceMap
593 // SetProfileStr: this param can be delete, because it is also parsed by MODULE_JSON file
GetStageHapVerifyInfo(HapVerifyInfo & hapVerifyInfo)594 bool ModuleJson::GetStageHapVerifyInfo(HapVerifyInfo& hapVerifyInfo)
595 {
596 std::unique_ptr<PtJson> appObj;
597 std::unique_ptr<PtJson> moduleObj;
598 if (!GetAppObject(appObj)) {
599 LOGE("GetAppObject failed!");
600 return false;
601 }
602 if (!GetModuleObject(moduleObj)) {
603 LOGE("GetModuleObject failed!");
604 return false;
605 }
606 std::string bundleName;
607 std::string bundleType;
608 std::list<DependencyItem> dependencyItems;
609 if (!GetBundleNameByAppObj(appObj, bundleName)) {
610 LOGE("GetBundleNameByAppObj failed!");
611 return false;
612 }
613 if (!GetStageBundleType(bundleType)) {
614 LOGE("GetStageBundleType failed!");
615 return false;
616 }
617 if (!GetDependencyItemsByModuleObj(moduleObj, dependencyItems, bundleName)) {
618 LOGE("GetDependencyItemsByModuleObj failed!");
619 return false;
620 }
621 hapVerifyInfo.SetBundleName(bundleName);
622 hapVerifyInfo.SetBundleType(bundleType);
623 hapVerifyInfo.SetDependencyItemList(dependencyItems);
624 if (!SetStageHapVerifyInfoByAppObj(appObj, hapVerifyInfo)) {
625 LOGE("SetStageHapVerifyInfoByAppObj failed!");
626 return false;
627 }
628 if (!SetStageHapVerifyInfoByModuleObj(moduleObj, hapVerifyInfo)) {
629 LOGE("SetStageHapVerifyInfoByModuleObj failed!");
630 return false;
631 }
632 if (!SetStageHapVerifyInfoExtByModuleObj(moduleObj, hapVerifyInfo)) {
633 LOGE("SetStageHapVerifyInfoExtByModuleObj failed!");
634 return false;
635 }
636 return true;
637 }
638
SetStageHapVerifyInfoByAppObj(std::unique_ptr<PtJson> & appObj,HapVerifyInfo & hapVerifyInfo)639 bool ModuleJson::SetStageHapVerifyInfoByAppObj(std::unique_ptr<PtJson>& appObj, HapVerifyInfo& hapVerifyInfo)
640 {
641 if (!appObj) {
642 LOGE("App node is null!");
643 return false;
644 }
645 std::string vendor;
646 Version version;
647 ModuleApiVersion moduleApiVersion;
648 std::string targetBundleName;
649 int32_t targetPriority = 0;
650 bool debug = false;
651 MultiAppMode multiAppMode;
652 std::list<std::string> assetAccessGroups;
653 if (!GetVendorByAppObj(appObj, vendor)) {
654 LOGE("GetVendorByAppObj failed!");
655 return false;
656 }
657 if (!GetStageVersionByAppObj(appObj, version)) {
658 LOGE("GetStageVersionByAppObj failed!");
659 return false;
660 }
661 if (!GetStageModuleApiVersionByAppObj(appObj, moduleApiVersion)) {
662 LOGE("GetStageModuleApiVersionByAppObj failed!");
663 return false;
664 }
665 if (!GetTargetBundleNameByAppObj(appObj, targetBundleName)) {
666 LOGE("GetTargetBundleNameByAppObj failed!");
667 return false;
668 }
669 if (!GetTargetPriorityByAppObj(appObj, targetPriority)) {
670 LOGE("GetTargetPriorityByAppObj failed!");
671 return false;
672 }
673 if (!GetStageDebugByAppObj(appObj, debug)) {
674 LOGE("GetStageDebugByAppObj failed!");
675 return false;
676 }
677 if (!GetMultiAppModeByAppObj(appObj, multiAppMode)) {
678 LOGE("GetMultiAppModeByAppObj failed!");
679 return false;
680 }
681 if (!GetAssetAccessGroupsByModuleObj(appObj, assetAccessGroups)) {
682 LOGE("GetAssetAccessGroupsByModuleObj failed!");
683 return false;
684 }
685 hapVerifyInfo.SetVendor(vendor);
686 hapVerifyInfo.SetVersion(version);
687 hapVerifyInfo.SetApiVersion(moduleApiVersion);
688 hapVerifyInfo.SetTargetBundleName(targetBundleName);
689 hapVerifyInfo.SetTargetPriority(targetPriority);
690 hapVerifyInfo.SetDebug(debug);
691 hapVerifyInfo.SetMultiAppMode(multiAppMode);
692 hapVerifyInfo.SetAssetAccessGroups(assetAccessGroups);
693 return true;
694 }
695
SetStageHapVerifyInfoByModuleObj(std::unique_ptr<PtJson> & moduleObj,HapVerifyInfo & hapVerifyInfo)696 bool ModuleJson::SetStageHapVerifyInfoByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo)
697 {
698 if (!moduleObj) {
699 LOGE("Module node is null!");
700 return false;
701 }
702 std::string moduleName;
703 std::list<std::string> deviceTypes;
704 std::string moduleType;
705 bool installationFree = false;
706 std::string targetModuleName;
707 int32_t targetModulePriority = 0;
708 std::string compileSdkType;
709 std::string compileSdkVersion;
710 if (!GetStageModuleNameByModuleObj(moduleObj, moduleName)) {
711 LOGE("GetStageModuleNameByModuleObj failed!");
712 return false;
713 }
714 if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
715 LOGE("GetStageDeviceTypesByModuleObj failed!");
716 return false;
717 }
718 if (!GetStageModuleTypeByModuleObj(moduleObj, moduleType)) {
719 LOGE("GetStageModuleTypeByModuleObj failed!");
720 return false;
721 }
722 if (!GetStageInstallationFreeByModuleObj(moduleObj, installationFree)) {
723 LOGE("GetStageInstallationFreeByModuleObj failed!");
724 return false;
725 }
726 if (!GetTargetModuleNameByModuleObj(moduleObj, targetModuleName) ||
727 !GetTargetModulePriorityByModuleObj(moduleObj, targetModulePriority)) {
728 LOGE("GetTargetModuleNameByModuleObj or GetTargetModulePriorityByModuleObj failed!");
729 return false;
730 }
731 if (!GetStageCompileSdkTypeByAppObj(moduleObj, compileSdkType) ||
732 !GetStageCompileSdkVersionByAppObj(moduleObj, compileSdkVersion)) {
733 LOGE("GetStageCompileSdkTypeByAppObj or GetStageCompileSdkVersionByAppObj failed!");
734 return false;
735 }
736 hapVerifyInfo.SetModuleName(moduleName);
737 hapVerifyInfo.SetDeviceTypes(deviceTypes);
738 hapVerifyInfo.SetModuleType(moduleType);
739 hapVerifyInfo.SetInstallationFree(installationFree);
740 hapVerifyInfo.SetTargetModuleName(targetModuleName);
741 hapVerifyInfo.SetTargetModulePriority(targetModulePriority);
742 hapVerifyInfo.SetCompileSdkType(compileSdkType);
743 hapVerifyInfo.SetCompileSdkVersion(compileSdkVersion);
744 return true;
745 }
746
SetStageHapVerifyInfoExtByModuleObj(std::unique_ptr<PtJson> & moduleObj,HapVerifyInfo & hapVerifyInfo)747 bool ModuleJson::SetStageHapVerifyInfoExtByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo)
748 {
749 if (!moduleObj) {
750 LOGE("Module node is null!");
751 return false;
752 }
753 DistroFilter distroFilter;
754 std::list<std::string> abilityNames;
755 std::list<std::string> extensionAbilityNames;
756 std::list<PreloadItem> preloadItems;
757 std::list<std::string> proxyDataUris;
758 std::map<std::string, std::list<std::string>> abilityContinueTypeMap;
759 if (!GetStageDistroFilterByModuleObj(moduleObj, hapVerifyInfo.GetResourceMap(), distroFilter)) {
760 LOGE("GetStageDistroFilterByModuleObj failed!");
761 return false;
762 }
763 if (!GetAbilityNamesByModuleObj(moduleObj, abilityNames)) {
764 LOGE("GetAbilityNamesByModuleObj failed!");
765 return false;
766 }
767 if (!GetExtensionAbilityNamesByModuleObj(moduleObj, extensionAbilityNames)) {
768 LOGE("GetExtensionAbilityNamesByModuleObj failed!");
769 return false;
770 }
771 if (!GetAtomicServicePreloadsByModuleObj(moduleObj, preloadItems)) {
772 LOGE("GetAtomicServicePreloadsByModuleObj failed!");
773 return false;
774 }
775 if (!GetProxyDataUrisByModuleObj(moduleObj, proxyDataUris)) {
776 LOGE("GetProxyDataUrisByModuleObj failed!");
777 return false;
778 }
779 if (!GetAbilityContinueTypeMapByModuleObj(moduleObj, abilityContinueTypeMap)) {
780 LOGE("GetAbilityContinueTypeMapByModuleObj failed!");
781 return false;
782 }
783 hapVerifyInfo.SetDistroFilter(distroFilter);
784 hapVerifyInfo.SetAbilityNames(abilityNames);
785 hapVerifyInfo.AddAbilityNames(extensionAbilityNames);
786 hapVerifyInfo.SetPreloadItems(preloadItems);
787 hapVerifyInfo.SetProxyDataUris(proxyDataUris);
788 hapVerifyInfo.SetContinueTypeMap(abilityContinueTypeMap);
789 return true;
790 }
791 } // namespace AppPackingTool
792 } // namespace OHOS
793