• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 <dirent.h>
19 #include <future>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 
24 #include "app_log_wrapper.h"
25 #include "app_privilege_capability.h"
26 #include "bundle_mgr_service.h"
27 #include "bundle_parser.h"
28 #include "bundle_permission_mgr.h"
29 #include "bundle_sandbox_app_helper.h"
30 #include "bundle_util.h"
31 #include "bundle_verify_mgr.h"
32 #include "directory_ex.h"
33 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
34 #include "distributed_bms_proxy.h"
35 #endif
36 #include "element_name.h"
37 #include "ffrt.h"
38 #include "if_system_ability_manager.h"
39 #include "installd_client.h"
40 #include "ipc_skeleton.h"
41 #include "iservice_registry.h"
42 #include "json_serializer.h"
43 #include "scope_guard.h"
44 #include "system_ability_definition.h"
45 
46 namespace OHOS {
47 namespace AppExecFwk {
48 namespace {
49 constexpr const char* SYSTEM_APP = "system";
50 constexpr const char* THIRD_PARTY_APP = "third-party";
51 }
52 
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)53 bool BundleMgrHostImpl::GetApplicationInfo(
54     const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo)
55 {
56     return GetApplicationInfo(appName, static_cast<int32_t>(flag), userId, appInfo);
57 }
58 
GetApplicationInfo(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)59 bool BundleMgrHostImpl::GetApplicationInfo(
60     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
61 {
62     APP_LOGD("start GetApplicationInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
63         appName.c_str(), flags, userId);
64     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
65         APP_LOGD("non-system app calling system api");
66         return true;
67     }
68     if (!VerifyQueryPermission(appName)) {
69         APP_LOGE("verify permission failed");
70         return false;
71     }
72     APP_LOGD("verify permission success, begin to GetApplicationInfo");
73     auto dataMgr = GetDataMgrFromService();
74     if (dataMgr == nullptr) {
75         APP_LOGE("DataMgr is nullptr");
76         return false;
77     }
78     return dataMgr->GetApplicationInfo(appName, flags, userId, appInfo);
79 }
80 
GetApplicationInfoV9(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)81 ErrCode BundleMgrHostImpl::GetApplicationInfoV9(
82     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
83 {
84     APP_LOGD("start GetApplicationInfoV9, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
85         appName.c_str(), flags, userId);
86     if (!VerifySystemApi()) {
87         APP_LOGE("non-system app calling system api");
88         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
89     }
90     if (!VerifyQueryPermission(appName)) {
91         APP_LOGE("verify permission failed");
92         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
93     }
94     APP_LOGD("verify permission success, bgein to GetApplicationInfoV9");
95     auto dataMgr = GetDataMgrFromService();
96     if (dataMgr == nullptr) {
97         APP_LOGE("DataMgr is nullptr");
98         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
99     }
100     return dataMgr->GetApplicationInfoV9(appName, flags, userId, appInfo);
101 }
102 
GetApplicationInfos(const ApplicationFlag flag,const int userId,std::vector<ApplicationInfo> & appInfos)103 bool BundleMgrHostImpl::GetApplicationInfos(
104     const ApplicationFlag flag, const int userId, std::vector<ApplicationInfo> &appInfos)
105 {
106     return GetApplicationInfos(static_cast<int32_t>(flag), userId, appInfos);
107 }
108 
GetApplicationInfos(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)109 bool BundleMgrHostImpl::GetApplicationInfos(
110     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
111 {
112     APP_LOGD("start GetApplicationInfos, flags : %{public}d, userId : %{public}d", flags, userId);
113     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
114         APP_LOGD("non-system app calling system api");
115         return true;
116     }
117     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
118         APP_LOGE("verify permission failed");
119         return false;
120     }
121     APP_LOGD("verify permission success, begin to GetApplicationInfos");
122     if (!BundlePermissionMgr::IsNativeTokenType() &&
123         (BundlePermissionMgr::GetHapApiVersion() >= Constants::API_VERSION_NINE)) {
124         APP_LOGD("GetApplicationInfos return empty, not support target level greater than or equal to api9");
125         return true;
126     }
127     auto dataMgr = GetDataMgrFromService();
128     if (dataMgr == nullptr) {
129         APP_LOGE("DataMgr is nullptr");
130         return false;
131     }
132     return dataMgr->GetApplicationInfos(flags, userId, appInfos);
133 }
134 
GetApplicationInfosV9(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)135 ErrCode BundleMgrHostImpl::GetApplicationInfosV9(
136     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
137 {
138     APP_LOGD("start GetApplicationInfosV9, flags : %{public}d, userId : %{public}d", flags, userId);
139     if (!VerifySystemApi()) {
140         APP_LOGE("non-system app calling system api");
141         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
142     }
143     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST)) {
144         APP_LOGE("verify permission failed");
145         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
146     }
147     APP_LOGD("verify permission success, begin to GetApplicationInfosV9");
148     auto dataMgr = GetDataMgrFromService();
149     if (dataMgr == nullptr) {
150         APP_LOGE("DataMgr is nullptr");
151         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
152         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
153     }
154     auto ret = dataMgr->GetApplicationInfosV9(flags, userId, appInfos);
155     if (ret == ERR_OK) {
156         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0);
157     } else {
158         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
159     }
160     return ret;
161 }
162 
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)163 bool BundleMgrHostImpl::GetBundleInfo(
164     const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId)
165 {
166     return GetBundleInfo(bundleName, static_cast<int32_t>(flag), bundleInfo, userId);
167 }
168 
GetBundleInfo(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)169 bool BundleMgrHostImpl::GetBundleInfo(
170     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
171 {
172     APP_LOGD("start GetBundleInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
173         bundleName.c_str(), flags, userId);
174     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
175         APP_LOGD("non-system app calling system api");
176         return true;
177     }
178     if (!VerifyQueryPermission(bundleName)) {
179         APP_LOGE("verify permission failed");
180         return false;
181     }
182     APP_LOGD("verify permission success, begin to GetBundleInfo");
183     auto dataMgr = GetDataMgrFromService();
184     if (dataMgr == nullptr) {
185         APP_LOGE("DataMgr is nullptr");
186         return false;
187     }
188     bool res = dataMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId);
189     if (!res) {
190         if (isBrokerServiceExisted_) {
191             return dataMgr->GetBundleInfoFromBmsExtension(bundleName, flags, bundleInfo, userId) == ERR_OK;
192         }
193     }
194     return res;
195 }
196 
GetBaseSharedBundleInfos(const std::string & bundleName,std::vector<BaseSharedBundleInfo> & baseSharedBundleInfos)197 ErrCode BundleMgrHostImpl::GetBaseSharedBundleInfos(const std::string &bundleName,
198     std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos)
199 {
200     APP_LOGD("start GetBaseSharedBundleInfos, bundleName : %{public}s", bundleName.c_str());
201     if (!VerifyQueryPermission(bundleName)) {
202         APP_LOGE("verify permission failed");
203         return false;
204     }
205     auto dataMgr = GetDataMgrFromService();
206     if (dataMgr == nullptr) {
207         APP_LOGE("DataMgr is nullptr");
208         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
209     }
210     return dataMgr->GetBaseSharedBundleInfos(bundleName, baseSharedBundleInfos);
211 }
212 
GetBundleInfoV9(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)213 ErrCode BundleMgrHostImpl::GetBundleInfoV9(
214     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
215 {
216     APP_LOGD("start GetBundleInfoV9, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
217         bundleName.c_str(), flags, userId);
218     if (!VerifySystemApi()) {
219         APP_LOGE("non-system app calling system api");
220         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
221     }
222     if (!VerifyQueryPermission(bundleName)) {
223         APP_LOGE("verify permission failed");
224         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
225     }
226     APP_LOGD("verify permission success, begin to GetBundleInfoV9");
227     auto dataMgr = GetDataMgrFromService();
228     if (dataMgr == nullptr) {
229         APP_LOGE("DataMgr is nullptr");
230         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
231     }
232     auto res = dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId);
233     if (res != ERR_OK) {
234         if (dataMgr->GetBundleInfoFromBmsExtension(bundleName, flags, bundleInfo, userId, true) == ERR_OK) {
235             return ERR_OK;
236         }
237     }
238     return res;
239 }
240 
GetBundleInfoForSelf(int32_t flags,BundleInfo & bundleInfo)241 ErrCode BundleMgrHostImpl::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo)
242 {
243     auto uid = IPCSkeleton::GetCallingUid();
244     int32_t userId = uid / Constants::BASE_USER_RANGE;
245     std::string bundleName;
246     auto dataMgr = GetDataMgrFromService();
247     if (dataMgr == nullptr) {
248         APP_LOGE("DataMgr is nullptr");
249         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
250     }
251     bool ret = GetBundleNameForUid(uid, bundleName);
252     if (!ret) {
253         APP_LOGE("GetBundleNameForUid failed");
254         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
255     }
256     return dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId);
257 }
258 
GetDependentBundleInfo(const std::string & sharedBundleName,BundleInfo & sharedBundleInfo)259 ErrCode BundleMgrHostImpl::GetDependentBundleInfo(const std::string &sharedBundleName, BundleInfo &sharedBundleInfo)
260 {
261     if (!VerifyDependency(sharedBundleName)) {
262         APP_LOGE("verify dependency failed");
263         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
264     }
265 
266     auto dataMgr = GetDataMgrFromService();
267     if (dataMgr == nullptr) {
268         APP_LOGE("DataMgr is nullptr");
269         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
270     }
271 
272     int32_t flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) |
273         static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
274     return dataMgr->GetSharedBundleInfo(sharedBundleName, flags, sharedBundleInfo);
275 }
276 
GetBundlePackInfo(const std::string & bundleName,const BundlePackFlag flag,BundlePackInfo & bundlePackInfo,int32_t userId)277 ErrCode BundleMgrHostImpl::GetBundlePackInfo(
278     const std::string &bundleName, const BundlePackFlag flag, BundlePackInfo &bundlePackInfo, int32_t userId)
279 {
280     return GetBundlePackInfo(bundleName, static_cast<int32_t>(flag), bundlePackInfo, userId);
281 }
282 
GetBundlePackInfo(const std::string & bundleName,int32_t flags,BundlePackInfo & bundlePackInfo,int32_t userId)283 ErrCode BundleMgrHostImpl::GetBundlePackInfo(
284     const std::string &bundleName, int32_t flags, BundlePackInfo &bundlePackInfo, int32_t userId)
285 {
286     // check permission
287     if (!VerifySystemApi()) {
288         APP_LOGE("non-system app calling system api");
289         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
290     }
291     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
292         APP_LOGE("SetModuleUpgradeFlag failed due to lack of permission");
293         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
294     }
295     auto dataMgr = GetDataMgrFromService();
296     if (dataMgr == nullptr) {
297         APP_LOGE("DataMgr is nullptr");
298         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
299     }
300     return dataMgr->GetBundlePackInfo(bundleName, flags, bundlePackInfo, userId);
301 }
302 
GetBundleUserInfo(const std::string & bundleName,int32_t userId,InnerBundleUserInfo & innerBundleUserInfo)303 bool BundleMgrHostImpl::GetBundleUserInfo(
304     const std::string &bundleName, int32_t userId, InnerBundleUserInfo &innerBundleUserInfo)
305 {
306     auto dataMgr = GetDataMgrFromService();
307     if (dataMgr == nullptr) {
308         APP_LOGE("DataMgr is nullptr");
309         return false;
310     }
311     return dataMgr->GetInnerBundleUserInfoByUserId(bundleName, userId, innerBundleUserInfo);
312 }
313 
GetBundleUserInfos(const std::string & bundleName,std::vector<InnerBundleUserInfo> & innerBundleUserInfos)314 bool BundleMgrHostImpl::GetBundleUserInfos(
315     const std::string &bundleName, std::vector<InnerBundleUserInfo> &innerBundleUserInfos)
316 {
317     auto dataMgr = GetDataMgrFromService();
318     if (dataMgr == nullptr) {
319         APP_LOGE("DataMgr is nullptr");
320         return false;
321     }
322     return dataMgr->GetInnerBundleUserInfos(bundleName, innerBundleUserInfos);
323 }
324 
GetBundleInfos(const BundleFlag flag,std::vector<BundleInfo> & bundleInfos,int32_t userId)325 bool BundleMgrHostImpl::GetBundleInfos(const BundleFlag flag, std::vector<BundleInfo> &bundleInfos, int32_t userId)
326 {
327     return GetBundleInfos(static_cast<int32_t>(flag), bundleInfos, userId);
328 }
329 
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)330 bool BundleMgrHostImpl::GetBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
331 {
332     APP_LOGD("start GetBundleInfos, flags : %{public}d, userId : %{public}d", flags, userId);
333     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
334         APP_LOGD("non-system app calling system api");
335         return true;
336     }
337     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
338         APP_LOGE("verify permission failed");
339         return false;
340     }
341     APP_LOGD("verify permission success, begin to GetBundleInfos");
342     if (!BundlePermissionMgr::IsNativeTokenType() &&
343         (BundlePermissionMgr::GetHapApiVersion() >= Constants::API_VERSION_NINE)) {
344         APP_LOGD("GetBundleInfos return empty, not support target level greater than or equal to api9");
345         return true;
346     }
347     auto dataMgr = GetDataMgrFromService();
348     if (dataMgr == nullptr) {
349         APP_LOGE("DataMgr is nullptr");
350         return false;
351     }
352     dataMgr->GetBundleInfos(flags, bundleInfos, userId);
353     dataMgr->GetBundleInfosFromBmsExtension(flags, bundleInfos, userId);
354     return !bundleInfos.empty();
355 }
356 
GetBundleInfosV9(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)357 ErrCode BundleMgrHostImpl::GetBundleInfosV9(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
358 {
359     APP_LOGD("start GetBundleInfosV9, flags : %{public}d, userId : %{public}d", flags, userId);
360     if (!VerifySystemApi()) {
361         APP_LOGE("non-system app calling system api");
362         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
363     }
364     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST)) {
365         APP_LOGE("verify permission failed");
366         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
367     }
368     APP_LOGD("verify permission success, begin to GetBundleInfosV9");
369     auto dataMgr = GetDataMgrFromService();
370     if (dataMgr == nullptr) {
371         APP_LOGE("DataMgr is nullptr");
372         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
373         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
374     }
375     auto res = dataMgr->GetBundleInfosV9(flags, bundleInfos, userId);
376     if (dataMgr->GetBundleInfosFromBmsExtension(flags, bundleInfos, userId, true) == ERR_OK) {
377         APP_LOGD("query bundle infos from bms extension successfully");
378         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0);
379         return ERR_OK;
380     }
381     if (res == ERR_OK) {
382         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0);
383     } else {
384         BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1);
385     }
386     return res;
387 }
388 
GetBundleNameForUid(const int uid,std::string & bundleName)389 bool BundleMgrHostImpl::GetBundleNameForUid(const int uid, std::string &bundleName)
390 {
391     APP_LOGD("start GetBundleNameForUid, uid : %{public}d", uid);
392     auto dataMgr = GetDataMgrFromService();
393     if (dataMgr == nullptr) {
394         APP_LOGE("DataMgr is nullptr");
395         return false;
396     }
397     return dataMgr->GetBundleNameForUid(uid, bundleName);
398 }
399 
GetBundlesForUid(const int uid,std::vector<std::string> & bundleNames)400 bool BundleMgrHostImpl::GetBundlesForUid(const int uid, std::vector<std::string> &bundleNames)
401 {
402     APP_LOGD("start GetBundlesForUid, uid : %{public}d", uid);
403     if (!BundlePermissionMgr::IsNativeTokenType()) {
404         APP_LOGE("verify token type failed");
405         return false;
406     }
407     auto dataMgr = GetDataMgrFromService();
408     if (dataMgr == nullptr) {
409         APP_LOGE("DataMgr is nullptr");
410         return false;
411     }
412     return dataMgr->GetBundlesForUid(uid, bundleNames);
413 }
414 
GetNameForUid(const int uid,std::string & name)415 ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name)
416 {
417     APP_LOGD("start GetNameForUid, uid : %{public}d", uid);
418     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
419         APP_LOGE("non-system app calling system api");
420         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
421     }
422     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
423         !BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
424         APP_LOGE("verify query permission failed");
425         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
426     }
427     auto dataMgr = GetDataMgrFromService();
428     if (dataMgr == nullptr) {
429         APP_LOGE("DataMgr is nullptr");
430         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
431     }
432     return dataMgr->GetNameForUid(uid, name);
433 }
434 
GetBundleGids(const std::string & bundleName,std::vector<int> & gids)435 bool BundleMgrHostImpl::GetBundleGids(const std::string &bundleName, std::vector<int> &gids)
436 {
437     APP_LOGD("start GetBundleGids, bundleName : %{public}s", bundleName.c_str());
438     if (!BundlePermissionMgr::IsNativeTokenType()) {
439         APP_LOGE("verify token type failed");
440         return false;
441     }
442     auto dataMgr = GetDataMgrFromService();
443     if (dataMgr == nullptr) {
444         APP_LOGE("DataMgr is nullptr");
445         return false;
446     }
447     return dataMgr->GetBundleGids(bundleName, gids);
448 }
449 
GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids)450 bool BundleMgrHostImpl::GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector<int> &gids)
451 {
452     APP_LOGD("start GetBundleGidsByUid, bundleName : %{public}s, uid : %{public}d", bundleName.c_str(), uid);
453     auto dataMgr = GetDataMgrFromService();
454     if (dataMgr == nullptr) {
455         APP_LOGE("DataMgr is nullptr");
456         return false;
457     }
458     return dataMgr->GetBundleGidsByUid(bundleName, uid, gids);
459 }
460 
CheckIsSystemAppByUid(const int uid)461 bool BundleMgrHostImpl::CheckIsSystemAppByUid(const int uid)
462 {
463     APP_LOGD("start CheckIsSystemAppByUid, uid : %{public}d", uid);
464     auto dataMgr = GetDataMgrFromService();
465     if (dataMgr == nullptr) {
466         APP_LOGE("DataMgr is nullptr");
467         return false;
468     }
469     return dataMgr->CheckIsSystemAppByUid(uid);
470 }
471 
GetBundleInfosByMetaData(const std::string & metaData,std::vector<BundleInfo> & bundleInfos)472 bool BundleMgrHostImpl::GetBundleInfosByMetaData(const std::string &metaData, std::vector<BundleInfo> &bundleInfos)
473 {
474     APP_LOGD("start GetBundleInfosByMetaData, metaData : %{public}s", metaData.c_str());
475     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
476         APP_LOGE("verify permission failed");
477         return false;
478     }
479     auto dataMgr = GetDataMgrFromService();
480     if (dataMgr == nullptr) {
481         APP_LOGE("DataMgr is nullptr");
482         return false;
483     }
484     return dataMgr->GetBundleInfosByMetaData(metaData, bundleInfos);
485 }
486 
QueryAbilityInfo(const Want & want,AbilityInfo & abilityInfo)487 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, AbilityInfo &abilityInfo)
488 {
489     return QueryAbilityInfo(want, GET_ABILITY_INFO_WITH_APPLICATION, Constants::UNSPECIFIED_USERID, abilityInfo);
490 }
491 
492 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,const sptr<IRemoteObject> & callBack)493 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId,
494     AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack)
495 {
496     if (!BundlePermissionMgr::VerifyCallingUid()) {
497         APP_LOGE("QueryAbilityInfo verify failed.");
498         return false;
499     }
500     auto connectAbilityMgr = GetConnectAbilityMgrFromService();
501     if (connectAbilityMgr == nullptr) {
502         APP_LOGE("connectAbilityMgr is nullptr");
503         return false;
504     }
505     return connectAbilityMgr->QueryAbilityInfo(want, flags, userId, abilityInfo, callBack);
506 }
507 
SilentInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callBack)508 bool BundleMgrHostImpl::SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack)
509 {
510     APP_LOGD("SilentInstall in");
511     auto connectMgr = GetConnectAbilityMgrFromService();
512     if (connectMgr == nullptr) {
513         APP_LOGE("connectMgr is nullptr");
514         return false;
515     }
516     return connectMgr->SilentInstall(want, userId, callBack);
517 }
518 
UpgradeAtomicService(const Want & want,int32_t userId)519 void BundleMgrHostImpl::UpgradeAtomicService(const Want &want, int32_t userId)
520 {
521     if (!BundlePermissionMgr::VerifyCallingUid()) {
522         APP_LOGE("UpgradeAtomicService verify failed.");
523         return;
524     }
525     auto connectAbilityMgr = GetConnectAbilityMgrFromService();
526     if (connectAbilityMgr == nullptr) {
527         APP_LOGE("connectAbilityMgr is nullptr");
528         return;
529     }
530     connectAbilityMgr->UpgradeAtomicService(want, userId);
531 }
532 
CheckAbilityEnableInstall(const Want & want,int32_t missionId,int32_t userId,const sptr<IRemoteObject> & callback)533 bool BundleMgrHostImpl::CheckAbilityEnableInstall(
534     const Want &want, int32_t missionId, int32_t userId, const sptr<IRemoteObject> &callback)
535 {
536     if (!BundlePermissionMgr::IsNativeTokenType()) {
537         APP_LOGE("verify token type failed");
538         return false;
539     }
540     auto elementName = want.GetElement();
541     if (elementName.GetDeviceID().empty() || elementName.GetBundleName().empty() ||
542         elementName.GetAbilityName().empty()) {
543         APP_LOGE("check ability install parameter is invalid");
544         return false;
545     }
546     auto bundleDistributedManager = DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleDistributedManager();
547     if (bundleDistributedManager == nullptr) {
548         APP_LOGE("bundleDistributedManager failed");
549         return false;
550     }
551     return bundleDistributedManager->CheckAbilityEnableInstall(want, missionId, userId, callback);
552 }
553 
ProcessPreload(const Want & want)554 bool BundleMgrHostImpl::ProcessPreload(const Want &want)
555 {
556     if (!BundlePermissionMgr::VerifyPreload(want)) {
557         APP_LOGE("ProcessPreload verify failed.");
558         return false;
559     }
560     APP_LOGD("begin to process preload.");
561     auto connectAbilityMgr = GetConnectAbilityMgrFromService();
562     if (connectAbilityMgr == nullptr) {
563         APP_LOGE("connectAbilityMgr is nullptr");
564         return false;
565     }
566     return connectAbilityMgr->ProcessPreload(want);
567 }
568 #endif
569 
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo)570 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo)
571 {
572     APP_LOGD("start QueryAbilityInfo, flags : %{public}d, userId : %{public}d", flags, userId);
573     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
574         APP_LOGD("non-system app calling system api");
575         return true;
576     }
577     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
578         APP_LOGE("verify permission failed");
579         return false;
580     }
581     APP_LOGD("verify permission success, begin to QueryAbilityInfo");
582     auto dataMgr = GetDataMgrFromService();
583     if (dataMgr == nullptr) {
584         APP_LOGE("DataMgr is nullptr");
585         return false;
586     }
587     bool res = dataMgr->QueryAbilityInfo(want, flags, userId, abilityInfo);
588     if (!res) {
589         if (isBrokerServiceExisted_) {
590             return (dataMgr->QueryAbilityInfoFromBmsExtension(want, flags, userId, abilityInfo) == ERR_OK);
591         }
592     }
593     return res;
594 }
595 
QueryAbilityInfos(const Want & want,std::vector<AbilityInfo> & abilityInfos)596 bool BundleMgrHostImpl::QueryAbilityInfos(const Want &want, std::vector<AbilityInfo> &abilityInfos)
597 {
598     return QueryAbilityInfos(
599         want, GET_ABILITY_INFO_WITH_APPLICATION, Constants::UNSPECIFIED_USERID, abilityInfos);
600 }
601 
QueryAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)602 bool BundleMgrHostImpl::QueryAbilityInfos(
603     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
604 {
605     APP_LOGD("start QueryAbilityInfos, flags : %{public}d, userId : %{public}d", flags, userId);
606     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
607         APP_LOGD("non-system app calling system api");
608         return true;
609     }
610     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
611         APP_LOGE("verify permission failed");
612         return false;
613     }
614     APP_LOGD("verify permission success, begin to QueryAbilityInfos");
615     auto dataMgr = GetDataMgrFromService();
616     if (dataMgr == nullptr) {
617         APP_LOGE("DataMgr is nullptr");
618         return false;
619     }
620     dataMgr->QueryAbilityInfos(want, flags, userId, abilityInfos);
621     dataMgr->QueryAbilityInfosFromBmsExtension(want, flags, userId, abilityInfos);
622     return !abilityInfos.empty();
623 }
624 
QueryAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)625 ErrCode BundleMgrHostImpl::QueryAbilityInfosV9(
626     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
627 {
628     APP_LOGD("start QueryAbilityInfosV9, flags : %{public}d, userId : %{public}d", flags, userId);
629     if (!VerifySystemApi()) {
630         APP_LOGE("non-system app calling system api");
631         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
632     }
633     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
634         APP_LOGE("verify permission failed");
635         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
636     }
637     APP_LOGD("verify permission success, begin to QueryAbilityInfosV9");
638     auto dataMgr = GetDataMgrFromService();
639     if (dataMgr == nullptr) {
640         APP_LOGE("DataMgr is nullptr");
641         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
642     }
643     auto res = dataMgr->QueryAbilityInfosV9(want, flags, userId, abilityInfos);
644     if (dataMgr->QueryAbilityInfosFromBmsExtension(want, flags, userId, abilityInfos, true) == ERR_OK) {
645         APP_LOGD("query ability infos from bms extension successfully");
646         return ERR_OK;
647     }
648     return res;
649 }
650 
QueryLauncherAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)651 ErrCode BundleMgrHostImpl::QueryLauncherAbilityInfos(
652     const Want &want, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
653 {
654     APP_LOGD("start QueryLauncherAbilityInfos, userId : %{public}d", userId);
655     if (!VerifySystemApi()) {
656         APP_LOGE("non-system app calling system api");
657         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
658     }
659     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
660         APP_LOGE("verify permission failed");
661         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
662     }
663     APP_LOGD("verify permission success, begin to QueryLauncherAbilityInfos");
664     auto dataMgr = GetDataMgrFromService();
665     if (dataMgr == nullptr) {
666         APP_LOGE("DataMgr is nullptr");
667         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
668     }
669     return dataMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos);
670 }
671 
QueryAllAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)672 bool BundleMgrHostImpl::QueryAllAbilityInfos(const Want &want, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
673 {
674     APP_LOGD("start QueryAllAbilityInfos, userId : %{public}d", userId);
675     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
676         APP_LOGD("non-system app calling system api");
677         return true;
678     }
679     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
680         APP_LOGE("verify permission failed");
681         return false;
682     }
683     APP_LOGD("verify permission success, begin to QueryAllAbilityInfos");
684     auto dataMgr = GetDataMgrFromService();
685     if (dataMgr == nullptr) {
686         APP_LOGE("DataMgr is nullptr");
687         return false;
688     }
689     return dataMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos) == ERR_OK;
690 }
691 
QueryAbilityInfoByUri(const std::string & abilityUri,AbilityInfo & abilityInfo)692 bool BundleMgrHostImpl::QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo)
693 {
694     APP_LOGD("start QueryAbilityInfoByUri, uri : %{private}s", abilityUri.c_str());
695     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
696         APP_LOGD("non-system app calling system api");
697         return true;
698     }
699     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
700         !BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
701         APP_LOGE("verify query permission failed");
702         return false;
703     }
704     auto dataMgr = GetDataMgrFromService();
705     if (dataMgr == nullptr) {
706         APP_LOGE("DataMgr is nullptr");
707         return false;
708     }
709     return dataMgr->QueryAbilityInfoByUri(abilityUri, Constants::UNSPECIFIED_USERID, abilityInfo);
710 }
711 
QueryAbilityInfosByUri(const std::string & abilityUri,std::vector<AbilityInfo> & abilityInfos)712 bool BundleMgrHostImpl::QueryAbilityInfosByUri(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos)
713 {
714     APP_LOGD("start QueryAbilityInfosByUri, uri : %{private}s", abilityUri.c_str());
715     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
716         APP_LOGD("non-system app calling system api");
717         return true;
718     }
719     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
720         APP_LOGE("verify permission failed");
721         return false;
722     }
723     auto dataMgr = GetDataMgrFromService();
724     if (dataMgr == nullptr) {
725         APP_LOGE("DataMgr is nullptr");
726         return false;
727     }
728     return dataMgr->QueryAbilityInfosByUri(abilityUri, abilityInfos);
729 }
730 
QueryAbilityInfoByUri(const std::string & abilityUri,int32_t userId,AbilityInfo & abilityInfo)731 bool BundleMgrHostImpl::QueryAbilityInfoByUri(
732     const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo)
733 {
734     APP_LOGD("start QueryAbilityInfoByUri, uri : %{private}s, userId : %{public}d", abilityUri.c_str(), userId);
735     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
736         APP_LOGD("non-system app calling system api");
737         return true;
738     }
739     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
740         !BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
741         APP_LOGE("verify query permission failed");
742         return false;
743     }
744     auto dataMgr = GetDataMgrFromService();
745     if (dataMgr == nullptr) {
746         APP_LOGE("DataMgr is nullptr");
747         return false;
748     }
749     return dataMgr->QueryAbilityInfoByUri(abilityUri, userId, abilityInfo);
750 }
751 
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)752 bool BundleMgrHostImpl::QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos)
753 {
754     auto dataMgr = GetDataMgrFromService();
755     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
756         APP_LOGE("verify permission failed");
757         return false;
758     }
759     if (dataMgr == nullptr) {
760         APP_LOGE("DataMgr is nullptr");
761         return false;
762     }
763     return dataMgr->QueryKeepAliveBundleInfos(bundleInfos);
764 }
765 
GetAbilityLabel(const std::string & bundleName,const std::string & abilityName)766 std::string BundleMgrHostImpl::GetAbilityLabel(const std::string &bundleName, const std::string &abilityName)
767 {
768     APP_LOGD("start GetAbilityLabel, bundleName : %{public}s, abilityName : %{public}s",
769         bundleName.c_str(), abilityName.c_str());
770     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
771         APP_LOGD("non-system app calling system api");
772         return Constants::EMPTY_STRING;
773     }
774     if (!VerifyQueryPermission(bundleName)) {
775         APP_LOGE("verify permission failed");
776         return Constants::EMPTY_STRING;
777     }
778     auto dataMgr = GetDataMgrFromService();
779     if (dataMgr == nullptr) {
780         APP_LOGE("DataMgr is nullptr");
781         return Constants::EMPTY_STRING;
782     }
783     std::string label;
784     ErrCode ret = dataMgr->GetAbilityLabel(bundleName, Constants::EMPTY_STRING, abilityName, label);
785     if (ret != ERR_OK) {
786         return Constants::EMPTY_STRING;
787     }
788     return label;
789 }
790 
GetAbilityLabel(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::string & label)791 ErrCode BundleMgrHostImpl::GetAbilityLabel(const std::string &bundleName, const std::string &moduleName,
792     const std::string &abilityName, std::string &label)
793 {
794     if (!VerifySystemApi()) {
795         APP_LOGE("non-system app calling system api");
796         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
797     }
798     if (!VerifyQueryPermission(bundleName)) {
799         APP_LOGE("verify permission failed");
800         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
801     }
802     auto dataMgr = GetDataMgrFromService();
803     if (dataMgr == nullptr) {
804         APP_LOGE("DataMgr is nullptr");
805         return ERR_APPEXECFWK_SERVICE_NOT_READY;
806     }
807     return dataMgr->GetAbilityLabel(bundleName, moduleName, abilityName, label);
808 }
809 
GetBundleArchiveInfo(const std::string & hapFilePath,const BundleFlag flag,BundleInfo & bundleInfo)810 bool BundleMgrHostImpl::GetBundleArchiveInfo(
811     const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo)
812 {
813     return GetBundleArchiveInfo(hapFilePath, static_cast<int32_t>(flag), bundleInfo);
814 }
815 
GetBundleArchiveInfo(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)816 bool BundleMgrHostImpl::GetBundleArchiveInfo(
817     const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
818 {
819     APP_LOGD("start GetBundleArchiveInfo, hapFilePath : %{public}s, flags : %{public}d", hapFilePath.c_str(), flags);
820     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
821         APP_LOGD("non-system app calling system api");
822         return true;
823     }
824     if (hapFilePath.find(Constants::RELATIVE_PATH) != std::string::npos) {
825         APP_LOGE("invalid hapFilePath");
826         return false;
827     }
828     if (hapFilePath.find(Constants::SANDBOX_DATA_PATH) == std::string::npos) {
829         std::string realPath;
830         auto ret = BundleUtil::CheckFilePath(hapFilePath, realPath);
831         if (ret != ERR_OK) {
832             APP_LOGE("GetBundleArchiveInfo file path %{private}s invalid", hapFilePath.c_str());
833             return false;
834         }
835 
836         InnerBundleInfo info;
837         BundleParser bundleParser;
838         ret = bundleParser.Parse(realPath, info);
839         if (ret != ERR_OK) {
840             APP_LOGE("parse bundle info failed, error: %{public}d", ret);
841             return false;
842         }
843         APP_LOGD("verify permission success, begin to GetBundleArchiveInfo");
844         info.GetBundleInfo(flags, bundleInfo, Constants::NOT_EXIST_USERID);
845         return true;
846     } else {
847         return GetBundleArchiveInfoBySandBoxPath(hapFilePath, flags, bundleInfo) == ERR_OK;
848     }
849 }
850 
GetBundleArchiveInfoV9(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)851 ErrCode BundleMgrHostImpl::GetBundleArchiveInfoV9(
852     const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
853 {
854     APP_LOGD("start GetBundleArchiveInfoV9, hapFilePath : %{public}s, flags : %{public}d", hapFilePath.c_str(), flags);
855     if (!VerifySystemApi()) {
856         APP_LOGE("non-system app calling system api");
857         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
858     }
859     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
860         APP_LOGE("verify permission failed");
861         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
862     }
863     if (hapFilePath.find(Constants::SANDBOX_DATA_PATH) == 0) {
864         APP_LOGD("sandbox path");
865         return GetBundleArchiveInfoBySandBoxPath(hapFilePath, flags, bundleInfo, true);
866     }
867     std::string realPath;
868     ErrCode ret = BundleUtil::CheckFilePath(hapFilePath, realPath);
869     if (ret != ERR_OK) {
870         APP_LOGE("GetBundleArchiveInfoV9 file path %{private}s invalid", hapFilePath.c_str());
871         return ERR_BUNDLE_MANAGER_INVALID_HAP_PATH;
872     }
873     InnerBundleInfo info;
874     BundleParser bundleParser;
875     ret = bundleParser.Parse(realPath, info);
876     if (ret != ERR_OK) {
877         APP_LOGE("parse bundle info failed, error: %{public}d", ret);
878         return ERR_BUNDLE_MANAGER_INVALID_HAP_PATH;
879     }
880     info.GetBundleInfoV9(flags, bundleInfo, Constants::NOT_EXIST_USERID);
881     return ERR_OK;
882 }
883 
GetBundleArchiveInfoBySandBoxPath(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo,bool fromV9)884 ErrCode BundleMgrHostImpl::GetBundleArchiveInfoBySandBoxPath(const std::string &hapFilePath,
885     int32_t flags, BundleInfo &bundleInfo, bool fromV9)
886 {
887     std::string bundleName;
888     int32_t apiVersion = fromV9 ? Constants::INVALID_API_VERSION : Constants::API_VERSION_NINE;
889     if (!VerifySystemApi(apiVersion)) {
890         APP_LOGE("non-system app calling system api");
891         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
892     }
893     if (!ObtainCallingBundleName(bundleName)) {
894         APP_LOGE("get calling bundleName failed");
895         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
896     }
897     std::string hapRealPath;
898     if (!BundleUtil::RevertToRealPath(hapFilePath, bundleName, hapRealPath)) {
899         APP_LOGE("GetBundleArchiveInfo RevertToRealPath failed");
900         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
901     }
902     std::string tempHapPath = Constants::BUNDLE_MANAGER_SERVICE_PATH +
903         Constants::PATH_SEPARATOR + std::to_string(BundleUtil::GetCurrentTime());
904     if (!BundleUtil::CreateDir(tempHapPath)) {
905         APP_LOGE("GetBundleArchiveInfo make temp dir failed");
906         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
907     }
908     std::string hapName = hapFilePath.substr(hapFilePath.find_last_of("//") + 1);
909     std::string tempHapFile = tempHapPath + Constants::PATH_SEPARATOR + hapName;
910     if (InstalldClient::GetInstance()->CopyFile(hapRealPath, tempHapFile) != ERR_OK) {
911         APP_LOGE("GetBundleArchiveInfo copy hap file failed");
912         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
913     }
914     std::string realPath;
915     auto ret = BundleUtil::CheckFilePath(tempHapFile, realPath);
916     if (ret != ERR_OK) {
917         APP_LOGE("CheckFilePath failed");
918         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
919     }
920     InnerBundleInfo info;
921     BundleParser bundleParser;
922     ret = bundleParser.Parse(realPath, info);
923     if (ret != ERR_OK) {
924         APP_LOGE("parse bundle info failed, error: %{public}d", ret);
925         BundleUtil::DeleteDir(tempHapPath);
926         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
927     }
928     BundleUtil::DeleteDir(tempHapPath);
929     APP_LOGD("verify permission success, begin to GetBundleArchiveInfo");
930     if (fromV9) {
931         info.GetBundleInfoV9(flags, bundleInfo, Constants::NOT_EXIST_USERID);
932     } else {
933         info.GetBundleInfo(flags, bundleInfo, Constants::NOT_EXIST_USERID);
934     }
935     return ERR_OK;
936 }
937 
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)938 bool BundleMgrHostImpl::GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo)
939 {
940     APP_LOGD("start GetHapModuleInfo");
941     return GetHapModuleInfo(abilityInfo, Constants::UNSPECIFIED_USERID, hapModuleInfo);
942 }
943 
GetHapModuleInfo(const AbilityInfo & abilityInfo,int32_t userId,HapModuleInfo & hapModuleInfo)944 bool BundleMgrHostImpl::GetHapModuleInfo(const AbilityInfo &abilityInfo, int32_t userId, HapModuleInfo &hapModuleInfo)
945 {
946     APP_LOGD("start GetHapModuleInfo with bundleName %{public}s and userId: %{public}d",
947         abilityInfo.bundleName.c_str(), userId);
948     std::string callingBundleName = "";
949     GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
950     APP_LOGD("callingBundleName : %{public}s", callingBundleName.c_str());
951 
952     if (!BundlePermissionMgr::IsNativeTokenType() && (callingBundleName != abilityInfo.bundleName)) {
953         APP_LOGE("invalid token or get module info of other bundle");
954         return false;
955     }
956 
957     if (!VerifyQueryPermission(abilityInfo.bundleName)) {
958         APP_LOGE("verify permission failed");
959         return false;
960     }
961     if (abilityInfo.bundleName.empty() || abilityInfo.package.empty()) {
962         APP_LOGE("fail to GetHapModuleInfo due to params empty");
963         return false;
964     }
965     auto dataMgr = GetDataMgrFromService();
966     if (dataMgr == nullptr) {
967         APP_LOGE("DataMgr is nullptr");
968         return false;
969     }
970     return dataMgr->GetHapModuleInfo(abilityInfo, hapModuleInfo, userId);
971 }
972 
GetLaunchWantForBundle(const std::string & bundleName,Want & want,int32_t userId)973 ErrCode BundleMgrHostImpl::GetLaunchWantForBundle(const std::string &bundleName, Want &want, int32_t userId)
974 {
975     APP_LOGD("start GetLaunchWantForBundle, bundleName : %{public}s", bundleName.c_str());
976     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
977         APP_LOGE("non-system app calling system api");
978         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
979     }
980     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
981         APP_LOGE("verify permission failed");
982         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
983     }
984 
985     APP_LOGD("verify permission success, begin to GetLaunchWantForBundle");
986     auto dataMgr = GetDataMgrFromService();
987     if (dataMgr == nullptr) {
988         APP_LOGE("DataMgr is nullptr");
989         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
990     }
991 
992     return dataMgr->GetLaunchWantForBundle(bundleName, want, userId);
993 }
994 
GetPermissionDef(const std::string & permissionName,PermissionDef & permissionDef)995 ErrCode BundleMgrHostImpl::GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef)
996 {
997     if (!VerifySystemApi()) {
998         APP_LOGE("non-system app calling system api");
999         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1000     }
1001     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1002         APP_LOGE("verify GET_BUNDLE_INFO_PRIVILEGED failed");
1003         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1004     }
1005     if (permissionName.empty()) {
1006         APP_LOGW("fail to GetPermissionDef due to params empty");
1007         return ERR_BUNDLE_MANAGER_QUERY_PERMISSION_DEFINE_FAILED;
1008     }
1009     return BundlePermissionMgr::GetPermissionDef(permissionName, permissionDef);
1010 }
1011 
CleanBundleCacheFiles(const std::string & bundleName,const sptr<ICleanCacheCallback> & cleanCacheCallback,int32_t userId)1012 ErrCode BundleMgrHostImpl::CleanBundleCacheFiles(
1013     const std::string &bundleName, const sptr<ICleanCacheCallback> &cleanCacheCallback,
1014     int32_t userId)
1015 {
1016     if (userId == Constants::UNSPECIFIED_USERID) {
1017         userId = BundleUtil::GetUserIdByCallingUid();
1018     }
1019 
1020     APP_LOGD("start CleanBundleCacheFiles, bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
1021     if (!VerifySystemApi()) {
1022         APP_LOGE("non-system app calling system api");
1023         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1024     }
1025     if (userId < 0) {
1026         APP_LOGE("userId is invalid");
1027         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1028         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1029     }
1030 
1031     if (bundleName.empty() || !cleanCacheCallback) {
1032         APP_LOGE("the cleanCacheCallback is nullptr or bundleName empty");
1033         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1034         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
1035     }
1036 
1037     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_REMOVECACHEFILE)) {
1038         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES permission denied");
1039         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1040         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1041     }
1042 
1043     ApplicationInfo applicationInfo;
1044     auto dataMgr = GetDataMgrFromService();
1045     if (dataMgr == nullptr) {
1046         APP_LOGE("DataMgr is nullptr");
1047         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1048         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
1049     }
1050 
1051     auto ret = dataMgr->GetApplicationInfoWithResponseId(bundleName,
1052         static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE), userId, applicationInfo);
1053     if (ret != ERR_OK) {
1054         APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
1055         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1056         return ret;
1057     }
1058 
1059     if (applicationInfo.isSystemApp && !applicationInfo.userDataClearable) {
1060         APP_LOGE("can not clean cacheFiles of %{public}s due to userDataClearable is false", bundleName.c_str());
1061         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, true);
1062         return ERR_BUNDLE_MANAGER_CAN_NOT_CLEAR_USER_DATA;
1063     }
1064 
1065     CleanBundleCacheTask(bundleName, cleanCacheCallback, dataMgr, userId);
1066     return ERR_OK;
1067 }
1068 
CleanBundleCacheTask(const std::string & bundleName,const sptr<ICleanCacheCallback> & cleanCacheCallback,const std::shared_ptr<BundleDataMgr> & dataMgr,int32_t userId)1069 void BundleMgrHostImpl::CleanBundleCacheTask(const std::string &bundleName,
1070     const sptr<ICleanCacheCallback> &cleanCacheCallback,
1071     const std::shared_ptr<BundleDataMgr> &dataMgr,
1072     int32_t userId)
1073 {
1074     std::vector<std::string> rootDir;
1075     for (const auto &el : Constants::BUNDLE_EL) {
1076         std::string dataDir = Constants::BUNDLE_APP_DATA_BASE_DIR + el +
1077             Constants::PATH_SEPARATOR + std::to_string(userId) + Constants::BASE + bundleName;
1078         rootDir.emplace_back(dataDir);
1079     }
1080 
1081     auto cleanCache = [bundleName, userId, rootDir, dataMgr, cleanCacheCallback, this]() {
1082         std::vector<std::string> caches;
1083         for (const auto &st : rootDir) {
1084             std::vector<std::string> cache;
1085             if (InstalldClient::GetInstance()->GetBundleCachePath(st, cache) != ERR_OK) {
1086                 APP_LOGW("GetBundleCachePath failed, path: %{public}s", st.c_str());
1087             }
1088             std::copy(cache.begin(), cache.end(), std::back_inserter(caches));
1089         }
1090 
1091         bool succeed = true;
1092         if (!caches.empty()) {
1093             for (const auto& cache : caches) {
1094                 ErrCode ret = InstalldClient::GetInstance()->CleanBundleDataDir(cache);
1095                 if (ret != ERR_OK) {
1096                     APP_LOGE("CleanBundleDataDir failed, path: %{private}s", cache.c_str());
1097                     succeed = false;
1098                 }
1099             }
1100         }
1101         EventReport::SendCleanCacheSysEvent(bundleName, userId, true, !succeed);
1102         APP_LOGD("CleanBundleCacheFiles with succeed %{public}d", succeed);
1103         cleanCacheCallback->OnCleanCacheFinished(succeed);
1104         InnerBundleUserInfo innerBundleUserInfo;
1105         if (!this->GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1106             APP_LOGW("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
1107             return;
1108         }
1109         NotifyBundleEvents installRes = {
1110             .bundleName = bundleName,
1111             .resultCode = ERR_OK,
1112             .type = NotifyType::BUNDLE_CACHE_CLEARED,
1113             .uid = innerBundleUserInfo.uid,
1114             .accessTokenId = innerBundleUserInfo.accessTokenId
1115         };
1116         NotifyBundleStatus(installRes);
1117     };
1118     ffrt::submit(cleanCache);
1119 }
1120 
CleanBundleDataFiles(const std::string & bundleName,const int userId)1121 bool BundleMgrHostImpl::CleanBundleDataFiles(const std::string &bundleName, const int userId)
1122 {
1123     APP_LOGD("start CleanBundleDataFiles, bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
1124     if (!VerifySystemApi()) {
1125         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES system api denied");
1126         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1127         return false;
1128     }
1129     if (bundleName.empty() || userId < 0) {
1130         APP_LOGE("the  bundleName empty or invalid userid");
1131         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1132         return false;
1133     }
1134     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_REMOVECACHEFILE)) {
1135         APP_LOGE("ohos.permission.REMOVE_CACHE_FILES permission denied");
1136         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1137         return false;
1138     }
1139     ApplicationInfo applicationInfo;
1140     if (GetApplicationInfoV9(bundleName, static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE),
1141         userId, applicationInfo) != ERR_OK) {
1142         APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
1143         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1144         return false;
1145     }
1146 
1147     if (applicationInfo.isSystemApp && !applicationInfo.userDataClearable) {
1148         APP_LOGE("can not clean dataFiles of %{public}s due to userDataClearable is false", bundleName.c_str());
1149         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1150         return false;
1151     }
1152 
1153     InnerBundleUserInfo innerBundleUserInfo;
1154     if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1155         APP_LOGE("%{public}s, userId:%{public}d, GetBundleUserInfo failed", bundleName.c_str(), userId);
1156         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1157         return false;
1158     }
1159 
1160     if (BundlePermissionMgr::ClearUserGrantedPermissionState(applicationInfo.accessTokenId)) {
1161         APP_LOGE("%{public}s, ClearUserGrantedPermissionState failed", bundleName.c_str());
1162         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1163         return false;
1164     }
1165 
1166     if (InstalldClient::GetInstance()->RemoveBundleDataDir(bundleName, userId) != ERR_OK) {
1167         APP_LOGE("%{public}s, RemoveBundleDataDir failed", bundleName.c_str());
1168         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1169         return false;
1170     }
1171     CreateDirParam createDirParam;
1172     createDirParam.bundleName = bundleName;
1173     createDirParam.userId = userId;
1174     createDirParam.uid = innerBundleUserInfo.uid;
1175     createDirParam.gid = innerBundleUserInfo.uid;
1176     createDirParam.apl = GetAppPrivilegeLevel(bundleName, userId);
1177     createDirParam.isPreInstallApp = IsPreInstallApp(bundleName);
1178     createDirParam.debug = applicationInfo.debug;
1179     if (InstalldClient::GetInstance()->CreateBundleDataDir(createDirParam)) {
1180         APP_LOGE("%{public}s, CreateBundleDataDir failed", bundleName.c_str());
1181         EventReport::SendCleanCacheSysEvent(bundleName, userId, false, true);
1182         return false;
1183     }
1184 
1185     EventReport::SendCleanCacheSysEvent(bundleName, userId, false, false);
1186     return true;
1187 }
1188 
RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)1189 bool BundleMgrHostImpl::RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
1190 {
1191     APP_LOGD("start RegisterBundleStatusCallback");
1192     if ((!bundleStatusCallback) || (bundleStatusCallback->GetBundleName().empty())) {
1193         APP_LOGE("the bundleStatusCallback is nullptr or bundleName empty");
1194         return false;
1195     }
1196     // check permission
1197     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
1198         APP_LOGE("register bundle status callback failed due to lack of permission");
1199         return false;
1200     }
1201 
1202     auto dataMgr = GetDataMgrFromService();
1203     if (dataMgr == nullptr) {
1204         APP_LOGE("DataMgr is nullptr");
1205         return false;
1206     }
1207     return dataMgr->RegisterBundleStatusCallback(bundleStatusCallback);
1208 }
1209 
RegisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)1210 bool BundleMgrHostImpl::RegisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
1211 {
1212     APP_LOGD("begin to RegisterBundleEventCallback");
1213     if (bundleEventCallback == nullptr) {
1214         APP_LOGE("bundleEventCallback is null");
1215         return false;
1216     }
1217     if (IPCSkeleton::GetCallingUid() != Constants::FOUNDATION_UID) {
1218         APP_LOGE("verify calling uid failed");
1219         return false;
1220     }
1221     auto dataMgr = GetDataMgrFromService();
1222     if (dataMgr == nullptr) {
1223         APP_LOGE("DataMgr is nullptr");
1224         return false;
1225     }
1226     return dataMgr->RegisterBundleEventCallback(bundleEventCallback);
1227 }
1228 
UnregisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)1229 bool BundleMgrHostImpl::UnregisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
1230 {
1231     APP_LOGD("begin to UnregisterBundleEventCallback");
1232     if (bundleEventCallback == nullptr) {
1233         APP_LOGE("bundleEventCallback is null");
1234         return false;
1235     }
1236     if (IPCSkeleton::GetCallingUid() != Constants::FOUNDATION_UID) {
1237         APP_LOGE("verify calling uid failed");
1238         return false;
1239     }
1240     auto dataMgr = GetDataMgrFromService();
1241     if (dataMgr == nullptr) {
1242         APP_LOGE("DataMgr is nullptr");
1243         return false;
1244     }
1245     return dataMgr->UnregisterBundleEventCallback(bundleEventCallback);
1246 }
1247 
ClearBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)1248 bool BundleMgrHostImpl::ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
1249 {
1250     APP_LOGD("start ClearBundleStatusCallback");
1251     if (!bundleStatusCallback) {
1252         APP_LOGE("the bundleStatusCallback is nullptr");
1253         return false;
1254     }
1255 
1256     // check permission
1257     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
1258         APP_LOGE("register bundle status callback failed due to lack of permission");
1259         return false;
1260     }
1261 
1262     auto dataMgr = GetDataMgrFromService();
1263     if (dataMgr == nullptr) {
1264         APP_LOGE("DataMgr is nullptr");
1265         return false;
1266     }
1267     return dataMgr->ClearBundleStatusCallback(bundleStatusCallback);
1268 }
1269 
UnregisterBundleStatusCallback()1270 bool BundleMgrHostImpl::UnregisterBundleStatusCallback()
1271 {
1272     APP_LOGD("start UnregisterBundleStatusCallback");
1273     // check permission
1274     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
1275         APP_LOGE("register bundle status callback failed due to lack of permission");
1276         return false;
1277     }
1278 
1279     auto dataMgr = GetDataMgrFromService();
1280     if (dataMgr == nullptr) {
1281         APP_LOGE("DataMgr is nullptr");
1282         return false;
1283     }
1284     return dataMgr->UnregisterBundleStatusCallback();
1285 }
1286 
DumpInfos(const DumpFlag flag,const std::string & bundleName,int32_t userId,std::string & result)1287 bool BundleMgrHostImpl::DumpInfos(
1288     const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result)
1289 {
1290     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1291         APP_LOGE("verify permission failed");
1292         return false;
1293     }
1294     bool ret = false;
1295     switch (flag) {
1296         case DumpFlag::DUMP_BUNDLE_LIST: {
1297             ret = DumpAllBundleInfoNames(userId, result);
1298             break;
1299         }
1300         case DumpFlag::DUMP_BUNDLE_INFO: {
1301             ret = DumpBundleInfo(bundleName, userId, result);
1302             break;
1303         }
1304         case DumpFlag::DUMP_SHORTCUT_INFO: {
1305             ret = DumpShortcutInfo(bundleName, userId, result);
1306             break;
1307         }
1308         default:
1309             APP_LOGE("dump flag error");
1310             return false;
1311     }
1312     return ret;
1313 }
1314 
DumpAllBundleInfoNames(int32_t userId,std::string & result)1315 bool BundleMgrHostImpl::DumpAllBundleInfoNames(int32_t userId, std::string &result)
1316 {
1317     APP_LOGD("DumpAllBundleInfoNames begin");
1318     if (userId != Constants::ALL_USERID) {
1319         return DumpAllBundleInfoNamesByUserId(userId, result);
1320     }
1321 
1322     auto userIds = GetExistsCommonUserIs();
1323     for (auto userId : userIds) {
1324         DumpAllBundleInfoNamesByUserId(userId, result);
1325     }
1326 
1327     APP_LOGD("DumpAllBundleInfoNames success");
1328     return true;
1329 }
1330 
DumpAllBundleInfoNamesByUserId(int32_t userId,std::string & result)1331 bool BundleMgrHostImpl::DumpAllBundleInfoNamesByUserId(int32_t userId, std::string &result)
1332 {
1333     APP_LOGI("DumpAllBundleInfoNamesByUserId begin");
1334     auto dataMgr = GetDataMgrFromService();
1335     if (dataMgr == nullptr) {
1336         APP_LOGE("DataMgr is nullptr");
1337         return false;
1338     }
1339 
1340     std::vector<std::string> bundleNames;
1341     if (!dataMgr->GetBundleList(bundleNames, userId)) {
1342         APP_LOGE("get bundle list failed by userId(%{public}d)", userId);
1343         return false;
1344     }
1345 
1346     result.append("ID: ");
1347     result.append(std::to_string(userId));
1348     result.append(":\n");
1349     for (const auto &name : bundleNames) {
1350         result.append("\t");
1351         result.append(name);
1352         result.append("\n");
1353     }
1354     APP_LOGI("DumpAllBundleInfoNamesByUserId successfully");
1355     return true;
1356 }
1357 
DumpBundleInfo(const std::string & bundleName,int32_t userId,std::string & result)1358 bool BundleMgrHostImpl::DumpBundleInfo(
1359     const std::string &bundleName, int32_t userId, std::string &result)
1360 {
1361     APP_LOGD("DumpBundleInfo begin");
1362     std::vector<InnerBundleUserInfo> innerBundleUserInfos;
1363     if (userId == Constants::ALL_USERID) {
1364         if (!GetBundleUserInfos(bundleName, innerBundleUserInfos)) {
1365             APP_LOGE("get all userInfos in bundle(%{public}s) failed", bundleName.c_str());
1366             return false;
1367         }
1368         userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
1369     } else {
1370         InnerBundleUserInfo innerBundleUserInfo;
1371         if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1372             APP_LOGI("get userInfo in bundle(%{public}s) failed", bundleName.c_str());
1373         }
1374         innerBundleUserInfos.emplace_back(innerBundleUserInfo);
1375     }
1376 
1377     BundleInfo bundleInfo;
1378     if (!GetBundleInfo(bundleName,
1379         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
1380         BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION |
1381         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
1382         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE, bundleInfo, userId)) {
1383         APP_LOGE("get bundleInfo(%{public}s) failed", bundleName.c_str());
1384         return false;
1385     }
1386 
1387     result.append(bundleName);
1388     result.append(":\n");
1389     nlohmann::json jsonObject = bundleInfo;
1390     jsonObject.erase("abilityInfos");
1391     jsonObject.erase("signatureInfo");
1392     jsonObject.erase("extensionAbilityInfo");
1393     jsonObject["applicationInfo"] = bundleInfo.applicationInfo;
1394     jsonObject["userInfo"] = innerBundleUserInfos;
1395     result.append(jsonObject.dump(Constants::DUMP_INDENT));
1396     result.append("\n");
1397     APP_LOGI("DumpBundleInfo success with bundleName %{public}s", bundleName.c_str());
1398     return true;
1399 }
1400 
DumpShortcutInfo(const std::string & bundleName,int32_t userId,std::string & result)1401 bool BundleMgrHostImpl::DumpShortcutInfo(
1402     const std::string &bundleName, int32_t userId, std::string &result)
1403 {
1404     APP_LOGD("DumpShortcutInfo begin");
1405     std::vector<ShortcutInfo> shortcutInfos;
1406     if (userId == Constants::ALL_USERID) {
1407         std::vector<InnerBundleUserInfo> innerBundleUserInfos;
1408         if (!GetBundleUserInfos(bundleName, innerBundleUserInfos)) {
1409             APP_LOGE("get all userInfos in bundle(%{public}s) failed", bundleName.c_str());
1410             return false;
1411         }
1412         userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
1413     }
1414 
1415     if (!GetShortcutInfos(bundleName, userId, shortcutInfos)) {
1416         APP_LOGE("get all shortcut info by bundle(%{public}s) failed", bundleName.c_str());
1417         return false;
1418     }
1419 
1420     result.append("shortcuts");
1421     result.append(":\n");
1422     for (const auto &info : shortcutInfos) {
1423         result.append("\"shortcut\"");
1424         result.append(":\n");
1425         nlohmann::json jsonObject = info;
1426         result.append(jsonObject.dump(Constants::DUMP_INDENT));
1427         result.append("\n");
1428     }
1429     APP_LOGD("DumpShortcutInfo success with bundleName %{public}s", bundleName.c_str());
1430     return true;
1431 }
1432 
IsModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool & isRemovable)1433 ErrCode BundleMgrHostImpl::IsModuleRemovable(const std::string &bundleName, const std::string &moduleName,
1434     bool &isRemovable)
1435 {
1436     // check permission
1437     if (!VerifySystemApi()) {
1438         APP_LOGE("non-system app calling system api");
1439         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1440     }
1441     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1442         APP_LOGE("IsModuleRemovable failed due to lack of permission");
1443         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1444     }
1445     auto dataMgr = GetDataMgrFromService();
1446     if (dataMgr == nullptr) {
1447         APP_LOGE("DataMgr is nullptr");
1448         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
1449     }
1450     return dataMgr->IsModuleRemovable(bundleName, moduleName, isRemovable);
1451 }
1452 
SetModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool isEnable)1453 bool BundleMgrHostImpl::SetModuleRemovable(const std::string &bundleName, const std::string &moduleName, bool isEnable)
1454 {
1455     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1456         APP_LOGE("SetModuleRemovable failed due to lack of permission");
1457         return false;
1458     }
1459     auto dataMgr = GetDataMgrFromService();
1460     if (dataMgr == nullptr) {
1461         APP_LOGE("DataMgr is nullptr");
1462         return false;
1463     }
1464     return dataMgr->SetModuleRemovable(bundleName, moduleName, isEnable);
1465 }
1466 
GetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName)1467 bool BundleMgrHostImpl::GetModuleUpgradeFlag(const std::string &bundleName, const std::string &moduleName)
1468 {
1469     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
1470         APP_LOGE("GetModuleUpgradeFlag failed due to lack of permission");
1471         return false;
1472     }
1473     auto dataMgr = GetDataMgrFromService();
1474     if (dataMgr == nullptr) {
1475         APP_LOGE("DataMgr is nullptr");
1476         return false;
1477     }
1478     return dataMgr->GetModuleUpgradeFlag(bundleName, moduleName);
1479 }
1480 
SetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName,int32_t upgradeFlag)1481 ErrCode BundleMgrHostImpl::SetModuleUpgradeFlag(const std::string &bundleName,
1482     const std::string &moduleName, int32_t upgradeFlag)
1483 {
1484     // check permission
1485     if (!VerifySystemApi()) {
1486         APP_LOGE("non-system app calling system api");
1487         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1488     }
1489     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
1490         APP_LOGE("SetModuleUpgradeFlag failed due to lack of permission");
1491         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1492     }
1493     auto dataMgr = GetDataMgrFromService();
1494     if (dataMgr == nullptr) {
1495         APP_LOGE("DataMgr is nullptr");
1496         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
1497     }
1498     return dataMgr->SetModuleUpgradeFlag(bundleName, moduleName, upgradeFlag);
1499 }
1500 
IsApplicationEnabled(const std::string & bundleName,bool & isEnable)1501 ErrCode BundleMgrHostImpl::IsApplicationEnabled(const std::string &bundleName, bool &isEnable)
1502 {
1503     APP_LOGD("start IsApplicationEnabled, bundleName : %{public}s", bundleName.c_str());
1504     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
1505         APP_LOGE("non-system app calling system api");
1506         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1507     }
1508     auto dataMgr = GetDataMgrFromService();
1509     if (dataMgr == nullptr) {
1510         APP_LOGE("DataMgr is nullptr");
1511         return ERR_APPEXECFWK_SERVICE_NOT_READY;
1512     }
1513     return dataMgr->IsApplicationEnabled(bundleName, isEnable);
1514 }
1515 
SetApplicationEnabled(const std::string & bundleName,bool isEnable,int32_t userId)1516 ErrCode BundleMgrHostImpl::SetApplicationEnabled(const std::string &bundleName, bool isEnable, int32_t userId)
1517 {
1518     APP_LOGD("SetApplicationEnabled begin");
1519     if (userId == Constants::UNSPECIFIED_USERID) {
1520         userId = BundleUtil::GetUserIdByCallingUid();
1521     }
1522     if (!VerifySystemApi()) {
1523         APP_LOGE("non-system app calling system api");
1524         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1525     }
1526     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
1527         APP_LOGE("verify permission failed");
1528         EventReport::SendComponentStateSysEvent(bundleName, "", userId, isEnable, true);
1529         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1530     }
1531     APP_LOGD("verify permission success, begin to SetApplicationEnabled");
1532     auto dataMgr = GetDataMgrFromService();
1533     if (dataMgr == nullptr) {
1534         APP_LOGE("DataMgr is nullptr");
1535         EventReport::SendComponentStateSysEvent(bundleName, "", userId, isEnable, true);
1536         return ERR_APPEXECFWK_SERVICE_NOT_READY;
1537     }
1538 
1539     auto ret = dataMgr->SetApplicationEnabled(bundleName, isEnable, userId);
1540     if (ret != ERR_OK) {
1541         APP_LOGE("Set application(%{public}s) enabled value faile.", bundleName.c_str());
1542         EventReport::SendComponentStateSysEvent(bundleName, "", userId, isEnable, true);
1543         return ret;
1544     }
1545 
1546     EventReport::SendComponentStateSysEvent(bundleName, "", userId, isEnable, false);
1547     InnerBundleUserInfo innerBundleUserInfo;
1548     if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
1549         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
1550         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1551     }
1552 
1553     NotifyBundleEvents installRes = {
1554         .bundleName = bundleName,
1555         .resultCode = ERR_OK,
1556         .type = NotifyType::APPLICATION_ENABLE,
1557         .uid = innerBundleUserInfo.uid,
1558         .accessTokenId = innerBundleUserInfo.accessTokenId
1559     };
1560     NotifyBundleStatus(installRes);
1561     APP_LOGD("SetApplicationEnabled finish");
1562     return ERR_OK;
1563 }
1564 
IsAbilityEnabled(const AbilityInfo & abilityInfo,bool & isEnable)1565 ErrCode BundleMgrHostImpl::IsAbilityEnabled(const AbilityInfo &abilityInfo, bool &isEnable)
1566 {
1567     APP_LOGD("start IsAbilityEnabled");
1568     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
1569         APP_LOGE("non-system app calling system api");
1570         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1571     }
1572     auto dataMgr = GetDataMgrFromService();
1573     if (dataMgr == nullptr) {
1574         APP_LOGE("DataMgr is nullptr");
1575         return ERR_APPEXECFWK_SERVICE_NOT_READY;
1576     }
1577     return dataMgr->IsAbilityEnabled(abilityInfo, isEnable);
1578 }
1579 
SetAbilityEnabled(const AbilityInfo & abilityInfo,bool isEnabled,int32_t userId)1580 ErrCode BundleMgrHostImpl::SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEnabled, int32_t userId)
1581 {
1582     APP_LOGD("start SetAbilityEnabled");
1583     if (userId == Constants::UNSPECIFIED_USERID) {
1584         userId = BundleUtil::GetUserIdByCallingUid();
1585     }
1586     if (!VerifySystemApi()) {
1587         APP_LOGE("non-system app calling system api");
1588         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1589     }
1590     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
1591         APP_LOGE("verify permission failed");
1592         EventReport::SendComponentStateSysEvent(
1593             abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, true);
1594         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1595     }
1596 
1597     APP_LOGD("verify permission success, begin to SetAbilityEnabled");
1598     auto dataMgr = GetDataMgrFromService();
1599     if (dataMgr == nullptr) {
1600         APP_LOGE("DataMgr is nullptr");
1601         EventReport::SendComponentStateSysEvent(
1602             abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, true);
1603         return ERR_APPEXECFWK_SERVICE_NOT_READY;
1604     }
1605     auto ret = dataMgr->SetAbilityEnabled(abilityInfo, isEnabled, userId);
1606     if (ret != ERR_OK) {
1607         APP_LOGE("Set ability(%{public}s) enabled value failed.", abilityInfo.bundleName.c_str());
1608         EventReport::SendComponentStateSysEvent(
1609             abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, true);
1610         return ret;
1611     }
1612 
1613     EventReport::SendComponentStateSysEvent(
1614         abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, false);
1615     InnerBundleUserInfo innerBundleUserInfo;
1616     if (!GetBundleUserInfo(abilityInfo.bundleName, userId, innerBundleUserInfo)) {
1617         APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", abilityInfo.bundleName.c_str());
1618         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1619     }
1620 
1621     NotifyBundleEvents installRes = {
1622         .bundleName = abilityInfo.bundleName,
1623         .abilityName = abilityInfo.name,
1624         .resultCode = ERR_OK,
1625         .type = NotifyType::APPLICATION_ENABLE,
1626         .uid = innerBundleUserInfo.uid,
1627         .accessTokenId = innerBundleUserInfo.accessTokenId,
1628     };
1629     NotifyBundleStatus(installRes);
1630     return ERR_OK;
1631 }
1632 
GetBundleInstaller()1633 sptr<IBundleInstaller> BundleMgrHostImpl::GetBundleInstaller()
1634 {
1635     APP_LOGD("start GetBundleInstaller");
1636     if (!VerifySystemApi()) {
1637         APP_LOGE("non-system app calling system api");
1638         return nullptr;
1639     }
1640     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
1641 }
1642 
GetBundleUserMgr()1643 sptr<IBundleUserMgr> BundleMgrHostImpl::GetBundleUserMgr()
1644 {
1645     int32_t callingUid = IPCSkeleton::GetCallingUid();
1646     if (callingUid != Constants::ACCOUNT_UID) {
1647         APP_LOGE("invalid calling uid %{public}d to GetbundleUserMgr", callingUid);
1648         return nullptr;
1649     }
1650     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleUserMgr();
1651 }
1652 
GetAllFormsInfo(std::vector<FormInfo> & formInfos)1653 bool BundleMgrHostImpl::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
1654 {
1655     APP_LOGD("start GetAllFormsInfo");
1656     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1657         APP_LOGE("verify permission failed");
1658         return false;
1659     }
1660     auto dataMgr = GetDataMgrFromService();
1661     if (dataMgr == nullptr) {
1662         APP_LOGE("DataMgr is nullptr");
1663         return false;
1664     }
1665     return dataMgr->GetAllFormsInfo(formInfos);
1666 }
1667 
GetFormsInfoByApp(const std::string & bundleName,std::vector<FormInfo> & formInfos)1668 bool BundleMgrHostImpl::GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos)
1669 {
1670     APP_LOGD("start GetFormsInfoByApp, bundleName : %{public}s", bundleName.c_str());
1671     if (!VerifyQueryPermission(bundleName)) {
1672         APP_LOGE("verify permission failed");
1673         return false;
1674     }
1675     auto dataMgr = GetDataMgrFromService();
1676     if (dataMgr == nullptr) {
1677         APP_LOGE("DataMgr is nullptr");
1678         return false;
1679     }
1680     return dataMgr->GetFormsInfoByApp(bundleName, formInfos);
1681 }
1682 
GetFormsInfoByModule(const std::string & bundleName,const std::string & moduleName,std::vector<FormInfo> & formInfos)1683 bool BundleMgrHostImpl::GetFormsInfoByModule(
1684     const std::string &bundleName, const std::string &moduleName, std::vector<FormInfo> &formInfos)
1685 {
1686     APP_LOGD("start GetFormsInfoByModule, bundleName : %{public}s, moduleName : %{public}s",
1687         bundleName.c_str(), moduleName.c_str());
1688     if (!VerifyQueryPermission(bundleName)) {
1689         APP_LOGE("verify permission failed");
1690         return false;
1691     }
1692     auto dataMgr = GetDataMgrFromService();
1693     if (dataMgr == nullptr) {
1694         APP_LOGE("DataMgr is nullptr");
1695         return false;
1696     }
1697     return dataMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos);
1698 }
1699 
GetShortcutInfos(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)1700 bool BundleMgrHostImpl::GetShortcutInfos(
1701     const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
1702 {
1703     return GetShortcutInfos(bundleName, Constants::UNSPECIFIED_USERID, shortcutInfos);
1704 }
1705 
GetShortcutInfos(const std::string & bundleName,int32_t userId,std::vector<ShortcutInfo> & shortcutInfos)1706 bool BundleMgrHostImpl::GetShortcutInfos(
1707     const std::string &bundleName, int32_t userId, std::vector<ShortcutInfo> &shortcutInfos)
1708 {
1709     APP_LOGD("start GetShortcutInfos, bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
1710     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
1711         APP_LOGD("non-system app calling system api");
1712         return true;
1713     }
1714     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1715         APP_LOGE("verify permission failed");
1716         return false;
1717     }
1718     APP_LOGD("verify permission success, begin to GetShortcutInfos");
1719     auto dataMgr = GetDataMgrFromService();
1720     if (dataMgr == nullptr) {
1721         APP_LOGE("DataMgr is nullptr");
1722         return false;
1723     }
1724     return dataMgr->GetShortcutInfos(bundleName, userId, shortcutInfos);
1725 }
1726 
GetShortcutInfoV9(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)1727 ErrCode BundleMgrHostImpl::GetShortcutInfoV9(const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
1728 {
1729     if (!VerifySystemApi()) {
1730         APP_LOGE("non-system app calling system api");
1731         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1732     }
1733     if (!VerifyPrivilegedPermission(bundleName)) {
1734         APP_LOGE("verify permission failed");
1735         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1736     }
1737     auto dataMgr = GetDataMgrFromService();
1738     if (dataMgr == nullptr) {
1739         APP_LOGE("DataMgr is nullptr");
1740         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1741     }
1742     return dataMgr->GetShortcutInfoV9(bundleName, Constants::UNSPECIFIED_USERID, shortcutInfos);
1743 }
1744 
GetAllCommonEventInfo(const std::string & eventKey,std::vector<CommonEventInfo> & commonEventInfos)1745 bool BundleMgrHostImpl::GetAllCommonEventInfo(const std::string &eventKey,
1746     std::vector<CommonEventInfo> &commonEventInfos)
1747 {
1748     APP_LOGD("start GetAllCommonEventInfo, eventKey : %{public}s", eventKey.c_str());
1749     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1750         APP_LOGE("verify permission failed");
1751         return false;
1752     }
1753     auto dataMgr = GetDataMgrFromService();
1754     if (dataMgr == nullptr) {
1755         APP_LOGE("DataMgr is nullptr");
1756         return false;
1757     }
1758     return dataMgr->GetAllCommonEventInfo(eventKey, commonEventInfos);
1759 }
1760 
GetDistributedBundleInfo(const std::string & networkId,const std::string & bundleName,DistributedBundleInfo & distributedBundleInfo)1761 bool BundleMgrHostImpl::GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName,
1762     DistributedBundleInfo &distributedBundleInfo)
1763 {
1764     APP_LOGD("start GetDistributedBundleInfo, bundleName : %{public}s", bundleName.c_str());
1765 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
1766     if (!BundlePermissionMgr::IsNativeTokenType()) {
1767         APP_LOGE("invalid token is not allowed to call this function");
1768         return false;
1769     }
1770     if (!VerifyQueryPermission(bundleName)) {
1771         APP_LOGE("verify permission failed");
1772         return false;
1773     }
1774     auto distributedBundleMgr = GetDistributedBundleMgrService();
1775     if (distributedBundleMgr == nullptr) {
1776         APP_LOGE("DistributedBundleMgrService is nullptr");
1777         return false;
1778     }
1779     return distributedBundleMgr->GetDistributedBundleInfo(networkId, bundleName, distributedBundleInfo);
1780 #else
1781     APP_LOGW("DISTRIBUTED_BUNDLE_FRAMEWORK is false");
1782     return false;
1783 #endif
1784 }
1785 
QueryExtensionAbilityInfos(const Want & want,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1786 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const Want &want, const int32_t &flag, const int32_t &userId,
1787     std::vector<ExtensionAbilityInfo> &extensionInfos)
1788 {
1789     APP_LOGD("QueryExtensionAbilityInfos without type begin");
1790     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
1791         APP_LOGD("non-system app calling system api");
1792         return true;
1793     }
1794     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
1795         APP_LOGE("verify permission failed");
1796         return false;
1797     }
1798     APP_LOGD("want uri is %{private}s", want.GetUriString().c_str());
1799     auto dataMgr = GetDataMgrFromService();
1800     if (dataMgr == nullptr) {
1801         APP_LOGE("DataMgr is nullptr");
1802         return false;
1803     }
1804     bool ret = dataMgr->QueryExtensionAbilityInfos(want, flag, userId, extensionInfos);
1805     if (!ret) {
1806         APP_LOGE("QueryExtensionAbilityInfos is failed");
1807         return false;
1808     }
1809     if (extensionInfos.empty()) {
1810         APP_LOGE("no valid extension info can be inquired");
1811         return false;
1812     }
1813     return true;
1814 }
1815 
QueryExtensionAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1816 ErrCode BundleMgrHostImpl::QueryExtensionAbilityInfosV9(const Want &want, int32_t flags, int32_t userId,
1817     std::vector<ExtensionAbilityInfo> &extensionInfos)
1818 {
1819     APP_LOGD("QueryExtensionAbilityInfosV9 without type begin");
1820     if (!VerifySystemApi()) {
1821         APP_LOGE("non-system app calling system api");
1822         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1823     }
1824     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
1825         APP_LOGE("verify permission failed");
1826         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1827     }
1828     APP_LOGD("want uri is %{private}s", want.GetUriString().c_str());
1829     auto dataMgr = GetDataMgrFromService();
1830     if (dataMgr == nullptr) {
1831         APP_LOGE("DataMgr is nullptr");
1832         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1833     }
1834     ErrCode ret = dataMgr->QueryExtensionAbilityInfosV9(want, flags, userId, extensionInfos);
1835     if (ret != ERR_OK) {
1836         APP_LOGE("QueryExtensionAbilityInfosV9 is failed");
1837         return ret;
1838     }
1839     if (extensionInfos.empty()) {
1840         APP_LOGE("no valid extension info can be inquired");
1841         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
1842     }
1843     return ERR_OK;
1844 }
1845 
QueryExtensionAbilityInfos(const Want & want,const ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1846 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const Want &want, const ExtensionAbilityType &extensionType,
1847     const int32_t &flag, const int32_t &userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
1848 {
1849     APP_LOGD("QueryExtensionAbilityInfos begin");
1850     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
1851         APP_LOGD("non-system app calling system api");
1852         return true;
1853     }
1854     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
1855         APP_LOGE("verify permission failed");
1856         return false;
1857     }
1858     auto dataMgr = GetDataMgrFromService();
1859     if (dataMgr == nullptr) {
1860         APP_LOGE("DataMgr is nullptr");
1861         return false;
1862     }
1863     std::vector<ExtensionAbilityInfo> infos;
1864     bool ret = dataMgr->QueryExtensionAbilityInfos(want, flag, userId, infos);
1865     if (!ret) {
1866         APP_LOGE("QueryExtensionAbilityInfos is failed");
1867         return false;
1868     }
1869     for_each(infos.begin(), infos.end(), [&extensionType, &extensionInfos](const auto &info)->decltype(auto) {
1870         APP_LOGD("QueryExtensionAbilityInfos extensionType is %{public}d, info.type is %{public}d",
1871             static_cast<int32_t>(extensionType), static_cast<int32_t>(info.type));
1872         if (extensionType == info.type) {
1873             extensionInfos.emplace_back(info);
1874         }
1875     });
1876     if (extensionInfos.empty()) {
1877         APP_LOGE("no valid extension info can be inquired");
1878         return false;
1879     }
1880     return true;
1881 }
1882 
QueryExtensionAbilityInfosV9(const Want & want,const ExtensionAbilityType & extensionType,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1883 ErrCode BundleMgrHostImpl::QueryExtensionAbilityInfosV9(const Want &want, const ExtensionAbilityType &extensionType,
1884     int32_t flags, int32_t userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
1885 {
1886     APP_LOGD("QueryExtensionAbilityInfosV9 begin");
1887     if (!VerifySystemApi()) {
1888         APP_LOGE("non-system app calling system api");
1889         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
1890     }
1891     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
1892         APP_LOGE("verify permission failed");
1893         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
1894     }
1895     auto dataMgr = GetDataMgrFromService();
1896     if (dataMgr == nullptr) {
1897         APP_LOGE("DataMgr is nullptr");
1898         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1899     }
1900     std::vector<ExtensionAbilityInfo> infos;
1901     ErrCode ret = dataMgr->QueryExtensionAbilityInfosV9(want, flags, userId, infos);
1902     if (ret != ERR_OK) {
1903         APP_LOGE("QueryExtensionAbilityInfosV9 is failed");
1904         return ret;
1905     }
1906     for_each(infos.begin(), infos.end(), [&extensionType, &extensionInfos](const auto &info)->decltype(auto) {
1907         APP_LOGD("QueryExtensionAbilityInfosV9 extensionType is %{public}d, info.type is %{public}d",
1908             static_cast<int32_t>(extensionType), static_cast<int32_t>(info.type));
1909         if (extensionType == info.type) {
1910             extensionInfos.emplace_back(info);
1911         }
1912     });
1913     if (extensionInfos.empty()) {
1914         APP_LOGE("no valid extension info can be inquired");
1915         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
1916     }
1917     return ERR_OK;
1918 }
1919 
QueryExtensionAbilityInfos(const ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1920 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const ExtensionAbilityType &extensionType, const int32_t &userId,
1921     std::vector<ExtensionAbilityInfo> &extensionInfos)
1922 {
1923     APP_LOGD("QueryExtensionAbilityInfos with type begin");
1924     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
1925         APP_LOGD("non-system app calling system api");
1926         return true;
1927     }
1928     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1929         APP_LOGE("verify permission failed");
1930         return false;
1931     }
1932     auto dataMgr = GetDataMgrFromService();
1933     if (dataMgr == nullptr) {
1934         APP_LOGE("DataMgr is nullptr");
1935         return false;
1936     }
1937     bool ret = dataMgr->QueryExtensionAbilityInfos(extensionType, userId, extensionInfos);
1938     if (!ret) {
1939         APP_LOGE("QueryExtensionAbilityInfos is failed");
1940         return false;
1941     }
1942 
1943     if (extensionInfos.empty()) {
1944         APP_LOGE("no valid extension info can be inquired");
1945         return false;
1946     }
1947     return true;
1948 }
1949 
GetDataMgrFromService()1950 const std::shared_ptr<BundleDataMgr> BundleMgrHostImpl::GetDataMgrFromService()
1951 {
1952     return DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
1953 }
1954 
1955 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
GetDistributedBundleMgrService()1956 const OHOS::sptr<IDistributedBms> BundleMgrHostImpl::GetDistributedBundleMgrService()
1957 {
1958     auto saMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1959     if (saMgr == nullptr) {
1960         APP_LOGE("saMgr is nullptr");
1961         return nullptr;
1962     }
1963     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
1964         saMgr->CheckSystemAbility(OHOS::DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1965     return OHOS::iface_cast<IDistributedBms>(remoteObject);
1966 }
1967 #endif
1968 
1969 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetConnectAbilityMgrFromService()1970 const std::shared_ptr<BundleConnectAbilityMgr> BundleMgrHostImpl::GetConnectAbilityMgrFromService()
1971 {
1972     return DelayedSingleton<BundleMgrService>::GetInstance()->GetConnectAbility();
1973 }
1974 #endif
1975 
GetExistsCommonUserIs()1976 std::set<int32_t> BundleMgrHostImpl::GetExistsCommonUserIs()
1977 {
1978     std::set<int32_t> userIds;
1979     auto dataMgr = GetDataMgrFromService();
1980     if (dataMgr == nullptr) {
1981         APP_LOGE("Get dataMgr shared_ptr nullptr");
1982         return userIds;
1983     }
1984 
1985     for (auto userId : dataMgr->GetAllUser()) {
1986         if (userId >= Constants::START_USERID) {
1987             userIds.insert(userId);
1988         }
1989     }
1990     return userIds;
1991 }
1992 
VerifyQueryPermission(const std::string & queryBundleName)1993 bool BundleMgrHostImpl::VerifyQueryPermission(const std::string &queryBundleName)
1994 {
1995     if (BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) ||
1996         BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
1997         APP_LOGD("verify query permission successfully");
1998         return true;
1999     }
2000     std::string callingBundleName;
2001     bool ret = GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
2002     APP_LOGD("callingBundleName : %{public}s", callingBundleName.c_str());
2003     if (ret && (queryBundleName == callingBundleName)) {
2004         APP_LOGD("query own info, verify success");
2005         return true;
2006     }
2007     APP_LOGD("verify query permission failed");
2008     return false;
2009 }
2010 
VerifyPrivilegedPermission(const std::string & queryBundleName)2011 bool BundleMgrHostImpl::VerifyPrivilegedPermission(const std::string &queryBundleName)
2012 {
2013     if (BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
2014         return true;
2015     }
2016     std::string callingBundleName;
2017     bool ret = GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
2018     APP_LOGD("callingBundleName : %{public}s", callingBundleName.c_str());
2019     if (ret && (queryBundleName == callingBundleName)) {
2020         APP_LOGD("query own info, verify success");
2021         return true;
2022     }
2023     APP_LOGE("verify query permission failed");
2024     return false;
2025 }
2026 
GetAppPrivilegeLevel(const std::string & bundleName,int32_t userId)2027 std::string BundleMgrHostImpl::GetAppPrivilegeLevel(const std::string &bundleName, int32_t userId)
2028 {
2029     APP_LOGD("start GetAppPrivilegeLevel");
2030     if (!VerifyQueryPermission(bundleName)) {
2031         APP_LOGE("verify permission failed");
2032         return Constants::EMPTY_STRING;
2033     }
2034     auto dataMgr = GetDataMgrFromService();
2035     if (dataMgr == nullptr) {
2036         APP_LOGE("DataMgr is nullptr");
2037         return Constants::EMPTY_STRING;
2038     }
2039     return dataMgr->GetAppPrivilegeLevel(bundleName, userId);
2040 }
2041 
VerifyCallingPermission(const std::string & permission)2042 bool BundleMgrHostImpl::VerifyCallingPermission(const std::string &permission)
2043 {
2044     APP_LOGD("VerifyCallingPermission begin");
2045     return BundlePermissionMgr::VerifyCallingPermission(permission);
2046 }
2047 
QueryExtensionAbilityInfoByUri(const std::string & uri,int32_t userId,ExtensionAbilityInfo & extensionAbilityInfo)2048 bool BundleMgrHostImpl::QueryExtensionAbilityInfoByUri(const std::string &uri, int32_t userId,
2049     ExtensionAbilityInfo &extensionAbilityInfo)
2050 {
2051     APP_LOGD("uri : %{private}s, userId : %{public}d", uri.c_str(), userId);
2052     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
2053         APP_LOGD("non-system app calling system api");
2054         return true;
2055     }
2056     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2057         !BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
2058         APP_LOGE("verify query permission failed");
2059         return false;
2060     }
2061     auto dataMgr = GetDataMgrFromService();
2062     if (dataMgr == nullptr) {
2063         APP_LOGE("DataMgr is nullptr");
2064         return false;
2065     }
2066     return dataMgr->QueryExtensionAbilityInfoByUri(uri, userId, extensionAbilityInfo);
2067 }
2068 
GetAppIdByBundleName(const std::string & bundleName,const int userId)2069 std::string BundleMgrHostImpl::GetAppIdByBundleName(const std::string &bundleName, const int userId)
2070 {
2071     APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2072     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) &&
2073         !BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
2074         APP_LOGE("verify query permission failed");
2075         return Constants::EMPTY_STRING;
2076     }
2077     auto dataMgr = GetDataMgrFromService();
2078     if (dataMgr == nullptr) {
2079         APP_LOGE("DataMgr is nullptr");
2080         return Constants::EMPTY_STRING;
2081     }
2082     BundleInfo bundleInfo;
2083     bool ret = dataMgr->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId);
2084     if (!ret) {
2085         APP_LOGE("get bundleInfo failed");
2086         return Constants::EMPTY_STRING;
2087     }
2088     APP_LOGD("appId is %{private}s", bundleInfo.appId.c_str());
2089     return bundleInfo.appId;
2090 }
2091 
GetAppType(const std::string & bundleName)2092 std::string BundleMgrHostImpl::GetAppType(const std::string &bundleName)
2093 {
2094     APP_LOGD("bundleName : %{public}s", bundleName.c_str());
2095     if (!VerifyQueryPermission(bundleName)) {
2096         APP_LOGE("verify permission failed");
2097         return Constants::EMPTY_STRING;
2098     }
2099     auto dataMgr = GetDataMgrFromService();
2100     if (dataMgr == nullptr) {
2101         APP_LOGE("DataMgr is nullptr");
2102         return Constants::EMPTY_STRING;
2103     }
2104     BundleInfo bundleInfo;
2105     bool ret = dataMgr->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, Constants::UNSPECIFIED_USERID);
2106     if (!ret) {
2107         APP_LOGE("get bundleInfo failed");
2108         return Constants::EMPTY_STRING;
2109     }
2110     bool isSystemApp = bundleInfo.applicationInfo.isSystemApp;
2111     std::string appType = isSystemApp ? SYSTEM_APP : THIRD_PARTY_APP;
2112     APP_LOGD("appType is %{public}s", appType.c_str());
2113     return appType;
2114 }
2115 
GetUidByBundleName(const std::string & bundleName,const int userId)2116 int BundleMgrHostImpl::GetUidByBundleName(const std::string &bundleName, const int userId)
2117 {
2118     APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2119     if (!BundlePermissionMgr::IsNativeTokenType()) {
2120         APP_LOGE("verify token type failed");
2121         return Constants::INVALID_UID;
2122     }
2123     auto dataMgr = GetDataMgrFromService();
2124     if (dataMgr == nullptr) {
2125         APP_LOGE("DataMgr is nullptr");
2126         return Constants::INVALID_UID;
2127     }
2128     std::vector<BundleInfo> bundleInfos;
2129     int32_t uid = Constants::INVALID_UID;
2130     bool ret = dataMgr->GetBundleInfos(GET_BUNDLE_DEFAULT, bundleInfos, userId);
2131     if (ret) {
2132         for (auto bundleInfo : bundleInfos) {
2133             if (bundleInfo.name == bundleName) {
2134                 uid = bundleInfo.uid;
2135                 break;
2136             }
2137         }
2138         APP_LOGD("get bundle uid success");
2139     } else {
2140         APP_LOGE("can not get bundleInfo's uid");
2141     }
2142     APP_LOGD("uid is %{public}d", uid);
2143     return uid;
2144 }
2145 
GetUidByDebugBundleName(const std::string & bundleName,const int userId)2146 int BundleMgrHostImpl::GetUidByDebugBundleName(const std::string &bundleName, const int userId)
2147 {
2148     APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
2149     auto dataMgr = GetDataMgrFromService();
2150     if (dataMgr == nullptr) {
2151         APP_LOGE("DataMgr is nullptr");
2152         return Constants::INVALID_UID;
2153     }
2154     ApplicationInfo appInfo;
2155     int32_t uid = Constants::INVALID_UID;
2156     bool ret = dataMgr->GetApplicationInfo(bundleName, GET_BUNDLE_DEFAULT, userId, appInfo);
2157     if (ret && appInfo.debug) {
2158         uid = appInfo.uid;
2159         APP_LOGD("get debug bundle uid success, uid is %{public}d", uid);
2160     } else {
2161         APP_LOGE("can not get bundleInfo's uid");
2162     }
2163     return uid;
2164 }
2165 
GetAbilityInfo(const std::string & bundleName,const std::string & abilityName,AbilityInfo & abilityInfo)2166 bool BundleMgrHostImpl::GetAbilityInfo(
2167     const std::string &bundleName, const std::string &abilityName, AbilityInfo &abilityInfo)
2168 {
2169     APP_LOGD("start GetAbilityInfo, bundleName : %{public}s, abilityName : %{public}s",
2170         bundleName.c_str(), abilityName.c_str());
2171     ElementName elementName("", bundleName, abilityName);
2172     Want want;
2173     want.SetElement(elementName);
2174     return QueryAbilityInfo(want, abilityInfo);
2175 }
2176 
GetAbilityInfo(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,AbilityInfo & abilityInfo)2177 bool BundleMgrHostImpl::GetAbilityInfo(
2178     const std::string &bundleName, const std::string &moduleName,
2179     const std::string &abilityName, AbilityInfo &abilityInfo)
2180 {
2181     APP_LOGD("start GetAbilityInfo, bundleName : %{public}s, moduleName : %{public}s, abilityName : %{public}s",
2182         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
2183     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
2184         APP_LOGD("non-system app calling system api");
2185         return true;
2186     }
2187     ElementName elementName("", bundleName, abilityName, moduleName);
2188     Want want;
2189     want.SetElement(elementName);
2190     return QueryAbilityInfo(want, abilityInfo);
2191 }
2192 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)2193 bool BundleMgrHostImpl::ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId,
2194     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo)
2195 {
2196     APP_LOGD("start ImplicitQueryInfoByPriority, flags : %{public}d, userId : %{public}d", flags, userId);
2197     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
2198         APP_LOGD("non-system app calling system api");
2199         return true;
2200     }
2201     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
2202         APP_LOGE("verify permission failed");
2203         return false;
2204     }
2205     auto dataMgr = GetDataMgrFromService();
2206     if (dataMgr == nullptr) {
2207         APP_LOGE("DataMgr is nullptr");
2208         return false;
2209     }
2210     return dataMgr->ImplicitQueryInfoByPriority(want, flags, userId, abilityInfo, extensionInfo);
2211 }
2212 
ImplicitQueryInfos(const Want & want,int32_t flags,int32_t userId,bool withDefault,std::vector<AbilityInfo> & abilityInfos,std::vector<ExtensionAbilityInfo> & extensionInfos)2213 bool BundleMgrHostImpl::ImplicitQueryInfos(const Want &want, int32_t flags, int32_t userId,  bool withDefault,
2214     std::vector<AbilityInfo> &abilityInfos, std::vector<ExtensionAbilityInfo> &extensionInfos)
2215 {
2216     APP_LOGD("begin to ImplicitQueryInfos, flags : %{public}d, userId : %{public}d", flags, userId);
2217     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
2218         APP_LOGD("non-system app calling system api");
2219         return true;
2220     }
2221     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
2222         APP_LOGE("verify permission failed");
2223         return false;
2224     }
2225     auto dataMgr = GetDataMgrFromService();
2226     if (dataMgr == nullptr) {
2227         APP_LOGE("DataMgr is nullptr");
2228         return false;
2229     }
2230     return dataMgr->ImplicitQueryInfos(want, flags, userId, withDefault, abilityInfos, extensionInfos);
2231 }
2232 
Dump(int fd,const std::vector<std::u16string> & args)2233 int BundleMgrHostImpl::Dump(int fd, const std::vector<std::u16string> &args)
2234 {
2235     std::string result;
2236     std::vector<std::string> argsStr;
2237     for (auto item : args) {
2238         argsStr.emplace_back(Str16ToStr8(item));
2239     }
2240 
2241     if (!DelayedSingleton<BundleMgrService>::GetInstance()->Hidump(argsStr, result)) {
2242         APP_LOGE("Hidump error");
2243         return ERR_APPEXECFWK_HIDUMP_ERROR;
2244     }
2245 
2246     int ret = dprintf(fd, "%s\n", result.c_str());
2247     if (ret < 0) {
2248         APP_LOGE("dprintf error");
2249         return ERR_APPEXECFWK_HIDUMP_ERROR;
2250     }
2251 
2252     return ERR_OK;
2253 }
2254 
GetAllDependentModuleNames(const std::string & bundleName,const std::string & moduleName,std::vector<std::string> & dependentModuleNames)2255 bool BundleMgrHostImpl::GetAllDependentModuleNames(const std::string &bundleName, const std::string &moduleName,
2256     std::vector<std::string> &dependentModuleNames)
2257 {
2258     APP_LOGD("GetAllDependentModuleNames: bundleName: %{public}s, moduleName: %{public}s",
2259         bundleName.c_str(), moduleName.c_str());
2260     if (!VerifyQueryPermission(bundleName)) {
2261         APP_LOGE("verify permission failed");
2262         return false;
2263     }
2264     auto dataMgr = GetDataMgrFromService();
2265     if (dataMgr == nullptr) {
2266         APP_LOGE("DataMgr is nullptr");
2267         return false;
2268     }
2269     return dataMgr->GetAllDependentModuleNames(bundleName, moduleName, dependentModuleNames);
2270 }
2271 
GetSandboxBundleInfo(const std::string & bundleName,int32_t appIndex,int32_t userId,BundleInfo & info)2272 ErrCode BundleMgrHostImpl::GetSandboxBundleInfo(
2273     const std::string &bundleName, int32_t appIndex, int32_t userId, BundleInfo &info)
2274 {
2275     APP_LOGD("start GetSandboxBundleInfo, bundleName : %{public}s, appIndex : %{public}d, userId : %{public}d",
2276         bundleName.c_str(), appIndex, userId);
2277     // check bundle name
2278     if (bundleName.empty()) {
2279         APP_LOGE("GetSandboxBundleInfo failed due to empty bundleName");
2280         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2281     }
2282     // check appIndex
2283     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
2284         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
2285         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2286     }
2287     if (!VerifyQueryPermission(bundleName)) {
2288         APP_LOGE("verify permission failed");
2289         return ERR_APPEXECFWK_PERMISSION_DENIED;
2290     }
2291     auto dataMgr = GetDataMgrFromService();
2292     if (dataMgr == nullptr) {
2293         APP_LOGE("DataMgr is nullptr");
2294         return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR;
2295     }
2296     auto sandboxAppHelper = dataMgr->GetSandboxAppHelper();
2297     if (sandboxAppHelper == nullptr) {
2298         APP_LOGE("sandboxAppHelper is nullptr");
2299         return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR;
2300     }
2301     int32_t requestUserId = dataMgr->GetUserId(userId);
2302     if (requestUserId == Constants::INVALID_USERID) {
2303         return ERR_APPEXECFWK_SANDBOX_QUERY_INVALID_USER_ID;
2304     }
2305     return sandboxAppHelper->GetSandboxAppBundleInfo(bundleName, appIndex, requestUserId, info);
2306 }
2307 
ObtainCallingBundleName(std::string & bundleName)2308 bool BundleMgrHostImpl::ObtainCallingBundleName(std::string &bundleName)
2309 {
2310     bool ret = GetBundleNameForUid(IPCSkeleton::GetCallingUid(), bundleName);
2311     if (!ret) {
2312         APP_LOGE("query calling bundle name failed");
2313         return false;
2314     }
2315     APP_LOGD("calling bundleName is : %{public}s", bundleName.c_str());
2316     return ret;
2317 }
2318 
GetBundleStats(const std::string & bundleName,int32_t userId,std::vector<int64_t> & bundleStats)2319 bool BundleMgrHostImpl::GetBundleStats(const std::string &bundleName, int32_t userId,
2320     std::vector<int64_t> &bundleStats)
2321 {
2322     if (!VerifyQueryPermission(bundleName)) {
2323         APP_LOGE("verify permission failed");
2324         return false;
2325     }
2326     auto dataMgr = GetDataMgrFromService();
2327     if (dataMgr == nullptr) {
2328         APP_LOGE("DataMgr is nullptr");
2329         return false;
2330     }
2331     return dataMgr->GetBundleStats(bundleName, userId, bundleStats);
2332 }
2333 
GetStringById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,int32_t userId,const std::string & localeInfo)2334 std::string BundleMgrHostImpl::GetStringById(const std::string &bundleName, const std::string &moduleName,
2335     uint32_t resId, int32_t userId, const std::string &localeInfo)
2336 {
2337     if (!BundlePermissionMgr::IsNativeTokenType()) {
2338         APP_LOGE("verify token type failed");
2339         return Constants::EMPTY_STRING;
2340     }
2341     auto dataMgr = GetDataMgrFromService();
2342     if (dataMgr == nullptr) {
2343         APP_LOGE("DataMgr is nullptr");
2344         return Constants::EMPTY_STRING;
2345     }
2346     return dataMgr->GetStringById(bundleName, moduleName, resId, userId, localeInfo);
2347 }
2348 
GetIconById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,uint32_t density,int32_t userId)2349 std::string BundleMgrHostImpl::GetIconById(
2350     const std::string &bundleName, const std::string &moduleName, uint32_t resId, uint32_t density, int32_t userId)
2351 {
2352     if (!BundlePermissionMgr::IsNativeTokenType()) {
2353         APP_LOGE("verify token type failed");
2354         return Constants::EMPTY_STRING;
2355     }
2356     auto dataMgr = GetDataMgrFromService();
2357     if (dataMgr == nullptr) {
2358         APP_LOGE("DataMgr is nullptr");
2359         return Constants::EMPTY_STRING;
2360     }
2361     return dataMgr->GetIconById(bundleName, moduleName, resId, density, userId);
2362 }
2363 
2364 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
GetDefaultAppProxy()2365 sptr<IDefaultApp> BundleMgrHostImpl::GetDefaultAppProxy()
2366 {
2367     return DelayedSingleton<BundleMgrService>::GetInstance()->GetDefaultAppProxy();
2368 }
2369 #endif
2370 
2371 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
GetAppControlProxy()2372 sptr<IAppControlMgr> BundleMgrHostImpl::GetAppControlProxy()
2373 {
2374     return DelayedSingleton<BundleMgrService>::GetInstance()->GetAppControlProxy();
2375 }
2376 #endif
2377 
GetQuickFixManagerProxy()2378 sptr<IQuickFixManager> BundleMgrHostImpl::GetQuickFixManagerProxy()
2379 {
2380 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
2381     return DelayedSingleton<BundleMgrService>::GetInstance()->GetQuickFixManagerProxy();
2382 #else
2383     return nullptr;
2384 #endif
2385 }
2386 
GetOverlayManagerProxy()2387 sptr<IOverlayManager> BundleMgrHostImpl::GetOverlayManagerProxy()
2388 {
2389 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
2390     return DelayedSingleton<BundleMgrService>::GetInstance()->GetOverlayManagerProxy();
2391 #else
2392     return nullptr;
2393 #endif
2394 }
2395 
GetSandboxAbilityInfo(const Want & want,int32_t appIndex,int32_t flags,int32_t userId,AbilityInfo & info)2396 ErrCode BundleMgrHostImpl::GetSandboxAbilityInfo(const Want &want, int32_t appIndex, int32_t flags, int32_t userId,
2397     AbilityInfo &info)
2398 {
2399     APP_LOGD("start GetSandboxAbilityInfo appIndex : %{public}d, userId : %{public}d", appIndex, userId);
2400     // check appIndex
2401     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
2402         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
2403         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2404     }
2405     if (!BundlePermissionMgr::IsNativeTokenType()) {
2406         APP_LOGE("invalid token is not allowed to call this function");
2407         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2408     }
2409     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
2410         APP_LOGE("verify permission failed");
2411         return ERR_APPEXECFWK_PERMISSION_DENIED;
2412     }
2413     auto dataMgr = GetDataMgrFromService();
2414     if (dataMgr == nullptr) {
2415         APP_LOGE("DataMgr is nullptr");
2416         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2417     }
2418 
2419     if (!dataMgr->QueryAbilityInfo(want, flags, userId, info, appIndex)) {
2420         APP_LOGE("query ability info failed");
2421         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2422     }
2423     return ERR_OK;
2424 }
2425 
GetSandboxExtAbilityInfos(const Want & want,int32_t appIndex,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos)2426 ErrCode BundleMgrHostImpl::GetSandboxExtAbilityInfos(const Want &want, int32_t appIndex, int32_t flags,
2427     int32_t userId, std::vector<ExtensionAbilityInfo> &infos)
2428 {
2429     APP_LOGD("start GetSandboxExtAbilityInfos appIndex : %{public}d, userId : %{public}d", appIndex, userId);
2430     // check appIndex
2431     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
2432         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
2433         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2434     }
2435     if (!BundlePermissionMgr::IsNativeTokenType()) {
2436         APP_LOGE("invalid token is not allowed to call this function");
2437         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2438     }
2439     if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
2440         APP_LOGE("verify permission failed");
2441         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2442     }
2443     auto dataMgr = GetDataMgrFromService();
2444     if (dataMgr == nullptr) {
2445         APP_LOGE("DataMgr is nullptr");
2446         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2447     }
2448 
2449     if (!dataMgr->QueryExtensionAbilityInfos(want, flags, userId, infos, appIndex)) {
2450         APP_LOGE("query extension ability info failed");
2451         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2452     }
2453     return ERR_OK;
2454 }
2455 
GetSandboxHapModuleInfo(const AbilityInfo & abilityInfo,int32_t appIndex,int32_t userId,HapModuleInfo & info)2456 ErrCode BundleMgrHostImpl::GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, int32_t appIndex, int32_t userId,
2457     HapModuleInfo &info)
2458 {
2459     APP_LOGD("start GetSandboxHapModuleInfo appIndex : %{public}d, userId : %{public}d", appIndex, userId);
2460     // check appIndex
2461     if (appIndex <= Constants::INITIAL_APP_INDEX || appIndex > Constants::MAX_APP_INDEX) {
2462         APP_LOGE("the appIndex %{public}d is invalid", appIndex);
2463         return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
2464     }
2465     if (!BundlePermissionMgr::IsNativeTokenType()) {
2466         APP_LOGE("invalid token is not allowed to call this function");
2467         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2468     }
2469     if (!VerifyQueryPermission(abilityInfo.bundleName)) {
2470         APP_LOGE("verify permission failed");
2471         return false;
2472     }
2473     auto dataMgr = GetDataMgrFromService();
2474     if (dataMgr == nullptr) {
2475         APP_LOGE("DataMgr is nullptr");
2476         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2477     }
2478     auto sandboxAppHelper = dataMgr->GetSandboxAppHelper();
2479     if (sandboxAppHelper == nullptr) {
2480         APP_LOGE("sandboxAppHelper is nullptr");
2481         return ERR_APPEXECFWK_SANDBOX_QUERY_INTERNAL_ERROR;
2482     }
2483     int32_t requestUserId = dataMgr->GetUserId(userId);
2484     if (requestUserId == Constants::INVALID_USERID) {
2485         return ERR_APPEXECFWK_SANDBOX_QUERY_INVALID_USER_ID;
2486     }
2487     return sandboxAppHelper->GetSandboxHapModuleInfo(abilityInfo, appIndex, requestUserId, info);
2488 }
2489 
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)2490 ErrCode BundleMgrHostImpl::GetMediaData(const std::string &bundleName, const std::string &moduleName,
2491     const std::string &abilityName, std::unique_ptr<uint8_t[]> &mediaDataPtr, size_t &len, int32_t userId)
2492 {
2493     if (!VerifySystemApi(Constants::API_VERSION_NINE)) {
2494         APP_LOGE("non-system app calling system api");
2495         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2496     }
2497     if (!VerifyQueryPermission(bundleName)) {
2498         APP_LOGE("verify permission failed");
2499         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2500     }
2501     auto dataMgr = GetDataMgrFromService();
2502     if (dataMgr == nullptr) {
2503         APP_LOGE("DataMgr is nullptr");
2504         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2505     }
2506     return dataMgr->GetMediaData(bundleName, moduleName, abilityName, mediaDataPtr, len, userId);
2507 }
2508 
NotifyBundleStatus(const NotifyBundleEvents & installRes)2509 void BundleMgrHostImpl::NotifyBundleStatus(const NotifyBundleEvents &installRes)
2510 {
2511     std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
2512     commonEventMgr->NotifyBundleStatus(installRes, nullptr);
2513 }
2514 
SetDebugMode(bool isDebug)2515 ErrCode BundleMgrHostImpl::SetDebugMode(bool isDebug)
2516 {
2517     int32_t callingUid = IPCSkeleton::GetCallingUid();
2518     if (callingUid != Constants::ROOT_UID && callingUid != Constants::BMS_UID) {
2519         APP_LOGE("invalid calling uid %{public}d to set debug mode", callingUid);
2520         return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_UID_CHECK_FAILED;
2521     }
2522     if (isDebug) {
2523         BundleVerifyMgr::EnableDebug();
2524     } else {
2525         BundleVerifyMgr::DisableDebug();
2526     }
2527     return ERR_OK;
2528 }
2529 
VerifySystemApi(int32_t beginApiVersion)2530 bool BundleMgrHostImpl::VerifySystemApi(int32_t beginApiVersion)
2531 {
2532     APP_LOGD("begin to verify system app");
2533     return BundlePermissionMgr::VerifySystemApp(beginApiVersion);
2534 }
2535 
GetAppProvisionInfo(const std::string & bundleName,int32_t userId,AppProvisionInfo & appProvisionInfo)2536 ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, int32_t userId,
2537     AppProvisionInfo &appProvisionInfo)
2538 {
2539     APP_LOGD("begin to GetAppProvisionInfo bundleName: %{public}s, userId: %{public}d", bundleName.c_str(),
2540         userId);
2541     if (!VerifySystemApi()) {
2542         APP_LOGE("non-system app calling system api");
2543         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2544     }
2545     if (!VerifyPrivilegedPermission(bundleName)) {
2546         APP_LOGE("verify permission failed");
2547         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2548     }
2549     auto dataMgr = GetDataMgrFromService();
2550     if (dataMgr == nullptr) {
2551         APP_LOGE("DataMgr is nullptr");
2552         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2553     }
2554     return dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo);
2555 }
2556 
GetProvisionMetadata(const std::string & bundleName,int32_t userId,std::vector<Metadata> & provisionMetadatas)2557 ErrCode BundleMgrHostImpl::GetProvisionMetadata(const std::string &bundleName, int32_t userId,
2558     std::vector<Metadata> &provisionMetadatas)
2559 {
2560     if (!VerifyQueryPermission(bundleName)) {
2561         APP_LOGE("verify permission failed");
2562         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2563     }
2564     auto dataMgr = GetDataMgrFromService();
2565     if (dataMgr == nullptr) {
2566         APP_LOGE("DataMgr is nullptr");
2567         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2568     }
2569     return dataMgr->GetProvisionMetadata(bundleName, userId, provisionMetadatas);
2570 }
2571 
GetAllSharedBundleInfo(std::vector<SharedBundleInfo> & sharedBundles)2572 ErrCode BundleMgrHostImpl::GetAllSharedBundleInfo(std::vector<SharedBundleInfo> &sharedBundles)
2573 {
2574     APP_LOGD("begin to GetAllSharedBundleInfo");
2575     if (!VerifySystemApi()) {
2576         APP_LOGE("non-system app calling system api");
2577         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2578     }
2579     if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
2580         APP_LOGE("verify permission failed");
2581         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2582     }
2583 
2584     auto dataMgr = GetDataMgrFromService();
2585     if (dataMgr == nullptr) {
2586         APP_LOGE("dataMgr is nullptr");
2587         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2588     }
2589     return dataMgr->GetAllSharedBundleInfo(sharedBundles);
2590 }
2591 
GetSharedBundleInfo(const std::string & bundleName,const std::string & moduleName,std::vector<SharedBundleInfo> & sharedBundles)2592 ErrCode BundleMgrHostImpl::GetSharedBundleInfo(const std::string &bundleName, const std::string &moduleName,
2593     std::vector<SharedBundleInfo> &sharedBundles)
2594 {
2595     APP_LOGD("GetSharedBundleInfo: bundleName: %{public}s, moduleName: %{public}s",
2596         bundleName.c_str(), moduleName.c_str());
2597     if (!VerifySystemApi()) {
2598         APP_LOGE("non-system app calling system api");
2599         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2600     }
2601     if (!VerifyPrivilegedPermission(bundleName)) {
2602         APP_LOGE("verify permission failed");
2603         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2604     }
2605 
2606     auto dataMgr = GetDataMgrFromService();
2607     if (dataMgr == nullptr) {
2608         APP_LOGE("dataMgr is nullptr");
2609         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2610     }
2611     return dataMgr->GetSharedBundleInfo(bundleName, moduleName, sharedBundles);
2612 }
2613 
GetSharedBundleInfoBySelf(const std::string & bundleName,SharedBundleInfo & sharedBundleInfo)2614 ErrCode BundleMgrHostImpl::GetSharedBundleInfoBySelf(const std::string &bundleName,
2615     SharedBundleInfo &sharedBundleInfo)
2616 {
2617     APP_LOGD("begin to GetSharedBundleInfoBySelf bundleName: %{public}s", bundleName.c_str());
2618     if (!VerifySystemApi()) {
2619         APP_LOGE("non-system app calling system api");
2620         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2621     }
2622     auto dataMgr = GetDataMgrFromService();
2623     if (dataMgr == nullptr) {
2624         APP_LOGE("DataMgr is nullptr");
2625         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2626     }
2627     return dataMgr->GetSharedBundleInfoBySelf(bundleName, sharedBundleInfo);
2628 }
2629 
GetSharedDependencies(const std::string & bundleName,const std::string & moduleName,std::vector<Dependency> & dependencies)2630 ErrCode BundleMgrHostImpl::GetSharedDependencies(const std::string &bundleName, const std::string &moduleName,
2631     std::vector<Dependency> &dependencies)
2632 {
2633     APP_LOGD("GetSharedDependencies: bundleName: %{public}s, moduleName: %{public}s",
2634         bundleName.c_str(), moduleName.c_str());
2635     if (!VerifyQueryPermission(bundleName)) {
2636         APP_LOGE("verify permission failed");
2637         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2638     }
2639     auto dataMgr = GetDataMgrFromService();
2640     if (dataMgr == nullptr) {
2641         APP_LOGE("DataMgr is nullptr");
2642         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2643     }
2644     return dataMgr->GetSharedDependencies(bundleName, moduleName, dependencies);
2645 }
2646 
VerifyDependency(const std::string & sharedBundleName)2647 bool BundleMgrHostImpl::VerifyDependency(const std::string &sharedBundleName)
2648 {
2649     std::string callingBundleName;
2650     bool ret = GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
2651     if (!ret) {
2652         APP_LOGE("GetBundleNameForUid failed");
2653         return false;
2654     }
2655 
2656     auto dataMgr = GetDataMgrFromService();
2657     if (dataMgr == nullptr) {
2658         APP_LOGE("DataMgr is nullptr");
2659         return false;
2660     }
2661 
2662     InnerBundleInfo callingBundleInfo;
2663     if (!dataMgr->FetchInnerBundleInfo(callingBundleName, callingBundleInfo)) {
2664         APP_LOGE("get callingBundleInfo failed");
2665         return false;
2666     }
2667 
2668     // check whether callingBundleName is dependent on sharedBundleName
2669     const auto& dependencies = callingBundleInfo.GetDependencies();
2670     auto iter = std::find_if(dependencies.begin(), dependencies.end(), [&sharedBundleName](const auto &dependency) {
2671         return dependency.bundleName == sharedBundleName;
2672     });
2673     if (iter == dependencies.end()) {
2674         APP_LOGE("%{public}s is not dependent on %{public}s", callingBundleName.c_str(), sharedBundleName.c_str());
2675         return false;
2676     }
2677     APP_LOGD("verify dependency successfully");
2678     return true;
2679 }
2680 
IsPreInstallApp(const std::string & bundleName)2681 bool BundleMgrHostImpl::IsPreInstallApp(const std::string &bundleName)
2682 {
2683     auto dataMgr = GetDataMgrFromService();
2684     if (dataMgr == nullptr) {
2685         APP_LOGE("DataMgr is nullptr");
2686         return false;
2687     }
2688     return dataMgr->IsPreInstallApp(bundleName);
2689 }
2690 
GetProxyDataInfos(const std::string & bundleName,const std::string & moduleName,std::vector<ProxyData> & proxyDatas,int32_t userId)2691 ErrCode BundleMgrHostImpl::GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
2692     std::vector<ProxyData> &proxyDatas, int32_t userId)
2693 {
2694     if (!BundlePermissionMgr::IsNativeTokenType()) {
2695         APP_LOGE("verify token type failed");
2696         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2697     }
2698     auto dataMgr = GetDataMgrFromService();
2699     if (dataMgr == nullptr) {
2700         APP_LOGE("DataMgr is nullptr");
2701         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2702     }
2703     return dataMgr->GetProxyDataInfos(bundleName, moduleName, userId, proxyDatas);
2704 }
2705 
GetAllProxyDataInfos(std::vector<ProxyData> & proxyDatas,int32_t userId)2706 ErrCode BundleMgrHostImpl::GetAllProxyDataInfos(std::vector<ProxyData> &proxyDatas, int32_t userId)
2707 {
2708     if (!BundlePermissionMgr::IsNativeTokenType()) {
2709         APP_LOGE("verify token type failed");
2710         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2711     }
2712     auto dataMgr = GetDataMgrFromService();
2713     if (dataMgr == nullptr) {
2714         APP_LOGE("DataMgr is nullptr");
2715         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2716     }
2717     return dataMgr->GetAllProxyDataInfos(userId, proxyDatas);
2718 }
2719 
GetSpecifiedDistributionType(const std::string & bundleName,std::string & specifiedDistributionType)2720 ErrCode BundleMgrHostImpl::GetSpecifiedDistributionType(const std::string &bundleName,
2721     std::string &specifiedDistributionType)
2722 {
2723     APP_LOGD("GetSpecifiedDistributionType bundleName: %{public}s", bundleName.c_str());
2724     if (!VerifySystemApi()) {
2725         APP_LOGE("non-system app calling system api");
2726         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2727     }
2728     if (!VerifyPrivilegedPermission(bundleName)) {
2729         APP_LOGE("verify permission failed");
2730         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2731     }
2732 
2733     auto dataMgr = GetDataMgrFromService();
2734     if (dataMgr == nullptr) {
2735         APP_LOGE("dataMgr is nullptr");
2736         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2737     }
2738     return dataMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType);
2739 }
2740 
GetAdditionalInfo(const std::string & bundleName,std::string & additionalInfo)2741 ErrCode BundleMgrHostImpl::GetAdditionalInfo(const std::string &bundleName,
2742     std::string &additionalInfo)
2743 {
2744     APP_LOGD("GetAdditionalInfo bundleName: %{public}s", bundleName.c_str());
2745     if (!VerifySystemApi()) {
2746         APP_LOGE("non-system app calling system api");
2747         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2748     }
2749     if (!VerifyPrivilegedPermission(bundleName)) {
2750         APP_LOGE("verify permission failed");
2751         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
2752     }
2753 
2754     auto dataMgr = GetDataMgrFromService();
2755     if (dataMgr == nullptr) {
2756         APP_LOGE("dataMgr is nullptr");
2757         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2758     }
2759     return dataMgr->GetAdditionalInfo(bundleName, additionalInfo);
2760 }
2761 
SetExtNameOrMIMEToApp(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const std::string & extName,const std::string & mimeType)2762 ErrCode BundleMgrHostImpl::SetExtNameOrMIMEToApp(const std::string &bundleName, const std::string &moduleName,
2763     const std::string &abilityName, const std::string &extName, const std::string &mimeType)
2764 {
2765     APP_LOGD("SetExtNameOrMIMEToApp bundleName: %{public}s, moduleName: %{public}s, \
2766         abilityName: %{public}s, extName: %{public}s, mimeType: %{public}s",
2767         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), extName.c_str(), mimeType.c_str());
2768     if (!BundlePermissionMgr::IsNativeTokenType()) {
2769         APP_LOGE("verify token type failed");
2770         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2771     }
2772     auto dataMgr = GetDataMgrFromService();
2773     if (dataMgr == nullptr) {
2774         APP_LOGE("dataMgr is nullptr");
2775         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2776     }
2777     return dataMgr->SetExtNameOrMIMEToApp(bundleName, moduleName, abilityName, extName, mimeType);
2778 }
2779 
DelExtNameOrMIMEToApp(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const std::string & extName,const std::string & mimeType)2780 ErrCode BundleMgrHostImpl::DelExtNameOrMIMEToApp(const std::string &bundleName, const std::string &moduleName,
2781     const std::string &abilityName, const std::string &extName, const std::string &mimeType)
2782 {
2783     APP_LOGD("DelExtNameOrMIMEToApp bundleName: %{public}s, moduleName: %{public}s, \
2784         abilityName: %{public}s, extName: %{public}s, mimeType: %{public}s",
2785         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), extName.c_str(), mimeType.c_str());
2786     if (!BundlePermissionMgr::IsNativeTokenType()) {
2787         APP_LOGE("verify token type failed");
2788         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
2789     }
2790     auto dataMgr = GetDataMgrFromService();
2791     if (dataMgr == nullptr) {
2792         APP_LOGE("dataMgr is nullptr");
2793         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2794     }
2795     return dataMgr->DelExtNameOrMIMEToApp(bundleName, moduleName, abilityName, extName, mimeType);
2796 }
2797 
QueryDataGroupInfos(const std::string & bundleName,int32_t userId,std::vector<DataGroupInfo> & infos)2798 bool BundleMgrHostImpl::QueryDataGroupInfos(const std::string &bundleName, int32_t userId,
2799     std::vector<DataGroupInfo> &infos)
2800 {
2801     APP_LOGD("QueryDataGroupInfos bundleName: %{public}s, userId: %{public}d", bundleName.c_str(), userId);
2802     if (!BundlePermissionMgr::IsNativeTokenType()) {
2803         APP_LOGE("verify token type failed");
2804         return false;
2805     }
2806     auto dataMgr = GetDataMgrFromService();
2807     if (dataMgr == nullptr) {
2808         APP_LOGE("dataMgr is nullptr");
2809         return false;
2810     }
2811     return dataMgr->QueryDataGroupInfos(bundleName, userId, infos);
2812 }
2813 
GetGroupDir(const std::string & dataGroupId,std::string & dir)2814 bool BundleMgrHostImpl::GetGroupDir(const std::string &dataGroupId, std::string &dir)
2815 {
2816     APP_LOGD("GetGroupDir dataGroupId: %{public}s", dataGroupId.c_str());
2817     auto dataMgr = GetDataMgrFromService();
2818     if (dataMgr == nullptr) {
2819         APP_LOGE("dataMgr is nullptr");
2820         return false;
2821     }
2822     return dataMgr->GetGroupDir(dataGroupId, dir);
2823 }
2824 
SetBrokerServiceStatus(bool isServiceExisted)2825 void BundleMgrHostImpl::SetBrokerServiceStatus(bool isServiceExisted)
2826 {
2827     APP_LOGD("broker service status is %{public}d", isServiceExisted);
2828     isBrokerServiceExisted_ = isServiceExisted;
2829 }
2830 
QueryAppGalleryBundleName(std::string & bundleName)2831 bool BundleMgrHostImpl::QueryAppGalleryBundleName(std::string &bundleName)
2832 {
2833     APP_LOGD("QueryAppGalleryBundleName in bundle host impl start");
2834     auto dataMgr = GetDataMgrFromService();
2835     if (dataMgr == nullptr) {
2836         APP_LOGE("DataMgr is nullptr");
2837         return false;
2838     }
2839     std::string abilityName;
2840     bool ret = dataMgr->QueryAppGalleryAbilityName(bundleName, abilityName);
2841     if (!ret) {
2842         APP_LOGE("get bundleName failed");
2843         return false;
2844     }
2845     APP_LOGD("bundleName is %{public}s", bundleName.c_str());
2846     return  true;
2847 }
2848 }  // namespace AppExecFwk
2849 }  // namespace OHOS
2850