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