1 /*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "bundle_manager.h"
16
17 #include <shared_mutex>
18 #include <vector>
19
20 #include "ability_info.h"
21 #include "app_log_wrapper.h"
22 #include "ability_info.h"
23 #include "bundle_error.h"
24 #include "bundle_info.h"
25 #include "bundle_mgr_client.h"
26 #include "bundle_manager_sync.h"
27 #include "bundle_mgr_proxy.h"
28 #include "common_func.h"
29 #include "extension_ability_info.h"
30 #include "ipc_skeleton.h"
31 #include "verify_manager_client.h"
32
33 namespace OHOS {
34 namespace CJSystemapi {
35
36 namespace BundleManager {
37
GetBundleInfoForSelf(int32_t bundleFlags)38 AppExecFwk::BundleInfo BundleManagerImpl::GetBundleInfoForSelf(int32_t bundleFlags)
39 {
40 APP_LOGI("BundleManagerImpl::GetBundleInfoForSelf inter");
41 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
42 AppExecFwk::BundleInfo bundleInfo;
43 iBundleMgr->GetBundleInfoForSelf(bundleFlags, bundleInfo);
44 return bundleInfo;
45 }
46
VerifyAbc(std::vector<std::string> abcPaths,bool flag)47 int32_t BundleManagerImpl::VerifyAbc(std::vector<std::string> abcPaths, bool flag)
48 {
49 ErrCode ret = AppExecFwk::VerifyManagerClient::GetInstance().Verify(abcPaths);
50 if (ret == ERR_OK && flag) {
51 AppExecFwk::VerifyManagerClient::GetInstance().RemoveFiles(abcPaths);
52 }
53 return AppExecFwk::CommonFunc::ConvertErrCode(ret);
54 }
55
checkExtensionAbilityName(const AppExecFwk::ExtensionAbilityInfo & extensionInfo,const std::string & abilityName,AppExecFwk::ExtensionAbilityInfo & targetExtensionInfo)56 std::tuple<bool, int32_t> checkExtensionAbilityName(const AppExecFwk::ExtensionAbilityInfo& extensionInfo,
57 const std::string& abilityName, AppExecFwk::ExtensionAbilityInfo& targetExtensionInfo)
58 {
59 bool ifExists = false;
60 if (extensionInfo.name == abilityName) {
61 ifExists = true;
62 if (!extensionInfo.enabled) {
63 APP_LOGI("extension disabled");
64 return {ifExists, ERROR_ABILITY_IS_DISABLED};
65 }
66 targetExtensionInfo = extensionInfo;
67 }
68 return {ifExists, SUCCESS_CODE};
69 }
70
CheckExtensionFromBundleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AppExecFwk::ExtensionAbilityInfo & targetExtensionInfo)71 ErrCode CheckExtensionFromBundleInfo(const AppExecFwk::BundleInfo& bundleInfo, const std::string& abilityName,
72 const std::string& moduleName, AppExecFwk::ExtensionAbilityInfo& targetExtensionInfo)
73 {
74 bool hasFoundModule = false;
75 bool ifExists = false;
76 int32_t res = SUCCESS_CODE;
77 for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
78 if (hapModuleInfo.moduleName != moduleName) {
79 continue;
80 }
81 hasFoundModule = true;
82 for (const auto& extensionInfo : hapModuleInfo.extensionInfos) {
83 std::tie(ifExists, res) = checkExtensionAbilityName(extensionInfo, abilityName, targetExtensionInfo);
84 if (res == ERROR_ABILITY_IS_DISABLED || (res == SUCCESS_CODE && ifExists == true)) {
85 return res;
86 }
87 }
88 }
89 return hasFoundModule ? ERROR_ABILITY_NOT_EXIST : ERROR_MODULE_NOT_EXIST;
90 }
91
GetProfileByExtensionAbility(std::string moduleName,std::string extensionAbilityName,char * metadataName)92 std::tuple<int32_t, std::vector<std::string>> BundleManagerImpl::GetProfileByExtensionAbility(
93 std::string moduleName, std::string extensionAbilityName, char* metadataName)
94 {
95 auto naBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
96 if (naBundleMgr == nullptr) {
97 return {ERROR_BUNDLE_SERVICE_EXCEPTION, {}};
98 }
99
100 if (extensionAbilityName.empty()) {
101 APP_LOGE("GetProfileByExtensionAbility failed due to empty extensionAbilityName");
102 return {ERROR_ABILITY_NOT_EXIST, {}};
103 }
104
105 if (moduleName.empty()) {
106 APP_LOGE("GetProfileByExtensionAbility failed due to empty moduleName");
107 return {ERROR_MODULE_NOT_EXIST, {}};
108 }
109
110 auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
111 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
112 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
113 auto getExtensionFlag = baseFlag +
114 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY);
115 AppExecFwk::BundleInfo bundleInfo;
116 ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(
117 naBundleMgr->GetBundleInfoForSelf(getExtensionFlag, bundleInfo));
118 if (ret != ERR_OK) {
119 APP_LOGE("GetProfileByExAbility failed");
120 return {ret, {}};
121 }
122 AppExecFwk::ExtensionAbilityInfo targetExtensionInfo;
123 ret = CheckExtensionFromBundleInfo(bundleInfo, extensionAbilityName, moduleName, targetExtensionInfo);
124 if (ret != ERR_OK) {
125 APP_LOGE("GetProfileByExAbility failed by CheckExtensionFromBundleInfo");
126 return {ret, {}};
127 }
128 AppExecFwk::BundleMgrClient client;
129 std::vector<std::string> profileVec;
130 if (!client.GetProfileFromExtension(targetExtensionInfo, metadataName, profileVec)) {
131 APP_LOGE("GetProfileByExAbility failed by GetProfileFromExtension");
132 return {ERROR_PROFILE_NOT_EXIST, {}};
133 }
134 return {SUCCESS_CODE, profileVec};
135 }
136
checkAbilityName(const AppExecFwk::AbilityInfo & abilityInfo,const std::string & abilityName,AppExecFwk::AbilityInfo & targetAbilityInfo)137 std::tuple<bool, int32_t> checkAbilityName(const AppExecFwk::AbilityInfo& abilityInfo,
138 const std::string& abilityName, AppExecFwk::AbilityInfo& targetAbilityInfo)
139 {
140 bool ifExists = false;
141 if (abilityInfo.name == abilityName) {
142 ifExists = true;
143 if (!abilityInfo.enabled) {
144 APP_LOGI("ability disabled");
145 return {ifExists, ERROR_ABILITY_IS_DISABLED};
146 }
147 targetAbilityInfo = abilityInfo;
148 }
149 return {ifExists, SUCCESS_CODE};
150 }
151
CheckAbilityFromBundleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AppExecFwk::AbilityInfo & targetAbilityInfo)152 ErrCode CheckAbilityFromBundleInfo(const AppExecFwk::BundleInfo& bundleInfo, const std::string& abilityName,
153 const std::string& moduleName, AppExecFwk::AbilityInfo& targetAbilityInfo)
154 {
155 bool hasFoundModule = false;
156 bool ifExists = false;
157 int32_t res = SUCCESS_CODE;
158 for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
159 if (hapModuleInfo.moduleName != moduleName) {
160 continue;
161 }
162 hasFoundModule = true;
163 for (const auto& abilityInfo : hapModuleInfo.abilityInfos) {
164 std::tie(ifExists, res) = checkAbilityName(abilityInfo, abilityName, targetAbilityInfo);
165 if (res == ERROR_ABILITY_IS_DISABLED || (res == SUCCESS_CODE && ifExists == true)) {
166 return res;
167 }
168 }
169 }
170 return hasFoundModule ? ERROR_ABILITY_NOT_EXIST : ERROR_MODULE_NOT_EXIST;
171 }
172
GetProfileByAbility(std::string moduleName,std::string abilityName,char * metadataName)173 std::tuple<int32_t, std::vector<std::string>> BundleManagerImpl::GetProfileByAbility(
174 std::string moduleName, std::string abilityName, char* metadataName)
175 {
176 APP_LOGI("GetProfileByAbility called");
177 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
178 if (iBundleMgr == nullptr) {
179 return {ERROR_BUNDLE_SERVICE_EXCEPTION, {}};
180 }
181
182 if (abilityName.empty()) {
183 APP_LOGE("GetProfileByAbility failed due to empty abilityName");
184 return {ERROR_ABILITY_NOT_EXIST, {}};
185 }
186
187 if (moduleName.empty()) {
188 APP_LOGE("GetProfileByAbility failed due to empty moduleName");
189 return {ERROR_MODULE_NOT_EXIST, {}};
190 }
191
192 auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
193 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
194 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
195 auto getAbilityFlag = baseFlag + static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY);
196 AppExecFwk::BundleInfo bundleInfo;
197 ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getAbilityFlag, bundleInfo));
198 if (ret != ERR_OK) {
199 APP_LOGE("GetProfileByAbility failed");
200 return {ret, {}};
201 }
202 AppExecFwk::AbilityInfo targetAbilityInfo;
203 ret = CheckAbilityFromBundleInfo(bundleInfo, abilityName, moduleName, targetAbilityInfo);
204 if (ret != ERR_OK) {
205 APP_LOGE("GetProfileByAbility failed by CheckAbilityFromBundleInfo");
206 return {ret, {}};
207 }
208 AppExecFwk::BundleMgrClient client;
209 std::vector<std::string> profileVec;
210 if (!client.GetProfileFromAbility(targetAbilityInfo, metadataName, profileVec)) {
211 APP_LOGE("GetProfileByAbility failed by GetProfileFromAbility");
212 return {ERROR_PROFILE_NOT_EXIST, {}};
213 }
214 return {SUCCESS_CODE, profileVec};
215 }
216
InnerCanOpenLink(std::string link,int32_t & code)217 bool BundleManagerImpl::InnerCanOpenLink(std::string link, int32_t& code)
218 {
219 bool canOpen = false;
220 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
221 if (iBundleMgr == nullptr) {
222 APP_LOGE("can not get iBundleMgr");
223 code = ERROR_BUNDLE_SERVICE_EXCEPTION;
224 return canOpen;
225 }
226 code = AppExecFwk::CommonFunc::ConvertErrCode(
227 iBundleMgr->CanOpenLink(link, canOpen));
228 if (code != NO_ERROR) {
229 APP_LOGE("CanOpenLink failed");
230 return canOpen;
231 }
232 return canOpen;
233 }
234
GetBundleInfo(const std::string & bundleName,int32_t bundleFlags,int32_t userId,AppExecFwk::BundleInfo & bundleInfo)235 int32_t BundleManagerImpl::GetBundleInfo(
236 const std::string& bundleName, int32_t bundleFlags, int32_t userId, AppExecFwk::BundleInfo& bundleInfo)
237 {
238 APP_LOGD("BundleManagerImpl::GetBundleInfo inter");
239 if (bundleName.size() == 0) {
240 return static_cast<int32_t>(ERROR_BUNDLE_NOT_EXIST);
241 }
242 if (userId == AppExecFwk::Constants::UNSPECIFIED_USERID) {
243 userId = IPCSkeleton::GetCallingUid() / AppExecFwk::Constants::BASE_USER_RANGE;
244 }
245 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
246 if (iBundleMgr == nullptr) {
247 APP_LOGE("BundleMgr is null");
248 return static_cast<int32_t>(ERROR_BUNDLE_SERVICE_EXCEPTION);
249 }
250 ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(
251 iBundleMgr->GetBundleInfoV9(bundleName, bundleFlags, bundleInfo, userId));
252 if (ret != NO_ERROR) {
253 APP_LOGD("GetBundleInfoV9 failed -n %{public}s -f %{public}d -u %{public}d", bundleName.c_str(), bundleFlags,
254 userId);
255 }
256 return ret;
257 }
258
GetBundleNameByUid(int32_t userId,int32_t * errcode)259 std::string BundleManagerImpl::GetBundleNameByUid(int32_t userId, int32_t* errcode)
260 {
261 APP_LOGD("BundleManagerImpl::GetBundleNameByUid inter");
262 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
263 if (iBundleMgr == nullptr) {
264 *errcode = static_cast<int32_t>(ERROR_BUNDLE_SERVICE_EXCEPTION);
265 return "";
266 }
267 std::string bundleName;
268 ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(iBundleMgr->GetNameForUid(userId, bundleName));
269 if (ret != ERR_OK) {
270 if (userId > AppExecFwk::Constants::BASE_APP_UID) {
271 APP_LOGE("failed uid: %{public}d bundleName: %{public}s", userId, bundleName.c_str());
272 }
273 APP_LOGE("GetNameForUid failed");
274 *errcode = ret;
275 return "";
276 }
277 return bundleName;
278 }
279
280 } // BundleManager
281 } // CJSystemapi
282 } // OHOS