From 2dc77dcba0e36cf3a96840553f42832260e84d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 28 Dec 2024 10:34:08 +0800 Subject: [PATCH 1/2] =?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 --- .../src/n_async/n_async_work_callback.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 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 4d0a1c98a..803709e99 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; + } + int ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret) { HILOGE("Failed to call uv_queue_work %{public}d", status); return; -- Gitee From 0a6b427e473e67cb856ca50f49b64eb491c384ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 28 Dec 2024 11:13:03 +0800 Subject: [PATCH 2/2] =?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 --- .../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 | 24 ++--- 6 files changed, 206 insertions(+), 162 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 c11015496..b6ea8f2f4 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 c42aa35ab..3c259d2a4 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 324ee94a7..6329ea5cb 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 a905646a5..513cfc052 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 f6de256a6..11a833ccd 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 803709e99..0bc39258f 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 @@ -68,8 +68,8 @@ NAsyncWorkCallback::~NAsyncWorkCallback() NAsyncContextCallback *ctx = ctx_; auto task = [ctx] () { delete ctx; - } - int ret = napi_send_event(env_, task, napi_eprio_immediate); + }; + auto ret = napi_send_event(env_, task, napi_eprio_immediate); if (ret) { HILOGE("Failed to call uv_queue_work %{public}d", status); return; @@ -225,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