diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp index de076ed7f7c8b575cfb13e971391486a6ef8bbe8..8e0893ae4c599a214ef27effaf549a7130728f3e 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -133,10 +133,12 @@ napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, delegate.get(), - [](napi_env env, void *nativeObject, void *hint) { IMSA_HILOGE("delete JsInputMethodEngineSetting"); }, - nullptr, nullptr); + napi_status status = napi_wrap( + env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsInputMethodEngineSetting napi_wrap failed: %{public}d", status); + return nullptr; + } if (delegate->loop_ == nullptr) { napi_get_uv_event_loop(env, &delegate->loop_); } @@ -384,113 +386,6 @@ bool JsInputMethodEngineSetting::Equals(napi_env env, napi_value value, napi_ref return isEquals; } -uv_work_t *JsInputMethodEngineSetting::GetUVwork(std::string type) -{ - IMSA_HILOGI("run in GetUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsInputMethodEngineSetting::GetStopInputUVwork(std::string type, std::string imeId) -{ - IMSA_HILOGI("run in GetStopInputUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->imeid = imeId; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsInputMethodEngineSetting::GetWindowIDUVwork(std::string type, uint32_t windowid) -{ - IMSA_HILOGI("run in GetWindowIDUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->windowid = windowid; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsInputMethodEngineSetting::GetSubtypeUVwork(std::string type, const SubProperty &property) -{ - IMSA_HILOGI("run in GetSubtypeUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->subProperty = property; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property) { napi_value subType = nullptr; @@ -537,10 +432,11 @@ napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const void JsInputMethodEngineSetting::OnInputStart() { - IMSA_HILOGI("run in OnInputStart"); + IMSA_HILOGD("run in"); std::string type = "inputStart"; uv_work_t *work = GetUVwork(type); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -556,7 +452,7 @@ void JsInputMethodEngineSetting::OnInputStart() } auto getInputStartProperty = [](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 2) { return false; } @@ -577,9 +473,10 @@ void JsInputMethodEngineSetting::OnInputStart() void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) { std::string type = isShow ? "keyboardShow" : "keyboardHide"; - IMSA_HILOGI("run in OnKeyboardStatus: %{public}s", type.c_str()); + IMSA_HILOGD("run in, %{public}s", type.c_str()); uv_work_t *work = GetUVwork(type); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -591,7 +488,7 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) }); auto getKeyboardStatusProperty = [](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -604,10 +501,11 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) { - IMSA_HILOGI("run in OnInputStop"); + IMSA_HILOGD("run in"); std::string type = "inputStop"; - uv_work_t *work = GetStopInputUVwork(type, imeId); + uv_work_t *work = GetUVwork(type, [&imeId](UvEntry &entry) { entry.imeid = imeId; }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -623,7 +521,7 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) } auto getInputStopProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -636,10 +534,11 @@ void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) { - IMSA_HILOGI("run in OnSetCallingWindow"); + IMSA_HILOGD("run in"); std::string type = "setCallingWindow"; - uv_work_t *work = GetWindowIDUVwork(type, windowId); + uv_work_t *work = GetUVwork(type, [windowId](UvEntry &entry) { entry.windowid = windowId; }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -655,7 +554,7 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) } auto getCallingWindowProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -668,10 +567,11 @@ void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) { - IMSA_HILOGI("run in OnSetSubtype"); + IMSA_HILOGD("run in"); std::string type = "setSubtype"; - uv_work_t *work = GetSubtypeUVwork(type, property); + uv_work_t *work = GetUVwork(type, [&property](UvEntry &entry) { entry.subProperty = property; }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -687,7 +587,7 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) } auto getSubtypeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -696,11 +596,40 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr"); return false; } - args[ARGC_ZERO] = {jsObject}; + args[ARGC_ZERO] = { jsObject }; return true; }; JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getSubtypeProperty); }); } + +uv_work_t *JsInputMethodEngineSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) +{ + IMSA_HILOGD("run in, type: %{public}s", type.c_str()); + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + + if (jsCbMap_[type].empty()) { + IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); + return nullptr; + } + entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); + if (entry == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + if (entrySetter != nullptr) { + entrySetter(*entry); + } + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + work->data = entry; + return work; +} } // namespace MiscServices } // namespace OHOS diff --git a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h index e42830389ceaaf34b50cf274bf8b438f863342c5..2ddda538052d251619455d5c30f7ab73670af4e0 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h +++ b/interfaces/kits/js/napi/inputmethodability/js_input_method_engine_setting.h @@ -57,10 +57,6 @@ private: static napi_value GetIMEInstance(napi_env env, napi_callback_info info, int flag); void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); - uv_work_t *GetUVwork(std::string type); - uv_work_t *GetStopInputUVwork(std::string type, std::string imeId); - uv_work_t *GetWindowIDUVwork(std::string type, uint32_t windowid); - uv_work_t *GetSubtypeUVwork(std::string type, const SubProperty &property); static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); static std::string GetStringProperty(napi_env env, napi_value jsString); static constexpr int32_t MAX_VALUE_LEN = 1024; @@ -76,6 +72,8 @@ private: { } }; + using EntrySetter = std::function; + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); uv_loop_s *loop_ = nullptr; std::recursive_mutex mutex_; std::map>> jsCbMap_; diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp index fb9f432587287549ddfa2933b1d51747281e9b76..a89522cf656ab117e5d7c29ab936cc2844aa2b75 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.cpp @@ -51,15 +51,19 @@ napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, controllerObject, - [](napi_env env, void *data, void *hint) { - auto *objInfo = reinterpret_cast(data); - if (objInfo != nullptr) { - delete objInfo; - } - }, - nullptr, nullptr); + auto finalize = [](napi_env env, void *data, void *hint) { + IMSA_HILOGE("JsKeyboardControllerEngine finalize"); + auto *objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + delete objInfo; + } + }; + napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsKeyboardControllerEngine napi_wrap failed: %{public}d", status); + delete controllerObject; + return nullptr; + } return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h index b7c599f956d4aa1c5a6ddd6c832e7c0ac2e9c6dd..95e6aeefb81cf777de39eae384420e1bb9123876 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_controller_engine.h @@ -33,6 +33,7 @@ struct HideKeyboardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -52,6 +53,7 @@ struct HideContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 00fb95d473d8d265ffaa04bbaf35622116a551ae..5a50b01dde9454c644a014d60b9f77989a09b180 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -113,10 +113,12 @@ napi_value JsKeyboardDelegateSetting::JsConstructor(napi_env env, napi_callback_ napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, delegate.get(), - [](napi_env env, void *nativeObject, void *hint) { IMSA_HILOGE("delete JsKeyboardDelegateSetting"); }, nullptr, - nullptr); + napi_status status = napi_wrap( + env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsKeyboardDelegateSetting napi_wrap failed: %{public}d", status); + return nullptr; + } if (delegate->loop_ == nullptr) { napi_get_uv_event_loop(env, &delegate->loop_); } @@ -355,13 +357,16 @@ napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) { - IMSA_HILOGI("run in OnKeyEvent"); + IMSA_HILOGD("run in"); KeyEventPara para{ keyCode, keyStatus, false }; std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp"); auto isDone = std::make_shared>(MAX_TIMEOUT, false); - uv_work_t *work = GetKeyEventUVwork(type, para, isDone); + uv_work_t *work = GetUVwork(type, [¶, isDone](UvEntry &entry) { + entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; + entry.isDone = isDone; + }); if (work == nullptr) { - IMSA_HILOGE("GetKeyEventUVwork nullptr"); + IMSA_HILOGE("failed to get uv work"); return false; } uv_queue_work( @@ -372,12 +377,12 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) delete work; }); auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } - napi_value jsObject = GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, - entry->keyEventPara.keyStatus); + napi_value jsObject = + GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus); if (jsObject == nullptr) { IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr"); return false; @@ -391,129 +396,18 @@ bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) return isDone->GetValue(); } -uv_work_t *JsKeyboardDelegateSetting::GetKeyEventUVwork( - std::string type, KeyEventPara para, std::shared_ptr> &isDone) -{ - IMSA_HILOGI("run in GetKeyEventUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; - entry->isDone = isDone; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - delete entry; - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsKeyboardDelegateSetting::GetCursorUVwork(std::string type, CursorPara para) -{ - IMSA_HILOGI("run in GetCursorUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->curPara.positionX = para.positionX; - entry->curPara.positionY = para.positionY; - entry->curPara.height = para.height; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsKeyboardDelegateSetting::GetSelectionUVwork(std::string type, SelectionPara para) -{ - IMSA_HILOGI("run in GetSelectionUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->selPara.oldBegin = para.oldBegin; - entry->selPara.oldEnd = para.oldEnd; - entry->selPara.newBegin = para.newBegin; - entry->selPara.newEnd = para.newEnd; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - -uv_work_t *JsKeyboardDelegateSetting::GetTextUVwork(std::string type, std::string text) -{ - IMSA_HILOGI("run in GetTextUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->text = text; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { - IMSA_HILOGI("run in OnCursorUpdate"); + IMSA_HILOGD("run in"); CursorPara para{ positionX, positionY, height }; std::string type = "cursorContextChange"; - uv_work_t *work = GetCursorUVwork(type, para); + uv_work_t *work = GetUVwork(type, [¶](UvEntry &entry) { + entry.curPara.positionX = para.positionX; + entry.curPara.positionY = para.positionY; + entry.curPara.height = para.height; + }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -525,7 +419,7 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi }); auto getCursorUpdateProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 3) { return false; } @@ -540,11 +434,17 @@ void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positi void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { - IMSA_HILOGI("run in OnSelectionChange"); + IMSA_HILOGD("run in"); SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd }; std::string type = "selectionChange"; - uv_work_t *work = GetSelectionUVwork(type, para); + uv_work_t *work = GetUVwork(type, [¶](UvEntry &entry) { + entry.selPara.oldBegin = para.oldBegin; + entry.selPara.oldEnd = para.oldEnd; + entry.selPara.newBegin = para.newBegin; + entry.selPara.newEnd = para.newEnd; + }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -556,7 +456,7 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE }); auto getSelectionChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 4) { return false; } @@ -572,10 +472,11 @@ void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldE void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) { - IMSA_HILOGI("run in OnTextChange"); + IMSA_HILOGD("run in"); std::string type = "textChange"; - uv_work_t *work = GetTextUVwork(type, text); + uv_work_t *work = GetUVwork(type, [&text](UvEntry &entry) { entry.text = text; }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -587,7 +488,7 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) }); auto getTextChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc == 0) { return false; } @@ -597,5 +498,34 @@ void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getTextChangeProperty); }); } + +uv_work_t *JsKeyboardDelegateSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) +{ + IMSA_HILOGD("run in, type: %{public}s", type.c_str()); + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + + if (jsCbMap_[type].empty()) { + IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); + return nullptr; + } + entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); + if (entry == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + if (entrySetter != nullptr) { + entrySetter(*entry); + } + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + work->data = entry; + return work; +} } // namespace MiscServices } // namespace OHOS diff --git a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h index 8d73dd78e1432340602d728ea1d8b16242a23b79..e39f06977b69384dc31c36b7a69ae0e2e8b84708 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h +++ b/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.h @@ -130,10 +130,8 @@ private: { } }; - uv_work_t *GetCursorUVwork(std::string type, CursorPara para); - uv_work_t *GetSelectionUVwork(std::string type, SelectionPara para); - uv_work_t *GetTextUVwork(std::string type, std::string text); - uv_work_t *GetKeyEventUVwork(std::string type, KeyEventPara para, std::shared_ptr> &isDone); + using EntrySetter = std::function; + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); uv_loop_s *loop_ = nullptr; std::recursive_mutex mutex_; std::map>> jsCbMap_; diff --git a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp index a8f5352397be7e03ede55660ddfcc32f028ffc51..d6fc398447d1ed34b4f01ce6044b70e7a5f30e9b 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -93,16 +93,19 @@ napi_value JsTextInputClientEngine::JsConstructor(napi_env env, napi_callback_in napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, clientObject, - [](napi_env env, void *data, void *hint) { - auto *objInfo = reinterpret_cast(data); - if (objInfo != nullptr) { - delete objInfo; - } - }, - nullptr, nullptr); - + auto finalize = [](napi_env env, void *data, void *hint) { + IMSA_HILOGE("JsTextInputClientEngine finalize"); + auto *objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + delete objInfo; + } + }; + napi_status status = napi_wrap(env, thisVar, clientObject, finalize, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsTextInputClientEngine napi_wrap failed: %{public}d", status); + delete clientObject; + return nullptr; + } return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h index 2493b5cb79c746333d4b4301271e3d1935a8f484..f0fe9525ac8ade3758b2cc3064443dc54b4fd2f7 100644 --- a/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h +++ b/interfaces/kits/js/napi/inputmethodability/js_text_input_client_engine.h @@ -38,6 +38,7 @@ struct SendKeyFunctionContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -58,6 +59,7 @@ struct MoveCursorContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -79,6 +81,7 @@ struct DeleteForwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -100,6 +103,7 @@ struct DeleteBackwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -121,6 +125,7 @@ struct InsertTextContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -142,6 +147,7 @@ struct GetForwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -163,6 +169,7 @@ struct GetBackwardContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -184,6 +191,7 @@ struct GetEditorAttributeContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodclient/async_call.h b/interfaces/kits/js/napi/inputmethodclient/async_call.h index d61728cbc5676a9102f6f4418ec1ab4af521a94f..a91f6bbf94be3430f4ea889cf708dff4b5a7178b 100644 --- a/interfaces/kits/js/napi/inputmethodclient/async_call.h +++ b/interfaces/kits/js/napi/inputmethodclient/async_call.h @@ -58,7 +58,9 @@ public: if (input_ == nullptr) { return napi_ok; } - return input_(env, argc, argv, self); + auto ret = input_(env, argc, argv, self); + input_ = nullptr; + return ret; } virtual napi_status operator()(napi_env env, napi_value *result) @@ -67,7 +69,9 @@ public: *result = nullptr; return napi_ok; } - return output_(env, result); + auto ret = output_(env, result); + output_ = nullptr; + return ret; } virtual void Exec() @@ -76,6 +80,7 @@ public: return; } exec_(this); + exec_ = nullptr; }; protected: diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 5c90cc05dc0b75fbc855560b9e024eb26e918aae..af866566bcec01cf65c6e6961c97667c388558d4 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -59,16 +59,20 @@ napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, controllerObject, - [](napi_env env, void *data, void *hint) { - auto *objInfo = reinterpret_cast(data); - if (objInfo != nullptr) { - IMSA_HILOGE("objInfo is nullptr"); - delete objInfo; - } - }, - nullptr, nullptr); + auto finalize = [](napi_env env, void *data, void *hint) { + IMSA_HILOGE("JsGetInputMethodController finalize"); + auto *objInfo = reinterpret_cast(data); + if (objInfo != nullptr) { + IMSA_HILOGE("objInfo is nullptr"); + delete objInfo; + } + }; + napi_status status = napi_wrap(env, thisVar, controllerObject, finalize, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsGetInputMethodController napi_wrap failed:%{public}d", status); + delete controllerObject; + return nullptr; + } return thisVar; } diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h index 1b9e9c5adab9e3753b0897294ef83eb37420002e..bd0f9e1965ebb17fe58cb7861d53447273738624 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -35,6 +35,7 @@ struct HandleContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 153e6c4c3aaf743c485d9c7e00ed84ed05bcdce4..260b8ced0b052623098f97f32fcbf89742544a60 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -79,9 +79,12 @@ napi_value JsGetInputMethodSetting::JsConstructor(napi_env env, napi_callback_in napi_get_null(env, &result); return result; } - napi_wrap( - env, thisVar, delegate.get(), - [](napi_env env, void *data, void *hint) { IMSA_HILOGE("delete JsInputMethodSetting"); }, nullptr, nullptr); + napi_status status = napi_wrap( + env, thisVar, delegate.get(), [](napi_env env, void *data, void *hint) {}, nullptr, nullptr); + if (status != napi_ok) { + IMSA_HILOGE("JsGetInputMethodSetting napi_wrap failed: %{public}d", status); + return nullptr; + } if (delegate->loop_ == nullptr) { napi_get_uv_event_loop(env, &delegate->loop_); } @@ -511,41 +514,16 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info return result; } -uv_work_t *JsGetInputMethodSetting::GetImeChangeUVwork( - std::string type, const Property &property, const SubProperty &subProperty) -{ - IMSA_HILOGI("run in GetImeChangeUVwork"); - UvEntry *entry = nullptr; - { - std::lock_guard lock(mutex_); - - if (jsCbMap_[type].empty()) { - IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); - return nullptr; - } - entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); - if (entry == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - entry->property = property; - entry->subProperty = subProperty; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - IMSA_HILOGE("entry ptr is nullptr!"); - return nullptr; - } - work->data = entry; - return work; -} - void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty) { - IMSA_HILOGI("run in OnImeChange"); + IMSA_HILOGD("run in"); std::string type = "imeChange"; - uv_work_t *work = GetImeChangeUVwork(type, property, subProperty); + uv_work_t *work = GetUVwork(type, [&property, &subProperty](UvEntry &entry) { + entry.property = property; + entry.subProperty = subProperty; + }); if (work == nullptr) { + IMSA_HILOGD("failed to get uv entry"); return; } uv_queue_work( @@ -560,7 +538,7 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro return; } auto getImeChangeProperty = [entry](napi_value *args, uint8_t argc, - std::shared_ptr item) -> bool { + std::shared_ptr item) -> bool { if (argc < 2) { return false; } @@ -577,5 +555,34 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro JsUtils::TraverseCallback(entry->vecCopy, ARGC_TWO, getImeChangeProperty); }); } + +uv_work_t *JsGetInputMethodSetting::GetUVwork(const std::string &type, EntrySetter entrySetter) +{ + IMSA_HILOGD("run in, type: %{public}s", type.c_str()); + UvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + + if (jsCbMap_[type].empty()) { + IMSA_HILOGE("%{public}s cb-vector is empty", type.c_str()); + return nullptr; + } + entry = new (std::nothrow) UvEntry(jsCbMap_[type], type); + if (entry == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + if (entrySetter != nullptr) { + entrySetter(*entry); + } + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGE("entry ptr is nullptr!"); + return nullptr; + } + work->data = entry; + return work; +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h index 2b0ff9e5c16ccae86f650423897a1b90a23c0883..eed93cc40cec6f30bd9f24d8763f8ec3999b0cdf 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -42,6 +42,7 @@ struct ListInputContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -63,6 +64,7 @@ struct DisplayOptionalInputMethodContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -84,6 +86,7 @@ struct GetInputMethodControllerContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); @@ -111,7 +114,6 @@ public: private: static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr ctxt); static JsGetInputMethodSetting *GetNative(napi_env env, napi_callback_info info); - uv_work_t *GetImeChangeUVwork(std::string type, const Property &property, const SubProperty &subProperty); static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); static napi_value GetJsConstProperty(napi_env env, uint32_t num); static napi_value DisplayInputMethod(napi_env env, napi_callback_info info, bool needThrowException); @@ -128,6 +130,8 @@ private: { } }; + using EntrySetter = std::function; + uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); static const std::string IMS_CLASS_NAME; static thread_local napi_ref IMSRef_; uv_loop_s *loop_ = nullptr; diff --git a/interfaces/kits/js/napi/inputmethodclient/js_input_method.h b/interfaces/kits/js/napi/inputmethodclient/js_input_method.h index 55c84ca05a366ac8928d495b65fafef2526151f3..95def0c91e6d6e5e6e1eab6c04bd94776f6dc4da 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_input_method.h +++ b/interfaces/kits/js/napi/inputmethodclient/js_input_method.h @@ -41,6 +41,7 @@ struct SwitchInputMethodContext : public AsyncCall::Context { napi_status operator()(napi_env env, napi_value *result) override { if (status != napi_ok) { + output_ = nullptr; return status; } return Context::operator()(env, result); diff --git a/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp b/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp index 3f2d07412f563e9c167e84c9ad003dff8c466a80..f7e16621edd32199fad93d128c4b3dd547e5526e 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp @@ -150,15 +150,17 @@ bool JsUtils::TraverseCallback(const std::vectorenv_, &scope); if (item->threadId_ != std::this_thread::get_id()) { + napi_close_handle_scope(item->env_, scope); continue; } - napi_value args[MAX_ARGMENT_COUNT]; if (!argsProvider(args, MAX_ARGMENT_COUNT, item)) { + napi_close_handle_scope(item->env_, scope); continue; } - napi_value callback = nullptr; napi_value global = nullptr; napi_value result = nullptr; @@ -168,12 +170,10 @@ bool JsUtils::TraverseCallback(const std::vectorenv_, &global); napi_status callStatus = napi_call_function(item->env_, global, callback, paramNum, args, &result); if (callStatus != napi_ok) { - IMSA_HILOGE( - "notify data change failed callStatus:%{public}d", callStatus); + IMSA_HILOGE("notify data change failed callStatus:%{public}d", callStatus); result = nullptr; } } - if (result != nullptr && !isOnKeyEvent) { napi_valuetype valueType = napi_undefined; napi_typeof(item->env_, result, &valueType); @@ -185,6 +185,7 @@ bool JsUtils::TraverseCallback(const std::vectorenv_, scope); } return isOnKeyEvent; }