From a268cb04576cf0823fab4ab39ab1d699990d3027 Mon Sep 17 00:00:00 2001 From: illybyy Date: Sun, 16 Apr 2023 00:39:50 +0800 Subject: [PATCH 1/7] improve property Signed-off-by: illybyy --- .../js_get_input_method_setting.cpp | 52 ++----------- .../js_get_input_method_setting.h | 1 - .../inputmethodclient/js_input_method.cpp | 76 +++---------------- .../napi/inputmethodclient/js_input_method.h | 1 - .../js/napi/inputmethodclient/js_property.h | 49 ++++++++++++ 5 files changed, 64 insertions(+), 115 deletions(-) create mode 100644 frameworks/js/napi/inputmethodclient/js_property.h 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 dab0d344d..3cbc95ad7 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -22,6 +22,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" +#include "js_property.h" namespace OHOS { namespace MiscServices { @@ -145,49 +146,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_status status = napi_typeof(env, argv, &valueType); - if (valueType == napi_object) { - 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); - - result = nullptr; - napi_get_named_property(env, argv, "label", &result); - JsUtils::GetValue(env, result, ctxt->property.label); - - result = nullptr; - napi_get_named_property(env, argv, "icon", &result); - JsUtils::GetValue(env, result, ctxt->property.icon); - - result = nullptr; - napi_get_named_property(env, argv, "iconId", &result); - status = JsUtils::GetValue(env, result, ctxt->property.iconId); - IMSA_HILOGD( - "methodId:%{public}s, packageName:%{public}s", ctxt->property.id.c_str(), ctxt->property.name.c_str()); - } - return status; -} - napi_value JsGetInputMethodSetting::ListInputMethod(napi_env env, napi_callback_info info) { IMSA_HILOGI("run in ListInputMethod"); @@ -304,8 +262,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); @@ -488,7 +448,7 @@ void JsGetInputMethodSetting::OnImeChange(const Property &property, const SubPro 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); if (subProperty == nullptr || property == nullptr) { IMSA_HILOGE("get KBCins or TICins failed:"); return false; 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 d5e2e66c9..bf1bd439b 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -112,7 +112,6 @@ public: void OnImeChange(const Property &property, const SubProperty &subProperty) 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 39fc8cea9..ceb0a825f 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.cpp +++ b/frameworks/js/napi/inputmethodclient/js_input_method.cpp @@ -22,6 +22,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" +#include "js_property.h" namespace OHOS { namespace MiscServices { @@ -42,34 +43,12 @@ 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_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; } @@ -96,39 +75,6 @@ napi_status JsInputMethod::GetInputMethodSubProperty( 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; @@ -199,7 +145,7 @@ napi_value JsInputMethod::GetJSInputMethodProperties(napi_env env, const std::ve return prop; } for (const auto &property : properties) { - napi_value pro = GetJsInputMethodProperty(env, property); + napi_value pro = JsProperty::Write(env, property); napi_set_element(env, prop, index, pro); index++; } @@ -215,6 +161,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 { @@ -246,13 +194,7 @@ 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) diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.h b/frameworks/js/napi/inputmethodclient/js_input_method.h index ae633d328..d52504f6e 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.h +++ b/frameworks/js/napi/inputmethodclient/js_input_method.h @@ -58,7 +58,6 @@ 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); diff --git a/frameworks/js/napi/inputmethodclient/js_property.h b/frameworks/js/napi/inputmethodclient/js_property.h new file mode 100644 index 000000000..370e620fe --- /dev/null +++ b/frameworks/js/napi/inputmethodclient/js_property.h @@ -0,0 +1,49 @@ +// +// Created by miaoer on 2023/4/15. +// + +#ifndef INPUTMETHOD_IMF_JS_PROPERTY_H +#define INPUTMETHOD_IMF_JS_PROPERTY_H +#include + +#include "../common/js_util.h" +#include "input_method_property.h" +#include "js_native_api_types.h" +namespace OHOS { +namespace MiscServices { +struct JsProperty { + static napi_value Write(napi_env env, const std::shared_ptr &nativeObject) + { + if (nativeObject == nullptr) { + return JsUtil::Const::Null(env); + } + return Write(env, *nativeObject); + } + static napi_value 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); + } + static bool 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); + } + JsUtil::Object::ReadProperty(env, object, "icon", nativeObject.icon); + JsUtil::Object::ReadProperty(env, object, "iconId", nativeObject.iconId); + return ret; + } +}; +} // namespace MiscServices +} // namespace OHOS +#endif //INPUTMETHOD_IMF_JS_PROPERTY_H -- Gitee From c2499d10f634a79769edafc2db59be578908f6ff Mon Sep 17 00:00:00 2001 From: illybyy Date: Sun, 16 Apr 2023 22:31:29 +0800 Subject: [PATCH 2/7] improve subtype Signed-off-by: illybyy --- frameworks/js/napi/common/js_property.cpp | 50 +++++++ frameworks/js/napi/common/js_property.h | 31 +++++ frameworks/js/napi/common/js_subtype.cpp | 57 ++++++++ frameworks/js/napi/common/js_subtype.h | 30 +++++ frameworks/js/napi/common/js_util.cpp | 4 + frameworks/js/napi/common/js_util.h | 1 + .../js_input_method_engine_setting.cpp | 51 +------- .../js_input_method_engine_setting.h | 1 - .../js_get_input_method_setting.cpp | 5 +- .../inputmethodclient/js_input_method.cpp | 122 ++++-------------- .../napi/inputmethodclient/js_input_method.h | 5 +- .../js/napi/inputmethodclient/js_property.h | 49 ------- 12 files changed, 202 insertions(+), 204 deletions(-) create mode 100644 frameworks/js/napi/common/js_property.cpp create mode 100644 frameworks/js/napi/common/js_property.h create mode 100644 frameworks/js/napi/common/js_subtype.cpp create mode 100644 frameworks/js/napi/common/js_subtype.h delete mode 100644 frameworks/js/napi/inputmethodclient/js_property.h diff --git a/frameworks/js/napi/common/js_property.cpp b/frameworks/js/napi/common/js_property.cpp new file mode 100644 index 000000000..2df77c33b --- /dev/null +++ b/frameworks/js/napi/common/js_property.cpp @@ -0,0 +1,50 @@ +/* +* 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" +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); + } + 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 000000000..446a2ba0c --- /dev/null +++ b/frameworks/js/napi/common/js_property.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_PROPERTY_H +#define INPUTMETHOD_IMF_JS_PROPERTY_H +#include + +#include "js_util.h" +#include "input_method_property.h" +#include "js_native_api_types.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 000000000..24f60b39a --- /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" +#include "js_native_api.h" +namespace OHOS { +namespace MiscServices { +napi_value OHOS::MiscServices::JsSubtype::Write(napi_env env, const std::shared_ptr &nativeObject) +{ + if (nativeObject == nullptr) { + return JsUtil::Const::Null(env); + } + return Write(env, *nativeObject); +} +napi_value OHOS::MiscServices::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 OHOS::MiscServices::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); + 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 000000000..b7f1e838b --- /dev/null +++ b/frameworks/js/napi/common/js_subtype.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_SUBTYPE_H +#define INPUTMETHOD_IMF_JS_SUBTYPE_H +#include +#include "input_method_property.h" +#include "js_native_api_types.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 6b39792d7..2b4793ce8 100644 --- a/frameworks/js/napi/common/js_util.cpp +++ b/frameworks/js/napi/common/js_util.cpp @@ -77,4 +77,8 @@ napi_value JsUtil::GetValue(napi_env env, bool in) napi_get_boolean(env, in, &out); return out; } +napi_value JsUtil::GetValue(napi_env env, napi_value in) +{ + return in; +} } // namespace OHOS::MiscServices \ 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 bda99eba2..5e390d2be 100644 --- a/frameworks/js/napi/common/js_util.h +++ b/frameworks/js/napi/common/js_util.h @@ -52,6 +52,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; 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 e47165ae1..c2898bd23 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,7 @@ #include "js_text_input_client_engine.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "../common/js_subtype.h" namespace OHOS { namespace MiscServices { @@ -416,54 +417,6 @@ napi_value JsInputMethodEngineSetting::UnSubscribe(napi_env env, napi_callback_i 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; -} - void JsInputMethodEngineSetting::OnInputStart() { IMSA_HILOGD("run in"); @@ -625,7 +578,7 @@ 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"); return false; 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 e6da4aad2..41d7afc9e 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.h @@ -83,7 +83,6 @@ private: static napi_value GetIMEInstance(napi_env env, napi_callback_info info, int flag); void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); - static napi_value GetResultOnSetSubtype(napi_env env, const SubProperty &property); static napi_ref NewWithRef(napi_env env, size_t argc, napi_value *argv, void **out, napi_value constructor); static void GetNativeContext(napi_env env, NativeValue *nativeContext, void *&contextPtr); static const std::string IMES_CLASS_NAME; 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 3cbc95ad7..9f11c1f59 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -22,7 +22,8 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" -#include "js_property.h" +#include "../common/js_property.h" +#include "../common/js_subtype.h" namespace OHOS { namespace MiscServices { @@ -447,8 +448,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 = 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; diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.cpp b/frameworks/js/napi/inputmethodclient/js_input_method.cpp index ceb0a825f..c7bc1fa14 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,7 +23,8 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" -#include "js_property.h" +#include "../common/js_property.h" +#include "../common/js_subtype.h" namespace OHOS { namespace MiscServices { @@ -41,7 +43,7 @@ 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) { JsUtil::Object::ReadProperty(env, argv, "name", ctxt->packageName); JsUtil::Object::ReadProperty(env, argv, "id", ctxt->methodId); @@ -54,102 +56,30 @@ napi_status JsInputMethod::GetInputMethodProperty( } 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::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 = JsProperty::Write(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) @@ -200,13 +130,7 @@ napi_value JsInputMethod::GetCurrentInputMethod(napi_env env, napi_callback_info 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) @@ -217,8 +141,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); @@ -256,8 +179,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 d52504f6e..73614ef3e 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.h +++ b/frameworks/js/napi/inputmethodclient/js_input_method.h @@ -60,13 +60,12 @@ public: static napi_value GetCurrentInputMethod(napi_env env, napi_callback_info info); 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_property.h b/frameworks/js/napi/inputmethodclient/js_property.h deleted file mode 100644 index 370e620fe..000000000 --- a/frameworks/js/napi/inputmethodclient/js_property.h +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by miaoer on 2023/4/15. -// - -#ifndef INPUTMETHOD_IMF_JS_PROPERTY_H -#define INPUTMETHOD_IMF_JS_PROPERTY_H -#include - -#include "../common/js_util.h" -#include "input_method_property.h" -#include "js_native_api_types.h" -namespace OHOS { -namespace MiscServices { -struct JsProperty { - static napi_value Write(napi_env env, const std::shared_ptr &nativeObject) - { - if (nativeObject == nullptr) { - return JsUtil::Const::Null(env); - } - return Write(env, *nativeObject); - } - static napi_value 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); - } - static bool 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); - } - JsUtil::Object::ReadProperty(env, object, "icon", nativeObject.icon); - JsUtil::Object::ReadProperty(env, object, "iconId", nativeObject.iconId); - return ret; - } -}; -} // namespace MiscServices -} // namespace OHOS -#endif //INPUTMETHOD_IMF_JS_PROPERTY_H -- Gitee From d25e58864f77a92ea93e60008840680dd002424f Mon Sep 17 00:00:00 2001 From: illybyy Date: Wed, 19 Apr 2023 14:26:23 +0800 Subject: [PATCH 3/7] compile code Signed-off-by: illybyy --- frameworks/js/napi/common/BUILD.gn | 48 +++++++++++++++++++ frameworks/js/napi/common/js_property.cpp | 2 + frameworks/js/napi/common/js_property.h | 3 +- frameworks/js/napi/common/js_subtype.cpp | 1 - frameworks/js/napi/common/js_subtype.h | 3 +- frameworks/js/napi/common/js_util.cpp | 6 ++- frameworks/js/napi/common/js_util.h | 13 +++-- .../js/napi/inputmethodability/BUILD.gn | 5 +- .../js_input_method_engine_setting.cpp | 2 +- frameworks/js/napi/inputmethodclient/BUILD.gn | 5 +- .../js_get_input_method_setting.cpp | 4 +- .../inputmethodclient/js_input_method.cpp | 5 +- 12 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 frameworks/js/napi/common/BUILD.gn diff --git a/frameworks/js/napi/common/BUILD.gn b/frameworks/js/napi/common/BUILD.gn new file mode 100644 index 000000000..54a2aaf81 --- /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 index 2df77c33b..2bcafbf09 100644 --- a/frameworks/js/napi/common/js_property.cpp +++ b/frameworks/js/napi/common/js_property.cpp @@ -13,6 +13,8 @@ * 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) diff --git a/frameworks/js/napi/common/js_property.h b/frameworks/js/napi/common/js_property.h index 446a2ba0c..53df62071 100644 --- a/frameworks/js/napi/common/js_property.h +++ b/frameworks/js/napi/common/js_property.h @@ -16,9 +16,8 @@ #define INPUTMETHOD_IMF_JS_PROPERTY_H #include -#include "js_util.h" #include "input_method_property.h" -#include "js_native_api_types.h" +#include "js_native_api.h" namespace OHOS { namespace MiscServices { struct JsProperty { diff --git a/frameworks/js/napi/common/js_subtype.cpp b/frameworks/js/napi/common/js_subtype.cpp index 24f60b39a..091837961 100644 --- a/frameworks/js/napi/common/js_subtype.cpp +++ b/frameworks/js/napi/common/js_subtype.cpp @@ -15,7 +15,6 @@ #include "js_subtype.h" #include "js_util.h" -#include "js_native_api.h" namespace OHOS { namespace MiscServices { napi_value OHOS::MiscServices::JsSubtype::Write(napi_env env, const std::shared_ptr &nativeObject) diff --git a/frameworks/js/napi/common/js_subtype.h b/frameworks/js/napi/common/js_subtype.h index b7f1e838b..0254a83bf 100644 --- a/frameworks/js/napi/common/js_subtype.h +++ b/frameworks/js/napi/common/js_subtype.h @@ -15,8 +15,9 @@ #ifndef INPUTMETHOD_IMF_JS_SUBTYPE_H #define INPUTMETHOD_IMF_JS_SUBTYPE_H #include + #include "input_method_property.h" -#include "js_native_api_types.h" +#include "js_native_api.h" namespace OHOS { namespace MiscServices { class JsSubtype { diff --git a/frameworks/js/napi/common/js_util.cpp b/frameworks/js/napi/common/js_util.cpp index 2b4793ce8..092a90b24 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) { @@ -81,4 +82,5 @@ napi_value JsUtil::GetValue(napi_env env, napi_value in) { return in; } -} // namespace OHOS::MiscServices \ No newline at end of file +} // 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 5e390d2be..7ce99ad7b 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 @@ -119,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 bd0b62dde..7a9f41f8b 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_runtime:abilitykit_native", 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 c2898bd23..0a0c72c2b 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -25,7 +25,7 @@ #include "js_text_input_client_engine.h" #include "napi/native_api.h" #include "napi/native_node_api.h" -#include "../common/js_subtype.h" +#include "js_subtype.h" namespace OHOS { namespace MiscServices { diff --git a/frameworks/js/napi/inputmethodclient/BUILD.gn b/frameworks/js/napi/inputmethodclient/BUILD.gn index 2b3723757..fadca2fcf 100644 --- a/frameworks/js/napi/inputmethodclient/BUILD.gn +++ b/frameworks/js/napi/inputmethodclient/BUILD.gn @@ -47,7 +47,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_setting.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 9f11c1f59..8ef32d94b 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -22,8 +22,8 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" -#include "../common/js_property.h" -#include "../common/js_subtype.h" +#include "js_property.h" +#include "js_subtype.h" namespace OHOS { namespace MiscServices { diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.cpp b/frameworks/js/napi/inputmethodclient/js_input_method.cpp index c7bc1fa14..38416e80e 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.cpp +++ b/frameworks/js/napi/inputmethodclient/js_input_method.cpp @@ -23,8 +23,9 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "string_ex.h" -#include "../common/js_property.h" -#include "../common/js_subtype.h" +#include "js_property.h" +#include "js_subtype.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { -- Gitee From 348d9cba2b4698eae80ffe681314ee070805c63a Mon Sep 17 00:00:00 2001 From: illybyy Date: Sun, 23 Apr 2023 17:23:01 +0800 Subject: [PATCH 4/7] add comment Signed-off-by: illybyy --- frameworks/js/napi/common/js_property.cpp | 1 + frameworks/js/napi/common/js_subtype.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/common/js_property.cpp b/frameworks/js/napi/common/js_property.cpp index 2bcafbf09..7a4063f7c 100644 --- a/frameworks/js/napi/common/js_property.cpp +++ b/frameworks/js/napi/common/js_property.cpp @@ -44,6 +44,7 @@ bool JsProperty::Read(napi_env env, napi_value object, Property &nativeObject) 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; diff --git a/frameworks/js/napi/common/js_subtype.cpp b/frameworks/js/napi/common/js_subtype.cpp index 091837961..1da8a1cb2 100644 --- a/frameworks/js/napi/common/js_subtype.cpp +++ b/frameworks/js/napi/common/js_subtype.cpp @@ -17,14 +17,14 @@ #include "js_util.h" namespace OHOS { namespace MiscServices { -napi_value OHOS::MiscServices::JsSubtype::Write(napi_env env, const std::shared_ptr &nativeObject) +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 OHOS::MiscServices::JsSubtype::Write(napi_env env, const SubProperty &nativeObject) +napi_value JsSubtype::Write(napi_env env, const SubProperty &nativeObject) { napi_value object = nullptr; napi_create_object(env, &object); @@ -39,10 +39,11 @@ napi_value OHOS::MiscServices::JsSubtype::Write(napi_env env, const SubProperty ret = ret && JsUtil::Object::WriteProperty(env, object, "language", nativeObject.language); return ret ? object : JsUtil::Const::Null(env); } -bool OHOS::MiscServices::JsSubtype::Read(napi_env env, napi_value object, SubProperty &nativeObject) +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); -- Gitee From 3f91c2299863e3dbadb42f6758c5c85d9e9145e0 Mon Sep 17 00:00:00 2001 From: illybyy Date: Sun, 23 Apr 2023 17:57:04 +0800 Subject: [PATCH 5/7] use RAII style scope manage Signed-off-by: illybyy --- .../js/napi/inputmethodability/panel_listener_impl.cpp | 5 ++--- frameworks/js/napi/inputmethodclient/js_utils.cpp | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp b/frameworks/js/napi/inputmethodability/panel_listener_impl.cpp index 2ca4fbc6c..bc2d9a722 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/js_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index dbaefa35f..e94fb9aa5 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; @@ -177,7 +175,6 @@ bool JsUtils::TraverseCallback( isOnKeyEvent = true; } } - napi_close_handle_scope(item->env_, scope); } return isOnKeyEvent; } -- Gitee From b0594de399c915be18189c679e2780e8338fec09 Mon Sep 17 00:00:00 2001 From: illybyy Date: Sun, 23 Apr 2023 18:23:43 +0800 Subject: [PATCH 6/7] change JsUtil method Signed-off-by: illybyy --- .../js_input_method_engine_setting.cpp | 13 ++--- .../js_keyboard_delegate_setting.cpp | 17 +++---- .../js/napi/inputmethodability/js_panel.cpp | 27 +++++------ .../js_text_input_client_engine.cpp | 48 +++++++------------ .../js_get_input_method_controller.cpp | 11 +++-- .../js_get_input_method_setting.cpp | 9 ++-- .../js/napi/inputmethodclient/js_utils.cpp | 29 ----------- .../js/napi/inputmethodclient/js_utils.h | 1 - 8 files changed, 54 insertions(+), 101 deletions(-) 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 ae727eb25..ae4c52ddb 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -27,6 +27,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "js_subtype.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -190,7 +191,7 @@ napi_value JsInputMethodEngineSetting::MoveCursor(napi_env env, napi_callback_in NAPI_ASSERT(env, argc == 1, "Wrong number of arguments, requires 1"); int32_t number = 0; - if (JsUtils::GetValue(env, argv[ARGC_ZERO], number) != napi_ok) { + if (!JsUtil::GetValue(env, argv[ARGC_ZERO], number)) { IMSA_HILOGE("Get number error"); } @@ -258,9 +259,9 @@ napi_value JsInputMethodEngineSetting::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 >= ARGC_TWO, "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; @@ -405,8 +406,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) { diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 53e64f761..f5d3d4101 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 { @@ -217,8 +218,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; @@ -246,24 +247,20 @@ 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) diff --git a/frameworks/js/napi/inputmethodability/js_panel.cpp b/frameworks/js/napi/inputmethodability/js_panel.cpp index 099b8ed8b..73d7fa67b 100644 --- a/frameworks/js/napi/inputmethodability/js_panel.cpp +++ b/frameworks/js/napi/inputmethodability/js_panel.cpp @@ -94,8 +94,8 @@ napi_value JsPanel::SetUiContent(napi_env env, napi_callback_info 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 1 or 2 parameters!", TYPE_NONE, status); - status = JsUtils::GetValue(env, argv[ARGC_ZERO], ctxt->path); - NAPI_ASSERT_BASE(env, status == napi_ok, "get path failed!", status); + bool ret = JsUtil::GetValue(env, argv[ARGC_ZERO], 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; @@ -162,12 +162,11 @@ 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); - status = JsUtils::GetValue(env, argv[ARGC_ZERO], ctxt->x); - NAPI_ASSERT_BASE(env, status == napi_ok, "get x failed!", status); - status = JsUtils::GetValue(env, argv[ARGC_ONE], ctxt->y); - NAPI_ASSERT_BASE(env, status == napi_ok, "get y failed!", status); + PARAM_CHECK_RETURN(env, argc > 1, " should 2 or 3 parameters! ", TYPE_NONE, napi_generic_failure); + bool status = JsUtil::GetValue(env, argv[ARGC_ZERO], ctxt->x); + NAPI_ASSERT_BASE(env, status, "get x failed!", napi_generic_failure); + status = JsUtil::GetValue(env, argv[ARGC_ONE], ctxt->y); + NAPI_ASSERT_BASE(env, status, "get y failed!", napi_generic_failure); return napi_ok; }; @@ -228,8 +227,8 @@ napi_value JsPanel::ChangeFlag(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); PARAM_CHECK_RETURN(env, argc > 0, " should 1 parameter! ", TYPE_NONE, nullptr); int32_t panelFlag = 0; - napi_status status = JsUtils::GetValue(env, argv[ARGC_ZERO], panelFlag); - NAPI_ASSERT(env, status == napi_ok, "get panelFlag failed!"); + bool status = JsUtil::GetValue(env, argv[ARGC_ZERO], 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!"); @@ -244,8 +243,8 @@ napi_value JsPanel::Subscribe(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 >= ARGC_ONE) && (argc <= ARGC_MAX), "err number of argument!"); - std::string type = ""; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + std::string type; + JsUtil::GetValue(env, argv[ARGC_ZERO], type); IMSA_HILOGD("on event type is: %{public}s", type.c_str()); napi_valuetype valuetype = napi_undefined; @@ -267,8 +266,8 @@ 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 = ""; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + std::string type; + JsUtil::GetValue(env, argv[ARGC_ZERO], type); IMSA_HILOGI("event type is: %{public}s", type.c_str()); std::shared_ptr observer = PanelListenerImpl::GetInstance(); auto inputMethodPanel = UnwrapPanel(env, 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 164166477..90227d844 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 { @@ -80,9 +81,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"); @@ -141,34 +140,19 @@ napi_value JsTextInputClientEngine::GetResultEditorAttribute( 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) @@ -176,7 +160,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); @@ -202,7 +186,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); @@ -228,7 +212,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); @@ -254,7 +238,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); @@ -280,7 +264,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); @@ -308,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/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 822250685..cbe192f5b 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.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 { @@ -173,9 +174,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); auto engine = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (engine == nullptr) { @@ -199,8 +200,8 @@ napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_i NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); PARAM_CHECK_RETURN(env, argc > 0, "should 1 parameters!", TYPE_NONE, nullptr); - std::string type = ""; - JsUtils::GetValue(env, argv[ARGC_ZERO], type); + std::string type; + JsUtil::GetValue(env, argv[ARGC_ZERO], type); PARAM_CHECK_RETURN(env, EVENT_TYPE.find(type) != EVENT_TYPE.end(), "unkown type", TYPE_NONE, nullptr); auto engine = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (engine == nullptr) { 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 8ef32d94b..46942c4fb 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -24,6 +24,7 @@ #include "string_ex.h" #include "js_property.h" #include "js_subtype.h" +#include "js_util.h" namespace OHOS { namespace MiscServices { @@ -344,8 +345,8 @@ napi_value JsGetInputMethodSetting::Subscribe(napi_env env, napi_callback_info i NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); NAPI_ASSERT(env, argc == ARGC_TWO, "Wrong number of arguments, requires 2"); - 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; @@ -401,8 +402,8 @@ napi_value JsGetInputMethodSetting::UnSubscribe(napi_env env, napi_callback_info NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); NAPI_ASSERT(env, argc == ARGC_ONE || argc == ARGC_TWO, "Wrong number of arguments, requires 1 or 2"); - 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()); auto engine = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (engine == nullptr) { diff --git a/frameworks/js/napi/inputmethodclient/js_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index e94fb9aa5..b131a7cf1 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.cpp +++ b/frameworks/js/napi/inputmethodclient/js_utils.cpp @@ -239,35 +239,6 @@ napi_status JsUtils::GetValue(napi_env env, napi_value in, bool &out) return napi_get_value_bool(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", status); - - 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_value <-> PanelInfo */ napi_status JsUtils::GetValue(napi_env env, napi_value in, PanelInfo &out) { diff --git a/frameworks/js/napi/inputmethodclient/js_utils.h b/frameworks/js/napi/inputmethodclient/js_utils.h index 6225a1291..eb6045981 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.h +++ b/frameworks/js/napi/inputmethodclient/js_utils.h @@ -94,7 +94,6 @@ public: 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, std::string &out); static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out); private: -- Gitee From 9b880f5c2ebba82b7014e7a5e6ef95272f71da2f Mon Sep 17 00:00:00 2001 From: illybyy Date: Sun, 23 Apr 2023 18:48:14 +0800 Subject: [PATCH 7/7] use JsUtil instead Signed-off-by: illybyy --- .../js_input_method_engine_setting.cpp | 38 +++++++------ .../js_input_method_engine_setting.h | 7 ++- .../js_keyboard_controller_engine.cpp | 5 +- .../js_keyboard_delegate_setting.cpp | 26 +++------ .../js/napi/inputmethodability/js_panel.cpp | 16 +++--- .../js_text_input_client_engine.cpp | 21 +++----- .../js_text_input_client_engine.h | 2 +- .../js_get_input_method_controller.cpp | 42 +++++---------- .../js_get_input_method_setting.cpp | 18 ++----- .../js/napi/inputmethodclient/js_utils.cpp | 53 +------------------ .../js/napi/inputmethodclient/js_utils.h | 5 -- 11 files changed, 69 insertions(+), 164 deletions(-) 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 ae4c52ddb..b1015227c 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -142,9 +142,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); @@ -196,10 +194,7 @@ napi_value JsInputMethodEngineSetting::MoveCursor(napi_env env, napi_callback_in } InputMethodAbility::GetInstance()->MoveCursor(number); - - napi_value result = nullptr; - napi_get_null(env, &result); - return result; + return JsUtil::Const::Null(env); } void JsInputMethodEngineSetting::RegisterListener( @@ -275,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, @@ -298,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(); @@ -314,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; }; @@ -420,9 +430,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; + return JsUtil::Const::Null(env); } void JsInputMethodEngineSetting::OnInputStart() 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 629a8829d..345b137ce 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" @@ -86,6 +87,8 @@ private: void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); 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 +97,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 6c01192da..9a9579cef 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 f5d3d4101..65aa234ce 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -120,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); @@ -233,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) @@ -265,18 +260,11 @@ napi_value JsKeyboardDelegateSetting::UnSubscribe(napi_env env, napi_callback_in 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 73d7fa67b..c4827036d 100644 --- a/frameworks/js/napi/inputmethodability/js_panel.cpp +++ b/frameworks/js/napi/inputmethodability/js_panel.cpp @@ -136,10 +136,10 @@ napi_value JsPanel::Resize(napi_env env, napi_callback_info 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); - status = JsUtils::GetValue(env, argv[ARGC_ZERO], ctxt->width); - NAPI_ASSERT_BASE(env, status == napi_ok, "get width failed!", status); - status = JsUtils::GetValue(env, argv[ARGC_ONE], ctxt->height); - NAPI_ASSERT_BASE(env, status == napi_ok, "get height failed!", status); + bool ret = JsUtil::GetValue(env, argv[ARGC_ZERO], ctxt->width); + NAPI_ASSERT_BASE(env, ret, "get width failed!", status); + status = JsUtil::GetValue(env, argv[ARGC_ONE], ctxt->height); + NAPI_ASSERT_BASE(env, ret, "get height failed!", status); return napi_ok; }; @@ -254,9 +254,7 @@ napi_value JsPanel::Subscribe(napi_env env, napi_callback_info info) auto inputMethodPanel = UnwrapPanel(env, thisVar); observer->SaveInfo(env, type, argv[ARGC_ONE], 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) @@ -273,9 +271,7 @@ napi_value JsPanel::UnSubscribe(napi_env env, napi_callback_info info) 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 90227d844..f0d623320 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -57,7 +57,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); @@ -122,20 +122,13 @@ 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) 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 d574315ec..7017fc8cb 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.h @@ -259,7 +259,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/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index cbe192f5b..aeac40cfd 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -69,9 +69,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); @@ -185,10 +183,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) @@ -208,37 +203,24 @@ napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_i 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 46942c4fb..d6776c2bf 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -77,9 +77,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); @@ -182,8 +180,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; }; @@ -360,10 +358,7 @@ napi_value JsGetInputMethodSetting::Subscribe(napi_env env, napi_callback_info i 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); } void JsGetInputMethodSetting::UnRegisterListener(napi_value callback, std::string type) @@ -416,9 +411,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) @@ -469,7 +462,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_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index b131a7cf1..213ff385c 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.cpp +++ b/frameworks/js/napi/inputmethodclient/js_utils.cpp @@ -170,7 +170,7 @@ bool JsUtils::TraverseCallback( if (valueType != napi_boolean) { continue; } - GetValue(item->env_, result, isResult); + JsUtil::GetValue(item->env_, result, isResult); if (isResult) { isOnKeyEvent = true; } @@ -213,56 +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", status); - 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", status); - 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", status); - return napi_get_value_bool(env, in, &out); -} - -/* 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; -} } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/js/napi/inputmethodclient/js_utils.h b/frameworks/js/napi/inputmethodclient/js_utils.h index eb6045981..6f20719dd 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.h +++ b/frameworks/js/napi/inputmethodclient/js_utils.h @@ -91,11 +91,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, PanelInfo &out); - private: static int32_t Convert(int32_t code); -- Gitee