diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 680a6eac8f5b6cebb92a9b18ab791e5f8a12d4ca..046205c4d8cc91cc113f1f4bf2576420f5bfc1fa 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2346,6 +2346,9 @@ private: void PauseOldMissionListManager(int32_t userId); void PauseOldConnectManager(int32_t userId); bool IsSystemUI(const std::string &bundleName) const; + int32_t GetUidByCloneBundleInfo(std::string &bundleName, int32_t callerUid, int32_t appIndex, int32_t userId) const; + sptr GetWantSenderByUserId(const WantSenderInfo &wantSenderInfo, + const sptr &callerToken, int32_t uid, int32_t callerUid, int32_t callerUserId); bool VerificationAllToken(const sptr &token); std::shared_ptr GetCurrentDataAbilityManager(); diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index ec88f82bdb6d3de8a0ae977c4abe71dcf5337fee..f6a53c166b589c8c25a40cda51b13c5370a2ebcd 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -5836,6 +5836,75 @@ int AbilityManagerService::UnRegisterMissionListener(const std::string &deviceId return dmsClient.UnRegisterMissionListener(Str8ToStr16(deviceId), listener->AsObject()); } +int32_t AbilityManagerService::GetUidByCloneBundleInfo( + std::string &bundleName, int32_t callerUid, int32_t appIndex, int32_t userId) const +{ + auto bms = AbilityUtil::GetBundleManagerHelper(); + CHECK_POINTER_AND_RETURN(bms, -1); + AppExecFwk::BundleInfo bundleInfo; + MultiAppUtils::GetRunningMultiAppIndex(bundleName, callerUid, appIndex); + if (IN_PROCESS_CALL(bms->GetCloneBundleInfo( + bundleName, static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION), + appIndex, bundleInfo, userId)) != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "failed get bundle info for %{public}s", bundleName.c_str()); + return -1; + } + return bundleInfo.uid; +} + +sptr AbilityManagerService::GetWantSenderByUserId(const WantSenderInfo &wantSenderInfo, + const sptr &callerToken, int32_t uid, int32_t callerUid, int32_t callerUserId) +{ + bool isSpecifyUserId = wantSenderInfo.userId >= 0; + int32_t userId = -1, appUid = -1, appIndex = 0; + std::string bundleName = ""; + if (!wantSenderInfo.allWants.empty()) { + bundleName = wantSenderInfo.allWants.back().want.GetElement().GetBundleName(); + } + bool isSACall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); + bool isSystemApp = AAFwk::PermissionVerification::GetInstance()->IsSystemAppCall(); + if (isSpecifyUserId && (isSACall || callerUserId == U0_USER_ID)) { + userId = wantSenderInfo.userId; + if (uid >= 0) { + appUid = uid; + } else { + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + } else if (!isSpecifyUserId && (isSACall || callerUserId == U0_USER_ID)) { + if (uid >= 0) { + if (DelayedSingleton::GetInstance()-> + GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); + return nullptr; + } + appUid = uid; + } else { + userId = callerUserId; + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + } else if (!isSystemApp) { + userId = callerUserId; + appUid = callerUid; + } else if (isSystemApp) { + if (isSpecifyUserId) { + userId = wantSenderInfo.userId; + } else { + userId = callerUserId; + } + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + std::shared_ptr pendingWantManager = GetPendingWantManagerByUserId(userId); + CHECK_POINTER_AND_RETURN(pendingWantManager, nullptr); + if (!CheckSenderWantInfo(appUid, wantSenderInfo)) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "check bundleName failed"); + return nullptr; + } + const_cast(wantSenderInfo).userId = userId; + TAG_LOGI(AAFwkTag::ABILITYMGR, "bundleName: %{public}s, appIndex: %{public}d, isSystemApp: %{public}d, " + "userId: %{public}d", bundleName.c_str(), appIndex, isSystemApp, userId); + return pendingWantManager->GetWantSender(callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex); +} + sptr AbilityManagerService::GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken, int32_t uid) diff --git a/test/unittest/ability_manager_service_eleven_test/BUILD.gn b/test/unittest/ability_manager_service_eleven_test/BUILD.gn index 81b99d53d7c242e28383540edc9056273a843ecc..18f8c549d46718feb3ddbc502b5a1a8dd7044687 100644 --- a/test/unittest/ability_manager_service_eleven_test/BUILD.gn +++ b/test/unittest/ability_manager_service_eleven_test/BUILD.gn @@ -35,6 +35,7 @@ ohos_unittest("ability_manager_service_eleven_test") { "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit/include", "${ability_runtime_test_path}/unittest/app_debug_listener_stub_test", + "${ability_runtime_test_path}/unittest/multi_app_utils_test/include", "${ability_runtime_services_path}/abilitymgr/include/mission", "${ability_runtime_services_path}/abilitymgr/include/scene_board", "mock/include", @@ -48,6 +49,7 @@ ohos_unittest("ability_manager_service_eleven_test") { "${ability_runtime_services_path}/abilitymgr/src/utils/timeout_state_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/window_options_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/request_id_util.cpp", + "${ability_runtime_test_path}/unittest/multi_app_utils_test/src/mock_app_mgr_service.cpp", "ability_manager_service_eleven_test.cpp", "mock/src/mock_my_flag.cpp", "mock/src/mock_permission_verification.cpp", diff --git a/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp b/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp index 8a91e68bbd284bb2fce1b3c930af629383a1c4e0..2c32fba9663f9054e1154365a44860c76c2bd357 100644 --- a/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp +++ b/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp @@ -930,6 +930,35 @@ HWTEST_F(AbilityManagerServiceElevenTest, SetResidentProcessEnabled_001, TestSiz TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceElevenTest SetResidentProcessEnabled_001 end"); } +/* + * Feature: AbilityManagerService + * Name: GetUidByCloneBundleInfo_001 + * Function: GetUidByCloneBundleInfo + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetUidByCloneBundleInfo + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetUidByCloneBundleInfo_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_001 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + std::string bundleName = "com.ohos.settings"; + int uid = -1; + int userId = 100; + AppExecFwk::MockAppMgrService::retCode_ = uid; + abilityMs->GetUidByCloneBundleInfo(bundleName, uid, 0, userId); + + bundleName = "com.applicaion.test"; + userId = 101; + auto result = abilityMs->GetUidByCloneBundleInfo(bundleName, uid, 0, userId); + EXPECT_EQ(result, uid); + + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_001 end"; +} + /* * Feature: AbilityManagerService * Name: VerificationToken_001