Add killprocess for collaborator

Signed-off-by: wangzhen <wangzhen416@huawei.com>
Change-Id: I6c59546c63321e76c6835cb33425a66d5c1a7692
This commit is contained in:
wangzhen
2025-04-22 11:49:41 +08:00
parent 68279685e8
commit 7e679de809
9 changed files with 122 additions and 2 deletions
@@ -1067,5 +1067,18 @@ ErrCode BundleMgrHelper::UnregisterPluginEventCallback(sptr<IBundleEventCallback
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
return bundleMgr->UnregisterPluginEventCallback(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
@@ -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
@@ -119,6 +119,9 @@ public:
AbilityInfo &pluginAbilityInfo);
ErrCode RegisterPluginEventCallback(sptr<IBundleEventCallback> pluginEventCallback);
ErrCode UnregisterPluginEventCallback(sptr<IBundleEventCallback> 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<IBundleMgr> Connect();
@@ -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<AbilityManagerCollaboratorProxy> delegator_;
int32_t SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
@@ -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;
@@ -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<uint32_t>(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<AppScheduler>::GetInstance()->IsMemorySizeSufficent()) {
@@ -299,5 +299,21 @@ HWTEST_F(AbilityManagerCollaboratorProxyTest, CheckStaticCfgPermission_0100, Tes
EXPECT_EQ(res, NO_ERROR);
EXPECT_EQ(static_cast<uint32_t>(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<uint32_t>(IAbilityManagerCollaborator::NOTIFY_KILL_PROCESSES), mock_->GetCode());
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -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)
{
@@ -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