diff --git a/upload/frameworks/libs/include/upload_task.h b/upload/frameworks/libs/include/upload_task.h index 48cf1c72..c9be2ca0 100644 --- a/upload/frameworks/libs/include/upload_task.h +++ b/upload/frameworks/libs/include/upload_task.h @@ -52,7 +52,6 @@ public: UPLOAD_API virtual bool Remove(); UPLOAD_API virtual void On(Type type, void *callback); UPLOAD_API virtual void Off(Type type, void *callback); - UPLOAD_API virtual void Off(Type type); UPLOAD_API void ExecuteTask(); static void Run(void *arg); virtual void OnRun(); diff --git a/upload/frameworks/libs/src/upload_task.cpp b/upload/frameworks/libs/src/upload_task.cpp index 49aa64fa..e52d8fdb 100644 --- a/upload/frameworks/libs/src/upload_task.cpp +++ b/upload/frameworks/libs/src/upload_task.cpp @@ -64,13 +64,6 @@ void UploadTask::On(Type type, void *callback) SetCallback(type, callback); } -void UploadTask::Off(Type type) -{ - UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "Off. In."); - std::lock_guard guard(mutex_); - SetCallback(type, nullptr); -} - void UploadTask::Off(Type type, void *callback) { UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "Off. In."); @@ -82,8 +75,6 @@ void UploadTask::Off(Type type, void *callback) if (type == TYPE_PROGRESS_CALLBACK && progressCallback_ != nullptr) { ((IProgressCallback*)callback)->Progress(uploadedSize_, totalSize_); - } else { - UPLOAD_HILOGD(UPLOAD_MODULE_FRAMEWORK, "Off. type[%{public}d] not match.", type); } SetCallback(type, nullptr); } diff --git a/upload/interfaces/kits/js/@ohos.request.d.ts b/upload/interfaces/kits/js/@ohos.request.d.ts index c29275d0..5d0d0919 100644 --- a/upload/interfaces/kits/js/@ohos.request.d.ts +++ b/upload/interfaces/kits/js/@ohos.request.d.ts @@ -342,10 +342,9 @@ declare namespace request { */ title?: string; /** - * Whether to display the background. + * Allow download background task notifications. * * @since 9 - * @permission N/A */ background?: boolean; } @@ -829,7 +828,7 @@ declare namespace request { * @permission ohos.permission.INTERNET * @return - */ - off(type:'complete' | 'fail'): void; + off(type:'complete' | 'fail', callback?: Callback>): void; /** * Deletes a upload session. diff --git a/upload/interfaces/kits/napi/include/upload_task_napi.h b/upload/interfaces/kits/napi/include/upload_task_napi.h index 6b5f8c22..bf55401e 100644 --- a/upload/interfaces/kits/napi/include/upload_task_napi.h +++ b/upload/interfaces/kits/napi/include/upload_task_napi.h @@ -117,6 +117,7 @@ private: size_t argc, napi_value *argv, napi_value self, napi_value *result); static napi_status OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); static napi_status OffComplete(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result); + static napi_status CheckOffCompleteParam(napi_env env, size_t argc, napi_value *argv, napi_value self); std::shared_ptr onProgress_ = nullptr; std::shared_ptr onHeaderReceive_ = nullptr; diff --git a/upload/interfaces/kits/napi/src/upload_task_napi.cpp b/upload/interfaces/kits/napi/src/upload_task_napi.cpp index e5a1c491..1ab64e98 100644 --- a/upload/interfaces/kits/napi/src/upload_task_napi.cpp +++ b/upload/interfaces/kits/napi/src/upload_task_napi.cpp @@ -295,30 +295,65 @@ napi_status UploadTaskNapi::OffHeaderReceive(napi_env env, return napi_ok; } -napi_status UploadTaskNapi::OffFail(napi_env env, size_t argc, napi_value *argv, napi_value self, napi_value *result) +napi_status UploadTaskNapi::CheckOffCompleteParam(napi_env env, size_t argc, napi_value *argv, napi_value self) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); - NAPI_ASSERT_BASE(env, argc == 0, "argc should be 0", napi_invalid_arg); + NAPI_ASSERT_BASE(env, argc == 0 || argc == 1, "argc should be 0 or 1", napi_invalid_arg); NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); - UploadTaskNapi *proxy = nullptr; - NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); - NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); - proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK); + if (argc == 1) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Input. argc == 1."); + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", napi_invalid_arg); + } return napi_ok; } -napi_status UploadTaskNapi::OffComplete(napi_env env, size_t argc, napi_value *argv, - napi_value self, napi_value *result) +napi_status UploadTaskNapi::OffFail(napi_env env, + size_t argc, napi_value *argv, napi_value self, napi_value *result) { - UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); - NAPI_ASSERT_BASE(env, argc == 0, "argc should be 0", napi_invalid_arg); - NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffFail."); + napi_status status = CheckOffCompleteParam(env, argc, argv, self); + if (status != napi_ok) { + return status; + } UploadTaskNapi *proxy = nullptr; NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); - proxy->napiUploadTask_->Off(TYPE_COMPLETE_CALLBACK); + + if (proxy->onFail_ == nullptr) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Fail. proxy->onFail_ == nullptr."); + return napi_generic_failure; + } else { + std::shared_ptr callback = std::make_shared(proxy, env, argv[0]); + proxy->napiUploadTask_->Off(TYPE_FAIL_CALLBACK, callback.get()); + proxy->onFail_ = nullptr; + } + return napi_ok; +} + + +napi_status UploadTaskNapi::OffComplete(napi_env env, + size_t argc, napi_value *argv, napi_value self, napi_value *result) +{ + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Enter OffComplete."); + napi_status status = CheckOffCompleteParam(env, argc, argv, self); + if (status != napi_ok) { + return status; + } + + UploadTaskNapi *proxy = nullptr; + NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast(&proxy)), napi_invalid_arg); + NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); + if (proxy->onComplete_ == nullptr) { + UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "CompleteCallback. proxy->OffComplete_ == nullptr."); + return napi_generic_failure; + } else { + std::shared_ptr callback = std::make_shared(proxy, env, argv[0]); + proxy->napiUploadTask_->Off(TYPE_COMPLETE_CALLBACK, callback.get()); + proxy->onComplete_ = nullptr; + } return napi_ok; }