From 3f61b34eeea96caea41ab7e78e85f7b83a45c6a8 Mon Sep 17 00:00:00 2001 From: cy7717 Date: Mon, 24 Jul 2023 10:12:58 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96uvwork?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cy7717 --- frameworks/js/napi/common/BUILD.gn | 17 +- .../js/napi/common/js_callback_handler.cpp | 52 +- .../js/napi/common/js_callback_handler.h | 40 +- .../js/napi/common/js_callback_info.cpp | 41 ++ frameworks/js/napi/common/js_callback_info.h | 48 ++ frameworks/js/napi/common/uv_work.cpp | 66 +++ frameworks/js/napi/common/uv_work.h | 34 ++ .../js_input_method_engine_setting.cpp | 194 +------ .../js_input_method_engine_setting.h | 15 +- .../js_keyboard_delegate_setting.cpp | 305 +++-------- .../js_keyboard_delegate_setting.h | 36 +- .../panel_listener_impl.cpp | 15 +- .../inputmethodability/panel_listener_impl.h | 6 - .../js_get_input_method_controller.cpp | 475 ++++-------------- .../js_get_input_method_controller.h | 22 +- .../js_get_input_method_setting.cpp | 122 +---- .../js_get_input_method_setting.h | 17 +- 17 files changed, 474 insertions(+), 1031 deletions(-) create mode 100644 frameworks/js/napi/common/js_callback_info.cpp create mode 100644 frameworks/js/napi/common/js_callback_info.h create mode 100644 frameworks/js/napi/common/uv_work.cpp create mode 100644 frameworks/js/napi/common/uv_work.h diff --git a/frameworks/js/napi/common/BUILD.gn b/frameworks/js/napi/common/BUILD.gn index 63fc5f46b..eaa8ce1a5 100644 --- a/frameworks/js/napi/common/BUILD.gn +++ b/frameworks/js/napi/common/BUILD.gn @@ -16,7 +16,10 @@ import("//build/ohos.gni") config("inputmethod_js_common_config") { visibility = [ ":*" ] - include_dirs = [ "./" ] + include_dirs = [ + "./", + "${inputmethod_path}/services/include", + ] ldflags = [ "-Wl,--exclude-libs=ALL" ] cflags = [ "-fdata-sections", @@ -27,20 +30,28 @@ config("inputmethod_js_common_config") { config("inputmethod_js_common_public_config") { visibility = [ "./*" ] - include_dirs = [ "./" ] + include_dirs = [ + "./", + "${inputmethod_path}/services/include", + ] } ohos_static_library("inputmethod_js_common") { sources = [ "event_checker.cpp", "js_callback_handler.cpp", + "js_callback_info.cpp", "js_callback_object.cpp", "js_util.cpp", + "uv_work.cpp", ] configs = [ ":inputmethod_js_common_config" ] public_configs = [ ":inputmethod_js_common_public_config" ] deps = [] - external_deps = [ "napi:ace_napi" ] + external_deps = [ + "napi:ace_napi", + "hilog:libhilog", + ] subsystem_name = "inputmethod" part_name = "imf" } diff --git a/frameworks/js/napi/common/js_callback_handler.cpp b/frameworks/js/napi/common/js_callback_handler.cpp index a6c0de7b2..81033d037 100644 --- a/frameworks/js/napi/common/js_callback_handler.cpp +++ b/frameworks/js/napi/common/js_callback_handler.cpp @@ -15,29 +15,61 @@ #include "js_callback_handler.h" +#include "global.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { -constexpr size_t MAX_ARGV_COUNT = 10; -void JsCallbackHandler::Execute( - const std::shared_ptr &object, const ArgContainer &argContainer, napi_value &output) +bool JsCallbackHandler::Call(uv_loop_s *loop, const std::vector> &cbObjects, + ParamGetter paramGetter, ResultSetter resultSetter) { - if (object->threadId_ != std::this_thread::get_id()) { - return; + auto callbackInfo = JsCallbackInfo::Generate(cbObjects, paramGetter, resultSetter); + if (callbackInfo == nullptr) { + return false; } - napi_value argv[MAX_ARGV_COUNT] = { nullptr }; - if (argContainer.argvProvider != nullptr && !argContainer.argvProvider(object->env_, argv, MAX_ARGV_COUNT)) { - return; + return UvWork::Call(loop, callbackInfo, DataType::CALLBACK_INFO, Traverse); +} + +void JsCallbackHandler::Traverse(uv_work_t *work, int uvStatus) +{ + std::shared_ptr callbackInfo( + static_cast(work->data), [work](JsCallbackInfo *data) { + delete data; + delete work; + }); + for (const auto &cbObject : callbackInfo->cbObjects) { + JsUtil::ScopeGuard scopeGuard(cbObject->env_); + CallbackParam param; + if (callbackInfo->paramGetter != nullptr && !callbackInfo->paramGetter(cbObject->env_, param)) { + IMSA_HILOGE("param get failed"); + continue; + } + auto jsOutput = Execute(cbObject, param); + if (callbackInfo->resultSetter != nullptr && jsOutput != nullptr + && callbackInfo->resultSetter(cbObject->env_, jsOutput)) { + break; + } + } +} + +napi_value JsCallbackHandler::Execute(const std::shared_ptr &object, const CallbackParam ¶m) +{ + napi_value output = nullptr; + if (object->threadId_ != std::this_thread::get_id()) { + IMSA_HILOGE("thread error"); + return output; } napi_value callback = nullptr; napi_value global = nullptr; napi_get_reference_value(object->env_, object->callback_, &callback); if (callback != nullptr) { napi_get_global(object->env_, &global); - auto status = napi_call_function(object->env_, global, callback, argContainer.argc, argv, &output); + IMSA_HILOGI("start call function"); + auto status = napi_call_function(object->env_, global, callback, param.num, param.argv, &output); if (status != napi_ok) { - output = nullptr; + IMSA_HILOGE("call function failed, status: %{public}d", status); } } + return output; } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/common/js_callback_handler.h b/frameworks/js/napi/common/js_callback_handler.h index 4f528e22b..38d18a4b8 100644 --- a/frameworks/js/napi/common/js_callback_handler.h +++ b/frameworks/js/napi/common/js_callback_handler.h @@ -15,47 +15,19 @@ #ifndef OHOS_INPUT_CALLBACK_HANDLER_H #define OHOS_INPUT_CALLBACK_HANDLER_H -#include "js_callback_object.h" -#include "js_util.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" +#include "js_callback_info.h" +#include "uv_work.h" namespace OHOS { namespace MiscServices { class JsCallbackHandler { public: - using ArgvProvider = std::function; - struct ArgContainer { - size_t argc{ 0 }; - ArgvProvider argvProvider{ nullptr }; - }; - // 0 means the callback has no param. - static void Traverse(const std::vector> &objects, - const ArgContainer &argContainer = { 0, nullptr }) - { - for (const auto &object : objects) { - JsUtil::ScopeGuard scopeGuard(object->env_); - napi_value jsOutput = nullptr; - Execute(object, argContainer, jsOutput); - } - } - template - static void Traverse( - const std::vector> &objects, const ArgContainer &argContainer, T &output) - { - for (const auto &object : objects) { - JsUtil::ScopeGuard scopeGuard(object->env_); - napi_value jsOutput = nullptr; - Execute(object, argContainer, jsOutput); - if (jsOutput != nullptr && JsUtil::GetValue(object->env_, jsOutput, output)) { - break; - } - } - } + static bool Call(uv_loop_s *loop, const std::vector> &cbObjects, + ParamGetter paramGetter = nullptr, ResultSetter resultSetter = nullptr); private: - static void Execute( - const std::shared_ptr &object, const ArgContainer &argContainer, napi_value &output); + static void Traverse(uv_work_t *work, int uvStatus); + static napi_value Execute(const std::shared_ptr &object, const CallbackParam &info); }; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/common/js_callback_info.cpp b/frameworks/js/napi/common/js_callback_info.cpp new file mode 100644 index 000000000..bdfd81a50 --- /dev/null +++ b/frameworks/js/napi/common/js_callback_info.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_callback_info.h" + +#include "global.h" + +namespace OHOS { +namespace MiscServices { +JsCallbackInfo *JsCallbackInfo::Generate(const std::vector> &cbObjects, + ParamGetter paramGetter, ResultSetter resultSetter) +{ + if (cbObjects.empty()) { + IMSA_HILOGE("cbObjects is empty"); + return nullptr; + } + auto callbackInfo = new (std::nothrow) JsCallbackInfo(cbObjects, paramGetter, resultSetter); + if (callbackInfo == nullptr) { + return nullptr; + } + return callbackInfo; +} +void JsCallbackInfo::Purge(void *data) +{ + auto *callbackInfo = static_cast(data); + delete callbackInfo; +} +} // namespace MiscServices +} // namespace OHOS diff --git a/frameworks/js/napi/common/js_callback_info.h b/frameworks/js/napi/common/js_callback_info.h new file mode 100644 index 000000000..2dbe3c298 --- /dev/null +++ b/frameworks/js/napi/common/js_callback_info.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_INPUT_CALLBACK_INFO_H +#define OHOS_INPUT_CALLBACK_INFO_H + +#include +#include + +#include "js_callback_object.h" + +namespace OHOS { +namespace MiscServices { +struct CallbackParam { + static constexpr uint32_t MAX_PARAM_NUM = 10; + uint32_t num = 0; + napi_value argv[MAX_PARAM_NUM] = { nullptr }; +}; +using ParamGetter = std::function; +using ResultSetter = std::function; +struct JsCallbackInfo { + JsCallbackInfo( + std::vector> cbObjects, ParamGetter paramGetter, ResultSetter resultSetter) + : cbObjects(std::move(cbObjects)), paramGetter(paramGetter), resultSetter(resultSetter) + { + } + static JsCallbackInfo *Generate(const std::vector> &cbObjects, + ParamGetter paramGetter, ResultSetter resultSetter); + static void Purge(void *data); + + std::vector> cbObjects; + ParamGetter paramGetter{ nullptr }; + ResultSetter resultSetter{ nullptr }; +}; +} // namespace MiscServices +} // namespace OHOS +#endif // OHOS_INPUT_CALLBACK_INFO_H diff --git a/frameworks/js/napi/common/uv_work.cpp b/frameworks/js/napi/common/uv_work.cpp new file mode 100644 index 000000000..109d729b3 --- /dev/null +++ b/frameworks/js/napi/common/uv_work.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "uv_work.h" + +#include "global.h" +#include "js_callback_info.h" +namespace OHOS { +namespace MiscServices { +bool UvWork::Call(uv_loop_s *loop, void *data, DataType type, uv_after_work_cb afterCallback) +{ + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + Purge(data, type); + return false; + } + work->data = data; + auto ret = uv_queue_work_with_qos( + loop, work, [](uv_work_t *work) {}, afterCallback, uv_qos_user_initiated); + if (ret != 0) { + IMSA_HILOGE("uv_queue_work_with_qos failed"); + Purge(work, type); + return false; + } + return true; +} + +void UvWork::Purge(void *data, DataType type) +{ + switch (type) { + case DataType::CALLBACK_INFO: { + JsCallbackInfo::Purge(data); + break; + } + default: { + return; + } + } +} + +void UvWork::Purge(uv_work_t *work, DataType type) +{ + if (work == nullptr) { + return; + } + if (work->data == nullptr) { + delete work; + return; + } + Purge(work->data, type); + delete work; + work = nullptr; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/common/uv_work.h b/frameworks/js/napi/common/uv_work.h new file mode 100644 index 000000000..48882cbc2 --- /dev/null +++ b/frameworks/js/napi/common/uv_work.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_INPUT_UV_WORK_H +#define OHOS_INPUT_UV_WORK_H + +#include + +#include +namespace OHOS { +namespace MiscServices { +enum class DataType : int32_t { CALLBACK_INFO = 0 }; +class UvWork { +public: + static bool Call(uv_loop_s *loop, void *data, DataType type, uv_after_work_cb afterCallback); + +private: + static void Purge(uv_work_t *work, DataType type); + static void Purge(void *data, DataType type); +}; +} // namespace MiscServices +} // namespace OHOS +#endif // OHOS_INPUT_UV_WORK_H diff --git a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp index 5aacdc437..e5fa5fb5e 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -511,198 +511,58 @@ napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const return subType; } +std::vector> JsInputMethodEngineSetting::GetCallbackObjects(const std::string &type) +{ + std::lock_guard lock(mutex_); + return jsCbMap_[type]; +} + void JsInputMethodEngineSetting::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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnInputStart:: entryptr is null"); - return; - } - auto getInputStartProperty = [](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 2) { - return false; - } - napi_value textInput = JsTextInputClientEngine::GetTextInputClientInstance(env); - napi_value keyBoardController = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env); - if (keyBoardController == nullptr || textInput == nullptr) { - IMSA_HILOGE("get KBCins or TICins failed:"); - return false; - } - // 0 means the first param of callback. - args[0] = keyBoardController; - // 1 means the second param of callback. - args[1] = textInput; - return true; - }; - // 2 means callback has 2 params. - JsCallbackHandler::Traverse(entry->vecCopy, { 2, getInputStartProperty }); - }); + auto paramGetter = [](napi_env env, CallbackParam ¶m) { + param.num = 2; + param.argv[0] = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env); + param.argv[1] = JsTextInputClientEngine::GetTextInputClientInstance(env); + return param.argv[0] != nullptr && param.argv[1] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("inputStart"), paramGetter); } void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) { std::string type = isShow ? "keyboardShow" : "keyboardHide"; 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - JsCallbackHandler::Traverse(entry->vecCopy); - }); + JsCallbackHandler::Call(loop_, GetCallbackObjects(type)); } void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) { IMSA_HILOGD("run in"); - std::string type = "inputStop"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnInputStop:: entryptr is null"); - return; - } - auto getInputStopProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - // 0 means the first param of callback. - napi_create_string_utf8(env, entry->imeid.c_str(), NAPI_AUTO_LENGTH, &args[0]); - return true; - }; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getInputStopProperty }); - }); + JsCallbackHandler::Call(loop_, GetCallbackObjects("inputStop")); } void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) { IMSA_HILOGD("run in"); - std::string type = "setCallingWindow"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("setCallingWindow:: entryptr is null"); - return; - } - auto getCallingWindowProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - // 0 means the first param of callback. - napi_create_uint32(env, entry->windowid, &args[0]); - return true; - }; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getCallingWindowProperty }); - }); + auto paramGetter = [windowId](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, windowId); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("setCallingWindow"), paramGetter); } void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) { IMSA_HILOGD("run in"); - std::string type = "setSubtype"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnSetSubtype:: entryptr is null"); - return; - } - auto getSubtypeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - napi_value jsObject = GetResultOnSetSubtype(env, entry->subProperty); - if (jsObject == nullptr) { - IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr"); - return false; - } - // 0 means the first param of callback. - args[0] = { jsObject }; - return true; - }; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSubtypeProperty }); - }); -} + auto paramGetter = [property](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = GetResultOnSetSubtype(env, property); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("setSubtype"), paramGetter); -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/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h index 4952e8d9d..583e5306c 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h @@ -20,7 +20,6 @@ #include #include #include -#include #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" #include "async_call.h" @@ -89,21 +88,9 @@ private: void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); + std::vector> GetCallbackObjects(const std::string &type); static const std::string IMES_CLASS_NAME; static thread_local napi_ref IMESRef_; - struct UvEntry { - std::vector> vecCopy; - std::string type; - std::string imeid; - uint32_t windowid = 0; - SubProperty subProperty; - UvEntry(const std::vector> &cbVec, const std::string &type) - : vecCopy(cbVec), type(type) - { - } - }; - 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/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 6d0fa79eb..79e26235c 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -283,270 +283,109 @@ napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t return KeyboardDelegate; } +std::vector> JsKeyboardDelegateSetting::GetCallbackObjects(const std::string &type) +{ + std::lock_guard lock(mutex_); + return jsCbMap_[type]; +} + bool JsKeyboardDelegateSetting::OnKeyEvent(const std::shared_ptr &keyEvent) { - IMSA_HILOGD("run in"); - std::string type = "keyEvent"; - auto isDone = std::make_shared>(MAX_TIMEOUT, false); - uv_work_t *work = GetUVwork(type, [keyEvent, isDone](UvEntry &entry) { - entry.pullKeyEventPara = keyEvent; - entry.isDone = isDone; - }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv work"); + IMSA_HILOGD("run in, whole keyEvent"); + auto paramGetter = [keyEvent](napi_env env, CallbackParam ¶m) { + param.num = 1; + napi_create_object(env, ¶m.argv[0]); + MMI::KeyEventNapi::CreateKeyEvent(env, keyEvent, param.argv[0]); + return param.argv[0] != nullptr; + }; + auto resultHandler = std::make_shared>(MAX_TIMEOUT, false); + auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { + bool isConsumed = false; + if (JsUtil::GetValue(env, jsOutput, isConsumed)) { + IMSA_HILOGI("whole key event handle result: %{public}d", isConsumed); + resultHandler->SetValue(isConsumed); + return true; + } return false; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - auto getKeyEventProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - napi_value keyEventObject{}; - auto result = napi_create_object(env, &keyEventObject); - CHECK_RETURN((result == napi_ok) && (keyEventObject != nullptr), "create object", false); - result = MMI::KeyEventNapi::CreateKeyEvent(env, entry->pullKeyEventPara, keyEventObject); - CHECK_RETURN((result == napi_ok) && (keyEventObject != nullptr), "create key event object", false); - // 0 means the first param of callback. - args[0] = keyEventObject; - return true; - }; - bool isConsumed = false; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getKeyEventProperty }, isConsumed); - entry->isDone->SetValue(isConsumed); - }); - bool isConsumed = isDone->GetValue(); - IMSA_HILOGI("key event handle result: %{public}d", isConsumed); - return isConsumed; + }; + return JsCallbackHandler::Call(loop_, GetCallbackObjects("keyEvent"), paramGetter, resultSetter) + ? resultHandler->GetValue() + : false; } bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) { 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 = GetUVwork(type, [¶, isDone](UvEntry &entry) { - entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent }; - entry.isDone = isDone; - }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv work"); + auto paramGetter = [keyCode, keyStatus](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = GetResultOnKeyEvent(env, keyCode, keyStatus); + return param.argv[0] != nullptr; + }; + auto resultHandler = std::make_shared>(MAX_TIMEOUT, false); + auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { + bool isConsumed = false; + if (JsUtil::GetValue(env, jsOutput, isConsumed)) { + IMSA_HILOGI("key event handle result: %{public}d", isConsumed); + resultHandler->SetValue(isConsumed); + return true; + } return false; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - auto getKeyEventProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - napi_value jsObject = - GetResultOnKeyEvent(env, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus); - if (jsObject == nullptr) { - IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr"); - return false; - } - // 0 means the first param of callback. - args[0] = jsObject; - return true; - }; - bool isConsumed = false; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getKeyEventProperty }, isConsumed); - entry->isDone->SetValue(isConsumed); - }); - bool isConsumed = isDone->GetValue(); - IMSA_HILOGI("key event handle result: %{public}d", isConsumed); - return isConsumed; + }; + return JsCallbackHandler::Call(loop_, GetCallbackObjects(type), paramGetter, resultSetter) + ? resultHandler->GetValue() + : false; } void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { IMSA_HILOGD("run in"); - CursorPara para{ positionX, positionY, height }; - std::string type = "cursorContextChange"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - - auto getCursorUpdateProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 3) { - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->curPara.positionX, &args[0]); - // 1 means the second param of callback. - napi_create_int32(env, entry->curPara.positionY, &args[1]); - // 2 means the third param of callback. - napi_create_int32(env, entry->curPara.height, &args[2]); - return true; - }; - // 3 means callback has three params. - JsCallbackHandler::Traverse(entry->vecCopy, { 3, getCursorUpdateProperty }); - }); + auto paramGetter = [positionX, positionY, height](napi_env env, CallbackParam ¶m) { + param.num = 3; + param.argv[0] = JsUtil::GetValue(env, positionX); + param.argv[1] = JsUtil::GetValue(env, positionY); + param.argv[2] = JsUtil::GetValue(env, height); + return param.argv[0] != nullptr && param.argv[1] != nullptr && param.argv[2] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("cursorContextChange"), paramGetter); } void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { IMSA_HILOGD("run in"); - SelectionPara para{ oldBegin, oldEnd, newBegin, newEnd }; - std::string type = "selectionChange"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - - auto getSelectionChangeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 4) { - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->selPara.oldBegin, &args[0]); - // 1 means the second param of callback. - napi_create_int32(env, entry->selPara.oldEnd, &args[1]); - // 2 means the third param of callback. - napi_create_int32(env, entry->selPara.newBegin, &args[2]); - // 3 means the fourth param of callback. - napi_create_int32(env, entry->selPara.newEnd, &args[3]); - return true; - }; - // 4 means callback has four params. - JsCallbackHandler::Traverse(entry->vecCopy, { 4, getSelectionChangeProperty }); - }); + auto paramGetter = [oldBegin, oldEnd, newBegin, newEnd](napi_env env, CallbackParam ¶m) { + param.num = 4; + param.argv[0] = JsUtil::GetValue(env, oldBegin); + param.argv[1] = JsUtil::GetValue(env, oldEnd); + param.argv[2] = JsUtil::GetValue(env, newBegin); + param.argv[3] = JsUtil::GetValue(env, newEnd); + return param.argv[0] != nullptr && param.argv[1] != nullptr && param.argv[2] != nullptr + && param.argv[3] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("selectionChange"), paramGetter); } void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) { IMSA_HILOGD("run in"); std::string type = "textChange"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - - auto getTextChangeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - // 0 means the first param of callback. - napi_create_string_utf8(env, entry->text.c_str(), NAPI_AUTO_LENGTH, &args[0]); - return true; - }; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getTextChangeProperty }); - }); + auto paramGetter = [text](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, text); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects(type), paramGetter); } void JsKeyboardDelegateSetting::OnEditorAttributeChange(const InputAttribute &inputAttribute) { IMSA_HILOGI("run in"); - std::string type = "editorAttributeChanged"; - uv_work_t *work = JsKeyboardDelegateSetting::GetUVwork(type, [&inputAttribute](UvEntry &entry) { - entry.inputAttribute = inputAttribute; - }); - if (work == nullptr) { - IMSA_HILOGD("failed to get uv entry"); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - - auto getEditorAttributeChangeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == 0) { - return false; - } - - napi_value jsObject = JsUtils::GetValue(env, entry->inputAttribute); - if (jsObject == nullptr) { - IMSA_HILOGE("get GetAttribute failed: jsObject is nullptr"); - return false; - } - // 0 means the first param of callback. - args[0] = jsObject; - return true; - }; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getEditorAttributeChangeProperty }); - }); -} - -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; + auto paramGetter = [inputAttribute](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtils::GetValue(env, inputAttribute); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("editorAttributeChanged"), paramGetter); } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h index 1f1880200..05fab116b 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h @@ -16,8 +16,6 @@ #ifndef INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H #define INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H -#include - #include #include #include @@ -57,42 +55,10 @@ 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); + std::vector> GetCallbackObjects(const std::string &type); static constexpr int32_t MAX_TIMEOUT = 2000; static const std::string KDS_CLASS_NAME; static thread_local napi_ref KDSRef_; - struct CursorPara { - int32_t positionX = 0; - int32_t positionY = 0; - int height = 0; - }; - struct SelectionPara { - int32_t oldBegin = 0; - int32_t oldEnd = 0; - int32_t newBegin = 0; - int32_t newEnd = 0; - }; - struct KeyEventPara { - int32_t keyCode = 0; - int32_t keyStatus = 0; - bool isOnKeyEvent = false; - }; - struct UvEntry { - std::vector> vecCopy; - std::string type; - CursorPara curPara; - SelectionPara selPara; - KeyEventPara keyEventPara; - std::shared_ptr pullKeyEventPara; - std::shared_ptr> isDone; - std::string text; - InputAttribute inputAttribute; - UvEntry(const std::vector> &cbVec, const std::string &type) - : vecCopy(cbVec), type(type) - { - } - }; - 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/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp index a14d8ee07..13b55a952 100644 --- a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp +++ b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp @@ -91,22 +91,9 @@ void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow) IMSA_HILOGE("no callback in map!"); return; } - work->data = new (std::nothrow) UvEntry(callback.second); uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env_, &loop); - uv_queue_work( - loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("entry is nullptr"); - return; - } - JsCallbackHandler::Traverse({ entry->cbCopy }); - }); + JsCallbackHandler::Call(loop, { callback.second }); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/inputmethodability/panel_listener_impl.h b/frameworks/js/napi/inputmethodability/panel_listener_impl.h index 496a8446f..3f0a0b468 100644 --- a/frameworks/js/napi/inputmethodability/panel_listener_impl.h +++ b/frameworks/js/napi/inputmethodability/panel_listener_impl.h @@ -18,7 +18,6 @@ #include #include -#include #include "concurrent_map.h" #include "js_callback_object.h" @@ -35,11 +34,6 @@ public: void OnPanelStatus(uint32_t windowId, bool isShow) override; void SaveInfo(napi_env env, const std::string &type, napi_value callback, uint32_t windowId); void RemoveInfo(const std::string &type, uint32_t windowId); - - struct UvEntry { - std::shared_ptr cbCopy; - explicit UvEntry(const std::shared_ptr &cb) : cbCopy(cb) {} - }; napi_env env_ = nullptr; ConcurrentMap>> callbacks_; static std::mutex listenerMutex_; diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index a94c046a1..6c12fab4b 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -28,7 +28,6 @@ namespace OHOS { namespace MiscServices { -constexpr size_t ARGC_ZERO = 0; constexpr size_t ARGC_ONE = 1; constexpr size_t ARGC_TWO = 2; const std::set EVENT_TYPE{ @@ -688,221 +687,76 @@ napi_value JsGetInputMethodController::StopInput(napi_env env, napi_callback_inf env, info, [] { return InputMethodController::GetInstance()->HideCurrentInput(); }, true, false); } +std::vector> JsGetInputMethodController::GetCallbackObjects(const std::string &type) +{ + std::lock_guard lock(mutex_); + return jsCbMap_[type]; +} + void JsGetInputMethodController::OnSelectByRange(int32_t start, int32_t end) { IMSA_HILOGD("run in, start: %{public}d, end: %{public}d", start, end); - std::string type = "selectByRange"; - uv_work_t *work = GetUVwork("selectByRange", [start, end](UvEntry &entry) { - entry.start = start; - entry.end = end; - }); - if (work == nullptr) { - IMSA_HILOGD("failed to get uv entry"); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnSelectByRange entryptr is null"); - return; - } - auto getProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < ARGC_ONE) { - return false; - } - napi_value range = CreateSelectRange(env, entry->start, entry->end); - if (range == nullptr) { - IMSA_HILOGE("set select range failed"); - return false; - } - // 0 means the first param of callback. - args[0] = range; - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getProperty }); - }); + auto paramGetter = [start, end](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = CreateSelectRange(env, start, end); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("selectByRange"), paramGetter); } void JsGetInputMethodController::OnSelectByMovement(int32_t direction) { IMSA_HILOGD("run in, direction: %{public}d", direction); - std::string type = "selectByMovement"; - uv_work_t *work = GetUVwork(type, [direction](UvEntry &entry) { entry.direction = direction; }); - if (work == nullptr) { - IMSA_HILOGD("failed to get uv entry"); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnSelectByMovement entryptr is null"); - return; - } - auto getProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 1) { - return false; - } - napi_value movement = CreateSelectMovement(env, entry->direction); - if (movement == nullptr) { - IMSA_HILOGE("set select movement failed"); - return false; - } - // 0 means the first param of callback. - args[0] = movement; - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getProperty }); - }); + auto paramGetter = [direction](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = CreateSelectMovement(env, direction); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("selectByMovement"), paramGetter); } void JsGetInputMethodController::InsertText(const std::u16string &text) { - std::string insertText = Str16ToStr8(text); - std::string type = "insertText"; - uv_work_t *work = GetUVwork(type, [&insertText](UvEntry &entry) { entry.text = insertText; }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("insertText entryptr is null."); - return; - } - - auto getInsertTextProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("insertText:getInsertTextProperty the number of argc is invalid."); - return false; - } - // 0 means the first param of callback. - napi_create_string_utf8(env, entry->text.c_str(), NAPI_AUTO_LENGTH, &args[0]); - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getInsertTextProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [text](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, Str16ToStr8(text)); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("insertText"), paramGetter); } void JsGetInputMethodController::DeleteRight(int32_t length) { - std::string type = "deleteRight"; - uv_work_t *work = GetUVwork(type, [&length](UvEntry &entry) { entry.length = length; }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("deleteRight entryptr is null."); - return; - } - - auto getDeleteForwardProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("deleteRight:getDeleteForwardProperty the number of argc is invalid."); - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->length, &args[0]); - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getDeleteForwardProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [length](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, length); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("deleteRight"), paramGetter); } void JsGetInputMethodController::DeleteLeft(int32_t length) { - std::string type = "deleteLeft"; - uv_work_t *work = GetUVwork(type, [&length](UvEntry &entry) { entry.length = length; }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("deleteLeft entryptr is null."); - return; - } - - auto getDeleteBackwardProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("deleteLeft::getDeleteBackwardProperty the number of argc is invalid."); - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->length, &args[0]); - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getDeleteBackwardProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [length](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, length); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("deleteLeft"), paramGetter); } void JsGetInputMethodController::SendKeyboardStatus(const KeyboardStatus &status) { - std::string type = "sendKeyboardStatus"; - uv_work_t *work = - GetUVwork(type, [&status](UvEntry &entry) { entry.keyboardStatus = static_cast(status); }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("sendKeyboardStatus entryptr is null."); - return; - } - - auto getSendKeyboardStatusProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("sendKeyboardStatus:getSendKeyboardStatusProperty the number of argc is invalid."); - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->keyboardStatus, &args[0]); - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSendKeyboardStatusProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [status](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, static_cast(status)); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("sendKeyboardStatus"), paramGetter); } napi_value JsGetInputMethodController::CreateSendFunctionKey(napi_env env, int32_t functionKey) @@ -919,207 +773,76 @@ napi_value JsGetInputMethodController::CreateSendFunctionKey(napi_env env, int32 void JsGetInputMethodController::SendFunctionKey(const FunctionKey &functionKey) { - std::string type = "sendFunctionKey"; - uv_work_t *work = GetUVwork(type, - [&functionKey](UvEntry &entry) { entry.enterKeyType = static_cast(functionKey.GetEnterKeyType()); }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("sendFunctionKey entryptr is null."); - return; - } - - auto getSendFunctionKeyProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("sendFunctionKey:getSendFunctionKeyProperty the number of argc is invalid."); - return false; - } - napi_value functionKey = CreateSendFunctionKey(env, entry->enterKeyType); - if (functionKey == nullptr) { - IMSA_HILOGE("set select movement failed"); - return false; - } - // 0 means the first param of callback. - args[0] = functionKey; - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getSendFunctionKeyProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [functionKey](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = CreateSendFunctionKey(env, static_cast(functionKey.GetEnterKeyType())); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("sendFunctionKey"), paramGetter); } void JsGetInputMethodController::MoveCursor(const Direction direction) { - std::string type = "moveCursor"; - uv_work_t *work = - GetUVwork(type, [&direction](UvEntry &entry) { entry.direction = static_cast(direction); }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("moveCursor entryptr is null."); - return; - } - - auto getMoveCursorProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("moveCursor:getMoveCursorProperty the number of argc is invalid."); - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, static_cast(entry->direction), &args[0]); - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getMoveCursorProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [direction](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, static_cast(direction)); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("moveCursor"), paramGetter); } void JsGetInputMethodController::HandleExtendAction(int32_t action) { - std::string type = "handleExtendAction"; - uv_work_t *work = GetUVwork(type, [&action](UvEntry &entry) { entry.action = action; }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("handleExtendAction entryptr is null."); - return; - } - auto getHandleExtendActionProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc == ARGC_ZERO) { - IMSA_HILOGE("handleExtendAction:getHandleExtendActionProperty the number of argc is invalid."); - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->action, &args[0]); - return true; - }; - // 1 means the callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getHandleExtendActionProperty }); - }); + IMSA_HILOGD("run in"); + auto paramGetter = [action](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, action); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("handleExtendAction"), paramGetter); } std::u16string JsGetInputMethodController::GetText(const std::string &type, int32_t number) { - auto textResultHandler = std::make_shared>(MAX_TIMEOUT, ""); - uv_work_t *work = GetUVwork(type, [&number, textResultHandler](UvEntry &entry) { - entry.number = number; - entry.textResultHandler = textResultHandler; - }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return u""; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("handleExtendAction entryptr is null."); - return; - } - auto fillArguments = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 1) { - IMSA_HILOGE("argc is err."); - return false; - } - // 0 means the first param of callback. - napi_create_int32(env, entry->number, &args[0]); - return true; - }; - std::string text; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, fillArguments }, text); - entry->textResultHandler->SetValue(text); - }); - return Str8ToStr16(textResultHandler->GetValue()); + IMSA_HILOGD("run in"); + auto paramGetter = [number](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtil::GetValue(env, number); + return param.argv[0] != nullptr; + }; + auto resultHandler = std::make_shared>(MAX_TIMEOUT, ""); + auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { + std::string text; + if (JsUtil::GetValue(env, jsOutput, text)) { + IMSA_HILOGD("get text success"); + resultHandler->SetValue(text); + return true; + } + return false; + }; + return JsCallbackHandler::Call(loop_, GetCallbackObjects(type), paramGetter, resultSetter) + ? Str8ToStr16(resultHandler->GetValue()) + : u""; } int32_t JsGetInputMethodController::GetTextIndexAtCursor() { - std::string type = "getTextIndexAtCursor"; - auto indexResultHandler = std::make_shared>(MAX_TIMEOUT, -1); - uv_work_t *work = - GetUVwork(type, [indexResultHandler](UvEntry &entry) { entry.indexResultHandler = indexResultHandler; }); - if (work == nullptr) { - IMSA_HILOGE("failed to get uv entry."); - return -1; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("handleExtendAction entryptr is null."); - return; - } - int32_t index = -1; - // 0 means callback has no params. - JsCallbackHandler::Traverse(entry->vecCopy, { 0, nullptr }, index); - entry->indexResultHandler->SetValue(index); - }); - return indexResultHandler->GetValue(); -} - -uv_work_t *JsGetInputMethodController::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); + IMSA_HILOGD("run in"); + auto resultHandler = std::make_shared>(MAX_TIMEOUT, -1); + auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { + int32_t index = -1; + if (JsUtil::GetValue(env, jsOutput, index)) { + IMSA_HILOGD("get index success: %{public}d", index); + resultHandler->SetValue(index); + return true; } - } - 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; + return false; + }; + return JsCallbackHandler::Call(loop_, GetCallbackObjects("getTextIndexAtCursor"), nullptr, resultSetter) + ? resultHandler->GetValue() + : -1; } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h index 8e4a17f20..8d91cec7a 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -205,29 +205,9 @@ private: static napi_value GetJsTextInputTypeProperty(napi_env env); static napi_value GetJsDirectionProperty(napi_env env); static napi_value GetJsExtendActionProperty(napi_env env); + std::vector> GetCallbackObjects(const std::string &type); static const std::set TEXT_EVENT_TYPE; static constexpr int32_t MAX_TIMEOUT = 3000; - struct UvEntry { - std::vector> vecCopy; - std::string type; - std::string text; - int32_t start = 0; - int32_t end = 0; - int32_t direction = 0; - int32_t length = 0; - int32_t action = 0; - int32_t keyboardStatus = 0; - int32_t enterKeyType = 0; - int32_t number = 0; - std::shared_ptr> textResultHandler; - std::shared_ptr> indexResultHandler; - explicit UvEntry(const std::vector> &cbVec, const std::string &type) - : vecCopy(cbVec), type(type) - { - } - }; - 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/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 7e64a0d52..eb296b4bc 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -476,120 +476,38 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info return result; } +std::vector> JsGetInputMethodSetting::GetCallbackObjects(const std::string &type) +{ + std::lock_guard lock(mutex_); + return jsCbMap_[type]; +} + void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty) { IMSA_HILOGD("run in"); - std::string type = "imeChange"; - 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( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnInputStart:: entryptr is null"); - return; - } - auto getImeChangeProperty = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 2) { - return false; - } - napi_value subProperty = JsInputMethod::GetJsInputMethodSubProperty(env, entry->subProperty); - napi_value property = JsInputMethod::GetJsInputMethodProperty(env, entry->property); - if (subProperty == nullptr || property == nullptr) { - IMSA_HILOGE("get KBCins or TICins failed:"); - return false; - } - // 0 means the first param of callback. - args[0] = property; - // 1 means the second param of callback. - args[1] = subProperty; - return true; - }; - // 2 means callback has two params. - JsCallbackHandler::Traverse(entry->vecCopy, { 2, getImeChangeProperty }); - }); + auto paramGetter = [property, subProperty](napi_env env, CallbackParam ¶m) { + param.num = 2; + param.argv[0] = JsInputMethod::GetJsInputMethodSubProperty(env, subProperty); + param.argv[1] = JsInputMethod::GetJsInputMethodProperty(env, property); + return param.argv[0] != nullptr && param.argv[1] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects("imeChange"), paramGetter); } void JsGetInputMethodSetting::OnPanelStatusChange( const InputWindowStatus &status, const std::vector &windowInfo) { - IMSA_HILOGI("status: %{public}u", static_cast(status)); + IMSA_HILOGI("run in, status: %{public}u", static_cast(status)); auto it = PANEL_STATUS.find(status); if (it == PANEL_STATUS.end()) { return; } - auto type = it->second; - uv_work_t *work = GetUVwork(type, [&windowInfo](UvEntry &entry) { entry.windowInfo = windowInfo; }); - if (work == nullptr) { - IMSA_HILOGD("failed to get uv entry"); - return; - } - uv_queue_work( - loop_, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - std::shared_ptr entry(static_cast(work->data), [work](UvEntry *data) { - delete data; - delete work; - }); - if (entry == nullptr) { - IMSA_HILOGE("OnInputStart:: entry is nullptr"); - return; - } - auto getWindowInfo = [entry](napi_env env, napi_value *args, uint8_t argc) -> bool { - if (argc < 1) { - return false; - } - auto windowInfo = JsUtils::GetValue(env, entry->windowInfo); - if (windowInfo == nullptr) { - IMSA_HILOGE("converse windowInfo failed"); - return false; - } - // 0 means the first param of callback. - args[0] = windowInfo; - return true; - }; - // 1 means callback has one param. - JsCallbackHandler::Traverse(entry->vecCopy, { 1, getWindowInfo }); - }); -} - -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; + auto paramGetter = [windowInfo](napi_env env, CallbackParam ¶m) { + param.num = 1; + param.argv[0] = JsUtils::GetValue(env, windowInfo); + return param.argv[0] != nullptr; + }; + JsCallbackHandler::Call(loop_, GetCallbackObjects(it->second), paramGetter); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h index c1cdde728..3d39b9cf9 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -15,8 +15,6 @@ #ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H #define INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H -#include - #include "async_call.h" #include "global.h" #include "input_method_controller.h" @@ -92,7 +90,6 @@ struct GetInputMethodControllerContext : public AsyncCall::Context { return Context::operator()(env, result); } }; - class JsGetInputMethodSetting : public InputMethodSettingListener { public: JsGetInputMethodSetting() = default; @@ -119,19 +116,7 @@ private: static napi_value GetIMSetting(napi_env env, napi_callback_info info, bool needThrowException); int32_t RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); - struct UvEntry { - std::vector> vecCopy; - std::string type; - Property property; - SubProperty subProperty; - std::vector windowInfo; - UvEntry(const std::vector> &cbVec, const std::string &type) - : vecCopy(cbVec), type(type) - { - } - }; - using EntrySetter = std::function; - uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); + std::vector> GetCallbackObjects(const std::string &type); static const std::string IMS_CLASS_NAME; static thread_local napi_ref IMSRef_; uv_loop_s *loop_ = nullptr; -- Gitee From 212221d7faa750bb1045fae7f4430fa175aea07b Mon Sep 17 00:00:00 2001 From: cy7717 Date: Thu, 17 Aug 2023 12:00:22 +0800 Subject: [PATCH 2/3] mod Signed-off-by: cy7717 --- frameworks/js/napi/common/BUILD.gn | 2 - .../js/napi/common/js_callback_handler.cpp | 112 +++++++++--- .../js/napi/common/js_callback_handler.h | 36 +++- .../js/napi/common/js_callback_info.cpp | 41 ----- frameworks/js/napi/common/js_callback_info.h | 48 ----- frameworks/js/napi/common/uv_work.cpp | 66 ------- frameworks/js/napi/common/uv_work.h | 34 ---- .../js_input_method_engine_setting.cpp | 57 ++++-- .../js_keyboard_delegate_setting.cpp | 126 +++++++------ .../panel_listener_impl.cpp | 6 +- .../js_get_input_method_controller.cpp | 173 +++++++++++------- .../js_get_input_method_setting.cpp | 30 +-- 12 files changed, 350 insertions(+), 381 deletions(-) delete mode 100644 frameworks/js/napi/common/js_callback_info.cpp delete mode 100644 frameworks/js/napi/common/js_callback_info.h delete mode 100644 frameworks/js/napi/common/uv_work.cpp delete mode 100644 frameworks/js/napi/common/uv_work.h diff --git a/frameworks/js/napi/common/BUILD.gn b/frameworks/js/napi/common/BUILD.gn index b530821d9..eeb614d94 100644 --- a/frameworks/js/napi/common/BUILD.gn +++ b/frameworks/js/napi/common/BUILD.gn @@ -42,10 +42,8 @@ ohos_static_library("inputmethod_js_common") { sources = [ "event_checker.cpp", "js_callback_handler.cpp", - "js_callback_info.cpp", "js_callback_object.cpp", "js_util.cpp", - "uv_work.cpp", ] configs = [ ":inputmethod_js_common_config" ] public_configs = [ ":inputmethod_js_common_public_config" ] diff --git a/frameworks/js/napi/common/js_callback_handler.cpp b/frameworks/js/napi/common/js_callback_handler.cpp index 1e5eda64d..355458b0d 100644 --- a/frameworks/js/napi/common/js_callback_handler.cpp +++ b/frameworks/js/napi/common/js_callback_handler.cpp @@ -19,57 +19,113 @@ #include "js_util.h" namespace OHOS { namespace MiscServices { -bool JsCallbackHandler::Call(uv_loop_s *loop, const std::vector> &cbObjects, - ParamGetter paramGetter, ResultSetter resultSetter) +constexpr int32_t MAX_WAIT_TIME = 5000; +BlockQueue JsCallbackHandler::callbackQueue_{ + MAX_WAIT_TIME +}; //todo 如果超时怎么处理 +JsCallbackHandler::JsCallbackHandler( + const std::shared_ptr &cbObject, ParamGetter paramGetter, ResultSetter resultSetter) { - auto callbackInfo = JsCallbackInfo::Generate(cbObjects, paramGetter, resultSetter); - if (callbackInfo == nullptr) { + env_ = cbObject->env_; + jsCallbackInfo_ = new (std::nothrow) + JsCallbackInfo(cbObject, paramGetter, resultSetter, std::chrono::system_clock::now()); +} + +bool JsCallbackHandler::Call() +{ + IMSA_HILOGD("run in"); + if (jsCallbackInfo_ == nullptr) { return false; } - return UvWork::Call(loop, callbackInfo, DataType::CALLBACK_INFO, Traverse); + auto work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + delete jsCallbackInfo_; + return false; + } + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + if (loop == nullptr) { + delete jsCallbackInfo_; + delete work; + return false; + } + // todo 在此处入队 + callbackQueue_.Push(jsCallbackInfo_->timeStamp); + work->data = jsCallbackInfo_; + auto ret = uv_queue_work_with_qos(loop, work, Queue, Execute, uv_qos_user_initiated); + if (ret != 0) { + IMSA_HILOGE("uv_queue_work_with_qos failed"); + delete jsCallbackInfo_; + delete work; + return false; + } + return true; +} + +void JsCallbackHandler::Queue(uv_work_t *work, int uvStatus) +{ + IMSA_HILOGD("run in"); + std::shared_ptr callbackInfo(static_cast(work->data)); + // todo 在此处等待 + callbackQueue_.Wait(callbackInfo->timeStamp); } -void JsCallbackHandler::Traverse(uv_work_t *work, int uvStatus) +void JsCallbackHandler::Execute(uv_work_t *work, int uvStatus) { + IMSA_HILOGD("run in"); std::shared_ptr callbackInfo( static_cast(work->data), [work](JsCallbackInfo *data) { + IMSA_HILOGD("release mem"); delete data; delete work; }); - for (const auto &cbObject : callbackInfo->cbObjects) { - JsUtil::ScopeGuard scopeGuard(cbObject->env_); - CallbackParam param; - if (callbackInfo->paramGetter != nullptr && !callbackInfo->paramGetter(cbObject->env_, param)) { - IMSA_HILOGE("param get failed"); - continue; - } - auto jsOutput = Execute(cbObject, param); - if (callbackInfo->resultSetter != nullptr && jsOutput != nullptr - && callbackInfo->resultSetter(cbObject->env_, jsOutput)) { - break; - } + + auto env = callbackInfo->cbObject->env_; + JsUtil::ScopeGuard scopeGuard(env); + napi_value jsOutput = nullptr; + std::vector param; + if (!GetParam(env, callbackInfo->paramGetter, param)) { + IMSA_HILOGE("param get failed"); + SetResult(callbackInfo->cbObject->env_, callbackInfo->resultSetter, jsOutput); + return; } + Invoke(callbackInfo->cbObject, param, jsOutput); + SetResult(callbackInfo->cbObject->env_, callbackInfo->resultSetter, jsOutput); + // todo 在此处出队 + callbackQueue_.Pop(); } -napi_value JsCallbackHandler::Execute(const std::shared_ptr &object, const CallbackParam ¶m) +bool JsCallbackHandler::GetParam(napi_env env, ParamGetter paramGetter, std::vector ¶m) { - napi_value output = nullptr; - if (object->threadId_ != std::this_thread::get_id()) { - IMSA_HILOGE("thread error"); - return output; + if (paramGetter == nullptr) { + return true; } + paramGetter(env, param); + return !param.empty(); +} + +void JsCallbackHandler::Invoke( + const std::shared_ptr &cbObject, const std::vector ¶m, napi_value &jsOutput) +{ napi_value callback = nullptr; napi_value global = nullptr; - napi_get_reference_value(object->env_, object->callback_, &callback); + napi_get_reference_value(cbObject->env_, cbObject->callback_, &callback); if (callback != nullptr) { - napi_get_global(object->env_, &global); - InputMethodSyncTrace tracer("Execute napi_call_function"); - auto status = napi_call_function(object->env_, global, callback, param.num, param.argv, &output); + napi_get_global(cbObject->env_, &global); + IMSA_HILOGI("start call function"); + auto status = napi_call_function(cbObject->env_, global, callback, param.size(), param.data(), &jsOutput); if (status != napi_ok) { IMSA_HILOGE("call function failed, status: %{public}d", status); } } - return output; +} + +void JsCallbackHandler::SetResult(napi_env env, ResultSetter resultSetter, napi_value jsOutput) +{ + if (resultSetter == nullptr) { + return; + } + resultSetter(env, jsOutput); } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/common/js_callback_handler.h b/frameworks/js/napi/common/js_callback_handler.h index b6f278d2a..4f6c6c725 100644 --- a/frameworks/js/napi/common/js_callback_handler.h +++ b/frameworks/js/napi/common/js_callback_handler.h @@ -15,20 +15,42 @@ #ifndef OHOS_INPUT_CALLBACK_HANDLER_H #define OHOS_INPUT_CALLBACK_HANDLER_H -#include "inputmethod_trace.h" -#include "js_callback_info.h" -#include "uv_work.h" +#include +#include "block_queue.h" +#include "js_callback_object.h" namespace OHOS { namespace MiscServices { class JsCallbackHandler { public: - static bool Call(uv_loop_s *loop, const std::vector> &cbObjects, - ParamGetter paramGetter = nullptr, ResultSetter resultSetter = nullptr); + using ParamGetter = std::function&)>; + using ResultSetter = std::function; + JsCallbackHandler(const std::shared_ptr &cbObject, ParamGetter paramGetter = nullptr, + ResultSetter resultSetter = nullptr); + bool Call(); private: - static void Traverse(uv_work_t *work, int uvStatus); - static napi_value Execute(const std::shared_ptr &object, const CallbackParam &info); + struct JsCallbackInfo { + JsCallbackInfo(std::shared_ptr cbObject, ParamGetter paramGetter, ResultSetter resultSetter, + std::chrono::system_clock::time_point timeStamp) + : cbObject(std::move(cbObject)), paramGetter(paramGetter), resultSetter(resultSetter), timeStamp(timeStamp) + { + } + std::shared_ptr cbObject; + ParamGetter paramGetter{ nullptr }; + ResultSetter resultSetter{ nullptr }; + std::chrono::system_clock::time_point timeStamp; + }; + static void Queue(uv_work_t *work, int uvStatus); + static void Execute(uv_work_t *work, int uvStatus); + static bool GetParam(napi_env env, ParamGetter paramGetter, std::vector ¶m); + static void Invoke( + const std::shared_ptr &cbObject, const std::vector ¶m, napi_value &jsOutput); + static void SetResult(napi_env env, ResultSetter resultSetter, napi_value jsOutput); + + JsCallbackInfo *jsCallbackInfo_{ nullptr }; + napi_env env_{}; + static BlockQueue callbackQueue_; }; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/common/js_callback_info.cpp b/frameworks/js/napi/common/js_callback_info.cpp deleted file mode 100644 index bdfd81a50..000000000 --- a/frameworks/js/napi/common/js_callback_info.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "js_callback_info.h" - -#include "global.h" - -namespace OHOS { -namespace MiscServices { -JsCallbackInfo *JsCallbackInfo::Generate(const std::vector> &cbObjects, - ParamGetter paramGetter, ResultSetter resultSetter) -{ - if (cbObjects.empty()) { - IMSA_HILOGE("cbObjects is empty"); - return nullptr; - } - auto callbackInfo = new (std::nothrow) JsCallbackInfo(cbObjects, paramGetter, resultSetter); - if (callbackInfo == nullptr) { - return nullptr; - } - return callbackInfo; -} -void JsCallbackInfo::Purge(void *data) -{ - auto *callbackInfo = static_cast(data); - delete callbackInfo; -} -} // namespace MiscServices -} // namespace OHOS diff --git a/frameworks/js/napi/common/js_callback_info.h b/frameworks/js/napi/common/js_callback_info.h deleted file mode 100644 index 2dbe3c298..000000000 --- a/frameworks/js/napi/common/js_callback_info.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef OHOS_INPUT_CALLBACK_INFO_H -#define OHOS_INPUT_CALLBACK_INFO_H - -#include -#include - -#include "js_callback_object.h" - -namespace OHOS { -namespace MiscServices { -struct CallbackParam { - static constexpr uint32_t MAX_PARAM_NUM = 10; - uint32_t num = 0; - napi_value argv[MAX_PARAM_NUM] = { nullptr }; -}; -using ParamGetter = std::function; -using ResultSetter = std::function; -struct JsCallbackInfo { - JsCallbackInfo( - std::vector> cbObjects, ParamGetter paramGetter, ResultSetter resultSetter) - : cbObjects(std::move(cbObjects)), paramGetter(paramGetter), resultSetter(resultSetter) - { - } - static JsCallbackInfo *Generate(const std::vector> &cbObjects, - ParamGetter paramGetter, ResultSetter resultSetter); - static void Purge(void *data); - - std::vector> cbObjects; - ParamGetter paramGetter{ nullptr }; - ResultSetter resultSetter{ nullptr }; -}; -} // namespace MiscServices -} // namespace OHOS -#endif // OHOS_INPUT_CALLBACK_INFO_H diff --git a/frameworks/js/napi/common/uv_work.cpp b/frameworks/js/napi/common/uv_work.cpp deleted file mode 100644 index 109d729b3..000000000 --- a/frameworks/js/napi/common/uv_work.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "uv_work.h" - -#include "global.h" -#include "js_callback_info.h" -namespace OHOS { -namespace MiscServices { -bool UvWork::Call(uv_loop_s *loop, void *data, DataType type, uv_after_work_cb afterCallback) -{ - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - Purge(data, type); - return false; - } - work->data = data; - auto ret = uv_queue_work_with_qos( - loop, work, [](uv_work_t *work) {}, afterCallback, uv_qos_user_initiated); - if (ret != 0) { - IMSA_HILOGE("uv_queue_work_with_qos failed"); - Purge(work, type); - return false; - } - return true; -} - -void UvWork::Purge(void *data, DataType type) -{ - switch (type) { - case DataType::CALLBACK_INFO: { - JsCallbackInfo::Purge(data); - break; - } - default: { - return; - } - } -} - -void UvWork::Purge(uv_work_t *work, DataType type) -{ - if (work == nullptr) { - return; - } - if (work->data == nullptr) { - delete work; - return; - } - Purge(work->data, type); - delete work; - work = nullptr; -} -} // namespace MiscServices -} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/common/uv_work.h b/frameworks/js/napi/common/uv_work.h deleted file mode 100644 index 48882cbc2..000000000 --- a/frameworks/js/napi/common/uv_work.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef OHOS_INPUT_UV_WORK_H -#define OHOS_INPUT_UV_WORK_H - -#include - -#include -namespace OHOS { -namespace MiscServices { -enum class DataType : int32_t { CALLBACK_INFO = 0 }; -class UvWork { -public: - static bool Call(uv_loop_s *loop, void *data, DataType type, uv_after_work_cb afterCallback); - -private: - static void Purge(uv_work_t *work, DataType type); - static void Purge(void *data, DataType type); -}; -} // namespace MiscServices -} // namespace OHOS -#endif // OHOS_INPUT_UV_WORK_H diff --git a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp index e5fa5fb5e..a752a7066 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -520,49 +520,66 @@ std::vector> JsInputMethodEngineSetting::GetCa void JsInputMethodEngineSetting::OnInputStart() { IMSA_HILOGD("run in"); - auto paramGetter = [](napi_env env, CallbackParam ¶m) { - param.num = 2; - param.argv[0] = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env); - param.argv[1] = JsTextInputClientEngine::GetTextInputClientInstance(env); - return param.argv[0] != nullptr && param.argv[1] != nullptr; + auto paramGetter = [](napi_env env, std::vector ¶m) { + auto value1 = JsKeyboardControllerEngine::GetKeyboardControllerInstance(env); + auto value2 = JsTextInputClientEngine::GetTextInputClientInstance(env); + if (value1 != nullptr && value2 != nullptr) { + param.insert(param.begin(), { value1, value2 }); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("inputStart"), paramGetter); + for (const auto &object : GetCallbackObjects("inputStart")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) { std::string type = isShow ? "keyboardShow" : "keyboardHide"; IMSA_HILOGD("run in, %{public}s", type.c_str()); - JsCallbackHandler::Call(loop_, GetCallbackObjects(type)); + for (const auto &object : GetCallbackObjects(type)) { + JsCallbackHandler handler(object); + handler.Call(); + } } -void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) +void JsInputMethodEngineSetting::OnInputStop() { IMSA_HILOGD("run in"); - JsCallbackHandler::Call(loop_, GetCallbackObjects("inputStop")); + for (const auto &object : GetCallbackObjects("inputStop")) { + JsCallbackHandler handler(object); + handler.Call(); + } } void JsInputMethodEngineSetting::OnSetCallingWindow(uint32_t windowId) { IMSA_HILOGD("run in"); - auto paramGetter = [windowId](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, windowId); - return param.argv[0] != nullptr; + auto paramGetter = [windowId](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, windowId); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("setCallingWindow"), paramGetter); + for (const auto &object : GetCallbackObjects("setCallingWindow")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) { IMSA_HILOGD("run in"); - auto paramGetter = [property](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = GetResultOnSetSubtype(env, property); - return param.argv[0] != nullptr; + auto paramGetter = [property](napi_env env, std::vector ¶m) { + auto value = GetResultOnSetSubtype(env, property); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("setSubtype"), paramGetter); - + for (const auto &object : GetCallbackObjects("setSubtype")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 42429ad0b..890936713 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -293,100 +293,120 @@ std::vector> JsKeyboardDelegateSetting::GetCal bool JsKeyboardDelegateSetting::OnKeyEvent(const std::shared_ptr &keyEvent) { IMSA_HILOGD("run in, whole keyEvent"); - auto paramGetter = [keyEvent](napi_env env, CallbackParam ¶m) { - param.num = 1; - napi_create_object(env, ¶m.argv[0]); - MMI::KeyEventNapi::CreateKeyEvent(env, keyEvent, param.argv[0]); - return param.argv[0] != nullptr; + auto paramGetter = [keyEvent](napi_env env, std::vector ¶m) { + napi_value value = nullptr; + napi_create_object(env, &value); + MMI::KeyEventNapi::CreateKeyEvent(env, keyEvent, value); + if (value != nullptr) { + param.push_back(value); + } }; auto resultHandler = std::make_shared>(MAX_TIMEOUT, false); auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { bool isConsumed = false; - if (JsUtil::GetValue(env, jsOutput, isConsumed)) { - IMSA_HILOGI("whole key event handle result: %{public}d", isConsumed); - resultHandler->SetValue(isConsumed); - return true; - } - return false; + auto ret = JsUtil::GetValue(env, jsOutput, isConsumed); + IMSA_HILOGI("whole key event handle ret: %{public}d, isConsumed: %{public}d", ret, isConsumed); + resultHandler->SetValue(isConsumed); }; - return JsCallbackHandler::Call(loop_, GetCallbackObjects("keyEvent"), paramGetter, resultSetter) - ? resultHandler->GetValue() - : false; + for (const auto &object : GetCallbackObjects("keyEvent")) { + JsCallbackHandler handler(object, paramGetter, resultSetter); + if (handler.Call()) { + return resultHandler->GetValue(); + } + } + return false;; } bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) { IMSA_HILOGD("run in"); std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp"); - auto paramGetter = [keyCode, keyStatus](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = GetResultOnKeyEvent(env, keyCode, keyStatus); - return param.argv[0] != nullptr; + auto paramGetter = [keyCode, keyStatus](napi_env env, std::vector ¶m) { + auto value = GetResultOnKeyEvent(env, keyCode, keyStatus); + if (value != nullptr) { + param.push_back(value); + } }; auto resultHandler = std::make_shared>(MAX_TIMEOUT, false); auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { bool isConsumed = false; - if (JsUtil::GetValue(env, jsOutput, isConsumed)) { - IMSA_HILOGI("key event handle result: %{public}d", isConsumed); - resultHandler->SetValue(isConsumed); - return true; - } - return false; + auto ret = JsUtil::GetValue(env, jsOutput, isConsumed); + IMSA_HILOGI("key event handle ret: %{public}d, isConsumed: %{public}d", ret, isConsumed); + resultHandler->SetValue(isConsumed); }; - return JsCallbackHandler::Call(loop_, GetCallbackObjects(type), paramGetter, resultSetter) - ? resultHandler->GetValue() - : false; + for (const auto &object : GetCallbackObjects(type)) { + JsCallbackHandler handler(object, paramGetter, resultSetter); + if (handler.Call()) { + return resultHandler->GetValue(); + } + } + return false; } void JsKeyboardDelegateSetting::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { IMSA_HILOGD("run in"); - auto paramGetter = [positionX, positionY, height](napi_env env, CallbackParam ¶m) { - param.num = 3; - param.argv[0] = JsUtil::GetValue(env, positionX); - param.argv[1] = JsUtil::GetValue(env, positionY); - param.argv[2] = JsUtil::GetValue(env, height); - return param.argv[0] != nullptr && param.argv[1] != nullptr && param.argv[2] != nullptr; + auto paramGetter = [positionX, positionY, height](napi_env env, std::vector ¶m) { + auto value1 = JsUtil::GetValue(env, positionX); + auto value2 = JsUtil::GetValue(env, positionY); + auto value3 = JsUtil::GetValue(env, height); + if (value1 != nullptr && value2 != nullptr && value3 != nullptr) { + param.insert(param.begin(), { value1, value2, value3 }); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("cursorContextChange"), paramGetter); + for (const auto &object : GetCallbackObjects("cursorContextChange")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsKeyboardDelegateSetting::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { IMSA_HILOGD("run in"); - auto paramGetter = [oldBegin, oldEnd, newBegin, newEnd](napi_env env, CallbackParam ¶m) { - param.num = 4; - param.argv[0] = JsUtil::GetValue(env, oldBegin); - param.argv[1] = JsUtil::GetValue(env, oldEnd); - param.argv[2] = JsUtil::GetValue(env, newBegin); - param.argv[3] = JsUtil::GetValue(env, newEnd); - return param.argv[0] != nullptr && param.argv[1] != nullptr && param.argv[2] != nullptr - && param.argv[3] != nullptr; + auto paramGetter = [oldBegin, oldEnd, newBegin, newEnd](napi_env env, std::vector ¶m) { + auto value1 = JsUtil::GetValue(env, oldBegin); + auto value2 = JsUtil::GetValue(env, oldEnd); + auto value3 = JsUtil::GetValue(env, newBegin); + auto value4 = JsUtil::GetValue(env, newEnd); + if (value1 != nullptr && value2 != nullptr && value3 != nullptr && value4 != nullptr) { + param.insert(param.begin(), { value1, value2, value3, value4 }); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("selectionChange"), paramGetter); + for (const auto &object : GetCallbackObjects("selectionChange")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsKeyboardDelegateSetting::OnTextChange(const std::string &text) { IMSA_HILOGD("run in"); std::string type = "textChange"; - auto paramGetter = [text](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, text); - return param.argv[0] != nullptr; + auto paramGetter = [text](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, text); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects(type), paramGetter); + for (const auto &object : GetCallbackObjects(type)) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsKeyboardDelegateSetting::OnEditorAttributeChange(const InputAttribute &inputAttribute) { IMSA_HILOGI("run in"); - auto paramGetter = [inputAttribute](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtils::GetValue(env, inputAttribute); - return param.argv[0] != nullptr; + auto paramGetter = [inputAttribute](napi_env env, std::vector ¶m) { + auto value = JsUtils::GetValue(env, inputAttribute); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("editorAttributeChanged"), paramGetter); + for (const auto &object : GetCallbackObjects("editorAttributeChanged")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp index d4ed4e36f..5916f2f18 100644 --- a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp +++ b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp @@ -89,10 +89,8 @@ void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow) IMSA_HILOGE("no callback in map!"); return; } - uv_loop_s *loop = nullptr; - - napi_get_uv_event_loop(callback.second->env_, &loop); - JsCallbackHandler::Call(loop, { callback.second }); + JsCallbackHandler handler(callback.second); + handler.Call(); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 6c12fab4b..febf00fc9 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -696,67 +696,91 @@ std::vector> JsGetInputMethodController::GetCa void JsGetInputMethodController::OnSelectByRange(int32_t start, int32_t end) { IMSA_HILOGD("run in, start: %{public}d, end: %{public}d", start, end); - auto paramGetter = [start, end](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = CreateSelectRange(env, start, end); - return param.argv[0] != nullptr; + auto paramGetter = [start, end](napi_env env, std::vector ¶m) { + autop value = CreateSelectRange(env, start, end); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("selectByRange"), paramGetter); + for (const auto &object : GetCallbackObjects("selectByRange")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::OnSelectByMovement(int32_t direction) { IMSA_HILOGD("run in, direction: %{public}d", direction); - auto paramGetter = [direction](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = CreateSelectMovement(env, direction); - return param.argv[0] != nullptr; + auto paramGetter = [direction](napi_env env, std::vector ¶m) { + auto value = CreateSelectMovement(env, direction); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("selectByMovement"), paramGetter); + for (const auto &object : GetCallbackObjects("selectByMovement")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::InsertText(const std::u16string &text) { IMSA_HILOGD("run in"); - auto paramGetter = [text](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, Str16ToStr8(text)); - return param.argv[0] != nullptr; + auto paramGetter = [text](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, Str16ToStr8(text)); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("insertText"), paramGetter); + for (const auto &object : GetCallbackObjects("insertText")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::DeleteRight(int32_t length) { IMSA_HILOGD("run in"); - auto paramGetter = [length](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, length); - return param.argv[0] != nullptr; + auto paramGetter = [length](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, length); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("deleteRight"), paramGetter); + for (const auto &object : GetCallbackObjects("deleteRight")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::DeleteLeft(int32_t length) { IMSA_HILOGD("run in"); - auto paramGetter = [length](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, length); - return param.argv[0] != nullptr; + auto paramGetter = [length](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, length); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("deleteLeft"), paramGetter); + for (const auto &object : GetCallbackObjects("deleteLeft")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::SendKeyboardStatus(const KeyboardStatus &status) { IMSA_HILOGD("run in"); - auto paramGetter = [status](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, static_cast(status)); - return param.argv[0] != nullptr; + auto paramGetter = [status](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, static_cast(status)); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("sendKeyboardStatus"), paramGetter); + for (const auto &object : GetCallbackObjects("sendKeyboardStatus")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } napi_value JsGetInputMethodController::CreateSendFunctionKey(napi_env env, int32_t functionKey) @@ -774,57 +798,71 @@ napi_value JsGetInputMethodController::CreateSendFunctionKey(napi_env env, int32 void JsGetInputMethodController::SendFunctionKey(const FunctionKey &functionKey) { IMSA_HILOGD("run in"); - auto paramGetter = [functionKey](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = CreateSendFunctionKey(env, static_cast(functionKey.GetEnterKeyType())); - return param.argv[0] != nullptr; + auto paramGetter = [functionKey](napi_env env, std::vector ¶m) { + auto value = CreateSendFunctionKey(env, static_cast(functionKey.GetEnterKeyType())); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("sendFunctionKey"), paramGetter); + for (const auto &object : GetCallbackObjects("sendFunctionKey")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::MoveCursor(const Direction direction) { IMSA_HILOGD("run in"); - auto paramGetter = [direction](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, static_cast(direction)); - return param.argv[0] != nullptr; + auto paramGetter = [direction](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, static_cast(direction)); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("moveCursor"), paramGetter); + for (const auto &object : GetCallbackObjects("moveCursor")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodController::HandleExtendAction(int32_t action) { IMSA_HILOGD("run in"); - auto paramGetter = [action](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, action); - return param.argv[0] != nullptr; + auto paramGetter = [action](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, action); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("handleExtendAction"), paramGetter); + for (const auto &object : GetCallbackObjects("handleExtendAction")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } std::u16string JsGetInputMethodController::GetText(const std::string &type, int32_t number) { IMSA_HILOGD("run in"); - auto paramGetter = [number](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtil::GetValue(env, number); - return param.argv[0] != nullptr; + auto paramGetter = [number](napi_env env, std::vector ¶m) { + auto value = JsUtil::GetValue(env, number); + if (value != nullptr) { + param.push_back(value); + } }; auto resultHandler = std::make_shared>(MAX_TIMEOUT, ""); auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { std::string text; - if (JsUtil::GetValue(env, jsOutput, text)) { - IMSA_HILOGD("get text success"); - resultHandler->SetValue(text); - return true; - } - return false; + auto ret = JsUtil::GetValue(env, jsOutput, text); + IMSA_HILOGI("get text ret: %{public}d", ret); + resultHandler->SetValue(text); }; - return JsCallbackHandler::Call(loop_, GetCallbackObjects(type), paramGetter, resultSetter) - ? Str8ToStr16(resultHandler->GetValue()) - : u""; + for (const auto &object : GetCallbackObjects(type)) { + JsCallbackHandler handler(object, paramGetter, resultSetter); + if (handler.Call()) { + return Str8ToStr16(resultHandler->GetValue()); + } + } + return u""; } int32_t JsGetInputMethodController::GetTextIndexAtCursor() @@ -833,16 +871,17 @@ int32_t JsGetInputMethodController::GetTextIndexAtCursor() auto resultHandler = std::make_shared>(MAX_TIMEOUT, -1); auto resultSetter = [resultHandler](napi_env env, napi_value jsOutput) { int32_t index = -1; - if (JsUtil::GetValue(env, jsOutput, index)) { - IMSA_HILOGD("get index success: %{public}d", index); - resultHandler->SetValue(index); - return true; - } - return false; + auto ret = JsUtil::GetValue(env, jsOutput, index); + IMSA_HILOGD("get index ret: %{public}d, index :%{public}d", ret, index); + resultHandler->SetValue(index); }; - return JsCallbackHandler::Call(loop_, GetCallbackObjects("getTextIndexAtCursor"), nullptr, resultSetter) - ? resultHandler->GetValue() - : -1; + for (const auto &object : GetCallbackObjects("getTextIndexAtCursor")) { + JsCallbackHandler handler(object, nullptr, resultSetter); + if (handler.Call()) { + return resultHandler->GetValue(); + } + } + return -1; } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp index eb296b4bc..cb199337b 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -485,13 +485,17 @@ std::vector> JsGetInputMethodSetting::GetCallb void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty) { IMSA_HILOGD("run in"); - auto paramGetter = [property, subProperty](napi_env env, CallbackParam ¶m) { - param.num = 2; - param.argv[0] = JsInputMethod::GetJsInputMethodSubProperty(env, subProperty); - param.argv[1] = JsInputMethod::GetJsInputMethodProperty(env, property); - return param.argv[0] != nullptr && param.argv[1] != nullptr; + auto paramGetter = [property, subProperty](napi_env env, std::vector ¶m) { + auto value1 = JsInputMethod::GetJsInputMethodSubProperty(env, subProperty); + auto value2 = JsInputMethod::GetJsInputMethodProperty(env, property); + if (value1 != nullptr && value2 != nullptr) { + param.insert(param.begin(), { value1, value2 }); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects("imeChange"), paramGetter); + for (const auto &object : GetCallbackObjects("imeChange")) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } void JsGetInputMethodSetting::OnPanelStatusChange( @@ -502,12 +506,16 @@ void JsGetInputMethodSetting::OnPanelStatusChange( if (it == PANEL_STATUS.end()) { return; } - auto paramGetter = [windowInfo](napi_env env, CallbackParam ¶m) { - param.num = 1; - param.argv[0] = JsUtils::GetValue(env, windowInfo); - return param.argv[0] != nullptr; + auto paramGetter = [windowInfo](napi_env env, std::vector ¶m) { + auto value = JsUtils::GetValue(env, windowInfo); + if (value != nullptr) { + param.push_back(value); + } }; - JsCallbackHandler::Call(loop_, GetCallbackObjects(it->second), paramGetter); + for (const auto &object : GetCallbackObjects(it->second)) { + JsCallbackHandler handler(object, paramGetter); + handler.Call(); + } } } // namespace MiscServices } // namespace OHOS \ No newline at end of file -- Gitee From ef3a7d6c6bfaf3bad99d2ecfb14aaea0198d4487 Mon Sep 17 00:00:00 2001 From: cy7717 Date: Thu, 17 Aug 2023 20:50:56 +0800 Subject: [PATCH 3/3] MOD Signed-off-by: cy7717 --- frameworks/js/napi/common/BUILD.gn | 1 + .../js/napi/common/js_callback_handler.cpp | 16 +++++++--------- frameworks/js/napi/common/js_callback_handler.h | 10 +++++----- .../js_input_method_engine_setting.cpp | 2 +- .../js_get_input_method_controller.cpp | 2 +- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/frameworks/js/napi/common/BUILD.gn b/frameworks/js/napi/common/BUILD.gn index eeb614d94..b04501fee 100644 --- a/frameworks/js/napi/common/BUILD.gn +++ b/frameworks/js/napi/common/BUILD.gn @@ -32,6 +32,7 @@ config("inputmethod_js_common_public_config") { visibility = [ "./*" ] include_dirs = [ "./", + "${inputmethod_path}/common", "${inputmethod_path}/frameworks/common", "${inputmethod_path}/services/dfx/include", "${inputmethod_path}/services/include", diff --git a/frameworks/js/napi/common/js_callback_handler.cpp b/frameworks/js/napi/common/js_callback_handler.cpp index 355458b0d..edbb2b90f 100644 --- a/frameworks/js/napi/common/js_callback_handler.cpp +++ b/frameworks/js/napi/common/js_callback_handler.cpp @@ -20,15 +20,13 @@ namespace OHOS { namespace MiscServices { constexpr int32_t MAX_WAIT_TIME = 5000; -BlockQueue JsCallbackHandler::callbackQueue_{ - MAX_WAIT_TIME -}; //todo 如果超时怎么处理 +BlockQueue JsCallbackHandler::callbackQueue_{ MAX_WAIT_TIME }; //todo 如果超时怎么处理 JsCallbackHandler::JsCallbackHandler( const std::shared_ptr &cbObject, ParamGetter paramGetter, ResultSetter resultSetter) { env_ = cbObject->env_; - jsCallbackInfo_ = new (std::nothrow) - JsCallbackInfo(cbObject, paramGetter, resultSetter, std::chrono::system_clock::now()); + jsCallbackInfo_ = new (std::nothrow) JsCallbackInfo( + cbObject, paramGetter, resultSetter, std::chrono::system_clock::now().time_since_epoch().count()); } bool JsCallbackHandler::Call() @@ -62,10 +60,10 @@ bool JsCallbackHandler::Call() return true; } -void JsCallbackHandler::Queue(uv_work_t *work, int uvStatus) +void JsCallbackHandler::Queue(uv_work_t *work) { IMSA_HILOGD("run in"); - std::shared_ptr callbackInfo(static_cast(work->data)); + JsCallbackInfo *callbackInfo = static_cast(work->data); // todo 在此处等待 callbackQueue_.Wait(callbackInfo->timeStamp); } @@ -86,11 +84,11 @@ void JsCallbackHandler::Execute(uv_work_t *work, int uvStatus) std::vector param; if (!GetParam(env, callbackInfo->paramGetter, param)) { IMSA_HILOGE("param get failed"); - SetResult(callbackInfo->cbObject->env_, callbackInfo->resultSetter, jsOutput); + SetResult(env, callbackInfo->resultSetter, jsOutput); return; } Invoke(callbackInfo->cbObject, param, jsOutput); - SetResult(callbackInfo->cbObject->env_, callbackInfo->resultSetter, jsOutput); + SetResult(env, callbackInfo->resultSetter, jsOutput); // todo 在此处出队 callbackQueue_.Pop(); } diff --git a/frameworks/js/napi/common/js_callback_handler.h b/frameworks/js/napi/common/js_callback_handler.h index 4f6c6c725..ee3b1cc3f 100644 --- a/frameworks/js/napi/common/js_callback_handler.h +++ b/frameworks/js/napi/common/js_callback_handler.h @@ -23,7 +23,7 @@ namespace OHOS { namespace MiscServices { class JsCallbackHandler { public: - using ParamGetter = std::function&)>; + using ParamGetter = std::function &)>; using ResultSetter = std::function; JsCallbackHandler(const std::shared_ptr &cbObject, ParamGetter paramGetter = nullptr, ResultSetter resultSetter = nullptr); @@ -32,16 +32,16 @@ public: private: struct JsCallbackInfo { JsCallbackInfo(std::shared_ptr cbObject, ParamGetter paramGetter, ResultSetter resultSetter, - std::chrono::system_clock::time_point timeStamp) + int64_t timeStamp) : cbObject(std::move(cbObject)), paramGetter(paramGetter), resultSetter(resultSetter), timeStamp(timeStamp) { } std::shared_ptr cbObject; ParamGetter paramGetter{ nullptr }; ResultSetter resultSetter{ nullptr }; - std::chrono::system_clock::time_point timeStamp; + int64_t timeStamp; }; - static void Queue(uv_work_t *work, int uvStatus); + static void Queue(uv_work_t *work); static void Execute(uv_work_t *work, int uvStatus); static bool GetParam(napi_env env, ParamGetter paramGetter, std::vector ¶m); static void Invoke( @@ -50,7 +50,7 @@ private: JsCallbackInfo *jsCallbackInfo_{ nullptr }; napi_env env_{}; - static BlockQueue callbackQueue_; + static BlockQueue callbackQueue_; }; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp index a752a7066..4d6d6f5c7 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -543,7 +543,7 @@ void JsInputMethodEngineSetting::OnKeyboardStatus(bool isShow) } } -void JsInputMethodEngineSetting::OnInputStop() +void JsInputMethodEngineSetting::OnInputStop(const std::string &imeId) { IMSA_HILOGD("run in"); for (const auto &object : GetCallbackObjects("inputStop")) { diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index febf00fc9..94e6d1001 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -697,7 +697,7 @@ void JsGetInputMethodController::OnSelectByRange(int32_t start, int32_t end) { IMSA_HILOGD("run in, start: %{public}d, end: %{public}d", start, end); auto paramGetter = [start, end](napi_env env, std::vector ¶m) { - autop value = CreateSelectRange(env, start, end); + auto value = CreateSelectRange(env, start, end); if (value != nullptr) { param.push_back(value); } -- Gitee