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
SetStageVersionCode(const int32_t & versionCode)91 bool ModuleJson::SetStageVersionCode(const int32_t& versionCode)
92 {
93 std::unique_ptr<PtJson> appObj;
94 if (!GetAppObject(appObj)) {
95 LOGE("GetAppObject failed!");
96 return false;
97 }
98 if (!appObj->Contains(VERSIONCODE.c_str())) {
99 LOGE("App node has no %s node!", VERSIONCODE.c_str());
100 return false;
101 }
102 if (appObj->SetInt(VERSIONCODE.c_str(), versionCode) != Result::SUCCESS) {
103 LOGE("App node set %s failed!", VERSIONCODE.c_str());
104 return false;
105 }
106 return true;
107 }
108
SetStageVersionName(const std::string & versionName)109 bool ModuleJson::SetStageVersionName(const std::string& versionName)
110 {
111 std::unique_ptr<PtJson> appObj;
112 if (!GetAppObject(appObj)) {
113 LOGE("GetAppObject failed!");
114 return false;
115 }
116 if (!appObj->Contains(VERSIONNAME.c_str())) {
117 LOGE("App node has no %s node!", VERSIONNAME.c_str());
118 return false;
119 }
120 if (appObj->SetString(VERSIONNAME.c_str(), versionName) != Result::SUCCESS) {
121 LOGE("App node set %s failed!", VERSIONNAME.c_str());
122 return false;
123 }
124 return true;
125 }
126
SetStageBundleName(const std::string & bundleName)127 bool ModuleJson::SetStageBundleName(const std::string& bundleName)
128 {
129 std::unique_ptr<PtJson> appObj;
130 if (!GetAppObject(appObj)) {
131 LOGE("GetAppObject failed!");
132 return false;
133 }
134 if (!appObj->Contains(BUNDLE_NAME.c_str())) {
135 if (!appObj->Add(BUNDLE_NAME.c_str(), bundleName.c_str())) {
136 LOGE("App node add %s failed!", BUNDLE_NAME.c_str());
137 return false;
138 }
139 return true;
140 }
141 if (appObj->SetString(BUNDLE_NAME.c_str(), bundleName) != Result::SUCCESS) {
142 LOGE("App node set %s failed!", BUNDLE_NAME.c_str());
143 return false;
144 }
145 return true;
146 }
147
SetStageMinCompatibleVersionCode(const int32_t & minCompatibleVersionCode)148 bool ModuleJson::SetStageMinCompatibleVersionCode(const int32_t& minCompatibleVersionCode)
149 {
150 std::unique_ptr<PtJson> appObj;
151 if (!GetAppObject(appObj)) {
152 LOGE("GetAppObject failed!");
153 return false;
154 }
155 if (!appObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
156 if (!appObj->Add(MIN_COMPATIBLE_VERSION_CODE.c_str(), minCompatibleVersionCode)) {
157 LOGE("App node add %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
158 return false;
159 }
160 return true;
161 }
162 if (appObj->SetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(), minCompatibleVersionCode) != Result::SUCCESS) {
163 LOGE("App node set %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
164 return false;
165 }
166 return true;
167 }
168
SetStageMinAPIVersion(const int32_t & minAPIVersion)169 bool ModuleJson::SetStageMinAPIVersion(const int32_t& minAPIVersion)
170 {
171 std::unique_ptr<PtJson> appObj;
172 if (!GetAppObject(appObj)) {
173 LOGE("GetAppObject failed!");
174 return false;
175 }
176 if (!appObj->Contains(MIN_API_VERSION.c_str())) {
177 if (!appObj->Add(MIN_API_VERSION.c_str(), minAPIVersion)) {
178 LOGE("App node add %s failed!", MIN_API_VERSION.c_str());
179 return false;
180 }
181 return true;
182 }
183 if (appObj->SetInt(MIN_API_VERSION.c_str(), minAPIVersion) != Result::SUCCESS) {
184 LOGE("App node set %s failed!", MIN_API_VERSION.c_str());
185 return false;
186 }
187 return true;
188 }
189
SetStageTargetAPIVersion(const int32_t & targetAPIVersion)190 bool ModuleJson::SetStageTargetAPIVersion(const int32_t& targetAPIVersion)
191 {
192 std::unique_ptr<PtJson> appObj;
193 if (!GetAppObject(appObj)) {
194 LOGE("GetAppObject failed!");
195 return false;
196 }
197 if (!appObj->Contains(TARGET_API_VERSION.c_str())) {
198 if (!appObj->Add(TARGET_API_VERSION.c_str(), targetAPIVersion)) {
199 LOGE("App node add %s failed!", TARGET_API_VERSION.c_str());
200 return false;
201 }
202 return true;
203 }
204 if (appObj->SetInt(TARGET_API_VERSION.c_str(), targetAPIVersion) != Result::SUCCESS) {
205 LOGE("App node set %s failed!", TARGET_API_VERSION.c_str());
206 return false;
207 }
208 return true;
209 }
210
SetStageApiReleaseType(const std::string & apiReleaseType)211 bool ModuleJson::SetStageApiReleaseType(const std::string& apiReleaseType)
212 {
213 std::unique_ptr<PtJson> appObj;
214 if (!GetAppObject(appObj)) {
215 LOGE("GetAppObject failed!");
216 return false;
217 }
218 if (!appObj->Contains(API_RELEASE_TYPE.c_str())) {
219 if (!appObj->Add(API_RELEASE_TYPE.c_str(), apiReleaseType.c_str())) {
220 LOGE("App node add %s failed!", API_RELEASE_TYPE.c_str());
221 return false;
222 }
223 return true;
224 }
225 if (appObj->SetString(API_RELEASE_TYPE.c_str(), apiReleaseType) != Result::SUCCESS) {
226 LOGE("App node set %s failed!", API_RELEASE_TYPE.c_str());
227 return false;
228 }
229 return true;
230 }
231
SetStageBundleType(const std::string & bundleType)232 bool ModuleJson::SetStageBundleType(const std::string& bundleType)
233 {
234 std::unique_ptr<PtJson> appObj;
235 if (!GetAppObject(appObj)) {
236 LOGE("GetAppObject failed!");
237 return false;
238 }
239 if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
240 if (!appObj->Add(BUNDLE_TYPE.c_str(), bundleType.c_str())) {
241 LOGE("App node add %s failed!", BUNDLE_TYPE.c_str());
242 return false;
243 }
244 return true;
245 }
246 if (appObj->SetString(BUNDLE_TYPE.c_str(), bundleType) != Result::SUCCESS) {
247 LOGE("App node set %s failed!", BUNDLE_TYPE.c_str());
248 return false;
249 }
250 return true;
251 }
252
SetStageInstallationFree(const bool & installationFree)253 bool ModuleJson::SetStageInstallationFree(const bool& installationFree)
254 {
255 std::unique_ptr<PtJson> moduleObj;
256 if (!GetModuleObject(moduleObj)) {
257 LOGE("GetModuleObject failed!");
258 return false;
259 }
260 if (!moduleObj->Contains(INSTALLATION_FREE.c_str())) {
261 if (!moduleObj->Add(INSTALLATION_FREE.c_str(), installationFree)) {
262 LOGE("App node add %s failed!", INSTALLATION_FREE.c_str());
263 return false;
264 }
265 return true;
266 }
267 if (moduleObj->SetBool(INSTALLATION_FREE.c_str(), installationFree) != Result::SUCCESS) {
268 LOGE("Module node set %s failed!", INSTALLATION_FREE.c_str());
269 return false;
270 }
271 return true;
272 }
273
SetStageDeliveryWithInstall(const bool & deliveryWithInstall)274 bool ModuleJson::SetStageDeliveryWithInstall(const bool& deliveryWithInstall)
275 {
276 std::unique_ptr<PtJson> moduleObj;
277 if (!GetModuleObject(moduleObj)) {
278 LOGE("GetModuleObject failed!");
279 return false;
280 }
281 if (!moduleObj->Contains(DELIVERY_WITH_INSTALL.c_str())) {
282 if (!moduleObj->Add(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall)) {
283 LOGE("App node add %s failed!", DELIVERY_WITH_INSTALL.c_str());
284 return false;
285 }
286 return true;
287 }
288 if (moduleObj->SetBool(DELIVERY_WITH_INSTALL.c_str(), deliveryWithInstall) != Result::SUCCESS) {
289 LOGE("Module node set %s failed!", DELIVERY_WITH_INSTALL.c_str());
290 return false;
291 }
292 return true;
293 }
294
SetStageDeviceTypes(const std::list<std::string> & deviceTypes)295 bool ModuleJson::SetStageDeviceTypes(const std::list<std::string>& deviceTypes)
296 {
297 std::unique_ptr<PtJson> moduleObj;
298 if (!GetModuleObject(moduleObj)) {
299 LOGE("GetModuleObject failed!");
300 return false;
301 }
302 if (!moduleObj->Contains(DEVICE_TYPES.c_str())) {
303 if (!moduleObj->Add(DEVICE_TYPES.c_str(), deviceTypes)) {
304 LOGE("App node add %s failed!", DEVICE_TYPES.c_str());
305 return false;
306 }
307 return true;
308 }
309 if (moduleObj->SetArray(DEVICE_TYPES.c_str(), deviceTypes) != Result::SUCCESS) {
310 LOGE("Module node set %s failed!", DEVICE_TYPES.c_str());
311 return false;
312 }
313 return true;
314 }
315
GetStageVersion(Version & version)316 bool ModuleJson::GetStageVersion(Version& version)
317 {
318 std::unique_ptr<PtJson> appObj;
319 if (!GetAppObject(appObj)) {
320 LOGE("GetAppObject failed!");
321 return false;
322 }
323 return GetStageVersionByAppObj(appObj, version);
324 }
325
GetStageVersionByAppObj(std::unique_ptr<PtJson> & appObj,Version & version)326 bool ModuleJson::GetStageVersionByAppObj(std::unique_ptr<PtJson>& appObj, Version& version)
327 {
328 if (!appObj) {
329 LOGE("App node is null!");
330 return false;
331 }
332 if (!appObj->Contains(VERSIONCODE.c_str()) || !appObj->Contains(VERSIONNAME.c_str())) {
333 LOGE("App node has no %s node or %s node", VERSIONCODE.c_str(), VERSIONNAME.c_str());
334 return false;
335 }
336 if (appObj->GetInt(VERSIONCODE.c_str(), &version.versionCode) != Result::SUCCESS) {
337 LOGE("App node get %s failed!", VERSIONCODE.c_str());
338 return false;
339 }
340 if (appObj->GetString(VERSIONNAME.c_str(), &version.versionName) != Result::SUCCESS) {
341 LOGE("App node get %s failed!", VERSIONNAME.c_str());
342 return false;
343 }
344 if (appObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
345 if (appObj->GetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(),
346 &version.minCompatibleVersionCode) != Result::SUCCESS) {
347 LOGE("App node get %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
348 return false;
349 }
350 } else {
351 version.minCompatibleVersionCode = version.versionCode;
352 }
353 return true;
354 }
355
GetStageInstallationFree(bool & installationFree)356 bool ModuleJson::GetStageInstallationFree(bool& installationFree)
357 {
358 std::unique_ptr<PtJson> moduleObj;
359 if (!GetModuleObject(moduleObj)) {
360 LOGE("GetModuleObject failed!");
361 return false;
362 }
363 return GetStageInstallationFreeByModuleObj(moduleObj, installationFree);
364 }
365
GetStageInstallationFreeByModuleObj(std::unique_ptr<PtJson> & moduleObj,bool & installationFree)366 bool ModuleJson::GetStageInstallationFreeByModuleObj(std::unique_ptr<PtJson>& moduleObj, bool& installationFree)
367 {
368 if (!moduleObj) {
369 LOGE("Module node is null!");
370 return false;
371 }
372 installationFree = false;
373 if (moduleObj->Contains(INSTALLATION_FREE.c_str())) {
374 if (moduleObj->GetBool(INSTALLATION_FREE.c_str(), &installationFree) != Result::SUCCESS) {
375 LOGE("Module node get %s failed!", INSTALLATION_FREE.c_str());
376 return false;
377 }
378 }
379 return true;
380 }
381
GetStageModuleApiVersion(ModuleApiVersion & moduleApiVersion)382 bool ModuleJson::GetStageModuleApiVersion(ModuleApiVersion& moduleApiVersion)
383 {
384 std::unique_ptr<PtJson> appObj;
385 if (!GetAppObject(appObj)) {
386 LOGE("GetAppObject failed!");
387 return false;
388 }
389 return GetStageModuleApiVersionByAppObj(appObj, moduleApiVersion);
390 }
391
GetStageModuleApiVersionByAppObj(std::unique_ptr<PtJson> & appObj,ModuleApiVersion & moduleApiVersion)392 bool ModuleJson::GetStageModuleApiVersionByAppObj(std::unique_ptr<PtJson>& appObj, ModuleApiVersion& moduleApiVersion)
393 {
394 if (!appObj) {
395 LOGE("App node is null!");
396 return false;
397 }
398 if (appObj->Contains(MIN_API_VERSION.c_str())) {
399 if (appObj->GetInt(MIN_API_VERSION.c_str(), &moduleApiVersion.compatibleApiVersion) != Result::SUCCESS) {
400 LOGE("App node get %s failed!", MIN_API_VERSION.c_str());
401 return false;
402 }
403 }
404 if (appObj->Contains(TARGET_API_VERSION.c_str())) {
405 if (appObj->GetInt(TARGET_API_VERSION.c_str(), &moduleApiVersion.targetApiVersion) != Result::SUCCESS) {
406 LOGE("App node get %s failed!", TARGET_API_VERSION.c_str());
407 return false;
408 }
409 }
410 if (appObj->Contains(API_RELEASE_TYPE.c_str())) {
411 if (appObj->GetString(API_RELEASE_TYPE.c_str(), &moduleApiVersion.releaseType) != Result::SUCCESS) {
412 LOGE("App node get %s failed!", API_RELEASE_TYPE.c_str());
413 return false;
414 }
415 }
416 return true;
417 }
418
GetStageModuleName(std::string & stageModuleName)419 bool ModuleJson::GetStageModuleName(std::string& stageModuleName)
420 {
421 std::unique_ptr<PtJson> moduleObj;
422 if (!GetModuleObject(moduleObj)) {
423 LOGE("GetModuleObject failed!");
424 return false;
425 }
426 return GetStageModuleNameByModuleObj(moduleObj, stageModuleName);
427 }
428
GetStageModuleNameByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & stageModuleName)429 bool ModuleJson::GetStageModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& stageModuleName)
430 {
431 if (!moduleObj) {
432 LOGE("Module node is null!");
433 return false;
434 }
435 if (!moduleObj->Contains(NAME.c_str())) {
436 LOGE("Module node has no %s node!", NAME.c_str());
437 return false;
438 }
439 if (moduleObj->GetString(NAME.c_str(), &stageModuleName) != Result::SUCCESS) {
440 LOGE("Module node get %s failed!", NAME.c_str());
441 return false;
442 }
443 return true;
444 }
445
GetStageCompileSdkType(std::string & compileSdkType)446 bool ModuleJson::GetStageCompileSdkType(std::string& compileSdkType)
447 {
448 std::unique_ptr<PtJson> appObj;
449 if (!GetAppObject(appObj)) {
450 LOGE("GetAppObject failed!");
451 return false;
452 }
453 return GetStageCompileSdkTypeByAppObj(appObj, compileSdkType);
454 }
455
GetStageCompileSdkTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkType)456 bool ModuleJson::GetStageCompileSdkTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkType)
457 {
458 if (!appObj) {
459 LOGE("App node is null!");
460 return false;
461 }
462 if (appObj->Contains(COMPILE_SDK_TYPE.c_str())) {
463 if (appObj->GetString(COMPILE_SDK_TYPE.c_str(), &compileSdkType) != Result::SUCCESS) {
464 LOGE("App node get %s failed!", COMPILE_SDK_TYPE.c_str());
465 return false;
466 }
467 } else {
468 compileSdkType = "";
469 }
470 return true;
471 }
472
GetStageCompileSdkVersion(std::string & compileSdkVersion)473 bool ModuleJson::GetStageCompileSdkVersion(std::string& compileSdkVersion)
474 {
475 std::unique_ptr<PtJson> appObj;
476 if (!GetAppObject(appObj)) {
477 LOGE("GetAppObject failed!");
478 return false;
479 }
480 return GetStageCompileSdkVersionByAppObj(appObj, compileSdkVersion);
481 }
482
GetStageCompileSdkVersionByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkVersion)483 bool ModuleJson::GetStageCompileSdkVersionByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkVersion)
484 {
485 if (!appObj) {
486 LOGE("App node is null!");
487 return false;
488 }
489 if (appObj->Contains(COMPILE_SDK_VERSION.c_str())) {
490 if (appObj->GetString(COMPILE_SDK_VERSION.c_str(), &compileSdkVersion) != Result::SUCCESS) {
491 LOGE("App node get %s failed!", COMPILE_SDK_VERSION.c_str());
492 return false;
493 }
494 } else {
495 compileSdkVersion = "";
496 }
497 return true;
498 }
499
GetStageDebug(bool & debug)500 bool ModuleJson::GetStageDebug(bool& debug)
501 {
502 std::unique_ptr<PtJson> appObj;
503 if (!GetAppObject(appObj)) {
504 LOGE("GetAppObject failed!");
505 return false;
506 }
507 return GetStageDebugByAppObj(appObj, debug);
508 }
509
GetStageDebugByAppObj(std::unique_ptr<PtJson> & appObj,bool & debug)510 bool ModuleJson::GetStageDebugByAppObj(std::unique_ptr<PtJson>& appObj, bool& debug)
511 {
512 if (!appObj) {
513 LOGE("App node is null!");
514 return false;
515 }
516 if (appObj->Contains(DEBUG.c_str())) {
517 if (appObj->GetBool(DEBUG.c_str(), &debug) != Result::SUCCESS) {
518 LOGE("App node get %s failed!", DEBUG.c_str());
519 return false;
520 }
521 } else {
522 debug = false;
523 }
524 return true;
525 }
526
527 // java: parseStageEntry / getDeviceTypesFromStageModule
GetStageEntry(std::list<std::string> & deviceTypes)528 bool ModuleJson::GetStageEntry(std::list<std::string>& deviceTypes)
529 {
530 std::unique_ptr<PtJson> moduleObj;
531 if (!GetModuleObject(moduleObj)) {
532 LOGE("GetModuleObject failed!");
533 return false;
534 }
535 std::string moduleType;
536 if (!moduleObj->Contains(TYPE.c_str())) {
537 LOGE("Module node has no %s node!", TYPE.c_str());
538 return false;
539 }
540 if (moduleObj->GetString(TYPE.c_str(), &moduleType) != Result::SUCCESS) {
541 LOGE("Module node get %s failed!", TYPE.c_str());
542 return false;
543 }
544 if (moduleType.compare(ENTRY) == 0) {
545 if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
546 LOGE("GetStageDeviceTypesByModuleObj failed!");
547 return false;
548 }
549 }
550 return true;
551 }
552
GetStageDeviceTypes(std::list<std::string> & deviceTypes)553 bool ModuleJson::GetStageDeviceTypes(std::list<std::string>& deviceTypes)
554 {
555 std::unique_ptr<PtJson> moduleObj;
556 if (!GetModuleObject(moduleObj)) {
557 LOGE("GetModuleObject failed!");
558 return false;
559 }
560 return GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes);
561 }
562
GetStageDeviceTypesByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & deviceTypes)563 bool ModuleJson::GetStageDeviceTypesByModuleObj(std::unique_ptr<PtJson>& moduleObj,
564 std::list<std::string>& deviceTypes)
565 {
566 if (!moduleObj) {
567 LOGE("Module node is null!");
568 return false;
569 }
570 if (!moduleObj->Contains(DEVICE_TYPES.c_str())) {
571 LOGE("Module node has no %s node!", DEVICE_TYPES.c_str());
572 return false;
573 }
574 std::unique_ptr<PtJson> deviceTypeObj;
575 if (moduleObj->GetArray(DEVICE_TYPES.c_str(), &deviceTypeObj) != Result::SUCCESS) {
576 LOGE("Module node get %s array node failed!", DEVICE_TYPES.c_str());
577 return false;
578 }
579 for (int32_t i = 0; i < deviceTypeObj->GetSize(); i++) {
580 deviceTypes.push_back(deviceTypeObj->Get(i)->GetString());
581 }
582 return true;
583 }
584
GetStageDistroFilter(DistroFilter & distroFilter,const std::map<std::string,std::string> & resourceMap)585 bool ModuleJson::GetStageDistroFilter(DistroFilter& distroFilter,
586 const std::map<std::string, std::string>& resourceMap)
587 {
588 std::list<ModuleMetadataInfo> moduleMetadataInfos;
589 if (!GetModuleMetadatas(moduleMetadataInfos, resourceMap)) {
590 LOGE("GetModuleMetadatas failed!");
591 return false;
592 }
593 return ParseModuleMetadatasToDistroFilter(moduleMetadataInfos, distroFilter);
594 }
595
GetStageDistroFilterByModuleObj(std::unique_ptr<PtJson> & moduleObj,const std::map<std::string,std::string> & resourceMap,DistroFilter & distroFilter)596 bool ModuleJson::GetStageDistroFilterByModuleObj(std::unique_ptr<PtJson>& moduleObj,
597 const std::map<std::string, std::string>& resourceMap, DistroFilter& distroFilter)
598 {
599 std::list<ModuleMetadataInfo> moduleMetadataInfos;
600 if (!GetModuleMetadatasByModuleObj(moduleObj, resourceMap, moduleMetadataInfos)) {
601 LOGE("GetModuleMetadatasByModuleObj failed!");
602 return false;
603 }
604 return ParseModuleMetadatasToDistroFilter(moduleMetadataInfos, distroFilter);
605 }
606
GetStageModuleType(std::string & moduleType)607 bool ModuleJson::GetStageModuleType(std::string& moduleType)
608 {
609 std::unique_ptr<PtJson> moduleObj;
610 if (!GetModuleObject(moduleObj)) {
611 LOGE("GetModuleObject failed!");
612 return false;
613 }
614 return GetStageModuleTypeByModuleObj(moduleObj, moduleType);
615 }
616
GetStageModuleTypeByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & moduleType)617 bool ModuleJson::GetStageModuleTypeByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleType)
618 {
619 if (!moduleObj) {
620 LOGE("Module node is null!");
621 return false;
622 }
623 if (moduleObj->Contains(TYPE.c_str())) {
624 if (moduleObj->GetString(TYPE.c_str(), &moduleType) != Result::SUCCESS) {
625 LOGE("Module node get %s failed!", TYPE.c_str());
626 return false;
627 }
628 } else {
629 moduleType = "";
630 }
631 return true;
632 }
633
GetStageBundleType(std::string & bundleType)634 bool ModuleJson::GetStageBundleType(std::string& bundleType)
635 {
636 std::unique_ptr<PtJson> appObj;
637 std::unique_ptr<PtJson> moduleObj;
638 if (!GetAppObject(appObj) || !GetModuleObject(moduleObj)) {
639 LOGE("GetAppObject or module node failed!");
640 return false;
641 }
642 if (!moduleObj->Contains(TYPE.c_str())) {
643 LOGE("Module node has no %s node!", TYPE.c_str());
644 return false;
645 }
646 std::string moduleName;
647 std::string moduleType;
648 if (!GetStageModuleNameByModuleObj(moduleObj, moduleName) ||
649 !GetStageModuleTypeByModuleObj(moduleObj, moduleType)) {
650 LOGE("GetStageModuleNameByModuleObj or GetStageModuleTypeByModuleObj failed!");
651 return false;
652 }
653 bool installationFree = false;
654 GetStageInstallationFreeByModuleObj(moduleObj, installationFree);
655 if (!appObj->Contains(BUNDLE_TYPE.c_str())) {
656 if (installationFree) {
657 LOGE("The app.json5 file configuration does not match the installationFree: "
658 "true settings. Add the bundleType field to the app.json5 file and set it atomicService.");
659 return false;
660 }
661 bundleType = APP;
662 return true;
663 } else if (appObj->GetString(BUNDLE_TYPE.c_str(), &bundleType) == Result::SUCCESS) {
664 return CheckStageBundleType(moduleName, moduleType, bundleType, installationFree);
665 }
666 return false;
667 }
668
GetStageAsanEnabled(bool & asanEnabled)669 bool ModuleJson::GetStageAsanEnabled(bool& asanEnabled)
670 {
671 std::unique_ptr<PtJson> appObj;
672 if (!GetAppObject(appObj)) {
673 LOGE("GetAppObject failed!");
674 return false;
675 }
676 return GetStageAsanEnabledByAppObj(appObj, asanEnabled);
677 }
678
GetStageAsanEnabledByAppObj(std::unique_ptr<PtJson> & appObj,bool & asanEnabled)679 bool ModuleJson::GetStageAsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& asanEnabled)
680 {
681 if (!appObj) {
682 LOGE("App node is null!");
683 return false;
684 }
685 if (appObj->Contains(ASAN_ENABLED.c_str())) {
686 if (appObj->GetBool(ASAN_ENABLED.c_str(), &asanEnabled) != Result::SUCCESS) {
687 LOGE("App node get %s failed!", ASAN_ENABLED.c_str());
688 return false;
689 }
690 } else {
691 asanEnabled = false;
692 }
693 return true;
694 }
695
GetStageTsanEnabled(bool & tsanEnabled)696 bool ModuleJson::GetStageTsanEnabled(bool& tsanEnabled)
697 {
698 std::unique_ptr<PtJson> appObj;
699 if (!GetAppObject(appObj)) {
700 LOGE("GetAppObject failed!");
701 return false;
702 }
703 return GetStageTsanEnabledByAppObj(appObj, tsanEnabled);
704 }
705
GetStageTsanEnabledByAppObj(std::unique_ptr<PtJson> & appObj,bool & tsanEnabled)706 bool ModuleJson::GetStageTsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& tsanEnabled)
707 {
708 if (!appObj) {
709 LOGE("App node is null!");
710 return false;
711 }
712 if (appObj->Contains(TSAN_ENABLED.c_str())) {
713 if (appObj->GetBool(TSAN_ENABLED.c_str(), &tsanEnabled) != Result::SUCCESS) {
714 LOGE("App node get %s failed!", TSAN_ENABLED.c_str());
715 return false;
716 }
717 } else {
718 tsanEnabled = false;
719 }
720 return true;
721 }
722
GetStageCompressNativeLibs(bool & compressNativeLibs)723 bool ModuleJson::GetStageCompressNativeLibs(bool& compressNativeLibs)
724 {
725 std::unique_ptr<PtJson> appObj;
726 if (!GetAppObject(appObj)) {
727 LOGE("GetAppObject failed!");
728 return false;
729 }
730 return GetStageCompressNativeLibsByAppObj(appObj, compressNativeLibs);
731 }
732
GetStageCompressNativeLibsByAppObj(std::unique_ptr<PtJson> & appObj,bool & compressNativeLibs)733 bool ModuleJson::GetStageCompressNativeLibsByAppObj(std::unique_ptr<PtJson>& appObj, bool& compressNativeLibs)
734 {
735 if (!appObj) {
736 LOGE("App node is null!");
737 return false;
738 }
739 if (appObj->Contains(COMPRESS_NATIVE_LIBS.c_str())) {
740 if (appObj->GetBool(COMPRESS_NATIVE_LIBS.c_str(), &compressNativeLibs) != Result::SUCCESS) {
741 LOGE("App node get %s failed!", COMPRESS_NATIVE_LIBS.c_str());
742 return false;
743 }
744 } else {
745 compressNativeLibs = false;
746 }
747 return true;
748 }
749
750 // apiReleaseType was included in ModuleApiVersion.
GetStageApiReleaseType(std::string & apiReleaseType)751 bool ModuleJson::GetStageApiReleaseType(std::string& apiReleaseType)
752 {
753 std::unique_ptr<PtJson> appObj;
754 if (!GetAppObject(appObj)) {
755 LOGE("GetAppObject failed!");
756 return false;
757 }
758 return GetStageApiReleaseTypeByAppObj(appObj, apiReleaseType);
759 }
760
GetStageApiReleaseTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & apiReleaseType)761 bool ModuleJson::GetStageApiReleaseTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& apiReleaseType)
762 {
763 if (!appObj) {
764 LOGE("App node is null!");
765 return false;
766 }
767 if (appObj->Contains(API_RELEASE_TYPE.c_str())) {
768 if (appObj->GetString(API_RELEASE_TYPE.c_str(), &apiReleaseType) != Result::SUCCESS) {
769 LOGE("App node get %s failed!", API_RELEASE_TYPE.c_str());
770 return false;
771 }
772 } else {
773 apiReleaseType = "";
774 }
775 return true;
776 }
777
778 // java Compressor.java : parseStageHapVerifyInfo / ModuleJsonUtil.java : parseStageHapVerifyInfo
779 // The following parameters require additional settings
780 // SetStageModule: stage is true, fa is false
781 // SetFileLength
782 // SetResourceMap
783 // SetProfileStr: this param can be delete, because it is also parsed by MODULE_JSON file
GetStageHapVerifyInfo(HapVerifyInfo & hapVerifyInfo)784 bool ModuleJson::GetStageHapVerifyInfo(HapVerifyInfo& hapVerifyInfo)
785 {
786 std::unique_ptr<PtJson> appObj;
787 std::unique_ptr<PtJson> moduleObj;
788 if (!GetAppObject(appObj)) {
789 LOGE("GetAppObject failed!");
790 return false;
791 }
792 if (!GetModuleObject(moduleObj)) {
793 LOGE("GetModuleObject failed!");
794 return false;
795 }
796 std::string bundleName;
797 std::string bundleType;
798 std::list<DependencyItem> dependencyItems;
799 if (!GetBundleNameByAppObj(appObj, bundleName)) {
800 LOGE("GetBundleNameByAppObj failed!");
801 return false;
802 }
803 if (!GetStageBundleType(bundleType)) {
804 LOGE("GetStageBundleType failed!");
805 return false;
806 }
807 if (!GetDependencyItemsByModuleObj(moduleObj, dependencyItems, bundleName)) {
808 LOGE("GetDependencyItemsByModuleObj failed!");
809 return false;
810 }
811 hapVerifyInfo.SetBundleName(bundleName);
812 hapVerifyInfo.SetBundleType(bundleType);
813 hapVerifyInfo.SetDependencyItemList(dependencyItems);
814 if (!SetStageHapVerifyInfoByAppObj(appObj, hapVerifyInfo)) {
815 LOGE("SetStageHapVerifyInfoByAppObj failed!");
816 return false;
817 }
818 if (!SetStageHapVerifyInfoByModuleObj(moduleObj, hapVerifyInfo)) {
819 LOGE("SetStageHapVerifyInfoByModuleObj failed!");
820 return false;
821 }
822 if (!SetStageHapVerifyInfoExtByModuleObj(moduleObj, hapVerifyInfo)) {
823 LOGE("SetStageHapVerifyInfoExtByModuleObj failed!");
824 return false;
825 }
826 return true;
827 }
828
SetStageHapVerifyInfoByAppObj(std::unique_ptr<PtJson> & appObj,HapVerifyInfo & hapVerifyInfo)829 bool ModuleJson::SetStageHapVerifyInfoByAppObj(std::unique_ptr<PtJson>& appObj, HapVerifyInfo& hapVerifyInfo)
830 {
831 if (!appObj) {
832 LOGE("App node is null!");
833 return false;
834 }
835 std::string vendor;
836 Version version;
837 ModuleApiVersion moduleApiVersion;
838 std::string targetBundleName;
839 int32_t targetPriority = 0;
840 bool debug = false;
841 MultiAppMode multiAppMode;
842 std::list<std::string> assetAccessGroups;
843 if (!GetVendorByAppObj(appObj, vendor)) {
844 LOGE("GetVendorByAppObj failed!");
845 return false;
846 }
847 if (!GetStageVersionByAppObj(appObj, version)) {
848 LOGE("GetStageVersionByAppObj failed!");
849 return false;
850 }
851 if (!GetStageModuleApiVersionByAppObj(appObj, moduleApiVersion)) {
852 LOGE("GetStageModuleApiVersionByAppObj failed!");
853 return false;
854 }
855 if (!GetTargetBundleNameByAppObj(appObj, targetBundleName)) {
856 LOGE("GetTargetBundleNameByAppObj failed!");
857 return false;
858 }
859 if (!GetTargetPriorityByAppObj(appObj, targetPriority)) {
860 LOGE("GetTargetPriorityByAppObj failed!");
861 return false;
862 }
863 if (!GetStageDebugByAppObj(appObj, debug)) {
864 LOGE("GetStageDebugByAppObj failed!");
865 return false;
866 }
867 if (!GetMultiAppModeByAppObj(appObj, multiAppMode)) {
868 LOGE("GetMultiAppModeByAppObj failed!");
869 return false;
870 }
871 if (!GetAssetAccessGroupsByModuleObj(appObj, assetAccessGroups)) {
872 LOGE("GetAssetAccessGroupsByModuleObj failed!");
873 return false;
874 }
875 hapVerifyInfo.SetVendor(vendor);
876 hapVerifyInfo.SetVersion(version);
877 hapVerifyInfo.SetApiVersion(moduleApiVersion);
878 hapVerifyInfo.SetTargetBundleName(targetBundleName);
879 hapVerifyInfo.SetTargetPriority(targetPriority);
880 hapVerifyInfo.SetDebug(debug);
881 hapVerifyInfo.SetMultiAppMode(multiAppMode);
882 hapVerifyInfo.SetAssetAccessGroups(assetAccessGroups);
883 return true;
884 }
885
SetStageHapVerifyInfoByModuleObj(std::unique_ptr<PtJson> & moduleObj,HapVerifyInfo & hapVerifyInfo)886 bool ModuleJson::SetStageHapVerifyInfoByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo)
887 {
888 if (!moduleObj) {
889 LOGE("Module node is null!");
890 return false;
891 }
892 std::string moduleName;
893 std::list<std::string> deviceTypes;
894 std::string moduleType;
895 bool installationFree = false;
896 std::string targetModuleName;
897 int32_t targetModulePriority = 0;
898 std::string compileSdkType;
899 std::string compileSdkVersion;
900 if (!GetStageModuleNameByModuleObj(moduleObj, moduleName)) {
901 LOGE("GetStageModuleNameByModuleObj failed!");
902 return false;
903 }
904 if (!GetStageDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
905 LOGE("GetStageDeviceTypesByModuleObj failed!");
906 return false;
907 }
908 if (!GetStageModuleTypeByModuleObj(moduleObj, moduleType)) {
909 LOGE("GetStageModuleTypeByModuleObj failed!");
910 return false;
911 }
912 if (!GetStageInstallationFreeByModuleObj(moduleObj, installationFree)) {
913 LOGE("GetStageInstallationFreeByModuleObj failed!");
914 return false;
915 }
916 if (!GetTargetModuleNameByModuleObj(moduleObj, targetModuleName) ||
917 !GetTargetModulePriorityByModuleObj(moduleObj, targetModulePriority)) {
918 LOGE("GetTargetModuleNameByModuleObj or GetTargetModulePriorityByModuleObj failed!");
919 return false;
920 }
921 if (!GetStageCompileSdkTypeByAppObj(moduleObj, compileSdkType) ||
922 !GetStageCompileSdkVersionByAppObj(moduleObj, compileSdkVersion)) {
923 LOGE("GetStageCompileSdkTypeByAppObj or GetStageCompileSdkVersionByAppObj failed!");
924 return false;
925 }
926 hapVerifyInfo.SetModuleName(moduleName);
927 hapVerifyInfo.SetDeviceTypes(deviceTypes);
928 hapVerifyInfo.SetModuleType(moduleType);
929 hapVerifyInfo.SetInstallationFree(installationFree);
930 hapVerifyInfo.SetTargetModuleName(targetModuleName);
931 hapVerifyInfo.SetTargetModulePriority(targetModulePriority);
932 hapVerifyInfo.SetCompileSdkType(compileSdkType);
933 hapVerifyInfo.SetCompileSdkVersion(compileSdkVersion);
934 return true;
935 }
936
SetStageHapVerifyInfoExtByModuleObj(std::unique_ptr<PtJson> & moduleObj,HapVerifyInfo & hapVerifyInfo)937 bool ModuleJson::SetStageHapVerifyInfoExtByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo)
938 {
939 if (!moduleObj) {
940 LOGE("Module node is null!");
941 return false;
942 }
943 DistroFilter distroFilter;
944 std::list<std::string> abilityNames;
945 std::list<std::string> extensionAbilityNames;
946 std::list<PreloadItem> preloadItems;
947 std::list<std::string> proxyDataUris;
948 std::map<std::string, std::list<std::string>> abilityContinueTypeMap;
949 if (!GetStageDistroFilterByModuleObj(moduleObj, hapVerifyInfo.GetResourceMap(), distroFilter)) {
950 LOGE("GetStageDistroFilterByModuleObj failed!");
951 return false;
952 }
953 if (!GetAbilityNamesByModuleObj(moduleObj, abilityNames)) {
954 LOGE("GetAbilityNamesByModuleObj failed!");
955 return false;
956 }
957 if (!GetExtensionAbilityNamesByModuleObj(moduleObj, extensionAbilityNames)) {
958 LOGE("GetExtensionAbilityNamesByModuleObj failed!");
959 return false;
960 }
961 if (!GetAtomicServicePreloadsByModuleObj(moduleObj, preloadItems)) {
962 LOGE("GetAtomicServicePreloadsByModuleObj failed!");
963 return false;
964 }
965 if (!GetProxyDataUrisByModuleObj(moduleObj, proxyDataUris)) {
966 LOGE("GetProxyDataUrisByModuleObj failed!");
967 return false;
968 }
969 if (!GetAbilityContinueTypeMapByModuleObj(moduleObj, abilityContinueTypeMap)) {
970 LOGE("GetAbilityContinueTypeMapByModuleObj failed!");
971 return false;
972 }
973 hapVerifyInfo.SetDistroFilter(distroFilter);
974 hapVerifyInfo.SetAbilityNames(abilityNames);
975 hapVerifyInfo.AddAbilityNames(extensionAbilityNames);
976 hapVerifyInfo.SetPreloadItems(preloadItems);
977 hapVerifyInfo.SetProxyDataUris(proxyDataUris);
978 hapVerifyInfo.SetContinueTypeMap(abilityContinueTypeMap);
979 return true;
980 }
981 } // namespace AppPackingTool
982 } // namespace OHOS
983