From 2838abe812798a0ef55dca6bae43d375baf34429 Mon Sep 17 00:00:00 2001 From: mayunteng_1 Date: Fri, 14 Feb 2025 14:56:18 +0800 Subject: [PATCH] Interface rectification Signed-off-by: mayunteng_1 Change-Id: I17fe803eab2a2c51656effa8c0252bcd1ee54d55 --- .../napi/pointer/include/js_pointer_manager.h | 24 + .../napi/pointer/src/js_pointer_manager.cpp | 472 ++++++++++++------ util/napi/include/util_napi_error.h | 1 + 3 files changed, 356 insertions(+), 141 deletions(-) diff --git a/frameworks/napi/pointer/include/js_pointer_manager.h b/frameworks/napi/pointer/include/js_pointer_manager.h index 0fb9025c8..a20dc26bf 100644 --- a/frameworks/napi/pointer/include/js_pointer_manager.h +++ b/frameworks/napi/pointer/include/js_pointer_manager.h @@ -46,6 +46,14 @@ struct AsyncContext : RefBase { napi_ref callback { nullptr }; int32_t errorCode { -1 }; StreamBuffer reserve; + bool state { false }; + int32_t rows { -1 }; + int32_t primaryButton { -1 }; + int32_t pointerSize { -1 }; + int32_t pointerColor { -1 }; + int32_t pointerStyle { -1 }; + int32_t windowId { -1 }; + int32_t pointerSpeed { -1 }; AsyncContext(napi_env env) : env(env) {} ~AsyncContext(); }; @@ -130,6 +138,22 @@ public: napi_value GetTouchpadScrollRows(napi_env env, napi_value handle = nullptr); napi_value SetCustomCursor(napi_env env, int32_t windowId, CustomCursor cursor, CursorOptions options); + static void AsyncWorkComplete(napi_env env, napi_status status, void* data); + static void GetHoverScrollStateExecute(napi_env env, void* data); + static void SetHoverScrollStateExecute(napi_env env, void* data); + static void GetMouseScrollRowsExecute(napi_env env, void* data); + static void SetMouseScrollRowsExecute(napi_env env, void* data); + static void GetMousePrimaryButtonExecute(napi_env env, void* data); + static void SetMousePrimaryButtonExecute(napi_env env, void* data); + static void GetPointerSizeExecute(napi_env env, void* data); + static void SetPointerSizeExecute(napi_env env, void* data); + static void GetPointerColorExecute(napi_env env, void* data); + static void SetPointerColorExecute(napi_env env, void* data); + static void GetPointerStyleExecute(napi_env env, void* data); + static void SetPointerStyleExecute(napi_env env, void* data); + static void GetPointerSpeedExecute(napi_env env, void* data); + static void SetPointerSpeedExecute(napi_env env, void* data); + private: napi_value SetTouchpadData(napi_env env, napi_value handle, int32_t errorCode); napi_value GetTouchpadBoolData(napi_env env, napi_value handle, bool data, int32_t errorCode); diff --git a/frameworks/napi/pointer/src/js_pointer_manager.cpp b/frameworks/napi/pointer/src/js_pointer_manager.cpp index 9a320641b..f40de940d 100644 --- a/frameworks/napi/pointer/src/js_pointer_manager.cpp +++ b/frameworks/napi/pointer/src/js_pointer_manager.cpp @@ -102,6 +102,38 @@ static bool GetResult(sptr asyncContext, napi_value *results, int3 return true; } +void JsPointerManager::AsyncWorkComplete(napi_env env, napi_status status, void* data) +{ + CALL_DEBUG_ENTER; + sptr asyncContext(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncContext->DecStrongRef(nullptr); + napi_value results[2] = { 0 }; + int32_t size = 2; + if (!GetResult(asyncContext, results, size)) { + MMI_HILOGE("Failed to create napi data"); + return; + } + if (asyncContext->deferred) { + if (asyncContext->errorCode == RET_OK) { + CHKRV(napi_resolve_deferred(env, asyncContext->deferred, results[1]), RESOLVE_DEFERRED); + } else { + CHKRV(napi_reject_deferred(env, asyncContext->deferred, results[0]), REJECT_DEFERRED); + } + } else { + napi_value callback = nullptr; + CHKRV(napi_get_reference_value(env, asyncContext->callback, &callback), GET_REFERENCE_VALUE); + napi_value callResult = nullptr; + CHKRV(napi_call_function(env, nullptr, callback, size, results, &callResult), CALL_FUNCTION); + } +} + void AsyncCallbackWork(sptr asyncContext) { CALL_DEBUG_ENTER; @@ -215,18 +247,21 @@ napi_value JsPointerManager::IsPointerVisibleSync(napi_env env) return result; } +void JsPointerManager::SetPointerColorExecute(napi_env env, void* data) +{ + CALL_DEBUG_ENTER; + sptr asyncContext(static_cast(data)); + int32_t pointerColor = asyncContext->pointerColor; + asyncContext->errorCode = InputManager::GetInstance()->SetPointerColor(pointerColor); + asyncContext->reserve << ReturnType::VOID; +} + napi_value JsPointerManager::SetPointerColor(napi_env env, int32_t color, napi_value handle) { CALL_DEBUG_ENTER; sptr asyncContext = new (std::nothrow) AsyncContext(env); CHKPP(asyncContext); - asyncContext->errorCode = InputManager::GetInstance()->SetPointerColor(color); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } - asyncContext->reserve << ReturnType::VOID; + asyncContext->pointerColor = color; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -237,23 +272,34 @@ napi_value JsPointerManager::SetPointerColor(napi_env env, int32_t color, napi_v } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetPointerColor", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetPointerColorExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } -napi_value JsPointerManager::GetPointerColor(napi_env env, napi_value handle) +void JsPointerManager::GetPointerColorExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); int32_t color = 1; asyncContext->errorCode = InputManager::GetInstance()->GetPointerColor(color); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::NUMBER << color; +} + +napi_value JsPointerManager::GetPointerColor(napi_env env, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); napi_value promise = nullptr; uint32_t initialRefCount = 1; if (handle != nullptr) { @@ -265,7 +311,17 @@ napi_value JsPointerManager::GetPointerColor(napi_env env, napi_value handle) } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetPointerColor", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetPointerColorExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -302,13 +358,21 @@ napi_value JsPointerManager::GetPointerColorSync(napi_env env) return result; } +void JsPointerManager::SetPointerSpeedExecute(napi_env env, void* data) +{ + CALL_DEBUG_ENTER; + sptr asyncContext(static_cast(data)); + int32_t pointerSpeed = asyncContext->pointerSpeed; + asyncContext->errorCode = InputManager::GetInstance()->SetPointerSpeed(pointerSpeed); + asyncContext->reserve << ReturnType::VOID; +} + napi_value JsPointerManager::SetPointerSpeed(napi_env env, int32_t pointerSpeed, napi_value handle) { CALL_DEBUG_ENTER; sptr asyncContext = new (std::nothrow) AsyncContext(env); CHKPP(asyncContext); - asyncContext->errorCode = InputManager::GetInstance()->SetPointerSpeed(pointerSpeed); - asyncContext->reserve << ReturnType::VOID; + asyncContext->pointerSpeed = pointerSpeed; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -316,7 +380,17 @@ napi_value JsPointerManager::SetPointerSpeed(napi_env env, int32_t pointerSpeed, } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetPointerSpeed", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetPointerSpeedExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -332,14 +406,20 @@ napi_value JsPointerManager::SetPointerSpeedSync(napi_env env, int32_t pointerSp return result; } -napi_value JsPointerManager::GetPointerSpeed(napi_env env, napi_value handle) +void JsPointerManager::GetPointerSpeedExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); int32_t pointerSpeed = 0; asyncContext->errorCode = InputManager::GetInstance()->GetPointerSpeed(pointerSpeed); asyncContext->reserve << ReturnType::NUMBER << pointerSpeed; +} + +napi_value JsPointerManager::GetPointerSpeed(napi_env env, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); napi_value promise = nullptr; uint32_t initial_refcount = 1; if (handle != nullptr) { @@ -348,7 +428,17 @@ napi_value JsPointerManager::GetPointerSpeed(napi_env env, napi_value handle) } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetPointerSpeed", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetPointerSpeedExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -362,57 +452,74 @@ napi_value JsPointerManager::GetPointerSpeedSync(napi_env env) return result; } +void JsPointerManager::SetMouseScrollRowsExecute(napi_env env, void* data) +{ + CALL_DEBUG_ENTER; + sptr asyncContext(static_cast(data)); + int32_t rows = asyncContext->rows; + asyncContext->errorCode = InputManager::GetInstance()->SetMouseScrollRows(rows); + asyncContext->reserve << ReturnType::VOID; +} + napi_value JsPointerManager::SetMouseScrollRows(napi_env env, int32_t rows, napi_value handle) { CALL_DEBUG_ENTER; sptr asyncContext = new (std::nothrow) AsyncContext(env); CHKPP(asyncContext); - asyncContext->errorCode = InputManager::GetInstance()->SetMouseScrollRows(rows); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } - asyncContext->reserve << ReturnType::VOID; + asyncContext->rows = rows; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); - if (napi_get_undefined(env, &promise) != napi_ok) { - CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE); - return nullptr; - } + CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED); } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetMouseScrollRows", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetMouseScrollRowsExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } -napi_value JsPointerManager::GetMouseScrollRows(napi_env env, napi_value handle) +void JsPointerManager::GetMouseScrollRowsExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); int32_t rows = 3; asyncContext->errorCode = InputManager::GetInstance()->GetMouseScrollRows(rows); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::NUMBER << rows; +} + +napi_value JsPointerManager::GetMouseScrollRows(napi_env env, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); napi_value promise = nullptr; - uint32_t initialRefCount = 1; if (handle != nullptr) { - CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE); - if (napi_get_undefined(env, &promise) != napi_ok) { - CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE); - return nullptr; - } + CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); + CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED); } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetMouseScrollRows", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetMouseScrollRowsExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -472,57 +579,74 @@ napi_value JsPointerManager::SetCustomCursorSync(napi_env env, int32_t windowId, return result; } +void JsPointerManager::SetPointerSizeExecute(napi_env env, void* data) +{ + CALL_DEBUG_ENTER; + sptr asyncContext(static_cast(data)); + int32_t size = asyncContext->pointerSize; + asyncContext->errorCode = InputManager::GetInstance()->SetPointerSize(size); + asyncContext->reserve << ReturnType::VOID; +} + napi_value JsPointerManager::SetPointerSize(napi_env env, int32_t size, napi_value handle) { CALL_DEBUG_ENTER; sptr asyncContext = new (std::nothrow) AsyncContext(env); CHKPP(asyncContext); - asyncContext->errorCode = InputManager::GetInstance()->SetPointerSize(size); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } - asyncContext->reserve << ReturnType::VOID; + asyncContext->pointerSize = size; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); - if (napi_get_undefined(env, &promise) != napi_ok) { - CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE); - return nullptr; - } + CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED); } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetPointerSize", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetPointerSizeExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } -napi_value JsPointerManager::GetPointerSize(napi_env env, napi_value handle) +void JsPointerManager::GetPointerSizeExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); int32_t size = 1; asyncContext->errorCode = InputManager::GetInstance()->GetPointerSize(size); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::NUMBER << size; +} + +napi_value JsPointerManager::GetPointerSize(napi_env env, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); napi_value promise = nullptr; - uint32_t initialRefCount = 1; if (handle != nullptr) { - CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE); - if (napi_get_undefined(env, &promise) != napi_ok) { - CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE); - return nullptr; - } + CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); + CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED); } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetPointerSize", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetPointerSizeExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -559,22 +683,24 @@ napi_value JsPointerManager::GetPointerSizeSync(napi_env env) return result; } -napi_value JsPointerManager::SetPointerStyle(napi_env env, int32_t windowid, int32_t pointerStyle, napi_value handle) +void JsPointerManager::SetPointerStyleExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); PointerStyle style; - style.id = pointerStyle; - asyncContext->errorCode = InputManager::GetInstance()->SetPointerStyle(windowid, style); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("The windowId is negative number and no system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, - "windowId is negative number and no system applications use system API"); - return nullptr; - } + style.id = asyncContext->pointerStyle; + int32_t windowId = asyncContext->windowId; + asyncContext->errorCode = InputManager::GetInstance()->SetPointerStyle(windowId, style); asyncContext->reserve << ReturnType::VOID; +} +napi_value JsPointerManager::SetPointerStyle(napi_env env, int32_t windowId, int32_t pointerStyle, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); + asyncContext->pointerStyle = pointerStyle; + asyncContext->windowId = windowId; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -582,7 +708,17 @@ napi_value JsPointerManager::SetPointerStyle(napi_env env, int32_t windowid, int } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetPointerStyle", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetPointerStyleExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -608,20 +744,22 @@ napi_value JsPointerManager::SetPointerStyleSync(napi_env env, int32_t windowid, return result; } -napi_value JsPointerManager::GetPointerStyle(napi_env env, int32_t windowid, napi_value handle) +void JsPointerManager::GetPointerStyleExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); + int32_t windowId = asyncContext->windowId; PointerStyle pointerStyle; - asyncContext->errorCode = InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("WindowId is negative number and no system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, - "WindowId is negative number and no system applications use system API"); - return nullptr; - } + asyncContext->errorCode = InputManager::GetInstance()->GetPointerStyle(windowId, pointerStyle); asyncContext->reserve << ReturnType::NUMBER << pointerStyle.id; +} + +napi_value JsPointerManager::GetPointerStyle(napi_env env, int32_t windowId, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); + asyncContext->windowId = windowId; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -629,7 +767,17 @@ napi_value JsPointerManager::GetPointerStyle(napi_env env, int32_t windowid, nap } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetPointerStyle", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetPointerStyleExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } @@ -696,20 +844,21 @@ napi_value JsPointerManager::LeaveCaptureMode(napi_env env, int32_t windowId, na return promise; } -napi_value JsPointerManager::SetMousePrimaryButton(napi_env env, int32_t primaryButton, napi_value handle) +void JsPointerManager::SetMousePrimaryButtonExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); - + sptr asyncContext(static_cast(data)); + int32_t primaryButton = asyncContext->primaryButton; asyncContext->errorCode = InputManager::GetInstance()->SetMousePrimaryButton(primaryButton); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::VOID; +} +napi_value JsPointerManager::SetMousePrimaryButton(napi_env env, int32_t primaryButton, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); + asyncContext->primaryButton = primaryButton; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -717,23 +866,34 @@ napi_value JsPointerManager::SetMousePrimaryButton(napi_env env, int32_t primary } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetMousePrimaryButton", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetMousePrimaryButtonExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } -napi_value JsPointerManager::GetMousePrimaryButton(napi_env env, napi_value handle) +void JsPointerManager::GetMousePrimaryButtonExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); + sptr asyncContext(static_cast(data)); int32_t primaryButton = 0; asyncContext->errorCode = InputManager::GetInstance()->GetMousePrimaryButton(primaryButton); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::NUMBER << primaryButton; +} + +napi_value JsPointerManager::GetMousePrimaryButton(napi_env env, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -741,24 +901,35 @@ napi_value JsPointerManager::GetMousePrimaryButton(napi_env env, napi_value hand } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetMousePrimaryButton", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetMousePrimaryButtonExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } -napi_value JsPointerManager::SetHoverScrollState(napi_env env, bool state, napi_value handle) +void JsPointerManager::SetHoverScrollStateExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); - + sptr asyncContext(static_cast(data)); + bool state = asyncContext->state; asyncContext->errorCode = InputManager::GetInstance()->SetHoverScrollState(state); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::VOID; +} +napi_value JsPointerManager::SetHoverScrollState(napi_env env, bool state, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); + asyncContext->state = state; napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -766,25 +937,34 @@ napi_value JsPointerManager::SetHoverScrollState(napi_env env, bool state, napi_ } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetHoverScrollState", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, SetHoverScrollStateExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } -napi_value JsPointerManager::GetHoverScrollState(napi_env env, napi_value handle) +void JsPointerManager::GetHoverScrollStateExecute(napi_env env, void* data) { CALL_DEBUG_ENTER; - sptr asyncContext = new (std::nothrow) AsyncContext(env); - CHKPP(asyncContext); - - bool state; + sptr asyncContext(static_cast(data)); + bool state = false; asyncContext->errorCode = InputManager::GetInstance()->GetHoverScrollState(state); - if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) { - MMI_HILOGE("Non system applications use system API"); - THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API"); - return nullptr; - } asyncContext->reserve << ReturnType::BOOL << state; +} +napi_value JsPointerManager::GetHoverScrollState(napi_env env, napi_value handle) +{ + CALL_DEBUG_ENTER; + sptr asyncContext = new (std::nothrow) AsyncContext(env); + CHKPP(asyncContext); napi_value promise = nullptr; if (handle != nullptr) { CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE); @@ -792,7 +972,17 @@ napi_value JsPointerManager::GetHoverScrollState(napi_env env, napi_value handle } else { CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE); } - AsyncCallbackWork(asyncContext); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "GetHoverScrollState", NAPI_AUTO_LENGTH, &resource); + asyncContext->IncStrongRef(nullptr); + napi_status status = napi_create_async_work(env, nullptr, resource, GetHoverScrollStateExecute, AsyncWorkComplete, + asyncContext.GetRefPtr(), &asyncContext->work); + if (status != napi_ok || + napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) { + MMI_HILOGE("Create async work failed"); + asyncContext->DecStrongRef(nullptr); + } return promise; } diff --git a/util/napi/include/util_napi_error.h b/util/napi/include/util_napi_error.h index f73b23e30..b7e5baf74 100644 --- a/util/napi/include/util_napi_error.h +++ b/util/napi/include/util_napi_error.h @@ -60,6 +60,7 @@ const std::map NAPI_ERRORS = { { COMMON_NON_INPUT_APPLICATION, { COMMON_NON_INPUT_APPLICATION, "it is prohibited for non-input applications." } }, { INPUT_DEVICE_NOT_SUPPORTED, { INPUT_DEVICE_NOT_SUPPORTED, "Capability not supported.\n" } }, { ERROR_WINDOW_ID_PERMISSION_DENIED, { ERROR_WINDOW_ID_PERMISSION_DENIED, "windowId is invalid.\n" } }, + { COMMON_USE_SYSAPI_ERROR, { COMMON_USE_SYSAPI_ERROR, "Use sysapi error." } }, }; #define THROWERR_CUSTOM(env, code, msg) \ -- Gitee