From 3eb39ef8a67ba92003f1b56feb5428b05ba2523d Mon Sep 17 00:00:00 2001 From: hanlin15 Date: Mon, 30 Jun 2025 09:36:42 +0800 Subject: [PATCH] arkts1.2_head Signed-off-by: hanlin15 Change-Id: I782ace18b4cf9227ee51b6b87c247ae99b6d19ec --- ipc/native/src/taihe/BUILD.gn | 82 ++++++ ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe | 248 ++++++++++++++++++ ipc/native/src/taihe/inc/rpc_taihe_error.h | 85 ++++++ .../src/taihe/inc/taihe_ani_remote_object.h | 52 ++++ ipc/native/src/taihe/inc/taihe_ashmem.h | 60 +++++ .../src/taihe/inc/taihe_deathrecipient.h | 49 ++++ ipc/native/src/taihe/inc/taihe_ipc_skeleton.h | 45 ++++ .../src/taihe/inc/taihe_iremote_broker.h | 39 +++ .../src/taihe/inc/taihe_message_option.h | 57 ++++ .../src/taihe/inc/taihe_message_sequence.h | 128 +++++++++ ipc/native/src/taihe/inc/taihe_parcelable.h | 40 +++ .../src/taihe/inc/taihe_remote_object.h | 86 ++++++ ipc/native/src/taihe/inc/taihe_remote_proxy.h | 65 +++++ ipc/native/src/taihe/src/ani_constructor.cpp | 37 +++ ipc/native/src/taihe/src/rpc_taihe_error.cpp | 28 ++ 15 files changed, 1101 insertions(+) create mode 100644 ipc/native/src/taihe/BUILD.gn create mode 100644 ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe create mode 100644 ipc/native/src/taihe/inc/rpc_taihe_error.h create mode 100644 ipc/native/src/taihe/inc/taihe_ani_remote_object.h create mode 100644 ipc/native/src/taihe/inc/taihe_ashmem.h create mode 100644 ipc/native/src/taihe/inc/taihe_deathrecipient.h create mode 100644 ipc/native/src/taihe/inc/taihe_ipc_skeleton.h create mode 100644 ipc/native/src/taihe/inc/taihe_iremote_broker.h create mode 100644 ipc/native/src/taihe/inc/taihe_message_option.h create mode 100644 ipc/native/src/taihe/inc/taihe_message_sequence.h create mode 100644 ipc/native/src/taihe/inc/taihe_parcelable.h create mode 100644 ipc/native/src/taihe/inc/taihe_remote_object.h create mode 100644 ipc/native/src/taihe/inc/taihe_remote_proxy.h create mode 100644 ipc/native/src/taihe/src/ani_constructor.cpp create mode 100644 ipc/native/src/taihe/src/rpc_taihe_error.cpp diff --git a/ipc/native/src/taihe/BUILD.gn b/ipc/native/src/taihe/BUILD.gn new file mode 100644 index 00000000..a81cbd77 --- /dev/null +++ b/ipc/native/src/taihe/BUILD.gn @@ -0,0 +1,82 @@ +# 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("//build/ohos/taihe_idl/taihe.gni") + +SUBSYSTEM_NAME = "communication" +PART_NAME = "ipc" +TAIHE_FILE_PATH = "$taihe_file_path/out/$SUBSYSTEM_NAME/$PART_NAME" +SUBSYSTEM_DIR = "//foundation/communication/ipc" + +copy_taihe_idl("rpc_taihe_idl") { + sources = [ "idl/ohos.rpc.rpc.taihe" ] +} + +ohos_taihe("rpc_taihe_generate") { + taihe_generated_file_path = "$TAIHE_FILE_PATH" + deps = [ ":rpc_taihe_idl" ] + outputs = [ + "$TAIHE_FILE_PATH/src/ohos.rpc.rpc.ani.cpp", + "$TAIHE_FILE_PATH/src/ohos.rpc.rpc.abi.c", + ] +} + +taihe_shared_library("rpc_taihe") { + taihe_generated_file_path = "$TAIHE_FILE_PATH" + 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", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.rpc.rpc.impl.cpp", + "src/rpc_taihe_error.cpp", + ] + deps = [ + ":rpc_taihe_generate", + "$SUBSYSTEM_DIR/interfaces/innerkits/ipc_core:ipc_core" + ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] +} + +generate_static_abc("rpc_taihe_abc") { + base_url = "$TAIHE_FILE_PATH" + files = [ "$TAIHE_FILE_PATH/@ohos.rpc.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/rpc_taihe_abc.abc" + dependencies = [ ":rpc_taihe_generate" ] +} + +ohos_prebuilt_etc("rpc_taihe_etc") { + source = "$target_out_dir/rpc_taihe_abc.abc" + module_install_dir = "framework" + part_name = "$PART_NAME" + subsystem_name = "$SUBSYSTEM_NAME" + deps = [ ":rpc_taihe_abc" ] +} + +group("rpc_taihe_components") { + deps = [ + ":rpc_taihe_etc", + ":rpc_taihe", + ] +} diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe new file mode 100644 index 00000000..e9d9d61f --- /dev/null +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -0,0 +1,248 @@ +/* + * 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. + */ + +@!namespace("@ohos.rpc", "rpc") + +@!sts_inject(""" +static { loadLibrary("rpc_taihe.z"); } +""") + +union IRemoteObjectUnion { + remoteProxy: RemoteProxy; + remoteObject: RemoteObject; + iRemoteObject: IRemoteObject; + @undefined errRet; +} + +interface IRemoteBroker { + AsObject(): IRemoteObjectUnion; +} + +interface Parcelable { + Marshalling(dataOut: MessageSequence): bool; + Unmarshalling(dataIn: MessageSequence): bool; +} + +interface DeathRecipient { + OnRemoteDied(): void; +} + +struct RequestResult { + errCode: i32; + code: i32; + data: MessageSequence; + reply: MessageSequence; +} + +@class +interface Ashmem { + MapReadWriteAshmem(): void; + GetNativePtr(): i64; + GetAshmemSize(): i32; +} + +@static("Ashmem") +@overload("create") +function CreateAshmem_WithTwoParam(name: String, size: i32): Ashmem; + +@static("Ashmem") +@overload("create") +function CreateAshmem_WithOneParam(ashmem: Ashmem): Ashmem; + + +@class +interface MessageSequence { + Reclaim(): void; + + WriteRemoteObject(object: IRemoteObjectUnion): void; + ReadRemoteObject(): IRemoteObjectUnion; + + WriteInterfaceToken(token: String): void; + ReadInterfaceToken(): String; + + GetCapacity(): i32; + SetCapacity(size: i32): void; + + WriteNoException(): void; + ReadException(): void; + + WriteInt(val: i32): void; + WriteLong(val: i64): void; + WriteBoolean(val: bool): void; + WriteString(val: String): void; + WriteParcelable(val: Parcelable): void; + + WriteByteArray(byteArray: Array): void; + WriteIntArray(intArray: Array): void; + WriteDoubleArray(doubleArray: Array): void; + WriteBooleanArray(booleanArray: Array): void; + WriteStringArray(stringArray: Array): void; + WriteParcelableArray(parcelableArray: Array): void; + + ReadInt(): i32; + ReadLong(): i64; + ReadBoolean(): bool; + ReadString(): String; + ReadParcelable(dataIn: Parcelable): void; + + @overload("readIntArray") + ReadIntArrayImpl(): Array; + @!sts_inject_into_class(""" + readIntArray(dataIn: Array): void { + let res: Array = this.readIntArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readDoubleArray") + ReadDoubleArrayImpl(): Array; + @!sts_inject_into_class(""" + readDoubleArray(dataIn: Array): void { + let res: Array = this.readDoubleArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readBooleanArray") + ReadBooleanArrayImpl(): Array; + @!sts_inject_into_class(""" + readBooleanArray(dataIn: Array): void { + let res: Array = this.readBooleanArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readStringArray") + ReadStringArrayImpl(): Array; + @!sts_inject_into_class(""" + readStringArray(dataIn: Array): void { + let res: Array = this.readStringArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + ReadParcelableArray(parcelableArray: Array): void; + + WriteFileDescriptor(fd: i32): void; + ReadFileDescriptor(): i32; + + WriteAshmem(ashmem: Ashmem): void; + ReadAshmem(): Ashmem; + + WriteRawDataBuffer(rawData: @arraybuffer Array, size: i32): void; + ReadRawDataBuffer(size: i32): @arraybuffer Array; + + GetNativePtr(): i64; + AddJsObjWeakRef(obj: MessageSequence): void; +} + +@static("MessageSequence") +@overload("create") +function CreateMessageSequence(): MessageSequence; + +@static("MessageSequence") +function CloseFileDescriptor(fd: i32): void; + +interface IRemoteObject { + GetLocalInterface(descriptor: String): IRemoteBroker; + + @gen_promise("sendMessageRequest") + @gen_async("sendMessageRequest") + SendMessageRequestSync(code: i32, data: MessageSequence, reply: MessageSequence, options: MessageOption): + RequestResult; + + RegisterDeathRecipient(recipient: DeathRecipient, flags: i32): void; + UnregisterDeathRecipient(recipient: DeathRecipient, flags: i32): void; + + GetDescriptor(): String; + IsObjectDead(): bool; +} + +@class +interface RemoteObject : IRemoteObject { + + @!sts_inject_into_class(""" + constructor(descriptor: String) { + this(this, descriptor); + } + """) + + //@gen_promise("onRemoteMessageRequest") + OnRemoteMessageRequest(code: i32, data: MessageSequence, reply: MessageSequence, options: MessageOption): bool; + + GetCallingPid(): i32; + GetCallingUid(): i32; + + ModifyLocalInterface(localInterface: IRemoteBroker, descriptor: String): void; + + GetNativePtr(): i64; + AddJsObjWeakRef(obj: RemoteObject, isNative: bool): void; +} + +@ctor("RemoteObject") +function CreateRemoteObject(self: RemoteObject, descriptor: String): RemoteObject; + +@ctor("RemoteObject") +function CreateRemoteObjectFromNative(nativePtr: i64): RemoteObject; + +@class +interface RemoteProxy : IRemoteObject { + GetNativePtr(): i64; + AddJsObjWeakRef(obj: RemoteProxy): void; +} + +@ctor("RemoteProxy") +function CreateRemoteProxyFromNative(nativePtr: Opaque): RemoteProxy; + +@class +interface MessageOption { + IsAsync(): bool; + SetAsync(isAsync: bool): void; + GetNativePtr(): i64; +} + +@ctor("MessageOption") +function CreateMessageOption_WithTwoParam(syncFlags: i32, waitTime: i32): MessageOption; + +@ctor("MessageOption") +function CreateMessageOption_WithOneParam(isAsync: bool): MessageOption; + +@ctor("MessageOption") +function CreateMessageOption(): MessageOption; + + +@class +interface IPCSkeleton { +} + +@static("IPCSkeleton") +function GetCallingPid(): i32; + +@static("IPCSkeleton") +function GetCallingUid(): i32; + +@static("IPCSkeleton") +function GetCallingTokenId(): u32; + +@static("IPCSkeleton") +function GetContextObject(): IRemoteObjectUnion; \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/rpc_taihe_error.h b/ipc/native/src/taihe/inc/rpc_taihe_error.h new file mode 100644 index 00000000..f1f248d9 --- /dev/null +++ b/ipc/native/src/taihe/inc/rpc_taihe_error.h @@ -0,0 +1,85 @@ +/* + * 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_TAIHE_ERROR_H +#define OHOS_TAIHE_ERROR_H + +#include +#include + +namespace OHOS { +enum RpcTaiheErrorCode { + CHECK_PARAM_ERROR, + OS_MMAP_ERROR, + OS_IOCTL_ERROR, + WRITE_TO_ASHMEM_ERROR, + READ_FROM_ASHMEM_ERROR, + ONLY_PROXY_OBJECT_PERMITTED_ERROR, + ONLY_REMOTE_OBJECT_PERMITTED_ERROR, + COMMUNICATION_ERROR, + PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, + WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, + READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + PARCEL_MEMORY_ALLOC_ERROR, + CALL_JS_METHOD_ERROR, + OS_DUP_ERROR +}; + +struct RpcTaiheErrorInfo { + int errorCode; + std::string errorMsg; +}; + +#define RPC_TAIHE_ERROR(error_code) \ + do { \ + taihe::set_business_error( \ + OHOS::RpcTaiheError::Convert(error_code), \ + OHOS::RpcTaiheError::ToMessage(error_code)); \ + return; \ + } while (0) + +#define RPC_TAIHE_ERROR_WITH_RETVAL(error_code, retVal) \ + do { \ + taihe::set_business_error( \ + OHOS::RpcTaiheError::Convert(error_code), \ + OHOS::RpcTaiheError::ToMessage(error_code)); \ + return retVal; \ + } while (0) + +const std::map RPC_TAIHE_ERR_MAP { + { CHECK_PARAM_ERROR, { 401, "check param error" } }, + { OS_MMAP_ERROR, { 1900001, "os mmap function failed" } }, + { OS_IOCTL_ERROR, { 1900002, "os ioctl function failed" } }, + { WRITE_TO_ASHMEM_ERROR, { 1900003, "write to ashmem failed" } }, + { READ_FROM_ASHMEM_ERROR, { 1900004, "read from ashmem failed" } }, + { ONLY_PROXY_OBJECT_PERMITTED_ERROR, { 1900005, "only proxy object permitted" } }, + { ONLY_REMOTE_OBJECT_PERMITTED_ERROR, { 1900006, "only remote object permitted" } }, + { COMMUNICATION_ERROR, { 1900007, "communication failed" } }, + { PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, { 1900008, "proxy or remote object is invalid" } }, + { WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, { 1900009, "write data to message sequence failed" } }, + { READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, { 1900010, "read data from message sequence failed" } }, + { PARCEL_MEMORY_ALLOC_ERROR, { 1900011, "parcel memory alloc failed" } }, + { CALL_JS_METHOD_ERROR, { 1900012, "call js method failed" } }, + { OS_DUP_ERROR, { 1900013, "os dup function failed" } } +}; + +class RpcTaiheError { +public: + static int32_t Convert(const int errCode); + static std::string ToMessage(const int errCode); +}; +} + +#endif // OHOS_TAIHE_ERROR_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_ani_remote_object.h b/ipc/native/src/taihe/inc/taihe_ani_remote_object.h new file mode 100644 index 00000000..011b2486 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_ani_remote_object.h @@ -0,0 +1,52 @@ +/* + * 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_IPC_TAIHE_ANI_REMOTE_OBJECT_H +#define OHOS_IPC_TAIHE_ANI_REMOTE_OBJECT_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "message_parcel.h" +#include "ipc_object_stub.h" + +namespace OHOS { + +class ANIRemoteObject : public OHOS::IPCObjectStub { +public: + ANIRemoteObject(const std::u16string &descriptor, ::ohos::rpc::rpc::weak::RemoteObject jsObj); + ~ANIRemoteObject(); + + int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, + OHOS::MessageOption &option) override; + + int GetObjectType() const override; + + ::ohos::rpc::rpc::RemoteObject GetJsObject(); + +private: + std::optional<::ohos::rpc::rpc::RemoteObject> jsObjRef_; +}; +} // namespace + +#endif // OHOS_IPC_TAIHE_ANI_REMOTE_OBJECT_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_ashmem.h b/ipc/native/src/taihe/inc/taihe_ashmem.h new file mode 100644 index 00000000..851f3645 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_ashmem.h @@ -0,0 +1,60 @@ +/* + * 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_IPC_TAIHE_ASHMEM_H +#define OHOS_IPC_TAIHE_ASHMEM_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "ashmem.h" + +namespace OHOS { + +class AshmemImpl { +public: + // only be used for returning invalid Ashmem. + explicit AshmemImpl(); + + explicit AshmemImpl(const char *name, int32_t size); + + explicit AshmemImpl(OHOS::sptr ashmem); + + int64_t GetNativePtr(); + + void MapReadWriteAshmem(); + + int32_t GetAshmemSize(); + + OHOS::sptr GetAshmem(); + + static ::ohos::rpc::rpc::Ashmem CreateAshmem_WithTwoParam(::taihe::string_view name, int32_t size); + static ::ohos::rpc::rpc::Ashmem CreateAshmem_WithOneParam(::ohos::rpc::rpc::weak::Ashmem ashmem); + +private: + OHOS::sptr ashmem_ = nullptr; +}; + +} // namespace + +#endif // OHOS_IPC_TAIHE_ASHMEM_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_deathrecipient.h b/ipc/native/src/taihe/inc/taihe_deathrecipient.h new file mode 100644 index 00000000..9ce31a55 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_deathrecipient.h @@ -0,0 +1,49 @@ +/* + * 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_IPC_TAIHE_DEATHRECIPIENT_H +#define OHOS_IPC_TAIHE_DEATHRECIPIENT_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "iremote_object.h" + +namespace OHOS { +class DeathRecipientImpl : public OHOS::IRemoteObject::DeathRecipient { +public: + DeathRecipientImpl(::ohos::rpc::rpc::DeathRecipient jsObjRef); + + void OnRemoteDied(const OHOS::wptr &object) override; + + int64_t GetNativePtr(); + + void AddJsObjWeakRef(::ohos::rpc::rpc::weak::DeathRecipient obj); + + static ::ohos::rpc::rpc::DeathRecipient CreateDeathRecipient(); + + ::ohos::rpc::rpc::DeathRecipient jsObjRef_; +}; +} // namespace + +#endif // OHOS_IPC_TAIHE_DEATHRECIPIENT_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h b/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h new file mode 100644 index 00000000..c7feff98 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h @@ -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. + */ + +#ifndef OHOS_IPC_TAIHE_IPC_SKELETON_H +#define OHOS_IPC_TAIHE_IPC_SKELETON_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "ipc_skeleton.h" + +namespace OHOS { + +class IPCSkeletonImpl { +public: + static int32_t GetCallingPid(); + static int32_t GetCallingUid(); + static uint32_t GetCallingTokenId(); + + static ::ohos::rpc::rpc::IRemoteObjectUnion GetContextObject(); +}; + +} // namespace + +#endif // OHOS_IPC_TAIHE_IPC_SKELETON_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_iremote_broker.h b/ipc/native/src/taihe/inc/taihe_iremote_broker.h new file mode 100644 index 00000000..e2d1bb98 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_iremote_broker.h @@ -0,0 +1,39 @@ +/* + * 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_IPC_TAIHE_IREMOTE_BROKER_H +#define OHOS_IPC_TAIHE_IREMOTE_BROKER_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +namespace OHOS { + +class IRemoteBrokerImpl { +public: + ::ohos::rpc::rpc::IRemoteObjectUnion AsObject(); +}; + +} // namespace + +#endif // OHOS_IPC_TAIHE_IREMOTE_BROKER_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_message_option.h b/ipc/native/src/taihe/inc/taihe_message_option.h new file mode 100644 index 00000000..0d204266 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_message_option.h @@ -0,0 +1,57 @@ +/* + * 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_IPC_TAIHE_MESSAGE_OPTION_H +#define OHOS_IPC_TAIHE_MESSAGE_OPTION_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "message_option.h" + +namespace OHOS { + +class MessageOptionImpl { +public: + MessageOptionImpl(int32_t syncFlags, int32_t waitTime); + + bool IsAsync(); + + void SetAsync(bool isAsync); + + int64_t GetNativePtr(); + + void AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageOption obj); + + static ::ohos::rpc::rpc::MessageOption CreateMessageOption_WithTwoParam(int32_t syncFlags, int32_t waitTime); + static ::ohos::rpc::rpc::MessageOption CreateMessageOption_WithOneParam(bool isAsync); + static ::ohos::rpc::rpc::MessageOption CreateMessageOption(); + +private: + std::shared_ptr messageOption_ = nullptr; + std::optional<::ohos::rpc::rpc::weak::MessageOption> jsObjRef_; +}; + +} // namespace + +#endif // OHOS_IPC_TAIHE_MESSAGE_OPTION_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_message_sequence.h b/ipc/native/src/taihe/inc/taihe_message_sequence.h new file mode 100644 index 00000000..5866e363 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_message_sequence.h @@ -0,0 +1,128 @@ +/* + * 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_IPC_TAIHE_MESSAGE_SEQUENCE_H +#define OHOS_IPC_TAIHE_MESSAGE_SEQUENCE_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "ipc_skeleton.h" +#include "message_parcel.h" + +namespace OHOS { +class MessageSequenceImpl { +public: + MessageSequenceImpl(); + + MessageSequenceImpl(OHOS::MessageParcel* messageparcel); + + ~MessageSequenceImpl(); + + void Reclaim(); + + void WriteRemoteObject(::ohos::rpc::rpc::IRemoteObjectUnion const& object); + + ::ohos::rpc::rpc::IRemoteObjectUnion ReadRemoteObject(); + + void WriteInterfaceToken(::taihe::string_view token); + + ::taihe::string ReadInterfaceToken(); + + int32_t GetCapacity(); + + void SetCapacity(int32_t size); + + void WriteNoException(); + + void ReadException(); + + void WriteInt(int32_t val); + + void WriteLong(int64_t val); + + void WriteBoolean(bool val); + + void WriteString(::taihe::string_view val); + + void WriteParcelable(::ohos::rpc::rpc::weak::Parcelable val); + + void WriteByteArray(::taihe::array_view byteArray); + + void WriteIntArray(::taihe::array_view intArray); + + void WriteDoubleArray(::taihe::array_view doubleArray); + + void WriteBooleanArray(::taihe::array_view booleanArray); + + void WriteStringArray(::taihe::array_view<::taihe::string> stringArray); + + void WriteParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray); + + int32_t ReadInt(); + + int64_t ReadLong(); + + bool ReadBoolean(); + + ::taihe::string ReadString(); + + void ReadParcelable(::ohos::rpc::rpc::weak::Parcelable dataIn); + + ::taihe::array ReadIntArrayImpl(); + + ::taihe::array ReadDoubleArrayImpl(); + + ::taihe::array ReadBooleanArrayImpl(); + + ::taihe::array<::taihe::string> ReadStringArrayImpl(); + + void ReadParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray); + + void WriteFileDescriptor(int32_t fd); + + int32_t ReadFileDescriptor(); + + void WriteAshmem(::ohos::rpc::rpc::weak::Ashmem ashmem); + + ::ohos::rpc::rpc::Ashmem ReadAshmem(); + + void WriteRawDataBuffer(::taihe::array_view rawData, int32_t size); + + ::taihe::array ReadRawDataBuffer(int32_t size); + + int64_t GetNativePtr(); + + void AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageSequence obj); + + static ::ohos::rpc::rpc::MessageSequence CreateMessageSequence(); + static void CloseFileDescriptor(int32_t fd); + +private: + OHOS::MessageParcel* nativeParcel_ = nullptr; + std::optional<::ohos::rpc::rpc::weak::MessageSequence> jsObjRef_; + bool isOwner_ = false; +}; +} // namespace + +#endif // OHOS_IPC_TAIHE_MESSAGE_SEQUENCE_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_parcelable.h b/ipc/native/src/taihe/inc/taihe_parcelable.h new file mode 100644 index 00000000..d2aeee6f --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_parcelable.h @@ -0,0 +1,40 @@ +/* + * 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_IPC_TAIHE_PARCELABLE_H +#define OHOS_IPC_TAIHE_PARCELABLE_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +namespace OHOS { +class ParcelableImpl { +public: + bool Marshalling(::ohos::rpc::rpc::weak::MessageSequence dataOut); + + bool Unmarshalling(::ohos::rpc::rpc::weak::MessageSequence dataIn); +}; + +} // namespace + +#endif // OHOS_IPC_TAIHE_PARCELABLE_H \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_remote_object.h b/ipc/native/src/taihe/inc/taihe_remote_object.h new file mode 100644 index 00000000..f3fbad6c --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_remote_object.h @@ -0,0 +1,86 @@ +/* + * 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_IPC_TAIHE_REMOTEOBJECT_H +#define OHOS_IPC_TAIHE_REMOTEOBJECT_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +#include +#include +#include +#include +#include + +#include "ipc_object_stub.h" +#include "taihe_ani_remote_object.h" + +namespace OHOS { + +class RemoteObjectImpl { +public: + // ETS to ANI + explicit RemoteObjectImpl(::taihe::string_view descriptor); + + // ANI to ETS + explicit RemoteObjectImpl(uintptr_t nativePtr); + + int32_t GetCallingPid(); + + int32_t GetCallingUid(); + + void ModifyLocalInterface(::ohos::rpc::rpc::weak::IRemoteBroker localInterface, ::taihe::string_view descriptor); + + ::ohos::rpc::rpc::IRemoteBroker GetLocalInterface(::taihe::string_view descriptor); + + ::ohos::rpc::rpc::RequestResult SendMessageRequestSync(int32_t code, ::ohos::rpc::rpc::weak::MessageSequence data, + ::ohos::rpc::rpc::weak::MessageSequence reply, ::ohos::rpc::rpc::weak::MessageOption options); + + void RegisterDeathRecipient(::ohos::rpc::rpc::weak::DeathRecipient recipient, int32_t flags); + + void UnregisterDeathRecipient(::ohos::rpc::rpc::weak::DeathRecipient recipient, int32_t flags); + + ::taihe::string GetDescriptor(); + + bool IsObjectDead(); + + bool OnRemoteMessageRequest(int32_t code, ::ohos::rpc::rpc::weak::MessageSequence data, + ::ohos::rpc::rpc::weak::MessageSequence reply, ::ohos::rpc::rpc::weak::MessageOption options); + + OHOS::sptr GetNativeObject(); + + int64_t GetNativePtr(); + + void AddJsObjWeakRef(::ohos::rpc::rpc::weak::RemoteObject obj, bool isNative); + + static ::ohos::rpc::rpc::RemoteObject CreateRemoteObject(::ohos::rpc::rpc::weak::RemoteObject jsSelf, + ::taihe::string_view descriptor); + static ::ohos::rpc::rpc::RemoteObject CreateRemoteObjectFromNative(uintptr_t nativePtr); + +private: + std::mutex mutex_; + OHOS::wptr wptrCachedObject_; + OHOS::sptr sptrCachedObject_; + ::taihe::string desc_; + std::optional<::ohos::rpc::rpc::RemoteObject> jsObjRef_; + std::optional<::ohos::rpc::rpc::IRemoteBroker> jsLocalInterface_; +}; + +} // namespace + +#endif // OHOS_IPC_TAIHE_REMOTEOBJECT_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 new file mode 100644 index 00000000..af0b7316 --- /dev/null +++ b/ipc/native/src/taihe/inc/taihe_remote_proxy.h @@ -0,0 +1,65 @@ +/* + * 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_IPC_TAIHE_REMOTEPROXY_H +#define OHOS_IPC_TAIHE_REMOTEPROXY_H + +#include "ohos.rpc.rpc.proj.hpp" +#include "ohos.rpc.rpc.impl.hpp" +#include "taihe/runtime.hpp" +#include "taihe/map.hpp" +#include "stdexcept" + +#include +#include +#include +#include + +#include "ipc_object_proxy.h" + +namespace OHOS { + +class RemoteProxyImpl { +public: + RemoteProxyImpl(uintptr_t nativePtr); + + ::ohos::rpc::rpc::IRemoteBroker GetLocalInterface(::taihe::string_view descriptor); + + ::ohos::rpc::rpc::RequestResult SendMessageRequestSync(int32_t code, ::ohos::rpc::rpc::weak::MessageSequence data, + ::ohos::rpc::rpc::weak::MessageSequence reply, ::ohos::rpc::rpc::weak::MessageOption options); + + void RegisterDeathRecipient(::ohos::rpc::rpc::weak::DeathRecipient recipient, int32_t flags); + + void UnregisterDeathRecipient(::ohos::rpc::rpc::weak::DeathRecipient recipient, int32_t flags); + + ::taihe::string GetDescriptor(); + + bool IsObjectDead(); + + int64_t GetNativePtr(); + + void AddJsObjWeakRef(::ohos::rpc::rpc::weak::RemoteProxy obj); + + static ::ohos::rpc::rpc::RemoteProxy CreateRemoteProxyFromNative(uintptr_t nativePtr); + +private: + OHOS::sptr cachedObject_; + std::optional<::ohos::rpc::rpc::RemoteProxy> jsObjRef_; + std::optional<::ohos::rpc::rpc::IRemoteBroker> jsLocalInterface_; + ::taihe::map<::ohos::rpc::rpc::DeathRecipient, OHOS::sptr> deathRecipientMap_; +}; + +} // namespace +#endif // OHOS_IPC_TAIHE_REMOTEPROXY_H \ No newline at end of file diff --git a/ipc/native/src/taihe/src/ani_constructor.cpp b/ipc/native/src/taihe/src/ani_constructor.cpp new file mode 100644 index 00000000..37b28460 --- /dev/null +++ b/ipc/native/src/taihe/src/ani_constructor.cpp @@ -0,0 +1,37 @@ +/* + * 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 "taihe/runtime.hpp" +#include "ohos.rpc.rpc.ani.hpp" + +#include "ipc_debug.h" +#include "log_tags.h" + +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_OTHER, "RpcTaiheImpl" }; + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + ani_status registerRes = ohos::rpc::rpc::ANIRegister(env); + if (ANI_OK != registerRes) { + ZLOGE(LOG_LABEL, "ohos::rpc::rpc::ANIRegister failed, errCode:%{public}d", registerRes); + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/ipc/native/src/taihe/src/rpc_taihe_error.cpp b/ipc/native/src/taihe/src/rpc_taihe_error.cpp new file mode 100644 index 00000000..34a2119d --- /dev/null +++ b/ipc/native/src/taihe/src/rpc_taihe_error.cpp @@ -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. + */ + +#include "rpc_taihe_error.h" + +namespace OHOS { +int32_t RpcTaiheError::Convert(const int errCode) +{ + return RPC_TAIHE_ERR_MAP.at(errCode).errorCode; +} + +std::string RpcTaiheError::ToMessage(const int errCode) +{ + return RPC_TAIHE_ERR_MAP.at(errCode).errorMsg; +} +} \ No newline at end of file -- Gitee