1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bundle_container.h"
17
18 #include <nlohmann/json.hpp>
19
20 #include "hilog_tag_wrapper.h"
21 #include "json_serializer.h"
22 #include "module_profile.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 constexpr const char *FILE_SEPARATOR = "/";
GetInstance()27 BundleContainer& BundleContainer::GetInstance()
28 {
29 static BundleContainer instance;
30 return instance;
31 }
32
LoadBundleInfos(const std::vector<uint8_t> & buffer,const std::string & resourcePath)33 void BundleContainer::LoadBundleInfos(const std::vector<uint8_t> &buffer, const std::string &resourcePath)
34 {
35 bundleInfo_ = std::make_shared<InnerBundleInfo>();
36 if (!bundleInfo_) {
37 TAG_LOGD(AAFwkTag::ABILITY_SIM, "null bundleInfo_");
38 return;
39 }
40
41 bundleInfo_->SetIsNewVersion(true);
42 ModuleProfile moduleProfile;
43 moduleProfile.TransformTo(buffer, *bundleInfo_);
44 resourcePath_ = resourcePath;
45 auto appInfo = std::make_shared<ApplicationInfo>();
46 bundleInfo_->GetApplicationInfo(0, Constants::UNSPECIFIED_USERID, *appInfo);
47 if (appInfo != nullptr) {
48 std::string bundleName = appInfo->bundleName;
49 std::string moduleName = appInfo->moduleInfos[0].moduleName;
50 auto key = bundleName + std::string(FILE_SEPARATOR) + moduleName;
51 bundleInfos_.emplace(key, bundleInfo_);
52 resourcePaths_.emplace(key, resourcePath_);
53 }
54 }
55
LoadDependencyHspInfo(const std::string & bundleName,const std::vector<AbilityRuntime::DependencyHspInfo> & dependencyHspInfos)56 void BundleContainer::LoadDependencyHspInfo(
57 const std::string &bundleName, const std::vector<AbilityRuntime::DependencyHspInfo> &dependencyHspInfos)
58 {
59 for (const auto &info : dependencyHspInfos) {
60 auto innerBundleInfo = std::make_shared<InnerBundleInfo>();
61 if (!innerBundleInfo) {
62 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null innerBundleInfo");
63 return;
64 }
65 innerBundleInfo->SetIsNewVersion(true);
66 ModuleProfile moduleProfile;
67 moduleProfile.TransformTo(info.moduleJsonBuffer, *innerBundleInfo);
68 BundleInfo bundleInfo;
69 innerBundleInfo->GetBundleInfoV9(
70 (static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
71 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)),
72 bundleInfo);
73 if (!bundleInfo.moduleNames.empty()) {
74 auto key = bundleName + std::string(FILE_SEPARATOR) + bundleInfo.moduleNames[0];
75 TAG_LOGD(AAFwkTag::ABILITY_SIM, "key: %{public}s", key.c_str());
76 bundleInfos_.emplace(key, innerBundleInfo);
77 resourcePaths_.emplace(key, info.resourcePath);
78 }
79 }
80 }
81
GetApplicationInfo() const82 std::shared_ptr<ApplicationInfo> BundleContainer::GetApplicationInfo() const
83 {
84 if (bundleInfo_ != nullptr) {
85 auto appInfo = std::make_shared<ApplicationInfo>();
86 bundleInfo_->GetApplicationInfo(0, Constants::UNSPECIFIED_USERID, *appInfo);
87 return appInfo;
88 }
89 return nullptr;
90 }
91
GetHapModuleInfo(const std::string & modulePackage) const92 std::shared_ptr<HapModuleInfo> BundleContainer::GetHapModuleInfo(const std::string &modulePackage) const
93 {
94 if (bundleInfo_ != nullptr) {
95 auto uid = Constants::UNSPECIFIED_USERID;
96 TAG_LOGI(AAFwkTag::ABILITY_SIM,
97 "modulePackage:%{public}s", modulePackage.c_str());
98 std::optional<HapModuleInfo> hapMouduleInfo = bundleInfo_->FindHapModuleInfo(modulePackage, uid);
99 if (hapMouduleInfo) {
100 auto hapInfo = std::make_shared<HapModuleInfo>();
101 *hapInfo = *hapMouduleInfo;
102 return hapInfo;
103 }
104 }
105 return nullptr;
106 }
107
GetAbilityInfo(const std::string & moduleName,const std::string & abilityName) const108 std::shared_ptr<AbilityInfo> BundleContainer::GetAbilityInfo(
109 const std::string &moduleName, const std::string &abilityName) const
110 {
111 if (bundleInfo_ != nullptr) {
112 auto uid = Constants::UNSPECIFIED_USERID;
113 std::optional<AbilityInfo> ablilityInfo = bundleInfo_->FindAbilityInfo(moduleName, abilityName, uid);
114 if (ablilityInfo) {
115 auto aInfo = std::make_shared<AbilityInfo>();
116 *aInfo = *ablilityInfo;
117 return aInfo;
118 }
119 }
120 return nullptr;
121 }
122
GetBundleInfo(const std::string & bundleName,const std::string & moduleName,BundleInfo & bundleInfo)123 void BundleContainer::GetBundleInfo(
124 const std::string &bundleName, const std::string &moduleName, BundleInfo &bundleInfo)
125 {
126 auto innerBundleInfo = GetInnerBundleInfo(bundleName, moduleName);
127 if (innerBundleInfo != nullptr) {
128 innerBundleInfo->GetBundleInfoV9(
129 (static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
130 static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)),
131 bundleInfo);
132 UpdateResourcePath(bundleName, moduleName, bundleInfo);
133 }
134 }
135
GetDependentBundleInfo(const std::string & bundleName,const std::string & moduleName,BundleInfo & sharedBundleInfo,GetDependentBundleInfoFlag flag)136 ErrCode BundleContainer::GetDependentBundleInfo(const std::string &bundleName, const std::string &moduleName,
137 BundleInfo &sharedBundleInfo, GetDependentBundleInfoFlag flag)
138 {
139 auto innerBundleInfo = GetInnerBundleInfo(bundleName, moduleName);
140 if (innerBundleInfo == nullptr) {
141 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
142 }
143
144 int32_t bundleInfoFlags = static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) |
145 static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
146 switch (flag) {
147 case GetDependentBundleInfoFlag::GET_ALL_DEPENDENT_BUNDLE_INFO: {
148 if (innerBundleInfo->GetAppServiceHspInfo(sharedBundleInfo) == ERR_OK) {
149 UpdateResourcePath(bundleName, moduleName, sharedBundleInfo);
150 return ERR_OK;
151 }
152 innerBundleInfo->GetSharedBundleInfo(bundleInfoFlags, sharedBundleInfo);
153 UpdateResourcePath(bundleName, moduleName, sharedBundleInfo);
154 return ERR_OK;
155 }
156 default:
157 return ERR_BUNDLE_MANAGER_PARAM_ERROR;
158 }
159 }
160
SetBundleCodeDir(const std::string & bundleCodeDir)161 void BundleContainer::SetBundleCodeDir(const std::string &bundleCodeDir)
162 {
163 bundleCodeDir_ = bundleCodeDir;
164 }
165
GetBundleCodeDir() const166 std::string BundleContainer::GetBundleCodeDir() const
167 {
168 return bundleCodeDir_;
169 }
170
GetInnerBundleInfo(const std::string & bundleName,const std::string & moduleName)171 std::shared_ptr<InnerBundleInfo> BundleContainer::GetInnerBundleInfo(
172 const std::string &bundleName, const std::string &moduleName)
173 {
174 auto key = bundleName + std::string(FILE_SEPARATOR) + moduleName;
175 auto it = bundleInfos_.find(key);
176 if (it == bundleInfos_.end()) {
177 TAG_LOGE(AAFwkTag::ABILITY_SIM, "find hsp innerBundleInfo fail");
178 return nullptr;
179 }
180 return it->second;
181 }
182
UpdateResourcePath(const std::string & bundleName,const std::string & moduleName,BundleInfo & bundleInfo)183 void BundleContainer::UpdateResourcePath(
184 const std::string &bundleName, const std::string &moduleName, BundleInfo &bundleInfo)
185 {
186 auto key = bundleName + std::string(FILE_SEPARATOR) + moduleName;
187 auto it = resourcePaths_.find(key);
188 if (it == resourcePaths_.end()) {
189 TAG_LOGE(AAFwkTag::ABILITY_SIM, "find hsp resourcePath fail");
190 return;
191 }
192 for (auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
193 hapModuleInfo.resourcePath = it->second;
194 }
195 }
196 } // namespace AppExecFwk
197 } // namespace OHOS
198