diff --git a/frameworks/inputmethod_ability/include/event_target.h b/frameworks/inputmethod_ability/include/event_target.h index e1a345a23d94e208ae152bd286c6afa18729756b..afd0c49248d0e9a3902cc980759bcfb03d83a338 100644 --- a/frameworks/inputmethod_ability/include/event_target.h +++ b/frameworks/inputmethod_ability/include/event_target.h @@ -17,6 +17,7 @@ #define INPUT_METHOD_NAPI_EVENT_TARGET_H #include "napi/native_api.h" +#include "napi/native_node_api.h" #include "global.h" #include "input_method_ability.h" @@ -37,7 +38,7 @@ namespace MiscServices { virtual void Once(const char *type, napi_value handler); virtual void Off(const char *type, napi_value handler); virtual void Off(const char *type); - virtual void Emit(const char *type, Event *event); + virtual void Emit(sptr &eventTarget, const char *type, Event *event); protected: napi_env env_; diff --git a/frameworks/inputmethod_ability/src/event_target.cpp b/frameworks/inputmethod_ability/src/event_target.cpp index 8269d9c7933ea70b5814d01888f58cfe521a9911..4c6f1b032f04058bf1d970829101b6c5b40eb1f8 100644 --- a/frameworks/inputmethod_ability/src/event_target.cpp +++ b/frameworks/inputmethod_ability/src/event_target.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include #include "event_target.h" #include "securec.h" @@ -29,6 +29,15 @@ namespace MiscServices { EventListener *back = nullptr; EventListener *next = nullptr; }; + struct EventTargetCB { + napi_env env; + napi_ref thisVarRef; + EventListener *first; + EventListener *last; + sptr &eventTarget; + const char *type; + Event *event; + }; EventTarget::EventTarget(napi_env env, napi_value thisVar) { IMSA_HILOGI("EventTarget::EventTarget"); @@ -159,27 +168,63 @@ namespace MiscServices { } } - void EventTarget::Emit(const char *type, Event *event) + void EventTarget::Emit(sptr &eventTarget, const char *type, Event *event) { IMSA_HILOGI("EventTarget::Emit"); - napi_handle_scope scope = nullptr; - napi_open_handle_scope(env_, &scope); + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + if (loop == nullptr) { + IMSA_HILOGI("EventTarget::Emit loop == nullptr"); + return; + } - napi_value thisVar = nullptr; - napi_get_reference_value(env_, thisVarRef_, &thisVar); - for (EventListener *eventListener = first_; eventListener != nullptr; eventListener = eventListener->next) { - if (strcmp(eventListener->type, type) == 0) { - napi_value jsEvent = event ? event->ToJsObject() : nullptr; - napi_value handler = nullptr; - napi_value result = nullptr; - napi_get_reference_value(env_, eventListener->handlerRef, &handler); - napi_call_function(env_, thisVar, handler, jsEvent ? 1 : 0, jsEvent ? &jsEvent : nullptr, &result); - if (eventListener->isOnce) { - Off(type, handler); + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + IMSA_HILOGI("EventTarget::Emit No memory work == nullptr"); + return; + } + + EventTargetCB *eventTargetCB = new (std::nothrow) EventTargetCB {.env = env_, .thisVarRef = thisVarRef_, + .first = first_, .last = last_, .type = type, .event = event, .eventTarget = eventTarget}; + + work->data = (void *)eventTargetCB; + + int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { + //Js Thread + if (work == nullptr) { + IMSA_HILOGI("EventTarget::Emit work == nullptr"); + return; + } + EventTargetCB *eventTargetCB = (EventTargetCB *)work->data; + do { + napi_handle_scope scope = nullptr; + napi_open_handle_scope(eventTargetCB->env, &scope); + + napi_value thisVar = nullptr; + napi_get_reference_value(eventTargetCB->env, eventTargetCB->thisVarRef, &thisVar); + for (EventListener *eventListener = eventTargetCB->first; eventListener != nullptr; eventListener = eventListener->next) { + if (strcmp(eventListener->type, eventTargetCB->type) == 0) { + napi_value jsEvent = eventTargetCB->event ? eventTargetCB->event->ToJsObject() : nullptr; + napi_value handler = nullptr; + napi_value result = nullptr; + napi_get_reference_value(eventTargetCB->env, eventListener->handlerRef, &handler); + napi_call_function(eventTargetCB->env, thisVar, handler, jsEvent ? 1 : 0, jsEvent ? &jsEvent : nullptr, &result); + if (eventListener->isOnce) { + eventTargetCB->eventTarget->Off(eventTargetCB->type, handler); + } + } } + napi_close_handle_scope(eventTargetCB->env, scope); + } while (0); + if (eventTargetCB) { + delete eventTargetCB; } + delete work; + }); + if (ret != 0) { + IMSA_HILOGI("EventTarget::Emit failed to execute libuv work queue"); + delete work; } - napi_close_handle_scope(env_, scope); } } } \ No newline at end of file diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index 9e2c15cf779ec288542b34053e1a698a0a51b126..c14fd5ff083bae4e7634b44ef7aab4f755bcc780 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -31,12 +31,15 @@ namespace MiscServices { InputMethodAbility::InputMethodAbility() : stop_(false) { + writeInputChannel = nullptr; Initialize(); OnConnect(); } InputMethodAbility::~InputMethodAbility() { + IMSA_HILOGI("InputMethodAbility::~InputMethodAbility"); + instance_ = nullptr; if (msgHandler != nullptr) { delete msgHandler; msgHandler = nullptr; @@ -96,8 +99,7 @@ namespace MiscServices { IMSA_HILOGI("InputMethodAbility::Initialize"); InitialInputWindow(); msgHandler = new MessageHandler(); - workThreadHandler = std::thread([this] - { + workThreadHandler = std::thread([this] { WorkThread(); }); } @@ -240,13 +242,13 @@ namespace MiscServices { void InputMethodAbility::ShowInputWindow() { IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); - eventTarget_->Emit("keyboardShow", nullptr); + eventTarget_->Emit(eventTarget_, "keyboardShow", nullptr); } void InputMethodAbility::DissmissInputWindow() { IMSA_HILOGI("InputMethodAbility::DissmissInputWindow"); - eventTarget_->Emit("keyboardHide", nullptr); + eventTarget_->Emit(eventTarget_, "keyboardHide", nullptr); } bool InputMethodAbility::InsertText(const std::string text) diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index b14aacd9404f070895af863a0054bddbf06a32a1..cdcebe40886b87894c501a2bacb53934db6975ef 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -327,8 +327,8 @@ namespace MiscServices { IMSA_HILOGI("PerUserSession::AddClient"); ClientInfo *clientInfo = GetClientInfo(inputClient); if (clientInfo != nullptr) { - IMSA_HILOGE("PerUserSession::AddClient clientInfo is not nullptr"); - return ErrorCode::ERROR_CLIENT_DUPLICATED; + IMSA_HILOGE("PerUserSession::AddClient clientInfo is exist, not need add."); + return ErrorCode::NO_ERROR; } sptr obj = inputClient->AsObject(); @@ -1245,9 +1245,7 @@ namespace MiscServices { sptr client = new InputClientProxy(clientObject); sptr interface = client; int remainClientNum = 0; - if (currentClient == interface) { - HideKeyboard(client); - } + HideKeyboard(client); int ret = RemoveClient(client, remainClientNum); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("PerUserSession::OnReleaseInput Aborted! Failed to RemoveClient [%{public}d]\n", userId_); @@ -1289,7 +1287,6 @@ namespace MiscServices { } if (imsCore[index] != nullptr) { IMSA_HILOGI("PerUserSession::onSetInputMethodCore End... Input Method Service has already been started ! "); - return; } imsCore[index] = core; int ret = StartInputMethod(index); @@ -1307,6 +1304,7 @@ namespace MiscServices { IMSA_HILOGI("PerUserSession::OnStartInput End...[%{public}d]\n", userId_); } } + IMSA_HILOGI("PerUserSession::OnStartInput End. currentClient is nullptr"); } /*! Stop input. Called by an input client.