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