From 01f27887c3ad263f85b1781348cced208fc07ba7 Mon Sep 17 00:00:00 2001 From: pancras Date: Sat, 13 Sep 2025 14:41:08 +0800 Subject: [PATCH] support taskKeeping processes Signed-off-by: pancras --- frameworks/include/background_task_manager.h | 7 +++++++ frameworks/src/background_task_manager.cpp | 8 ++++++++ interfaces/innerkits/IBackgroundTaskMgr.idl | 1 + .../innerkits/include/background_task_mgr_helper.h | 7 +++++++ .../innerkits/src/background_task_mgr_helper.cpp | 5 +++++ .../bgtask_client_unit_test.cpp | 12 ++++++++++++ services/common/include/bgtask_config.h | 3 ++- services/common/src/bgtask_config.cpp | 11 ++++++++++- .../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, 76 insertions(+), 2 deletions(-) diff --git a/frameworks/include/background_task_manager.h b/frameworks/include/background_task_manager.h index 9981b03e..42739cc6 100644 --- a/frameworks/include/background_task_manager.h +++ b/frameworks/include/background_task_manager.h @@ -274,6 +274,13 @@ public: */ ErrCode IsModeSupported(ContinuousTaskParam &taskParam); + /** + * @brief Set supported taskKeeping processes. + * @param processSet process set. + * @return Returns ERR_OK if success, else failure. + */ + ErrCode SetSupportedTaskKeepingProcesses(const std::set &processSet); + private: bool GetBackgroundTaskManagerProxy(); diff --git a/frameworks/src/background_task_manager.cpp b/frameworks/src/background_task_manager.cpp index 366f037f..27022fbd 100644 --- a/frameworks/src/background_task_manager.cpp +++ b/frameworks/src/background_task_manager.cpp @@ -480,6 +480,14 @@ ErrCode BackgroundTaskManager::IsModeSupported(ContinuousTaskParam &taskParam) return proxy_->IsModeSupported(taskParam); } +ErrCode BackgroundTaskManager::SetSupportedTaskKeepingProcesses(const std::set &processSet) +{ + std::lock_guard lock(mutex_); + GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN + + return proxy_->SetSupportedTaskKeepingProcesses(processSet); +} + BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager) : backgroundTaskManager_(backgroundTaskManager) {} diff --git a/interfaces/innerkits/IBackgroundTaskMgr.idl b/interfaces/innerkits/IBackgroundTaskMgr.idl index 81f848f9..b0c4015a 100644 --- a/interfaces/innerkits/IBackgroundTaskMgr.idl +++ b/interfaces/innerkits/IBackgroundTaskMgr.idl @@ -55,4 +55,5 @@ interface OHOS.BackgroundTaskMgr.IBackgroundTaskMgr { void SetBgTaskConfig([in] String configData, [in] int sourceType); [oneway] void SuspendContinuousAudioTask([in] int uid); void IsModeSupported([in] ContinuousTaskParam taskParam); + void SetSupportedTaskKeepingProcesses([in] Set processSet); } diff --git a/interfaces/innerkits/include/background_task_mgr_helper.h b/interfaces/innerkits/include/background_task_mgr_helper.h index ea440597..0c4a7ccd 100644 --- a/interfaces/innerkits/include/background_task_mgr_helper.h +++ b/interfaces/innerkits/include/background_task_mgr_helper.h @@ -217,6 +217,13 @@ public: * @return Returns ERR_OK if success, else failure. */ static ErrCode IsModeSupported(ContinuousTaskParam &taskParam); + + /** + * @brief Set supported taskKeeping processes. + * @param processSet process set. + * @return Returns ERR_OK if success, else failure. + */ + static ErrCode SetSupportedTaskKeepingProcesses(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 3e003f07..2bc74b1c 100644 --- a/interfaces/innerkits/src/background_task_mgr_helper.cpp +++ b/interfaces/innerkits/src/background_task_mgr_helper.cpp @@ -144,5 +144,10 @@ ErrCode BackgroundTaskMgrHelper::IsModeSupported(ContinuousTaskParam &taskParam) { return DelayedSingleton::GetInstance()->IsModeSupported(taskParam); } + +ErrCode BackgroundTaskMgrHelper::SetSupportedTaskKeepingProcesses(const std::set &processSet) +{ + return DelayedSingleton::GetInstance()->SetSupportedTaskKeepingProcesses(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 a8ce8ad3..ee8ccdcb 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 @@ -1021,5 +1021,17 @@ HWTEST_F(BgTaskClientUnitTest, IsModeSupported_001, TestSize.Level1) ContinuousTaskParam taskParam = ContinuousTaskParam(); EXPECT_EQ(BackgroundTaskMgrHelper::IsModeSupported(taskParam), ERR_OK); } + +/** + * @tc.name: SetSupportedTaskKeepingProcesses_001 + * @tc.desc: test SetSupportedTaskKeepingProcesses. + * @tc.type: FUNC + * @tc.require: issueICVQZF + */ +HWTEST_F(BgTaskClientUnitTest, SetSupportedTaskKeepingProcesses_001, TestSize.Level1) +{ + std::set processSet; + EXPECT_EQ(BackgroundTaskMgrHelper::SetSupportedTaskKeepingProcesses(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..052929b1 100644 --- a/services/common/include/bgtask_config.h +++ b/services/common/include/bgtask_config.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -32,6 +32,7 @@ public: bool IsTaskKeepingExemptedQuatoApp(const std::string &bundleName); int32_t GetTransientTaskExemptedQuato(); bool AddExemptedQuatoData(const std::string &configData, int32_t sourceType); + void SetSupportedTaskKeepingProcesses(const std::set &processSet); private: void LoadConfigFile(); diff --git a/services/common/src/bgtask_config.cpp b/services/common/src/bgtask_config.cpp index 2f45999c..bd2330b2 100644 --- a/services/common/src/bgtask_config.cpp +++ b/services/common/src/bgtask_config.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -201,5 +201,14 @@ int32_t BgtaskConfig::GetTransientTaskExemptedQuato() std::lock_guard lock(configMutex_); return transientTaskExemptedQuato_; } + +void BgtaskConfig::SetSupportedTaskKeepingProcesses(const std::set &processSet) +{ + std::lock_guard lock(configMutex_); + taskKeepingExemptedQuatoList_.insert(processSet.begin(), processSet.end()); + 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 47ff142e..894ee959 100644 --- a/services/core/include/background_task_mgr_service.h +++ b/services/core/include/background_task_mgr_service.h @@ -95,6 +95,7 @@ public: ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType) override; ErrCode SuspendContinuousAudioTask(int32_t uid) override; ErrCode IsModeSupported(const ContinuousTaskParam &taskParam) override; + ErrCode SetSupportedTaskKeepingProcesses(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 2fef7484..b8f56002 100644 --- a/services/core/src/background_task_mgr_service.cpp +++ b/services/core/src/background_task_mgr_service.cpp @@ -560,6 +560,15 @@ ErrCode BackgroundTaskMgrService::IsModeSupported(const ContinuousTaskParam &tas return BgContinuousTaskMgr::GetInstance()->IsModeSupported(paramPtr); } +ErrCode BackgroundTaskMgrService::SetSupportedTaskKeepingProcesses(const std::set &processSet) +{ + if (!CheckCallingToken() || !CheckCallingProcess()) { + return ERR_BGTASK_PERMISSION_DENIED; + } + DelayedSingleton::GetInstance()->SetSupportedTaskKeepingProcesses(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..4c2560c2 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 SetSupportedTaskKeepingProcesses. + * @tc.type: FUNC + * @tc.require: issueICVQZF + */ +HWTEST_F(BgTaskManagerUnitTest, BgTaskManagerUnitTest_056, TestSize.Level1) +{ + std::set processSet; + processSet.insert("com.test.app"); + DelayedSingleton::GetInstance()->SetSupportedTaskKeepingProcesses(processSet); + EXPECT_EQ(DelayedSingleton::GetInstance()->taskKeepingExemptedQuatoList_.empty(), false); +} } } -- Gitee