diff --git a/interfaces/innerkits/include/continuous_task_param.h b/interfaces/innerkits/include/continuous_task_param.h index b0017bbe7bb2904af10ff780ffeec394a6916527..de4c637ba5eb950829a694bc2d8c9856bf4705cf 100644 --- a/interfaces/innerkits/include/continuous_task_param.h +++ b/interfaces/innerkits/include/continuous_task_param.h @@ -39,6 +39,7 @@ struct ContinuousTaskParam : public Parcelable { std::vector bgSubModeIds_ {}; bool isCombinedTaskNotification_ {false}; int32_t combinedNotificationTaskId_ {-1}; + int32_t updateTaskId_ {-1}; bool isByRequestObject_ {false}; int32_t notificationId_ {-1}; // out int32_t continuousTaskId_ {-1}; // out @@ -47,9 +48,14 @@ struct ContinuousTaskParam : public Parcelable { ContinuousTaskParam(bool isNewApi, uint32_t bgModeId, const std::shared_ptr wantAgent, const std::string &abilityName, const sptr abilityToken, const std::string &appName, bool isBatchApi = false, - const std::vector &bgModeIds = {}, int32_t abilityId = -1); + const std::vector &bgModeIds = {}, int32_t abilityId = -1) + : isNewApi_(isNewApi), bgModeId_(bgModeId), wantAgent_(wantAgent), abilityName_(abilityName), + abilityToken_(abilityToken), appName_(appName), isBatchApi_(isBatchApi), bgModeIds_(bgModeIds), + abilityId_(abilityId) {} bool ReadFromParcel(Parcel &parcel); + bool ReadFromParcelNewApi(Parcel &parcel); bool Marshalling(Parcel &parcel) const override; + bool MarshallingNewApi(Parcel &parcel) const; static ContinuousTaskParam *Unmarshalling(Parcel &parcel); }; diff --git a/interfaces/innerkits/src/continuous_task_param.cpp b/interfaces/innerkits/src/continuous_task_param.cpp index 2b43494bf05c83293eedc56292e592b253b3c511..dba126ba4ba4c7e667ff996225cb278ec18909e4 100644 --- a/interfaces/innerkits/src/continuous_task_param.cpp +++ b/interfaces/innerkits/src/continuous_task_param.cpp @@ -21,28 +21,6 @@ namespace OHOS { namespace BackgroundTaskMgr { -ContinuousTaskParam::ContinuousTaskParam(bool isNewApi, uint32_t bgModeId, - const std::shared_ptr wantAgent, const std::string &abilityName, - const sptr abilityToken, const std::string &appName, bool isBatchApi, - const std::vector &bgModeIds, int32_t abilityId) - : isNewApi_(isNewApi), bgModeId_(bgModeId), wantAgent_(wantAgent), abilityName_(abilityName), - abilityToken_(abilityToken), appName_(appName), isBatchApi_(isBatchApi), bgModeIds_(bgModeIds), - abilityId_(abilityId) { - if (isBatchApi_ && bgModeIds_.size() > 0) { - auto findNonDataTransfer = [](const auto &target) { - return target != BackgroundMode::DATA_TRANSFER; - }; - auto iter = std::find_if(bgModeIds_.begin(), bgModeIds_.end(), findNonDataTransfer); - if (iter != bgModeIds_.end()) { - bgModeId_ = *iter; - } else { - bgModeId_ = bgModeIds_[0]; - } - } else { - bgModeIds_.push_back(bgModeId); - } -} - bool ContinuousTaskParam::ReadFromParcel(Parcel &parcel) { if (!parcel.ReadBool(isNewApi_)) { @@ -93,16 +71,32 @@ bool ContinuousTaskParam::ReadFromParcel(Parcel &parcel) BGTASK_LOGE("Failed to read the abilityId"); return false; } + if (!ReadFromParcelNewApi(parcel)) { + return false; + } + return true; +} + +bool ContinuousTaskParam::ReadFromParcelNewApi(Parcel &parcel) +{ if (!parcel.ReadUInt32Vector(&bgSubModeIds_)) { + BGTASK_LOGE("Failed to read the bgSubModeIds"); return false; } if (!parcel.ReadBool(isCombinedTaskNotification_)) { + BGTASK_LOGE("Failed to read the isCombinedTaskNotification"); return false; } if (!parcel.ReadInt32(combinedNotificationTaskId_)) { + BGTASK_LOGE("Failed to read the combinedNotificationTaskId"); + return false; + } + if (!parcel.ReadInt32(updateTaskId_)) { + BGTASK_LOGE("Failed to read the updateTaskId"); return false; } if (!parcel.ReadBool(isByRequestObject_)) { + BGTASK_LOGE("Failed to read the isByRequestObject"); return false; } return true; @@ -214,21 +208,38 @@ bool ContinuousTaskParam::Marshalling(Parcel &parcel) const BGTASK_LOGE("Failed to write the abilityId"); return false; } + if (!MarshallingNewApi(parcel)) { + return false; + } + return true; +} + +bool ContinuousTaskParam::MarshallingNewApi(Parcel &parcel) const +{ if (!parcel.WriteUInt32Vector(bgSubModeIds_)) { + BGTASK_LOGE("Failed to write the bgSubModeIds"); return false; } if (!parcel.WriteBool(isCombinedTaskNotification_)) { + BGTASK_LOGE("Failed to write the isCombinedTaskNotification"); return false; } if (!parcel.WriteInt32(combinedNotificationTaskId_)) { + BGTASK_LOGE("Failed to write the combinedNotificationTaskId"); + return false; + } + if (!parcel.WriteInt32(updateTaskId_)) { + BGTASK_LOGE("Failed to write the updateTaskId"); return false; } if (!parcel.WriteBool(isByRequestObject_)) { + BGTASK_LOGE("Failed to write the isByRequestObject"); return false; } return true; } + bool ContinuousTaskParamForInner::Marshalling(Parcel &parcel) const { if (!parcel.WriteBool(isStart_)) { 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 1c90be193b3886509b1d18aa974bb39cf57a5fd5..39466dfe3ce25993d813e38675695b1869c00d24 100644 --- a/interfaces/kits/napi/src/bg_continuous_task_napi_module.cpp +++ b/interfaces/kits/napi/src/bg_continuous_task_napi_module.cpp @@ -629,27 +629,8 @@ bool StartBackgroundRunningCheckParamBeforeSubmit(napi_env env, napi_value *argv return true; } -bool StartBackgroundRunningCheckRequest(napi_env env, napi_value *argv, bool isThrow, - AsyncCallbackInfo *asyncCallbackInfo) +bool StartBackgroundRunningCheckModes(napi_env env, bool isThrow, AsyncCallbackInfo *asyncCallbackInfo) { - if (GetAbilityContext(env, argv[0], asyncCallbackInfo->abilityContext) == nullptr) { - BGTASK_LOGE("Get ability context failed"); - Common::HandleParamErr(env, ERR_CONTEXT_NULL_OR_TYPE_ERR, isThrow); - asyncCallbackInfo->errCode = ERR_CONTEXT_NULL_OR_TYPE_ERR; - return false; - } - const std::shared_ptr info = asyncCallbackInfo->abilityContext->GetAbilityInfo(); - if (info == nullptr) { - BGTASK_LOGE("abilityInfo is null"); - Common::HandleParamErr(env, ERR_ABILITY_INFO_EMPTY, isThrow); - asyncCallbackInfo->errCode = ERR_ABILITY_INFO_EMPTY; - return false; - } - if (!Common::GetContinuousTaskRequest(env, argv[1], asyncCallbackInfo->request)) { - Common::HandleErrCode(env, ERR_BGTASK_CONTINUOUS_REQUEST_NULL_OR_TYPE, true); - asyncCallbackInfo->errCode = ERR_BGTASK_CONTINUOUS_REQUEST_NULL_OR_TYPE; - return false; - } std::vector continuousTaskModes = asyncCallbackInfo->request->GetContinuousTaskModes(); std::vector continuousTaskSubmodes = asyncCallbackInfo->request->GetContinuousTaskSubmodes(); if (continuousTaskModes.size() == 0 || continuousTaskSubmodes.size() == 0) { @@ -687,6 +668,55 @@ bool StartBackgroundRunningCheckRequest(napi_env env, napi_value *argv, bool isT return true; } +bool StartBackgroundRunningCheckRequest(napi_env env, napi_value *argv, bool isThrow, + AsyncCallbackInfo *asyncCallbackInfo) +{ + if (GetAbilityContext(env, argv[0], asyncCallbackInfo->abilityContext) == nullptr) { + BGTASK_LOGE("Get ability context failed"); + Common::HandleParamErr(env, ERR_CONTEXT_NULL_OR_TYPE_ERR, isThrow); + asyncCallbackInfo->errCode = ERR_CONTEXT_NULL_OR_TYPE_ERR; + return false; + } + const std::shared_ptr info = asyncCallbackInfo->abilityContext->GetAbilityInfo(); + if (info == nullptr) { + BGTASK_LOGE("abilityInfo is null"); + Common::HandleParamErr(env, ERR_ABILITY_INFO_EMPTY, isThrow); + asyncCallbackInfo->errCode = ERR_ABILITY_INFO_EMPTY; + return false; + } + if (!Common::GetContinuousTaskRequest(env, argv[1], asyncCallbackInfo->request)) { + Common::HandleErrCode(env, ERR_BGTASK_CONTINUOUS_REQUEST_NULL_OR_TYPE, true); + asyncCallbackInfo->errCode = ERR_BGTASK_CONTINUOUS_REQUEST_NULL_OR_TYPE; + return false; + } + if (!StartBackgroundRunningCheckModes(env, isThrow, asyncCallbackInfo)) { + BGTASK_LOGE("check continuous modes fail"); + return false; + } + return true; +} + +bool UpdateBackgroundRunningCheckParam(napi_env env, napi_value *argv, AsyncCallbackInfo *asyncCallbackInfo, + bool isThrow) +{ + // argv[0] : context : AbilityContext + if (GetAbilityContext(env, argv[0], asyncCallbackInfo->abilityContext) == nullptr) { + BGTASK_LOGE("Get ability context failed"); + Common::HandleParamErr(env, ERR_CONTEXT_NULL_OR_TYPE_ERR, isThrow); + asyncCallbackInfo->errCode = ERR_CONTEXT_NULL_OR_TYPE_ERR; + return false; + } + + // argv[1] : bgMode : BackgroundMode + if (GetBackgroundMode(env, argv[1], asyncCallbackInfo) == nullptr) { + BGTASK_LOGE("input bgmode param not number"); + Common::HandleParamErr(env, ERR_BGMODE_NULL_OR_TYPE_ERR, isThrow); + asyncCallbackInfo->errCode = ERR_BGMODE_NULL_OR_TYPE_ERR; + return false; + } + return true; +} + napi_value UpdateBackgroundRunning(napi_env env, napi_callback_info info, bool isThrow) { HitraceScoped traceScoped(HITRACE_TAG_OHOS, @@ -712,22 +742,9 @@ napi_value UpdateBackgroundRunning(napi_env env, napi_callback_info info, bool i napi_is_array(env, value, &isArray); napi_value ret {nullptr}; if (isArray) { - // argv[0] : context : AbilityContext - if (GetAbilityContext(env, argv[0], asyncCallbackInfo->abilityContext) == nullptr) { - BGTASK_LOGE("Get ability context failed"); - Common::HandleParamErr(env, ERR_CONTEXT_NULL_OR_TYPE_ERR, isThrow); - asyncCallbackInfo->errCode = ERR_CONTEXT_NULL_OR_TYPE_ERR; - return WrapVoidToJS(env); - } - - // argv[1] : bgMode : BackgroundMode - if (GetBackgroundMode(env, argv[1], asyncCallbackInfo) == nullptr) { - BGTASK_LOGE("input bgmode param not number"); - Common::HandleParamErr(env, ERR_BGMODE_NULL_OR_TYPE_ERR, isThrow); - asyncCallbackInfo->errCode = ERR_BGMODE_NULL_OR_TYPE_ERR; - return WrapVoidToJS(env); + if (UpdateBackgroundRunningCheckParam(env, argv, asyncCallbackInfo, isThrow)) { + ret = UpdateBackgroundRunningPromise(env, asyncCallbackInfo, isThrow); } - ret = UpdateBackgroundRunningPromise(env, asyncCallbackInfo, isThrow); } else { BGTASK_LOGE("UpdateBackgroundRunningByRequestPromise"); } @@ -790,6 +807,16 @@ napi_value StartBackgroundRunningByRequestPromise(napi_env env, AsyncCallbackInf return promise; } +napi_value StartBackgroundRunningSubmit(napi_env env, napi_value *argv, AsyncCallbackInfo *asyncCallbackInfo, + bool isThrow) +{ + if (!StartBackgroundRunningCheckRequest(env, argv, isThrow, asyncCallbackInfo)) { + return nullptr; + } else { + return StartBackgroundRunningByRequestPromise(env, asyncCallbackInfo, isThrow); + } +} + napi_value StartBackgroundRunning(napi_env env, napi_callback_info info, bool isThrow) { HitraceScoped traceScoped(HITRACE_TAG_OHOS, @@ -812,16 +839,7 @@ napi_value StartBackgroundRunning(napi_env env, napi_callback_info info, bool is } napi_value ret {nullptr}; if (MAX_START_BG_RUNNING_BY_RQUEST_PARAMS == argc) { - if (!StartBackgroundRunningCheckRequest(env, argv, isThrow, asyncCallbackInfo)) { - callbackPtr.release(); - if (asyncCallbackInfo != nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - } - return WrapVoidToJS(env); - } else { - ret = StartBackgroundRunningByRequestPromise(env, asyncCallbackInfo, isThrow); - } + ret = StartBackgroundRunningSubmit(env, argv, asyncCallbackInfo, isThrow); } else { if (!StartBackgroundRunningCheckParamBeforeSubmit(env, argv, MAX_START_BG_RUNNING_PARAMS, isThrow, asyncCallbackInfo)) { @@ -998,6 +1016,28 @@ napi_value StopBackgroundRunningByTaskIdPromise(napi_env env, AsyncCallbackInfo return promise; } +napi_value StopBackgroundRunningBySubmit(napi_env env, napi_value *argv, AsyncCallbackInfo *asyncCallbackInfo, + bool isThrow) +{ + napi_valuetype valueType = napi_undefined; + napi_value value = argv[1]; + napi_typeof(env, value, &valueType); + if (valueType == napi_number) { + int32_t taskIdValue = -1; + napi_get_value_int32(env, value, &taskIdValue); + if (taskIdValue < 0) { + Common::HandleErrCode(env, ERR_BGTASK_CONTINUOUS_TASKID_INVALID, true); + asyncCallbackInfo->errCode = ERR_BGTASK_CONTINUOUS_TASKID_INVALID; + return nullptr; + } else { + asyncCallbackInfo->removeTaskId = taskIdValue; + return StopBackgroundRunningByTaskIdPromise(env, asyncCallbackInfo, isThrow); + } + } else { + return StopBackgroundRunningAsync(env, argv, MAX_STOP_BG_RUNNING_PARAMS - 1, asyncCallbackInfo, isThrow); + } +} + napi_value StopBackgroundRunning(napi_env env, napi_callback_info info, bool isThrow) { HitraceScoped traceScoped(HITRACE_TAG_OHOS, @@ -1030,22 +1070,7 @@ napi_value StopBackgroundRunning(napi_env env, napi_callback_info info, bool isT napi_value ret {nullptr}; if (argc == MAX_STOP_BG_RUNNING_PARAMS) { - napi_valuetype valueType = napi_undefined; - napi_value value = argv[1]; - napi_typeof(env, value, &valueType); - if (valueType == napi_number) { - int32_t taskIdValue = -1; - napi_get_value_int32(env, value, &taskIdValue); - if (taskIdValue < 0) { - Common::HandleErrCode(env, ERR_BGTASK_CONTINUOUS_TASKID_INVALID, true); - asyncCallbackInfo->errCode = ERR_BGTASK_CONTINUOUS_TASKID_INVALID; - } else { - asyncCallbackInfo->removeTaskId = taskIdValue; - ret = StopBackgroundRunningByTaskIdPromise(env, asyncCallbackInfo, isThrow); - } - } else { - ret = StopBackgroundRunningAsync(env, argv, MAX_STOP_BG_RUNNING_PARAMS - 1, asyncCallbackInfo, isThrow); - } + ret = StopBackgroundRunningBySubmit(env, argv, asyncCallbackInfo, isThrow); } else { ret = StopBackgroundRunningPromise(env, asyncCallbackInfo, isThrow); } diff --git a/services/continuous_task/include/bg_continuous_task_mgr.h b/services/continuous_task/include/bg_continuous_task_mgr.h index d2e51506758799bb8fa46ffa2f288b56728146cc..f2f43aeda79954c7acbe6c94c8fd1073b09b81a3 100644 --- a/services/continuous_task/include/bg_continuous_task_mgr.h +++ b/services/continuous_task/include/bg_continuous_task_mgr.h @@ -185,6 +185,10 @@ private: bool IsExistCallback(int32_t uid, uint32_t type); ErrCode CheckCombinedTaskNotifacation(std::shared_ptr &record, bool &sendNotification); bool StopContinuousTaskByUserInner(const std::string &key); + ErrCode DetermineMatchCombinedTaskNotifacation(std::shared_ptr recordParam, + bool &sendNotification); + ErrCode StartBackgroundRunningSubmit(std::shared_ptr continuousTaskRecord, + std::string &taskInfoMapKey); private: std::atomic isSysReady_ {false}; int32_t bgTaskUid_ {-1}; diff --git a/services/continuous_task/src/bg_continuous_task_mgr.cpp b/services/continuous_task/src/bg_continuous_task_mgr.cpp index 8e1ef6f7139618ea1d76abb20a0326f1245e3356..d2e96a7fd856e4727ade527cf835b5a30c6efd8f 100644 --- a/services/continuous_task/src/bg_continuous_task_mgr.cpp +++ b/services/continuous_task/src/bg_continuous_task_mgr.cpp @@ -875,7 +875,12 @@ ErrCode BgContinuousTaskMgr::StartBackgroundRunningInner(std::shared_ptruid_, continuousTaskRecord->userId_, continuousTaskRecord->bundleName_, mainAbilityLabel); } + return StartBackgroundRunningSubmit(continuousTaskRecord, taskInfoMapKey); +} +ErrCode BgContinuousTaskMgr::StartBackgroundRunningSubmit(std::shared_ptr continuousTaskRecord, + std::string &taskInfoMapKey) +{ ErrCode ret; if (continuousTaskRecord->isFromWebview_) { ret = CheckBgmodeTypeForInner(continuousTaskRecord->bgModeId_); @@ -918,22 +923,29 @@ ErrCode BgContinuousTaskMgr::CheckCombinedTaskNotifacation(std::shared_ptrisCombinedTaskNotification_) { - BGTASK_LOGE("current continuous task not support merge, uid: %{public}d", recordParam->uid_); + BGTASK_LOGE("current task not support merge, uid: %{public}d", recordParam->uid_); return ERR_BGTASK_CONTINUOUS_NOT_MERGE_CURRENTTASK_COMBINED_FALSE; } } else { sendNotification = true; return ERR_OK; } + return DetermineMatchCombinedTaskNotifacation(recordParam, sendNotification); +} + +ErrCode BgContinuousTaskMgr::DetermineMatchCombinedTaskNotifacation(std::shared_ptr recordParam, + bool &sendNotification) +{ + int32_t mergeNotificationTaskId = recordParam->combinedNotificationTaskId_; ErrCode ret = ERR_BGTASK_CONTINUOUS_TASKID_INVALID; for (const auto &record : continuousTaskInfosMap_) { if (!record.second) { - BGTASK_LOGE("task is null"); + BGTASK_LOGE("current task is null"); continue; } if (record.second->uid_ != recordParam->uid_) { - BGTASK_LOGE("task uid is not equal, task uid: %{public}d, param uid: %{public}d", - record.second->uid_, recordParam->uid_); + BGTASK_LOGE("current task uid not equal, task uid: %{public}d, param uid: %{public}d", record.second->uid_, + recordParam->uid_); continue; } if (record.second->GetContinuousTaskId() != mergeNotificationTaskId) { @@ -942,37 +954,31 @@ ErrCode BgContinuousTaskMgr::CheckCombinedTaskNotifacation(std::shared_ptrisCombinedTaskNotification_) { - ret = ERR_BGTASK_CONTINUOUS_NOT_MERGE_COMBINED_FALSE; BGTASK_LOGE("continuous task not support merge, taskId: %{public}d", mergeNotificationTaskId); - return ret; + return ERR_BGTASK_CONTINUOUS_NOT_MERGE_COMBINED_FALSE; } if (record.second->GetNotificationId() == -1) { - ret = ERR_BGTASK_CONTINUOUS_NOT_MERGE_NOTIFICATION_NOT_EXIST; BGTASK_LOGE("continuous task notification not exist, taskId: %{public}d", mergeNotificationTaskId); - return ret; + return ERR_BGTASK_CONTINUOUS_NOT_MERGE_NOTIFICATION_NOT_EXIST; } if (!CommonUtils::CheckModesSame(record.second->bgModeIds_, recordParam->bgModeIds_)) { - ret = ERR_BGTASK_CONTINUOUS_MODE_OR_SUBMODE_TYPE_MISMATCH; BGTASK_LOGE("continuous task modes mismatch, taskId: %{public}d", mergeNotificationTaskId); - return ret; + return ERR_BGTASK_CONTINUOUS_MODE_OR_SUBMODE_TYPE_MISMATCH; } if (!CommonUtils::CheckModesSame(record.second->bgSubModeIds_, recordParam->bgSubModeIds_)) { - ret = ERR_BGTASK_CONTINUOUS_MODE_OR_SUBMODE_TYPE_MISMATCH; BGTASK_LOGE("continuous task submodes mismatch, taskId: %{public}d", mergeNotificationTaskId); - return ret; + return ERR_BGTASK_CONTINUOUS_MODE_OR_SUBMODE_TYPE_MISMATCH; } if (CommonUtils::CheckExistMode(recordParam->bgModeIds_, BackgroundMode::DATA_TRANSFER)) { - ret = ERR_BGTASK_CONTINUOUS_DATA_TRANSFER_NOT_MERGE_NOTIFICATION; BGTASK_LOGE("continuous task mode: DATA_TRANSFER not support merge, taskId: %{public}d", mergeNotificationTaskId); - return ret; + return ERR_BGTASK_CONTINUOUS_DATA_TRANSFER_NOT_MERGE_NOTIFICATION; } else { - ret = ERR_OK; sendNotification = false; recordParam->notificationId_ = record.second->GetNotificationId(); recordParam->notificationLabel_ = record.second->GetNotificationLabel(); record.second->combinedNotificationTaskId_ = mergeNotificationTaskId; - break; + return ERR_OK; } } return ret; diff --git a/services/test/unittest/bg_continuous_task_mgr_test.cpp b/services/test/unittest/bg_continuous_task_mgr_test.cpp index fcd082fe2036e61e19ae37c687dff0ecf6847dd4..57594dcb48d658d93cbccd12fd76d1adaa9a329d 100644 --- a/services/test/unittest/bg_continuous_task_mgr_test.cpp +++ b/services/test/unittest/bg_continuous_task_mgr_test.cpp @@ -243,7 +243,7 @@ HWTEST_F(BgContinuousTaskMgrTest, StartAndUpdateBackgroundRunning_001, TestSize. std::make_shared(), "ability1", nullptr, "Entry", true, {}); EXPECT_NE(taskParam1, nullptr); - EXPECT_EQ((int32_t)bgContinuousTaskMgr_->StartBackgroundRunning(taskParam1), (int32_t)ERR_BGTASK_INVALID_BGMODE); + EXPECT_EQ((int32_t)bgContinuousTaskMgr_->StartBackgroundRunning(taskParam1), (int32_t)ERR_BGTASK_CHECK_TASK_PARAM); // 2 set configure mode is CONFIGURE_ALL_MODES bgContinuousTaskMgr_->cachedBundleInfos_.clear();