diff --git a/services/abilitymgr/include/utils/clone_for_account_util.h b/services/abilitymgr/include/utils/clone_for_account_util.h index dd1c3f6127..8d706c1c02 100644 --- a/services/abilitymgr/include/utils/clone_for_account_util.h +++ b/services/abilitymgr/include/utils/clone_for_account_util.h @@ -26,7 +26,7 @@ namespace OHOS { namespace AAFwk { class CloneForAccountUtil { public: - static void ProcessAppIndex(Want &want, int32_t userId); + static bool ProcessAppIndex(Want &want, int32_t userId); }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index d3d4622819..6b1955f26c 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -699,7 +699,8 @@ int AbilityManagerService::StartAbility(const Want &want, int32_t userId, int re userId = GetValidUserId(userId); } #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(want), userId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), userId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); XCOLLIE_TIMER_LESS_IGNORE(__PRETTY_FUNCTION__, !want.GetDeviceId().empty()); @@ -1392,7 +1393,8 @@ int AbilityManagerService::StartAbilityInner(StartAbilityWrapParam ¶m) } int32_t oriValidUserId = GetValidUserId(param.userId); #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(param.want, oriValidUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(param.want, oriValidUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif std::string dialogSessionId = param.want.GetStringParam("dialogSessionId"); bool isSendDialogResult = false; @@ -1838,7 +1840,8 @@ int AbilityManagerService::StartAbilityDetails(const Want &want, const AbilitySt EventInfo eventInfo = BuildEventInfo(want, userId); int32_t oriValidUserId = GetValidUserId(userId); #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif if (want.GetBoolParam(AbilityConfig::DEBUG_APP, false)) { if (!system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) { @@ -2219,7 +2222,8 @@ int AbilityManagerService::StartAbilityForOptionInner(const Want &want, const St EventInfo eventInfo = BuildEventInfo(want, userId); int32_t oriValidUserId = GetValidUserId(userId); #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif // prevent the app from dominating the screen if (callerToken == nullptr && !IsCallerSceneBoard() && !isCallByShortcut && @@ -2629,7 +2633,8 @@ int32_t AbilityManagerService::StartUIAbilitiesInSplitWindowModeHandleWant(const int32_t requestCode = DEFAULT_INVAL_VALUE; int32_t appIndex = 0; #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(secondaryWant), validUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(secondaryWant), validUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif AbilityUtil::RemoveShowModeKey(const_cast(secondaryWant)); auto result = CheckWantForSplitMode(secondaryWant, callerToken, validUserId, appIndex); @@ -2781,7 +2786,8 @@ int32_t AbilityManagerService::StartUIAbilitiesHandleWant(const Want &want, sptr uint32_t specifyTokenId = 0; int32_t requestCode = DEFAULT_INVAL_VALUE; #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(want), validUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), validUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif AbilityUtil::RemoveShowModeKey(const_cast(want)); @@ -3126,7 +3132,8 @@ int AbilityManagerService::StartUIAbilityBySCB(sptr sessionInfo, Ab #ifdef ENABLE_CLONE_FOR_ACCOUNT if (sessionInfo->want.GetBoolParam(ServerConstant::IS_CALL_BY_SCB, true)) { auto currentUserId = IPCSkeleton::GetCallingUid() / BASE_USER_RANGE; - CloneForAccountUtil::ProcessAppIndex(sessionInfo->want, currentUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(sessionInfo->want, currentUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); } #endif sessionInfo->want.SetParam(AbilityRuntime::GlobalConstant::PAGE_CONFIG, params.pageConfig); @@ -9950,7 +9957,8 @@ int AbilityManagerService::StartAbilityByCallWithErrMsg(const Want &want, const } int32_t oriValidUserId = GetValidUserId(accountId); #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif AbilityUtil::RemoveWantKey(const_cast(want)); int32_t appIndex = 0; @@ -10074,7 +10082,8 @@ int AbilityManagerService::StartAbilityForPrelaunch(const Want &want, const int3 } int32_t oriValidUserId = GetValidUserId(DEFAULT_INVAL_VALUE); #ifdef ENABLE_CLONE_FOR_ACCOUNT - CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), oriValidUserId), + RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif auto shouldBlockFunc = [aams = shared_from_this()]() { return aams->ShouldBlockAllAppStart(); }; AbilityInterceptorParam interceptorParam = AbilityInterceptorParam(want, 0, oriValidUserId, true, nullptr, @@ -11216,7 +11225,8 @@ int AbilityManagerService::StartUserTest(const Want &want, const sptr(want), GetValidUserId(DEFAULT_INVAL_VALUE)); + CHECK_TRUE_RETURN_RET(!CloneForAccountUtil::ProcessAppIndex(const_cast(want), + GetValidUserId(DEFAULT_INVAL_VALUE)), RESOLVE_ABILITY_ERR, "CloneForAccountUtil::ProcessAppIndex failed"); #endif int32_t appIndex = 0; if (!StartAbilityUtils::GetAppIndex(want, nullptr, appIndex) || appIndex != 0) { diff --git a/services/abilitymgr/src/utils/clone_for_account_util.cpp b/services/abilitymgr/src/utils/clone_for_account_util.cpp index f9ece3c358..288451aff4 100644 --- a/services/abilitymgr/src/utils/clone_for_account_util.cpp +++ b/services/abilitymgr/src/utils/clone_for_account_util.cpp @@ -21,23 +21,29 @@ namespace OHOS { namespace AAFwk { -void CloneForAccountUtil::ProcessAppIndex(Want &want, int32_t userId) +bool CloneForAccountUtil::ProcessAppIndex(Want &want, int32_t userId) { want.RemoveParam(Want::PARAM_APP_CLONE_INDEX_KEY); + if (want.GetElement().GetAbilityName().empty()) { + TAG_LOGI(AAFwkTag::ABILITYMGR, "implicit start, skip"); + return true; + } + auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); if (bundleMgrHelper == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "bundleMgrHelper is nullptr"); - return; + return false; } AppExecFwk::AbilityInfo abilityInfo; if (!IN_PROCESS_CALL(bundleMgrHelper->QueryEnabledAbilityInfo(want, userId, abilityInfo))) { TAG_LOGE(AAFwkTag::ABILITYMGR, "QueryEnabledAbilityInfo failed"); - return; + return false; } want.SetParam(Want::PARAM_APP_CLONE_INDEX_KEY, abilityInfo.appIndex); TAG_LOGI(AAFwkTag::ABILITYMGR, "CloneForAccount resolved appIndex: %{public}d", abilityInfo.appIndex); + return true; } } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/clone_for_account_util_test/clone_for_account_util_test.cpp b/test/unittest/clone_for_account_util_test/clone_for_account_util_test.cpp index 4ef8877620..d3c9f7cd26 100644 --- a/test/unittest/clone_for_account_util_test/clone_for_account_util_test.cpp +++ b/test/unittest/clone_for_account_util_test/clone_for_account_util_test.cpp @@ -46,7 +46,7 @@ void CloneForAccountUtilTest::TearDown() {} /** * @tc.name: ProcessAppIndex_001 - * @tc.desc: Test ProcessAppIndex success with appIndex in want + * @tc.desc: Test ProcessAppIndex returns true on success with appIndex in want * @tc.type: FUNC */ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_001, TestSize.Level1) @@ -61,15 +61,14 @@ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_001, TestSize.Level1) AppExecFwk::BundleMgrHelper::abilityInfoResult = mockAbilityInfo; AppExecFwk::BundleMgrHelper::retQueryEnabledAbilityInfo = true; - CloneForAccountUtil::ProcessAppIndex(want, 100); - + EXPECT_TRUE(CloneForAccountUtil::ProcessAppIndex(want, 100)); EXPECT_TRUE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); EXPECT_EQ(want.GetIntParam(Want::PARAM_APP_CLONE_INDEX_KEY, -1), 1); } /** * @tc.name: ProcessAppIndex_002 - * @tc.desc: Test ProcessAppIndex with bundleMgrHelper null + * @tc.desc: Test ProcessAppIndex returns false when bundleMgrHelper is null * @tc.type: FUNC */ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_002, TestSize.Level1) @@ -80,14 +79,13 @@ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_002, TestSize.Level1) want.SetElementName("com.test.bundle", "MainAbility"); want.SetParam(Want::PARAM_APP_CLONE_INDEX_KEY, 1); - CloneForAccountUtil::ProcessAppIndex(want, 100); - + EXPECT_FALSE(CloneForAccountUtil::ProcessAppIndex(want, 100)); EXPECT_FALSE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); } /** * @tc.name: ProcessAppIndex_003 - * @tc.desc: Test ProcessAppIndex with QueryEnabledAbilityInfo failed + * @tc.desc: Test ProcessAppIndex returns false when QueryEnabledAbilityInfo fails * @tc.type: FUNC */ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_003, TestSize.Level1) @@ -98,14 +96,13 @@ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_003, TestSize.Level1) want.SetElementName("com.test.bundle", "MainAbility"); want.SetParam(Want::PARAM_APP_CLONE_INDEX_KEY, 1); - CloneForAccountUtil::ProcessAppIndex(want, 100); - + EXPECT_FALSE(CloneForAccountUtil::ProcessAppIndex(want, 100)); EXPECT_FALSE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); } /** * @tc.name: ProcessAppIndex_004 - * @tc.desc: Test ProcessAppIndex with no appIndex in want + * @tc.desc: Test ProcessAppIndex returns true with no appIndex in want * @tc.type: FUNC */ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_004, TestSize.Level1) @@ -119,8 +116,7 @@ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_004, TestSize.Level1) AppExecFwk::BundleMgrHelper::abilityInfoResult = mockAbilityInfo; AppExecFwk::BundleMgrHelper::retQueryEnabledAbilityInfo = true; - CloneForAccountUtil::ProcessAppIndex(want, 100); - + EXPECT_TRUE(CloneForAccountUtil::ProcessAppIndex(want, 100)); EXPECT_TRUE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); EXPECT_EQ(want.GetIntParam(Want::PARAM_APP_CLONE_INDEX_KEY, -1), 0); } @@ -142,15 +138,14 @@ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_005, TestSize.Level1) AppExecFwk::BundleMgrHelper::abilityInfoResult = mockAbilityInfo; AppExecFwk::BundleMgrHelper::retQueryEnabledAbilityInfo = true; - CloneForAccountUtil::ProcessAppIndex(want, 100); - + EXPECT_TRUE(CloneForAccountUtil::ProcessAppIndex(want, 100)); EXPECT_TRUE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); EXPECT_EQ(want.GetIntParam(Want::PARAM_APP_CLONE_INDEX_KEY, -1), 2); } /** * @tc.name: ProcessAppIndex_006 - * @tc.desc: Test ProcessAppIndex removes appIndex before query, query fails + * @tc.desc: Test ProcessAppIndex returns false and removes appIndex when query fails * @tc.type: FUNC */ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_006, TestSize.Level1) @@ -161,8 +156,24 @@ HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_006, TestSize.Level1) want.SetElementName("com.test.bundle", "MainAbility"); want.SetParam(Want::PARAM_APP_CLONE_INDEX_KEY, 99); - CloneForAccountUtil::ProcessAppIndex(want, 100); + EXPECT_FALSE(CloneForAccountUtil::ProcessAppIndex(want, 100)); + EXPECT_FALSE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); +} +/** + * @tc.name: ProcessAppIndex_007 + * @tc.desc: Test ProcessAppIndex returns true on implicit start without query + * @tc.type: FUNC + */ +HWTEST_F(CloneForAccountUtilTest, ProcessAppIndex_007, TestSize.Level1) +{ + Want want; + want.SetAction("ohos.want.action.viewData"); + want.SetParam(Want::PARAM_APP_CLONE_INDEX_KEY, 1); + + AppExecFwk::BundleMgrHelper::retQueryEnabledAbilityInfo = false; + + EXPECT_TRUE(CloneForAccountUtil::ProcessAppIndex(want, 100)); EXPECT_FALSE(want.HasParameter(Want::PARAM_APP_CLONE_INDEX_KEY)); }