diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index c37b99749ad91005318b9cb7505c89f58b3147f6..c61a314d4b36dfacb53b4732c2d3e36ba2049746 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 af29725f35f7d7c676421d832c4972580261a19a..d157cd58c2c9d715c26696e1600220363a7a1ed4 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 b465c3c0b2b3ef284dc2519c62088f3109cc628a..4d7843bc64ce0e83fa2f0ae70d6c7d034bb4c60b 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 a745438edb0b4f7e85654f5c3562fb996aff2043..74e14e9ce7abea58f3ba53b4ead6a6769a84d9f8 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -1592,6 +1592,44 @@ 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 needNotify = 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; + } + needNotify = true; + } + return needNotify; +} + #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 8d27010f65e7086519e1d8f3873b3943fb971b44..42bdfa15d4f64f0d134982efe191c3e573df548c 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 228d075073bcc57c7a18459d8a3a9cfaf758e27e..3175230791a114f01ecac77c6e0d3b7a5f646db5 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 0cb3ea00e9fa3eb5a86dd1202e2ba4ec89192491..700387d2a554053b4f683e4aaeb6b404609c501d 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 c253b23dbea5630f3ca2a10e3e0d60983269646c..a3c09b1df2780be6686b74574c7e3255ba552172 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,177 @@ HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_SignRestartAppFlag_0300, EXPECT_TRUE(record->GetRestartAppFlag()); 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