• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "string_ex.h"
23 
24 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
25 #include "app_control_constants.h"
26 #include "app_control_manager.h"
27 #endif
28 #include "bundle_mgr_client.h"
29 #include "bundle_permission_mgr.h"
30 #include "common_profile.h"
31 #include "distributed_module_info.h"
32 #include "distributed_ability_info.h"
33 #include "free_install_params.h"
34 #include "mime_type_mgr.h"
35 #include "parameters.h"
36 
37 namespace OHOS {
38 namespace AppExecFwk {
39 namespace {
40 const std::string APP_TYPE = "appType";
41 const std::string UID = "uid";
42 const std::string GID = "gid";
43 const std::string BUNDLE_STATUS = "bundleStatus";
44 const std::string BASE_APPLICATION_INFO = "baseApplicationInfo";
45 const std::string BASE_BUNDLE_INFO = "baseBundleInfo";
46 const std::string BASE_ABILITY_INFO = "baseAbilityInfos";
47 const std::string INNER_MODULE_INFO = "innerModuleInfos";
48 const std::string SKILL_INFOS = "skillInfos";
49 const std::string USER_ID = "userId_";
50 const std::string APP_FEATURE = "appFeature";
51 const std::string CAN_UNINSTALL = "canUninstall";
52 const std::string NAME = "name";
53 const std::string MODULE_PACKAGE = "modulePackage";
54 const std::string MODULE_PATH = "modulePath";
55 const std::string MODULE_NAME = "moduleName";
56 const std::string MODULE_DESCRIPTION = "description";
57 const std::string MODULE_DESCRIPTION_ID = "descriptionId";
58 const std::string MODULE_ICON = "icon";
59 const std::string MODULE_ICON_ID = "iconId";
60 const std::string MODULE_LABEL = "label";
61 const std::string MODULE_LABEL_ID = "labelId";
62 const std::string MODULE_DESCRIPTION_INSTALLATION_FREE = "installationFree";
63 const std::string MODULE_IS_REMOVABLE = "isRemovable";
64 const std::string MODULE_UPGRADE_FLAG = "upgradeFlag";
65 const std::string MODULE_IS_ENTRY = "isEntry";
66 const std::string MODULE_METADATA = "metaData";
67 const std::string MODULE_COLOR_MODE = "colorMode";
68 const std::string MODULE_DISTRO = "distro";
69 const std::string MODULE_REQ_CAPABILITIES = "reqCapabilities";
70 const std::string MODULE_DATA_DIR = "moduleDataDir";
71 const std::string MODULE_RES_PATH = "moduleResPath";
72 const std::string MODULE_HAP_PATH = "hapPath";
73 const std::string MODULE_ABILITY_KEYS = "abilityKeys";
74 const std::string MODULE_SKILL_KEYS = "skillKeys";
75 const std::string MODULE_FORMS = "formInfos";
76 const std::string MODULE_SHORTCUT = "shortcutInfos";
77 const std::string MODULE_COMMON_EVENT = "commonEvents";
78 const std::string MODULE_MAIN_ABILITY = "mainAbility";
79 const std::string MODULE_ENTRY_ABILITY_KEY = "entryAbilityKey";
80 const std::string MODULE_DEPENDENCIES = "dependencies";
81 const std::string MODULE_IS_LIB_ISOLATED = "isLibIsolated";
82 const std::string MODULE_NATIVE_LIBRARY_PATH = "nativeLibraryPath";
83 const std::string MODULE_CPU_ABI = "cpuAbi";
84 const std::string NEW_BUNDLE_NAME = "newBundleName";
85 const std::string MODULE_SRC_PATH = "srcPath";
86 const std::string MODULE_HASH_VALUE = "hashValue";
87 const std::string SCHEME_SEPARATOR = "://";
88 const std::string PORT_SEPARATOR = ":";
89 const std::string PATH_SEPARATOR = "/";
90 const std::string PARAM_SEPARATOR = "?";
91 const std::string IS_PREINSTALL_APP = "isPreInstallApp";
92 const std::string INSTALL_MARK = "installMark";
93 const char WILDCARD = '*';
94 const std::string TYPE_WILDCARD = "*/*";
95 const std::string INNER_BUNDLE_USER_INFOS = "innerBundleUserInfos";
96 const std::string MODULE_PROCESS = "process";
97 const std::string MODULE_SRC_ENTRANCE = "srcEntrance";
98 const std::string MODULE_DEVICE_TYPES = "deviceTypes";
99 const std::string MODULE_VIRTUAL_MACHINE = "virtualMachine";
100 const std::string MODULE_UI_SYNTAX = "uiSyntax";
101 const std::string MODULE_PAGES = "pages";
102 const std::string MODULE_META_DATA = "metadata";
103 const std::string MODULE_REQUEST_PERMISSIONS = "requestPermissions";
104 const std::string MODULE_DEFINE_PERMISSIONS = "definePermissions";
105 const std::string MODULE_EXTENSION_KEYS = "extensionKeys";
106 const std::string MODULE_EXTENSION_SKILL_KEYS = "extensionSkillKeys";
107 const std::string MODULE_IS_MODULE_JSON = "isModuleJson";
108 const std::string MODULE_IS_STAGE_BASED_MODEL = "isStageBasedModel";
109 const std::string BUNDLE_IS_NEW_VERSION = "isNewVersion";
110 const std::string BUNDLE_IS_NEED_UPDATE = "upgradeFlag";
111 const std::string BUNDLE_BASE_EXTENSION_INFOS = "baseExtensionInfos";
112 const std::string BUNDLE_EXTENSION_SKILL_INFOS = "extensionSkillInfos";
113 const std::string BUNDLE_PACK_INFO = "bundlePackInfo";
114 const std::string ALLOWED_ACLS = "allowedAcls";
115 const std::string META_DATA_SHORTCUTS_NAME = "ohos.ability.shortcuts";
116 const std::string APP_INDEX = "appIndex";
117 const std::string BUNDLE_IS_SANDBOX_APP = "isSandboxApp";
118 const std::string BUNDLE_SANDBOX_PERSISTENT_INFO = "sandboxPersistentInfo";
119 const std::string MODULE_COMPILE_MODE = "compileMode";
120 const std::string BUNDLE_HQF_INFOS = "hqfInfos";
121 const std::string MODULE_TARGET_MODULE_NAME = "targetModuleName";
122 const std::string MODULE_TARGET_PRIORITY = "targetPriority";
123 const std::string MODULE_OVERLAY_MODULE_INFO = "overlayModuleInfo";
124 const std::string OVERLAY_BUNDLE_INFO = "overlayBundleInfo";
125 const std::string OVERLAY_TYPE = "overlayType";
126 const std::string APPLY_QUICK_FIX_FREQUENCY = "applyQuickFixFrequency";
127 const std::string MODULE_ATOMIC_SERVICE_MODULE_TYPE = "atomicServiceModuleType";
128 const std::string MODULE_PRELOADS = "preloads";
129 const std::string INNER_SHARED_MODULE_INFO = "innerSharedModuleInfos";
130 const std::string MODULE_BUNDLE_TYPE = "bundleType";
131 const std::string MODULE_VERSION_CODE = "versionCode";
132 const std::string MODULE_VERSION_NAME = "versionName";
133 const std::string MODULE_PROXY_DATAS = "proxyDatas";
134 const std::string MODULE_BUILD_HASH = "buildHash";
135 const std::string MODULE_ISOLATION_MODE = "isolationMode";
136 const std::string MODULE_COMPRESS_NATIVE_LIBS = "compressNativeLibs";
137 const std::string MODULE_NATIVE_LIBRARY_FILE_NAMES = "nativeLibraryFileNames";
138 const std::string MODULE_AOT_COMPILE_STATUS = "aotCompileStatus";
139 const std::string DATA_GROUP_INFOS = "dataGroupInfos";
140 const int32_t SINGLE_HSP_VERSION = 1;
141 const std::map<std::string, IsolationMode> ISOLATION_MODE_MAP = {
142     {"isolationOnly", IsolationMode::ISOLATION_ONLY},
143     {"nonisolationOnly", IsolationMode::NONISOLATION_ONLY},
144     {"isolationFirst", IsolationMode::ISOLATION_FIRST},
145 };
146 const std::string NATIVE_LIBRARY_PATH_SYMBOL = "!/";
147 
148 const std::string STR_PHONE = "phone";
149 const std::string STR_DEFAULT = "default";
150 
ConvertCompileMode(const std::string & compileMode)151 inline CompileMode ConvertCompileMode(const std::string& compileMode)
152 {
153     if (compileMode == Profile::COMPILE_MODE_ES_MODULE) {
154         return CompileMode::ES_MODULE;
155     } else {
156         return CompileMode::JS_BUNDLE;
157     }
158 }
159 
NameAndUserIdToKey(const std::string & bundleName,int32_t userId)160 const std::string NameAndUserIdToKey(const std::string &bundleName, int32_t userId)
161 {
162     return bundleName + Constants::FILE_UNDERLINE + std::to_string(userId);
163 }
164 }  // namespace
165 
SetAOTCompileStatus(const std::string & moduleName,AOTCompileStatus aotCompileStatus)166 void InnerBundleInfo::SetAOTCompileStatus(const std::string &moduleName, AOTCompileStatus aotCompileStatus)
167 {
168     auto item = innerModuleInfos_.find(moduleName);
169     if (item == innerModuleInfos_.end()) {
170         APP_LOGE("moduleName %{public}s not exist", moduleName.c_str());
171         return;
172     }
173     item->second.aotCompileStatus = aotCompileStatus;
174 }
175 
GetAOTCompileStatus(const std::string & moduleName) const176 AOTCompileStatus InnerBundleInfo::GetAOTCompileStatus(const std::string &moduleName) const
177 {
178     auto item = innerModuleInfos_.find(moduleName);
179     if (item == innerModuleInfos_.end()) {
180         APP_LOGE("moduleName %{public}s not exist", moduleName.c_str());
181         return AOTCompileStatus::NOT_COMPILED;
182     }
183     return item->second.aotCompileStatus;
184 }
185 
ResetAOTFlags()186 void InnerBundleInfo::ResetAOTFlags()
187 {
188     baseApplicationInfo_->arkNativeFilePath.clear();
189     baseApplicationInfo_->arkNativeFileAbi.clear();
190     std::for_each(innerModuleInfos_.begin(), innerModuleInfos_.end(), [](auto &item) {
191         item.second.aotCompileStatus = AOTCompileStatus::NOT_COMPILED;
192     });
193 }
194 
Match(const OHOS::AAFwk::Want & want) const195 bool Skill::Match(const OHOS::AAFwk::Want &want) const
196 {
197     bool matchAction = MatchAction(want.GetAction());
198     if (!matchAction) {
199         APP_LOGD("Action does not match");
200         return false;
201     }
202     bool matchEntities = MatchEntities(want.GetEntities());
203     if (!matchEntities) {
204         APP_LOGD("Entities does not match");
205         return false;
206     }
207     std::vector<std::string> vecTypes;
208     auto deviceType = OHOS::system::GetDeviceType();
209     APP_LOGD("DeviceType %{public}s", deviceType.c_str());
210     if (STR_PHONE == deviceType || STR_DEFAULT == deviceType) {
211         vecTypes = want.GetStringArrayParam(OHOS::AAFwk::Want::PARAM_ABILITY_URITYPES);
212     }
213     if (vecTypes.size() > 0) {
214         for (std::string strType : vecTypes) {
215             if (MatchUriAndType(want.GetUriString(), strType)) {
216                 APP_LOGD("type %{public}s, Is Matched", strType.c_str());
217                 return true;
218             }
219         }
220         return false;
221     }
222     bool matchUriAndType = MatchUriAndType(want.GetUriString(), want.GetType());
223     if (!matchUriAndType) {
224         APP_LOGD("Uri or Type does not match");
225         return false;
226     }
227     return true;
228 }
229 
MatchLauncher(const OHOS::AAFwk::Want & want) const230 bool Skill::MatchLauncher(const OHOS::AAFwk::Want &want) const
231 {
232     bool matchAction = MatchAction(want.GetAction());
233     if (!matchAction) {
234         APP_LOGD("Action does not match");
235         return false;
236     }
237     bool matchEntities = MatchEntities(want.GetEntities());
238     if (!matchEntities) {
239         APP_LOGD("Entities does not match");
240         return false;
241     }
242     return true;
243 }
244 
MatchAction(const std::string & action) const245 bool Skill::MatchAction(const std::string &action) const
246 {
247     // config actions empty, no match
248     if (actions.empty()) {
249         return false;
250     }
251     // config actions not empty, param empty, match
252     if (action.empty()) {
253         return true;
254     }
255     auto actionMatcher = [action] (const std::string &configAction) {
256         if (action == configAction) {
257             return true;
258         }
259         if (action == Constants::ACTION_HOME && configAction == Constants::WANT_ACTION_HOME) {
260             return true;
261         }
262         if (action == Constants::WANT_ACTION_HOME && configAction == Constants::ACTION_HOME) {
263             return true;
264         }
265         return false;
266     };
267     // config actions not empty, param not empty, if config actions contains param action, match
268     return std::find_if(actions.cbegin(), actions.cend(), actionMatcher) != actions.cend();
269 }
270 
MatchEntities(const std::vector<std::string> & paramEntities) const271 bool Skill::MatchEntities(const std::vector<std::string> &paramEntities) const
272 {
273     // param entities empty, match
274     if (paramEntities.empty()) {
275         return true;
276     }
277     // config entities empty, param entities not empty, not match
278     if (entities.empty()) {
279         return false;
280     }
281     // config entities not empty, param entities not empty, if every param entity in config entities, match
282     std::vector<std::string>::size_type size = paramEntities.size();
283     for (std::vector<std::string>::size_type i = 0; i < size; i++) {
284         bool ret = std::find(entities.cbegin(), entities.cend(), paramEntities[i]) == entities.cend();
285         if (ret) {
286             return false;
287         }
288     }
289     return true;
290 }
291 
MatchUriAndType(const std::string & uriString,const std::string & type) const292 bool Skill::MatchUriAndType(const std::string &uriString, const std::string &type) const
293 {
294     if (uriString.empty() && type.empty()) {
295         // case1 : param uri empty, param type empty
296         if (uris.empty()) {
297             return true;
298         }
299         for (const SkillUri &skillUri : uris) {
300             if (skillUri.scheme.empty() && skillUri.type.empty()) {
301                 return true;
302             }
303         }
304         return false;
305     }
306     if (uris.empty()) {
307         return false;
308     }
309     if (!uriString.empty() && type.empty()) {
310         // case2 : param uri not empty, param type empty
311         for (const SkillUri &skillUri : uris) {
312             if (MatchUri(uriString, skillUri) && skillUri.type.empty()) {
313                 return true;
314             }
315         }
316         // if uri is a file path, match type by the suffix
317         return MatchMimeType(uriString);
318     } else if (uriString.empty() && !type.empty()) {
319         // case3 : param uri empty, param type not empty
320         for (const SkillUri &skillUri : uris) {
321             if (skillUri.scheme.empty() && MatchType(type, skillUri.type)) {
322                 return true;
323             }
324         }
325         return false;
326     } else {
327         // case4 : param uri not empty, param type not empty
328         for (const SkillUri &skillUri : uris) {
329             if (MatchUri(uriString, skillUri) && MatchType(type, skillUri.type)) {
330                 return true;
331             }
332         }
333         return false;
334     }
335 }
336 
StartsWith(const std::string & sourceString,const std::string & targetPrefix) const337 bool Skill::StartsWith(const std::string &sourceString, const std::string &targetPrefix) const
338 {
339     return sourceString.rfind(targetPrefix, 0) == 0;
340 }
341 
GetOptParamUri(const std::string & uriString) const342 std::string Skill::GetOptParamUri(const std::string &uriString) const
343 {
344     std::size_t pos = uriString.rfind(PARAM_SEPARATOR);
345     if (pos == std::string::npos) {
346         return uriString;
347     }
348     return uriString.substr(0, pos);
349 }
350 
MatchUri(const std::string & uriString,const SkillUri & skillUri) const351 bool Skill::MatchUri(const std::string &uriString, const SkillUri &skillUri) const
352 {
353     if (skillUri.scheme.empty()) {
354         return uriString.empty();
355     }
356     if (skillUri.host.empty()) {
357         // config uri is : scheme
358         // belows are param uri matched conditions:
359         // 1.scheme
360         // 2.scheme:
361         // 3.scheme:/
362         // 4.scheme://
363         return uriString == skillUri.scheme || StartsWith(uriString, skillUri.scheme + PORT_SEPARATOR);
364     }
365     std::string optParamUri = GetOptParamUri(uriString);
366     std::string skillUriString;
367     skillUriString.append(skillUri.scheme).append(SCHEME_SEPARATOR).append(skillUri.host);
368     if (!skillUri.port.empty()) {
369         skillUriString.append(PORT_SEPARATOR).append(skillUri.port);
370     }
371     if (skillUri.path.empty() && skillUri.pathStartWith.empty() && skillUri.pathRegex.empty()) {
372         // with port, config uri is : scheme://host:port
373         // belows are param uri matched conditions:
374         // 1.scheme://host:port
375         // 2.scheme://host:port/path
376 
377         // without port, config uri is : scheme://host
378         // belows are param uri matched conditions:
379         // 1.scheme://host
380         // 2.scheme://host/path
381         // 3.scheme://host:port     scheme://host:port/path
382         bool ret = (optParamUri == skillUriString || StartsWith(optParamUri, skillUriString + PATH_SEPARATOR));
383         if (skillUri.port.empty()) {
384             ret = ret || StartsWith(optParamUri, skillUriString + PORT_SEPARATOR);
385         }
386         return ret;
387     }
388     skillUriString.append(PATH_SEPARATOR);
389     // if one of path, pathStartWith, pathRegex match, then match
390     if (!skillUri.path.empty()) {
391         // path match
392         std::string pathUri(skillUriString);
393         pathUri.append(skillUri.path);
394         if (optParamUri == pathUri) {
395             return true;
396         }
397     }
398     if (!skillUri.pathStartWith.empty()) {
399         // pathStartWith match
400         std::string pathStartWithUri(skillUriString);
401         pathStartWithUri.append(skillUri.pathStartWith);
402         if (StartsWith(optParamUri, pathStartWithUri)) {
403             return true;
404         }
405     }
406     if (!skillUri.pathRegex.empty()) {
407         // pathRegex match
408         std::string pathRegexUri(skillUriString);
409         pathRegexUri.append(skillUri.pathRegex);
410         try {
411             std::regex regex(pathRegexUri);
412             if (regex_match(optParamUri, regex)) {
413                 return true;
414             }
415         } catch(...) {
416             APP_LOGE("regex error");
417         }
418     }
419     return false;
420 }
421 
MatchType(const std::string & type,const std::string & skillUriType) const422 bool Skill::MatchType(const std::string &type, const std::string &skillUriType) const
423 {
424     // type is not empty
425     if (skillUriType.empty()) {
426         return false;
427     }
428     // only match */*
429     if (type == Constants::TYPE_ONLY_MATCH_WILDCARD) {
430         return skillUriType == TYPE_WILDCARD;
431     }
432     if (type == TYPE_WILDCARD || skillUriType == TYPE_WILDCARD) {
433         // param is */* or config is */*
434         return true;
435     }
436     bool paramTypeRegex = type.back() == WILDCARD;
437     if (paramTypeRegex) {
438         // param is string/*
439         std::string prefix = type.substr(0, type.length() - 1);
440         return skillUriType.find(prefix) == 0;
441     }
442     bool typeRegex = skillUriType.back() == WILDCARD;
443     if (typeRegex) {
444         // config is string/*
445         std::string prefix = skillUriType.substr(0, skillUriType.length() - 1);
446         return type.find(prefix) == 0;
447     } else {
448         return type == skillUriType;
449     }
450 }
451 
MatchMimeType(const std::string & uriString) const452 bool Skill::MatchMimeType(const std::string & uriString) const
453 {
454     std::vector<std::string> mimeTypes;
455     bool ret = MimeTypeMgr::GetMimeTypeByUri(uriString, mimeTypes);
456     if (!ret) {
457         return false;
458     }
459     for (const SkillUri &skillUri : uris) {
460         for (const auto &mimeType : mimeTypes) {
461             if ((MatchUri(uriString, skillUri) || skillUri.scheme.empty())
462                 && MatchType(mimeType, skillUri.type)) {
463                 return true;
464             }
465         }
466     }
467     return false;
468 }
469 
InnerBundleInfo()470 InnerBundleInfo::InnerBundleInfo()
471 {
472     baseApplicationInfo_ = std::make_shared<ApplicationInfo>();
473     if (baseApplicationInfo_ == nullptr) {
474         APP_LOGE("baseApplicationInfo_ is nullptr, create failed");
475     }
476     baseBundleInfo_ = std::make_shared<BundleInfo>();
477     if (baseBundleInfo_ == nullptr) {
478         APP_LOGE("baseBundleInfo_ is nullptr, create failed");
479     }
480     bundlePackInfo_ = std::make_shared<BundlePackInfo>();
481     if (bundlePackInfo_ == nullptr) {
482         APP_LOGE("bundlePackInfo_ is nullptr, create failed");
483     }
484     APP_LOGD("inner bundle info instance is created");
485 }
486 
operator =(const InnerBundleInfo & info)487 InnerBundleInfo &InnerBundleInfo::operator=(const InnerBundleInfo &info)
488 {
489     if (this == &info) {
490         return *this;
491     }
492     this->appType_ = info.appType_;
493     this->uid_ = info.uid_;
494     this->gid_ = info.gid_;
495     this->userId_ = info.userId_;
496     this->bundleStatus_ = info.bundleStatus_;
497     this->appFeature_ = info.appFeature_;
498     this->allowedAcls_ = info.allowedAcls_;
499     this->mark_ = info.mark_;
500     this->appIndex_ = info.appIndex_;
501     this->isSandboxApp_ = info.isSandboxApp_;
502     this->currentPackage_ = info.currentPackage_;
503     this->onlyCreateBundleUser_ = info.onlyCreateBundleUser_;
504     this->innerModuleInfos_ = info.innerModuleInfos_;
505     this->innerSharedModuleInfos_ = info.innerSharedModuleInfos_;
506     this->formInfos_ = info.formInfos_;
507     this->commonEvents_ = info.commonEvents_;
508     this->shortcutInfos_ = info.shortcutInfos_;
509     this->baseAbilityInfos_ = info.baseAbilityInfos_;
510     this->skillInfos_ = info.skillInfos_;
511     this->innerBundleUserInfos_ = info.innerBundleUserInfos_;
512     this->bundlePackInfo_ = std::make_shared<BundlePackInfo>();
513     if (info.bundlePackInfo_ != nullptr) {
514         *(this->bundlePackInfo_) = *(info.bundlePackInfo_);
515     }
516     this->isNewVersion_ = info.isNewVersion_;
517     this->baseExtensionInfos_= info.baseExtensionInfos_;
518     this->extensionSkillInfos_ = info.extensionSkillInfos_;
519     this->baseApplicationInfo_ = std::make_shared<ApplicationInfo>();
520     if (info.baseApplicationInfo_ != nullptr) {
521         *(this->baseApplicationInfo_) = *(info.baseApplicationInfo_);
522     }
523     this->baseBundleInfo_ = std::make_shared<BundleInfo>();
524     if (info.baseBundleInfo_ != nullptr) {
525         *(this->baseBundleInfo_) = *(info.baseBundleInfo_);
526     }
527     this->hqfInfos_ = info.hqfInfos_;
528     this->overlayBundleInfo_ = info.overlayBundleInfo_;
529     this->overlayType_ = info.overlayType_;
530     this->applyQuickFixFrequency_ = info.applyQuickFixFrequency_;
531     this->provisionMetadatas_ = info.provisionMetadatas_;
532     this->dataGroupInfos_ = info.dataGroupInfos_;
533     return *this;
534 }
535 
~InnerBundleInfo()536 InnerBundleInfo::~InnerBundleInfo()
537 {
538     APP_LOGD("inner bundle info instance is destroyed");
539 }
540 
to_json(nlohmann::json & jsonObject,const Distro & distro)541 void to_json(nlohmann::json &jsonObject, const Distro &distro)
542 {
543     jsonObject = nlohmann::json {
544             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_DELIVERY_WITH_INSTALL, distro.deliveryWithInstall},
545             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_NAME, distro.moduleName},
546             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_TYPE, distro.moduleType},
547             {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_INSTALLATION_FREE, distro.installationFree}
548     };
549 }
550 
to_json(nlohmann::json & jsonObject,const DefinePermission & definePermission)551 void to_json(nlohmann::json &jsonObject, const DefinePermission &definePermission)
552 {
553     jsonObject = nlohmann::json {
554         {Profile::DEFINEPERMISSION_NAME, definePermission.name},
555         {Profile::DEFINEPERMISSION_GRANT_MODE, definePermission.grantMode},
556         {Profile::DEFINEPERMISSION_AVAILABLE_LEVEL, definePermission.availableLevel},
557         {Profile::DEFINEPERMISSION_PROVISION_ENABLE, definePermission.provisionEnable},
558         {Profile::DEFINEPERMISSION_DISTRIBUTED_SCENE_ENABLE, definePermission.distributedSceneEnable},
559         {Profile::LABEL, definePermission.label},
560         {Profile::LABEL_ID, definePermission.labelId},
561         {Profile::DESCRIPTION, definePermission.description},
562         {Profile::DESCRIPTION_ID, definePermission.descriptionId}
563     };
564 }
565 
to_json(nlohmann::json & jsonObject,const Dependency & dependency)566 void to_json(nlohmann::json &jsonObject, const Dependency &dependency)
567 {
568     jsonObject = nlohmann::json {
569         {Profile::DEPENDENCIES_MODULE_NAME, dependency.moduleName},
570         {Profile::DEPENDENCIES_BUNDLE_NAME, dependency.bundleName},
571         {Profile::APP_VERSION_CODE, dependency.versionCode}
572     };
573 }
574 
to_json(nlohmann::json & jsonObject,const InnerModuleInfo & info)575 void to_json(nlohmann::json &jsonObject, const InnerModuleInfo &info)
576 {
577     jsonObject = nlohmann::json {
578         {NAME, info.name},
579         {MODULE_PACKAGE, info.modulePackage},
580         {MODULE_NAME, info.moduleName},
581         {MODULE_PATH, info.modulePath},
582         {MODULE_DATA_DIR, info.moduleDataDir},
583         {MODULE_RES_PATH, info.moduleResPath},
584         {MODULE_IS_ENTRY, info.isEntry},
585         {MODULE_METADATA, info.metaData},
586         {MODULE_COLOR_MODE, info.colorMode},
587         {MODULE_DISTRO, info.distro},
588         {MODULE_DESCRIPTION, info.description},
589         {MODULE_DESCRIPTION_ID, info.descriptionId},
590         {MODULE_ICON, info.icon},
591         {MODULE_ICON_ID, info.iconId},
592         {MODULE_LABEL, info.label},
593         {MODULE_LABEL_ID, info.labelId},
594         {MODULE_DESCRIPTION_INSTALLATION_FREE, info.installationFree},
595         {MODULE_IS_REMOVABLE, info.isRemovable},
596         {MODULE_UPGRADE_FLAG, info.upgradeFlag},
597         {MODULE_REQ_CAPABILITIES, info.reqCapabilities},
598         {MODULE_ABILITY_KEYS, info.abilityKeys},
599         {MODULE_SKILL_KEYS, info.skillKeys},
600         {MODULE_MAIN_ABILITY, info.mainAbility},
601         {MODULE_ENTRY_ABILITY_KEY, info.entryAbilityKey},
602         {MODULE_SRC_PATH, info.srcPath},
603         {MODULE_HASH_VALUE, info.hashValue},
604         {MODULE_PROCESS, info.process},
605         {MODULE_SRC_ENTRANCE, info.srcEntrance},
606         {MODULE_DEVICE_TYPES, info.deviceTypes},
607         {MODULE_VIRTUAL_MACHINE, info.virtualMachine},
608         {MODULE_UI_SYNTAX, info.uiSyntax},
609         {MODULE_PAGES, info.pages},
610         {MODULE_META_DATA, info.metadata},
611         {MODULE_REQUEST_PERMISSIONS, info.requestPermissions},
612         {MODULE_DEFINE_PERMISSIONS, info.definePermissions},
613         {MODULE_EXTENSION_KEYS, info.extensionKeys},
614         {MODULE_EXTENSION_SKILL_KEYS, info.extensionSkillKeys},
615         {MODULE_IS_MODULE_JSON, info.isModuleJson},
616         {MODULE_IS_STAGE_BASED_MODEL, info.isStageBasedModel},
617         {MODULE_DEPENDENCIES, info.dependencies},
618         {MODULE_IS_LIB_ISOLATED, info.isLibIsolated},
619         {MODULE_NATIVE_LIBRARY_PATH, info.nativeLibraryPath},
620         {MODULE_CPU_ABI, info.cpuAbi},
621         {MODULE_HAP_PATH, info.hapPath},
622         {MODULE_COMPILE_MODE, info.compileMode},
623         {MODULE_TARGET_MODULE_NAME, info.targetModuleName},
624         {MODULE_TARGET_PRIORITY, info.targetPriority},
625         {MODULE_OVERLAY_MODULE_INFO, info.overlayModuleInfo},
626         {MODULE_ATOMIC_SERVICE_MODULE_TYPE, info.atomicServiceModuleType},
627         {MODULE_PRELOADS, info.preloads},
628         {MODULE_BUNDLE_TYPE, info.bundleType},
629         {MODULE_VERSION_CODE, info.versionCode},
630         {MODULE_VERSION_NAME, info.versionName},
631         {MODULE_PROXY_DATAS, info.proxyDatas},
632         {MODULE_BUILD_HASH, info.buildHash},
633         {MODULE_ISOLATION_MODE, info.isolationMode},
634         {MODULE_COMPRESS_NATIVE_LIBS, info.compressNativeLibs},
635         {MODULE_NATIVE_LIBRARY_FILE_NAMES, info.nativeLibraryFileNames},
636         {MODULE_AOT_COMPILE_STATUS, info.aotCompileStatus},
637     };
638 }
639 
to_json(nlohmann::json & jsonObject,const SkillUri & uri)640 void to_json(nlohmann::json &jsonObject, const SkillUri &uri)
641 {
642     jsonObject = nlohmann::json {
643         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_SCHEME, uri.scheme},
644         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_HOST, uri.host},
645         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PORT, uri.port},
646         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATH, uri.path},
647         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATHSTARTWITH, uri.pathStartWith},
648         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATHREGEX, uri.pathRegex},
649         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_TYPE, uri.type}
650     };
651 }
652 
to_json(nlohmann::json & jsonObject,const Skill & skill)653 void to_json(nlohmann::json &jsonObject, const Skill &skill)
654 {
655     jsonObject = nlohmann::json {
656         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_ACTIONS, skill.actions},
657         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_ENTITIES, skill.entities},
658         {ProfileReader::BUNDLE_MODULE_PROFILE_KEY_URIS, skill.uris}
659     };
660 }
661 
to_json(nlohmann::json & jsonObject,const InstallMark & installMark)662 void to_json(nlohmann::json &jsonObject, const InstallMark &installMark)
663 {
664     jsonObject = nlohmann::json {
665         {ProfileReader::BUNDLE_INSTALL_MARK_BUNDLE, installMark.bundleName},
666         {ProfileReader::BUNDLE_INSTALL_MARK_PACKAGE, installMark.packageName},
667         {ProfileReader::BUNDLE_INSTALL_MARK_STATUS, installMark.status}
668     };
669 }
670 
ToJson(nlohmann::json & jsonObject) const671 void InnerBundleInfo::ToJson(nlohmann::json &jsonObject) const
672 {
673     jsonObject[APP_TYPE] = appType_;
674     jsonObject[BUNDLE_STATUS] = bundleStatus_;
675     jsonObject[ALLOWED_ACLS] = allowedAcls_;
676     jsonObject[BASE_APPLICATION_INFO] = *baseApplicationInfo_;
677     jsonObject[BASE_BUNDLE_INFO] = *baseBundleInfo_;
678     jsonObject[BASE_ABILITY_INFO] = baseAbilityInfos_;
679     jsonObject[INNER_MODULE_INFO] = innerModuleInfos_;
680     jsonObject[INNER_SHARED_MODULE_INFO] = innerSharedModuleInfos_;
681     jsonObject[SKILL_INFOS] = skillInfos_;
682     jsonObject[USER_ID] = userId_;
683     jsonObject[APP_FEATURE] = appFeature_;
684     jsonObject[MODULE_FORMS] = formInfos_;
685     jsonObject[MODULE_SHORTCUT] = shortcutInfos_;
686     jsonObject[MODULE_COMMON_EVENT] = commonEvents_;
687     jsonObject[INSTALL_MARK] = mark_;
688     jsonObject[INNER_BUNDLE_USER_INFOS] = innerBundleUserInfos_;
689     jsonObject[BUNDLE_IS_NEW_VERSION] = isNewVersion_;
690     jsonObject[BUNDLE_BASE_EXTENSION_INFOS] = baseExtensionInfos_;
691     jsonObject[BUNDLE_EXTENSION_SKILL_INFOS] = extensionSkillInfos_;
692     jsonObject[BUNDLE_PACK_INFO] = *bundlePackInfo_;
693     jsonObject[APP_INDEX] = appIndex_;
694     jsonObject[BUNDLE_IS_SANDBOX_APP] = isSandboxApp_;
695     jsonObject[BUNDLE_HQF_INFOS] = hqfInfos_;
696     jsonObject[OVERLAY_BUNDLE_INFO] = overlayBundleInfo_;
697     jsonObject[OVERLAY_TYPE] = overlayType_;
698     jsonObject[APPLY_QUICK_FIX_FREQUENCY] = applyQuickFixFrequency_;
699     jsonObject[DATA_GROUP_INFOS] = dataGroupInfos_;
700 }
701 
from_json(const nlohmann::json & jsonObject,InnerModuleInfo & info)702 void from_json(const nlohmann::json &jsonObject, InnerModuleInfo &info)
703 {
704     // these are not required fields.
705     const auto &jsonObjectEnd = jsonObject.end();
706     int32_t parseResult = ERR_OK;
707     GetValueIfFindKey<std::string>(jsonObject,
708         jsonObjectEnd,
709         NAME,
710         info.name,
711         JsonType::STRING,
712         false,
713         parseResult,
714         ArrayType::NOT_ARRAY);
715     GetValueIfFindKey<std::string>(jsonObject,
716         jsonObjectEnd,
717         MODULE_PACKAGE,
718         info.modulePackage,
719         JsonType::STRING,
720         false,
721         parseResult,
722         ArrayType::NOT_ARRAY);
723     GetValueIfFindKey<std::string>(jsonObject,
724         jsonObjectEnd,
725         MODULE_NAME,
726         info.moduleName,
727         JsonType::STRING,
728         false,
729         parseResult,
730         ArrayType::NOT_ARRAY);
731     GetValueIfFindKey<std::string>(jsonObject,
732         jsonObjectEnd,
733         MODULE_PATH,
734         info.modulePath,
735         JsonType::STRING,
736         false,
737         parseResult,
738         ArrayType::NOT_ARRAY);
739     GetValueIfFindKey<std::string>(jsonObject,
740         jsonObjectEnd,
741         MODULE_DATA_DIR,
742         info.moduleDataDir,
743         JsonType::STRING,
744         false,
745         parseResult,
746         ArrayType::NOT_ARRAY);
747     GetValueIfFindKey<std::string>(jsonObject,
748         jsonObjectEnd,
749         MODULE_HAP_PATH,
750         info.hapPath,
751         JsonType::STRING,
752         false,
753         parseResult,
754         ArrayType::NOT_ARRAY);
755     GetValueIfFindKey<std::string>(jsonObject,
756         jsonObjectEnd,
757         MODULE_RES_PATH,
758         info.moduleResPath,
759         JsonType::STRING,
760         false,
761         parseResult,
762         ArrayType::NOT_ARRAY);
763     GetValueIfFindKey<bool>(jsonObject,
764         jsonObjectEnd,
765         MODULE_IS_ENTRY,
766         info.isEntry,
767         JsonType::BOOLEAN,
768         false,
769         parseResult,
770         ArrayType::NOT_ARRAY);
771     GetValueIfFindKey<MetaData>(jsonObject,
772         jsonObjectEnd,
773         MODULE_METADATA,
774         info.metaData,
775         JsonType::OBJECT,
776         false,
777         parseResult,
778         ArrayType::NOT_ARRAY);
779     GetValueIfFindKey<ModuleColorMode>(jsonObject,
780         jsonObjectEnd,
781         MODULE_COLOR_MODE,
782         info.colorMode,
783         JsonType::NUMBER,
784         false,
785         parseResult,
786         ArrayType::NOT_ARRAY);
787     GetValueIfFindKey<Distro>(jsonObject,
788         jsonObjectEnd,
789         MODULE_DISTRO,
790         info.distro,
791         JsonType::OBJECT,
792         false,
793         parseResult,
794         ArrayType::NOT_ARRAY);
795     GetValueIfFindKey<std::string>(jsonObject,
796         jsonObjectEnd,
797         MODULE_DESCRIPTION,
798         info.description,
799         JsonType::STRING,
800         false,
801         parseResult,
802         ArrayType::NOT_ARRAY);
803     GetValueIfFindKey<int32_t>(jsonObject,
804         jsonObjectEnd,
805         MODULE_DESCRIPTION_ID,
806         info.descriptionId,
807         JsonType::NUMBER,
808         false,
809         parseResult,
810         ArrayType::NOT_ARRAY);
811     GetValueIfFindKey<std::string>(jsonObject,
812         jsonObjectEnd,
813         MODULE_ICON,
814         info.icon,
815         JsonType::STRING,
816         false,
817         parseResult,
818         ArrayType::NOT_ARRAY);
819     GetValueIfFindKey<int32_t>(jsonObject,
820         jsonObjectEnd,
821         MODULE_ICON_ID,
822         info.iconId,
823         JsonType::NUMBER,
824         false,
825         parseResult,
826         ArrayType::NOT_ARRAY);
827     GetValueIfFindKey<std::string>(jsonObject,
828         jsonObjectEnd,
829         MODULE_LABEL,
830         info.label,
831         JsonType::STRING,
832         false,
833         parseResult,
834         ArrayType::NOT_ARRAY);
835     GetValueIfFindKey<int32_t>(jsonObject,
836         jsonObjectEnd,
837         MODULE_LABEL_ID,
838         info.labelId,
839         JsonType::NUMBER,
840         false,
841         parseResult,
842         ArrayType::NOT_ARRAY);
843     GetValueIfFindKey<std::string>(jsonObject,
844         jsonObjectEnd,
845         MODULE_MAIN_ABILITY,
846         info.mainAbility,
847         JsonType::STRING,
848         false,
849         parseResult,
850         ArrayType::NOT_ARRAY);
851     GetValueIfFindKey<std::string>(jsonObject,
852         jsonObjectEnd,
853         MODULE_ENTRY_ABILITY_KEY,
854         info.entryAbilityKey,
855         JsonType::STRING,
856         false,
857         parseResult,
858         ArrayType::NOT_ARRAY);
859     GetValueIfFindKey<std::string>(jsonObject,
860         jsonObjectEnd,
861         MODULE_SRC_PATH,
862         info.srcPath,
863         JsonType::STRING,
864         false,
865         parseResult,
866         ArrayType::NOT_ARRAY);
867     GetValueIfFindKey<std::string>(jsonObject,
868         jsonObjectEnd,
869         MODULE_HASH_VALUE,
870         info.hashValue,
871         JsonType::STRING,
872         false,
873         parseResult,
874         ArrayType::NOT_ARRAY);
875     GetValueIfFindKey<bool>(jsonObject,
876         jsonObjectEnd,
877         MODULE_DESCRIPTION_INSTALLATION_FREE,
878         info.installationFree,
879         JsonType::BOOLEAN,
880         false,
881         parseResult,
882         ArrayType::NOT_ARRAY);
883     GetValueIfFindKey<std::map<std::string, bool>>(jsonObject,
884         jsonObjectEnd,
885         MODULE_IS_REMOVABLE,
886         info.isRemovable,
887         JsonType::OBJECT,
888         false,
889         parseResult,
890         ArrayType::NOT_ARRAY);
891     GetValueIfFindKey<int32_t>(jsonObject,
892         jsonObjectEnd,
893         MODULE_UPGRADE_FLAG,
894         info.upgradeFlag,
895         JsonType::NUMBER,
896         false,
897         parseResult,
898         ArrayType::NOT_ARRAY);
899     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
900         jsonObjectEnd,
901         MODULE_REQ_CAPABILITIES,
902         info.reqCapabilities,
903         JsonType::ARRAY,
904         false,
905         parseResult,
906         ArrayType::STRING);
907     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
908         jsonObjectEnd,
909         MODULE_ABILITY_KEYS,
910         info.abilityKeys,
911         JsonType::ARRAY,
912         false,
913         parseResult,
914         ArrayType::STRING);
915     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
916         jsonObjectEnd,
917         MODULE_SKILL_KEYS,
918         info.skillKeys,
919         JsonType::ARRAY,
920         false,
921         parseResult,
922         ArrayType::STRING);
923     GetValueIfFindKey<std::string>(jsonObject,
924         jsonObjectEnd,
925         MODULE_PROCESS,
926         info.process,
927         JsonType::STRING,
928         false,
929         parseResult,
930         ArrayType::NOT_ARRAY);
931     GetValueIfFindKey<std::string>(jsonObject,
932         jsonObjectEnd,
933         MODULE_SRC_ENTRANCE,
934         info.srcEntrance,
935         JsonType::STRING,
936         false,
937         parseResult,
938         ArrayType::NOT_ARRAY);
939     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
940         jsonObjectEnd,
941         MODULE_DEVICE_TYPES,
942         info.deviceTypes,
943         JsonType::ARRAY,
944         false,
945         parseResult,
946         ArrayType::STRING);
947     GetValueIfFindKey<std::string>(jsonObject,
948         jsonObjectEnd,
949         MODULE_VIRTUAL_MACHINE,
950         info.virtualMachine,
951         JsonType::STRING,
952         false,
953         parseResult,
954         ArrayType::NOT_ARRAY);
955     GetValueIfFindKey<std::string>(jsonObject,
956         jsonObjectEnd,
957         MODULE_UI_SYNTAX,
958         info.uiSyntax,
959         JsonType::STRING,
960         false,
961         parseResult,
962         ArrayType::NOT_ARRAY);
963     GetValueIfFindKey<std::string>(jsonObject,
964         jsonObjectEnd,
965         MODULE_PAGES,
966         info.pages,
967         JsonType::STRING,
968         false,
969         parseResult,
970         ArrayType::NOT_ARRAY);
971     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
972         jsonObjectEnd,
973         MODULE_META_DATA,
974         info.metadata,
975         JsonType::ARRAY,
976         false,
977         parseResult,
978         ArrayType::OBJECT);
979     GetValueIfFindKey<std::vector<RequestPermission>>(jsonObject,
980         jsonObjectEnd,
981         MODULE_REQUEST_PERMISSIONS,
982         info.requestPermissions,
983         JsonType::ARRAY,
984         false,
985         parseResult,
986         ArrayType::OBJECT);
987     GetValueIfFindKey<std::vector<DefinePermission>>(jsonObject,
988         jsonObjectEnd,
989         MODULE_DEFINE_PERMISSIONS,
990         info.definePermissions,
991         JsonType::ARRAY,
992         false,
993         parseResult,
994         ArrayType::OBJECT);
995     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
996         jsonObjectEnd,
997         MODULE_EXTENSION_KEYS,
998         info.extensionKeys,
999         JsonType::ARRAY,
1000         false,
1001         parseResult,
1002         ArrayType::STRING);
1003     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1004         jsonObjectEnd,
1005         MODULE_EXTENSION_SKILL_KEYS,
1006         info.extensionSkillKeys,
1007         JsonType::ARRAY,
1008         false,
1009         parseResult,
1010         ArrayType::STRING);
1011     GetValueIfFindKey<bool>(jsonObject,
1012         jsonObjectEnd,
1013         MODULE_IS_MODULE_JSON,
1014         info.isModuleJson,
1015         JsonType::BOOLEAN,
1016         false,
1017         parseResult,
1018         ArrayType::NOT_ARRAY);
1019     GetValueIfFindKey<bool>(jsonObject,
1020         jsonObjectEnd,
1021         MODULE_IS_STAGE_BASED_MODEL,
1022         info.isStageBasedModel,
1023         JsonType::BOOLEAN,
1024         false,
1025         parseResult,
1026         ArrayType::NOT_ARRAY);
1027     GetValueIfFindKey<std::vector<Dependency>>(jsonObject,
1028         jsonObjectEnd,
1029         MODULE_DEPENDENCIES,
1030         info.dependencies,
1031         JsonType::ARRAY,
1032         false,
1033         parseResult,
1034         ArrayType::OBJECT);
1035     GetValueIfFindKey<std::string>(jsonObject,
1036         jsonObjectEnd,
1037         MODULE_COMPILE_MODE,
1038         info.compileMode,
1039         JsonType::STRING,
1040         false,
1041         parseResult,
1042         ArrayType::NOT_ARRAY);
1043     GetValueIfFindKey<bool>(jsonObject,
1044         jsonObjectEnd,
1045         MODULE_IS_LIB_ISOLATED,
1046         info.isLibIsolated,
1047         JsonType::BOOLEAN,
1048         false,
1049         parseResult,
1050         ArrayType::NOT_ARRAY);
1051     GetValueIfFindKey<std::string>(jsonObject,
1052         jsonObjectEnd,
1053         MODULE_NATIVE_LIBRARY_PATH,
1054         info.nativeLibraryPath,
1055         JsonType::STRING,
1056         false,
1057         parseResult,
1058         ArrayType::NOT_ARRAY);
1059     GetValueIfFindKey<std::string>(jsonObject,
1060         jsonObjectEnd,
1061         MODULE_CPU_ABI,
1062         info.cpuAbi,
1063         JsonType::STRING,
1064         false,
1065         parseResult,
1066         ArrayType::NOT_ARRAY);
1067     GetValueIfFindKey<std::string>(jsonObject,
1068         jsonObjectEnd,
1069         MODULE_TARGET_MODULE_NAME,
1070         info.targetModuleName,
1071         JsonType::STRING,
1072         false,
1073         parseResult,
1074         ArrayType::NOT_ARRAY);
1075     GetValueIfFindKey<int32_t>(jsonObject,
1076         jsonObjectEnd,
1077         MODULE_TARGET_PRIORITY,
1078         info.targetPriority,
1079         JsonType::NUMBER,
1080         false,
1081         parseResult,
1082         ArrayType::NOT_ARRAY);
1083     GetValueIfFindKey<std::vector<OverlayModuleInfo>>(jsonObject,
1084         jsonObjectEnd,
1085         MODULE_OVERLAY_MODULE_INFO,
1086         info.overlayModuleInfo,
1087         JsonType::ARRAY,
1088         false,
1089         parseResult,
1090         ArrayType::OBJECT);
1091     GetValueIfFindKey<AtomicServiceModuleType>(jsonObject,
1092         jsonObjectEnd,
1093         MODULE_ATOMIC_SERVICE_MODULE_TYPE,
1094         info.atomicServiceModuleType,
1095         JsonType::NUMBER,
1096         false,
1097         parseResult,
1098         ArrayType::NOT_ARRAY);
1099     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1100         jsonObjectEnd,
1101         MODULE_PRELOADS,
1102         info.preloads,
1103         JsonType::ARRAY,
1104         false,
1105         parseResult,
1106         ArrayType::STRING);
1107     GetValueIfFindKey<BundleType>(jsonObject,
1108         jsonObjectEnd,
1109         MODULE_BUNDLE_TYPE,
1110         info.bundleType,
1111         JsonType::NUMBER,
1112         false,
1113         parseResult,
1114         ArrayType::NOT_ARRAY);
1115     GetValueIfFindKey<uint32_t>(jsonObject,
1116         jsonObjectEnd,
1117         MODULE_VERSION_CODE,
1118         info.versionCode,
1119         JsonType::NUMBER,
1120         false,
1121         parseResult,
1122         ArrayType::NOT_ARRAY);
1123     GetValueIfFindKey<std::string>(jsonObject,
1124         jsonObjectEnd,
1125         MODULE_VERSION_NAME,
1126         info.versionName,
1127         JsonType::STRING,
1128         false,
1129         parseResult,
1130         ArrayType::NOT_ARRAY);
1131     GetValueIfFindKey<std::vector<ProxyData>>(jsonObject,
1132         jsonObjectEnd,
1133         MODULE_PROXY_DATAS,
1134         info.proxyDatas,
1135         JsonType::ARRAY,
1136         false,
1137         parseResult,
1138         ArrayType::OBJECT);
1139     GetValueIfFindKey<std::string>(jsonObject,
1140         jsonObjectEnd,
1141         MODULE_BUILD_HASH,
1142         info.buildHash,
1143         JsonType::STRING,
1144         false,
1145         parseResult,
1146         ArrayType::NOT_ARRAY);
1147     GetValueIfFindKey<std::string>(jsonObject,
1148         jsonObjectEnd,
1149         MODULE_ISOLATION_MODE,
1150         info.isolationMode,
1151         JsonType::STRING,
1152         false,
1153         parseResult,
1154         ArrayType::NOT_ARRAY);
1155     GetValueIfFindKey<bool>(jsonObject,
1156         jsonObjectEnd,
1157         MODULE_COMPRESS_NATIVE_LIBS,
1158         info.compressNativeLibs,
1159         JsonType::BOOLEAN,
1160         false,
1161         parseResult,
1162         ArrayType::NOT_ARRAY);
1163     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1164         jsonObjectEnd,
1165         MODULE_NATIVE_LIBRARY_FILE_NAMES,
1166         info.nativeLibraryFileNames,
1167         JsonType::ARRAY,
1168         false,
1169         parseResult,
1170         ArrayType::STRING);
1171     GetValueIfFindKey<AOTCompileStatus>(jsonObject,
1172         jsonObjectEnd,
1173         MODULE_AOT_COMPILE_STATUS,
1174         info.aotCompileStatus,
1175         JsonType::NUMBER,
1176         false,
1177         parseResult,
1178         ArrayType::NOT_ARRAY);
1179     if (parseResult != ERR_OK) {
1180         APP_LOGE("read InnerModuleInfo from database error, error code : %{public}d", parseResult);
1181     }
1182 }
1183 
from_json(const nlohmann::json & jsonObject,SkillUri & uri)1184 void from_json(const nlohmann::json &jsonObject, SkillUri &uri)
1185 {
1186     // these are required fields.
1187     const auto &jsonObjectEnd = jsonObject.end();
1188     int32_t parseResult = ERR_OK;
1189     GetValueIfFindKey<std::string>(jsonObject,
1190         jsonObjectEnd,
1191         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_SCHEME,
1192         uri.scheme,
1193         JsonType::STRING,
1194         false,
1195         parseResult,
1196         ArrayType::NOT_ARRAY);
1197     // these are not required fields.
1198     GetValueIfFindKey<std::string>(jsonObject,
1199         jsonObjectEnd,
1200         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_HOST,
1201         uri.host,
1202         JsonType::STRING,
1203         false,
1204         parseResult,
1205         ArrayType::NOT_ARRAY);
1206     GetValueIfFindKey<std::string>(jsonObject,
1207         jsonObjectEnd,
1208         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PORT,
1209         uri.port,
1210         JsonType::STRING,
1211         false,
1212         parseResult,
1213         ArrayType::NOT_ARRAY);
1214     GetValueIfFindKey<std::string>(jsonObject,
1215         jsonObjectEnd,
1216         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATH,
1217         uri.path,
1218         JsonType::STRING,
1219         false,
1220         parseResult,
1221         ArrayType::NOT_ARRAY);
1222     GetValueIfFindKey<std::string>(jsonObject,
1223         jsonObjectEnd,
1224         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATHSTARTWITH,
1225         uri.pathStartWith,
1226         JsonType::STRING,
1227         false,
1228         parseResult,
1229         ArrayType::NOT_ARRAY);
1230     GetValueIfFindKey<std::string>(jsonObject,
1231         jsonObjectEnd,
1232         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATHREGX,
1233         uri.pathRegex,
1234         JsonType::STRING,
1235         false,
1236         parseResult,
1237         ArrayType::NOT_ARRAY);
1238     GetValueIfFindKey<std::string>(jsonObject,
1239         jsonObjectEnd,
1240         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_PATHREGEX,
1241         uri.pathRegex,
1242         JsonType::STRING,
1243         false,
1244         parseResult,
1245         ArrayType::NOT_ARRAY);
1246     GetValueIfFindKey<std::string>(jsonObject,
1247         jsonObjectEnd,
1248         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_TYPE,
1249         uri.type,
1250         JsonType::STRING,
1251         false,
1252         parseResult,
1253         ArrayType::NOT_ARRAY);
1254     if (parseResult != ERR_OK) {
1255         APP_LOGE("SkillUri from_json error, error code : %{public}d", parseResult);
1256     }
1257 }
1258 
from_json(const nlohmann::json & jsonObject,Skill & skill)1259 void from_json(const nlohmann::json &jsonObject, Skill &skill)
1260 {
1261     // these are not required fields.
1262     const auto &jsonObjectEnd = jsonObject.end();
1263     int32_t parseResult = ERR_OK;
1264     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1265         jsonObjectEnd,
1266         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_ACTIONS,
1267         skill.actions,
1268         JsonType::ARRAY,
1269         false,
1270         parseResult,
1271         ArrayType::STRING);
1272     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1273         jsonObjectEnd,
1274         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_ENTITIES,
1275         skill.entities,
1276         JsonType::ARRAY,
1277         false,
1278         parseResult,
1279         ArrayType::STRING);
1280     GetValueIfFindKey<std::vector<SkillUri>>(jsonObject,
1281         jsonObjectEnd,
1282         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_URIS,
1283         skill.uris,
1284         JsonType::ARRAY,
1285         false,
1286         parseResult,
1287         ArrayType::OBJECT);
1288     if (parseResult != ERR_OK) {
1289         APP_LOGE("Skill from_json error, error code : %{public}d", parseResult);
1290     }
1291 }
1292 
from_json(const nlohmann::json & jsonObject,Distro & distro)1293 void from_json(const nlohmann::json &jsonObject, Distro &distro)
1294 {
1295     const auto &jsonObjectEnd = jsonObject.end();
1296     int32_t parseResult = ERR_OK;
1297     GetValueIfFindKey<bool>(jsonObject,
1298         jsonObjectEnd,
1299         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_DELIVERY_WITH_INSTALL,
1300         distro.deliveryWithInstall,
1301         JsonType::BOOLEAN,
1302         true,
1303         parseResult,
1304         ArrayType::NOT_ARRAY);
1305     GetValueIfFindKey<std::string>(jsonObject,
1306         jsonObjectEnd,
1307         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_NAME,
1308         distro.moduleName,
1309         JsonType::STRING,
1310         true,
1311         parseResult,
1312         ArrayType::NOT_ARRAY);
1313     GetValueIfFindKey<std::string>(jsonObject,
1314         jsonObjectEnd,
1315         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_TYPE,
1316         distro.moduleType,
1317         JsonType::STRING,
1318         true,
1319         parseResult,
1320         ArrayType::NOT_ARRAY);
1321     // mustFlag decide by distro.moduleType
1322     GetValueIfFindKey<bool>(jsonObject,
1323         jsonObjectEnd,
1324         ProfileReader::BUNDLE_MODULE_PROFILE_KEY_MODULE_INSTALLATION_FREE,
1325         distro.installationFree,
1326         JsonType::BOOLEAN,
1327         false,
1328         parseResult,
1329         ArrayType::NOT_ARRAY);
1330     if (parseResult != ERR_OK) {
1331         APP_LOGE("Distro from_json error, error code : %{public}d", parseResult);
1332     }
1333 }
1334 
from_json(const nlohmann::json & jsonObject,InstallMark & installMark)1335 void from_json(const nlohmann::json &jsonObject, InstallMark &installMark)
1336 {
1337     const auto &jsonObjectEnd = jsonObject.end();
1338     int32_t parseResult = ERR_OK;
1339     GetValueIfFindKey<std::string>(jsonObject,
1340         jsonObjectEnd,
1341         ProfileReader::BUNDLE_INSTALL_MARK_BUNDLE,
1342         installMark.bundleName,
1343         JsonType::STRING,
1344         false,
1345         parseResult,
1346         ArrayType::NOT_ARRAY);
1347     GetValueIfFindKey<std::string>(jsonObject,
1348         jsonObjectEnd,
1349         ProfileReader::BUNDLE_INSTALL_MARK_PACKAGE,
1350         installMark.packageName,
1351         JsonType::STRING,
1352         false,
1353         parseResult,
1354         ArrayType::NOT_ARRAY);
1355     GetValueIfFindKey<int32_t>(jsonObject,
1356         jsonObjectEnd,
1357         ProfileReader::BUNDLE_INSTALL_MARK_STATUS,
1358         installMark.status,
1359         JsonType::NUMBER,
1360         false,
1361         parseResult,
1362         ArrayType::NOT_ARRAY);
1363     if (parseResult != ERR_OK) {
1364         APP_LOGE("InstallMark from_json error, error code : %{public}d", parseResult);
1365     }
1366 }
1367 
from_json(const nlohmann::json & jsonObject,DefinePermission & definePermission)1368 void from_json(const nlohmann::json &jsonObject, DefinePermission &definePermission)
1369 {
1370     const auto &jsonObjectEnd = jsonObject.end();
1371     int32_t parseResult = ERR_OK;
1372     GetValueIfFindKey<std::string>(jsonObject,
1373         jsonObjectEnd,
1374         Profile::DEFINEPERMISSION_NAME,
1375         definePermission.name,
1376         JsonType::STRING,
1377         false,
1378         parseResult,
1379         ArrayType::NOT_ARRAY);
1380     GetValueIfFindKey<std::string>(jsonObject,
1381         jsonObjectEnd,
1382         Profile::DEFINEPERMISSION_GRANT_MODE,
1383         definePermission.grantMode,
1384         JsonType::STRING,
1385         false,
1386         parseResult,
1387         ArrayType::NOT_ARRAY);
1388     GetValueIfFindKey<std::string>(jsonObject,
1389         jsonObjectEnd,
1390         Profile::DEFINEPERMISSION_AVAILABLE_LEVEL,
1391         definePermission.availableLevel,
1392         JsonType::STRING,
1393         false,
1394         parseResult,
1395         ArrayType::NOT_ARRAY);
1396     GetValueIfFindKey<bool>(jsonObject,
1397         jsonObjectEnd,
1398         Profile::DEFINEPERMISSION_PROVISION_ENABLE,
1399         definePermission.provisionEnable,
1400         JsonType::BOOLEAN,
1401         false,
1402         parseResult,
1403         ArrayType::NOT_ARRAY);
1404     GetValueIfFindKey<bool>(jsonObject,
1405         jsonObjectEnd,
1406         Profile::DEFINEPERMISSION_DISTRIBUTED_SCENE_ENABLE,
1407         definePermission.distributedSceneEnable,
1408         JsonType::BOOLEAN,
1409         false,
1410         parseResult,
1411         ArrayType::NOT_ARRAY);
1412     GetValueIfFindKey<std::string>(jsonObject,
1413         jsonObjectEnd,
1414         Profile::LABEL,
1415         definePermission.label,
1416         JsonType::STRING,
1417         false,
1418         parseResult,
1419         ArrayType::NOT_ARRAY);
1420     GetValueIfFindKey<int32_t>(jsonObject,
1421         jsonObjectEnd,
1422         Profile::LABEL_ID,
1423         definePermission.labelId,
1424         JsonType::NUMBER,
1425         false,
1426         parseResult,
1427         ArrayType::NOT_ARRAY);
1428     GetValueIfFindKey<std::string>(jsonObject,
1429         jsonObjectEnd,
1430         Profile::DESCRIPTION,
1431         definePermission.description,
1432         JsonType::STRING,
1433         false,
1434         parseResult,
1435         ArrayType::NOT_ARRAY);
1436     GetValueIfFindKey<int32_t>(jsonObject,
1437         jsonObjectEnd,
1438         Profile::DESCRIPTION_ID,
1439         definePermission.descriptionId,
1440         JsonType::NUMBER,
1441         false,
1442         parseResult,
1443         ArrayType::NOT_ARRAY);
1444     if (parseResult != ERR_OK) {
1445         APP_LOGE("DefinePermission from_json error, error code : %{public}d", parseResult);
1446     }
1447 }
1448 
from_json(const nlohmann::json & jsonObject,Dependency & dependency)1449 void from_json(const nlohmann::json &jsonObject, Dependency &dependency)
1450 {
1451     const auto &jsonObjectEnd = jsonObject.end();
1452     int32_t parseResult = ERR_OK;
1453     GetValueIfFindKey<std::string>(jsonObject,
1454         jsonObjectEnd,
1455         Profile::DEPENDENCIES_MODULE_NAME,
1456         dependency.moduleName,
1457         JsonType::STRING,
1458         false,
1459         parseResult,
1460         ArrayType::NOT_ARRAY);
1461     GetValueIfFindKey<std::string>(jsonObject,
1462         jsonObjectEnd,
1463         Profile::DEPENDENCIES_BUNDLE_NAME,
1464         dependency.bundleName,
1465         JsonType::STRING,
1466         false,
1467         parseResult,
1468         ArrayType::NOT_ARRAY);
1469     GetValueIfFindKey<int>(jsonObject,
1470         jsonObjectEnd,
1471         Profile::APP_VERSION_CODE,
1472         dependency.versionCode,
1473         JsonType::NUMBER,
1474         false,
1475         parseResult,
1476         ArrayType::NOT_ARRAY);
1477     if (parseResult != ERR_OK) {
1478         APP_LOGE("Dependency from_json error, error code : %{public}d", parseResult);
1479     }
1480 }
1481 
FromJson(const nlohmann::json & jsonObject)1482 int32_t InnerBundleInfo::FromJson(const nlohmann::json &jsonObject)
1483 {
1484     const auto &jsonObjectEnd = jsonObject.end();
1485     int32_t parseResult = ERR_OK;
1486     GetValueIfFindKey<Constants::AppType>(jsonObject,
1487         jsonObjectEnd,
1488         APP_TYPE,
1489         appType_,
1490         JsonType::NUMBER,
1491         true,
1492         parseResult,
1493         ArrayType::NOT_ARRAY);
1494     GetValueIfFindKey<int>(jsonObject,
1495         jsonObjectEnd,
1496         UID,
1497         uid_,
1498         JsonType::NUMBER,
1499         false,
1500         parseResult,
1501         ArrayType::NOT_ARRAY);
1502     GetValueIfFindKey<int>(jsonObject,
1503         jsonObjectEnd,
1504         GID,
1505         gid_,
1506         JsonType::NUMBER,
1507         false,
1508         parseResult,
1509         ArrayType::NOT_ARRAY);
1510     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1511         jsonObjectEnd,
1512         ALLOWED_ACLS,
1513         allowedAcls_,
1514         JsonType::ARRAY,
1515         false,
1516         parseResult,
1517         ArrayType::STRING);
1518     GetValueIfFindKey<BundleStatus>(jsonObject,
1519         jsonObjectEnd,
1520         BUNDLE_STATUS,
1521         bundleStatus_,
1522         JsonType::NUMBER,
1523         true,
1524         parseResult,
1525         ArrayType::NOT_ARRAY);
1526     GetValueIfFindKey<BundleInfo>(jsonObject,
1527         jsonObjectEnd,
1528         BASE_BUNDLE_INFO,
1529         *baseBundleInfo_,
1530         JsonType::OBJECT,
1531         true,
1532         parseResult,
1533         ArrayType::NOT_ARRAY);
1534     GetValueIfFindKey<ApplicationInfo>(jsonObject,
1535         jsonObjectEnd,
1536         BASE_APPLICATION_INFO,
1537         *baseApplicationInfo_,
1538         JsonType::OBJECT,
1539         true,
1540         parseResult,
1541         ArrayType::NOT_ARRAY);
1542     GetValueIfFindKey<std::map<std::string, AbilityInfo>>(jsonObject,
1543         jsonObjectEnd,
1544         BASE_ABILITY_INFO,
1545         baseAbilityInfos_,
1546         JsonType::OBJECT,
1547         true,
1548         parseResult,
1549         ArrayType::NOT_ARRAY);
1550     GetValueIfFindKey<std::map<std::string, InnerModuleInfo>>(jsonObject,
1551         jsonObjectEnd,
1552         INNER_MODULE_INFO,
1553         innerModuleInfos_,
1554         JsonType::OBJECT,
1555         true,
1556         parseResult,
1557         ArrayType::NOT_ARRAY);
1558     GetValueIfFindKey<std::map<std::string, std::vector<InnerModuleInfo>>>(jsonObject,
1559         jsonObjectEnd,
1560         INNER_SHARED_MODULE_INFO,
1561         innerSharedModuleInfos_,
1562         JsonType::OBJECT,
1563         false,
1564         parseResult,
1565         ArrayType::NOT_ARRAY);
1566     GetValueIfFindKey<std::map<std::string, std::vector<Skill>>>(jsonObject,
1567         jsonObjectEnd,
1568         SKILL_INFOS,
1569         skillInfos_,
1570         JsonType::OBJECT,
1571         true,
1572         parseResult,
1573         ArrayType::NOT_ARRAY);
1574     GetValueIfFindKey<int>(jsonObject,
1575         jsonObjectEnd,
1576         USER_ID,
1577         userId_,
1578         JsonType::NUMBER,
1579         true,
1580         parseResult,
1581         ArrayType::NOT_ARRAY);
1582     GetValueIfFindKey<std::string>(jsonObject,
1583         jsonObjectEnd,
1584         APP_FEATURE,
1585         appFeature_,
1586         JsonType::STRING,
1587         true,
1588         parseResult,
1589         ArrayType::NOT_ARRAY);
1590     GetValueIfFindKey<std::map<std::string, std::vector<FormInfo>>>(jsonObject,
1591         jsonObjectEnd,
1592         MODULE_FORMS,
1593         formInfos_,
1594         JsonType::OBJECT,
1595         true,
1596         parseResult,
1597         ArrayType::NOT_ARRAY);
1598     GetValueIfFindKey<std::map<std::string, ShortcutInfo>>(jsonObject,
1599         jsonObjectEnd,
1600         MODULE_SHORTCUT,
1601         shortcutInfos_,
1602         JsonType::OBJECT,
1603         true,
1604         parseResult,
1605         ArrayType::NOT_ARRAY);
1606     GetValueIfFindKey<std::map<std::string, CommonEventInfo>>(jsonObject,
1607         jsonObjectEnd,
1608         MODULE_COMMON_EVENT,
1609         commonEvents_,
1610         JsonType::OBJECT,
1611         true,
1612         parseResult,
1613         ArrayType::NOT_ARRAY);
1614     GetValueIfFindKey<InstallMark>(jsonObject,
1615         jsonObjectEnd,
1616         INSTALL_MARK,
1617         mark_,
1618         JsonType::OBJECT,
1619         false,
1620         parseResult,
1621         ArrayType::NOT_ARRAY);
1622     int32_t isOldVersion = ERR_OK;
1623     GetValueIfFindKey<std::map<std::string, InnerBundleUserInfo>>(jsonObject,
1624         jsonObjectEnd,
1625         INNER_BUNDLE_USER_INFOS,
1626         innerBundleUserInfos_,
1627         JsonType::OBJECT,
1628         true,
1629         isOldVersion,
1630         ArrayType::NOT_ARRAY);
1631     if (parseResult == ERR_OK && isOldVersion == ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP) {
1632         // To be compatible with the old database,
1633         // if the old data does not have bundleUserInfos,
1634         // the default user information needs to be constructed.
1635         BuildDefaultUserInfo();
1636     }
1637     GetValueIfFindKey<bool>(jsonObject,
1638         jsonObjectEnd,
1639         BUNDLE_IS_NEW_VERSION,
1640         isNewVersion_,
1641         JsonType::BOOLEAN,
1642         false,
1643         parseResult,
1644         ArrayType::NOT_ARRAY);
1645     GetValueIfFindKey<std::map<std::string, ExtensionAbilityInfo>>(jsonObject,
1646         jsonObjectEnd,
1647         BUNDLE_BASE_EXTENSION_INFOS,
1648         baseExtensionInfos_,
1649         JsonType::OBJECT,
1650         false,
1651         parseResult,
1652         ArrayType::NOT_ARRAY);
1653     GetValueIfFindKey<std::map<std::string, std::vector<Skill>>>(jsonObject,
1654         jsonObjectEnd,
1655         BUNDLE_EXTENSION_SKILL_INFOS,
1656         extensionSkillInfos_,
1657         JsonType::OBJECT,
1658         false,
1659         parseResult,
1660         ArrayType::NOT_ARRAY);
1661     GetValueIfFindKey<BundlePackInfo>(jsonObject,
1662         jsonObjectEnd,
1663         BUNDLE_PACK_INFO,
1664         *bundlePackInfo_,
1665         JsonType::OBJECT,
1666         false,
1667         parseResult,
1668         ArrayType::NOT_ARRAY);
1669     GetValueIfFindKey<int>(jsonObject,
1670         jsonObjectEnd,
1671         APP_INDEX,
1672         appIndex_,
1673         JsonType::NUMBER,
1674         false,
1675         parseResult,
1676         ArrayType::NOT_ARRAY);
1677     GetValueIfFindKey<bool>(jsonObject,
1678         jsonObjectEnd,
1679         BUNDLE_IS_SANDBOX_APP,
1680         isSandboxApp_,
1681         JsonType::BOOLEAN,
1682         false,
1683         parseResult,
1684         ArrayType::NOT_ARRAY);
1685     GetValueIfFindKey<std::vector<HqfInfo>>(jsonObject,
1686         jsonObjectEnd,
1687         BUNDLE_HQF_INFOS,
1688         hqfInfos_,
1689         JsonType::ARRAY,
1690         false,
1691         parseResult,
1692         ArrayType::OBJECT);
1693     GetValueIfFindKey<std::vector<OverlayBundleInfo>>(jsonObject,
1694         jsonObjectEnd,
1695         OVERLAY_BUNDLE_INFO,
1696         overlayBundleInfo_,
1697         JsonType::ARRAY,
1698         false,
1699         parseResult,
1700         ArrayType::OBJECT);
1701     GetValueIfFindKey<int32_t>(jsonObject,
1702         jsonObjectEnd,
1703         OVERLAY_TYPE,
1704         overlayType_,
1705         JsonType::NUMBER,
1706         false,
1707         parseResult,
1708         ArrayType::NOT_ARRAY);
1709     GetValueIfFindKey<int32_t>(jsonObject,
1710         jsonObjectEnd,
1711         APPLY_QUICK_FIX_FREQUENCY,
1712         applyQuickFixFrequency_,
1713         JsonType::NUMBER,
1714         false,
1715         parseResult,
1716         ArrayType::NOT_ARRAY);
1717     GetValueIfFindKey<std::unordered_map<std::string, std::vector<DataGroupInfo>>>(jsonObject,
1718         jsonObjectEnd,
1719         DATA_GROUP_INFOS,
1720         dataGroupInfos_,
1721         JsonType::OBJECT,
1722         false,
1723         parseResult,
1724         ArrayType::NOT_ARRAY);
1725     if (parseResult != ERR_OK) {
1726         APP_LOGE("read InnerBundleInfo from database error, error code : %{public}d", parseResult);
1727     }
1728     return parseResult;
1729 }
1730 
BuildDefaultUserInfo()1731 void InnerBundleInfo::BuildDefaultUserInfo()
1732 {
1733     APP_LOGD("BuildDefaultUserInfo: bundleName: %{public}s.",
1734         baseApplicationInfo_->bundleName.c_str());
1735     InnerBundleUserInfo defaultInnerBundleUserInfo;
1736     defaultInnerBundleUserInfo.bundleUserInfo.userId = GetUserId();
1737     defaultInnerBundleUserInfo.uid = uid_;
1738     defaultInnerBundleUserInfo.gids.emplace_back(gid_);
1739     defaultInnerBundleUserInfo.installTime = baseBundleInfo_->installTime;
1740     defaultInnerBundleUserInfo.updateTime = baseBundleInfo_->updateTime;
1741     defaultInnerBundleUserInfo.bundleName = baseApplicationInfo_->bundleName;
1742     defaultInnerBundleUserInfo.bundleUserInfo.enabled = baseApplicationInfo_->enabled;
1743     AddInnerBundleUserInfo(defaultInnerBundleUserInfo);
1744 }
1745 
FindHapModuleInfo(const std::string & modulePackage,int32_t userId) const1746 std::optional<HapModuleInfo> InnerBundleInfo::FindHapModuleInfo(const std::string &modulePackage, int32_t userId) const
1747 {
1748     auto it = innerModuleInfos_.find(modulePackage);
1749     if (it == innerModuleInfos_.end()) {
1750         APP_LOGE("can not find module %{public}s", modulePackage.c_str());
1751         return std::nullopt;
1752     }
1753     HapModuleInfo hapInfo;
1754     hapInfo.name = it->second.name;
1755     hapInfo.package = it->second.modulePackage;
1756     hapInfo.moduleName = it->second.moduleName;
1757     hapInfo.description = it->second.description;
1758     hapInfo.descriptionId = it->second.descriptionId;
1759     hapInfo.label = it->second.label;
1760     hapInfo.labelId = it->second.labelId;
1761     hapInfo.iconPath = it->second.icon;
1762     hapInfo.iconId = it->second.iconId;
1763     hapInfo.mainAbility = it->second.mainAbility;
1764     hapInfo.srcPath = it->second.srcPath;
1765     hapInfo.hapPath = it->second.hapPath;
1766     hapInfo.supportedModes = baseApplicationInfo_->supportedModes;
1767     hapInfo.reqCapabilities = it->second.reqCapabilities;
1768     hapInfo.colorMode = it->second.colorMode;
1769     hapInfo.isRemovable = it->second.isRemovable;
1770     hapInfo.upgradeFlag = it->second.upgradeFlag;
1771     hapInfo.isLibIsolated = it->second.isLibIsolated;
1772     hapInfo.nativeLibraryPath = it->second.nativeLibraryPath;
1773     hapInfo.cpuAbi = it->second.cpuAbi;
1774 
1775     hapInfo.bundleName = baseApplicationInfo_->bundleName;
1776     hapInfo.mainElementName = it->second.mainAbility;
1777     hapInfo.pages = it->second.pages;
1778     hapInfo.process = it->second.process;
1779     hapInfo.resourcePath = it->second.moduleResPath;
1780     hapInfo.srcEntrance = it->second.srcEntrance;
1781     hapInfo.uiSyntax = it->second.uiSyntax;
1782     hapInfo.virtualMachine = it->second.virtualMachine;
1783     hapInfo.deliveryWithInstall = it->second.distro.deliveryWithInstall;
1784     hapInfo.installationFree = it->second.distro.installationFree;
1785     hapInfo.isModuleJson = it->second.isModuleJson;
1786     hapInfo.isStageBasedModel = it->second.isStageBasedModel;
1787     std::string moduleType = it->second.distro.moduleType;
1788     if (moduleType == Profile::MODULE_TYPE_ENTRY) {
1789         hapInfo.moduleType = ModuleType::ENTRY;
1790     } else if (moduleType == Profile::MODULE_TYPE_FEATURE) {
1791         hapInfo.moduleType = ModuleType::FEATURE;
1792     } else if (moduleType == Profile::MODULE_TYPE_SHARED) {
1793         hapInfo.moduleType = ModuleType::SHARED;
1794     } else {
1795         hapInfo.moduleType = ModuleType::UNKNOWN;
1796     }
1797     std::string key;
1798     key.append(".").append(modulePackage).append(".");
1799     for (const auto &extension : baseExtensionInfos_) {
1800         if (extension.first.find(key) != std::string::npos) {
1801             hapInfo.extensionInfos.emplace_back(extension.second);
1802         }
1803     }
1804     hapInfo.metadata = it->second.metadata;
1805     bool first = false;
1806     for (auto &ability : baseAbilityInfos_) {
1807         if (ability.second.name == Constants::APP_DETAIL_ABILITY) {
1808             continue;
1809         }
1810         if (ability.first.find(key) != std::string::npos) {
1811             if (!first) {
1812                 hapInfo.deviceTypes = ability.second.deviceTypes;
1813                 first = true;
1814             }
1815             auto &abilityInfo = hapInfo.abilityInfos.emplace_back(ability.second);
1816             GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION |
1817                 ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
1818                 abilityInfo.applicationInfo);
1819         }
1820     }
1821     hapInfo.dependencies = it->second.dependencies;
1822     hapInfo.compileMode = ConvertCompileMode(it->second.compileMode);
1823     for (const auto &hqf : hqfInfos_) {
1824         if (hqf.moduleName == it->second.moduleName) {
1825             hapInfo.hqfInfo = hqf;
1826             break;
1827         }
1828     }
1829     hapInfo.atomicServiceModuleType = it->second.atomicServiceModuleType;
1830     for (const auto &item : it->second.preloads) {
1831         PreloadItem preload(item);
1832         hapInfo.preloads.emplace_back(preload);
1833     }
1834     for (const auto &item : it->second.proxyDatas) {
1835         ProxyData proxyData(item);
1836         hapInfo.proxyDatas.emplace_back(proxyData);
1837     }
1838     hapInfo.buildHash = it->second.buildHash;
1839     hapInfo.isolationMode = GetIsolationMode(it->second.isolationMode);
1840     hapInfo.compressNativeLibs = it->second.compressNativeLibs;
1841     hapInfo.nativeLibraryFileNames = it->second.nativeLibraryFileNames;
1842     hapInfo.aotCompileStatus = it->second.aotCompileStatus;
1843     return hapInfo;
1844 }
1845 
FindAbilityInfo(const std::string & moduleName,const std::string & abilityName,int32_t userId) const1846 std::optional<AbilityInfo> InnerBundleInfo::FindAbilityInfo(
1847     const std::string &moduleName,
1848     const std::string &abilityName,
1849     int32_t userId) const
1850 {
1851     for (const auto &ability : baseAbilityInfos_) {
1852         auto abilityInfo = ability.second;
1853         if ((abilityInfo.name == abilityName) &&
1854             (moduleName.empty() || (abilityInfo.moduleName == moduleName))) {
1855             GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION |
1856                 ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
1857                 abilityInfo.applicationInfo);
1858             return abilityInfo;
1859         }
1860     }
1861 
1862     return std::nullopt;
1863 }
1864 
FindAbilityInfoV9(const std::string & moduleName,const std::string & abilityName) const1865 std::optional<AbilityInfo> InnerBundleInfo::FindAbilityInfoV9(
1866     const std::string &moduleName, const std::string &abilityName) const
1867 {
1868     for (const auto &ability : baseAbilityInfos_) {
1869         auto abilityInfo = ability.second;
1870         if ((abilityInfo.name == abilityName) &&
1871             (moduleName.empty() || (abilityInfo.moduleName == moduleName))) {
1872             return abilityInfo;
1873         }
1874     }
1875     APP_LOGE("bundleName: %{public}s not find moduleName:%{public}s, abilityName:%{public}s",
1876         GetBundleName().c_str(), moduleName.c_str(), abilityName.c_str());
1877     return std::nullopt;
1878 }
1879 
FindAbilityInfo(const std::string & moduleName,const std::string & abilityName,AbilityInfo & info) const1880 ErrCode InnerBundleInfo::FindAbilityInfo(
1881     const std::string &moduleName, const std::string &abilityName, AbilityInfo &info) const
1882 {
1883     bool isModuleFind = false;
1884     for (const auto &ability : baseAbilityInfos_) {
1885         auto abilityInfo = ability.second;
1886         if ((abilityInfo.moduleName == moduleName)) {
1887             isModuleFind = true;
1888             if (abilityInfo.name == abilityName) {
1889                 info = abilityInfo;
1890                 return ERR_OK;
1891             }
1892         }
1893     }
1894     APP_LOGE("bundleName: %{public}s not find moduleName:%{public}s, abilityName:%{public}s, isModuleFind:%{public}d",
1895         GetBundleName().c_str(), moduleName.c_str(), abilityName.c_str(), isModuleFind);
1896     if (isModuleFind) {
1897         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
1898     } else {
1899         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
1900     }
1901 }
1902 
FindAbilityInfos(int32_t userId) const1903 std::optional<std::vector<AbilityInfo>> InnerBundleInfo::FindAbilityInfos(int32_t userId) const
1904 {
1905     if (!HasInnerBundleUserInfo(userId)) {
1906         return std::nullopt;
1907     }
1908 
1909     std::vector<AbilityInfo> abilitys;
1910     for (const auto &ability : baseAbilityInfos_) {
1911         if (ability.second.name == Constants::APP_DETAIL_ABILITY) {
1912             continue;
1913         }
1914         auto abilityInfo = ability.second;
1915         GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION |
1916             ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
1917             abilityInfo.applicationInfo);
1918         abilitys.emplace_back(abilityInfo);
1919     }
1920 
1921     if (abilitys.empty()) {
1922         return std::nullopt;
1923     }
1924     return abilitys;
1925 }
1926 
FindExtensionInfo(const std::string & moduleName,const std::string & extensionName) const1927 std::optional<ExtensionAbilityInfo> InnerBundleInfo::FindExtensionInfo(
1928     const std::string &moduleName, const std::string &extensionName) const
1929 {
1930     for (const auto &extension : baseExtensionInfos_) {
1931         if ((extension.second.name == extensionName) &&
1932             (moduleName.empty() || (extension.second.moduleName == moduleName))) {
1933             return extension.second;
1934         }
1935     }
1936 
1937     return std::nullopt;
1938 }
1939 
FindExtensionInfos() const1940 std::optional<std::vector<ExtensionAbilityInfo>> InnerBundleInfo::FindExtensionInfos() const
1941 {
1942     std::vector<ExtensionAbilityInfo> extensions;
1943     for (const auto &extension : baseExtensionInfos_) {
1944         extensions.emplace_back(extension.second);
1945     }
1946 
1947     if (extensions.empty()) {
1948         return std::nullopt;
1949     }
1950 
1951     return extensions;
1952 }
1953 
AddModuleInfo(const InnerBundleInfo & newInfo)1954 bool InnerBundleInfo::AddModuleInfo(const InnerBundleInfo &newInfo)
1955 {
1956     if (newInfo.currentPackage_.empty()) {
1957         APP_LOGE("current package is empty");
1958         return false;
1959     }
1960     if (FindModule(newInfo.currentPackage_)) {
1961         APP_LOGE("current package %{public}s is exist", currentPackage_.c_str());
1962         return false;
1963     }
1964     AddInnerModuleInfo(newInfo.innerModuleInfos_);
1965     AddModuleAbilityInfo(newInfo.baseAbilityInfos_);
1966     AddModuleSkillInfo(newInfo.skillInfos_);
1967     AddModuleExtensionInfos(newInfo.baseExtensionInfos_);
1968     AddModuleExtensionSkillInfos(newInfo.extensionSkillInfos_);
1969     AddModuleFormInfo(newInfo.formInfos_);
1970     AddModuleShortcutInfo(newInfo.shortcutInfos_);
1971     AddModuleCommonEvent(newInfo.commonEvents_);
1972     UpdateIsCompressNativeLibs();
1973     return true;
1974 }
1975 
UpdateBaseBundleInfo(const BundleInfo & bundleInfo,bool isEntry)1976 void InnerBundleInfo::UpdateBaseBundleInfo(const BundleInfo &bundleInfo, bool isEntry)
1977 {
1978     baseBundleInfo_->name = bundleInfo.name;
1979 
1980     baseBundleInfo_->versionCode = bundleInfo.versionCode;
1981     baseBundleInfo_->versionName = bundleInfo.versionName;
1982     baseBundleInfo_->minCompatibleVersionCode = bundleInfo.minCompatibleVersionCode;
1983 
1984     baseBundleInfo_->compatibleVersion = bundleInfo.compatibleVersion;
1985     baseBundleInfo_->targetVersion = bundleInfo.targetVersion;
1986 
1987     baseBundleInfo_->isKeepAlive = bundleInfo.isKeepAlive;
1988     baseBundleInfo_->singleton = bundleInfo.singleton;
1989     if (!baseBundleInfo_->isPreInstallApp) {
1990         baseBundleInfo_->isPreInstallApp = bundleInfo.isPreInstallApp;
1991     }
1992 
1993     baseBundleInfo_->vendor = bundleInfo.vendor;
1994     baseBundleInfo_->releaseType = bundleInfo.releaseType;
1995     if (!baseBundleInfo_->isNativeApp) {
1996         baseBundleInfo_->isNativeApp = bundleInfo.isNativeApp;
1997     }
1998 
1999     if (isEntry) {
2000         baseBundleInfo_->mainEntry = bundleInfo.mainEntry;
2001         baseBundleInfo_->entryModuleName = bundleInfo.entryModuleName;
2002     }
2003 }
2004 
UpdateBaseApplicationInfo(const ApplicationInfo & applicationInfo)2005 void InnerBundleInfo::UpdateBaseApplicationInfo(const ApplicationInfo &applicationInfo)
2006 {
2007     baseApplicationInfo_->name = applicationInfo.name;
2008     baseApplicationInfo_->bundleName = applicationInfo.bundleName;
2009 
2010     baseApplicationInfo_->versionCode = applicationInfo.versionCode;
2011     baseApplicationInfo_->versionName = applicationInfo.versionName;
2012     baseApplicationInfo_->minCompatibleVersionCode = applicationInfo.minCompatibleVersionCode;
2013 
2014     baseApplicationInfo_->apiCompatibleVersion = applicationInfo.apiCompatibleVersion;
2015     baseApplicationInfo_->apiTargetVersion = applicationInfo.apiTargetVersion;
2016 
2017     baseApplicationInfo_->iconPath = applicationInfo.iconPath;
2018     baseApplicationInfo_->iconId = applicationInfo.iconId;
2019     baseApplicationInfo_->label = applicationInfo.label;
2020     baseApplicationInfo_->labelId = applicationInfo.labelId;
2021     baseApplicationInfo_->description = applicationInfo.description;
2022     baseApplicationInfo_->descriptionId = applicationInfo.descriptionId;
2023     baseApplicationInfo_->iconResource = applicationInfo.iconResource;
2024     baseApplicationInfo_->labelResource = applicationInfo.labelResource;
2025     baseApplicationInfo_->descriptionResource = applicationInfo.descriptionResource;
2026     baseApplicationInfo_->singleton = applicationInfo.singleton;
2027     baseApplicationInfo_->userDataClearable = applicationInfo.userDataClearable;
2028     baseApplicationInfo_->accessible = applicationInfo.accessible;
2029 
2030     if (!baseApplicationInfo_->isSystemApp) {
2031         baseApplicationInfo_->isSystemApp = applicationInfo.isSystemApp;
2032     }
2033     if (!baseApplicationInfo_->isLauncherApp) {
2034         baseApplicationInfo_->isLauncherApp = applicationInfo.isLauncherApp;
2035     }
2036 
2037     baseApplicationInfo_->apiReleaseType = applicationInfo.apiReleaseType;
2038     baseApplicationInfo_->debug = applicationInfo.debug;
2039     baseApplicationInfo_->deviceId = applicationInfo.deviceId;
2040     baseApplicationInfo_->distributedNotificationEnabled = applicationInfo.distributedNotificationEnabled;
2041     baseApplicationInfo_->entityType = applicationInfo.entityType;
2042     baseApplicationInfo_->process = applicationInfo.process;
2043     baseApplicationInfo_->supportedModes = applicationInfo.supportedModes;
2044     baseApplicationInfo_->vendor = applicationInfo.vendor;
2045     baseApplicationInfo_->appDistributionType = applicationInfo.appDistributionType;
2046     baseApplicationInfo_->appProvisionType = applicationInfo.appProvisionType;
2047     baseApplicationInfo_->formVisibleNotify = applicationInfo.formVisibleNotify;
2048     baseApplicationInfo_->needAppDetail = applicationInfo.needAppDetail;
2049     baseApplicationInfo_->appDetailAbilityLibraryPath = applicationInfo.appDetailAbilityLibraryPath;
2050     UpdatePrivilegeCapability(applicationInfo);
2051     SetHideDesktopIcon(applicationInfo.hideDesktopIcon);
2052 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
2053     baseApplicationInfo_->targetBundleName = applicationInfo.targetBundleName;
2054     baseApplicationInfo_->targetPriority = applicationInfo.targetPriority;
2055 #endif
2056 }
2057 
UpdateAppDetailAbilityAttrs()2058 void InnerBundleInfo::UpdateAppDetailAbilityAttrs()
2059 {
2060     if (IsExistLauncherAbility()) {
2061         baseApplicationInfo_->needAppDetail = false;
2062         baseApplicationInfo_->appDetailAbilityLibraryPath = Constants::EMPTY_STRING;
2063     }
2064     for (auto iter = baseAbilityInfos_.begin(); iter != baseAbilityInfos_.end(); ++iter) {
2065         if (iter->second.name == Constants::APP_DETAIL_ABILITY) {
2066             if (!baseApplicationInfo_->needAppDetail) {
2067                 baseAbilityInfos_.erase(iter);
2068                 return;
2069             }
2070             if (isNewVersion_) {
2071                 iter->second.labelId = baseApplicationInfo_->labelId;
2072                 iter->second.iconId =
2073                     (baseApplicationInfo_->iconId == 0) ? iter->second.iconId : baseApplicationInfo_->iconId;
2074             }
2075             return;
2076         }
2077     }
2078 }
2079 
IsHideDesktopIcon() const2080 bool InnerBundleInfo::IsHideDesktopIcon() const
2081 {
2082     return baseApplicationInfo_->hideDesktopIcon ? true : !IsExistLauncherAbility();
2083 }
2084 
IsExistLauncherAbility() const2085 bool InnerBundleInfo::IsExistLauncherAbility() const
2086 {
2087     bool isExistLauncherAbility = false;
2088     OHOS::AAFwk::Want want;
2089     want.SetAction(OHOS::AAFwk::Want::ACTION_HOME);
2090     want.AddEntity(OHOS::AAFwk::Want::ENTITY_HOME);
2091     for (const auto& abilityInfoPair : baseAbilityInfos_) {
2092         auto skillsPair = skillInfos_.find(abilityInfoPair.first);
2093         if (skillsPair == skillInfos_.end()) {
2094             continue;
2095         }
2096         for (const Skill& skill : skillsPair->second) {
2097             if (skill.MatchLauncher(want) && (abilityInfoPair.second.type == AbilityType::PAGE)) {
2098                 isExistLauncherAbility = true;
2099                 break;
2100             }
2101         }
2102     }
2103     return isExistLauncherAbility;
2104 }
2105 
UpdateNativeLibAttrs(const ApplicationInfo & applicationInfo)2106 void InnerBundleInfo::UpdateNativeLibAttrs(const ApplicationInfo &applicationInfo)
2107 {
2108     baseApplicationInfo_->cpuAbi = applicationInfo.cpuAbi;
2109     baseApplicationInfo_->nativeLibraryPath = applicationInfo.nativeLibraryPath;
2110 }
2111 
UpdateArkNativeAttrs(const ApplicationInfo & applicationInfo)2112 void InnerBundleInfo::UpdateArkNativeAttrs(const ApplicationInfo &applicationInfo)
2113 {
2114     baseApplicationInfo_->arkNativeFileAbi = applicationInfo.arkNativeFileAbi;
2115     baseApplicationInfo_->arkNativeFilePath = applicationInfo.arkNativeFilePath;
2116 }
2117 
UpdatePrivilegeCapability(const ApplicationInfo & applicationInfo)2118 void InnerBundleInfo::UpdatePrivilegeCapability(const ApplicationInfo &applicationInfo)
2119 {
2120     SetKeepAlive(applicationInfo.keepAlive);
2121     baseApplicationInfo_->runningResourcesApply = applicationInfo.runningResourcesApply;
2122     baseApplicationInfo_->associatedWakeUp = applicationInfo.associatedWakeUp;
2123     SetAllowCommonEvent(applicationInfo.allowCommonEvent);
2124     baseApplicationInfo_->resourcesApply = applicationInfo.resourcesApply;
2125 }
2126 
UpdateRemovable(bool isPreInstall,bool removable)2127 void InnerBundleInfo::UpdateRemovable(bool isPreInstall, bool removable)
2128 {
2129 #ifdef USE_PRE_BUNDLE_PROFILE
2130     if (!isPreInstall) {
2131         return;
2132     }
2133 #endif
2134 
2135     baseApplicationInfo_->removable = removable;
2136 }
2137 
UpdateModuleInfo(const InnerBundleInfo & newInfo)2138 void InnerBundleInfo::UpdateModuleInfo(const InnerBundleInfo &newInfo)
2139 {
2140     if (newInfo.currentPackage_.empty()) {
2141         APP_LOGE("no package in new info");
2142         return;
2143     }
2144 
2145     RemoveModuleInfo(newInfo.currentPackage_);
2146     AddInnerModuleInfo(newInfo.innerModuleInfos_);
2147     AddModuleAbilityInfo(newInfo.baseAbilityInfos_);
2148     AddModuleSkillInfo(newInfo.skillInfos_);
2149     AddModuleExtensionInfos(newInfo.baseExtensionInfos_);
2150     AddModuleExtensionSkillInfos(newInfo.extensionSkillInfos_);
2151     AddModuleFormInfo(newInfo.formInfos_);
2152     AddModuleShortcutInfo(newInfo.shortcutInfos_);
2153     AddModuleCommonEvent(newInfo.commonEvents_);
2154     UpdateIsCompressNativeLibs();
2155 }
2156 
GetMaxVerBaseSharedBundleInfo(const std::string & moduleName,BaseSharedBundleInfo & baseSharedBundleInfo) const2157 bool InnerBundleInfo::GetMaxVerBaseSharedBundleInfo(const std::string &moduleName,
2158     BaseSharedBundleInfo &baseSharedBundleInfo) const
2159 {
2160     auto it = innerSharedModuleInfos_.find(moduleName);
2161     if (it == innerSharedModuleInfos_.end()) {
2162         APP_LOGE("The shared module(%{public}s) infomation does not exist", moduleName.c_str());
2163         return false;
2164     }
2165     auto sharedModuleInfoVector = it->second;
2166     if (sharedModuleInfoVector.empty()) {
2167         APP_LOGE("No version exists for the shared module(%{public}s)", moduleName.c_str());
2168         return false;
2169     }
2170     InnerModuleInfo innerModuleInfo = sharedModuleInfoVector.front();
2171     if (innerModuleInfo.bundleType != BundleType::SHARED) {
2172         APP_LOGE("GetMaxVerBaseSharedBundleInfo failed, bundleType is invalid!");
2173         return false;
2174     }
2175     baseSharedBundleInfo.bundleName = baseBundleInfo_->name;
2176     baseSharedBundleInfo.moduleName = innerModuleInfo.moduleName;
2177     baseSharedBundleInfo.versionCode = innerModuleInfo.versionCode;
2178     baseSharedBundleInfo.nativeLibraryPath = innerModuleInfo.nativeLibraryPath;
2179     baseSharedBundleInfo.hapPath = innerModuleInfo.hapPath;
2180     baseSharedBundleInfo.compressNativeLibs = innerModuleInfo.compressNativeLibs;
2181     baseSharedBundleInfo.nativeLibraryFileNames = innerModuleInfo.nativeLibraryFileNames;
2182     return true;
2183 }
2184 
GetBaseSharedBundleInfo(const std::string & moduleName,uint32_t versionCode,BaseSharedBundleInfo & baseSharedBundleInfo) const2185 bool InnerBundleInfo::GetBaseSharedBundleInfo(const std::string &moduleName, uint32_t versionCode,
2186     BaseSharedBundleInfo &baseSharedBundleInfo) const
2187 {
2188     auto it = innerSharedModuleInfos_.find(moduleName);
2189     if (it == innerSharedModuleInfos_.end()) {
2190         APP_LOGE("The shared module(%{public}s) infomation does not exist", moduleName.c_str());
2191         return false;
2192     }
2193     auto sharedModuleInfoVector = it->second;
2194     if (sharedModuleInfoVector.empty()) {
2195         APP_LOGE("No version exists for the shared module(%{public}s)", moduleName.c_str());
2196         return false;
2197     }
2198     for (const auto &item : sharedModuleInfoVector) {
2199         if (item.bundleType != BundleType::SHARED) {
2200             APP_LOGE("GetBaseSharedBundleInfo failed, bundleType is invalid!");
2201             return false;
2202         }
2203         if (item.versionCode == versionCode) {
2204             baseSharedBundleInfo.bundleName = baseBundleInfo_->name;
2205             baseSharedBundleInfo.moduleName = item.moduleName;
2206             baseSharedBundleInfo.versionCode = item.versionCode;
2207             baseSharedBundleInfo.nativeLibraryPath = item.nativeLibraryPath;
2208             baseSharedBundleInfo.hapPath = item.hapPath;
2209             baseSharedBundleInfo.compressNativeLibs = item.compressNativeLibs;
2210             baseSharedBundleInfo.nativeLibraryFileNames = item.nativeLibraryFileNames;
2211             return true;
2212         }
2213     }
2214     APP_LOGE("GetBaseSharedBundleInfo failed, the version(%{public}d) is not exists for this module(%{public}s)",
2215         versionCode, moduleName.c_str());
2216     return false;
2217 }
2218 
InsertInnerSharedModuleInfo(const std::string & moduleName,const InnerModuleInfo & innerModuleInfo)2219 void InnerBundleInfo::InsertInnerSharedModuleInfo(const std::string &moduleName,
2220     const InnerModuleInfo &innerModuleInfo)
2221 {
2222     auto iterator = innerSharedModuleInfos_.find(moduleName);
2223     if (iterator != innerSharedModuleInfos_.end()) {
2224         auto innerModuleInfoVector = iterator->second;
2225         bool insertFlag = false;
2226         for (unsigned long i = 0; i < innerModuleInfoVector.size(); i++) {
2227             if (innerModuleInfo.versionCode == innerModuleInfoVector.at(i).versionCode) {
2228                 // if the inserted versionCode same as the existing one, replace old innerModuleInfo.
2229                 innerModuleInfoVector.at(i) = innerModuleInfo;
2230                 insertFlag = true;
2231                 break;
2232             } else if (innerModuleInfo.versionCode > innerModuleInfoVector.at(i).versionCode) {
2233                 // if the inserted versionCode bigger then the existing one, insert the specified location.
2234                 innerModuleInfoVector.emplace(innerModuleInfoVector.begin() + i, innerModuleInfo);
2235                 insertFlag = true;
2236                 break;
2237             } else {
2238                 continue;
2239             }
2240         }
2241         if (!insertFlag) {
2242             // insert innerModuleInfo in last location.
2243             innerModuleInfoVector.emplace(innerModuleInfoVector.end(), innerModuleInfo);
2244         }
2245         innerSharedModuleInfos_[moduleName] = innerModuleInfoVector;
2246     } else {
2247         std::vector<InnerModuleInfo> newInnerModuleInfoVector;
2248         newInnerModuleInfoVector.emplace_back(innerModuleInfo);
2249         innerSharedModuleInfos_.try_emplace(moduleName, newInnerModuleInfoVector);
2250     }
2251 }
2252 
SetSharedModuleNativeLibraryPath(const std::string & nativeLibraryPath)2253 void InnerBundleInfo::SetSharedModuleNativeLibraryPath(const std::string &nativeLibraryPath)
2254 {
2255     auto sharedModuleInfoIterator = innerSharedModuleInfos_.find(currentPackage_);
2256     auto moduleInfoIterator = innerModuleInfos_.find(currentPackage_);
2257     if ((sharedModuleInfoIterator == innerSharedModuleInfos_.end()) ||
2258         (moduleInfoIterator == innerModuleInfos_.end())) {
2259         APP_LOGE("The shared module(%{public}s) infomation does not exist", currentPackage_.c_str());
2260         return;
2261     }
2262     auto &innerModuleInfoVector = sharedModuleInfoIterator->second;
2263     for (auto iter = innerModuleInfoVector.begin(); iter != innerModuleInfoVector.end(); ++iter) {
2264         if (iter->versionCode == moduleInfoIterator->second.versionCode) {
2265             iter->nativeLibraryPath = nativeLibraryPath;
2266             return;
2267         }
2268     }
2269 }
2270 
GetSharedBundleInfo(SharedBundleInfo & sharedBundleInfo) const2271 bool InnerBundleInfo::GetSharedBundleInfo(SharedBundleInfo &sharedBundleInfo) const
2272 {
2273     sharedBundleInfo.name = GetBundleName();
2274     sharedBundleInfo.compatiblePolicy = CompatiblePolicy::BACKWARD_COMPATIBILITY;
2275     std::vector<SharedModuleInfo> sharedModuleInfos;
2276     for (const auto &infoVector : innerSharedModuleInfos_) {
2277         for (const auto &info : infoVector.second) {
2278             SharedModuleInfo sharedModuleInfo;
2279             sharedModuleInfo.name = info.name;
2280             sharedModuleInfo.versionCode = info.versionCode;
2281             sharedModuleInfo.versionName = info.versionName;
2282             sharedModuleInfo.description = info.description;
2283             sharedModuleInfo.descriptionId = info.descriptionId;
2284             sharedModuleInfo.compressNativeLibs = info.compressNativeLibs;
2285             sharedModuleInfo.hapPath = info.hapPath;
2286             sharedModuleInfo.cpuAbi = info.cpuAbi;
2287             sharedModuleInfo.nativeLibraryPath = info.nativeLibraryPath;
2288             sharedModuleInfo.nativeLibraryFileNames = info.nativeLibraryFileNames;
2289             sharedModuleInfos.emplace_back(sharedModuleInfo);
2290         }
2291     }
2292     sharedBundleInfo.sharedModuleInfos = sharedModuleInfos;
2293     return true;
2294 }
2295 
GetSharedDependencies(const std::string & moduleName,std::vector<Dependency> & dependencies) const2296 bool InnerBundleInfo::GetSharedDependencies(const std::string &moduleName,
2297     std::vector<Dependency> &dependencies) const
2298 {
2299     if (innerModuleInfos_.find(moduleName) != innerModuleInfos_.end()) {
2300         dependencies = innerModuleInfos_.at(moduleName).dependencies;
2301         return true;
2302     }
2303     APP_LOGE("GetSharedDependencies can not find module %{public}s", moduleName.c_str());
2304     return false;
2305 }
2306 
GetAllSharedDependencies(const std::string & moduleName,std::vector<Dependency> & dependencies) const2307 bool InnerBundleInfo::GetAllSharedDependencies(const std::string &moduleName,
2308     std::vector<Dependency> &dependencies) const
2309 {
2310     if (!GetSharedDependencies(moduleName, dependencies)) {
2311         return false;
2312     }
2313     std::deque<Dependency> dependenciesDeque;
2314     std::copy(dependencies.begin(), dependencies.end(), std::back_inserter(dependenciesDeque));
2315     dependencies.clear();
2316     while (!dependenciesDeque.empty()) {
2317         bool isAdd = true;
2318         Dependency itemDependency = dependenciesDeque.front();
2319         dependenciesDeque.pop_front();
2320         for (const auto &item : dependencies) {
2321             if (item.bundleName == itemDependency.bundleName && item.moduleName == itemDependency.moduleName &&
2322                 item.versionCode == itemDependency.versionCode) {
2323                 isAdd = false;
2324                 break;
2325             }
2326         }
2327         if (isAdd) {
2328             dependencies.push_back(itemDependency);
2329             std::vector<Dependency> tempDependencies;
2330             if (GetSharedDependencies(itemDependency.moduleName, tempDependencies)) {
2331                 std::copy(tempDependencies.begin(), tempDependencies.end(), std::back_inserter(dependenciesDeque));
2332             }
2333         }
2334     }
2335     return true;
2336 }
2337 
RemoveModuleInfo(const std::string & modulePackage)2338 void InnerBundleInfo::RemoveModuleInfo(const std::string &modulePackage)
2339 {
2340     auto it = innerModuleInfos_.find(modulePackage);
2341     if (it == innerModuleInfos_.end()) {
2342         APP_LOGE("The module(%{public}s) infomation does not exist", modulePackage.c_str());
2343         return;
2344     }
2345 
2346     auto oldModuleInfo = it->second;
2347     if (oldModuleInfo.isEntry) {
2348         baseBundleInfo_->mainEntry.clear();
2349         baseBundleInfo_->entryModuleName.clear();
2350     }
2351     innerModuleInfos_.erase(it);
2352     std::string key;
2353     key.append(".").append(modulePackage).append(".");
2354     for (auto iter = shortcutInfos_.begin(); iter != shortcutInfos_.end();) {
2355         if (iter->first.find(key) != std::string::npos) {
2356             shortcutInfos_.erase(iter++);
2357         } else {
2358             ++iter;
2359         }
2360     }
2361 
2362     for (auto iter = commonEvents_.begin(); iter != commonEvents_.end();) {
2363         if (iter->first.find(key) != std::string::npos) {
2364             commonEvents_.erase(iter++);
2365         } else {
2366             ++iter;
2367         }
2368     }
2369 
2370     // delete old abilityInfos
2371     for (auto abilityKey : oldModuleInfo.abilityKeys) {
2372         auto abilityItem = baseAbilityInfos_.find(abilityKey);
2373         if (abilityItem == baseAbilityInfos_.end()) {
2374             continue;
2375         }
2376 
2377         baseAbilityInfos_.erase(abilityItem);
2378         formInfos_.erase(abilityKey);
2379     }
2380 
2381     // delete old skillInfos
2382     for (auto skillKey : oldModuleInfo.skillKeys) {
2383         auto skillItem = skillInfos_.find(skillKey);
2384         if (skillItem == skillInfos_.end()) {
2385             continue;
2386         }
2387 
2388         skillInfos_.erase(skillItem);
2389     }
2390 
2391     // delete old extensionInfos
2392     for (auto extensionKey : oldModuleInfo.extensionKeys) {
2393         auto extensionItem = baseExtensionInfos_.find(extensionKey);
2394         if (extensionItem == baseExtensionInfos_.end()) {
2395             continue;
2396         }
2397 
2398         baseExtensionInfos_.erase(extensionItem);
2399     }
2400 
2401     // delete old extensionSkillInfos
2402     for (auto extensionSkillKey : oldModuleInfo.extensionSkillKeys) {
2403         auto extensionSkillItem = extensionSkillInfos_.find(extensionSkillKey);
2404         if (extensionSkillItem == extensionSkillInfos_.end()) {
2405             continue;
2406         }
2407 
2408         extensionSkillInfos_.erase(extensionSkillItem);
2409     }
2410 }
2411 
ToString() const2412 std::string InnerBundleInfo::ToString() const
2413 {
2414     nlohmann::json j;
2415     ToJson(j);
2416     return j.dump();
2417 }
2418 
GetApplicationInfo(int32_t flags,int32_t userId,ApplicationInfo & appInfo) const2419 void InnerBundleInfo::GetApplicationInfo(int32_t flags, int32_t userId, ApplicationInfo &appInfo) const
2420 {
2421     InnerBundleUserInfo innerBundleUserInfo;
2422     if (!GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
2423         APP_LOGE("can not find userId %{public}d when get applicationInfo", userId);
2424         return;
2425     }
2426 
2427     appInfo = *baseApplicationInfo_;
2428     if (appInfo.removable && !innerBundleUserInfo.isRemovable) {
2429         appInfo.removable = false;
2430     }
2431 
2432     appInfo.accessTokenId = innerBundleUserInfo.accessTokenId;
2433     appInfo.accessTokenIdEx = innerBundleUserInfo.accessTokenIdEx;
2434     appInfo.enabled = innerBundleUserInfo.bundleUserInfo.enabled;
2435     appInfo.uid = innerBundleUserInfo.uid;
2436 
2437     for (const auto &info : innerModuleInfos_) {
2438         bool deCompress = info.second.hapPath.empty();
2439         ModuleInfo moduleInfo;
2440         moduleInfo.moduleName = info.second.moduleName;
2441         if (deCompress) {
2442             moduleInfo.moduleSourceDir = info.second.modulePath;
2443             appInfo.moduleSourceDirs.emplace_back(info.second.modulePath);
2444         }
2445         moduleInfo.preloads = info.second.preloads;
2446         appInfo.moduleInfos.emplace_back(moduleInfo);
2447         if (deCompress && info.second.isEntry) {
2448             appInfo.entryDir = info.second.modulePath;
2449         }
2450         if ((static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_PERMISSION) ==
2451             GET_APPLICATION_INFO_WITH_PERMISSION) {
2452             for (const auto &item : info.second.requestPermissions) {
2453                 appInfo.permissions.push_back(item.name);
2454             }
2455         }
2456         if ((static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_METADATA) == GET_APPLICATION_INFO_WITH_METADATA) {
2457             bool isModuleJson = info.second.isModuleJson;
2458             if (!isModuleJson && info.second.metaData.customizeData.size() > 0) {
2459                 appInfo.metaData[info.second.moduleName] = info.second.metaData.customizeData;
2460             }
2461             if (isModuleJson && info.second.metadata.size() > 0) {
2462                 appInfo.metadata[info.second.moduleName] = info.second.metadata;
2463             }
2464         }
2465         if ((static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT) !=
2466             GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT) {
2467             appInfo.fingerprint.clear();
2468         }
2469     }
2470     if (!appInfo.permissions.empty()) {
2471         RemoveDuplicateName(appInfo.permissions);
2472     }
2473 }
2474 
GetApplicationInfoV9(int32_t flags,int32_t userId,ApplicationInfo & appInfo) const2475 ErrCode InnerBundleInfo::GetApplicationInfoV9(int32_t flags, int32_t userId, ApplicationInfo &appInfo) const
2476 {
2477     InnerBundleUserInfo innerBundleUserInfo;
2478     if (!GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
2479         APP_LOGE("can not find userId %{public}d when get applicationInfo", userId);
2480         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2481     }
2482 
2483     appInfo = *baseApplicationInfo_;
2484     if (appInfo.removable && !innerBundleUserInfo.isRemovable) {
2485         appInfo.removable = false;
2486     }
2487 
2488     appInfo.accessTokenId = innerBundleUserInfo.accessTokenId;
2489     appInfo.accessTokenIdEx = innerBundleUserInfo.accessTokenIdEx;
2490     appInfo.enabled = innerBundleUserInfo.bundleUserInfo.enabled;
2491     appInfo.uid = innerBundleUserInfo.uid;
2492 
2493     for (const auto &info : innerModuleInfos_) {
2494         bool deCompress = info.second.hapPath.empty();
2495         ModuleInfo moduleInfo;
2496         moduleInfo.moduleName = info.second.moduleName;
2497         if (deCompress) {
2498             moduleInfo.moduleSourceDir = info.second.modulePath;
2499             appInfo.moduleSourceDirs.emplace_back(info.second.modulePath);
2500         }
2501         moduleInfo.preloads = info.second.preloads;
2502         appInfo.moduleInfos.emplace_back(moduleInfo);
2503         if (deCompress && info.second.isEntry) {
2504             appInfo.entryDir = info.second.modulePath;
2505         }
2506         if ((static_cast<uint32_t>(flags) &
2507             static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION)) ==
2508             static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION)) {
2509             for (const auto &item : info.second.requestPermissions) {
2510                 appInfo.permissions.push_back(item.name);
2511             }
2512         }
2513         if ((static_cast<uint32_t>(flags) &
2514             static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_METADATA)) ==
2515             static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_METADATA)) {
2516             bool isModuleJson = info.second.isModuleJson;
2517             if (!isModuleJson && info.second.metaData.customizeData.size() > 0) {
2518                 appInfo.metaData[info.second.moduleName] = info.second.metaData.customizeData;
2519             }
2520             if (isModuleJson && info.second.metadata.size() > 0) {
2521                 appInfo.metadata[info.second.moduleName] = info.second.metadata;
2522             }
2523         }
2524     }
2525     if (!appInfo.permissions.empty()) {
2526         RemoveDuplicateName(appInfo.permissions);
2527     }
2528     return ERR_OK;
2529 }
2530 
GetBundleInfo(int32_t flags,BundleInfo & bundleInfo,int32_t userId) const2531 bool InnerBundleInfo::GetBundleInfo(int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
2532 {
2533     InnerBundleUserInfo innerBundleUserInfo;
2534     if (!GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
2535         APP_LOGE("can not find userId %{public}d when GetBundleInfo bundleName:%{public}s",
2536             userId, GetBundleName().c_str());
2537         return false;
2538     }
2539 
2540     bundleInfo = *baseBundleInfo_;
2541 
2542     bundleInfo.uid = innerBundleUserInfo.uid;
2543     if (!innerBundleUserInfo.gids.empty()) {
2544         bundleInfo.gid = innerBundleUserInfo.gids[0];
2545     }
2546     bundleInfo.installTime = innerBundleUserInfo.installTime;
2547     bundleInfo.updateTime = innerBundleUserInfo.updateTime;
2548     bundleInfo.appIndex = appIndex_;
2549     bundleInfo.overlayType = overlayType_;
2550 
2551     GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
2552         bundleInfo.applicationInfo);
2553     for (const auto &info : innerModuleInfos_) {
2554         if ((static_cast<uint32_t>(flags) & GET_BUNDLE_WITH_REQUESTED_PERMISSION)
2555             == GET_BUNDLE_WITH_REQUESTED_PERMISSION) {
2556             for (const auto &item : info.second.requestPermissions) {
2557                 bundleInfo.reqPermissions.push_back(item.name);
2558             }
2559             for (const auto &item : info.second.definePermissions) {
2560                 bundleInfo.defPermissions.push_back(item.name);
2561             }
2562         }
2563         bundleInfo.hapModuleNames.emplace_back(info.second.modulePackage);
2564         auto hapmoduleinfo = FindHapModuleInfo(info.second.modulePackage, userId);
2565         if (hapmoduleinfo) {
2566             GetModuleWithHashValue(flags, info.second.modulePackage, *hapmoduleinfo);
2567             bundleInfo.hapModuleInfos.emplace_back(*hapmoduleinfo);
2568             bundleInfo.moduleNames.emplace_back(info.second.moduleName);
2569             bundleInfo.moduleDirs.emplace_back(info.second.modulePath);
2570             bundleInfo.modulePublicDirs.emplace_back(info.second.moduleDataDir);
2571             bundleInfo.moduleResPaths.emplace_back(info.second.moduleResPath);
2572         } else {
2573             APP_LOGE("can not find hapmoduleinfo %{public}s", info.second.moduleName.c_str());
2574         }
2575     }
2576     if ((static_cast<uint32_t>(flags) & GET_BUNDLE_WITH_REQUESTED_PERMISSION)
2577         == GET_BUNDLE_WITH_REQUESTED_PERMISSION) {
2578         if (!bundleInfo.reqPermissions.empty()) {
2579             RemoveDuplicateName(bundleInfo.reqPermissions);
2580         }
2581         if (!bundleInfo.defPermissions.empty()) {
2582             RemoveDuplicateName(bundleInfo.defPermissions);
2583         }
2584         if (!BundlePermissionMgr::GetRequestPermissionStates(bundleInfo,
2585             bundleInfo.applicationInfo.accessTokenId, bundleInfo.applicationInfo.deviceId)) {
2586             APP_LOGE("get request permission state failed");
2587         }
2588         bundleInfo.reqPermissionDetails = GetAllRequestPermissions();
2589     }
2590     GetBundleWithAbilities(flags, bundleInfo, userId);
2591     GetBundleWithExtension(flags, bundleInfo, userId);
2592     return true;
2593 }
2594 
GetBundleInfoV9(int32_t flags,BundleInfo & bundleInfo,int32_t userId) const2595 ErrCode InnerBundleInfo::GetBundleInfoV9(int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
2596 {
2597     InnerBundleUserInfo innerBundleUserInfo;
2598     if (!GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
2599         APP_LOGE("can not find userId %{public}d when GetBundleInfo", userId);
2600         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2601     }
2602 
2603     bundleInfo = *baseBundleInfo_;
2604 
2605     bundleInfo.uid = innerBundleUserInfo.uid;
2606     if (!innerBundleUserInfo.gids.empty()) {
2607         bundleInfo.gid = innerBundleUserInfo.gids[0];
2608     }
2609     bundleInfo.installTime = innerBundleUserInfo.installTime;
2610     bundleInfo.updateTime = innerBundleUserInfo.updateTime;
2611     bundleInfo.appIndex = appIndex_;
2612 
2613     for (const auto &info : innerModuleInfos_) {
2614         bundleInfo.hapModuleNames.emplace_back(info.second.modulePackage);
2615         auto hapmoduleinfo = FindHapModuleInfo(info.second.modulePackage, userId);
2616         if (hapmoduleinfo) {
2617             bundleInfo.moduleNames.emplace_back(info.second.moduleName);
2618             bundleInfo.moduleDirs.emplace_back(info.second.modulePath);
2619             bundleInfo.modulePublicDirs.emplace_back(info.second.moduleDataDir);
2620             bundleInfo.moduleResPaths.emplace_back(info.second.moduleResPath);
2621         } else {
2622             APP_LOGE("can not find hapmoduleinfo %{public}s", info.second.moduleName.c_str());
2623         }
2624     }
2625     ProcessBundleFlags(flags, userId, bundleInfo);
2626     return ERR_OK;
2627 }
2628 
GetSharedBundleInfo(int32_t flags,BundleInfo & bundleInfo) const2629 bool InnerBundleInfo::GetSharedBundleInfo(int32_t flags, BundleInfo &bundleInfo) const
2630 {
2631     bundleInfo = *baseBundleInfo_;
2632     ProcessBundleWithHapModuleInfoFlag(flags, bundleInfo, Constants::ALL_USERID);
2633     bundleInfo.applicationInfo = *baseApplicationInfo_;
2634     return true;
2635 }
2636 
ProcessBundleFlags(int32_t flags,int32_t userId,BundleInfo & bundleInfo) const2637 void InnerBundleInfo::ProcessBundleFlags(
2638     int32_t flags, int32_t userId, BundleInfo &bundleInfo) const
2639 {
2640     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION))
2641         == static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)) {
2642         if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA))
2643             == static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)) {
2644             GetApplicationInfoV9(static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_METADATA), userId,
2645                 bundleInfo.applicationInfo);
2646         } else {
2647             GetApplicationInfoV9(static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_DEFAULT), userId,
2648                 bundleInfo.applicationInfo);
2649         }
2650     }
2651     GetBundleWithReqPermissionsV9(flags, userId, bundleInfo);
2652     ProcessBundleWithHapModuleInfoFlag(flags, bundleInfo, userId);
2653     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO))
2654         == static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)) {
2655         bundleInfo.signatureInfo.appId = baseBundleInfo_->appId;
2656         bundleInfo.signatureInfo.fingerprint = baseApplicationInfo_->fingerprint;
2657     }
2658 }
2659 
GetBundleWithReqPermissionsV9(int32_t flags,int32_t userId,BundleInfo & bundleInfo) const2660 void InnerBundleInfo::GetBundleWithReqPermissionsV9(int32_t flags, int32_t userId, BundleInfo &bundleInfo) const
2661 {
2662     if ((static_cast<uint32_t>(flags) &
2663         static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION))
2664         != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION)) {
2665         return;
2666     }
2667     for (const auto &info : innerModuleInfos_) {
2668         for (const auto &item : info.second.requestPermissions) {
2669             bundleInfo.reqPermissions.push_back(item.name);
2670         }
2671         for (const auto &item : info.second.definePermissions) {
2672             bundleInfo.defPermissions.push_back(item.name);
2673         }
2674     }
2675     if (!bundleInfo.reqPermissions.empty()) {
2676         RemoveDuplicateName(bundleInfo.reqPermissions);
2677     }
2678     if (!bundleInfo.defPermissions.empty()) {
2679         RemoveDuplicateName(bundleInfo.defPermissions);
2680     }
2681     InnerBundleUserInfo innerBundleUserInfo;
2682     if (!GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
2683         APP_LOGE("can not find userId %{public}d when get applicationInfo", userId);
2684         return;
2685     }
2686     uint32_t tokenId = innerBundleUserInfo.accessTokenId;
2687     std::string deviceId = baseApplicationInfo_->deviceId;
2688     if (!BundlePermissionMgr::GetRequestPermissionStates(bundleInfo, tokenId, deviceId)) {
2689         APP_LOGE("get request permission state failed");
2690     }
2691     bundleInfo.reqPermissionDetails = GetAllRequestPermissions();
2692 }
2693 
GetModuleWithHashValue(int32_t flags,const std::string & modulePackage,HapModuleInfo & hapModuleInfo) const2694 void InnerBundleInfo::GetModuleWithHashValue(
2695     int32_t flags, const std::string &modulePackage, HapModuleInfo &hapModuleInfo) const
2696 {
2697     if (!(static_cast<uint32_t>(flags) & GET_BUNDLE_WITH_HASH_VALUE)) {
2698         return;
2699     }
2700 
2701     auto it = innerModuleInfos_.find(modulePackage);
2702     if (it == innerModuleInfos_.end()) {
2703         APP_LOGE("can not find module %{public}s", modulePackage.c_str());
2704         return;
2705     }
2706 
2707     hapModuleInfo.hashValue = it->second.hashValue;
2708 }
2709 
ProcessBundleWithHapModuleInfoFlag(int32_t flags,BundleInfo & bundleInfo,int32_t userId) const2710 void InnerBundleInfo::ProcessBundleWithHapModuleInfoFlag(int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
2711 {
2712     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE))
2713         != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE)) {
2714         bundleInfo.hapModuleInfos.clear();
2715         return;
2716     }
2717     for (const auto &info : innerModuleInfos_) {
2718         auto hapmoduleinfo = FindHapModuleInfo(info.second.modulePackage, userId);
2719         if (hapmoduleinfo) {
2720             HapModuleInfo hapModuleInfo = *hapmoduleinfo;
2721             auto it = innerModuleInfos_.find(info.second.modulePackage);
2722             if (it == innerModuleInfos_.end()) {
2723                 APP_LOGE("can not find module %{public}s", info.second.modulePackage.c_str());
2724             } else {
2725                 hapModuleInfo.hashValue = it->second.hashValue;
2726             }
2727             if (hapModuleInfo.hapPath.empty()) {
2728                 hapModuleInfo.moduleSourceDir = info.second.modulePath;
2729             }
2730             if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA))
2731                 != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)) {
2732                 hapModuleInfo.metadata.clear();
2733             }
2734 
2735             GetBundleWithAbilitiesV9(flags, hapModuleInfo, userId);
2736             GetBundleWithExtensionAbilitiesV9(flags, hapModuleInfo);
2737             bundleInfo.hapModuleInfos.emplace_back(hapModuleInfo);
2738         }
2739     }
2740 }
2741 
GetBundleWithAbilitiesV9(int32_t flags,HapModuleInfo & hapModuleInfo,int32_t userId) const2742 void InnerBundleInfo::GetBundleWithAbilitiesV9(int32_t flags, HapModuleInfo &hapModuleInfo, int32_t userId) const
2743 {
2744     hapModuleInfo.abilityInfos.clear();
2745     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY))
2746         != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY)) {
2747         return;
2748     }
2749     APP_LOGD("Get bundleInfo with abilities.");
2750     for (auto &ability : baseAbilityInfos_) {
2751         if ((ability.second.moduleName != hapModuleInfo.moduleName) ||
2752             (ability.second.name == Constants::APP_DETAIL_ABILITY)) {
2753             continue;
2754         }
2755         bool isEnabled = IsAbilityEnabled(ability.second, userId);
2756         if (!(static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE))
2757             && !isEnabled) {
2758             APP_LOGW("%{public}s is disabled,", ability.second.name.c_str());
2759             continue;
2760         }
2761         AbilityInfo abilityInfo = ability.second;
2762         abilityInfo.enabled = isEnabled;
2763 
2764         if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA))
2765             != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)) {
2766             abilityInfo.metaData.customizeData.clear();
2767             abilityInfo.metadata.clear();
2768         }
2769         hapModuleInfo.abilityInfos.emplace_back(abilityInfo);
2770     }
2771 }
2772 
GetBundleWithExtensionAbilitiesV9(int32_t flags,HapModuleInfo & hapModuleInfo) const2773 void InnerBundleInfo::GetBundleWithExtensionAbilitiesV9(int32_t flags, HapModuleInfo &hapModuleInfo) const
2774 {
2775     hapModuleInfo.extensionInfos.clear();
2776     if ((static_cast<uint32_t>(flags) &
2777         static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY))
2778         != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY)) {
2779         return;
2780     }
2781     APP_LOGD("Get bundleInfo with extensionAbilities.");
2782     for (const auto &extensionInfo : baseExtensionInfos_) {
2783         if (extensionInfo.second.moduleName != hapModuleInfo.moduleName || !extensionInfo.second.enabled) {
2784             continue;
2785         }
2786         ExtensionAbilityInfo info = extensionInfo.second;
2787 
2788         if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA))
2789             != static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)) {
2790             info.metadata.clear();
2791         }
2792         hapModuleInfo.extensionInfos.emplace_back(info);
2793     }
2794 }
2795 
GetBundleWithAbilities(int32_t flags,BundleInfo & bundleInfo,int32_t userId) const2796 void InnerBundleInfo::GetBundleWithAbilities(int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
2797 {
2798     APP_LOGD("bundleName:%{public}s userid:%{public}d", bundleInfo.name.c_str(), userId);
2799     if (static_cast<uint32_t>(flags) & GET_BUNDLE_WITH_ABILITIES) {
2800         for (auto &ability : baseAbilityInfos_) {
2801             if (ability.second.name == Constants::APP_DETAIL_ABILITY) {
2802                 continue;
2803             }
2804             bool isEnabled = IsAbilityEnabled(ability.second, userId);
2805             if (!(static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_DISABLE)
2806                 && !isEnabled) {
2807                 APP_LOGW("%{public}s is disabled,", ability.second.name.c_str());
2808                 continue;
2809             }
2810             AbilityInfo abilityInfo = ability.second;
2811             abilityInfo.enabled = isEnabled;
2812             bundleInfo.abilityInfos.emplace_back(abilityInfo);
2813         }
2814     }
2815 }
2816 
GetBundleWithExtension(int32_t flags,BundleInfo & bundleInfo,int32_t userId) const2817 void InnerBundleInfo::GetBundleWithExtension(int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
2818 {
2819     APP_LOGD("get bundleInfo with extensionInfo begin");
2820     if ((static_cast<uint32_t>(flags) & GET_BUNDLE_WITH_EXTENSION_INFO) == GET_BUNDLE_WITH_EXTENSION_INFO) {
2821         for (const auto &extensionInfo : baseExtensionInfos_) {
2822             if (!extensionInfo.second.enabled) {
2823                 continue;
2824             }
2825             ExtensionAbilityInfo info = extensionInfo.second;
2826             bundleInfo.extensionInfos.emplace_back(info);
2827         }
2828     }
2829     APP_LOGD("get bundleInfo with extensionInfo end");
2830 }
2831 
CheckSpecialMetaData(const std::string & metaData) const2832 bool InnerBundleInfo::CheckSpecialMetaData(const std::string &metaData) const
2833 {
2834     if (isNewVersion_) {
2835         for (const auto &moduleInfo : innerModuleInfos_) {
2836             for (const auto &data : moduleInfo.second.metadata) {
2837                 if (metaData == data.name) {
2838                     return true;
2839                 }
2840             }
2841         }
2842         return false;
2843     }
2844     // old version
2845     for (const auto &moduleInfo : innerModuleInfos_) {
2846         for (const auto &data : moduleInfo.second.metaData.customizeData) {
2847             if (metaData == data.name) {
2848                 return true;
2849             }
2850         }
2851     }
2852     return false;
2853 }
2854 
GetFormsInfoByModule(const std::string & moduleName,std::vector<FormInfo> & formInfos) const2855 void InnerBundleInfo::GetFormsInfoByModule(const std::string &moduleName, std::vector<FormInfo> &formInfos) const
2856 {
2857     for (const auto &data : formInfos_) {
2858         for (auto &form : data.second) {
2859             if (form.moduleName == moduleName) {
2860                 formInfos.emplace_back(form);
2861             }
2862         }
2863     }
2864 }
2865 
GetFormsInfoByApp(std::vector<FormInfo> & formInfos) const2866 void InnerBundleInfo::GetFormsInfoByApp(std::vector<FormInfo> &formInfos) const
2867 {
2868     for (const auto &data : formInfos_) {
2869         std::copy(data.second.begin(), data.second.end(), std::back_inserter(formInfos));
2870     }
2871 }
2872 
GetShortcutInfos(std::vector<ShortcutInfo> & shortcutInfos) const2873 void InnerBundleInfo::GetShortcutInfos(std::vector<ShortcutInfo> &shortcutInfos) const
2874 {
2875     if (isNewVersion_) {
2876         AbilityInfo abilityInfo;
2877         GetMainAbilityInfo(abilityInfo);
2878         if ((!abilityInfo.resourcePath.empty() || !abilityInfo.hapPath.empty())
2879             && abilityInfo.metadata.size() > 0) {
2880             std::vector<std::string> rawJson;
2881             BundleMgrClient bundleMgrClient;
2882             bool ret = bundleMgrClient.GetResConfigFile(abilityInfo, META_DATA_SHORTCUTS_NAME, rawJson);
2883             if (!ret) {
2884                 APP_LOGD("GetResConfigFile return false");
2885                 return;
2886             }
2887             if (rawJson.size() == 0) {
2888                 APP_LOGD("rawJson size 0. skip.");
2889                 return;
2890             }
2891             nlohmann::json jsonObject = nlohmann::json::parse(rawJson[0], nullptr, false);
2892             if (jsonObject.is_discarded()) {
2893                 APP_LOGE("shortcuts json invalid");
2894                 return;
2895             }
2896             ShortcutJson shortcutJson = jsonObject.get<ShortcutJson>();
2897             for (const Shortcut &item : shortcutJson.shortcuts) {
2898                 ShortcutInfo shortcutInfo;
2899                 shortcutInfo.bundleName = abilityInfo.bundleName;
2900                 shortcutInfo.moduleName = abilityInfo.moduleName;
2901                 InnerProcessShortcut(item, shortcutInfo);
2902                 shortcutInfos.emplace_back(shortcutInfo);
2903             }
2904         }
2905         return;
2906     }
2907     for (const auto &shortcut : shortcutInfos_) {
2908         shortcutInfos.emplace_back(shortcut.second);
2909     }
2910 }
2911 
GetCommonEvents(const std::string & eventKey,std::vector<CommonEventInfo> & commonEvents) const2912 void InnerBundleInfo::GetCommonEvents(const std::string &eventKey, std::vector<CommonEventInfo> &commonEvents) const
2913 {
2914     CommonEventInfo item;
2915     for (const auto &commonEvent : commonEvents_) {
2916         for (const auto &event : commonEvent.second.events) {
2917             if (event == eventKey) {
2918                 item = commonEvent.second;
2919                 item.uid = GetUid(GetUserId());
2920                 commonEvents.emplace_back(item);
2921                 break;
2922             }
2923         }
2924     }
2925 }
2926 
GetInnerModuleInfoByModuleName(const std::string & moduleName) const2927 std::optional<InnerModuleInfo> InnerBundleInfo::GetInnerModuleInfoByModuleName(const std::string &moduleName) const
2928 {
2929     for (const auto &innerModuleInfo : innerModuleInfos_) {
2930         APP_LOGD("info.moduleName = %{public}s, moduleName= %{public}s",
2931             innerModuleInfo.second.moduleName.c_str(), moduleName.c_str());
2932         if (innerModuleInfo.second.moduleName == moduleName) {
2933             return innerModuleInfo.second;
2934         }
2935     }
2936     return std::nullopt;
2937 }
2938 
GetModuleNames(std::vector<std::string> & moduleNames) const2939 void InnerBundleInfo::GetModuleNames(std::vector<std::string> &moduleNames) const
2940 {
2941     for (const auto &innerModuleInfo : innerModuleInfos_) {
2942         moduleNames.emplace_back(innerModuleInfo.second.moduleName);
2943     }
2944 }
2945 
ResetBundleState(int32_t userId)2946 void InnerBundleInfo::ResetBundleState(int32_t userId)
2947 {
2948     if (userId == Constants::ALL_USERID) {
2949         for (auto& innerBundleUserInfo : innerBundleUserInfos_) {
2950             innerBundleUserInfo.second.bundleUserInfo.Reset();
2951         }
2952 
2953         return;
2954     }
2955 
2956     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
2957     if (innerBundleUserInfos_.find(key) == innerBundleUserInfos_.end()) {
2958         APP_LOGD("no this user %{public}s", key.c_str());
2959         return;
2960     }
2961 
2962     innerBundleUserInfos_.at(key).bundleUserInfo.Reset();
2963 }
2964 
RemoveInnerBundleUserInfo(int32_t userId)2965 void InnerBundleInfo::RemoveInnerBundleUserInfo(int32_t userId)
2966 {
2967     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
2968     auto infoItem = innerBundleUserInfos_.find(key);
2969     if (infoItem == innerBundleUserInfos_.end()) {
2970         return;
2971     }
2972 
2973     auto result = innerBundleUserInfos_.erase(key);
2974     if (result == 0) {
2975         APP_LOGE("remove userId:%{public}d key:%{public}s info failed", userId, key.c_str());
2976     }
2977     for (auto &innerModuleInfo : innerModuleInfos_) {
2978         DeleteModuleRemovable(innerModuleInfo.second.moduleName, userId);
2979     }
2980 }
2981 
AddInnerBundleUserInfo(const InnerBundleUserInfo & innerBundleUserInfo)2982 void InnerBundleInfo::AddInnerBundleUserInfo(
2983     const InnerBundleUserInfo& innerBundleUserInfo)
2984 {
2985     auto& key = NameAndUserIdToKey(
2986         GetBundleName(), innerBundleUserInfo.bundleUserInfo.userId);
2987     auto infoItem = innerBundleUserInfos_.find(key);
2988     if (infoItem == innerBundleUserInfos_.end()) {
2989         innerBundleUserInfos_.emplace(key, innerBundleUserInfo);
2990         return;
2991     }
2992 
2993     innerBundleUserInfos_[key] = innerBundleUserInfo;
2994 }
2995 
GetInnerBundleUserInfo(int32_t userId,InnerBundleUserInfo & innerBundleUserInfo) const2996 bool InnerBundleInfo::GetInnerBundleUserInfo(
2997     int32_t userId, InnerBundleUserInfo& innerBundleUserInfo) const
2998 {
2999     if (userId == Constants::NOT_EXIST_USERID) {
3000         return true;
3001     }
3002 
3003     if (userId == Constants::ALL_USERID) {
3004         if (innerBundleUserInfos_.empty()) {
3005             return false;
3006         }
3007 
3008         innerBundleUserInfo = innerBundleUserInfos_.begin()->second;
3009         return true;
3010     }
3011 
3012     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3013     auto infoItem = innerBundleUserInfos_.find(key);
3014     if (infoItem == innerBundleUserInfos_.end()) {
3015         return false;
3016     }
3017 
3018     innerBundleUserInfo = infoItem->second;
3019     return true;
3020 }
3021 
HasInnerBundleUserInfo(int32_t userId) const3022 bool InnerBundleInfo::HasInnerBundleUserInfo(int32_t userId) const
3023 {
3024     if (userId == Constants::ALL_USERID || userId == Constants::ANY_USERID) {
3025         return !innerBundleUserInfos_.empty();
3026     }
3027 
3028     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3029     auto infoItem = innerBundleUserInfos_.find(key);
3030     return infoItem != innerBundleUserInfos_.end();
3031 }
3032 
SetBundleInstallTime(const int64_t time,int32_t userId)3033 void InnerBundleInfo::SetBundleInstallTime(const int64_t time, int32_t userId)
3034 {
3035     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3036     auto infoItem = innerBundleUserInfos_.find(key);
3037     if (infoItem == innerBundleUserInfos_.end()) {
3038         return;
3039     }
3040 
3041     infoItem->second.installTime = time;
3042     infoItem->second.updateTime = time;
3043 }
3044 
SetAccessTokenId(uint32_t accessToken,const int32_t userId)3045 void InnerBundleInfo::SetAccessTokenId(uint32_t accessToken, const int32_t userId)
3046 {
3047     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3048     auto infoItem = innerBundleUserInfos_.find(key);
3049     if (infoItem == innerBundleUserInfos_.end()) {
3050         return;
3051     }
3052 
3053     infoItem->second.accessTokenId = accessToken;
3054 }
3055 
SetAccessTokenIdEx(const Security::AccessToken::AccessTokenIDEx accessTokenIdEx,const int32_t userId)3056 void InnerBundleInfo::SetAccessTokenIdEx(
3057     const Security::AccessToken::AccessTokenIDEx accessTokenIdEx,
3058     const int32_t userId)
3059 {
3060     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3061     auto infoItem = innerBundleUserInfos_.find(key);
3062     if (infoItem == innerBundleUserInfos_.end()) {
3063         return;
3064     }
3065 
3066     infoItem->second.accessTokenId = accessTokenIdEx.tokenIdExStruct.tokenID;
3067     infoItem->second.accessTokenIdEx = accessTokenIdEx.tokenIDEx;
3068 }
3069 
SetBundleUpdateTime(const int64_t time,int32_t userId)3070 void InnerBundleInfo::SetBundleUpdateTime(const int64_t time, int32_t userId)
3071 {
3072     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3073     auto infoItem = innerBundleUserInfos_.find(key);
3074     if (infoItem == innerBundleUserInfos_.end()) {
3075         return;
3076     }
3077 
3078     infoItem->second.updateTime = time;
3079 }
3080 
IsAbilityEnabled(const AbilityInfo & abilityInfo,int32_t userId) const3081 bool InnerBundleInfo::IsAbilityEnabled(const AbilityInfo &abilityInfo, int32_t userId) const
3082 {
3083     APP_LOGD("IsAbilityEnabled bundleName:%{public}s, userId:%{public}d", abilityInfo.bundleName.c_str(), userId);
3084     if (userId == Constants::NOT_EXIST_USERID) {
3085         return true;
3086     }
3087     auto& key = NameAndUserIdToKey(abilityInfo.bundleName, userId);
3088     auto infoItem = innerBundleUserInfos_.find(key);
3089     if (infoItem == innerBundleUserInfos_.end()) {
3090         APP_LOGE("innerBundleUserInfos find key:%{public}s, error", key.c_str());
3091         return false;
3092     }
3093     auto disabledAbilities = infoItem->second.bundleUserInfo.disabledAbilities;
3094     if (std::find(disabledAbilities.begin(), disabledAbilities.end(), abilityInfo.name) != disabledAbilities.end()) {
3095         return false;
3096     } else {
3097         return true;
3098     }
3099 }
3100 
SetOverlayModuleState(const std::string & moduleName,int32_t state,int32_t userId)3101 void InnerBundleInfo::SetOverlayModuleState(const std::string &moduleName, int32_t state, int32_t userId)
3102 {
3103     APP_LOGD("start to set overlay moduleInfo state of module %{public}s", moduleName.c_str());
3104     if (overlayType_ == NON_OVERLAY_TYPE) {
3105         APP_LOGW("no overlay module");
3106         return;
3107     }
3108     for (auto &innerUserInfo : innerBundleUserInfos_) {
3109         if (innerUserInfo.second.bundleUserInfo.userId != userId) {
3110             continue;
3111         }
3112 
3113         auto &overlayStates = innerUserInfo.second.bundleUserInfo.overlayModulesState;
3114         bool isSetSucc = std::any_of(overlayStates.begin(), overlayStates.end(), [&moduleName, &state](auto &item) {
3115             if (item.find(moduleName + Constants::FILE_UNDERLINE) != std::string::npos) {
3116                 item = moduleName + Constants::FILE_UNDERLINE + std::to_string(state);
3117                 return true;
3118             }
3119             return false;
3120         });
3121         if (!isSetSucc) {
3122             APP_LOGD("no overlay module state info under user %{public}d", userId);
3123             overlayStates.emplace_back(moduleName + Constants::FILE_UNDERLINE + std::to_string(state));
3124         }
3125     }
3126 }
3127 
SetOverlayModuleState(const std::string & moduleName,int32_t state)3128 void InnerBundleInfo::SetOverlayModuleState(const std::string &moduleName, int32_t state)
3129 {
3130     APP_LOGD("start to set overlay moduleInfo state of module %{public}s", moduleName.c_str());
3131     if (overlayType_ == NON_OVERLAY_TYPE) {
3132         APP_LOGW("no overlay module");
3133         return;
3134     }
3135     for (auto &innerUserInfo : innerBundleUserInfos_) {
3136         auto &overlayStates = innerUserInfo.second.bundleUserInfo.overlayModulesState;
3137         bool isSetSucc = std::any_of(overlayStates.begin(), overlayStates.end(), [&moduleName, &state](auto &item) {
3138             if (item.find(moduleName + Constants::FILE_UNDERLINE) != std::string::npos) {
3139                 item = moduleName + Constants::FILE_UNDERLINE + std::to_string(state);
3140                 return true;
3141             }
3142             return false;
3143         });
3144         if (!isSetSucc) {
3145             overlayStates.emplace_back(moduleName + Constants::FILE_UNDERLINE + std::to_string(state));
3146         }
3147     }
3148 }
3149 
GetOverlayModuleState(const std::string & moduleName,int32_t userId,int32_t & state) const3150 bool InnerBundleInfo::GetOverlayModuleState(const std::string &moduleName, int32_t userId, int32_t &state) const
3151 {
3152     APP_LOGD("start to get overlay state of moduleName:%{public}s, userId:%{public}d", moduleName.c_str(), userId);
3153     if (userId == Constants::NOT_EXIST_USERID) {
3154         APP_LOGE("invalid userId %{public}d", userId);
3155         return false;
3156     }
3157     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3158     auto infoItem = innerBundleUserInfos_.find(key);
3159     if (infoItem == innerBundleUserInfos_.end()) {
3160         APP_LOGE("no userInfo under userId %{public}d", userId);
3161         return false;
3162     }
3163 
3164     auto overlayModulesState = infoItem->second.bundleUserInfo.overlayModulesState;
3165     if (overlayModulesState.empty()) {
3166         APP_LOGE("no overlay module installed under userId %{public}d", userId);
3167         return false;
3168     }
3169     for (const auto &item : overlayModulesState) {
3170         auto pos = item.find(moduleName + Constants::FILE_UNDERLINE);
3171         if (pos == std::string::npos) {
3172             continue;
3173         }
3174         return OHOS::StrToInt(item.substr(moduleName.length() + 1), state);
3175     }
3176     APP_LOGE("no overlay module installed under userId %{public}d", userId);
3177     return false;
3178 }
3179 
ClearOverlayModuleStates(const std::string & moduleName)3180 void InnerBundleInfo::ClearOverlayModuleStates(const std::string &moduleName)
3181 {
3182     // delete overlay module state
3183     for (auto &innerUserInfo : innerBundleUserInfos_) {
3184         auto &overlayStates = innerUserInfo.second.bundleUserInfo.overlayModulesState;
3185         auto iter = std::find_if(overlayStates.begin(), overlayStates.end(), [&moduleName](const auto &item) {
3186             if (item.find(moduleName + Constants::FILE_UNDERLINE) != std::string::npos) {
3187                 return true;
3188             }
3189             return false;
3190         });
3191         if (iter != overlayStates.end()) {
3192             overlayStates.erase(iter);
3193         }
3194     }
3195 }
3196 
IsAbilityEnabledV9(const AbilityInfo & abilityInfo,int32_t userId,bool & isEnable) const3197 ErrCode InnerBundleInfo::IsAbilityEnabledV9(const AbilityInfo &abilityInfo, int32_t userId, bool &isEnable) const
3198 {
3199     APP_LOGD("IsAbilityEnabled bundleName:%{public}s, userId:%{public}d", abilityInfo.bundleName.c_str(), userId);
3200     if (userId == Constants::NOT_EXIST_USERID) {
3201         isEnable = true;
3202         return ERR_OK;
3203     }
3204     auto& key = NameAndUserIdToKey(abilityInfo.bundleName, userId);
3205     auto infoItem = innerBundleUserInfos_.find(key);
3206     if (infoItem == innerBundleUserInfos_.end()) {
3207         APP_LOGE("innerBundleUserInfos find key:%{public}s, error", key.c_str());
3208         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
3209     }
3210     auto disabledAbilities = infoItem->second.bundleUserInfo.disabledAbilities;
3211     if (std::find(disabledAbilities.begin(), disabledAbilities.end(), abilityInfo.name) != disabledAbilities.end()) {
3212         isEnable = false;
3213     } else {
3214         isEnable = true;
3215     }
3216     return ERR_OK;
3217 }
3218 
SetAbilityEnabled(const std::string & moduleName,const std::string & abilityName,bool isEnabled,int32_t userId)3219 ErrCode InnerBundleInfo::SetAbilityEnabled(
3220     const std::string &moduleName, const std::string &abilityName, bool isEnabled, int32_t userId)
3221 {
3222     APP_LOGD("SetAbilityEnabled : %{public}s, %{public}s, %{public}d",
3223         moduleName.c_str(), abilityName.c_str(), userId);
3224     for (const auto &ability : baseAbilityInfos_) {
3225         if ((ability.second.name == abilityName) &&
3226             (moduleName.empty() || (ability.second.moduleName == moduleName))) {
3227             auto &key = NameAndUserIdToKey(GetBundleName(), userId);
3228             auto infoItem = innerBundleUserInfos_.find(key);
3229             if (infoItem == innerBundleUserInfos_.end()) {
3230                 APP_LOGE("SetAbilityEnabled find innerBundleUserInfo failed");
3231                 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
3232             }
3233 
3234             auto iter = std::find(infoItem->second.bundleUserInfo.disabledAbilities.begin(),
3235                                   infoItem->second.bundleUserInfo.disabledAbilities.end(),
3236                                   abilityName);
3237             if (iter != infoItem->second.bundleUserInfo.disabledAbilities.end()) {
3238                 if (isEnabled) {
3239                     infoItem->second.bundleUserInfo.disabledAbilities.erase(iter);
3240                 }
3241             } else {
3242                 if (!isEnabled) {
3243                     infoItem->second.bundleUserInfo.disabledAbilities.push_back(abilityName);
3244                 }
3245             }
3246             return ERR_OK;
3247         }
3248     }
3249     APP_LOGE("SetAbilityEnabled find abilityInfo failed");
3250     return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3251 }
3252 
RemoveDuplicateName(std::vector<std::string> & name) const3253 void InnerBundleInfo::RemoveDuplicateName(std::vector<std::string> &name) const
3254 {
3255     std::sort(name.begin(), name.end());
3256     auto iter = std::unique(name.begin(), name.end());
3257     name.erase(iter, name.end());
3258 }
3259 
GetAllDefinePermissions() const3260 std::vector<DefinePermission> InnerBundleInfo::GetAllDefinePermissions() const
3261 {
3262     std::vector<DefinePermission> definePermissions;
3263     for (const auto &info : innerModuleInfos_) {
3264         std::transform(info.second.definePermissions.begin(),
3265             info.second.definePermissions.end(),
3266             std::back_inserter(definePermissions),
3267             [](const auto &p) { return p; });
3268     }
3269     if (!definePermissions.empty()) {
3270         std::sort(definePermissions.begin(), definePermissions.end(),
3271             [](DefinePermission defPermA, DefinePermission defPermB) {
3272                 return defPermA.name < defPermB.name;
3273             });
3274         auto iter = std::unique(definePermissions.begin(), definePermissions.end(),
3275             [](DefinePermission defPermA, DefinePermission defPermB) {
3276                 return defPermA.name == defPermB.name;
3277             });
3278         definePermissions.erase(iter, definePermissions.end());
3279     }
3280     return definePermissions;
3281 }
3282 
GetAllRequestPermissions() const3283 std::vector<RequestPermission> InnerBundleInfo::GetAllRequestPermissions() const
3284 {
3285     std::vector<RequestPermission> requestPermissions;
3286     for (const auto &info : innerModuleInfos_) {
3287         for (auto item : info.second.requestPermissions) {
3288             item.moduleName = info.second.moduleName;
3289             requestPermissions.push_back(item);
3290         }
3291     }
3292     if (!requestPermissions.empty()) {
3293         std::sort(requestPermissions.begin(), requestPermissions.end(),
3294             [](RequestPermission reqPermA, RequestPermission reqPermB) {
3295                 if (reqPermA.name == reqPermB.name) {
3296                     return reqPermA.reasonId > reqPermB.reasonId;
3297                 }
3298                 return reqPermA.name < reqPermB.name;
3299             });
3300         auto iter = std::unique(requestPermissions.begin(), requestPermissions.end(),
3301             [](RequestPermission reqPermA, RequestPermission reqPermB) {
3302                 return reqPermA.name == reqPermB.name;
3303             });
3304         requestPermissions.erase(iter, requestPermissions.end());
3305     }
3306     return requestPermissions;
3307 }
3308 
SetApplicationEnabled(bool enabled,int32_t userId)3309 ErrCode InnerBundleInfo::SetApplicationEnabled(bool enabled, int32_t userId)
3310 {
3311     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3312     auto infoItem = innerBundleUserInfos_.find(key);
3313     if (infoItem == innerBundleUserInfos_.end()) {
3314         APP_LOGE("SetApplicationEnabled can not find:%{public}s bundleUserInfo in userId: %{public}d",
3315             GetBundleName().c_str(), userId);
3316         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
3317     }
3318 
3319     infoItem->second.bundleUserInfo.enabled = enabled;
3320     return ERR_OK;
3321 }
3322 
GetCurModuleName() const3323 const std::string &InnerBundleInfo::GetCurModuleName() const
3324 {
3325     if (innerModuleInfos_.find(currentPackage_) != innerModuleInfos_.end()) {
3326         return innerModuleInfos_.at(currentPackage_).moduleName;
3327     }
3328 
3329     return Constants::EMPTY_STRING;
3330 }
3331 
IsBundleRemovable() const3332 bool InnerBundleInfo::IsBundleRemovable() const
3333 {
3334     if (IsPreInstallApp()) {
3335         APP_LOGE("PreInstallApp should not be cleaned");
3336         return false;
3337     }
3338 
3339     for (const auto &innerModuleInfo : innerModuleInfos_) {
3340         if (!innerModuleInfo.second.installationFree) {
3341             return false;
3342         }
3343 
3344         for (const auto &stateIter : innerModuleInfo.second.isRemovable) {
3345             if (!stateIter.second) {
3346                 return false;
3347             }
3348         }
3349     }
3350 
3351     return true;
3352 }
3353 
GetLastInstallationTime() const3354 int64_t InnerBundleInfo::GetLastInstallationTime() const
3355 {
3356     int64_t installTime = 0;
3357     for (const auto &innerBundleUserInfo : innerBundleUserInfos_) {
3358         installTime = innerBundleUserInfo.second.updateTime > installTime ?
3359             innerBundleUserInfo.second.updateTime : installTime;
3360     }
3361 
3362     return installTime;
3363 }
3364 
GetRemovableModules(std::vector<std::string> & moduleToDelete) const3365 bool InnerBundleInfo::GetRemovableModules(std::vector<std::string> &moduleToDelete) const
3366 {
3367     for (const auto &innerModuleInfo : innerModuleInfos_) {
3368         if (!innerModuleInfo.second.installationFree) {
3369             continue;
3370         }
3371 
3372         bool canDelete = true;
3373         for (const auto &stateIter : innerModuleInfo.second.isRemovable) {
3374             if (!stateIter.second) {
3375                 canDelete = false;
3376                 break;
3377             }
3378         }
3379 
3380         if (canDelete) {
3381             moduleToDelete.emplace_back(innerModuleInfo.second.moduleName);
3382         }
3383     }
3384 
3385     return !moduleToDelete.empty();
3386 }
3387 
GetFreeInstallModules(std::vector<std::string> & freeInstallModule) const3388 bool InnerBundleInfo::GetFreeInstallModules(std::vector<std::string> &freeInstallModule) const
3389 {
3390     for (const auto &innerModuleInfo : innerModuleInfos_) {
3391         if (!innerModuleInfo.second.installationFree) {
3392             continue;
3393         }
3394 
3395         freeInstallModule.emplace_back(innerModuleInfo.second.moduleName);
3396     }
3397 
3398     return !freeInstallModule.empty();
3399 }
3400 
IsUserExistModule(const std::string & moduleName,int32_t userId) const3401 bool InnerBundleInfo::IsUserExistModule(const std::string &moduleName, int32_t userId) const
3402 {
3403     APP_LOGD("userId:%{public}d moduleName:%{public}s", userId, moduleName.c_str());
3404     auto modInfoItem = GetInnerModuleInfoByModuleName(moduleName);
3405     if (!modInfoItem) {
3406         APP_LOGE("get InnerModuleInfo by moduleName(%{public}s) failed", moduleName.c_str());
3407         return false;
3408     }
3409 
3410     auto item = modInfoItem->isRemovable.find(std::to_string(userId));
3411     if (item == modInfoItem->isRemovable.end()) {
3412         APP_LOGE("userId:%{public}d has not moduleName:%{public}s", userId, moduleName.c_str());
3413         return false;
3414     }
3415 
3416     APP_LOGD("userId:%{public}d exist moduleName:%{public}s", userId, moduleName.c_str());
3417     return true;
3418 }
3419 
IsModuleRemovable(const std::string & moduleName,int32_t userId,bool & isRemovable) const3420 ErrCode InnerBundleInfo::IsModuleRemovable(
3421     const std::string &moduleName, int32_t userId, bool &isRemovable) const
3422 {
3423     APP_LOGD("userId:%{public}d moduleName:%{public}s", userId, moduleName.c_str());
3424     auto modInfoItem = GetInnerModuleInfoByModuleName(moduleName);
3425     if (!modInfoItem) {
3426         APP_LOGE("get InnerModuleInfo by moduleName(%{public}s) failed", moduleName.c_str());
3427         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
3428     }
3429 
3430     auto item = modInfoItem->isRemovable.find(std::to_string(userId));
3431     if (item == modInfoItem->isRemovable.end()) {
3432         APP_LOGW("userId:%{public}d has not moduleName:%{public}s", userId, moduleName.c_str());
3433         isRemovable = false;
3434         return ERR_OK;
3435     }
3436 
3437     isRemovable = item->second;
3438     APP_LOGD("userId:%{public}d, moduleName:%{public}s, isRemovable:%{public}d,",
3439         userId, moduleName.c_str(), isRemovable);
3440     return ERR_OK;
3441 }
3442 
AddModuleRemovableInfo(InnerModuleInfo & info,const std::string & stringUserId,bool isEnable) const3443 bool InnerBundleInfo::AddModuleRemovableInfo(
3444     InnerModuleInfo &info, const std::string &stringUserId, bool isEnable) const
3445 {
3446     auto item = info.isRemovable.find(stringUserId);
3447     if (item == info.isRemovable.end()) {
3448         auto result = info.isRemovable.try_emplace(stringUserId, isEnable);
3449         if (!result.second) {
3450             APP_LOGE("add userId:%{public}s isRemovable:%{public}d failed", stringUserId.c_str(), isEnable);
3451             return false;
3452         }
3453 
3454         APP_LOGD("add userId:%{public}s isRemovable:%{public}d into map", stringUserId.c_str(), isEnable);
3455         return true;
3456     }
3457 
3458     item->second = isEnable;
3459     APP_LOGD("set userId:%{public}s isEnable:%{public}d ok", stringUserId.c_str(), isEnable);
3460     return true;
3461 }
3462 
SetModuleRemovable(const std::string & moduleName,bool isEnable,int32_t userId)3463 bool InnerBundleInfo::SetModuleRemovable(const std::string &moduleName, bool isEnable, int32_t userId)
3464 {
3465     std::string stringUserId = std::to_string(userId);
3466     APP_LOGD("userId:%{public}d moduleName:%{public}s isEnable:%{public}d", userId, moduleName.c_str(), isEnable);
3467     for (auto &innerModuleInfo : innerModuleInfos_) {
3468         if (innerModuleInfo.second.moduleName == moduleName) {
3469             return AddModuleRemovableInfo(innerModuleInfo.second, stringUserId, isEnable);
3470         }
3471     }
3472 
3473     return false;
3474 }
3475 
DeleteModuleRemovableInfo(InnerModuleInfo & info,const std::string & stringUserId)3476 void InnerBundleInfo::DeleteModuleRemovableInfo(InnerModuleInfo &info, const std::string &stringUserId)
3477 {
3478     auto item = info.isRemovable.find(stringUserId);
3479     if (item == info.isRemovable.end()) {
3480         return;
3481     }
3482 
3483     info.isRemovable.erase(stringUserId);
3484 }
3485 
DeleteModuleRemovable(const std::string & moduleName,int32_t userId)3486 void InnerBundleInfo::DeleteModuleRemovable(const std::string &moduleName, int32_t userId)
3487 {
3488     std::string stringUserId = std::to_string(userId);
3489     APP_LOGD("userId:%{public}d moduleName:%{public}s", userId, moduleName.c_str());
3490     for (auto &innerModuleInfo : innerModuleInfos_) {
3491         if (innerModuleInfo.second.moduleName == moduleName) {
3492             DeleteModuleRemovableInfo(innerModuleInfo.second, stringUserId);
3493             return;
3494         }
3495     }
3496 }
3497 
SetModuleUpgradeFlag(std::string moduleName,int32_t upgradeFlag)3498 ErrCode InnerBundleInfo::SetModuleUpgradeFlag(std::string moduleName, int32_t upgradeFlag)
3499 {
3500     APP_LOGD("moduleName= %{public}s, upgradeFlag = %{public}d", moduleName.c_str(), upgradeFlag);
3501     for (auto &innerModuleInfo : innerModuleInfos_) {
3502         if (innerModuleInfo.second.moduleName == moduleName) {
3503             innerModuleInfo.second.upgradeFlag = upgradeFlag;
3504             return ERR_OK;
3505         }
3506     }
3507     return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
3508 }
3509 
GetModuleUpgradeFlag(std::string moduleName) const3510 int32_t InnerBundleInfo::GetModuleUpgradeFlag(std::string moduleName) const
3511 {
3512     auto moduleInfo = GetInnerModuleInfoByModuleName(moduleName);
3513     if (!moduleInfo) {
3514         APP_LOGE("get InnerModuleInfo by moduleName(%{public}s) failed", moduleName.c_str());
3515         return UpgradeFlag::NOT_UPGRADE;
3516     }
3517     APP_LOGD("innerModuleInfo.upgradeFlag : %{public}d", moduleInfo->upgradeFlag);
3518     return moduleInfo->upgradeFlag;
3519 }
3520 
GetResponseUserId(int32_t requestUserId) const3521 int32_t InnerBundleInfo::GetResponseUserId(int32_t requestUserId) const
3522 {
3523     if (innerBundleUserInfos_.empty()) {
3524         APP_LOGE("Get responseUserId failed due to user map is empty.");
3525         return Constants::INVALID_USERID;
3526     }
3527 
3528     if (requestUserId == Constants::ANY_USERID) {
3529         return innerBundleUserInfos_.begin()->second.bundleUserInfo.userId;
3530     }
3531 
3532     if (HasInnerBundleUserInfo(requestUserId)) {
3533         return requestUserId;
3534     }
3535 
3536     if (requestUserId < Constants::START_USERID) {
3537         APP_LOGD("requestUserId(%{public}d) less than start userId.", requestUserId);
3538         return Constants::INVALID_USERID;
3539     }
3540 
3541     int32_t responseUserId = Constants::INVALID_USERID;
3542     for (const auto &innerBundleUserInfo : innerBundleUserInfos_) {
3543         if (innerBundleUserInfo.second.bundleUserInfo.userId < Constants::START_USERID) {
3544             responseUserId = innerBundleUserInfo.second.bundleUserInfo.userId;
3545             break;
3546         }
3547     }
3548 
3549     APP_LOGD("requestUserId(%{public}d) and responseUserId(%{public}d).", requestUserId, responseUserId);
3550     return responseUserId;
3551 }
3552 
GetUriPrefixList(std::vector<std::string> & uriPrefixList,const std::string & excludeModule) const3553 void InnerBundleInfo::GetUriPrefixList(std::vector<std::string> &uriPrefixList,
3554     const std::string &excludeModule) const
3555 {
3556     std::vector<std::string> uriList;
3557     for (const auto &abilityInfoPair : baseAbilityInfos_) {
3558         if (abilityInfoPair.second.uri.empty()) {
3559             continue;
3560         }
3561         if (!excludeModule.empty() && abilityInfoPair.first.find(excludeModule) == 0) {
3562             continue;
3563         }
3564         uriList.emplace_back(abilityInfoPair.second.uri);
3565     }
3566     for (const auto &extensionInfoPair : baseExtensionInfos_) {
3567         if (extensionInfoPair.second.uri.empty()) {
3568             continue;
3569         }
3570         if (!excludeModule.empty() && extensionInfoPair.first.find(excludeModule) == 0) {
3571             continue;
3572         }
3573         uriList.emplace_back(extensionInfoPair.second.uri);
3574     }
3575     for (const std::string &uri : uriList) {
3576         size_t schemePos = uri.find(Constants::URI_SEPARATOR);
3577         if (schemePos == uri.npos) {
3578             continue;
3579         }
3580         size_t cutPos = uri.find(Constants::SEPARATOR, schemePos + Constants::URI_SEPARATOR_LEN);
3581         std::string uriPrefix;
3582         if (cutPos == uri.npos) {
3583             uriPrefix = uri;
3584         } else {
3585             uriPrefix = uri.substr(0, cutPos);
3586         }
3587         uriPrefixList.emplace_back(uriPrefix);
3588     }
3589 }
3590 
GetUriPrefixList(std::vector<std::string> & uriPrefixList,int32_t userId,const std::string & excludeModule) const3591 void InnerBundleInfo::GetUriPrefixList(std::vector<std::string> &uriPrefixList, int32_t userId,
3592     const std::string &excludeModule) const
3593 {
3594     auto& key = NameAndUserIdToKey(GetBundleName(), userId);
3595     auto infoItem = innerBundleUserInfos_.find(key);
3596     if (infoItem == innerBundleUserInfos_.end()) {
3597         return;
3598     }
3599     GetUriPrefixList(uriPrefixList, excludeModule);
3600 }
3601 
GetDependentModuleNames(const std::string & moduleName,std::vector<std::string> & dependentModuleNames) const3602 bool InnerBundleInfo::GetDependentModuleNames(const std::string &moduleName,
3603     std::vector<std::string> &dependentModuleNames) const
3604 {
3605     for (auto iter = innerModuleInfos_.begin(); iter != innerModuleInfos_.end(); ++iter) {
3606         if (iter->second.moduleName == moduleName) {
3607             for (const auto &dependency : iter->second.dependencies) {
3608                 dependentModuleNames.push_back(dependency.moduleName);
3609             }
3610             return true;
3611         }
3612     }
3613     APP_LOGE("GetDependentModuleNames can not find module %{public}s", moduleName.c_str());
3614     return false;
3615 }
3616 
GetAllDependentModuleNames(const std::string & moduleName,std::vector<std::string> & dependentModuleNames) const3617 bool InnerBundleInfo::GetAllDependentModuleNames(const std::string &moduleName,
3618     std::vector<std::string> &dependentModuleNames) const
3619 {
3620     if (!GetDependentModuleNames(moduleName, dependentModuleNames)) {
3621         return false;
3622     }
3623     std::deque<std::string> moduleDeque;
3624     std::copy(dependentModuleNames.begin(), dependentModuleNames.end(), std::back_inserter(moduleDeque));
3625     dependentModuleNames.clear();
3626     while (!moduleDeque.empty()) {
3627         std::string name = moduleDeque.front();
3628         moduleDeque.pop_front();
3629         if (std::find(dependentModuleNames.begin(), dependentModuleNames.end(), name) == dependentModuleNames.end()) {
3630             dependentModuleNames.push_back(name);
3631             std::vector<std::string> tempModuleNames;
3632             if (GetDependentModuleNames(name, tempModuleNames)) {
3633                 std::copy(tempModuleNames.begin(), tempModuleNames.end(), std::back_inserter(moduleDeque));
3634             }
3635         }
3636     }
3637     return true;
3638 }
3639 
GetMainAbility() const3640 std::string InnerBundleInfo::GetMainAbility() const
3641 {
3642     AbilityInfo abilityInfo;
3643     GetMainAbilityInfo(abilityInfo);
3644     return abilityInfo.name;
3645 }
3646 
GetMainAbilityInfo(AbilityInfo & abilityInfo) const3647 void InnerBundleInfo::GetMainAbilityInfo(AbilityInfo &abilityInfo) const
3648 {
3649     for (const auto& item : innerModuleInfos_) {
3650         const std::string& key = item.second.entryAbilityKey;
3651         if (!key.empty() && (baseAbilityInfos_.count(key) != 0)) {
3652             abilityInfo = baseAbilityInfos_.at(key);
3653             if (item.second.isEntry) {
3654                 return;
3655             }
3656         }
3657     }
3658 }
3659 
HasEntry() const3660 bool InnerBundleInfo::HasEntry() const
3661 {
3662     return std::any_of(innerModuleInfos_.begin(), innerModuleInfos_.end(), [](const auto &item) {
3663             return item.second.isEntry;
3664         });
3665 }
3666 
SetAppDistributionType(const std::string & appDistributionType)3667 void InnerBundleInfo::SetAppDistributionType(const std::string &appDistributionType)
3668 {
3669     baseApplicationInfo_->appDistributionType = appDistributionType;
3670 }
3671 
GetAppDistributionType() const3672 std::string InnerBundleInfo::GetAppDistributionType() const
3673 {
3674     return baseApplicationInfo_->appDistributionType;
3675 }
3676 
SetAppProvisionType(const std::string & appProvisionType)3677 void InnerBundleInfo::SetAppProvisionType(const std::string &appProvisionType)
3678 {
3679     baseApplicationInfo_->appProvisionType = appProvisionType;
3680 }
3681 
GetAppProvisionType() const3682 std::string InnerBundleInfo::GetAppProvisionType() const
3683 {
3684     return baseApplicationInfo_->appProvisionType;
3685 }
3686 
SetAppCrowdtestDeadline(int64_t crowdtestDeadline)3687 void InnerBundleInfo::SetAppCrowdtestDeadline(int64_t crowdtestDeadline)
3688 {
3689     baseApplicationInfo_->crowdtestDeadline = crowdtestDeadline;
3690 }
3691 
GetAppCrowdtestDeadline() const3692 int64_t InnerBundleInfo::GetAppCrowdtestDeadline() const
3693 {
3694     return baseApplicationInfo_->crowdtestDeadline;
3695 }
3696 
GetDistroModuleName() const3697 std::vector<std::string> InnerBundleInfo::GetDistroModuleName() const
3698 {
3699     std::vector<std::string> moduleVec;
3700     for (const auto &item : innerModuleInfos_) {
3701         moduleVec.push_back(item.second.moduleName);
3702     }
3703     return moduleVec;
3704 }
3705 
GetModuleNameByPackage(const std::string & packageName) const3706 std::string InnerBundleInfo::GetModuleNameByPackage(const std::string &packageName) const
3707 {
3708     auto it = innerModuleInfos_.find(packageName);
3709     if (it == innerModuleInfos_.end()) {
3710         return Constants::EMPTY_STRING;
3711     }
3712     return it->second.moduleName;
3713 }
3714 
GetModuleTypeByPackage(const std::string & packageName) const3715 std::string InnerBundleInfo::GetModuleTypeByPackage(const std::string &packageName) const
3716 {
3717     auto it = innerModuleInfos_.find(packageName);
3718     if (it == innerModuleInfos_.end()) {
3719         return Constants::EMPTY_STRING;
3720     }
3721     return it->second.distro.moduleType;
3722 }
3723 
GetAppQuickFix() const3724 AppQuickFix InnerBundleInfo::GetAppQuickFix() const
3725 {
3726     return baseApplicationInfo_->appQuickFix;
3727 }
3728 
SetAppQuickFix(const AppQuickFix & appQuickFix)3729 void InnerBundleInfo::SetAppQuickFix(const AppQuickFix &appQuickFix)
3730 {
3731     baseApplicationInfo_->appQuickFix = appQuickFix;
3732     if (appQuickFix.deployedAppqfInfo.hqfInfos.empty() && appQuickFix.deployingAppqfInfo.hqfInfos.empty()) {
3733         baseApplicationInfo_->appQuickFix.bundleName = Constants::EMPTY_STRING;
3734         baseApplicationInfo_->appQuickFix.versionCode = 0;
3735         baseApplicationInfo_->appQuickFix.versionName = Constants::EMPTY_STRING;
3736     }
3737     SetQuickFixHqfInfos(appQuickFix.deployedAppqfInfo.hqfInfos);
3738 }
3739 
GetQuickFixHqfInfos() const3740 std::vector<HqfInfo> InnerBundleInfo::GetQuickFixHqfInfos() const
3741 {
3742     return hqfInfos_;
3743 }
3744 
SetQuickFixHqfInfos(const std::vector<HqfInfo> & hqfInfos)3745 void InnerBundleInfo::SetQuickFixHqfInfos(const std::vector<HqfInfo> &hqfInfos)
3746 {
3747     hqfInfos_ = hqfInfos;
3748 }
3749 
FetchNativeSoAttrs(const std::string & requestPackage,std::string & cpuAbi,std::string & nativeLibraryPath) const3750 bool InnerBundleInfo::FetchNativeSoAttrs(
3751     const std::string &requestPackage, std::string &cpuAbi, std::string &nativeLibraryPath) const
3752 {
3753     auto moduleIter = innerModuleInfos_.find(requestPackage);
3754     if (moduleIter == innerModuleInfos_.end()) {
3755         APP_LOGE("requestPackage(%{public}s) is not exist", requestPackage.c_str());
3756         return false;
3757     }
3758 
3759     auto &moduleInfo = moduleIter->second;
3760     if (!moduleInfo.compressNativeLibs) {
3761         cpuAbi = moduleInfo.cpuAbi;
3762         nativeLibraryPath = moduleInfo.nativeLibraryPath;
3763         return !nativeLibraryPath.empty();
3764     }
3765 
3766     if (moduleInfo.isLibIsolated) {
3767         cpuAbi = moduleInfo.cpuAbi;
3768         nativeLibraryPath = moduleInfo.nativeLibraryPath;
3769     } else {
3770         cpuAbi = baseApplicationInfo_->cpuAbi;
3771         nativeLibraryPath = baseApplicationInfo_->nativeLibraryPath;
3772     }
3773 
3774     return !nativeLibraryPath.empty();
3775 }
3776 
IsLibIsolated(const std::string & moduleName) const3777 bool InnerBundleInfo::IsLibIsolated(const std::string &moduleName) const
3778 {
3779     auto moduleInfo = GetInnerModuleInfoByModuleName(moduleName);
3780     if (!moduleInfo) {
3781         APP_LOGE("Get moduleInfo(%{public}s) failed.", moduleName.c_str());
3782         return false;
3783     }
3784 
3785     return moduleInfo->isLibIsolated;
3786 }
3787 
GetDeviceType(const std::string & packageName) const3788 std::vector<std::string> InnerBundleInfo::GetDeviceType(const std::string &packageName) const
3789 {
3790     auto it = innerModuleInfos_.find(packageName);
3791     if (it == innerModuleInfos_.end()) {
3792         APP_LOGW("%{public}s is not existed", packageName.c_str());
3793         return std::vector<std::string>();
3794     }
3795     return innerModuleInfos_.at(packageName).deviceTypes;
3796 }
3797 
AddApplyQuickFixFrequency()3798 void InnerBundleInfo::AddApplyQuickFixFrequency()
3799 {
3800     ++applyQuickFixFrequency_;
3801 }
3802 
GetApplyQuickFixFrequency() const3803 int32_t InnerBundleInfo::GetApplyQuickFixFrequency() const
3804 {
3805     return applyQuickFixFrequency_;
3806 }
3807 
ResetApplyQuickFixFrequency()3808 void InnerBundleInfo::ResetApplyQuickFixFrequency()
3809 {
3810     applyQuickFixFrequency_ = 0;
3811 }
3812 
GetAllHspVersion() const3813 std::vector<uint32_t> InnerBundleInfo::GetAllHspVersion() const
3814 {
3815     std::vector<uint32_t> versionCodes;
3816     for (const auto &[moduleName, modules] : innerSharedModuleInfos_) {
3817         for (const auto &module : modules) {
3818             if (std::find(versionCodes.begin(), versionCodes.end(), module.versionCode) == versionCodes.end()) {
3819                 versionCodes.emplace_back(module.versionCode);
3820             }
3821         }
3822     }
3823     return versionCodes;
3824 }
3825 
DeleteHspModuleByVersion(int32_t versionCode)3826 void InnerBundleInfo::DeleteHspModuleByVersion(int32_t versionCode)
3827 {
3828     for (auto modulesIt = innerSharedModuleInfos_.begin(); modulesIt != innerSharedModuleInfos_.end();) {
3829         if (modulesIt->second.size() == SINGLE_HSP_VERSION &&
3830             modulesIt->second.front().versionCode == static_cast<uint32_t>(versionCode)) {
3831             modulesIt = innerSharedModuleInfos_.erase(modulesIt);
3832         } else {
3833             modulesIt->second.erase(
3834                 std::remove_if(modulesIt->second.begin(), modulesIt->second.end(),
3835                     [versionCode] (InnerModuleInfo &module) {
3836                         return module.versionCode == static_cast<uint32_t>(versionCode);
3837                     }));
3838             ++modulesIt;
3839         }
3840     }
3841 }
3842 
GetProxyDataInfos(const std::string & moduleName,std::vector<ProxyData> & proxyDatas) const3843 ErrCode InnerBundleInfo::GetProxyDataInfos(
3844     const std::string &moduleName, std::vector<ProxyData> &proxyDatas) const
3845 {
3846     if (moduleName == Constants::EMPTY_STRING) {
3847         GetAllProxyDataInfos(proxyDatas);
3848         return ERR_OK;
3849     }
3850     auto moduleIt = std::find_if(innerModuleInfos_.begin(), innerModuleInfos_.end(), [&moduleName](const auto &info) {
3851         return info.second.moduleName == moduleName;
3852     });
3853     if (moduleIt != innerModuleInfos_.end()) {
3854         proxyDatas.insert(
3855             proxyDatas.end(), moduleIt->second.proxyDatas.begin(), moduleIt->second.proxyDatas.end());
3856     } else {
3857         APP_LOGE("moduleName %{public}s not found", moduleName.c_str());
3858         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
3859     }
3860     if (proxyDatas.empty()) {
3861         APP_LOGW("proxyDatas is empty");
3862     }
3863     return ERR_OK;
3864 }
3865 
GetAllProxyDataInfos(std::vector<ProxyData> & proxyDatas) const3866 void InnerBundleInfo::GetAllProxyDataInfos(std::vector<ProxyData> &proxyDatas) const
3867 {
3868     for (const auto &innerModuleInfo : innerModuleInfos_) {
3869         proxyDatas.insert(
3870             proxyDatas.end(), innerModuleInfo.second.proxyDatas.begin(), innerModuleInfo.second.proxyDatas.end());
3871     }
3872 }
3873 
GetIsolationMode(const std::string & isolationMode) const3874 IsolationMode InnerBundleInfo::GetIsolationMode(const std::string &isolationMode) const
3875 {
3876     auto isolationModeRes = ISOLATION_MODE_MAP.find(isolationMode);
3877     if (isolationModeRes != ISOLATION_MODE_MAP.end()) {
3878         return isolationModeRes->second;
3879     } else {
3880         return IsolationMode::NONISOLATION_FIRST;
3881     }
3882 }
3883 
SetModuleHapPath(const std::string & hapPath)3884 void InnerBundleInfo::SetModuleHapPath(const std::string &hapPath)
3885 {
3886     if (innerModuleInfos_.count(currentPackage_) == 1) {
3887         innerModuleInfos_.at(currentPackage_).hapPath = hapPath;
3888         for (auto &abilityInfo : baseAbilityInfos_) {
3889             abilityInfo.second.hapPath = hapPath;
3890         }
3891         for (auto &extensionInfo : baseExtensionInfos_) {
3892             extensionInfo.second.hapPath = hapPath;
3893         }
3894         if (!innerModuleInfos_.at(currentPackage_).compressNativeLibs &&
3895             !innerModuleInfos_.at(currentPackage_).nativeLibraryPath.empty()) {
3896             auto pos = hapPath.rfind(Constants::PATH_SEPARATOR);
3897             if (pos != std::string::npos) {
3898                 innerModuleInfos_.at(currentPackage_).nativeLibraryPath =
3899                     hapPath.substr(pos + 1, hapPath.length() - pos - 1) + NATIVE_LIBRARY_PATH_SYMBOL +
3900                     innerModuleInfos_.at(currentPackage_).nativeLibraryPath;
3901                 return;
3902             }
3903             innerModuleInfos_.at(currentPackage_).nativeLibraryPath =
3904                 hapPath + NATIVE_LIBRARY_PATH_SYMBOL + innerModuleInfos_.at(currentPackage_).nativeLibraryPath;
3905         }
3906     }
3907 }
3908 
IsCompressNativeLibs(const std::string & moduleName) const3909 bool InnerBundleInfo::IsCompressNativeLibs(const std::string &moduleName) const
3910 {
3911     auto moduleInfo = GetInnerModuleInfoByModuleName(moduleName);
3912     if (!moduleInfo) {
3913         APP_LOGE("Get moduleInfo(%{public}s) failed.", moduleName.c_str());
3914         return true; // compressNativeLibs default true
3915     }
3916 
3917     return moduleInfo->compressNativeLibs;
3918 }
3919 
SetNativeLibraryFileNames(const std::string & moduleName,const std::vector<std::string> & fileNames)3920 void InnerBundleInfo::SetNativeLibraryFileNames(const std::string &moduleName,
3921     const std::vector<std::string> &fileNames)
3922 {
3923     if (innerModuleInfos_.find(moduleName) == innerModuleInfos_.end()) {
3924         APP_LOGE("innerBundleInfo does not contain the module: %{public}s.", moduleName.c_str());
3925         return;
3926     }
3927     innerModuleInfos_.at(moduleName).nativeLibraryFileNames = fileNames;
3928 }
3929 
UpdateSharedModuleInfo()3930 void InnerBundleInfo::UpdateSharedModuleInfo()
3931 {
3932     auto sharedModuleInfoIter = innerSharedModuleInfos_.find(currentPackage_);
3933     auto moduleInfoIter = innerModuleInfos_.find(currentPackage_);
3934     if ((sharedModuleInfoIter == innerSharedModuleInfos_.end()) ||
3935         (moduleInfoIter == innerModuleInfos_.end())) {
3936         APP_LOGE("The shared module(%{public}s) infomation does not exist", currentPackage_.c_str());
3937         return;
3938     }
3939     auto &innerModuleInfoVector = sharedModuleInfoIter->second;
3940     for (auto iter = innerModuleInfoVector.begin(); iter != innerModuleInfoVector.end(); ++iter) {
3941         if (iter->versionCode == moduleInfoIter->second.versionCode) {
3942             iter->hapPath = moduleInfoIter->second.hapPath;
3943             iter->compressNativeLibs = moduleInfoIter->second.compressNativeLibs;
3944             iter->cpuAbi = moduleInfoIter->second.cpuAbi;
3945             iter->nativeLibraryPath = moduleInfoIter->second.nativeLibraryPath;
3946             iter->nativeLibraryFileNames = moduleInfoIter->second.nativeLibraryFileNames;
3947             return;
3948         }
3949     }
3950 }
3951 
SetExtName(const std::string & moduleName,const std::string & abilityName,const std::string extName)3952 ErrCode InnerBundleInfo::SetExtName(
3953     const std::string &moduleName, const std::string &abilityName, const std::string extName)
3954 {
3955     auto abilityInfoPair = baseAbilityInfos_.find(abilityName);
3956     if (abilityInfoPair == baseAbilityInfos_.end()) {
3957         APP_LOGE("ability %{public}s not exists", abilityName.c_str());
3958         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3959     }
3960     if (moduleName != abilityInfoPair->second.moduleName) {
3961         APP_LOGE("module %{public}s not exists", moduleName.c_str());
3962         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
3963     }
3964     auto &supportExtNames = abilityInfoPair->second.supportExtNames;
3965     bool duplicated = std::any_of(supportExtNames.begin(), supportExtNames.end(), [&extName](const auto &name) {
3966             return extName == name;
3967     });
3968     if (duplicated) {
3969         APP_LOGW("extName %{public}s already exist in ability %{public}s", extName.c_str(), abilityName.c_str());
3970         return ERR_BUNDLE_MANAGER_DUPLICATED_EXT_OR_TYPE;
3971     }
3972     supportExtNames.emplace_back(extName);
3973     return ERR_OK;
3974 }
3975 
SetMimeType(const std::string & moduleName,const std::string & abilityName,const std::string mimeType)3976 ErrCode InnerBundleInfo::SetMimeType(
3977     const std::string &moduleName, const std::string &abilityName, const std::string mimeType)
3978 {
3979     auto abilityInfoPair = baseAbilityInfos_.find(abilityName);
3980     if (abilityInfoPair == baseAbilityInfos_.end()) {
3981         APP_LOGE("ability %{public}s not exists", abilityName.c_str());
3982         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3983     }
3984     if (moduleName != abilityInfoPair->second.moduleName) {
3985         APP_LOGE("module %{public}s not exists", moduleName.c_str());
3986         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
3987     }
3988     auto &supportMimeTypes = abilityInfoPair->second.supportMimeTypes;
3989     bool duplicated = std::any_of(supportMimeTypes.begin(), supportMimeTypes.end(), [&mimeType](const auto &type) {
3990             return mimeType == type;
3991     });
3992     if (duplicated) {
3993         APP_LOGW("MIME type %{public}s already exist in ability %{public}s", mimeType.c_str(), abilityName.c_str());
3994         return ERR_BUNDLE_MANAGER_DUPLICATED_EXT_OR_TYPE;
3995     }
3996     abilityInfoPair->second.supportMimeTypes.emplace_back(mimeType);
3997     return ERR_OK;
3998 }
3999 
DelExtName(const std::string & moduleName,const std::string & abilityName,const std::string extName)4000 ErrCode InnerBundleInfo::DelExtName(
4001     const std::string &moduleName, const std::string &abilityName, const std::string extName)
4002 {
4003     auto abilityInfoPair = baseAbilityInfos_.find(abilityName);
4004     if (abilityInfoPair == baseAbilityInfos_.end()) {
4005         APP_LOGE("ability %{public}s not exists", abilityName.c_str());
4006         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
4007     }
4008     if (moduleName != abilityInfoPair->second.moduleName) {
4009         APP_LOGE("module %{public}s not exists", moduleName.c_str());
4010         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
4011     }
4012     auto &supportExtNames = abilityInfoPair->second.supportExtNames;
4013     supportExtNames.erase(std::remove(supportExtNames.begin(), supportExtNames.end(), extName), supportExtNames.end());
4014     return ERR_OK;
4015 }
4016 
DelMimeType(const std::string & moduleName,const std::string & abilityName,const std::string mimeType)4017 ErrCode InnerBundleInfo::DelMimeType(
4018     const std::string &moduleName, const std::string &abilityName, const std::string mimeType)
4019 {
4020     auto abilityInfoPair = baseAbilityInfos_.find(abilityName);
4021     if (abilityInfoPair == baseAbilityInfos_.end()) {
4022         APP_LOGE("ability %{public}s not exists", abilityName.c_str());
4023         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
4024     }
4025     if (moduleName != abilityInfoPair->second.moduleName) {
4026         APP_LOGE("module %{public}s not exists", moduleName.c_str());
4027         return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST;
4028     }
4029     auto &supportMimeTypes = abilityInfoPair->second.supportMimeTypes;
4030     supportMimeTypes.erase(
4031         std::remove(supportMimeTypes.begin(), supportMimeTypes.end(), mimeType), supportMimeTypes.end());
4032     return ERR_OK;
4033 }
4034 
UpdateIsCompressNativeLibs()4035 void InnerBundleInfo::UpdateIsCompressNativeLibs()
4036 {
4037     if (innerModuleInfos_.empty()) {
4038         baseApplicationInfo_->isCompressNativeLibs = true;
4039         return;
4040     }
4041     baseApplicationInfo_->isCompressNativeLibs = false;
4042     for (const auto &info : innerModuleInfos_) {
4043         baseApplicationInfo_->isCompressNativeLibs =
4044             (baseApplicationInfo_->isCompressNativeLibs || info.second.compressNativeLibs) ? true : false;
4045     }
4046 }
4047 
SetResourcesApply(const std::vector<int32_t> & resourcesApply)4048 void InnerBundleInfo::SetResourcesApply(const std::vector<int32_t> &resourcesApply)
4049 {
4050     baseApplicationInfo_->resourcesApply = resourcesApply;
4051 }
4052 
InnerProcessShortcut(const Shortcut & oldShortcut,ShortcutInfo & shortcutInfo) const4053 void InnerBundleInfo::InnerProcessShortcut(const Shortcut &oldShortcut, ShortcutInfo &shortcutInfo) const
4054 {
4055     shortcutInfo.id = oldShortcut.shortcutId;
4056     shortcutInfo.icon = oldShortcut.icon;
4057     shortcutInfo.label = oldShortcut.label;
4058     shortcutInfo.iconId = oldShortcut.iconId;
4059     if (shortcutInfo.iconId == 0) {
4060         auto iter = oldShortcut.icon.find(PORT_SEPARATOR);
4061         if (iter != std::string::npos) {
4062             shortcutInfo.iconId = atoi(oldShortcut.icon.substr(iter + 1).c_str());
4063         }
4064     }
4065     shortcutInfo.labelId = oldShortcut.labelId;
4066     if (shortcutInfo.labelId == 0) {
4067         auto iter = oldShortcut.label.find(PORT_SEPARATOR);
4068         if (iter != std::string::npos) {
4069             shortcutInfo.labelId = atoi(oldShortcut.label.substr(iter + 1).c_str());
4070         }
4071     }
4072     for (const ShortcutWant &shortcutWant : oldShortcut.wants) {
4073         ShortcutIntent shortcutIntent;
4074         shortcutIntent.targetBundle = shortcutWant.bundleName;
4075         shortcutIntent.targetModule = shortcutWant.moduleName;
4076         shortcutIntent.targetClass = shortcutWant.abilityName;
4077         shortcutInfo.intents.emplace_back(shortcutIntent);
4078     }
4079 }
4080 
GetEntryModuleName() const4081 std::string InnerBundleInfo::GetEntryModuleName() const
4082 {
4083     for (const auto &item : innerModuleInfos_) {
4084         if (item.second.isEntry) {
4085             return item.second.modulePackage;
4086         }
4087     }
4088     return Constants::EMPTY_STRING;
4089 }
4090 }  // namespace AppExecFwk
4091 }  // namespace OHOS
4092