1 /*
2  * Copyright (c) 2023-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 "inner_bundle_info.h"
17 
18 #include <algorithm>
19 #include <deque>
20 #include <regex>
21 #include <unistd.h>
22 
23 #include "common_profile.h"
24 #include "hilog_tag_wrapper.h"
25 #include "string_ex.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const std::string APP_TYPE = "appType";
31 const std::string UID = "uid";
32 const std::string GID = "gid";
33 const std::string BUNDLE_STATUS = "bundleStatus";
34 const std::string BASE_APPLICATION_INFO = "baseApplicationInfo";
35 const std::string BASE_BUNDLE_INFO = "baseBundleInfo";
36 const std::string BASE_ABILITY_INFO = "baseAbilityInfos";
37 const std::string INNER_MODULE_INFO = "innerModuleInfos";
38 const std::string USER_ID = "userId_";
39 const std::string APP_FEATURE = "appFeature";
40 const std::string CAN_UNINSTALL = "canUninstall";
41 const std::string NAME = "name";
42 const std::string MODULE_PACKAGE = "modulePackage";
43 const std::string MODULE_PATH = "modulePath";
44 const std::string MODULE_NAME = "moduleName";
45 const std::string MODULE_DESCRIPTION = "description";
46 const std::string MODULE_DESCRIPTION_ID = "descriptionId";
47 const std::string MODULE_ICON = "icon";
48 const std::string MODULE_ICON_ID = "iconId";
49 const std::string MODULE_LABEL = "label";
50 const std::string MODULE_LABEL_ID = "labelId";
51 const std::string MODULE_DESCRIPTION_INSTALLATION_FREE = "installationFree";
52 const std::string MODULE_IS_REMOVABLE = "isRemovable";
53 const std::string MODULE_UPGRADE_FLAG = "upgradeFlag";
54 const std::string MODULE_IS_ENTRY = "isEntry";
55 const std::string MODULE_METADATA = "metaData";
56 const std::string MODULE_COLOR_MODE = "colorMode";
57 const std::string MODULE_DISTRO = "distro";
58 const std::string MODULE_REQ_CAPABILITIES = "reqCapabilities";
59 const std::string MODULE_DATA_DIR = "moduleDataDir";
60 const std::string MODULE_RES_PATH = "moduleResPath";
61 const std::string MODULE_HAP_PATH = "hapPath";
62 const std::string MODULE_ABILITY_KEYS = "abilityKeys";
63 const std::string MODULE_MAIN_ABILITY = "mainAbility";
64 const std::string MODULE_ENTRY_ABILITY_KEY = "entryAbilityKey";
65 const std::string MODULE_DEPENDENCIES = "dependencies";
66 const std::string MODULE_IS_LIB_ISOLATED = "isLibIsolated";
67 const std::string MODULE_NATIVE_LIBRARY_PATH = "nativeLibraryPath";
68 const std::string MODULE_CPU_ABI = "cpuAbi";
69 const std::string NEW_BUNDLE_NAME = "newBundleName";
70 const std::string MODULE_SRC_PATH = "srcPath";
71 const std::string MODULE_HASH_VALUE = "hashValue";
72 const std::string SCHEME_SEPARATOR = "://";
73 const std::string PORT_SEPARATOR = ":";
74 const std::string PATH_SEPARATOR = "/";
75 const std::string PARAM_SEPARATOR = "?";
76 const std::string INSTALL_MARK = "installMark";
77 const std::string TYPE_WILDCARD = "*/*";
78 const std::string MODULE_PROCESS = "process";
79 const std::string MODULE_SRC_ENTRANCE = "srcEntrance";
80 const std::string MODULE_DEVICE_TYPES = "deviceTypes";
81 const std::string MODULE_VIRTUAL_MACHINE = "virtualMachine";
82 const std::string MODULE_UI_SYNTAX = "uiSyntax";
83 const std::string MODULE_PAGES = "pages";
84 const std::string MODULE_META_DATA = "metadata";
85 const std::string MODULE_EXTENSION_KEYS = "extensionKeys";
86 const std::string MODULE_IS_MODULE_JSON = "isModuleJson";
87 const std::string MODULE_IS_STAGE_BASED_MODEL = "isStageBasedModel";
88 const std::string BUNDLE_IS_NEW_VERSION = "isNewVersion";
89 const std::string BUNDLE_IS_NEED_UPDATE = "upgradeFlag";
90 const std::string BUNDLE_BASE_EXTENSION_INFOS = "baseExtensionInfos";
91 const std::string ALLOWED_ACLS = "allowedAcls";
92 const std::string META_DATA_SHORTCUTS_NAME = "ohos.ability.shortcuts";
93 const std::string APP_INDEX = "appIndex";
94 const std::string BUNDLE_IS_SANDBOX_APP = "isSandboxApp";
95 const std::string BUNDLE_SANDBOX_PERSISTENT_INFO = "sandboxPersistentInfo";
96 const std::string MODULE_COMPILE_MODE = "compileMode";
97 const std::string BUNDLE_HQF_INFOS = "hqfInfos";
98 const std::string MODULE_TARGET_MODULE_NAME = "targetModuleName";
99 const std::string MODULE_TARGET_PRIORITY = "targetPriority";
100 const std::string MODULE_ATOMIC_SERVICE_MODULE_TYPE = "atomicServiceModuleType";
101 const std::string MODULE_PRELOADS = "preloads";
102 const std::string MODULE_BUNDLE_TYPE = "bundleType";
103 const std::string MODULE_VERSION_CODE = "versionCode";
104 const std::string MODULE_VERSION_NAME = "versionName";
105 const std::string MODULE_PROXY_DATAS = "proxyDatas";
106 const std::string MODULE_BUILD_HASH = "buildHash";
107 const std::string MODULE_ISOLATION_MODE = "isolationMode";
108 const std::string MODULE_COMPRESS_NATIVE_LIBS = "compressNativeLibs";
109 const std::string MODULE_NATIVE_LIBRARY_FILE_NAMES = "nativeLibraryFileNames";
110 const std::string MODULE_AOT_COMPILE_STATUS = "aotCompileStatus";
111 const std::string DATA_GROUP_INFOS = "dataGroupInfos";
112 const std::map<std::string, IsolationMode> ISOLATION_MODE_MAP = {
113     {"isolationOnly", IsolationMode::ISOLATION_ONLY},
114     {"nonisolationOnly", IsolationMode::NONISOLATION_ONLY},
115     {"isolationFirst", IsolationMode::ISOLATION_FIRST},
116 };
117 const std::string NATIVE_LIBRARY_PATH_SYMBOL = "!/";
118 
119 const std::string STR_PHONE = "phone";
120 const std::string STR_DEFAULT = "default";
121 
ConvertCompileMode(const std::string & compileMode)122 inline CompileMode ConvertCompileMode(const std::string &compileMode)
123 {
124     if (compileMode == Profile::COMPILE_MODE_ES_MODULE) {
125         return CompileMode::ES_MODULE;
126     } else {
127         return CompileMode::JS_BUNDLE;
128     }
129 }
130 } // namespace
131 
InnerBundleInfo()132 InnerBundleInfo::InnerBundleInfo()
133 {
134     baseApplicationInfo_ = std::make_shared<ApplicationInfo>();
135     if (baseApplicationInfo_ == nullptr) {
136         TAG_LOGE(AAFwkTag::ABILITY_SIM, "null baseApplicationInfo_");
137     }
138     TAG_LOGD(AAFwkTag::ABILITY_SIM, "instance created");
139 }
140 
operator =(const InnerBundleInfo & info)141 InnerBundleInfo &InnerBundleInfo::operator=(const InnerBundleInfo &info)
142 {
143     if (this == &info) {
144         return *this;
145     }
146     this->appType_ = info.appType_;
147     this->userId_ = info.userId_;
148     this->bundleStatus_ = info.bundleStatus_;
149     this->appFeature_ = info.appFeature_;
150     this->allowedAcls_ = info.allowedAcls_;
151     this->appIndex_ = info.appIndex_;
152     this->isSandboxApp_ = info.isSandboxApp_;
153     this->currentPackage_ = info.currentPackage_;
154     this->onlyCreateBundleUser_ = info.onlyCreateBundleUser_;
155     this->innerModuleInfos_ = info.innerModuleInfos_;
156     this->baseAbilityInfos_ = info.baseAbilityInfos_;
157     this->isNewVersion_ = info.isNewVersion_;
158     this->baseExtensionInfos_= info.baseExtensionInfos_;
159     this->baseApplicationInfo_ = std::make_shared<ApplicationInfo>();
160     if (info.baseApplicationInfo_ != nullptr) {
161         *(this->baseApplicationInfo_) = *(info.baseApplicationInfo_);
162     }
163     this->provisionMetadatas_ = info.provisionMetadatas_;
164     return *this;
165 }
166 
~InnerBundleInfo()167 InnerBundleInfo::~InnerBundleInfo()
168 {
169     TAG_LOGD(AAFwkTag::ABILITY_SIM, "instance destroyed");
170 }
171 
to_json(nlohmann::json & jsonObject,const Distro & distro)172 void to_json(nlohmann::json &jsonObject, const Distro &distro)
173 {
174     jsonObject = nlohmann::json {
175             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_DELIVERY_WITH_INSTALL, distro.deliveryWithInstall},
176             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_NAME, distro.moduleName},
177             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_TYPE, distro.moduleType},
178             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_INSTALLATION_FREE, distro.installationFree}
179     };
180 }
181 
to_json(nlohmann::json & jsonObject,const Dependency & dependency)182 void to_json(nlohmann::json &jsonObject, const Dependency &dependency)
183 {
184     jsonObject = nlohmann::json {
185         {Profile::DEPENDENCIES_MODULE_NAME, dependency.moduleName},
186         {Profile::DEPENDENCIES_BUNDLE_NAME, dependency.bundleName},
187         {Profile::APP_VERSION_CODE, dependency.versionCode}
188     };
189 }
190 
to_json(nlohmann::json & jsonObject,const InnerModuleInfo & info)191 void to_json(nlohmann::json &jsonObject, const InnerModuleInfo &info)
192 {
193     jsonObject = nlohmann::json {
194         {NAME, info.name},
195         {MODULE_PACKAGE, info.modulePackage},
196         {MODULE_NAME, info.moduleName},
197         {MODULE_PATH, info.modulePath},
198         {MODULE_DATA_DIR, info.moduleDataDir},
199         {MODULE_RES_PATH, info.moduleResPath},
200         {MODULE_IS_ENTRY, info.isEntry},
201         {MODULE_METADATA, info.metaData},
202         {MODULE_COLOR_MODE, info.colorMode},
203         {MODULE_DISTRO, info.distro},
204         {MODULE_DESCRIPTION, info.description},
205         {MODULE_DESCRIPTION_ID, info.descriptionId},
206         {MODULE_ICON, info.icon},
207         {MODULE_ICON_ID, info.iconId},
208         {MODULE_LABEL, info.label},
209         {MODULE_LABEL_ID, info.labelId},
210         {MODULE_DESCRIPTION_INSTALLATION_FREE, info.installationFree},
211         {MODULE_IS_REMOVABLE, info.isRemovable},
212         {MODULE_UPGRADE_FLAG, info.upgradeFlag},
213         {MODULE_REQ_CAPABILITIES, info.reqCapabilities},
214         {MODULE_ABILITY_KEYS, info.abilityKeys},
215         {MODULE_MAIN_ABILITY, info.mainAbility},
216         {MODULE_ENTRY_ABILITY_KEY, info.entryAbilityKey},
217         {MODULE_SRC_PATH, info.srcPath},
218         {MODULE_HASH_VALUE, info.hashValue},
219         {MODULE_PROCESS, info.process},
220         {MODULE_SRC_ENTRANCE, info.srcEntrance},
221         {MODULE_DEVICE_TYPES, info.deviceTypes},
222         {MODULE_VIRTUAL_MACHINE, info.virtualMachine},
223         {MODULE_UI_SYNTAX, info.uiSyntax},
224         {MODULE_PAGES, info.pages},
225         {MODULE_META_DATA, info.metadata},
226         {MODULE_EXTENSION_KEYS, info.extensionKeys},
227         {MODULE_IS_MODULE_JSON, info.isModuleJson},
228         {MODULE_IS_STAGE_BASED_MODEL, info.isStageBasedModel},
229         {MODULE_DEPENDENCIES, info.dependencies},
230         {MODULE_IS_LIB_ISOLATED, info.isLibIsolated},
231         {MODULE_NATIVE_LIBRARY_PATH, info.nativeLibraryPath},
232         {MODULE_CPU_ABI, info.cpuAbi},
233         {MODULE_HAP_PATH, info.hapPath},
234         {MODULE_COMPILE_MODE, info.compileMode},
235         {MODULE_TARGET_MODULE_NAME, info.targetModuleName},
236         {MODULE_TARGET_PRIORITY, info.targetPriority},
237         {MODULE_ATOMIC_SERVICE_MODULE_TYPE, info.atomicServiceModuleType},
238         {MODULE_PRELOADS, info.preloads},
239         {MODULE_BUNDLE_TYPE, info.bundleType},
240         {MODULE_VERSION_CODE, info.versionCode},
241         {MODULE_VERSION_NAME, info.versionName},
242         {MODULE_PROXY_DATAS, info.proxyDatas},
243         {MODULE_BUILD_HASH, info.buildHash},
244         {MODULE_ISOLATION_MODE, info.isolationMode},
245         {MODULE_COMPRESS_NATIVE_LIBS, info.compressNativeLibs},
246         {MODULE_NATIVE_LIBRARY_FILE_NAMES, info.nativeLibraryFileNames},
247         {MODULE_AOT_COMPILE_STATUS, info.aotCompileStatus},
248     };
249 }
250 
ToJson(nlohmann::json & jsonObject) const251 void InnerBundleInfo::ToJson(nlohmann::json &jsonObject) const
252 {
253     jsonObject[APP_TYPE] = appType_;
254     jsonObject[BUNDLE_STATUS] = bundleStatus_;
255     jsonObject[ALLOWED_ACLS] = allowedAcls_;
256     jsonObject[BASE_APPLICATION_INFO] = *baseApplicationInfo_;
257     jsonObject[BASE_ABILITY_INFO] = baseAbilityInfos_;
258     jsonObject[INNER_MODULE_INFO] = innerModuleInfos_;
259     jsonObject[USER_ID] = userId_;
260     jsonObject[APP_FEATURE] = appFeature_;
261     jsonObject[BUNDLE_IS_NEW_VERSION] = isNewVersion_;
262     jsonObject[BUNDLE_BASE_EXTENSION_INFOS] = baseExtensionInfos_;
263     jsonObject[APP_INDEX] = appIndex_;
264     jsonObject[BUNDLE_IS_SANDBOX_APP] = isSandboxApp_;
265 }
266 
from_json(const nlohmann::json & jsonObject,InnerModuleInfo & info)267 void from_json(const nlohmann::json &jsonObject, InnerModuleInfo &info)
268 {
269     // these are not required fields.
270     const auto &jsonObjectEnd = jsonObject.end();
271     int32_t parseResult = ERR_OK;
272     GetValueIfFindKey<std::string>(jsonObject,
273         jsonObjectEnd,
274         NAME,
275         info.name,
276         JsonType::STRING,
277         false,
278         parseResult,
279         ArrayType::NOT_ARRAY);
280     GetValueIfFindKey<std::string>(jsonObject,
281         jsonObjectEnd,
282         MODULE_PACKAGE,
283         info.modulePackage,
284         JsonType::STRING,
285         false,
286         parseResult,
287         ArrayType::NOT_ARRAY);
288     GetValueIfFindKey<std::string>(jsonObject,
289         jsonObjectEnd,
290         MODULE_NAME,
291         info.moduleName,
292         JsonType::STRING,
293         false,
294         parseResult,
295         ArrayType::NOT_ARRAY);
296     GetValueIfFindKey<std::string>(jsonObject,
297         jsonObjectEnd,
298         MODULE_PATH,
299         info.modulePath,
300         JsonType::STRING,
301         false,
302         parseResult,
303         ArrayType::NOT_ARRAY);
304     GetValueIfFindKey<std::string>(jsonObject,
305         jsonObjectEnd,
306         MODULE_DATA_DIR,
307         info.moduleDataDir,
308         JsonType::STRING,
309         false,
310         parseResult,
311         ArrayType::NOT_ARRAY);
312     GetValueIfFindKey<std::string>(jsonObject,
313         jsonObjectEnd,
314         MODULE_HAP_PATH,
315         info.hapPath,
316         JsonType::STRING,
317         false,
318         parseResult,
319         ArrayType::NOT_ARRAY);
320     GetValueIfFindKey<std::string>(jsonObject,
321         jsonObjectEnd,
322         MODULE_RES_PATH,
323         info.moduleResPath,
324         JsonType::STRING,
325         false,
326         parseResult,
327         ArrayType::NOT_ARRAY);
328     GetValueIfFindKey<bool>(jsonObject,
329         jsonObjectEnd,
330         MODULE_IS_ENTRY,
331         info.isEntry,
332         JsonType::BOOLEAN,
333         false,
334         parseResult,
335         ArrayType::NOT_ARRAY);
336     GetValueIfFindKey<MetaData>(jsonObject,
337         jsonObjectEnd,
338         MODULE_METADATA,
339         info.metaData,
340         JsonType::OBJECT,
341         false,
342         parseResult,
343         ArrayType::NOT_ARRAY);
344     GetValueIfFindKey<ModuleColorMode>(jsonObject,
345         jsonObjectEnd,
346         MODULE_COLOR_MODE,
347         info.colorMode,
348         JsonType::NUMBER,
349         false,
350         parseResult,
351         ArrayType::NOT_ARRAY);
352     GetValueIfFindKey<Distro>(jsonObject,
353         jsonObjectEnd,
354         MODULE_DISTRO,
355         info.distro,
356         JsonType::OBJECT,
357         false,
358         parseResult,
359         ArrayType::NOT_ARRAY);
360     GetValueIfFindKey<std::string>(jsonObject,
361         jsonObjectEnd,
362         MODULE_DESCRIPTION,
363         info.description,
364         JsonType::STRING,
365         false,
366         parseResult,
367         ArrayType::NOT_ARRAY);
368     GetValueIfFindKey<int32_t>(jsonObject,
369         jsonObjectEnd,
370         MODULE_DESCRIPTION_ID,
371         info.descriptionId,
372         JsonType::NUMBER,
373         false,
374         parseResult,
375         ArrayType::NOT_ARRAY);
376     GetValueIfFindKey<std::string>(jsonObject,
377         jsonObjectEnd,
378         MODULE_ICON,
379         info.icon,
380         JsonType::STRING,
381         false,
382         parseResult,
383         ArrayType::NOT_ARRAY);
384     GetValueIfFindKey<int32_t>(jsonObject,
385         jsonObjectEnd,
386         MODULE_ICON_ID,
387         info.iconId,
388         JsonType::NUMBER,
389         false,
390         parseResult,
391         ArrayType::NOT_ARRAY);
392     GetValueIfFindKey<std::string>(jsonObject,
393         jsonObjectEnd,
394         MODULE_LABEL,
395         info.label,
396         JsonType::STRING,
397         false,
398         parseResult,
399         ArrayType::NOT_ARRAY);
400     GetValueIfFindKey<int32_t>(jsonObject,
401         jsonObjectEnd,
402         MODULE_LABEL_ID,
403         info.labelId,
404         JsonType::NUMBER,
405         false,
406         parseResult,
407         ArrayType::NOT_ARRAY);
408     GetValueIfFindKey<std::string>(jsonObject,
409         jsonObjectEnd,
410         MODULE_MAIN_ABILITY,
411         info.mainAbility,
412         JsonType::STRING,
413         false,
414         parseResult,
415         ArrayType::NOT_ARRAY);
416     GetValueIfFindKey<std::string>(jsonObject,
417         jsonObjectEnd,
418         MODULE_ENTRY_ABILITY_KEY,
419         info.entryAbilityKey,
420         JsonType::STRING,
421         false,
422         parseResult,
423         ArrayType::NOT_ARRAY);
424     GetValueIfFindKey<std::string>(jsonObject,
425         jsonObjectEnd,
426         MODULE_SRC_PATH,
427         info.srcPath,
428         JsonType::STRING,
429         false,
430         parseResult,
431         ArrayType::NOT_ARRAY);
432     GetValueIfFindKey<std::string>(jsonObject,
433         jsonObjectEnd,
434         MODULE_HASH_VALUE,
435         info.hashValue,
436         JsonType::STRING,
437         false,
438         parseResult,
439         ArrayType::NOT_ARRAY);
440     GetValueIfFindKey<bool>(jsonObject,
441         jsonObjectEnd,
442         MODULE_DESCRIPTION_INSTALLATION_FREE,
443         info.installationFree,
444         JsonType::BOOLEAN,
445         false,
446         parseResult,
447         ArrayType::NOT_ARRAY);
448     GetValueIfFindKey<std::map<std::string, bool>>(jsonObject,
449         jsonObjectEnd,
450         MODULE_IS_REMOVABLE,
451         info.isRemovable,
452         JsonType::OBJECT,
453         false,
454         parseResult,
455         ArrayType::NOT_ARRAY);
456     GetValueIfFindKey<int32_t>(jsonObject,
457         jsonObjectEnd,
458         MODULE_UPGRADE_FLAG,
459         info.upgradeFlag,
460         JsonType::NUMBER,
461         false,
462         parseResult,
463         ArrayType::NOT_ARRAY);
464     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
465         jsonObjectEnd,
466         MODULE_REQ_CAPABILITIES,
467         info.reqCapabilities,
468         JsonType::ARRAY,
469         false,
470         parseResult,
471         ArrayType::STRING);
472     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
473         jsonObjectEnd,
474         MODULE_ABILITY_KEYS,
475         info.abilityKeys,
476         JsonType::ARRAY,
477         false,
478         parseResult,
479         ArrayType::STRING);
480     GetValueIfFindKey<std::string>(jsonObject,
481         jsonObjectEnd,
482         MODULE_PROCESS,
483         info.process,
484         JsonType::STRING,
485         false,
486         parseResult,
487         ArrayType::NOT_ARRAY);
488     GetValueIfFindKey<std::string>(jsonObject,
489         jsonObjectEnd,
490         MODULE_SRC_ENTRANCE,
491         info.srcEntrance,
492         JsonType::STRING,
493         false,
494         parseResult,
495         ArrayType::NOT_ARRAY);
496     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
497         jsonObjectEnd,
498         MODULE_DEVICE_TYPES,
499         info.deviceTypes,
500         JsonType::ARRAY,
501         false,
502         parseResult,
503         ArrayType::STRING);
504     GetValueIfFindKey<std::string>(jsonObject,
505         jsonObjectEnd,
506         MODULE_VIRTUAL_MACHINE,
507         info.virtualMachine,
508         JsonType::STRING,
509         false,
510         parseResult,
511         ArrayType::NOT_ARRAY);
512     GetValueIfFindKey<std::string>(jsonObject,
513         jsonObjectEnd,
514         MODULE_UI_SYNTAX,
515         info.uiSyntax,
516         JsonType::STRING,
517         false,
518         parseResult,
519         ArrayType::NOT_ARRAY);
520     GetValueIfFindKey<std::string>(jsonObject,
521         jsonObjectEnd,
522         MODULE_PAGES,
523         info.pages,
524         JsonType::STRING,
525         false,
526         parseResult,
527         ArrayType::NOT_ARRAY);
528     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
529         jsonObjectEnd,
530         MODULE_META_DATA,
531         info.metadata,
532         JsonType::ARRAY,
533         false,
534         parseResult,
535         ArrayType::OBJECT);
536     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
537         jsonObjectEnd,
538         MODULE_EXTENSION_KEYS,
539         info.extensionKeys,
540         JsonType::ARRAY,
541         false,
542         parseResult,
543         ArrayType::STRING);
544     GetValueIfFindKey<bool>(jsonObject,
545         jsonObjectEnd,
546         MODULE_IS_MODULE_JSON,
547         info.isModuleJson,
548         JsonType::BOOLEAN,
549         false,
550         parseResult,
551         ArrayType::NOT_ARRAY);
552     GetValueIfFindKey<bool>(jsonObject,
553         jsonObjectEnd,
554         MODULE_IS_STAGE_BASED_MODEL,
555         info.isStageBasedModel,
556         JsonType::BOOLEAN,
557         false,
558         parseResult,
559         ArrayType::NOT_ARRAY);
560     GetValueIfFindKey<std::vector<Dependency>>(jsonObject,
561         jsonObjectEnd,
562         MODULE_DEPENDENCIES,
563         info.dependencies,
564         JsonType::ARRAY,
565         false,
566         parseResult,
567         ArrayType::OBJECT);
568     GetValueIfFindKey<std::string>(jsonObject,
569         jsonObjectEnd,
570         MODULE_COMPILE_MODE,
571         info.compileMode,
572         JsonType::STRING,
573         false,
574         parseResult,
575         ArrayType::NOT_ARRAY);
576     GetValueIfFindKey<bool>(jsonObject,
577         jsonObjectEnd,
578         MODULE_IS_LIB_ISOLATED,
579         info.isLibIsolated,
580         JsonType::BOOLEAN,
581         false,
582         parseResult,
583         ArrayType::NOT_ARRAY);
584     GetValueIfFindKey<std::string>(jsonObject,
585         jsonObjectEnd,
586         MODULE_NATIVE_LIBRARY_PATH,
587         info.nativeLibraryPath,
588         JsonType::STRING,
589         false,
590         parseResult,
591         ArrayType::NOT_ARRAY);
592     GetValueIfFindKey<std::string>(jsonObject,
593         jsonObjectEnd,
594         MODULE_CPU_ABI,
595         info.cpuAbi,
596         JsonType::STRING,
597         false,
598         parseResult,
599         ArrayType::NOT_ARRAY);
600     GetValueIfFindKey<std::string>(jsonObject,
601         jsonObjectEnd,
602         MODULE_TARGET_MODULE_NAME,
603         info.targetModuleName,
604         JsonType::STRING,
605         false,
606         parseResult,
607         ArrayType::NOT_ARRAY);
608     GetValueIfFindKey<int32_t>(jsonObject,
609         jsonObjectEnd,
610         MODULE_TARGET_PRIORITY,
611         info.targetPriority,
612         JsonType::NUMBER,
613         false,
614         parseResult,
615         ArrayType::NOT_ARRAY);
616     GetValueIfFindKey<AtomicServiceModuleType>(jsonObject,
617         jsonObjectEnd,
618         MODULE_ATOMIC_SERVICE_MODULE_TYPE,
619         info.atomicServiceModuleType,
620         JsonType::NUMBER,
621         false,
622         parseResult,
623         ArrayType::NOT_ARRAY);
624     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
625         jsonObjectEnd,
626         MODULE_PRELOADS,
627         info.preloads,
628         JsonType::ARRAY,
629         false,
630         parseResult,
631         ArrayType::STRING);
632     GetValueIfFindKey<BundleType>(jsonObject,
633         jsonObjectEnd,
634         MODULE_BUNDLE_TYPE,
635         info.bundleType,
636         JsonType::NUMBER,
637         false,
638         parseResult,
639         ArrayType::NOT_ARRAY);
640     GetValueIfFindKey<uint32_t>(jsonObject,
641         jsonObjectEnd,
642         MODULE_VERSION_CODE,
643         info.versionCode,
644         JsonType::NUMBER,
645         false,
646         parseResult,
647         ArrayType::NOT_ARRAY);
648     GetValueIfFindKey<std::string>(jsonObject,
649         jsonObjectEnd,
650         MODULE_VERSION_NAME,
651         info.versionName,
652         JsonType::STRING,
653         false,
654         parseResult,
655         ArrayType::NOT_ARRAY);
656     GetValueIfFindKey<std::vector<ProxyData>>(jsonObject,
657         jsonObjectEnd,
658         MODULE_PROXY_DATAS,
659         info.proxyDatas,
660         JsonType::ARRAY,
661         false,
662         parseResult,
663         ArrayType::OBJECT);
664     GetValueIfFindKey<std::string>(jsonObject,
665         jsonObjectEnd,
666         MODULE_BUILD_HASH,
667         info.buildHash,
668         JsonType::STRING,
669         false,
670         parseResult,
671         ArrayType::NOT_ARRAY);
672     GetValueIfFindKey<std::string>(jsonObject,
673         jsonObjectEnd,
674         MODULE_ISOLATION_MODE,
675         info.isolationMode,
676         JsonType::STRING,
677         false,
678         parseResult,
679         ArrayType::NOT_ARRAY);
680     GetValueIfFindKey<bool>(jsonObject,
681         jsonObjectEnd,
682         MODULE_COMPRESS_NATIVE_LIBS,
683         info.compressNativeLibs,
684         JsonType::BOOLEAN,
685         false,
686         parseResult,
687         ArrayType::NOT_ARRAY);
688     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
689         jsonObjectEnd,
690         MODULE_NATIVE_LIBRARY_FILE_NAMES,
691         info.nativeLibraryFileNames,
692         JsonType::ARRAY,
693         false,
694         parseResult,
695         ArrayType::STRING);
696     GetValueIfFindKey<AOTCompileStatus>(jsonObject,
697         jsonObjectEnd,
698         MODULE_AOT_COMPILE_STATUS,
699         info.aotCompileStatus,
700         JsonType::NUMBER,
701         false,
702         parseResult,
703         ArrayType::NOT_ARRAY);
704     if (parseResult != ERR_OK) {
705         TAG_LOGE(
706             AAFwkTag::ABILITY_SIM, "read InnerModuleInfo from database error:%{public}d", parseResult);
707     }
708 }
709 
from_json(const nlohmann::json & jsonObject,Dependency & dependency)710 void from_json(const nlohmann::json &jsonObject, Dependency &dependency)
711 {
712     const auto &jsonObjectEnd = jsonObject.end();
713     int32_t parseResult = ERR_OK;
714     GetValueIfFindKey<std::string>(jsonObject,
715         jsonObjectEnd,
716         Profile::DEPENDENCIES_MODULE_NAME,
717         dependency.moduleName,
718         JsonType::STRING,
719         false,
720         parseResult,
721         ArrayType::NOT_ARRAY);
722     GetValueIfFindKey<std::string>(jsonObject,
723         jsonObjectEnd,
724         Profile::DEPENDENCIES_BUNDLE_NAME,
725         dependency.bundleName,
726         JsonType::STRING,
727         false,
728         parseResult,
729         ArrayType::NOT_ARRAY);
730     GetValueIfFindKey<int>(jsonObject,
731         jsonObjectEnd,
732         Profile::APP_VERSION_CODE,
733         dependency.versionCode,
734         JsonType::NUMBER,
735         false,
736         parseResult,
737         ArrayType::NOT_ARRAY);
738     if (parseResult != ERR_OK) {
739         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Dependency error:%{public}d", parseResult);
740     }
741 }
742 
from_json(const nlohmann::json & jsonObject,Distro & distro)743 void from_json(const nlohmann::json &jsonObject, Distro &distro)
744 {
745     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
746     const auto &jsonObjectEnd = jsonObject.end();
747     int32_t parseResult = ERR_OK;
748     GetValueIfFindKey<bool>(jsonObject,
749         jsonObjectEnd,
750         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_DELIVERY_WITH_INSTALL,
751         distro.deliveryWithInstall,
752         JsonType::BOOLEAN,
753         true,
754         parseResult,
755         ArrayType::NOT_ARRAY);
756     GetValueIfFindKey<std::string>(jsonObject,
757         jsonObjectEnd,
758         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_NAME,
759         distro.moduleName,
760         JsonType::STRING,
761         true,
762         parseResult,
763         ArrayType::NOT_ARRAY);
764     GetValueIfFindKey<std::string>(jsonObject,
765         jsonObjectEnd,
766         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_TYPE,
767         distro.moduleType,
768         JsonType::STRING,
769         true,
770         parseResult,
771         ArrayType::NOT_ARRAY);
772     // mustFlag decide by distro.moduleType
773     GetValueIfFindKey<bool>(jsonObject,
774         jsonObjectEnd,
775         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_INSTALLATION_FREE,
776         distro.installationFree,
777         JsonType::BOOLEAN,
778         false,
779         parseResult,
780         ArrayType::NOT_ARRAY);
781     if (parseResult != ERR_OK) {
782         TAG_LOGE(AAFwkTag::ABILITY_SIM, "Distro error:%{public}d", parseResult);
783     }
784 }
785 
FromJson(const nlohmann::json & jsonObject)786 int32_t InnerBundleInfo::FromJson(const nlohmann::json &jsonObject)
787 {
788     const auto &jsonObjectEnd = jsonObject.end();
789     int32_t parseResult = ERR_OK;
790     GetValueIfFindKey<Constants::AppType>(jsonObject,
791         jsonObjectEnd,
792         APP_TYPE,
793         appType_,
794         JsonType::NUMBER,
795         true,
796         parseResult,
797         ArrayType::NOT_ARRAY);
798     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
799         jsonObjectEnd,
800         ALLOWED_ACLS,
801         allowedAcls_,
802         JsonType::ARRAY,
803         false,
804         parseResult,
805         ArrayType::STRING);
806     GetValueIfFindKey<BundleStatus>(jsonObject,
807         jsonObjectEnd,
808         BUNDLE_STATUS,
809         bundleStatus_,
810         JsonType::NUMBER,
811         true,
812         parseResult,
813         ArrayType::NOT_ARRAY);
814     GetValueIfFindKey<ApplicationInfo>(jsonObject,
815         jsonObjectEnd,
816         BASE_APPLICATION_INFO,
817         *baseApplicationInfo_,
818         JsonType::OBJECT,
819         true,
820         parseResult,
821         ArrayType::NOT_ARRAY);
822     GetValueIfFindKey<std::map<std::string, AbilityInfo>>(jsonObject,
823         jsonObjectEnd,
824         BASE_ABILITY_INFO,
825         baseAbilityInfos_,
826         JsonType::OBJECT,
827         true,
828         parseResult,
829         ArrayType::NOT_ARRAY);
830     GetValueIfFindKey<std::map<std::string, InnerModuleInfo>>(jsonObject,
831         jsonObjectEnd,
832         INNER_MODULE_INFO,
833         innerModuleInfos_,
834         JsonType::OBJECT,
835         true,
836         parseResult,
837         ArrayType::NOT_ARRAY);
838     GetValueIfFindKey<int>(jsonObject,
839         jsonObjectEnd,
840         USER_ID,
841         userId_,
842         JsonType::NUMBER,
843         true,
844         parseResult,
845         ArrayType::NOT_ARRAY);
846     GetValueIfFindKey<std::string>(jsonObject,
847         jsonObjectEnd,
848         APP_FEATURE,
849         appFeature_,
850         JsonType::STRING,
851         true,
852         parseResult,
853         ArrayType::NOT_ARRAY);
854     GetValueIfFindKey<bool>(jsonObject,
855         jsonObjectEnd,
856         BUNDLE_IS_NEW_VERSION,
857         isNewVersion_,
858         JsonType::BOOLEAN,
859         false,
860         parseResult,
861         ArrayType::NOT_ARRAY);
862     GetValueIfFindKey<std::map<std::string, ExtensionAbilityInfo>>(jsonObject,
863         jsonObjectEnd,
864         BUNDLE_BASE_EXTENSION_INFOS,
865         baseExtensionInfos_,
866         JsonType::OBJECT,
867         false,
868         parseResult,
869         ArrayType::NOT_ARRAY);
870     GetValueIfFindKey<int>(jsonObject,
871         jsonObjectEnd,
872         APP_INDEX,
873         appIndex_,
874         JsonType::NUMBER,
875         false,
876         parseResult,
877         ArrayType::NOT_ARRAY);
878     GetValueIfFindKey<bool>(jsonObject,
879         jsonObjectEnd,
880         BUNDLE_IS_SANDBOX_APP,
881         isSandboxApp_,
882         JsonType::BOOLEAN,
883         false,
884         parseResult,
885         ArrayType::NOT_ARRAY);
886     if (parseResult != ERR_OK) {
887         TAG_LOGE(
888             AAFwkTag::ABILITY_SIM, "read InnerBundleInfo from database error:%{public}d", parseResult);
889     }
890     return parseResult;
891 }
892 
FindHapModuleInfo(const std::string & modulePackage,int32_t userId) const893 std::optional<HapModuleInfo> InnerBundleInfo::FindHapModuleInfo(const std::string &modulePackage, int32_t userId) const
894 {
895     auto it = innerModuleInfos_.find(modulePackage);
896     if (it == innerModuleInfos_.end()) {
897         TAG_LOGE(AAFwkTag::ABILITY_SIM, "not find module %{public}s", modulePackage.c_str());
898         return std::nullopt;
899     }
900     HapModuleInfo hapInfo;
901     hapInfo.name = it->second.name;
902     hapInfo.package = it->second.modulePackage;
903     hapInfo.moduleName = it->second.moduleName;
904     hapInfo.description = it->second.description;
905     hapInfo.descriptionId = it->second.descriptionId;
906     hapInfo.label = it->second.label;
907     hapInfo.labelId = it->second.labelId;
908     hapInfo.iconPath = it->second.icon;
909     hapInfo.iconId = it->second.iconId;
910     hapInfo.mainAbility = it->second.mainAbility;
911     hapInfo.srcPath = it->second.srcPath;
912     hapInfo.hapPath = it->second.hapPath;
913     hapInfo.supportedModes = baseApplicationInfo_->supportedModes;
914     hapInfo.reqCapabilities = it->second.reqCapabilities;
915     hapInfo.colorMode = it->second.colorMode;
916     hapInfo.isRemovable = it->second.isRemovable;
917     hapInfo.upgradeFlag = it->second.upgradeFlag;
918     hapInfo.isLibIsolated = it->second.isLibIsolated;
919     hapInfo.nativeLibraryPath = it->second.nativeLibraryPath;
920     hapInfo.cpuAbi = it->second.cpuAbi;
921 
922     hapInfo.bundleName = baseApplicationInfo_->bundleName;
923     hapInfo.mainElementName = it->second.mainAbility;
924     hapInfo.pages = it->second.pages;
925     hapInfo.process = it->second.process;
926     hapInfo.resourcePath = it->second.moduleResPath;
927     hapInfo.srcEntrance = it->second.srcEntrance;
928     hapInfo.uiSyntax = it->second.uiSyntax;
929     hapInfo.virtualMachine = it->second.virtualMachine;
930     hapInfo.deliveryWithInstall = it->second.distro.deliveryWithInstall;
931     hapInfo.installationFree = it->second.distro.installationFree;
932     hapInfo.isModuleJson = it->second.isModuleJson;
933     hapInfo.isStageBasedModel = it->second.isStageBasedModel;
934     std::string moduleType = it->second.distro.moduleType;
935     if (moduleType == Profile::MODULE_TYPE_ENTRY) {
936         hapInfo.moduleType = ModuleType::ENTRY;
937     } else if (moduleType == Profile::MODULE_TYPE_FEATURE) {
938         hapInfo.moduleType = ModuleType::FEATURE;
939     } else if (moduleType == Profile::MODULE_TYPE_SHARED) {
940         hapInfo.moduleType = ModuleType::SHARED;
941     } else {
942         hapInfo.moduleType = ModuleType::UNKNOWN;
943     }
944     std::string key;
945     key.append(".").append(modulePackage).append(".");
946     for (const auto &extension : baseExtensionInfos_) {
947         if (extension.first.find(key) != std::string::npos) {
948             hapInfo.extensionInfos.emplace_back(extension.second);
949         }
950     }
951     hapInfo.metadata = it->second.metadata;
952     bool first = false;
953     for (auto &ability : baseAbilityInfos_) {
954         if (ability.second.name == Constants::APP_DETAIL_ABILITY) {
955             continue;
956         }
957         if (ability.first.find(key) != std::string::npos) {
958             if (!first) {
959                 hapInfo.deviceTypes = ability.second.deviceTypes;
960                 first = true;
961             }
962             auto &abilityInfo = hapInfo.abilityInfos.emplace_back(ability.second);
963             GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION |
964                 ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
965                 abilityInfo.applicationInfo);
966         }
967     }
968     hapInfo.dependencies = it->second.dependencies;
969     hapInfo.compileMode = ConvertCompileMode(it->second.compileMode);
970     hapInfo.atomicServiceModuleType = it->second.atomicServiceModuleType;
971     for (const auto &item : it->second.preloads) {
972         PreloadItem preload(item);
973         hapInfo.preloads.emplace_back(preload);
974     }
975     for (const auto &item : it->second.proxyDatas) {
976         ProxyData proxyData(item);
977         hapInfo.proxyDatas.emplace_back(proxyData);
978     }
979     hapInfo.buildHash = it->second.buildHash;
980     hapInfo.isolationMode = GetIsolationMode(it->second.isolationMode);
981     hapInfo.compressNativeLibs = it->second.compressNativeLibs;
982     hapInfo.nativeLibraryFileNames = it->second.nativeLibraryFileNames;
983     hapInfo.aotCompileStatus = it->second.aotCompileStatus;
984     return hapInfo;
985 }
986 
FindAbilityInfo(const std::string & moduleName,const std::string & abilityName,int32_t userId) const987 std::optional<AbilityInfo> InnerBundleInfo::FindAbilityInfo(
988     const std::string &moduleName,
989     const std::string &abilityName,
990     int32_t userId) const
991 {
992     for (const auto &ability : baseAbilityInfos_) {
993         auto abilityInfo = ability.second;
994         if ((abilityInfo.name == abilityName) &&
995             (moduleName.empty() || (abilityInfo.moduleName == moduleName))) {
996             GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION |
997                 ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
998                 abilityInfo.applicationInfo);
999             return abilityInfo;
1000         }
1001     }
1002 
1003     return std::nullopt;
1004 }
1005 
FindAbilityInfo(const std::string & moduleName,const std::string & abilityName,AbilityInfo & info) const1006 ErrCode InnerBundleInfo::FindAbilityInfo(
1007     const std::string &moduleName, const std::string &abilityName, AbilityInfo &info) const
1008 {
1009     bool isModuleFind = false;
1010     for (const auto &ability : baseAbilityInfos_) {
1011         auto abilityInfo = ability.second;
1012         if ((abilityInfo.moduleName == moduleName)) {
1013             isModuleFind = true;
1014             if (abilityInfo.name == abilityName) {
1015                 info = abilityInfo;
1016                 return ERR_OK;
1017             }
1018         }
1019     }
1020     TAG_LOGE(AAFwkTag::ABILITY_SIM,
1021         "bundleName:%{public}s not find moduleName:%{public}s, abilityName:%{public}s, isModuleFind:%{public}d",
1022         GetBundleName().c_str(), moduleName.c_str(), abilityName.c_str(), isModuleFind);
1023     if (isModuleFind) {
1024         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
1025     } else {
1026         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
1027     }
1028 }
1029 
ToString() const1030 std::string InnerBundleInfo::ToString() const
1031 {
1032     nlohmann::json j;
1033     ToJson(j);
1034     return j.dump();
1035 }
1036 
GetApplicationInfo(int32_t flags,int32_t userId,ApplicationInfo & appInfo) const1037 void InnerBundleInfo::GetApplicationInfo(int32_t flags, int32_t userId, ApplicationInfo &appInfo) const
1038 {
1039     appInfo = *baseApplicationInfo_;
1040 
1041     for (const auto &info : innerModuleInfos_) {
1042         bool deCompress = info.second.hapPath.empty();
1043         ModuleInfo moduleInfo;
1044         moduleInfo.moduleName = info.second.moduleName;
1045         if (deCompress) {
1046             moduleInfo.moduleSourceDir = info.second.modulePath;
1047             appInfo.moduleSourceDirs.emplace_back(info.second.modulePath);
1048         }
1049         moduleInfo.preloads = info.second.preloads;
1050         appInfo.moduleInfos.emplace_back(moduleInfo);
1051         if (deCompress && info.second.isEntry) {
1052             appInfo.entryDir = info.second.modulePath;
1053         }
1054         if ((static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_METADATA) == GET_APPLICATION_INFO_WITH_METADATA) {
1055             bool isModuleJson = info.second.isModuleJson;
1056             if (!isModuleJson && info.second.metaData.customizeData.size() > 0) {
1057                 appInfo.metaData[info.second.moduleName] = info.second.metaData.customizeData;
1058             }
1059             if (isModuleJson && info.second.metadata.size() > 0) {
1060                 appInfo.metadata[info.second.moduleName] = info.second.metadata;
1061             }
1062         }
1063         if ((static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT) !=
1064             GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT) {
1065             appInfo.fingerprint.clear();
1066         }
1067     }
1068     if (!appInfo.permissions.empty()) {
1069         RemoveDuplicateName(appInfo.permissions);
1070     }
1071 }
1072 
RemoveDuplicateName(std::vector<std::string> & name) const1073 void InnerBundleInfo::RemoveDuplicateName(std::vector<std::string> &name) const
1074 {
1075     std::sort(name.begin(), name.end());
1076     auto iter = std::unique(name.begin(), name.end());
1077     name.erase(iter, name.end());
1078 }
1079 
GetIsolationMode(const std::string & isolationMode) const1080 IsolationMode InnerBundleInfo::GetIsolationMode(const std::string &isolationMode) const
1081 {
1082     auto isolationModeRes = ISOLATION_MODE_MAP.find(isolationMode);
1083     if (isolationModeRes != ISOLATION_MODE_MAP.end()) {
1084         return isolationModeRes->second;
1085     } else {
1086         return IsolationMode::NONISOLATION_FIRST;
1087     }
1088 }
1089 }  // namespace AppExecFwk
1090 }  // namespace OHOS
1091