diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index d4c62a7e068024e1aea32f145a5fdf3b082df686..cf2d0f24da074ddc7371d7d10aec137d16b4d87f 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -369,6 +369,47 @@ void JsKeyboardDelegateSetting::DealKeyEvent(const std::shared_ptr &key } } +void JsKeyboardDelegateSetting::HandleKeyEventResult(const std::shared_ptr &keyEventEntry, + const std::shared_ptr &keyCodeEntry, const sptr &consumer, bool consumeResult) +{ + if (!consumeResult) { + IMSA_HILOGW("ime is not consumed."); + } + auto env = GetNapiEnv(keyEventEntry, keyCodeEntry); + if (env == nullptr) { + IMSA_HILOGW("consumer callback directly."); + if (consumer != nullptr) { + consumer->OnKeyEventResult(consumeResult); + } + return; + } + JsTextInputClientEngine::HandleKeyEventResult(env, consumer, consumeResult); +} + +napi_env JsKeyboardDelegateSetting::GetNapiEnv( + const std::shared_ptr &keyEventEntry, const std::shared_ptr &keyCodeEntry) +{ + if (keyEventEntry != nullptr) { + auto vecCopy = keyEventEntry->vecCopy; + auto iter = std::find_if(vecCopy.begin(), vecCopy.end(), [](const std::shared_ptr &object) { + return object != nullptr && object->threadId_ == std::this_thread::get_id(); + }); + if (iter != vecCopy.end()) { + return (*iter)->env_; + } + } + if (keyCodeEntry != nullptr) { + auto vecCopy = keyCodeEntry->vecCopy; + auto iter = std::find_if(vecCopy.begin(), vecCopy.end(), [](const std::shared_ptr &object) { + return object != nullptr && object->threadId_ == std::this_thread::get_id(); + }); + if (iter != vecCopy.end()) { + return (*iter)->env_; + } + } + return nullptr; +} + bool JsKeyboardDelegateSetting::OnKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) { diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h index 6449f625aac04e3ce660f3c23cbcfeb468020617..3eca70d10682f1eefef62902548e5b67701bde91 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h @@ -57,6 +57,9 @@ private: static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); + void HandleKeyEventResult(const std::shared_ptr &keyEventEntry, + const std::shared_ptr &keyCodeEntry, const sptr &consumer, bool consumeResult); + napi_env GetNapiEnv(const std::shared_ptr &keyEventEntry, const std::shared_ptr &keyCodeEntry); static constexpr int32_t MAX_TIMEOUT = 2000; static const std::string KDS_CLASS_NAME; static thread_local napi_ref KDSRef_; diff --git a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp index be80d33d30d6705fc961dd52f6e68582a201f37f..494be8c8381e99bb8d75fa8c76eb858571253743 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -1154,5 +1154,21 @@ int32_t JsTextInputClientEngine::JsMessageHandler::OnMessage(const ArrayBuffer & eventHandler->PostTask(task, "IMC_MsgHandler_OnMessage", 0, AppExecFwk::EventQueue::Priority::VIP); return ErrorCode::NO_ERROR; } + +void JsTextInputClientEngine::HandleKeyEventResult( + napi_env env, const sptr &consumer, bool consumeResult) +{ + IMSA_HILOGD("run in."); + auto ctxt = std::make_shared(consumeResult, consumer); + auto exec = [ctxt](AsyncCall::Context *ctx) { + if (ctxt->consumer != nullptr) { + ctxt->consumer->OnKeyEventResult(ctxt->consumeResult); + } + ctxt->SetState(napi_ok); + }; + // 0 means JsAPI:HandleKeyEventResult has 0 params at most. + AsyncCall asyncCall(env, ctxt); + ASYNC_POST(env, exec); +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h index b644c95d5ebf25328503ab8b0c8a10c4371fd53d..7d582defa70e3d4f36cf1cbe567ae8a9ba6ee106 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h @@ -23,11 +23,11 @@ #include "global.h" #include "js_message_handler_info.h" #include "js_util.h" +#include "keyevent_consumer_proxy.h" #include "msg_handler_callback_interface.h" #include "native_engine/native_engine.h" #include "native_engine/native_value.h" #include "wm_common.h" - namespace OHOS { namespace MiscServices { struct PrivateCommandInfo { @@ -362,6 +362,21 @@ struct FinishTextPreviewContext : public AsyncCall::Context { } }; +struct KeyEventConsumerContext : public AsyncCall::Context { + sptr consumer{ nullptr }; + bool consumeResult{ false }; + KeyEventConsumerContext(bool consumeResult, sptr consumer) + : Context(nullptr, nullptr), consumer(std::move(consumer)), consumeResult(consumeResult){}; + napi_status operator()(napi_env env, napi_value *result) override + { + if (status_ != napi_ok) { + output_ = nullptr; + return status_; + } + return Context::operator()(env, result); + } +}; + class JsTextInputClientEngine { public: JsTextInputClientEngine() = default; @@ -398,6 +413,7 @@ public: static napi_value FinishTextPreviewSync(napi_env env, napi_callback_info info); static napi_value SendMessage(napi_env env, napi_callback_info info); static napi_value RecvMessage(napi_env env, napi_callback_info info); + static void HandleKeyEventResult(napi_env env, const sptr &consumer, bool consumeResult); class JsMessageHandler : public MsgHandlerCallbackInterface { public: explicit JsMessageHandler(napi_env env, napi_value onTerminated, napi_value onMessage) diff --git a/frameworks/js/napi/inputmethodclient/async_call.cpp b/frameworks/js/napi/inputmethodclient/async_call.cpp index 80d1720d39ac9417f2f1107244d35d28e1e11cb2..4d8ea054cb9cf1808529cc42978b010bc6fc3949 100644 --- a/frameworks/js/napi/inputmethodclient/async_call.cpp +++ b/frameworks/js/napi/inputmethodclient/async_call.cpp @@ -52,6 +52,14 @@ AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptrself); } +AsyncCall::AsyncCall(napi_env env, std::shared_ptr context) +{ + context_ = new AsyncContext(); + NAPI_ASSERT_RETURN_VOID(env, context_ != nullptr, "context_ != nullptr"); + context_->isNeedCb = false; + context_->ctx = std::move(context); +} + AsyncCall::~AsyncCall() { if (context_ == nullptr) { @@ -97,10 +105,12 @@ napi_value AsyncCall::Post(napi_env env, Context::ExecAction exec, std::shared_p } context_->ctx->exec_ = std::move(exec); napi_value promise = nullptr; - if (context_->callback == nullptr) { - napi_create_promise(env, &context_->defer, &promise); - } else { - napi_get_undefined(env, &promise); + if (context_->isNeedCb) { + if (context_->callback == nullptr) { + napi_create_promise(env, &context_->defer, &promise); + } else { + napi_get_undefined(env, &promise); + } } napi_async_work work = context_->work; napi_value resource = nullptr; @@ -199,10 +209,12 @@ void AsyncCall::OnComplete(napi_env env, napi_status status, void *data) napi_reject_deferred(env, context->defer, result[ARG_ERROR]); } } else { - napi_value callback = nullptr; - napi_get_reference_value(env, context->callback, &callback); - napi_value returnValue; - napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue); + if (context->callback != nullptr) { + napi_value callback = nullptr; + napi_get_reference_value(env, context->callback, &callback); + napi_value returnValue; + napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue); + } } DeleteContext(env, context); } diff --git a/frameworks/js/napi/inputmethodclient/async_call.h b/frameworks/js/napi/inputmethodclient/async_call.h index f37a26da7d5d8ed71796cbdb5da35dec8a7a859f..f170edd9d5b47a6dc28aad425f2330dbbacf6b00 100644 --- a/frameworks/js/napi/inputmethodclient/async_call.h +++ b/frameworks/js/napi/inputmethodclient/async_call.h @@ -116,6 +116,7 @@ public: }; AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr context, size_t maxParamCount); + AsyncCall(napi_env env, std::shared_ptr context); ~AsyncCall(); napi_value Call(napi_env env, Context::ExecAction exec = nullptr, const std::string &resourceName = "AsyncCall"); napi_value Post(napi_env env, Context::ExecAction exec, std::shared_ptr queue, const char *func); @@ -133,6 +134,7 @@ private: napi_deferred defer = nullptr; napi_async_work work = nullptr; std::shared_ptr queue = nullptr; + bool isNeedCb{ true }; }; static void DeleteContext(napi_env env, AsyncContext *context); diff --git a/frameworks/native/inputmethod_controller/src/keyevent_consumer_proxy.cpp b/frameworks/native/inputmethod_controller/src/keyevent_consumer_proxy.cpp index ae17e7456d2d9b19442e63b574aafe0c09579a45..b3c62e88111af21742b8e47004c2fb21dab79c69 100644 --- a/frameworks/native/inputmethod_controller/src/keyevent_consumer_proxy.cpp +++ b/frameworks/native/inputmethod_controller/src/keyevent_consumer_proxy.cpp @@ -31,11 +31,8 @@ KeyEventConsumerProxy::KeyEventConsumerProxy(const sptr &object) int32_t KeyEventConsumerProxy::OnKeyEventResult(bool isConsumed) { return SendRequest( - KEY_EVENT_RESULT, - [isConsumed](MessageParcel &parcel) { - return ITypesUtil::Marshal(parcel, isConsumed); - }, - nullptr, MessageOption::TF_ASYNC); + KEY_EVENT_RESULT, [isConsumed](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, isConsumed); }, + nullptr); } void KeyEventConsumerProxy::OnKeyEventConsumeResult(bool isConsumed)