diff --git a/services/el5filekeymanager/include/el5_memory_manager.h b/services/el5filekeymanager/include/el5_memory_manager.h index 1b4fd487fc9a1a380c1247dcebb38673325c645b..7542b415c6052b5fb4a9760f7ae83b707fc7b7d5 100644 --- a/services/el5filekeymanager/include/el5_memory_manager.h +++ b/services/el5filekeymanager/include/el5_memory_manager.h @@ -32,15 +32,15 @@ public: static El5MemoryManager& GetInstance(); void AddFunctionRuningNum(); void DecreaseFunctionRuningNum(); + bool IsFunctionFinished(); bool IsAllowUnloadService(); - void SetIsDelayedToUnload(bool isUnload); - bool IsDelayedToUnload(); + void SetIsAllowUnloadService(bool allow); private: - bool isDelayedToUnload_ = false; + bool isAllowUnloadService_ = false; int32_t callFuncRunningNum_ = 0; std::mutex callNumberMutex_; - std::mutex isDelayedMutex_; + std::mutex isAllowUnloadServiceMutex_; }; } // namespace AccessToken } // namespace Security diff --git a/services/el5filekeymanager/src/el5_filekey_manager_service.cpp b/services/el5filekeymanager/src/el5_filekey_manager_service.cpp index ff1394399ecc32b6e4ac735ac735b7e0395dafbf..db5a49f6f3d429d659c76260c28e5c2837b947e9 100644 --- a/services/el5filekeymanager/src/el5_filekey_manager_service.cpp +++ b/services/el5filekeymanager/src/el5_filekey_manager_service.cpp @@ -143,7 +143,9 @@ void El5FilekeyManagerService::OnAddSystemAbility(int32_t systemAbilityId, const #ifdef THEME_SCREENLOCK_MGR_ENABLE if (systemAbilityId == SCREENLOCK_SERVICE_ID) { // screen is unlocked, sa is called by USER_REMOVED, auto stop in 30s. - if (!ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked()) { + bool isScreenLocked = ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked(); + El5MemoryManager::GetInstance().SetIsAllowUnloadService(!isScreenLocked); + if (!isScreenLocked) { LOG_INFO("Init when screen is unlocked."); PostDelayedUnloadTask(SCREEN_ON_DELAY_TIME); } @@ -166,7 +168,6 @@ void El5FilekeyManagerService::PostDelayedUnloadTask(uint32_t delayedTime) return; } - El5MemoryManager::GetInstance().SetIsDelayedToUnload(true); int32_t ret = systemAbilityManager->UnloadSystemAbility(EL5_FILEKEY_MANAGER_SERVICE_ID); if (ret != ERR_OK) { LOG_ERROR("Unload el5_filekey_manager failed."); diff --git a/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp b/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp index bd2c05e0543d62a19aec724e545d2651d345bc06..f1730a71b369ac752d637e2cd2a878325221e2b0 100644 --- a/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp +++ b/services/el5filekeymanager/src/el5_filekey_manager_service_ability.cpp @@ -26,6 +26,7 @@ namespace { REGISTER_SYSTEM_ABILITY_BY_ID(El5FilekeyManagerServiceAbility, EL5_FILEKEY_MANAGER_SERVICE_ID, false); constexpr int32_t SA_READY_TO_UNLOAD = 0; constexpr int32_t SA_REFUSE_TO_UNLOAD = -1; +const std::string LOW_MEMORY_PARAM = "resourceschedule.memmgr.low.memory.prepare"; } El5FilekeyManagerServiceAbility::El5FilekeyManagerServiceAbility(int32_t systemAbilityId, bool runOnCreate) @@ -56,7 +57,6 @@ void El5FilekeyManagerServiceAbility::OnStart(const SystemAbilityOnDemandReason LOG_ERROR("Failed to init the El5FilekeyManagerService instance."); return; } - El5MemoryManager::GetInstance().SetIsDelayedToUnload(false); AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); AddSystemAbilityListener(SCREENLOCK_SERVICE_ID); @@ -85,10 +85,14 @@ void El5FilekeyManagerServiceAbility::OnStart(const SystemAbilityOnDemandReason int32_t El5FilekeyManagerServiceAbility::OnIdle(const SystemAbilityOnDemandReason &idleReason) { - if (El5MemoryManager::GetInstance().IsDelayedToUnload() || + std::string reasonName = idleReason.GetName(); + LOG_INFO("IldeReason name=%{public}s, value=%{public}s.", reasonName.c_str(), idleReason.GetValue().c_str()); + if (reasonName != LOW_MEMORY_PARAM) { + return SA_READY_TO_UNLOAD; + } + + if (El5MemoryManager::GetInstance().IsFunctionFinished() && El5MemoryManager::GetInstance().IsAllowUnloadService()) { - LOG_INFO("IldeReason name=%{public}s, value=%{public}s.", - idleReason.GetName().c_str(), idleReason.GetValue().c_str()); return SA_READY_TO_UNLOAD; } diff --git a/services/el5filekeymanager/src/el5_filekey_manager_subscriber.cpp b/services/el5filekeymanager/src/el5_filekey_manager_subscriber.cpp index 0759c6a13843ca8e400264401ea745727f69afa2..2a87086fac4f097bc494c4bc9047ab67b45e25b3 100644 --- a/services/el5filekeymanager/src/el5_filekey_manager_subscriber.cpp +++ b/services/el5filekeymanager/src/el5_filekey_manager_subscriber.cpp @@ -18,6 +18,7 @@ #include "common_event_support.h" #include "el5_filekey_manager_log.h" #include "el5_filekey_manager_service.h" +#include "el5_memory_manager.h" #include "want.h" namespace OHOS { @@ -40,6 +41,7 @@ void El5FilekeyManagerSubscriber::OnReceiveEvent(const EventFwk::CommonEventData if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { DelayedSingleton::GetInstance()->PostDelayedUnloadTask(SCREEN_ON_DELAY_TIME); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) { + El5MemoryManager::GetInstance().SetIsAllowUnloadService(false); // cancel unload task when screen is locked DelayedSingleton::GetInstance()->CancelDelayedUnloadTask(); } diff --git a/services/el5filekeymanager/src/el5_memory_manager.cpp b/services/el5filekeymanager/src/el5_memory_manager.cpp index 66a2a3bd4792bc051f12e29ecc4210c4d488da75..3dc656c8241aadec388436cd862b50792c2737a9 100644 --- a/services/el5filekeymanager/src/el5_memory_manager.cpp +++ b/services/el5filekeymanager/src/el5_memory_manager.cpp @@ -53,7 +53,7 @@ void El5MemoryManager::DecreaseFunctionRuningNum() callFuncRunningNum_--; } -bool El5MemoryManager::IsAllowUnloadService() +bool El5MemoryManager::IsFunctionFinished() { std::lock_guard lock(callNumberMutex_); if (callFuncRunningNum_ == 0) { @@ -63,16 +63,17 @@ bool El5MemoryManager::IsAllowUnloadService() return false; } -void El5MemoryManager::SetIsDelayedToUnload(bool isUnload) +bool El5MemoryManager::IsAllowUnloadService() { - std::lock_guard lock(isDelayedMutex_); - isDelayedToUnload_ = isUnload; + std::lock_guard lock(isAllowUnloadServiceMutex_); + return isAllowUnloadService_; } -bool El5MemoryManager::IsDelayedToUnload() +void El5MemoryManager::SetIsAllowUnloadService(bool allow) { - std::lock_guard lock(isDelayedMutex_); - return isDelayedToUnload_; + LOG_INFO("The allow flag is (%{public}d).", allow); + std::lock_guard lock(isAllowUnloadServiceMutex_); + isAllowUnloadService_ = allow; } } // namespace AccessToken } // namespace Security diff --git a/services/el5filekeymanager/test/src/el5_filekey_memory_manager_unittest.cpp b/services/el5filekeymanager/test/src/el5_filekey_memory_manager_unittest.cpp index 23f08b72c7b0eba34a17007688a04e1075405ec4..ea2150a34dca8249f4c3d323eb2a94d144866637 100644 --- a/services/el5filekeymanager/test/src/el5_filekey_memory_manager_unittest.cpp +++ b/services/el5filekeymanager/test/src/el5_filekey_memory_manager_unittest.cpp @@ -25,6 +25,7 @@ constexpr int32_t MAX_RUNNING_NUM = 256; constexpr int64_t EXTRA_PARAM = 3; constexpr int32_t SA_READY_TO_UNLOAD = 0; constexpr int32_t SA_REFUSE_TO_UNLOAD = -1; +const std::string LOW_MEMORY_PARAM = "resourceschedule.memmgr.low.memory.prepare"; } // namespace void El5FilekeyMemoryManagerTest::SetUp() @@ -35,34 +36,29 @@ void El5FilekeyMemoryManagerTest::SetUp() /** * @tc.name: MemoryManagerTest001 - * @tc.desc: test AddFunctionRuningNum and DecreaseFunctionRuningNum. + * @tc.desc: test OnIdle * @tc.type: FUNC * @tc.require: issueICIZZE */ HWTEST_F(El5FilekeyMemoryManagerTest, MemoryManagerTest001, TestSize.Level1) { - El5MemoryManager::GetInstance().SetIsDelayedToUnload(false); - OHOS::SystemAbilityOnDemandReason reason(OHOS::OnDemandReasonId::PARAM, "test", "true", EXTRA_PARAM); + OHOS::SystemAbilityOnDemandReason reason1(OHOS::OnDemandReasonId::PARAM, "test", "true", EXTRA_PARAM); + EXPECT_EQ(SA_READY_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason1)); + + El5MemoryManager::GetInstance().SetIsAllowUnloadService(false); + OHOS::SystemAbilityOnDemandReason reason2(OHOS::OnDemandReasonId::PARAM, LOW_MEMORY_PARAM, "true", EXTRA_PARAM); + EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2)); + for (int32_t i = 0; i <= MAX_RUNNING_NUM + 1; i++) { El5MemoryManager::GetInstance().AddFunctionRuningNum(); } - EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason)); + EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2)); + + El5MemoryManager::GetInstance().SetIsAllowUnloadService(true); + EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2)); + for (int32_t i = 0; i <= MAX_RUNNING_NUM + 1; i++) { El5MemoryManager::GetInstance().DecreaseFunctionRuningNum(); } - EXPECT_EQ(SA_READY_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason)); -} - -/** - * @tc.name: MemoryManagerTest002 - * @tc.desc: test SetIsDelayedToUnload and IsDelayedToUnload. - * @tc.type: FUNC - * @tc.require: issueICIZZE - */ -HWTEST_F(El5FilekeyMemoryManagerTest, MemoryManagerTest002, TestSize.Level1) -{ - El5MemoryManager::GetInstance().SetIsDelayedToUnload(true); - OHOS::SystemAbilityOnDemandReason reason(OHOS::OnDemandReasonId::PARAM, "test", "true", EXTRA_PARAM); - EXPECT_EQ(SA_READY_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason)); - El5MemoryManager::GetInstance().SetIsDelayedToUnload(false); + EXPECT_EQ(SA_READY_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2)); }