From 84000b41e92ffdd81d8e64acd95ede91be4941d9 Mon Sep 17 00:00:00 2001 From: hanlin15 Date: Thu, 31 Jul 2025 16:41:01 +0800 Subject: [PATCH] fix:del ani Signed-off-by: hanlin15 Change-Id: I232c41e87d157cefdae7aa969086d3b336b1408b --- bundle.json | 11 - config.gni | 1 - ipc/native/src/ani/BUILD.gn | 19 - ipc/native/src/ani/rpc/BUILD.gn | 66 --- ipc/native/src/ani/rpc/ets/@ohos.rpc.ets | 171 ------ .../src/ani/rpc/include/ani_remote_object.h | 29 -- .../src/ani/rpc/include/ani_rpc_error.h | 45 -- .../src/ani/rpc/include/ani_util_class.h | 86 --- .../src/ani/rpc/include/ani_util_common.h | 172 ------ .../src/ani/rpc/include/ani_util_conversion.h | 58 --- .../src/ani/rpc/include/ani_util_native_ptr.h | 192 ------- ipc/native/src/ani/rpc/include/ani_utils.h | 24 - ipc/native/src/ani/rpc/src/ani_rpc_error.cpp | 67 --- ipc/native/src/ani/rpc/src/rpc_ani.cpp | 493 ------------------ 14 files changed, 1434 deletions(-) delete mode 100644 ipc/native/src/ani/BUILD.gn delete mode 100644 ipc/native/src/ani/rpc/BUILD.gn delete mode 100644 ipc/native/src/ani/rpc/ets/@ohos.rpc.ets delete mode 100644 ipc/native/src/ani/rpc/include/ani_remote_object.h delete mode 100644 ipc/native/src/ani/rpc/include/ani_rpc_error.h delete mode 100644 ipc/native/src/ani/rpc/include/ani_util_class.h delete mode 100644 ipc/native/src/ani/rpc/include/ani_util_common.h delete mode 100644 ipc/native/src/ani/rpc/include/ani_util_conversion.h delete mode 100644 ipc/native/src/ani/rpc/include/ani_util_native_ptr.h delete mode 100644 ipc/native/src/ani/rpc/include/ani_utils.h delete mode 100644 ipc/native/src/ani/rpc/src/ani_rpc_error.cpp delete mode 100644 ipc/native/src/ani/rpc/src/rpc_ani.cpp diff --git a/bundle.json b/bundle.json index 9c6c5ca8..4148f1c5 100644 --- a/bundle.json +++ b/bundle.json @@ -56,7 +56,6 @@ "build": { "sub_component": [ "//foundation/communication/ipc:ipc_components", - "//foundation/communication/ipc/ipc/native/src/ani:rpc_ani_packages", "//foundation/communication/ipc/ipc/native/src/taihe/:rpc_taihe_components" ], "inner_kits": [ @@ -84,16 +83,6 @@ "header_base": "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include" } }, - { - "name": "//foundation/communication/ipc/ipc/native/src/ani/rpc:rpc_ani", - "header": { - "header_files": [ - "ani_remote_object.h", - "ani_utils.h" - ], - "header_base": "//foundation/communication/ipc/ipc/native/src/ani/rpc/include" - } - }, { "name": "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single", "header": { diff --git a/config.gni b/config.gni index bf6ea89d..d3131d6f 100644 --- a/config.gni +++ b/config.gni @@ -11,7 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -rpc_ani_path = "//foundation/communication/ipc/ipc/native/src/ani" IPC_ROOT_PATH = "//foundation/communication/ipc" IPC_UNITTEST_ROOT_PATH = "$IPC_ROOT_PATH/test/unittest" diff --git a/ipc/native/src/ani/BUILD.gn b/ipc/native/src/ani/BUILD.gn deleted file mode 100644 index 7073075d..00000000 --- a/ipc/native/src/ani/BUILD.gn +++ /dev/null @@ -1,19 +0,0 @@ -# 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/communication/ipc/config.gni") - -group("rpc_ani_packages") { - deps = [ "${rpc_ani_path}/rpc:rpc_ani_package" ] -} diff --git a/ipc/native/src/ani/rpc/BUILD.gn b/ipc/native/src/ani/rpc/BUILD.gn deleted file mode 100644 index 45f22cd6..00000000 --- a/ipc/native/src/ani/rpc/BUILD.gn +++ /dev/null @@ -1,66 +0,0 @@ -# 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") - -group("rpc_ani_package") { - deps = [ - ":rpc_abc_etc", - ":rpc_ani", - ] -} - -config("ani_rpc_public_config") { - include_dirs = [ "include" ] -} - -ohos_shared_library("rpc_ani") { - sources = [ - "src/rpc_ani.cpp", - "src/ani_rpc_error.cpp" - ] - include_dirs = [ - "include", - "../../../../../utils/include", - "../../../../../interfaces/innerkits/ipc_core/include", - ] - public_configs = [ ":ani_rpc_public_config" ] - - deps = [ "../../../../../interfaces/innerkits/ipc_core:ipc_core" ] - external_deps = [ - "c_utils:utils", - "hilog:libhilog", - "runtime_core:ani", - ] - subsystem_name = "communication" - part_name = "ipc" - output_extension = "so" -} - -generate_static_abc("rpc_abc") { - base_url = "./ets" - files = [ "./ets/@ohos.rpc.ets" ] - dst_file = "$target_out_dir/rpc.abc" - out_puts = [ "$target_out_dir/rpc.abc" ] - is_boot_abc = "True" - device_dst_file = "/system/framework/rpc.abc" -} - -ohos_prebuilt_etc("rpc_abc_etc") { - source = "$target_out_dir/rpc.abc" - module_install_dir = "framework" - subsystem_name = "communication" - part_name = "ipc" - deps = [ ":rpc_abc" ] -} diff --git a/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets b/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets deleted file mode 100644 index f2356888..00000000 --- a/ipc/native/src/ani/rpc/ets/@ohos.rpc.ets +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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 hilog from '@ohos.hilog'; - - - -export namespace rpc { - export class Cleaner { - static { loadLibrary("rpc_ani") } - static callback(cleaner: Cleaner): void { - console.println("[Cleaner] enter Cleaner.callback"); - cleaner.clean() - } - constructor(targetPtr: long) { - this.targetPtr = targetPtr - } - - native clean(): void - - private targetPtr: long = 0 - } - - class FinalizationAgent { - constructor(obj: T, ptr: long) { - this.register(obj, ptr); - } - - register(obj: T, ptr: long): void { - this.unregisterToken = {}; - this.cleaner = new Cleaner(ptr); - finalizer.register(obj, this.cleaner!, this.unregisterToken); - } - - unregister(): void { - finalizer.unregister(this.unregisterToken); - } - - private cleaner: Cleaner | null = null; - private unregisterToken: object; - } - - let finalizer = new FinalizationRegistry(Cleaner.callback) - export class MessageSequence { - constructor() {} - native readString(): string; - native writeString(val: string): void; - native readInterfaceToken(): string; - - private nativePtr: long = 0; - } - - export class MessageOption { - static { loadLibrary("rpc_ani") } - - constructor(asyncFlag: boolean) { - hilog.info(0x0000, 'rpc', '[ETS] Enter MessageOption.constructor(asyncFlag)'); - if (asyncFlag === true) { - this.init(1, 0); - } - else { - this.init(0, 0); - } - this.registerCleaner(); - } - - constructor(syncFlags?: number, waitTime?: number) { - hilog.info(0x0000, 'rpc', '[ETS] Enter MessageOption.constructor(syncFlags, waitTime)'); - if (syncFlags != undefined && waitTime != undefined) { - this.init(syncFlags, waitTime); - } - else if (syncFlags === undefined && waitTime === undefined) { - this.init(0, 0); - } - else if (syncFlags === undefined && waitTime != undefined) { - this.init(0, waitTime); - } - else if (syncFlags != undefined && waitTime === undefined) { - this.init(syncFlags, 0); - } - this.registerCleaner(); - } - - registerCleaner(): void { - this.fzAgent = new FinalizationAgent(this, this.nativePtr); - } - - unregisterCleaner(): void { - this.fzAgent.unregister(); - } - - native init(syncFlags: number, waitTime: number): void; - - getNativePtr():long { - return this.nativePtr; - } - - private nativePtr: long = 0; - private fzAgent: FinalizationAgent; - } - - export abstract class IRemoteObject { - native getDescriptor(): string; - - private descriptor:string; - } - - export class RemoteObject extends IRemoteObject { - static { loadLibrary("rpc_ani") } - - constructor(descriptor: string) { - this.init(descriptor); - this.fzAgent = new FinalizationAgent(this, this.nativePtr); - } - - native init(descriptor: string): void; - native getDescriptor(): string; - - unregisterCleaner(): void { - this.fzAgent.unregister(); - } - - getNativePtr(): long { - return this.nativePtr; - } - - public onRemoteMessageRequest( - code: number, - data: rpc.MessageSequence, - reply: rpc.MessageSequence, - options: rpc.MessageOption - ): boolean | Promise { - hilog.info(0x0000, 'rpc', '[ETS] Enter RemoteObject.onRemoteMessageRequest(' + code + ', ...)'); - return true; - } - - private onRemoteMessageRequestSync( - code: number, - data: rpc.MessageSequence, - reply: rpc.MessageSequence, - options: rpc.MessageOption - ): boolean { - const result = this.onRemoteMessageRequest(code, data, reply, options); - if (result instanceof Promise) { - return await (result as Promise); - } - return result as boolean; - } - - private nativePtr: long = 0; - private fzAgent: FinalizationAgent; - } - - export class RemoteProxy extends IRemoteObject { - native getDescriptor(): string; - - private nativePtr: long = 0; - } -} diff --git a/ipc/native/src/ani/rpc/include/ani_remote_object.h b/ipc/native/src/ani/rpc/include/ani_remote_object.h deleted file mode 100644 index 3f1e7874..00000000 --- a/ipc/native/src/ani/rpc/include/ani_remote_object.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 ANI_REMOTE_OBJECT_H -#define ANI_REMOTE_OBJECT_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 // ANI_REMOTE_OBJECT_H \ No newline at end of file diff --git a/ipc/native/src/ani/rpc/include/ani_rpc_error.h b/ipc/native/src/ani/rpc/include/ani_rpc_error.h deleted file mode 100644 index 0bf89ba7..00000000 --- a/ipc/native/src/ani/rpc/include/ani_rpc_error.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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_ANI_ERROR_H -#define OHOS_ANI_ERROR_H - -#include -#include -#include - -namespace OHOS { -enum errorCode : int32_t { - PROXY_OR_REMOTE_OBJECT_INVALID_ERROR = 1900008, - WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR = 1900009, - READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR = 1900010, - CALL_JS_METHOD_ERROR = 1900012 -}; - -class AniError { -public: - AniError() = default; - static void ThrowError(ani_env *env, int errCode); - -private: - inline static const std::unordered_map aniErrMap_ { - {PROXY_OR_REMOTE_OBJECT_INVALID_ERROR, "proxy or remote object is invalid"}, - {WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR, "write data to message sequence failed"}, - {READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, "read data from message sequence failed"}, - {CALL_JS_METHOD_ERROR, "call js method failed"} - }; -}; // AniError -} // namespace OHOS -#endif // OHOS_ANI_ERROR_H diff --git a/ipc/native/src/ani/rpc/include/ani_util_class.h b/ipc/native/src/ani/rpc/include/ani_util_class.h deleted file mode 100644 index 14e4ac8b..00000000 --- a/ipc/native/src/ani/rpc/include/ani_util_class.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 ANI_UTIL_CLASS_H -#define ANI_UTIL_CLASS_H - -#include -#include - -#include "ani_util_common.h" - -class AniTypeFinder { -public: - AniTypeFinder(ani_env *env) : env_(env) {} - - expected FindNamespace(const char *nsName) - { - ani_namespace ns; - ani_status status = env_->FindNamespace(nsName, &ns); - if (ANI_OK != status) { - return status; - } - return ns; - } - - template - expected FindNamespace(const char *firstNs, const char *nextNs, Names... restNs) - { - const std::string nsName = std::string(firstNs).append(".").append(nextNs); - return FindNamespace(nsName.c_str(), restNs...); - } - - expected FindClass(const char *clsName) - { - ani_class cls; - ani_status status = env_->FindClass(clsName, &cls); - if (ANI_OK != status) { - return status; - } - return cls; - } - - expected FindClass(const char *nsName, const char *clsName) - { - const std::string fullClsName = std::string(nsName).append(".").append(clsName); - return FindClass(fullClsName.c_str()); - } - - template - expected FindClass(const char *firstNs, - const char *secondNs, - Names... restNs, - const char *clsName) - { - const std::string nsName = std::string(firstNs).append(".").append(secondNs); - return FindClass(nsName.c_str(), restNs..., clsName); - } - - expected FindEnum(const char *nsName, const char *enumName) - { - ani_enum aniEnum {}; - const std::string fullEnumName = std::string(nsName).append(".").append(enumName); - ani_status status = env_->FindEnum(fullEnumName.c_str(), &aniEnum); - if (ANI_OK != status) { - return status; - } - return aniEnum; - } - -private: - ani_env *env_ = nullptr; -}; - -#endif diff --git a/ipc/native/src/ani/rpc/include/ani_util_common.h b/ipc/native/src/ani/rpc/include/ani_util_common.h deleted file mode 100644 index 9ffdb7b4..00000000 --- a/ipc/native/src/ani/rpc/include/ani_util_common.h +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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 ANI_UTIL_COMMON_H -#define ANI_UTIL_COMMON_H - -#include -#include -#include -#include -#include - -template -class expected { -private: - std::variant data_; - bool has_value_; - -public: - expected(const T &value) : data_(value), has_value_(true) {} - - expected(T &&value) : data_(std::move(value)), has_value_(true) {} - - expected(const E &error) : data_(error), has_value_(false) {} - - expected(E &&error) : data_(std::move(error)), has_value_(false) {} - - bool has_value() const noexcept - { - return has_value_; - } - - explicit operator bool() const noexcept - { - return has_value(); - } - - T &value() & - { - if (!has_value()) { - std::terminate(); - } - return std::get(data_); - } - - const T &value() const & - { - if (!has_value()) { - std::terminate(); - } - return std::get(data_); - } - - T &&value() && - { - if (!has_value()) { - std::terminate(); - } - return std::get(std::move(data_)); - } - - E &error() & - { - if (has_value()) { - std::terminate(); - } - return std::get(data_); - } - - const E &error() const & - { - if (has_value()) { - std::terminate(); - } - return std::get(data_); - } - - E &&error() && - { - if (has_value()) { - std::terminate(); - } - return std::get(std::move(data_)); - } - - T &operator*() & - { - return value(); - } - - const T &operator*() const & - { - return value(); - } - - T &&operator*() && - { - return std::move(*this).value(); - } - - T *operator->() - { - return &value(); - } - - const T *operator->() const - { - return &value(); - } - - template - T value_or(U &&default_value) const & - { - return has_value() ? value() : static_cast(std::forward(default_value)); - } - - template - T value_or(U &&default_value) && - { - return has_value() ? std::move(*this).value() : static_cast(std::forward(default_value)); - } -}; - -template -class FinalAction { -public: - explicit FinalAction(F func) : func_(std::move(func)) {} - - ~FinalAction() noexcept(noexcept(func_())) - { - if (!dismissed_) { - func_(); - } - } - - FinalAction(const FinalAction &) = delete; - FinalAction &operator=(const FinalAction &) = delete; - - FinalAction(FinalAction &&other) noexcept : func_(std::move(other.func_)), dismissed_(other.dismissed_) - { - other.dismissed_ = true; - } - - void dismiss() noexcept - { - dismissed_ = true; - } - -private: - F func_; - bool dismissed_ = false; -}; - -template -inline FinalAction finally(F &&func) -{ - return FinalAction(std::forward(func)); -} - -#endif diff --git a/ipc/native/src/ani/rpc/include/ani_util_conversion.h b/ipc/native/src/ani/rpc/include/ani_util_conversion.h deleted file mode 100644 index eba55dff..00000000 --- a/ipc/native/src/ani/rpc/include/ani_util_conversion.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 ANI_UTIL_CONVERSION_H -#define ANI_UTIL_CONVERSION_H - -#include - -#include -#include -#include -#include - -#include "ani_util_class.h" -#include "ani_util_common.h" - -class AniStringUtils { -public: - static std::string ToStd(ani_env *env, ani_string ani_str) - { - ani_size strSize; - env->String_GetUTF8Size(ani_str, &strSize); - - std::vector buffer(strSize + 1); // +1 for null terminator - char *utf8_buffer = buffer.data(); - - // String_GetUTF8 Supportted by https://gitee.com/openharmony/arkcompiler_runtime_core/pulls/3416 - ani_size bytes_written = 0; - env->String_GetUTF8(ani_str, utf8_buffer, strSize + 1, &bytes_written); - - utf8_buffer[bytes_written] = '\0'; - std::string content = std::string(utf8_buffer); - return content; - } - - static ani_string ToAni(ani_env *env, const std::string &str) - { - ani_string aniStr = nullptr; - if (ANI_OK != env->String_NewUTF8(str.data(), str.size(), &aniStr)) { - return nullptr; - } - return aniStr; - } -}; - -#endif diff --git a/ipc/native/src/ani/rpc/include/ani_util_native_ptr.h b/ipc/native/src/ani/rpc/include/ani_util_native_ptr.h deleted file mode 100644 index 79594d4f..00000000 --- a/ipc/native/src/ani/rpc/include/ani_util_native_ptr.h +++ /dev/null @@ -1,192 +0,0 @@ -/* - * 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 ANI_UTIL_OBJECT_H -#define ANI_UTIL_OBJECT_H - -#include -#include -#include - -class NativeObject { -public: - virtual ~NativeObject() = default; -}; - -template -class StdSharedPtrHolder : public NativeObject { -public: - StdSharedPtrHolder(const std::shared_ptr &sptr) : sptr_(sptr) {} - - std::shared_ptr Get() - { - return sptr_; - } - - std::shared_ptr GetOrDefault() - { - if (!sptr_) { - sptr_ = std::make_shared(); - } - return sptr_; - } - -private: - std::shared_ptr sptr_; -}; - -template -class OhSharedPtrHolder : public NativeObject { -public: - OhSharedPtrHolder(const OHOS::sptr &sptr) : sptr_(sptr) {} - - OHOS::sptr Get() - { - return sptr_; - } - - OHOS::sptr GetOrDefault() - { - if (!sptr_) { - sptr_ = OHOS::sptr::MakeSptr(); - } - return sptr_; - } - -private: - OHOS::sptr sptr_; -}; - -class AniObjectUtils { -public: - static ani_object Create(ani_env *env, const char *nsName, const char *clsName, ...) - { - ani_object nullobj {}; - - const std::string fullClsName = std::string(nsName).append(".").append(clsName); - ani_class cls; - if (ANI_OK != env->FindClass(fullClsName.c_str(), &cls)) { - return nullobj; - } - - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { - return nullobj; - } - - ani_object obj; - va_list args; - va_start(args, clsName); - ani_status status = env->Object_New_V(cls, ctor, &obj, args); - va_end(args); - if (ANI_OK != status) { - return nullobj; - } - return obj; - } - - static ani_object Create(ani_env *env, const char *clsName, ...) - { - ani_object nullobj {}; - - ani_class cls; - if (ANI_OK != env->FindClass(clsName, &cls)) { - return nullobj; - } - - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { - return nullobj; - } - - ani_object obj; - va_list args; - va_start(args, clsName); - ani_status status = env->Object_New_V(cls, ctor, &obj, args); - va_end(args); - if (ANI_OK != status) { - return nullobj; - } - return obj; - } - - static ani_object Create(ani_env *env, ani_class cls, ...) - { - ani_object nullobj {}; - - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { - return nullobj; - } - - ani_object obj; - va_list args; - va_start(args, cls); - ani_status status = env->Object_New_V(cls, ctor, &obj, args); - va_end(args); - if (ANI_OK != status) { - return nullobj; - } - return obj; - } - - template - static ani_status Wrap(ani_env *env, ani_object object, T *nativePtr, const char *propName = "nativePtr") - { - return env->Object_SetFieldByName_Long(object, propName, reinterpret_cast(nativePtr)); - } - - template - static T *Unwrap(ani_env *env, ani_object object, const char *propName = "nativePtr") - { - ani_long nativePtr; - if (ANI_OK != env->Object_GetFieldByName_Long(object, propName, &nativePtr)) { - return nullptr; - } - return reinterpret_cast(nativePtr); - } -}; - -class NativePtrCleaner { -public: - static void Clean([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) - { - ani_long ptr = 0; - if (ANI_OK != env->Object_GetFieldByName_Long(object, "targetPtr", &ptr)) { - return; - } - delete reinterpret_cast(ptr); - } - - NativePtrCleaner(ani_env *env) : env_(env) {} - - ani_status Bind(ani_class cls) - { - std::array methods = { - ani_native_function {"clean", nullptr, reinterpret_cast(NativePtrCleaner::Clean)}, - }; - - if (ANI_OK != env_->Class_BindNativeMethods(cls, methods.data(), methods.size())) { - return (ani_status)ANI_ERROR; - }; - - return ANI_OK; - } - -private: - ani_env *env_ = nullptr; -}; - -#endif diff --git a/ipc/native/src/ani/rpc/include/ani_utils.h b/ipc/native/src/ani/rpc/include/ani_utils.h deleted file mode 100644 index e31dd997..00000000 --- a/ipc/native/src/ani/rpc/include/ani_utils.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 ANI_UTILS_H -#define ANI_UTILS_H - -#include "ani_util_class.h" -#include "ani_util_common.h" -#include "ani_util_conversion.h" -#include "ani_util_native_ptr.h" - -#endif diff --git a/ipc/native/src/ani/rpc/src/ani_rpc_error.cpp b/ipc/native/src/ani/rpc/src/ani_rpc_error.cpp deleted file mode 100644 index 880f9917..00000000 --- a/ipc/native/src/ani/rpc/src/ani_rpc_error.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 "ani_rpc_error.h" -#include "ipc_debug.h" -#include "log_tags.h" - -namespace OHOS { -static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_ID_IPC_NAPI, "ani_rpc_error"}; - -static void ThrowBusinessError(ani_env *env, int errCode, const std::string &errMsg) -{ - static const char *errorClsName = "L@ohos/base/BusinessError;"; - ZLOGD(LOG_LABEL, "Begin ThrowBusinessError."); - - ani_class cls {}; - if (ANI_OK != env->FindClass(errorClsName, &cls)) { - ZLOGE(LOG_LABEL, "find class BusinessError %{public}s failed", errorClsName); - return; - } - - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { - ZLOGE(LOG_LABEL, "find method BusinessError.constructor failed"); - return; - } - - ani_object errorObject; - if (ANI_OK != env->Object_New(cls, ctor, &errorObject)) { - ZLOGE(LOG_LABEL, "create BusinessError object failed"); - return; - } - - ani_double aniErrCode = static_cast(errCode); - ani_string errMsgStr; - if (ANI_OK != env->String_NewUTF8(errMsg.c_str(), errMsg.size(), &errMsgStr)) { - ZLOGE(LOG_LABEL, "convert errMsg to ani_string failed"); - } - ZLOGD(LOG_LABEL, "ThrowBusinessError: errMsg: %{public}s.", errMsg.c_str()); - if (ANI_OK != env->Object_SetFieldByName_Double(errorObject, "code", aniErrCode)) { - ZLOGE(LOG_LABEL, "set error code failed"); - } - if (ANI_OK != env->Object_SetPropertyByName_Ref(errorObject, "message", errMsgStr)) { - ZLOGE(LOG_LABEL, "set error message failed"); - } - - env->ThrowError(static_cast(errorObject)); -} - -void AniError::ThrowError(ani_env *env, int errCode) -{ - ThrowBusinessError(env, errCode, aniErrMap_.at(errCode)); -} - -} // namespace OHOS \ No newline at end of file diff --git a/ipc/native/src/ani/rpc/src/rpc_ani.cpp b/ipc/native/src/ani/rpc/src/rpc_ani.cpp deleted file mode 100644 index 96f2ab93..00000000 --- a/ipc/native/src/ani/rpc/src/rpc_ani.cpp +++ /dev/null @@ -1,493 +0,0 @@ -/* - * 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 "ani_remote_object.h" -#include "ani_rpc_error.h" -#include "ani_utils.h" -#include "ipc_debug.h" -#include "ipc_object_proxy.h" -#include "ipc_object_stub.h" -#include "iremote_object.h" -#include "log_tags.h" -#include "message_parcel.h" -#include "string_ex.h" -#include -#include -#include - -using namespace OHOS; - -static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_ID_IPC_NAPI, "rpc_ani"}; - -static ani_object CreateMessageSequence([[maybe_unused]] ani_env *env, MessageParcel &msgParcel) -{ - ZLOGI(LOG_LABEL, "[ANI] enter CreateMessageSequence func"); - static const char *className = "@ohos.rpc.rpc.MessageSequence"; - ani_class cls; - ani_object nullobj {}; - if (ANI_OK != env->FindClass(className, &cls)) { - ZLOGE(LOG_LABEL, "[ANI] Not found MessageSequence Class: '%{public}s'", className); - return nullobj; - } - - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { - ZLOGE(LOG_LABEL, "[ANI] Not found MessageSequence ctor"); - return nullobj; - } - - ani_object sequenceObj; - if (ANI_OK != env->Object_New(cls, ctor, &sequenceObj)) { - ZLOGE(LOG_LABEL, "[ANI] New MessageSequence Fail"); - return nullobj; - } - - AniObjectUtils::Wrap(env, sequenceObj, &msgParcel); - return sequenceObj; -} - -static ani_object DoubleToObject(ani_env *env, double value) -{ - ani_object aniObject = nullptr; - ani_double doubleValue = static_cast(value); - static const char *className = "Lstd/core/Double;"; - ani_class aniClass; - if (ANI_OK != env->FindClass(className, &aniClass)) { - ZLOGE(LOG_LABEL, "Not found '%{public}s'", className); - return aniObject; - } - ani_method personInfoCtor; - if (ANI_OK != env->Class_FindMethod(aniClass, "", "D:V", &personInfoCtor)) { - ZLOGE(LOG_LABEL, "Class_GetMethod Failed '%{public}s' ", className); - return aniObject; - } - - if (ANI_OK != env->Object_New(aniClass, personInfoCtor, &aniObject, doubleValue)) { - ZLOGE(LOG_LABEL, "Object_New Failed '%{public}s' ", className); - return aniObject; - } - return aniObject; -} - -static ani_object CreateMessageOption([[maybe_unused]] ani_env *env, MessageOption &option) -{ - ZLOGI(LOG_LABEL, "[ANI] enter CreateMessageOption func, flags: %{public}u", option.GetFlags()); - - static const char *className = "@ohos.rpc.rpc.MessageOption"; - ani_class cls; - ani_object nullobj {}; - if (ANI_OK != env->FindClass(className, &cls)) { - ZLOGE(LOG_LABEL, "[ANI] Not found MessageOption Class: '%{public}s'", className); - return nullobj; - } - - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { - ZLOGE(LOG_LABEL, "[ANI] Not found MessageOption ctor"); - return nullobj; - } - - ani_object flagsObj = DoubleToObject(env, option.GetFlags()); - ani_object waitTimeObj = DoubleToObject(env, option.GetWaitTime()); - - ZLOGI(LOG_LABEL, "[ANI] CreateMessageOption Object_New"); - ani_object optionObj; - if (ANI_OK != env->Object_New(cls, ctor, &optionObj, flagsObj, waitTimeObj)) { - ZLOGE(LOG_LABEL, "[ANI] [ANI] New MessageOption Fail"); - return nullobj; - } - - return optionObj; -} - -static void InitMessageOption([[maybe_unused]] ani_env *env, - [[maybe_unused]] ani_object obj, - ani_double syncFlags, - ani_double waitTimeParam) -{ - ZLOGI(LOG_LABEL, "[ANI] enter InitMessageOption func syncFlags: %{public}f, waitTimeParam: %{public}f", syncFlags, - waitTimeParam); - int flags = MessageOption::TF_SYNC; - int waitTime = MessageOption::TF_WAIT_TIME; - - int32_t syncFlagsValue = static_cast(syncFlags); - int32_t waitTimeValue = static_cast(waitTimeParam); - if (syncFlagsValue != 0 && waitTimeValue != 0) { - flags = MessageOption::TF_ASYNC; - waitTime = waitTimeValue; - } - - auto messageOptionHolder = new StdSharedPtrHolder(std::make_shared(flags, waitTime)); - AniObjectUtils::Wrap>(env, obj, messageOptionHolder); - ZLOGI(LOG_LABEL, "[ANI] InitMessageOption end"); -} - -class IPCAniStub : public IPCObjectStub { -public: - IPCAniStub(ani_env *env, ani_object remoteObject, const std::u16string &descriptor) - : IPCObjectStub(descriptor), env_(env) - { - ZLOGI(LOG_LABEL, "[ANI] enter IPCAniStub ctor"); - if (ANI_OK != env_->GlobalReference_Create(reinterpret_cast(remoteObject), &saveRemote_)) { - ZLOGE(LOG_LABEL, "[ANI] GlobalReference_Create failed"); - } - } - - int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override - { - ZLOGI(LOG_LABEL, "[ANI] enter IPCAniStub OnRemoteRequest"); - ani_env *env = env_; - static const char *remoteObjClsName = "@ohos.rpc.rpc.RemoteObject"; - ani_class cls; - if (ANI_OK != env->FindClass(remoteObjClsName, &cls)) { - ZLOGE(LOG_LABEL, "[ANI] Not found '%{public}s'", remoteObjClsName); - return ANI_NOT_FOUND; - } - - ani_object aniData = CreateMessageSequence(env, data); - ani_object aniReply = CreateMessageSequence(env, reply); - ani_object aniOption = CreateMessageOption(env, option); - - auto obj = reinterpret_cast(saveRemote_); - ZLOGI(LOG_LABEL, "[ANI] before Object_CallMethodByName_Void onRemoteMessageRequestSync"); - ani_boolean result; - if (ANI_OK != env_->Object_CallMethodByName_Boolean(obj, "onRemoteMessageRequestSync", nullptr, &result, - (ani_double)code, aniData, aniReply, aniOption)) { - AniError::ThrowError(env_, CALL_JS_METHOD_ERROR); - } - ZLOGI(LOG_LABEL, "[ANI] after Object_CallMethodByName_Void onRemoteMessageRequestSync"); - - return result; - } - - ~IPCAniStub() - { - ZLOGI(LOG_LABEL, "[ANI] enter IPCAniStub dtor"); - if (ANI_OK != env_->GlobalReference_Delete(saveRemote_)) { - ZLOGE(LOG_LABEL, "[ANI] GlobalReference_Delete failed"); - } - } - -private: - ani_env *env_ = nullptr; - ani_ref saveRemote_; -}; - -class IPCObjectRemoteHolder : public NativeObject { -public: - IPCObjectRemoteHolder(ani_env *env, ani_object remoteObject, const std::u16string &descriptor) - : env_(env), remoteObject_(remoteObject), descriptor_(descriptor) - { - if (ANI_OK != env->GlobalReference_Create(reinterpret_cast(remoteObject), - reinterpret_cast(&remoteObject_))) { - ZLOGE(LOG_LABEL, "[ANI] GlobalReference_Create failed"); - } - } - - std::string GetDescriptor() - { - std::string ret = Str16ToStr8(descriptor_); - ZLOGI(LOG_LABEL, "[ANI] enter IPCObjectRemoteHolder GetDescriptor, descriptor:%{public}s", ret.c_str()); - return ret; - } - - sptr Get(ani_env *env) - { - sptr tmp = object_.promote(); - if (nullptr == tmp && nullptr != env) { - tmp = sptr::MakeSptr(env, remoteObject_, descriptor_); - object_ = tmp; - } - return tmp; - } - - ~IPCObjectRemoteHolder() - { - ZLOGI(LOG_LABEL, "[ANI] enter IPCObjectRemoteHolder dtor"); - if (ANI_OK != env_->GlobalReference_Delete(remoteObject_)) { - ZLOGE(LOG_LABEL, "[ANI] GlobalReference_Delete failed"); - } - } - -private: - ani_env *env_ = nullptr; - ani_object remoteObject_; - std::u16string descriptor_; - wptr object_; -}; - -class IPCObjectProxyHolder { -public: - std::string GetDescriptor() - { - ZLOGI(LOG_LABEL, "[ANI] enter IPCObjectRemoteHolder GetDescriptor"); - if (!object_) { - return ""; - } - return OHOS::Str16ToStr8(object_->GetInterfaceDescriptor()); - } - -private: - sptr object_; -}; - -sptr AniGetNativeRemoteObject(ani_env *env, ani_object obj) -{ - ZLOGI(LOG_LABEL, "[ANI] enter AniGetNativeRemoteObject func"); - auto holder = AniObjectUtils::Unwrap(env, obj); - if (holder == nullptr) { - ZLOGI(LOG_LABEL, "[ANI] IPCObjectRemoteHolder is nullptr"); - return nullptr; - } - return holder->Get(env); -} - -static ani_object CreateJsProxyRemoteObject(ani_env *env, const sptr target) -{ - auto holder = new OhSharedPtrHolder(target); - if (holder == nullptr) { - ZLOGE(LOG_LABEL, "[ANI] OhSharedPtrHolder constructor failed"); - return nullptr; - } - ani_object jsRemoteProxy = AniObjectUtils::Create(env, "@ohos.rpc.rpc", "RemoteProxy"); - if (jsRemoteProxy == nullptr) { - ZLOGE(LOG_LABEL, "[ANI] Create jsRemoteProxy failed"); - return nullptr; - } - AniObjectUtils::Wrap>(env, jsRemoteProxy, holder); - return jsRemoteProxy; -} - -ani_object ANI_ohos_rpc_CreateJsRemoteObject(ani_env *env, sptr remoteObject) -{ - if (remoteObject == nullptr) { - ZLOGE(LOG_LABEL, "[ANI] RemoteObject is nullptr"); - return nullptr; - } - - return CreateJsProxyRemoteObject(env, remoteObject); -} - -static ani_string MessageSequenceReadString([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) -{ - ZLOGI(LOG_LABEL, "[ANI] enter MessageSequenceReadString func"); - auto parcel = AniObjectUtils::Unwrap(env, object); - if (parcel == nullptr) { - AniError::ThrowError(env, READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); - return ani_string {}; - } - auto str = parcel->ReadString(); - return AniStringUtils::ToAni(env, str); -} - -static bool MessageSequenceWriteString([[maybe_unused]] ani_env *env, - [[maybe_unused]] ani_object object, - ani_string str) -{ - ZLOGI(LOG_LABEL, "[ANI] enter MessageSequenceWriteString func"); - auto parcel = AniObjectUtils::Unwrap(env, object); - if (parcel == nullptr) { - AniError::ThrowError(env, WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); - return false; - } - auto stringContent = AniStringUtils::ToStd(env, str); - return parcel->WriteString(stringContent); -} - -static ani_string MessageSequencereadInterfaceToken([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) -{ - ZLOGI(LOG_LABEL, "[ANI] enter MessageSequencereadInterfaceToken func"); - auto parcel = AniObjectUtils::Unwrap(env, object); - if (parcel == nullptr) { - AniError::ThrowError(env, READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); - return ani_string {}; - } - auto str = parcel->ReadInterfaceToken(); - std::string outString = Str16ToStr8(str.c_str()); - return AniStringUtils::ToAni(env, outString); -} - -static void RemoteObjectInit([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, ani_string descriptor) -{ - ZLOGI(LOG_LABEL, "[ANI] enter RemoteObjectInit func"); - auto descriptorStr = AniStringUtils::ToStd(env, static_cast(descriptor)); - auto objectRemoteHolder = new IPCObjectRemoteHolder(env, object, OHOS::Str8ToStr16(descriptorStr)); - AniObjectUtils::Wrap(env, object, objectRemoteHolder); -} - -static ani_string GetRemoteObjectDescriptor([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) -{ - ZLOGI(LOG_LABEL, "[ANI] enter GetRemoteObjectDescriptor func"); - ani_string result_string {}; - auto objectRemoteHolder = AniObjectUtils::Unwrap(env, object); - if (objectRemoteHolder == nullptr) { - ZLOGE(LOG_LABEL, "[ANI] objectRemoteHolder is nullptr"); - AniError::ThrowError(env, PROXY_OR_REMOTE_OBJECT_INVALID_ERROR); - return result_string; - } - auto descriptorStr = objectRemoteHolder->GetDescriptor(); - ZLOGI(LOG_LABEL, "[ANI] get descriptor: %{public}s", descriptorStr.c_str()); - env->String_NewUTF8(descriptorStr.c_str(), descriptorStr.size(), &result_string); - return result_string; -} - -static ani_string GetRemoteProxyDescriptor([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object) -{ - ZLOGI(LOG_LABEL, "[ANI] enter GetRemoteProxyDescriptor func"); - ani_string result_string {}; - auto objectProxyHolder = AniObjectUtils::Unwrap(env, object); - if (objectProxyHolder == nullptr) { - ZLOGE(LOG_LABEL, "[ANI] objectProxyHolder is nullptr"); - AniError::ThrowError(env, PROXY_OR_REMOTE_OBJECT_INVALID_ERROR); - return result_string; - } - auto descriptorStr = objectProxyHolder->GetDescriptor(); - ZLOGI(LOG_LABEL, "[ANI] get descriptor: %{public}s", descriptorStr.c_str()); - env->String_NewUTF8(descriptorStr.c_str(), descriptorStr.size(), &result_string); - return result_string; -} - -static ani_status BindMessageSequenceClassMethods(ani_env *env, const char *nsName) -{ - const std::string msgSeqClsName = std::string(nsName).append(".MessageSequence"); - ani_class msgSequenceClass; - if (ANI_OK != env->FindClass(msgSeqClsName.c_str(), &msgSequenceClass)) { - ZLOGE(LOG_LABEL, "[ANI] Not found '%{public}s'", msgSeqClsName.c_str()); - return ANI_NOT_FOUND; - } - - std::array methods = { - ani_native_function {"readString", nullptr, reinterpret_cast(MessageSequenceReadString)}, - ani_native_function {"writeString", nullptr, reinterpret_cast(MessageSequenceWriteString)}, - ani_native_function {"readInterfaceToken", nullptr, - reinterpret_cast(MessageSequencereadInterfaceToken)}, - }; - - if (ANI_OK != env->Class_BindNativeMethods(msgSequenceClass, methods.data(), methods.size())) { - ZLOGE(LOG_LABEL, "[ANI] Cannot bind native methods to '%{public}s'", msgSeqClsName.c_str()); - return ANI_ERROR; - }; - - return ANI_OK; -} - -static ani_status BindMessageOptionClassMethods(ani_env *env, const char *nsName) -{ - const std::string msgOptClsName = std::string(nsName).append(".MessageOption"); - ani_class msgOptionClass; - if (ANI_OK != env->FindClass(msgOptClsName.c_str(), &msgOptionClass)) { - ZLOGE(LOG_LABEL, "[ANI] Not found '%{public}s'", msgOptClsName.c_str()); - return ANI_NOT_FOUND; - } - - std::array methods = { - ani_native_function {"init", nullptr, reinterpret_cast(InitMessageOption)}, - }; - - if (ANI_OK != env->Class_BindNativeMethods(msgOptionClass, methods.data(), methods.size())) { - ZLOGE(LOG_LABEL, "[ANI] Cannot bind native methods to '%{public}s'", msgOptClsName.c_str()); - return ANI_ERROR; - }; - - return ANI_OK; -} - -static ani_status BindRemoteObjectClassMethods(ani_env *env, const char *nsName) -{ - const std::string remoteObjClsName = std::string(nsName).append(".RemoteObject"); - ani_class remoteObjClass; - if (ANI_OK != env->FindClass(remoteObjClsName.c_str(), &remoteObjClass)) { - ZLOGE(LOG_LABEL, "[ANI] Not found '%{public}s'", remoteObjClsName.c_str()); - return ANI_NOT_FOUND; - } - - std::array methods = { - ani_native_function {"getDescriptor", nullptr, reinterpret_cast(GetRemoteObjectDescriptor)}, - ani_native_function {"init", "Lstd/core/String;:V", reinterpret_cast(RemoteObjectInit)}, - }; - - if (ANI_OK != env->Class_BindNativeMethods(remoteObjClass, methods.data(), methods.size())) { - ZLOGE(LOG_LABEL, "[ANI] Cannot bind native methods to '%{public}s'", remoteObjClsName.c_str()); - return ANI_ERROR; - }; - - return ANI_OK; -} - -static ani_status BindRemoteProxyClassMethods(ani_env *env, const char *nsName) -{ - const std::string remoteProxyClsName = std::string(nsName).append(".RemoteProxy"); - ani_class remoteProxyClass; - if (ANI_OK != env->FindClass(remoteProxyClsName.c_str(), &remoteProxyClass)) { - ZLOGE(LOG_LABEL, "[ANI] Not found '%{public}s'", remoteProxyClsName.c_str()); - return ANI_NOT_FOUND; - } - - std::array methods = { - ani_native_function {"getDescriptor", nullptr, reinterpret_cast(GetRemoteProxyDescriptor)}, - }; - - if (ANI_OK != env->Class_BindNativeMethods(remoteProxyClass, methods.data(), methods.size())) { - ZLOGE(LOG_LABEL, "[ANI] Cannot bind native methods to '%{public}s'", remoteProxyClsName.c_str()); - return ANI_ERROR; - }; - - return ANI_OK; -} - -static ani_status BindCleanerclassMethods(ani_env *env, const char *nsName) -{ - auto cleanerCls = AniTypeFinder(env).FindClass(nsName, "Cleaner"); - return NativePtrCleaner(env).Bind(cleanerCls.value()); -} - -ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) -{ - ani_env *env; - if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { - ZLOGE(LOG_LABEL, "[ANI] Unsupported ANI_VERSION_1"); - return ANI_ERROR; - } - - static const char *nsName = "@ohos.rpc.rpc"; - - if (ANI_OK != BindMessageSequenceClassMethods(env, nsName)) { - ZLOGE(LOG_LABEL, "[ANI] BindMessageSequenceClassMethods failed"); - return ANI_ERROR; - } - - if (ANI_OK != BindMessageOptionClassMethods(env, nsName)) { - ZLOGE(LOG_LABEL, "[ANI] BindMessageOptionClassMethods failed"); - return ANI_ERROR; - } - - if (ANI_OK != BindRemoteObjectClassMethods(env, nsName)) { - ZLOGE(LOG_LABEL, "[ANI] BindRemoteObjectClassMethods failed"); - return ANI_ERROR; - } - - if (ANI_OK != BindRemoteProxyClassMethods(env, nsName)) { - ZLOGE(LOG_LABEL, "[ANI] BindRemoteProxyClassMethods failed"); - return ANI_ERROR; - } - - if (ANI_OK != BindCleanerclassMethods(env, nsName)) { - ZLOGE(LOG_LABEL, "[ANI] BindCleanerclassMethods failed"); - return ANI_ERROR; - } - - *result = ANI_VERSION_1; - return ANI_OK; -} -- Gitee