From ef553af496f7b61b1d91e4d0e7c5a7fc3d9eae54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 28 Dec 2024 11:47:00 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_tasksignal/task_signal_entity.cpp | 65 ++++++------- .../class_watcher/watcher_n_exporter.cpp | 95 +++++++------------ .../js/src/mod_fs/properties/connectdfs.cpp | 69 ++++++++++---- .../kits/js/src/mod_fs/properties/copy.cpp | 65 +++++++++++-- .../copy_listener/trans_listener.cpp | 50 ++++++---- .../src/n_async/n_async_work_callback.cpp | 34 +++---- 6 files changed, 209 insertions(+), 169 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/task_signal_entity.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/task_signal_entity.cpp index c11015496b..b6ea8f2f4b 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/task_signal_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/task_signal_entity.cpp @@ -27,46 +27,35 @@ void TaskSignalEntity::OnCancel() } auto env = callbackContext_->env_; callbackContext_->filePath_ = taskSignal_->filePath_; - napi_get_uv_event_loop(env, &loop); - if (loop == nullptr) { - return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - return; - } - work->data = reinterpret_cast(callbackContext_.get()); - int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - JSCallbackContext *callbackContext = reinterpret_cast(work->data); - if (callbackContext == nullptr) { - return; - } - if (!callbackContext->ref_) { - return; - } - napi_handle_scope scope = nullptr; - napi_status ret = napi_open_handle_scope(callbackContext->env_, &scope); - if (ret != napi_ok) { - return; - } - napi_env env = callbackContext->env_; - napi_value jsCallback = callbackContext->ref_.Deref(env).val_; - napi_value filePath = LibN::NVal::CreateUTF8String(env, callbackContext->filePath_).val_; - napi_value retVal = nullptr; - ret = napi_call_function(env, nullptr, jsCallback, 1, &filePath, &retVal); - if (ret != napi_ok) { - HILOGE("Failed to call napi_call_function, ret: %{public}d", ret); - } - ret = napi_close_handle_scope(callbackContext->env_, scope); - if (ret != napi_ok) { - HILOGE("Failed to close handle scope, ret: %{public}d", ret); - } - delete work; - }, uv_qos_user_initiated); + JSCallbackContext *callbackContext = callbackContext_.get(); + auto task = [callbackContext] () { + if (callbackContext == nullptr) { + return; + } + if (!callbackContext->ref_) { + return; + } + napi_handle_scope scope = nullptr; + napi_status ret = napi_open_handle_scope(callbackContext->env_, &scope); + if (ret != napi_ok) { + return; + } + napi_env env = callbackContext->env_; + napi_value jsCallback = callbackContext->ref_.Deref(env).val_; + napi_value filePath = LibN::NVal::CreateUTF8String(env, callbackContext->filePath_).val_; + napi_value retVal = nullptr; + ret = napi_call_function(env, nullptr, jsCallback, 1, &filePath, &retVal); + if (ret != napi_ok) { + HILOGE("Failed to call napi_call_function, ret: %{public}d", ret); + } + ret = napi_close_handle_scope(callbackContext->env_, scope); + if (ret != napi_ok) { + HILOGE("Failed to close handle scope, ret: %{public}d", ret); + } + }; + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret != 0) { HILOGE("Failed to uv_queue_work_with_qos, ret: %{public}d", ret); - delete work; } } } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index c42aa35ab7..3c259d2a46 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -118,85 +118,60 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; } -static void WatcherCallbackComplete(uv_work_t *work, int stat) -{ - if (work == nullptr) { - HILOGE("Failed to get uv_queue_work pointer"); - return; - } - - WatcherNExporter::JSCallbackContext *callbackContext = - reinterpret_cast(work->data); - do { - if (callbackContext == nullptr) { - HILOGE("Failed to create context pointer"); - break; - } - if (!callbackContext->ref_) { - HILOGE("Failed to get nref reference"); - break; - } - napi_handle_scope scope = nullptr; - napi_status status = napi_open_handle_scope(callbackContext->env_, &scope); - if (status != napi_ok) { - HILOGE("Failed to open handle scope, status: %{public}d", status); - break; - } - napi_env env = callbackContext->env_; - napi_value jsCallback = callbackContext->ref_.Deref(env).val_; - NVal objn = NVal::CreateObject(env); - objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); - objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); - objn.AddProp("cookie", NVal::CreateUint32(env, callbackContext->cookie_).val_); - napi_value retVal = nullptr; - status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); - if (status != napi_ok) { - HILOGE("Failed to call napi_call_function, status: %{public}d", status); - } - status = napi_close_handle_scope(callbackContext->env_, scope); - if (status != napi_ok) { - HILOGE("Failed to close handle scope, status: %{public}d", status); - } - } while (0); - delete callbackContext; - delete work; -} - void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std::string &fileName, const uint32_t &event, const uint32_t &cookie) { - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env, &loop); - if (loop == nullptr) { - HILOGE("Failed to get uv event loop"); - return; - } if (!callback) { HILOGE("Failed to parse watcher callback"); return; } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - HILOGE("Failed to create uv_work_t pointer"); - return; - } JSCallbackContext *callbackContext = new (std::nothrow) JSCallbackContext(callback); if (callbackContext == nullptr) { - delete work; return; } callbackContext->env_ = env; callbackContext->fileName_ = fileName; callbackContext->event_ = event; callbackContext->cookie_ = cookie; - work->data = reinterpret_cast(callbackContext); - int ret = uv_queue_work( - loop, work, [](uv_work_t *work) {}, reinterpret_cast(WatcherCallbackComplete)); + auto task = [callbackContext] () { + do { + if (callbackContext == nullptr) { + HILOGE("Failed to create context pointer"); + break; + } + if (!callbackContext->ref_) { + HILOGE("Failed to get nref reference"); + break; + } + napi_handle_scope scope = nullptr; + napi_status status = napi_open_handle_scope(callbackContext->env_, &scope); + if (status != napi_ok) { + HILOGE("Failed to open handle scope, status: %{public}d", status); + break; + } + napi_env env = callbackContext->env_; + napi_value jsCallback = callbackContext->ref_.Deref(env).val_; + NVal objn = NVal::CreateObject(env); + objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); + objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); + objn.AddProp("cookie", NVal::CreateUint32(env, callbackContext->cookie_).val_); + napi_value retVal = nullptr; + status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); + if (status != napi_ok) { + HILOGE("Failed to call napi_call_function, status: %{public}d", status); + } + status = napi_close_handle_scope(callbackContext->env_, scope); + if (status != napi_ok) { + HILOGE("Failed to close handle scope, status: %{public}d", status); + } + } while (0); + delete callbackContext; + }; + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); delete callbackContext; - delete work; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp b/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp index 324ee94a7c..6329ea5cbb 100644 --- a/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp @@ -296,24 +296,10 @@ void UvWorkAfterOnStaus(uv_work_t *work, int status) void NAPIDfsListener::OnStatus(const std::string &networkId, int32_t status) { HILOGI("NAPIDfsListener::OnStatus called"); - uv_loop_s *loop = nullptr; - - napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { - HILOGE("NAPIDfsListener::OnStatus, loop == nullptr"); - return; - } - - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - return; - } auto connectDfsCB = new (std::nothrow) ConnectDfsCB; if (connectDfsCB == nullptr) { HILOGE("NAPIDfsListener::OnStatus, connectDfsCb == nullptr"); - delete work; - work = nullptr; return; } connectDfsCB->cbBase.cbInfo.env = env_; @@ -324,15 +310,60 @@ void NAPIDfsListener::OnStatus(const std::string &networkId, int32_t status) } connectDfsCB->networkId = networkId; connectDfsCB->status = status; - work->data = static_cast(connectDfsCB); + auto task = [connectDfsCB] () { + HILOGI("UvWorkAfterOnStaus called"); + napi_handle_scope scope = nullptr; + if (connectDfsCB == nullptr) { + HILOGE("ConnectDfsCB, GetParam connectDfsCB is null"); + return nullptr; + } + napi_open_handle_scope(connectDfsCB->cbBase.cbInfo.env, scope); + if (scope == nullptr) { + delete connectDfsCB; + connectDfsCB = nullptr; + return nullptr; + } + HILOGI("ConnectDfsCB::CheckAndGetParameters GetParam end"); + if (connectDfsCB == nullptr) { + return; + } + HILOGI("UvWorkAfterOnStaus, status = %{public}d", connectDfsCB->status); + + napi_value result[NARG_CNT::TWO] = {nullptr}; + result[NARG_POS::FIRST] = WrapString(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->networkId.c_str(), "networkId"); + result[NARG_POS::SECOND] = WrapInt32(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->status, "status"); + if (connectDfsCB->cbBase.deferred == nullptr) { + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_get_undefined(connectDfsCB->cbBase.cbInfo.env, &undefined); + napi_value callResult = nullptr; + napi_get_reference_value(connectDfsCB->cbBase.cbInfo.env, + connectDfsCB->cbBase.cbInfo.callback, &callback); + napi_call_function(connectDfsCB->cbBase.cbInfo.env, undefined, callback, NARG_CNT::TWO, result, &callResult); + if (connectDfsCB->cbBase.cbInfo.callback != nullptr) { + napi_delete_reference(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.cbInfo.callback); + } + } else { + napi_value res[NARG_CNT::TWO] = { nullptr }; + napi_get_undefined(connectDfsCB->cbBase.cbInfo.env, &res[NARG_POS::SECOND]); + if (connectDfsCB->status == ERRNO_NOERR) { + napi_resolve_deferred(connectDfsCB->cbBase.cbInfo.env, + connectDfsCB->cbBase.deferred, res[NARG_POS::SECOND]); + } else { + res[NARG_POS::FIRST] = NError(connectDfsCB->status).GetNapiErr(connectDfsCB->cbBase.cbInfo.env); + napi_reject_deferred(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.deferred, res[NARG_POS::FIRST]); + } + } - int rev = uv_queue_work( - loop, work, [](uv_work_t *work) {}, UvWorkAfterOnStaus); + napi_close_handle_scope(connectDfsCB->cbBase.cbInfo.env, scope); + delete connectDfsCB; + connectDfsCB = nullptr; + HILOGI("UvWorkAfterOnStaus end"); + }; + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (rev != ERRNO_NOERR) { delete connectDfsCB; connectDfsCB = nullptr; - delete work; - work = nullptr; } HILOGI("NAPIDfsListener::OnStatus end"); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index a905646a57..513cfc0522 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -617,19 +617,64 @@ uv_work_t *Copy::GetUVwork(std::shared_ptr infos) void Copy::OnFileReceive(std::shared_ptr infos) { - uv_work_t *work = GetUVwork(infos); - if (work == nullptr) { - HILOGE("failed to get uv work"); - return; + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + auto iter = jsCbMap_.find(*infos); + if (iter == jsCbMap_.end()) { + HILOGE("Failed to find callback"); + return nullptr; + } + auto callback = iter->second; + infos->env = callback->env; + entry = new (std::nothrow) UvEntry(iter->second, infos); + if (entry == nullptr) { + HILOGE("entry ptr is nullptr."); + return nullptr; + } + entry->progressSize = callback->progressSize; + entry->totalSize = callback->totalSize; } - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(infos->env, &loop); - auto ret = uv_queue_work( - loop, work, [](uv_work_t *work) {}, reinterpret_cast(ReceiveComplete)); + + auto task = [entry] () { + if (entry == nullptr) { + HILOGE("entry pointer is nullptr."); + return; + } + auto processedSize = entry->progressSize; + if (processedSize < entry->callback->maxProgressSize) { + return; + } + entry->callback->maxProgressSize = processedSize; + + napi_handle_scope scope = nullptr; + napi_env env = entry->callback->env; + napi_status status = napi_open_handle_scope(env, &scope); + if (status != napi_ok) { + HILOGE("Failed to open handle scope, status: %{public}d.", status); + return; + } + NVal obj = NVal::CreateObject(env); + if (processedSize <= MAX_VALUE && entry->totalSize <= MAX_VALUE) { + obj.AddProp("processedSize", NVal::CreateInt64(env, processedSize).val_); + obj.AddProp("totalSize", NVal::CreateInt64(env, entry->totalSize).val_); + } + napi_value result = nullptr; + napi_value jsCallback = entry->callback->nRef.Deref(env).val_; + status = napi_call_function(env, nullptr, jsCallback, 1, &(obj.val_), &result); + if (status != napi_ok) { + HILOGE("Failed to get result, status: %{public}d.", status); + } + status = napi_close_handle_scope(env, scope); + if (status != napi_ok) { + HILOGE("Failed to close scope, status: %{public}d.", status); + } + }; + + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret != 0) { HILOGE("failed to uv_queue_work"); - delete (reinterpret_cast(work->data)); - delete work; + delete entry; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp index f6de256a69..11a833ccdf 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_listener/trans_listener.cpp @@ -299,34 +299,46 @@ int32_t TransListener::OnFileReceive(uint64_t totalBytes, uint64_t processedByte return ENOMEM; } - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(callback_->env, &loop); - if (loop == nullptr) { - HILOGE("Failed to get uv event loop"); - return ENOMEM; - } - - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - HILOGE("Failed to create uv_work_t pointer"); - return ENOMEM; - } - UvEntry *entry = new (std::nothrow) UvEntry(callback_); if (entry == nullptr) { HILOGE("entry ptr is nullptr"); - delete work; return ENOMEM; } entry->progressSize = processedBytes; entry->totalSize = totalBytes; - work->data = entry; - int retVal = uv_queue_work( - loop, work, [](uv_work_t *work) {}, reinterpret_cast(CallbackComplete)); + auto task = [entry] () { + if (entry == nullptr) { + HILOGE("entry pointer is nullptr."); + return; + } + napi_handle_scope scope = nullptr; + napi_env env = entry->callback->env; + napi_status status = napi_open_handle_scope(env, &scope); + if (status != napi_ok) { + HILOGE("Failed to open handle scope, status: %{public}d.", status); + return; + } + NVal obj = NVal::CreateObject(env); + if (entry->progressSize <= MAX_VALUE && entry->totalSize <= MAX_VALUE) { + obj.AddProp("processedSize", NVal::CreateInt64(env, entry->progressSize).val_); + obj.AddProp("totalSize", NVal::CreateInt64(env, entry->totalSize).val_); + } + + napi_value result = nullptr; + napi_value jsCallback = entry->callback->nRef.Deref(env).val_; + status = napi_call_function(env, nullptr, jsCallback, 1, &(obj.val_), &result); + if (status != napi_ok) { + HILOGE("Failed to get result, status: %{public}d.", status); + } + status = napi_close_handle_scope(env, scope); + if (status != napi_ok) { + HILOGE("Failed to close scope, status: %{public}d.", status); + } + }; + auto retVal = napi_send_event(env_, task, napi_eprio_immediate); if (retVal != 0) { HILOGE("failed to get uv_queue_work"); - delete (reinterpret_cast(work->data)); - delete work; + delete entry; return ENOMEM; } return ERRNO_NOERR; diff --git a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp index 4d0a1c98a0..0bc39258f3 100644 --- a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp +++ b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp @@ -65,15 +65,11 @@ NAsyncWorkCallback::~NAsyncWorkCallback() HILOGE("Failed to new uv_work_t"); return; } - work->data = static_cast(ctx_); - - int ret = uv_queue_work( - loop, work.get(), [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - NAsyncContextCallback *ctx = static_cast(work->data); - delete ctx; - delete work; - }); + NAsyncContextCallback *ctx = ctx_; + auto task = [ctx] () { + delete ctx; + }; + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret) { HILOGE("Failed to call uv_queue_work %{public}d", status); return; @@ -229,23 +225,15 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) workArgs->ptr = this; workArgs->cb = cbComplete; - work->data = static_cast(workArgs.get()); - - int ret = uv_queue_work( - loop, work.get(), [](uv_work_t *work) { - HILOGI("Enter, %{public}zu", (size_t)work); - }, - [](uv_work_t *work, int status) { - HILOGI("AsyncWork Enter, %{public}zu", (size_t)work); - auto workArgs = static_cast(work->data); - AfterWorkCallback(workArgs->ptr->env_, napi_ok, workArgs->ptr->ctx_, workArgs->cb); - delete workArgs; - delete work; - }); + auto workArgs = workArgs.get(); + auto task = [workArgs] () { + AfterWorkCallback(workArgs->ptr->env_, napi_ok, workArgs->ptr->ctx_, workArgs->cb); + delete workArgs; + }; + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret) { HILOGE("Failed to call uv_queue_work %{public}d", status); workArgs.reset(); - work.reset(); return; } workArgs.release(); -- Gitee From 22fd30677d030e2e392de1be531072d25648eef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 28 Dec 2024 15:36:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=95=B4=E6=94=B9=EF=BC=9A=E8=B6=85=E5=A4=A7=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../class_watcher/watcher_n_exporter.cpp | 87 ++++++++++++------- .../js/src/mod_fs/properties/connectdfs.cpp | 62 +++---------- .../kits/js/src/mod_fs/properties/copy.cpp | 59 ++----------- .../kits/js/src/mod_fs/properties/copy.h | 2 +- .../src/n_async/n_async_work_callback.cpp | 20 +---- 5 files changed, 78 insertions(+), 152 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 3c259d2a46..90a62dbadf 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -118,6 +118,50 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; } +static void WatcherCallbackComplete(uv_work_t *work) +{ + if (work == nullptr) { + HILOGE("Failed to get uv_queue_work pointer"); + return; + } + + WatcherNExporter::JSCallbackContext *callbackContext = + reinterpret_cast(work->data); + do { + if (callbackContext == nullptr) { + HILOGE("Failed to create context pointer"); + break; + } + if (!callbackContext->ref_) { + HILOGE("Failed to get nref reference"); + break; + } + napi_handle_scope scope = nullptr; + napi_status status = napi_open_handle_scope(callbackContext->env_, &scope); + if (status != napi_ok) { + HILOGE("Failed to open handle scope, status: %{public}d", status); + break; + } + napi_env env = callbackContext->env_; + napi_value jsCallback = callbackContext->ref_.Deref(env).val_; + NVal objn = NVal::CreateObject(env); + objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); + objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); + objn.AddProp("cookie", NVal::CreateUint32(env, callbackContext->cookie_).val_); + napi_value retVal = nullptr; + status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); + if (status != napi_ok) { + HILOGE("Failed to call napi_call_function, status: %{public}d", status); + } + status = napi_close_handle_scope(callbackContext->env_, scope); + if (status != napi_ok) { + HILOGE("Failed to close handle scope, status: %{public}d", status); + } + } while (0); + delete callbackContext; + delete work; +} + void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std::string &fileName, const uint32_t &event, const uint32_t &cookie) { @@ -125,53 +169,30 @@ void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std:: HILOGE("Failed to parse watcher callback"); return; } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOGE("Failed to create uv_work_t pointer"); + return; + } JSCallbackContext *callbackContext = new (std::nothrow) JSCallbackContext(callback); if (callbackContext == nullptr) { + delete work; return; } callbackContext->env_ = env; callbackContext->fileName_ = fileName; callbackContext->event_ = event; callbackContext->cookie_ = cookie; - auto task = [callbackContext] () { - do { - if (callbackContext == nullptr) { - HILOGE("Failed to create context pointer"); - break; - } - if (!callbackContext->ref_) { - HILOGE("Failed to get nref reference"); - break; - } - napi_handle_scope scope = nullptr; - napi_status status = napi_open_handle_scope(callbackContext->env_, &scope); - if (status != napi_ok) { - HILOGE("Failed to open handle scope, status: %{public}d", status); - break; - } - napi_env env = callbackContext->env_; - napi_value jsCallback = callbackContext->ref_.Deref(env).val_; - NVal objn = NVal::CreateObject(env); - objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); - objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); - objn.AddProp("cookie", NVal::CreateUint32(env, callbackContext->cookie_).val_); - napi_value retVal = nullptr; - status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); - if (status != napi_ok) { - HILOGE("Failed to call napi_call_function, status: %{public}d", status); - } - status = napi_close_handle_scope(callbackContext->env_, scope); - if (status != napi_ok) { - HILOGE("Failed to close handle scope, status: %{public}d", status); - } - } while (0); - delete callbackContext; + work->data = reinterpret_cast(callbackContext); + auto task = [work] () { + WatcherCallbackComplete(work); }; auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); delete callbackContext; + delete work; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp b/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp index 6329ea5cbb..c2fb18b8c7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp @@ -249,7 +249,7 @@ napi_value WrapString(napi_env &env, const std::string ¶m, const std::string return jsValue; } -void UvWorkAfterOnStaus(uv_work_t *work, int status) +void UvWorkAfterOnStaus(uv_work_t *work) { HILOGI("UvWorkAfterOnStaus called"); napi_handle_scope scope = nullptr; @@ -297,9 +297,16 @@ void NAPIDfsListener::OnStatus(const std::string &networkId, int32_t status) { HILOGI("NAPIDfsListener::OnStatus called"); + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + return; + } + auto connectDfsCB = new (std::nothrow) ConnectDfsCB; if (connectDfsCB == nullptr) { HILOGE("NAPIDfsListener::OnStatus, connectDfsCb == nullptr"); + delete work; + work = nullptr; return; } connectDfsCB->cbBase.cbInfo.env = env_; @@ -310,60 +317,17 @@ void NAPIDfsListener::OnStatus(const std::string &networkId, int32_t status) } connectDfsCB->networkId = networkId; connectDfsCB->status = status; - auto task = [connectDfsCB] () { - HILOGI("UvWorkAfterOnStaus called"); - napi_handle_scope scope = nullptr; - if (connectDfsCB == nullptr) { - HILOGE("ConnectDfsCB, GetParam connectDfsCB is null"); - return nullptr; - } - napi_open_handle_scope(connectDfsCB->cbBase.cbInfo.env, scope); - if (scope == nullptr) { - delete connectDfsCB; - connectDfsCB = nullptr; - return nullptr; - } - HILOGI("ConnectDfsCB::CheckAndGetParameters GetParam end"); - if (connectDfsCB == nullptr) { - return; - } - HILOGI("UvWorkAfterOnStaus, status = %{public}d", connectDfsCB->status); - - napi_value result[NARG_CNT::TWO] = {nullptr}; - result[NARG_POS::FIRST] = WrapString(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->networkId.c_str(), "networkId"); - result[NARG_POS::SECOND] = WrapInt32(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->status, "status"); - if (connectDfsCB->cbBase.deferred == nullptr) { - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_get_undefined(connectDfsCB->cbBase.cbInfo.env, &undefined); - napi_value callResult = nullptr; - napi_get_reference_value(connectDfsCB->cbBase.cbInfo.env, - connectDfsCB->cbBase.cbInfo.callback, &callback); - napi_call_function(connectDfsCB->cbBase.cbInfo.env, undefined, callback, NARG_CNT::TWO, result, &callResult); - if (connectDfsCB->cbBase.cbInfo.callback != nullptr) { - napi_delete_reference(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.cbInfo.callback); - } - } else { - napi_value res[NARG_CNT::TWO] = { nullptr }; - napi_get_undefined(connectDfsCB->cbBase.cbInfo.env, &res[NARG_POS::SECOND]); - if (connectDfsCB->status == ERRNO_NOERR) { - napi_resolve_deferred(connectDfsCB->cbBase.cbInfo.env, - connectDfsCB->cbBase.deferred, res[NARG_POS::SECOND]); - } else { - res[NARG_POS::FIRST] = NError(connectDfsCB->status).GetNapiErr(connectDfsCB->cbBase.cbInfo.env); - napi_reject_deferred(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.deferred, res[NARG_POS::FIRST]); - } - } + work->data = static_cast(connectDfsCB); - napi_close_handle_scope(connectDfsCB->cbBase.cbInfo.env, scope); - delete connectDfsCB; - connectDfsCB = nullptr; - HILOGI("UvWorkAfterOnStaus end"); + auto task = [work] () { + UvWorkAfterOnStaus(work); }; auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (rev != ERRNO_NOERR) { delete connectDfsCB; connectDfsCB = nullptr; + delete work; + work = nullptr; } HILOGI("NAPIDfsListener::OnStatus end"); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index 513cfc0522..adf01553f6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -617,63 +617,18 @@ uv_work_t *Copy::GetUVwork(std::shared_ptr infos) void Copy::OnFileReceive(std::shared_ptr infos) { - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - auto iter = jsCbMap_.find(*infos); - if (iter == jsCbMap_.end()) { - HILOGE("Failed to find callback"); - return nullptr; - } - auto callback = iter->second; - infos->env = callback->env; - entry = new (std::nothrow) UvEntry(iter->second, infos); - if (entry == nullptr) { - HILOGE("entry ptr is nullptr."); - return nullptr; - } - entry->progressSize = callback->progressSize; - entry->totalSize = callback->totalSize; + uv_work_t *work = GetUVwork(infos); + if (work == nullptr) { + HILOGE("failed to get uv work"); + return; } - - auto task = [entry] () { - if (entry == nullptr) { - HILOGE("entry pointer is nullptr."); - return; - } - auto processedSize = entry->progressSize; - if (processedSize < entry->callback->maxProgressSize) { - return; - } - entry->callback->maxProgressSize = processedSize; - - napi_handle_scope scope = nullptr; - napi_env env = entry->callback->env; - napi_status status = napi_open_handle_scope(env, &scope); - if (status != napi_ok) { - HILOGE("Failed to open handle scope, status: %{public}d.", status); - return; - } - NVal obj = NVal::CreateObject(env); - if (processedSize <= MAX_VALUE && entry->totalSize <= MAX_VALUE) { - obj.AddProp("processedSize", NVal::CreateInt64(env, processedSize).val_); - obj.AddProp("totalSize", NVal::CreateInt64(env, entry->totalSize).val_); - } - napi_value result = nullptr; - napi_value jsCallback = entry->callback->nRef.Deref(env).val_; - status = napi_call_function(env, nullptr, jsCallback, 1, &(obj.val_), &result); - if (status != napi_ok) { - HILOGE("Failed to get result, status: %{public}d.", status); - } - status = napi_close_handle_scope(env, scope); - if (status != napi_ok) { - HILOGE("Failed to close scope, status: %{public}d.", status); - } + auto task = [work] () { + ReceiveComplete(work); }; - auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret != 0) { HILOGE("failed to uv_queue_work"); + delete (reinterpret_cast(work->data)); delete entry; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.h b/interfaces/kits/js/src/mod_fs/properties/copy.h index e0c30ca703..5c3a475d3c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy.h @@ -151,7 +151,7 @@ private: static void GetNotifyEvent(std::shared_ptr infos); static void StartNotify(std::shared_ptr infos, std::shared_ptr callback); static uv_work_t *GetUVwork(std::shared_ptr infos); - static void ReceiveComplete(uv_work_t *work, int stat); + static void ReceiveComplete(uv_work_t *work); static std::shared_ptr GetRegisteredListener(std::shared_ptr infos); static void CloseNotifyFd(std::shared_ptr infos, std::shared_ptr callback); static void CloseNotifyFdLocked(std::shared_ptr infos, std::shared_ptr callback); diff --git a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp index 0bc39258f3..1a2e82c846 100644 --- a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp +++ b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp @@ -204,19 +204,6 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) ctx_->cbExec_ = nullptr; ctx_->cbComplete_ = nullptr; - uv_loop_s *loop = nullptr; - napi_status status = napi_get_uv_event_loop(env_, &loop); - if (status != napi_ok) { - HILOGE("Failed to get uv event loop"); - return; - } - - auto work = CreateUniquePtr(); - if (!work) { - HILOGE("Failed to new uv_work_t"); - return; - } - struct WorkArgs { NAsyncWorkCallback *ptr = nullptr; NContextCBComplete cb; @@ -225,10 +212,10 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) workArgs->ptr = this; workArgs->cb = cbComplete; - auto workArgs = workArgs.get(); auto task = [workArgs] () { - AfterWorkCallback(workArgs->ptr->env_, napi_ok, workArgs->ptr->ctx_, workArgs->cb); - delete workArgs; + auto workArgsRaw = workArgs.get(); + AfterWorkCallback(workArgsRaw->ptr->env_, napi_ok, workArgsRaw->ptr->ctx_, workArgsRaw->cb); + delete workArgsRaw; }; auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret) { @@ -237,7 +224,6 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) return; } workArgs.release(); - work.release(); } } // namespace LibN } // namespace FileManagement -- Gitee From 0a540948fae85949758944cf936bfda394540b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 28 Dec 2024 15:40:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=BB=86=E8=8A=82=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/mod_fs/properties/copy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index adf01553f6..2dfb49be31 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -629,7 +629,7 @@ void Copy::OnFileReceive(std::shared_ptr infos) if (ret != 0) { HILOGE("failed to uv_queue_work"); delete (reinterpret_cast(work->data)); - delete entry; + delete work; } } -- Gitee From 5188a213aec8fcf53cc4819a6a6dedaa47c7a9e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 28 Dec 2024 15:50:49 +0800 Subject: [PATCH 4/4] =?UTF-8?q?lambda=E6=8D=95=E8=8E=B7=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp index 1a2e82c846..119cc1061b 100644 --- a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp +++ b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp @@ -211,9 +211,8 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) auto workArgs = make_unique(); workArgs->ptr = this; workArgs->cb = cbComplete; - - auto task = [workArgs] () { - auto workArgsRaw = workArgs.get(); + auto workArgsRaw = workArgs.get(); + auto task = [workArgsRaw] () { AfterWorkCallback(workArgsRaw->ptr->env_, napi_ok, workArgsRaw->ptr->ctx_, workArgsRaw->cb); delete workArgsRaw; }; -- Gitee