diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_constants.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_constants.h index 6ada3e6844c63fdd3d1fcd123b8e0bc9071eee14..3b69fa094cfb34804a27b7bbf09e1da490a27269 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_constants.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_constants.h @@ -40,6 +40,12 @@ enum class ApplicationPendingState { BACKGROUNDING, }; +enum class ApplicationScheduleState { + SCHEDULE_READY = 0, + SCHEDULE_FOREGROUNDING, + SCHEDULE_BACKGROUNDING, +}; + enum class AbilityState { ABILITY_STATE_CREATE = 0, ABILITY_STATE_READY, diff --git a/services/appmgr/include/app_running_record.h b/services/appmgr/include/app_running_record.h index 93cb3e6e422633dc40642f53977eae24a48607e2..c821f29927030db894c9743937e5a3fb75be6481 100644 --- a/services/appmgr/include/app_running_record.h +++ b/services/appmgr/include/app_running_record.h @@ -732,6 +732,9 @@ public: void SetApplicationPendingState(ApplicationPendingState pendingState); ApplicationPendingState GetApplicationPendingState() const; + void SetApplicationScheduleState(ApplicationScheduleState scheduleState); + ApplicationScheduleState GetApplicationScheduleState() const; + void GetSplitModeAndFloatingMode(bool &isSplitScreenMode, bool &isFloatingWindowMode); void AddChildProcessRecord(pid_t pid, const std::shared_ptr record); @@ -900,6 +903,7 @@ private: bool isStageBasedModel_ = false; ApplicationState curState_ = ApplicationState::APP_STATE_CREATE; // current state of this process ApplicationPendingState pendingState_ = ApplicationPendingState::READY; + ApplicationScheduleState scheduleState_ = ApplicationScheduleState::SCHEDULE_READY; bool isFocused_ = false; // if process is focused. /** * If there is an ability is foregrounding, this flag will be true, diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 8a46819a4dbb53be609bfc17510bca49d237377b..5679039f2011a4ccde8cb32f26932c72107a9d0b 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -997,6 +997,12 @@ void AppMgrServiceInner::ApplicationForegrounded(const int32_t recordId) TAG_LOGE(AAFwkTag::APPMGR, "get app record failed"); return; } + // Prevent forged requests from changing the app's state. + if (appRecord->GetApplicationScheduleState() != ApplicationScheduleState::SCHEDULE_FOREGROUNDING) { + TAG_LOGE(AAFwkTag::APPMGR, "app is not scheduling to foreground."); + return; + } + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_READY); ApplicationState appState = appRecord->GetState(); if (appState == ApplicationState::APP_STATE_READY || appState == ApplicationState::APP_STATE_BACKGROUND) { if (appState == ApplicationState::APP_STATE_BACKGROUND) { @@ -1037,6 +1043,12 @@ void AppMgrServiceInner::ApplicationBackgrounded(const int32_t recordId) TAG_LOGE(AAFwkTag::APPMGR, "get app record failed"); return; } + // Prevent forged requests from changing the app's state. + if (appRecord->GetApplicationScheduleState() != ApplicationScheduleState::SCHEDULE_BACKGROUNDING) { + TAG_LOGE(AAFwkTag::APPMGR, "app is not scheduling to background."); + return; + } + appRecord->SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_READY); if (appRecord->GetState() == ApplicationState::APP_STATE_FOREGROUND) { appRecord->SetState(ApplicationState::APP_STATE_BACKGROUND); bool needNotifyApp = !AAFwk::UIExtensionUtils::IsUIExtension(appRecord->GetExtensionType()) diff --git a/services/appmgr/src/app_running_record.cpp b/services/appmgr/src/app_running_record.cpp index 33d3b6120976a940e7663f5ef9ac85ae8344cd5a..b14938bb5e9eab0a13e923538b0fa0e3d4b6ed93 100644 --- a/services/appmgr/src/app_running_record.cpp +++ b/services/appmgr/src/app_running_record.cpp @@ -632,6 +632,7 @@ void AppRunningRecord::LaunchPendingAbilities() } void AppRunningRecord::ScheduleForegroundRunning() { + SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_FOREGROUNDING); if (appLifeCycleDeal_) { appLifeCycleDeal_->ScheduleForegroundRunning(); } @@ -639,6 +640,7 @@ void AppRunningRecord::ScheduleForegroundRunning() void AppRunningRecord::ScheduleBackgroundRunning() { + SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_BACKGROUNDING); int32_t recordId = GetRecordId(); auto serviceInner = appMgrServiceInner_; auto appbackgroundtask = [recordId, serviceInner]() { @@ -2120,6 +2122,16 @@ ApplicationPendingState AppRunningRecord::GetApplicationPendingState() const return pendingState_; } +void AppRunningRecord::SetApplicationScheduleState(ApplicationScheduleState scheduleState) +{ + scheduleState_ = scheduleState; +} + +ApplicationScheduleState AppRunningRecord::GetApplicationScheduleState() const +{ + return scheduleState_; +} + void AppRunningRecord::AddChildProcessRecord(pid_t pid, const std::shared_ptr record) { if (!record) {