diff --git a/interfaces/innerkits/common/include/accessibility_utils.h b/interfaces/innerkits/common/include/accessibility_utils.h index b26515ecca9191e15b94e9a3caa752aa365c16cf..e3ae4790b3156fa28f9a85c0542632c6c09dae60 100644 --- a/interfaces/innerkits/common/include/accessibility_utils.h +++ b/interfaces/innerkits/common/include/accessibility_utils.h @@ -54,7 +54,8 @@ std::string ConvertDaltonizationTypeToString(OHOS::AccessibilityConfig::DALTONIZ void ConvertRectToJS(napi_env env, napi_value result, const OHOS::Accessibility::Rect& rect); void ConvertAccessibleAbilityInfosToJS(napi_env env, napi_value& result, std::vector& accessibleAbilityInfos); -bool ConvertEventInfoJSToNAPI(napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo); +bool ConvertEventInfoJSToNAPI( + napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo); OHOS::AccessibilityConfig::DALTONIZATION_TYPE ConvertStringToDaltonizationTypes(std::string& type); void ConvertActionArgsJSToNAPI( napi_env env, napi_value object, std::map& args, OHOS::Accessibility::ActionType action); @@ -75,7 +76,13 @@ bool ConvertJSToCapabilities(napi_env env, napi_value arrayValue, uint32_t &capa uint32_t GetColorValue(napi_env env, napi_value object, napi_value propertyNameValue); uint32_t GetColorValue(napi_env env, napi_value value); uint32_t ConvertColorStringToNumer(std::string colorStr); +bool ConvertEventInfoJSToNAPIPart( + napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo); std::string ConvertColorToString(uint32_t color); +std::string ConvertStringJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty); +int32_t ConvertIntJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty); +void ConvertStringArrayJSToNAPI(napi_env env, napi_value object, + napi_value propertyNameValue, bool &hasProperty, std::vector &stringArray); OHOS::Accessibility::ActionType ConvertStringToAccessibleOperationType(const std::string &type); OHOS::Accessibility::AccessibilityAbilityTypes ConvertStringToAccessibilityAbilityTypes(const std::string &type); diff --git a/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension_context.cpp b/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension_context.cpp index dde55f90f2cb4868c47632b2903d5d92d0c2fd74..fd1530f1a0f9d1f0070df17c05ee5e27dc4fd222 100644 --- a/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension_context.cpp +++ b/interfaces/kits/napi/accessibility_extension_module_loader/src/napi_accessibility_extension_context.cpp @@ -222,7 +222,11 @@ private: int32_t focus = isAccessibilityFocus ? FOCUS_TYPE_ACCESSIBILITY : FOCUS_TYPE_INPUT; HILOG_DEBUG("focus type is [%{public}d]", focus); + return GetFoucusElementCompleteTask(engine, focus, lastParam); + } + NativeValue* GetFoucusElementCompleteTask(NativeEngine& engine, int32_t focus, NativeValue* lastParam) + { AsyncTask::CompleteCallback complete = [weak = context_, focus](NativeEngine& engine, AsyncTask& task, int32_t status) { HILOG_INFO("GetFocusElement begin"); @@ -306,7 +310,12 @@ private: lastParam = nullptr; HILOG_INFO("argc is others, use promise"); } + return GetWindowRootElementCompleteTask(engine, windowId, isActiveWindow, lastParam); + } + NativeValue* GetWindowRootElementCompleteTask( + NativeEngine& engine, int32_t windowId, bool isActiveWindow, NativeValue* lastParam) + { AsyncTask::CompleteCallback complete = [weak = context_, windowId, isActiveWindow](NativeEngine& engine, AsyncTask& task, int32_t status) { HILOG_INFO("GetWindowRootElement begin"); @@ -363,37 +372,24 @@ private: bool hasDisplayId = false; NativeValue* lastParam = nullptr; if (info.argc >= ARGS_SIZE_TWO) { - if (info.argv[PARAM0] != nullptr && info.argv[PARAM1] != nullptr && - info.argv[PARAM0]->TypeOf() == NATIVE_NUMBER && info.argv[PARAM1]->TypeOf() == NATIVE_FUNCTION) { - if (ConvertFromJsValue(engine, info.argv[PARAM0], displayId)) { - lastParam = info.argv[PARAM1]; - hasDisplayId = true; - } else { - HILOG_ERROR("Convert displayId from js value failed"); - lastParam = info.argv[PARAM1]; - } - } else if (info.argv[PARAM1] != nullptr && info.argv[PARAM1]->TypeOf() == NATIVE_FUNCTION) { + if (info.argv[PARAM1] != nullptr && info.argv[PARAM1]->TypeOf() == NATIVE_FUNCTION) { HILOG_INFO("argc is more than two, use callback: situation 1"); lastParam = info.argv[PARAM1]; } else if (info.argv[PARAM0] != nullptr && info.argv[PARAM0]->TypeOf() == NATIVE_FUNCTION) { HILOG_INFO("argc is more than two, use callback: situation 2"); lastParam = info.argv[PARAM0]; - } else if (info.argv[PARAM0] != nullptr && info.argv[PARAM0]->TypeOf() == NATIVE_NUMBER) { - HILOG_INFO("argc is more than two, use promise: situation 3"); - lastParam = nullptr; - hasDisplayId = ConvertFromJsValue(engine, info.argv[PARAM0], displayId); } else { lastParam = nullptr; - HILOG_INFO("argc is more than two, use promise"); + } + if (info.argv[PARAM0] != nullptr && info.argv[PARAM0]->TypeOf() == NATIVE_NUMBER) { + hasDisplayId = ConvertFromJsValue(engine, info.argv[PARAM0], displayId); + HILOG_INFO("argc is more than two, use promise: situation 3"); } } else if (info.argc == ARGS_SIZE_ONE) { if (info.argv[PARAM0] != nullptr && info.argv[PARAM0]->TypeOf() == NATIVE_FUNCTION) { lastParam = info.argv[PARAM0]; - } else { - if ((info.argv[PARAM0] != nullptr && info.argv[PARAM0]->TypeOf() == NATIVE_NUMBER) && - ConvertFromJsValue(engine, info.argv[PARAM0], displayId)) { - hasDisplayId = true; - } + } else if (info.argv[PARAM0] != nullptr && info.argv[PARAM0]->TypeOf() == NATIVE_NUMBER) { + hasDisplayId = ConvertFromJsValue(engine, info.argv[PARAM0], displayId); lastParam = nullptr; HILOG_INFO("argc is one, use promise"); } @@ -515,7 +511,12 @@ private: ERROR_MESSAGE_PARAMETER_ERROR)); return engine.CreateUndefined(); } + return GestureInjectCompleteTask(engine, info, gesturePath); + } + NativeValue* GestureInjectCompleteTask( + NativeEngine& engine, NativeCallbackInfo& info, std::shared_ptr gesturePath) + { AsyncTask::CompleteCallback complete = [weak = context_, gesturePath]( NativeEngine& engine, AsyncTask& task, int32_t status) { diff --git a/interfaces/kits/napi/src/napi_accessibility_utils.cpp b/interfaces/kits/napi/src/napi_accessibility_utils.cpp index 7a6208361844e882cf4466f103647e2344b7776b..4aa5879169b42fe132778692a2f73d9a1f7d92ae 100644 --- a/interfaces/kits/napi/src/napi_accessibility_utils.cpp +++ b/interfaces/kits/napi/src/napi_accessibility_utils.cpp @@ -791,18 +791,59 @@ void ConvertActionArgsJSToNAPI( } } -bool ConvertEventInfoJSToNAPI(napi_env env, napi_value object, AccessibilityEventInfo& eventInfo) +int32_t ConvertIntJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty) +{ + int32_t dataValue = 0; + napi_has_property(env, object, propertyNameValue, &hasProperty); + if (hasProperty) { + napi_value itemValue = nullptr; + napi_get_property(env, object, propertyNameValue, &itemValue); + napi_get_value_int32(env, itemValue, &dataValue); + } + return dataValue; +} + +std::string ConvertStringJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty) +{ + std::string str = ""; + napi_has_property(env, object, propertyNameValue, &hasProperty); + if (hasProperty) { + napi_value itemValue = nullptr; + napi_get_property(env, object, propertyNameValue, &itemValue); + str = GetStringFromNAPI(env, itemValue); + } + return str; +} + +void ConvertStringArrayJSToNAPI(napi_env env, napi_value object, + napi_value propertyNameValue, bool &hasProperty, std::vector &stringArray) +{ + std::string str = ""; + napi_has_property(env, object, propertyNameValue, &hasProperty); + if (hasProperty) { + napi_value contentsValue = nullptr; + napi_get_property(env, object, propertyNameValue, &contentsValue); + napi_value data = nullptr; + uint32_t dataLen = 0; + napi_get_array_length(env, contentsValue, &dataLen); + for (uint32_t i = 0; i < dataLen; i++) { + napi_get_element(env, contentsValue, i, &data); + str = GetStringFromNAPI(env, data); + stringArray.push_back(str); + } + } +} + +bool ConvertEventInfoJSToNAPI( + napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo) { - napi_value propertyNameValue = nullptr; bool hasProperty = false; int32_t dataValue = 0; std::string str = ""; + napi_value propertyNameValue = nullptr; napi_create_string_utf8(env, "type", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value value = nullptr; - napi_get_property(env, object, propertyNameValue, &value); - str = GetStringFromNAPI(env, value); EventType eventType = ConvertStringToEventInfoTypes(str); eventInfo.SetEventType(eventType); if (eventType == TYPE_VIEW_INVALID) { @@ -813,136 +854,105 @@ bool ConvertEventInfoJSToNAPI(napi_env env, napi_value object, AccessibilityEven } napi_create_string_utf8(env, "windowUpdateType", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value windowUpdateTypeValue = nullptr; - napi_get_property(env, object, propertyNameValue, &windowUpdateTypeValue); - str = GetStringFromNAPI(env, windowUpdateTypeValue); eventInfo.SetEventType(TYPE_WINDOW_UPDATE); eventInfo.SetWindowChangeTypes(ConvertStringToWindowUpdateTypes(str)); } - + napi_create_string_utf8(env, "bundleName", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value bundleNameValue = nullptr; - napi_get_property(env, object, propertyNameValue, &bundleNameValue); - str = GetStringFromNAPI(env, bundleNameValue); - if (str == "") { + if (str != "") { + eventInfo.SetBundleName(str); + } else { return false; } - eventInfo.SetBundleName(str); } else { return false; } napi_create_string_utf8(env, "componentType", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value componentTypeValue = nullptr; - napi_get_property(env, object, propertyNameValue, &componentTypeValue); - str = GetStringFromNAPI(env, componentTypeValue); eventInfo.SetComponentType(str); } napi_create_string_utf8(env, "pageId", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value pageIdValue = nullptr; - napi_get_property(env, object, propertyNameValue, &pageIdValue); - napi_get_value_int32(env, pageIdValue, &dataValue); eventInfo.SetPageId(dataValue); } napi_create_string_utf8(env, "description", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value descriptionValue = nullptr; - napi_get_property(env, object, propertyNameValue, &descriptionValue); - str = GetStringFromNAPI(env, descriptionValue); eventInfo.SetDescription(str); } - napi_create_string_utf8(env, "triggerAction", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); - if (hasProperty) { - napi_value triggerActionValue = nullptr; - napi_get_property(env, object, propertyNameValue, &triggerActionValue); - str = GetStringFromNAPI(env, triggerActionValue); - eventInfo.SetTriggerAction(ConvertStringToAccessibleOperationType(str)); - if (eventInfo.GetTriggerAction() == ACCESSIBILITY_ACTION_INVALID) { - return false; - } - } else { - return false; - } - napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value textMoveUnitValue = nullptr; - napi_get_property(env, object, propertyNameValue, &textMoveUnitValue); - str = GetStringFromNAPI(env, textMoveUnitValue); eventInfo.SetTextMovementStep(ConvertStringToTextMoveUnit(str)); } + return ConvertEventInfoJSToNAPIPart(env, object, eventInfo); +} + +bool ConvertEventInfoJSToNAPIPart( + napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo) +{ + bool hasProperty = false; + int32_t dataValue = 0; + std::string str = ""; + napi_value propertyNameValue = nullptr; napi_create_string_utf8(env, "contents", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + std::vector stringArray {}; + ConvertStringArrayJSToNAPI(env, object, propertyNameValue, hasProperty, stringArray); if (hasProperty) { - napi_value contentsValue = nullptr; - napi_get_property(env, object, propertyNameValue, &contentsValue); - napi_value data = nullptr; - uint32_t dataLen = 0; - napi_get_array_length(env, contentsValue, &dataLen); - for (uint32_t i = 0; i < dataLen; i++) { - napi_get_element(env, contentsValue, i, &data); - str = GetStringFromNAPI(env, data); + for (auto str : stringArray) { eventInfo.AddContent(str); } } + napi_create_string_utf8(env, "triggerAction", NAPI_AUTO_LENGTH, &propertyNameValue); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); + if (hasProperty) { + eventInfo.SetTriggerAction(ConvertStringToAccessibleOperationType(str)); + if (eventInfo.GetTriggerAction() == ACCESSIBILITY_ACTION_INVALID) { + return false; + } + } else { + return false; + } + napi_create_string_utf8(env, "lastContent", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value lastContentValue = nullptr; - napi_get_property(env, object, propertyNameValue, &lastContentValue); - str = GetStringFromNAPI(env, lastContentValue); eventInfo.SetLatestContent(str); } napi_create_string_utf8(env, "beginIndex", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value beginIndexValue = nullptr; - napi_get_property(env, object, propertyNameValue, &beginIndexValue); - napi_get_value_int32(env, beginIndexValue, &dataValue); eventInfo.SetBeginIndex(dataValue); } napi_create_string_utf8(env, "currentIndex", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value currentIndexValue = nullptr; - napi_get_property(env, object, propertyNameValue, ¤tIndexValue); - napi_get_value_int32(env, currentIndexValue, &dataValue); eventInfo.SetCurrentIndex(dataValue); } napi_create_string_utf8(env, "endIndex", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value endIndexValue = nullptr; - napi_get_property(env, object, propertyNameValue, &endIndexValue); - napi_get_value_int32(env, endIndexValue, &dataValue); eventInfo.SetEndIndex(dataValue); } napi_create_string_utf8(env, "itemCount", NAPI_AUTO_LENGTH, &propertyNameValue); - napi_has_property(env, object, propertyNameValue, &hasProperty); + dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty); if (hasProperty) { - napi_value itemCountValue = nullptr; - napi_get_property(env, object, propertyNameValue, &itemCountValue); - napi_get_value_int32(env, itemCountValue, &dataValue); eventInfo.SetItemCounts(dataValue); } return true;