From dcdc114cad8e2cd34d2d0448eac49e0dd2204638 Mon Sep 17 00:00:00 2001 From: swg3156201044 Date: Thu, 7 Nov 2024 06:33:26 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90=E7=B3=BB=E7=BB=9F=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E3=80=91napi=E5=81=87=E5=BC=82=E6=AD=A5=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: swg3156201044 --- .../napi/osaccount/include/napi_os_account.h | 2 +- .../napi/osaccount/src/napi_os_account.cpp | 104 ++++++++---------- 2 files changed, 45 insertions(+), 61 deletions(-) diff --git a/interfaces/kits/napi/osaccount/include/napi_os_account.h b/interfaces/kits/napi/osaccount/include/napi_os_account.h index f39689efb..d58d43b08 100644 --- a/interfaces/kits/napi/osaccount/include/napi_os_account.h +++ b/interfaces/kits/napi/osaccount/include/napi_os_account.h @@ -368,7 +368,7 @@ napi_value Subscribe(napi_env env, napi_callback_info cbInfo); napi_value Unsubscribe(napi_env env, napi_callback_info cbInfo); -void UvQueueWorkOnAccountsSubNotify(uv_work_t *work, int status); +std::function OnAccountsSubNotifyTask(std::shared_ptr subscriberOAWorkerData); void UnsubscribeSync(napi_env env, UnsubscribeCBInfo *unsubscribeCBInfo); diff --git a/interfaces/kits/napi/osaccount/src/napi_os_account.cpp b/interfaces/kits/napi/osaccount/src/napi_os_account.cpp index a107b7e48..29d9b9a1c 100644 --- a/interfaces/kits/napi/osaccount/src/napi_os_account.cpp +++ b/interfaces/kits/napi/osaccount/src/napi_os_account.cpp @@ -1532,21 +1532,9 @@ void SubscriberPtr::OnAccountsSwitch(const int &newId, const int &oldId) void SubscriberPtr::OnAccountsSubNotify(const int &newId, const int &oldId) { - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { - ACCOUNT_LOGE("loop instance is nullptr"); - return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - ACCOUNT_LOGE("insufficient memory for work!"); - return; - } - SubscriberOAWorker *subscriberOAWorker = new (std::nothrow) SubscriberOAWorker(); + std::shared_ptr subscriberOAWorker = std::make_shared(); if (subscriberOAWorker == nullptr) { ACCOUNT_LOGE("insufficient memory for SubscriberAccountsWorker!"); - delete work; return; } subscriberOAWorker->oldId = oldId; @@ -1554,17 +1542,15 @@ void SubscriberPtr::OnAccountsSubNotify(const int &newId, const int &oldId) subscriberOAWorker->env = env_; subscriberOAWorker->ref = ref_; subscriberOAWorker->subscriber = this; - work->data = reinterpret_cast(subscriberOAWorker); - int32_t ret = - uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAccountsSubNotify, uv_qos_default); - if (ret != 0) { - ACCOUNT_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", ret); - delete work; - delete subscriberOAWorker; + auto task = OnAccountsSubNotifyTask(subscriberOAWorker); + if (napi_ok != napi_send_event(env_, task, napi_eprio_vip)) { + ACCOUNT_LOGE("Post task failed"); + return; } + ACCOUNT_LOGI("Post task finish"); } -static napi_value CreateSwitchEventInfoObj(std::unique_ptr &subscriberOAWorker) +static napi_value CreateSwitchEventInfoObj(const std::shared_ptr &subscriberOAWorker) { napi_env env = subscriberOAWorker->env; napi_value objInfo = nullptr; @@ -1579,52 +1565,50 @@ static napi_value CreateSwitchEventInfoObj(std::unique_ptr & return objInfo; } -void UvQueueWorkOnAccountsSubNotify(uv_work_t *work, int status) +std::function OnAccountsSubNotifyTask(std::shared_ptr subscriberOAWorkerData) { - std::unique_ptr workPtr(work); - napi_handle_scope scope = nullptr; - if (!InitUvWorkCallbackEnv(work, scope)) { - return; - } - std::unique_ptr subscriberOAWorkerData(reinterpret_cast(work->data)); - bool isFound = false; - { - std::lock_guard lock(g_lockForOsAccountSubscribers); - SubscriberPtr *subscriber = subscriberOAWorkerData->subscriber; - for (auto subscriberInstance : g_osAccountSubscribers) { - isFound = std::any_of(subscriberInstance.second.begin(), subscriberInstance.second.end(), - [subscriber](const SubscribeCBInfo *item) { + return [subscriberOAWorkerData] { + ACCOUNT_LOGI("Enter OnAccountsSubNotify task"); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(subscriberOAWorkerData->env, &scope); + if (scope == nullptr) { + ACCOUNT_LOGE("Fail to open scope"); + return; + } + bool isFound = false; + { + std::lock_guard lock(g_lockForOsAccountSubscribers); + SubscriberPtr *subscriber = subscriberOAWorkerData->subscriber; + for (auto subscriberInstance : g_osAccountSubscribers) { + isFound = std::any_of(subscriberInstance.second.begin(), subscriberInstance.second.end(), + [subscriber] (const SubscribeCBInfo *item) { return item->subscriber.get() == subscriber; }); - if (isFound) { - ACCOUNT_LOGD("os account subscriber has been found."); - break; + if (isFound) { + ACCOUNT_LOGD("OsAccount subscriber has been found."); + break; + } } } - } - if (isFound) { - OsAccountSubscribeInfo subscribeInfo; - OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType; - subscriberOAWorkerData->subscriber->GetSubscribeInfo(subscribeInfo); - subscribeInfo.GetOsAccountSubscribeType(osSubscribeType); + if (isFound) { + OsAccountSubscribeInfo subscribeInfo; + OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType; + subscriberOAWorkerData->subscriber->GetSubscribeInfo(subscribeInfo); + subscribeInfo.GetOsAccountSubscribeType(osSubscribeType); - napi_value result[ARGS_SIZE_ONE] = {nullptr}; - if ((osSubscribeType == SWITCHING || osSubscribeType == SWITCHED)) { - ACCOUNT_LOGI("Switch condition, return oldId=%{public}d and newId=%{public}d.", - subscriberOAWorkerData->oldId, subscriberOAWorkerData->newId); - result[PARAMZERO] = CreateSwitchEventInfoObj(subscriberOAWorkerData); - } else { - napi_create_int32(subscriberOAWorkerData->env, subscriberOAWorkerData->newId, &result[PARAMZERO]); + napi_value result[ARGS_SIZE_ONE] = { nullptr }; + if ((osSubscribeType == SWITCHING || osSubscribeType == SWITCHED)) { + ACCOUNT_LOGI("Switch condition, return oldId=%{public}d and newId=%{public}d.", + subscriberOAWorkerData->oldId, subscriberOAWorkerData->newId); + result[PARAMZERO] = CreateSwitchEventInfoObj(subscriberOAWorkerData); + } else { + napi_create_int32(subscriberOAWorkerData->env, subscriberOAWorkerData->newId, &result[PARAMZERO]); + } + NapiCallVoidFunction( + subscriberOAWorkerData->env, &result[PARAMZERO], ARGS_SIZE_ONE, subscriberOAWorkerData->ref); } - napi_value undefined = nullptr; - napi_get_undefined(subscriberOAWorkerData->env, &undefined); - napi_value callback = nullptr; - napi_get_reference_value(subscriberOAWorkerData->env, subscriberOAWorkerData->ref, &callback); - napi_value resultOut = nullptr; - napi_call_function( - subscriberOAWorkerData->env, undefined, callback, ARGS_SIZE_ONE, &result[0], &resultOut); - } - napi_close_handle_scope(subscriberOAWorkerData->env, scope); + napi_close_handle_scope(subscriberOAWorkerData->env, scope); + }; } void SubscriberPtr::SetEnv(const napi_env &env) From e3ab1f5032cb768925416358ae080b66077a92f1 Mon Sep 17 00:00:00 2001 From: swg3156201044 Date: Fri, 8 Nov 2024 11:44:52 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?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: swg3156201044 --- interfaces/kits/napi/osaccount/include/napi_os_account.h | 2 +- interfaces/kits/napi/osaccount/src/napi_os_account.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/napi/osaccount/include/napi_os_account.h b/interfaces/kits/napi/osaccount/include/napi_os_account.h index d58d43b08..bc9f736bc 100644 --- a/interfaces/kits/napi/osaccount/include/napi_os_account.h +++ b/interfaces/kits/napi/osaccount/include/napi_os_account.h @@ -368,7 +368,7 @@ napi_value Subscribe(napi_env env, napi_callback_info cbInfo); napi_value Unsubscribe(napi_env env, napi_callback_info cbInfo); -std::function OnAccountsSubNotifyTask(std::shared_ptr subscriberOAWorkerData); +std::function OnAccountsSubNotifyTask(const std::shared_ptr &subscriberOAWorkerData); void UnsubscribeSync(napi_env env, UnsubscribeCBInfo *unsubscribeCBInfo); diff --git a/interfaces/kits/napi/osaccount/src/napi_os_account.cpp b/interfaces/kits/napi/osaccount/src/napi_os_account.cpp index 29d9b9a1c..c418c4c1d 100644 --- a/interfaces/kits/napi/osaccount/src/napi_os_account.cpp +++ b/interfaces/kits/napi/osaccount/src/napi_os_account.cpp @@ -1565,7 +1565,7 @@ static napi_value CreateSwitchEventInfoObj(const std::shared_ptr OnAccountsSubNotifyTask(std::shared_ptr subscriberOAWorkerData) +std::function OnAccountsSubNotifyTask(const std::shared_ptr &subscriberOAWorkerData) { return [subscriberOAWorkerData] { ACCOUNT_LOGI("Enter OnAccountsSubNotify task"); @@ -1582,7 +1582,7 @@ std::function OnAccountsSubNotifyTask(std::shared_ptrsubscriber.get() == subscriber; + return item->subscriber.get() == subscriber; }); if (isFound) { ACCOUNT_LOGD("OsAccount subscriber has been found.");