diff --git a/interfaces/kits/napi/accessibility_config/src/napi_accessibility_config.cpp b/interfaces/kits/napi/accessibility_config/src/napi_accessibility_config.cpp index 94933dc3..3b81d3ef 100644 --- a/interfaces/kits/napi/accessibility_config/src/napi_accessibility_config.cpp +++ b/interfaces/kits/napi/accessibility_config/src/napi_accessibility_config.cpp @@ -1055,7 +1055,6 @@ void EnableAbilityListsObserverImpl::UnsubscribeObserver(napi_env env, napi_valu std::lock_guard lock(mutex_); for (auto iter = enableAbilityListsObservers_.begin(); iter != enableAbilityListsObservers_.end();) { if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->callback_)) { - napi_delete_reference((*iter)->env_, (*iter)->callback_); enableAbilityListsObservers_.erase(iter); return; } else { @@ -1068,10 +1067,6 @@ void EnableAbilityListsObserverImpl::UnsubscribeObservers() { HILOG_DEBUG(); std::lock_guard lock(mutex_); - for (auto iter = enableAbilityListsObservers_.begin(); iter != enableAbilityListsObservers_.end();) { - napi_delete_reference((*iter)->env_, (*iter)->callback_); - enableAbilityListsObservers_.erase(iter); - } enableAbilityListsObservers_.clear(); } @@ -1081,7 +1076,6 @@ void EnableAbilityListsObserverImpl::UnsubscribeInstallObserver(napi_env env, na std::lock_guard lock(mutex_); for (auto iter = installAbilityListsObservers_.begin(); iter != installAbilityListsObservers_.end(); iter++) { if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->callback_)) { - napi_delete_reference((*iter)->env_, (*iter)->callback_); installAbilityListsObservers_.erase(iter); return; } @@ -1092,9 +1086,5 @@ void EnableAbilityListsObserverImpl::UnsubscribeInstallObservers() { HILOG_DEBUG(); std::lock_guard lock(mutex_); - for (auto iter = installAbilityListsObservers_.begin(); iter != installAbilityListsObservers_.end();) { - napi_delete_reference((*iter)->env_, (*iter)->callback_); - installAbilityListsObservers_.erase(iter); - } installAbilityListsObservers_.clear(); } diff --git a/interfaces/kits/napi/include/napi_accessibility_system_ability_client.h b/interfaces/kits/napi/include/napi_accessibility_system_ability_client.h index cd4ce069..667b505c 100644 --- a/interfaces/kits/napi/include/napi_accessibility_system_ability_client.h +++ b/interfaces/kits/napi/include/napi_accessibility_system_ability_client.h @@ -59,6 +59,10 @@ private: ffrt::mutex mutex_; OHOS::Accessibility::AccessibilityStateEventType type_; std::vector> observers_ = {}; + +private: + void DeleteObserverReference(napi_env env, std::shared_ptr observer); + int DeleteObserverReferenceWork(napi_env env, uv_work_t *work); }; struct NAccessibilitySystemAbilityClient { diff --git a/interfaces/kits/napi/src/napi_accessibility_system_ability_client.cpp b/interfaces/kits/napi/src/napi_accessibility_system_ability_client.cpp index e24b91ef..e8b993d5 100644 --- a/interfaces/kits/napi/src/napi_accessibility_system_ability_client.cpp +++ b/interfaces/kits/napi/src/napi_accessibility_system_ability_client.cpp @@ -1418,6 +1418,66 @@ void StateListenerImpl::OnStateChanged(const bool state) } } +void StateListenerImpl::DeleteObserverReference(napi_env env, std::shared_ptr observer) +{ + if (observer == nullptr) { + return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + HILOG_ERROR("failed to create work"); + return; + } + AccessibilityCallbackInfo *callbackInfo = new(std::nothrow) AccessibilityCallbackInfo(); + if (callbackInfo == nullptr) { + HILOG_ERROR("failed to create callbackInfo"); + delete work; + work = nullptr; + return; + } + callbackInfo->env_ = observer->env_; + callbackInfo->ref_ = observer->handlerRef_; + work->data = static_cast(callbackInfo); + + int ret = DeleteObserverReferenceWork(env, work); + if (ret != RET_OK) { + HILOG_ERROR("failed to execute delete observer reference"); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + } +} + +int StateListenerImpl::DeleteObserverReferenceWork(napi_env env, uv_work_t *work) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env, &loop); + if (loop == nullptr) { + return RET_ERR_FAILED; + } + int ret = uv_queue_work_with_qos( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + AccessibilityCallbackInfo *callbackInfo = static_cast(work->data); + napi_env tmpEnv = callbackInfo->env_; + auto closeScope = [tmpEnv](napi_handle_scope scope) { + napi_close_handle_scope(tmpEnv, scope); + }; + std::unique_ptr scope( + OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope); + napi_delete_reference(tmpEnv, callbackInfo->ref_); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + }, + uv_qos_default); + return ret; +} + void StateListenerImpl::SubscribeObserver(napi_env env, napi_value observer) { HILOG_INFO(); @@ -1445,7 +1505,7 @@ void StateListenerImpl::UnsubscribeObserver(napi_env env, napi_value observer) std::lock_guard lock(mutex_); for (auto iter = observers_.begin(); iter != observers_.end();) { if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) { - napi_delete_reference((*iter)->env_, (*iter)->handlerRef_); + DeleteObserverReference(env, *iter); observers_.erase(iter); return; } else { @@ -1459,7 +1519,7 @@ void StateListenerImpl::UnsubscribeObservers() HILOG_INFO(); std::lock_guard lock(mutex_); for (auto iter = observers_.begin(); iter != observers_.end();) { - napi_delete_reference((*iter)->env_, (*iter)->handlerRef_); + DeleteObserverReference((*iter)->env_, *iter); observers_.erase(iter); } observers_.clear();