From 92e6df4b808937b04c5f0ced674274bd6d23d1fa Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Tue, 24 Jun 2025 19:34:17 +0800 Subject: [PATCH] childProcessManager_0624 Signed-off-by: zhangzezhong --- frameworks/ets/ani/BUILD.gn | 1 + .../ets/ani/child_process_manager/BUILD.gn | 66 +++++++ .../include/ets_child_process_manager.h | 26 +++ .../src/ets_child_process_manager.cpp | 172 ++++++++++++++++++ .../@ohos.app.ability.childProcessManager.ets | 51 ++++++ frameworks/ets/ets/BUILD.gn | 17 ++ 6 files changed, 333 insertions(+) create mode 100644 frameworks/ets/ani/child_process_manager/BUILD.gn create mode 100644 frameworks/ets/ani/child_process_manager/include/ets_child_process_manager.h create mode 100644 frameworks/ets/ani/child_process_manager/src/ets_child_process_manager.cpp create mode 100644 frameworks/ets/ets/@ohos.app.ability.childProcessManager.ets diff --git a/frameworks/ets/ani/BUILD.gn b/frameworks/ets/ani/BUILD.gn index 0b5e04d5f18..fbe083a8dfc 100644 --- a/frameworks/ets/ani/BUILD.gn +++ b/frameworks/ets/ani/BUILD.gn @@ -24,6 +24,7 @@ group("ani_packages") { "${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/app_manager:ability_app_manager_ani_kit", + "${ability_runtime_path}/frameworks/ets/ani/child_process_manager:ability_child_process_manager_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/form_extension_ability:form_extension_ability_etc", "${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", diff --git a/frameworks/ets/ani/child_process_manager/BUILD.gn b/frameworks/ets/ani/child_process_manager/BUILD.gn new file mode 100644 index 00000000000..037d0249c9d --- /dev/null +++ b/frameworks/ets/ani/child_process_manager/BUILD.gn @@ -0,0 +1,66 @@ +# 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/ohos.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_shared_library("ability_child_process_manager_ani_kit") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + include_dirs = [ + "./include", + "${ability_runtime_path}/frameworks/ets/ani/enum_convert", + ] + + configs = [] + + public_configs = [] + + sources = [] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", + ] + + external_deps = [ + "hilog:libhilog", + "ipc:ipc_capi", + "runtime_core:ani", + ] + + if (ability_runtime_child_process) { + sources += [ "./src/ets_child_process_manager.cpp" ] + deps += [ "${ability_runtime_innerkits_path}/child_process_manager:child_process_manager" ] + } + + if (ability_runtime_graphics) { + external_deps += [ "image_framework:image" ] + } + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "ability" + part_name = "ability_runtime" +} \ No newline at end of file diff --git a/frameworks/ets/ani/child_process_manager/include/ets_child_process_manager.h b/frameworks/ets/ani/child_process_manager/include/ets_child_process_manager.h new file mode 100644 index 00000000000..7076424efb4 --- /dev/null +++ b/frameworks/ets/ani/child_process_manager/include/ets_child_process_manager.h @@ -0,0 +1,26 @@ +/* + * 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_ETS_CHILD_PROCESS_MANAGER_H +#define OHOS_ABILITY_RUNTIME_ETS_CHILD_PROCESS_MANAGER_H + +#include "sts_runtime.h" + +namespace OHOS { +namespace ChildProcessManagerEts { +void EtsChildProcessManagerInit(ani_env *env); +} // namespace ChildProcessManagerEts +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ETS_CHILD_PROCESS_MANAGER_H \ No newline at end of file diff --git a/frameworks/ets/ani/child_process_manager/src/ets_child_process_manager.cpp b/frameworks/ets/ani/child_process_manager/src/ets_child_process_manager.cpp new file mode 100644 index 00000000000..d13bfd8a2b2 --- /dev/null +++ b/frameworks/ets/ani/child_process_manager/src/ets_child_process_manager.cpp @@ -0,0 +1,172 @@ +/* + * 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 "ets_child_process_manager.h" + +#include "ani_common_util.h" +#include "ani_enum_convert.h" +#include "child_process_manager.h" +#include "hilog_tag_wrapper.h" +#include "sts_error_utils.h" + +namespace OHOS { +namespace ChildProcessManagerEts { +namespace { +constexpr const char* CHILD_PROCESS_MANAGER_NAME_SPACE = "L@ohos/app/ability/childProcessManager/childProcessManager;"; +enum { + MODE_SELF_FORK = 0, + MODE_APP_SPAWN_FORK = 1, +}; +} + +class EtsChildProcessManager { +public: + EtsChildProcessManager() = default; + ~EtsChildProcessManager() = default; + + static EtsChildProcessManager &GetInstance() + { + static EtsChildProcessManager instance; + return instance; + } + + static void StartChildProcess(ani_env *env, ani_string etsSrcEntry, ani_enum_item etsStartMode, + ani_object callback) + { + GetInstance().OnStartChildProcess(env, etsSrcEntry, etsStartMode, callback); + } + +private: + void OnStartChildProcess(ani_env *env, ani_string etsSrcEntry, ani_enum_item etsStartMode, ani_object callback) + { + TAG_LOGD(AAFwkTag::PROCESSMGR, "called"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "env is null"); + return; + } + + if (AbilityRuntime::ChildProcessManager::GetInstance().IsChildProcess()) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "Already in child process"); + AbilityRuntime::ThrowStsError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_OPERATION_NOT_SUPPORTED); + return; + } + + std::string srcEntry; + if (!AppExecFwk::GetStdString(env, etsSrcEntry, srcEntry)) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "Parse srcEntry failed"); + AbilityRuntime::ThrowStsInvalidParamError(env, "Parse param srcEntry failed, must be a valid string."); + return; + } + TAG_LOGD(AAFwkTag::PROCESSMGR, "srcEntry %{public}s", srcEntry.c_str()); + + ani_int startMode; + if (!AAFwk::AniEnumConvertUtil::EnumConvertStsToNative(env, etsStartMode, startMode)) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "Parse startMode failed"); + AbilityRuntime::ThrowStsInvalidParamError(env, + "Unsupported startMode, must be StartMode.SELF_FORK or StartMode.APP_SPAWN_FORK."); + return; + } + TAG_LOGD(AAFwkTag::PROCESSMGR, "startMode %{public}d", startMode); + + if (startMode != MODE_SELF_FORK && startMode != MODE_APP_SPAWN_FORK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "Invalid StartMode"); + AbilityRuntime::ThrowStsInvalidParamError(env, + "Unsupported startMode, must be StartMode.SELF_FORK or StartMode.APP_SPAWN_FORK."); + return; + } + + if (startMode == MODE_SELF_FORK) { + StartChildProcessSelfForkTask(env, srcEntry, callback); + } else { + StartChildProcessAppSpawnForkTask(env, srcEntry, callback); + } + } + + static void StartChildProcessSelfForkTask(ani_env *env, std::string &etsSrcEntry, ani_object callback) + { + pid_t pid = 0; + AbilityRuntime::ChildProcessManagerErrorCode errorCode = + AbilityRuntime::ChildProcessManager::GetInstance().StartChildProcessBySelfFork(etsSrcEntry, pid); + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::CreateStsErrorByNativeErr(env, static_cast(errorCode)), nullptr); + } + + static void StartChildProcessAppSpawnForkTask(ani_env *env, std::string &etsSrcEntry, ani_object callback) + { + pid_t pid = -1; + AbilityRuntime::ChildProcessManagerErrorCode errorCode = + AbilityRuntime::ChildProcessManager::GetInstance().StartChildProcessByAppSpawnFork(etsSrcEntry, pid); + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::CreateStsErrorByNativeErr(env, static_cast(errorCode)), nullptr); + } +}; + +void EtsChildProcessManagerInit(ani_env *env) +{ + TAG_LOGD(AAFwkTag::PROCESSMGR, "EtsChildProcessManagerInit call"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null env"); + return; + } + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "ResetError failed"); + } + ani_status status = ANI_ERROR; + ani_namespace ns; + status = env->FindNamespace(CHILD_PROCESS_MANAGER_NAME_SPACE, &ns); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "FindNamespace childProcessManager failed status : %{public}d", status); + return; + } + std::array kitFunctions = { + ani_native_function {"nativeStartChildProcess", + "Lstd/core/String;L@ohos/app/ability/childProcessManager/childProcessManager/StartMode;" + "Lutils/AbilityUtils/AsyncCallbackWrapper;:V", + reinterpret_cast(EtsChildProcessManager::StartChildProcess)} + }; + status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size()); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "Namespace_BindNativeFunctions failed status : %{public}d", status); + } + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "ResetError failed"); + } + TAG_LOGD(AAFwkTag::PROCESSMGR, "EtsChildProcessManagerInit end"); +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + TAG_LOGD(AAFwkTag::PROCESSMGR, "in ChildProcessManagerEts.ANI_Constructor"); + if (vm == nullptr || result == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null vm or result"); + return ANI_INVALID_ARGS; + } + + ani_env *env = nullptr; + ani_status status = ANI_ERROR; + status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "GetEnv failed, status=%{public}d", status); + return ANI_NOT_FOUND; + } + EtsChildProcessManagerInit(env); + *result = ANI_VERSION_1; + TAG_LOGD(AAFwkTag::PROCESSMGR, "ChildProcessManagerEts.ANI_Constructor finished"); + return ANI_OK; +} +} +} // namespace ChildProcessManagerEts +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.app.ability.childProcessManager.ets b/frameworks/ets/ets/@ohos.app.ability.childProcessManager.ets new file mode 100644 index 00000000000..0da90c002ed --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.childProcessManager.ets @@ -0,0 +1,51 @@ +/* + * 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 { BusinessError, AsyncCallback } from '@ohos.base'; +import AsyncCallbackWrapper from './utils/AbilityUtils'; + +namespace childProcessManager { + loadLibrary("ability_child_process_manager_ani_kit.z") + + export const enum StartMode { + SELF_FORK = 0, + APP_SPAWN_FORK = 1, + } + + export native function nativeStartChildProcess(srcEntry: string, startMode: StartMode, callback: AsyncCallbackWrapper): void; + export function startChildProcess(srcEntry: string, startMode: StartMode): Promise { + let p = new Promise((resolve:(data: number)=>void, reject:(err: BusinessError)=>void):void => { + let myCall = new AsyncCallbackWrapper((err: BusinessError, data: number)=>{ + if (err.code == 0) { + resolve(data); + } else { + reject(err); + } + }); + taskpool.execute((): void => { + childProcessManager.nativeStartChildProcess(srcEntry, startMode, myCall); + }); + }); + return p; + } + export function startChildProcess(srcEntry: string, startMode: StartMode, callback: AsyncCallback): void { + let myCall = new AsyncCallbackWrapper(callback); + taskpool.execute((): void => { + childProcessManager.nativeStartChildProcess(srcEntry, startMode, myCall); + }); + } +} + +export default childProcessManager; \ No newline at end of file diff --git a/frameworks/ets/ets/BUILD.gn b/frameworks/ets/ets/BUILD.gn index 911c61b064a..7fcb54f94ae 100644 --- a/frameworks/ets/ets/BUILD.gn +++ b/frameworks/ets/ets/BUILD.gn @@ -647,6 +647,22 @@ ohos_prebuilt_etc("ability_runtime_app_manager_abc_etc") { deps = [ ":ability_runtime_app_manager_abc" ] } +generate_static_abc("ability_runtime_child_process_manager_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.childProcessManager.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_child_process_manager_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_child_process_manager_abc_etc") { + source = "$target_out_dir/ability_runtime_child_process_manager_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_child_process_manager_abc" ] +} + generate_static_abc("ui_extension_ability_ani_abc") { base_url = "./" @@ -1055,6 +1071,7 @@ group("ets_packages") { ":ability_runtime_ability_state_data_abc_etc", ":ability_runtime_ability_utils_abc_etc", ":ability_runtime_app_manager_abc_etc", + ":ability_runtime_child_process_manager_abc_etc", ":ability_runtime_app_state_data_abc_etc", ":ability_runtime_application_context_abc_etc", ":ability_runtime_application_state_change_callback_abc_etc", -- Gitee