diff --git a/interfaces/innerkits/acfwk/include/accessibility_config_observer.h b/interfaces/innerkits/acfwk/include/accessibility_config_observer.h index f2b220cc3b7f2e7e311898a8239f291d11f72cc5..e875f171acfb8dae2892493865575ca7975701fb 100644 --- a/interfaces/innerkits/acfwk/include/accessibility_config_observer.h +++ b/interfaces/innerkits/acfwk/include/accessibility_config_observer.h @@ -18,6 +18,7 @@ #include #include +#include #include "accessibility_config.h" #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -31,11 +32,17 @@ public: void OnConfigChanged(const OHOS::AccessibilityConfig::ConfigValue& value); void NotifyStateChanged2JS(bool enabled); + int NotifyStateChanged(uv_work_t *work); void NotifyPropertyChanged2JS(const OHOS::AccessibilityConfig::CaptionProperty &caption); + int NotifyPropertyChanged(uv_work_t *work); void NotifyStringChanged2JS(const std::string& value); + int NotifyStringChanged(uv_work_t *work); void NotifyIntChanged2JS(int32_t value); + int NotifyIntChanged(uv_work_t *work); void NotifyUintChanged2JS(uint32_t value); + int NotifyUintChanged(uv_work_t *work); void NotifyFloatChanged2JS(float value); + int NotifyFloatChanged(uv_work_t *work); napi_env env_ = nullptr; napi_ref handlerRef_ = nullptr; diff --git a/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension.cpp b/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension.cpp index 25f8597a05213d4fbbd1daf6aca78948d6d7a3f2..f1aa89aed9757a81e0bf2bbbf97842cce701cba0 100644 --- a/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension.cpp +++ b/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension.cpp @@ -275,6 +275,11 @@ void ConvertAccessibilityElementToJS(napi_env env, napi_value objEventInfo, HILOG_ERROR("Failed to create AccessibilityElement."); return; } + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + if (scope == nullptr) { + return; + } napi_value nTargetObject = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, NAccessibilityElement::consRef_, &constructor); @@ -293,6 +298,7 @@ void ConvertAccessibilityElementToJS(napi_env env, napi_value objEventInfo, nullptr); HILOG_DEBUG("napi_wrap status: %{public}d", (int)sts); NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objEventInfo, "target", nTargetObject)); + napi_close_handle_scope(env, scope); } void NAccessibilityExtension::OnAccessibilityEvent(const AccessibilityEventInfo& eventInfo) @@ -329,6 +335,12 @@ void NAccessibilityExtension::OnAccessibilityEvent(const AccessibilityEventInfo& [](uv_work_t *work) {}, [](uv_work_t *work, int status) { AccessibilityEventInfoCallbackInfo *data = static_cast(work->data); + + napi_handle_scope scope = nullptr; + napi_open_handle_scope(reinterpret_cast(data->engine_), &scope); + if (scope == nullptr) { + return; + } napi_value napiEventInfo = nullptr; napi_create_object(reinterpret_cast(data->engine_), &napiEventInfo); @@ -351,7 +363,7 @@ void NAccessibilityExtension::OnAccessibilityEvent(const AccessibilityEventInfo& NativeValue* nativeEventInfo = reinterpret_cast(napiEventInfo); NativeValue* argv[] = {nativeEventInfo}; data->extension_->CallObjectMethod("onAccessibilityEvent", argv, 1); - + napi_close_handle_scope(reinterpret_cast(data->engine_), scope); delete data; data = nullptr; delete work; @@ -394,6 +406,11 @@ bool NAccessibilityExtension::OnKeyPressEvent(const std::shared_ptr(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(reinterpret_cast(data->engine_), &scope); + if (scope == nullptr) { + return; + } napi_value napiEventInfo = nullptr; if (napi_create_object(reinterpret_cast(data->engine_), &napiEventInfo) != napi_ok) { HILOG_ERROR("Create keyEvent object failed."); @@ -422,7 +439,7 @@ bool NAccessibilityExtension::OnKeyPressEvent(const std::shared_ptrsyncPromise_.set_value(result); - + napi_close_handle_scope(reinterpret_cast(data->engine_), scope); delete data; data = nullptr; delete work; diff --git a/interfaces/kits/napi/src/napi_accessibility_config_observer.cpp b/interfaces/kits/napi/src/napi_accessibility_config_observer.cpp index fe2d2ef28237bcb3ce2964035cba597b8bc4ccf7..9dc6d3ace20235360190504e3de878e32acebc13 100644 --- a/interfaces/kits/napi/src/napi_accessibility_config_observer.cpp +++ b/interfaces/kits/napi/src/napi_accessibility_config_observer.cpp @@ -81,32 +81,18 @@ void NAccessibilityConfigObserver::OnConfigChanged(const ConfigValue &value) } } -void NAccessibilityConfigObserver::NotifyStateChanged2JS(bool enabled) +int NAccessibilityConfigObserver::NotifyStateChanged(uv_work_t *work) { - HILOG_INFO("id = [%{public}d] enabled = [%{public}s]", static_cast(configId_), enabled ? "true" : "false"); - - StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo(); - if (!callbackInfo) { - HILOG_ERROR("Failed to create callbackInfo."); - return; - } - callbackInfo->state_ = enabled; - callbackInfo->env_ = env_; - callbackInfo->ref_ = handlerRef_; - uv_work_t *work = new(std::nothrow) uv_work_t; - if (!work) { - HILOG_ERROR("Failed to create work."); - delete callbackInfo; - callbackInfo = nullptr; - return; - } - work->data = static_cast(callbackInfo); - uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env_, &loop); int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { StateCallbackInfo *callbackInfo = static_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(callbackInfo->env_, &scope); + if (scope == nullptr) { + return; + } napi_value jsEvent; napi_create_object(callbackInfo->env_, &jsEvent); napi_get_boolean(callbackInfo->env_, callbackInfo->state_, &jsEvent); @@ -120,11 +106,213 @@ void NAccessibilityConfigObserver::NotifyStateChanged2JS(bool enabled) int32_t result; napi_get_value_int32(callbackInfo->env_, callResult, &result); HILOG_INFO("NotifyStateChangedJS napi_call_function result[%{public}d]", result); + napi_close_handle_scope(callbackInfo->env_, scope); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + }); + return ret; +} + +int NAccessibilityConfigObserver::NotifyPropertyChanged(uv_work_t *work) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + CaptionCallbackInfo *callbackInfo = static_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(callbackInfo->env_, &scope); + if (scope == nullptr) { + return; + } + napi_value jsEvent; + napi_create_object(callbackInfo->env_, &jsEvent); + ConvertCaptionPropertyToJS(callbackInfo->env_, jsEvent, callbackInfo->caption_); + + napi_value handler = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); + napi_value undefined = nullptr; + napi_get_undefined(callbackInfo->env_, &undefined); + napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); + int32_t result; + napi_get_value_int32(callbackInfo->env_, callResult, &result); + HILOG_INFO("NotifyPropertyChangedJS napi_call_function result[%{public}d]", result); + napi_close_handle_scope(callbackInfo->env_, scope); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + }); + return ret; +} + +int NAccessibilityConfigObserver::NotifyStringChanged(uv_work_t *work) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + StateCallbackInfo *callbackInfo = static_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(callbackInfo->env_, &scope); + if (scope == nullptr) { + return; + } + napi_value jsEvent; + napi_create_string_utf8(callbackInfo->env_, callbackInfo->stringValue_.c_str(), + callbackInfo->stringValue_.length(), &jsEvent); + napi_value handler = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); + napi_value undefined = nullptr; + napi_get_undefined(callbackInfo->env_, &undefined); + napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); + size_t result; + const uint32_t BUF_SIZE = 1024; + char buf[BUF_SIZE] = {0}; + napi_get_value_string_utf8(callbackInfo->env_, callResult, buf, BUF_SIZE, &result); + HILOG_INFO("NotifyStringChanged2JSInner napi_call_function result[%{public}zu]", result); + napi_close_handle_scope(callbackInfo->env_, scope); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + }); + return ret; +} + +int NAccessibilityConfigObserver::NotifyIntChanged(uv_work_t *work) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + int ret = uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + StateCallbackInfo *callbackInfo = static_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(callbackInfo->env_, &scope); + if (scope == nullptr) { + return; + } + napi_value jsEvent; + napi_create_int32(callbackInfo->env_, callbackInfo->int32Value_, &jsEvent); + + napi_value handler = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); + napi_value undefined = nullptr; + napi_get_undefined(callbackInfo->env_, &undefined); + napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); + int32_t result; + napi_get_value_int32(callbackInfo->env_, callResult, &result); + HILOG_INFO("NotifyIntChanged2JSInner napi_call_function result[%{public}d]", result); + napi_close_handle_scope(callbackInfo->env_, scope); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + }); + return ret; +} + +int NAccessibilityConfigObserver::NotifyUintChanged(uv_work_t *work) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + int ret = uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + StateCallbackInfo *callbackInfo = static_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(callbackInfo->env_, &scope); + if (scope == nullptr) { + return; + } + napi_value jsEvent; + napi_create_uint32(callbackInfo->env_, callbackInfo->uint32Value_, &jsEvent); + + napi_value handler = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); + napi_value undefined = nullptr; + napi_get_undefined(callbackInfo->env_, &undefined); + napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); + uint32_t result; + napi_get_value_uint32(callbackInfo->env_, callResult, &result); + HILOG_INFO("NotifyUintChanged2JSInner napi_call_function result[%{public}d]", result); + napi_close_handle_scope(callbackInfo->env_, scope); + delete callbackInfo; + callbackInfo = nullptr; + delete work; + work = nullptr; + }); + return ret; +} + +int NAccessibilityConfigObserver::NotifyFloatChanged(uv_work_t *work) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + int ret = uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + StateCallbackInfo *callbackInfo = static_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(callbackInfo->env_, &scope); + if (scope == nullptr) { + return; + } + napi_value jsEvent; + napi_create_double(callbackInfo->env_, double(callbackInfo->floatValue_), &jsEvent); + + napi_value handler = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); + napi_value undefined = nullptr; + napi_get_undefined(callbackInfo->env_, &undefined); + napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); + int32_t result; + napi_get_value_int32(callbackInfo->env_, callResult, &result); + HILOG_INFO("NotifyFloatChanged2JSInner napi_call_function result[%{public}d]", result); + napi_close_handle_scope(callbackInfo->env_, scope); delete callbackInfo; callbackInfo = nullptr; delete work; work = nullptr; }); + return ret; +} + +void NAccessibilityConfigObserver::NotifyStateChanged2JS(bool enabled) +{ + HILOG_INFO("id = [%{public}d] enabled = [%{public}s]", static_cast(configId_), enabled ? "true" : "false"); + + StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo(); + if (!callbackInfo) { + HILOG_ERROR("Failed to create callbackInfo."); + return; + } + callbackInfo->state_ = enabled; + callbackInfo->env_ = env_; + callbackInfo->ref_ = handlerRef_; + uv_work_t *work = new(std::nothrow) uv_work_t; + if (!work) { + HILOG_ERROR("Failed to create work."); + delete callbackInfo; + callbackInfo = nullptr; + return; + } + work->data = static_cast(callbackInfo); + int ret = NotifyStateChanged(work); if (ret != 0) { HILOG_ERROR("Failed to execute NotifyStateChanged2JS work queue"); delete callbackInfo; @@ -154,30 +342,7 @@ void NAccessibilityConfigObserver::NotifyPropertyChanged2JS(const OHOS::Accessib return; } work->data = static_cast(callbackInfo); - - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - CaptionCallbackInfo *callbackInfo = static_cast(work->data); - napi_value jsEvent; - napi_create_object(callbackInfo->env_, &jsEvent); - ConvertCaptionPropertyToJS(callbackInfo->env_, jsEvent, callbackInfo->caption_); - - napi_value handler = nullptr; - napi_value callResult = nullptr; - napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); - napi_value undefined = nullptr; - napi_get_undefined(callbackInfo->env_, &undefined); - napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); - int32_t result; - napi_get_value_int32(callbackInfo->env_, callResult, &result); - HILOG_INFO("NotifyPropertyChangedJS napi_call_function result[%{public}d]", result); - delete callbackInfo; - callbackInfo = nullptr; - delete work; - work = nullptr; - }); + int ret = NotifyPropertyChanged(work); if (ret != 0) { HILOG_ERROR("Failed to execute NotifyPropertyChanged2JS work queue"); delete callbackInfo; @@ -207,31 +372,7 @@ void NAccessibilityConfigObserver::NotifyStringChanged2JS(const std::string& val return; } work->data = static_cast(callbackInfo); - - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - StateCallbackInfo *callbackInfo = static_cast(work->data); - napi_value jsEvent; - napi_create_string_utf8(callbackInfo->env_, callbackInfo->stringValue_.c_str(), - callbackInfo->stringValue_.length(), &jsEvent); - napi_value handler = nullptr; - napi_value callResult = nullptr; - napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); - napi_value undefined = nullptr; - napi_get_undefined(callbackInfo->env_, &undefined); - napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); - size_t result; - const uint32_t BUF_SIZE = 1024; - char buf[BUF_SIZE] = {0}; - napi_get_value_string_utf8(callbackInfo->env_, callResult, buf, BUF_SIZE, &result); - HILOG_INFO("NotifyStringChanged2JSInner napi_call_function result[%{public}zu]", result); - delete callbackInfo; - callbackInfo = nullptr; - delete work; - work = nullptr; - }); + int ret = NotifyStringChanged(work); if (ret != 0) { HILOG_ERROR("Failed to execute NotifyStringChanged2JS work queue"); delete callbackInfo; @@ -261,32 +402,7 @@ void NAccessibilityConfigObserver::NotifyIntChanged2JS(int32_t value) return; } work->data = static_cast(callbackInfo); - - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - int ret = uv_queue_work( - loop, - work, - [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - StateCallbackInfo *callbackInfo = static_cast(work->data); - napi_value jsEvent; - napi_create_int32(callbackInfo->env_, callbackInfo->int32Value_, &jsEvent); - - napi_value handler = nullptr; - napi_value callResult = nullptr; - napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); - napi_value undefined = nullptr; - napi_get_undefined(callbackInfo->env_, &undefined); - napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); - int32_t result; - napi_get_value_int32(callbackInfo->env_, callResult, &result); - HILOG_INFO("NotifyIntChanged2JSInner napi_call_function result[%{public}d]", result); - delete callbackInfo; - callbackInfo = nullptr; - delete work; - work = nullptr; - }); + int ret = NotifyIntChanged(work); if (ret != 0) { HILOG_ERROR("Failed to execute NotifyIntChanged2JS work queue"); delete callbackInfo; @@ -316,32 +432,7 @@ void NAccessibilityConfigObserver::NotifyUintChanged2JS(uint32_t value) return; } work->data = static_cast(callbackInfo); - - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - int ret = uv_queue_work( - loop, - work, - [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - StateCallbackInfo *callbackInfo = static_cast(work->data); - napi_value jsEvent; - napi_create_uint32(callbackInfo->env_, callbackInfo->uint32Value_, &jsEvent); - - napi_value handler = nullptr; - napi_value callResult = nullptr; - napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); - napi_value undefined = nullptr; - napi_get_undefined(callbackInfo->env_, &undefined); - napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); - uint32_t result; - napi_get_value_uint32(callbackInfo->env_, callResult, &result); - HILOG_INFO("NotifyUintChanged2JSInner napi_call_function result[%{public}d]", result); - delete callbackInfo; - callbackInfo = nullptr; - delete work; - work = nullptr; - }); + int ret = NotifyUintChanged(work); if (ret != 0) { HILOG_ERROR("Failed to execute NotifyUintChanged2JS work queue"); delete callbackInfo; @@ -371,32 +462,7 @@ void NAccessibilityConfigObserver::NotifyFloatChanged2JS(float value) return; } work->data = static_cast(callbackInfo); - - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - int ret = uv_queue_work( - loop, - work, - [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - StateCallbackInfo *callbackInfo = static_cast(work->data); - napi_value jsEvent; - napi_create_double(callbackInfo->env_, double(callbackInfo->floatValue_), &jsEvent); - - napi_value handler = nullptr; - napi_value callResult = nullptr; - napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler); - napi_value undefined = nullptr; - napi_get_undefined(callbackInfo->env_, &undefined); - napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult); - int32_t result; - napi_get_value_int32(callbackInfo->env_, callResult, &result); - HILOG_INFO("NotifyFloatChanged2JSInner napi_call_function result[%{public}d]", result); - delete callbackInfo; - callbackInfo = nullptr; - delete work; - work = nullptr; - }); + int ret = NotifyFloatChanged(work); if (ret != 0) { HILOG_ERROR("Failed to execute NotifyFloatChanged2JS work queue"); delete callbackInfo;