add interface TerminateMission

Signed-off-by: donglin <donglin9@huawei.com>
Change-Id: I2676a7ec2238cc7d12df93764018e325979c999c
This commit is contained in:
donglin 2024-07-24 11:58:14 +00:00
parent f7b9d82e83
commit ffa879eb2c
13 changed files with 185 additions and 15 deletions

View File

@ -140,6 +140,11 @@ public:
GET_CB_INFO_AND_CALL(env, info, JsAppManager, OnClearUpAppData);
}
static napi_value TerminateMission(napi_env env, napi_callback_info info)
{
GET_CB_INFO_AND_CALL(env, info, JsAppManager, OnTerminateMission);
}
static napi_value IsSharedBundleRunning(napi_env env, napi_callback_info info)
{
GET_CB_INFO_AND_CALL(env, info, JsAppManager, OnIsSharedBundleRunning);
@ -658,7 +663,7 @@ private:
std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
auto asyncTask = [appManager = appManager_, env, task = napiAsyncTask.get()]() {
if (appManager == nullptr) {
TAG_LOGW(AAFwkTag::APPMGR, "abilityManager nullptr");
TAG_LOGW(AAFwkTag::APPMGR, "appManager nullptr");
task->Reject(env, CreateJsError(env, AbilityErrorCode::ERROR_CODE_INNER));
delete task;
return;
@ -956,6 +961,46 @@ private:
return result;
}
napi_value OnTerminateMission(napi_env env, size_t argc, napi_value* argv)
{
TAG_LOGD(AAFwkTag::APPMGR, "OnTerminateMission call.");
if (argc < ARGC_ONE) {
TAG_LOGE(AAFwkTag::APPMGR, "Params not match");
ThrowTooFewParametersError(env);
return CreateJsUndefined(env);
}
int32_t missionId = 0;
if (!ConvertFromJsValue(env, argv[INDEX_ZERO], missionId)) {
TAG_LOGE(AAFwkTag::APPMGR, "get missionId wrong!");
ThrowInvalidParamError(env, "Parse param missionId failed, must be a number.");
return CreateJsUndefined(env);
}
napi_value result = nullptr;
std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, nullptr, &result);
auto asyncTask = [missionId, env, task = napiAsyncTask.get()]() {
auto amsClient = AAFwk::AbilityManagerClient::GetInstance();
if (amsClient == nullptr) {
TAG_LOGW(AAFwkTag::APPMGR, "amsClient nullptr");
task->Reject(env, CreateJsError(env, AbilityErrorCode::ERROR_CODE_INNER));
delete task;
return;
}
auto ret = amsClient->TerminateMission(missionId);
(ret == ERR_OK) ? task->ResolveWithNoError(env, CreateJsUndefined(env)) :
task->Reject(env, CreateJsErrorByNativeErr(env, ret, "Terminate mission failed."));
delete task;
};
if (napi_status::napi_ok != napi_send_event(env, asyncTask, napi_eprio_high)) {
napiAsyncTask->Reject(env, CreateJsErrorByNativeErr(env,
static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INNER), "Terminate mission failed."));
} else {
napiAsyncTask.release();
}
return result;
}
napi_value OnIsSharedBundleRunning(napi_env env, size_t argc, napi_value* argv)
{
TAG_LOGD(AAFwkTag::APPMGR, "called");
@ -1446,6 +1491,7 @@ napi_value JsAppManagerInit(napi_env env, napi_value exportObj)
JsAppManager::KillProcessesByBundleName);
BindNativeFunction(env, exportObj, "clearUpApplicationData", moduleName, JsAppManager::ClearUpApplicationData);
BindNativeFunction(env, exportObj, "clearUpAppData", moduleName, JsAppManager::ClearUpAppData);
BindNativeFunction(env, exportObj, "terminateMission", moduleName, JsAppManager::TerminateMission);
BindNativeFunction(env, exportObj, "getAppMemorySize", moduleName, JsAppManager::GetAppMemorySize);
BindNativeFunction(env, exportObj, "isRamConstrainedDevice", moduleName, JsAppManager::IsRamConstrainedDevice);
BindNativeFunction(env, exportObj, "isSharedBundleRunning", moduleName, JsAppManager::IsSharedBundleRunning);

View File

@ -1542,6 +1542,14 @@ public:
*/
int32_t OpenLink(const Want& want, sptr<IRemoteObject> callerToken, int32_t userId, int requestCode);
/**
* Terminate process by bundleName.
*
* @param missionId, The mission id of the UIAbility need to be terminated.
* @return Returns ERR_OK on success, others on failure.
*/
ErrCode TerminateMission(int32_t missionId);
private:
AbilityManagerClient();
DISALLOW_COPY_AND_MOVE(AbilityManagerClient);

View File

@ -1680,6 +1680,17 @@ public:
{
return 0;
}
/**
* Terminate the mission.
*
* @param missionId, The mission id of the UIAbility need to be terminated.
* @return Returns ERR_OK on success, others on failure.
*/
virtual int32_t TerminateMission(int32_t missionId)
{
return 0;
}
};
} // namespace AAFwk
} // namespace OHOS

View File

@ -543,6 +543,9 @@ enum class AbilityManagerInterfaceCode {
REQUEST_ASSERT_FAULT_DIALOG = 6116,
// ipc id for notify the operation status of the user
NOTIFY_DEBUG_ASSERT_RESULT = 6117,
// ipc id for terminate mission
TERMINATE_MISSION = 6118,
};
} // namespace AAFwk
} // namespace OHOS

View File

@ -1303,6 +1303,14 @@ public:
virtual int32_t OpenLink(const Want& want, sptr<IRemoteObject> callerToken,
int32_t userId = DEFAULT_INVAL_VALUE, int requestCode = DEFAULT_INVAL_VALUE) override;
/**
* Terminate the mission.
*
* @param missionId, The mission id of the UIAbility need to be terminated.
* @return Returns ERR_OK on success, others on failure.
*/
virtual int32_t TerminateMission(int32_t missionId) override;
private:
template <typename T>
int GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos);

View File

@ -1741,6 +1741,8 @@ public:
void NotifySCBToHandleAtomicServiceException(const std::string& sessionId, int errCode,
const std::string& reason);
int32_t TerminateMission(int32_t missionId) override;
// MSG 0 - 20 represents timeout message
static constexpr uint32_t LOAD_TIMEOUT_MSG = 0;
static constexpr uint32_t ACTIVE_TIMEOUT_MSG = 1;

View File

@ -347,6 +347,7 @@ private:
int HandleOnRemoteRequestInnerSecond(uint32_t code, MessageParcel &data,
MessageParcel &reply, MessageOption &option);
int32_t OpenLinkInner(MessageParcel &data, MessageParcel &reply);
int32_t TerminateMissionInner(MessageParcel &data, MessageParcel &reply);
};
} // namespace AAFwk
} // namespace OHOS

View File

@ -1944,5 +1944,35 @@ ErrCode AbilityManagerClient::OpenLink(const Want& want, sptr<IRemoteObject> cal
CHECK_POINTER_RETURN_INVALID_VALUE(abms);
return abms->OpenLink(want, callerToken, userId, requestCode);
}
ErrCode AbilityManagerClient::TerminateMission(int32_t missionId)
{
TAG_LOGI(AAFwkTag::ABILITYMGR, "TerminateMission begin.");
#ifdef SUPPORT_SCREEN
if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
auto sceneSessionManager = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
CHECK_POINTER_RETURN_INVALID_VALUE(sceneSessionManager);
TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
auto err = sceneSessionManager->TerminateSessionByPersistentId(missionId);
if (err != OHOS::Rosen::WMError::WM_OK) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "TerminateMission failed, err: %{public}d.", static_cast<int32_t>(err));
}
if (err == Rosen::WMError::WM_ERROR_INVALID_PERMISSION) {
return CHECK_PERMISSION_FAILED;
}
if (err == Rosen::WMError::WM_ERROR_NOT_SYSTEM_APP) {
return ERR_NOT_SYSTEM_APP;
}
return static_cast<int32_t>(err);
}
#endif //SUPPORT_SCREEN
auto abms = GetAbilityManager();
CHECK_POINTER_RETURN_INVALID_VALUE(abms);
int32_t ret = abms->TerminateMission(missionId);
if (ret != ERR_OK) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "TerminateMission failed, err: %{public}d.", ret);
}
return ret;
}
} // namespace AAFwk
} // namespace OHOS

View File

@ -5456,5 +5456,28 @@ ErrCode AbilityManagerProxy::OpenLink(const Want& want, sptr<IRemoteObject> call
}
return reply.ReadInt32();
}
int32_t AbilityManagerProxy::TerminateMission(int32_t missionId)
{
MessageParcel data;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(data)) {
return IPC_PROXY_ERR;
}
if (!data.WriteInt32(missionId)) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "appCloneIndex write failed.");
return INNER_ERR;
}
auto error = SendRequest(AbilityManagerInterfaceCode::TERMINATE_MISSION,
data, reply, option);
if (error != NO_ERROR) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "Send request error: %{public}d", error);
return error;
}
return reply.ReadInt32();
}
} // namespace AAFwk
} // namespace OHOS

View File

@ -11580,5 +11580,22 @@ void AbilityManagerService::SetAbilityRequestSessionInfo(AbilityRequest &ability
IPCSkeleton::GetCallingTokenID()));
abilityRequest.sessionInfo = sessionInfo;
}
int32_t AbilityManagerService::TerminateMission(int32_t missionId)
{
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
TAG_LOGI(AAFwkTag::ABILITYMGR, "TerminateMission call");
auto missionListManager = GetCurrentMissionListManager();
CHECK_POINTER_AND_RETURN(missionListManager, ERR_NO_INIT);
CHECK_CALLER_IS_SYSTEM_APP;
if (!PermissionVerification::GetInstance()->VerifyCallingPermission(
PermissionConstants::PERMISSION_KILL_APP_PROCESSES)) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "Permission verification failed");
return CHECK_PERMISSION_FAILED;
}
return missionListManager->ClearMission(missionId);
}
} // namespace AAFwk
} // namespace OHOS

View File

@ -757,6 +757,9 @@ int AbilityManagerStub::OnRemoteRequestInnerNineteenth(uint32_t code, MessagePar
if (interfaceCode == AbilityManagerInterfaceCode::OPEN_LINK) {
return OpenLinkInner(data, reply);
}
if (interfaceCode == AbilityManagerInterfaceCode::TERMINATE_MISSION) {
return TerminateMissionInner(data, reply);
}
return ERR_CODE_NOT_EXIST;
}
@ -3984,5 +3987,16 @@ int32_t AbilityManagerStub::OpenLinkInner(MessageParcel &data, MessageParcel &re
reply.WriteInt32(result);
return result;
}
int32_t AbilityManagerStub::TerminateMissionInner(MessageParcel &data, MessageParcel &reply)
{
int32_t missionId = data.ReadInt32();
int32_t result = TerminateMission(missionId);
if (result != NO_ERROR && result != ERR_OPEN_LINK_START_ABILITY_DEFAULT_OK) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "OpenLink failed.");
}
reply.WriteInt32(result);
return result;
}
} // namespace AAFwk
} // namespace OHOS

View File

@ -23,7 +23,8 @@
namespace OHOS {
namespace AAFwk {
namespace {
const std::string UIEXTENSION_MODAL_TYPE = "ability.want.params.modalType";
constexpr const char* UIEXTENSION_MODAL_TYPE = "ability.want.params.modalType";
constexpr const char* INTERCEPT_MISSION_ID = "intercept_missionId";
}
DisposedObserver::DisposedObserver(const AppExecFwk::DisposedRule &disposedRule,
@ -35,19 +36,26 @@ void DisposedObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData
{
TAG_LOGD(AAFwkTag::ABILITYMGR, "Call");
std::lock_guard<ffrt::mutex> guard(observerLock_);
if (abilityStateData.abilityState == static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND)) {
token_ = abilityStateData.token;
auto abilityRecord = Token::GetAbilityRecordByToken(token_);
if (abilityRecord && !abilityRecord->GetAbilityInfo().isStageBasedModel) {
auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>();
Want want = *disposedRule_.want;
want.SetParam(UIEXTENSION_MODAL_TYPE, 1);
bool ret = IN_PROCESS_CALL(systemUIExtension->CreateModalUIExtension(want));
if (!ret) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to start system UIExtension");
}
interceptor_->UnregisterObserver(abilityStateData.bundleName);
if (abilityStateData.abilityState != static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND)) {
return;
}
token_ = abilityStateData.token;
auto abilityRecord = Token::GetAbilityRecordByToken(token_);
if (abilityRecord && !abilityRecord->GetAbilityInfo().isStageBasedModel) {
auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>();
Want want = *disposedRule_.want;
want.SetParam(UIEXTENSION_MODAL_TYPE, 1);
auto sessionInfo = abilityRecord->GetSessionInfo();
if (sessionInfo != nullptr) {
want.SetParam(INTERCEPT_MISSION_ID, sessionInfo->persistentId);
} else {
want.SetParam(INTERCEPT_MISSION_ID, abilityRecord->GetMissionId());
}
bool ret = IN_PROCESS_CALL(systemUIExtension->CreateModalUIExtension(want));
if (!ret) {
TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to start system UIExtension");
}
interceptor_->UnregisterObserver(abilityStateData.bundleName);
}
}

View File

@ -1227,6 +1227,5 @@ HWTEST_F(AbilityManagerServiceFourthTest, ChangeUIAbilityVisibilityBySCB_001, Te
abilityMs_->ChangeUIAbilityVisibilityBySCB(MockSessionInfo(0), true);
TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceFourthTest ChangeUIAbilityVisibilityBySCB_001 end");
}
} // namespace AAFwk
} // namespace OHOS