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