• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_mgr_host_impl.h"
17 
18 #include "account_helper.h"
19 #include "app_log_tag_wrapper.h"
20 #include "app_mgr_interface.h"
21 #include "aot/aot_handler.h"
22 #include "bms_extension_client.h"
23 #include "bundle_parser.h"
24 #include "bundle_permission_mgr.h"
25 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
26 #include "distributed_bms_proxy.h"
27 #endif
28 #include "hitrace_meter.h"
29 #include "installd_client.h"
30 #include "ipc_skeleton.h"
31 #include "iservice_registry.h"
32 #include "system_ability_helper.h"
33 #include "inner_bundle_clone_common.h"
34 #ifdef DEVICE_USAGE_STATISTICS_ENABLED
35 #include "bundle_active_client.h"
36 #include "bundle_active_period_stats.h"
37 #endif
38 #include "scope_guard.h"
39 #include "xcollie_helper.h"
40 #include "system_ability_definition.h"
41 
42 namespace OHOS {
43 namespace AppExecFwk {
44 namespace {
45 constexpr const char* SYSTEM_APP = "system";
46 constexpr const char* THIRD_PARTY_APP = "third-party";
47 constexpr const char* APP_LINKING = "applinking";
48 constexpr const char* EMPTY_ABILITY_NAME = "";
49 const std::string FUNCATION_GET_NAME_FOR_UID = "BundleMgrHostImpl::GetNameForUid";
50 const std::string FUNCATION_GET_OVERLAY_MANAGER_PROXY = "BundleMgrHostImpl::GetOverlayManagerProxy";
51 const std::string FUNCATION_GET_BUNDLE_RESOURCE_PROXY = "BundleMgrHostImpl::GetBundleResourceProxy";
52 const std::string FUNCATION_VERIFY_SYSTEM_API = "BundleMgrHostImpl::VerifySystemApi";
53 const std::string FUNCATION_VERIFY_CALLING_PERMISSION = "BundleMgrHostImpl::VerifyCallingPermission";
54 const std::string FUNCATION_GET_CLONE_BUNDLE_INFO = "BundleMgrHostImpl::GetCloneBundleInfo";
55 const std::string FUNCATION_GET_SHARED_BUNDLE_INFO_BY_SELF = "BundleMgrHostImpl::GetSharedBundleInfoBySelf";
56 const std::string FUNCATION_GET_HAP_MODULE_INFO = "BundleMgrHostImpl::GetHapModuleInfo";
57 const std::string FUNCATION_BATCH_BUNDLE_INFO = "BundleMgrHostImpl::BatchGetBundleInfo";
58 const std::string FUNCATION_GET_BUNDLE_INFO = "BundleMgrHostImpl::GetBundleInfo";
59 }
60 
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)61 bool BundleMgrHostImpl::GetApplicationInfo(
62     const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo)
63 {
64     return GetApplicationInfo(appName, static_cast<int32_t>(flag), userId, appInfo);
65 }
66 
GetApplicationInfo(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)67 bool BundleMgrHostImpl::GetApplicationInfo(
68     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
69 {
70     LOG_D(BMS_TAG_QUERY, "GetApplicationInfo bundleName:%{public}s flags:%{public}d userId:%{public}d",
71         appName.c_str(), flags, userId);
72     if (!BundlePermissionMgr::IsSystemApp() &&
73         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
74         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
75         return true;
76     }
77     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
78         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
79         !BundlePermissionMgr::IsBundleSelfCalling(appName)) {
80         LOG_E(BMS_TAG_QUERY, "verify permission failed");
81         return false;
82     }
83     auto dataMgr = GetDataMgrFromService();
84     if (dataMgr == nullptr) {
85         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
86         return false;
87     }
88     return dataMgr->GetApplicationInfo(appName, flags, userId, appInfo);
89 }
90 
GetApplicationInfoV9(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)91 ErrCode BundleMgrHostImpl::GetApplicationInfoV9(
92     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
93 {
94     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
95     LOG_D(BMS_TAG_QUERY, "GetApplicationInfoV9 bundleName:%{public}s flags:%{public}d userId:%{public}d",
96         appName.c_str(), flags, userId);
97     if (!BundlePermissionMgr::IsSystemApp()) {
98         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
99         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
100     }
101     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
102         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
103         !BundlePermissionMgr::IsBundleSelfCalling(appName)) {
104         LOG_E(BMS_TAG_QUERY, "verify permission failed");
105         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
106     }
107     auto dataMgr = GetDataMgrFromService();
108     if (dataMgr == nullptr) {
109         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
110         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
111     }
112     return dataMgr->GetApplicationInfoV9(appName, flags, userId, appInfo);
113 }
114 
GetApplicationInfos(const ApplicationFlag flag,const int userId,std::vector<ApplicationInfo> & appInfos)115 bool BundleMgrHostImpl::GetApplicationInfos(
116     const ApplicationFlag flag, const int userId, std::vector<ApplicationInfo> &appInfos)
117 {
118     return GetApplicationInfos(static_cast<int32_t>(flag), userId, appInfos);
119 }
120 
GetApplicationInfos(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)121 bool BundleMgrHostImpl::GetApplicationInfos(
122     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
123 {
124     LOG_D(BMS_TAG_QUERY, "GetApplicationInfos flags:%{public}d userId:%{public}d", flags, userId);
125     if (!BundlePermissionMgr::IsSystemApp() &&
126         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
127         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
128         return true;
129     }
130     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
131         LOG_E(BMS_TAG_QUERY, "verify permission failed");
132         return false;
133     }
134     if (!BundlePermissionMgr::IsNativeTokenType() &&
135         (BundlePermissionMgr::GetHapApiVersion() >= ServiceConstants::API_VERSION_NINE)) {
136         LOG_D(BMS_TAG_QUERY,
137             "GetApplicationInfos return empty, not support target level greater than or equal to api9");
138         return true;
139     }
140     auto dataMgr = GetDataMgrFromService();
141     if (dataMgr == nullptr) {
142         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
143         return false;
144     }
145     return dataMgr->GetApplicationInfos(flags, userId, appInfos);
146 }
147 
GetApplicationInfosV9(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)148 ErrCode BundleMgrHostImpl::GetApplicationInfosV9(
149     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
150 {
151     LOG_D(BMS_TAG_QUERY, "GetApplicationInfosV9 flags:%{public}d userId:%{public}d", flags, userId);
152     if (!BundlePermissionMgr::IsSystemApp()) {
153         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
154         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
155     }
156     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST)) {
157         LOG_E(BMS_TAG_QUERY, "verify permission failed");
158         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
159     }
160     auto dataMgr = GetDataMgrFromService();
161     if (dataMgr == nullptr) {
162         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
163         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
164         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
165     }
166     auto ret = dataMgr->GetApplicationInfosV9(flags, userId, appInfos);
167     if (ret == ERR_OK) {
168         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0);
169     } else {
170         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
171     }
172     return ret;
173 }
174 
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)175 bool BundleMgrHostImpl::GetBundleInfo(
176     const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId)
177 {
178     return GetBundleInfo(bundleName, static_cast<int32_t>(flag), bundleInfo, userId);
179 }
180 
GetBundleInfo(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)181 bool BundleMgrHostImpl::GetBundleInfo(
182     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
183 {
184     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
185     LOG_D(BMS_TAG_QUERY,
186         "start GetBundleInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
187         bundleName.c_str(), flags, userId);
188     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_BUNDLE_INFO);
189     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
190     // API9 need to be system app
191     if (!BundlePermissionMgr::IsSystemApp() &&
192         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
193         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
194         return true;
195     }
196     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
197         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
198         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
199         LOG_E(BMS_TAG_QUERY, "verify permission failed");
200         return false;
201     }
202     LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfo");
203     auto dataMgr = GetDataMgrFromService();
204     if (dataMgr == nullptr) {
205         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
206         return false;
207     }
208     bool res = dataMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId);
209     if (!res) {
210         if (isBrokerServiceExisted_) {
211             auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
212             return bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId) == ERR_OK;
213         }
214     }
215     return res;
216 }
217 
GetBaseSharedBundleInfos(const std::string & bundleName,std::vector<BaseSharedBundleInfo> & baseSharedBundleInfos,GetDependentBundleInfoFlag flag)218 ErrCode BundleMgrHostImpl::GetBaseSharedBundleInfos(const std::string &bundleName,
219     std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos, GetDependentBundleInfoFlag flag)
220 {
221     APP_LOGD("start GetBaseSharedBundleInfos, bundleName : %{public}s", bundleName.c_str());
222     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
223         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
224         APP_LOGE("verify permission failed");
225         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
226     }
227     auto dataMgr = GetDataMgrFromService();
228     if (dataMgr == nullptr) {
229         APP_LOGE("DataMgr is nullptr");
230         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
231     }
232     return dataMgr->GetBaseSharedBundleInfos(bundleName, baseSharedBundleInfos, flag);
233 }
234 
GetBundleInfoV9(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)235 ErrCode BundleMgrHostImpl::GetBundleInfoV9(
236     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
237 {
238     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
239     LOG_D(BMS_TAG_QUERY, "GetBundleInfoV9, bundleName:%{public}s, flags:%{public}d, userId:%{public}d",
240         bundleName.c_str(), flags, userId);
241     if (!BundlePermissionMgr::IsSystemApp()) {
242         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
243         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
244     }
245     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
246         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
247         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
248         LOG_E(BMS_TAG_QUERY, "verify permission failed");
249         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
250     }
251     LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfoV9");
252     auto dataMgr = GetDataMgrFromService();
253     if (dataMgr == nullptr) {
254         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
255         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
256     }
257     auto res = dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId);
258     if (res != ERR_OK) {
259         if (isBrokerServiceExisted_) {
260             auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
261             if (bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId, true) == ERR_OK) {
262                 return ERR_OK;
263             }
264         }
265     }
266     return res;
267 }
268 
BatchGetBundleInfo(const std::vector<std::string> & bundleNames,int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)269 ErrCode BundleMgrHostImpl::BatchGetBundleInfo(const std::vector<std::string> &bundleNames, int32_t flags,
270     std::vector<BundleInfo> &bundleInfos, int32_t userId)
271 {
272     APP_LOGI("start BatchGetBundleInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
273         BundleUtil::ToString(bundleNames).c_str(), flags, userId);
274     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_BATCH_BUNDLE_INFO);
275     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
276     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED})) {
277         APP_LOGE("verify permission failed");
278         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
279     }
280     APP_LOGD("verify permission success, begin to BatchGetBundleInfo");
281     auto dataMgr = GetDataMgrFromService();
282     if (dataMgr == nullptr) {
283         APP_LOGE("DataMgr is nullptr");
284         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
285     }
286     dataMgr->BatchGetBundleInfo(bundleNames, flags, bundleInfos, userId);
287     if (bundleInfos.size() == bundleNames.size()) {
288         return ERR_OK;
289     }
290     if (isBrokerServiceExisted_) {
291         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
292         bmsExtensionClient->BatchGetBundleInfo(bundleNames, flags, bundleInfos, userId, true);
293     }
294     return bundleInfos.empty() ? ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST : ERR_OK;
295 }
296 
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)297 ErrCode BundleMgrHostImpl::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
298 {
299     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
300     auto uid = IPCSkeleton::GetCallingUid();
301     int32_t userId = uid / Constants::BASE_USER_RANGE;
302     std::string bundleName;
303     auto dataMgr = GetDataMgrFromService();
304     if (dataMgr == nullptr) {
305         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
306         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
307     }
308     int32_t appIndex = 0;
309     auto ret = dataMgr->GetBundleNameAndIndexForUid(uid, bundleName, appIndex);
310     if (ret != ERR_OK) {
311         LOG_NOFUNC_E(BMS_TAG_QUERY, "GetBundleNameForUid failed uid:%{public}d", uid);
312         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
313     }
314     return dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId, appIndex);
315 }
316 
GetDependentBundleInfo(const std::string & sharedBundleName,BundleInfo & sharedBundleInfo,GetDependentBundleInfoFlag flag)317 ErrCode BundleMgrHostImpl::GetDependentBundleInfo(const std::string &sharedBundleName,
318     BundleInfo &sharedBundleInfo, GetDependentBundleInfoFlag flag)
319 {
320     auto dataMgr = GetDataMgrFromService();
321     if (dataMgr == nullptr) {
322         APP_LOGE("DataMgr is nullptr");
323         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
324     }
325 
326     int32_t bundleInfoFlags = static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) |
327         static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
328     switch (flag) {
329         case GetDependentBundleInfoFlag::GET_APP_CROSS_HSP_BUNDLE_INFO: {
330             if (!VerifyDependency(sharedBundleName)) {
331                 APP_LOGE("failed");
332                 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
333             }
334             return dataMgr->GetSharedBundleInfo(sharedBundleName, bundleInfoFlags, sharedBundleInfo);
335         }
336         case GetDependentBundleInfoFlag::GET_APP_SERVICE_HSP_BUNDLE_INFO: {
337             // no need to check permission for app service hsp
338             return dataMgr->GetAppServiceHspBundleInfo(sharedBundleName, sharedBundleInfo);
339         }
340         case GetDependentBundleInfoFlag::GET_ALL_DEPENDENT_BUNDLE_INFO: {
341             if (dataMgr->GetAppServiceHspBundleInfo(sharedBundleName, sharedBundleInfo) == ERR_OK) {
342                 return ERR_OK;
343             }
344             if (!VerifyDependency(sharedBundleName)) {
345                 APP_LOGE("failed");
346                 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
347             }
348             return dataMgr->GetSharedBundleInfo(sharedBundleName, bundleInfoFlags, sharedBundleInfo);
349         }
350         default:
351             return ERR_BUNDLE_MANAGER_PARAM_ERROR;
352     }
353 }
354 
GetBundlePackInfo(const std::string & bundleName,const BundlePackFlag flag,BundlePackInfo & bundlePackInfo,int32_t userId)355 ErrCode BundleMgrHostImpl::GetBundlePackInfo(
356     const std::string &bundleName, const BundlePackFlag flag, BundlePackInfo &bundlePackInfo, int32_t userId)
357 {
358     return GetBundlePackInfo(bundleName, static_cast<int32_t>(flag), bundlePackInfo, userId);
359 }
360 
GetBundlePackInfo(const std::string & bundleName,int32_t flags,BundlePackInfo & bundlePackInfo,int32_t userId)361 ErrCode BundleMgrHostImpl::GetBundlePackInfo(
362     const std::string &bundleName, int32_t flags, BundlePackInfo &bundlePackInfo, int32_t userId)
363 {
364     // check permission
365     if (!BundlePermissionMgr::IsSystemApp()) {
366         APP_LOGE("non-system app calling system api");
367         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
368     }
369     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
370         APP_LOGE("GetBundlePackInfo failed due to lack of permission");
371         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
372     }
373     auto dataMgr = GetDataMgrFromService();
374     if (dataMgr == nullptr) {
375         APP_LOGE("DataMgr is nullptr");
376         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
377     }
378     return dataMgr->GetBundlePackInfo(bundleName, flags, bundlePackInfo, userId);
379 }
380 
GetBundleUserInfo(const std::string & bundleName,int32_t userId,InnerBundleUserInfo & innerBundleUserInfo)381 bool BundleMgrHostImpl::GetBundleUserInfo(
382     const std::string &bundleName, int32_t userId, InnerBundleUserInfo &innerBundleUserInfo)
383 {
384     auto dataMgr = GetDataMgrFromService();
385     if (dataMgr == nullptr) {
386         APP_LOGE("DataMgr is nullptr");
387         return false;
388     }
389     return dataMgr->GetInnerBundleUserInfoByUserId(bundleName, userId, innerBundleUserInfo);
390 }
391 
GetBundleUserInfos(const std::string & bundleName,std::vector<InnerBundleUserInfo> & innerBundleUserInfos)392 bool BundleMgrHostImpl::GetBundleUserInfos(
393     const std::string &bundleName, std::vector<InnerBundleUserInfo> &innerBundleUserInfos)
394 {
395     auto dataMgr = GetDataMgrFromService();
396     if (dataMgr == nullptr) {
397         APP_LOGE("DataMgr is nullptr");
398         return false;
399     }
400     return dataMgr->GetInnerBundleUserInfos(bundleName, innerBundleUserInfos);
401 }
402 
GetBundleInfos(const BundleFlag flag,std::vector<BundleInfo> & bundleInfos,int32_t userId)403 bool BundleMgrHostImpl::GetBundleInfos(const BundleFlag flag, std::vector<BundleInfo> &bundleInfos, int32_t userId)
404 {
405     return GetBundleInfos(static_cast<int32_t>(flag), bundleInfos, userId);
406 }
407 
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)408 bool BundleMgrHostImpl::GetBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
409 {
410     LOG_I(BMS_TAG_QUERY, "-f: %{public}d, -u: %{public}d uid:%{public}d", flags,
411         userId, IPCSkeleton::GetCallingUid());
412     // API9 need to be system app
413     if (!BundlePermissionMgr::IsSystemApp() &&
414         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
415         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
416         return true;
417     }
418     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
419         LOG_E(BMS_TAG_QUERY, "verify permission failed");
420         return false;
421     }
422     LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfos");
423     if (!BundlePermissionMgr::IsNativeTokenType() &&
424         (BundlePermissionMgr::GetHapApiVersion() >= ServiceConstants::API_VERSION_NINE)) {
425         LOG_D(BMS_TAG_QUERY,
426             "GetBundleInfos return empty, not support target level greater than or equal to api9");
427         return true;
428     }
429     auto dataMgr = GetDataMgrFromService();
430     if (dataMgr == nullptr) {
431         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
432         return false;
433     }
434     dataMgr->GetBundleInfos(flags, bundleInfos, userId);
435     if (isBrokerServiceExisted_) {
436         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
437         bmsExtensionClient->GetBundleInfos(flags, bundleInfos, userId);
438     }
439     return !bundleInfos.empty();
440 }
441 
GetBundleInfosV9(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)442 ErrCode BundleMgrHostImpl::GetBundleInfosV9(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
443 {
444     LOG_I(BMS_TAG_QUERY, "-f: %{public}d, -u: %{public}d uid:%{public}d", flags,
445         userId, IPCSkeleton::GetCallingUid());
446     if (!BundlePermissionMgr::IsSystemApp()) {
447         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
448         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
449     }
450     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST)) {
451         LOG_E(BMS_TAG_QUERY, "verify permission failed");
452         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
453     }
454     LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfosV9");
455     auto dataMgr = GetDataMgrFromService();
456     if (dataMgr == nullptr) {
457         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
458         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
459         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
460     }
461     auto res = dataMgr->GetBundleInfosV9(flags, bundleInfos, userId);
462     // menu profile is currently not supported in BrokerService
463     bool getMenu = ((static_cast<uint32_t>(flags) & BundleFlag::GET_BUNDLE_WITH_MENU)
464         == BundleFlag::GET_BUNDLE_WITH_MENU);
465     if (isBrokerServiceExisted_ && !getMenu) {
466         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
467         if (bmsExtensionClient->GetBundleInfos(flags, bundleInfos, userId, true) == ERR_OK) {
468             LOG_D(BMS_TAG_QUERY, "query bundle infos from bms extension successfully");
469             BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0);
470             return ERR_OK;
471         }
472     }
473     if (res == ERR_OK) {
474         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0);
475     } else {
476         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
477     }
478     return res;
479 }
480 
GetBundleNameForUid(const int uid,std::string & bundleName)481 bool BundleMgrHostImpl::GetBundleNameForUid(const int uid, std::string &bundleName)
482 {
483     APP_LOGD("start GetBundleNameForUid, uid : %{public}d", uid);
484     if (!BundlePermissionMgr::IsSystemApp() &&
485         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
486         APP_LOGE("non-system app calling system api");
487         return false;
488     }
489     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
490         Constants::PERMISSION_GET_BUNDLE_INFO})) {
491         APP_LOGE("verify query permission failed");
492         return false;
493     }
494     auto dataMgr = GetDataMgrFromService();
495     if (dataMgr == nullptr) {
496         APP_LOGE("DataMgr is nullptr");
497         return false;
498     }
499     return dataMgr->GetBundleNameForUid(uid, bundleName);
500 }
501 
GetBundlesForUid(const int uid,std::vector<std::string> & bundleNames)502 bool BundleMgrHostImpl::GetBundlesForUid(const int uid, std::vector<std::string> &bundleNames)
503 {
504     APP_LOGD("start GetBundlesForUid, uid : %{public}d", uid);
505     if (!BundlePermissionMgr::IsSystemApp()) {
506         APP_LOGE("non-system app calling system api");
507         return false;
508     }
509     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
510         Constants::PERMISSION_GET_BUNDLE_INFO})) {
511         APP_LOGE("verify permission failed");
512         return false;
513     }
514     auto dataMgr = GetDataMgrFromService();
515     if (dataMgr == nullptr) {
516         APP_LOGE("DataMgr is nullptr");
517         return false;
518     }
519     return dataMgr->GetBundlesForUid(uid, bundleNames);
520 }
521 
GetNameForUid(const int uid,std::string & name)522 ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name)
523 {
524     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
525     APP_LOGD("start GetNameForUid, uid : %{public}d", uid);
526     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_NAME_FOR_UID);
527     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
528     if (!BundlePermissionMgr::IsSystemApp() &&
529         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
530         APP_LOGE("non-system app calling system api");
531         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
532     }
533     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
534         Constants::PERMISSION_GET_BUNDLE_INFO})) {
535         APP_LOGE("verify query permission failed");
536         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
537     }
538     auto dataMgr = GetDataMgrFromService();
539     if (dataMgr == nullptr) {
540         APP_LOGE("DataMgr is nullptr");
541         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
542     }
543     auto ret = dataMgr->GetNameForUid(uid, name);
544     if (ret != ERR_OK && isBrokerServiceExisted_) {
545         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
546         ret = bmsExtensionClient->GetBundleNameByUid(uid, name);
547         if (ret != ERR_OK) {
548             return ERR_BUNDLE_MANAGER_INVALID_UID;
549         }
550     }
551     return ret;
552 }
553 
GetNameAndIndexForUid(const int uid,std::string & bundleName,int32_t & appIndex)554 ErrCode BundleMgrHostImpl::GetNameAndIndexForUid(const int uid, std::string &bundleName, int32_t &appIndex)
555 {
556     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
557     APP_LOGD("start GetNameAndIndexForUid, uid : %{public}d", uid);
558     if (!BundlePermissionMgr::IsSystemApp()) {
559         APP_LOGE("non-system app calling system api");
560         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
561     }
562     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
563         Constants::PERMISSION_GET_BUNDLE_INFO})) {
564         APP_LOGE("verify query permission failed");
565         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
566     }
567     auto dataMgr = GetDataMgrFromService();
568     if (dataMgr == nullptr) {
569         APP_LOGE("DataMgr is nullptr");
570         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
571     }
572     return dataMgr->GetBundleNameAndIndexForUid(uid, bundleName, appIndex);
573 }
574 
GetBundleGids(const std::string & bundleName,std::vector<int> & gids)575 bool BundleMgrHostImpl::GetBundleGids(const std::string &bundleName, std::vector<int> &gids)
576 {
577     APP_LOGD("start GetBundleGids, bundleName : %{public}s", bundleName.c_str());
578     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
579         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
580         APP_LOGE("verify token type failed");
581         return false;
582     }
583     auto dataMgr = GetDataMgrFromService();
584     if (dataMgr == nullptr) {
585         APP_LOGE("DataMgr is nullptr");
586         return false;
587     }
588     return dataMgr->GetBundleGids(bundleName, gids);
589 }
590 
GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids)591 bool BundleMgrHostImpl::GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector<int> &gids)
592 {
593     APP_LOGD("start GetBundleGidsByUid, bundleName : %{public}s, uid : %{public}d", bundleName.c_str(), uid);
594     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
595         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
596         APP_LOGE("verify token type failed");
597         return false;
598     }
599     auto dataMgr = GetDataMgrFromService();
600     if (dataMgr == nullptr) {
601         APP_LOGE("DataMgr is nullptr");
602         return false;
603     }
604     return dataMgr->GetBundleGidsByUid(bundleName, uid, gids);
605 }
606 
CheckIsSystemAppByUid(const int uid)607 bool BundleMgrHostImpl::CheckIsSystemAppByUid(const int uid)
608 {
609     APP_LOGD("start CheckIsSystemAppByUid, uid : %{public}d", uid);
610     if (!BundlePermissionMgr::IsSystemApp()) {
611         APP_LOGE("non-system app calling system api");
612         return false;
613     }
614     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
615         APP_LOGE("verify permission failed");
616         return false;
617     }
618     auto dataMgr = GetDataMgrFromService();
619     if (dataMgr == nullptr) {
620         APP_LOGE("DataMgr is nullptr");
621         return false;
622     }
623     return dataMgr->CheckIsSystemAppByUid(uid);
624 }
625 
GetBundleInfosByMetaData(const std::string & metaData,std::vector<BundleInfo> & bundleInfos)626 bool BundleMgrHostImpl::GetBundleInfosByMetaData(const std::string &metaData, std::vector<BundleInfo> &bundleInfos)
627 {
628     APP_LOGD("start GetBundleInfosByMetaData, metaData : %{public}s", metaData.c_str());
629     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
630         APP_LOGE("verify permission failed");
631         return false;
632     }
633     auto dataMgr = GetDataMgrFromService();
634     if (dataMgr == nullptr) {
635         APP_LOGE("DataMgr is nullptr");
636         return false;
637     }
638     return dataMgr->GetBundleInfosByMetaData(metaData, bundleInfos);
639 }
640 
QueryAbilityInfo(const Want & want,AbilityInfo & abilityInfo)641 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, AbilityInfo &abilityInfo)
642 {
643     return QueryAbilityInfo(want, GET_ABILITY_INFO_WITH_APPLICATION, Constants::UNSPECIFIED_USERID, abilityInfo);
644 }
645 
646 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)647 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId,
648     AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack)
649 {
650     if (!BundlePermissionMgr::IsSystemApp()) {
651         LOG_E(BMS_TAG_QUERY, "check is system app failed");
652         return false;
653     }
654     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
655         Constants::PERMISSION_GET_BUNDLE_INFO})) {
656         LOG_E(BMS_TAG_QUERY, "verify permission failed");
657         return false;
658     }
659     auto connectAbilityMgr = GetConnectAbilityMgrFromService();
660     if (connectAbilityMgr == nullptr) {
661         LOG_E(BMS_TAG_QUERY, "connectAbilityMgr is nullptr");
662         return false;
663     }
664     return connectAbilityMgr->QueryAbilityInfo(want, flags, userId, abilityInfo, callBack);
665 }
666 
SilentInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)667 bool BundleMgrHostImpl::SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
668 {
669     APP_LOGD("SilentInstall in");
670     auto connectMgr = GetConnectAbilityMgrFromService();
671     if (connectMgr == nullptr) {
672         APP_LOGE("connectMgr is nullptr");
673         return false;
674     }
675     return connectMgr->SilentInstall(want, userId, callBack);
676 }
677 
UpgradeAtomicService(const Want & want,int32_t userId)678 void BundleMgrHostImpl::UpgradeAtomicService(const Want &want, int32_t userId)
679 {
680     if (!BundlePermissionMgr::IsSystemApp()) {
681         APP_LOGE("check is system app failed");
682         return;
683     }
684 
685     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
686         APP_LOGE("verify permission failed");
687         return;
688     }
689     auto connectAbilityMgr = GetConnectAbilityMgrFromService();
690     if (connectAbilityMgr == nullptr) {
691         APP_LOGE("connectAbilityMgr is nullptr");
692         return;
693     }
694     connectAbilityMgr->UpgradeAtomicService(want, userId);
695 }
696 
CheckAbilityEnableInstall(const Want & want,int32_t missionId,int32_t userId,const sptr<IRemoteObject> & callback)697 bool BundleMgrHostImpl::CheckAbilityEnableInstall(
698     const Want &want, int32_t missionId, int32_t userId, const sptr<IRemoteObject> &callback)
699 {
700     if (!BundlePermissionMgr::IsSystemApp()) {
701         APP_LOGE("check is system app failed");
702         return false;
703     }
704 
705     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
706         APP_LOGE("verify permission failed");
707         return false;
708     }
709     auto elementName = want.GetElement();
710     if (elementName.GetDeviceID().empty() || elementName.GetBundleName().empty() ||
711         elementName.GetAbilityName().empty()) {
712         APP_LOGE("check ability install parameter is invalid");
713         return false;
714     }
715     auto bundleDistributedManager = DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleDistributedManager();
716     if (bundleDistributedManager == nullptr) {
717         APP_LOGE("bundleDistributedManager failed");
718         return false;
719     }
720     return bundleDistributedManager->CheckAbilityEnableInstall(want, missionId, userId, callback);
721 }
722 
ProcessPreload(const Want & want)723 bool BundleMgrHostImpl::ProcessPreload(const Want &want)
724 {
725     if (!BundlePermissionMgr::VerifyPreload(want)) {
726         APP_LOGE("ProcessPreload verify failed");
727         return false;
728     }
729     APP_LOGD("begin to process preload");
730     auto connectAbilityMgr = GetConnectAbilityMgrFromService();
731     if (connectAbilityMgr == nullptr) {
732         APP_LOGE("connectAbilityMgr is nullptr");
733         return false;
734     }
735     return connectAbilityMgr->ProcessPreload(want);
736 }
737 #endif
738 
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo)739 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo)
740 {
741     LOG_D(BMS_TAG_QUERY, "start QueryAbilityInfo, flags : %{public}d, userId : %{public}d", flags, userId);
742     if (!BundlePermissionMgr::IsSystemApp() &&
743         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
744         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
745         return true;
746     }
747     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
748         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
749         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
750         LOG_E(BMS_TAG_QUERY, "verify permission failed");
751         return false;
752     }
753     APP_LOGD("verify permission success, begin to QueryAbilityInfo");
754     auto dataMgr = GetDataMgrFromService();
755     if (dataMgr == nullptr) {
756         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
757         return false;
758     }
759     bool res = dataMgr->QueryAbilityInfo(want, flags, userId, abilityInfo);
760     if (!res) {
761         if (!IsAppLinking(flags) && isBrokerServiceExisted_) {
762             auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
763             return (bmsExtensionClient->QueryAbilityInfo(want, flags, userId, abilityInfo) == ERR_OK);
764         }
765     }
766     return res;
767 }
768 
QueryAbilityInfos(const Want & want,std::vector<AbilityInfo> & abilityInfos)769 bool BundleMgrHostImpl::QueryAbilityInfos(const Want &want, std::vector<AbilityInfo> &abilityInfos)
770 {
771     return QueryAbilityInfos(
772         want, GET_ABILITY_INFO_WITH_APPLICATION, Constants::UNSPECIFIED_USERID, abilityInfos);
773 }
774 
QueryAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)775 bool BundleMgrHostImpl::QueryAbilityInfos(
776     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
777 {
778     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
779     LOG_D(BMS_TAG_QUERY, "start QueryAbilityInfos, flags : %{public}d, userId : %{public}d", flags, userId);
780     if (!BundlePermissionMgr::IsSystemApp() &&
781         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
782         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
783         return true;
784     }
785     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
786         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
787         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
788         LOG_E(BMS_TAG_QUERY, "verify permission failed");
789         return false;
790     }
791     auto dataMgr = GetDataMgrFromService();
792     if (dataMgr == nullptr) {
793         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
794         return false;
795     }
796     dataMgr->QueryAbilityInfos(want, flags, userId, abilityInfos);
797     if (!IsAppLinking(flags) && isBrokerServiceExisted_) {
798         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
799         bmsExtensionClient->QueryAbilityInfos(want, flags, userId, abilityInfos);
800     }
801     return !abilityInfos.empty();
802 }
803 
QueryAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)804 ErrCode BundleMgrHostImpl::QueryAbilityInfosV9(
805     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
806 {
807     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
808     LOG_D(BMS_TAG_QUERY, "start QueryAbilityInfosV9, flags : %{public}d, userId : %{public}d", flags, userId);
809     if (!BundlePermissionMgr::IsSystemApp()) {
810         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
811         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
812     }
813     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
814         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
815         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
816         LOG_E(BMS_TAG_QUERY, "verify permission failed");
817         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
818     }
819     auto dataMgr = GetDataMgrFromService();
820     if (dataMgr == nullptr) {
821         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
822         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
823     }
824     auto res = dataMgr->QueryAbilityInfosV9(want, flags, userId, abilityInfos);
825     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
826     if (!IsAppLinking(flags) && isBrokerServiceExisted_ &&
827         bmsExtensionClient->QueryAbilityInfos(want, flags, userId, abilityInfos, true) == ERR_OK) {
828         LOG_D(BMS_TAG_QUERY, "query ability infos from bms extension successfully");
829         return ERR_OK;
830     }
831     return res;
832 }
833 
BatchQueryAbilityInfos(const std::vector<Want> & wants,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)834 ErrCode BundleMgrHostImpl::BatchQueryAbilityInfos(
835     const std::vector<Want> &wants, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
836 {
837     APP_LOGD("start BatchQueryAbilityInfos, flags : %{public}d, userId : %{public}d", flags, userId);
838     if (!BundlePermissionMgr::IsSystemApp()) {
839         APP_LOGE("non-system app calling system api");
840         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
841     }
842     bool callingPermission = BundlePermissionMgr::VerifyCallingPermissionsForAll(
843         { Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED, Constants::PERMISSION_GET_BUNDLE_INFO });
844     for (size_t i = 0; i < wants.size(); i++) {
845         if (!callingPermission && !BundlePermissionMgr::IsBundleSelfCalling(wants[i].GetElement().GetBundleName())) {
846             APP_LOGE("verify is bundle self calling failed");
847             return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
848         }
849     }
850     APP_LOGD("verify permission success, begin to BatchQueryAbilityInfos");
851     auto dataMgr = GetDataMgrFromService();
852     if (dataMgr == nullptr) {
853         APP_LOGE("DataMgr is nullptr");
854         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
855     }
856     auto res = dataMgr->BatchQueryAbilityInfos(wants, flags, userId, abilityInfos);
857     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
858     if (!IsAppLinking(flags) && isBrokerServiceExisted_ &&
859         bmsExtensionClient->BatchQueryAbilityInfos(wants, flags, userId, abilityInfos, true) == ERR_OK) {
860         APP_LOGD("query ability infos from bms extension successfully");
861         return ERR_OK;
862     }
863     return res;
864 }
865 
QueryLauncherAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)866 ErrCode BundleMgrHostImpl::QueryLauncherAbilityInfos(
867     const Want &want, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
868 {
869     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
870     LOG_D(BMS_TAG_QUERY, "start QueryLauncherAbilityInfos, userId : %{public}d", userId);
871     if (!BundlePermissionMgr::IsSystemApp()) {
872         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
873         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
874     }
875     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
876         LOG_E(BMS_TAG_QUERY, "verify permission failed");
877         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
878     }
879     APP_LOGD("verify permission success, begin to QueryLauncherAbilityInfos");
880     auto dataMgr = GetDataMgrFromService();
881     if (dataMgr == nullptr) {
882         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
883         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
884     }
885 
886     auto ret = dataMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos);
887     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
888     if (bmsExtensionClient->QueryLauncherAbility(want, userId, abilityInfos) == ERR_OK) {
889         LOG_D(BMS_TAG_QUERY, "query launcher ability infos from bms extension successfully");
890         return ERR_OK;
891     }
892     return ret;
893 }
894 
QueryAllAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)895 bool BundleMgrHostImpl::QueryAllAbilityInfos(const Want &want, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
896 {
897     LOG_D(BMS_TAG_QUERY, "start QueryAllAbilityInfos, userId : %{public}d", userId);
898     if (!BundlePermissionMgr::IsSystemApp() &&
899         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
900         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
901         return true;
902     }
903     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
904         LOG_E(BMS_TAG_QUERY, "verify permission failed");
905         return false;
906     }
907     APP_LOGD("verify permission success, begin to QueryAllAbilityInfos");
908     auto dataMgr = GetDataMgrFromService();
909     if (dataMgr == nullptr) {
910         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
911         return false;
912     }
913     bool res = dataMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos) == ERR_OK;
914     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
915     if (bmsExtensionClient->QueryLauncherAbility(want, userId, abilityInfos) == ERR_OK) {
916         LOG_D(BMS_TAG_QUERY, "query launcher ability infos from bms extension successfully");
917         return true;
918     }
919     return res;
920 }
921 
QueryAbilityInfoByUri(const std::string & abilityUri,AbilityInfo & abilityInfo)922 bool BundleMgrHostImpl::QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo)
923 {
924     LOG_D(BMS_TAG_QUERY, "start QueryAbilityInfoByUri, uri : %{private}s", abilityUri.c_str());
925     // API9 need to be system app, otherwise return empty data
926     if (!BundlePermissionMgr::IsSystemApp() &&
927         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
928         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
929         return true;
930     }
931     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
932         Constants::PERMISSION_GET_BUNDLE_INFO})) {
933         LOG_E(BMS_TAG_QUERY, "verify query permission failed");
934         return false;
935     }
936     auto dataMgr = GetDataMgrFromService();
937     if (dataMgr == nullptr) {
938         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
939         return false;
940     }
941     return dataMgr->QueryAbilityInfoByUri(abilityUri, Constants::UNSPECIFIED_USERID, abilityInfo);
942 }
943 
QueryAbilityInfosByUri(const std::string & abilityUri,std::vector<AbilityInfo> & abilityInfos)944 bool BundleMgrHostImpl::QueryAbilityInfosByUri(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos)
945 {
946     LOG_D(BMS_TAG_QUERY, "start QueryAbilityInfosByUri, uri : %{private}s", abilityUri.c_str());
947     // API9 need to be system app, otherwise return empty data
948     if (!BundlePermissionMgr::IsSystemApp() &&
949         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
950         return true;
951     }
952     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
953         LOG_E(BMS_TAG_QUERY, "verify permission failed");
954         return false;
955     }
956     auto dataMgr = GetDataMgrFromService();
957     if (dataMgr == nullptr) {
958         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
959         return false;
960     }
961     return dataMgr->QueryAbilityInfosByUri(abilityUri, abilityInfos);
962 }
963 
QueryAbilityInfoByUri(const std::string & abilityUri,int32_t userId,AbilityInfo & abilityInfo)964 bool BundleMgrHostImpl::QueryAbilityInfoByUri(
965     const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo)
966 {
967     LOG_D(BMS_TAG_QUERY, "start QueryAbilityInfoByUri, uri : %{private}s, userId : %{public}d",
968         abilityUri.c_str(), userId);
969     if (!BundlePermissionMgr::IsSystemApp() &&
970         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
971         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
972         return true;
973     }
974     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO,
975         Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED})) {
976         LOG_E(BMS_TAG_QUERY, "verify query permission failed");
977         return false;
978     }
979     auto dataMgr = GetDataMgrFromService();
980     if (dataMgr == nullptr) {
981         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
982         return false;
983     }
984     return dataMgr->QueryAbilityInfoByUri(abilityUri, userId, abilityInfo);
985 }
986 
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)987 bool BundleMgrHostImpl::QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos)
988 {
989     auto dataMgr = GetDataMgrFromService();
990     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
991         APP_LOGE("verify permission failed");
992         return false;
993     }
994     if (dataMgr == nullptr) {
995         APP_LOGE("DataMgr is nullptr");
996         return false;
997     }
998     return dataMgr->QueryKeepAliveBundleInfos(bundleInfos);
999 }
1000 
GetAbilityLabel(const std::string & bundleName,const std::string & abilityName)1001 std::string BundleMgrHostImpl::GetAbilityLabel(const std::string &bundleName, const std::string &abilityName)
1002 {
1003     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1004     APP_LOGD("start GetAbilityLabel, bundleName : %{public}s, abilityName : %{public}s",
1005         bundleName.c_str(), abilityName.c_str());
1006     // API9 need to be system app otherwise return empty data
1007     if (!BundlePermissionMgr::IsSystemApp() &&
1008         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
1009         APP_LOGD("non-system app calling system api");
1010         return Constants::EMPTY_STRING;
1011     }
1012     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
1013         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
1014         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
1015         APP_LOGE("verify permission failed");
1016         return Constants::EMPTY_STRING;
1017     }
1018     auto dataMgr = GetDataMgrFromService();
1019     if (dataMgr == nullptr) {
1020         APP_LOGE("DataMgr is nullptr");
1021         return Constants::EMPTY_STRING;
1022     }
1023     std::string label;
1024     ErrCode ret = dataMgr->GetAbilityLabel(bundleName, Constants::EMPTY_STRING, abilityName, label);
1025     if (ret != ERR_OK) {
1026         return Constants::EMPTY_STRING;
1027     }
1028     return label;
1029 }
1030 
GetAbilityLabel(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::string & label)1031 ErrCode BundleMgrHostImpl::GetAbilityLabel(const std::string &bundleName, const std::string &moduleName,
1032     const std::string &abilityName, std::string &label)
1033 {
1034     if (!BundlePermissionMgr::IsSystemApp()) {
1035         APP_LOGE("non-system app calling system api");
1036         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1037     }
1038     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
1039         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
1040         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
1041         APP_LOGE("verify permission failed");
1042         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1043     }
1044     auto dataMgr = GetDataMgrFromService();
1045     if (dataMgr == nullptr) {
1046         APP_LOGE("DataMgr is nullptr");
1047         return ERR_APPEXECFWK_SERVICE_NOT_READY;
1048     }
1049     return dataMgr->GetAbilityLabel(bundleName, moduleName, abilityName, label);
1050 }
1051 
GetBundleArchiveInfo(const std::string & hapFilePath,const BundleFlag flag,BundleInfo & bundleInfo)1052 bool BundleMgrHostImpl::GetBundleArchiveInfo(
1053     const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo)
1054 {
1055     return GetBundleArchiveInfo(hapFilePath, static_cast<int32_t>(flag), bundleInfo);
1056 }
1057 
GetBundleArchiveInfo(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)1058 bool BundleMgrHostImpl::GetBundleArchiveInfo(
1059     const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
1060 {
1061     APP_LOGD("start GetBundleArchiveInfo, hapFilePath : %{private}s, flags : %{public}d",
1062         hapFilePath.c_str(), flags);
1063     if (!BundlePermissionMgr::IsSystemApp() &&
1064         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
1065         APP_LOGD("non-system app calling system api");
1066         return true;
1067     }
1068     if (hapFilePath.find(ServiceConstants::RELATIVE_PATH) != std::string::npos) {
1069         APP_LOGE("invalid hapFilePath");
1070         return false;
1071     }
1072     if (hapFilePath.find(ServiceConstants::SANDBOX_DATA_PATH) == std::string::npos) {
1073         std::string realPath;
1074         auto ret = BundleUtil::CheckFilePath(hapFilePath, realPath);
1075         if (ret != ERR_OK) {
1076             APP_LOGE("GetBundleArchiveInfo file path %{private}s invalid", hapFilePath.c_str());
1077             return false;
1078         }
1079 
1080         InnerBundleInfo info;
1081         BundleParser bundleParser;
1082         ret = bundleParser.Parse(realPath, info);
1083         if (ret != ERR_OK) {
1084             APP_LOGE("parse bundle info failed, error: %{public}d", ret);
1085             return false;
1086         }
1087         APP_LOGD("verify permission success, begin to GetBundleArchiveInfo");
1088         SetProvisionInfoToInnerBundleInfo(realPath, info);
1089         info.GetBundleInfo(flags, bundleInfo, ServiceConstants::NOT_EXIST_USERID);
1090         return true;
1091     } else {
1092         return GetBundleArchiveInfoBySandBoxPath(hapFilePath, flags, bundleInfo) == ERR_OK;
1093     }
1094 }
1095 
GetBundleArchiveInfoV9(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)1096 ErrCode BundleMgrHostImpl::GetBundleArchiveInfoV9(
1097     const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
1098 {
1099     APP_LOGD("start GetBundleArchiveInfoV9, hapFilePath : %{private}s, flags : %{public}d",
1100         hapFilePath.c_str(), flags);
1101     if (!BundlePermissionMgr::IsSystemApp()) {
1102         APP_LOGE("non-system app calling system api");
1103         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1104     }
1105     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1106         APP_LOGE("verify permission failed");
1107         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1108     }
1109     if (hapFilePath.find(ServiceConstants::RELATIVE_PATH) != std::string::npos) {
1110         APP_LOGD("invalid hapFilePath");
1111         return ERR_BUNDLE_MANAGER_INVALID_HAP_PATH;
1112     }
1113     if (hapFilePath.find(ServiceConstants::SANDBOX_DATA_PATH) == 0) {
1114         APP_LOGD("sandbox path");
1115         return GetBundleArchiveInfoBySandBoxPath(hapFilePath, flags, bundleInfo, true);
1116     }
1117     std::string realPath;
1118     ErrCode ret = BundleUtil::CheckFilePath(hapFilePath, realPath);
1119     if (ret != ERR_OK) {
1120         APP_LOGE("GetBundleArchiveInfoV9 file path %{private}s invalid", hapFilePath.c_str());
1121         return ERR_BUNDLE_MANAGER_INVALID_HAP_PATH;
1122     }
1123     InnerBundleInfo info;
1124     BundleParser bundleParser;
1125     ret = bundleParser.Parse(realPath, info);
1126     if (ret != ERR_OK) {
1127         APP_LOGE("parse bundle info failed, error: %{public}d", ret);
1128         return ERR_BUNDLE_MANAGER_INVALID_HAP_PATH;
1129     }
1130     if ((static_cast<uint32_t>(flags) & static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO))
1131         == static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)) {
1132         SetProvisionInfoToInnerBundleInfo(realPath, info);
1133     }
1134     info.GetBundleInfoV9(flags, bundleInfo, ServiceConstants::NOT_EXIST_USERID);
1135     return ERR_OK;
1136 }
1137 
GetBundleArchiveInfoBySandBoxPath(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo,bool fromV9)1138 ErrCode BundleMgrHostImpl::GetBundleArchiveInfoBySandBoxPath(const std::string &hapFilePath,
1139     int32_t flags, BundleInfo &bundleInfo, bool fromV9)
1140 {
1141     std::string bundleName;
1142     int32_t apiVersion = fromV9 ? Constants::INVALID_API_VERSION : ServiceConstants::API_VERSION_NINE;
1143     if (!BundlePermissionMgr::IsSystemApp() && !BundlePermissionMgr::VerifyCallingBundleSdkVersion(apiVersion)) {
1144         APP_LOGE("non-system app calling system api");
1145         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1146     }
1147     if (!ObtainCallingBundleName(bundleName)) {
1148         APP_LOGE("get calling bundleName failed");
1149         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1150     }
1151     std::string hapRealPath;
1152     if (!BundleUtil::RevertToRealPath(hapFilePath, bundleName, hapRealPath)) {
1153         APP_LOGE("GetBundleArchiveInfo RevertToRealPath failed");
1154         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1155     }
1156     std::string tempHapPath = ServiceConstants::BUNDLE_MANAGER_SERVICE_PATH +
1157         ServiceConstants::PATH_SEPARATOR + std::to_string(BundleUtil::GetCurrentTimeNs());
1158     if (!BundleUtil::CreateDir(tempHapPath)) {
1159         APP_LOGE("GetBundleArchiveInfo make temp dir failed");
1160         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1161     }
1162     std::string hapName = hapFilePath.substr(hapFilePath.find_last_of("//") + 1);
1163     std::string tempHapFile = tempHapPath + ServiceConstants::PATH_SEPARATOR + hapName;
1164     if (InstalldClient::GetInstance()->CopyFile(hapRealPath, tempHapFile) != ERR_OK) {
1165         APP_LOGE("GetBundleArchiveInfo copy hap file failed");
1166         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1167     }
1168     std::string realPath;
1169     auto ret = BundleUtil::CheckFilePath(tempHapFile, realPath);
1170     if (ret != ERR_OK) {
1171         APP_LOGE("CheckFilePath failed");
1172         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1173     }
1174     InnerBundleInfo info;
1175     BundleParser bundleParser;
1176     ret = bundleParser.Parse(realPath, info);
1177     if (ret != ERR_OK) {
1178         APP_LOGE("parse bundle info failed, error: %{public}d", ret);
1179         BundleUtil::DeleteDir(tempHapPath);
1180         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1181     }
1182     SetProvisionInfoToInnerBundleInfo(realPath, info);
1183     BundleUtil::DeleteDir(tempHapPath);
1184     if (fromV9) {
1185         info.GetBundleInfoV9(flags, bundleInfo, ServiceConstants::NOT_EXIST_USERID);
1186     } else {
1187         info.GetBundleInfo(flags, bundleInfo, ServiceConstants::NOT_EXIST_USERID);
1188     }
1189     return ERR_OK;
1190 }
1191 
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)1192 bool BundleMgrHostImpl::GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo)
1193 {
1194     APP_LOGD("start GetHapModuleInfo");
1195     return GetHapModuleInfo(abilityInfo, Constants::UNSPECIFIED_USERID, hapModuleInfo);
1196 }
1197 
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)1198 bool BundleMgrHostImpl::GetHapModuleInfo(const AbilityInfo &abilityInfo, int32_t userId, HapModuleInfo &hapModuleInfo)
1199 {
1200     APP_LOGD("start GetHapModuleInfo with bundleName %{public}s and userId: %{public}d",
1201         abilityInfo.bundleName.c_str(), userId);
1202     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_HAP_MODULE_INFO);
1203     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
1204     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
1205         !BundlePermissionMgr::IsBundleSelfCalling(abilityInfo.bundleName)) {
1206         APP_LOGE("verify permission failed");
1207         return false;
1208     }
1209     if (abilityInfo.bundleName.empty() || abilityInfo.package.empty()) {
1210         APP_LOGE("fail to GetHapModuleInfo due to params empty");
1211         return false;
1212     }
1213     auto dataMgr = GetDataMgrFromService();
1214     if (dataMgr == nullptr) {
1215         APP_LOGE("DataMgr is nullptr");
1216         return false;
1217     }
1218     return dataMgr->GetHapModuleInfo(abilityInfo, hapModuleInfo, userId);
1219 }
1220 
GetLaunchWantForBundle(const std::string & bundleName,Want & want,int32_t userId)1221 ErrCode BundleMgrHostImpl::GetLaunchWantForBundle(const std::string &bundleName, Want &want, int32_t userId)
1222 {
1223     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1224     APP_LOGD("start GetLaunchWantForBundle, bundleName : %{public}s", bundleName.c_str());
1225     if (!BundlePermissionMgr::IsSystemApp() &&
1226         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
1227         APP_LOGE("non-system app calling system api");
1228         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1229     }
1230     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1231         APP_LOGE("verify permission failed");
1232         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1233     }
1234 
1235     APP_LOGD("verify permission success, begin to GetLaunchWantForBundle");
1236     auto dataMgr = GetDataMgrFromService();
1237     if (dataMgr == nullptr) {
1238         APP_LOGE("DataMgr is nullptr");
1239         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1240     }
1241 
1242     return dataMgr->GetLaunchWantForBundle(bundleName, want, userId);
1243 }
1244 
GetPermissionDef(const std::string & permissionName,PermissionDef & permissionDef)1245 ErrCode BundleMgrHostImpl::GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef)
1246 {
1247     if (!BundlePermissionMgr::IsSystemApp()) {
1248         APP_LOGE("non-system app calling system api");
1249         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1250     }
1251     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1252         APP_LOGE("verify GET_BUNDLE_INFO_PRIVILEGED failed");
1253         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1254     }
1255     if (permissionName.empty()) {
1256         APP_LOGW("fail to GetPermissionDef due to params empty");
1257         return ERR_BUNDLE_MANAGER_QUERY_PERMISSION_DEFINE_FAILED;
1258     }
1259     return BundlePermissionMgr::GetPermissionDef(permissionName, permissionDef);
1260 }
1261 
CleanBundleCacheFilesAutomatic(uint64_t cacheSize)1262 ErrCode BundleMgrHostImpl::CleanBundleCacheFilesAutomatic(uint64_t cacheSize)
1263 {
1264     if (cacheSize == 0) {
1265         APP_LOGE("parameter error, cache size must be greater than 0");
1266         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
1267     }
1268 
1269     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_REMOVECACHEFILE)) {
1270         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES permission denied");
1271         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1272     }
1273 
1274     // Get current active userId
1275     int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
1276     APP_LOGI("current active userId is %{public}d", currentUserId);
1277     if (currentUserId == Constants::INVALID_USERID) {
1278         APP_LOGE("currentUserId %{public}d is invalid", currentUserId);
1279         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1280     }
1281 
1282     // Get apps use time under the current active user
1283     int64_t startTime = 0;
1284     int64_t endTime = BundleUtil::GetCurrentTimeMs();
1285     const int32_t PERIOD_ANNUALLY = 4; // 4 is the number of the period ANN
1286     uint32_t notRunningSum = 0; // The total amount of application that is not running
1287 #ifdef DEVICE_USAGE_STATISTICS_ENABLED
1288     std::vector<DeviceUsageStats::BundleActivePackageStats> useStats;
1289     DeviceUsageStats::BundleActiveClient::GetInstance().QueryBundleStatsInfoByInterval(
1290         useStats, PERIOD_ANNUALLY, startTime, endTime, currentUserId);
1291 
1292     if (useStats.empty()) {
1293         APP_LOGE("useStats under the current active user is empty");
1294         return ERR_BUNDLE_MANAGER_DEVICE_USAGE_STATS_EMPTY;
1295     }
1296 
1297     // Sort apps use time from small to large under the current active user
1298     std::sort(useStats.begin(), useStats.end(),
1299         [](DeviceUsageStats::BundleActivePackageStats a,
1300         DeviceUsageStats::BundleActivePackageStats b) {
1301             return a.totalInFrontTime_ < b.totalInFrontTime_;
1302         });
1303 
1304     // Get all running apps
1305     sptr<IAppMgr> appMgrProxy =
1306         iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
1307     if (appMgrProxy == nullptr) {
1308         APP_LOGE("fail to find the app mgr service to check app is running");
1309         return ERR_BUNDLE_MANAGER_GET_SYSTEM_ABILITY_FAILED;
1310     }
1311 
1312     std::vector<RunningProcessInfo> runningList;
1313     int result = appMgrProxy->GetAllRunningProcesses(runningList);
1314     if (result != ERR_OK) {
1315         APP_LOGE("GetAllRunningProcesses failed");
1316         return ERR_BUNDLE_MANAGER_GET_ALL_RUNNING_PROCESSES_FAILED;
1317     }
1318 
1319     std::unordered_set<std::string> runningSet;
1320     for (const auto &info : runningList) {
1321         runningSet.insert(info.bundleNames.begin(), info.bundleNames.end());
1322     }
1323 
1324     uint64_t cleanCacheSum = 0; // The total amount of application cache currently cleaned
1325     for (auto useStat : useStats) {
1326         if (runningSet.find(useStat.bundleName_) == runningSet.end()) {
1327             notRunningSum++;
1328             uint64_t cleanCacheSize = 0; // The cache size of a single application cleaned up
1329             ErrCode ret = CleanBundleCacheFilesGetCleanSize(useStat.bundleName_, currentUserId, cleanCacheSize);
1330             if (ret != ERR_OK) {
1331                 return ret;
1332             }
1333             APP_LOGI("bundleName : %{public}s, cleanCacheSize: %{public}" PRIu64 "",
1334                 useStat.bundleName_.c_str(), cleanCacheSize);
1335             cleanCacheSum += cleanCacheSize;
1336             if (cleanCacheSum >= cacheSize) {
1337                 return ERR_OK;
1338             }
1339         }
1340     }
1341 #endif
1342     if (notRunningSum == 0) {
1343         APP_LOGE("All apps are running under the current active user");
1344         return ERR_BUNDLE_MANAGER_ALL_BUNDLES_ARE_RUNNING;
1345     }
1346 
1347     return ERR_OK;
1348 }
1349 
CleanBundleCacheFilesGetCleanSize(const std::string & bundleName,int32_t userId,uint64_t & cleanCacheSize)1350 ErrCode BundleMgrHostImpl::CleanBundleCacheFilesGetCleanSize(const std::string &bundleName,
1351     int32_t userId, uint64_t &cleanCacheSize)
1352 {
1353     APP_LOGI("start CleanBundleCacheFilesGetCleanSize, bundleName : %{public}s, userId : %{public}d",
1354         bundleName.c_str(), userId);
1355 
1356     if (!BundlePermissionMgr::IsSystemApp()) {
1357         APP_LOGE("non-system app calling system api");
1358         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1359     }
1360 
1361     if (userId < 0) {
1362         APP_LOGE("userId is invalid");
1363         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1364         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1365     }
1366 
1367     if (bundleName.empty()) {
1368         APP_LOGE("the bundleName empty");
1369         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1370         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
1371     }
1372 
1373     ApplicationInfo applicationInfo;
1374     auto dataMgr = GetDataMgrFromService();
1375     if (dataMgr == nullptr) {
1376         APP_LOGE("DataMgr is nullptr");
1377         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1378         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
1379     }
1380 
1381     auto ret = dataMgr->GetApplicationInfoWithResponseId(bundleName,
1382         static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE), userId, applicationInfo);
1383     if (ret != ERR_OK) {
1384         APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
1385         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1386         return ret;
1387     }
1388 
1389     if (!applicationInfo.userDataClearable) {
1390         APP_LOGE("can not clean cacheFiles of %{public}s due to userDataClearable is false", bundleName.c_str());
1391         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1392         return ERR_BUNDLE_MANAGER_CAN_NOT_CLEAR_USER_DATA;
1393     }
1394 
1395     CleanBundleCacheTaskGetCleanSize(bundleName, userId, cleanCacheSize);
1396     return ERR_OK;
1397 }
1398 
CleanBundleCacheTaskGetCleanSize(const std::string & bundleName,int32_t userId,uint64_t & cleanCacheSize)1399 void BundleMgrHostImpl::CleanBundleCacheTaskGetCleanSize(const std::string &bundleName,
1400     int32_t userId, uint64_t &cleanCacheSize)
1401 {
1402     std::vector<std::string> rootDir;
1403     for (const auto &el : ServiceConstants::BUNDLE_EL) {
1404         std::string dataDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + el +
1405             ServiceConstants::PATH_SEPARATOR + std::to_string(userId) + ServiceConstants::BASE + bundleName;
1406         rootDir.emplace_back(dataDir);
1407     }
1408 
1409     std::vector<std::string> caches;
1410     for (const auto &st : rootDir) {
1411         std::vector<std::string> cache;
1412         if (InstalldClient::GetInstance()->GetBundleCachePath(st, cache) != ERR_OK) {
1413             APP_LOGE("GetBundleCachePath failed, path: %{public}s", st.c_str());
1414         }
1415         std::copy(cache.begin(), cache.end(), std::back_inserter(caches));
1416     }
1417 
1418     bool succeed = true;
1419     if (!caches.empty()) {
1420         for (const auto& cache : caches) {
1421             int64_t cacheSize = InstalldClient::GetInstance()->GetDiskUsage(cache, true);
1422             ErrCode ret = InstalldClient::GetInstance()->CleanBundleDataDir(cache);
1423             if (ret != ERR_OK) {
1424                 APP_LOGE("CleanBundleDataDir failed, path: %{public}s", cache.c_str());
1425                 succeed = false;
1426             }
1427             cleanCacheSize += static_cast<uint64_t>(cacheSize);
1428         }
1429     }
1430     EventReport::SendCleanCacheSysEvent(bundleName, userId, true, !succeed);
1431     APP_LOGI("CleanBundleCacheFiles with succeed %{public}d", succeed);
1432     InnerBundleUserInfo innerBundleUserInfo;
1433     if (!this->GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1434         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
1435         return;
1436     }
1437     NotifyBundleEvents installRes = {
1438         .bundleName = bundleName,
1439         .resultCode = ERR_OK,
1440         .type = NotifyType::BUNDLE_CACHE_CLEARED,
1441         .uid = innerBundleUserInfo.uid,
1442         .accessTokenId = innerBundleUserInfo.accessTokenId
1443     };
1444     NotifyBundleStatus(installRes);
1445 }
1446 
CheckAppIndex(const std::string & bundleName,int32_t userId,int32_t appIndex)1447 bool BundleMgrHostImpl::CheckAppIndex(const std::string &bundleName, int32_t userId, int32_t appIndex)
1448 {
1449     if (appIndex == 0) {
1450         return true;
1451     }
1452     if (appIndex < 0) {
1453         APP_LOGE("appIndex is invalid");
1454         return false;
1455     }
1456     auto dataMgr = GetDataMgrFromService();
1457     if (dataMgr == nullptr) {
1458         APP_LOGE("DataMgr is nullptr");
1459         return false;
1460     }
1461     std::vector<int32_t> appIndexes = dataMgr->GetCloneAppIndexes(bundleName, userId);
1462     bool isAppIndexValid = std::find(appIndexes.cbegin(), appIndexes.cend(), appIndex) == appIndexes.cend();
1463     if (isAppIndexValid) {
1464         APP_LOGE("appIndex is not in the installed appIndexes range.");
1465         return false;
1466     }
1467     return true;
1468 }
1469 
CleanBundleCacheFiles(const std::string & bundleName,const sptr<ICleanCacheCallback> cleanCacheCallback,int32_t userId,int32_t appIndex)1470 ErrCode BundleMgrHostImpl::CleanBundleCacheFiles(
1471     const std::string &bundleName, const sptr<ICleanCacheCallback> cleanCacheCallback,
1472     int32_t userId, int32_t appIndex)
1473 {
1474     if (userId == Constants::UNSPECIFIED_USERID) {
1475         userId = BundleUtil::GetUserIdByCallingUid();
1476     }
1477     APP_LOGD("start -n %{public}s -u %{public}d -i %{public}d", bundleName.c_str(), userId, appIndex);
1478     if (!BundlePermissionMgr::IsSystemApp()) {
1479         APP_LOGE("non-system app calling system api");
1480         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1481     }
1482     if (userId < 0) {
1483         APP_LOGE("userId is invalid");
1484         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1485         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1486     }
1487 
1488     if (!CheckAppIndex(bundleName, userId, appIndex)) {
1489         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1490         return ERR_APPEXECFWK_APP_INDEX_OUT_OF_RANGE;
1491     }
1492 
1493     if (bundleName.empty() || !cleanCacheCallback) {
1494         APP_LOGE("the cleanCacheCallback is nullptr or bundleName empty");
1495         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1496         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
1497     }
1498 
1499     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_REMOVECACHEFILE) &&
1500         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
1501         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES permission denied");
1502         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1503         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1504     }
1505 
1506     if (isBrokerServiceExisted_ && !IsBundleExist(bundleName)) {
1507         return ClearCache(bundleName, cleanCacheCallback, userId);
1508     }
1509 
1510     ApplicationInfo applicationInfo;
1511     auto dataMgr = GetDataMgrFromService();
1512     if (dataMgr == nullptr) {
1513         APP_LOGE("DataMgr is nullptr");
1514         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1515         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
1516     }
1517 
1518     auto ret = dataMgr->GetApplicationInfoWithResponseId(bundleName,
1519         static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE), userId, applicationInfo);
1520     if (ret != ERR_OK) {
1521         APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
1522         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1523         return ret;
1524     }
1525 
1526     if (!applicationInfo.userDataClearable) {
1527         APP_LOGE("can not clean cacheFiles of %{public}s due to userDataClearable is false", bundleName.c_str());
1528         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, true, true);
1529         return ERR_BUNDLE_MANAGER_CAN_NOT_CLEAR_USER_DATA;
1530     }
1531 
1532     CleanBundleCacheTask(bundleName, cleanCacheCallback, dataMgr, userId, appIndex);
1533     return ERR_OK;
1534 }
1535 
CleanBundleCacheTask(const std::string & bundleName,const sptr<ICleanCacheCallback> cleanCacheCallback,const std::shared_ptr<BundleDataMgr> & dataMgr,int32_t userId,int32_t appIndex)1536 void BundleMgrHostImpl::CleanBundleCacheTask(const std::string &bundleName,
1537     const sptr<ICleanCacheCallback> cleanCacheCallback,
1538     const std::shared_ptr<BundleDataMgr> &dataMgr,
1539     int32_t userId, int32_t appIndex)
1540 {
1541     std::vector<std::string> rootDir;
1542     std::string suffixName = bundleName;
1543     if (appIndex > 0) {
1544         suffixName = BundleCloneCommonHelper::GetCloneDataDir(bundleName, appIndex);
1545     }
1546     std::vector<std::string> bundleEls = ServiceConstants::BUNDLE_EL;
1547     bundleEls.push_back(ServiceConstants::DIR_EL5);
1548     for (const auto &el : bundleEls) {
1549         std::string dataDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + el +
1550             ServiceConstants::PATH_SEPARATOR + std::to_string(userId) + ServiceConstants::BASE + suffixName;
1551         rootDir.emplace_back(dataDir);
1552     }
1553 
1554     auto cleanCache = [bundleName, userId, rootDir, dataMgr, cleanCacheCallback, appIndex, this]() {
1555         std::vector<std::string> caches;
1556         for (const auto &st : rootDir) {
1557             std::vector<std::string> cache;
1558             if (InstalldClient::GetInstance()->GetBundleCachePath(st, cache) != ERR_OK) {
1559                 APP_LOGW("GetBundleCachePath failed, path: %{public}s", st.c_str());
1560             }
1561             std::copy(cache.begin(), cache.end(), std::back_inserter(caches));
1562         }
1563 
1564         std::string shaderCachePath;
1565         shaderCachePath.append(ServiceConstants::SHADER_CACHE_PATH).append(bundleName);
1566         caches.push_back(shaderCachePath);
1567 
1568         bool succeed = true;
1569         if (!caches.empty()) {
1570             for (const auto& cache : caches) {
1571                 ErrCode ret = InstalldClient::GetInstance()->CleanBundleDataDir(cache);
1572                 if (ret != ERR_OK) {
1573                     APP_LOGE("CleanBundleDataDir failed, path: %{private}s", cache.c_str());
1574                     succeed = false;
1575                 }
1576             }
1577         }
1578         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, !succeed);
1579         APP_LOGD("CleanBundleCacheFiles with succeed %{public}d", succeed);
1580         cleanCacheCallback->OnCleanCacheFinished(succeed);
1581         InnerBundleUserInfo innerBundleUserInfo;
1582         if (!this->GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1583             APP_LOGW("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
1584             return;
1585         }
1586         NotifyBundleEvents installRes;
1587         if (appIndex > 0) {
1588             std::map<std::string, InnerBundleCloneInfo> cloneInfos = innerBundleUserInfo.cloneInfos;
1589             auto cloneInfoIter = cloneInfos.find(std::to_string(appIndex));
1590             if (cloneInfoIter == cloneInfos.end()) {
1591                 APP_LOGW("Get calling userCloneInfo in bundle(%{public}s) failed, appIndex:%{public}d",
1592                     bundleName.c_str(), appIndex);
1593                 return;
1594             }
1595             int32_t uid = cloneInfoIter->second.uid;
1596             installRes = {
1597                 .bundleName = bundleName,
1598                 .resultCode = ERR_OK,
1599                 .type = NotifyType::BUNDLE_CACHE_CLEARED,
1600                 .uid = uid,
1601                 .accessTokenId = innerBundleUserInfo.accessTokenId,
1602                 .appIndex = appIndex
1603             };
1604             NotifyBundleStatus(installRes);
1605             return;
1606         }
1607         installRes = {
1608             .bundleName = bundleName,
1609             .resultCode = ERR_OK,
1610             .type = NotifyType::BUNDLE_CACHE_CLEARED,
1611             .uid = innerBundleUserInfo.uid,
1612             .accessTokenId = innerBundleUserInfo.accessTokenId
1613         };
1614         NotifyBundleStatus(installRes);
1615     };
1616     ffrt::submit(cleanCache);
1617 }
1618 
CleanBundleDataFiles(const std::string & bundleName,const int userId,const int appIndex)1619 bool BundleMgrHostImpl::CleanBundleDataFiles(const std::string &bundleName, const int userId, const int appIndex)
1620 {
1621     APP_LOGD("start CleanBundleDataFiles, bundleName : %{public}s, userId:%{public}d, appIndex:%{public}d",
1622         bundleName.c_str(), userId, appIndex);
1623     if (!BundlePermissionMgr::IsSystemApp()) {
1624         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES system api denied");
1625         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1626         return false;
1627     }
1628     if (bundleName.empty() || userId < 0) {
1629         APP_LOGE("the  bundleName empty or invalid userid");
1630         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1631         return false;
1632     }
1633     if (!CheckAppIndex(bundleName, userId, appIndex)) {
1634         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1635         return false;
1636     }
1637     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_REMOVECACHEFILE)) {
1638         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES permission denied");
1639         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1640         return false;
1641     }
1642     if (isBrokerServiceExisted_ && !IsBundleExist(bundleName)) {
1643         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
1644         ErrCode ret = bmsExtensionClient->ClearData(bundleName, userId);
1645         APP_LOGI("ret : %{public}d", ret);
1646         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, ret != ERR_OK);
1647         return ret == ERR_OK;
1648     }
1649     ApplicationInfo applicationInfo;
1650     auto dataMgr = GetDataMgrFromService();
1651     if (dataMgr == nullptr || dataMgr->GetApplicationInfoV9(bundleName,
1652         static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE),
1653         userId, applicationInfo, appIndex) != ERR_OK) {
1654         APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
1655         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1656         return false;
1657     }
1658 
1659     if (!applicationInfo.userDataClearable) {
1660         APP_LOGE("can not clean dataFiles of %{public}s due to userDataClearable is false", bundleName.c_str());
1661         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1662         return false;
1663     }
1664     InnerBundleUserInfo innerBundleUserInfo;
1665     if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1666         APP_LOGE("%{public}s, userId:%{public}d, GetBundleUserInfo failed", bundleName.c_str(), userId);
1667         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1668         return false;
1669     }
1670     if (BundlePermissionMgr::ClearUserGrantedPermissionState(applicationInfo.accessTokenId)) {
1671         APP_LOGE("%{public}s, ClearUserGrantedPermissionState failed", bundleName.c_str());
1672         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1673         return false;
1674     }
1675     if (InstalldClient::GetInstance()->CleanBundleDataDirByName(bundleName, userId, appIndex) != ERR_OK) {
1676         APP_LOGE("%{public}s, CleanBundleDataDirByName failed", bundleName.c_str());
1677         EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, true);
1678         return false;
1679     }
1680     EventReport::SendCleanCacheSysEventWithIndex(bundleName, userId, appIndex, false, false);
1681     return true;
1682 }
1683 
RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)1684 bool BundleMgrHostImpl::RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
1685 {
1686     APP_LOGD("start RegisterBundleStatusCallback");
1687     if ((!bundleStatusCallback) || (bundleStatusCallback->GetBundleName().empty())) {
1688         APP_LOGE("the bundleStatusCallback is nullptr or bundleName empty");
1689         return false;
1690     }
1691     // check permission
1692     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::LISTEN_BUNDLE_CHANGE)) {
1693         APP_LOGE("register bundle status callback failed due to lack of permission");
1694         return false;
1695     }
1696 
1697     auto dataMgr = GetDataMgrFromService();
1698     if (dataMgr == nullptr) {
1699         APP_LOGE("DataMgr is nullptr");
1700         return false;
1701     }
1702     return dataMgr->RegisterBundleStatusCallback(bundleStatusCallback);
1703 }
1704 
RegisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)1705 bool BundleMgrHostImpl::RegisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
1706 {
1707     APP_LOGD("begin to RegisterBundleEventCallback");
1708     if (bundleEventCallback == nullptr) {
1709         APP_LOGE("bundleEventCallback is null");
1710         return false;
1711     }
1712     auto uid = IPCSkeleton::GetCallingUid();
1713     if (uid != Constants::FOUNDATION_UID && uid != Constants::CODE_PROTECT_UID) {
1714         APP_LOGE("verify calling uid failed, uid : %{public}d", uid);
1715         return false;
1716     }
1717     auto dataMgr = GetDataMgrFromService();
1718     if (dataMgr == nullptr) {
1719         APP_LOGE("DataMgr is nullptr");
1720         return false;
1721     }
1722     return dataMgr->RegisterBundleEventCallback(bundleEventCallback);
1723 }
1724 
UnregisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)1725 bool BundleMgrHostImpl::UnregisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
1726 {
1727     APP_LOGD("begin to UnregisterBundleEventCallback");
1728     if (bundleEventCallback == nullptr) {
1729         APP_LOGE("bundleEventCallback is null");
1730         return false;
1731     }
1732     auto uid = IPCSkeleton::GetCallingUid();
1733     if (uid != Constants::FOUNDATION_UID) {
1734         APP_LOGE("verify calling uid failed, uid : %{public}d", uid);
1735         return false;
1736     }
1737     auto dataMgr = GetDataMgrFromService();
1738     if (dataMgr == nullptr) {
1739         APP_LOGE("DataMgr is nullptr");
1740         return false;
1741     }
1742     return dataMgr->UnregisterBundleEventCallback(bundleEventCallback);
1743 }
1744 
ClearBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)1745 bool BundleMgrHostImpl::ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
1746 {
1747     APP_LOGD("start ClearBundleStatusCallback");
1748     if (!bundleStatusCallback) {
1749         APP_LOGE("the bundleStatusCallback is nullptr");
1750         return false;
1751     }
1752 
1753     // check permission
1754     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::LISTEN_BUNDLE_CHANGE)) {
1755         APP_LOGE("register bundle status callback failed due to lack of permission");
1756         return false;
1757     }
1758 
1759     auto dataMgr = GetDataMgrFromService();
1760     if (dataMgr == nullptr) {
1761         APP_LOGE("DataMgr is nullptr");
1762         return false;
1763     }
1764     return dataMgr->ClearBundleStatusCallback(bundleStatusCallback);
1765 }
1766 
UnregisterBundleStatusCallback()1767 bool BundleMgrHostImpl::UnregisterBundleStatusCallback()
1768 {
1769     APP_LOGD("start UnregisterBundleStatusCallback");
1770     // check permission
1771     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::LISTEN_BUNDLE_CHANGE)) {
1772         APP_LOGE("register bundle status callback failed due to lack of permission");
1773         return false;
1774     }
1775 
1776     auto dataMgr = GetDataMgrFromService();
1777     if (dataMgr == nullptr) {
1778         APP_LOGE("DataMgr is nullptr");
1779         return false;
1780     }
1781     return dataMgr->UnregisterBundleStatusCallback();
1782 }
1783 
CompileProcessAOT(const std::string & bundleName,const std::string & compileMode,bool isAllBundle,std::vector<std::string> & compileResults)1784 ErrCode BundleMgrHostImpl::CompileProcessAOT(const std::string &bundleName, const std::string &compileMode,
1785     bool isAllBundle, std::vector<std::string> &compileResults)
1786 {
1787     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1788         APP_LOGE("verify permission failed");
1789         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1790     }
1791     return AOTHandler::GetInstance().HandleCompile(bundleName, compileMode, isAllBundle, compileResults);
1792 }
1793 
CompileReset(const std::string & bundleName,bool isAllBundle)1794 ErrCode BundleMgrHostImpl::CompileReset(const std::string &bundleName, bool isAllBundle)
1795 {
1796     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1797         APP_LOGE("verify permission failed");
1798         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1799     }
1800     AOTHandler::GetInstance().HandleResetAOT(bundleName, isAllBundle);
1801     return ERR_OK;
1802 }
1803 
CopyAp(const std::string & bundleName,bool isAllBundle,std::vector<std::string> & results)1804 ErrCode BundleMgrHostImpl::CopyAp(const std::string &bundleName, bool isAllBundle, std::vector<std::string> &results)
1805 {
1806     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1807         APP_LOGE("verify permission failed");
1808         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1809     }
1810     return AOTHandler::GetInstance().HandleCopyAp(bundleName, isAllBundle, results);
1811 }
1812 
DumpInfos(const DumpFlag flag,const std::string & bundleName,int32_t userId,std::string & result)1813 bool BundleMgrHostImpl::DumpInfos(
1814     const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result)
1815 {
1816     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1817         APP_LOGE("verify permission failed");
1818         return false;
1819     }
1820     bool ret = false;
1821     switch (flag) {
1822         case DumpFlag::DUMP_BUNDLE_LIST: {
1823             ret = DumpAllBundleInfoNames(userId, result);
1824             break;
1825         }
1826         case DumpFlag::DUMP_BUNDLE_INFO: {
1827             ret = DumpBundleInfo(bundleName, userId, result);
1828             break;
1829         }
1830         case DumpFlag::DUMP_SHORTCUT_INFO: {
1831             ret = DumpShortcutInfo(bundleName, userId, result);
1832             break;
1833         }
1834         default:
1835             APP_LOGE("dump flag error");
1836             return false;
1837     }
1838     return ret;
1839 }
1840 
DumpAllBundleInfoNames(int32_t userId,std::string & result)1841 bool BundleMgrHostImpl::DumpAllBundleInfoNames(int32_t userId, std::string &result)
1842 {
1843     APP_LOGD("DumpAllBundleInfoNames begin");
1844     if (userId != Constants::ALL_USERID) {
1845         return DumpAllBundleInfoNamesByUserId(userId, result);
1846     }
1847 
1848     auto userIds = GetExistsCommonUserIs();
1849     for (auto userId : userIds) {
1850         DumpAllBundleInfoNamesByUserId(userId, result);
1851     }
1852 
1853     APP_LOGD("DumpAllBundleInfoNames success");
1854     return true;
1855 }
1856 
DumpAllBundleInfoNamesByUserId(int32_t userId,std::string & result)1857 bool BundleMgrHostImpl::DumpAllBundleInfoNamesByUserId(int32_t userId, std::string &result)
1858 {
1859     APP_LOGI("DumpAllBundleInfoNamesByUserId begin");
1860     auto dataMgr = GetDataMgrFromService();
1861     if (dataMgr == nullptr) {
1862         APP_LOGE("DataMgr is nullptr");
1863         return false;
1864     }
1865 
1866     std::vector<std::string> bundleNames;
1867     if (!dataMgr->GetBundleList(bundleNames, userId)) {
1868         APP_LOGE("get bundle list failed by userId(%{public}d)", userId);
1869         return false;
1870     }
1871 
1872     result.append("ID: ");
1873     result.append(std::to_string(userId));
1874     result.append(":\n");
1875     for (const auto &name : bundleNames) {
1876         result.append("\t");
1877         result.append(name);
1878         result.append("\n");
1879     }
1880     APP_LOGI("DumpAllBundleInfoNamesByUserId successfully");
1881     return true;
1882 }
1883 
DumpBundleInfo(const std::string & bundleName,int32_t userId,std::string & result)1884 bool BundleMgrHostImpl::DumpBundleInfo(
1885     const std::string &bundleName, int32_t userId, std::string &result)
1886 {
1887     APP_LOGD("DumpBundleInfo begin");
1888     std::vector<InnerBundleUserInfo> innerBundleUserInfos;
1889     InnerBundleUserInfo innerBundleUserInfo;
1890     if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo) &&
1891         !GetBundleUserInfo(bundleName, Constants::DEFAULT_USERID, innerBundleUserInfo)) {
1892         APP_LOGE("get all userInfos in bundle(%{public}s) failed", bundleName.c_str());
1893         return false;
1894     }
1895     innerBundleUserInfos.emplace_back(innerBundleUserInfo);
1896 
1897     BundleInfo bundleInfo;
1898     if (!GetBundleInfo(bundleName,
1899         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
1900         BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION |
1901         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
1902         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE |
1903         BundleFlag::GET_BUNDLE_WITH_MENU |
1904         BundleFlag::GET_BUNDLE_WITH_ROUTER_MAP, bundleInfo, userId)) {
1905         APP_LOGE("get bundleInfo(%{public}s) failed", bundleName.c_str());
1906         return false;
1907     }
1908 
1909     result.append(bundleName);
1910     result.append(":\n");
1911     nlohmann::json jsonObject = bundleInfo;
1912     jsonObject.erase("abilityInfos");
1913     jsonObject.erase("signatureInfo");
1914     jsonObject.erase("extensionAbilityInfo");
1915     jsonObject["applicationInfo"] = bundleInfo.applicationInfo;
1916     jsonObject["userInfo"] = innerBundleUserInfos;
1917     jsonObject["appIdentifier"] = bundleInfo.signatureInfo.appIdentifier;
1918     result.append(jsonObject.dump(Constants::DUMP_INDENT));
1919     result.append("\n");
1920     APP_LOGD("DumpBundleInfo success with bundleName %{public}s", bundleName.c_str());
1921     return true;
1922 }
1923 
DumpShortcutInfo(const std::string & bundleName,int32_t userId,std::string & result)1924 bool BundleMgrHostImpl::DumpShortcutInfo(
1925     const std::string &bundleName, int32_t userId, std::string &result)
1926 {
1927     APP_LOGD("DumpShortcutInfo begin");
1928     std::vector<ShortcutInfo> shortcutInfos;
1929     if (userId == Constants::ALL_USERID) {
1930         std::vector<InnerBundleUserInfo> innerBundleUserInfos;
1931         if (!GetBundleUserInfos(bundleName, innerBundleUserInfos)) {
1932             APP_LOGE("get all userInfos in bundle(%{public}s) failed", bundleName.c_str());
1933             return false;
1934         }
1935         userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
1936     }
1937 
1938     if (!GetShortcutInfos(bundleName, userId, shortcutInfos)) {
1939         APP_LOGE("get all shortcut info by bundle(%{public}s) failed", bundleName.c_str());
1940         return false;
1941     }
1942 
1943     result.append("shortcuts");
1944     result.append(":\n");
1945     for (const auto &info : shortcutInfos) {
1946         result.append("\"shortcut\"");
1947         result.append(":\n");
1948         nlohmann::json jsonObject = info;
1949         result.append(jsonObject.dump(Constants::DUMP_INDENT));
1950         result.append("\n");
1951     }
1952     APP_LOGD("DumpShortcutInfo success with bundleName %{public}s", bundleName.c_str());
1953     return true;
1954 }
1955 
IsModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool & isRemovable)1956 ErrCode BundleMgrHostImpl::IsModuleRemovable(const std::string &bundleName, const std::string &moduleName,
1957     bool &isRemovable)
1958 {
1959     // check permission
1960     if (!BundlePermissionMgr::IsSystemApp()) {
1961         APP_LOGE("non-system app calling system api");
1962         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1963     }
1964     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1965         APP_LOGE("IsModuleRemovable failed due to lack of permission");
1966         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1967     }
1968     auto dataMgr = GetDataMgrFromService();
1969     if (dataMgr == nullptr) {
1970         APP_LOGE("DataMgr is nullptr");
1971         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
1972     }
1973     return dataMgr->IsModuleRemovable(bundleName, moduleName, isRemovable);
1974 }
1975 
SetModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool isEnable)1976 bool BundleMgrHostImpl::SetModuleRemovable(const std::string &bundleName, const std::string &moduleName, bool isEnable)
1977 {
1978     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1979         APP_LOGE("SetModuleRemovable failed due to lack of permission");
1980         return false;
1981     }
1982     auto dataMgr = GetDataMgrFromService();
1983     if (dataMgr == nullptr) {
1984         APP_LOGE("DataMgr is nullptr");
1985         return false;
1986     }
1987     return dataMgr->SetModuleRemovable(bundleName, moduleName, isEnable);
1988 }
1989 
GetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName)1990 bool BundleMgrHostImpl::GetModuleUpgradeFlag(const std::string &bundleName, const std::string &moduleName)
1991 {
1992     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE)) {
1993         APP_LOGE("GetModuleUpgradeFlag failed due to lack of permission");
1994         return false;
1995     }
1996     auto dataMgr = GetDataMgrFromService();
1997     if (dataMgr == nullptr) {
1998         APP_LOGE("DataMgr is nullptr");
1999         return false;
2000     }
2001     return dataMgr->GetModuleUpgradeFlag(bundleName, moduleName);
2002 }
2003 
SetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName,int32_t upgradeFlag)2004 ErrCode BundleMgrHostImpl::SetModuleUpgradeFlag(const std::string &bundleName,
2005     const std::string &moduleName, int32_t upgradeFlag)
2006 {
2007     // check permission
2008     if (!BundlePermissionMgr::IsSystemApp()) {
2009         APP_LOGE("non-system app calling system api");
2010         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2011     }
2012     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE)) {
2013         APP_LOGE("SetModuleUpgradeFlag failed due to lack of permission");
2014         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2015     }
2016     auto dataMgr = GetDataMgrFromService();
2017     if (dataMgr == nullptr) {
2018         APP_LOGE("DataMgr is nullptr");
2019         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
2020     }
2021     return dataMgr->SetModuleUpgradeFlag(bundleName, moduleName, upgradeFlag);
2022 }
2023 
IsApplicationEnabled(const std::string & bundleName,bool & isEnable)2024 ErrCode BundleMgrHostImpl::IsApplicationEnabled(const std::string &bundleName, bool &isEnable)
2025 {
2026     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2027     APP_LOGD("start IsApplicationEnabled, bundleName : %{public}s", bundleName.c_str());
2028     if (!BundlePermissionMgr::IsSystemApp() &&
2029         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2030         APP_LOGE("non-system app calling system api");
2031         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2032     }
2033     auto dataMgr = GetDataMgrFromService();
2034     if (dataMgr == nullptr) {
2035         APP_LOGE("DataMgr is nullptr");
2036         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2037     }
2038     return dataMgr->IsApplicationEnabled(bundleName, 0, isEnable);
2039 }
2040 
IsCloneApplicationEnabled(const std::string & bundleName,int32_t appIndex,bool & isEnable)2041 ErrCode BundleMgrHostImpl::IsCloneApplicationEnabled(const std::string &bundleName, int32_t appIndex, bool &isEnable)
2042 {
2043     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2044     APP_LOGD("start IsCloneApplicationEnabled, bundleName: %{public}s appIndex: %{public}d",
2045         bundleName.c_str(), appIndex);
2046     if (!BundlePermissionMgr::IsSystemApp()) {
2047         APP_LOGE("non-system app calling system api");
2048         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2049     }
2050     auto dataMgr = GetDataMgrFromService();
2051     if (dataMgr == nullptr) {
2052         APP_LOGE("DataMgr is nullptr");
2053         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2054     }
2055     return dataMgr->IsApplicationEnabled(bundleName, appIndex, isEnable);
2056 }
2057 
SetApplicationEnabled(const std::string & bundleName,bool isEnable,int32_t userId)2058 ErrCode BundleMgrHostImpl::SetApplicationEnabled(const std::string &bundleName, bool isEnable, int32_t userId)
2059 {
2060     std::string caller = GetCallerName();
2061     APP_LOGW_NOFUNC("SetApplicationEnabled %{public}s %{public}d %{public}d caller:%{public}s",
2062         bundleName.c_str(), isEnable, userId, caller.c_str());
2063     if (userId == Constants::UNSPECIFIED_USERID) {
2064         userId = BundleUtil::GetUserIdByCallingUid();
2065     }
2066     if (!BundlePermissionMgr::IsSystemApp()) {
2067         APP_LOGE("non-system app calling system api");
2068         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2069     }
2070     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
2071         APP_LOGE("verify permission failed");
2072         EventReport::SendComponentStateSysEventForException(bundleName, "", userId, isEnable, 0, caller);
2073         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2074     }
2075     if (!CheckCanSetEnable(bundleName)) {
2076         APP_LOGE("bundle in white-list");
2077         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2078     }
2079     auto dataMgr = GetDataMgrFromService();
2080     if (dataMgr == nullptr) {
2081         APP_LOGE("DataMgr is nullptr");
2082         EventReport::SendComponentStateSysEventForException(bundleName, "", userId, isEnable, 0, caller);
2083         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2084     }
2085 
2086     auto ret = dataMgr->SetApplicationEnabled(bundleName, 0, isEnable, caller, userId);
2087     if (ret != ERR_OK) {
2088         APP_LOGE("Set application(%{public}s) enabled value faile", bundleName.c_str());
2089         EventReport::SendComponentStateSysEventForException(bundleName, "", userId, isEnable, 0, caller);
2090         return ret;
2091     }
2092 
2093     EventReport::SendComponentStateSysEvent(bundleName, "", userId, isEnable, 0, caller);
2094     InnerBundleUserInfo innerBundleUserInfo;
2095     if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
2096         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
2097         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2098     }
2099 
2100     NotifyBundleEvents installRes = {
2101         .bundleName = bundleName,
2102         .resultCode = ERR_OK,
2103         .type = NotifyType::APPLICATION_ENABLE,
2104         .uid = innerBundleUserInfo.uid,
2105         .accessTokenId = innerBundleUserInfo.accessTokenId,
2106         .isApplicationEnabled = isEnable
2107     };
2108     std::string identity = IPCSkeleton::ResetCallingIdentity();
2109     NotifyBundleStatus(installRes);
2110     IPCSkeleton::SetCallingIdentity(identity);
2111     return ERR_OK;
2112 }
2113 
SetCloneApplicationEnabled(const std::string & bundleName,int32_t appIndex,bool isEnable,int32_t userId)2114 ErrCode BundleMgrHostImpl::SetCloneApplicationEnabled(
2115     const std::string &bundleName, int32_t appIndex, bool isEnable, int32_t userId)
2116 {
2117     std::string caller = GetCallerName();
2118     APP_LOGW_NOFUNC("SetCloneApplicationEnabled param %{public}s %{public}d %{public}d %{public}d caller:%{public}s",
2119         bundleName.c_str(), appIndex, isEnable, userId, caller.c_str());
2120     if (!BundlePermissionMgr::IsSystemApp()) {
2121         APP_LOGE("non-system app calling system api");
2122         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2123     }
2124     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
2125         APP_LOGE("verify permission failed");
2126         EventReport::SendComponentStateSysEventForException(bundleName, "", userId, isEnable, appIndex, caller);
2127         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2128     }
2129     if (userId == Constants::UNSPECIFIED_USERID) {
2130         userId = BundleUtil::GetUserIdByCallingUid();
2131     }
2132     APP_LOGD("verify permission success, begin to SetCloneApplicationEnabled");
2133     auto dataMgr = GetDataMgrFromService();
2134     if (dataMgr == nullptr) {
2135         APP_LOGE("DataMgr is nullptr");
2136         EventReport::SendComponentStateSysEventForException(bundleName, "", userId, isEnable, appIndex, caller);
2137         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2138     }
2139     auto ret = dataMgr->SetApplicationEnabled(bundleName, appIndex, isEnable, caller, userId);
2140     if (ret != ERR_OK) {
2141         APP_LOGE("Set application(%{public}s) enabled value fail", bundleName.c_str());
2142         EventReport::SendComponentStateSysEventForException(bundleName, "", userId, isEnable, appIndex, caller);
2143         return ret;
2144     }
2145 
2146     EventReport::SendComponentStateSysEvent(bundleName, "", userId, isEnable, appIndex, caller);
2147     InnerBundleUserInfo innerBundleUserInfo;
2148     if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
2149         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
2150         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2151     }
2152 
2153     NotifyBundleEvents installRes = {
2154         .bundleName = bundleName,
2155         .resultCode = ERR_OK,
2156         .type = NotifyType::APPLICATION_ENABLE,
2157         .uid = innerBundleUserInfo.uid,
2158         .accessTokenId = innerBundleUserInfo.accessTokenId,
2159         .appIndex = appIndex
2160     };
2161     NotifyBundleStatus(installRes);
2162     APP_LOGD("SetCloneApplicationEnabled finish");
2163     return ERR_OK;
2164 }
2165 
IsAbilityEnabled(const AbilityInfo & abilityInfo,bool & isEnable)2166 ErrCode BundleMgrHostImpl::IsAbilityEnabled(const AbilityInfo &abilityInfo, bool &isEnable)
2167 {
2168     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2169     APP_LOGD("start IsAbilityEnabled");
2170     if (!BundlePermissionMgr::IsSystemApp() &&
2171         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2172         APP_LOGE("non-system app calling system api");
2173         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2174     }
2175     auto dataMgr = GetDataMgrFromService();
2176     if (dataMgr == nullptr) {
2177         APP_LOGE("DataMgr is nullptr");
2178         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2179     }
2180     return dataMgr->IsAbilityEnabled(abilityInfo, 0, isEnable);
2181 }
2182 
IsCloneAbilityEnabled(const AbilityInfo & abilityInfo,int32_t appIndex,bool & isEnable)2183 ErrCode BundleMgrHostImpl::IsCloneAbilityEnabled(const AbilityInfo &abilityInfo, int32_t appIndex, bool &isEnable)
2184 {
2185     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2186     APP_LOGD("start IsCloneAbilityEnabled");
2187     if (!BundlePermissionMgr::IsSystemApp()) {
2188         APP_LOGE("non-system app calling system api");
2189         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2190     }
2191     auto dataMgr = GetDataMgrFromService();
2192     if (dataMgr == nullptr) {
2193         APP_LOGE("DataMgr is nullptr");
2194         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2195     }
2196     return dataMgr->IsAbilityEnabled(abilityInfo, appIndex, isEnable);
2197 }
2198 
SetAbilityEnabled(const AbilityInfo & abilityInfo,bool isEnabled,int32_t userId)2199 ErrCode BundleMgrHostImpl::SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEnabled, int32_t userId)
2200 {
2201     std::string caller = GetCallerName();
2202     APP_LOGW_NOFUNC("SetAbilityEnabled %{public}s %{public}d %{public}d caller:%{public}s",
2203         abilityInfo.name.c_str(), isEnabled, userId, caller.c_str());
2204     if (userId == Constants::UNSPECIFIED_USERID) {
2205         userId = BundleUtil::GetUserIdByCallingUid();
2206     }
2207     if (!BundlePermissionMgr::IsSystemApp()) {
2208         APP_LOGE("non-system app calling system api");
2209         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2210     }
2211     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
2212         APP_LOGE("verify permission failed");
2213         EventReport::SendComponentStateSysEventForException(abilityInfo.bundleName, abilityInfo.name,
2214             userId, isEnabled, 0, caller);
2215         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2216     }
2217     if (!CheckCanSetEnable(abilityInfo.bundleName)) {
2218         APP_LOGE("bundle in white-list");
2219         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2220     }
2221     auto dataMgr = GetDataMgrFromService();
2222     if (dataMgr == nullptr) {
2223         APP_LOGE("DataMgr is nullptr");
2224         EventReport::SendComponentStateSysEventForException(abilityInfo.bundleName, abilityInfo.name,
2225             userId, isEnabled, 0, caller);
2226         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2227     }
2228     auto ret = dataMgr->SetAbilityEnabled(abilityInfo, 0, isEnabled, userId);
2229     if (ret != ERR_OK) {
2230         APP_LOGE("Set ability(%{public}s) enabled value failed", abilityInfo.bundleName.c_str());
2231         EventReport::SendComponentStateSysEventForException(abilityInfo.bundleName, abilityInfo.name,
2232             userId, isEnabled, 0, caller);
2233         return ret;
2234     }
2235     EventReport::SendComponentStateSysEvent(abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, 0, caller);
2236     InnerBundleUserInfo innerBundleUserInfo;
2237     if (!GetBundleUserInfo(abilityInfo.bundleName, userId, innerBundleUserInfo)) {
2238         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", abilityInfo.bundleName.c_str());
2239         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2240     }
2241     NotifyBundleEvents installRes = {.bundleName = abilityInfo.bundleName, .abilityName = abilityInfo.name,
2242         .resultCode = ERR_OK, .type = NotifyType::APPLICATION_ENABLE, .uid = innerBundleUserInfo.uid,
2243         .accessTokenId = innerBundleUserInfo.accessTokenId,
2244     };
2245     NotifyBundleStatus(installRes);
2246     return ERR_OK;
2247 }
2248 
SetCloneAbilityEnabled(const AbilityInfo & abilityInfo,int32_t appIndex,bool isEnabled,int32_t userId)2249 ErrCode BundleMgrHostImpl::SetCloneAbilityEnabled(const AbilityInfo &abilityInfo,
2250     int32_t appIndex, bool isEnabled, int32_t userId)
2251 {
2252     std::string caller = GetCallerName();
2253     APP_LOGW_NOFUNC("SetCloneAbilityEnabled %{public}s %{public}d %{public}d %{public}d caller:%{public}s",
2254         abilityInfo.name.c_str(), appIndex, isEnabled, userId, caller.c_str());
2255     if (!BundlePermissionMgr::IsSystemApp()) {
2256         APP_LOGE("non-system app calling system api");
2257         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2258     }
2259     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
2260         APP_LOGE("verify permission failed");
2261         EventReport::SendComponentStateSysEventForException(abilityInfo.bundleName, abilityInfo.name,
2262             userId, isEnabled, appIndex, caller);
2263         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2264     }
2265     if (userId == Constants::UNSPECIFIED_USERID) {
2266         userId = BundleUtil::GetUserIdByCallingUid();
2267     }
2268     auto dataMgr = GetDataMgrFromService();
2269     if (dataMgr == nullptr) {
2270         APP_LOGE("DataMgr is nullptr");
2271         EventReport::SendComponentStateSysEventForException(abilityInfo.bundleName, abilityInfo.name,
2272             userId, isEnabled, appIndex, caller);
2273         return ERR_APPEXECFWK_SERVICE_NOT_READY;
2274     }
2275     auto ret = dataMgr->SetAbilityEnabled(abilityInfo, appIndex, isEnabled, userId);
2276     if (ret != ERR_OK) {
2277         APP_LOGE("Set ability(%{public}s) enabled value failed", abilityInfo.bundleName.c_str());
2278         EventReport::SendComponentStateSysEventForException(abilityInfo.bundleName, abilityInfo.name,
2279             userId, isEnabled, appIndex, caller);
2280         return ret;
2281     }
2282     EventReport::SendComponentStateSysEvent(
2283         abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, appIndex, caller);
2284     InnerBundleUserInfo innerBundleUserInfo;
2285     if (!GetBundleUserInfo(abilityInfo.bundleName, userId, innerBundleUserInfo)) {
2286         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", abilityInfo.bundleName.c_str());
2287         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2288     }
2289     NotifyBundleEvents installRes = {.bundleName = abilityInfo.bundleName, .abilityName = abilityInfo.name,
2290         .resultCode = ERR_OK, .type = NotifyType::APPLICATION_ENABLE, .uid = innerBundleUserInfo.uid,
2291         .accessTokenId = innerBundleUserInfo.accessTokenId, .appIndex = appIndex
2292     };
2293     NotifyBundleStatus(installRes);
2294     return ERR_OK;
2295 }
2296 
GetBundleInstaller()2297 sptr<IBundleInstaller> BundleMgrHostImpl::GetBundleInstaller()
2298 {
2299     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2300     APP_LOGD("start GetBundleInstaller");
2301     if (!VerifySystemApi()) {
2302         APP_LOGE("non-system app calling system api");
2303         return nullptr;
2304     }
2305     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
2306 }
2307 
GetBundleUserMgr()2308 sptr<IBundleUserMgr> BundleMgrHostImpl::GetBundleUserMgr()
2309 {
2310     int32_t callingUid = IPCSkeleton::GetCallingUid();
2311     if (callingUid != ServiceConstants::ACCOUNT_UID) {
2312         APP_LOGE("invalid calling uid %{public}d to GetbundleUserMgr", callingUid);
2313         return nullptr;
2314     }
2315     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleUserMgr();
2316 }
2317 
GetVerifyManager()2318 sptr<IVerifyManager> BundleMgrHostImpl::GetVerifyManager()
2319 {
2320     return DelayedSingleton<BundleMgrService>::GetInstance()->GetVerifyManager();
2321 }
2322 
GetExtendResourceManager()2323 sptr<IExtendResourceManager> BundleMgrHostImpl::GetExtendResourceManager()
2324 {
2325     return DelayedSingleton<BundleMgrService>::GetInstance()->GetExtendResourceManager();
2326 }
2327 
GetAllFormsInfo(std::vector<FormInfo> & formInfos)2328 bool BundleMgrHostImpl::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
2329 {
2330     APP_LOGD("start GetAllFormsInfo");
2331     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
2332         APP_LOGE("verify permission failed");
2333         return false;
2334     }
2335     auto dataMgr = GetDataMgrFromService();
2336     if (dataMgr == nullptr) {
2337         APP_LOGE("DataMgr is nullptr");
2338         return false;
2339     }
2340     return dataMgr->GetAllFormsInfo(formInfos);
2341 }
2342 
GetFormsInfoByApp(const std::string & bundleName,std::vector<FormInfo> & formInfos)2343 bool BundleMgrHostImpl::GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos)
2344 {
2345     APP_LOGD("start GetFormsInfoByApp, bundleName : %{public}s", bundleName.c_str());
2346     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2347         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2348         APP_LOGE("verify permission failed");
2349         return false;
2350     }
2351     auto dataMgr = GetDataMgrFromService();
2352     if (dataMgr == nullptr) {
2353         APP_LOGE("DataMgr is nullptr");
2354         return false;
2355     }
2356     return dataMgr->GetFormsInfoByApp(bundleName, formInfos);
2357 }
2358 
GetFormsInfoByModule(const std::string & bundleName,const std::string & moduleName,std::vector<FormInfo> & formInfos)2359 bool BundleMgrHostImpl::GetFormsInfoByModule(
2360     const std::string &bundleName, const std::string &moduleName, std::vector<FormInfo> &formInfos)
2361 {
2362     APP_LOGD("start GetFormsInfoByModule, bundleName : %{public}s, moduleName : %{public}s",
2363         bundleName.c_str(), moduleName.c_str());
2364     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2365         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2366         APP_LOGE("verify permission failed");
2367         return false;
2368     }
2369     auto dataMgr = GetDataMgrFromService();
2370     if (dataMgr == nullptr) {
2371         APP_LOGE("DataMgr is nullptr");
2372         return false;
2373     }
2374     return dataMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos);
2375 }
2376 
GetShortcutInfos(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)2377 bool BundleMgrHostImpl::GetShortcutInfos(
2378     const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
2379 {
2380     int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
2381     APP_LOGD("current active userId is %{public}d", currentUserId);
2382     if (currentUserId == Constants::INVALID_USERID) {
2383         APP_LOGW("current userId is invalid");
2384         return false;
2385     }
2386 
2387     return GetShortcutInfos(bundleName, currentUserId, shortcutInfos);
2388 }
2389 
GetShortcutInfos(const std::string & bundleName,int32_t userId,std::vector<ShortcutInfo> & shortcutInfos)2390 bool BundleMgrHostImpl::GetShortcutInfos(
2391     const std::string &bundleName, int32_t userId, std::vector<ShortcutInfo> &shortcutInfos)
2392 {
2393     APP_LOGD("start GetShortcutInfos, bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2394     // API9 need to be system app otherwise return empty data
2395     if (!BundlePermissionMgr::IsSystemApp() &&
2396         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2397         APP_LOGD("non-system app calling system api");
2398         return true;
2399     }
2400     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
2401         APP_LOGE("verify permission failed");
2402         return false;
2403     }
2404     APP_LOGD("verify permission success, begin to GetShortcutInfos");
2405     auto dataMgr = GetDataMgrFromService();
2406     if (dataMgr == nullptr) {
2407         APP_LOGE("DataMgr is nullptr");
2408         return false;
2409     }
2410     return dataMgr->GetShortcutInfos(bundleName, userId, shortcutInfos);
2411 }
2412 
GetShortcutInfoV9(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos,int32_t userId)2413 ErrCode BundleMgrHostImpl::GetShortcutInfoV9(const std::string &bundleName,
2414     std::vector<ShortcutInfo> &shortcutInfos, int32_t userId)
2415 {
2416     if (!BundlePermissionMgr::IsSystemApp()) {
2417         APP_LOGE("non-system app calling system api");
2418         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2419     }
2420     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2421         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2422         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2423         APP_LOGE("verify permission failed");
2424         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2425     }
2426     auto dataMgr = GetDataMgrFromService();
2427     if (dataMgr == nullptr) {
2428         APP_LOGE("DataMgr is nullptr");
2429         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2430     }
2431     return dataMgr->GetShortcutInfoV9(bundleName, userId, shortcutInfos);
2432 }
2433 
GetAllCommonEventInfo(const std::string & eventKey,std::vector<CommonEventInfo> & commonEventInfos)2434 bool BundleMgrHostImpl::GetAllCommonEventInfo(const std::string &eventKey,
2435     std::vector<CommonEventInfo> &commonEventInfos)
2436 {
2437     APP_LOGD("start GetAllCommonEventInfo, eventKey : %{public}s", eventKey.c_str());
2438     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
2439         APP_LOGE("verify permission failed");
2440         return false;
2441     }
2442     auto dataMgr = GetDataMgrFromService();
2443     if (dataMgr == nullptr) {
2444         APP_LOGE("DataMgr is nullptr");
2445         return false;
2446     }
2447     return dataMgr->GetAllCommonEventInfo(eventKey, commonEventInfos);
2448 }
2449 
GetDistributedBundleInfo(const std::string & networkId,const std::string & bundleName,DistributedBundleInfo & distributedBundleInfo)2450 bool BundleMgrHostImpl::GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName,
2451     DistributedBundleInfo &distributedBundleInfo)
2452 {
2453     APP_LOGD("start GetDistributedBundleInfo, bundleName : %{public}s", bundleName.c_str());
2454 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
2455     if (!BundlePermissionMgr::IsSystemApp()) {
2456         APP_LOGE("Non-system app calling system api");
2457         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2458     }
2459     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2460         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2461         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2462         APP_LOGE("verify permission failed");
2463         return false;
2464     }
2465     auto distributedBundleMgr = GetDistributedBundleMgrService();
2466     if (distributedBundleMgr == nullptr) {
2467         APP_LOGE("DistributedBundleMgrService is nullptr");
2468         return false;
2469     }
2470     return distributedBundleMgr->GetDistributedBundleInfo(networkId, bundleName, distributedBundleInfo);
2471 #else
2472     APP_LOGW("DISTRIBUTED_BUNDLE_FRAMEWORK is false");
2473     return false;
2474 #endif
2475 }
2476 
QueryExtensionAbilityInfos(const Want & want,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2477 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const Want &want, const int32_t &flag, const int32_t &userId,
2478     std::vector<ExtensionAbilityInfo> &extensionInfos)
2479 {
2480     LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfos without type begin");
2481     // API9 need to be system app, otherwise return empty data
2482     if (!BundlePermissionMgr::IsSystemApp() &&
2483         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2484         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
2485         return true;
2486     }
2487     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2488         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2489         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
2490         LOG_E(BMS_TAG_QUERY, "verify permission failed");
2491         return false;
2492     }
2493     LOG_D(BMS_TAG_QUERY, "want uri is %{private}s", want.GetUriString().c_str());
2494     auto dataMgr = GetDataMgrFromService();
2495     if (dataMgr == nullptr) {
2496         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
2497         return false;
2498     }
2499     (void)dataMgr->QueryExtensionAbilityInfos(want, flag, userId, extensionInfos);
2500     dataMgr->QueryAllCloneExtensionInfos(want, flag, userId, extensionInfos);
2501     if (extensionInfos.empty()) {
2502         LOG_E(BMS_TAG_QUERY, "no valid extension info can be inquired");
2503         return false;
2504     }
2505     return true;
2506 }
2507 
QueryExtensionAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2508 ErrCode BundleMgrHostImpl::QueryExtensionAbilityInfosV9(const Want &want, int32_t flags, int32_t userId,
2509     std::vector<ExtensionAbilityInfo> &extensionInfos)
2510 {
2511     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2512     LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfosV9 without type begin");
2513     if (!BundlePermissionMgr::IsSystemApp()) {
2514         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
2515         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2516     }
2517     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2518         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2519         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
2520         LOG_E(BMS_TAG_QUERY, "verify permission failed");
2521         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2522     }
2523     LOG_D(BMS_TAG_QUERY, "want uri is %{private}s", want.GetUriString().c_str());
2524     auto dataMgr = GetDataMgrFromService();
2525     if (dataMgr == nullptr) {
2526         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
2527         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2528     }
2529     ErrCode ret = dataMgr->QueryExtensionAbilityInfosV9(want, flags, userId, extensionInfos);
2530     dataMgr->QueryAllCloneExtensionInfosV9(want, flags, userId, extensionInfos);
2531 
2532     if (extensionInfos.empty()) {
2533         if (ret != ERR_OK) {
2534             LOG_E(BMS_TAG_QUERY, "query extension ability fail, %{public}d", ret);
2535             return ret;
2536         }
2537         LOG_E(BMS_TAG_QUERY, "no valid extension info can be inquired");
2538         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
2539     }
2540     return ERR_OK;
2541 }
2542 
QueryExtensionAbilityInfos(const Want & want,const ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2543 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const Want &want, const ExtensionAbilityType &extensionType,
2544     const int32_t &flag, const int32_t &userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
2545 {
2546     LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfos begin");
2547     // API9 need to be system app, otherwise return empty data
2548     if (!BundlePermissionMgr::IsSystemApp() &&
2549         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2550         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
2551         return true;
2552     }
2553     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2554         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2555         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
2556         LOG_E(BMS_TAG_QUERY, "verify permission failed");
2557         return false;
2558     }
2559     auto dataMgr = GetDataMgrFromService();
2560     if (dataMgr == nullptr) {
2561         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
2562         return false;
2563     }
2564     std::vector<ExtensionAbilityInfo> infos;
2565     (void)dataMgr->QueryExtensionAbilityInfos(want, flag, userId, infos);
2566     dataMgr->QueryAllCloneExtensionInfos(want, flag, userId, infos);
2567 
2568     for_each(infos.begin(), infos.end(), [&extensionType, &extensionInfos](const auto &info)->decltype(auto) {
2569         LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfos extensionType:%{public}d info.type:%{public}d",
2570             static_cast<int32_t>(extensionType), static_cast<int32_t>(info.type));
2571         if (extensionType == info.type) {
2572             extensionInfos.emplace_back(info);
2573         }
2574     });
2575     if (extensionInfos.empty()) {
2576         LOG_E(BMS_TAG_QUERY, "no valid extension info can be inquired");
2577         return false;
2578     }
2579     return true;
2580 }
2581 
QueryExtensionAbilityInfosV9(const Want & want,const ExtensionAbilityType & extensionType,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2582 ErrCode BundleMgrHostImpl::QueryExtensionAbilityInfosV9(const Want &want, const ExtensionAbilityType &extensionType,
2583     int32_t flags, int32_t userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
2584 {
2585     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
2586     LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfosV9 begin");
2587     if (!BundlePermissionMgr::IsSystemApp()) {
2588         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
2589         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2590     }
2591     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2592         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2593         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
2594         LOG_E(BMS_TAG_QUERY, "verify permission failed");
2595         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2596     }
2597     auto dataMgr = GetDataMgrFromService();
2598     if (dataMgr == nullptr) {
2599         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
2600         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2601     }
2602     std::vector<ExtensionAbilityInfo> infos;
2603     ErrCode ret = dataMgr->QueryExtensionAbilityInfosV9(want, flags, userId, infos);
2604     dataMgr->QueryAllCloneExtensionInfosV9(want, flags, userId, infos);
2605     for_each(infos.begin(), infos.end(), [&extensionType, &extensionInfos](const auto &info)->decltype(auto) {
2606         LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfosV9 extensionType:%{public}d info.type:%{public}d",
2607             static_cast<int32_t>(extensionType), static_cast<int32_t>(info.type));
2608         if (extensionType == info.type) {
2609             extensionInfos.emplace_back(info);
2610         }
2611     });
2612     if (extensionInfos.empty()) {
2613         if (ret != ERR_OK) {
2614             LOG_E(BMS_TAG_QUERY, "query extension ability fail, %{public}d", ret);
2615             return ret;
2616         }
2617         LOG_E(BMS_TAG_QUERY, "no valid extension info can be inquired");
2618         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
2619     }
2620     return ERR_OK;
2621 }
2622 
QueryExtensionAbilityInfos(const ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)2623 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const ExtensionAbilityType &extensionType, const int32_t &userId,
2624     std::vector<ExtensionAbilityInfo> &extensionInfos)
2625 {
2626     LOG_D(BMS_TAG_QUERY, "QueryExtensionAbilityInfos with type begin");
2627     // API9 need to be system app, otherwise return empty data
2628     if (!BundlePermissionMgr::IsSystemApp() &&
2629         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2630         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
2631         return true;
2632     }
2633     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2634         Constants::PERMISSION_GET_BUNDLE_INFO})) {
2635         LOG_E(BMS_TAG_QUERY, "verify permission failed");
2636         return false;
2637     }
2638     auto dataMgr = GetDataMgrFromService();
2639     if (dataMgr == nullptr) {
2640         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
2641         return false;
2642     }
2643     bool ret = dataMgr->QueryExtensionAbilityInfos(extensionType, userId, extensionInfos);
2644     if (!ret) {
2645         LOG_E(BMS_TAG_QUERY, "QueryExtensionAbilityInfos is failed");
2646         return false;
2647     }
2648 
2649     if (extensionInfos.empty()) {
2650         LOG_E(BMS_TAG_QUERY, "no valid extension info can be inquired");
2651         return false;
2652     }
2653     return true;
2654 }
2655 
GetDataMgrFromService()2656 const std::shared_ptr<BundleDataMgr> BundleMgrHostImpl::GetDataMgrFromService()
2657 {
2658     return DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
2659 }
2660 
2661 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
GetDistributedBundleMgrService()2662 const OHOS::sptr<IDistributedBms> BundleMgrHostImpl::GetDistributedBundleMgrService()
2663 {
2664     auto saMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
2665     if (saMgr == nullptr) {
2666         APP_LOGE("saMgr is nullptr");
2667         return nullptr;
2668     }
2669     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
2670         saMgr->CheckSystemAbility(OHOS::DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
2671     return OHOS::iface_cast<IDistributedBms>(remoteObject);
2672 }
2673 #endif
2674 
2675 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetConnectAbilityMgrFromService()2676 const std::shared_ptr<BundleConnectAbilityMgr> BundleMgrHostImpl::GetConnectAbilityMgrFromService()
2677 {
2678     int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
2679     return DelayedSingleton<BundleMgrService>::GetInstance()->GetConnectAbility(currentUserId);
2680 }
2681 #endif
2682 
GetExistsCommonUserIs()2683 std::set<int32_t> BundleMgrHostImpl::GetExistsCommonUserIs()
2684 {
2685     std::set<int32_t> userIds;
2686     auto dataMgr = GetDataMgrFromService();
2687     if (dataMgr == nullptr) {
2688         APP_LOGE("Get dataMgr shared_ptr nullptr");
2689         return userIds;
2690     }
2691 
2692     for (auto userId : dataMgr->GetAllUser()) {
2693         if (userId >= Constants::START_USERID) {
2694             userIds.insert(userId);
2695         }
2696     }
2697     return userIds;
2698 }
2699 
GetAppPrivilegeLevel(const std::string & bundleName,int32_t userId)2700 std::string BundleMgrHostImpl::GetAppPrivilegeLevel(const std::string &bundleName, int32_t userId)
2701 {
2702     APP_LOGD("start GetAppPrivilegeLevel");
2703     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2704         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2705         APP_LOGE("verify permission failed");
2706         return Constants::EMPTY_STRING;
2707     }
2708     auto dataMgr = GetDataMgrFromService();
2709     if (dataMgr == nullptr) {
2710         APP_LOGE("DataMgr is nullptr");
2711         return Constants::EMPTY_STRING;
2712     }
2713     return dataMgr->GetAppPrivilegeLevel(bundleName, userId);
2714 }
2715 
VerifyCallingPermission(const std::string & permission)2716 bool BundleMgrHostImpl::VerifyCallingPermission(const std::string &permission)
2717 {
2718     APP_LOGD("VerifyCallingPermission begin");
2719     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_VERIFY_CALLING_PERMISSION);
2720     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
2721     return BundlePermissionMgr::VerifyCallingPermissionForAll(permission);
2722 }
2723 
QueryExtensionAbilityInfoByUri(const std::string & uri,int32_t userId,ExtensionAbilityInfo & extensionAbilityInfo)2724 bool BundleMgrHostImpl::QueryExtensionAbilityInfoByUri(const std::string &uri, int32_t userId,
2725     ExtensionAbilityInfo &extensionAbilityInfo)
2726 {
2727     LOG_I(BMS_TAG_QUERY, "uri : %{private}s, userId : %{public}d", uri.c_str(), userId);
2728     // API9 need to be system app, otherwise return empty data
2729     if (!BundlePermissionMgr::IsSystemApp() &&
2730         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2731         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
2732         return true;
2733     }
2734     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2735         Constants::PERMISSION_GET_BUNDLE_INFO})) {
2736         LOG_E(BMS_TAG_QUERY, "verify query permission failed");
2737         return false;
2738     }
2739     auto dataMgr = GetDataMgrFromService();
2740     if (dataMgr == nullptr) {
2741         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
2742         return false;
2743     }
2744     return dataMgr->QueryExtensionAbilityInfoByUri(uri, userId, extensionAbilityInfo);
2745 }
2746 
GetAppIdByBundleName(const std::string & bundleName,const int userId)2747 std::string BundleMgrHostImpl::GetAppIdByBundleName(const std::string &bundleName, const int userId)
2748 {
2749     APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2750     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2751         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2752         APP_LOGE("verify query permission failed");
2753         return Constants::EMPTY_STRING;
2754     }
2755     auto dataMgr = GetDataMgrFromService();
2756     if (dataMgr == nullptr) {
2757         APP_LOGE("DataMgr is nullptr");
2758         return Constants::EMPTY_STRING;
2759     }
2760     BundleInfo bundleInfo;
2761     bool ret = dataMgr->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId);
2762     if (!ret) {
2763         APP_LOGE("get bundleInfo failed");
2764         return Constants::EMPTY_STRING;
2765     }
2766     APP_LOGD("appId is %{private}s", bundleInfo.appId.c_str());
2767     return bundleInfo.appId;
2768 }
2769 
GetAppType(const std::string & bundleName)2770 std::string BundleMgrHostImpl::GetAppType(const std::string &bundleName)
2771 {
2772     APP_LOGD("bundleName : %{public}s", bundleName.c_str());
2773     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2774         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2775         APP_LOGE("verify permission failed");
2776         return Constants::EMPTY_STRING;
2777     }
2778     auto dataMgr = GetDataMgrFromService();
2779     if (dataMgr == nullptr) {
2780         APP_LOGE("DataMgr is nullptr");
2781         return Constants::EMPTY_STRING;
2782     }
2783     BundleInfo bundleInfo;
2784     bool ret = dataMgr->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, Constants::UNSPECIFIED_USERID);
2785     if (!ret) {
2786         APP_LOGE("get bundleInfo failed");
2787         return Constants::EMPTY_STRING;
2788     }
2789     bool isSystemApp = bundleInfo.applicationInfo.isSystemApp;
2790     std::string appType = isSystemApp ? SYSTEM_APP : THIRD_PARTY_APP;
2791     APP_LOGD("appType is %{public}s", appType.c_str());
2792     return appType;
2793 }
2794 
GetUidByBundleName(const std::string & bundleName,const int32_t userId)2795 int32_t BundleMgrHostImpl::GetUidByBundleName(const std::string &bundleName, const int32_t userId)
2796 {
2797     return GetUidByBundleName(bundleName, userId, 0);
2798 }
2799 
GetUidByBundleName(const std::string & bundleName,const int32_t userId,int32_t appIndex)2800 int32_t BundleMgrHostImpl::GetUidByBundleName(const std::string &bundleName, const int32_t userId, int32_t appIndex)
2801 {
2802     APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2803     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2804         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2805         APP_LOGE("verify token type failed");
2806         return Constants::INVALID_UID;
2807     }
2808     auto dataMgr = GetDataMgrFromService();
2809     if (dataMgr == nullptr) {
2810         APP_LOGE("DataMgr is nullptr");
2811         return Constants::INVALID_UID;
2812     }
2813     return dataMgr->GetUidByBundleName(bundleName, userId, appIndex);
2814 }
2815 
GetUidByDebugBundleName(const std::string & bundleName,const int userId)2816 int BundleMgrHostImpl::GetUidByDebugBundleName(const std::string &bundleName, const int userId)
2817 {
2818     APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2819     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2820         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2821         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2822         APP_LOGE("verify token type failed");
2823         return Constants::INVALID_UID;
2824     }
2825     auto dataMgr = GetDataMgrFromService();
2826     if (dataMgr == nullptr) {
2827         APP_LOGE("DataMgr is nullptr");
2828         return Constants::INVALID_UID;
2829     }
2830     ApplicationInfo appInfo;
2831     int32_t uid = Constants::INVALID_UID;
2832     bool ret = dataMgr->GetApplicationInfo(bundleName, GET_BUNDLE_DEFAULT, userId, appInfo);
2833     if (ret && appInfo.debug) {
2834         uid = appInfo.uid;
2835         APP_LOGD("get debug bundle uid success, uid is %{public}d", uid);
2836     } else {
2837         APP_LOGE("can not get bundleInfo's uid");
2838     }
2839     return uid;
2840 }
2841 
GetAbilityInfo(const std::string & bundleName,const std::string & abilityName,AbilityInfo & abilityInfo)2842 bool BundleMgrHostImpl::GetAbilityInfo(
2843     const std::string &bundleName, const std::string &abilityName, AbilityInfo &abilityInfo)
2844 {
2845     LOG_D(BMS_TAG_QUERY, "start GetAbilityInfo, bundleName:%{public}s abilityName:%{public}s",
2846         bundleName.c_str(), abilityName.c_str());
2847     ElementName elementName("", bundleName, abilityName);
2848     Want want;
2849     want.SetElement(elementName);
2850     return QueryAbilityInfo(want, abilityInfo);
2851 }
2852 
GetAbilityInfo(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,AbilityInfo & abilityInfo)2853 bool BundleMgrHostImpl::GetAbilityInfo(
2854     const std::string &bundleName, const std::string &moduleName,
2855     const std::string &abilityName, AbilityInfo &abilityInfo)
2856 {
2857     LOG_D(BMS_TAG_QUERY,
2858         "start GetAbilityInfo bundleName:%{public}s moduleName:%{public}s abilityName:%{public}s",
2859         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
2860     if (!VerifySystemApi(ServiceConstants::API_VERSION_NINE)) {
2861         LOG_D(BMS_TAG_QUERY, "non-system app calling system api");
2862         return true;
2863     }
2864     ElementName elementName("", bundleName, abilityName, moduleName);
2865     Want want;
2866     want.SetElement(elementName);
2867     return QueryAbilityInfo(want, abilityInfo);
2868 }
2869 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)2870 bool BundleMgrHostImpl::ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId,
2871     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo)
2872 {
2873     APP_LOGD("start ImplicitQueryInfoByPriority, flags : %{public}d, userId : %{public}d", flags, userId);
2874     if (!BundlePermissionMgr::IsSystemApp() &&
2875         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2876         APP_LOGD("non-system app calling system api");
2877         return true;
2878     }
2879     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2880         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2881         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
2882         APP_LOGE("verify permission failed");
2883         return false;
2884     }
2885     auto dataMgr = GetDataMgrFromService();
2886     if (dataMgr == nullptr) {
2887         APP_LOGE("DataMgr is nullptr");
2888         return false;
2889     }
2890     return dataMgr->ImplicitQueryInfoByPriority(want, flags, userId, abilityInfo, extensionInfo);
2891 }
2892 
ImplicitQueryInfos(const Want & want,int32_t flags,int32_t userId,bool withDefault,std::vector<AbilityInfo> & abilityInfos,std::vector<ExtensionAbilityInfo> & extensionInfos,bool & findDefaultApp)2893 bool BundleMgrHostImpl::ImplicitQueryInfos(const Want &want, int32_t flags, int32_t userId,  bool withDefault,
2894     std::vector<AbilityInfo> &abilityInfos, std::vector<ExtensionAbilityInfo> &extensionInfos, bool &findDefaultApp)
2895 {
2896     APP_LOGD("begin to ImplicitQueryInfos, flags : %{public}d, userId : %{public}d", flags, userId);
2897     if (!BundlePermissionMgr::IsSystemApp() &&
2898         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
2899         APP_LOGD("non-system app calling system api");
2900         return true;
2901     }
2902     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2903         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2904         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
2905         APP_LOGE("verify permission failed");
2906         return false;
2907     }
2908     auto dataMgr = GetDataMgrFromService();
2909     if (dataMgr == nullptr) {
2910         APP_LOGE("DataMgr is nullptr");
2911         return false;
2912     }
2913     findDefaultApp = false;
2914     auto ret = dataMgr->ImplicitQueryInfos(
2915         want, flags, userId, withDefault, abilityInfos, extensionInfos, findDefaultApp);
2916     if (ret && findDefaultApp) {
2917         APP_LOGD("default app has been found and unnecessary to find from bms extension");
2918         return ret;
2919     }
2920     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
2921     if (!IsAppLinking(flags) && isBrokerServiceExisted_ &&
2922         bmsExtensionClient->ImplicitQueryAbilityInfos(want, flags, userId, abilityInfos, false) == ERR_OK) {
2923         APP_LOGD("implicitly query from bms extension successfully");
2924         FilterAbilityInfos(abilityInfos);
2925         return true;
2926     }
2927     return ret;
2928 }
2929 
FilterAbilityInfos(std::vector<AbilityInfo> & abilityInfos)2930 void BundleMgrHostImpl::FilterAbilityInfos(std::vector<AbilityInfo> &abilityInfos)
2931 {
2932     AbilityInfo appLinkingAbility;
2933     bool hasAppLinking = false;
2934     for (const auto& ability : abilityInfos) {
2935         if (ability.kind == APP_LINKING) {
2936             appLinkingAbility = ability;
2937             hasAppLinking = true;
2938             break;
2939         }
2940     }
2941     if (hasAppLinking) {
2942         abilityInfos.clear();
2943         abilityInfos.push_back(appLinkingAbility);
2944     }
2945 }
2946 
Dump(int fd,const std::vector<std::u16string> & args)2947 int BundleMgrHostImpl::Dump(int fd, const std::vector<std::u16string> &args)
2948 {
2949     std::string result;
2950     std::vector<std::string> argsStr;
2951     for (auto item : args) {
2952         argsStr.emplace_back(Str16ToStr8(item));
2953     }
2954 
2955     if (!DelayedSingleton<BundleMgrService>::GetInstance()->Hidump(argsStr, result)) {
2956         APP_LOGE("Hidump error");
2957         return ERR_APPEXECFWK_HIDUMP_ERROR;
2958     }
2959 
2960     int ret = dprintf(fd, "%s\n", result.c_str());
2961     if (ret < 0) {
2962         APP_LOGE("dprintf error");
2963         return ERR_APPEXECFWK_HIDUMP_ERROR;
2964     }
2965 
2966     return ERR_OK;
2967 }
2968 
GetAllDependentModuleNames(const std::string & bundleName,const std::string & moduleName,std::vector<std::string> & dependentModuleNames)2969 bool BundleMgrHostImpl::GetAllDependentModuleNames(const std::string &bundleName, const std::string &moduleName,
2970     std::vector<std::string> &dependentModuleNames)
2971 {
2972     APP_LOGD("GetAllDependentModuleNames: bundleName: %{public}s, moduleName: %{public}s",
2973         bundleName.c_str(), moduleName.c_str());
2974     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
2975         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
2976         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
2977         APP_LOGE("verify permission failed");
2978         return false;
2979     }
2980     auto dataMgr = GetDataMgrFromService();
2981     if (dataMgr == nullptr) {
2982         APP_LOGE("DataMgr is nullptr");
2983         return false;
2984     }
2985     return dataMgr->GetAllDependentModuleNames(bundleName, moduleName, dependentModuleNames);
2986 }
2987 
GetSandboxBundleInfo(const std::string & bundleName,int32_t appIndex,int32_t userId,BundleInfo & info)2988 ErrCode BundleMgrHostImpl::GetSandboxBundleInfo(
2989     const std::string &bundleName, int32_t appIndex, int32_t userId, BundleInfo &info)
2990 {
2991     APP_LOGD("start GetSandboxBundleInfo, bundleName : %{public}s, appIndex : %{public}d, userId : %{public}d",
2992         bundleName.c_str(), appIndex, userId);
2993     // check bundle name
2994     if (bundleName.empty()) {
2995         APP_LOGE("GetSandboxBundleInfo failed due to empty bundleName");
2996         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2997     }
2998     // check appIndex
2999     if (appIndex <= Constants::INITIAL_SANDBOX_APP_INDEX || appIndex > Constants::MAX_SANDBOX_APP_INDEX) {
3000         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
3001         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
3002     }
3003     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3004         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3005         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3006         APP_LOGE("verify permission failed");
3007         return ERR_APPEXECFWK_PERMISSION_DENIED;
3008     }
3009     auto dataMgr = GetDataMgrFromService();
3010     if (dataMgr == nullptr) {
3011         APP_LOGE("DataMgr is nullptr");
3012         return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR;
3013     }
3014     auto sandboxAppHelper = dataMgr->GetSandboxAppHelper();
3015     if (sandboxAppHelper == nullptr) {
3016         APP_LOGE("sandboxAppHelper is nullptr");
3017         return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR;
3018     }
3019     int32_t requestUserId = dataMgr->GetUserId(userId);
3020     if (requestUserId == Constants::INVALID_USERID) {
3021         return ERR_APPEXECFWK_SANDBOX_QUERY_INVALID_USER_ID;
3022     }
3023     return sandboxAppHelper->GetSandboxAppBundleInfo(bundleName, appIndex, requestUserId, info);
3024 }
3025 
ObtainCallingBundleName(std::string & bundleName)3026 bool BundleMgrHostImpl::ObtainCallingBundleName(std::string &bundleName)
3027 {
3028     auto dataMgr = GetDataMgrFromService();
3029     if (dataMgr == nullptr) {
3030         APP_LOGE("DataMgr is nullptr");
3031         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3032     }
3033     bool ret = dataMgr->GetBundleNameForUid(IPCSkeleton::GetCallingUid(), bundleName);
3034     if (!ret) {
3035         APP_LOGE("query calling bundle name failed");
3036         return false;
3037     }
3038     APP_LOGD("calling bundleName is : %{public}s", bundleName.c_str());
3039     return ret;
3040 }
3041 
GetBundleStats(const std::string & bundleName,int32_t userId,std::vector<int64_t> & bundleStats,int32_t appIndex,uint32_t statFlag)3042 bool BundleMgrHostImpl::GetBundleStats(const std::string &bundleName, int32_t userId,
3043     std::vector<int64_t> &bundleStats, int32_t appIndex, uint32_t statFlag)
3044 {
3045     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3046         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3047         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3048         APP_LOGE("verify permission failed");
3049         return false;
3050     }
3051     if (bundleName.empty()) {
3052         APP_LOGE("bundleName empty");
3053         return false;
3054     }
3055     if (!CheckAppIndex(bundleName, userId, appIndex)) {
3056         return false;
3057     }
3058     auto dataMgr = GetDataMgrFromService();
3059     if (dataMgr == nullptr) {
3060         APP_LOGE("DataMgr is nullptr");
3061         return false;
3062     }
3063     if (isBrokerServiceExisted_ && !IsBundleExist(bundleName)) {
3064         auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
3065         ErrCode ret = bmsExtensionClient->GetBundleStats(bundleName, userId, bundleStats);
3066         APP_LOGI("ret : %{public}d", ret);
3067         return ret == ERR_OK;
3068     }
3069     return dataMgr->GetBundleStats(bundleName, userId, bundleStats, appIndex, statFlag);
3070 }
3071 
GetAllBundleStats(int32_t userId,std::vector<int64_t> & bundleStats)3072 bool BundleMgrHostImpl::GetAllBundleStats(int32_t userId, std::vector<int64_t> &bundleStats)
3073 {
3074     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3075         Constants::PERMISSION_GET_BUNDLE_INFO})) {
3076         APP_LOGE("verify permission failed");
3077         return false;
3078     }
3079     auto dataMgr = GetDataMgrFromService();
3080     if (dataMgr == nullptr) {
3081         APP_LOGE("DataMgr is nullptr");
3082         return false;
3083     }
3084     return dataMgr->GetAllBundleStats(userId, bundleStats);
3085 }
3086 
GetStringById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,int32_t userId,const std::string & localeInfo)3087 std::string BundleMgrHostImpl::GetStringById(const std::string &bundleName, const std::string &moduleName,
3088     uint32_t resId, int32_t userId, const std::string &localeInfo)
3089 {
3090     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3091         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3092         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3093         APP_LOGE("verify token type failed");
3094         return Constants::EMPTY_STRING;
3095     }
3096     auto dataMgr = GetDataMgrFromService();
3097     if (dataMgr == nullptr) {
3098         APP_LOGE("DataMgr is nullptr");
3099         return Constants::EMPTY_STRING;
3100     }
3101     return dataMgr->GetStringById(bundleName, moduleName, resId, userId, localeInfo);
3102 }
3103 
GetIconById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,uint32_t density,int32_t userId)3104 std::string BundleMgrHostImpl::GetIconById(
3105     const std::string &bundleName, const std::string &moduleName, uint32_t resId, uint32_t density, int32_t userId)
3106 {
3107     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3108         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3109         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3110         APP_LOGE("verify token type failed");
3111         return Constants::EMPTY_STRING;
3112     }
3113     auto dataMgr = GetDataMgrFromService();
3114     if (dataMgr == nullptr) {
3115         APP_LOGE("DataMgr is nullptr");
3116         return Constants::EMPTY_STRING;
3117     }
3118     return dataMgr->GetIconById(bundleName, moduleName, resId, density, userId);
3119 }
3120 
3121 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
GetDefaultAppProxy()3122 sptr<IDefaultApp> BundleMgrHostImpl::GetDefaultAppProxy()
3123 {
3124     return DelayedSingleton<BundleMgrService>::GetInstance()->GetDefaultAppProxy();
3125 }
3126 #endif
3127 
3128 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
GetAppControlProxy()3129 sptr<IAppControlMgr> BundleMgrHostImpl::GetAppControlProxy()
3130 {
3131     return DelayedSingleton<BundleMgrService>::GetInstance()->GetAppControlProxy();
3132 }
3133 #endif
3134 
GetQuickFixManagerProxy()3135 sptr<IQuickFixManager> BundleMgrHostImpl::GetQuickFixManagerProxy()
3136 {
3137 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
3138     return DelayedSingleton<BundleMgrService>::GetInstance()->GetQuickFixManagerProxy();
3139 #else
3140     return nullptr;
3141 #endif
3142 }
3143 
GetOverlayManagerProxy()3144 sptr<IOverlayManager> BundleMgrHostImpl::GetOverlayManagerProxy()
3145 {
3146 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
3147     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_OVERLAY_MANAGER_PROXY);
3148     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
3149     return DelayedSingleton<BundleMgrService>::GetInstance()->GetOverlayManagerProxy();
3150 #else
3151     return nullptr;
3152 #endif
3153 }
3154 
GetSandboxAbilityInfo(const Want & want,int32_t appIndex,int32_t flags,int32_t userId,AbilityInfo & info)3155 ErrCode BundleMgrHostImpl::GetSandboxAbilityInfo(const Want &want, int32_t appIndex, int32_t flags, int32_t userId,
3156     AbilityInfo &info)
3157 {
3158     APP_LOGD("start GetSandboxAbilityInfo appIndex : %{public}d, userId : %{public}d", appIndex, userId);
3159     // check appIndex
3160     if (appIndex <= Constants::INITIAL_SANDBOX_APP_INDEX || appIndex > Constants::MAX_SANDBOX_APP_INDEX) {
3161         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
3162         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
3163     }
3164     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3165         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3166         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
3167         APP_LOGE("verify permission failed");
3168         return ERR_APPEXECFWK_PERMISSION_DENIED;
3169     }
3170     auto dataMgr = GetDataMgrFromService();
3171     if (dataMgr == nullptr) {
3172         APP_LOGE("DataMgr is nullptr");
3173         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3174     }
3175 
3176     if (!(dataMgr->QueryAbilityInfo(want, flags, userId, info, appIndex)
3177         || dataMgr->QueryAbilityInfo(want, flags, Constants::DEFAULT_USERID, info, appIndex))) {
3178         APP_LOGE("query ability info failed");
3179         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3180     }
3181     return ERR_OK;
3182 }
3183 
GetSandboxExtAbilityInfos(const Want & want,int32_t appIndex,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos)3184 ErrCode BundleMgrHostImpl::GetSandboxExtAbilityInfos(const Want &want, int32_t appIndex, int32_t flags,
3185     int32_t userId, std::vector<ExtensionAbilityInfo> &infos)
3186 {
3187     APP_LOGD("start GetSandboxExtAbilityInfos appIndex : %{public}d, userId : %{public}d", appIndex, userId);
3188     // check appIndex
3189     if (appIndex <= Constants::INITIAL_SANDBOX_APP_INDEX || appIndex > Constants::MAX_SANDBOX_APP_INDEX) {
3190         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
3191         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
3192     }
3193     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3194         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3195         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
3196         APP_LOGE("verify permission failed");
3197         return ERR_APPEXECFWK_PERMISSION_DENIED;
3198     }
3199     auto dataMgr = GetDataMgrFromService();
3200     if (dataMgr == nullptr) {
3201         APP_LOGE("DataMgr is nullptr");
3202         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3203     }
3204 
3205     if (!(dataMgr->QueryExtensionAbilityInfos(want, flags, userId, infos, appIndex)
3206         || dataMgr->QueryExtensionAbilityInfos(want, flags, Constants::DEFAULT_USERID, infos, appIndex))) {
3207         APP_LOGE("query extension ability info failed");
3208         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3209     }
3210     return ERR_OK;
3211 }
3212 
GetSandboxHapModuleInfo(const AbilityInfo & abilityInfo,int32_t appIndex,int32_t userId,HapModuleInfo & info)3213 ErrCode BundleMgrHostImpl::GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, int32_t appIndex, int32_t userId,
3214     HapModuleInfo &info)
3215 {
3216     APP_LOGD("start GetSandboxHapModuleInfo appIndex : %{public}d, userId : %{public}d", appIndex, userId);
3217     // check appIndex
3218     if (appIndex <= Constants::INITIAL_SANDBOX_APP_INDEX || appIndex > Constants::MAX_SANDBOX_APP_INDEX) {
3219         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
3220         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
3221     }
3222     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3223         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3224         !BundlePermissionMgr::IsBundleSelfCalling(abilityInfo.bundleName)) {
3225         APP_LOGE("verify permission failed");
3226         return ERR_APPEXECFWK_PERMISSION_DENIED;
3227     }
3228     auto dataMgr = GetDataMgrFromService();
3229     if (dataMgr == nullptr) {
3230         APP_LOGE("DataMgr is nullptr");
3231         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3232     }
3233     auto sandboxAppHelper = dataMgr->GetSandboxAppHelper();
3234     if (sandboxAppHelper == nullptr) {
3235         APP_LOGE("sandboxAppHelper is nullptr");
3236         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
3237     }
3238     int32_t requestUserId = dataMgr->GetUserId(userId);
3239     if (requestUserId == Constants::INVALID_USERID) {
3240         return ERR_APPEXECFWK_SANDBOX_QUERY_INVALID_USER_ID;
3241     }
3242     return sandboxAppHelper->GetSandboxHapModuleInfo(abilityInfo, appIndex, requestUserId, info);
3243 }
3244 
GetMediaData(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::unique_ptr<uint8_t[]> & mediaDataPtr,size_t & len,int32_t userId)3245 ErrCode BundleMgrHostImpl::GetMediaData(const std::string &bundleName, const std::string &moduleName,
3246     const std::string &abilityName, std::unique_ptr<uint8_t[]> &mediaDataPtr, size_t &len, int32_t userId)
3247 {
3248     // API9 need to be system app, otherwise return empty data
3249     if (!BundlePermissionMgr::IsSystemApp() &&
3250         !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
3251         APP_LOGE("non-system app calling system api");
3252         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3253     }
3254     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3255         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3256         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3257         APP_LOGE("verify permission failed");
3258         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3259     }
3260     auto dataMgr = GetDataMgrFromService();
3261     if (dataMgr == nullptr) {
3262         APP_LOGE("DataMgr is nullptr");
3263         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3264     }
3265     return dataMgr->GetMediaData(bundleName, moduleName, abilityName, mediaDataPtr, len, userId);
3266 }
3267 
NotifyBundleStatus(const NotifyBundleEvents & installRes)3268 void BundleMgrHostImpl::NotifyBundleStatus(const NotifyBundleEvents &installRes)
3269 {
3270     std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
3271     commonEventMgr->NotifyBundleStatus(installRes, nullptr);
3272 }
3273 
SetDebugMode(bool isDebug)3274 ErrCode BundleMgrHostImpl::SetDebugMode(bool isDebug)
3275 {
3276     int32_t callingUid = IPCSkeleton::GetCallingUid();
3277     if (callingUid != Constants::ROOT_UID && callingUid != ServiceConstants::BMS_UID) {
3278         APP_LOGE("invalid calling uid %{public}d to set debug mode", callingUid);
3279         return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_UID_CHECK_FAILED;
3280     }
3281     if (isDebug) {
3282         BundleVerifyMgr::EnableDebug();
3283     } else {
3284         BundleVerifyMgr::DisableDebug();
3285     }
3286     return ERR_OK;
3287 }
3288 
VerifySystemApi(int32_t beginApiVersion)3289 bool BundleMgrHostImpl::VerifySystemApi(int32_t beginApiVersion)
3290 {
3291     APP_LOGD("begin to verify system app");
3292     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_VERIFY_SYSTEM_API);
3293     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
3294     return BundlePermissionMgr::VerifySystemApp(beginApiVersion);
3295 }
3296 
GetAppProvisionInfo(const std::string & bundleName,int32_t userId,AppProvisionInfo & appProvisionInfo)3297 ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, int32_t userId,
3298     AppProvisionInfo &appProvisionInfo)
3299 {
3300     APP_LOGD("begin to GetAppProvisionInfo bundleName: %{public}s, userId: %{public}d", bundleName.c_str(),
3301         userId);
3302     if (!BundlePermissionMgr::IsSystemApp()) {
3303         APP_LOGE("non-system app calling system api");
3304         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3305     }
3306     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
3307         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3308         APP_LOGE("verify permission failed");
3309         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3310     }
3311     auto dataMgr = GetDataMgrFromService();
3312     if (dataMgr == nullptr) {
3313         APP_LOGE("DataMgr is nullptr");
3314         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3315     }
3316     return dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo);
3317 }
3318 
GetProvisionMetadata(const std::string & bundleName,int32_t userId,std::vector<Metadata> & provisionMetadatas)3319 ErrCode BundleMgrHostImpl::GetProvisionMetadata(const std::string &bundleName, int32_t userId,
3320     std::vector<Metadata> &provisionMetadatas)
3321 {
3322     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3323         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3324         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3325         APP_LOGE("verify permission failed");
3326         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3327     }
3328     auto dataMgr = GetDataMgrFromService();
3329     if (dataMgr == nullptr) {
3330         APP_LOGE("DataMgr is nullptr");
3331         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3332     }
3333     return dataMgr->GetProvisionMetadata(bundleName, userId, provisionMetadatas);
3334 }
3335 
GetAllSharedBundleInfo(std::vector<SharedBundleInfo> & sharedBundles)3336 ErrCode BundleMgrHostImpl::GetAllSharedBundleInfo(std::vector<SharedBundleInfo> &sharedBundles)
3337 {
3338     APP_LOGD("begin to GetAllSharedBundleInfo");
3339     if (!BundlePermissionMgr::IsSystemApp()) {
3340         APP_LOGE("non-system app calling system api");
3341         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3342     }
3343     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3344         APP_LOGE("verify permission failed");
3345         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3346     }
3347 
3348     auto dataMgr = GetDataMgrFromService();
3349     if (dataMgr == nullptr) {
3350         APP_LOGE("dataMgr is nullptr");
3351         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3352     }
3353     return dataMgr->GetAllSharedBundleInfo(sharedBundles);
3354 }
3355 
GetSharedBundleInfo(const std::string & bundleName,const std::string & moduleName,std::vector<SharedBundleInfo> & sharedBundles)3356 ErrCode BundleMgrHostImpl::GetSharedBundleInfo(const std::string &bundleName, const std::string &moduleName,
3357     std::vector<SharedBundleInfo> &sharedBundles)
3358 {
3359     APP_LOGD("GetSharedBundleInfo: bundleName: %{public}s, moduleName: %{public}s",
3360         bundleName.c_str(), moduleName.c_str());
3361     if (!BundlePermissionMgr::IsSystemApp()) {
3362         APP_LOGE("non-system app calling system api");
3363         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3364     }
3365     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
3366         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3367         APP_LOGE("verify permission failed");
3368         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3369     }
3370 
3371     auto dataMgr = GetDataMgrFromService();
3372     if (dataMgr == nullptr) {
3373         APP_LOGE("dataMgr is nullptr");
3374         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3375     }
3376     return dataMgr->GetSharedBundleInfo(bundleName, moduleName, sharedBundles);
3377 }
3378 
GetSharedBundleInfoBySelf(const std::string & bundleName,SharedBundleInfo & sharedBundleInfo)3379 ErrCode BundleMgrHostImpl::GetSharedBundleInfoBySelf(const std::string &bundleName,
3380     SharedBundleInfo &sharedBundleInfo)
3381 {
3382     APP_LOGD("begin to GetSharedBundleInfoBySelf bundleName: %{public}s", bundleName.c_str());
3383     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_SHARED_BUNDLE_INFO_BY_SELF);
3384     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
3385     if (!BundlePermissionMgr::IsSystemApp()) {
3386         APP_LOGE("non-system app calling system api");
3387         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3388     }
3389     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3390         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3391         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3392         APP_LOGE("verify permission failed");
3393         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3394     }
3395 
3396     auto dataMgr = GetDataMgrFromService();
3397     if (dataMgr == nullptr) {
3398         APP_LOGE("DataMgr is nullptr");
3399         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3400     }
3401     return dataMgr->GetSharedBundleInfoBySelf(bundleName, sharedBundleInfo);
3402 }
3403 
GetSharedDependencies(const std::string & bundleName,const std::string & moduleName,std::vector<Dependency> & dependencies)3404 ErrCode BundleMgrHostImpl::GetSharedDependencies(const std::string &bundleName, const std::string &moduleName,
3405     std::vector<Dependency> &dependencies)
3406 {
3407     APP_LOGD("GetSharedDependencies: bundleName: %{public}s, moduleName: %{public}s",
3408         bundleName.c_str(), moduleName.c_str());
3409     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
3410         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3411         APP_LOGE("verify permission failed");
3412         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3413     }
3414     auto dataMgr = GetDataMgrFromService();
3415     if (dataMgr == nullptr) {
3416         APP_LOGE("DataMgr is nullptr");
3417         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3418     }
3419     return dataMgr->GetSharedDependencies(bundleName, moduleName, dependencies);
3420 }
3421 
VerifyDependency(const std::string & sharedBundleName)3422 bool BundleMgrHostImpl::VerifyDependency(const std::string &sharedBundleName)
3423 {
3424     auto dataMgr = GetDataMgrFromService();
3425     if (dataMgr == nullptr) {
3426         APP_LOGE("DataMgr is nullptr");
3427         return false;
3428     }
3429 
3430     std::string callingBundleName;
3431     bool ret = dataMgr->GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
3432     if (!ret) {
3433         APP_LOGE("GetBundleNameForUid failed");
3434         return false;
3435     }
3436 
3437     InnerBundleInfo callingBundleInfo;
3438     if (!dataMgr->FetchInnerBundleInfo(callingBundleName, callingBundleInfo)) {
3439         APP_LOGE("get %{public}s failed", callingBundleName.c_str());
3440         return false;
3441     }
3442 
3443     // check whether callingBundleName is dependent on sharedBundleName
3444     const auto& dependencies = callingBundleInfo.GetDependencies();
3445     auto iter = std::find_if(dependencies.begin(), dependencies.end(), [&sharedBundleName](const auto &dependency) {
3446         return dependency.bundleName == sharedBundleName;
3447     });
3448     if (iter == dependencies.end()) {
3449         APP_LOGE("%{public}s is not dependent on %{public}s", callingBundleName.c_str(), sharedBundleName.c_str());
3450         return false;
3451     }
3452     APP_LOGD("verify dependency successfully");
3453     return true;
3454 }
3455 
IsPreInstallApp(const std::string & bundleName)3456 bool BundleMgrHostImpl::IsPreInstallApp(const std::string &bundleName)
3457 {
3458     auto dataMgr = GetDataMgrFromService();
3459     if (dataMgr == nullptr) {
3460         APP_LOGE("DataMgr is nullptr");
3461         return false;
3462     }
3463     return dataMgr->IsPreInstallApp(bundleName);
3464 }
3465 
GetProxyDataInfos(const std::string & bundleName,const std::string & moduleName,std::vector<ProxyData> & proxyDatas,int32_t userId)3466 ErrCode BundleMgrHostImpl::GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
3467     std::vector<ProxyData> &proxyDatas, int32_t userId)
3468 {
3469     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3470         APP_LOGE("verify token type failed");
3471         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3472     }
3473     auto dataMgr = GetDataMgrFromService();
3474     if (dataMgr == nullptr) {
3475         APP_LOGE("DataMgr is nullptr");
3476         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3477     }
3478     return dataMgr->GetProxyDataInfos(bundleName, moduleName, userId, proxyDatas);
3479 }
3480 
GetAllProxyDataInfos(std::vector<ProxyData> & proxyDatas,int32_t userId)3481 ErrCode BundleMgrHostImpl::GetAllProxyDataInfos(std::vector<ProxyData> &proxyDatas, int32_t userId)
3482 {
3483     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3484         APP_LOGE("verify token type failed");
3485         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3486     }
3487     auto dataMgr = GetDataMgrFromService();
3488     if (dataMgr == nullptr) {
3489         APP_LOGE("DataMgr is nullptr");
3490         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3491     }
3492     return dataMgr->GetAllProxyDataInfos(userId, proxyDatas);
3493 }
3494 
GetSpecifiedDistributionType(const std::string & bundleName,std::string & specifiedDistributionType)3495 ErrCode BundleMgrHostImpl::GetSpecifiedDistributionType(const std::string &bundleName,
3496     std::string &specifiedDistributionType)
3497 {
3498     APP_LOGD("GetSpecifiedDistributionType bundleName: %{public}s", bundleName.c_str());
3499     if (!BundlePermissionMgr::IsSystemApp()) {
3500         APP_LOGE("non-system app calling system api");
3501         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3502     }
3503     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
3504         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3505         APP_LOGE("verify permission failed");
3506         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3507     }
3508 
3509     auto dataMgr = GetDataMgrFromService();
3510     if (dataMgr == nullptr) {
3511         APP_LOGE("dataMgr is nullptr");
3512         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3513     }
3514     return dataMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType);
3515 }
3516 
GetAdditionalInfo(const std::string & bundleName,std::string & additionalInfo)3517 ErrCode BundleMgrHostImpl::GetAdditionalInfo(const std::string &bundleName,
3518     std::string &additionalInfo)
3519 {
3520     APP_LOGD("GetAdditionalInfo bundleName: %{public}s", bundleName.c_str());
3521     if (!BundlePermissionMgr::IsSystemApp()) {
3522         APP_LOGE("non-system app calling system api");
3523         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3524     }
3525     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3526         APP_LOGE("verify permission failed");
3527         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3528     }
3529 
3530     auto dataMgr = GetDataMgrFromService();
3531     if (dataMgr == nullptr) {
3532         APP_LOGE("dataMgr is nullptr");
3533         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3534     }
3535     return dataMgr->GetAdditionalInfo(bundleName, additionalInfo);
3536 }
3537 
SetExtNameOrMIMEToApp(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const std::string & extName,const std::string & mimeType)3538 ErrCode BundleMgrHostImpl::SetExtNameOrMIMEToApp(const std::string &bundleName, const std::string &moduleName,
3539     const std::string &abilityName, const std::string &extName, const std::string &mimeType)
3540 {
3541     APP_LOGD("SetExtNameOrMIMEToApp bundleName: %{public}s, moduleName: %{public}s, \
3542         abilityName: %{public}s, extName: %{public}s, mimeType: %{public}s",
3543         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), extName.c_str(), mimeType.c_str());
3544     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3545         APP_LOGE("verify permission failed");
3546         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3547     }
3548     auto dataMgr = GetDataMgrFromService();
3549     if (dataMgr == nullptr) {
3550         APP_LOGE("dataMgr is nullptr");
3551         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3552     }
3553     return dataMgr->SetExtNameOrMIMEToApp(bundleName, moduleName, abilityName, extName, mimeType);
3554 }
3555 
DelExtNameOrMIMEToApp(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const std::string & extName,const std::string & mimeType)3556 ErrCode BundleMgrHostImpl::DelExtNameOrMIMEToApp(const std::string &bundleName, const std::string &moduleName,
3557     const std::string &abilityName, const std::string &extName, const std::string &mimeType)
3558 {
3559     APP_LOGD("DelExtNameOrMIMEToApp bundleName: %{public}s, moduleName: %{public}s, \
3560         abilityName: %{public}s, extName: %{public}s, mimeType: %{public}s",
3561         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), extName.c_str(), mimeType.c_str());
3562     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3563         APP_LOGE("verify permission failed");
3564         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3565     }
3566     auto dataMgr = GetDataMgrFromService();
3567     if (dataMgr == nullptr) {
3568         APP_LOGE("dataMgr is nullptr");
3569         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3570     }
3571     return dataMgr->DelExtNameOrMIMEToApp(bundleName, moduleName, abilityName, extName, mimeType);
3572 }
3573 
QueryDataGroupInfos(const std::string & bundleName,int32_t userId,std::vector<DataGroupInfo> & infos)3574 bool BundleMgrHostImpl::QueryDataGroupInfos(const std::string &bundleName, int32_t userId,
3575     std::vector<DataGroupInfo> &infos)
3576 {
3577     APP_LOGD("QueryDataGroupInfos bundleName: %{public}s, userId: %{public}d", bundleName.c_str(), userId);
3578     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3579         APP_LOGE("verify permission failed");
3580         return false;
3581     }
3582     auto dataMgr = GetDataMgrFromService();
3583     if (dataMgr == nullptr) {
3584         APP_LOGE("dataMgr is nullptr");
3585         return false;
3586     }
3587     return dataMgr->QueryDataGroupInfos(bundleName, userId, infos);
3588 }
3589 
GetGroupDir(const std::string & dataGroupId,std::string & dir)3590 bool BundleMgrHostImpl::GetGroupDir(const std::string &dataGroupId, std::string &dir)
3591 {
3592     APP_LOGD("GetGroupDir dataGroupId: %{public}s", dataGroupId.c_str());
3593     auto dataMgr = GetDataMgrFromService();
3594     if (dataMgr == nullptr) {
3595         APP_LOGE("dataMgr is nullptr");
3596         return false;
3597     }
3598     return dataMgr->GetGroupDir(dataGroupId, dir);
3599 }
3600 
SetBrokerServiceStatus(bool isServiceExisted)3601 void BundleMgrHostImpl::SetBrokerServiceStatus(bool isServiceExisted)
3602 {
3603     APP_LOGD("broker service status is %{public}d", isServiceExisted);
3604     isBrokerServiceExisted_ = isServiceExisted;
3605 }
3606 
QueryAppGalleryBundleName(std::string & bundleName)3607 bool BundleMgrHostImpl::QueryAppGalleryBundleName(std::string &bundleName)
3608 {
3609     APP_LOGD("QueryAppGalleryBundleName in bundle host impl start");
3610     auto dataMgr = GetDataMgrFromService();
3611     if (dataMgr == nullptr) {
3612         APP_LOGE("DataMgr is nullptr");
3613         return false;
3614     }
3615     std::string abilityName;
3616     bool ret = dataMgr->QueryAppGalleryAbilityName(bundleName, abilityName);
3617     if (!ret) {
3618         APP_LOGE("get bundleName failed");
3619         return false;
3620     }
3621     APP_LOGD("bundleName is %{public}s", bundleName.c_str());
3622     return  true;
3623 }
3624 
QueryExtensionAbilityInfosWithTypeName(const Want & want,const std::string & typeName,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)3625 ErrCode BundleMgrHostImpl::QueryExtensionAbilityInfosWithTypeName(const Want &want, const std::string &typeName,
3626     int32_t flags, int32_t userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
3627 {
3628     if (!BundlePermissionMgr::IsSystemApp()) {
3629         LOG_E(BMS_TAG_QUERY, "Non-system app calling system api");
3630         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3631     }
3632     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3633         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3634         !BundlePermissionMgr::IsBundleSelfCalling(want.GetElement().GetBundleName())) {
3635         LOG_E(BMS_TAG_QUERY, "Verify permission failed");
3636         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3637     }
3638     auto dataMgr = GetDataMgrFromService();
3639     if (dataMgr == nullptr) {
3640         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
3641         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3642     }
3643     std::vector<ExtensionAbilityInfo> infos;
3644     ErrCode ret = dataMgr->QueryExtensionAbilityInfosV9(want, flags, userId, infos);
3645     dataMgr->QueryAllCloneExtensionInfosV9(want, flags, userId, infos);
3646     if (infos.empty()) {
3647         if (ret != ERR_OK) {
3648             LOG_E(BMS_TAG_QUERY,
3649                 "QueryExtensionAbilityInfosV9 is failed, -type %{public}s, -f %{public}d -u %{public}d ret: %{public}d",
3650                 typeName.c_str(), flags, userId, ret);
3651             return ret;
3652         }
3653         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3654     }
3655     if (typeName.empty()) {
3656         extensionInfos = infos;
3657     } else {
3658         for_each(infos.begin(), infos.end(), [&typeName, &extensionInfos](const auto &info)->decltype(auto) {
3659             APP_LOGD("Input typeName is %{public}s, info.type is %{public}s",
3660                 typeName.c_str(), info.extensionTypeName.c_str());
3661             if (typeName == info.extensionTypeName) {
3662                 extensionInfos.emplace_back(info);
3663             }
3664         });
3665     }
3666     if (extensionInfos.empty()) {
3667         LOG_E(BMS_TAG_QUERY, "No valid extension info can be inquired");
3668         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3669     }
3670     return ERR_OK;
3671 }
3672 
QueryExtensionAbilityInfosOnlyWithTypeName(const std::string & typeName,uint32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)3673 ErrCode BundleMgrHostImpl::QueryExtensionAbilityInfosOnlyWithTypeName(const std::string &typeName,
3674     uint32_t flags, int32_t userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
3675 {
3676     if (!BundlePermissionMgr::IsSystemApp()) {
3677         APP_LOGE("Non-system app calling system api");
3678         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3679     }
3680     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3681         Constants::PERMISSION_GET_BUNDLE_INFO})) {
3682         APP_LOGE("Verify permission failed");
3683         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3684     }
3685     auto dataMgr = GetDataMgrFromService();
3686     if (dataMgr == nullptr) {
3687         APP_LOGE("DataMgr is nullptr");
3688         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3689     }
3690     if (typeName.empty()) {
3691         APP_LOGE("Input typeName is empty");
3692         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3693     }
3694     std::vector<ExtensionAbilityInfo> infos;
3695     ErrCode ret = dataMgr->QueryExtensionAbilityInfosByExtensionTypeName(typeName, flags, userId, infos);
3696     if (ret != ERR_OK) {
3697         APP_LOGE("QueryExtensionAbilityInfos is failed");
3698         return ret;
3699     }
3700     if ((flags &
3701         static_cast<uint32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_BY_TYPE_NAME)) ==
3702         static_cast<uint32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_BY_TYPE_NAME)) {
3703         extensionInfos = infos;
3704         return ret;
3705     }
3706     for_each(infos.begin(), infos.end(), [&typeName, &extensionInfos](const auto &info)->decltype(auto) {
3707         APP_LOGD("Input typeName is %{public}s, info.type is %{public}s",
3708             typeName.c_str(), info.extensionTypeName.c_str());
3709         if (typeName == info.extensionTypeName) {
3710             extensionInfos.emplace_back(info);
3711         }
3712     });
3713     if (extensionInfos.empty()) {
3714         APP_LOGE("No valid extension info can be inquired");
3715         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3716     }
3717     return ERR_OK;
3718 }
3719 
ResetAOTCompileStatus(const std::string & bundleName,const std::string & moduleName,int32_t triggerMode)3720 ErrCode BundleMgrHostImpl::ResetAOTCompileStatus(const std::string &bundleName, const std::string &moduleName,
3721     int32_t triggerMode)
3722 {
3723     APP_LOGD("ResetAOTCompileStatus begin");
3724     auto dataMgr = GetDataMgrFromService();
3725     if (dataMgr == nullptr) {
3726         APP_LOGE("dataMgr is null");
3727         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3728     }
3729     std::string callingBundleName;
3730     ErrCode ret = dataMgr->GetNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
3731     if (ret != ERR_OK || bundleName != callingBundleName) {
3732         APP_LOGE("verify permission failed");
3733         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3734     }
3735     return dataMgr->ResetAOTCompileStatus(bundleName, moduleName, triggerMode);
3736 }
3737 
GetJsonProfile(ProfileType profileType,const std::string & bundleName,const std::string & moduleName,std::string & profile,int32_t userId)3738 ErrCode BundleMgrHostImpl::GetJsonProfile(ProfileType profileType, const std::string &bundleName,
3739     const std::string &moduleName, std::string &profile, int32_t userId)
3740 {
3741     APP_LOGD("GetJsonProfile profileType: %{public}d, bundleName: %{public}s, moduleName: %{public}s"
3742         "userId: %{public}d", profileType, bundleName.c_str(), moduleName.c_str(), userId);
3743     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED,
3744         Constants::PERMISSION_GET_BUNDLE_INFO}) &&
3745         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
3746         APP_LOGE("verify permission failed");
3747         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3748     }
3749     if (!BundlePermissionMgr::IsSystemApp() &&
3750         profileType != ProfileType::NETWORK_PROFILE &&
3751         profileType != ProfileType::PKG_CONTEXT_PROFILE) {
3752         APP_LOGE("non-system app calling system api");
3753         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3754     }
3755     auto dataMgr = GetDataMgrFromService();
3756     if (dataMgr == nullptr) {
3757         APP_LOGE("dataMgr is nullptr");
3758         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3759     }
3760     return dataMgr->GetJsonProfile(profileType, bundleName, moduleName, profile, userId);
3761 }
3762 
SetAdditionalInfo(const std::string & bundleName,const std::string & additionalInfo)3763 ErrCode BundleMgrHostImpl::SetAdditionalInfo(const std::string &bundleName, const std::string &additionalInfo)
3764 {
3765     APP_LOGD("Called. BundleName: %{public}s", bundleName.c_str());
3766     if (!BundlePermissionMgr::IsSystemApp()) {
3767         APP_LOGE("Non-system app calling system api");
3768         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3769     }
3770 
3771     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3772         APP_LOGE("Verify permission failed");
3773         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3774     }
3775 
3776     std::string appGalleryBundleName;
3777     QueryAppGalleryBundleName(appGalleryBundleName);
3778 
3779     std::string callingBundleName;
3780     ObtainCallingBundleName(callingBundleName);
3781 
3782     if (appGalleryBundleName.empty() || callingBundleName.empty() || appGalleryBundleName != callingBundleName) {
3783         APP_LOGE("Failed, appGalleryBundleName: %{public}s. callingBundleName: %{public}s",
3784             appGalleryBundleName.c_str(), callingBundleName.c_str());
3785         return ERR_BUNDLE_MANAGER_NOT_APP_GALLERY_CALL;
3786     }
3787 
3788     auto dataMgr = GetDataMgrFromService();
3789     if (dataMgr == nullptr) {
3790         APP_LOGE("DataMgr is nullptr");
3791         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3792     }
3793     return dataMgr->SetAdditionalInfo(bundleName, additionalInfo);
3794 }
3795 
CreateBundleDataDir(int32_t userId)3796 ErrCode BundleMgrHostImpl::CreateBundleDataDir(int32_t userId)
3797 {
3798     if (!BundlePermissionMgr::IsCallingUidValid(Constants::ROOT_UID)) {
3799         APP_LOGE("IsCallingUidValid failed");
3800         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3801     }
3802 
3803     auto dataMgr = GetDataMgrFromService();
3804     if (dataMgr == nullptr) {
3805         APP_LOGE("DataMgr is nullptr");
3806         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3807     }
3808     return dataMgr->CreateBundleDataDir(userId);
3809 }
3810 
GetBundleResourceProxy()3811 sptr<IBundleResource> BundleMgrHostImpl::GetBundleResourceProxy()
3812 {
3813 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
3814     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_BUNDLE_RESOURCE_PROXY);
3815     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
3816     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleResourceProxy();
3817 #else
3818     return nullptr;
3819 #endif
3820 }
3821 
GetPreferableBundleInfoFromHapPaths(const std::vector<std::string> & hapPaths,BundleInfo & bundleInfo)3822 bool BundleMgrHostImpl::GetPreferableBundleInfoFromHapPaths(const std::vector<std::string> &hapPaths,
3823     BundleInfo &bundleInfo)
3824 {
3825     if (hapPaths.empty()) {
3826         return false;
3827     }
3828     for (auto hapPath: hapPaths) {
3829         BundleInfo resultBundleInfo;
3830         if (!GetBundleArchiveInfo(hapPath, GET_BUNDLE_DEFAULT, resultBundleInfo)) {
3831             return false;
3832         }
3833         bundleInfo = resultBundleInfo;
3834         if (!bundleInfo.hapModuleInfos.empty()) {
3835             bundleInfo.hapModuleInfos[0].hapPath = hapPath;
3836             if (bundleInfo.hapModuleInfos[0].moduleType == ModuleType::ENTRY) {
3837                 return true;
3838             }
3839         }
3840     }
3841     return true;
3842 }
3843 
GetRecoverableApplicationInfo(std::vector<RecoverableApplicationInfo> & recoverableApplicaitons)3844 ErrCode BundleMgrHostImpl::GetRecoverableApplicationInfo(
3845     std::vector<RecoverableApplicationInfo> &recoverableApplicaitons)
3846 {
3847     APP_LOGD("begin to GetRecoverableApplicationInfo");
3848     if (!BundlePermissionMgr::IsSystemApp()) {
3849         APP_LOGE("non-system app calling system api");
3850         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3851     }
3852     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3853         APP_LOGE("verify permission failed");
3854         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3855     }
3856     auto dataMgr = GetDataMgrFromService();
3857     if (dataMgr == nullptr) {
3858         APP_LOGE("dataMgr is nullptr");
3859         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3860     }
3861     int32_t userId = AccountHelper::GetCurrentActiveUserId();
3862     if (userId == Constants::INVALID_USERID) {
3863         APP_LOGE("userId %{public}d is invalid", userId);
3864         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
3865     }
3866     std::vector<PreInstallBundleInfo> recoverableBundleInfos = dataMgr->GetRecoverablePreInstallBundleInfos();
3867     for (auto recoverableBundleInfo: recoverableBundleInfos) {
3868         std::string bundleName = recoverableBundleInfo.GetBundleName();
3869         BmsExtensionDataMgr bmsExtensionDataMgr;
3870         if (bmsExtensionDataMgr.IsAppInBlocklist(bundleName, userId)) {
3871             APP_LOGI("recover app %{public}s is in blocklist", bundleName.c_str());
3872             continue;
3873         }
3874         RecoverableApplicationInfo recoverableApplication;
3875         recoverableApplication.bundleName = bundleName;
3876         recoverableApplication.labelId = recoverableBundleInfo.GetLabelId();
3877         recoverableApplication.iconId = recoverableBundleInfo.GetIconId();
3878         recoverableApplication.systemApp = recoverableBundleInfo.GetSystemApp();
3879         recoverableApplication.codePaths = recoverableBundleInfo.GetBundlePaths();
3880         recoverableApplication.moduleName = recoverableBundleInfo.GetModuleName();
3881         recoverableApplication.bundleType = recoverableBundleInfo.GetBundleType();
3882         recoverableApplicaitons.emplace_back(recoverableApplication);
3883     }
3884     return ERR_OK;
3885 }
3886 
GetUninstalledBundleInfo(const std::string bundleName,BundleInfo & bundleInfo)3887 ErrCode BundleMgrHostImpl::GetUninstalledBundleInfo(const std::string bundleName, BundleInfo &bundleInfo)
3888 {
3889     APP_LOGD("begin to GetUninstalledBundleInfo");
3890     if (!BundlePermissionMgr::IsSystemApp()) {
3891         APP_LOGE("non-system app calling system api");
3892         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3893     }
3894     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3895         APP_LOGE("verify permission failed");
3896         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3897     }
3898     auto dataMgr = GetDataMgrFromService();
3899     if (dataMgr == nullptr) {
3900         APP_LOGE("dataMgr is nullptr");
3901         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3902     }
3903     int32_t userId = AccountHelper::GetCurrentActiveUserId();
3904     if (userId == Constants::INVALID_USERID) {
3905         APP_LOGE("userId %{public}d is invalid", userId);
3906         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
3907     }
3908     if (dataMgr->HasUserInstallInBundle(bundleName, Constants::DEFAULT_USERID) ||
3909         dataMgr->HasUserInstallInBundle(bundleName, userId)) {
3910         APP_LOGE("bundle has installed");
3911         return ERR_APPEXECFWK_FAILED_GET_BUNDLE_INFO;
3912     }
3913     PreInstallBundleInfo preInstallBundleInfo;
3914     if (!dataMgr->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo)) {
3915         APP_LOGE("get preinstallBundleInfo failed");
3916         return ERR_APPEXECFWK_FAILED_GET_BUNDLE_INFO;
3917     }
3918     if (!GetPreferableBundleInfoFromHapPaths(
3919         preInstallBundleInfo.GetBundlePaths(), bundleInfo)) {
3920         APP_LOGE("prefect bundle is not found");
3921         return ERR_APPEXECFWK_FAILED_GET_BUNDLE_INFO;
3922     }
3923     return ERR_OK;
3924 }
3925 
IsBundleExist(const std::string & bundleName)3926 bool BundleMgrHostImpl::IsBundleExist(const std::string &bundleName)
3927 {
3928     auto dataMgr = GetDataMgrFromService();
3929     if (dataMgr == nullptr) {
3930         APP_LOGE("dataMgr is nullptr");
3931         return false;
3932     }
3933     return dataMgr->IsBundleExist(bundleName);
3934 }
3935 
ClearCache(const std::string & bundleName,const sptr<ICleanCacheCallback> cleanCacheCallback,int32_t userId)3936 ErrCode BundleMgrHostImpl::ClearCache(const std::string &bundleName,
3937     const sptr<ICleanCacheCallback> cleanCacheCallback, int32_t userId)
3938 {
3939     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
3940     ErrCode ret = bmsExtensionClient->ClearCache(bundleName, cleanCacheCallback->AsObject(), userId);
3941     APP_LOGI("ret : %{public}d", ret);
3942     if (ret != ERR_OK) {
3943         ret = ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
3944     }
3945     EventReport::SendCleanCacheSysEvent(bundleName, userId, true, ret != ERR_OK);
3946     return ret;
3947 }
3948 
CanOpenLink(const std::string & link,bool & canOpen)3949 ErrCode BundleMgrHostImpl::CanOpenLink(
3950     const std::string &link, bool &canOpen)
3951 {
3952     APP_LOGD("start CanOpenLink, link : %{public}s", link.c_str());
3953     auto dataMgr = GetDataMgrFromService();
3954     if (dataMgr == nullptr) {
3955         APP_LOGE("DataMgr is nullptr");
3956         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3957     }
3958     return dataMgr->CanOpenLink(link, canOpen);
3959 }
3960 
GetOdid(std::string & odid)3961 ErrCode BundleMgrHostImpl::GetOdid(std::string &odid)
3962 {
3963     APP_LOGD("start GetOdid");
3964     auto dataMgr = GetDataMgrFromService();
3965     if (dataMgr == nullptr) {
3966         APP_LOGE("DataMgr is nullptr");
3967         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3968     }
3969     return dataMgr->GetOdid(odid);
3970 }
3971 
GetAllPreinstalledApplicationInfos(std::vector<PreinstalledApplicationInfo> & preinstalledApplicationInfos)3972 ErrCode BundleMgrHostImpl::GetAllPreinstalledApplicationInfos(
3973     std::vector<PreinstalledApplicationInfo> &preinstalledApplicationInfos)
3974 {
3975     if (!BundlePermissionMgr::IsSystemApp()) {
3976         APP_LOGE("Non-system app calling system api");
3977         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
3978     }
3979     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
3980         APP_LOGE("Verify permission failed");
3981         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
3982     }
3983     auto dataMgr = GetDataMgrFromService();
3984     if (dataMgr == nullptr) {
3985         APP_LOGE("DataMgr is nullptr");
3986         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
3987     }
3988     std::vector<PreInstallBundleInfo> preInstallBundleInfos = dataMgr->GetAllPreInstallBundleInfos();
3989     for (auto &preInstallBundleInfo: preInstallBundleInfos) {
3990         PreinstalledApplicationInfo preinstalledApplicationInfo;
3991         preinstalledApplicationInfo.bundleName = preInstallBundleInfo.GetBundleName();
3992         preinstalledApplicationInfo.moduleName = preInstallBundleInfo.GetModuleName();
3993         preinstalledApplicationInfo.labelId = preInstallBundleInfo.GetLabelId();
3994         preinstalledApplicationInfo.iconId = preInstallBundleInfo.GetIconId();
3995         preinstalledApplicationInfos.emplace_back(preinstalledApplicationInfo);
3996     }
3997     return ERR_OK;
3998 }
3999 
GetAllBundleInfoByDeveloperId(const std::string & developerId,std::vector<BundleInfo> & bundleInfos,int32_t userId)4000 ErrCode BundleMgrHostImpl::GetAllBundleInfoByDeveloperId(const std::string &developerId,
4001     std::vector<BundleInfo> &bundleInfos, int32_t userId)
4002 {
4003     APP_LOGI("start GetAllBundleInfoByDeveloperId for developerId: %{public}s with user: %{public}d",
4004         developerId.c_str(), userId);
4005     if (!BundlePermissionMgr::IsSystemApp()) {
4006         APP_LOGE("non-system app calling system api");
4007         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4008     }
4009     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
4010         APP_LOGE("verify permission failed");
4011         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4012     }
4013     APP_LOGI("verify permission success, begin to GetAllBundleInfoByDeveloperId");
4014     auto dataMgr = GetDataMgrFromService();
4015     if (dataMgr == nullptr) {
4016         APP_LOGE("DataMgr is nullptr");
4017         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4018     }
4019     return dataMgr->GetAllBundleInfoByDeveloperId(developerId, bundleInfos, userId);
4020 }
4021 
GetDeveloperIds(const std::string & appDistributionType,std::vector<std::string> & developerIdList,int32_t userId)4022 ErrCode BundleMgrHostImpl::GetDeveloperIds(const std::string &appDistributionType,
4023     std::vector<std::string> &developerIdList, int32_t userId)
4024 {
4025     APP_LOGI("start GetDeveloperIds for appDistributionType: %{public}s with user: %{public}d",
4026         appDistributionType.c_str(), userId);
4027     if (!BundlePermissionMgr::IsSystemApp()) {
4028         APP_LOGE("non-system app calling system api");
4029         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4030     }
4031     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
4032         APP_LOGE("verify permission failed");
4033         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4034     }
4035     APP_LOGI("verify permission success, begin to GetDeveloperIds");
4036     auto dataMgr = GetDataMgrFromService();
4037     if (dataMgr == nullptr) {
4038         APP_LOGE("DataMgr is nullptr");
4039         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4040     }
4041     return dataMgr->GetDeveloperIds(appDistributionType, developerIdList, userId);
4042 }
4043 
SwitchUninstallState(const std::string & bundleName,const bool & state,bool isNeedSendNotify)4044 ErrCode BundleMgrHostImpl::SwitchUninstallState(const std::string &bundleName, const bool &state,
4045     bool isNeedSendNotify)
4046 {
4047     APP_LOGD("start SwitchUninstallState, bundleName : %{public}s, state : %{public}d", bundleName.c_str(), state);
4048     if (!BundlePermissionMgr::IsSystemApp()) {
4049         APP_LOGE("non-system app calling system api");
4050         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4051     }
4052     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(
4053         ServiceConstants::PERMISSION_CHANGE_BUNDLE_UNINSTALL_STATE)) {
4054         APP_LOGE("verify permission failed");
4055         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4056     }
4057     APP_LOGD("verify permission success, begin to SwitchUninstallState");
4058     auto dataMgr = GetDataMgrFromService();
4059     if (dataMgr == nullptr) {
4060         APP_LOGE("DataMgr is nullptr");
4061         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4062     }
4063     auto resCode = dataMgr->SwitchUninstallState(bundleName, state, isNeedSendNotify);
4064     if (resCode != ERR_OK) {
4065         APP_LOGE("set status fail");
4066         return resCode;
4067     }
4068     if (!isNeedSendNotify) {
4069         APP_LOGI("no need notify %{public}s", bundleName.c_str());
4070         return resCode;
4071     }
4072     InnerBundleInfo innerBundleInfo;
4073     bool isSuccess = dataMgr->FetchInnerBundleInfo(bundleName, innerBundleInfo);
4074     if (!isSuccess) {
4075         APP_LOGE("get innerBundleInfo fail");
4076         return resCode;
4077     }
4078     AbilityInfo mainAbilityInfo;
4079     int32_t currentActiveUserId = AccountHelper::GetCurrentActiveUserId();
4080     innerBundleInfo.GetMainAbilityInfo(mainAbilityInfo);
4081     NotifyBundleEvents installRes = {
4082         .isModuleUpdate = false,
4083         .type = NotifyType::UPDATE,
4084         .resultCode = ERR_OK,
4085         .accessTokenId = innerBundleInfo.GetAccessTokenId(currentActiveUserId),
4086         .uid = innerBundleInfo.GetUid(currentActiveUserId),
4087         .bundleType = static_cast<int32_t>(innerBundleInfo.GetApplicationBundleType()),
4088         .bundleName = innerBundleInfo.GetBundleName(),
4089         .modulePackage = innerBundleInfo.GetModuleNameVec()[0],
4090         .abilityName = mainAbilityInfo.name,
4091         .appDistributionType = innerBundleInfo.GetAppDistributionType(),
4092     };
4093     std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
4094     commonEventMgr->NotifyBundleStatus(installRes, dataMgr);
4095     return resCode;
4096 }
4097 
SetProvisionInfoToInnerBundleInfo(const std::string & hapPath,InnerBundleInfo & info)4098 void BundleMgrHostImpl::SetProvisionInfoToInnerBundleInfo(const std::string &hapPath, InnerBundleInfo &info)
4099 {
4100     Security::Verify::HapVerifyResult hapVerifyResult;
4101     ErrCode verifyRes = BundleVerifyMgr::HapVerify(hapPath, hapVerifyResult);
4102     if (verifyRes != ERR_OK) {
4103         return;
4104     }
4105     Security::Verify::ProvisionInfo provisionInfo = hapVerifyResult.GetProvisionInfo();
4106     bool isSystemApp = provisionInfo.bundleInfo.appFeature == ServiceConstants::HOS_SYSTEM_APP;
4107     info.SetAppType(isSystemApp ? Constants::AppType::SYSTEM_APP : Constants::AppType::THIRD_PARTY_APP);
4108     info.SetProvisionId(provisionInfo.appId);
4109     info.SetCertificateFingerprint(provisionInfo.fingerprint);
4110     info.SetAppIdentifier(provisionInfo.bundleInfo.appIdentifier);
4111     info.SetAppPrivilegeLevel(provisionInfo.bundleInfo.apl);
4112     bool isDebug = provisionInfo.type == Security::Verify::ProvisionType::DEBUG;
4113     info.SetAppProvisionType(isDebug ? Constants::APP_PROVISION_TYPE_DEBUG : Constants::APP_PROVISION_TYPE_RELEASE);
4114     std::string distributionType;
4115     auto typeIter = APP_DISTRIBUTION_TYPE_MAPS.find(provisionInfo.distributionType);
4116     if (typeIter == APP_DISTRIBUTION_TYPE_MAPS.end()) {
4117         distributionType = Constants::APP_DISTRIBUTION_TYPE_NONE;
4118     }
4119     distributionType = typeIter->second;
4120     info.SetAppDistributionType(distributionType);
4121 }
4122 
QueryAbilityInfoByContinueType(const std::string & bundleName,const std::string & continueType,AbilityInfo & abilityInfo,int32_t userId)4123 ErrCode BundleMgrHostImpl::QueryAbilityInfoByContinueType(const std::string &bundleName,
4124     const std::string &continueType, AbilityInfo &abilityInfo, int32_t userId)
4125 {
4126     APP_LOGD("QueryAbilityInfoByContinueType, bundleName : %{public}s, continueType : %{public}s, userId: %{public}d",
4127         bundleName.c_str(), continueType.c_str(), userId);
4128     if (!BundlePermissionMgr::IsSystemApp()) {
4129         APP_LOGE("non-system app calling system api");
4130         EventReport::SendQueryAbilityInfoByContinueTypeSysEvent(bundleName, EMPTY_ABILITY_NAME,
4131             ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, userId, continueType);
4132         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4133     }
4134     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED}) &&
4135         !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
4136         APP_LOGE("verify permission failed");
4137         EventReport::SendQueryAbilityInfoByContinueTypeSysEvent(bundleName, EMPTY_ABILITY_NAME,
4138             ERR_BUNDLE_MANAGER_PERMISSION_DENIED, userId, continueType);
4139         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4140     }
4141     APP_LOGD("verify permission success, begin to QueryAbilityInfoByContinueType");
4142     auto dataMgr = GetDataMgrFromService();
4143     if (dataMgr == nullptr) {
4144         APP_LOGE("DataMgr is nullptr");
4145         EventReport::SendQueryAbilityInfoByContinueTypeSysEvent(bundleName, EMPTY_ABILITY_NAME,
4146             ERR_BUNDLE_MANAGER_INTERNAL_ERROR, userId, continueType);
4147         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4148     }
4149     ErrCode res = dataMgr->QueryAbilityInfoByContinueType(bundleName, continueType, abilityInfo, userId);
4150     std::string abilityName;
4151     if (res == ERR_OK) {
4152         abilityName = abilityInfo.name;
4153     }
4154     EventReport::SendQueryAbilityInfoByContinueTypeSysEvent(bundleName, abilityName, res, userId, continueType);
4155     return res;
4156 }
4157 
QueryCloneAbilityInfo(const ElementName & element,int32_t flags,int32_t appIndex,AbilityInfo & abilityInfo,int32_t userId)4158 ErrCode BundleMgrHostImpl::QueryCloneAbilityInfo(const ElementName &element,
4159     int32_t flags, int32_t appIndex, AbilityInfo &abilityInfo, int32_t userId)
4160 {
4161     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
4162     std::string bundleName = element.GetBundleName();
4163     std::string abilityName = element.GetAbilityName();
4164     LOG_D(BMS_TAG_QUERY,
4165         "flags : %{public}d, userId : %{public}d, bundleName: %{public}s, abilityName: %{public}s",
4166         flags, userId, bundleName.c_str(), abilityName.c_str());
4167 
4168     if (bundleName.empty() || abilityName.empty()) {
4169         LOG_E(BMS_TAG_QUERY, "invalid params");
4170         return ERR_APPEXECFWK_CLONE_QUERY_PARAM_ERROR;
4171     }
4172     if (!BundlePermissionMgr::IsSystemApp()) {
4173         LOG_E(BMS_TAG_QUERY, "non-system app calling system api");
4174         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4175     }
4176     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED})
4177         && !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
4178         LOG_E(BMS_TAG_QUERY, "verify permission failed");
4179         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4180     }
4181     auto dataMgr = GetDataMgrFromService();
4182     if (dataMgr == nullptr) {
4183         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
4184         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4185     }
4186     auto res = dataMgr->QueryCloneAbilityInfo(element, flags, userId, appIndex, abilityInfo);
4187     if (res != ERR_OK) {
4188         LOG_E(BMS_TAG_QUERY, "QueryCloneAbilityInfo fail, err: %{public}d", res);
4189         return res;
4190     }
4191     return ERR_OK;
4192 }
4193 
GetCloneBundleInfo(const std::string & bundleName,int32_t flags,int32_t appIndex,BundleInfo & bundleInfo,int32_t userId)4194 ErrCode BundleMgrHostImpl::GetCloneBundleInfo(const std::string &bundleName, int32_t flags,
4195     int32_t appIndex, BundleInfo &bundleInfo, int32_t userId)
4196 {
4197     int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_CLONE_BUNDLE_INFO);
4198     ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); });
4199     if (!BundlePermissionMgr::IsSystemApp()) {
4200         APP_LOGE("non-system app calling system api");
4201         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4202     }
4203     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)
4204         && !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
4205         APP_LOGE("verify permission failed");
4206         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4207     }
4208     APP_LOGD("verify permission success, begin to GetCloneBundleInfo");
4209     auto dataMgr = GetDataMgrFromService();
4210     if (dataMgr == nullptr) {
4211         APP_LOGE("DataMgr is nullptr");
4212         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4213     }
4214     auto res = dataMgr->GetCloneBundleInfo(bundleName, flags, appIndex, bundleInfo, userId);
4215     if (res != ERR_OK) {
4216         APP_LOGE_NOFUNC("GetCloneBundleInfo fail -n %{public}s -u %{public}d -i %{public}d -f %{public}d"
4217             " err:%{public}d", bundleName.c_str(), userId, appIndex, flags, res);
4218         return res;
4219     }
4220     return ERR_OK;
4221 }
4222 
GetCloneAppIndexes(const std::string & bundleName,std::vector<int32_t> & appIndexes,int32_t userId)4223 ErrCode BundleMgrHostImpl::GetCloneAppIndexes(const std::string &bundleName, std::vector<int32_t> &appIndexes,
4224     int32_t userId)
4225 {
4226     APP_LOGD("start GetCloneAppIndexes bundleName = %{public}s, userId = %{public}d", bundleName.c_str(), userId);
4227     if (!BundlePermissionMgr::IsSystemApp()) {
4228         APP_LOGE("non-system app calling system api");
4229         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4230     }
4231     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)
4232         && !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) {
4233         APP_LOGE("verify permission failed");
4234         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4235     }
4236     APP_LOGD("verify permission success, begin to GetCloneAppIndexes");
4237     auto dataMgr = GetDataMgrFromService();
4238     if (dataMgr == nullptr) {
4239         APP_LOGE("DataMgr is nullptr");
4240         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4241     }
4242     appIndexes = dataMgr->GetCloneAppIndexes(bundleName, userId);
4243     return ERR_OK;
4244 }
4245 
GetLaunchWant(Want & want)4246 ErrCode BundleMgrHostImpl::GetLaunchWant(Want &want)
4247 {
4248     APP_LOGD("start GetLaunchWant");
4249     auto dataMgr = GetDataMgrFromService();
4250     if (dataMgr == nullptr) {
4251         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
4252         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4253     }
4254     auto uid = IPCSkeleton::GetCallingUid();
4255     std::string bundleName;
4256     auto ret = dataMgr->GetBundleNameForUid(uid, bundleName);
4257     if (!ret) {
4258         LOG_NOFUNC_E(BMS_TAG_QUERY, "GetBundleNameForUid failed uid:%{public}d", uid);
4259         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
4260     }
4261     int32_t userId = AccountHelper::GetOsAccountLocalIdFromUid(uid);
4262     return dataMgr->GetLaunchWantForBundle(bundleName, want, userId);
4263 }
4264 
QueryCloneExtensionAbilityInfoWithAppIndex(const ElementName & element,int32_t flags,int32_t appIndex,ExtensionAbilityInfo & extensionAbilityInfo,int32_t userId)4265 ErrCode BundleMgrHostImpl::QueryCloneExtensionAbilityInfoWithAppIndex(const ElementName &element, int32_t flags,
4266     int32_t appIndex, ExtensionAbilityInfo &extensionAbilityInfo, int32_t userId)
4267 {
4268     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
4269     LOG_D(BMS_TAG_QUERY, "QueryCloneExtensionAbilityInfoWithAppIndex without type begin");
4270     if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED})) {
4271         LOG_E(BMS_TAG_QUERY, "verify permission failed");
4272         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4273     }
4274     auto dataMgr = GetDataMgrFromService();
4275     if (dataMgr == nullptr) {
4276         LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr");
4277         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4278     }
4279     std::string bundleName = element.GetBundleName();
4280     std::string extensionName = element.GetAbilityName();
4281     if (bundleName.empty() || extensionName.empty()) {
4282         LOG_E(BMS_TAG_QUERY,
4283             "QueryCloneExtensionAbilityInfoWithAppIndex is failed, bundleName:%{public}s, extensionName:%{public}s",
4284             bundleName.c_str(), extensionName.c_str());
4285         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
4286     }
4287 
4288     Want want;
4289     want.SetElement(element);
4290     ErrCode ret = dataMgr->ExplicitQueryExtensionInfoV9(want, flags, userId, extensionAbilityInfo, appIndex);
4291     if (ret != ERR_OK) {
4292         LOG_D(BMS_TAG_QUERY, "explicit queryExtensionInfo error");
4293         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
4294     }
4295     return ERR_OK;
4296 }
4297 
GetSignatureInfoByBundleName(const std::string & bundleName,SignatureInfo & signatureInfo)4298 ErrCode BundleMgrHostImpl::GetSignatureInfoByBundleName(const std::string &bundleName, SignatureInfo &signatureInfo)
4299 {
4300     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
4301     int32_t uid = OHOS::IPCSkeleton::GetCallingUid();
4302     if (uid != Constants::FOUNDATION_UID) {
4303         LOG_E(BMS_TAG_DEFAULT, "uid: %{public}d not foundation", uid);
4304         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4305     }
4306     auto dataMgr = GetDataMgrFromService();
4307     if (dataMgr == nullptr) {
4308         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
4309         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4310     }
4311     return dataMgr->GetSignatureInfoByBundleName(bundleName, signatureInfo);
4312 }
4313 
CheckCanSetEnable(const std::string & bundleName)4314 bool BundleMgrHostImpl::CheckCanSetEnable(const std::string &bundleName)
4315 {
4316     std::vector<std::string> noDisablingList;
4317     std::string configPath = BundleUtil::GetNoDisablingConfigPath();
4318     ErrCode ret = BundleParser::ParseNoDisablingList(configPath, noDisablingList);
4319     if (ret != ERR_OK) {
4320         LOG_W(BMS_TAG_DEFAULT, "GetNoDisablingList failed");
4321         return true;
4322     }
4323     auto it = std::find(noDisablingList.begin(), noDisablingList.end(), bundleName);
4324     if (it == noDisablingList.end()) {
4325         return true;
4326     }
4327     return false;
4328 }
4329 
AddDesktopShortcutInfo(const ShortcutInfo & shortcutInfo,int32_t userId)4330 ErrCode BundleMgrHostImpl::AddDesktopShortcutInfo(const ShortcutInfo &shortcutInfo, int32_t userId)
4331 {
4332     if (!BundlePermissionMgr::IsSystemApp()) {
4333         APP_LOGE("Non-system app calling system api");
4334         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4335     }
4336     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_MANAGER_SHORTCUT)) {
4337         APP_LOGE("Verify permission failed");
4338         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4339     }
4340     auto dataMgr = GetDataMgrFromService();
4341     if (dataMgr == nullptr) {
4342         APP_LOGE("DataMgr is nullptr");
4343         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4344     }
4345     return dataMgr->AddDesktopShortcutInfo(shortcutInfo, userId);
4346 }
4347 
DeleteDesktopShortcutInfo(const ShortcutInfo & shortcutInfo,int32_t userId)4348 ErrCode BundleMgrHostImpl::DeleteDesktopShortcutInfo(const ShortcutInfo &shortcutInfo, int32_t userId)
4349 {
4350     if (!BundlePermissionMgr::IsSystemApp()) {
4351         APP_LOGE("Non-system app calling system api");
4352         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4353     }
4354     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_MANAGER_SHORTCUT)) {
4355         APP_LOGE("Verify permission failed");
4356         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4357     }
4358     auto dataMgr = GetDataMgrFromService();
4359     if (dataMgr == nullptr) {
4360         APP_LOGE("DataMgr is nullptr");
4361         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4362     }
4363     return dataMgr->DeleteDesktopShortcutInfo(shortcutInfo, userId);
4364 }
4365 
GetAllDesktopShortcutInfo(int32_t userId,std::vector<ShortcutInfo> & shortcutInfos)4366 ErrCode BundleMgrHostImpl::GetAllDesktopShortcutInfo(int32_t userId, std::vector<ShortcutInfo> &shortcutInfos)
4367 {
4368     if (!BundlePermissionMgr::IsSystemApp()) {
4369         APP_LOGE("Non-system app calling system api");
4370         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4371     }
4372     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_MANAGER_SHORTCUT)) {
4373         APP_LOGE("Verify permission failed");
4374         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4375     }
4376     auto dataMgr = GetDataMgrFromService();
4377     if (dataMgr == nullptr) {
4378         APP_LOGE("DataMgr is nullptr");
4379         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4380     }
4381     return dataMgr->GetAllDesktopShortcutInfo(userId, shortcutInfos);
4382 }
4383 
IsAppLinking(int32_t flags) const4384 bool BundleMgrHostImpl::IsAppLinking(int32_t flags) const
4385 {
4386     if ((static_cast<uint32_t>(flags) &
4387         static_cast<uint32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APP_LINKING)) ==
4388         static_cast<uint32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APP_LINKING)) {
4389         APP_LOGI("contains app linking flag, no need to query from bms extension");
4390         return true;
4391     }
4392     return false;
4393 }
4394 
GetOdidByBundleName(const std::string & bundleName,std::string & odid)4395 ErrCode BundleMgrHostImpl::GetOdidByBundleName(const std::string &bundleName, std::string &odid)
4396 {
4397     APP_LOGD("start GetOdidByBundleName");
4398     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
4399         APP_LOGE("Verify permission failed");
4400         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4401     }
4402     auto dataMgr = GetDataMgrFromService();
4403     if (dataMgr == nullptr) {
4404         APP_LOGE("DataMgr is nullptr");
4405         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4406     }
4407     return dataMgr->GetOdidByBundleName(bundleName, odid);
4408 }
4409 
GetBundleInfosForContinuation(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)4410 bool BundleMgrHostImpl::GetBundleInfosForContinuation(int32_t flags, std::vector<BundleInfo> &bundleInfos,
4411     int32_t userId)
4412 {
4413     GetBundleInfos(flags, bundleInfos, userId);
4414     auto dataMgr = GetDataMgrFromService();
4415     dataMgr->GetBundleInfosForContinuation(bundleInfos);
4416     return !bundleInfos.empty();
4417 }
4418 
GetContinueBundleNames(const std::string & continueBundleName,std::vector<std::string> & bundleNames,int32_t userId)4419 ErrCode BundleMgrHostImpl::GetContinueBundleNames(
4420     const std::string &continueBundleName, std::vector<std::string> &bundleNames, int32_t userId)
4421 {
4422     if (continueBundleName.empty()) {
4423         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
4424     }
4425 
4426     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
4427         APP_LOGE("Verify permission failed");
4428         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4429     }
4430 
4431     auto dataMgr = GetDataMgrFromService();
4432     if (dataMgr == nullptr) {
4433         APP_LOGE("DataMgr is nullptr");
4434         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4435     }
4436     return dataMgr->GetContinueBundleNames(continueBundleName, bundleNames, userId);
4437 }
4438 
IsBundleInstalled(const std::string & bundleName,int32_t userId,int32_t appIndex,bool & isInstalled)4439 ErrCode BundleMgrHostImpl::IsBundleInstalled(const std::string &bundleName, int32_t userId,
4440     int32_t appIndex, bool &isInstalled)
4441 {
4442     APP_LOGD("IsBundleInstalled -n %{public}s -u %{public}d -i %{public}d", bundleName.c_str(), userId, appIndex);
4443     if (!BundlePermissionMgr::IsSystemApp()) {
4444         APP_LOGE("Non-system app calling system api");
4445         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
4446     }
4447     if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
4448         APP_LOGE("Verify permission failed");
4449         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
4450     }
4451 
4452     auto dataMgr = GetDataMgrFromService();
4453     if (dataMgr == nullptr) {
4454         APP_LOGE("DataMgr is nullptr");
4455         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4456     }
4457     return dataMgr->IsBundleInstalled(bundleName, userId, appIndex, isInstalled);
4458 }
4459 
GetCallerName()4460 std::string BundleMgrHostImpl::GetCallerName()
4461 {
4462     auto dataMgr = GetDataMgrFromService();
4463     if (dataMgr == nullptr) {
4464         APP_LOGE("DataMgr is nullptr");
4465         return Constants::EMPTY_STRING;
4466     }
4467     std::string callerName;
4468     int32_t uid = IPCSkeleton::GetCallingUid();
4469     auto ret = dataMgr->GetNameForUid(uid, callerName);
4470     if (ret != ERR_OK) {
4471         callerName = std::to_string(uid);
4472     }
4473     return callerName;
4474 }
4475 }  // namespace AppExecFwk
4476 }  // namespace OHOS
4477