diff --git a/bundle.json b/bundle.json index 9c6c5ca84dbb78a2377e476b3232f7580b5f462b..3785e1b6233cf70a059086fbd57e9d49bf325673 100644 --- a/bundle.json +++ b/bundle.json @@ -61,7 +61,16 @@ ], "inner_kits": [ { - "name": "//foundation/communication/ipc/ipc/native/src/taihe:rpc_taihe_idl" + "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", diff --git a/ipc/native/src/taihe/BUILD.gn b/ipc/native/src/taihe/BUILD.gn index 1c9c51a2da0253b48fc74d7cceb210d6a2c193a1..eebdead5c3bbd4633504415981c7d578c1a470ac 100644 --- a/ipc/native/src/taihe/BUILD.gn +++ b/ipc/native/src/taihe/BUILD.gn @@ -21,6 +21,15 @@ if (support_jsapi) { TAIHE_FILE_PATH = "$taihe_file_path/out/$SUBSYSTEM_NAME/$PART_NAME" SUBSYSTEM_DIR = "//foundation/communication/ipc" +config("taihe_inc_config") { + include_dirs = [ + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core/include", + "$SUBSYSTEM_DIR/ipc/native/src/taihe/inc", + "$SUBSYSTEM_DIR/utils/include", + "$SUBSYSTEM_DIR/ipc/native/src/napi_common/include", + ] +} + copy_taihe_idl("rpc_taihe_idl") { sources = [ "idl/ohos.rpc.rpc.taihe" ] } @@ -39,15 +48,12 @@ 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_inc_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", @@ -85,7 +91,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 4e088d255edc7b2a41510d2793ca85078188e217..2e433a6c3f83756c2522f58c9bca62a317e8ebde 100644 --- a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -180,6 +180,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..7f6efbf2bb489979c4d35a56c71b8736e2e158d1 --- /dev/null +++ b/ipc/native/src/taihe/inc/remote_object_taihe_ani.h @@ -0,0 +1,31 @@ +/* + * 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; +} +class RemoteObjectTaiheAni { +public: + OHOS::sptr nativeObject_; +}; +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..290a661e14c1d055e32c5c24fe03554f42966596 100644 --- a/ipc/native/src/taihe/inc/taihe_remote_proxy.h +++ b/ipc/native/src/taihe/inc/taihe_remote_proxy.h @@ -28,12 +28,13 @@ #include #include "ipc_object_proxy.h" +#include "taihe_deathrecipient.h" namespace OHOS { class RemoteProxyImpl { public: - RemoteProxyImpl(uintptr_t nativePtr); + RemoteProxyImpl(uintptr_t nativePtr, bool bRemoteObj = false); ::ohos::rpc::rpc::IRemoteBroker GetLocalInterface(::taihe::string_view descriptor); @@ -53,8 +54,8 @@ public: void AddJsObjWeakRef(::ohos::rpc::rpc::weak::RemoteProxy obj); static ::ohos::rpc::rpc::RemoteProxy CreateRemoteProxyFromNative(uintptr_t nativePtr); - private: + OHOS::sptr remoteObject_; OHOS::sptr cachedObject_; std::optional<::ohos::rpc::rpc::RemoteProxy> jsObjRef_; std::optional<::ohos::rpc::rpc::IRemoteBroker> jsLocalInterface_; 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 bfbd9e11d22a2b99fd5798dde31acdca78b710bf..f03f975390843497ccd4106c84fc6939af42d052 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -29,6 +29,7 @@ #include "message_option.h" #include "refbase.h" #include "rpc_taihe_error.h" +#include "remote_object_taihe_ani.h" #include "taihe_ani_remote_object.h" #include "taihe_ashmem.h" @@ -199,13 +200,23 @@ int ANIRemoteObject::GetObjectType() const } // RemoteProxyImpl -RemoteProxyImpl::RemoteProxyImpl(uintptr_t nativePtr) +RemoteProxyImpl::RemoteProxyImpl(uintptr_t nativePtr, bool bRemoteObj) { if (reinterpret_cast(nativePtr) == nullptr) { ZLOGE(LOG_LABEL, "nativePtr is null"); TH_THROW(std::runtime_error, "RemoteProxyImpl nativePtr is nullptr"); return; } + if (bRemoteObj) { + auto proxy = reinterpret_cast(nativePtr); + if (proxy == nullptr) { + ZLOGE(LOG_LABEL, "reinterpret_cast nativePtr failed"); + TH_THROW(std::runtime_error, "RemoteProxyImpl reinterpret_cast nativePtr failed"); + return; + } + remoteObject_ = proxy->nativeObject_; + return; + } auto proxy = reinterpret_cast(nativePtr); if (proxy == nullptr) { ZLOGE(LOG_LABEL, "reinterpret_cast nativePtr failed"); @@ -727,6 +738,34 @@ void MessageSequenceImpl::WriteInterfaceToken(::taihe::string_view token) } } +int64_t unwrapRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion const& obj) +{ + if (obj.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::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) { + 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"); + TH_THROW(std::runtime_error, "nativePtr is null"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::ohos::rpc::rpc::IRemoteObjectUnion::make_errRet()); + } + ::ohos::rpc::rpc::RemoteProxy obj = taihe::make_holder(nativePtr, true); + return ::ohos::rpc::rpc::IRemoteObjectUnion::make_remoteProxy(obj); +} + ::taihe::string MessageSequenceImpl::ReadInterfaceToken() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, @@ -1277,7 +1316,8 @@ void MessageOptionImpl::AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageOption ob jsObjRef_ = std::optional<::ohos::rpc::rpc::MessageOption>(std::in_place, obj); } -::ohos::rpc::rpc::MessageOption MessageOptionImpl::CreateMessageOption_WithTwoParam(int32_t syncFlags, int32_t waitTime) +::ohos::rpc::rpc::MessageOption MessageOptionImpl::CreateMessageOption_WithTwoParam(int32_t syncFlags, + int32_t waitTime) { return taihe::make_holder(syncFlags, waitTime); } @@ -1339,4 +1379,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..ba89fbb32ad491a72ba5d60a39fee6434d0f64ef --- /dev/null +++ b/ipc/native/src/taihe/src/remote_object_taihe_ani.cpp @@ -0,0 +1,75 @@ +/* + * 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 "ipc_debug.h" +#include "iremote_object.h" +#include "ipc_object_stub.h" +#include "hilog/log.h" +#include "log_tags.h" +// This file is for legacy ANI backward compatibility + +using namespace OHOS; +using namespace arkts; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_OTHER, "rpc_taihe" }; + +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) { + ZLOGE(LOG_LABEL, "FindNamespace error"); + return nullptr; + } + ani_function createFunc; + if (env->Namespace_FindFunction(imageNamespace, "wrapRemoteObject", nullptr, &createFunc) != ANI_OK) { + ZLOGE(LOG_LABEL, "FindFunction error"); + return nullptr; + } + ani_ref remoteObj; + if (env->Function_Call_Ref(createFunc, &remoteObj, reinterpret_cast(taiheAniobj.get())) == ANI_OK) { + taiheAniobj.release(); + } else { + ZLOGE(LOG_LABEL, "Call_Ref error"); + 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) { + ZLOGE(LOG_LABEL, "FindNamespace error"); + return nullptr; + } + ani_function createFunc; + if (env->Namespace_FindFunction(imageNamespace, "unwrapRemoteObject", nullptr, &createFunc) != ANI_OK) { + ZLOGE(LOG_LABEL, "FindFunction error"); + return nullptr; + } + ani_long implPtr; + if (!(env->Function_Call_Long(createFunc, &implPtr, obj) == ANI_OK)) { + ZLOGE(LOG_LABEL, "Call_Long error"); + return nullptr; + } + return reinterpret_cast(implPtr); +} \ No newline at end of file