diff --git a/bundle.json b/bundle.json index 9c6c5ca84dbb78a2377e476b3232f7580b5f462b..ec0ad79b95d975f5b07be0051c883a8e37db643b 100644 --- a/bundle.json +++ b/bundle.json @@ -63,6 +63,15 @@ { "name": "//foundation/communication/ipc/ipc/native/src/taihe:rpc_taihe_idl" }, + { + "name": "//foundation/communication/ipc/ipc/native/src/taihe:rpc_taihe", + "header": { + "header_files": [ + "remote_object_taihe_ani.h" + ], + "header_base": "//foundation/communication/ipc/ipc/native/src/taihe/inc" + } + }, { "name": "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", "header": { diff --git a/ipc/native/src/taihe/BUILD.gn b/ipc/native/src/taihe/BUILD.gn index c196bf7174922284ccb61b69a2010adc1e24e408..1ad9264eb5832961516fc96ca538387172ca9863 100644 --- a/ipc/native/src/taihe/BUILD.gn +++ b/ipc/native/src/taihe/BUILD.gn @@ -20,6 +20,13 @@ if (support_jsapi) { PART_NAME = "ipc" TAIHE_FILE_PATH = "$taihe_file_path/out/$SUBSYSTEM_NAME/$PART_NAME" SUBSYSTEM_DIR = "//foundation/communication/ipc" +config("taihe_include_config") { + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core/include", + "$SUBSYSTEM_DIR/ipc/native/src/taihe/inc", + "$SUBSYSTEM_DIR/utils/include", + ] +} copy_taihe_idl("rpc_taihe_idl") { sources = [ "idl/ohos.rpc.rpc.taihe" ] @@ -39,23 +46,23 @@ if (support_jsapi) { part_name = "$PART_NAME" subsystem_name = "$SUBSYSTEM_NAME" sources = get_target_outputs(":rpc_taihe_generate") - include_dirs = [ - "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core/include", - "$SUBSYSTEM_DIR/ipc/native/src/taihe/inc", - "$SUBSYSTEM_DIR/utils/include", - ] + public_configs = [ ":taihe_include_config" ] sources += [ "src/ani_constructor.cpp", "src/ohos.rpc.rpc.impl.cpp", "src/rpc_taihe_error.cpp", + "src/remote_object_taihe_ani.cpp", ] deps = [ ":rpc_taihe_generate", - "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core:ipc_core" + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core:ipc_core", + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_napi_common:ipc_napi", ] external_deps = [ "c_utils:utils", "hilog:libhilog", + "napi:ace_napi", + "runtime_core:ani_helpers", ] } @@ -82,7 +89,17 @@ if (support_jsapi) { ] } } else { + SUBSYSTEM_DIR = "//foundation/communication/ipc" + ohos_shared_library("rpc_taihe") { + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core/include", + "$SUBSYSTEM_DIR/ipc/native/src/taihe/inc", + "$SUBSYSTEM_DIR/utils/include", + ] + } group("rpc_taihe_components") { - deps = [] + deps = [ + ":rpc_taihe" + ] } } \ No newline at end of file diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe index 9a1c7e52308277cfc76237b3f8ab3dc4d602993e..2184ff5b0f03961560fd4785b9f2cc4f29bcabff 100644 --- a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -163,6 +163,10 @@ function CreateMessageSequence(): MessageSequence; @static("MessageSequence") function CloseFileDescriptor(fd: i32): void; +function unwrapRemoteObject(obj: IRemoteObjectUnion): i64; + +function wrapRemoteObject(nativePtr: i64): IRemoteObjectUnion; + interface IRemoteObject { GetLocalInterface(descriptor: String): IRemoteBroker; diff --git a/ipc/native/src/taihe/inc/remote_object_taihe_ani.h b/ipc/native/src/taihe/inc/remote_object_taihe_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..b4e83837de2b411c8c6b2eee68d3cbdfa7ca3eb6 --- /dev/null +++ b/ipc/native/src/taihe/inc/remote_object_taihe_ani.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 REMOTE_OBJECT_TAIHE_ANI_H +#define REMOTE_OBJECT_TAIHE_ANI_H + +#include +#include + +namespace OHOS { + class IRemoteObject; +} +OHOS::sptr AniGetNativeRemoteObject(ani_env *env, ani_object obj); +ani_object ANI_ohos_rpc_CreateJsRemoteObject(ani_env *env, OHOS::sptr remoteObject); +#endif // REMOTE_OBJECT_TAIHE_ANI_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_remote_proxy.h b/ipc/native/src/taihe/inc/taihe_remote_proxy.h index 9ec46e09605ddb2f27fe273999040c3d044976f0..c13ee7e056b670f0c6d7eb8922dee06de25cc78f 100644 --- a/ipc/native/src/taihe/inc/taihe_remote_proxy.h +++ b/ipc/native/src/taihe/inc/taihe_remote_proxy.h @@ -28,6 +28,7 @@ #include #include "ipc_object_proxy.h" +#include "taihe_deathrecipient.h" namespace OHOS { diff --git a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp index 8d3ea8e5882f56d8fcb71e70db5291f5d70391e6..30a1ab02fa7200b1617524a2e45341c4f8f005d8 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -590,6 +590,34 @@ void MessageSequenceImpl::WriteRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion } } +int64_t unwrapRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion const& obj) +{ + if (obj.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteObject) { + ZLOGD(LOG_LABEL, "reinterpret_cast remoteObject"); + auto &remoteStub = obj.get_remoteObject_ref(); + int64_t objectptr = remoteStub->GetNativePtr(); + return objectptr; + } + if (obj.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteProxy) { + ZLOGD(LOG_LABEL, "reinterpret_cast remoteProxy"); + auto &remoteProxy = obj.get_remoteProxy_ref(); + int64_t proxyptr = remoteProxy->GetNativePtr(); + return proxyptr; + } + return 0; +} + +::ohos::rpc::rpc::IRemoteObjectUnion wrapRemoteObject(int64_t nativePtr) +{ + if (reinterpret_cast(nativePtr) == nullptr) { + ZLOGE(LOG_LABEL, "nativePtr is nullptr"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::ohos::rpc::rpc::IRemoteObjectUnion::make_errRet()); + } + auto jsProxy = RemoteProxyImpl::CreateRemoteProxyFromNative(nativePtr); + return ::ohos::rpc::rpc::IRemoteObjectUnion::make_remoteProxy(jsProxy); +} + void MessageSequenceImpl::WriteInterfaceToken(::taihe::string_view token) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); @@ -1215,4 +1243,6 @@ TH_EXPORT_CPP_API_GetCallingPid(OHOS::IPCSkeletonImpl::GetCallingPid); TH_EXPORT_CPP_API_GetCallingUid(OHOS::IPCSkeletonImpl::GetCallingUid); TH_EXPORT_CPP_API_GetCallingTokenId(OHOS::IPCSkeletonImpl::GetCallingTokenId); TH_EXPORT_CPP_API_GetContextObject(OHOS::IPCSkeletonImpl::GetContextObject); +TH_EXPORT_CPP_API_unwrapRemoteObject(OHOS::unwrapRemoteObject); +TH_EXPORT_CPP_API_wrapRemoteObject(OHOS::wrapRemoteObject); // NOLINTEND \ No newline at end of file diff --git a/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp b/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d39c20d82b2e5b55efc50365469b2177e797d5c7 --- /dev/null +++ b/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp @@ -0,0 +1,78 @@ +/* + * 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 "remote_object_taihe_ani.h" +#include +#include "hilog/log.h" +#include "ipc_debug.h" +#include "log_tags.h" +#include "taihe_remote_proxy.h" +// This file is for legacy ANI backward compatibility + +using namespace OHOS; +using namespace arkts; + +class RemoteObjectTaiheAni { +public: + OHOS::sptr nativeObject_; +}; + +ani_object ANI_ohos_rpc_CreateJsRemoteObject(ani_env* env, OHOS::sptr remoteObject) +{ + std::unique_ptr taiheAniobj = std::make_unique(); + taiheAniobj->nativeObject_ = remoteObject; + + ani_signature::Namespace ns = ani_signature::Builder::BuildNamespace("@ohos.rpc.rpc"); + ani_namespace imageNamespace; + if (env->FindNamespace(ns.Descriptor().c_str(), &imageNamespace) != ANI_OK) { + return nullptr; + } + ani_function createFunc; + if (env->Namespace_FindFunction(imageNamespace, "wrapRemoteObject", nullptr, &createFunc) != ANI_OK) { + return nullptr; + } + ani_ref remoteObj; + if (env->Function_Call_Ref(createFunc, &remoteObj, reinterpret_cast(taiheAniobj.get())) == ANI_OK) { + taiheAniobj.release(); + } else { + return nullptr; + } + return reinterpret_cast(remoteObj); +} + + +OHOS::sptr AniGetNativeRemoteObject(ani_env* env, ani_object obj) +{ + ani_signature::Namespace ns = ani_signature::Builder::BuildNamespace("@ohos.rpc.rpc"); + ani_namespace imageNamespace; + if (env->FindNamespace(ns.Descriptor().c_str(), &imageNamespace) != ANI_OK) { + return nullptr; + } + ani_function createFunc; + if (env->Namespace_FindFunction(imageNamespace, "unwrapRemoteObject", nullptr, &createFunc) != ANI_OK) { + return nullptr; + } + ani_long implPtr; + if (!(env->Function_Call_Long(createFunc, &implPtr, obj) == ANI_OK)) { + return nullptr; + } + IRemoteObject* remoteObject = reinterpret_cast(implPtr); + if (remoteObject == nullptr) { + return nullptr; + } + OHOS::sptr nativeStub = + reinterpret_cast(remoteObject); + return nativeStub; +} \ No newline at end of file