!2244 【系统账号】napi假异步整改

Merge pull request !2244 from swg/sync
This commit is contained in:
openharmony_ci 2024-11-08 12:35:53 +00:00 committed by Gitee
commit 857990fc7c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 46 additions and 62 deletions

View File

@ -368,7 +368,7 @@ napi_value Subscribe(napi_env env, napi_callback_info cbInfo);
napi_value Unsubscribe(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<void()> OnAccountsSubNotifyTask(const std::shared_ptr<SubscriberOAWorker> &subscriberOAWorkerData);
void UnsubscribeSync(napi_env env, UnsubscribeCBInfo *unsubscribeCBInfo); void UnsubscribeSync(napi_env env, UnsubscribeCBInfo *unsubscribeCBInfo);

View File

@ -1532,21 +1532,9 @@ void SubscriberPtr::OnAccountsSwitch(const int &newId, const int &oldId)
void SubscriberPtr::OnAccountsSubNotify(const int &newId, const int &oldId) void SubscriberPtr::OnAccountsSubNotify(const int &newId, const int &oldId)
{ {
uv_loop_s *loop = nullptr; std::shared_ptr<SubscriberOAWorker> subscriberOAWorker = std::make_shared<SubscriberOAWorker>();
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();
if (subscriberOAWorker == nullptr) { if (subscriberOAWorker == nullptr) {
ACCOUNT_LOGE("insufficient memory for SubscriberAccountsWorker!"); ACCOUNT_LOGE("insufficient memory for SubscriberAccountsWorker!");
delete work;
return; return;
} }
subscriberOAWorker->oldId = oldId; subscriberOAWorker->oldId = oldId;
@ -1554,17 +1542,15 @@ void SubscriberPtr::OnAccountsSubNotify(const int &newId, const int &oldId)
subscriberOAWorker->env = env_; subscriberOAWorker->env = env_;
subscriberOAWorker->ref = ref_; subscriberOAWorker->ref = ref_;
subscriberOAWorker->subscriber = this; subscriberOAWorker->subscriber = this;
work->data = reinterpret_cast<void *>(subscriberOAWorker); auto task = OnAccountsSubNotifyTask(subscriberOAWorker);
int32_t ret = if (napi_ok != napi_send_event(env_, task, napi_eprio_vip)) {
uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAccountsSubNotify, uv_qos_default); ACCOUNT_LOGE("Post task failed");
if (ret != 0) { return;
ACCOUNT_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", ret);
delete work;
delete subscriberOAWorker;
} }
ACCOUNT_LOGI("Post task finish");
} }
static napi_value CreateSwitchEventInfoObj(std::unique_ptr<SubscriberOAWorker> &subscriberOAWorker) static napi_value CreateSwitchEventInfoObj(const std::shared_ptr<SubscriberOAWorker> &subscriberOAWorker)
{ {
napi_env env = subscriberOAWorker->env; napi_env env = subscriberOAWorker->env;
napi_value objInfo = nullptr; napi_value objInfo = nullptr;
@ -1579,52 +1565,50 @@ static napi_value CreateSwitchEventInfoObj(std::unique_ptr<SubscriberOAWorker> &
return objInfo; return objInfo;
} }
void UvQueueWorkOnAccountsSubNotify(uv_work_t *work, int status) std::function<void()> OnAccountsSubNotifyTask(const std::shared_ptr<SubscriberOAWorker> &subscriberOAWorkerData)
{ {
std::unique_ptr<uv_work_t> workPtr(work); return [subscriberOAWorkerData] {
napi_handle_scope scope = nullptr; ACCOUNT_LOGI("Enter OnAccountsSubNotify task");
if (!InitUvWorkCallbackEnv(work, scope)) { napi_handle_scope scope = nullptr;
return; napi_open_handle_scope(subscriberOAWorkerData->env, &scope);
} if (scope == nullptr) {
std::unique_ptr<SubscriberOAWorker> subscriberOAWorkerData(reinterpret_cast<SubscriberOAWorker *>(work->data)); ACCOUNT_LOGE("Fail to open scope");
bool isFound = false; return;
{ }
std::lock_guard<std::mutex> lock(g_lockForOsAccountSubscribers); bool isFound = false;
SubscriberPtr *subscriber = subscriberOAWorkerData->subscriber; {
for (auto subscriberInstance : g_osAccountSubscribers) { std::lock_guard<std::mutex> lock(g_lockForOsAccountSubscribers);
isFound = std::any_of(subscriberInstance.second.begin(), subscriberInstance.second.end(), SubscriberPtr *subscriber = subscriberOAWorkerData->subscriber;
[subscriber](const SubscribeCBInfo *item) { for (auto subscriberInstance : g_osAccountSubscribers) {
return item->subscriber.get() == subscriber; isFound = std::any_of(subscriberInstance.second.begin(), subscriberInstance.second.end(),
[subscriber] (const SubscribeCBInfo *item) {
return item->subscriber.get() == subscriber;
}); });
if (isFound) { if (isFound) {
ACCOUNT_LOGD("os account subscriber has been found."); ACCOUNT_LOGD("OsAccount subscriber has been found.");
break; break;
}
} }
} }
} if (isFound) {
if (isFound) { OsAccountSubscribeInfo subscribeInfo;
OsAccountSubscribeInfo subscribeInfo; OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType;
OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType; subscriberOAWorkerData->subscriber->GetSubscribeInfo(subscribeInfo);
subscriberOAWorkerData->subscriber->GetSubscribeInfo(subscribeInfo); subscribeInfo.GetOsAccountSubscribeType(osSubscribeType);
subscribeInfo.GetOsAccountSubscribeType(osSubscribeType);
napi_value result[ARGS_SIZE_ONE] = {nullptr}; napi_value result[ARGS_SIZE_ONE] = { nullptr };
if ((osSubscribeType == SWITCHING || osSubscribeType == SWITCHED)) { if ((osSubscribeType == SWITCHING || osSubscribeType == SWITCHED)) {
ACCOUNT_LOGI("Switch condition, return oldId=%{public}d and newId=%{public}d.", ACCOUNT_LOGI("Switch condition, return oldId=%{public}d and newId=%{public}d.",
subscriberOAWorkerData->oldId, subscriberOAWorkerData->newId); subscriberOAWorkerData->oldId, subscriberOAWorkerData->newId);
result[PARAMZERO] = CreateSwitchEventInfoObj(subscriberOAWorkerData); result[PARAMZERO] = CreateSwitchEventInfoObj(subscriberOAWorkerData);
} else { } else {
napi_create_int32(subscriberOAWorkerData->env, subscriberOAWorkerData->newId, &result[PARAMZERO]); napi_create_int32(subscriberOAWorkerData->env, subscriberOAWorkerData->newId, &result[PARAMZERO]);
}
NapiCallVoidFunction(
subscriberOAWorkerData->env, &result[PARAMZERO], ARGS_SIZE_ONE, subscriberOAWorkerData->ref);
} }
napi_value undefined = nullptr; napi_close_handle_scope(subscriberOAWorkerData->env, scope);
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);
} }
void SubscriberPtr::SetEnv(const napi_env &env) void SubscriberPtr::SetEnv(const napi_env &env)