From f2242712fc585f8ec2ae3aea25f5753a413ee7b4 Mon Sep 17 00:00:00 2001 From: pancras Date: Sat, 6 Sep 2025 09:51:09 +0800 Subject: [PATCH 1/2] support task keeping exemption Signed-off-by: pancras --- frameworks/include/background_task_manager.h | 7 +++++++ frameworks/src/background_task_manager.cpp | 7 +++++++ interfaces/innerkits/IBackgroundTaskMgr.idl | 1 + .../innerkits/include/background_task_mgr_helper.h | 6 ++++++ .../innerkits/src/background_task_mgr_helper.cpp | 5 +++++ .../bgtask_client_unit_test.cpp | 12 ++++++++++++ services/common/include/bgtask_config.h | 7 ++----- services/common/src/bgtask_config.cpp | 9 +++++++++ .../core/include/background_task_mgr_service.h | 1 + services/core/src/background_task_mgr_service.cpp | 9 +++++++++ .../test/unittest/bgtask_manager_unit_test.cpp | 14 ++++++++++++++ 11 files changed, 73 insertions(+), 5 deletions(-) diff --git a/frameworks/include/background_task_manager.h b/frameworks/include/background_task_manager.h index 5c21c9ae..f05a2ff3 100644 --- a/frameworks/include/background_task_manager.h +++ b/frameworks/include/background_task_manager.h @@ -267,6 +267,13 @@ public: */ ErrCode SuspendContinuousAudioTask(int32_t uid); + /** + * @brief Set taskKeeping exempted processes. + * @param processSet exemption process set. + * @return Returns ERR_OK if success, else failure. + */ + ErrCode SetTaskKeepingExemptedProcesses(const std::set &processSet); + private: bool GetBackgroundTaskManagerProxy(); diff --git a/frameworks/src/background_task_manager.cpp b/frameworks/src/background_task_manager.cpp index 87a5b5aa..8e470767 100644 --- a/frameworks/src/background_task_manager.cpp +++ b/frameworks/src/background_task_manager.cpp @@ -472,6 +472,13 @@ ErrCode BackgroundTaskManager::SuspendContinuousAudioTask(int32_t uid) return proxy_->SuspendContinuousAudioTask(uid); } +ErrCode BackgroundTaskManager::SetTaskKeepingExemptedProcesses(const std::set &processSet) +{ + std::lock_guard lock(mutex_); + GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN + return proxy_->SetTaskKeepingExemptedProcesses(processSet); +} + BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager) : backgroundTaskManager_(backgroundTaskManager) {} diff --git a/interfaces/innerkits/IBackgroundTaskMgr.idl b/interfaces/innerkits/IBackgroundTaskMgr.idl index c35e02aa..56dba10b 100644 --- a/interfaces/innerkits/IBackgroundTaskMgr.idl +++ b/interfaces/innerkits/IBackgroundTaskMgr.idl @@ -54,4 +54,5 @@ interface OHOS.BackgroundTaskMgr.IBackgroundTaskMgr { void AVSessionNotifyUpdateNotification([in] int uid, [in] int pid, [in] boolean isPublish); void SetBgTaskConfig([in] String configData, [in] int sourceType); [oneway] void SuspendContinuousAudioTask([in] int uid); + void SetTaskKeepingExemptedProcesses([in] Set processSet); } diff --git a/interfaces/innerkits/include/background_task_mgr_helper.h b/interfaces/innerkits/include/background_task_mgr_helper.h index 826e5794..a5624412 100644 --- a/interfaces/innerkits/include/background_task_mgr_helper.h +++ b/interfaces/innerkits/include/background_task_mgr_helper.h @@ -211,6 +211,12 @@ public: */ static ErrCode SuspendContinuousAudioTask(int32_t uid); + /** + * @brief Set taskKeeping exempted processes. + * @param processSet exemption process set. + * @return Returns ERR_OK if success, else failure. + */ + static ErrCode SetTaskKeepingExemptedProcesses(const std::set &processSet); }; } // namespace BackgroundTaskMgr } // namespace OHOS diff --git a/interfaces/innerkits/src/background_task_mgr_helper.cpp b/interfaces/innerkits/src/background_task_mgr_helper.cpp index f0559bfd..9e34c9fc 100644 --- a/interfaces/innerkits/src/background_task_mgr_helper.cpp +++ b/interfaces/innerkits/src/background_task_mgr_helper.cpp @@ -139,5 +139,10 @@ ErrCode BackgroundTaskMgrHelper::SuspendContinuousAudioTask(int32_t uid) { return DelayedSingleton::GetInstance()->SuspendContinuousAudioTask(uid); } + +ErrCode BackgroundTaskMgrHelper::SetTaskKeepingExemptedProcesses(const std::set &processSet) +{ + return DelayedSingleton::GetInstance()->SetTaskKeepingExemptedProcesses(processSet); +} } // namespace BackgroundTaskMgr } // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp b/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp index dd8e6f7a..df9f1010 100644 --- a/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp +++ b/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp @@ -1007,5 +1007,17 @@ HWTEST_F(BgTaskClientUnitTest, ContinuousTaskRequest_001, TestSize.Level1) info3->SetWantAgent(std::make_shared()); EXPECT_EQ(info3->GetContinuousTaskId(), 1); } + +/** + * @tc.name: SetTaskKeepingExemptedProcesses_001 + * @tc.desc: test SetTaskKeepingExemptedProcesses. + * @tc.type: FUNC + * @tc.require: issueICVQZF + */ +HWTEST_F(BgTaskClientUnitTest, SetTaskKeepingExemptedProcesses_001, TestSize.Level1) +{ + std::set processSet; + EXPECT_EQ(BackgroundTaskMgrHelper::SetTaskKeepingExemptedProcesses(processSet), ERR_OK); +} } } \ No newline at end of file diff --git a/services/common/include/bgtask_config.h b/services/common/include/bgtask_config.h index 780ee84f..6fb28aa9 100644 --- a/services/common/include/bgtask_config.h +++ b/services/common/include/bgtask_config.h @@ -32,6 +32,7 @@ public: bool IsTaskKeepingExemptedQuatoApp(const std::string &bundleName); int32_t GetTransientTaskExemptedQuato(); bool AddExemptedQuatoData(const std::string &configData, int32_t sourceType); + void SetTaskKeepingExemptedProcesses(const std::set &processSet); private: void LoadConfigFile(); @@ -43,11 +44,7 @@ private: bool isInit_ = false; std::set transientTaskExemptedQuatoList_ {}; std::set transientTaskCloudExemptedQuatoList_ {}; - std::set taskKeepingExemptedQuatoList_ = { - {"com.sankuai.hmeituan.itakeawaybiz"}, - {"cn.wps.mobileoffice.hap.ent"}, - {"com.estrongs.hm.pop"}, - }; + std::set taskKeepingExemptedQuatoList_ = {}; int32_t transientTaskExemptedQuato_ = 10 * 1000; // 10s std::mutex configMutex_; }; diff --git a/services/common/src/bgtask_config.cpp b/services/common/src/bgtask_config.cpp index 2f45999c..43e709c4 100644 --- a/services/common/src/bgtask_config.cpp +++ b/services/common/src/bgtask_config.cpp @@ -201,5 +201,14 @@ int32_t BgtaskConfig::GetTransientTaskExemptedQuato() std::lock_guard lock(configMutex_); return transientTaskExemptedQuato_; } + +void BgtaskConfig::SetTaskKeepingExemptedProcesses(const std::set &processSet) +{ + std::lock_guard lock(configMutex_); + taskKeepingExemptedQuatoList_ = processSet; + for (const auto &item : taskKeepingExemptedQuatoList_) { + BGTASK_LOGI("taskKeeping Exemption proc: %{public}s", item.c_str()); + } +} } } \ No newline at end of file diff --git a/services/core/include/background_task_mgr_service.h b/services/core/include/background_task_mgr_service.h index 324afea0..c4d024c5 100644 --- a/services/core/include/background_task_mgr_service.h +++ b/services/core/include/background_task_mgr_service.h @@ -94,6 +94,7 @@ public: ErrCode AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish = false) override; ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType) override; ErrCode SuspendContinuousAudioTask(int32_t uid) override; + ErrCode SetTaskKeepingExemptedProcesses(const std::set &processSet) override; int32_t Dump(int32_t fd, const std::vector &args) override; void ForceCancelSuspendDelay(int32_t requestId); diff --git a/services/core/src/background_task_mgr_service.cpp b/services/core/src/background_task_mgr_service.cpp index 3f1f5b32..98eb4e31 100644 --- a/services/core/src/background_task_mgr_service.cpp +++ b/services/core/src/background_task_mgr_service.cpp @@ -546,6 +546,15 @@ ErrCode BackgroundTaskMgrService::SuspendContinuousAudioTask(int32_t uid) return ERR_OK; } +ErrCode BackgroundTaskMgrService::SetTaskKeepingExemptedProcesses(const std::set &processSet) +{ + if (!CheckCallingToken() || !CheckCallingProcess()) { + return ERR_BGTASK_PERMISSION_DENIED; + } + DelayedSingleton::GetInstance()->SetTaskKeepingExemptedProcesses(processSet); + return ERR_OK; +} + bool BackgroundTaskMgrService::CheckAtomicService() { uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID(); diff --git a/services/test/unittest/bgtask_manager_unit_test.cpp b/services/test/unittest/bgtask_manager_unit_test.cpp index 798f152f..43445265 100644 --- a/services/test/unittest/bgtask_manager_unit_test.cpp +++ b/services/test/unittest/bgtask_manager_unit_test.cpp @@ -817,5 +817,19 @@ HWTEST_F(BgTaskManagerUnitTest, BgTaskManagerUnitTest_055, TestSize.Level0) bgTransientTaskMgr_->keyInfoMap_[1] = keyInfo; EXPECT_EQ(bgTransientTaskMgr_->GetAllTransientTasks(remainingQuota, list), ERR_OK); } + +/** + * @tc.name: BgTaskManagerUnitTest_056 + * @tc.desc: test SetTaskKeepingExemptedProcesses. + * @tc.type: FUNC + * @tc.require: issueICVQZF + */ +HWTEST_F(BgTaskManagerUnitTest, BgTaskManagerUnitTest_056, TestSize.Level1) +{ + std::set processSet; + processSet.insert("com.test.app"); + DelayedSingleton::GetInstance()->SetTaskKeepingExemptedProcesses(processSet); + EXPECT_EQ(DelayedSingleton::GetInstance()->taskKeepingExemptedQuatoList_.empty(), false); +} } } -- Gitee From 539b79d1779f8ba6d354e8cae206fbaf8203ae68 Mon Sep 17 00:00:00 2001 From: PancrasLiu Date: Sat, 6 Sep 2025 11:36:11 +0000 Subject: [PATCH 2/2] support task keeping exemption Signed-off-by: PancrasLiu --- services/common/include/bgtask_config.h | 6 +++++- services/common/src/bgtask_config.cpp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/services/common/include/bgtask_config.h b/services/common/include/bgtask_config.h index 6fb28aa9..a28a45ba 100644 --- a/services/common/include/bgtask_config.h +++ b/services/common/include/bgtask_config.h @@ -44,7 +44,11 @@ private: bool isInit_ = false; std::set transientTaskExemptedQuatoList_ {}; std::set transientTaskCloudExemptedQuatoList_ {}; - std::set taskKeepingExemptedQuatoList_ = {}; + std::set taskKeepingExemptedQuatoList_ = { + {"com.sankuai.hmeituan.itakeawaybiz"}, + {"cn.wps.mobileoffice.hap.ent"}, + {"com.estrongs.hm.pop"}, + }; int32_t transientTaskExemptedQuato_ = 10 * 1000; // 10s std::mutex configMutex_; }; diff --git a/services/common/src/bgtask_config.cpp b/services/common/src/bgtask_config.cpp index 43e709c4..2388712c 100644 --- a/services/common/src/bgtask_config.cpp +++ b/services/common/src/bgtask_config.cpp @@ -205,7 +205,7 @@ int32_t BgtaskConfig::GetTransientTaskExemptedQuato() void BgtaskConfig::SetTaskKeepingExemptedProcesses(const std::set &processSet) { std::lock_guard lock(configMutex_); - taskKeepingExemptedQuatoList_ = processSet; + taskKeepingExemptedQuatoList_.insert(processSet.begin(), processSet.end()); for (const auto &item : taskKeepingExemptedQuatoList_) { BGTASK_LOGI("taskKeeping Exemption proc: %{public}s", item.c_str()); } -- Gitee