diff --git a/frameworks/include/background_task_manager.h b/frameworks/include/background_task_manager.h index 5c21c9ae7deba5132ece8e3ff1bfd88a4c2eb348..db9313a653aa1067f2ca2cb668d333cbd818aa4f 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 Get is request mode supported. + * @param isModeSupported is request mode supported. + * @return Returns ERR_OK if success, else failure. + */ + ErrCode IsModeSupported(bool &isModeSupported); + private: bool GetBackgroundTaskManagerProxy(); diff --git a/frameworks/src/background_task_manager.cpp b/frameworks/src/background_task_manager.cpp index 87a5b5aa4bee86f1fc27ea29f889f0d0274fba98..3a40237ff0144106286dad8f0e8d3010fcc9b2df 100644 --- a/frameworks/src/background_task_manager.cpp +++ b/frameworks/src/background_task_manager.cpp @@ -472,6 +472,14 @@ ErrCode BackgroundTaskManager::SuspendContinuousAudioTask(int32_t uid) return proxy_->SuspendContinuousAudioTask(uid); } +ErrCode BackgroundTaskManager::IsModeSupported(bool &isModeSupported) +{ + std::lock_guard lock(mutex_); + GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN + + return proxy_->IsModeSupported(isModeSupported); +} + BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager) : backgroundTaskManager_(backgroundTaskManager) {} diff --git a/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp b/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp index e7c4fac06290c15af7447b2219dcf2f27ac46df9..49ffb2e3c6af8f21885b574b1d0d6c88a344a02c 100644 --- a/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp +++ b/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp @@ -667,5 +667,25 @@ HWTEST_F(BgTaskFrameworkAbnormalUnitTest, BackgroundTaskMgrProxyAbnormalTest_020 MessageParcelHelper::BgTaskFwkAbnormalSetWriteInt32WithParamFlag(false); EXPECT_EQ(backgroundTaskMgrProxy.SuspendContinuousAudioTask(1), ERR_INVALID_DATA); } + +/** + * @tc.name: BackgroundTaskMgrProxyAbnormalTest_021 + * @tc.desc: test IsModeSupported. + * @tc.type: FUNC + * @tc.require: issueICWQV5 + */ +HWTEST_F(BgTaskFrameworkAbnormalUnitTest, BackgroundTaskMgrProxyAbnormalTest_021, TestSize.Level2) +{ + BackgroundTaskMgrProxy backgroundTaskMgrProxy = BackgroundTaskMgrProxy(nullptr); + + MessageParcelHelper::BgTaskFwkAbnormalSetWriteInterfaceTokenFlag(false); + bool isModeSupported = false; + EXPECT_EQ(backgroundTaskMgrProxy.IsModeSupported(isModeSupported), ERR_INVALID_VALUE); + + MessageParcelHelper::BgTaskFwkAbnormalSetWriteInterfaceTokenFlag(true); + MessageParcelHelper::BgTaskFwkAbnormalSetWriteInt32WithParamFlag(false); + EXPECT_EQ(backgroundTaskMgrProxy.IsModeSupported(isModeSupported), ERR_INVALID_DATA); +} + } } diff --git a/frameworks/test/unittest/bgtask_framework_unit_test.cpp b/frameworks/test/unittest/bgtask_framework_unit_test.cpp index 66db2ea41cabb76f0d29cbaee91c03743690d57a..65ac361f331bcb69470ab14e3f731df61cd54808 100644 --- a/frameworks/test/unittest/bgtask_framework_unit_test.cpp +++ b/frameworks/test/unittest/bgtask_framework_unit_test.cpp @@ -927,10 +927,28 @@ HWTEST_F(BgTaskFrameworkUnitTest, SuspendContinuousAudioTask_001, TestSize.Level SystemAbilityManagerClient::GetInstance().action_ = "set_null"; EXPECT_EQ(DelayedSingleton::GetInstance()->SuspendContinuousAudioTask(1), ERR_BGTASK_SERVICE_NOT_CONNECTED); - SystemAbilityManagerClient::GetInstance().action_ = ""; EXPECT_EQ(DelayedSingleton::GetInstance()->SuspendContinuousAudioTask(1), ERR_OK); } + +/** + * @tc.name: IsModeSupported_001 + * @tc.desc: test IsModeSupported. + * @tc.type: FUNC + * @tc.require: issueICWQV5 + */ +HWTEST_F(BgTaskFrameworkUnitTest, IsModeSupported_001, TestSize.Level1) +{ + DelayedSingleton::GetInstance()->proxy_ = nullptr; + SystemAbilityManagerClient::GetInstance().action_ = "set_null"; + bool isModeSupported = false; + EXPECT_EQ(DelayedSingleton::GetInstance()->IsModeSupported(isModeSupported), + ERR_BGTASK_SERVICE_NOT_CONNECTED); + + SystemAbilityManagerClient::GetInstance().action_ = ""; + EXPECT_EQ(DelayedSingleton::GetInstance()->IsModeSupported(isModeSupported), + ERR_OK); +} } } diff --git a/interfaces/innerkits/IBackgroundTaskMgr.idl b/interfaces/innerkits/IBackgroundTaskMgr.idl index c35e02aa3d68c342fe32ba41bf1845e52b0ed4be..b95c4354e642730faf45da80c4b0f3860a8b8791 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 IsModeSupported([out] boolean isModeSupported); } diff --git a/interfaces/innerkits/include/background_task_mgr_helper.h b/interfaces/innerkits/include/background_task_mgr_helper.h index 826e5794fb99e5f1057a4db2c6a2d6d2b5eaee9c..8ca83b1d5845969a2dce9c39d2cc3be80f3d7dc6 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 Get is request mode supported. + * @param isModeSupported is request mode supported. + * @return Returns ERR_OK if success, else failure. + */ + static ErrCode IsModeSupported(bool &isModeSupported); }; } // 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 f0559bfd7a080b79506f36aabd917b7f51397ce8..bec832acf8fc38d0856fc4d54087365df34f38ac 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::IsModeSupported(bool &isModeSupported) +{ + return DelayedSingleton::GetInstance()->IsModeSupported(isModeSupported); +} } // namespace BackgroundTaskMgr } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/include/bg_continuous_task_napi_module.h b/interfaces/kits/napi/include/bg_continuous_task_napi_module.h index bb1cb612a7398726911b9fe895816721d1242802..22a39847dbde81e8f6922cef1484619f811fcf8e 100644 --- a/interfaces/kits/napi/include/bg_continuous_task_napi_module.h +++ b/interfaces/kits/napi/include/bg_continuous_task_napi_module.h @@ -29,6 +29,7 @@ napi_value StopBackgroundRunningThrow(napi_env env, napi_callback_info info); napi_value OnOnContinuousTaskCallback(napi_env env, napi_callback_info info); napi_value OffOnContinuousTaskCallback(napi_env env, napi_callback_info info); napi_value GetAllContinuousTasksThrow(napi_env env, napi_callback_info info); +napi_value IsModeSupported(napi_env env, napi_callback_info info); } // namespace BackgroundTaskMgr } // namespace OHOS #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_CANCEL_SUSPEND_DELAY_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/init_bgtaskmgr.h b/interfaces/kits/napi/include/init_bgtaskmgr.h index d5db321eccc27b93ea284bbd1b1e5186b157df0b..df60531317607d0d2373ea44bf26db471587cac6 100644 --- a/interfaces/kits/napi/include/init_bgtaskmgr.h +++ b/interfaces/kits/napi/include/init_bgtaskmgr.h @@ -34,6 +34,8 @@ napi_value ContinuousTaskCancelReasonInit(napi_env env, napi_value exports); napi_value ContinuousTaskSuspendReasonInit(napi_env env, napi_value exports); napi_value ContinuousTaskModeInit(napi_env env, napi_value exports); napi_value ContinuousTaskSubModeInit(napi_env env, napi_value exports); +napi_value ContinuousTaskRequestInit(napi_env env, napi_value exports); +napi_value ContinuousTaskRequestConstructor(napi_env env, napi_callback_info exports); static napi_value InitApi(napi_env env, napi_value exports); #ifdef __cplusplus diff --git a/interfaces/kits/napi/src/bg_continuous_task_napi_module.cpp b/interfaces/kits/napi/src/bg_continuous_task_napi_module.cpp index 24c51d4089a41fed4006fff247bc3cd37bad2725..ab1c1e9c1bfd429b618a7bdb1827b1cd83387db5 100644 --- a/interfaces/kits/napi/src/bg_continuous_task_napi_module.cpp +++ b/interfaces/kits/napi/src/bg_continuous_task_napi_module.cpp @@ -1384,6 +1384,29 @@ napi_value GetAllContinuousTasks(napi_env env, napi_callback_info info, bool isT return ret; } +napi_value IsModeSupported(napi_env env, napi_callback_info info) +{ + HitraceScoped traceScoped(HITRACE_TAG_OHOS, + "BackgroundTaskManager::ContinuousTask::Napi::IsModeSupported"); + if (env == nullptr) { + BGTASK_LOGE("env param invaild."); + return WrapVoidToJS(env); + } + + bool isModeSupported = false; + ErrCode errCode = BackgroundTaskMgrHelper::IsModeSupported(isModeSupported); + if (errCode != ERR_OK) { + BGTASK_LOGE("IsModeSupported failed."); + Common::HandleErrCode(env, errCode, true); + return nullptr; + } + + napi_value ret {nullptr}; + napi_get_boolean(env, isModeSupported, &ret); + + return ret; +} + napi_value StartBackgroundRunning(napi_env env, napi_callback_info info) { return StartBackgroundRunning(env, info, false); diff --git a/interfaces/kits/napi/src/init_bgtaskmgr.cpp b/interfaces/kits/napi/src/init_bgtaskmgr.cpp index b3d99e365f081e74aaaca594e58edc109095b0bc..4b575bae7d8bab21d4a9bb347778ecad577b5684 100644 --- a/interfaces/kits/napi/src/init_bgtaskmgr.cpp +++ b/interfaces/kits/napi/src/init_bgtaskmgr.cpp @@ -59,6 +59,25 @@ napi_value BackgroundTaskMgrInit(napi_env env, napi_value exports) return exports; } +napi_value ContinuousTaskRequestInit(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("isModeSupported", IsModeSupported), + }; + napi_value cons; + NAPI_CALL(env, napi_define_class(env, "ContinuousTaskRequest", NAPI_AUTO_LENGTH, ContinuousTaskRequestConstructor, + nullptr, sizeof(desc) / sizeof(desc[0]), desc, &cons)); + NAPI_CALL(env, napi_set_named_property(env, exports, "ContinuousTaskRequest", cons)); + return exports; +} + +napi_value ContinuousTaskRequestConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + return thisVar; +} + void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName) { napi_value prop = nullptr; @@ -253,6 +272,7 @@ static napi_value InitApi(napi_env env, napi_value exports) ContinuousTaskSuspendReasonInit(env, exports); ContinuousTaskModeInit(env, exports); ContinuousTaskSubModeInit(env, exports); + ContinuousTaskRequestInit(env, exports); return exports; } 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 950c0b6eff149d4c7905c3be2ef344bade9361d2..eddf25bf08023a3f164084abefa6e95aad735421 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 @@ -1009,5 +1009,17 @@ HWTEST_F(BgTaskClientUnitTest, ContinuousTaskRequest_001, TestSize.Level1) info3->SetWantAgent(std::make_shared()); EXPECT_EQ(info3->GetContinuousTaskId(), 1); } + +/** + * @tc.name: IsModeSupported_001 + * @tc.desc: test IsModeSupported interface. + * @tc.type: FUNC + * @tc.require: issueICWQV5 + */ +HWTEST_F(BgTaskClientUnitTest, IsModeSupported_001, TestSize.Level1) +{ + bool isModeSupported = false; + EXPECT_EQ(BackgroundTaskMgrHelper::IsModeSupported(isModeSupported), ERR_OK); +} } } \ No newline at end of file diff --git a/services/continuous_task/include/bg_continuous_task_mgr.h b/services/continuous_task/include/bg_continuous_task_mgr.h index 921e82991c99be7a818e93a8bfcf0826867b3769..8c729e330c3431988d0785d5a926922627d40aa8 100644 --- a/services/continuous_task/include/bg_continuous_task_mgr.h +++ b/services/continuous_task/include/bg_continuous_task_mgr.h @@ -93,6 +93,7 @@ public: ErrCode GetContinuousTaskApps(std::vector> &list, int32_t uid = -1); ErrCode AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish = false); ErrCode DebugContinuousTaskInner(const sptr &taskParam); + ErrCode IsModeSupported(bool &isModeSupported); bool StopContinuousTaskByUser(const std::string &mapKey); void OnAccountsStateChanged(int32_t id); void OnBundleInfoChanged(const std::string &action, const std::string &bundleName, int32_t uid); diff --git a/services/continuous_task/src/bg_continuous_task_mgr.cpp b/services/continuous_task/src/bg_continuous_task_mgr.cpp index 14fb96571d2d96a2d1b1412dd0a6ff5c263f3200..a2a52cc13ea0f1efa804f977689760b8e21e0a6c 100644 --- a/services/continuous_task/src/bg_continuous_task_mgr.cpp +++ b/services/continuous_task/src/bg_continuous_task_mgr.cpp @@ -2372,6 +2372,18 @@ void BgContinuousTaskMgr::HandleRemoveTaskByMode(uint32_t mode) } } +ErrCode BgContinuousTaskMgr::IsModeSupported(bool &isModeSupported) +{ + if (!isSysReady_.load()) { + BGTASK_LOGW("manager is not ready"); + return ERR_BGTASK_SYS_NOT_READY; + } + + isModeSupported = true; + BGTASK_LOGD("isModeSupported: %{public}d", isModeSupported); + return ERR_OK; +} + void BgContinuousTaskMgr::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) { if (!isSysReady_.load()) { diff --git a/services/core/include/background_task_mgr_service.h b/services/core/include/background_task_mgr_service.h index 324afea0dc4465c7b6159932d72bd0531418534f..66635fb7407998320fe003aec6877ac2d7259bf8 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 IsModeSupported(bool &isModeSupported) 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 3f1f5b325a975ff581b249366bf6fd55a8b40f0f..4ba461095f881bd08cb4f6a7ca98436fb07d25df 100644 --- a/services/core/src/background_task_mgr_service.cpp +++ b/services/core/src/background_task_mgr_service.cpp @@ -546,6 +546,20 @@ ErrCode BackgroundTaskMgrService::SuspendContinuousAudioTask(int32_t uid) return ERR_OK; } +ErrCode BackgroundTaskMgrService::IsModeSupported(bool &isModeSupported) +{ + if (CheckAtomicService()) { + pid_t callingPid = IPCSkeleton::GetCallingPid(); + pid_t callingUid = IPCSkeleton::GetCallingUid(); + BGTASK_LOGE("uid %{public}d pid %{public}d Check atomisc service fail, IsModeSupported not allowed", + callingUid, callingPid); + return ERR_BGTASK_PERMISSION_DENIED; + } + + BgContinuousTaskMgr::GetInstance()->IsModeSupported(isModeSupported); + return ERR_OK; +} + bool BackgroundTaskMgrService::CheckAtomicService() { uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID();