From eb7d3b27306f46abd29d33f54ed8e73a17347f5d Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Thu, 22 May 2025 09:20:27 +0800 Subject: [PATCH] ChildProcessManager Signed-off-by: zhangzezhong --- frameworks/ets/ani/BUILD.gn | 1 + .../ets/ani/child_process_manager/BUILD.gn | 66 +++++++ .../include/sts_child_process_manager.h | 26 +++ .../src/sts_child_process_manager.cpp | 172 ++++++++++++++++++ .../ets/@ohos.app.ability.ChildProcess.ets | 20 ++ .../@ohos.app.ability.ChildProcessArgs.ets | 24 +++ .../@ohos.app.ability.ChildProcessOptions.ets | 22 +++ .../@ohos.app.ability.childProcessManager.ets | 47 +++++ frameworks/ets/ets/BUILD.gn | 68 +++++++ .../child_process_manager/child_process.cpp | 3 + .../sts_child_process.cpp | 133 ++++++++++++++ .../inner_api/child_process_manager/BUILD.gn | 3 + .../include/sts_child_process.h | 43 +++++ 13 files changed, 628 insertions(+) create mode 100644 frameworks/ets/ani/child_process_manager/BUILD.gn create mode 100644 frameworks/ets/ani/child_process_manager/include/sts_child_process_manager.h create mode 100644 frameworks/ets/ani/child_process_manager/src/sts_child_process_manager.cpp create mode 100644 frameworks/ets/ets/@ohos.app.ability.ChildProcess.ets create mode 100644 frameworks/ets/ets/@ohos.app.ability.ChildProcessArgs.ets create mode 100644 frameworks/ets/ets/@ohos.app.ability.ChildProcessOptions.ets create mode 100644 frameworks/ets/ets/@ohos.app.ability.childProcessManager.ets create mode 100644 frameworks/native/ability/native/child_process_manager/sts_child_process.cpp create mode 100644 interfaces/inner_api/child_process_manager/include/sts_child_process.h diff --git a/frameworks/ets/ani/BUILD.gn b/frameworks/ets/ani/BUILD.gn index f487ec8a122..be2bcb25ef7 100644 --- a/frameworks/ets/ani/BUILD.gn +++ b/frameworks/ets/ani/BUILD.gn @@ -21,6 +21,7 @@ group("ani_packages") { "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", "${ability_runtime_path}/frameworks/ets/ani/ani_wantagent_common:ani_wantagent_common", "${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..1fdb3a7f9fe --- /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/sts_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" +} diff --git a/frameworks/ets/ani/child_process_manager/include/sts_child_process_manager.h b/frameworks/ets/ani/child_process_manager/include/sts_child_process_manager.h new file mode 100644 index 00000000000..90428210a2b --- /dev/null +++ b/frameworks/ets/ani/child_process_manager/include/sts_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_STS_CHILD_PROCESS_MANAGER_H +#define OHOS_ABILITY_RUNTIME_STS_CHILD_PROCESS_MANAGER_H + +#include "sts_runtime.h" + +namespace OHOS { +namespace ChildProcessManagerSts { +void StsChildProcessManagerRegistryInit(ani_env *env); +} // namespace ChildProcessManagerSts +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_STS_CHILD_PROCESS_MANAGER_H \ No newline at end of file diff --git a/frameworks/ets/ani/child_process_manager/src/sts_child_process_manager.cpp b/frameworks/ets/ani/child_process_manager/src/sts_child_process_manager.cpp new file mode 100644 index 00000000000..6e4ef638da4 --- /dev/null +++ b/frameworks/ets/ani/child_process_manager/src/sts_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 "sts_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 ChildProcessManagerSts { +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 StsChildProcessManager { +public: + StsChildProcessManager() = default; + ~StsChildProcessManager() = default; + + static void StartChildProcess(ani_env *env, ani_string stsSrcEntry, ani_enum_item stsStartMode, + ani_object callback) + { + TAG_LOGD(AAFwkTag::PROCESSMGR, "StartChildProcess 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, stsSrcEntry, 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, "StartChildProcess srcEntry %{public}s", srcEntry.c_str()); + + ani_int startMode; + if (!AAFwk::AniEnumConvertUtil::EnumConvert_StsToNative(env, stsStartMode, 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, "StartChildProcess 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); + } + + TAG_LOGD(AAFwkTag::PROCESSMGR, "StartChildProcess end"); + } + +private: + static void StartChildProcessSelfForkTask(ani_env *env, std::string &stsSrcEntry, ani_object callback) + { + pid_t pid = 0; + AbilityRuntime::ChildProcessManagerErrorCode errorCode = + AbilityRuntime::ChildProcessManager::GetInstance().StartChildProcessBySelfFork(stsSrcEntry, pid); + if (errorCode != AbilityRuntime::ChildProcessManagerErrorCode::ERR_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "StartChildProcessBySelfFork failed, errorCode is %{public}d", errorCode); + return; + } + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::CreateStsErrorByNativeErr(env, static_cast(errorCode)), nullptr); + } + + static void StartChildProcessAppSpawnForkTask(ani_env *env, std::string &stsSrcEntry, ani_object callback) + { + pid_t pid = -1; + AbilityRuntime::ChildProcessManagerErrorCode errorCode = + AbilityRuntime::ChildProcessManager::GetInstance().StartChildProcessByAppSpawnFork(stsSrcEntry, pid); + if (errorCode != AbilityRuntime::ChildProcessManagerErrorCode::ERR_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "StartChildProcessAppSpawnForkTask failed, errorCode is %{public}d", + errorCode); + return; + } + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::CreateStsErrorByNativeErr(env, static_cast(errorCode)), nullptr); + } +}; + +void StsChildProcessManagerRegistryInit(ani_env *env) +{ + TAG_LOGD(AAFwkTag::PROCESSMGR, "StsChildProcessManagerRegistryInit 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;" + "L@ohos/app/ability/childProcessManager/AsyncCallbackWrapper;:V", + reinterpret_cast(StsChildProcessManager::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, "StsChildProcessManagerRegistryInit end"); +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + TAG_LOGD(AAFwkTag::PROCESSMGR, "in ChildProcessManagerSts.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; + } + StsChildProcessManagerRegistryInit(env); + *result = ANI_VERSION_1; + TAG_LOGD(AAFwkTag::PROCESSMGR, "ChildProcessManagerSts.ANI_Constructor finished"); + return ANI_OK; +} +} +} +} \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.app.ability.ChildProcess.ets b/frameworks/ets/ets/@ohos.app.ability.ChildProcess.ets new file mode 100644 index 00000000000..b3973193ad5 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.ChildProcess.ets @@ -0,0 +1,20 @@ +/* + * 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 { ChildProcessArgs } from '@ohos.app.ability.ChildProcessArgs'; + +export default class ChildProcess { + onStart(args?: ChildProcessArgs): void {}; +} \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.app.ability.ChildProcessArgs.ets b/frameworks/ets/ets/@ohos.app.ability.ChildProcessArgs.ets new file mode 100644 index 00000000000..cf9d22a2c39 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.ChildProcessArgs.ets @@ -0,0 +1,24 @@ +/* + * 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 interface ChildProcessArgs { + entryParams?: string; + fds?: Record; +} + +class ChildProcessArgsImpl implements ChildProcessArgs { + entryParams?: string|undefined; + fds?: Record|undefined; +} \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.app.ability.ChildProcessOptions.ets b/frameworks/ets/ets/@ohos.app.ability.ChildProcessOptions.ets new file mode 100644 index 00000000000..bd2d8dd13f5 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.ChildProcessOptions.ets @@ -0,0 +1,22 @@ +/* + * 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 interface ChildProcessOptions { + isolationMode?: boolean; +} + +class ChildProcessOptionsImpl implements ChildProcessOptions { + isolationMode?: boolean|undefined; +} \ 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..2b92a933a85 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.childProcessManager.ets @@ -0,0 +1,47 @@ +/* + * 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'; + +export default 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); + }); + } +} \ No newline at end of file diff --git a/frameworks/ets/ets/BUILD.gn b/frameworks/ets/ets/BUILD.gn index ae9a2fc41a8..491fde5a27e 100644 --- a/frameworks/ets/ets/BUILD.gn +++ b/frameworks/ets/ets/BUILD.gn @@ -580,6 +580,70 @@ ohos_prebuilt_etc("ability_runtime_app_manager_abc_etc") { deps = [ ":ability_runtime_app_manager_abc" ] } +generate_static_abc("ability_runtime_child_process_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.ChildProcess.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_child_process_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_child_process_abc_etc") { + source = "$target_out_dir/ability_runtime_child_process_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_child_process_abc" ] +} + +generate_static_abc("ability_runtime_child_process_args_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.ChildProcessArgs.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_child_process_args_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_child_process_args_abc_etc") { + source = "$target_out_dir/ability_runtime_child_process_args_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_child_process_args_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("ability_runtime_child_process_options_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.ChildProcessOptions.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_child_process_options_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_child_process_options_abc_etc") { + source = "$target_out_dir/ability_runtime_child_process_options_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_child_process_options_abc" ] +} + generate_static_abc("ui_extension_ability_ani_abc") { base_url = "./" @@ -820,6 +884,10 @@ 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_abc_etc", + ":ability_runtime_child_process_args_abc_etc", + ":ability_runtime_child_process_manager_abc_etc", + ":ability_runtime_child_process_options_abc_etc", ":ability_runtime_app_state_data_abc_etc", ":ability_runtime_application_context_abc_etc", ":ability_runtime_base_context_abc_etc", diff --git a/frameworks/native/ability/native/child_process_manager/child_process.cpp b/frameworks/native/ability/native/child_process_manager/child_process.cpp index b2c87b8c0bf..c26c7103212 100644 --- a/frameworks/native/ability/native/child_process_manager/child_process.cpp +++ b/frameworks/native/ability/native/child_process_manager/child_process.cpp @@ -16,6 +16,7 @@ #include "child_process.h" #include "js_child_process.h" +#include "sts_child_process.h" namespace OHOS { namespace AbilityRuntime { @@ -27,6 +28,8 @@ std::shared_ptr ChildProcess::Create(const std::unique_ptrGetLanguage()) { case AbilityRuntime::Runtime::Language::JS: return AbilityRuntime::JsChildProcess::Create(runtime); + case AbilityRuntime::Runtime::Language::STS: + return AbilityRuntime::StsChildProcess::Create(runtime); default: return std::make_shared(); } diff --git a/frameworks/native/ability/native/child_process_manager/sts_child_process.cpp b/frameworks/native/ability/native/child_process_manager/sts_child_process.cpp new file mode 100644 index 00000000000..7cd062bc3e3 --- /dev/null +++ b/frameworks/native/ability/native/child_process_manager/sts_child_process.cpp @@ -0,0 +1,133 @@ +/* + * 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 "sts_child_process.h" + +#include "hilog_tag_wrapper.h" +#include "hitrace_meter.h" + +namespace OHOS { +namespace AbilityRuntime { +std::shared_ptr StsChildProcess::Create(const std::unique_ptr &runtime) +{ + return std::make_shared(static_cast(*runtime)); +} + +StsChildProcess::StsChildProcess(STSRuntime &stsRuntime) : stsRuntime_(stsRuntime) {} + +StsChildProcess::~StsChildProcess() +{ + TAG_LOGI(AAFwkTag::PROCESSMGR, "called"); +} + +void StsChildProcess::ResetEnv(ani_env* env) +{ + if (env) { + env->DescribeError(); + env->ResetError(); + } +} + +bool StsChildProcess::Init(const std::shared_ptr &info) +{ + TAG_LOGI(AAFwkTag::PROCESSMGR, "Init called"); + if (info == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null info"); + return false; + } + bool ret = ChildProcess::Init(info); + if (!ret) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "ChildProcess init failed"); + return false; + } + if (info->srcEntry.empty()) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "Empty info srcEntry"); + return false; + } + std::string srcPath = info->srcEntry; + if (srcPath.rfind(".") != std::string::npos) { + srcPath.erase(srcPath.rfind(".")); + } + srcPath.append(".abc"); + std::string moduleName(info->moduleName); + moduleName.append("::").append(info->name); + + stsChildProcessObj_ = stsRuntime_.LoadModule(moduleName, srcPath, info->hapPath, info->isEsModule, false, ""); + if (stsChildProcessObj_ == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null stsChildProcessObj_"); + return false; + } + return true; +} + +void StsChildProcess::OnStart() +{ + TAG_LOGI(AAFwkTag::PROCESSMGR, "OnStart called"); + ChildProcess::OnStart(); + CallObjectMethod(false, "onStart", ":V"); +} + +void StsChildProcess::OnStart(std::shared_ptr args) +{ + TAG_LOGI(AAFwkTag::PROCESSMGR, "StsChildProcess OnStart called"); + if (!args) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null args"); + return; + } + ChildProcess::OnStart(args); + + CallObjectMethod(false, "onStart", "L@ohos/app/ability/ChildProcessArgs/ChildProcessArgs;:V"); +} + +bool StsChildProcess::CallObjectMethod(bool withResult, const char *name, const char *signature, ...) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, std::string("CallObjectMethod:") + name); + if (stsChildProcessObj_ == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "stsChildProcessObj_ nullptr"); + return false; + } + + auto env = stsRuntime_.GetAniEnv(); + ResetEnv(env); + ani_status status = ANI_OK; + ani_method method = nullptr; + if ((status = env->Class_FindMethod(stsChildProcessObj_->aniCls, name, signature, &method)) != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "status: %{public}d", status); + return false; + } + env->ResetError(); + if (withResult) { + ani_boolean res = 0; + va_list args; + va_start(args, signature); + if ((status = env->Object_CallMethod_Boolean(stsChildProcessObj_->aniObj, method, &res, args)) != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "status: %{public}d", status); + stsRuntime_.HandleUncaughtError(); + } + va_end(args); + return res; + } + va_list args; + va_start(args, signature); + if ((status = env->Object_CallMethod_Void_V(stsChildProcessObj_->aniObj, method, args)) != ANI_OK) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "status: %{public}d", status); + stsRuntime_.HandleUncaughtError(); + return false; + } + va_end(args); + return false; +} +} +} diff --git a/interfaces/inner_api/child_process_manager/BUILD.gn b/interfaces/inner_api/child_process_manager/BUILD.gn index 0e50d438a49..a04483f49d9 100644 --- a/interfaces/inner_api/child_process_manager/BUILD.gn +++ b/interfaces/inner_api/child_process_manager/BUILD.gn @@ -52,6 +52,8 @@ ohos_shared_library("child_process_manager") { "ipc:ipc_core", "napi:ace_napi", "samgr:samgr_proxy", + "runtime_core:ani", + "hitrace:hitrace_meter", ] defines = [] @@ -62,6 +64,7 @@ ohos_shared_library("child_process_manager") { "${ability_runtime_native_path}/ability/native/child_process_manager/child_process_manager.cpp", "${ability_runtime_native_path}/ability/native/child_process_manager/child_process_manager_error_utils.cpp", "${ability_runtime_native_path}/ability/native/child_process_manager/js_child_process.cpp", + "${ability_runtime_native_path}/ability/native/child_process_manager/sts_child_process.cpp", "${ability_runtime_native_path}/ability/native/child_process_manager/native_args_child_process.cpp", "${ability_runtime_native_path}/ability/native/child_process_manager/native_child_ipc_process.cpp", ] diff --git a/interfaces/inner_api/child_process_manager/include/sts_child_process.h b/interfaces/inner_api/child_process_manager/include/sts_child_process.h new file mode 100644 index 00000000000..7193ae968c3 --- /dev/null +++ b/interfaces/inner_api/child_process_manager/include/sts_child_process.h @@ -0,0 +1,43 @@ +/* + * 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_STS_CHILD_PROCESS_H +#define OHOS_ABILITY_RUNTIME_STS_CHILD_PROCESS_H + +#include "child_process.h" +#include "sts_runtime.h" + +namespace OHOS { +namespace AbilityRuntime { +class StsChildProcess : public ChildProcess { +public: + explicit StsChildProcess(STSRuntime &stsRuntime); + ~StsChildProcess() override; + + static void ResetEnv(ani_env* env); + static std::shared_ptr Create(const std::unique_ptr &runtime); + bool Init(const std::shared_ptr &info) override; + void OnStart() override; + void OnStart(std::shared_ptr args) override; + +private: + bool CallObjectMethod(bool withResult, const char* name, const char* signature, ...); + + STSRuntime& stsRuntime_; + std::shared_ptr stsChildProcessObj_; +}; +} +} +#endif // OHOS_ABILITY_RUNTIME_STS_CHILD_PROCESS_H \ No newline at end of file -- Gitee