From 32e98aec5e22cdb32406c78c8a076effce138839 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Mon, 14 Jul 2025 20:59:13 +0800 Subject: [PATCH] add PacMap code Signed-off-by: zhangzezhong --- frameworks/ets/ani/BUILD.gn | 1 + frameworks/ets/ani/featureAbility/BUILD.gn | 45 ++++++ .../include/ani_data_ability_helper.h | 27 ++++ .../src/ani_data_ability_helper.cpp | 148 ++++++++++++++++++ frameworks/ets/ets/BUILD.gn | 17 ++ .../ets/ets/ability/dataAbilityHelper.ets | 16 ++ 6 files changed, 254 insertions(+) create mode 100644 frameworks/ets/ani/featureAbility/BUILD.gn create mode 100644 frameworks/ets/ani/featureAbility/include/ani_data_ability_helper.h create mode 100644 frameworks/ets/ani/featureAbility/src/ani_data_ability_helper.cpp create mode 100644 frameworks/ets/ets/ability/dataAbilityHelper.ets diff --git a/frameworks/ets/ani/BUILD.gn b/frameworks/ets/ani/BUILD.gn index 9ccd869f389..787d95712e7 100644 --- a/frameworks/ets/ani/BUILD.gn +++ b/frameworks/ets/ani/BUILD.gn @@ -22,6 +22,7 @@ group("ani_packages") { "${ability_runtime_path}/frameworks/ets/ani/ani_wantagent_common:ani_wantagent_common", "${ability_runtime_path}/frameworks/ets/ani/app_manager:app_manager_ani", "${ability_runtime_path}/frameworks/ets/ani/application:application_ani", + "${ability_runtime_path}/frameworks/ets/ani/featureAbility:featureability_ani", "${ability_runtime_path}/frameworks/ets/ani/insight_intent/insight_intent_driver:insight_intent_driver_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/native_constructor:context_ani", "${ability_runtime_path}/frameworks/ets/ani/ui_extension_callback:ani_ui_extension_callback", diff --git a/frameworks/ets/ani/featureAbility/BUILD.gn b/frameworks/ets/ani/featureAbility/BUILD.gn new file mode 100644 index 00000000000..402c63b24cf --- /dev/null +++ b/frameworks/ets/ani/featureAbility/BUILD.gn @@ -0,0 +1,45 @@ +# Copyright (c) 2025 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("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_shared_library("featureability_ani") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + include_dirs = [ "./include" ] + + sources = [ "./src/ani_data_ability_helper.cpp" ] + + deps = [ + "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/common:perm_verification", + ] + + external_deps = [ + "runtime_core:ani", + "hilog:libhilog", + ] + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "ability" + part_name = "ability_runtime" +} \ No newline at end of file diff --git a/frameworks/ets/ani/featureAbility/include/ani_data_ability_helper.h b/frameworks/ets/ani/featureAbility/include/ani_data_ability_helper.h new file mode 100644 index 00000000000..f1d2a052653 --- /dev/null +++ b/frameworks/ets/ani/featureAbility/include/ani_data_ability_helper.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +#ifndef OHOS_ABILITY_RUNTIME_ANI_DATA_ABILITY_HELPER_H +#define OHOS_ABILITY_RUNTIME_ANI_DATA_ABILITY_HELPER_H + +#include "ani.h" +#include "data_ability_helper_impl.h" + +namespace OHOS { +namespace AppExecFwk { +void AnalysisPacMap(AppExecFwk::PacMap &pacMap, ani_env* env, const ani_object &aniObject); +} // namespace AppExecFwk +} // namespace OHOS +#endif /* OHOS_ABILITY_RUNTIME_ANI_DATA_ABILITY_HELPER_H */ \ No newline at end of file diff --git a/frameworks/ets/ani/featureAbility/src/ani_data_ability_helper.cpp b/frameworks/ets/ani/featureAbility/src/ani_data_ability_helper.cpp new file mode 100644 index 00000000000..3a87b18352f --- /dev/null +++ b/frameworks/ets/ani/featureAbility/src/ani_data_ability_helper.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2025 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 "ani_data_ability_helper.h" + +#include "ani_common_util.h" +#include "hilog_tag_wrapper.h" +namespace OHOS { +namespace AppExecFwk { +namespace { +constexpr const char* CLASSNAME_DOUBLE = "Lstd/core/Double;"; +constexpr const char* CLASSNAME_BOOL = "Lstd/core/Boolean;"; +constexpr const char* CLASSNAME_STRING = "Lstd/core/String;"; +constexpr const char* CLASSNAME_ARRAY = "Lescompat/Array;"; +} +bool IsInstanceOf(ani_env* env, const char* name, ani_object aniValue) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::FA, "env null"); + return false; + } + + ani_status status = ANI_ERROR; + ani_class cls = nullptr; + if ((status = env->FindClass(name, &cls)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status: %{public}d", status); + return false; + } + ani_boolean isInstanceOf; + if ((status = env->Object_InstanceOf(aniValue, cls, &isInstanceOf)) != ANI_OK) { + TAG_LOGE(AAFwkTag::FA, "Object_InstanceOf status: %{public}d", status); + return false; + } + return isInstanceOf; +} + +void SetPacMapObject(AppExecFwk::PacMap &pacMap, ani_env* env, std::string keyStr, ani_object aniValue) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::FA, "env null"); + return; + } + ani_status status = ANI_ERROR; + ani_boolean isNull = ANI_FALSE; + if ((status = env->Reference_IsNull(aniValue, &isNull)) != ANI_OK) { + TAG_LOGE(AAFwkTag::FA, "Reference_IsNull status: %{public}d", status); + return; + } + if (IsInstanceOf(env, CLASSNAME_DOUBLE, aniValue)) { + ani_double value = 0; + if ((status = env->Object_CallMethodByName_Double(aniValue, "doubleValue", nullptr, &value)) != ANI_OK) { + TAG_LOGE(AAFwkTag::FA, "Object_CallMethodByName_Double status: %{public}d", status); + return; + } + pacMap.PutDoubleValue(keyStr, static_cast(value)); + } else if (IsInstanceOf(env, CLASSNAME_BOOL, aniValue)) { + ani_boolean value = ANI_FALSE; + if ((status = env->Object_CallMethodByName_Boolean(aniValue, "unboxed", nullptr, &value)) != ANI_OK) { + TAG_LOGE(AAFwkTag::FA, "Object_CallMethodByName_Boolean status: %{public}d", status); + return; + } + pacMap.PutBooleanValue(keyStr, static_cast(value)); + } else if (IsInstanceOf(env, CLASSNAME_STRING, aniValue)) { + ani_string aniString = static_cast(aniValue); + std::string value = ""; + if (!GetStdString(env, aniString, value)) { + TAG_LOGE(AAFwkTag::FA, "GetStdString failed"); + return; + } + pacMap.PutStringValue(keyStr, value); + } else if (IsInstanceOf(env, CLASSNAME_ARRAY, aniValue)) { + std::vector stringList; + if (!UnwrapArrayString(env, aniValue, stringList)) { + TAG_LOGE(AAFwkTag::FA, "UnwrapArrayString failed"); + return; + } + pacMap.PutStringValueArray(keyStr, stringList); + } else if (isNull) { + pacMap.PutObject(keyStr, nullptr); + } else { + TAG_LOGE(AAFwkTag::FA, "pacMap type error"); + } +} + +void AnalysisPacMap(AppExecFwk::PacMap &pacMap, ani_env* env, const ani_object &aniObject) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::FA, "env null"); + return; + } + ani_ref iter = nullptr; + ani_status status = ANI_ERROR; + status = env->Object_CallMethodByName_Ref(aniObject, "$_iterator", nullptr, &iter); + if (ANI_OK != status) { + TAG_LOGE(AAFwkTag::FA, "Failed to get keys iterator status: %{public}d", status); + return; + } + ani_ref next = nullptr; + ani_boolean done = ANI_FALSE; + while (ANI_OK == env->Object_CallMethodByName_Ref(static_cast(iter), "next", nullptr, &next)) { + status = env->Object_GetFieldByName_Boolean(static_cast(next), "done", &done); + if (ANI_OK != status) { + TAG_LOGE(AAFwkTag::FA, "Failed to check iterator done status: %{public}d", status); + return; + } + if (done) { + TAG_LOGD(AAFwkTag::FA, "[forEachMapEntry] done break"); + return; + } + ani_ref keyValue = nullptr; + status = env->Object_GetFieldByName_Ref(static_cast(next), "value", &keyValue); + if (ANI_OK != status) { + TAG_LOGE(AAFwkTag::FA, "Failed to get key value status: %{public}d", status); + return; + } + ani_ref aniKey = nullptr; + status = env->TupleValue_GetItem_Ref(static_cast(keyValue), 0, &aniKey); + if (ANI_OK != status) { + TAG_LOGE(AAFwkTag::FA, "Failed to get key Item status: %{public}d", status); + return; + } + ani_ref aniVal = nullptr; + status = env->TupleValue_GetItem_Ref(static_cast(keyValue), 1, &aniVal); + if (ANI_OK != status) { + TAG_LOGE(AAFwkTag::FA, "Failed to get key Item status: %{public}d", status); + return; + } + std::string mapKey = ""; + if (!GetStdString(env, static_cast(aniKey), mapKey)) { + TAG_LOGE(AAFwkTag::FA, "GetStdString failed"); + return; + } + SetPacMapObject(pacMap, env, mapKey, static_cast(aniVal)); + } +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ets/BUILD.gn b/frameworks/ets/ets/BUILD.gn index a9c6ff11039..951e6065a59 100644 --- a/frameworks/ets/ets/BUILD.gn +++ b/frameworks/ets/ets/BUILD.gn @@ -997,6 +997,22 @@ ohos_prebuilt_etc("appRecovery_abc_etc") { deps = [ ":appRecovery_abc" ] } +generate_static_abc("ability_runtime_data_ability_helper_abc") { + base_url = "./" + files = [ "./ability/dataAbilityHelper.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_data_ability_helper_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_data_ability_helper_abc_etc") { + source = "$target_out_dir/ability_runtime_data_ability_helper_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_data_ability_helper_abc" ] +} + group("ets_packages") { deps = [ ":ability_application_abc_etc", @@ -1028,6 +1044,7 @@ group("ets_packages") { ":ability_runtime_connect_options_abc_etc", ":ability_runtime_context_abc_etc", ":ability_runtime_custom_data_abc_etc", + ":ability_runtime_data_ability_helper_abc_etc", ":ability_runtime_data_uri_utils_abc_etc", ":ability_runtime_environment_callback_abc_etc", ":ability_runtime_error_code_abc_etc", diff --git a/frameworks/ets/ets/ability/dataAbilityHelper.ets b/frameworks/ets/ets/ability/dataAbilityHelper.ets new file mode 100644 index 00000000000..7eabf2a7eb0 --- /dev/null +++ b/frameworks/ets/ets/ability/dataAbilityHelper.ets @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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. +*/ + +type PacMap = Record | null>; \ No newline at end of file -- Gitee