diff --git a/frameworks/js/napi/common/BUILD.gn b/frameworks/js/napi/common/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..54a2aaf811c3ff15a76b6da97672dd8eb1a4efc0 --- /dev/null +++ b/frameworks/js/napi/common/BUILD.gn @@ -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. + +import("//base/inputmethod/imf/inputmethod.gni") +import("//build/ohos.gni") + +config("inputmethod_js_common_config") { + visibility = [ ":*" ] + include_dirs = [ "./" ] + ldflags = [ "-Wl,--exclude-libs=ALL" ] + cflags = [ + "-fdata-sections", + "-ffunction-sections", + "-fvisibility=hidden", + ] +} + +config("inputmethod_js_common_public_config") { + visibility = [] + include_dirs = [ + "./", + "${inputmethod_path}/frameworks/native/inputmethod_controller/include", + ] +} + +ohos_static_library("inputmethod_js_common") { + sources = [ + "js_property.cpp", + "js_subtype.cpp", + "js_util.cpp", + ] + configs = [ ":inputmethod_js_common_config" ] + public_configs = [ ":inputmethod_js_common_public_config" ] + deps = [] + external_deps = [ "napi:ace_napi" ] + subsystem_name = "inputmethod" + part_name = "imf" +} diff --git a/frameworks/js/napi/common/js_property.cpp b/frameworks/js/napi/common/js_property.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a4063f7c91f756f695af73d4ee9da80b8ad2614 --- /dev/null +++ b/frameworks/js/napi/common/js_property.cpp @@ -0,0 +1,53 @@ +/* +* 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_property.h" + +#include "js_util.h" +namespace OHOS { +namespace MiscServices { +napi_value JsProperty::Write(napi_env env, const Property &nativeObject) +{ + napi_value object = nullptr; + napi_create_object(env, &object); + auto ret = JsUtil::Object::WriteProperty(env, object, "name", nativeObject.name); + ret = ret && JsUtil::Object::WriteProperty(env, object, "packageName", nativeObject.name); + ret = ret && JsUtil::Object::WriteProperty(env, object, "id", nativeObject.id); + ret = ret && JsUtil::Object::WriteProperty(env, object, "methodId", nativeObject.id); + ret = ret && JsUtil::Object::WriteProperty(env, object, "icon", nativeObject.icon); + ret = ret && JsUtil::Object::WriteProperty(env, object, "iconId", nativeObject.iconId); + return ret ? object : JsUtil::Const::Null(env); +} +napi_value JsProperty::Write(napi_env env, const std::shared_ptr &nativeObject) +{ + if (nativeObject == nullptr) { + return JsUtil::Const::Null(env); + } + return Write(env, *nativeObject); +} +bool JsProperty::Read(napi_env env, napi_value object, Property &nativeObject) +{ + auto ret = JsUtil::Object::ReadProperty(env, object, "name", nativeObject.name); + ret = ret && JsUtil::Object::ReadProperty(env, object, "id", nativeObject.id); + if (nativeObject.name.empty() || nativeObject.id.empty()) { + ret = JsUtil::Object::ReadProperty(env, object, "packageName", nativeObject.name); + ret = ret && JsUtil::Object::ReadProperty(env, object, "methodId", nativeObject.id); + } + // ignore optional attributes here + JsUtil::Object::ReadProperty(env, object, "icon", nativeObject.icon); + JsUtil::Object::ReadProperty(env, object, "iconId", nativeObject.iconId); + return ret; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/common/js_property.h b/frameworks/js/napi/common/js_property.h new file mode 100644 index 0000000000000000000000000000000000000000..53df620718f112a9bf52fe273ce717c95a0ed96b --- /dev/null +++ b/frameworks/js/napi/common/js_property.h @@ -0,0 +1,30 @@ +/* +* 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 INPUTMETHOD_IMF_JS_PROPERTY_H +#define INPUTMETHOD_IMF_JS_PROPERTY_H +#include + +#include "input_method_property.h" +#include "js_native_api.h" +namespace OHOS { +namespace MiscServices { +struct JsProperty { + static napi_value Write(napi_env env, const std::shared_ptr &nativeObject); + static napi_value Write(napi_env env, const Property &nativeObject); + static bool Read(napi_env env, napi_value object, Property &nativeObject); +}; +} // namespace MiscServices +} // namespace OHOS +#endif // INPUTMETHOD_IMF_JS_PROPERTY_H diff --git a/frameworks/js/napi/common/js_subtype.cpp b/frameworks/js/napi/common/js_subtype.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1da8a1cb2b395c6abe8a110c4043de348b9b75df --- /dev/null +++ b/frameworks/js/napi/common/js_subtype.cpp @@ -0,0 +1,57 @@ +/* +* 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_subtype.h" + +#include "js_util.h" +namespace OHOS { +namespace MiscServices { +napi_value JsSubtype::Write(napi_env env, const std::shared_ptr &nativeObject) +{ + if (nativeObject == nullptr) { + return JsUtil::Const::Null(env); + } + return Write(env, *nativeObject); +} +napi_value JsSubtype::Write(napi_env env, const SubProperty &nativeObject) +{ + napi_value object = nullptr; + napi_create_object(env, &object); + auto ret = JsUtil::Object::WriteProperty(env, object, "name", nativeObject.name); + ret = ret && JsUtil::Object::WriteProperty(env, object, "id", nativeObject.id); + ret = ret && JsUtil::Object::WriteProperty(env, object, "label", nativeObject.label); + ret = ret && JsUtil::Object::WriteProperty(env, object, "labelId", nativeObject.labelId); + ret = ret && JsUtil::Object::WriteProperty(env, object, "icon", nativeObject.icon); + ret = ret && JsUtil::Object::WriteProperty(env, object, "iconId", nativeObject.iconId); + ret = ret && JsUtil::Object::WriteProperty(env, object, "mode", nativeObject.mode); + ret = ret && JsUtil::Object::WriteProperty(env, object, "locale", nativeObject.locale); + ret = ret && JsUtil::Object::WriteProperty(env, object, "language", nativeObject.language); + return ret ? object : JsUtil::Const::Null(env); +} +bool JsSubtype::Read(napi_env env, napi_value object, SubProperty &nativeObject) +{ + auto ret = JsUtil::Object::ReadProperty(env, object, "name", nativeObject.name); + ret = ret && JsUtil::Object::ReadProperty(env, object, "id", nativeObject.id); + // ignore optional attributes here + JsUtil::Object::ReadProperty(env, object, "label", nativeObject.label); + JsUtil::Object::ReadProperty(env, object, "labelId", nativeObject.labelId); + JsUtil::Object::ReadProperty(env, object, "icon", nativeObject.icon); + JsUtil::Object::ReadProperty(env, object, "iconId", nativeObject.iconId); + JsUtil::Object::ReadProperty(env, object, "mode", nativeObject.mode); + JsUtil::Object::ReadProperty(env, object, "locale", nativeObject.locale); + JsUtil::Object::ReadProperty(env, object, "language", nativeObject.language); + return ret; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/common/js_subtype.h b/frameworks/js/napi/common/js_subtype.h new file mode 100644 index 0000000000000000000000000000000000000000..0254a83bf68ecdd0616b08611188d2b72765c7d6 --- /dev/null +++ b/frameworks/js/napi/common/js_subtype.h @@ -0,0 +1,31 @@ +/* +* 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 INPUTMETHOD_IMF_JS_SUBTYPE_H +#define INPUTMETHOD_IMF_JS_SUBTYPE_H +#include + +#include "input_method_property.h" +#include "js_native_api.h" +namespace OHOS { +namespace MiscServices { +class JsSubtype { +public: + static napi_value Write(napi_env env, const std::shared_ptr &nativeObject); + static napi_value Write(napi_env env, const SubProperty &nativeObject); + static bool Read(napi_env env, napi_value object, SubProperty &nativeObject); +}; +} // namespace MiscServices +} // namespace OHOS +#endif //INPUTMETHOD_IMF_JS_SUBTYPE_H diff --git a/frameworks/js/napi/common/js_util.cpp b/frameworks/js/napi/common/js_util.cpp index 6b39792d71fdd0d2f5585db75683b7bce8055270..092a90b24a72b7374b1036b02cf9189724836d00 100644 --- a/frameworks/js/napi/common/js_util.cpp +++ b/frameworks/js/napi/common/js_util.cpp @@ -14,7 +14,8 @@ */ #include "js_util.h" -namespace OHOS::MiscServices { +namespace OHOS { +namespace MiscServices { constexpr int64_t JS_NUMBER_MAX_VALUE = (1LL << 53) - 1; bool JsUtil::GetValue(napi_env env, napi_value in, std::string &out) { @@ -77,4 +78,9 @@ napi_value JsUtil::GetValue(napi_env env, bool in) napi_get_boolean(env, in, &out); return out; } -} // namespace OHOS::MiscServices \ No newline at end of file +napi_value JsUtil::GetValue(napi_env env, napi_value in) +{ + return in; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/common/js_util.h b/frameworks/js/napi/common/js_util.h index bda99eba28c9db69482ba467ab2c216e14a5ed65..7ce99ad7b6f7f79de19d0500217ed9741b045f72 100644 --- a/frameworks/js/napi/common/js_util.h +++ b/frameworks/js/napi/common/js_util.h @@ -13,14 +13,16 @@ * limitations under the License. */ -#ifndef OHOS_JS_UTIL_H -#define OHOS_JS_UTIL_H +#ifndef OHOS_MISCSERVICES_JS_UTIL_H +#define OHOS_MISCSERVICES_JS_UTIL_H #include #include #include "napi/native_api.h" +#include "napi/native_common.h" #include "napi/native_node_api.h" -namespace OHOS::MiscServices { +namespace OHOS { +namespace MiscServices { class JsUtil { public: // js to native @@ -52,6 +54,7 @@ public: static napi_value GetValue(napi_env env, uint32_t in); static napi_value GetValue(napi_env env, int64_t in); static napi_value GetValue(napi_env env, bool in); + static napi_value GetValue(napi_env env, napi_value in); template static napi_value GetValue(napi_env env, const std::vector &items) { napi_value array = nullptr; @@ -118,5 +121,6 @@ public: napi_handle_scope scope_; }; }; -} // namespace OHOS::MiscServices -#endif // OHOS_JS_UTIL_H +} // namespace MiscServices +} // namespace OHOS +#endif // OHOS_MISCSERVICES_JS_UTIL_H diff --git a/frameworks/js/napi/inputmethodability/BUILD.gn b/frameworks/js/napi/inputmethodability/BUILD.gn index 30e1f7745aabc23800611da9106a182b415b4afd..5a0a9d08120c6fd7102ae7f3d6217352a09c4d5b 100644 --- a/frameworks/js/napi/inputmethodability/BUILD.gn +++ b/frameworks/js/napi/inputmethodability/BUILD.gn @@ -58,7 +58,10 @@ ohos_shared_library("inputmethodengine") { configs = [ ":inputmethodengine_native_config" ] - deps = [ "${inputmethod_path}/frameworks/native/inputmethod_ability:inputmethod_ability" ] + deps = [ + "${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common", + "${inputmethod_path}/frameworks/native/inputmethod_ability:inputmethod_ability", + ] external_deps = [ "ability_base:configuration", 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 c730b648875c554cd830a5c244947c5765b0d9a6..08a8b1c58cb58d2831a7eb18ceb0ad4c874979f3 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -25,6 +25,8 @@ #include "js_text_input_client_engine.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "js_subtype.h" +#include "js_util.h" #include "napi_base_context.h" namespace OHOS { @@ -139,9 +141,7 @@ napi_value JsInputMethodEngineSetting::JsConstructor(napi_env env, napi_callback auto setting = GetInputMethodEngineSetting(); if (setting == nullptr || !InitInputMethodSetting()) { IMSA_HILOGE("failed to get setting"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_status status = napi_wrap( env, thisVar, setting.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr); @@ -180,6 +180,22 @@ napi_value JsInputMethodEngineSetting::GetIMEInstance(napi_env env, napi_callbac return instance; } +napi_value JsInputMethodEngineSetting::MoveCursor(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_MAX; + napi_value argv[ARGC_MAX] = { nullptr }; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + NAPI_ASSERT(env, argc == 1, "Wrong number of arguments, requires 1"); + + int32_t number = 0; + if (!JsUtil::GetValue(env, argv[ARGC_ZERO], number)) { + IMSA_HILOGE("Get number error"); + } + + InputMethodAbility::GetInstance()->MoveCursor(number); + return JsUtil::Const::Null(env); +} + void JsInputMethodEngineSetting::RegisterListener( napi_value callback, std::string type, std::shared_ptr callbackObj) { @@ -238,9 +254,9 @@ napi_value JsInputMethodEngineSetting::Subscribe(napi_env env, napi_callback_inf PARAM_CHECK_RETURN( env, (argc >= ARGC_TWO) && (argc <= ARGC_MAX), "Wrong number of arguments, requires 2", TYPE_NONE, nullptr); - std::string type = ""; - napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], type); - NAPI_ASSERT_BASE(env, status == napi_ok, "get type failed!", nullptr); + std::string type; + bool ret = JsUtil::GetValue(env, argv[ARGC_ZERO], type); + NAPI_ASSERT_BASE(env, ret, "get type failed!", nullptr); IMSA_HILOGE("event type is: %{public}s", type.c_str()); napi_valuetype valueType = napi_undefined; @@ -254,10 +270,7 @@ napi_value JsInputMethodEngineSetting::Subscribe(napi_env env, napi_callback_inf std::shared_ptr callback = std::make_shared(env, argv[ARGC_ONE], std::this_thread::get_id()); engine->RegisterListener(argv[ARGC_ONE], type, callback); - - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_status JsInputMethodEngineSetting::GetContext(napi_env env, napi_value in, @@ -277,6 +290,24 @@ napi_status JsInputMethodEngineSetting::GetContext(napi_env env, napi_value in, return napi_ok; } +/* napi_value <-> PanelInfo */ +napi_status JsInputMethodEngineSetting::GetValue(napi_env env, napi_value in, PanelInfo &out) +{ + IMSA_HILOGD("get PanelInfo"); + int32_t panelType = 0; + bool ret = JsUtil::Object::ReadProperty(env, in, "type", panelType); + + // flag is optional. flag isn't need when panelType is status_bar. + int32_t panelFlag = 0; + if (panelType != PanelType::STATUS_BAR) { + ret = ret && JsUtil::Object::ReadProperty(env, in, "flag", panelFlag); + } + + out.panelType = PanelType(panelType); + out.panelFlag = PanelFlag(panelFlag); + return ret ? napi_ok : napi_generic_failure; +} + napi_value JsInputMethodEngineSetting::CreatePanel(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); @@ -293,7 +324,7 @@ napi_value JsInputMethodEngineSetting::CreatePanel(napi_env env, napi_callback_i // 1 means parameter of info napi_typeof(env, argv[1], &valueType); PARAM_CHECK_RETURN(env, valueType == napi_object, " panelInfo: ", TYPE_OBJECT, napi_invalid_arg); - status = JsUtils::GetValue(env, argv[1], ctxt->panelInfo); + status = GetValue(env, argv[1], ctxt->panelInfo); PARAM_CHECK_RETURN(env, status == napi_ok, " panelInfo: ", TYPE_OBJECT, napi_invalid_arg); return status; }; @@ -387,8 +418,8 @@ napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_i void *data = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); PARAM_CHECK_RETURN(env, argc >= 1, "Wrong number of arguments, requires 1 or 2", TYPE_NONE, nullptr); - std::string type = ""; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + std::string type; + JsUtil::GetValue(env, argv[ARGC_ZERO], type); IMSA_HILOGD("event type is: %{public}s", type.c_str()); auto setting = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (setting == nullptr) { @@ -401,57 +432,7 @@ napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_i PARAM_CHECK_RETURN(env, valueType == napi_function, " 'callback' ", TYPE_FUNCTION, nullptr); } setting->UnRegisterListener(argv[ARGC_ONE], type); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; -} - -napi_value JsInputMethodEngineSetting::GetResultOnSetSubtype(napi_env env, const SubProperty &property) -{ - napi_value subType = nullptr; - napi_create_object(env, &subType); - - napi_value label = nullptr; - napi_create_string_utf8(env, property.label.c_str(), property.name.size(), &label); - napi_set_named_property(env, subType, "label", label); - - napi_value labelId = nullptr; - napi_create_int32(env, property.labelId, &labelId); - napi_set_named_property(env, subType, "labelId", labelId); - - napi_value name = nullptr; - napi_create_string_utf8(env, property.name.c_str(), property.name.size(), &name); - napi_set_named_property(env, subType, "name", name); - - napi_value id = nullptr; - napi_create_string_utf8(env, property.id.c_str(), property.id.size(), &id); - napi_set_named_property(env, subType, "id", id); - - napi_value mode = nullptr; - napi_create_string_utf8(env, property.mode.c_str(), property.mode.size(), &mode); - napi_set_named_property(env, subType, "mode", mode); - - napi_value locale = nullptr; - napi_create_string_utf8(env, property.locale.c_str(), property.locale.size(), &locale); - napi_set_named_property(env, subType, "locale", locale); - - napi_value language = nullptr; - napi_create_string_utf8(env, property.language.c_str(), property.language.size(), &language); - napi_set_named_property(env, subType, "language", language); - - napi_value icon = nullptr; - napi_create_string_utf8(env, property.icon.c_str(), property.icon.size(), &icon); - napi_set_named_property(env, subType, "icon", icon); - - napi_value iconId = nullptr; - napi_create_int32(env, property.iconId, &iconId); - napi_set_named_property(env, subType, "iconId", iconId); - - napi_value extra = nullptr; - napi_create_object(env, &extra); - napi_set_named_property(env, subType, "extra", extra); - - return subType; + return JsUtil::Const::Null(env); } void JsInputMethodEngineSetting::OnInputStart() @@ -615,9 +596,9 @@ void JsInputMethodEngineSetting::OnSetSubtype(const SubProperty &property) if (argc == 0) { return false; } - napi_value jsObject = GetResultOnSetSubtype(item->env_, entry->subProperty); + napi_value jsObject = JsSubtype::Write(item->env_, entry->subProperty); if (jsObject == nullptr) { - IMSA_HILOGE("get GetResultOnSetSubtype failed: jsObject is nullptr"); + IMSA_HILOGE("prepare js sub-property failed"); return false; } args[ARGC_ZERO] = { jsObject }; 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 e17f5cab161e31a181ea63c0103f7149983d2f00..13cf2da28c92ada7d6196c5a16326cd6ddb66003 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" @@ -84,8 +85,9 @@ private: std::shared_ptr &context); 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); static napi_ref New(napi_env env, void **out, napi_value constructor); + static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out); + static const std::string IMES_CLASS_NAME; static thread_local napi_ref IMESRef_; struct UvEntry { @@ -94,8 +96,8 @@ private: std::string imeid; uint32_t windowid = 0; SubProperty subProperty; - UvEntry(const std::vector> &cbVec, const std::string &type) - : vecCopy(cbVec), type(type) + UvEntry(std::vector> cbVec, std::string type) + : vecCopy(std::move(cbVec)), type(std::move(type)) { } }; diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_controller_engine.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_controller_engine.cpp index d8e973218c8632d9ab528ebbae68987f1f22f6d8..88736664b6915d69608c972f41d8fbc22ce07c09 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_controller_engine.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_controller_engine.cpp @@ -18,6 +18,7 @@ #include "input_method_ability.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -46,9 +47,7 @@ napi_value JsKeyboardControllerEngine::JsConstructor(napi_env env, napi_callback JsKeyboardControllerEngine *controllerObject = new (std::nothrow) JsKeyboardControllerEngine(); if (controllerObject == nullptr) { IMSA_HILOGE("controllerObject is nullptr"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } auto finalize = [](napi_env env, void *data, void *hint) { IMSA_HILOGE("JsKeyboardControllerEngine finalize"); diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 53e64f7618f42c9ab62fcb0461aa47032c8ae7aa..65aa234ce43ad311f93ecbb63aaca8b175d79551 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -21,6 +21,7 @@ #include "js_utils.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -119,9 +120,7 @@ napi_value JsKeyboardDelegateSetting::JsConstructor(napi_env env, napi_callback_ auto delegate = GetKeyboardDelegateSetting(); if (delegate == nullptr || !InitKeyboardDelegate()) { IMSA_HILOGE("failed to get delegate"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_status status = napi_wrap( env, thisVar, delegate.get(), [](napi_env env, void *nativeObject, void *hint) {}, nullptr, nullptr); @@ -217,8 +216,8 @@ napi_value JsKeyboardDelegateSetting::Subscribe(napi_env env, napi_callback_info void *data = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); PARAM_CHECK_RETURN(env, argc >= ARGC_TWO, "Wrong number of arguments, requires 2", TYPE_NONE, nullptr); - std::string type = ""; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + std::string type; + JsUtil::GetValue(env, argv[ARGC_ZERO], type); IMSA_HILOGE("event type is: %{public}s", type.c_str()); napi_valuetype valueType = napi_undefined; @@ -232,10 +231,7 @@ napi_value JsKeyboardDelegateSetting::Subscribe(napi_env env, napi_callback_info std::shared_ptr callback = std::make_shared(env, argv[1], std::this_thread::get_id()); engine->RegisterListener(argv[ARGC_ONE], type, callback); - - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_value JsKeyboardDelegateSetting::UnSubscribe(napi_env env, napi_callback_info info) @@ -246,40 +242,29 @@ napi_value JsKeyboardDelegateSetting::UnSubscribe(napi_env env, napi_callback_in void *data = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, nullptr); - - std::string type = ""; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); - auto delegate = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (delegate == nullptr) { return nullptr; } - if (argc == ARGC_TWO) { napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[ARGC_ONE], &valueType); PARAM_CHECK_RETURN(env, valueType == napi_function, "callback", TYPE_FUNCTION, nullptr); } + + std::string type; + JsUtil::GetValue(env, argv[ARGC_ZERO], type); delegate->UnRegisterListener(argv[ARGC_ONE], type); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t keyCode, int32_t keyStatus) { - napi_value KeyboardDelegate = nullptr; - napi_create_object(env, &KeyboardDelegate); - - napi_value jsKeyCode = nullptr; - napi_create_int32(env, keyCode, &jsKeyCode); - napi_set_named_property(env, KeyboardDelegate, "keyCode", jsKeyCode); - - napi_value jsKeyAction = nullptr; - napi_create_int32(env, keyStatus, &jsKeyAction); - napi_set_named_property(env, KeyboardDelegate, "keyAction", jsKeyAction); - - return KeyboardDelegate; + napi_value object = nullptr; + napi_create_object(env, &object); + JsUtil::Object::WriteProperty(env, object, "keyCode", keyCode); + JsUtil::Object::WriteProperty(env, object, "keyAction", keyStatus); + return object; } bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus) diff --git a/frameworks/js/napi/inputmethodability/js_panel.cpp b/frameworks/js/napi/inputmethodability/js_panel.cpp index 84d5eee3bb7154a7e149f7abccf9e58584eede01..c55fa3c384b66bebc210613972bed6489c1c2129 100644 --- a/frameworks/js/napi/inputmethodability/js_panel.cpp +++ b/frameworks/js/napi/inputmethodability/js_panel.cpp @@ -92,8 +92,8 @@ napi_value JsPanel::SetUiContent(napi_env env, napi_callback_info info) napi_status status = napi_generic_failure; PARAM_CHECK_RETURN(env, argc >= 1, "should 1 or 2 parameters!", TYPE_NONE, status); // 0 means the first param path - status = JsUtils::GetValue(env, argv[0], ctxt->path); - NAPI_ASSERT_BASE(env, status == napi_ok, "get path failed!", status); + bool ret = JsUtil::GetValue(env, argv[0], ctxt->path); + NAPI_ASSERT_BASE(env, ret, "get path failed!", status); // if type of argv[1] is object, we will get value of 'storage' from it. if (argc >= 2) { napi_valuetype valueType = napi_undefined; @@ -136,11 +136,11 @@ napi_value JsPanel::Resize(napi_env env, napi_callback_info info) napi_status status = napi_generic_failure; PARAM_CHECK_RETURN(env, argc > 1, "should 2 or 3 parameters!", TYPE_NONE, status); // 0 means the first param width - status = JsUtils::GetValue(env, argv[0], ctxt->width); - NAPI_ASSERT_BASE(env, status == napi_ok, "get width failed!", status); + bool ret = JsUtil::GetValue(env, argv[ARGC_ZERO], ctxt->width); + NAPI_ASSERT_BASE(env, ret, "get width failed!", status); // 1 means the second param height - status = JsUtils::GetValue(env, argv[1], ctxt->height); - NAPI_ASSERT_BASE(env, status == napi_ok, "get height failed!", status); + status = JsUtil::GetValue(env, argv[1], ctxt->height); + NAPI_ASSERT_BASE(env, ret, "get height failed!", status); return napi_ok; }; @@ -164,14 +164,13 @@ napi_value JsPanel::MoveTo(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(env, info); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { - napi_status status = napi_generic_failure; - PARAM_CHECK_RETURN(env, argc > 1, " should 2 or 3 parameters! ", TYPE_NONE, status); + PARAM_CHECK_RETURN(env, argc > 1, " should 2 or 3 parameters! ", TYPE_NONE, napi_generic_failure); // 0 means the first param x - status = JsUtils::GetValue(env, argv[0], ctxt->x); - NAPI_ASSERT_BASE(env, status == napi_ok, "get x failed!", status); + bool status = JsUtil::GetValue(env, argv[0], ctxt->x); + NAPI_ASSERT_BASE(env, status, "get x failed!", napi_generic_failure); // 1 means the second param y - status = JsUtils::GetValue(env, argv[1], ctxt->y); - NAPI_ASSERT_BASE(env, status == napi_ok, "get y failed!", status); + status = JsUtil::GetValue(env, argv[1], ctxt->y); + NAPI_ASSERT_BASE(env, status, "get y failed!", napi_generic_failure); return napi_ok; }; @@ -236,8 +235,8 @@ napi_value JsPanel::ChangeFlag(napi_env env, napi_callback_info info) PARAM_CHECK_RETURN(env, argc > 0, " should 1 parameter! ", TYPE_NONE, nullptr); int32_t panelFlag = 0; // 0 means the first param flag - napi_status status = JsUtils::GetValue(env, argv[0], panelFlag); - NAPI_ASSERT(env, status == napi_ok, "get panelFlag failed!"); + bool status = JsUtil::GetValue(env, argv[0], panelFlag); + NAPI_ASSERT(env, status, "get panelFlag failed!"); auto inputMethodPanel = UnwrapPanel(env, thisVar); auto ret = inputMethodPanel->ChangePanelFlag(PanelFlag(panelFlag)); NAPI_ASSERT(env, ret == ErrorCode::NO_ERROR, "ChangePanelFlag failed!"); @@ -253,9 +252,9 @@ napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); // 2 means it has two params. NAPI_ASSERT(env, (argc >= 2) && (argc <= ARGC_MAX), "err number of argument!"); - std::string type = ""; + std::string type; // 0 means the first param type - JsUtils::GetValue(env, argv[0], type); + JsUtil::GetValue(env, argv[0], type); IMSA_HILOGD("on event type is: %{public}s", type.c_str()); napi_valuetype valuetype = napi_undefined; @@ -266,9 +265,7 @@ napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info) // 1 means the second param callback. observer->SaveInfo(env, type, argv[1], inputMethodPanel->windowId_); inputMethodPanel->SetPanelStatusListener(observer); - napi_value result = nullptr; - napi_get_undefined(env, &result); - return result; + return JsUtil::Const::Undefined(env); } napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info) @@ -278,17 +275,15 @@ napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments, requires 1 or 2"); - std::string type = ""; + std::string type; // 0 means the first param type - JsUtils::GetValue(env, argv[0], type); + JsUtil::GetValue(env, argv[0], type); IMSA_HILOGI("event type is: %{public}s", type.c_str()); std::shared_ptr observer = PanelListenerImpl::GetInstance(); auto inputMethodPanel = UnwrapPanel(env, thisVar); observer->RemoveInfo(type, inputMethodPanel->windowId_); inputMethodPanel->RemovePanelListener(type); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } std::shared_ptr JsPanel::UnwrapPanel(napi_env env, napi_value thisVar) diff --git a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp index 806d101bfb4bdbc6af07d1023d7bb590586375b6..fbcdaa18056a73d24c0c55bf90d9bbf00134628b 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -20,6 +20,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -57,7 +58,7 @@ napi_value JsTextInputClientEngine::MoveCursor(napi_env env, napi_callback_info auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, " should 1 or 2 parameters! ", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->num); + return JsUtil::GetValue(env, argv[0], ctxt->num) ? napi_ok : napi_generic_failure; }; auto exec = [ctxt](AsyncCall::Context *ctx) { int32_t code = InputMethodAbility::GetInstance()->MoveCursor(ctxt->num); @@ -82,9 +83,7 @@ napi_value JsTextInputClientEngine::JsConstructor(napi_env env, napi_callback_in JsTextInputClientEngine *clientObject = new (std::nothrow) JsTextInputClientEngine(); if (clientObject == nullptr) { IMSA_HILOGE("clientObject is nullptr"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } auto finalize = [](napi_env env, void *data, void *hint) { IMSA_HILOGE("JsTextInputClientEngine finalize"); @@ -125,52 +124,30 @@ napi_value JsTextInputClientEngine::GetResult(napi_env env, std::string &text) } napi_value JsTextInputClientEngine::GetResultEditorAttribute( - napi_env env, std::shared_ptr getEditorAttribute) + napi_env env, const std::shared_ptr &getEditorAttribute) { - napi_value editorAttribute = nullptr; - napi_create_object(env, &editorAttribute); - - napi_value jsValue = nullptr; - napi_create_int32(env, getEditorAttribute->enterKeyType, &jsValue); - napi_set_named_property(env, editorAttribute, "enterKeyType", jsValue); - - napi_value jsMethodId = nullptr; - napi_create_int32(env, getEditorAttribute->inputPattern, &jsMethodId); - napi_set_named_property(env, editorAttribute, "inputPattern", jsMethodId); - - return editorAttribute; + napi_value object = nullptr; + napi_create_object(env, &object); + JsUtil::Object::WriteProperty(env, object, "enterKeyType", getEditorAttribute->enterKeyType); + JsUtil::Object::WriteProperty(env, object, "inputPattern", getEditorAttribute->inputPattern); + return object; } napi_status JsTextInputClientEngine::GetSelectRange(napi_env env, napi_value argv, std::shared_ptr ctxt) { - napi_status status = napi_generic_failure; - napi_value napiValue = nullptr; - status = napi_get_named_property(env, argv, "start", &napiValue); - PARAM_CHECK_RETURN(env, status == napi_ok, "missing start parameter.", TYPE_NONE, status); - status = JsUtils::GetValue(env, napiValue, ctxt->start); - NAPI_ASSERT_BASE(env, status == napi_ok, "failed to get start value", status); - - status = napi_get_named_property(env, argv, "end", &napiValue); - PARAM_CHECK_RETURN(env, status == napi_ok, "missing end parameter.", TYPE_NONE, status); - status = JsUtils::GetValue(env, napiValue, ctxt->end); - if (status != napi_ok) { - IMSA_HILOGE("failed to get end value"); - } - return status; + bool ret = JsUtil::Object::ReadProperty(env, argv, "start", ctxt->start); + PARAM_CHECK_RETURN(env, ret, "missing start parameter.", TYPE_NONE, napi_generic_failure); + ret = JsUtil::Object::ReadProperty(env, argv, "end", ctxt->end); + PARAM_CHECK_RETURN(env, ret, "missing start parameter.", TYPE_NONE, napi_generic_failure); + return napi_ok; } napi_status JsTextInputClientEngine::GetSelectMovement( napi_env env, napi_value argv, std::shared_ptr ctxt) { - napi_status status = napi_generic_failure; - napi_value napiValue = nullptr; - status = napi_get_named_property(env, argv, "direction", &napiValue); - PARAM_CHECK_RETURN(env, status == napi_ok, "missing direction parameter.", TYPE_NONE, status); - status = JsUtils::GetValue(env, napiValue, ctxt->direction); - if (status != napi_ok) { - IMSA_HILOGE("failed to get direction value"); - } - return status; + bool ret = JsUtil::Object::ReadProperty(env, argv, "direction", ctxt->direction); + PARAM_CHECK_RETURN(env, ret, "missing direction parameter.", TYPE_NONE, napi_generic_failure); + return ret ? napi_ok : napi_generic_failure; } napi_value JsTextInputClientEngine::SendKeyFunction(napi_env env, napi_callback_info info) @@ -178,7 +155,7 @@ napi_value JsTextInputClientEngine::SendKeyFunction(napi_env env, napi_callback_ auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->action); + return JsUtil::GetValue(env, argv[0], ctxt->action) ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_status status = napi_get_boolean(env, ctxt->isSendKeyFunction, result); @@ -205,7 +182,7 @@ napi_value JsTextInputClientEngine::DeleteForward(napi_env env, napi_callback_in auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->length); + return JsUtil::GetValue(env, argv[0], ctxt->length) ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_status status = napi_get_boolean(env, ctxt->isDeleteForward, result); @@ -232,7 +209,7 @@ napi_value JsTextInputClientEngine::DeleteBackward(napi_env env, napi_callback_i auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->length); + return JsUtil::GetValue(env, argv[0], ctxt->length) ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_status status = napi_get_boolean(env, ctxt->isDeleteBackward, result); @@ -259,7 +236,7 @@ napi_value JsTextInputClientEngine::InsertText(napi_env env, napi_callback_info auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->text); + return JsUtil::GetValue(env, argv[0], ctxt->text) ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_status status = napi_get_boolean(env, ctxt->isInsertText, result); @@ -286,7 +263,7 @@ napi_value JsTextInputClientEngine::GetForward(napi_env env, napi_callback_info auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->length); + return JsUtil::GetValue(env, argv[0], ctxt->length) ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_value data = GetResult(env, ctxt->text); @@ -315,7 +292,7 @@ napi_value JsTextInputClientEngine::GetBackward(napi_env env, napi_callback_info auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should 1 or 2 parameters!", TYPE_NONE, napi_generic_failure); - return JsUtils::GetValue(env, argv[0], ctxt->length); + return JsUtil::GetValue(env, argv[0], ctxt->length) ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_value data = GetResult(env, ctxt->text); diff --git a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h index bd385d2da6b660fe5654cf92aea363e92eb6a901..20b9ca28db7d1a2962e1ee208073f395900e51cd 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h @@ -280,7 +280,7 @@ private: static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); static napi_value GetResult(napi_env env, std::string &text); static napi_value GetResultEditorAttribute( - napi_env env, std::shared_ptr getEditorAttribute); + napi_env env, const std::shared_ptr &getEditorAttribute); static const std::string TIC_CLASS_NAME; static thread_local napi_ref TICRef_; diff --git a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp index 2ca4fbc6c7af1b39518719093b269b3209577944..bc2d9a722b6e39c000ebb57f15d3431e1abb0933 100644 --- a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp +++ b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp @@ -16,6 +16,7 @@ #include "panel_listener_impl.h" #include "js_utils.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -106,8 +107,7 @@ void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow) delete work; }); CHECK_RETURN_VOID(entry != nullptr, "OnInputStart:: entry is null."); - napi_handle_scope scope = nullptr; - napi_open_handle_scope(entry->cbCopy->env_, &scope); + JsUtil::ScopeGuard scope(entry->cbCopy->env); napi_get_reference_value(entry->cbCopy->env_, entry->cbCopy->callback_, &callback); if (callback != nullptr) { napi_value global = nullptr; @@ -118,7 +118,6 @@ void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow) IMSA_HILOGE("notify data change failed callStatus:%{public}d", callStatus); } } - napi_close_handle_scope(entry->cbCopy->env_, scope); }); } } // namespace MiscServices diff --git a/frameworks/js/napi/inputmethodclient/BUILD.gn b/frameworks/js/napi/inputmethodclient/BUILD.gn index 3e23ae68ea9a403946cc9964c34550cacf0d28b9..480fb3904ab6b8aa264c261aba25ece9201dbb77 100644 --- a/frameworks/js/napi/inputmethodclient/BUILD.gn +++ b/frameworks/js/napi/inputmethodclient/BUILD.gn @@ -48,7 +48,10 @@ ohos_shared_library("inputmethod") { configs = [ ":imf_config" ] - deps = [ "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client" ] + deps = [ + "${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common", + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client", + ] external_deps = [ "ability_runtime:abilitykit_native", 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 61f230f16d68e66a5d136bce1862e15c97a6a977..5909c5c6440796006f089ac2eff16a40feae893e 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -21,6 +21,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -86,9 +87,7 @@ napi_value JsGetInputMethodController::JsConstructor(napi_env env, napi_callback auto controllerObject = GetInstance(); if (controllerObject == nullptr) { IMSA_HILOGE("controllerObject is nullptr"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_status status = napi_wrap( env, thisVar, controllerObject.get(), [](napi_env env, void *data, void *hint) {}, nullptr, nullptr); @@ -191,9 +190,9 @@ napi_value JsGetInputMethodController::Subscribe(napi_env env, napi_callback_inf NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); PARAM_CHECK_RETURN(env, argc > 1, "should 2 parameters!", TYPE_NONE, nullptr); - std::string type = ""; - napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], type); - PARAM_CHECK_RETURN(env, status == napi_ok, "callback", TYPE_FUNCTION, nullptr); + std::string type; + bool status = JsUtil::GetValue(env, argv[ARGC_ZERO], type); + PARAM_CHECK_RETURN(env, status, "callback", TYPE_FUNCTION, nullptr); if (TEXT_EVENT_TYPE.find(type) != TEXT_EVENT_TYPE.end()) { if (!InputMethodController::GetInstance()->WasAttached()) { @@ -209,10 +208,7 @@ napi_value JsGetInputMethodController::Subscribe(napi_env env, napi_callback_inf std::shared_ptr callback = std::make_shared(env, argv[ARGC_ONE], std::this_thread::get_id()); engine->RegisterListener(argv[ARGC_ONE], type, callback); - - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_info info) @@ -227,48 +223,33 @@ napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_i } std::string type; - napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], type); - if ((status != napi_ok) || (EVENT_TYPE.find(type) == EVENT_TYPE.end() && - TEXT_EVENT_TYPE.find(type) == TEXT_EVENT_TYPE.end())) { + auto ret = JsUtil::GetValue(env, argv[0], type); + if (!ret || (EVENT_TYPE.find(type) == EVENT_TYPE.end() && TEXT_EVENT_TYPE.find(type) == TEXT_EVENT_TYPE.end())) { return nullptr; } - auto engine = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (engine == nullptr) { return nullptr; } engine->UnRegisterListener(type); - - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_value JsGetInputMethodController::CreateSelectRange(napi_env env, int32_t start, int32_t end) { - napi_value range = nullptr; - napi_create_object(env, &range); - - napi_value value = nullptr; - napi_create_int32(env, start, &value); - napi_set_named_property(env, range, "start", value); - - napi_create_int32(env, end, &value); - napi_set_named_property(env, range, "end", value); - - return range; + napi_value object = nullptr; + napi_create_object(env, &object); + JsUtil::Object::WriteProperty(env, object, "start", start); + JsUtil::Object::WriteProperty(env, object, "end", end); + return object; } napi_value JsGetInputMethodController::CreateSelectMovement(napi_env env, int32_t direction) { - napi_value movement = nullptr; - napi_create_object(env, &movement); - - napi_value value = nullptr; - napi_create_int32(env, direction, &value); - napi_set_named_property(env, movement, "direction", value); - - return movement; + napi_value object = nullptr; + napi_create_object(env, &object); + JsUtil::Object::WriteProperty(env, object, "direction", direction); + return object; } napi_value JsGetInputMethodController::HandleSoftKeyboard( 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 4594ff373a1a176018f0e1fd7fcd635084accaaf..672df01b8dbd140693591befd5fcbab66a09bb55 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -23,6 +23,9 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" +#include "js_property.h" +#include "js_subtype.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -78,9 +81,7 @@ napi_value JsGetInputMethodSetting::JsConstructor(napi_env env, napi_callback_in auto delegate = GetInputMethodSettingInstance(); if (delegate == nullptr) { IMSA_HILOGE("settingObject is nullptr"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } napi_status status = napi_wrap( env, thisVar, delegate.get(), [](napi_env env, void *data, void *hint) {}, nullptr, nullptr); @@ -148,35 +149,6 @@ napi_value JsGetInputMethodSetting::GetIMSetting(napi_env env, napi_callback_inf return instance; } -napi_status JsGetInputMethodSetting::GetInputMethodProperty( - napi_env env, napi_value argv, std::shared_ptr ctxt) -{ - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv, &valueType); - PARAM_CHECK_RETURN(env, valueType == napi_object, "Parameter error.", TYPE_OBJECT, napi_invalid_arg); - napi_value result = nullptr; - napi_get_named_property(env, argv, "name", &result); - JsUtils::GetValue(env, result, ctxt->property.name); - - result = nullptr; - napi_get_named_property(env, argv, "id", &result); - JsUtils::GetValue(env, result, ctxt->property.id); - - if (ctxt->property.name.empty() || ctxt->property.id.empty()) { - result = nullptr; - napi_get_named_property(env, argv, "packageName", &result); - JsUtils::GetValue(env, result, ctxt->property.name); - - result = nullptr; - napi_get_named_property(env, argv, "methodId", &result); - JsUtils::GetValue(env, result, ctxt->property.id); - } - PARAM_CHECK_RETURN(env, (!ctxt->property.name.empty() && !ctxt->property.id.empty()), "Parameter error.", - TYPE_NONE, napi_invalid_arg); - IMSA_HILOGD("methodId:%{public}s, packageName:%{public}s", ctxt->property.id.c_str(), ctxt->property.name.c_str()); - return napi_ok; -} - napi_value JsGetInputMethodSetting::ListInputMethod(napi_env env, napi_callback_info info) { IMSA_HILOGI("run in ListInputMethod"); @@ -212,8 +184,8 @@ napi_value JsGetInputMethodSetting::GetInputMethods(napi_env env, napi_callback_ auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 0, "should has one parameter.", TYPE_NONE, napi_invalid_arg); bool enable = false; - napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], enable); - PARAM_CHECK_RETURN(env, status == napi_ok, "enable.", TYPE_NUMBER, napi_invalid_arg); + bool ret = JsUtil::GetValue(env, argv[ARGC_ZERO], enable); + PARAM_CHECK_RETURN(env, ret, "enable.", TYPE_NUMBER, napi_invalid_arg); ctxt->inputMethodStatus = enable ? InputMethodStatus::ENABLE : InputMethodStatus::DISABLE; return napi_ok; }; @@ -297,8 +269,10 @@ napi_value JsGetInputMethodSetting::ListInputMethodSubtype(napi_env env, napi_ca napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); PARAM_CHECK_RETURN(env, valueType == napi_object, "inputMethodProperty", TYPE_OBJECT, napi_invalid_arg); - napi_status status = JsGetInputMethodSetting::GetInputMethodProperty(env, argv[0], ctxt); - return status; + bool ret = JsProperty::Read(env, argv[0], ctxt->property); + PARAM_CHECK_RETURN(env, (!ctxt->property.name.empty() && !ctxt->property.id.empty()), "Parameter error.", + TYPE_NONE, napi_invalid_arg); + return ret ? napi_ok : napi_generic_failure; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { *result = JsInputMethod::GetJSInputMethodSubProperties(env, ctxt->subProperties); @@ -395,7 +369,7 @@ napi_value JsGetInputMethodSetting::Subscribe(napi_env env, napi_callback_info i NAPI_ASSERT(env, argc > ARGC_ONE, "Wrong number of arguments, requires least 2"); std::string type; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + JsUtil::GetValue(env, argv[ARGC_ZERO], type); NAPI_ASSERT(env, EVENT_TYPE.find(type) != EVENT_TYPE.end(), "subscribe type error"); napi_valuetype valuetype = napi_undefined; @@ -413,9 +387,7 @@ napi_value JsGetInputMethodSetting::Subscribe(napi_env env, napi_callback_info i if (errCode == EXCEPTION_PERMISSION) { JsUtils::ThrowException(env, errCode, "", TYPE_NONE); } - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } void JsGetInputMethodSetting::UnRegisterListener(napi_value callback, std::string type) @@ -458,7 +430,7 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info NAPI_ASSERT(env, argc > ARGC_ZERO, "Wrong number of arguments, requires least 1"); std::string type; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + JsUtil::GetValue(env, argv[ARGC_ZERO], type); NAPI_ASSERT(env, EVENT_TYPE.find(type) != EVENT_TYPE.end(), "subscribe type error"); auto engine = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (engine == nullptr) { @@ -471,9 +443,7 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info NAPI_ASSERT(env, valuetype == napi_function, "callback is not a function"); } engine->UnRegisterListener(argv[ARGC_ONE], type); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty) @@ -504,8 +474,8 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro if (argc < 2) { return false; } - napi_value subProperty = JsInputMethod::GetJsInputMethodSubProperty(item->env_, entry->subProperty); - napi_value property = JsInputMethod::GetJsInputMethodProperty(item->env_, entry->property); + napi_value property = JsProperty::Write(item->env_, entry->property); + napi_value subProperty = JsSubtype::Write(item->env_, entry->subProperty); if (subProperty == nullptr || property == nullptr) { IMSA_HILOGE("get KBCins or TICins failed:"); return false; @@ -566,7 +536,6 @@ uv_work_t *JsGetInputMethodSetting::GetUVwork(const std::string &type, EntrySett 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; 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 c3329c8c22964c8b05776c3703867837f5e43ed7..575278e0d424f9d8b895ce1551a1e3f96fda991e 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -113,7 +113,6 @@ public: void OnPanelStatusChange(const InputWindowStatus &status, const std::vector &windowInfo) override; private: - static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr ctxt); static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); static napi_value GetJsConstProperty(napi_env env, uint32_t num); static napi_value GetIMSetting(napi_env env, napi_callback_info info, bool needThrowException); diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.cpp b/frameworks/js/napi/inputmethodclient/js_input_method.cpp index f298be13d3838517ecddcf2e9fefe277fcf3d0db..86b27a547699e667a11d508378bd9503a92f6568 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.cpp +++ b/frameworks/js/napi/inputmethodclient/js_input_method.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include "js_input_method.h" #include "event_handler.h" @@ -22,6 +23,9 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" +#include "js_property.h" +#include "js_subtype.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -40,170 +44,43 @@ napi_value JsInputMethod::Init(napi_env env, napi_value exports) }; napi_status JsInputMethod::GetInputMethodProperty( - napi_env env, napi_value argv, std::shared_ptr ctxt) + napi_env env, napi_value argv, const std::shared_ptr &ctxt) { - napi_valuetype valueType = napi_undefined; - napi_status status = napi_generic_failure; - napi_typeof(env, argv, &valueType); - if (valueType != napi_object) { - IMSA_HILOGE("valueType error"); - return status; - } - napi_value result = nullptr; - napi_get_named_property(env, argv, "name", &result); - status = JsUtils::GetValue(env, result, ctxt->packageName); - NAPI_ASSERT_BASE(env, status == napi_ok, "get ctxt->packageName failed!", status); - result = nullptr; - napi_get_named_property(env, argv, "id", &result); - status = JsUtils::GetValue(env, result, ctxt->methodId); - NAPI_ASSERT_BASE(env, status == napi_ok, "get ctxt->methodId failed!", status); + JsUtil::Object::ReadProperty(env, argv, "name", ctxt->packageName); + JsUtil::Object::ReadProperty(env, argv, "id", ctxt->methodId); if (ctxt->packageName.empty() || ctxt->methodId.empty()) { - result = nullptr; - napi_get_named_property(env, argv, "packageName", &result); - status = JsUtils::GetValue(env, result, ctxt->packageName); - NAPI_ASSERT_BASE(env, status == napi_ok, "get ctxt->packageName failed!", status); - - result = nullptr; - napi_get_named_property(env, argv, "methodId", &result); - status = JsUtils::GetValue(env, result, ctxt->methodId); - NAPI_ASSERT_BASE(env, status == napi_ok, "get ctxt->methodId failed!", status); + JsUtil::Object::ReadProperty(env, argv, "packageName", ctxt->packageName); + JsUtil::Object::ReadProperty(env, argv, "methodId", ctxt->methodId); } - PARAM_CHECK_RETURN(env, (!ctxt->packageName.empty() && !ctxt->methodId.empty()), "JsInputMethod, Parameter error.", - TYPE_NONE, napi_invalid_arg); IMSA_HILOGI("methodId:%{public}s and packageName:%{public}s", ctxt->methodId.c_str(), ctxt->packageName.c_str()); return napi_ok; } napi_status JsInputMethod::GetInputMethodSubProperty( - napi_env env, napi_value argv, std::shared_ptr ctxt) + napi_env env, napi_value argv, const std::shared_ptr &ctxt) { - napi_valuetype valueType = napi_undefined; - napi_status status = napi_generic_failure; - status = napi_typeof(env, argv, &valueType); - if (valueType == napi_object) { - napi_value result = nullptr; - status = napi_get_named_property(env, argv, "name", &result); - PARAM_CHECK_RETURN(env, status == napi_ok, " name ", TYPE_STRING, status); - status = JsUtils::GetValue(env, result, ctxt->name); - NAPI_ASSERT_BASE(env, status == napi_ok, "get ctxt->name failed!", status); - result = nullptr; - status = napi_get_named_property(env, argv, "id", &result); - PARAM_CHECK_RETURN(env, status == napi_ok, " id ", TYPE_STRING, status); - status = JsUtils::GetValue(env, result, ctxt->id); - NAPI_ASSERT_BASE(env, status == napi_ok, "get ctxt->id failed!", status); - IMSA_HILOGI("name:%{public}s and id:%{public}s", ctxt->name.c_str(), ctxt->id.c_str()); - } - return status; -} - -napi_value JsInputMethod::GetJsInputMethodProperty(napi_env env, const Property &property) -{ - napi_value prop = nullptr; - napi_create_object(env, &prop); - - napi_value packageName = nullptr; - napi_create_string_utf8(env, property.name.c_str(), NAPI_AUTO_LENGTH, &packageName); - napi_set_named_property(env, prop, "packageName", packageName); - napi_set_named_property(env, prop, "name", packageName); - - napi_value methodId = nullptr; - napi_create_string_utf8(env, property.id.c_str(), NAPI_AUTO_LENGTH, &methodId); - napi_set_named_property(env, prop, "methodId", methodId); - napi_set_named_property(env, prop, "id", methodId); - - napi_value icon = nullptr; - napi_create_string_utf8(env, property.icon.c_str(), NAPI_AUTO_LENGTH, &icon); - napi_set_named_property(env, prop, "icon", icon); - - napi_value iconId = nullptr; - napi_create_int32(env, property.iconId, &iconId); - napi_set_named_property(env, prop, "iconId", iconId); - - napi_value label = nullptr; - napi_create_string_utf8(env, property.label.c_str(), NAPI_AUTO_LENGTH, &label); - napi_set_named_property(env, prop, "label", label); - - napi_value labelId = nullptr; - napi_create_int32(env, property.labelId, &labelId); - napi_set_named_property(env, prop, "labelId", labelId); - return prop; -} - -napi_value JsInputMethod::GetJsInputMethodSubProperty(napi_env env, const SubProperty &subProperty) -{ - napi_value prop = nullptr; - napi_create_object(env, &prop); - - napi_value id = nullptr; - napi_create_string_utf8(env, subProperty.id.c_str(), NAPI_AUTO_LENGTH, &id); - napi_set_named_property(env, prop, "id", id); - - napi_value label = nullptr; - napi_create_string_utf8(env, subProperty.label.c_str(), NAPI_AUTO_LENGTH, &label); - napi_set_named_property(env, prop, "label", label); - - napi_value labelId = nullptr; - napi_create_int32(env, subProperty.labelId, &labelId); - napi_set_named_property(env, prop, "labelId", labelId); - - napi_value name = nullptr; - napi_create_string_utf8(env, subProperty.name.c_str(), NAPI_AUTO_LENGTH, &name); - napi_set_named_property(env, prop, "name", name); - - napi_value mode = nullptr; - napi_create_string_utf8(env, subProperty.mode.c_str(), NAPI_AUTO_LENGTH, &mode); - napi_set_named_property(env, prop, "mode", mode); - - napi_value locale = nullptr; - napi_create_string_utf8(env, subProperty.locale.c_str(), NAPI_AUTO_LENGTH, &locale); - napi_set_named_property(env, prop, "locale", locale); - - napi_value language = nullptr; - napi_create_string_utf8(env, subProperty.language.c_str(), NAPI_AUTO_LENGTH, &language); - napi_set_named_property(env, prop, "language", language); - - napi_value icon = nullptr; - napi_create_string_utf8(env, subProperty.icon.c_str(), NAPI_AUTO_LENGTH, &icon); - napi_set_named_property(env, prop, "icon", icon); - - napi_value iconId = nullptr; - napi_create_int32(env, subProperty.iconId, &iconId); - napi_set_named_property(env, prop, "iconId", iconId); - return prop; + JsUtil::Object::ReadProperty(env, argv, "name", ctxt->name); + JsUtil::Object::ReadProperty(env, argv, "id", ctxt->id); + PARAM_CHECK_RETURN( + env, !ctxt->name.empty() && !ctxt->id.empty(), " name or id ", TYPE_STRING, napi_generic_failure); + IMSA_HILOGI("name:%{public}s and id:%{public}s", ctxt->name.c_str(), ctxt->id.c_str()); + return napi_ok; } napi_value JsInputMethod::GetJSInputMethodSubProperties(napi_env env, const std::vector &subProperties) { - uint32_t index = 0; - napi_value prop = nullptr; - napi_create_array(env, &prop); - if (prop == nullptr) { - IMSA_HILOGE("create array failed"); - return prop; - } - for (const auto &subproperty : subProperties) { - napi_value pro = GetJsInputMethodSubProperty(env, subproperty); - napi_set_element(env, prop, index, pro); - index++; - } - return prop; + std::vector jsProperties(subProperties.size()); + std::for_each(subProperties.begin(), subProperties.end(), + [env, &jsProperties](const SubProperty &item) { jsProperties.emplace_back(JsSubtype::Write(env, item)); }); + return JsUtil::GetValue(env, jsProperties); } napi_value JsInputMethod::GetJSInputMethodProperties(napi_env env, const std::vector &properties) { - uint32_t index = 0; - napi_value prop = nullptr; - napi_create_array(env, &prop); - if (prop == nullptr) { - IMSA_HILOGE("create array failed"); - return prop; - } - for (const auto &property : properties) { - napi_value pro = GetJsInputMethodProperty(env, property); - napi_set_element(env, prop, index, pro); - index++; - } - return prop; + std::vector jsProperties(properties.size()); + std::for_each(properties.begin(), properties.end(), + [env, &jsProperties](const Property &item) { jsProperties.emplace_back(JsProperty::Write(env, item)); }); + return JsUtil::GetValue(env, jsProperties); } napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info info) @@ -215,6 +92,8 @@ napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info inf napi_typeof(env, argv[0], &valueType); PARAM_CHECK_RETURN(env, valueType == napi_object, " target: ", TYPE_OBJECT, napi_invalid_arg); napi_status status = GetInputMethodProperty(env, argv[0], ctxt); + PARAM_CHECK_RETURN(env, (!ctxt->packageName.empty() && !ctxt->methodId.empty()), + "JsInputMethod, Parameter error.", TYPE_NONE, napi_invalid_arg); return status; }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { @@ -247,25 +126,13 @@ napi_value JsInputMethod::SwitchInputMethod(napi_env env, napi_callback_info inf napi_value JsInputMethod::GetCurrentInputMethod(napi_env env, napi_callback_info info) { std::shared_ptr property = InputMethodController::GetInstance()->GetCurrentInputMethod(); - if (property == nullptr) { - IMSA_HILOGE("get current inputmethod is nullptr"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; - } - return GetJsInputMethodProperty(env, *property); + return JsProperty::Write(env, property); } napi_value JsInputMethod::GetCurrentInputMethodSubtype(napi_env env, napi_callback_info info) { std::shared_ptr subProperty = InputMethodController::GetInstance()->GetCurrentInputMethodSubtype(); - if (subProperty == nullptr) { - IMSA_HILOGE("get current inputmethodsubtype is nullptr"); - napi_value result = nullptr; - napi_get_null(env, &result); - return result; - } - return GetJsInputMethodSubProperty(env, *subProperty); + return JsSubtype::Write(env, subProperty); } napi_value JsInputMethod::SwitchCurrentInputMethodSubtype(napi_env env, napi_callback_info info) @@ -276,8 +143,7 @@ napi_value JsInputMethod::SwitchCurrentInputMethodSubtype(napi_env env, napi_cal napi_valuetype valueType = napi_undefined; napi_typeof(env, argv[0], &valueType); PARAM_CHECK_RETURN(env, valueType == napi_object, "inputMethodSubtype: ", TYPE_OBJECT, napi_object_expected); - napi_status status = GetInputMethodSubProperty(env, argv[0], ctxt); - return status; + return GetInputMethodSubProperty(env, argv[0], ctxt); }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_status status = napi_get_boolean(env, ctxt->isSwitchInput, result); @@ -316,8 +182,7 @@ napi_value JsInputMethod::SwitchCurrentInputMethodAndSubtype(napi_env env, napi_ PARAM_CHECK_RETURN(env, valueType == napi_object, "inputMethodProperty: ", TYPE_OBJECT, napi_object_expected); napi_typeof(env, argv[1], &valueType); PARAM_CHECK_RETURN(env, valueType == napi_object, "inputMethodSubtype: ", TYPE_OBJECT, napi_object_expected); - napi_status status = GetInputMethodSubProperty(env, argv[1], ctxt); - return status; + return GetInputMethodSubProperty(env, argv[1], ctxt); }; auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { napi_status status = napi_get_boolean(env, ctxt->isSwitchInput, result); diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.h b/frameworks/js/napi/inputmethodclient/js_input_method.h index ae633d3285b2ab5c6296d49248b72f2267d5398a..73614ef3e898d9d56850dfdfecfb6a1a077cf0ee 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.h +++ b/frameworks/js/napi/inputmethodclient/js_input_method.h @@ -58,16 +58,14 @@ public: static napi_value SwitchCurrentInputMethodAndSubtype(napi_env env, napi_callback_info info); static napi_value GetCurrentInputMethodSubtype(napi_env env, napi_callback_info info); static napi_value GetCurrentInputMethod(napi_env env, napi_callback_info info); - static napi_value GetJsInputMethodProperty(napi_env env, const Property &property); static napi_value GetJSInputMethodSubProperties(napi_env env, const std::vector &subProperties); static napi_value GetJSInputMethodProperties(napi_env env, const std::vector &properties); - static napi_value GetJsInputMethodSubProperty(napi_env env, const SubProperty &subProperty); private: static napi_status GetInputMethodProperty( - napi_env env, napi_value argv, std::shared_ptr ctxt); + napi_env env, napi_value argv, const std::shared_ptr &ctxt); static napi_status GetInputMethodSubProperty( - napi_env env, napi_value argv, std::shared_ptr ctxt); + napi_env env, napi_value argv, const std::shared_ptr &ctxt); static constexpr std::int32_t MAX_VALUE_LEN = 4096; static constexpr size_t PARAM_POS_TWO = 2; static constexpr size_t PARAM_POS_ONE = 1; diff --git a/frameworks/js/napi/inputmethodclient/js_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index 8fcb3281f2b1d92a6bc5aa02ecefcebe609cc751..fd4c93b4a4d4bbdf48492d65a3111e9a9ef314ec 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.cpp +++ b/frameworks/js/napi/inputmethodclient/js_utils.cpp @@ -14,6 +14,7 @@ */ #include "js_utils.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -142,15 +143,12 @@ bool JsUtils::TraverseCallback( bool isResult = false; bool isOnKeyEvent = false; for (const auto &item : vecCopy) { - napi_handle_scope scope = nullptr; - napi_open_handle_scope(item->env_, &scope); + JsUtil::ScopeGuard scope(item->env_); if (item->threadId_ != std::this_thread::get_id()) { - napi_close_handle_scope(item->env_, scope); continue; } napi_value args[MAX_ARGMENT_COUNT]; if (!argsProvider(args, MAX_ARGMENT_COUNT, item)) { - napi_close_handle_scope(item->env_, scope); continue; } napi_value callback = nullptr; @@ -172,12 +170,11 @@ bool JsUtils::TraverseCallback( if (valueType != napi_boolean) { continue; } - GetValue(item->env_, result, isResult); + JsUtil::GetValue(item->env_, result, isResult); if (isResult) { isOnKeyEvent = true; } } - napi_close_handle_scope(item->env_, scope); } return isOnKeyEvent; } @@ -216,149 +213,5 @@ void *JsUtils::GetNativeSelf(napi_env env, napi_callback_info info) NAPI_ASSERT(env, (status == napi_ok && native != nullptr), "napi_unwrap failed!"); return native; } - -napi_status JsUtils::GetValue(napi_env env, napi_value in, int32_t &out) -{ - napi_valuetype type = napi_undefined; - napi_status status = napi_typeof(env, in, &type); - NAPI_ASSERT_BASE(env, (status == napi_ok) && (type == napi_number), "invalid type", napi_generic_failure); - return napi_get_value_int32(env, in, &out); -} - -/* napi_value <-> uint32_t */ -napi_status JsUtils::GetValue(napi_env env, napi_value in, uint32_t &out) -{ - napi_valuetype type = napi_undefined; - napi_status status = napi_typeof(env, in, &type); - NAPI_ASSERT_BASE(env, (status == napi_ok) && (type == napi_number), "invalid type", napi_generic_failure); - return napi_get_value_uint32(env, in, &out); -} - -napi_status JsUtils::GetValue(napi_env env, napi_value in, bool &out) -{ - napi_valuetype type = napi_undefined; - napi_status status = napi_typeof(env, in, &type); - NAPI_ASSERT_BASE(env, (status == napi_ok) && (type == napi_boolean), "invalid type", napi_generic_failure); - return napi_get_value_bool(env, in, &out); -} - -napi_status JsUtils::GetValue(napi_env env, napi_value in, double &out) -{ - napi_valuetype type = napi_undefined; - napi_status status = napi_typeof(env, in, &type); - NAPI_ASSERT_BASE(env, (status == napi_ok) && (type == napi_number), "invalid double type", napi_generic_failure); - return napi_get_value_double(env, in, &out); -} - -/* napi_value <-> std::string */ -napi_status JsUtils::GetValue(napi_env env, napi_value in, std::string &out) -{ - IMSA_HILOGD("JsUtils get string value in."); - napi_valuetype type = napi_undefined; - napi_status status = napi_typeof(env, in, &type); - NAPI_ASSERT_BASE(env, (status == napi_ok) && (type == napi_string), "invalid type", napi_generic_failure); - - size_t maxLen = STR_MAX_LENGTH; - status = napi_get_value_string_utf8(env, in, NULL, 0, &maxLen); - if (maxLen <= 0) { - return status; - } - IMSA_HILOGD("napi_value -> std::string get length %{public}zu", maxLen); - char *buf = new (std::nothrow) char[maxLen + STR_TAIL_LENGTH]; - if (buf != nullptr) { - size_t len = 0; - status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len); - if (status == napi_ok) { - buf[len] = 0; - out = std::string(buf); - } - delete[] buf; - } else { - status = napi_generic_failure; - } - return status; -} - -napi_status JsUtils::GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out) -{ - napi_valuetype valueType = napi_undefined; - napi_status status = napi_typeof(env, in, &valueType); - if ((status == napi_ok) && (valueType == napi_object)) { - status = napi_get_named_property(env, in, type.c_str(), &out); - return status; - } - return napi_generic_failure; -} - -/* napi_value <-> PanelInfo */ -napi_status JsUtils::GetValue(napi_env env, napi_value in, PanelInfo &out) -{ - IMSA_HILOGD("napi_value -> PanelInfo "); - napi_value propType = nullptr; - napi_status status = napi_get_named_property(env, in, "type", &propType); - NAPI_ASSERT_BASE(env, (status == napi_ok), "no property type ", status); - int32_t panelType = 0; - status = GetValue(env, propType, panelType); - NAPI_ASSERT_BASE(env, (status == napi_ok), "no value of type ", status); - - // flag is optional. flag isn't need when panelType is status_bar. - int32_t panelFlag = 0; - if (panelType != PanelType::STATUS_BAR) { - napi_value propFlag = nullptr; - status = napi_get_named_property(env, in, "flag", &propFlag); - NAPI_ASSERT_BASE(env, (status == napi_ok), "no property flag ", status); - status = JsUtils::GetValue(env, propFlag, panelFlag); - NAPI_ASSERT_BASE(env, (status == napi_ok), "no value of flag ", status); - } - - out.panelType = PanelType(panelType); - out.panelFlag = PanelFlag(panelFlag); - return status; -} - -napi_value JsUtils::GetValue(napi_env env, const std::vector &in) -{ - napi_value array = nullptr; - uint32_t index = 0; - napi_create_array(env, &array); - if (array == nullptr) { - IMSA_HILOGE("create array failed"); - return array; - } - for (const auto &info : in) { - napi_value jsInfo = GetValue(env, info); - napi_set_element(env, array, index, jsInfo); - ++index; - } - return array; -} - -napi_value JsUtils::GetValue(napi_env env, const InputWindowInfo &in) -{ - napi_value info = nullptr; - napi_create_object(env, &info); - - napi_value name = nullptr; - napi_create_string_utf8(env, in.name.c_str(), in.name.size(), &name); - napi_set_named_property(env, info, "name", name); - - napi_value left = nullptr; - napi_create_int32(env, in.left, &left); - napi_set_named_property(env, info, "left", left); - - napi_value top = nullptr; - napi_create_int32(env, in.top, &top); - napi_set_named_property(env, info, "top", top); - - napi_value width = nullptr; - napi_create_uint32(env, in.width, &width); - napi_set_named_property(env, info, "width", width); - - napi_value height = nullptr; - napi_create_uint32(env, in.height, &height); - napi_set_named_property(env, info, "height", height); - - return info; -} } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/inputmethodclient/js_utils.h b/frameworks/js/napi/inputmethodclient/js_utils.h index 8bb0cef30948b622b1bfff6cac8e6452fe9da14b..1a0d3feb129364f9eaabdee6fa5c3983badddb77 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.h +++ b/frameworks/js/napi/inputmethodclient/js_utils.h @@ -93,16 +93,6 @@ public: static void *GetNativeSelf(napi_env env, napi_callback_info info); - static napi_status GetValue(napi_env env, napi_value in, int32_t &out); - static napi_status GetValue(napi_env env, napi_value in, uint32_t &out); - static napi_status GetValue(napi_env env, napi_value in, bool &out); - static napi_status GetValue(napi_env env, napi_value in, double &out); - static napi_status GetValue(napi_env env, napi_value in, std::string &out); - static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out); - static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out); - static napi_value GetValue(napi_env env, const std::vector &in); - static napi_value GetValue(napi_env env, const InputWindowInfo &in); - private: static const std::string ToMessage(int32_t code);