diff --git a/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp b/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp index 38122da391..0bc24ab705 100644 --- a/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp +++ b/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp @@ -1067,5 +1067,18 @@ ErrCode BundleMgrHelper::UnregisterPluginEventCallback(sptrUnregisterPluginEventCallback(pluginEventCallback); } + +ErrCode BundleMgrHelper::GetCloneBundleInfoExt(const std::string &bundleName, uint32_t flags, int32_t appIndex, + int32_t userId, BundleInfo &bundleInfo) +{ + TAG_LOGI(AAFwkTag::BUNDLEMGRHELPER, "GetCloneBundleInfoExt"); + auto bundleMgr = Connect(); + if (bundleMgr == nullptr) { + TAG_LOGE(AAFwkTag::BUNDLEMGRHELPER, "null bundleMgr"); + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + return bundleMgr->GetCloneBundleInfoExt(bundleName, flags, appIndex, userId, bundleInfo); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/ability_manager/include/iability_manager_collaborator.h b/interfaces/inner_api/ability_manager/include/iability_manager_collaborator.h index cb9c51c8c8..7c8df2e139 100644 --- a/interfaces/inner_api/ability_manager/include/iability_manager_collaborator.h +++ b/interfaces/inner_api/ability_manager/include/iability_manager_collaborator.h @@ -196,6 +196,17 @@ public: return 0; } + /** + * @brief kill processes by bundleName. + * @param bundleName the bundleName of processes to be killed. + * @param userId the user id of processes to be killed. + * @return 0 when on success or else failed. + */ + virtual int32_t NotifyKillProcesses(const std::string &bundleName, int32_t userId) + { + return 0; + } + enum { NOTIFY_START_ABILITY = 1, NOTIFY_MISSION_CREATED, @@ -216,6 +227,7 @@ public: CHECK_STATIC_CFG_PERMISSION, UPDATE_CALLER_IF_NEED, UPDATE_TARGET_IF_NEED, + NOTIFY_KILL_PROCESSES, }; }; } // namespace AAFwk diff --git a/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h b/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h index 1457eefc8d..c221af6963 100644 --- a/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h +++ b/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h @@ -119,6 +119,9 @@ public: AbilityInfo &pluginAbilityInfo); ErrCode RegisterPluginEventCallback(sptr pluginEventCallback); ErrCode UnregisterPluginEventCallback(sptr pluginEventCallback); + // for collaborator (along with normal) + ErrCode GetCloneBundleInfoExt(const std::string &bundleName, uint32_t flags, int32_t appIndex, + int32_t userId, BundleInfo &bundleInfo); private: sptr Connect(); diff --git a/services/abilitymgr/include/ability_manager_collaborator_proxy.h b/services/abilitymgr/include/ability_manager_collaborator_proxy.h index df7cddd854..c3fc8e67cb 100644 --- a/services/abilitymgr/include/ability_manager_collaborator_proxy.h +++ b/services/abilitymgr/include/ability_manager_collaborator_proxy.h @@ -162,6 +162,14 @@ public: * @return 0 when update target successfully or else failed. */ virtual int32_t UpdateTargetIfNeed(Want &want) override; + + /** + * @brief kill processes by bundleName. + * @param bundleName the bundleName of processes to be killed. + * @param userId the user id of processes to be killed. + * @return 0 when on success or else failed. + */ + virtual int32_t NotifyKillProcesses(const std::string &bundleName, int32_t userId) override; private: static inline BrokerDelegator delegator_; int32_t SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/abilitymgr/src/ability_manager_collaborator_proxy.cpp b/services/abilitymgr/src/ability_manager_collaborator_proxy.cpp index 8466f31dae..deb1c22d31 100644 --- a/services/abilitymgr/src/ability_manager_collaborator_proxy.cpp +++ b/services/abilitymgr/src/ability_manager_collaborator_proxy.cpp @@ -537,6 +537,37 @@ int32_t AbilityManagerCollaboratorProxy::UpdateCallerIfNeed(Want &want) return NO_ERROR; } +int32_t AbilityManagerCollaboratorProxy::NotifyKillProcesses(const std::string &bundleName, int32_t userId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + if (!data.WriteInterfaceToken(AbilityManagerCollaboratorProxy::GetDescriptor())) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "write token fail"); + return ERR_INVALID_OPERATION; + } + if (!data.WriteString16(Str8ToStr16(bundleName))) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "bundleName write fail"); + return ERR_INVALID_OPERATION; + } + if (!data.WriteInt32(userId)) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "userId write fail"); + return ERR_INVALID_OPERATION; + } + auto remote = Remote(); + if (!remote) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null remote"); + return ERR_INVALID_OPERATION; + } + int32_t ret = remote->SendRequest( + IAbilityManagerCollaborator::NOTIFY_KILL_PROCESSES, data, reply, option); + if (ret != NO_ERROR) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "request error:%{public}d", ret); + return ret; + } + return NO_ERROR; +} + int32_t AbilityManagerCollaboratorProxy::UpdateTargetIfNeed(Want &want) { MessageParcel data; diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 0c7ddfc837..25ab30f4f0 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -7101,12 +7101,32 @@ int AbilityManagerService::KillProcess(const std::string &bundleName, bool clear CHECK_POINTER_AND_RETURN(bms, KILL_PROCESS_FAILED); int32_t userId = GetUserId(); AppExecFwk::BundleInfo bundleInfo; - if (IN_PROCESS_CALL(bms->GetCloneBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, appIndex, - bundleInfo, userId)) != ERR_OK) { + if (IN_PROCESS_CALL(bms->GetCloneBundleInfoExt(bundleName, + static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION), + appIndex, userId, bundleInfo)) != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "get bundle info when kill process failed"); return GET_BUNDLE_INFO_FAILED; } + int32_t collaboratorType = -1; + if (bundleInfo.applicationInfo.codePath == std::to_string(CollaboratorType::RESERVE_TYPE)) { + collaboratorType = CollaboratorType::RESERVE_TYPE; + } else if (bundleInfo.applicationInfo.codePath == std::to_string(CollaboratorType::OTHERS_TYPE)) { + collaboratorType = CollaboratorType::OTHERS_TYPE; + } + if (collaboratorType != -1) { + TAG_LOGI(AAFwkTag::ABILITYMGR, "Collaborator kill"); + auto collaborator = GetCollaborator(collaboratorType); + if (collaborator == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "Collaborator null"); + return KILL_PROCESS_FAILED; + } + if (collaborator->NotifyKillProcesses(bundleName, userId) != ERR_OK) { + return KILL_PROCESS_FAILED; + } + return ERR_OK; + } + KeepAliveType type; if (KeepAliveUtils::IsKeepAliveBundle(bundleInfo, userId, type) && DelayedSingleton::GetInstance()->IsMemorySizeSufficent()) { diff --git a/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_proxy_test.cpp b/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_proxy_test.cpp index 9d9fe5e4e0..fc7c361d79 100644 --- a/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_proxy_test.cpp +++ b/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_proxy_test.cpp @@ -299,5 +299,21 @@ HWTEST_F(AbilityManagerCollaboratorProxyTest, CheckStaticCfgPermission_0100, Tes EXPECT_EQ(res, NO_ERROR); EXPECT_EQ(static_cast(IAbilityManagerCollaborator::CHECK_STATIC_CFG_PERMISSION), mock_->GetCode()); } + +/** + * @tc.number: NotifyKillProcesses_0100 + * @tc.desc: NotifyKillProcesses + * @tc.type: FUNC + */ +HWTEST_F(AbilityManagerCollaboratorProxyTest, NotifyKillProcesses_0100, TestSize.Level1) +{ + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(mock_.GetRefPtr(), &AbilityManagerCollaboratorStubMock::InvokeSendRequest)); + std::string bundleName; + int32_t res = proxy_->NotifyKillProcesses(bundleName, 0); + EXPECT_EQ(res, NO_ERROR); + EXPECT_EQ(static_cast(IAbilityManagerCollaborator::NOTIFY_KILL_PROCESSES), mock_->GetCode()); +} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_stub_mock.h b/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_stub_mock.h index 8c623be717..4bee3eeb2c 100644 --- a/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_stub_mock.h +++ b/test/unittest/ability_manager_collaborator_proxy_test/ability_manager_collaborator_stub_mock.h @@ -90,6 +90,7 @@ public: MOCK_METHOD2(OpenFile, int(const Uri& uri, uint32_t flag)); MOCK_METHOD2(NotifyMissionBindPid, void(int32_t missionId, int32_t pid)); MOCK_METHOD2(CheckStaticCfgPermission, int32_t(const Want &want, bool isImplicit)); + MOCK_METHOD2(NotifyKillProcesses, int32_t(const std::string &, int32_t)); int InvokeSendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/test/unittest/bundle_mgr_helper_test/bundle_mgr_helper_test.cpp b/test/unittest/bundle_mgr_helper_test/bundle_mgr_helper_test.cpp index 85115eae14..7624899366 100644 --- a/test/unittest/bundle_mgr_helper_test/bundle_mgr_helper_test.cpp +++ b/test/unittest/bundle_mgr_helper_test/bundle_mgr_helper_test.cpp @@ -894,5 +894,21 @@ HWTEST_F(BundleMgrHelperTest, BundleMgrHelperTest_GetPluginInfosForSelf_001, Tes auto ret = bundleMgrHelper->GetPluginInfosForSelf(pluginBundleInfos); EXPECT_NE(ret, ERR_OK); } + +/** + * @tc.name: BundleMgrHelperTest_GetCloneBundleInfoExt_001 + * @tc.desc: GetSignatureInfoByBundleName + * @tc.type: FUNC + */ +HWTEST_F(BundleMgrHelperTest, BundleMgrHelperTest_GetCloneBundleInfoExt_001, TestSize.Level1) +{ + std::string bundleName; + int32_t userId = 100; + int32_t appIndex = 0; + uint32_t flag = 0; + AppExecFwk::BundleInfo bundleInfo; + auto ret = bundleMgrHelper->GetCloneBundleInfoExt(bundleName, flag, appIndex, userId, bundleInfo); + EXPECT_NE(ret, ERR_OK); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file