From 5a2c0f37eead762feb48f3fe64b35fec1c17ed65 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Tue, 5 Aug 2025 20:52:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96std::string=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=BD=BF=E7=94=A8=E5=85=B1=E9=80=9A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangzezhong --- .../include/ets_ability_delegator.h | 2 - .../src/ets_ability_delegator.cpp | 99 ++++--------------- 2 files changed, 21 insertions(+), 80 deletions(-) diff --git a/frameworks/ets/ani/ability_delegator/include/ets_ability_delegator.h b/frameworks/ets/ani/ability_delegator/include/ets_ability_delegator.h index b809b3d7368..5bc16206eb0 100644 --- a/frameworks/ets/ani/ability_delegator/include/ets_ability_delegator.h +++ b/frameworks/ets/ani/ability_delegator/include/ets_ability_delegator.h @@ -87,8 +87,6 @@ public: static ani_ref GetCurrentTopAbility(ani_env* env, [[maybe_unused]]ani_class aniClass, ani_object callback); private: - static void RetrieveStringFromAni(ani_env *env, ani_string string, std::string &resString); - static ani_object WrapShellCmdResult(ani_env *env, std::unique_ptr result); static bool ParseMonitorPara(ani_env *env, ani_object monitorObj, diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp index 7e4022d402b..647e216ce53 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp @@ -47,9 +47,6 @@ std::mutex g_mutexAbilityRecord; namespace { constexpr const char* CONTEXT_CLASS_NAME = "Lapplication/Context/Context;"; constexpr const char* SHELL_CMD_RESULT_CLASS_NAME = "Lapplication/shellCmdResult/ShellCmdResultImpl;"; -constexpr const char* ABILITY_MONITOR_INNER_CLASS_NAME = "Lapplication/AbilityMonitor/AbilityMonitorInner;"; -constexpr const char* ABILITY_STAGE_MONITOR_INNER_CLASS_NAME = - "Lapplication/AbilityStageMonitor/AbilityStageMonitorInner;"; constexpr const char* ABILITY_STAGE_CLASS_NAME = "L@ohos/app/ability/AbilityStage/AbilityStage;"; constexpr int COMMON_FAILED = 16000100; } @@ -280,43 +277,23 @@ void EtsAbilityDelegator::PrintSync(ani_env *env, [[maybe_unused]]ani_class aniC TAG_LOGE(AAFwkTag::DELEGATOR, "env is nullptr"); return; } - std::string msgStr = ""; - ani_size sz {}; - env->String_GetUTF8Size(msg, &sz); - msgStr.resize(sz + 1); - env->String_GetUTF8SubString(msg, 0, sz, msgStr.data(), msgStr.size(), &sz); - TAG_LOGD(AAFwkTag::DELEGATOR, "PrintSync %{public}s", msgStr.c_str()); - + std::string strMsg = ""; + if (!AppExecFwk::GetStdString(env, msg, strMsg)) { + TAG_LOGE(AAFwkTag::DELEGATOR, "GetStdString Failed"); + AbilityRuntime::EtsErrorUtil::ThrowError(env, + static_cast(AbilityRuntime::AbilityErrorCode::ERROR_CODE_INVALID_PARAM), + "Parse msg failed, msg must be string."); + return; + } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::ETS); if (delegator == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "null delegator"); return; } - - delegator->Print(msgStr); + delegator->Print(strMsg); return; } -void EtsAbilityDelegator::RetrieveStringFromAni(ani_env *env, ani_string str, std::string &res) -{ - if (env == nullptr) { - TAG_LOGE(AAFwkTag::DELEGATOR, "env is nullptr"); - return; - } - ani_size sz {}; - ani_status status = ANI_ERROR; - if ((status = env->String_GetUTF8Size(str, &sz)) != ANI_OK) { - TAG_LOGE(AAFwkTag::DELEGATOR, "status: %{public}d", status); - return; - } - res.resize(sz + 1); - if ((status = env->String_GetUTF8SubString(str, 0, sz, res.data(), res.size(), &sz)) != ANI_OK) { - TAG_LOGE(AAFwkTag::DELEGATOR, "status: %{public}d", status); - return; - } - res.resize(sz); -} - void EtsAbilityDelegator::AddAbilityMonitor(ani_env *env, [[maybe_unused]]ani_class aniClass, ani_object monitorObj, ani_object callback) { @@ -893,33 +870,16 @@ bool EtsAbilityDelegator::ParseMonitorParaInner(ani_env *env, ani_object monitor TAG_LOGE(AAFwkTag::DELEGATOR, "env or monitorObj is nullptr"); return false; } - ani_class monitorCls = nullptr; - ani_status status = env->FindClass(ABILITY_MONITOR_INNER_CLASS_NAME, &monitorCls); - if (status != ANI_OK) { - TAG_LOGE(AAFwkTag::DELEGATOR, "FindClass failed status: %{public}d", status); - return false; - } - ani_ref moduleNameRef = nullptr; - status = env->Object_GetPropertyByName_Ref(monitorObj, "moduleName", &moduleNameRef); - if (ANI_OK != status) { - TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref "); - AbilityRuntime::EtsErrorUtil::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER); - return false; - } std::string strModuleName = ""; - ani_string aniModuleString = static_cast(moduleNameRef); - RetrieveStringFromAni(env, aniModuleString, strModuleName); - ani_ref abilityNameRef = nullptr; - status = env->Object_GetPropertyByName_Ref(monitorObj, "abilityName", &abilityNameRef); - if (ANI_OK != status) { - TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref "); - AbilityRuntime::EtsErrorUtil::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER); + if (!AppExecFwk::GetFieldStringByName(env, monitorObj, "moduleName", strModuleName)) { + TAG_LOGE(AAFwkTag::DELEGATOR, "GetStdString strModuleName Failed"); return false; } std::string strAbilityName = ""; - ani_string aniAbilityName = static_cast(abilityNameRef); - RetrieveStringFromAni(env, aniAbilityName, strAbilityName); - + if (!AppExecFwk::GetFieldStringByName(env, monitorObj, "abilityName", strAbilityName)) { + TAG_LOGE(AAFwkTag::DELEGATOR, "GetStdString strAbilityName Failed"); + return false; + } std::shared_ptr abilityMonitor = nullptr; if (strModuleName.empty()) { abilityMonitor = std::make_shared(strAbilityName); @@ -990,33 +950,16 @@ bool EtsAbilityDelegator::ParseStageMonitorParaInner(ani_env *env, ani_object st TAG_LOGE(AAFwkTag::DELEGATOR, "env or stageMonitorObj is nullptr"); return false; } - ani_class monitorCls = nullptr; - ani_status status = env->FindClass(ABILITY_STAGE_MONITOR_INNER_CLASS_NAME, &monitorCls); - if (status != ANI_OK) { - TAG_LOGE(AAFwkTag::DELEGATOR, "FindClass failed status: %{public}d", status); - return false; - } - ani_ref moduleNameRef = nullptr; - status = env->Object_GetPropertyByName_Ref(stageMonitorObj, "moduleName", &moduleNameRef); - if (ANI_OK != status) { - TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref failed status: %{public}d", status); - AbilityRuntime::EtsErrorUtil::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER); - return false; - } std::string strModuleName = ""; - ani_string aniModuleString = static_cast(moduleNameRef); - RetrieveStringFromAni(env, aniModuleString, strModuleName); - TAG_LOGD(AAFwkTag::DELEGATOR, "strModuleName %{public}s ", strModuleName.c_str()); - ani_ref srcEntranceRef = nullptr; - status = env->Object_GetPropertyByName_Ref(stageMonitorObj, "srcEntrance", &srcEntranceRef); - if (ANI_OK != status) { - TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref "); - AbilityRuntime::EtsErrorUtil::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER); + if (!AppExecFwk::GetFieldStringByName(env, stageMonitorObj, "moduleName", strModuleName)) { + TAG_LOGE(AAFwkTag::DELEGATOR, "GetStdString strModuleName Failed"); return false; } std::string srcEntrance = ""; - ani_string aniSrcEntranceRef = static_cast(srcEntranceRef); - RetrieveStringFromAni(env, aniSrcEntranceRef, srcEntrance); + if (!AppExecFwk::GetFieldStringByName(env, stageMonitorObj, "srcEntrance", srcEntrance)) { + TAG_LOGE(AAFwkTag::DELEGATOR, "GetStdString srcEntrance Failed"); + return false; + } TAG_LOGD(AAFwkTag::DELEGATOR, "srcEntrance %{public}s ", srcEntrance.c_str()); stageMonitor = std::make_shared(strModuleName, srcEntrance); return true; -- Gitee