diff --git a/frameworks/ets/ani/BUILD.gn b/frameworks/ets/ani/BUILD.gn index 05770ddaf6b5f19a62687ee9906e4fe0f3946f52..bb6c0efc9eebadaf386e3c5ae3deec9b2440268b 100644 --- a/frameworks/ets/ani/BUILD.gn +++ b/frameworks/ets/ani/BUILD.gn @@ -27,6 +27,7 @@ group("ani_packages") { "${ability_runtime_path}/frameworks/ets/ani/app/ability_stage_context:ability_stage_context_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/app/application_context:application_context_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/app/context:context_ani_kit", + "${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 0000000000000000000000000000000000000000..402c63b24cfc793adb7f9e2b2e113f1d678ea3b0 --- /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 0000000000000000000000000000000000000000..9dbccb6f094388c31089a7c04e067e2efbc5c0a0 --- /dev/null +++ b/frameworks/ets/ani/featureAbility/include/ani_data_ability_helper.h @@ -0,0 +1,28 @@ +/* + * 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 + +typedef struct __ani_env ani_env; +typedef class __ani_object *ani_object; + +namespace OHOS { +namespace AppExecFwk { +class PacMap; +void AnalysisPacMap(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 0000000000000000000000000000000000000000..f5f9334173d10fbfa5bc3bd07b4fb5773b9ab526 --- /dev/null +++ b/frameworks/ets/ani/featureAbility/src/ani_data_ability_helper.cpp @@ -0,0 +1,156 @@ +/* + * 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.h" +#include "ani_common_util.h" +#include "data_ability_helper_impl.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 || cls == nullptr) { + TAG_LOGE(AAFwkTag::FA, "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; + 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)); + return; + } + 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)); + return; + } + 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); + return; + } + 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); + return; + } + ani_boolean isNull = ANI_FALSE; + if ((status = env->Reference_IsNull(aniValue, &isNull)) != ANI_OK || !isNull) { + TAG_LOGE(AAFwkTag::FA, "Reference_IsNull status: %{public}d or pacMap type error", status); + return; + } + pacMap.PutObject(keyStr, nullptr); +} + +void AnalysisPacMap(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 f4e8af9bf495b2d78c18608670d6f41310236f21..d8a560b1c8213ec4004b7efa64875f7028a24806 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" ] +} + generate_static_abc("ability_runtime_mission_info_abc") { base_url = "./" files = [ "./application/MissionInfo.ets" ] @@ -1059,6 +1075,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 0000000000000000000000000000000000000000..d2d63e4244482745dd71dae56f0ca29c2b426d0c --- /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. +*/ + +export type PacMap = Record | null>; \ No newline at end of file