From 3ce515935da7b4e903b7ebbed99d849646124fcb Mon Sep 17 00:00:00 2001 From: zhang_hao_zheng Date: Tue, 8 Jul 2025 14:31:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwelink=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=86=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhang_hao_zheng Change-Id: Ia483656b67a2efcb7e0689f2201774ed6876eaed --- .../appmgr/include/app_mgr_service_inner.h | 1 + services/appmgr/include/app_running_manager.h | 1 + services/appmgr/src/app_mgr_service_inner.cpp | 20 +++++++-- services/appmgr/src/app_running_manager.cpp | 41 +++++++++++++++++++ .../mock/src/mock_app_running_manager.cpp | 5 +++ .../mock/src/mock_app_running_manager.cpp | 5 +++ .../mock/src/mock_app_running_manager.cpp | 5 +++ .../app_running_manager_second_test.cpp | 1 + 8 files changed, 75 insertions(+), 4 deletions(-) diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index c37b99749ad..c61a314d4b3 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -2073,6 +2073,7 @@ private: void SetKeepAliveEnableStateAndNotify(const std::shared_ptr& appRecord, bool enable); void SetKeepAliveAppServiceAndNotify(const std::shared_ptr& appRecord, bool enable); bool GetKeepAliveState(const std::shared_ptr &appRecord); + void OnProcessDied(std::shared_ptr appRecord); bool isInitAppWaitingDebugListExecuted_ = false; std::atomic sceneBoardAttachFlag_ = true; std::atomic willKillPidsNum_ = 0; diff --git a/services/appmgr/include/app_running_manager.h b/services/appmgr/include/app_running_manager.h index af29725f35f..d157cd58c2c 100644 --- a/services/appmgr/include/app_running_manager.h +++ b/services/appmgr/include/app_running_manager.h @@ -348,6 +348,7 @@ public: int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr &callback); bool IsApplicationFirstForeground(const AppRunningRecord &foregroundingRecord); bool IsApplicationBackground(const AppRunningRecord &backgroundingRecord); + bool NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr currentAppRecord); bool IsApplicationFirstFocused(const AppRunningRecord &foregroundingRecord); bool IsApplicationUnfocused(const std::string &bundleName); #ifdef SUPPORT_SCREEN diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index b465c3c0b2b..4d7843bc64c 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -1532,7 +1532,8 @@ void AppMgrServiceInner::ApplicationForegrounded(const int32_t recordId) } appRecord->PopForegroundingAbilityTokens(); - TAG_LOGI(AAFwkTag::APPMGR, "ApplicationForegrounded, bundle: %{public}s", appRecord->GetBundleName().c_str()); + TAG_LOGI(AAFwkTag::APPMGR, "ApplicationForegrounded, bundle: %{public}s, pendingState: %{public}d", + appRecord->GetBundleName().c_str(), appRecord->GetApplicationPendingState()); if (appRecord->GetApplicationPendingState() == ApplicationPendingState::BACKGROUNDING) { appRecord->ScheduleBackgroundRunning(); } else if (appRecord->GetApplicationPendingState() == ApplicationPendingState::FOREGROUNDING) { @@ -4584,7 +4585,7 @@ void AppMgrServiceInner::ClearAppRunningData(const std::shared_ptrStateChangedNotifyObserver(abilityRecord, static_cast(AbilityState::ABILITY_STATE_TERMINATED), true, false); } - DelayedSingleton::GetInstance()->OnProcessDied(appRecord); + OnProcessDied(appRecord); DelayedSingleton::GetInstance()->OnProcessKilled(appRecord); // kill render if exist. @@ -4727,7 +4728,7 @@ void AppMgrServiceInner::TerminateApplication(const std::shared_ptrGetBundleName())) { RemoveRunningSharedBundleList(appRecord->GetBundleName()); } - DelayedSingleton::GetInstance()->OnProcessDied(appRecord); + OnProcessDied(appRecord); DelayedSingleton::GetInstance()->OnProcessKilled(appRecord); auto appInfo = appRecord->GetApplicationInfo(); if (appInfo != nullptr && !appRunningManager_->IsAppExist(appInfo->accessTokenId)) { @@ -7941,7 +7942,7 @@ void AppMgrServiceInner::ApplicationTerminatedSendProcessEvent(const std::shared return; } - DelayedSingleton::GetInstance()->OnProcessDied(appRecord); + OnProcessDied(appRecord); DelayedSingleton::GetInstance()->OnProcessKilled(appRecord); if (!GetAppRunningStateByBundleName(appRecord->GetBundleName())) { RemoveRunningSharedBundleList(appRecord->GetBundleName()); @@ -10168,5 +10169,16 @@ int32_t AppMgrServiceInner::DemoteCurrentFromCandidateMasterProcess() return ERR_OK; } +void AppMgrServiceInner::OnProcessDied(std::shared_ptr appRecord) +{ + if (appRunningManager_ == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "running manager null"); + return; + } + if (appRunningManager_->NeedNotifyAppStateChangeWhenProcessDied(appRecord)) { + OnAppStateChanged(appRecord, ApplicationState::APP_STATE_BACKGROUND, true, false); + } + DelayedSingleton::GetInstance()->OnProcessDied(appRecord); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_running_manager.cpp b/services/appmgr/src/app_running_manager.cpp index a745438edb0..988c136e86c 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -1592,6 +1592,47 @@ bool AppRunningManager::IsApplicationBackground(const AppRunningRecord &backgrou } return true; } + +bool AppRunningManager::NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr currentAppRecord) +{ + TAG_LOGD(AAFwkTag::APPMGR, "NeedNotifyAppStateChangeWhenProcessDied called"); + if (currentAppRecord == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "null currentAppRecord"); + return false; + } + if (currentAppRecord->GetState() != ApplicationState::APP_STATE_FOREGROUND) { + return false; + } + std::lock_guard guard(runningRecordMapMutex_); + bool hasBackgroundApp = false; + for (const auto &item : appRunningRecordMap_) { + const auto &appRecord = item.second; + if (appRecord == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "null appRecord"); + continue; + } + if (appRecord == currentAppRecord) { + continue; + } + if (AAFwk::UIExtensionUtils::IsUIExtension(appRecord->GetExtensionType()) + || AAFwk::UIExtensionUtils::IsWindowExtension(appRecord->GetExtensionType())) { + continue; + } + auto state = appRecord->GetState(); + if (appRecord->GetUid() != currentAppRecord->GetUid()) { + continue; + } + if (state == ApplicationState::APP_STATE_FOREGROUND) { + return false; + } + hasBackgroundApp = true; + } + if (!hasBackgroundApp) { + return false; + } + return true; +} + #ifdef SUPPORT_SCREEN void AppRunningManager::OnWindowVisibilityChanged( const std::vector> &windowVisibilityInfos) diff --git a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp index 8d27010f65e..42bdfa15d4f 100644 --- a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp +++ b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_running_manager.cpp @@ -321,6 +321,11 @@ bool AppRunningManager::IsApplicationBackground(const AppRunningRecord &backgrou { return true; } + +bool AppRunningManager::NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr currentAppRecord) +{ + return true; +} #ifdef SUPPORT_SCREEN void AppRunningManager::OnWindowVisibilityChanged( const std::vector> &windowVisibilityInfos) diff --git a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp index 228d075073b..3175230791a 100644 --- a/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp +++ b/test/unittest/app_mgr_service_inner_ninth_test/mock/src/mock_app_running_manager.cpp @@ -330,6 +330,11 @@ bool AppRunningManager::IsApplicationBackground(const AppRunningRecord &backgrou { return true; } + +bool AppRunningManager::NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr currentAppRecord) +{ + return true; +} #ifdef SUPPORT_SCREEN void AppRunningManager::OnWindowVisibilityChanged( const std::vector> &windowVisibilityInfos) diff --git a/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp b/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp index 0cb3ea00e9f..700387d2a55 100644 --- a/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp +++ b/test/unittest/app_mgr_service_inner_seventh_test/mock/src/mock_app_running_manager.cpp @@ -321,6 +321,11 @@ bool AppRunningManager::IsApplicationBackground(const AppRunningRecord &backgrou { return true; } + +bool AppRunningManager::NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr currentAppRecord) +{ + return true; +} #ifdef SUPPORT_SCREEN void AppRunningManager::OnWindowVisibilityChanged( const std::vector> &windowVisibilityInfos) diff --git a/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp b/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp index c253b23dbea..430f4ce747c 100644 --- a/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp +++ b/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp @@ -1608,5 +1608,6 @@ HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_SignRestartAppFlag_0300, EXPECT_TRUE(record->GetRestartAppFlag()); TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_SignRestartAppFlag_0300 end"); } + } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file -- Gitee From f4bc47256d796b62e4e436d73048206222d665f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=8A=E5=B3=A5?= Date: Wed, 9 Jul 2025 08:10:39 +0000 Subject: [PATCH 2/3] update test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张昊峥 --- .../app_running_manager_second_test.cpp | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp b/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp index 430f4ce747c..a3c09b1df27 100644 --- a/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp +++ b/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp @@ -1609,5 +1609,176 @@ HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_SignRestartAppFlag_0300, TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_SignRestartAppFlag_0300 end"); } +/** + * @tc.name: AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0100 + * @tc.desc: Test NeedNotifyAppStateChangeWhenProcessDied + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0100, + TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect step1 succeed + */ + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0100 start"); + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect true + */ + auto ret = appRunningManager->NeedNotifyAppStateChangeWhenProcessDied(nullptr); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0100 end"); +} + +/** + * @tc.name: AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0200 + * @tc.desc: Test NeedNotifyAppStateChangeWhenProcessDied + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0200, + TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect step1 succeed + */ + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0200 start"); + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect true + */ + auto currentRecord = std::make_shared(appInfo_, 1, PROCESS_NAME); + currentRecord->SetState(ApplicationState::APP_STATE_BACKGROUND); + auto ret = appRunningManager->NeedNotifyAppStateChangeWhenProcessDied(currentRecord); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0200 end"); +} + +/** + * @tc.name: AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0300 + * @tc.desc: Test NeedNotifyAppStateChangeWhenProcessDied + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0300, + TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect step1 succeed + */ + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0300 start"); + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + auto recordId = AppRecordId::Create(); + appRunningManager->appRunningRecordMap_.emplace(recordId, nullptr); + + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect true + */ + auto currentRecord = std::make_shared(appInfo_, 1, PROCESS_NAME); + currentRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + auto ret = appRunningManager->NeedNotifyAppStateChangeWhenProcessDied(currentRecord); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0300 end"); +} + +/** + * @tc.name: AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0400 + * @tc.desc: Test NeedNotifyAppStateChangeWhenProcessDied + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0400, + TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect step1 succeed + */ + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0400 start"); + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + appInfo_->bundleName = BUNDLE_NAME; + auto record = appRunningManager->CreateAppRunningRecord(appInfo_, PROCESS_NAME, bundleInfo, ""); + record->extensionType_ = AppExecFwk::ExtensionAbilityType::UI; + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect true + */ + auto currentRecord = std::make_shared(appInfo_, 1, PROCESS_NAME); + currentRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + auto ret = appRunningManager->NeedNotifyAppStateChangeWhenProcessDied(currentRecord); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0400 end"); +} + +/** + * @tc.name: AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0500 + * @tc.desc: Test NeedNotifyAppStateChangeWhenProcessDied + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0500, + TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect step1 succeed + */ + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0500 start"); + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + appInfo_->bundleName = BUNDLE_NAME; + auto record = appRunningManager->CreateAppRunningRecord(appInfo_, PROCESS_NAME, bundleInfo, ""); + record->extensionType_ = AppExecFwk::ExtensionAbilityType::SERVICE; + record->SetState(ApplicationState::APP_STATE_FOREGROUND); + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect true + */ + auto currentRecord = std::make_shared(appInfo_, 1, PROCESS_NAME); + currentRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + record->SetUid(currentRecord->GetUid()); + auto ret = appRunningManager->NeedNotifyAppStateChangeWhenProcessDied(currentRecord); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0500 end"); +} + +/** + * @tc.name: AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0600 + * @tc.desc: Test NeedNotifyAppStateChangeWhenProcessDied + * @tc.type: FUNC + */ +HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0600, + TestSize.Level1) +{ + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect step1 succeed + */ + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0600 start"); + auto appRunningManager = std::make_shared(); + EXPECT_NE(appRunningManager, nullptr); + appInfo_->bundleName = BUNDLE_NAME; + auto record = appRunningManager->CreateAppRunningRecord(appInfo_, PROCESS_NAME, bundleInfo, ""); + record->extensionType_ = AppExecFwk::ExtensionAbilityType::SERVICE; + record->SetState(ApplicationState::APP_STATE_BACKGROUND); + /** + * @tc.steps: step1. Initialize AppRunningManager instance + * @tc.expected: expect true + */ + auto currentRecord = std::make_shared(appInfo_, 1, PROCESS_NAME); + currentRecord->SetState(ApplicationState::APP_STATE_FOREGROUND); + record->SetUid(currentRecord->GetUid()); + auto ret = appRunningManager->NeedNotifyAppStateChangeWhenProcessDied(currentRecord); + EXPECT_TRUE(ret); + TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_NeedNotifyAppStateChangeWhenProcessDied_0600 end"); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file -- Gitee From c8674559733ba4af9f7aba560a1743a5e309b2ae Mon Sep 17 00:00:00 2001 From: zhang_hao_zheng Date: Wed, 9 Jul 2025 16:40:12 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhang_hao_zheng Change-Id: Iaf927432b0992c5c1d4c65927cb016bf16b47c50 --- services/appmgr/src/app_running_manager.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/services/appmgr/src/app_running_manager.cpp b/services/appmgr/src/app_running_manager.cpp index 988c136e86c..74e14e9ce7a 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -1604,7 +1604,7 @@ bool AppRunningManager::NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr< return false; } std::lock_guard guard(runningRecordMapMutex_); - bool hasBackgroundApp = false; + bool needNotify = false; for (const auto &item : appRunningRecordMap_) { const auto &appRecord = item.second; if (appRecord == nullptr) { @@ -1625,12 +1625,9 @@ bool AppRunningManager::NeedNotifyAppStateChangeWhenProcessDied(std::shared_ptr< if (state == ApplicationState::APP_STATE_FOREGROUND) { return false; } - hasBackgroundApp = true; + needNotify = true; } - if (!hasBackgroundApp) { - return false; - } - return true; + return needNotify; } #ifdef SUPPORT_SCREEN -- Gitee