1 /*
2 * Copyright (c) 2021-2022 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
21 #include "app_log_wrapper.h"
22 #include "bundle_mgr_service.h"
23 #include "bundle_parser.h"
24 #include "bundle_permission_mgr.h"
25 #include "bundle_util.h"
26 #include "directory_ex.h"
27 #include "element_name.h"
28 #include "installd_client.h"
29 #include "ipc_skeleton.h"
30 #include "json_serializer.h"
31
32 namespace OHOS {
33 namespace AppExecFwk {
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)34 bool BundleMgrHostImpl::GetApplicationInfo(
35 const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo)
36 {
37 return GetApplicationInfo(appName, static_cast<int32_t>(flag), userId, appInfo);
38 }
39
GetApplicationInfo(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)40 bool BundleMgrHostImpl::GetApplicationInfo(
41 const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo)
42 {
43 APP_LOGD("start GetApplicationInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
44 appName.c_str(), flags, userId);
45 if (!VerifyQueryPermission(appName)) {
46 APP_LOGE("verify permission failed");
47 return false;
48 }
49 APP_LOGD("verify permission success, bgein to GetApplicationInfo");
50 auto dataMgr = GetDataMgrFromService();
51 if (dataMgr == nullptr) {
52 APP_LOGE("DataMgr is nullptr");
53 return false;
54 }
55 return dataMgr->GetApplicationInfo(appName, flags, userId, appInfo);
56 }
57
GetApplicationInfos(const ApplicationFlag flag,const int userId,std::vector<ApplicationInfo> & appInfos)58 bool BundleMgrHostImpl::GetApplicationInfos(
59 const ApplicationFlag flag, const int userId, std::vector<ApplicationInfo> &appInfos)
60 {
61 return GetApplicationInfos(static_cast<int32_t>(flag), userId, appInfos);
62 }
63
GetApplicationInfos(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos)64 bool BundleMgrHostImpl::GetApplicationInfos(
65 int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos)
66 {
67 APP_LOGD("start GetApplicationInfos, flags : %{public}d, userId : %{public}d", flags, userId);
68 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
69 APP_LOGE("verify permission failed");
70 return false;
71 }
72 APP_LOGD("verify permission success, bgein to GetApplicationInfos");
73 auto dataMgr = GetDataMgrFromService();
74 if (dataMgr == nullptr) {
75 APP_LOGE("DataMgr is nullptr");
76 return false;
77 }
78 return dataMgr->GetApplicationInfos(flags, userId, appInfos);
79 }
80
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)81 bool BundleMgrHostImpl::GetBundleInfo(
82 const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId)
83 {
84 return GetBundleInfo(bundleName, static_cast<int32_t>(flag), bundleInfo, userId);
85 }
86
GetBundleInfo(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId)87 bool BundleMgrHostImpl::GetBundleInfo(
88 const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId)
89 {
90 APP_LOGD("start GetBundleInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d",
91 bundleName.c_str(), flags, userId);
92 if (!VerifyQueryPermission(bundleName)) {
93 APP_LOGE("verify permission failed");
94 return false;
95 }
96 APP_LOGD("verify permission success, bgein to GetBundleInfo");
97 auto dataMgr = GetDataMgrFromService();
98 if (dataMgr == nullptr) {
99 APP_LOGE("DataMgr is nullptr");
100 return false;
101 }
102 return dataMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId);
103 }
104
GetBundleUserInfo(const std::string & bundleName,int32_t userId,InnerBundleUserInfo & innerBundleUserInfo)105 bool BundleMgrHostImpl::GetBundleUserInfo(
106 const std::string &bundleName, int32_t userId, InnerBundleUserInfo &innerBundleUserInfo)
107 {
108 auto dataMgr = GetDataMgrFromService();
109 if (dataMgr == nullptr) {
110 APP_LOGE("DataMgr is nullptr");
111 return false;
112 }
113 return dataMgr->GetInnerBundleUserInfoByUserId(bundleName, userId, innerBundleUserInfo);
114 }
115
GetBundleUserInfos(const std::string & bundleName,std::vector<InnerBundleUserInfo> & innerBundleUserInfos)116 bool BundleMgrHostImpl::GetBundleUserInfos(
117 const std::string &bundleName, std::vector<InnerBundleUserInfo> &innerBundleUserInfos)
118 {
119 auto dataMgr = GetDataMgrFromService();
120 if (dataMgr == nullptr) {
121 APP_LOGE("DataMgr is nullptr");
122 return false;
123 }
124 return dataMgr->GetInnerBundleUserInfos(bundleName, innerBundleUserInfos);
125 }
126
GetBundleInfos(const BundleFlag flag,std::vector<BundleInfo> & bundleInfos,int32_t userId)127 bool BundleMgrHostImpl::GetBundleInfos(const BundleFlag flag, std::vector<BundleInfo> &bundleInfos, int32_t userId)
128 {
129 return GetBundleInfos(static_cast<int32_t>(flag), bundleInfos, userId);
130 }
131
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId)132 bool BundleMgrHostImpl::GetBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId)
133 {
134 APP_LOGD("start GetBundleInfos, flags : %{public}d, userId : %{public}d", flags, userId);
135 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
136 APP_LOGE("verify permission failed");
137 return false;
138 }
139 APP_LOGD("verify permission success, bgein to GetBundleInfos");
140 auto dataMgr = GetDataMgrFromService();
141 if (dataMgr == nullptr) {
142 APP_LOGE("DataMgr is nullptr");
143 return false;
144 }
145 return dataMgr->GetBundleInfos(flags, bundleInfos, userId);
146 }
147
GetBundleNameForUid(const int uid,std::string & bundleName)148 bool BundleMgrHostImpl::GetBundleNameForUid(const int uid, std::string &bundleName)
149 {
150 auto dataMgr = GetDataMgrFromService();
151 if (dataMgr == nullptr) {
152 APP_LOGE("DataMgr is nullptr");
153 return false;
154 }
155 return dataMgr->GetBundleNameForUid(uid, bundleName);
156 }
157
GetBundlesForUid(const int uid,std::vector<std::string> & bundleNames)158 bool BundleMgrHostImpl::GetBundlesForUid(const int uid, std::vector<std::string> &bundleNames)
159 {
160 auto dataMgr = GetDataMgrFromService();
161 if (dataMgr == nullptr) {
162 APP_LOGE("DataMgr is nullptr");
163 return false;
164 }
165 return dataMgr->GetBundlesForUid(uid, bundleNames);
166 }
167
GetNameForUid(const int uid,std::string & name)168 bool BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name)
169 {
170 auto dataMgr = GetDataMgrFromService();
171 if (dataMgr == nullptr) {
172 APP_LOGE("DataMgr is nullptr");
173 return false;
174 }
175 return dataMgr->GetNameForUid(uid, name);
176 }
177
GetBundleGids(const std::string & bundleName,std::vector<int> & gids)178 bool BundleMgrHostImpl::GetBundleGids(const std::string &bundleName, std::vector<int> &gids)
179 {
180 auto dataMgr = GetDataMgrFromService();
181 if (dataMgr == nullptr) {
182 APP_LOGE("DataMgr is nullptr");
183 return false;
184 }
185 return dataMgr->GetBundleGids(bundleName, gids);
186 }
187
GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids)188 bool BundleMgrHostImpl::GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector<int> &gids)
189 {
190 auto dataMgr = GetDataMgrFromService();
191 if (dataMgr == nullptr) {
192 APP_LOGE("DataMgr is nullptr");
193 return false;
194 }
195 return dataMgr->GetBundleGidsByUid(bundleName, uid, gids);
196 }
197
CheckIsSystemAppByUid(const int uid)198 bool BundleMgrHostImpl::CheckIsSystemAppByUid(const int uid)
199 {
200 auto dataMgr = GetDataMgrFromService();
201 if (dataMgr == nullptr) {
202 APP_LOGE("DataMgr is nullptr");
203 return false;
204 }
205 return dataMgr->CheckIsSystemAppByUid(uid);
206 }
207
GetBundleInfosByMetaData(const std::string & metaData,std::vector<BundleInfo> & bundleInfos)208 bool BundleMgrHostImpl::GetBundleInfosByMetaData(const std::string &metaData, std::vector<BundleInfo> &bundleInfos)
209 {
210 auto dataMgr = GetDataMgrFromService();
211 if (dataMgr == nullptr) {
212 APP_LOGE("DataMgr is nullptr");
213 return false;
214 }
215 return dataMgr->GetBundleInfosByMetaData(metaData, bundleInfos);
216 }
217
QueryAbilityInfo(const Want & want,AbilityInfo & abilityInfo)218 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, AbilityInfo &abilityInfo)
219 {
220 return QueryAbilityInfo(want, GET_ABILITY_INFO_WITH_APPLICATION, Constants::UNSPECIFIED_USERID, abilityInfo);
221 }
222
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo)223 bool BundleMgrHostImpl::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo)
224 {
225 if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
226 APP_LOGE("verify permission failed");
227 return false;
228 }
229 APP_LOGD("verify permission success, bgein to QueryAbilityInfo");
230 auto dataMgr = GetDataMgrFromService();
231 if (dataMgr == nullptr) {
232 APP_LOGE("DataMgr is nullptr");
233 return false;
234 }
235 return dataMgr->QueryAbilityInfo(want, flags, userId, abilityInfo);
236 }
237
QueryAbilityInfos(const Want & want,std::vector<AbilityInfo> & abilityInfos)238 bool BundleMgrHostImpl::QueryAbilityInfos(const Want &want, std::vector<AbilityInfo> &abilityInfos)
239 {
240 return QueryAbilityInfos(
241 want, GET_ABILITY_INFO_WITH_APPLICATION, Constants::UNSPECIFIED_USERID, abilityInfos);
242 }
243
QueryAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos)244 bool BundleMgrHostImpl::QueryAbilityInfos(
245 const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
246 {
247 if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
248 APP_LOGE("verify permission failed");
249 return false;
250 }
251 APP_LOGD("verify permission success, bgein to QueryAbilityInfos");
252 auto dataMgr = GetDataMgrFromService();
253 if (dataMgr == nullptr) {
254 APP_LOGE("DataMgr is nullptr");
255 return false;
256 }
257 return dataMgr->QueryAbilityInfos(want, flags, userId, abilityInfos);
258 }
259
QueryAbilityInfosForClone(const Want & want,std::vector<AbilityInfo> & abilityInfos)260 bool BundleMgrHostImpl::QueryAbilityInfosForClone(const Want &want, std::vector<AbilityInfo> &abilityInfos)
261 {
262 auto dataMgr = GetDataMgrFromService();
263 if (dataMgr == nullptr) {
264 APP_LOGE("DataMgr is nullptr");
265 return false;
266 }
267 return dataMgr->QueryAbilityInfosForClone(want, abilityInfos);
268 }
269
QueryAllAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)270 bool BundleMgrHostImpl::QueryAllAbilityInfos(const Want &want, int32_t userId, std::vector<AbilityInfo> &abilityInfos)
271 {
272 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
273 APP_LOGE("verify permission failed");
274 return false;
275 }
276 APP_LOGD("verify permission success, bgein to QueryAllAbilityInfos");
277 auto dataMgr = GetDataMgrFromService();
278 if (dataMgr == nullptr) {
279 APP_LOGE("DataMgr is nullptr");
280 return false;
281 }
282 return dataMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos);
283 }
284
QueryAbilityInfoByUri(const std::string & abilityUri,AbilityInfo & abilityInfo)285 bool BundleMgrHostImpl::QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo)
286 {
287 APP_LOGD("start QueryAbilityInfoByUri, uri : %{private}s", abilityUri.c_str());
288 auto dataMgr = GetDataMgrFromService();
289 if (dataMgr == nullptr) {
290 APP_LOGE("DataMgr is nullptr");
291 return false;
292 }
293 return dataMgr->QueryAbilityInfoByUri(abilityUri, Constants::UNSPECIFIED_USERID, abilityInfo);
294 }
295
QueryAbilityInfosByUri(const std::string & abilityUri,std::vector<AbilityInfo> & abilityInfos)296 bool BundleMgrHostImpl::QueryAbilityInfosByUri(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos)
297 {
298 APP_LOGD("start QueryAbilityInfosByUri, uri : %{private}s", abilityUri.c_str());
299 auto dataMgr = GetDataMgrFromService();
300 if (dataMgr == nullptr) {
301 APP_LOGE("DataMgr is nullptr");
302 return false;
303 }
304 return dataMgr->QueryAbilityInfosByUri(abilityUri, abilityInfos);
305 }
306
QueryAbilityInfoByUri(const std::string & abilityUri,int32_t userId,AbilityInfo & abilityInfo)307 bool BundleMgrHostImpl::QueryAbilityInfoByUri(
308 const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo)
309 {
310 APP_LOGD("start QueryAbilityInfoByUri, uri : %{private}s, userId : %{public}d", abilityUri.c_str(), userId);
311 auto dataMgr = GetDataMgrFromService();
312 if (dataMgr == nullptr) {
313 APP_LOGE("DataMgr is nullptr");
314 return false;
315 }
316 return dataMgr->QueryAbilityInfoByUri(abilityUri, userId, abilityInfo);
317 }
318
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)319 bool BundleMgrHostImpl::QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos)
320 {
321 auto dataMgr = GetDataMgrFromService();
322 if (dataMgr == nullptr) {
323 APP_LOGE("DataMgr is nullptr");
324 return false;
325 }
326 return dataMgr->QueryKeepAliveBundleInfos(bundleInfos);
327 }
328
GetAbilityLabel(const std::string & bundleName,const std::string & className)329 std::string BundleMgrHostImpl::GetAbilityLabel(const std::string &bundleName, const std::string &className)
330 {
331 if (!VerifyQueryPermission(bundleName)) {
332 APP_LOGE("verify permission failed");
333 return Constants::EMPTY_STRING;
334 }
335 auto dataMgr = GetDataMgrFromService();
336 if (dataMgr == nullptr) {
337 APP_LOGE("DataMgr is nullptr");
338 return Constants::EMPTY_STRING;
339 }
340 return dataMgr->GetAbilityLabel(bundleName, className);
341 }
342
GetBundleArchiveInfo(const std::string & hapFilePath,const BundleFlag flag,BundleInfo & bundleInfo)343 bool BundleMgrHostImpl::GetBundleArchiveInfo(
344 const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo)
345 {
346 return GetBundleArchiveInfo(hapFilePath, static_cast<int32_t>(flag), bundleInfo);
347 }
348
GetBundleArchiveInfo(const std::string & hapFilePath,int32_t flags,BundleInfo & bundleInfo)349 bool BundleMgrHostImpl::GetBundleArchiveInfo(
350 const std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo)
351 {
352 std::string realPath;
353 auto ret = BundleUtil::CheckFilePath(hapFilePath, realPath);
354 if (ret != ERR_OK) {
355 APP_LOGE("GetBundleArchiveInfo file path %{public}s invalid", hapFilePath.c_str());
356 return false;
357 }
358 InnerBundleInfo info;
359 BundleParser bundleParser;
360 ret = bundleParser.Parse(realPath, info);
361 if (ret != ERR_OK) {
362 APP_LOGE("parse bundle info failed, error: %{public}d", ret);
363 return false;
364 }
365 APP_LOGD("verify permission success, bgein to GetBundleArchiveInfo");
366 info.GetBundleInfo(flags, bundleInfo, Constants::NOT_EXIST_USERID);
367 return true;
368 }
369
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo)370 bool BundleMgrHostImpl::GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo)
371 {
372 if (abilityInfo.bundleName.empty() || abilityInfo.package.empty()) {
373 APP_LOGE("fail to GetHapModuleInfo due to params empty");
374 return false;
375 }
376 auto dataMgr = GetDataMgrFromService();
377 if (dataMgr == nullptr) {
378 APP_LOGE("DataMgr is nullptr");
379 return false;
380 }
381 return dataMgr->GetHapModuleInfo(abilityInfo, hapModuleInfo);
382 }
383
GetLaunchWantForBundle(const std::string & bundleName,Want & want)384 bool BundleMgrHostImpl::GetLaunchWantForBundle(const std::string &bundleName, Want &want)
385 {
386 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
387 APP_LOGE("verify permission failed");
388 return false;
389 }
390 APP_LOGD("verify permission success, bgein to GetLaunchWantForBundle");
391 auto dataMgr = GetDataMgrFromService();
392 if (dataMgr == nullptr) {
393 APP_LOGE("DataMgr is nullptr");
394 return false;
395 }
396 return dataMgr->GetLaunchWantForBundle(bundleName, want);
397 }
398
CheckPublicKeys(const std::string & firstBundleName,const std::string & secondBundleName)399 int BundleMgrHostImpl::CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName)
400 {
401 auto dataMgr = GetDataMgrFromService();
402 if (dataMgr == nullptr) {
403 APP_LOGE("DataMgr is nullptr");
404 return false;
405 }
406 return dataMgr->CheckPublicKeys(firstBundleName, secondBundleName);
407 }
408
CheckPermission(const std::string & bundleName,const std::string & permission)409 int BundleMgrHostImpl::CheckPermission(const std::string &bundleName, const std::string &permission)
410 {
411 if (bundleName.empty() || permission.empty()) {
412 APP_LOGE("fail to CheckPermission due to params empty");
413 return Constants::PERMISSION_NOT_GRANTED;
414 }
415 int32_t userId = BundleUtil::GetUserIdByCallingUid();
416 return BundlePermissionMgr::VerifyPermission(bundleName, permission, userId);
417 }
418
CheckPermissionByUid(const std::string & bundleName,const std::string & permission,const int userId)419 int BundleMgrHostImpl::CheckPermissionByUid(
420 const std::string &bundleName, const std::string &permission, const int userId)
421 {
422 if (bundleName.empty() || permission.empty()) {
423 APP_LOGE("fail to CheckPermission due to params empty");
424 return Constants::PERMISSION_NOT_GRANTED;
425 }
426 return BundlePermissionMgr::VerifyPermission(bundleName, permission, userId);
427 }
428
GetPermissionDef(const std::string & permissionName,PermissionDef & permissionDef)429 bool BundleMgrHostImpl::GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef)
430 {
431 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
432 APP_LOGE("verify GET_BUNDLE_INFO_PRIVILEGED failed");
433 return false;
434 }
435 if (permissionName.empty()) {
436 APP_LOGE("fail to GetPermissionDef due to params empty");
437 return false;
438 }
439 return BundlePermissionMgr::GetPermissionDef(permissionName, permissionDef);
440 }
441
GetAllPermissionGroupDefs(std::vector<PermissionDef> & permissionDefs)442 bool BundleMgrHostImpl::GetAllPermissionGroupDefs(std::vector<PermissionDef> &permissionDefs)
443 {
444 return true;
445 }
446
GetAppsGrantedPermissions(const std::vector<std::string> & permissions,std::vector<std::string> & appNames)447 bool BundleMgrHostImpl::GetAppsGrantedPermissions(
448 const std::vector<std::string> &permissions, std::vector<std::string> &appNames)
449 {
450 return true;
451 }
452
HasSystemCapability(const std::string & capName)453 bool BundleMgrHostImpl::HasSystemCapability(const std::string &capName)
454 {
455 return true;
456 }
457
GetSystemAvailableCapabilities(std::vector<std::string> & systemCaps)458 bool BundleMgrHostImpl::GetSystemAvailableCapabilities(std::vector<std::string> &systemCaps)
459 {
460 return true;
461 }
462
IsSafeMode()463 bool BundleMgrHostImpl::IsSafeMode()
464 {
465 return true;
466 }
467
CleanBundleCacheFiles(const std::string & bundleName,const sptr<ICleanCacheCallback> & cleanCacheCallback,int32_t userId)468 bool BundleMgrHostImpl::CleanBundleCacheFiles(
469 const std::string &bundleName, const sptr<ICleanCacheCallback> &cleanCacheCallback,
470 int32_t userId)
471 {
472 if (userId == Constants::UNSPECIFIED_USERID) {
473 userId = BundleUtil::GetUserIdByCallingUid();
474 }
475 if (bundleName.empty() || !cleanCacheCallback || (userId < 0)) {
476 APP_LOGE("the cleanCacheCallback is nullptr or bundleName empty or invalid userId");
477 return false;
478 }
479 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERNISSION_REMOVECACHEFILE)) {
480 APP_LOGE("ohos.permission.REMOVE_CACHE_FILES permission denied");
481 return false;
482 }
483 ApplicationInfo applicationInfo;
484 auto dataMgr = GetDataMgrFromService();
485 if (dataMgr == nullptr) {
486 APP_LOGE("DataMgr is nullptr");
487 return false;
488 }
489 if (!dataMgr->GetApplicationInfo(bundleName, ApplicationFlag::GET_BASIC_APPLICATION_INFO,
490 userId, applicationInfo)) {
491 APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
492 return false;
493 }
494 CleanBundleCacheTask(bundleName, cleanCacheCallback, applicationInfo.dataDir, userId);
495 return true;
496 }
497
CleanBundleCacheTask(const std::string & bundleName,const sptr<ICleanCacheCallback> & cleanCacheCallback,const std::string & applicationDataDir,int32_t userId)498 void BundleMgrHostImpl::CleanBundleCacheTask(const std::string &bundleName,
499 const sptr<ICleanCacheCallback> &cleanCacheCallback,
500 const std::string &applicationDataDir,
501 int32_t userId)
502 {
503 std::vector<std::string> rootDir;
504 rootDir.emplace_back(applicationDataDir);
505 for (const auto &el : Constants::BUNDLE_EL) {
506 std::string dataDir = Constants::BUNDLE_APP_DATA_BASE_DIR + el +
507 Constants::FILE_SEPARATOR_CHAR + std::to_string(userId) + Constants::BASE + bundleName;
508 rootDir.emplace_back(dataDir);
509 }
510 auto cleanCache = [rootDir, cleanCacheCallback]() {
511 std::vector<std::string> caches;
512 for (const auto &st : rootDir) {
513 std::vector<std::string> cache;
514 if (InstalldClient::GetInstance()->GetBundleCachePath(st, cache) != ERR_OK) {
515 APP_LOGE("GetBundleCachePath failed, path: %{public}s", st.c_str());
516 }
517 for (const auto &item : cache) {
518 caches.emplace_back(item);
519 }
520 }
521 bool error = false;
522 if (!caches.empty()) {
523 for (const auto& cache : caches) {
524 error = InstalldClient::GetInstance()->CleanBundleDataDir(cache);
525 if (error) {
526 break;
527 }
528 }
529 }
530 APP_LOGD("CleanBundleCacheFiles with error %{public}d", error);
531 cleanCacheCallback->OnCleanCacheFinished(error);
532 };
533 handler_->PostTask(cleanCache);
534 }
535
CleanBundleDataFiles(const std::string & bundleName,const int userId)536 bool BundleMgrHostImpl::CleanBundleDataFiles(const std::string &bundleName, const int userId)
537 {
538 if (bundleName.empty() || userId < 0) {
539 APP_LOGE("the bundleName empty or invalid userid");
540 return false;
541 }
542 ApplicationInfo applicationInfo;
543 if (!GetApplicationInfo(bundleName, ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, applicationInfo)) {
544 APP_LOGE("can not get application info of %{public}s", bundleName.c_str());
545 return false;
546 }
547 InnerBundleUserInfo innerBundleUserInfo;
548 if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
549 APP_LOGE("%{public}s, userId:%{public}d, GetBundleUserInfo failed", bundleName.c_str(), userId);
550 return false;
551 }
552 if (BundlePermissionMgr::ClearUserGrantedPermissionState(applicationInfo.accessTokenId)) {
553 APP_LOGE("%{public}s, ClearUserGrantedPermissionState failed", bundleName.c_str());
554 return false;
555 }
556 bool isStartUserId = false;
557 if (userId == Constants::START_USERID) {
558 isStartUserId = true;
559 if (InstalldClient::GetInstance()->RemoveDir(applicationInfo.dataDir) != ERR_OK) {
560 APP_LOGE("%{public}s, RemoveDir:%{public}s failed", bundleName.c_str(), applicationInfo.dataDir.c_str());
561 return false;
562 }
563 }
564 if (InstalldClient::GetInstance()->RemoveBundleDataDir(bundleName, userId) != ERR_OK) {
565 APP_LOGE("%{public}s, RemoveBundleDataDir failed", bundleName.c_str());
566 return false;
567 }
568 if (InstalldClient::GetInstance()->CreateBundleDataDir(applicationInfo.dataDir, userId, innerBundleUserInfo.uid,
569 innerBundleUserInfo.uid, GetAppPrivilegeLevel(bundleName), isStartUserId) != ERR_OK) {
570 APP_LOGE("%{public}s, CreateBundleDataDir failed", bundleName.c_str());
571 return false;
572 }
573 return true;
574 }
575
RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)576 bool BundleMgrHostImpl::RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
577 {
578 if ((!bundleStatusCallback) || (bundleStatusCallback->GetBundleName().empty())) {
579 APP_LOGE("the bundleStatusCallback is nullptr or bundleName empty");
580 return false;
581 }
582 // check permission
583 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
584 APP_LOGE("register bundle status callback failed due to lack of permission");
585 return false;
586 }
587
588 auto dataMgr = GetDataMgrFromService();
589 if (dataMgr == nullptr) {
590 APP_LOGE("DataMgr is nullptr");
591 return false;
592 }
593 return dataMgr->RegisterBundleStatusCallback(bundleStatusCallback);
594 }
595
RegisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)596 bool BundleMgrHostImpl::RegisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
597 {
598 APP_LOGD("begin to RegisterBundleEventCallback");
599 if (bundleEventCallback == nullptr) {
600 APP_LOGE("bundleEventCallback is null");
601 return false;
602 }
603 auto dataMgr = GetDataMgrFromService();
604 if (dataMgr == nullptr) {
605 APP_LOGE("DataMgr is nullptr");
606 return false;
607 }
608 return dataMgr->RegisterBundleEventCallback(bundleEventCallback);
609 }
610
UnregisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)611 bool BundleMgrHostImpl::UnregisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
612 {
613 APP_LOGD("begin to UnregisterBundleEventCallback");
614 if (bundleEventCallback == nullptr) {
615 APP_LOGE("bundleEventCallback is null");
616 return false;
617 }
618 auto dataMgr = GetDataMgrFromService();
619 if (dataMgr == nullptr) {
620 APP_LOGE("DataMgr is nullptr");
621 return false;
622 }
623 return dataMgr->UnregisterBundleEventCallback(bundleEventCallback);
624 }
625
ClearBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)626 bool BundleMgrHostImpl::ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
627 {
628 if (!bundleStatusCallback) {
629 APP_LOGE("the bundleStatusCallback is nullptr");
630 return false;
631 }
632
633 // check permission
634 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
635 APP_LOGE("register bundle status callback failed due to lack of permission");
636 return false;
637 }
638
639 auto dataMgr = GetDataMgrFromService();
640 if (dataMgr == nullptr) {
641 APP_LOGE("DataMgr is nullptr");
642 return false;
643 }
644 return dataMgr->ClearBundleStatusCallback(bundleStatusCallback);
645 }
646
UnregisterBundleStatusCallback()647 bool BundleMgrHostImpl::UnregisterBundleStatusCallback()
648 {
649 // check permission
650 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
651 APP_LOGE("register bundle status callback failed due to lack of permission");
652 return false;
653 }
654
655 auto dataMgr = GetDataMgrFromService();
656 if (dataMgr == nullptr) {
657 APP_LOGE("DataMgr is nullptr");
658 return false;
659 }
660 return dataMgr->UnregisterBundleStatusCallback();
661 }
662
DumpInfos(const DumpFlag flag,const std::string & bundleName,int32_t userId,std::string & result)663 bool BundleMgrHostImpl::DumpInfos(
664 const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result)
665 {
666 APP_LOGD("dump infos begin");
667 bool ret = false;
668 switch (flag) {
669 case DumpFlag::DUMP_BUNDLE_LIST: {
670 ret = DumpAllBundleInfoNames(userId, result);
671 break;
672 }
673 case DumpFlag::DUMP_ALL_BUNDLE_INFO: {
674 ret = DumpAllBundleInfos(userId, result);
675 break;
676 }
677 case DumpFlag::DUMP_BUNDLE_INFO: {
678 ret = DumpBundleInfo(bundleName, userId, result);
679 break;
680 }
681 case DumpFlag::DUMP_SHORTCUT_INFO: {
682 ret = DumpShortcutInfo(bundleName, userId, result);
683 break;
684 }
685 default:
686 APP_LOGE("dump flag error");
687 return false;
688 }
689 return ret;
690 }
691
DumpAllBundleInfoNames(int32_t userId,std::string & result)692 bool BundleMgrHostImpl::DumpAllBundleInfoNames(int32_t userId, std::string &result)
693 {
694 if (userId != Constants::ALL_USERID) {
695 return DumpAllBundleInfoNamesByUserId(userId, result);
696 }
697
698 auto userIds = GetExistsCommonUserIs();
699 for (auto userId : userIds) {
700 DumpAllBundleInfoNamesByUserId(userId, result);
701 }
702
703 APP_LOGI("get installed bundles success");
704 return true;
705 }
706
DumpAllBundleInfoNamesByUserId(int32_t userId,std::string & result)707 bool BundleMgrHostImpl::DumpAllBundleInfoNamesByUserId(int32_t userId, std::string &result)
708 {
709 auto dataMgr = GetDataMgrFromService();
710 if (dataMgr == nullptr) {
711 APP_LOGE("DataMgr is nullptr");
712 return false;
713 }
714
715 std::vector<std::string> bundleNames;
716 if (!dataMgr->GetBundleList(bundleNames, userId)) {
717 APP_LOGE("get bundle list failed by userId(%{public}d)", userId);
718 return false;
719 }
720
721 result.append("ID: ");
722 result.append(std::to_string(userId));
723 result.append(":\n");
724 for (const auto &name : bundleNames) {
725 result.append("\t");
726 result.append(name);
727 result.append("\n");
728 }
729 return true;
730 }
731
DumpAllBundleInfos(int32_t userId,std::string & result)732 bool BundleMgrHostImpl::DumpAllBundleInfos(int32_t userId, std::string &result)
733 {
734 std::vector<BundleInfo> bundleInfos;
735 if (!GetBundleInfos(BundleFlag::GET_BUNDLE_WITH_ABILITIES | BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO,
736 bundleInfos, userId)) {
737 APP_LOGE("get bundleInfos failed.");
738 return false;
739 }
740
741 for (const auto &info : bundleInfos) {
742 std::vector<InnerBundleUserInfo> innerBundleUserInfos;
743 if (userId == Constants::ALL_USERID) {
744 if (!GetBundleUserInfos(info.name, innerBundleUserInfos)) {
745 APP_LOGE("get all userInfos in bundle(%{public}s) failed", info.name.c_str());
746 return false;
747 }
748 } else {
749 InnerBundleUserInfo innerBundleUserInfo;
750 if (!GetBundleUserInfo(info.name, userId, innerBundleUserInfo)) {
751 APP_LOGI("get all userInfo in bundle(%{public}s) failed", info.name.c_str());
752 }
753 innerBundleUserInfos.emplace_back(innerBundleUserInfo);
754 }
755
756 result.append(info.name);
757 result.append(":\n");
758 nlohmann::json jsonObject = info;
759 jsonObject["hapModuleInfos"] = info.hapModuleInfos;
760 jsonObject["userInfo"] = innerBundleUserInfos;
761 result.append(jsonObject.dump(Constants::DUMP_INDENT));
762 result.append("\n");
763 }
764 APP_LOGI("get all bundle info success");
765 return true;
766 }
767
DumpBundleInfo(const std::string & bundleName,int32_t userId,std::string & result)768 bool BundleMgrHostImpl::DumpBundleInfo(
769 const std::string &bundleName, int32_t userId, std::string &result)
770 {
771 APP_LOGD("dump bundle info begin");
772 std::vector<InnerBundleUserInfo> innerBundleUserInfos;
773 if (userId == Constants::ALL_USERID) {
774 if (!GetBundleUserInfos(bundleName, innerBundleUserInfos)) {
775 APP_LOGE("get all userInfos in bundle(%{public}s) failed", bundleName.c_str());
776 return false;
777 }
778 userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
779 } else {
780 InnerBundleUserInfo innerBundleUserInfo;
781 if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
782 APP_LOGI("get userInfo in bundle(%{public}s) failed", bundleName.c_str());
783 }
784 innerBundleUserInfos.emplace_back(innerBundleUserInfo);
785 }
786
787 BundleInfo bundleInfo;
788 if (!GetBundleInfo(bundleName,
789 BundleFlag::GET_BUNDLE_WITH_ABILITIES | BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION |
790 BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId)) {
791 APP_LOGE("get bundleInfo(%{public}s) failed", bundleName.c_str());
792 return false;
793 }
794
795 result.append(bundleName);
796 result.append(":\n");
797 nlohmann::json jsonObject = bundleInfo;
798 jsonObject["hapModuleInfos"] = bundleInfo.hapModuleInfos;
799 jsonObject["userInfo"] = innerBundleUserInfos;
800 result.append(jsonObject.dump(Constants::DUMP_INDENT));
801 result.append("\n");
802 APP_LOGI("get %{public}s bundle info success", bundleName.c_str());
803 return true;
804 }
805
DumpShortcutInfo(const std::string & bundleName,int32_t userId,std::string & result)806 bool BundleMgrHostImpl::DumpShortcutInfo(
807 const std::string &bundleName, int32_t userId, std::string &result)
808 {
809 std::vector<ShortcutInfo> shortcutInfos;
810 if (userId == Constants::ALL_USERID) {
811 std::vector<InnerBundleUserInfo> innerBundleUserInfos;
812 if (!GetBundleUserInfos(bundleName, innerBundleUserInfos)) {
813 APP_LOGE("get all userInfos in bundle(%{public}s) failed", bundleName.c_str());
814 return false;
815 }
816 userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
817 }
818
819 if (!GetShortcutInfos(bundleName, userId, shortcutInfos)) {
820 APP_LOGE("get all shortcut info by bundle(%{public}s) failed", bundleName.c_str());
821 return false;
822 }
823
824 result.append("shortcuts");
825 result.append(":\n");
826 for (const auto &info : shortcutInfos) {
827 result.append("\"shortcut\"");
828 result.append(":\n");
829 nlohmann::json jsonObject = info;
830 result.append(jsonObject.dump(Constants::DUMP_INDENT));
831 result.append("\n");
832 }
833 APP_LOGI("get %{public}s shortcut info success", bundleName.c_str());
834 return true;
835 }
836
IsApplicationEnabled(const std::string & bundleName)837 bool BundleMgrHostImpl::IsApplicationEnabled(const std::string &bundleName)
838 {
839 auto dataMgr = GetDataMgrFromService();
840 if (dataMgr == nullptr) {
841 APP_LOGE("DataMgr is nullptr");
842 return false;
843 }
844 return dataMgr->IsApplicationEnabled(bundleName);
845 }
846
SetApplicationEnabled(const std::string & bundleName,bool isEnable,int32_t userId)847 bool BundleMgrHostImpl::SetApplicationEnabled(const std::string &bundleName, bool isEnable, int32_t userId)
848 {
849 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
850 APP_LOGE("verify permission failed");
851 return false;
852 }
853 APP_LOGD("verify permission success, bgein to SetApplicationEnabled");
854 auto dataMgr = GetDataMgrFromService();
855 if (dataMgr == nullptr) {
856 APP_LOGE("DataMgr is nullptr");
857 return false;
858 }
859
860 if (!dataMgr->SetApplicationEnabled(bundleName, isEnable, userId)) {
861 APP_LOGE("Set application(%{public}s) enabled value faile.", bundleName.c_str());
862 return false;
863 }
864
865 InnerBundleUserInfo innerBundleUserInfo;
866 if (!GetBundleUserInfo(bundleName, userId, innerBundleUserInfo)) {
867 APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", bundleName.c_str());
868 return false;
869 }
870
871 dataMgr->NotifyBundleStatus(bundleName,
872 Constants::EMPTY_STRING,
873 Constants::EMPTY_STRING,
874 ERR_OK,
875 NotifyType::APPLICATION_ENABLE,
876 innerBundleUserInfo.uid);
877 return true;
878 }
879
IsAbilityEnabled(const AbilityInfo & abilityInfo)880 bool BundleMgrHostImpl::IsAbilityEnabled(const AbilityInfo &abilityInfo)
881 {
882 auto dataMgr = GetDataMgrFromService();
883 if (dataMgr == nullptr) {
884 APP_LOGE("DataMgr is nullptr");
885 return false;
886 }
887 return dataMgr->IsAbilityEnabled(abilityInfo);
888 }
889
SetAbilityEnabled(const AbilityInfo & abilityInfo,bool isEnabled,int32_t userId)890 bool BundleMgrHostImpl::SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEnabled, int32_t userId)
891 {
892 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE)) {
893 APP_LOGE("verify permission failed");
894 return false;
895 }
896 APP_LOGD("verify permission success, bgein to SetAbilityEnabled");
897 auto dataMgr = GetDataMgrFromService();
898 if (dataMgr == nullptr) {
899 APP_LOGE("DataMgr is nullptr");
900 return false;
901 }
902
903 if (!dataMgr->SetAbilityEnabled(abilityInfo, isEnabled, userId)) {
904 APP_LOGE("Set ability(%{public}s) enabled value faile.", abilityInfo.bundleName.c_str());
905 return false;
906 }
907
908 InnerBundleUserInfo innerBundleUserInfo;
909 if (!GetBundleUserInfo(abilityInfo.bundleName, userId, innerBundleUserInfo)) {
910 APP_LOGE("Get calling userInfo in bundle(%{public}s) failed", abilityInfo.bundleName.c_str());
911 return false;
912 }
913
914 dataMgr->NotifyBundleStatus(abilityInfo.bundleName,
915 Constants::EMPTY_STRING,
916 abilityInfo.name,
917 ERR_OK,
918 NotifyType::APPLICATION_ENABLE,
919 innerBundleUserInfo.uid);
920 return true;
921 }
922
GetAbilityIcon(const std::string & bundleName,const std::string & className)923 std::string BundleMgrHostImpl::GetAbilityIcon(const std::string &bundleName, const std::string &className)
924 {
925 auto dataMgr = GetDataMgrFromService();
926 if (dataMgr == nullptr) {
927 APP_LOGE("DataMgr is nullptr");
928 return Constants::EMPTY_STRING;
929 }
930 return dataMgr->GetAbilityIcon(bundleName, className);
931 }
932
933 #ifdef SUPPORT_GRAPHICS
GetAbilityPixelMapIcon(const std::string & bundleName,const std::string & abilityName)934 std::shared_ptr<Media::PixelMap> BundleMgrHostImpl::GetAbilityPixelMapIcon(const std::string &bundleName,
935 const std::string &abilityName)
936 {
937 if (!VerifyQueryPermission(bundleName)) {
938 APP_LOGE("verify permission failed");
939 return nullptr;
940 }
941 auto dataMgr = GetDataMgrFromService();
942 if (dataMgr == nullptr) {
943 APP_LOGE("DataMgr is nullptr");
944 return nullptr;
945 }
946 return dataMgr->GetAbilityPixelMapIcon(bundleName, abilityName);
947 }
948 #endif
949
GetBundleInstaller()950 sptr<IBundleInstaller> BundleMgrHostImpl::GetBundleInstaller()
951 {
952 return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
953 }
954
GetBundleUserMgr()955 sptr<IBundleUserMgr> BundleMgrHostImpl::GetBundleUserMgr()
956 {
957 return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleUserMgr();
958 }
959
CanRequestPermission(const std::string & bundleName,const std::string & permissionName,const int userId)960 bool BundleMgrHostImpl::CanRequestPermission(
961 const std::string &bundleName, const std::string &permissionName, const int userId)
962 {
963 return true;
964 }
965
RequestPermissionFromUser(const std::string & bundleName,const std::string & permissionName,const int userId)966 bool BundleMgrHostImpl::RequestPermissionFromUser(
967 const std::string &bundleName, const std::string &permissionName, const int userId)
968 {
969 return true;
970 }
971
RegisterAllPermissionsChanged(const sptr<OnPermissionChangedCallback> & callback)972 bool BundleMgrHostImpl::RegisterAllPermissionsChanged(const sptr<OnPermissionChangedCallback> &callback)
973 {
974 auto dataMgr = GetDataMgrFromService();
975 if (dataMgr == nullptr) {
976 APP_LOGE("DataMgr is nullptr");
977 return false;
978 }
979 return dataMgr->RegisterAllPermissionsChanged(callback);
980 }
981
RegisterPermissionsChanged(const std::vector<int> & uids,const sptr<OnPermissionChangedCallback> & callback)982 bool BundleMgrHostImpl::RegisterPermissionsChanged(
983 const std::vector<int> &uids, const sptr<OnPermissionChangedCallback> &callback)
984 {
985 auto dataMgr = GetDataMgrFromService();
986 if (dataMgr == nullptr) {
987 APP_LOGE("DataMgr is nullptr");
988 return false;
989 }
990 return dataMgr->RegisterPermissionsChanged(uids, callback);
991 }
992
UnregisterPermissionsChanged(const sptr<OnPermissionChangedCallback> & callback)993 bool BundleMgrHostImpl::UnregisterPermissionsChanged(const sptr<OnPermissionChangedCallback> &callback)
994 {
995 auto dataMgr = GetDataMgrFromService();
996 if (dataMgr == nullptr) {
997 APP_LOGE("DataMgr is nullptr");
998 return false;
999 }
1000 return dataMgr->UnregisterPermissionsChanged(callback);
1001 }
1002
GetAllFormsInfo(std::vector<FormInfo> & formInfos)1003 bool BundleMgrHostImpl::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
1004 {
1005 auto dataMgr = GetDataMgrFromService();
1006 if (dataMgr == nullptr) {
1007 APP_LOGE("DataMgr is nullptr");
1008 return false;
1009 }
1010 return dataMgr->GetAllFormsInfo(formInfos);
1011 }
1012
GetFormsInfoByApp(const std::string & bundleName,std::vector<FormInfo> & formInfos)1013 bool BundleMgrHostImpl::GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos)
1014 {
1015 auto dataMgr = GetDataMgrFromService();
1016 if (dataMgr == nullptr) {
1017 APP_LOGE("DataMgr is nullptr");
1018 return false;
1019 }
1020 return dataMgr->GetFormsInfoByApp(bundleName, formInfos);
1021 }
1022
GetFormsInfoByModule(const std::string & bundleName,const std::string & moduleName,std::vector<FormInfo> & formInfos)1023 bool BundleMgrHostImpl::GetFormsInfoByModule(
1024 const std::string &bundleName, const std::string &moduleName, std::vector<FormInfo> &formInfos)
1025 {
1026 auto dataMgr = GetDataMgrFromService();
1027 if (dataMgr == nullptr) {
1028 APP_LOGE("DataMgr is nullptr");
1029 return false;
1030 }
1031 return dataMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos);
1032 }
1033
GetShortcutInfos(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)1034 bool BundleMgrHostImpl::GetShortcutInfos(
1035 const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
1036 {
1037 return GetShortcutInfos(bundleName, Constants::UNSPECIFIED_USERID, shortcutInfos);
1038 }
1039
GetShortcutInfos(const std::string & bundleName,int32_t userId,std::vector<ShortcutInfo> & shortcutInfos)1040 bool BundleMgrHostImpl::GetShortcutInfos(
1041 const std::string &bundleName, int32_t userId, std::vector<ShortcutInfo> &shortcutInfos)
1042 {
1043 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1044 APP_LOGE("verify permission failed");
1045 return false;
1046 }
1047 APP_LOGD("verify permission success, bgein to GetShortcutInfos");
1048 auto dataMgr = GetDataMgrFromService();
1049 if (dataMgr == nullptr) {
1050 APP_LOGE("DataMgr is nullptr");
1051 return false;
1052 }
1053 return dataMgr->GetShortcutInfos(bundleName, userId, shortcutInfos);
1054 }
1055
GetAllCommonEventInfo(const std::string & eventKey,std::vector<CommonEventInfo> & commonEventInfos)1056 bool BundleMgrHostImpl::GetAllCommonEventInfo(const std::string &eventKey,
1057 std::vector<CommonEventInfo> &commonEventInfos)
1058 {
1059 auto dataMgr = GetDataMgrFromService();
1060 if (dataMgr == nullptr) {
1061 APP_LOGE("DataMgr is nullptr");
1062 return false;
1063 }
1064 return dataMgr->GetAllCommonEventInfo(eventKey, commonEventInfos);
1065 }
1066
GetModuleUsageRecords(const int32_t number,std::vector<ModuleUsageRecord> & moduleUsageRecords)1067 bool BundleMgrHostImpl::GetModuleUsageRecords(const int32_t number, std::vector<ModuleUsageRecord> &moduleUsageRecords)
1068 {
1069 auto dataMgr = GetDataMgrFromService();
1070 if (dataMgr == nullptr) {
1071 APP_LOGE("DataMgr is nullptr");
1072 return false;
1073 }
1074 return dataMgr->GetUsageRecords(number, moduleUsageRecords);
1075 }
1076
NotifyAbilityLifeStatus(const std::string & bundleName,const std::string & abilityName,const int64_t launchTime,const int uid)1077 bool BundleMgrHostImpl::NotifyAbilityLifeStatus(
1078 const std::string &bundleName, const std::string &abilityName, const int64_t launchTime, const int uid)
1079 {
1080 APP_LOGI("NotifyAbilityLifeStatus begin");
1081 auto task = [this, bundleName, abilityName, launchTime, uid] {
1082 auto dataMgr = GetDataMgrFromService();
1083 dataMgr->NotifyAbilityLifeStatus(bundleName, abilityName, launchTime, uid);
1084 };
1085 handler_->PostTask(task);
1086 APP_LOGI("NotifyAbilityLifeStatus end");
1087 return true;
1088 }
1089
RemoveClonedBundle(const std::string & bundleName,const int32_t uid)1090 bool BundleMgrHostImpl::RemoveClonedBundle(const std::string &bundleName, const int32_t uid)
1091 {
1092 APP_LOGI("RemoveClonedBundle begin");
1093 if (bundleName.empty()) {
1094 APP_LOGI("remove cloned bundle failed");
1095 return false;
1096 }
1097 auto cloneMgr = GetCloneMgrFromService();
1098 if (cloneMgr == nullptr) {
1099 APP_LOGE("cloneMgr is nullptr");
1100 return false;
1101 }
1102 std::string struid = std::to_string(uid);
1103 std::string newName = bundleName + "#" + struid;
1104 return cloneMgr->RemoveClonedBundle(bundleName, newName);
1105 }
1106
BundleClone(const std::string & bundleName)1107 bool BundleMgrHostImpl::BundleClone(const std::string &bundleName)
1108 {
1109 APP_LOGI("bundle clone begin");
1110 if (bundleName.empty()) {
1111 APP_LOGI("bundle clone failed");
1112 return false;
1113 }
1114 auto cloneMgr = GetCloneMgrFromService();
1115 if (cloneMgr == nullptr) {
1116 APP_LOGE("cloneMgr is nullptr");
1117 return false;
1118 }
1119 auto result = cloneMgr->BundleClone(bundleName);
1120 return result;
1121 }
1122
CheckBundleNameInAllowList(const std::string & bundleName)1123 bool BundleMgrHostImpl::CheckBundleNameInAllowList(const std::string &bundleName)
1124 {
1125 APP_LOGI("Check BundleName In AllowList begin");
1126 if (bundleName.empty()) {
1127 APP_LOGI("Check BundleName In AllowList failed");
1128 return false;
1129 }
1130 auto cloneMgr = GetCloneMgrFromService();
1131 if (cloneMgr == nullptr) {
1132 APP_LOGE("cloneMgr is nullptr");
1133 return false;
1134 }
1135 auto result = cloneMgr->CheckBundleNameInAllowList(bundleName);
1136 return result;
1137 }
1138
GetDistributedBundleInfo(const std::string & networkId,int32_t userId,const std::string & bundleName,DistributedBundleInfo & distributedBundleInfo)1139 bool BundleMgrHostImpl::GetDistributedBundleInfo(
1140 const std::string &networkId, int32_t userId, const std::string &bundleName,
1141 DistributedBundleInfo &distributedBundleInfo)
1142 {
1143 auto dataMgr = GetDataMgrFromService();
1144 if (dataMgr == nullptr) {
1145 APP_LOGE("DataMgr is nullptr");
1146 return false;
1147 }
1148 return dataMgr->GetDistributedBundleInfo(networkId, userId, bundleName, distributedBundleInfo);
1149 }
1150
QueryExtensionAbilityInfos(const Want & want,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1151 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const Want &want, const int32_t &flag, const int32_t &userId,
1152 std::vector<ExtensionAbilityInfo> &extensionInfos)
1153 {
1154 APP_LOGD("QueryExtensionAbilityInfos without type begin");
1155 if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
1156 APP_LOGE("verify permission failed");
1157 return false;
1158 }
1159 APP_LOGD("want uri is %{private}s", want.GetUriString().c_str());
1160 auto dataMgr = GetDataMgrFromService();
1161 if (dataMgr == nullptr) {
1162 APP_LOGE("DataMgr is nullptr");
1163 return false;
1164 }
1165 bool ret = dataMgr->QueryExtensionAbilityInfos(want, flag, userId, extensionInfos);
1166 if (!ret) {
1167 APP_LOGE("QueryExtensionAbilityInfos is failed");
1168 return false;
1169 }
1170 if (extensionInfos.empty()) {
1171 APP_LOGE("no valid extension info can be inquired");
1172 return false;
1173 }
1174 return true;
1175 }
1176
QueryExtensionAbilityInfos(const Want & want,const ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1177 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const Want &want, const ExtensionAbilityType &extensionType,
1178 const int32_t &flag, const int32_t &userId, std::vector<ExtensionAbilityInfo> &extensionInfos)
1179 {
1180 APP_LOGD("QueryExtensionAbilityInfos begin");
1181 if (!VerifyQueryPermission(want.GetElement().GetBundleName())) {
1182 APP_LOGE("verify permission failed");
1183 return false;
1184 }
1185 auto dataMgr = GetDataMgrFromService();
1186 if (dataMgr == nullptr) {
1187 APP_LOGE("DataMgr is nullptr");
1188 return false;
1189 }
1190 std::vector<ExtensionAbilityInfo> infos;
1191 bool ret = dataMgr->QueryExtensionAbilityInfos(want, flag, userId, infos);
1192 if (!ret) {
1193 APP_LOGE("QueryExtensionAbilityInfos is failed");
1194 return false;
1195 }
1196 for_each(infos.begin(), infos.end(), [&extensionType, &extensionInfos](const auto &info)->decltype(auto) {
1197 APP_LOGD("QueryExtensionAbilityInfos extensionType is %{public}d, info.type is %{public}d",
1198 static_cast<int32_t>(extensionType), static_cast<int32_t>(info.type));
1199 if (extensionType == info.type) {
1200 extensionInfos.emplace_back(info);
1201 }
1202 });
1203 if (extensionInfos.empty()) {
1204 APP_LOGE("no valid extension info can be inquired");
1205 return false;
1206 }
1207 return true;
1208 }
1209
QueryExtensionAbilityInfos(const ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos)1210 bool BundleMgrHostImpl::QueryExtensionAbilityInfos(const ExtensionAbilityType &extensionType, const int32_t &userId,
1211 std::vector<ExtensionAbilityInfo> &extensionInfos)
1212 {
1213 APP_LOGD("QueryExtensionAbilityInfos with type begin");
1214 auto dataMgr = GetDataMgrFromService();
1215 if (dataMgr == nullptr) {
1216 APP_LOGE("DataMgr is nullptr");
1217 return false;
1218 }
1219 bool ret = dataMgr->QueryExtensionAbilityInfos(extensionType, userId, extensionInfos);
1220 if (!ret) {
1221 APP_LOGE("QueryExtensionAbilityInfos is failed");
1222 return false;
1223 }
1224
1225 if (extensionInfos.empty()) {
1226 APP_LOGE("no valid extension info can be inquired");
1227 return false;
1228 }
1229 return true;
1230 }
1231
GetCloneMgrFromService()1232 const std::shared_ptr<BundleCloneMgr> BundleMgrHostImpl::GetCloneMgrFromService()
1233 {
1234 return DelayedSingleton<BundleMgrService>::GetInstance()->GetCloneMgr();
1235 }
1236
GetDataMgrFromService()1237 const std::shared_ptr<BundleDataMgr> BundleMgrHostImpl::GetDataMgrFromService()
1238 {
1239 return DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
1240 }
1241
GetExistsCommonUserIs()1242 std::set<int32_t> BundleMgrHostImpl::GetExistsCommonUserIs()
1243 {
1244 std::set<int32_t> userIds;
1245 auto dataMgr = GetDataMgrFromService();
1246 if (dataMgr == nullptr) {
1247 APP_LOGE("Get dataMgr shared_ptr nullptr");
1248 return userIds;
1249 }
1250
1251 for (auto userId : dataMgr->GetAllUser()) {
1252 if (userId >= Constants::START_USERID) {
1253 userIds.insert(userId);
1254 }
1255 }
1256 return userIds;
1257 }
1258
VerifyQueryPermission(const std::string & queryBundleName)1259 bool BundleMgrHostImpl::VerifyQueryPermission(const std::string &queryBundleName)
1260 {
1261 std::string callingBundleName;
1262 bool ret = GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
1263 if (ret && (queryBundleName == callingBundleName)) {
1264 APP_LOGD("query own info, verify success");
1265 return true;
1266 }
1267 if (BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
1268 APP_LOGD("verify GET_BUNDLE_INFO_PRIVILEGED success");
1269 return true;
1270 }
1271 // adapt HO
1272 ApplicationInfo applicationInfo;
1273 auto dataMgr = GetDataMgrFromService();
1274 if (dataMgr == nullptr) {
1275 APP_LOGE("get DataMgr failed");
1276 return false;
1277 }
1278 bool retVal = dataMgr->GetApplicationInfo(
1279 callingBundleName, GET_BASIC_APPLICATION_INFO, Constants::UNSPECIFIED_USERID, applicationInfo);
1280 if (!retVal) {
1281 APP_LOGE("GetApplicationInfo failed");
1282 return false;
1283 }
1284 if (applicationInfo.apiCompatibleVersion <= Constants::PERMISSION_COMPATIBLE_API_VERSION) {
1285 APP_LOGD("begin to verify GET_BUNDLE_INFO");
1286 if (BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO)) {
1287 APP_LOGD("verify GET_BUNDLE_INFO success");
1288 return true;
1289 }
1290 }
1291 APP_LOGE("verify query permission failed");
1292 return false;
1293 }
1294
GetAppPrivilegeLevel(const std::string & bundleName,int32_t userId)1295 std::string BundleMgrHostImpl::GetAppPrivilegeLevel(const std::string &bundleName, int32_t userId)
1296 {
1297 auto dataMgr = GetDataMgrFromService();
1298 if (dataMgr == nullptr) {
1299 APP_LOGE("DataMgr is nullptr");
1300 return Constants::EMPTY_STRING;
1301 }
1302 return dataMgr->GetAppPrivilegeLevel(bundleName, userId);
1303 }
1304
VerifyCallingPermission(const std::string & permission)1305 bool BundleMgrHostImpl::VerifyCallingPermission(const std::string &permission)
1306 {
1307 APP_LOGD("VerifyCallingPermission begin");
1308 return BundlePermissionMgr::VerifyCallingPermission(permission);
1309 }
1310
GetAccessibleAppCodePaths(int32_t userId)1311 std::vector<std::string> BundleMgrHostImpl::GetAccessibleAppCodePaths(int32_t userId)
1312 {
1313 APP_LOGD("GetAccessibleAppCodePaths begin");
1314 auto dataMgr = GetDataMgrFromService();
1315 if (dataMgr == nullptr) {
1316 APP_LOGE("DataMgr is nullptr");
1317 std::vector<std::string> vec;
1318 return vec;
1319 }
1320
1321 return dataMgr->GetAccessibleAppCodePaths(userId);
1322 }
1323
QueryExtensionAbilityInfoByUri(const std::string & uri,int32_t userId,ExtensionAbilityInfo & extensionAbilityInfo)1324 bool BundleMgrHostImpl::QueryExtensionAbilityInfoByUri(const std::string &uri, int32_t userId,
1325 ExtensionAbilityInfo &extensionAbilityInfo)
1326 {
1327 APP_LOGD("uri : %{private}s, userId : %{public}d", uri.c_str(), userId);
1328 auto dataMgr = GetDataMgrFromService();
1329 if (dataMgr == nullptr) {
1330 APP_LOGE("DataMgr is nullptr");
1331 return false;
1332 }
1333 return dataMgr->QueryExtensionAbilityInfoByUri(uri, userId, extensionAbilityInfo);
1334 }
1335
GetAppIdByBundleName(const std::string & bundleName,const int userId)1336 std::string BundleMgrHostImpl::GetAppIdByBundleName(const std::string &bundleName, const int userId)
1337 {
1338 APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
1339 auto dataMgr = GetDataMgrFromService();
1340 if (dataMgr == nullptr) {
1341 APP_LOGE("DataMgr is nullptr");
1342 return Constants::EMPTY_STRING;
1343 }
1344 BundleInfo bundleInfo;
1345 bool ret = dataMgr->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId);
1346 if (!ret) {
1347 APP_LOGE("get bundleInfo failed");
1348 return Constants::EMPTY_STRING;
1349 }
1350 APP_LOGD("appId is %{private}s", bundleInfo.appId.c_str());
1351 return bundleInfo.appId;
1352 }
1353
GetAppType(const std::string & bundleName)1354 std::string BundleMgrHostImpl::GetAppType(const std::string &bundleName)
1355 {
1356 APP_LOGD("bundleName : %{public}s", bundleName.c_str());
1357 auto dataMgr = GetDataMgrFromService();
1358 if (dataMgr == nullptr) {
1359 APP_LOGE("DataMgr is nullptr");
1360 return Constants::EMPTY_STRING;
1361 }
1362 BundleInfo bundleInfo;
1363 bool ret = dataMgr->GetBundleInfo(bundleName, GET_BUNDLE_DEFAULT, bundleInfo, Constants::UNSPECIFIED_USERID);
1364 if (!ret) {
1365 APP_LOGE("get bundleInfo failed");
1366 return Constants::EMPTY_STRING;
1367 }
1368 bool isSystemApp = bundleInfo.applicationInfo.isSystemApp;
1369 std::string appType = isSystemApp ? Constants::SYSTEM_APP : Constants::THIRD_PARTY_APP;
1370 APP_LOGD("appType is %{public}s", appType.c_str());
1371 return appType;
1372 }
1373
GetUidByBundleName(const std::string & bundleName,const int userId)1374 int BundleMgrHostImpl::GetUidByBundleName(const std::string &bundleName, const int userId)
1375 {
1376 APP_LOGD("bundleName : %{public}s, userId : %{public}d", bundleName.c_str(), userId);
1377 auto dataMgr = GetDataMgrFromService();
1378 if (dataMgr == nullptr) {
1379 APP_LOGE("DataMgr is nullptr");
1380 return Constants::INVALID_UID;
1381 }
1382 std::vector<BundleInfo> bundleInfos;
1383 int32_t uid = Constants::INVALID_UID;
1384 bool ret = dataMgr->GetBundleInfos(GET_BUNDLE_DEFAULT, bundleInfos, userId);
1385 if (ret) {
1386 for (auto bundleInfo : bundleInfos) {
1387 if (userId == Constants::C_UESRID) {
1388 if (bundleInfo.name == bundleName && bundleInfo.applicationInfo.isCloned == true) {
1389 uid = bundleInfo.uid;
1390 break;
1391 }
1392 } else {
1393 if (bundleInfo.name == bundleName && bundleInfo.applicationInfo.isCloned == false) {
1394 uid = bundleInfo.uid;
1395 break;
1396 }
1397 }
1398 }
1399 APP_LOGD("get bundle uid success");
1400 } else {
1401 APP_LOGE("can not get bundleInfo's uid");
1402 }
1403 APP_LOGD("uid is %{public}d", uid);
1404 return uid;
1405 }
1406
GetAbilityInfo(const std::string & bundleName,const std::string & abilityName,AbilityInfo & abilityInfo)1407 bool BundleMgrHostImpl::GetAbilityInfo(
1408 const std::string &bundleName, const std::string &abilityName, AbilityInfo &abilityInfo)
1409 {
1410 ElementName elementName(Constants::CURRENT_DEVICE_ID, bundleName, abilityName);
1411 Want want;
1412 want.SetElement(elementName);
1413 return QueryAbilityInfo(want, abilityInfo);
1414 }
1415
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)1416 bool BundleMgrHostImpl::ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId,
1417 AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo)
1418 {
1419 APP_LOGD("start ImplicitQueryInfoByPriority, flags : %{public}d, userId : %{public}d", flags, userId);
1420 auto dataMgr = GetDataMgrFromService();
1421 if (dataMgr == nullptr) {
1422 APP_LOGE("DataMgr is nullptr");
1423 return false;
1424 }
1425 return dataMgr->ImplicitQueryInfoByPriority(want, flags, userId, abilityInfo, extensionInfo);
1426 }
1427 } // namespace AppExecFwk
1428 } // namespace OHOS
1429