From b47f82483d1dbb2ce1297491521231c2992c6b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Tue, 19 Aug 2025 11:23:52 +0800 Subject: [PATCH 1/3] add transfer for backupSession MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- interfaces/kits/ani/backup/BUILD.gn | 2 + .../ani/backup/ets/@ohos.backup.transfer.ets | 42 +++++ .../kits/ani/backup/include/backup_session.h | 44 ++++++ .../backup/include/backup_session_transfer.h | 39 +++++ .../kits/ani/backup/src/ani_constructor.cpp | 6 +- .../kits/ani/backup/src/backup_session.cpp | 80 ++++++++++ .../backup/src/backup_session_transfer.cpp | 146 ++++++++++++++++++ .../src/incr_backup_session_transfer.cpp | 4 +- .../backup/src/incremental_backup_session.cpp | 4 +- .../js/backup/session_backup_n_exporter.cpp | 97 +++++++++++- .../js/backup/session_backup_n_exporter.h | 12 ++ 11 files changed, 464 insertions(+), 12 deletions(-) create mode 100644 interfaces/kits/ani/backup/include/backup_session.h create mode 100644 interfaces/kits/ani/backup/include/backup_session_transfer.h create mode 100644 interfaces/kits/ani/backup/src/backup_session.cpp create mode 100644 interfaces/kits/ani/backup/src/backup_session_transfer.cpp diff --git a/interfaces/kits/ani/backup/BUILD.gn b/interfaces/kits/ani/backup/BUILD.gn index 6193628f4..d554f928e 100644 --- a/interfaces/kits/ani/backup/BUILD.gn +++ b/interfaces/kits/ani/backup/BUILD.gn @@ -36,6 +36,8 @@ ohos_shared_library("ani_session_transfer") { "../../../common/src/common_func.cpp", "../../../common/src/sandbox_helper.cpp", "../../../common/src/json_utils.cpp", + "src/backup_session_transfer.cpp", + "src/backup_session.cpp", "src/incr_backup_session_transfer.cpp", "src/incremental_backup_session.cpp", "src/ani_constructor.cpp", diff --git a/interfaces/kits/ani/backup/ets/@ohos.backup.transfer.ets b/interfaces/kits/ani/backup/ets/@ohos.backup.transfer.ets index 59613abae..0ff303f23 100644 --- a/interfaces/kits/ani/backup/ets/@ohos.backup.transfer.ets +++ b/interfaces/kits/ani/backup/ets/@ohos.backup.transfer.ets @@ -30,6 +30,19 @@ export namespace backup { native clean(): void } + class BackupSessionCleaner implements Cleaner { + static { + loadLibrary("ani_session_transfer"); + } + private session: long = 0 + private callbacks: long = 0 + constructor(session: long, callbacks: long) { + this.session = session; + this.callbacks = callbacks; + } + native clean(): void + } + export function callback(cleaner: Cleaner): void { cleaner.clean(); } @@ -44,6 +57,35 @@ export namespace backup { public backupPriority: int = 0; } + class BackupSession { + public session: long = 0; + public callbacks: long = 0; + private cleaner: BackupSessionCleaner | null = null; + constructor(session: long, callbacks: long) { + this.session = session; + this.callbacks = callbacks; + this.registerCleaner(); + } + registerCleaner(): void { + this.cleaner = new BackupSessionCleaner(this.session, this.callbacks); + destroyRegister.register(this, this.cleaner!, unregisterToken); + } + } + + class BackupSessionTransfer { + static { + loadLibrary("ani_session_transfer"); + } + private static native transferStaticSession(input: ESValue): Object; + private static native transferDynamicSession(input: Object): ESValue; + static transferStatic(input: Any): Object { // 1.1(dynamic) => 1.2(static) + return BackupSessionTransfer.transferStaticSession(ESValue.wrap(input)); + } + static transferDynamic(input: Object): Any { // 1.2 => 1.1 + return BackupSessionTransfer.transferDynamicSession(input); + } + } + class IncrementalBackupSession { static { loadLibrary("ani_session_transfer"); diff --git a/interfaces/kits/ani/backup/include/backup_session.h b/interfaces/kits/ani/backup/include/backup_session.h new file mode 100644 index 000000000..84b87d981 --- /dev/null +++ b/interfaces/kits/ani/backup/include/backup_session.h @@ -0,0 +1,44 @@ +/* + * 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 BACKUP_SESSION__H +#define BACKUP_SESSION__H + +#include +#include +#include "b_session_backup.h" +#include "general_callbacks.h" +#include "native_engine/native_engine.h" + +namespace OHOS::FileManagement::Backup { +class BackupSession { +public: + ~BackupSession() = default; + BackupSession(const BackupSession&) = delete; + BackupSession(BackupSession&&) = delete; + BackupSession& operator=(const BackupSession&) = delete; + BackupSession& operator=(BackupSession&&) = delete; + static void Init(ani_env *aniEnv); + + std::unique_ptr session; + std::shared_ptr callbacks; +}; + +class BackupSessionCleaner { +public: + static void Clean(ani_env *aniEnv, ani_object object); +}; +} // namespace OHOS::FileManagement::Backup +#endif // BACKUP_SESSION__H \ No newline at end of file diff --git a/interfaces/kits/ani/backup/include/backup_session_transfer.h b/interfaces/kits/ani/backup/include/backup_session_transfer.h new file mode 100644 index 000000000..13980acc7 --- /dev/null +++ b/interfaces/kits/ani/backup/include/backup_session_transfer.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 BACKUP_SESSION_TRANSFER_H +#define BACKUP_SESSION_TRANSFER_H + +#include +#include +#include "native_engine/native_engine.h" + +namespace OHOS::FileManagement::Backup { +class BackupSessionTransfer { +public: + BackupSessionTransfer() = default; + ~BackupSessionTransfer() = default; + + BackupSessionTransfer(const BackupSessionTransfer&) = delete; + BackupSessionTransfer(BackupSessionTransfer&&) = delete; + BackupSessionTransfer& operator=(const BackupSessionTransfer&) = delete; + BackupSessionTransfer& operator=(BackupSessionTransfer&&) = delete; + + static void Init(ani_env *aniEnv); + static ani_object TransferStaticSession(ani_env *aniEnv, ani_class aniCls, ani_object input); + static ani_ref TransferDynamicSession(ani_env *aniEnv, ani_class aniCls, ani_object input); +}; +} // namespace OHOS::FileManagement::Backup +#endif // BACKUP_SESSION_TRANSFER_H \ No newline at end of file diff --git a/interfaces/kits/ani/backup/src/ani_constructor.cpp b/interfaces/kits/ani/backup/src/ani_constructor.cpp index bda7c8957..1648bcf00 100644 --- a/interfaces/kits/ani/backup/src/ani_constructor.cpp +++ b/interfaces/kits/ani/backup/src/ani_constructor.cpp @@ -14,8 +14,10 @@ */ #include -#include "incr_backup_session_transfer.h" +#include "backup_session.h" +#include "backup_session_transfer.h" #include "incremental_backup_session.h" +#include "incr_backup_session_transfer.h" namespace OHOS::FileManagement::Backup { extern "C" { @@ -27,6 +29,8 @@ extern "C" { } IncreBackupSessionTransfer::Init(env); IncrementalBackupSession::Init(env); + BackupSession::Init(env); + BackupSessionTransfer::Init(env); *result = ANI_VERSION_1; return ANI_OK; } diff --git a/interfaces/kits/ani/backup/src/backup_session.cpp b/interfaces/kits/ani/backup/src/backup_session.cpp new file mode 100644 index 000000000..f70bca41a --- /dev/null +++ b/interfaces/kits/ani/backup/src/backup_session.cpp @@ -0,0 +1,80 @@ +/* + * 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 "backup_session.h" +#include +#include +#include +#include +#include "ani_utils.h" +#include "b_sa/b_sa_utils.h" +#include "filemgmt_libhilog.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "native_engine/native_engine.h" + +namespace OHOS::FileManagement::Backup { +using namespace LibN; + +namespace { +const char *BACKUP_SESSION_CLEANER_CLASS_NAME = "L@ohos/backup/transfer/backup/BackupSessionCleaner;"; +constexpr int32_t E_OK = 0; +} + +void BackupSession::Init(ani_env *aniEnv) +{ + HILOGD("Init BackupSession begin"); + if (aniEnv == nullptr) { + HILOGE("aniEnv is null"); + return; + } + ani_class clsCleaner = AniUtils::GetAniClsByName(aniEnv, BACKUP_SESSION_CLEANER_CLASS_NAME); + if (clsCleaner == nullptr) { + return; + } + std::array cleanNativeFuncs = { + ani_native_function {"clean", ":V", reinterpret_cast(BackupSessionCleaner::Clean)}, + }; + auto status = aniEnv->Class_BindNativeMethods(clsCleaner, cleanNativeFuncs.data(), cleanNativeFuncs.size()); + if (status != ANI_OK) { + HILOGE("Class_BindNativeMethods failed status: %{public}d", status); + return; + } + HILOGD("Init BackupSession end"); +} + +void BackupSessionCleaner::Clean(ani_env *aniEnv, ani_object object) +{ + ani_status ret = ANI_ERROR; + ani_long session {}; + if ((ret = aniEnv->Object_GetPropertyByName_Long(object, "session", &session)) != ANI_OK) { + HILOGE("get field session failed, ret:%{public}d", ret); + } else { + BackupSession* sessionPtr = reinterpret_cast(session); + if (sessionPtr != nullptr) { + delete sessionPtr; + } + } + ani_long callbacks {}; + if ((ret = aniEnv->Object_GetPropertyByName_Long(object, "callbacks", &callbacks)) != ANI_OK) { + HILOGE("get field callbacks failed, ret:%{public}d", ret); + } else { + GeneralCallbacks* callbackPtr = reinterpret_cast(callbacks); + if (callbackPtr != nullptr) { + delete callbackPtr; + } + } +} +} \ No newline at end of file diff --git a/interfaces/kits/ani/backup/src/backup_session_transfer.cpp b/interfaces/kits/ani/backup/src/backup_session_transfer.cpp new file mode 100644 index 000000000..ed55f4a4e --- /dev/null +++ b/interfaces/kits/ani/backup/src/backup_session_transfer.cpp @@ -0,0 +1,146 @@ +/* + * 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 "backup_session_transfer.h" + +#include +#include +#include +#include +#include +#include "interop_js/arkts_interop_js_api.h" +#include "interop_js/arkts_esvalue.h" +#include "filemgmt_libhilog.h" +#include "ani_utils.h" +#include "backup_session.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "native_engine/native_engine.h" +#include "session_backup_n_exporter.h" + +namespace OHOS::FileManagement::Backup { +namespace { +const char *BACKUP_SESSION_TRANSFER_CLASS_NAME = "L@ohos/backup/transfer/backup/BackupSessionTransfer;"; +const char *BACKUP_SESSION_CLASS_NAME = "L@ohos/backup/transfer/backup/BackupSession;"; +} + +void BackupSessionTransfer::Init(ani_env *aniEnv) +{ + HILOGD("Init transfer native method begin"); + if (aniEnv == nullptr) { + HILOGE("aniEnv is null"); + return; + } + ani_class cls = AniUtils::GetAniClsByName(aniEnv, BACKUP_SESSION_TRANSFER_CLASS_NAME); + if (cls == nullptr) { + return; + } + std::array nativeFuncs = { + ani_native_function { "transferStaticSession", nullptr, + reinterpret_cast(BackupSessionTransfer::TransferStaticSession)}, + ani_native_function { "transferDynamicSession", nullptr, + reinterpret_cast(BackupSessionTransfer::TransferDynamicSession)}, + }; + status = aniEnv->Class_BindStaticNativeMethods(cls, nativeFuncs.data(), nativeFuncs.size()); + if (status != ANI_OK) { + HILOGE("Class_BindNativeMethods failed status: %{public}d", status); + return; + } + HILOGD("Init transfer native method end"); +} + +ani_object BackupSessionTransfer::TransferStaticSession(ani_env *aniEnv, ani_class aniCls, ani_object input) +{ + HILOGD("Transfer BackupSession Static begin"); + if (aniEnv == nullptr) { + HILOGE("aniEnv is null"); + return nullptr; + } + + void *unwrapResult = nullptr; + bool unwrapRet = arkts_esvalue_unwrap(aniEnv, input, &unwrapResult); + if (!unwrapRet) { + HILOGE("Failed to unwrap"); + return nullptr; + } + if (unwrapResult == nullptr) { + HILOGE("UnwrapResult is nullptr"); + return nullptr; + } + BackupSession* entity = reinterpret_cast(unwrapResult); + ani_class cls = AniUtils::GetAniClsByName(aniEnv, BACKUP_SESSION_CLASS_NAME); + if (cls == nullptr) { + HILOGE("Call FindClass failed"); + return nullptr; + } + ani_method constructFunc; + ani_status ret = ANI_ERROR; + if ((ret = aniEnv->Class_FindMethod(cls, "", nullptr, &constructFunc)) != ANI_OK) { + HILOGE("Call Class_FindMethod failed, ret = %{public}d", ret); + return nullptr; + } + ani_object outObj; + ret = aniEnv->Object_New(cls, constructFunc, &outObj, entity->session.release(), entity->callbacks.get()); + if (ret != ANI_OK) { + HILOGE("Call Object_New failed, ret = %{public}d", ret); + return nullptr; + } + HILOGD("Transfer BackupSession Static end"); + return outObj; +} + +ani_ref BackupSessionTransfer::TransferDynamicSession(ani_env *aniEnv, ani_class aniCls, ani_object input) +{ + HILOGD("TransferDynamicSession start"); + if (aniEnv == nullptr) { + HILOGE("aniEnv is null"); + return nullptr; + } + // 1.2->1.1 + ani_status ret = ANI_ERROR; + ani_long session {}; + if ((ret = aniEnv->Object_GetPropertyByName_Long(input, "session", &session)) != ANI_OK) { + HILOGE("get field session failed, ret:%{public}d", ret); + return nullptr; + } + ani_long callbacks {}; + if ((ret = aniEnv->Object_GetPropertyByName_Long(input, "callbacks", &callbacks)) != ANI_OK) { + HILOGE("get field callbacks failed, ret:%{public}d", ret); + return nullptr; + } + std::unique_ptr entity = std::make_unique(); + std::unique_ptr sessionPtr(reinterpret_cast(session)); + entity->session = move(sessionPtr); + std::shared_ptr callbackPtr(reinterpret_cast(callbacks)); + entity->callbacks = callbackPtr; + napi_env jsEnv; + if (!arkts_napi_scope_open(aniEnv, &jsEnv)) { + HILOGE("Failed to arkts_napi_scope_open"); + return nullptr; + } + napi_value napiEntity = SessionBackupNExporter::CreateByEntity(jsEnv, move(entity)); + if (napiEntity == nullptr) { + HILOGD("Failed to create napi obj"); + return nullptr; + } + ani_ref outObj; + if (!arkts_napi_scope_close_n(jsEnv, 1, &napiEntity, &outObj)) { + HILOGE("Failed to arkts_napi_scope_close_n"); + return nullptr; + } + HILOGD("TransferDynamicSession end"); + return outObj; +} +} \ No newline at end of file diff --git a/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp b/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp index 4aebe1ac7..166cab41d 100644 --- a/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp +++ b/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp @@ -31,7 +31,7 @@ namespace OHOS::FileManagement::Backup { namespace { -const char *INCR_BACKUP_SESSION_TRANSFER_CLASS_NAME = "L@ohos/backup/transfer/backup/IncrBackupSessionTransfer;"; +const char *BACKUP_SESSION_TRANSFER_CLASS_NAME = "L@ohos/backup/transfer/backup/IncrBackupSessionTransfer;"; const char *INCR_BACKUP_SESSION_CLASS_NAME = "L@ohos/backup/transfer/backup/IncrementalBackupSession;"; } @@ -44,7 +44,7 @@ void IncreBackupSessionTransfer::Init(ani_env *aniEnv) } ani_class cls = nullptr; - auto status = aniEnv->FindClass(INCR_BACKUP_SESSION_TRANSFER_CLASS_NAME, &cls); + auto status = aniEnv->FindClass(BACKUP_SESSION_TRANSFER_CLASS_NAME, &cls); if (status != ANI_OK) { HILOGE("FindClass failed status: %{public}d", status); return; diff --git a/interfaces/kits/ani/backup/src/incremental_backup_session.cpp b/interfaces/kits/ani/backup/src/incremental_backup_session.cpp index 0041b7b79..f9225e570 100644 --- a/interfaces/kits/ani/backup/src/incremental_backup_session.cpp +++ b/interfaces/kits/ani/backup/src/incremental_backup_session.cpp @@ -29,7 +29,7 @@ namespace OHOS::FileManagement::Backup { using namespace LibN; namespace { -const char *INCR_BACKUP_SESSION_CLASS_NAME = "L@ohos/backup/transfer/backup/IncrementalBackupSession;"; +const char *BACKUP_SESSION_CLASS_NAME = "L@ohos/backup/transfer/backup/IncrementalBackupSession;"; const char *B_INCREMENTAL_DATA_CLASS_NAME = "L@ohos/backup/transfer/backup/BIncrementalData;"; const char *INCR_BACKUP_SESSION_CLEANER_CLASS_NAME = "L@ohos/backup/transfer/backup/IncrBackupSessionCleaner;"; constexpr int32_t E_OK = 0; @@ -56,7 +56,7 @@ void IncrementalBackupSession::Init(ani_env *aniEnv) return; } - ani_class cls = AniUtils::GetAniClsByName(aniEnv, INCR_BACKUP_SESSION_CLASS_NAME); + ani_class cls = AniUtils::GetAniClsByName(aniEnv, BACKUP_SESSION_CLASS_NAME); if (cls == nullptr) { return; } diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index bcd5846fc..d86dac5ba 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -21,11 +21,9 @@ #include "b_filesystem/b_file.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" -#include "b_session_backup.h" #include "backup_kit_inner.h" #include "directory_ex.h" #include "filemgmt_libhilog.h" -#include "general_callbacks.h" #include "parse_inc_info_from_js.h" #include "service_proxy.h" @@ -33,11 +31,6 @@ namespace OHOS::FileManagement::Backup { using namespace std; using namespace LibN; -struct BackupEntity { - unique_ptr session; - shared_ptr callbacks; -}; - static void OnFileReady(weak_ptr pCallbacks, const BFileInfo &fileInfo, UniqueFd fd, int sysErrno) { auto callbacks = pCallbacks.lock(); @@ -741,6 +734,96 @@ bool SessionBackupNExporter::Export() return exports_.AddProp(className, classValue); } +napi_value SessionBackupNExporter::ConstructorFromEntity(napi_env env, napi_callback_info cbinfo) +{ + HILOGD("called ConstructorFromEntity begin"); + if (!SAUtils::CheckBackupPermission()) { + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return nullptr; + } + void* entityRawPtr = nullptr; + if (napi_ok != napi_get_value_external(env, funcArg[NARG_POS::FIRST], &entityRawPtr)) { + HILOGE("parse entity raw ptr for napi_value fail"); + return nullptr; + } + BackupEntity* entity = reinterpret_cast(entityRawPtr); + if (entity == nullptr) { + NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not session pointer.").GetCode()).ThrowErr(env); + return nullptr; + } + std::unique_ptr backupEntity(entity); + if (backupEntity->session == nullptr || backupEntity->callbacks == nullptr) { + HILOGD("session or callback is null"); + return nullptr; + } + if (!SetSessionBackupEntity(env, funcArg, std::move(backupEntity))) { + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to set IncrBackupEntity entity.").GetCode()).ThrowErr(env); + return nullptr; + } + HILOGD("called ConstructorFromEntity end"); + return funcArg.GetThisVar(); +} + +napi_value SessionBackupNExporter::CreateByEntity(napi_env env, std::unique_ptr entity) +{ + HILOGD("CreateByEntity begin"); + if (entity == nullptr) { + HILOGE("entity is null"); + return nullptr; + } + vector props = { + NVal::DeclareNapiFunction("getLocalCapabilities", GetLocalCapabilities), + NVal::DeclareNapiFunction("getBackupDataSize", GetBackupDataSize), + NVal::DeclareNapiFunction("appendBundles", AppendBundles), + NVal::DeclareNapiFunction("release", Release), + NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), + }; + auto [defRet, constroctor] = NClass::DefineClass(env, napiClassName_, ConstructorFromEntity, + std::move(props)); + if (!defRet) { + HILOGE("Failed to define class"); + return nullptr; + } + napi_value instance; + napi_value napiEntity; + auto finalize = [](napi_env env, void *data, void *hint) { + std::unique_ptr entity(static_cast(data)); + if (entity == nullptr) { + HILOGE("Entity is nullptr"); + return; + } + if (entity->callbacks == nullptr) { + HILOGE("Callbacks is nullptr"); + return; + } + entity->callbacks->RemoveCallbackRef(); + }; + if (napi_ok != napi_create_external(env, (void *)entity.release(), finalize, nullptr, &napiEntity)) { + HILOGE("wrap entity prt fail"); + return nullptr; + } + size_t argc = 1; + napi_value args[1] = { napiEntity }; + + napi_status napiStatus = napi_new_instance(env, constroctor, argc, args, &instance); + if (napi_status::napi_ok != napiStatus) { + HILOGE("Failed to napi_new_instance, status=%{public}d", napiStatus); + return nullptr; + } + HILOGD("CreateByEntity end"); + return instance; +} + string SessionBackupNExporter::GetClassName() { return SessionBackupNExporter::className; diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index 708f3b9b7..9c819beee 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -15,12 +15,21 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_BACKUP_N_EXPORTER_H #define INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_SESSION_BACKUP_N_EXPORTER_H +#include +#include "b_session_backup.h" +#include "general_callbacks.h" #include "n_exporter.h" namespace OHOS::FileManagement::Backup { +struct BackupEntity { + unique_ptr session; + shared_ptr callbacks; +}; + class SessionBackupNExporter final : public LibN::NExporter { public: inline static const std::string className = "SessionBackup"; + inline static const std::string napiClassName_ = "NapiSessionBackup"; bool Export() override; std::string GetClassName() override; @@ -33,6 +42,9 @@ public: static napi_value Cancel(napi_env env, napi_callback_info cbinfo); static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); + static napi_value ConstructorFromEntity(napi_env env, napi_callback_info cbinfo); + static napi_value CreateByEntity(napi_env env, std::unique_ptr entity); + SessionBackupNExporter(napi_env env, napi_value exports); ~SessionBackupNExporter() override; }; -- Gitee From 76d05e669bcd4bf44921532e2d39db5295e96727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Fri, 22 Aug 2025 15:33:16 +0800 Subject: [PATCH 2/3] fix compile bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- interfaces/kits/ani/backup/include/backup_session.h | 6 +++--- interfaces/kits/ani/backup/src/backup_session.cpp | 1 - interfaces/kits/ani/backup/src/backup_session_transfer.cpp | 2 +- interfaces/kits/js/backup/session_backup_n_exporter.cpp | 1 - interfaces/kits/js/backup/session_backup_n_exporter.h | 4 ++-- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/ani/backup/include/backup_session.h b/interfaces/kits/ani/backup/include/backup_session.h index 84b87d981..ae3ff98cf 100644 --- a/interfaces/kits/ani/backup/include/backup_session.h +++ b/interfaces/kits/ani/backup/include/backup_session.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef BACKUP_SESSION__H -#define BACKUP_SESSION__H +#ifndef BACKUP_SESSION_H +#define BACKUP_SESSION_H #include #include @@ -41,4 +41,4 @@ public: static void Clean(ani_env *aniEnv, ani_object object); }; } // namespace OHOS::FileManagement::Backup -#endif // BACKUP_SESSION__H \ No newline at end of file +#endif // BACKUP_SESSION_H \ No newline at end of file diff --git a/interfaces/kits/ani/backup/src/backup_session.cpp b/interfaces/kits/ani/backup/src/backup_session.cpp index f70bca41a..8971dfd13 100644 --- a/interfaces/kits/ani/backup/src/backup_session.cpp +++ b/interfaces/kits/ani/backup/src/backup_session.cpp @@ -30,7 +30,6 @@ using namespace LibN; namespace { const char *BACKUP_SESSION_CLEANER_CLASS_NAME = "L@ohos/backup/transfer/backup/BackupSessionCleaner;"; -constexpr int32_t E_OK = 0; } void BackupSession::Init(ani_env *aniEnv) diff --git a/interfaces/kits/ani/backup/src/backup_session_transfer.cpp b/interfaces/kits/ani/backup/src/backup_session_transfer.cpp index ed55f4a4e..ee89a53b5 100644 --- a/interfaces/kits/ani/backup/src/backup_session_transfer.cpp +++ b/interfaces/kits/ani/backup/src/backup_session_transfer.cpp @@ -53,7 +53,7 @@ void BackupSessionTransfer::Init(ani_env *aniEnv) ani_native_function { "transferDynamicSession", nullptr, reinterpret_cast(BackupSessionTransfer::TransferDynamicSession)}, }; - status = aniEnv->Class_BindStaticNativeMethods(cls, nativeFuncs.data(), nativeFuncs.size()); + int32_t status = aniEnv->Class_BindStaticNativeMethods(cls, nativeFuncs.data(), nativeFuncs.size()); if (status != ANI_OK) { HILOGE("Class_BindNativeMethods failed status: %{public}d", status); return; diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index d86dac5ba..01190fd92 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -15,7 +15,6 @@ #include "session_backup_n_exporter.h" #include -#include #include "b_error/b_error.h" #include "b_filesystem/b_file.h" diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index 9c819beee..d6b28af66 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -22,8 +22,8 @@ namespace OHOS::FileManagement::Backup { struct BackupEntity { - unique_ptr session; - shared_ptr callbacks; + std::unique_ptr session; + std::shared_ptr callbacks; }; class SessionBackupNExporter final : public LibN::NExporter { -- Gitee From 118b2938a26ee619fa59e418c1cfef8bd7ff37a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Mon, 25 Aug 2025 09:51:37 +0800 Subject: [PATCH 3/3] fix review opinion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- interfaces/kits/ani/backup/src/backup_session.cpp | 4 ++++ interfaces/kits/ani/backup/src/backup_session_transfer.cpp | 2 +- .../kits/ani/backup/src/incr_backup_session_transfer.cpp | 2 +- interfaces/kits/ani/backup/src/incremental_backup_session.cpp | 4 ++++ interfaces/kits/js/backup/session_backup_n_exporter.cpp | 2 +- .../kits/js/backup/session_incremental_backup_n_exporter.cpp | 2 +- 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/ani/backup/src/backup_session.cpp b/interfaces/kits/ani/backup/src/backup_session.cpp index 8971dfd13..7ef5ab86a 100644 --- a/interfaces/kits/ani/backup/src/backup_session.cpp +++ b/interfaces/kits/ani/backup/src/backup_session.cpp @@ -56,6 +56,10 @@ void BackupSession::Init(ani_env *aniEnv) void BackupSessionCleaner::Clean(ani_env *aniEnv, ani_object object) { + if (aniEnv == nullptr) { + HILOGE("aniEnv is null"); + return; + } ani_status ret = ANI_ERROR; ani_long session {}; if ((ret = aniEnv->Object_GetPropertyByName_Long(object, "session", &session)) != ANI_OK) { diff --git a/interfaces/kits/ani/backup/src/backup_session_transfer.cpp b/interfaces/kits/ani/backup/src/backup_session_transfer.cpp index ee89a53b5..f05ae10de 100644 --- a/interfaces/kits/ani/backup/src/backup_session_transfer.cpp +++ b/interfaces/kits/ani/backup/src/backup_session_transfer.cpp @@ -132,7 +132,7 @@ ani_ref BackupSessionTransfer::TransferDynamicSession(ani_env *aniEnv, ani_class } napi_value napiEntity = SessionBackupNExporter::CreateByEntity(jsEnv, move(entity)); if (napiEntity == nullptr) { - HILOGD("Failed to create napi obj"); + HILOGE("Failed to create napi obj"); return nullptr; } ani_ref outObj; diff --git a/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp b/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp index 166cab41d..5112f269d 100644 --- a/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp +++ b/interfaces/kits/ani/backup/src/incr_backup_session_transfer.cpp @@ -137,7 +137,7 @@ ani_ref IncreBackupSessionTransfer::TransferDynamicSession(ani_env *aniEnv, ani_ } napi_value napiEntity = SessionIncrementalBackupNExporter::CreateByEntity(jsEnv, entity); if (napiEntity == nullptr) { - HILOGD("Failed to create napi obj"); + HILOGE("Failed to create napi obj"); delete entity; return nullptr; } diff --git a/interfaces/kits/ani/backup/src/incremental_backup_session.cpp b/interfaces/kits/ani/backup/src/incremental_backup_session.cpp index f9225e570..eae27f285 100644 --- a/interfaces/kits/ani/backup/src/incremental_backup_session.cpp +++ b/interfaces/kits/ani/backup/src/incremental_backup_session.cpp @@ -299,6 +299,10 @@ ani_int IncrementalBackupSession::CleanBundleTempDir(ani_env *aniEnv, ani_object void IncrBackupSessionCleaner::Clean(ani_env *aniEnv, ani_object object) { + if (aniEnv == nullptr) { + HILOGE("aniEnv is null"); + return; + } ani_status ret = ANI_ERROR; ani_long session {}; if ((ret = aniEnv->Object_GetPropertyByName_Long(object, "session", &session)) != ANI_OK) { diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index 01190fd92..c1de836aa 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -761,7 +761,7 @@ napi_value SessionBackupNExporter::ConstructorFromEntity(napi_env env, napi_call } std::unique_ptr backupEntity(entity); if (backupEntity->session == nullptr || backupEntity->callbacks == nullptr) { - HILOGD("session or callback is null"); + HILOGE("session or callback is null"); return nullptr; } if (!SetSessionBackupEntity(env, funcArg, std::move(backupEntity))) { diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index 3c525df4d..a0c496810 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -764,7 +764,7 @@ napi_value SessionIncrementalBackupNExporter::ConstructorFromEntity(napi_env env } std::unique_ptr backupEntity(entity); if (backupEntity->session == nullptr || backupEntity->callbacks == nullptr) { - HILOGD("session or callback is null"); + HILOGE("session or callback is null"); return nullptr; } if (!SetIncrementalBackupEntity(env, funcArg, std::move(backupEntity))) { -- Gitee