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
GetFaVersion(Version & version)90 bool ModuleJson::GetFaVersion(Version& version)
91 {
92 std::unique_ptr<PtJson> appObj;
93 if (!GetAppObject(appObj)) {
94 LOGE("GetAppObject failed!");
95 return false;
96 }
97 return GetFaVersionByAppObj(appObj, version);
98 }
99
GetFaVersionByAppObj(std::unique_ptr<PtJson> & appObj,Version & version)100 bool ModuleJson::GetFaVersionByAppObj(std::unique_ptr<PtJson>& appObj, Version& version)
101 {
102 if (!appObj) {
103 LOGE("App node is null!");
104 return false;
105 }
106 if (!appObj->Contains(VERSION.c_str())) {
107 LOGE("App node has no %s node!", VERSION.c_str());
108 return false;
109 }
110 std::unique_ptr<PtJson> versionObj;
111 if (appObj->GetObject(VERSION.c_str(), &versionObj) != Result::SUCCESS) {
112 LOGE("App node get %s node failed!", VERSION.c_str());
113 return false;
114 }
115 return GetFaVersionByVersionObj(versionObj, version);
116 }
117
GetFaVersionByVersionObj(std::unique_ptr<PtJson> & versionObj,Version & version)118 bool ModuleJson::GetFaVersionByVersionObj(std::unique_ptr<PtJson>& versionObj, Version& version)
119 {
120 if (!versionObj) {
121 LOGE("Version node is null!");
122 return false;
123 }
124 if (!versionObj->Contains(CODE.c_str()) || !versionObj->Contains(NAME.c_str())) {
125 LOGE("Version node has no %s node or %s node", CODE.c_str(), NAME.c_str());
126 return false;
127 }
128 if (versionObj->GetInt(CODE.c_str(), &version.versionCode) != Result::SUCCESS) {
129 LOGE("Version node get %s failed!", CODE.c_str());
130 return false;
131 }
132 if (versionObj->GetString(NAME.c_str(), &version.versionName) != Result::SUCCESS) {
133 LOGE("Version node get %s failed!", NAME.c_str());
134 return false;
135 }
136 if (versionObj->Contains(MIN_COMPATIBLE_VERSION_CODE.c_str())) {
137 if (versionObj->GetInt(MIN_COMPATIBLE_VERSION_CODE.c_str(),
138 &version.minCompatibleVersionCode) != Result::SUCCESS) {
139 LOGE("Version node get %s failed!", MIN_COMPATIBLE_VERSION_CODE.c_str());
140 return false;
141 }
142 } else {
143 version.minCompatibleVersionCode = version.versionCode;
144 }
145 return true;
146 }
147
SetFaVersionCode(const int32_t & versionCode)148 bool ModuleJson::SetFaVersionCode(const int32_t& versionCode)
149 {
150 std::unique_ptr<PtJson> versionObj;
151 if (!GetVersionObject(versionObj)) {
152 LOGE("GetVersionObject failed!");
153 return false;
154 }
155 if (!versionObj->Contains(CODE.c_str())) {
156 LOGE("Version node has no %s node!", CODE.c_str());
157 return false;
158 }
159 if (versionObj->SetInt(CODE.c_str(), versionCode) != Result::SUCCESS) {
160 LOGE("Version node set %s failed!", CODE.c_str());
161 return false;
162 }
163 return true;
164 }
165
SetFaVersionName(const std::string & versionName)166 bool ModuleJson::SetFaVersionName(const std::string& versionName)
167 {
168 std::unique_ptr<PtJson> versionObj;
169 if (!GetVersionObject(versionObj)) {
170 LOGE("GetVersionObject failed!");
171 return false;
172 }
173 if (!versionObj->Contains(NAME.c_str())) {
174 LOGE("Version node has no %s node!", NAME.c_str());
175 return false;
176 }
177 if (versionObj->SetString(NAME.c_str(), versionName) != Result::SUCCESS) {
178 LOGE("Version node set %s failed!", NAME.c_str());
179 return false;
180 }
181 return true;
182 }
183
GetFaInstallationFree(bool & installationFree)184 bool ModuleJson::GetFaInstallationFree(bool& installationFree)
185 {
186 std::unique_ptr<PtJson> distroObj;
187 if (!GetDistroObject(distroObj)) {
188 LOGE("GetDistroObject failed!");
189 return false;
190 }
191 return GetFaInstallationFreeByDistroObj(distroObj, installationFree);
192 }
193
GetFaInstallationFreeByModuleObj(std::unique_ptr<PtJson> & moduleObj,bool & installationFree)194 bool ModuleJson::GetFaInstallationFreeByModuleObj(std::unique_ptr<PtJson>& moduleObj, bool& installationFree)
195 {
196 if (!moduleObj) {
197 LOGE("Module node is null!");
198 return false;
199 }
200 std::unique_ptr<PtJson> distroObj;
201 if (!GetDistroObjectByModuleObj(moduleObj, distroObj)) {
202 LOGE("GetDistroObjectByModuleObj failed!");
203 return false;
204 }
205 return GetFaInstallationFreeByDistroObj(distroObj, installationFree);
206 }
207
GetFaInstallationFreeByDistroObj(std::unique_ptr<PtJson> & distroObj,bool & installationFree)208 bool ModuleJson::GetFaInstallationFreeByDistroObj(std::unique_ptr<PtJson>& distroObj, bool& installationFree)
209 {
210 if (!distroObj) {
211 LOGE("Distro node is null!");
212 return false;
213 }
214 installationFree = false;
215 if (distroObj->Contains(INSTALLATION_FREE.c_str())) {
216 if (distroObj->GetBool(INSTALLATION_FREE.c_str(), &installationFree) != Result::SUCCESS) {
217 LOGE("Distro node get %s failed!", INSTALLATION_FREE.c_str());
218 return false;
219 }
220 }
221 return true;
222 }
223
GetFaBundleType(std::string & bundleType)224 bool ModuleJson::GetFaBundleType(std::string& bundleType)
225 {
226 bool installationFree = false;
227 if (!GetFaInstallationFree(installationFree)) {
228 return false;
229 }
230 if (installationFree) {
231 bundleType = ATOMIC_SERVICE;
232 } else {
233 bundleType = APP;
234 }
235 return true;
236 }
237
GetFaModuleApiVersion(ModuleApiVersion & moduleApiVersion)238 bool ModuleJson::GetFaModuleApiVersion(ModuleApiVersion& moduleApiVersion)
239 {
240 std::unique_ptr<PtJson> appObj;
241 if (!GetAppObject(appObj)) {
242 LOGE("GetAppObject failed!");
243 return false;
244 }
245 return GetFaModuleApiVersionByAppObj(appObj, moduleApiVersion);
246 }
247
GetFaModuleApiVersionByAppObj(std::unique_ptr<PtJson> & appObj,ModuleApiVersion & moduleApiVersion)248 bool ModuleJson::GetFaModuleApiVersionByAppObj(std::unique_ptr<PtJson>& appObj, ModuleApiVersion& moduleApiVersion)
249 {
250 if (!appObj) {
251 LOGE("App node is null!");
252 return false;
253 }
254 if (!appObj->Contains(API_VERSION.c_str())) {
255 LOGE("App node has no %s node!", API_VERSION.c_str());
256 return false;
257 }
258 std::unique_ptr<PtJson> apiVersionObj;
259 if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
260 LOGE("App node get %s node failed!", API_VERSION.c_str());
261 return false;
262 }
263 return GetFaModuleApiVersionByApiVersionObj(apiVersionObj, moduleApiVersion);
264 }
265
GetFaModuleApiVersionByApiVersionObj(std::unique_ptr<PtJson> & apiVersionObj,ModuleApiVersion & moduleApiVersion)266 bool ModuleJson::GetFaModuleApiVersionByApiVersionObj(
267 std::unique_ptr<PtJson>& apiVersionObj, ModuleApiVersion& moduleApiVersion)
268 {
269 if (!apiVersionObj) {
270 LOGE("ApiVersion node is null!");
271 return false;
272 }
273 if (apiVersionObj->Contains(COMPATIBLE.c_str())) {
274 if (apiVersionObj->GetInt(COMPATIBLE.c_str(), &moduleApiVersion.compatibleApiVersion) != Result::SUCCESS) {
275 LOGE("ApiVersion node get %s node failed!", COMPATIBLE.c_str());
276 return false;
277 }
278 }
279 if (apiVersionObj->Contains(TARGET.c_str())) {
280 if (apiVersionObj->GetInt(TARGET.c_str(), &moduleApiVersion.targetApiVersion) != Result::SUCCESS) {
281 LOGE("ApiVersion node get %s node failed!", TARGET.c_str());
282 return false;
283 }
284 }
285 if (apiVersionObj->Contains(RELEASE_TYPE.c_str())) {
286 if (apiVersionObj->GetString(RELEASE_TYPE.c_str(), &moduleApiVersion.releaseType) != Result::SUCCESS) {
287 LOGE("ApiVersion node get %s node failed!", RELEASE_TYPE.c_str());
288 return false;
289 }
290 }
291 return true;
292 }
293
GetFaModuleName(std::string & faModuleName)294 bool ModuleJson::GetFaModuleName(std::string& faModuleName)
295 {
296 std::unique_ptr<PtJson> moduleObj;
297 if (!GetModuleObject(moduleObj)) {
298 LOGE("GetModuleObject failed!");
299 return false;
300 }
301 std::unique_ptr<PtJson> distroObj;
302 if (!GetDistroObjectByModuleObj(moduleObj, distroObj)) {
303 LOGE("GetDistroObjectByModuleObj failed!");
304 return false;
305 }
306 if (!distroObj->Contains(MODULE_NAME.c_str())) {
307 LOGE("Distro node has no %s node!", MODULE_NAME.c_str());
308 return false;
309 }
310 if (distroObj->GetString(MODULE_NAME.c_str(), &faModuleName) != Result::SUCCESS) {
311 LOGE("Distro node get %s node failed!", MODULE_NAME.c_str());
312 return false;
313 }
314 return true;
315 }
316
GetFaModuleNameByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & faModuleName)317 bool ModuleJson::GetFaModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& faModuleName)
318 {
319 if (!moduleObj) {
320 LOGE("Module node is null!");
321 return false;
322 }
323 std::unique_ptr<PtJson> distroObj;
324 if (!GetDistroObjectByModuleObj(moduleObj, distroObj)) {
325 LOGE("GetDistroObjectByModuleObj failed!");
326 return false;
327 }
328 return GetFaModuleNameByDistroObj(distroObj, faModuleName);
329 }
330
GetFaModuleNameByDistroObj(std::unique_ptr<PtJson> & distroObj,std::string & faModuleName)331 bool ModuleJson::GetFaModuleNameByDistroObj(std::unique_ptr<PtJson>& distroObj, std::string& faModuleName)
332 {
333 if (!distroObj) {
334 LOGE("Distro node is null!");
335 return false;
336 }
337 if (!distroObj->Contains(MODULE_NAME.c_str())) {
338 LOGE("Distro node has no %s node!", MODULE_NAME.c_str());
339 return false;
340 }
341 if (distroObj->GetString(MODULE_NAME.c_str(), &faModuleName) != Result::SUCCESS) {
342 LOGE("Distro node get %s node failed!", MODULE_NAME.c_str());
343 return false;
344 }
345 return true;
346 }
347
GetFaPackageStr(std::string & packageStr)348 bool ModuleJson::GetFaPackageStr(std::string& packageStr)
349 {
350 std::unique_ptr<PtJson> moduleObj;
351 if (!GetModuleObject(moduleObj)) {
352 LOGE("GetModuleObject failed!");
353 return false;
354 }
355 return GetFaPackageStrByModuleObj(moduleObj, packageStr);
356 }
357
GetFaPackageStrByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & packageStr)358 bool ModuleJson::GetFaPackageStrByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& packageStr)
359 {
360 if (!moduleObj) {
361 LOGE("Module node is null!");
362 return false;
363 }
364 if (!moduleObj->Contains(PACKAGE.c_str())) {
365 LOGE("Module node has no %s node!", PACKAGE.c_str());
366 return false;
367 }
368 if (moduleObj->GetString(PACKAGE.c_str(), &packageStr) != Result::SUCCESS) {
369 LOGE("Module node get %s failed!", PACKAGE.c_str());
370 return false;
371 }
372 return true;
373 }
374
GetFaCompileSdkType(std::string & compileSdkType)375 bool ModuleJson::GetFaCompileSdkType(std::string& compileSdkType)
376 {
377 std::unique_ptr<PtJson> appObj;
378 if (!GetAppObject(appObj)) {
379 LOGE("GetAppObject failed!");
380 return false;
381 }
382 return GetFaCompileSdkTypeByAppObj(appObj, compileSdkType);
383 }
384
GetFaCompileSdkTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkType)385 bool ModuleJson::GetFaCompileSdkTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkType)
386 {
387 if (!appObj) {
388 LOGE("App node is null!");
389 return false;
390 }
391 if (!appObj->Contains(API_VERSION.c_str())) {
392 LOGE("App node has no %s node!", API_VERSION.c_str());
393 return false;
394 }
395 std::unique_ptr<PtJson> apiVersionObj;
396 if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
397 LOGE("App node get %s node failed!", API_VERSION.c_str());
398 return false;
399 }
400 if (apiVersionObj->Contains(COMPILE_SDK_TYPE.c_str())) {
401 if (apiVersionObj->GetString(COMPILE_SDK_TYPE.c_str(), &compileSdkType) != Result::SUCCESS) {
402 LOGE("ApiVersion node get %s failed!", COMPILE_SDK_TYPE.c_str());
403 return false;
404 }
405 } else {
406 compileSdkType = "";
407 }
408 return true;
409 }
410
GetFaCompileSdkVersion(std::string & compileSdkVersion)411 bool ModuleJson::GetFaCompileSdkVersion(std::string& compileSdkVersion)
412 {
413 std::unique_ptr<PtJson> appObj;
414 if (!GetAppObject(appObj)) {
415 LOGE("GetAppObject failed!");
416 return false;
417 }
418 return GetFaCompileSdkVersionByAppObj(appObj, compileSdkVersion);
419 }
420
GetFaCompileSdkVersionByAppObj(std::unique_ptr<PtJson> & appObj,std::string & compileSdkVersion)421 bool ModuleJson::GetFaCompileSdkVersionByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkVersion)
422 {
423 if (!appObj) {
424 LOGE("App node is null!");
425 return false;
426 }
427 if (!appObj->Contains(API_VERSION.c_str())) {
428 LOGE("App node has no %s node!", API_VERSION.c_str());
429 return false;
430 }
431 std::unique_ptr<PtJson> apiVersionObj;
432 if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
433 LOGE("App node get %s node failed!", API_VERSION.c_str());
434 return false;
435 }
436 if (apiVersionObj->Contains(COMPILE_SDK_VERSION.c_str())) {
437 if (apiVersionObj->GetString(COMPILE_SDK_VERSION.c_str(), &compileSdkVersion) != Result::SUCCESS) {
438 LOGE("ApiVersion node get %s failed!", COMPILE_SDK_VERSION.c_str());
439 return false;
440 }
441 } else {
442 compileSdkVersion = "";
443 }
444 return true;
445 }
446
GetFaDebug(bool & debug)447 bool ModuleJson::GetFaDebug(bool& debug)
448 {
449 std::unique_ptr<PtJson> deviceConfigObj;
450 if (!GetDeviceConfigObject(deviceConfigObj)) {
451 LOGE("GetAppObject failed!");
452 return false;
453 }
454 return GetFaDebugByDeviceConfigObj(deviceConfigObj, debug);
455 }
456
GetFaDebugByDeviceConfigObj(std::unique_ptr<PtJson> & deviceConfigObj,bool & debug)457 bool ModuleJson::GetFaDebugByDeviceConfigObj(std::unique_ptr<PtJson>& deviceConfigObj, bool& debug)
458 {
459 if (!deviceConfigObj) {
460 LOGE("DeviceConfig node is null!");
461 return false;
462 }
463 if (!deviceConfigObj->Contains(DEFAULT.c_str())) {
464 LOGE("DeviceConfig node has no %s node!", DEFAULT.c_str());
465 return false;
466 }
467 std::unique_ptr<PtJson> defaultObj;
468 if (deviceConfigObj->GetObject(DEFAULT.c_str(), &defaultObj) != Result::SUCCESS) {
469 LOGE("DeviceConfig node get %s failed!", DEFAULT.c_str());
470 return false;
471 }
472 if (defaultObj->Contains(DEBUG.c_str())) {
473 if (defaultObj->GetBool(DEBUG.c_str(), &debug) != Result::SUCCESS) {
474 LOGE("debug node get %s failed!", DEBUG.c_str());
475 return false;
476 }
477 }
478 return true;
479 }
480
481 // java: parseFaEntry / getDeviceTypeFromFAModule
482 // parseFaEntry not called anywhere
GetFaEntry(std::list<std::string> & deviceTypes)483 bool ModuleJson::GetFaEntry(std::list<std::string>& deviceTypes)
484 {
485 std::unique_ptr<PtJson> moduleObj;
486 if (!GetModuleObject(moduleObj)) {
487 LOGE("GetModuleObject failed!");
488 return false;
489 }
490 std::unique_ptr<PtJson> distroObj;
491 if (!moduleObj->Contains(DISTRO.c_str())) {
492 LOGE("Module node has no %s node!", DISTRO.c_str());
493 return false;
494 }
495 if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
496 LOGE("Module node get %s node failed!", DISTRO.c_str());
497 return false;
498 }
499 std::string moduleType;
500 if (!distroObj->Contains(MODULE_TYPE.c_str())) {
501 LOGE("Distro node has no %s node!", MODULE_TYPE.c_str());
502 return false;
503 }
504 if (distroObj->GetString(MODULE_TYPE.c_str(), &moduleType) != Result::SUCCESS) {
505 LOGE("Distro node get %s failed!", MODULE_TYPE.c_str());
506 return false;
507 }
508 if (moduleType.compare(ENTRY) == 0) {
509 if (!GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
510 LOGE("GetFaDeviceTypesByModuleObj failed!");
511 return false;
512 }
513 }
514 return true;
515 }
516
GetFaDeviceTypes(std::list<std::string> & deviceTypes)517 bool ModuleJson::GetFaDeviceTypes(std::list<std::string>& deviceTypes)
518 {
519 std::unique_ptr<PtJson> moduleObj;
520 if (!GetModuleObject(moduleObj)) {
521 LOGE("GetModuleObject failed!");
522 return false;
523 }
524 return GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes);
525 }
526
GetFaDeviceTypesByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::list<std::string> & deviceTypes)527 bool ModuleJson::GetFaDeviceTypesByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& deviceTypes)
528 {
529 if (!moduleObj) {
530 LOGE("Module node is null!");
531 return false;
532 }
533 if (!moduleObj->Contains(DEVICE_TYPE.c_str())) {
534 LOGE("Module node has no %s node!", DEVICE_TYPE.c_str());
535 return false;
536 }
537 std::unique_ptr<PtJson> deviceTypeObj;
538 if (moduleObj->GetArray(DEVICE_TYPE.c_str(), &deviceTypeObj) != Result::SUCCESS) {
539 LOGE("Module node get %s array node failed!", DEVICE_TYPE.c_str());
540 return false;
541 }
542 for (int32_t i = 0; i < deviceTypeObj->GetSize(); i++) {
543 deviceTypes.push_back(deviceTypeObj->Get(i)->GetString());
544 }
545 return true;
546 }
547
GetFaModuleType(std::string & moduleType)548 bool ModuleJson::GetFaModuleType(std::string& moduleType)
549 {
550 std::unique_ptr<PtJson> moduleObj;
551 if (!GetModuleObject(moduleObj)) {
552 LOGE("GetModuleObject failed!");
553 return false;
554 }
555 return GetFaModuleTypeByModuleObj(moduleObj, moduleType);
556 }
557
558 // java : parseFAIsEntry
GetFaModuleTypeByModuleObj(std::unique_ptr<PtJson> & moduleObj,std::string & moduleType)559 bool ModuleJson::GetFaModuleTypeByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleType)
560 {
561 if (!moduleObj) {
562 LOGE("Module node is null!");
563 return false;
564 }
565 moduleType = "";
566 if (moduleObj->Contains(DISTRO.c_str())) {
567 std::unique_ptr<PtJson> distroObj;
568 if (moduleObj->GetObject(DISTRO.c_str(), &distroObj) != Result::SUCCESS) {
569 LOGE("Module node get %s node failed!", DISTRO.c_str());
570 return false;
571 }
572 if (distroObj->Contains(MODULE_TYPE.c_str())) {
573 if (distroObj->GetString(MODULE_TYPE.c_str(), &moduleType) != Result::SUCCESS) {
574 LOGE("Module node get %s failed!", MODULE_TYPE.c_str());
575 return false;
576 }
577 }
578 }
579 return true;
580 }
581
GetFaReleaseType(std::string & releaseType)582 bool ModuleJson::GetFaReleaseType(std::string& releaseType)
583 {
584 std::unique_ptr<PtJson> appObj;
585 if (!GetAppObject(appObj)) {
586 LOGE("GetAppObject failed!");
587 return false;
588 }
589 return GetFaReleaseTypeByAppObj(appObj, releaseType);
590 }
591
GetFaReleaseTypeByAppObj(std::unique_ptr<PtJson> & appObj,std::string & releaseType)592 bool ModuleJson::GetFaReleaseTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& releaseType)
593 {
594 if (!appObj) {
595 LOGE("App node is null!");
596 return false;
597 }
598 std::unique_ptr<PtJson> apiVersionObj;
599 releaseType = "";
600 if (appObj->Contains(API_VERSION.c_str())) {
601 if (appObj->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
602 LOGE("App node get %s node failed!", API_VERSION.c_str());
603 return false;
604 }
605 if (apiVersionObj->Contains(RELEASE_TYPE.c_str())) {
606 if (apiVersionObj->GetString(RELEASE_TYPE.c_str(), &releaseType) != Result::SUCCESS) {
607 LOGE("App node get %s failed!", RELEASE_TYPE.c_str());
608 return false;
609 }
610 }
611 }
612 return true;
613 }
614
GetFaAsanEnabled(bool & asanEnabled)615 bool ModuleJson::GetFaAsanEnabled(bool& asanEnabled)
616 {
617 std::unique_ptr<PtJson> appObj;
618 if (!GetAppObject(appObj)) {
619 LOGE("GetAppObject failed!");
620 return false;
621 }
622 return GetFaAsanEnabledByAppObj(appObj, asanEnabled);
623 }
624
GetFaAsanEnabledByAppObj(std::unique_ptr<PtJson> & appObj,bool & asanEnabled)625 bool ModuleJson::GetFaAsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& asanEnabled)
626 {
627 if (!appObj) {
628 LOGE("App node is null!");
629 return false;
630 }
631 if (appObj->Contains(ASAN_ENABLED.c_str())) {
632 if (appObj->GetBool(ASAN_ENABLED.c_str(), &asanEnabled) != Result::SUCCESS) {
633 LOGE("App node get %s failed!", ASAN_ENABLED.c_str());
634 return false;
635 }
636 } else {
637 asanEnabled = false;
638 }
639 return true;
640 }
641
GetFaDistroFilter(DistroFilter & distroFilter)642 bool ModuleJson::GetFaDistroFilter(DistroFilter& distroFilter)
643 {
644 std::unique_ptr<PtJson> moduleObj;
645 if (!GetModuleObject(moduleObj)) {
646 LOGE("GetModuleObject failed!");
647 return false;
648 }
649 return GetFaDistroFilterByModuleObj(moduleObj, distroFilter);
650 }
651
GetFaDistroFilterByModuleObj(std::unique_ptr<PtJson> & moduleObj,DistroFilter & distroFilter)652 bool ModuleJson::GetFaDistroFilterByModuleObj(std::unique_ptr<PtJson>& moduleObj, DistroFilter& distroFilter)
653 {
654 if (!moduleObj) {
655 LOGE("Module node is null!");
656 return false;
657 }
658 if (moduleObj->Contains(DISTRO_FILTER.c_str())) {
659 std::string distroFilterStr;
660 if (moduleObj->GetString(DISTRO_FILTER.c_str(), &distroFilterStr) != Result::SUCCESS) {
661 LOGE("Module node get %s failed!", DISTRO_FILTER.c_str());
662 return false;
663 }
664 std::unique_ptr<PtJson> distroFilterJsonObj = PtJson::Parse(distroFilterStr);
665 if (!distroFilterJsonObj) {
666 LOGE("Parse distro filter string failed!");
667 return false;
668 }
669 std::unique_ptr<PtJson> distroFilterObj;
670 if (distroFilterJsonObj->Contains(DISTRO_FILTER.c_str())) {
671 if (distroFilterJsonObj->GetObject(DISTRO_FILTER.c_str(), &distroFilterObj) != Result::SUCCESS) {
672 LOGE("DistroFilter node get %s failed!", DISTRO_FILTER.c_str());
673 return false;
674 }
675 }
676 if (!distroFilter.ParseFromJson(distroFilterObj)) {
677 LOGE("Parse distro filter failed!");
678 return false;
679 }
680 }
681 return true;
682 }
683
GetFaHapVerifyInfo(HapVerifyInfo & hapVerifyInfo)684 bool ModuleJson::GetFaHapVerifyInfo(HapVerifyInfo& hapVerifyInfo)
685 {
686 std::unique_ptr<PtJson> appObj;
687 std::unique_ptr<PtJson> moduleObj;
688 if (!GetAppObject(appObj)) {
689 LOGE("GetAppObject failed!");
690 return false;
691 }
692 if (!GetModuleObject(moduleObj)) {
693 LOGE("GetModuleObject failed!");
694 return false;
695 }
696 std::string bundleName;
697 std::string bundleType;
698 std::list<DependencyItem> dependencyItems;
699 bool debug = false;
700 if (!GetBundleNameByAppObj(appObj, bundleName)) {
701 LOGE("GetBundleNameByAppObj failed!");
702 return false;
703 }
704 if (!GetFaBundleType(bundleType)) {
705 LOGE("GetFaBundleType failed!");
706 return false;
707 }
708 if (!GetDependencyItemsByModuleObj(moduleObj, dependencyItems, bundleName)) {
709 LOGE("GetDependencyItemsByModuleObj failed!");
710 return false;
711 }
712 GetFaDebug(debug);
713 hapVerifyInfo.SetBundleName(bundleName);
714 hapVerifyInfo.SetBundleType(bundleType);
715 hapVerifyInfo.SetDependencyItemList(dependencyItems);
716 hapVerifyInfo.SetDebug(debug);
717 if (!SetFaHapVerifyInfoByAppObj(appObj, hapVerifyInfo)) {
718 LOGE("SetFaHapVerifyInfoByAppObj failed!");
719 return false;
720 }
721 if (!SetFaHapVerifyInfoByModuleObj(moduleObj, hapVerifyInfo)) {
722 LOGE("SetFaHapVerifyInfoByModuleObj failed!");
723 return false;
724 }
725 return true;
726 }
727
SetFaHapVerifyInfoByAppObj(std::unique_ptr<PtJson> & appObj,HapVerifyInfo & hapVerifyInfo)728 bool ModuleJson::SetFaHapVerifyInfoByAppObj(std::unique_ptr<PtJson>& appObj, HapVerifyInfo& hapVerifyInfo)
729 {
730 if (!appObj) {
731 LOGE("App node is null!");
732 return false;
733 }
734 std::string vendor;
735 Version version;
736 ModuleApiVersion moduleApiVersion;
737 std::string compileSdkType;
738 std::string compileSdkVersion;
739 if (!GetVendorByAppObj(appObj, vendor)) {
740 LOGE("GetVendorByAppObj failed!");
741 return false;
742 }
743 if (!GetFaVersionByAppObj(appObj, version)) {
744 LOGE("GetFaVersionByAppObj failed!");
745 return false;
746 }
747 if (!GetFaModuleApiVersionByAppObj(appObj, moduleApiVersion)) {
748 LOGE("GetFaModuleApiVersionByAppObj failed!");
749 return false;
750 }
751 if (!GetFaCompileSdkTypeByAppObj(appObj, compileSdkType)) {
752 LOGE("GetFaCompileSdkTypeByAppObj failed!");
753 return false;
754 }
755 if (!GetFaCompileSdkVersionByAppObj(appObj, compileSdkVersion)) {
756 LOGE("GetFaCompileSdkVersionByAppObj failed!");
757 return false;
758 }
759 hapVerifyInfo.SetVendor(vendor);
760 hapVerifyInfo.SetVersion(version);
761 hapVerifyInfo.SetApiVersion(moduleApiVersion);
762 hapVerifyInfo.SetCompileSdkType(compileSdkType);
763 hapVerifyInfo.SetCompileSdkVersion(compileSdkVersion);
764 return true;
765 }
766
SetFaHapVerifyInfoByModuleObj(std::unique_ptr<PtJson> & moduleObj,HapVerifyInfo & hapVerifyInfo)767 bool ModuleJson::SetFaHapVerifyInfoByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo)
768 {
769 if (!moduleObj) {
770 LOGE("Module node is null!");
771 return false;
772 }
773 std::string moduleName;
774 DistroFilter distroFilter;
775 std::list<std::string> deviceTypes;
776 std::list<std::string> abilityNames;
777 std::string moduleType;
778 std::string packageStr;
779 bool installationFree = false;
780 if (!GetFaModuleNameByModuleObj(moduleObj, moduleName)) {
781 LOGE("GetFaModuleNameByModuleObj failed!");
782 return false;
783 }
784 if (!GetFaDistroFilterByModuleObj(moduleObj, distroFilter)) {
785 LOGE("GetFaDistroFilterByModuleObj failed!");
786 return false;
787 }
788 if (!GetFaDeviceTypesByModuleObj(moduleObj, deviceTypes)) {
789 LOGE("GetFaDeviceTypesByModuleObj failed!");
790 return false;
791 }
792 if (!GetAbilityNamesByModuleObj(moduleObj, abilityNames)) {
793 LOGE("GetAbilityNamesByModuleObj failed!");
794 return false;
795 }
796 if (!GetFaModuleTypeByModuleObj(moduleObj, moduleType)) {
797 LOGE("GetFaModuleTypeByModuleObj failed!");
798 return false;
799 }
800 if (!GetFaPackageStrByModuleObj(moduleObj, packageStr)) {
801 LOGE("GetFaPackageStrByModuleObj failed!");
802 return false;
803 }
804 if (!GetFaInstallationFreeByModuleObj(moduleObj, installationFree)) {
805 LOGE("GetFaInstallationFreeByModuleObj failed!");
806 return false;
807 }
808 hapVerifyInfo.SetModuleName(moduleName);
809 hapVerifyInfo.SetDistroFilter(distroFilter);
810 hapVerifyInfo.SetDeviceTypes(deviceTypes);
811 hapVerifyInfo.SetAbilityNames(abilityNames);
812 hapVerifyInfo.SetModuleType(moduleType);
813 hapVerifyInfo.SetPackageName(packageStr);
814 hapVerifyInfo.SetInstallationFree(installationFree);
815 return true;
816 }
817 } // namespace AppPackingTool
818 } // namespace OHOS
819